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