lottie/optimization: update the layer item only when it has change.
[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     for (auto i = mLayers.rbegin(); i != mLayers.rend(); ++i) {
293        LOTLayerItem *layer = *i;
294        layer->render(&painter);
295     }
296
297 //    for(auto i : mRenderList) {
298 //        if (i->mType == LOTNode::TypeFill) {
299 //            if (i->mFlag & ChangeFlagPath) {
300 //                FTOutline *outline = VRaster::toFTOutline(i->mFinalPath);
301 //                i->mRleInfo = VRaster::instance().generateFillInfo(outline);
302 //                VRaster::deleteFTOutline(outline);
303 //                i->mFlag = ChangeFlagNone;
304 //            }
305 //            VBrush brush(i->mColor.r, i->mColor.g, i->mColor.b, i->mColor.a);
306 //            painter.setBrush(brush);
307 //            if (!i->mMaskRle.isEmpty()) {
308 //                VRle rle = i->mRleInfo + i->mMaskRle;
309 //                painter.drawRle(VPoint(), rle);
310 //            } else {
311 //                painter.drawRle(VPoint(), i->mRleInfo);
312 //            }
313 //        } else if(i->mType == LOTNode::TypeStroke) {
314 //            if (i->mFlag & ChangeFlagPath) {
315 //                VPath final = i->mFinalPath;
316 //                if (i->mStroke.dashArraySize) {
317 //                    VDasher dasher(i->mStroke.dashArray, i->mStroke.dashArraySize);
318 //                    final = dasher.dashed(i->mFinalPath);
319 //                }
320 //                FTOutline *outline = VRaster::toFTOutline(final);
321 //                i->mRleInfo = VRaster::instance().generateStrokeInfo(outline, CapStyle::Round, JoinStyle::Round, i->mStroke.width);
322 //                VRaster::deleteFTOutline(outline);
323 //                i->mFlag = ChangeFlagNone;
324 //            }
325 //            VBrush brush(i->mColor.r, i->mColor.g, i->mColor.b, i->mColor.a);
326 //            painter.setBrush(brush);
327 //            painter.drawRle(VPoint(), i->mRleInfo);
328 //        } else if (i->mType == LOTNode::Type::TypeGFill) {
329 //            if (i->mFlag & ChangeFlagPath) {
330 //                FTOutline *outline = VRaster::toFTOutline(i->mFinalPath);
331 //                i->mRleInfo = VRaster::instance().generateFillInfo(outline);
332 //                VRaster::deleteFTOutline(outline);
333 //                i->mFlag = ChangeFlagNone;
334 //            }
335 //            painter.setBrush(i->mBrush);
336 //            painter.drawRle(VPoint(), i->mRleInfo);
337
338 //        }
339 //    }
340     return true;
341 }
342
343 void LOTMaskItem::update(int frameNo, const VMatrix &parentMatrix,
344                          float parentAlpha, const DirtyFlag &flag)
345 {
346     if (mData->mShape.isStatic()) {
347         if (mLocalPath.isEmpty()) {
348             mLocalPath = mData->mShape.value(frameNo).toPath();
349         }
350     } else {
351         mLocalPath = mData->mShape.value(frameNo).toPath();
352     }
353     float opacity = mData->opacity(frameNo);
354     opacity = opacity * parentAlpha;
355
356     VPath path = mLocalPath;
357     path.transform(parentMatrix);
358
359     FTOutline *outline = VRaster::toFTOutline(path);
360     mRle = VRaster::instance().generateFillInfo(outline);
361     VRaster::deleteFTOutline(outline);
362
363     mRle = mRle * (opacity * 255);
364
365     if (mData->mInv) {
366         mRle = ~mRle;
367     }
368 }
369
370 void LOTLayerItem::render(VPainter *painter)
371 {
372     std::vector<VDrawable *> list;
373     renderList(list);
374     VRle mask = maskRle();
375     for(auto i : list) {
376         i->preprocess();
377         painter->setBrush(i->mBrush);
378         if (!mask.isEmpty()) {
379             VRle rle = i->mRle + mask;
380             painter->drawRle(VPoint(), rle);
381         } else {
382             painter->drawRle(VPoint(), i->mRle);
383         }
384     }
385 }
386
387 VRle LOTLayerItem::maskRle()
388 {
389     VRle rle;
390     for (auto &i : mMasks) {
391         switch (i->maskMode()) {
392             case LOTMaskData::Mode::Add: {
393                 rle = rle + i->mRle;
394                 break;
395             }
396             case LOTMaskData::Mode::Substarct: {
397                 rle = rle - i->mRle;
398                 break;
399             }
400             case LOTMaskData::Mode::Intersect: {
401                 rle = rle & i->mRle;
402                 break;
403             }
404             default:
405                 break;
406         }
407     }
408     return rle;
409 }
410
411 LOTLayerItem::LOTLayerItem(LOTLayerData *layerData):mLayerData(layerData),
412                                                     mParentLayer(nullptr),
413                                                     mPrecompLayer(nullptr),
414                                                     mFrameNo(-1),
415                                                     mDirtyFlag(DirtyFlagBit::All)
416 {
417     if (mLayerData->mHasMask) {
418         for (auto i : mLayerData->mMasks) {
419             mMasks.push_back(std::unique_ptr<LOTMaskItem>(new LOTMaskItem(i.get())));
420         }
421     }
422 }
423
424 void LOTLayerItem::updateStaticProperty()
425 {
426    if (mParentLayer)
427      mParentLayer->updateStaticProperty();
428
429    mStatic = mLayerData->isStatic();
430    mStatic = mParentLayer ? (mStatic & mParentLayer->isStatic()) : mStatic;
431    mStatic = mPrecompLayer ? (mStatic & mPrecompLayer->isStatic()) : mStatic;
432 }
433
434 void LOTLayerItem::update(int frameNo, const VMatrix &parentMatrix, float parentAlpha)
435 {
436    mFrameNo = frameNo;
437    // 1. check if the layer is part of the current frame
438    if (!visible()) return;
439
440    // 2. calculate the parent matrix and alpha
441    VMatrix m = matrix(frameNo) * parentMatrix;
442    float alpha = parentAlpha * opacity(frameNo);
443
444    //6. update the mask
445    if (hasMask()) {
446        for (auto &i : mMasks)
447            i->update(frameNo, m, alpha, mDirtyFlag);
448    }
449
450    // 3. update the dirty flag based on the change
451    if (!mCombinedMatrix.fuzzyCompare(m)) {
452        mDirtyFlag |= DirtyFlagBit::Matrix;
453    }
454    if (!vCompare(mCombinedAlpha, alpha)) {
455        mDirtyFlag |= DirtyFlagBit::Alpha;
456    }
457    mCombinedMatrix = m;
458    mCombinedAlpha = alpha;
459
460    // 4. if no parent property change and layer is static then nothing to do.
461    if ((flag() & DirtyFlagBit::None) && isStatic())
462       return;
463
464    //5. update the content of the layer
465    updateContent();
466
467    //6. reset the dirty flag
468    mDirtyFlag = DirtyFlagBit::None;
469 }
470
471 float
472 LOTLayerItem::opacity(int frameNo) const
473 {
474    return mLayerData->mTransform->opacity(frameNo);
475 }
476
477 VMatrix
478 LOTLayerItem::matrix(int frameNo) const
479 {
480     if (mParentLayer)
481         return mLayerData->mTransform->matrix(frameNo) * mParentLayer->matrix(frameNo);
482     else
483         return mLayerData->mTransform->matrix(frameNo);
484 }
485
486 bool LOTLayerItem::visible() const
487 {
488    if (frameNo() >= mLayerData->inFrame() && frameNo() < mLayerData->outFrame())
489       return true;
490    else
491       return false;
492 }
493
494
495
496 LOTCompLayerItem::LOTCompLayerItem(LOTLayerData *layerModel):LOTLayerItem(layerModel)
497 {
498    for(auto i : mLayerData->mChildren) {
499       LOTLayerData *layerModel = dynamic_cast<LOTLayerData *>(i.get());
500       if (layerModel) {
501          LOTLayerItem *layerItem = LOTCompItem::createLayerItem(layerModel);
502          if (layerItem) {
503             mLayers.push_back(layerItem);
504             mLayerMap[layerItem->id()] = layerItem;
505          }
506       }
507    }
508
509    //2. update parent layer
510    for(auto i : mLayers) {
511       int id = i->parentId();
512       if (id >=0) {
513          auto search = mLayerMap.find(id);
514          if (search != mLayerMap.end()) {
515            LOTLayerItem *parentLayer = search->second;
516            i->setParentLayer(parentLayer);
517          }
518       }
519       i->setPrecompLayer(this);
520    }
521 }
522
523 void LOTCompLayerItem::updateStaticProperty()
524 {
525     LOTLayerItem::updateStaticProperty();
526
527     for(auto i : mLayers) {
528        i->updateStaticProperty();
529     }
530 }
531
532 LOTCompLayerItem::~LOTCompLayerItem()
533 {
534     for(auto i : mLayers) {
535        delete i;
536     }
537 }
538
539 void LOTCompLayerItem::updateContent()
540 {
541     // update the layer from back to front
542     for (auto i = mLayers.rbegin(); i != mLayers.rend(); ++i) {
543        LOTLayerItem *layer = *i;
544        layer->update(frameNo(), combinedMatrix(), combinedAlpha());
545     }
546 }
547
548 void LOTCompLayerItem::renderList(std::vector<VDrawable *> &list)
549 {
550     if (!visible()) return;
551
552     // update the layer from back to front
553     for (auto i = mLayers.rbegin(); i != mLayers.rend(); ++i) {
554        LOTLayerItem *layer = *i;
555        layer->renderList(list);
556     }
557 }
558
559 LOTSolidLayerItem::LOTSolidLayerItem(LOTLayerData *layerData):LOTLayerItem(layerData)
560 {
561
562 }
563
564 void LOTSolidLayerItem::updateContent()
565 {
566    if (!mRenderNode) {
567       mRenderNode = std::unique_ptr<VDrawable>(new VDrawable());
568       mRenderNode->mType = VDrawable::Type::Fill;
569       mRenderNode->mFlag |= VDrawable::DirtyState::All;
570    }
571
572    if (flag() & DirtyFlagBit::Matrix) {
573        VPath path;
574        path.addRect(VRectF(0, 0, mLayerData->solidWidth(), mLayerData->solidHeight()));
575        path.transform(combinedMatrix());
576        mRenderNode->mFlag |= VDrawable::DirtyState::Path;
577        mRenderNode->mPath = path;
578    }
579    if (flag() & DirtyFlagBit::Alpha) {
580        LottieColor color = mLayerData->solidColor();
581        VBrush brush(color.toColor(combinedAlpha()));
582        mRenderNode->setBrush(brush);
583        mRenderNode->mFlag |= VDrawable::DirtyState::Brush;
584    }
585 }
586
587 void LOTSolidLayerItem::renderList(std::vector<VDrawable *> &list)
588 {
589     if (!visible()) return;
590
591     list.push_back(mRenderNode.get());
592 }
593
594 LOTNullLayerItem::LOTNullLayerItem(LOTLayerData *layerData):LOTLayerItem(layerData)
595 {
596
597 }
598 void LOTNullLayerItem::updateContent()
599 {
600
601 }
602
603
604 LOTShapeLayerItem::LOTShapeLayerItem(LOTLayerData *layerData):LOTLayerItem(layerData)
605 {
606     mRoot = new LOTContentGroupItem(nullptr);
607     mRoot->addChildren(layerData);
608     mRoot->processPaintOperation();
609 }
610
611 LOTShapeLayerItem::~LOTShapeLayerItem()
612 {
613     delete mRoot;
614 }
615
616 LOTContentItem * LOTShapeLayerItem::createContentItem(LOTData *contentData)
617 {
618     switch(contentData->type()) {
619         case LOTData::Type::ShapeGroup: {
620             return new LOTContentGroupItem(static_cast<LOTShapeGroupData *>(contentData));
621             break;
622         }
623         case LOTData::Type::Rect: {
624             return new LOTRectItem(static_cast<LOTRectData *>(contentData));
625             break;
626         }
627         case LOTData::Type::Ellipse: {
628             return new LOTEllipseItem(static_cast<LOTEllipseData *>(contentData));
629             break;
630         }
631         case LOTData::Type::Shape: {
632             return new LOTShapeItem(static_cast<LOTShapeData *>(contentData));
633             break;
634         }
635         case LOTData::Type::Polystar: {
636             return new LOTPolystarItem(static_cast<LOTPolystarData *>(contentData));
637             break;
638         }
639         case LOTData::Type::Fill: {
640             return new LOTFillItem(static_cast<LOTFillData *>(contentData));
641             break;
642         }
643         case LOTData::Type::GFill: {
644             return new LOTGFillItem(static_cast<LOTGFillData *>(contentData));
645             break;
646         }
647         case LOTData::Type::Stroke: {
648             return new LOTStrokeItem(static_cast<LOTStrokeData *>(contentData));
649             break;
650         }
651         case LOTData::Type::GStroke: {
652             return new LOTGStrokeItem(static_cast<LOTGStrokeData *>(contentData));
653             break;
654         }
655         case LOTData::Type::Repeater: {
656                 return new LOTRepeaterItem(static_cast<LOTRepeaterData *>(contentData));
657                 break;
658             }
659         default:
660             return nullptr;
661             break;
662     }
663 }
664
665 void LOTShapeLayerItem::updateContent()
666 {
667    mRoot->update(frameNo(), combinedMatrix(), combinedAlpha(), flag());
668 }
669
670 void LOTShapeLayerItem::renderList(std::vector<VDrawable *> &list)
671 {
672     if (!visible()) return;
673     mRoot->renderList(list);
674 }
675
676 VPath::Direction LOTContentItem::direction(bool isCW)
677 {
678    if (isCW)
679       return VPath::Direction::CW;
680    else
681       return VPath::Direction::CCW;
682 }
683
684
685 LOTContentGroupItem::LOTContentGroupItem(LOTShapeGroupData *data):mData(data)
686 {
687    addChildren(mData);
688 }
689
690 void LOTContentGroupItem::addChildren(LOTGroupData *data)
691 {
692    if (!data) return;
693
694    for(auto i : data->mChildren) {
695       LOTData *data = i.get();
696       LOTContentItem *content = LOTShapeLayerItem::createContentItem(data);
697       if (content)
698          mContents.push_back(content);
699    }
700 }
701
702 LOTContentGroupItem::~LOTContentGroupItem()
703 {
704     for(auto i : mContents) {
705         delete i;
706     }
707 }
708
709
710 void LOTContentGroupItem::update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag)
711 {
712    VMatrix m = parentMatrix;
713    float alpha = parentAlpha;
714    DirtyFlag newFlag = flag;
715
716    if (mData) {
717       // update the matrix and the flag
718       if ((flag & DirtyFlagBit::Matrix) || !mData->mTransform->staticMatrix() ) {
719          newFlag |= DirtyFlagBit::Matrix;
720       }
721       m = mData->mTransform->matrix(frameNo) * parentMatrix;
722       alpha *= mData->mTransform->opacity(frameNo);
723
724       if (!floatCmp(alpha, parentAlpha)) {
725          newFlag |= DirtyFlagBit::Alpha;
726       }
727    }
728
729    for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
730       (*i)->update(frameNo, m, alpha, newFlag);
731    }
732 }
733
734 void LOTContentGroupItem::renderList(std::vector<VDrawable *> &list)
735 {
736     for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
737        (*i)->renderList(list);
738     }
739 }
740
741 void LOTContentGroupItem::processPaintOperation()
742 {
743    std::vector<LOTPaintDataItem *> list;
744    paintOperationHelper(list);
745 }
746
747 void LOTContentGroupItem::paintOperationHelper(std::vector<LOTPaintDataItem *> &list)
748 {
749    int curOpCount = list.size();
750    for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
751       auto child = *i;
752       if (auto pathNode = dynamic_cast<LOTPathDataItem *>(child)) {
753          // the node is a path data node add the paint operation list to it.
754          pathNode->addPaintOperation(list, curOpCount);
755       } else if (auto paintNode = dynamic_cast<LOTPaintDataItem *>(child)) {
756          // add it to the paint operation list
757          list.push_back(paintNode);
758       } else if (auto groupNode = dynamic_cast<LOTContentGroupItem *>(child)) {
759          // update the groups node with current list
760          groupNode->paintOperationHelper(list);
761       }
762    }
763    list.erase(list.begin() + curOpCount, list.end());
764 }
765
766 void LOTPathDataItem::addPaintOperation(std::vector<LOTPaintDataItem *> &list, int externalCount)
767 {
768     for(auto paintItem : list) {
769       bool sameGroup = (externalCount-- > 0) ? false : true;
770       mNodeList.push_back(std::unique_ptr<VDrawable>(new VDrawable()));
771       mRenderList.push_back(LOTRenderNode(this, paintItem, mNodeList.back().get(), sameGroup));
772     }
773 }
774
775
776 void LOTPathDataItem::update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag)
777 {
778    mPathChanged = false;
779    mCombinedAlpha = parentAlpha;
780
781    // 1. update the local path if needed
782    if (!(mInit && mStaticPath)) {
783       mLocalPath = getPath(frameNo);
784       mInit = true;
785       mPathChanged = true;
786    }
787
788    // 2. apply path operation if needed
789    // TODO
790
791    // 3. compute the final path with parentMatrix
792    if ((flag & DirtyFlagBit::Matrix) || mPathChanged) {
793       mFinalPath = mLocalPath;
794       mFinalPath.transform(parentMatrix);
795       mPathChanged = true;
796    }
797
798    // 2. update the rendernode list
799    for (const auto &i : mRenderList) {
800       i.drawable->mFlag = VDrawable::DirtyState::None;
801       i.paintNodeRef->updateRenderNode(i.pathNodeRef, i.drawable, i.sameGroup);
802       if (mPathChanged) {
803           i.drawable->mPath = mFinalPath;
804           i.drawable->mFlag |= VDrawable::DirtyState::Path;
805       }
806    }
807 }
808
809 void LOTPathDataItem::renderList(std::vector<VDrawable *> &list)
810 {
811    for (const auto &i : mRenderList) {
812        list.push_back(i.drawable);
813    }
814 }
815
816 VPath LOTPathDataItem::path() const
817 {
818    return mFinalPath;
819 }
820
821
822 LOTRectItem::LOTRectItem(LOTRectData *data):LOTPathDataItem(data->isStatic()),mData(data)
823 {
824 }
825
826 VPath LOTRectItem::getPath(int frameNo)
827 {
828    VPointF pos = mData->mPos.value(frameNo);
829    VPointF size = mData->mSize.value(frameNo);
830    float radius = mData->mRound.value(frameNo);
831    VRectF r(pos.x() - size.x()/2, pos.y() - size.y()/2, size.x(), size.y());
832
833    VPath path;
834    path.addRoundRect(r, radius, radius, direction(mData->isDirectionCW()));
835
836    return path;
837 }
838
839 LOTEllipseItem::LOTEllipseItem(LOTEllipseData *data):LOTPathDataItem(data->isStatic()),mData(data)
840 {
841
842 }
843
844 VPath LOTEllipseItem::getPath(int frameNo)
845 {
846    VPointF pos = mData->mPos.value(frameNo);
847    VPointF size = mData->mSize.value(frameNo);
848    VRectF r(pos.x() - size.x()/2, pos.y() - size.y()/2, size.x(), size.y());
849
850    VPath path;
851    path.addOval(r, direction(mData->isDirectionCW()));
852
853    return path;
854 }
855
856 LOTShapeItem::LOTShapeItem(LOTShapeData *data):LOTPathDataItem(data->isStatic()),mData(data)
857 {
858
859 }
860
861 VPath LOTShapeItem::getPath(int frameNo)
862 {
863     LottieShapeData shapeData = mData->mShape.value(frameNo);
864
865     if (shapeData.mPoints.empty())
866      return VPath();
867
868     VPath path;
869
870     int size = shapeData.mPoints.size();
871     const VPointF *points = shapeData.mPoints.data();
872     path.moveTo(points[0]);
873     for (int i = 1 ; i < size; i+=3) {
874        path.cubicTo(points[i], points[i+1], points[i+2]);
875     }
876     if (shapeData.mClosed)
877       path.close();
878
879    return path;
880 }
881
882
883 LOTPolystarItem::LOTPolystarItem(LOTPolystarData *data):LOTPathDataItem(data->isStatic()),mData(data)
884 {
885
886 }
887
888 VPath LOTPolystarItem::getPath(int frameNo)
889 {
890    VPointF pos = mData->mPos.value(frameNo);
891    float points = mData->mPointCount.value(frameNo);
892    float innerRadius = mData->mInnerRadius.value(frameNo);
893    float outerRadius = mData->mOuterRadius.value(frameNo);
894    float innerRoundness = mData->mInnerRoundness.value(frameNo);
895    float outerRoundness = mData->mOuterRoundness.value(frameNo);
896    float rotation = mData->mRotation.value(frameNo);
897
898    VPath path;
899    VMatrix m;
900
901    if (mData->mType == LOTPolystarData::PolyType::Star) {
902         path.addPolystarStar(0.0, 0.0, 0.0, points,
903                              innerRadius, outerRadius,
904                              innerRoundness, outerRoundness,
905                              direction(mData->isDirectionCW()));
906    } else {
907         path.addPolystarPolygon(0.0, 0.0, 0.0, points,
908                                 outerRadius, outerRoundness,
909                                 direction(mData->isDirectionCW()));
910    }
911
912    m.translate(pos.x(), pos.y()).rotate(rotation);
913    m.rotate(rotation);
914    path.transform(m);
915
916    return path;
917 }
918
919
920
921 /*
922  * PaintData Node handling
923  *
924  */
925
926 void LOTPaintDataItem::update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag)
927 {
928    mContentChanged = false;
929    mParentAlpha = parentAlpha;
930    mParentMatrix = parentMatrix;
931    mFlag = flag;
932    mFrameNo = frameNo;
933    // 1. update the local content if needed
934   // if (!(mInit && mStaticContent)) {
935       mInit = true;
936       updateContent(frameNo);
937       mContentChanged = true;
938   // }
939 }
940
941
942 LOTFillItem::LOTFillItem(LOTFillData *data):LOTPaintDataItem(data->isStatic()),mData(data)
943 {
944 }
945
946 void LOTFillItem::updateContent(int frameNo)
947 {
948    LottieColor c = mData->mColor.value(frameNo);
949    float opacity = mData->opacity(frameNo);
950    mColor = c.toColor(opacity);
951    mFillRule = mData->fillRule();
952 }
953
954 void LOTFillItem::updateRenderNode(LOTPathDataItem *pathNode, VDrawable *drawable, bool sameParent)
955 {
956     VColor color = mColor;
957     if (sameParent)
958       color.setAlpha(color.a * pathNode->combinedAlpha());
959     else
960       color.setAlpha(color.a  * parentAlpha() * pathNode->combinedAlpha());
961     VBrush brush(color);
962     drawable->setBrush(brush);
963     drawable->setFillRule(mFillRule);
964 }
965
966
967 LOTGFillItem::LOTGFillItem(LOTGFillData *data):LOTPaintDataItem(data->isStatic()),mData(data)
968 {
969 }
970
971 void LOTGFillItem::updateContent(int frameNo)
972 {
973     mData->update(mGradient, frameNo);
974     mGradient->mMatrix = mParentMatrix;
975     mFillRule = mData->fillRule();
976 }
977
978 void LOTGFillItem::updateRenderNode(LOTPathDataItem *pathNode, VDrawable *drawable, bool sameParent)
979 {
980     drawable->setBrush(VBrush(mGradient.get()));
981     drawable->setFillRule(mFillRule);
982 }
983
984 LOTStrokeItem::LOTStrokeItem(LOTStrokeData *data):LOTPaintDataItem(data->isStatic()),mData(data)
985 {
986     mDashArraySize = 0;
987 }
988
989 void LOTStrokeItem::updateContent(int frameNo)
990 {
991     LottieColor c = mData->mColor.value(frameNo);
992     float opacity = mData->opacity(frameNo);
993     mColor = c.toColor(opacity);
994     mCap = mData->capStyle();
995     mJoin = mData->joinStyle();
996     mMiterLimit = mData->meterLimit();
997     mWidth = mData->width(frameNo);
998     if (mData->hasDashInfo()) {
999         mDashArraySize = mData->getDashInfo(frameNo, mDashArray);
1000     }
1001 }
1002
1003 static float getScale(const VMatrix &matrix)
1004 {
1005     constexpr float SQRT_2 = 1.41421;
1006     VPointF p1(0,0);
1007     VPointF p2(SQRT_2,SQRT_2);
1008     p1 = matrix.map(p1);
1009     p2 = matrix.map(p2);
1010     VPointF final = p2 - p1;
1011
1012     return std::sqrt( final.x() * final.x() + final.y() * final.y());
1013 }
1014
1015 void LOTStrokeItem::updateRenderNode(LOTPathDataItem *pathNode, VDrawable *drawable, bool sameParent)
1016 {
1017     VColor color = mColor;
1018     if (sameParent)
1019       color.setAlpha(color.a * pathNode->combinedAlpha());
1020     else
1021       color.setAlpha(color.a  * parentAlpha() * pathNode->combinedAlpha());
1022
1023     VBrush brush(color);
1024     drawable->setBrush(brush);
1025
1026     drawable->setStrokeInfo(mCap, mJoin, mMiterLimit,  mWidth * getScale(mParentMatrix));
1027     if (mDashArraySize) {
1028         drawable->setDashInfo(mDashArray, mDashArraySize);
1029     }
1030 }
1031
1032 LOTGStrokeItem::LOTGStrokeItem(LOTGStrokeData *data):LOTPaintDataItem(data->isStatic()),mData(data)
1033 {
1034     mDashArraySize = 0;
1035 }
1036
1037 void LOTGStrokeItem::updateContent(int frameNo)
1038 {
1039     mData->update(mGradient, frameNo);
1040     mGradient->mMatrix = mParentMatrix;
1041     mCap = mData->capStyle();
1042     mJoin = mData->joinStyle();
1043     mMiterLimit = mData->meterLimit();
1044     mWidth = mData->width(frameNo);
1045     if (mData->hasDashInfo()) {
1046         mDashArraySize = mData->getDashInfo(frameNo, mDashArray);
1047     }
1048 }
1049
1050 void LOTGStrokeItem::updateRenderNode(LOTPathDataItem *pathNode, VDrawable *drawable, bool sameParent)
1051 {
1052     drawable->setBrush(VBrush(mGradient.get()));
1053     drawable->setStrokeInfo(mCap, mJoin, mMiterLimit,  mWidth * getScale(mParentMatrix));
1054     if (mDashArraySize) {
1055         drawable->setDashInfo(mDashArray, mDashArraySize);
1056     }
1057 }
1058
1059 LOTTrimItem::LOTTrimItem(LOTTrimData *data):mData(data)
1060 {
1061
1062 }
1063
1064 LOTRepeaterItem::LOTRepeaterItem(LOTRepeaterData *data):mData(data)
1065 {
1066
1067 }
1068
1069 void LOTRepeaterItem::update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag)
1070 {
1071
1072 }
1073
1074 void LOTRepeaterItem::renderList(std::vector<VDrawable *> &list)
1075 {
1076
1077 }
1078