From 15b256d1bf2e7282ea0d629bc622d4d8b7a5028f Mon Sep 17 00:00:00 2001 From: "sub.mohanty@samsung.com" Date: Fri, 16 Nov 2018 22:13:33 +0900 Subject: [PATCH] lottie: optimize VPath::length() api implementation. Change-Id: Ieb32e5e6103261fadbf4287d4e6f82353414caac --- src/vector/vpath.cpp | 20 ++++++++++++++++---- src/vector/vpath.h | 2 ++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/vector/vpath.cpp b/src/vector/vpath.cpp index 0f246d9..1830922 100644 --- a/src/vector/vpath.cpp +++ b/src/vector/vpath.cpp @@ -13,11 +13,16 @@ void VPath::VPathData::transform(const VMatrix &m) for (auto &i : m_points) { i = m.map(i); } + mLengthDirty = true; } float VPath::VPathData::length() const { - float len = 0.0; + if (!mLengthDirty) return mLength; + + mLengthDirty = false; + mLength = 0.0; + int i = 0; for (auto e : m_elements) { switch (e) { @@ -25,12 +30,12 @@ float VPath::VPathData::length() const i++; break; case VPath::Element::LineTo: { - len += VLine( m_points[i-1], m_points[i]).length(); + mLength += VLine( m_points[i-1], m_points[i]).length(); i++; break; } case VPath::Element::CubicTo: { - len += VBezier::fromPoints(m_points[i-1], m_points[i], + mLength += VBezier::fromPoints(m_points[i-1], m_points[i], m_points[i+1], m_points[i+2]).length(); i += 3; break; @@ -40,7 +45,7 @@ float VPath::VPathData::length() const } } - return len; + return mLength; } void VPath::VPathData::checkNewSegment() @@ -58,6 +63,7 @@ void VPath::VPathData::moveTo(float x, float y) m_elements.emplace_back(VPath::Element::MoveTo); m_points.emplace_back(x,y); m_segments++; + mLengthDirty = true; } void VPath::VPathData::lineTo(float x, float y) @@ -65,6 +71,7 @@ void VPath::VPathData::lineTo(float x, float y) checkNewSegment(); m_elements.emplace_back(VPath::Element::LineTo); m_points.emplace_back(x,y); + mLengthDirty = true; } void VPath::VPathData::cubicTo(float cx1, float cy1, float cx2, float cy2, @@ -75,6 +82,7 @@ void VPath::VPathData::cubicTo(float cx1, float cy1, float cx2, float cy2, m_points.emplace_back(cx1, cy1); m_points.emplace_back(cx2, cy2); m_points.emplace_back(ex, ey); + mLengthDirty = true; } void VPath::VPathData::close() @@ -87,6 +95,7 @@ void VPath::VPathData::close() } m_elements.push_back(VPath::Element::Close); mNewSegment = true; + mLengthDirty = true; } void VPath::VPathData::reset() @@ -96,6 +105,8 @@ void VPath::VPathData::reset() m_elements.clear(); m_points.clear(); m_segments = 0; + mLength = 0; + mLengthDirty = false; } int VPath::VPathData::segments() const @@ -672,6 +683,7 @@ void VPath::VPathData::addPath(const VPathData &path) std::copy(path.m_elements.begin(), path.m_elements.end(), back_inserter(m_elements)); m_segments += segment; + mLengthDirty = true; } V_END_NAMESPACE diff --git a/src/vector/vpath.h b/src/vector/vpath.h index 6a22225..bc74316 100644 --- a/src/vector/vpath.h +++ b/src/vector/vpath.h @@ -86,6 +86,8 @@ private: std::vector m_elements; int m_segments; VPointF mStartPoint; + mutable float mLength{0}; + mutable bool mLengthDirty{true}; bool mNewSegment; }; -- 2.7.4