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