summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsubhransu mohanty <sub.mohanty@samsung.com>2017-12-19 13:23:57 +0900
committersubhransu mohanty <sub.mohanty@samsung.com>2018-04-12 13:51:52 +0900
commitc4c8bdf944b0ed200c85aced3da4c29f241a2fe8 (patch)
treeaf4bf4297a2f72c88fab84aaf4c4f4ae965a672b
parent29958de5a82cced0d80fe99b2ac7171df70d7ed8 (diff)
lottie: use shared_ptr for memory footprint optimization
-rw-r--r--ssg/src/lottie/lottiemodel.cpp22
-rw-r--r--ssg/src/lottie/lottiemodel.h43
-rw-r--r--ssg/src/lottie/lottieparser.cpp23
3 files changed, 67 insertions, 21 deletions
diff --git a/ssg/src/lottie/lottiemodel.cpp b/ssg/src/lottie/lottiemodel.cpp
index 7c98f3e124..0cd113ba30 100644
--- a/ssg/src/lottie/lottiemodel.cpp
+++ b/ssg/src/lottie/lottiemodel.cpp
@@ -1,6 +1,17 @@
1#include "lottiemodel.h" 1#include "lottiemodel.h"
2 2
3/* 3/*
4 * makes a deep copy of the object as well as all its children
5 *
6 */
7LottieGroupObject::LottieGroupObject(const LottieGroupObject &other):LottieObject(other.mType)
8{
9 for(auto child: other.mChildren) {
10 mChildren.push_back(child->copy());
11 }
12}
13
14/*
4 * Convert the AE shape format to 15 * Convert the AE shape format to
5 * list of bazier curves 16 * list of bazier curves
6 */ 17 */
@@ -9,7 +20,7 @@ void LottieShapeObject::process()
9 if (mShape.isStatic()) { 20 if (mShape.isStatic()) {
10 mShape.mValue.process(mClosed); 21 mShape.mValue.process(mClosed);
11 } else { 22 } else {
12 for (auto &keyframe : mShape.mAnimInfo->mKeyFrames) { 23 for (auto &keyframe : mShape.mAnimInfo.get()->mKeyFrames) {
13 keyframe.mStartValue.process(mClosed); 24 keyframe.mStartValue.process(mClosed);
14 keyframe.mEndValue.process(mClosed); 25 keyframe.mEndValue.process(mClosed);
15 } 26 }
@@ -21,7 +32,7 @@ void LottieShapeObject::process()
21 * list of bazier curves 32 * list of bazier curves
22 * The final structure will be M+size*C 33 * The final structure will be M+size*C
23 */ 34 */
24void LottieShape::process(bool closed) 35void LottieShapeData::process(bool closed)
25{ 36{
26 if (mInPoint.size() != mOutPoint.size() || 37 if (mInPoint.size() != mOutPoint.size() ||
27 mInPoint.size() != mVertices.size()) { 38 mInPoint.size() != mVertices.size()) {
@@ -50,3 +61,10 @@ void LottieShape::process(bool closed)
50 mOutPoint = std::vector<SGPointF>(); 61 mOutPoint = std::vector<SGPointF>();
51 mVertices = std::vector<SGPointF>(); 62 mVertices = std::vector<SGPointF>();
52} 63}
64
65void LottieShape::process(bool closed)
66{
67 if (mShapeData)
68 mShapeData.get()->process(closed);
69}
70
diff --git a/ssg/src/lottie/lottiemodel.h b/ssg/src/lottie/lottiemodel.h
index a0d1fc99b5..f9893703d5 100644
--- a/ssg/src/lottie/lottiemodel.h
+++ b/ssg/src/lottie/lottiemodel.h
@@ -2,6 +2,7 @@
2#define LOTTIEMODEL_H 2#define LOTTIEMODEL_H
3 3
4#include<vector> 4#include<vector>
5#include<memory>
5#include<unordered_map> 6#include<unordered_map>
6#include"sgpoint.h" 7#include"sgpoint.h"
7#include"sgrect.h" 8#include"sgrect.h"
@@ -70,7 +71,7 @@ public:
70 T mEndValue; 71 T mEndValue;
71 int mStartFrame; 72 int mStartFrame;
72 int mEndFrame; 73 int mEndFrame;
73 LottieInterpolater *mInterpolator; 74 std::shared_ptr<LottieInterpolater> mInterpolator;
74 75
75 /* this is for interpolating position along a path 76 /* this is for interpolating position along a path
76 * Need to move to other place because its only applicable 77 * Need to move to other place because its only applicable
@@ -93,12 +94,12 @@ class LottieAnimatable
93{ 94{
94public: 95public:
95 LottieAnimatable():mValue(),mAnimInfo(nullptr){} 96 LottieAnimatable():mValue(),mAnimInfo(nullptr){}
96 LottieAnimatable(const T &value): mValue(value), mAnimInfo(nullptr){} 97 LottieAnimatable(const T &value): mValue(value){}
97 constexpr bool isStatic() const {return ((mAnimInfo==nullptr) ? true : false);} 98 constexpr bool isStatic() const {if (mAnimInfo) return false; else return true;}
98public: 99public:
99 T mValue; 100 T mValue;
100 LottieAnimInfo<T> *mAnimInfo; 101 int mPropertyIndex; /* "ix" */
101 int mPropertyIndex; /* "ix" */ 102 std::shared_ptr<LottieAnimInfo<T>> mAnimInfo;
102}; 103};
103 104
104enum class LottieBlendMode 105enum class LottieBlendMode
@@ -133,6 +134,7 @@ public:
133 virtual ~LottieObject(){} 134 virtual ~LottieObject(){}
134 LottieObject(LottieObject::Type type): mType(type){} 135 LottieObject(LottieObject::Type type): mType(type){}
135 bool isStatic(); 136 bool isStatic();
137 virtual LottieObject *copy() = 0;
136public: 138public:
137 LottieObject::Type mType; 139 LottieObject::Type mType;
138}; 140};
@@ -141,6 +143,11 @@ class LottieGroupObject: public LottieObject
141{ 143{
142public: 144public:
143 LottieGroupObject(LottieObject::Type type):LottieObject(type){} 145 LottieGroupObject(LottieObject::Type type):LottieObject(type){}
146 LottieGroupObject(const LottieGroupObject &other);
147 ~LottieGroupObject() {
148 for(auto child : mChildren)
149 delete child;
150 }
144public: 151public:
145 std::vector<LottieObject *> mChildren; 152 std::vector<LottieObject *> mChildren;
146}; 153};
@@ -152,6 +159,7 @@ public:
152 {visitor->visit(this); visitor->visitChildren(this);} 159 {visitor->visit(this); visitor->visitChildren(this);}
153 160
154 LottieShapeGroup():LottieGroupObject(LottieObject::Type::ShapeGroup){} 161 LottieShapeGroup():LottieGroupObject(LottieObject::Type::ShapeGroup){}
162 LottieObject *copy() {return new LottieShapeGroup(*this);}
155}; 163};
156 164
157class LottieTransform; 165class LottieTransform;
@@ -161,6 +169,7 @@ public:
161 void accept(LottieObjectVisitor *visitor) override 169 void accept(LottieObjectVisitor *visitor) override
162 {visitor->visit(this); visitor->visitChildren(this);} 170 {visitor->visit(this); visitor->visitChildren(this);}
163 LottieComposition():LottieGroupObject(LottieObject::Type::Composition){} 171 LottieComposition():LottieGroupObject(LottieObject::Type::Composition){}
172 LottieObject *copy() {return new LottieComposition(*this);}
164public: 173public:
165 SGRect mBound; 174 SGRect mBound;
166 bool mAnimation = false; 175 bool mAnimation = false;
@@ -170,7 +179,8 @@ public:
170 long mStartTime; 179 long mStartTime;
171 LottieBlendMode mBlendMode; 180 LottieBlendMode mBlendMode;
172 float mTimeStreatch; 181 float mTimeStreatch;
173 std::unordered_map<std::string, LottieInterpolater*> mInterpolatorCache; 182 std::unordered_map<std::string,
183 std::shared_ptr<LottieInterpolater>> mInterpolatorCache;
174}; 184};
175 185
176class LottieLayer : public LottieGroupObject 186class LottieLayer : public LottieGroupObject
@@ -179,7 +189,7 @@ public:
179 void accept(LottieObjectVisitor *visitor) override 189 void accept(LottieObjectVisitor *visitor) override
180 {visitor->visit(this); visitor->visitChildren(this);} 190 {visitor->visit(this); visitor->visitChildren(this);}
181 LottieLayer():LottieGroupObject(LottieObject::Type::Layer),mParentId(-1),mId(-1){} 191 LottieLayer():LottieGroupObject(LottieObject::Type::Layer),mParentId(-1),mId(-1){}
182 192 LottieObject *copy() {return new LottieLayer(*this);}
183public: 193public:
184 SGRect mBound; 194 SGRect mBound;
185 int mlayerType; //lottie layer type (solid/shape/precomp) 195 int mlayerType; //lottie layer type (solid/shape/precomp)
@@ -207,6 +217,7 @@ public:
207 mOpacity(100), 217 mOpacity(100),
208 mSkew(0), 218 mSkew(0),
209 mSkewAxis(0){} 219 mSkewAxis(0){}
220 LottieObject *copy() {return new LottieTransform(*this);}
210public: 221public:
211 LottieAnimatable<float> mRotation; /* "r" */ 222 LottieAnimatable<float> mRotation; /* "r" */
212 LottieAnimatable<SGPointF> mScale; /* "s" */ 223 LottieAnimatable<SGPointF> mScale; /* "s" */
@@ -223,6 +234,7 @@ public:
223 void accept(LottieObjectVisitor *visitor) final 234 void accept(LottieObjectVisitor *visitor) final
224 {visitor->visit(this);} 235 {visitor->visit(this);}
225 LottieFillObject():LottieObject(LottieObject::Type::Fill){} 236 LottieFillObject():LottieObject(LottieObject::Type::Fill){}
237 LottieObject *copy() {return new LottieFillObject(*this);}
226public: 238public:
227 LottieAnimatable<LottieColor> mColor; /* "c" */ 239 LottieAnimatable<LottieColor> mColor; /* "c" */
228 LottieAnimatable<int> mOpacity; /* "o" */ 240 LottieAnimatable<int> mOpacity; /* "o" */
@@ -235,6 +247,7 @@ public:
235 void accept(LottieObjectVisitor *visitor) final 247 void accept(LottieObjectVisitor *visitor) final
236 {visitor->visit(this);} 248 {visitor->visit(this);}
237 LottieStrokeObject():LottieObject(LottieObject::Type::Stroke){} 249 LottieStrokeObject():LottieObject(LottieObject::Type::Stroke){}
250 LottieObject *copy() {return new LottieStrokeObject(*this);}
238public: 251public:
239 LottieAnimatable<LottieColor> mColor; /* "c" */ 252 LottieAnimatable<LottieColor> mColor; /* "c" */
240 LottieAnimatable<int> mOpacity; /* "o" */ 253 LottieAnimatable<int> mOpacity; /* "o" */
@@ -245,7 +258,7 @@ public:
245 bool mEnabled = true; /* "fillEnabled" */ 258 bool mEnabled = true; /* "fillEnabled" */
246}; 259};
247 260
248class LottieShape 261class LottieShapeData
249{ 262{
250public: 263public:
251 void process(bool closed =false); 264 void process(bool closed =false);
@@ -256,6 +269,13 @@ public:
256 int mSegments; 269 int mSegments;
257}; 270};
258 271
272class LottieShape
273{
274public:
275 void process(bool closed =false);
276 std::shared_ptr<LottieShapeData> mShapeData;
277};
278
259class LottieShapeObject : public LottieObject 279class LottieShapeObject : public LottieObject
260{ 280{
261public: 281public:
@@ -263,6 +283,7 @@ public:
263 {visitor->visit(this);} 283 {visitor->visit(this);}
264 void process(); 284 void process();
265 LottieShapeObject():LottieObject(LottieObject::Type::Shape){} 285 LottieShapeObject():LottieObject(LottieObject::Type::Shape){}
286 LottieObject *copy() {return new LottieShapeObject(*this);}
266public: 287public:
267 LottieAnimatable<LottieShape> mShape; 288 LottieAnimatable<LottieShape> mShape;
268 bool mClosed = false; 289 bool mClosed = false;
@@ -277,6 +298,7 @@ public:
277 mPos(SGPointF(0,0)), 298 mPos(SGPointF(0,0)),
278 mSize(SGPointF(0,0)), 299 mSize(SGPointF(0,0)),
279 mRound(0){} 300 mRound(0){}
301 LottieObject *copy() {return new LottieRectObject(*this);}
280public: 302public:
281 LottieAnimatable<SGPointF> mPos; 303 LottieAnimatable<SGPointF> mPos;
282 LottieAnimatable<SGPointF> mSize; 304 LottieAnimatable<SGPointF> mSize;
@@ -291,6 +313,7 @@ public:
291 LottieEllipseObject():LottieObject(LottieObject::Type::Ellipse), 313 LottieEllipseObject():LottieObject(LottieObject::Type::Ellipse),
292 mPos(SGPointF(0,0)), 314 mPos(SGPointF(0,0)),
293 mSize(SGPointF(0,0)){} 315 mSize(SGPointF(0,0)){}
316 LottieObject *copy() {return new LottieEllipseObject(*this);}
294public: 317public:
295 LottieAnimatable<SGPointF> mPos; 318 LottieAnimatable<SGPointF> mPos;
296 LottieAnimatable<SGPointF> mSize; 319 LottieAnimatable<SGPointF> mSize;
@@ -310,6 +333,7 @@ public:
310 mEnd(0), 333 mEnd(0),
311 mOffset(0), 334 mOffset(0),
312 mTrimType(TrimType::Simultaneously){} 335 mTrimType(TrimType::Simultaneously){}
336 LottieObject *copy() {return new LottieTrimObject(*this);}
313public: 337public:
314 LottieAnimatable<float> mStart; 338 LottieAnimatable<float> mStart;
315 LottieAnimatable<float> mEnd; 339 LottieAnimatable<float> mEnd;
@@ -326,6 +350,7 @@ public:
326 mCopies(0), 350 mCopies(0),
327 mOffset(0), 351 mOffset(0),
328 mTransform(nullptr){} 352 mTransform(nullptr){}
353 LottieObject *copy() {return new LottieRepeaterObject(*this);}
329public: 354public:
330 LottieAnimatable<float> mCopies; 355 LottieAnimatable<float> mCopies;
331 LottieAnimatable<float> mOffset; 356 LottieAnimatable<float> mOffset;
diff --git a/ssg/src/lottie/lottieparser.cpp b/ssg/src/lottie/lottieparser.cpp
index 87f105ab5d..086b72cdb1 100644
--- a/ssg/src/lottie/lottieparser.cpp
+++ b/ssg/src/lottie/lottieparser.cpp
@@ -933,13 +933,16 @@ void LottieParser::getValue(LottieShape &obj)
933 933
934 RAPIDJSON_ASSERT(PeekType() == kObjectType); 934 RAPIDJSON_ASSERT(PeekType() == kObjectType);
935 EnterObject(); 935 EnterObject();
936 // make sure the shared_ptr is valid.
937 if (!obj.mShapeData)
938 obj.mShapeData = std::make_shared<LottieShapeData>();//(new LottieShapeData());
936 while (const char* key = NextObjectKey()) { 939 while (const char* key = NextObjectKey()) {
937 if (0 == strcmp(key, "i")) { 940 if (0 == strcmp(key, "i")) {
938 parseArrayValue(obj.mInPoint); 941 parseArrayValue(obj.mShapeData.get()->mInPoint);
939 } else if (0 == strcmp(key, "o")) { 942 } else if (0 == strcmp(key, "o")) {
940 parseArrayValue(obj.mOutPoint); 943 parseArrayValue(obj.mShapeData.get()->mOutPoint);
941 } else if (0 == strcmp(key, "v")) { 944 } else if (0 == strcmp(key, "v")) {
942 parseArrayValue(obj.mVertices); 945 parseArrayValue(obj.mShapeData.get()->mVertices);
943 } else { 946 } else {
944 RAPIDJSON_ASSERT(0); 947 RAPIDJSON_ASSERT(0);
945 Skip(nullptr); 948 Skip(nullptr);
@@ -1044,9 +1047,9 @@ void LottieParser::parseKeyFrame(LottieAnimInfo<T> &obj)
1044 if (search != compRef->mInterpolatorCache.end()) { 1047 if (search != compRef->mInterpolatorCache.end()) {
1045 keyframe.mInterpolator = search->second; 1048 keyframe.mInterpolator = search->second;
1046 } else { 1049 } else {
1047 keyframe.mInterpolator = new LottieInterpolater(); 1050 keyframe.mInterpolator = std::make_shared<LottieInterpolater>();
1048 keyframe.mInterpolator->mInTangent = inTangent; 1051 keyframe.mInterpolator.get()->mInTangent = inTangent;
1049 keyframe.mInterpolator->mOutTangent = outTangent; 1052 keyframe.mInterpolator.get()->mOutTangent = outTangent;
1050 compRef->mInterpolatorCache[interpolatorKey] = keyframe.mInterpolator; 1053 compRef->mInterpolatorCache[interpolatorKey] = keyframe.mInterpolator;
1051 } 1054 }
1052 } else { 1055 } else {
@@ -1074,8 +1077,8 @@ LottieParser::parseShapeProperty(LottieAnimatable<LottieShape> &obj)
1074 while (NextArrayValue()) { 1077 while (NextArrayValue()) {
1075 RAPIDJSON_ASSERT(PeekType() == kObjectType); 1078 RAPIDJSON_ASSERT(PeekType() == kObjectType);
1076 if (!obj.mAnimInfo) 1079 if (!obj.mAnimInfo)
1077 obj.mAnimInfo = new LottieAnimInfo<LottieShape>(); 1080 obj.mAnimInfo.reset(new LottieAnimInfo<LottieShape>());
1078 parseKeyFrame(*obj.mAnimInfo); 1081 parseKeyFrame(*obj.mAnimInfo.get());
1079 } 1082 }
1080 } else { 1083 } else {
1081 getValue(obj.mValue); 1084 getValue(obj.mValue);
@@ -1106,8 +1109,8 @@ void LottieParser::parseProperty(LottieAnimatable<T> &obj)
1106 /* property with keyframe info*/ 1109 /* property with keyframe info*/
1107 if (PeekType() == kObjectType) { 1110 if (PeekType() == kObjectType) {
1108 if (!obj.mAnimInfo) 1111 if (!obj.mAnimInfo)
1109 obj.mAnimInfo = new LottieAnimInfo<T>(); 1112 obj.mAnimInfo.reset(new LottieAnimInfo<T>());
1110 parseKeyFrame(*obj.mAnimInfo); 1113 parseKeyFrame(*obj.mAnimInfo.get());
1111 } else { 1114 } else {
1112 /* Read before modifying. 1115 /* Read before modifying.
1113 * as there is no way of knowing if the 1116 * as there is no way of knowing if the