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