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