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