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