lottie: keep the content in back-tofron order in lottie scenegraph.
[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     // keep the content in back-to-front order.
561     std::reverse(mContents.begin(), mContents.end());
562 }
563
564 void LOTContentGroupItem::update(int frameNo, const VMatrix &parentMatrix,
565                                  float parentAlpha, const DirtyFlag &flag)
566 {
567     VMatrix   m = parentMatrix;
568     float     alpha = parentAlpha;
569     DirtyFlag newFlag = flag;
570
571     if (mData) {
572         // update the matrix and the flag
573         if ((flag & DirtyFlagBit::Matrix) ||
574             !mData->mTransform->staticMatrix()) {
575             newFlag |= DirtyFlagBit::Matrix;
576         }
577         m = mData->mTransform->matrix(frameNo);
578         m *= parentMatrix;
579         alpha *= mData->mTransform->opacity(frameNo);
580
581         if (!vCompare(alpha, parentAlpha)) {
582             newFlag |= DirtyFlagBit::Alpha;
583         }
584     }
585
586     mMatrix = m;
587
588     for (const auto &content : mContents) {
589         content->update(frameNo, m, alpha, newFlag);
590     }
591 }
592
593 void LOTContentGroupItem::applyTrim()
594 {
595     for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
596         auto content = (*i).get();
597         if (auto trim = dynamic_cast<LOTTrimItem *>(content)) {
598             trim->update();
599         } else if (auto group = dynamic_cast<LOTContentGroupItem *>(content)) {
600             group->applyTrim();
601         }
602     }
603 }
604
605 void LOTContentGroupItem::renderList(std::vector<VDrawable *> &list)
606 {
607     for (const auto &content : mContents) {
608         content->renderList(list);
609     }
610 }
611
612 void LOTContentGroupItem::processPaintItems(
613     std::vector<LOTPathDataItem *> &list)
614 {
615     int curOpCount = list.size();
616     for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
617         auto content = (*i).get();
618         if (auto pathNode = dynamic_cast<LOTPathDataItem *>(content)) {
619             // add it to the list
620             list.push_back(pathNode);
621         } else if (auto paintNode = dynamic_cast<LOTPaintDataItem *>(content)) {
622             // the node is a paint data node update the path list of the paint item.
623             paintNode->addPathItems(list, curOpCount);
624         } else if (auto groupNode =
625                        dynamic_cast<LOTContentGroupItem *>(content)) {
626             // update the groups node with current list
627             groupNode->processPaintItems(list);
628         }
629     }
630 }
631
632 void LOTContentGroupItem::processTrimItems(
633     std::vector<LOTPathDataItem *> &list)
634 {
635     int curOpCount = list.size();
636     for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
637         auto content = (*i).get();
638         if (auto pathNode = dynamic_cast<LOTPathDataItem *>(content)) {
639             // add it to the list
640             list.push_back(pathNode);
641         } else if (auto trimNode = dynamic_cast<LOTTrimItem *>(content)) {
642             // the node is a paint data node update the path list of the paint item.
643             trimNode->addPathItems(list, curOpCount);
644         } else if (auto groupNode =
645                        dynamic_cast<LOTContentGroupItem *>(content)) {
646             // update the groups node with current list
647             groupNode->processTrimItems(list);
648         }
649     }
650 }
651
652 void LOTPathDataItem::update(int frameNo, const VMatrix &,
653                              float, const DirtyFlag &flag)
654 {
655     mPathChanged = false;
656
657     // 1. update the local path if needed
658     if (hasChanged(frameNo)) {
659         updatePath(mLocalPath, frameNo);
660         mPathChanged = true;
661         mNeedUpdate = true;
662     }
663
664     mTemp = mLocalPath;
665
666     // 3. compute the final path with parentMatrix
667     if ((flag & DirtyFlagBit::Matrix) || mPathChanged) {
668         mPathChanged = true;
669     }
670 }
671
672 const VPath & LOTPathDataItem::finalPath()
673 {
674     if (mPathChanged || mNeedUpdate) {
675         mFinalPath.clone(mTemp);
676         mFinalPath.transform(static_cast<LOTContentGroupItem *>(parent())->matrix());
677         mNeedUpdate = false;
678     }
679     return mFinalPath;
680 }
681 LOTRectItem::LOTRectItem(LOTRectData *data)
682     : LOTPathDataItem(data->isStatic()), mData(data)
683 {
684 }
685
686 void LOTRectItem::updatePath(VPath& path, int frameNo)
687 {
688     VPointF pos = mData->mPos.value(frameNo);
689     VPointF size = mData->mSize.value(frameNo);
690     float   roundness = mData->mRound.value(frameNo);
691     VRectF  r(pos.x() - size.x() / 2, pos.y() - size.y() / 2, size.x(),
692              size.y());
693
694     path.reset();
695     path.addRoundRect(r, roundness, roundness, mData->direction());
696     updateCache(frameNo, pos, size, roundness);
697 }
698
699 LOTEllipseItem::LOTEllipseItem(LOTEllipseData *data)
700     : LOTPathDataItem(data->isStatic()), mData(data)
701 {
702 }
703
704 void LOTEllipseItem::updatePath(VPath& path, int frameNo)
705 {
706     VPointF pos = mData->mPos.value(frameNo);
707     VPointF size = mData->mSize.value(frameNo);
708     VRectF  r(pos.x() - size.x() / 2, pos.y() - size.y() / 2, size.x(),
709              size.y());
710
711     path.reset();
712     path.addOval(r, mData->direction());
713     updateCache(frameNo, pos, size);
714 }
715
716 LOTShapeItem::LOTShapeItem(LOTShapeData *data)
717     : LOTPathDataItem(data->isStatic()), mData(data)
718 {
719 }
720
721 void LOTShapeItem::updatePath(VPath& path, int frameNo)
722 {
723     mData->mShape.value(frameNo).toPath(path);
724 }
725
726 LOTPolystarItem::LOTPolystarItem(LOTPolystarData *data)
727     : LOTPathDataItem(data->isStatic()), mData(data)
728 {
729 }
730
731 void LOTPolystarItem::updatePath(VPath& path, int frameNo)
732 {
733     VPointF pos = mData->mPos.value(frameNo);
734     float   points = mData->mPointCount.value(frameNo);
735     float   innerRadius = mData->mInnerRadius.value(frameNo);
736     float   outerRadius = mData->mOuterRadius.value(frameNo);
737     float   innerRoundness = mData->mInnerRoundness.value(frameNo);
738     float   outerRoundness = mData->mOuterRoundness.value(frameNo);
739     float   rotation = mData->mRotation.value(frameNo);
740
741     path.reset();
742     VMatrix m;
743
744     if (mData->mType == LOTPolystarData::PolyType::Star) {
745         path.addPolystar(points, innerRadius, outerRadius, innerRoundness,
746                          outerRoundness, 0.0, 0.0, 0.0, mData->direction());
747     } else {
748         path.addPolygon(points, outerRadius, outerRoundness, 0.0, 0.0, 0.0,
749                         mData->direction());
750     }
751
752     m.translate(pos.x(), pos.y()).rotate(rotation);
753     m.rotate(rotation);
754     path.transform(m);
755     updateCache(frameNo, pos, points, innerRadius, outerRadius,
756                 innerRoundness, outerRoundness, rotation);
757 }
758
759 /*
760  * PaintData Node handling
761  *
762  */
763 LOTPaintDataItem::LOTPaintDataItem(bool staticContent):mDrawable(std::make_unique<LOTDrawable>()),
764                                                        mStaticContent(staticContent){}
765
766 void LOTPaintDataItem::update(int frameNo, const VMatrix &parentMatrix,
767                               float parentAlpha, const DirtyFlag &flag)
768 {
769     mRenderNodeUpdate = true;
770     mParentAlpha = parentAlpha;
771     mFlag = flag;
772     mFrameNo = frameNo;
773
774     updateContent(frameNo);
775 }
776
777 void LOTPaintDataItem::updateRenderNode()
778 {
779     bool dirty = false;
780     for (auto &i : mPathItems) {
781         if (i->dirty()) {
782             dirty = true;
783             break;
784         }
785     }
786
787     if (dirty) {
788         mPath.reset();
789
790         for (auto &i : mPathItems) {
791             mPath.addPath(i->finalPath());
792         }
793         mDrawable->setPath(mPath);
794     } else {
795         if (mDrawable->mFlag & VDrawable::DirtyState::Path)
796             mDrawable->mPath = mPath;
797     }
798 }
799
800 void LOTPaintDataItem::renderList(std::vector<VDrawable *> &list)
801 {
802     if (mRenderNodeUpdate) {
803         updateRenderNode();
804         LOTPaintDataItem::updateRenderNode();
805         mRenderNodeUpdate = false;
806     }
807     list.push_back(mDrawable.get());
808 }
809
810
811 void LOTPaintDataItem::addPathItems(std::vector<LOTPathDataItem *> &list, int startOffset)
812 {
813     std::copy(list.begin() + startOffset, list.end(), back_inserter(mPathItems));
814 }
815
816
817 LOTFillItem::LOTFillItem(LOTFillData *data)
818     : LOTPaintDataItem(data->isStatic()), mData(data)
819 {
820 }
821
822 void LOTFillItem::updateContent(int frameNo)
823 {
824     LottieColor c = mData->mColor.value(frameNo);
825     float       opacity = mData->opacity(frameNo);
826     mColor = c.toColor(opacity);
827     mFillRule = mData->fillRule();
828 }
829
830 void LOTFillItem::updateRenderNode()
831 {
832     VColor color = mColor;
833
834     color.setAlpha(color.a * parentAlpha());
835     VBrush brush(color);
836     mDrawable->setBrush(brush);
837     mDrawable->setFillRule(mFillRule);
838 }
839
840 LOTGFillItem::LOTGFillItem(LOTGFillData *data)
841     : LOTPaintDataItem(data->isStatic()), mData(data)
842 {
843 }
844
845 void LOTGFillItem::updateContent(int frameNo)
846 {
847     mData->update(mGradient, frameNo);
848     mGradient->mMatrix = static_cast<LOTContentGroupItem *>(parent())->matrix();
849     mFillRule = mData->fillRule();
850 }
851
852 void LOTGFillItem::updateRenderNode()
853 {
854     mDrawable->setBrush(VBrush(mGradient.get()));
855     mDrawable->setFillRule(mFillRule);
856 }
857
858 LOTStrokeItem::LOTStrokeItem(LOTStrokeData *data)
859     : LOTPaintDataItem(data->isStatic()), mData(data)
860 {
861     mDashArraySize = 0;
862 }
863
864 void LOTStrokeItem::updateContent(int frameNo)
865 {
866     LottieColor c = mData->mColor.value(frameNo);
867     float       opacity = mData->opacity(frameNo);
868     mColor = c.toColor(opacity);
869     mCap = mData->capStyle();
870     mJoin = mData->joinStyle();
871     mMiterLimit = mData->meterLimit();
872     mWidth = mData->width(frameNo);
873     if (mData->hasDashInfo()) {
874         mDashArraySize = mData->getDashInfo(frameNo, mDashArray);
875     }
876 }
877
878 static float getScale(const VMatrix &matrix)
879 {
880     constexpr float SQRT_2 = 1.41421;
881     VPointF         p1(0, 0);
882     VPointF         p2(SQRT_2, SQRT_2);
883     p1 = matrix.map(p1);
884     p2 = matrix.map(p2);
885     VPointF final = p2 - p1;
886
887     return std::sqrt(final.x() * final.x() + final.y() * final.y()) / 2.0;
888 }
889
890 void LOTStrokeItem::updateRenderNode()
891 {
892     VColor color = mColor;
893
894     color.setAlpha(color.a * parentAlpha());
895     VBrush brush(color);
896     mDrawable->setBrush(brush);
897     float scale = getScale(static_cast<LOTContentGroupItem *>(parent())->matrix());
898     mDrawable->setStrokeInfo(mCap, mJoin, mMiterLimit,
899                             mWidth * scale);
900     if (mDashArraySize) {
901         for (int i = 0 ; i < mDashArraySize ; i++)
902             mDashArray[i] *= scale;
903         mDrawable->setDashInfo(mDashArray, mDashArraySize);
904     }
905 }
906
907 LOTGStrokeItem::LOTGStrokeItem(LOTGStrokeData *data)
908     : LOTPaintDataItem(data->isStatic()), mData(data)
909 {
910     mDashArraySize = 0;
911 }
912
913 void LOTGStrokeItem::updateContent(int frameNo)
914 {
915     mData->update(mGradient, frameNo);
916     mGradient->mMatrix = static_cast<LOTContentGroupItem *>(parent())->matrix();
917     mCap = mData->capStyle();
918     mJoin = mData->joinStyle();
919     mMiterLimit = mData->meterLimit();
920     mWidth = mData->width(frameNo);
921     if (mData->hasDashInfo()) {
922         mDashArraySize = mData->getDashInfo(frameNo, mDashArray);
923     }
924 }
925
926 void LOTGStrokeItem::updateRenderNode()
927 {
928     float scale = getScale(mGradient->mMatrix);
929     mDrawable->setBrush(VBrush(mGradient.get()));
930     mDrawable->setStrokeInfo(mCap, mJoin, mMiterLimit,
931                             mWidth * scale);
932     if (mDashArraySize) {
933         for (int i = 0 ; i < mDashArraySize ; i++)
934             mDashArray[i] *= scale;
935         mDrawable->setDashInfo(mDashArray, mDashArraySize);
936     }
937 }
938
939 LOTTrimItem::LOTTrimItem(LOTTrimData *data) : mData(data) {}
940
941 void LOTTrimItem::update(int frameNo, const VMatrix &/*parentMatrix*/,
942                          float /*parentAlpha*/, const DirtyFlag &/*flag*/)
943 {
944     mDirty = false;
945
946     if (mCache.mFrameNo == frameNo) return;
947
948     float   start = mData->start(frameNo);
949     float   end = mData->end(frameNo);
950     float   offset = mData->offset(frameNo);
951
952     if (!(vCompare(mCache.mStart, start) && vCompare(mCache.mEnd, end) &&
953           vCompare(mCache.mOffset, offset))) {
954         mDirty = true;
955         mCache.mStart = start;
956         mCache.mEnd = end;
957         mCache.mOffset = offset;
958     }
959     mCache.mFrameNo = frameNo;
960 }
961
962 void LOTTrimItem::update()
963 {
964     // when both path and trim are not dirty
965     if (!(mDirty || pathDirty())) return;
966
967     //@TODO take the offset and trim type into account.
968     for (auto &i : mPathItems) {
969         VPathMesure pm;
970         pm.setStart(mCache.mStart);
971         pm.setEnd(mCache.mEnd);
972         pm.setOffset(mCache.mOffset);
973         i->updatePath(pm.trim(i->localPath()));
974     }
975 }
976
977
978 void LOTTrimItem::addPathItems(std::vector<LOTPathDataItem *> &list, int startOffset)
979 {
980     std::copy(list.begin() + startOffset, list.end(), back_inserter(mPathItems));
981 }
982
983
984 LOTRepeaterItem::LOTRepeaterItem(LOTRepeaterData *data) : mData(data) {}
985
986 void LOTRepeaterItem::update(int /*frameNo*/, const VMatrix &/*parentMatrix*/,
987                              float /*parentAlpha*/, const DirtyFlag &/*flag*/)
988 {
989 }
990
991 void LOTRepeaterItem::renderList(std::vector<VDrawable *> &/*list*/) {}
992
993 void LOTDrawable::sync()
994 {
995     mCNode.mFlag = ChangeFlagNone;
996     if (mFlag & DirtyState::None) return;
997
998     if (mFlag & DirtyState::Path) {
999         const std::vector<VPath::Element> &elm = mPath.elements();
1000         const std::vector<VPointF> &       pts = mPath.points();
1001         const float *ptPtr = reinterpret_cast<const float *>(pts.data());
1002         const char * elmPtr = reinterpret_cast<const char *>(elm.data());
1003         mCNode.mPath.elmPtr = elmPtr;
1004         mCNode.mPath.elmCount = elm.size();
1005         mCNode.mPath.ptPtr = ptPtr;
1006         mCNode.mPath.ptCount = 2 * pts.size();
1007         mCNode.mFlag |= ChangeFlagPath;
1008     }
1009
1010     if (mStroke.enable) {
1011         mCNode.mStroke.width = mStroke.width;
1012         mCNode.mStroke.meterLimit = mStroke.meterLimit;
1013         mCNode.mStroke.enable = 1;
1014
1015         switch (mFillRule) {
1016         case FillRule::EvenOdd:
1017             mCNode.mFillRule = LOTFillRule::FillEvenOdd;
1018             break;
1019         default:
1020             mCNode.mFillRule = LOTFillRule::FillWinding;
1021             break;
1022         }
1023
1024         switch (mStroke.cap) {
1025         case CapStyle::Flat:
1026             mCNode.mStroke.cap = LOTCapStyle::CapFlat;
1027             break;
1028         case CapStyle::Square:
1029             mCNode.mStroke.cap = LOTCapStyle::CapSquare;
1030             break;
1031         case CapStyle::Round:
1032             mCNode.mStroke.cap = LOTCapStyle::CapRound;
1033             break;
1034         default:
1035             mCNode.mStroke.cap = LOTCapStyle::CapFlat;
1036             break;
1037         }
1038
1039         switch (mStroke.join) {
1040         case JoinStyle::Miter:
1041             mCNode.mStroke.join = LOTJoinStyle::JoinMiter;
1042             break;
1043         case JoinStyle::Bevel:
1044             mCNode.mStroke.join = LOTJoinStyle::JoinBevel;
1045             break;
1046         case JoinStyle::Round:
1047             mCNode.mStroke.join = LOTJoinStyle::JoinRound;
1048             break;
1049         default:
1050             mCNode.mStroke.join = LOTJoinStyle::JoinMiter;
1051             break;
1052         }
1053
1054         mCNode.mStroke.dashArray = mStroke.mDash.data();
1055         mCNode.mStroke.dashArraySize = mStroke.mDash.size();
1056
1057     } else {
1058         mCNode.mStroke.enable = 0;
1059     }
1060
1061     switch (mBrush.type()) {
1062     case VBrush::Type::Solid:
1063         mCNode.mType = LOTBrushType::BrushSolid;
1064         mCNode.mColor.r = mBrush.mColor.r;
1065         mCNode.mColor.g = mBrush.mColor.g;
1066         mCNode.mColor.b = mBrush.mColor.b;
1067         mCNode.mColor.a = mBrush.mColor.a;
1068         break;
1069     case VBrush::Type::LinearGradient:
1070         mCNode.mType = LOTBrushType::BrushGradient;
1071         mCNode.mGradient.type = LOTGradientType::GradientLinear;
1072         mCNode.mGradient.start.x = mBrush.mGradient->linear.x1;
1073         mCNode.mGradient.start.y = mBrush.mGradient->linear.y1;
1074         mCNode.mGradient.end.x = mBrush.mGradient->linear.x2;
1075         mCNode.mGradient.end.y = mBrush.mGradient->linear.y2;
1076         break;
1077     case VBrush::Type::RadialGradient:
1078         mCNode.mType = LOTBrushType::BrushGradient;
1079         mCNode.mGradient.type = LOTGradientType::GradientRadial;
1080         mCNode.mGradient.center.x = mBrush.mGradient->radial.cx;
1081         mCNode.mGradient.center.y = mBrush.mGradient->radial.cy;
1082         mCNode.mGradient.focal.x = mBrush.mGradient->radial.fx;
1083         mCNode.mGradient.focal.y = mBrush.mGradient->radial.fy;
1084         mCNode.mGradient.cradius = mBrush.mGradient->radial.cradius;
1085         mCNode.mGradient.fradius = mBrush.mGradient->radial.fradius;
1086         break;
1087     default:
1088         break;
1089     }
1090 }