lottie: removed dead code.
[platform/core/uifw/lottie-player.git] / src / lottie / lottieitem.cpp
1 #include "lottieitem.h"
2 #include"vbitmap.h"
3 #include"vpainter.h"
4 #include"vraster.h"
5 #include"vdasher.h"
6 #include <cmath>
7
8
9 VDrawable::VDrawable():mFlag(DirtyState::All),
10                        mType(Type::Fill),
11                        mFillRule(FillRule::Winding)
12 {
13     mStroke.dashArraySize = 0;
14     mStroke.cap = CapStyle::Round;
15     mStroke.join= JoinStyle::Round;
16     mStroke.meterLimit = 10;
17     mStroke.enable = false;
18 }
19
20 void VDrawable::preprocess()
21 {
22     if (mFlag & (DirtyState::Path)) {
23         if (mStroke.enable) {
24             VPath newPath = mPath;
25             if (mStroke.dashArraySize) {
26                 VDasher dasher(mStroke.dashArray, mStroke.dashArraySize);
27                 newPath = dasher.dashed(mPath);
28             }
29             FTOutline *outline = VRaster::toFTOutline(newPath);
30             mRle = VRaster::instance().generateStrokeInfo(outline, mStroke.cap, mStroke.join,
31                                                           mStroke.width, mStroke.meterLimit);
32             VRaster::deleteFTOutline(outline);
33         } else {
34             FTOutline *outline = VRaster::toFTOutline(mPath);
35             mRle = VRaster::instance().generateFillInfo(outline, mFillRule);
36             VRaster::deleteFTOutline(outline);
37         }
38         mFlag &= ~DirtyFlag(DirtyState::Path);
39     }
40 }
41
42 void VDrawable::setStrokeInfo(CapStyle cap, JoinStyle join, float meterLimit, float strokeWidth)
43 {
44     mStroke.enable = true;
45     mStroke.cap = cap;
46     mStroke.join = join;
47     mStroke.meterLimit = meterLimit;
48     mStroke.width = strokeWidth;
49     mFlag |= DirtyState::Path;
50 }
51 void VDrawable::setDashInfo(float *array, int size)
52 {
53     mStroke.dashArray = array;
54     mStroke.dashArraySize = size;
55     mFlag |= DirtyState::Path;
56 }
57
58 void VDrawable::sync()
59 {
60     mCNode.mFlag = ChangeFlagNone;
61     if (mFlag & DirtyState::None) return;
62
63     if (mFlag & DirtyState::Path) {
64         const std::vector<VPath::Element> &elm = mPath.elements();
65         const std::vector<VPointF> &pts  = mPath.points();
66         const float *ptPtr = reinterpret_cast<const float *>(pts.data());
67         const char *elmPtr = reinterpret_cast<const char *>(elm.data());
68         mCNode.mPath.elmPtr = elmPtr;
69         mCNode.mPath.elmCount = elm.size();
70         mCNode.mPath.ptPtr = ptPtr;
71         mCNode.mPath.ptCount = 2 * pts.size();
72         mCNode.mFlag |= ChangeFlagPath;
73     }
74
75     if (mStroke.enable) {
76         mCNode.mStroke.width = mStroke.width;
77         mCNode.mStroke.meterLimit = mStroke.meterLimit;
78         mCNode.mStroke.enable = 1;
79
80         switch (mFillRule) {
81         case FillRule::EvenOdd:
82             mCNode.mFillRule = LOTNode::EvenOdd;
83             break;
84         default:
85             mCNode.mFillRule = LOTNode::Winding;
86             break;
87         }
88
89         switch (mStroke.cap) {
90         case CapStyle::Flat:
91             mCNode.mStroke.cap = LOTNode::FlatCap;
92             break;
93         case CapStyle::Square:
94             mCNode.mStroke.cap = LOTNode::SquareCap;
95             break;
96         case CapStyle::Round:
97             mCNode.mStroke.cap = LOTNode::RoundCap;
98             break;
99         default:
100             mCNode.mStroke.cap = LOTNode::FlatCap;
101             break;
102         }
103
104         switch (mStroke.join) {
105         case JoinStyle::Miter:
106             mCNode.mStroke.join = LOTNode::MiterJoin;
107             break;
108         case JoinStyle::Bevel:
109             mCNode.mStroke.join = LOTNode::BevelJoin;
110             break;
111         case JoinStyle::Round:
112             mCNode.mStroke.join = LOTNode::RoundJoin;
113             break;
114         default:
115             mCNode.mStroke.join = LOTNode::MiterJoin;
116             break;
117         }
118
119         mCNode.mStroke.dashArray = mStroke.dashArray;
120         mCNode.mStroke.dashArraySize = mStroke.dashArraySize;
121
122     } else {
123         mCNode.mStroke.enable = 0;
124     }
125
126     switch (mBrush.type()) {
127     case VBrush::Type::Solid:
128         mCNode.mType = LOTNode::BrushSolid;
129         mCNode.mColor.r = mBrush.mColor.r;
130         mCNode.mColor.g = mBrush.mColor.g;
131         mCNode.mColor.b = mBrush.mColor.b;
132         mCNode.mColor.a = mBrush.mColor.a;
133         break;
134     case VBrush::Type::LinearGradient:
135         mCNode.mType = LOTNode::BrushGradient;
136         mCNode.mGradient.type = LOTNode::Gradient::Linear;
137         mCNode.mGradient.start.x = mBrush.mGradient->linear.x1;
138         mCNode.mGradient.start.y = mBrush.mGradient->linear.y1;
139         mCNode.mGradient.end.x = mBrush.mGradient->linear.x2;
140         mCNode.mGradient.end.y = mBrush.mGradient->linear.y2;
141         break;
142     case VBrush::Type::RadialGradient:
143         mCNode.mType = LOTNode::BrushGradient;
144         mCNode.mGradient.type = LOTNode::Gradient::Radial;
145         mCNode.mGradient.center.x = mBrush.mGradient->radial.cx;
146         mCNode.mGradient.center.y = mBrush.mGradient->radial.cy;
147         mCNode.mGradient.focal.x = mBrush.mGradient->radial.fx;
148         mCNode.mGradient.focal.y = mBrush.mGradient->radial.fy;
149         mCNode.mGradient.cradius = mBrush.mGradient->radial.cradius;
150         mCNode.mGradient.fradius = mBrush.mGradient->radial.fradius;
151         break;
152     default:
153         break;
154     }
155 }
156
157 /* Lottie Layer Rules
158  * 1. time stretch is pre calculated and applied to all the properties of the lottilayer model and all its children
159  * 2. The frame property could be reversed using,time-reverse layer property in AE. which means (start frame > endFrame)
160  * 3.
161  */
162
163 LOTCompItem::LOTCompItem(LOTModel *model):mRootModel(model), mUpdateViewBox(false),mCurFrameNo(-1)
164 {
165    // 1. build layer item list
166    mCompData = model->mRoot.get();
167    for(auto i : mCompData->mChildren) {
168       LOTLayerData *layerData = dynamic_cast<LOTLayerData *>(i.get());
169       if (layerData) {
170          LOTLayerItem *layerItem = LOTCompItem::createLayerItem(layerData);
171          if (layerItem) {
172             mLayers.push_back(layerItem);
173             mLayerMap[layerItem->id()] = layerItem;
174          }
175       }
176    }
177
178    //2. update parent layer
179    for(auto i : mLayers) {
180       int id = i->parentId();
181       if (id >=0) {
182          auto search = mLayerMap.find(id);
183          if (search != mLayerMap.end()) {
184            LOTLayerItem *parentLayer = search->second;
185            i->setParentLayer(parentLayer);
186          }
187       }
188    }
189    //3. update static property of each layer
190    for(auto i : mLayers) {
191       i->updateStaticProperty();
192    }
193
194    mViewSize = mCompData->size();
195 }
196
197 LOTCompItem::~LOTCompItem()
198 {
199     for(auto i : mLayers) {
200        delete i;
201     }
202 }
203
204 LOTLayerItem *
205 LOTCompItem::createLayerItem(LOTLayerData *layerData)
206 {
207     switch(layerData->mLayerType) {
208         case LayerType::Precomp: {
209             return new LOTCompLayerItem(layerData);
210             break;
211         }
212         case LayerType::Solid: {
213             return new LOTSolidLayerItem(layerData);
214             break;
215         }
216         case LayerType::Shape: {
217             return new LOTShapeLayerItem(layerData);
218             break;
219         }
220         case LayerType::Null: {
221             return new LOTNullLayerItem(layerData);
222             break;
223         }
224         default:
225             return nullptr;
226             break;
227     }
228 }
229
230 void LOTCompItem::resize(const VSize &size)
231 {
232    if (mViewSize == size) return;
233    mViewSize = size;
234    mUpdateViewBox = true;
235 }
236
237 VSize LOTCompItem::size() const
238 {
239    return mViewSize;
240 }
241
242 bool LOTCompItem::update(int frameNo)
243 {
244    VMatrix m;
245    float sx, sy;
246
247    // check if cached frame is same as requested frame.
248    if (!mUpdateViewBox && (mCurFrameNo == frameNo)) return false;
249
250    sx = mViewSize.width() / float(mCompData->size().width());
251    sy = mViewSize.height() / float(mCompData->size().height());
252    float scale = fmin(sx, sy);
253    m.scale(scale, scale);
254
255    // update the layer from back to front
256    for (auto i = mLayers.rbegin(); i != mLayers.rend(); ++i) {
257       LOTLayerItem *layer = *i;
258       layer->update(frameNo, m, 1.0);
259    }
260    buildRenderList();
261    mCurFrameNo = frameNo;
262    mUpdateViewBox = false;
263    return true;
264 }
265
266 void LOTCompItem::buildRenderList()
267 {
268     mRenderList.clear();
269     std::vector<VDrawable *> list;
270     for (auto i = mLayers.rbegin(); i != mLayers.rend(); ++i) {
271        LOTLayerItem *layer = *i;
272        layer->renderList(list);
273     }
274
275     for(auto i : list) {
276         i->sync();
277         mRenderList.push_back(&i->mCNode);
278     }
279 }
280
281 const std::vector<LOTNode *>& LOTCompItem::renderList() const
282 {
283     return mRenderList;
284 }
285
286 bool LOTCompItem::render(const LOTBuffer &buffer)
287 {
288     VBitmap bitmap((uchar *)buffer.buffer, buffer.width, buffer.height,
289                    buffer.bytesPerLine, VBitmap::Format::ARGB32_Premultiplied, nullptr, nullptr);
290
291     VPainter painter(&bitmap);
292     VRle mask;
293     for (auto i = mLayers.rbegin(); i != mLayers.rend(); ++i) {
294        LOTLayerItem *layer = *i;
295        layer->render(&painter, mask);
296     }
297
298     return true;
299 }
300
301 void LOTMaskItem::update(int frameNo, const VMatrix &parentMatrix,
302                          float parentAlpha, const DirtyFlag &flag)
303 {
304     if (mData->mShape.isStatic()) {
305         if (mLocalPath.isEmpty()) {
306             mLocalPath = mData->mShape.value(frameNo).toPath();
307         }
308     } else {
309         mLocalPath = mData->mShape.value(frameNo).toPath();
310     }
311     float opacity = mData->opacity(frameNo);
312     opacity = opacity * parentAlpha;
313
314     VPath path = mLocalPath;
315     path.transform(parentMatrix);
316
317     FTOutline *outline = VRaster::toFTOutline(path);
318     mRle = VRaster::instance().generateFillInfo(outline);
319     VRaster::deleteFTOutline(outline);
320
321     mRle = mRle * (opacity * 255);
322
323     if (mData->mInv) {
324         mRle = ~mRle;
325     }
326 }
327
328 void LOTLayerItem::render(VPainter *painter, const VRle &inheritMask)
329 {
330     std::vector<VDrawable *> list;
331     renderList(list);
332     VRle mask = inheritMask;
333     if (hasMask()) {
334         if (mask.isEmpty())
335             mask = maskRle();
336         else
337             mask = mask & inheritMask;
338     }
339     for(auto i : list) {
340         i->preprocess();
341         painter->setBrush(i->mBrush);
342         if (!mask.isEmpty()) {
343             VRle rle = i->mRle & mask;
344             painter->drawRle(VPoint(), rle);
345         } else {
346             painter->drawRle(VPoint(), i->mRle);
347         }
348     }
349 }
350
351 VRle LOTLayerItem::maskRle()
352 {
353     VRle rle;
354     for (auto &i : mMasks) {
355         switch (i->maskMode()) {
356             case LOTMaskData::Mode::Add: {
357                 rle = rle + i->mRle;
358                 break;
359             }
360             case LOTMaskData::Mode::Substarct: {
361                 if (rle.isEmpty() && !mBoundingRect.isEmpty())
362                     rle = VRle::toRle(mBoundingRect);
363                 rle = rle - i->mRle;
364                 break;
365             }
366             case LOTMaskData::Mode::Intersect: {
367                 rle = rle & i->mRle;
368                 break;
369             }
370             default:
371                 break;
372         }
373     }
374     return rle;
375 }
376
377 LOTLayerItem::LOTLayerItem(LOTLayerData *layerData):mLayerData(layerData),
378                                                     mParentLayer(nullptr),
379                                                     mPrecompLayer(nullptr),
380                                                     mFrameNo(-1),
381                                                     mDirtyFlag(DirtyFlagBit::All)
382 {
383     if (mLayerData->mHasMask) {
384         for (auto i : mLayerData->mMasks) {
385             mMasks.push_back(std::unique_ptr<LOTMaskItem>(new LOTMaskItem(i.get())));
386         }
387     }
388 }
389
390 void LOTLayerItem::updateStaticProperty()
391 {
392    if (mParentLayer)
393      mParentLayer->updateStaticProperty();
394
395    mStatic = mLayerData->isStatic();
396    mStatic = mParentLayer ? (mStatic & mParentLayer->isStatic()) : mStatic;
397    mStatic = mPrecompLayer ? (mStatic & mPrecompLayer->isStatic()) : mStatic;
398 }
399
400 void LOTLayerItem::update(int frameNo, const VMatrix &parentMatrix, float parentAlpha)
401 {
402    mFrameNo = frameNo;
403    // 1. check if the layer is part of the current frame
404    if (!visible()) return;
405
406    // 2. calculate the parent matrix and alpha
407    VMatrix m = matrix(frameNo) * parentMatrix;
408    float alpha = parentAlpha * opacity(frameNo);
409
410    //6. update the mask
411    if (hasMask()) {
412        for (auto &i : mMasks)
413            i->update(frameNo, m, alpha, mDirtyFlag);
414    }
415
416    // 3. update the dirty flag based on the change
417    if (!mCombinedMatrix.fuzzyCompare(m)) {
418        mDirtyFlag |= DirtyFlagBit::Matrix;
419    }
420    if (!vCompare(mCombinedAlpha, alpha)) {
421        mDirtyFlag |= DirtyFlagBit::Alpha;
422    }
423    mCombinedMatrix = m;
424    mCombinedAlpha = alpha;
425
426    // 4. if no parent property change and layer is static then nothing to do.
427    if ((flag() & DirtyFlagBit::None) && isStatic())
428       return;
429
430    //5. update the content of the layer
431    updateContent();
432
433    //6. reset the dirty flag
434    mDirtyFlag = DirtyFlagBit::None;
435 }
436
437 float
438 LOTLayerItem::opacity(int frameNo) const
439 {
440    return mLayerData->mTransform->opacity(frameNo);
441 }
442
443 VMatrix
444 LOTLayerItem::matrix(int frameNo) const
445 {
446     if (mParentLayer)
447         return mLayerData->mTransform->matrix(frameNo) * mParentLayer->matrix(frameNo);
448     else
449         return mLayerData->mTransform->matrix(frameNo);
450 }
451
452 bool LOTLayerItem::visible() const
453 {
454    if (frameNo() >= mLayerData->inFrame() && frameNo() < mLayerData->outFrame())
455       return true;
456    else
457       return false;
458 }
459
460
461
462 LOTCompLayerItem::LOTCompLayerItem(LOTLayerData *layerModel):LOTLayerItem(layerModel)
463 {
464    for(auto i : mLayerData->mChildren) {
465       LOTLayerData *layerModel = dynamic_cast<LOTLayerData *>(i.get());
466       if (layerModel) {
467          LOTLayerItem *layerItem = LOTCompItem::createLayerItem(layerModel);
468          if (layerItem) {
469             mLayers.push_back(layerItem);
470             mLayerMap[layerItem->id()] = layerItem;
471          }
472       }
473    }
474
475    //2. update parent layer
476    for(auto i : mLayers) {
477       int id = i->parentId();
478       if (id >=0) {
479          auto search = mLayerMap.find(id);
480          if (search != mLayerMap.end()) {
481            LOTLayerItem *parentLayer = search->second;
482            i->setParentLayer(parentLayer);
483          }
484       }
485       i->setPrecompLayer(this);
486    }
487 }
488
489 void LOTCompLayerItem::updateStaticProperty()
490 {
491     LOTLayerItem::updateStaticProperty();
492
493     for(auto i : mLayers) {
494        i->updateStaticProperty();
495     }
496 }
497
498 void LOTCompLayerItem::render(VPainter *painter, const VRle &inheritMask)
499 {
500     VRle mask = inheritMask;
501
502     if (hasMask()) {
503         if (mask.isEmpty())
504             mask = maskRle();
505         else
506             mask = mask & inheritMask;
507     }
508
509     for(auto i : mLayers) {
510        i->render(painter, mask);
511     }
512 }
513
514 LOTCompLayerItem::~LOTCompLayerItem()
515 {
516     for(auto i : mLayers) {
517        delete i;
518     }
519 }
520
521 void LOTCompLayerItem::updateContent()
522 {
523     // update the layer from back to front
524     for (auto i = mLayers.rbegin(); i != mLayers.rend(); ++i) {
525        LOTLayerItem *layer = *i;
526        layer->update(frameNo(), combinedMatrix(), combinedAlpha());
527     }
528 }
529
530 void LOTCompLayerItem::renderList(std::vector<VDrawable *> &list)
531 {
532     if (!visible()) return;
533
534     // update the layer from back to front
535     for (auto i = mLayers.rbegin(); i != mLayers.rend(); ++i) {
536        LOTLayerItem *layer = *i;
537        layer->renderList(list);
538     }
539 }
540
541 LOTSolidLayerItem::LOTSolidLayerItem(LOTLayerData *layerData):LOTLayerItem(layerData)
542 {
543
544 }
545
546 void LOTSolidLayerItem::updateContent()
547 {
548    if (!mRenderNode) {
549       mRenderNode = std::unique_ptr<VDrawable>(new VDrawable());
550       mRenderNode->mType = VDrawable::Type::Fill;
551       mRenderNode->mFlag |= VDrawable::DirtyState::All;
552    }
553
554    if (flag() & DirtyFlagBit::Matrix) {
555        VPath path;
556        path.addRect(VRectF(0, 0, mLayerData->solidWidth(), mLayerData->solidHeight()));
557        path.transform(combinedMatrix());
558        mRenderNode->mFlag |= VDrawable::DirtyState::Path;
559        mRenderNode->mPath = path;
560    }
561    if (flag() & DirtyFlagBit::Alpha) {
562        LottieColor color = mLayerData->solidColor();
563        VBrush brush(color.toColor(combinedAlpha()));
564        mRenderNode->setBrush(brush);
565        mRenderNode->mFlag |= VDrawable::DirtyState::Brush;
566    }
567 }
568
569 void LOTSolidLayerItem::renderList(std::vector<VDrawable *> &list)
570 {
571     if (!visible()) return;
572
573     list.push_back(mRenderNode.get());
574 }
575
576 LOTNullLayerItem::LOTNullLayerItem(LOTLayerData *layerData):LOTLayerItem(layerData)
577 {
578
579 }
580 void LOTNullLayerItem::updateContent()
581 {
582
583 }
584
585
586 LOTShapeLayerItem::LOTShapeLayerItem(LOTLayerData *layerData):LOTLayerItem(layerData)
587 {
588     mRoot = new LOTContentGroupItem(nullptr);
589     mRoot->addChildren(layerData);
590     mRoot->processPaintOperation();
591 }
592
593 LOTShapeLayerItem::~LOTShapeLayerItem()
594 {
595     delete mRoot;
596 }
597
598 LOTContentItem * LOTShapeLayerItem::createContentItem(LOTData *contentData)
599 {
600     switch(contentData->type()) {
601         case LOTData::Type::ShapeGroup: {
602             return new LOTContentGroupItem(static_cast<LOTShapeGroupData *>(contentData));
603             break;
604         }
605         case LOTData::Type::Rect: {
606             return new LOTRectItem(static_cast<LOTRectData *>(contentData));
607             break;
608         }
609         case LOTData::Type::Ellipse: {
610             return new LOTEllipseItem(static_cast<LOTEllipseData *>(contentData));
611             break;
612         }
613         case LOTData::Type::Shape: {
614             return new LOTShapeItem(static_cast<LOTShapeData *>(contentData));
615             break;
616         }
617         case LOTData::Type::Polystar: {
618             return new LOTPolystarItem(static_cast<LOTPolystarData *>(contentData));
619             break;
620         }
621         case LOTData::Type::Fill: {
622             return new LOTFillItem(static_cast<LOTFillData *>(contentData));
623             break;
624         }
625         case LOTData::Type::GFill: {
626             return new LOTGFillItem(static_cast<LOTGFillData *>(contentData));
627             break;
628         }
629         case LOTData::Type::Stroke: {
630             return new LOTStrokeItem(static_cast<LOTStrokeData *>(contentData));
631             break;
632         }
633         case LOTData::Type::GStroke: {
634             return new LOTGStrokeItem(static_cast<LOTGStrokeData *>(contentData));
635             break;
636         }
637         case LOTData::Type::Repeater: {
638                 return new LOTRepeaterItem(static_cast<LOTRepeaterData *>(contentData));
639                 break;
640             }
641         default:
642             return nullptr;
643             break;
644     }
645 }
646
647 void LOTShapeLayerItem::updateContent()
648 {
649    mRoot->update(frameNo(), combinedMatrix(), combinedAlpha(), flag());
650 }
651
652 void LOTShapeLayerItem::renderList(std::vector<VDrawable *> &list)
653 {
654     if (!visible()) return;
655     mRoot->renderList(list);
656 }
657
658 VPath::Direction LOTContentItem::direction(bool isCW)
659 {
660    if (isCW)
661       return VPath::Direction::CW;
662    else
663       return VPath::Direction::CCW;
664 }
665
666
667 LOTContentGroupItem::LOTContentGroupItem(LOTShapeGroupData *data):mData(data)
668 {
669    addChildren(mData);
670 }
671
672 void LOTContentGroupItem::addChildren(LOTGroupData *data)
673 {
674    if (!data) return;
675
676    for(auto i : data->mChildren) {
677       LOTData *data = i.get();
678       LOTContentItem *content = LOTShapeLayerItem::createContentItem(data);
679       if (content)
680          mContents.push_back(content);
681    }
682 }
683
684 LOTContentGroupItem::~LOTContentGroupItem()
685 {
686     for(auto i : mContents) {
687         delete i;
688     }
689 }
690
691
692 void LOTContentGroupItem::update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag)
693 {
694    VMatrix m = parentMatrix;
695    float alpha = parentAlpha;
696    DirtyFlag newFlag = flag;
697
698    if (mData) {
699       // update the matrix and the flag
700       if ((flag & DirtyFlagBit::Matrix) || !mData->mTransform->staticMatrix() ) {
701          newFlag |= DirtyFlagBit::Matrix;
702       }
703       m = mData->mTransform->matrix(frameNo) * parentMatrix;
704       alpha *= mData->mTransform->opacity(frameNo);
705
706       if (!floatCmp(alpha, parentAlpha)) {
707          newFlag |= DirtyFlagBit::Alpha;
708       }
709    }
710
711    for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
712       (*i)->update(frameNo, m, alpha, newFlag);
713    }
714 }
715
716 void LOTContentGroupItem::renderList(std::vector<VDrawable *> &list)
717 {
718     for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
719        (*i)->renderList(list);
720     }
721 }
722
723 void LOTContentGroupItem::processPaintOperation()
724 {
725    std::vector<LOTPaintDataItem *> list;
726    paintOperationHelper(list);
727 }
728
729 void LOTContentGroupItem::paintOperationHelper(std::vector<LOTPaintDataItem *> &list)
730 {
731    int curOpCount = list.size();
732    for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
733       auto child = *i;
734       if (auto pathNode = dynamic_cast<LOTPathDataItem *>(child)) {
735          // the node is a path data node add the paint operation list to it.
736          pathNode->addPaintOperation(list, curOpCount);
737       } else if (auto paintNode = dynamic_cast<LOTPaintDataItem *>(child)) {
738          // add it to the paint operation list
739          list.push_back(paintNode);
740       } else if (auto groupNode = dynamic_cast<LOTContentGroupItem *>(child)) {
741          // update the groups node with current list
742          groupNode->paintOperationHelper(list);
743       }
744    }
745    list.erase(list.begin() + curOpCount, list.end());
746 }
747
748 void LOTPathDataItem::addPaintOperation(std::vector<LOTPaintDataItem *> &list, int externalCount)
749 {
750     for(auto paintItem : list) {
751       bool sameGroup = (externalCount-- > 0) ? false : true;
752       mNodeList.push_back(std::unique_ptr<VDrawable>(new VDrawable()));
753       mRenderList.push_back(LOTRenderNode(this, paintItem, mNodeList.back().get(), sameGroup));
754     }
755 }
756
757
758 void LOTPathDataItem::update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag)
759 {
760    mPathChanged = false;
761    mCombinedAlpha = parentAlpha;
762
763    // 1. update the local path if needed
764    if (!(mInit && mStaticPath)) {
765       mLocalPath = getPath(frameNo);
766       mInit = true;
767       mPathChanged = true;
768    }
769
770    // 2. apply path operation if needed
771    // TODO
772
773    // 3. compute the final path with parentMatrix
774    if ((flag & DirtyFlagBit::Matrix) || mPathChanged) {
775       mFinalPath = mLocalPath;
776       mFinalPath.transform(parentMatrix);
777       mPathChanged = true;
778    }
779
780    // 2. update the rendernode list
781    for (const auto &i : mRenderList) {
782       i.drawable->mFlag = VDrawable::DirtyState::None;
783       i.paintNodeRef->updateRenderNode(i.pathNodeRef, i.drawable, i.sameGroup);
784       if (mPathChanged) {
785           i.drawable->mPath = mFinalPath;
786           i.drawable->mFlag |= VDrawable::DirtyState::Path;
787       }
788    }
789 }
790
791 void LOTPathDataItem::renderList(std::vector<VDrawable *> &list)
792 {
793    for (const auto &i : mRenderList) {
794        list.push_back(i.drawable);
795    }
796 }
797
798 VPath LOTPathDataItem::path() const
799 {
800    return mFinalPath;
801 }
802
803
804 LOTRectItem::LOTRectItem(LOTRectData *data):LOTPathDataItem(data->isStatic()),mData(data)
805 {
806 }
807
808 VPath LOTRectItem::getPath(int frameNo)
809 {
810    VPointF pos = mData->mPos.value(frameNo);
811    VPointF size = mData->mSize.value(frameNo);
812    float radius = mData->mRound.value(frameNo);
813    VRectF r(pos.x() - size.x()/2, pos.y() - size.y()/2, size.x(), size.y());
814
815    VPath path;
816    path.addRoundRect(r, radius, radius, direction(mData->isDirectionCW()));
817
818    return path;
819 }
820
821 LOTEllipseItem::LOTEllipseItem(LOTEllipseData *data):LOTPathDataItem(data->isStatic()),mData(data)
822 {
823
824 }
825
826 VPath LOTEllipseItem::getPath(int frameNo)
827 {
828    VPointF pos = mData->mPos.value(frameNo);
829    VPointF size = mData->mSize.value(frameNo);
830    VRectF r(pos.x() - size.x()/2, pos.y() - size.y()/2, size.x(), size.y());
831
832    VPath path;
833    path.addOval(r, direction(mData->isDirectionCW()));
834
835    return path;
836 }
837
838 LOTShapeItem::LOTShapeItem(LOTShapeData *data):LOTPathDataItem(data->isStatic()),mData(data)
839 {
840
841 }
842
843 VPath LOTShapeItem::getPath(int frameNo)
844 {
845     LottieShapeData shapeData = mData->mShape.value(frameNo);
846
847     if (shapeData.mPoints.empty())
848      return VPath();
849
850     VPath path;
851
852     int size = shapeData.mPoints.size();
853     const VPointF *points = shapeData.mPoints.data();
854     path.moveTo(points[0]);
855     for (int i = 1 ; i < size; i+=3) {
856        path.cubicTo(points[i], points[i+1], points[i+2]);
857     }
858     if (shapeData.mClosed)
859       path.close();
860
861    return path;
862 }
863
864
865 LOTPolystarItem::LOTPolystarItem(LOTPolystarData *data):LOTPathDataItem(data->isStatic()),mData(data)
866 {
867
868 }
869
870 VPath LOTPolystarItem::getPath(int frameNo)
871 {
872    VPointF pos = mData->mPos.value(frameNo);
873    float points = mData->mPointCount.value(frameNo);
874    float innerRadius = mData->mInnerRadius.value(frameNo);
875    float outerRadius = mData->mOuterRadius.value(frameNo);
876    float innerRoundness = mData->mInnerRoundness.value(frameNo);
877    float outerRoundness = mData->mOuterRoundness.value(frameNo);
878    float rotation = mData->mRotation.value(frameNo);
879
880    VPath path;
881    VMatrix m;
882
883    if (mData->mType == LOTPolystarData::PolyType::Star) {
884         path.addPolystarStar(0.0, 0.0, 0.0, points,
885                              innerRadius, outerRadius,
886                              innerRoundness, outerRoundness,
887                              direction(mData->isDirectionCW()));
888    } else {
889         path.addPolystarPolygon(0.0, 0.0, 0.0, points,
890                                 outerRadius, outerRoundness,
891                                 direction(mData->isDirectionCW()));
892    }
893
894    m.translate(pos.x(), pos.y()).rotate(rotation);
895    m.rotate(rotation);
896    path.transform(m);
897
898    return path;
899 }
900
901
902
903 /*
904  * PaintData Node handling
905  *
906  */
907
908 void LOTPaintDataItem::update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag)
909 {
910    mContentChanged = false;
911    mParentAlpha = parentAlpha;
912    mParentMatrix = parentMatrix;
913    mFlag = flag;
914    mFrameNo = frameNo;
915    // 1. update the local content if needed
916   // if (!(mInit && mStaticContent)) {
917       mInit = true;
918       updateContent(frameNo);
919       mContentChanged = true;
920   // }
921 }
922
923
924 LOTFillItem::LOTFillItem(LOTFillData *data):LOTPaintDataItem(data->isStatic()),mData(data)
925 {
926 }
927
928 void LOTFillItem::updateContent(int frameNo)
929 {
930    LottieColor c = mData->mColor.value(frameNo);
931    float opacity = mData->opacity(frameNo);
932    mColor = c.toColor(opacity);
933    mFillRule = mData->fillRule();
934 }
935
936 void LOTFillItem::updateRenderNode(LOTPathDataItem *pathNode, VDrawable *drawable, bool sameParent)
937 {
938     VColor color = mColor;
939     if (sameParent)
940       color.setAlpha(color.a * pathNode->combinedAlpha());
941     else
942       color.setAlpha(color.a  * parentAlpha() * pathNode->combinedAlpha());
943     VBrush brush(color);
944     drawable->setBrush(brush);
945     drawable->setFillRule(mFillRule);
946 }
947
948
949 LOTGFillItem::LOTGFillItem(LOTGFillData *data):LOTPaintDataItem(data->isStatic()),mData(data)
950 {
951 }
952
953 void LOTGFillItem::updateContent(int frameNo)
954 {
955     mData->update(mGradient, frameNo);
956     mGradient->mMatrix = mParentMatrix;
957     mFillRule = mData->fillRule();
958 }
959
960 void LOTGFillItem::updateRenderNode(LOTPathDataItem *pathNode, VDrawable *drawable, bool sameParent)
961 {
962     drawable->setBrush(VBrush(mGradient.get()));
963     drawable->setFillRule(mFillRule);
964 }
965
966 LOTStrokeItem::LOTStrokeItem(LOTStrokeData *data):LOTPaintDataItem(data->isStatic()),mData(data)
967 {
968     mDashArraySize = 0;
969 }
970
971 void LOTStrokeItem::updateContent(int frameNo)
972 {
973     LottieColor c = mData->mColor.value(frameNo);
974     float opacity = mData->opacity(frameNo);
975     mColor = c.toColor(opacity);
976     mCap = mData->capStyle();
977     mJoin = mData->joinStyle();
978     mMiterLimit = mData->meterLimit();
979     mWidth = mData->width(frameNo);
980     if (mData->hasDashInfo()) {
981         mDashArraySize = mData->getDashInfo(frameNo, mDashArray);
982     }
983 }
984
985 static float getScale(const VMatrix &matrix)
986 {
987     constexpr float SQRT_2 = 1.41421;
988     VPointF p1(0,0);
989     VPointF p2(SQRT_2,SQRT_2);
990     p1 = matrix.map(p1);
991     p2 = matrix.map(p2);
992     VPointF final = p2 - p1;
993
994     return std::sqrt( final.x() * final.x() + final.y() * final.y());
995 }
996
997 void LOTStrokeItem::updateRenderNode(LOTPathDataItem *pathNode, VDrawable *drawable, bool sameParent)
998 {
999     VColor color = mColor;
1000     if (sameParent)
1001       color.setAlpha(color.a * pathNode->combinedAlpha());
1002     else
1003       color.setAlpha(color.a  * parentAlpha() * pathNode->combinedAlpha());
1004
1005     VBrush brush(color);
1006     drawable->setBrush(brush);
1007
1008     drawable->setStrokeInfo(mCap, mJoin, mMiterLimit,  mWidth * getScale(mParentMatrix));
1009     if (mDashArraySize) {
1010         drawable->setDashInfo(mDashArray, mDashArraySize);
1011     }
1012 }
1013
1014 LOTGStrokeItem::LOTGStrokeItem(LOTGStrokeData *data):LOTPaintDataItem(data->isStatic()),mData(data)
1015 {
1016     mDashArraySize = 0;
1017 }
1018
1019 void LOTGStrokeItem::updateContent(int frameNo)
1020 {
1021     mData->update(mGradient, frameNo);
1022     mGradient->mMatrix = mParentMatrix;
1023     mCap = mData->capStyle();
1024     mJoin = mData->joinStyle();
1025     mMiterLimit = mData->meterLimit();
1026     mWidth = mData->width(frameNo);
1027     if (mData->hasDashInfo()) {
1028         mDashArraySize = mData->getDashInfo(frameNo, mDashArray);
1029     }
1030 }
1031
1032 void LOTGStrokeItem::updateRenderNode(LOTPathDataItem *pathNode, VDrawable *drawable, bool sameParent)
1033 {
1034     drawable->setBrush(VBrush(mGradient.get()));
1035     drawable->setStrokeInfo(mCap, mJoin, mMiterLimit,  mWidth * getScale(mParentMatrix));
1036     if (mDashArraySize) {
1037         drawable->setDashInfo(mDashArray, mDashArraySize);
1038     }
1039 }
1040
1041 LOTTrimItem::LOTTrimItem(LOTTrimData *data):mData(data)
1042 {
1043
1044 }
1045
1046 LOTRepeaterItem::LOTRepeaterItem(LOTRepeaterData *data):mData(data)
1047 {
1048
1049 }
1050
1051 void LOTRepeaterItem::update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag)
1052 {
1053
1054 }
1055
1056 void LOTRepeaterItem::renderList(std::vector<VDrawable *> &list)
1057 {
1058
1059 }
1060