lottie/player: refactor composition class to keep a precomplayer instead of list... 89/186789/3
authorsubhransu mohanty <sub.mohanty@samsung.com>
Tue, 14 Aug 2018 09:57:43 +0000 (18:57 +0900)
committerHermet Park <chuneon.park@samsung.com>
Tue, 14 Aug 2018 10:09:39 +0000 (10:09 +0000)
Change-Id: I7398392e52233e4c3d0452b03bcfd46261253601

src/lottie/lottieitem.cpp
src/lottie/lottieitem.h
src/lottie/lottiemodel.h
src/lottie/lottieparser.cpp

index 1dd37e8..9f20684 100644 (file)
 LOTCompItem::LOTCompItem(LOTModel *model)
     : mRootModel(model), mUpdateViewBox(false), mCurFrameNo(-1)
 {
-    // 1. build layer item list
     mCompData = model->mRoot.get();
-    for (auto &i : mCompData->mChildren) {
-        LOTLayerData *layerData = dynamic_cast<LOTLayerData *>(i.get());
-        if (layerData) {
-            LOTLayerItem *layerItem = LOTCompItem::createLayerItem(layerData);
-            if (layerItem) {
-                mLayers.push_back(layerItem);
-                mLayerMap[layerItem->id()] = layerItem;
-            }
-        }
-    }
-
-    // 2. update parent layer
-    for (auto &i : mLayers) {
-        int id = i->parentId();
-        if (id >= 0) {
-            auto search = mLayerMap.find(id);
-            if (search != mLayerMap.end()) {
-                LOTLayerItem *parentLayer = search->second;
-                i->setParentLayer(parentLayer);
-            }
-        }
-    }
-    // 3. update static property of each layer
-    for (auto &i : mLayers) {
-        i->updateStaticProperty();
-    }
-
+    mRootLayer = std::unique_ptr<LOTLayerItem>(createLayerItem(mCompData->mRootLayer.get()));
+    mRootLayer->updateStaticProperty();
     mViewSize = mCompData->size();
 }
 
-LOTCompItem::~LOTCompItem()
-{
-    for (auto &i : mLayers) {
-        delete i;
-    }
-}
-
 LOTLayerItem *LOTCompItem::createLayerItem(LOTLayerData *layerData)
 {
     switch (layerData->mLayerType) {
@@ -112,12 +79,8 @@ bool LOTCompItem::update(int frameNo)
 
     VMatrix m;
     m.scale(scale, scale).translate(tx, ty);
+    mRootLayer->update(frameNo, m, 1.0);
 
-    // update the layer from back to front
-    for (auto i = mLayers.rbegin(); i != mLayers.rend(); ++i) {
-        LOTLayerItem *layer = *i;
-        layer->update(frameNo, m, 1.0);
-    }
     buildRenderList();
     mCurFrameNo = frameNo;
     mUpdateViewBox = false;
@@ -127,10 +90,7 @@ bool LOTCompItem::update(int frameNo)
 void LOTCompItem::buildRenderList()
 {
     mDrawableList.clear();
-    for (auto i = mLayers.rbegin(); i != mLayers.rend(); ++i) {
-        LOTLayerItem *layer = *i;
-        layer->renderList(mDrawableList);
-    }
+    mRootLayer->renderList(mDrawableList);
 
     mRenderList.clear();
     for (auto &i : mDrawableList) {
@@ -159,10 +119,7 @@ bool LOTCompItem::render(const LOTBuffer &buffer)
 
     VPainter painter(&bitmap);
     VRle     mask;
-    for (auto i = mLayers.rbegin(); i != mLayers.rend(); ++i) {
-        LOTLayerItem *layer = *i;
-        layer->render(&painter, mask);
-    }
+    mRootLayer->render(&painter, mask);
 
     return true;
 }
index eebc24e..42721ce 100644 (file)
@@ -31,7 +31,6 @@ class LOTCompItem
 {
 public:
    LOTCompItem(LOTModel *model);
-   ~LOTCompItem();
    static LOTLayerItem * createLayerItem(LOTLayerData *layerData);
    bool update(int frameNo);
    void resize(const VSize &size);
@@ -44,8 +43,7 @@ private:
    VSize                                      mViewSize;
    LOTModel                                   *mRootModel;
    LOTCompositionData                         *mCompData;
-   std::vector<LOTLayerItem *>                 mLayers;
-   std::unordered_map<int, LOTLayerItem *>     mLayerMap;
+   std::unique_ptr<LOTLayerItem>               mRootLayer;
    bool                                        mUpdateViewBox;
    int                                         mCurFrameNo;
    std::vector<LOTNode *>                      mRenderList;
index b9dba2c..57199b6 100644 (file)
@@ -330,10 +330,12 @@ public:
     long                 mEndFrame = 0;
     float                mFrameRate;
     LottieBlendMode      mBlendMode;
+    std::shared_ptr<LOTLayerData> mRootLayer;
     std::unordered_map<std::string,
                        std::shared_ptr<VInterpolator>> mInterpolatorCache;
     std::unordered_map<std::string,
                        std::shared_ptr<LOTAsset>>    mAssets;
+
 };
 
 class LOTLayerData : public LOTGroupData
index af1304f..822cfa4 100644 (file)
@@ -579,12 +579,9 @@ void LottieParserImpl::parseComposition()
         }
     }
     resolveLayerRefs();
-    // update the static property of Composition
-    bool staticFlag = true;
-    for (auto child : comp->mChildren) {
-        staticFlag &= child.get()->isStatic();
-    }
-    comp->setStatic(staticFlag);
+    comp->setStatic(comp->mRootLayer->isStatic());
+    comp->mRootLayer->mInFrame = comp->mStartFrame;
+    comp->mRootLayer->mOutFrame = comp->mEndFrame;
 
     mComposition = sharedComposition;
 }
@@ -634,14 +631,20 @@ std::shared_ptr<LOTAsset> LottieParserImpl::parseAsset()
     return sharedAsset;
 }
 
-void LottieParserImpl::parseLayers(LOTCompositionData *composition)
+void LottieParserImpl::parseLayers(LOTCompositionData *comp)
 {
+    comp->mRootLayer = std::make_shared<LOTLayerData>();
+    comp->mRootLayer->mLayerType = LayerType::Precomp;
+    comp->mRootLayer->mTransform = std::make_shared<LOTTransformData>();
+    bool staticFlag = true;
     RAPIDJSON_ASSERT(PeekType() == kArrayType);
     EnterArray();
     while (NextArrayValue()) {
         std::shared_ptr<LOTData> layer = parseLayer();
-        composition->mChildren.push_back(layer);
+        staticFlag &= layer->isStatic();
+        comp->mRootLayer->mChildren.push_back(layer);
     }
+    comp->mRootLayer->setStatic(staticFlag);
 }
 
 LottieColor LottieParserImpl::toColor(const char *str)