lottie: reuse path for reducing constructor/destructor calls from getPath()/toPath()

It helps to improve performance by avoiding constructor/destructor calls.
The getPath function is renamed to updatePath function.
And it does not return VPath anymore.

Change-Id: I6e6cc52ec3f1560aac2ee0633cdf5a8c224dcc6e
This commit is contained in:
Youngbok Shin 2018-08-01 20:15:15 +09:00 committed by Subhransu Mohanty
parent 4b823247bd
commit b9fe04a251
3 changed files with 20 additions and 27 deletions

View File

@ -172,10 +172,10 @@ void LOTMaskItem::update(int frameNo, const VMatrix &parentMatrix,
{ {
if (mData->mShape.isStatic()) { if (mData->mShape.isStatic()) {
if (mLocalPath.isEmpty()) { if (mLocalPath.isEmpty()) {
mLocalPath = mData->mShape.value(frameNo).toPath(); mData->mShape.value(frameNo).toPath(mLocalPath);
} }
} else { } else {
mLocalPath = mData->mShape.value(frameNo).toPath(); mData->mShape.value(frameNo).toPath(mLocalPath);
} }
float opacity = mData->opacity(frameNo); float opacity = mData->opacity(frameNo);
opacity = opacity * parentAlpha; opacity = opacity * parentAlpha;
@ -670,7 +670,7 @@ void LOTPathDataItem::update(int frameNo, const VMatrix &parentMatrix,
// 1. update the local path if needed // 1. update the local path if needed
if (!(mInit && mStaticPath)) { if (!(mInit && mStaticPath)) {
mLocalPath = getPath(frameNo); updatePath(mLocalPath, frameNo);
mInit = true; mInit = true;
mPathChanged = true; mPathChanged = true;
} }
@ -726,7 +726,7 @@ LOTRectItem::LOTRectItem(LOTRectData *data)
{ {
} }
VPath LOTRectItem::getPath(int frameNo) void LOTRectItem::updatePath(VPath& path, int frameNo)
{ {
VPointF pos = mData->mPos.value(frameNo); VPointF pos = mData->mPos.value(frameNo);
VPointF size = mData->mSize.value(frameNo); VPointF size = mData->mSize.value(frameNo);
@ -734,10 +734,8 @@ VPath LOTRectItem::getPath(int frameNo)
VRectF r(pos.x() - size.x() / 2, pos.y() - size.y() / 2, size.x(), VRectF r(pos.x() - size.x() / 2, pos.y() - size.y() / 2, size.x(),
size.y()); size.y());
VPath path; path.reset();
path.addRoundRect(r, radius, radius, mData->direction()); path.addRoundRect(r, radius, radius, mData->direction());
return path;
} }
LOTEllipseItem::LOTEllipseItem(LOTEllipseData *data) LOTEllipseItem::LOTEllipseItem(LOTEllipseData *data)
@ -745,17 +743,15 @@ LOTEllipseItem::LOTEllipseItem(LOTEllipseData *data)
{ {
} }
VPath LOTEllipseItem::getPath(int frameNo) void LOTEllipseItem::updatePath(VPath& path, int frameNo)
{ {
VPointF pos = mData->mPos.value(frameNo); VPointF pos = mData->mPos.value(frameNo);
VPointF size = mData->mSize.value(frameNo); VPointF size = mData->mSize.value(frameNo);
VRectF r(pos.x() - size.x() / 2, pos.y() - size.y() / 2, size.x(), VRectF r(pos.x() - size.x() / 2, pos.y() - size.y() / 2, size.x(),
size.y()); size.y());
VPath path; path.reset();
path.addOval(r, mData->direction()); path.addOval(r, mData->direction());
return path;
} }
LOTShapeItem::LOTShapeItem(LOTShapeData *data) LOTShapeItem::LOTShapeItem(LOTShapeData *data)
@ -763,9 +759,9 @@ LOTShapeItem::LOTShapeItem(LOTShapeData *data)
{ {
} }
VPath LOTShapeItem::getPath(int frameNo) void LOTShapeItem::updatePath(VPath& path, int frameNo)
{ {
return mData->mShape.value(frameNo).toPath(); mData->mShape.value(frameNo).toPath(path);
} }
LOTPolystarItem::LOTPolystarItem(LOTPolystarData *data) LOTPolystarItem::LOTPolystarItem(LOTPolystarData *data)
@ -773,7 +769,7 @@ LOTPolystarItem::LOTPolystarItem(LOTPolystarData *data)
{ {
} }
VPath LOTPolystarItem::getPath(int frameNo) void LOTPolystarItem::updatePath(VPath& path, int frameNo)
{ {
VPointF pos = mData->mPos.value(frameNo); VPointF pos = mData->mPos.value(frameNo);
float points = mData->mPointCount.value(frameNo); float points = mData->mPointCount.value(frameNo);
@ -783,7 +779,7 @@ VPath LOTPolystarItem::getPath(int frameNo)
float outerRoundness = mData->mOuterRoundness.value(frameNo); float outerRoundness = mData->mOuterRoundness.value(frameNo);
float rotation = mData->mRotation.value(frameNo); float rotation = mData->mRotation.value(frameNo);
VPath path; path.reset();
VMatrix m; VMatrix m;
if (mData->mType == LOTPolystarData::PolyType::Star) { if (mData->mType == LOTPolystarData::PolyType::Star) {
@ -797,8 +793,6 @@ VPath LOTPolystarItem::getPath(int frameNo)
m.translate(pos.x(), pos.y()).rotate(rotation); m.translate(pos.x(), pos.y()).rotate(rotation);
m.rotate(rotation); m.rotate(rotation);
path.transform(m); path.transform(m);
return path;
} }
/* /*

View File

@ -225,7 +225,7 @@ private:
bool mPathChanged; bool mPathChanged;
float mCombinedAlpha; float mCombinedAlpha;
protected: protected:
virtual VPath getPath(int frameNo) = 0; virtual void updatePath(VPath& path, int frameNo) = 0;
}; };
class LOTRectItem: public LOTPathDataItem class LOTRectItem: public LOTPathDataItem
@ -233,7 +233,7 @@ class LOTRectItem: public LOTPathDataItem
public: public:
LOTRectItem(LOTRectData *data); LOTRectItem(LOTRectData *data);
protected: protected:
VPath getPath(int frameNo) final; void updatePath(VPath& path, int frameNo) final;
LOTRectData *mData; LOTRectData *mData;
}; };
@ -242,7 +242,7 @@ class LOTEllipseItem: public LOTPathDataItem
public: public:
LOTEllipseItem(LOTEllipseData *data); LOTEllipseItem(LOTEllipseData *data);
private: private:
VPath getPath(int frameNo) final; void updatePath(VPath& path, int frameNo) final;
LOTEllipseData *mData; LOTEllipseData *mData;
}; };
@ -251,7 +251,7 @@ class LOTShapeItem: public LOTPathDataItem
public: public:
LOTShapeItem(LOTShapeData *data); LOTShapeItem(LOTShapeData *data);
private: private:
VPath getPath(int frameNo) final; void updatePath(VPath& path, int frameNo) final;
LOTShapeData *mData; LOTShapeData *mData;
}; };
@ -260,7 +260,7 @@ class LOTPolystarItem: public LOTPathDataItem
public: public:
LOTPolystarItem(LOTPolystarData *data); LOTPolystarItem(LOTPolystarData *data);
private: private:
VPath getPath(int frameNo) final; void updatePath(VPath& path, int frameNo) final;
LOTPolystarData *mData; LOTPolystarData *mData;
}; };

View File

@ -106,10 +106,11 @@ public:
void reserve(int size) { void reserve(int size) {
mPoints.reserve(mPoints.size() + size); mPoints.reserve(mPoints.size() + size);
} }
VPath toPath() const{ void toPath(VPath& path) {
if (mPoints.empty()) return VPath(); path.reset();
if (mPoints.empty()) return;
VPath path;
int size = mPoints.size(); int size = mPoints.size();
const VPointF *points = mPoints.data(); const VPointF *points = mPoints.data();
/* reserve exact memory requirement at once /* reserve exact memory requirement at once
@ -123,8 +124,6 @@ public:
} }
if (mClosed) if (mClosed)
path.close(); path.close();
return path;
} }
public: public:
std::vector<VPointF> mPoints; std::vector<VPointF> mPoints;