lottie/vector: refactored VRle class to use cow_ptr.
[platform/core/uifw/lottie-player.git] / src / lottie / lottieitem.cpp
1 #include "lottieitem.h"
2 #include <cmath>
3 #include "vbitmap.h"
4 #include "vdasher.h"
5 #include "vpainter.h"
6 #include "vraster.h"
7
8 /* Lottie Layer Rules
9  * 1. time stretch is pre calculated and applied to all the properties of the
10  * lottilayer model and all its children
11  * 2. The frame property could be reversed using,time-reverse layer property in
12  * AE. which means (start frame > endFrame) 3.
13  */
14
15 LOTCompItem::LOTCompItem(LOTModel *model)
16     : mRootModel(model), mUpdateViewBox(false), mCurFrameNo(-1)
17 {
18     // 1. build layer item list
19     mCompData = model->mRoot.get();
20     for (auto &i : mCompData->mChildren) {
21         LOTLayerData *layerData = dynamic_cast<LOTLayerData *>(i.get());
22         if (layerData) {
23             LOTLayerItem *layerItem = LOTCompItem::createLayerItem(layerData);
24             if (layerItem) {
25                 mLayers.push_back(layerItem);
26                 mLayerMap[layerItem->id()] = layerItem;
27             }
28         }
29     }
30
31     // 2. update parent layer
32     for (auto &i : mLayers) {
33         int id = i->parentId();
34         if (id >= 0) {
35             auto search = mLayerMap.find(id);
36             if (search != mLayerMap.end()) {
37                 LOTLayerItem *parentLayer = search->second;
38                 i->setParentLayer(parentLayer);
39             }
40         }
41     }
42     // 3. update static property of each layer
43     for (auto &i : mLayers) {
44         i->updateStaticProperty();
45     }
46
47     mViewSize = mCompData->size();
48 }
49
50 LOTCompItem::~LOTCompItem()
51 {
52     for (auto &i : mLayers) {
53         delete i;
54     }
55 }
56
57 LOTLayerItem *LOTCompItem::createLayerItem(LOTLayerData *layerData)
58 {
59     switch (layerData->mLayerType) {
60     case LayerType::Precomp: {
61         return new LOTCompLayerItem(layerData);
62         break;
63     }
64     case LayerType::Solid: {
65         return new LOTSolidLayerItem(layerData);
66         break;
67     }
68     case LayerType::Shape: {
69         return new LOTShapeLayerItem(layerData);
70         break;
71     }
72     case LayerType::Null: {
73         return new LOTNullLayerItem(layerData);
74         break;
75     }
76     default:
77         return nullptr;
78         break;
79     }
80 }
81
82 void LOTCompItem::resize(const VSize &size)
83 {
84     if (mViewSize == size) return;
85     mViewSize = size;
86     mUpdateViewBox = true;
87 }
88
89 VSize LOTCompItem::size() const
90 {
91     return mViewSize;
92 }
93
94 bool LOTCompItem::update(int frameNo)
95 {
96     // check if cached frame is same as requested frame.
97     if (!mUpdateViewBox && (mCurFrameNo == frameNo)) return false;
98
99     /*
100      * if viewbox dosen't scale exactly to the viewport
101      * we scale the viewbox keeping AspectRatioPreserved and then align the
102      * viewbox to the viewport using AlignCenter rule.
103      */
104     VSize viewPort = mViewSize;
105     VSize viewBox = mCompData->size();
106
107     float sx = float(viewPort.width()) / viewBox.width();
108     float sy = float(viewPort.height()) / viewBox.height();
109     float scale = fmin(sx, sy);
110     float tx = (viewPort.width() - viewBox.width() * scale) * 0.5;
111     float ty = (viewPort.height() - viewBox.height() * scale) * 0.5;
112
113     VMatrix m;
114     m.scale(scale, scale).translate(tx, ty);
115
116     // update the layer from back to front
117     for (auto i = mLayers.rbegin(); i != mLayers.rend(); ++i) {
118         LOTLayerItem *layer = *i;
119         layer->update(frameNo, m, 1.0);
120     }
121     buildRenderList();
122     mCurFrameNo = frameNo;
123     mUpdateViewBox = false;
124     return true;
125 }
126
127 void LOTCompItem::buildRenderList()
128 {
129     mDrawableList.clear();
130     for (auto i = mLayers.rbegin(); i != mLayers.rend(); ++i) {
131         LOTLayerItem *layer = *i;
132         layer->renderList(mDrawableList);
133     }
134
135     mRenderList.clear();
136     for (auto &i : mDrawableList) {
137         LOTDrawable *lotDrawable = static_cast<LOTDrawable *>(i);
138         lotDrawable->sync();
139         mRenderList.push_back(&lotDrawable->mCNode);
140     }
141 }
142
143 const std::vector<LOTNode *> &LOTCompItem::renderList() const
144 {
145     return mRenderList;
146 }
147
148 bool LOTCompItem::render(const LOTBuffer &buffer)
149 {
150     VBitmap bitmap((uchar *)buffer.buffer, buffer.width, buffer.height,
151                    buffer.bytesPerLine, VBitmap::Format::ARGB32_Premultiplied,
152                    nullptr, nullptr);
153
154     /* schedule all preprocess task for this frame at once.
155      */
156     for (auto &e : mDrawableList) {
157         e->preprocess();
158     }
159
160     VPainter painter(&bitmap);
161     VRle     mask;
162     for (auto i = mLayers.rbegin(); i != mLayers.rend(); ++i) {
163         LOTLayerItem *layer = *i;
164         layer->render(&painter, mask);
165     }
166
167     return true;
168 }
169
170 void LOTMaskItem::update(int frameNo, const VMatrix &parentMatrix,
171                          float parentAlpha, const DirtyFlag &flag)
172 {
173     if (mData->mShape.isStatic()) {
174         if (mLocalPath.isEmpty()) {
175             mData->mShape.value(frameNo).toPath(mLocalPath);
176         }
177     } else {
178         mData->mShape.value(frameNo).toPath(mLocalPath);
179     }
180     float opacity = mData->opacity(frameNo);
181     opacity = opacity * parentAlpha;
182     mCombinedAlpha = opacity;
183
184     VPath path = mLocalPath;
185     path.transform(parentMatrix);
186
187     mRleTask = VRaster::instance().generateFillInfo(std::move(path), std::move(mRle));
188     mRle = VRle();
189 }
190
191 VRle LOTMaskItem::rle()
192 {
193     if (mRleTask.valid()) {
194         mRle = mRleTask.get();
195         if (!vCompare(mCombinedAlpha, 1.0f))
196             mRle *= (mCombinedAlpha * 255);
197         if (mData->mInv) mRle.invert();
198     }
199     return mRle;
200 }
201
202 void LOTLayerItem::render(VPainter *painter, const VRle &inheritMask)
203 {
204     std::vector<VDrawable *> list;
205     renderList(list);
206     VRle mask = inheritMask;
207     if (hasMask()) {
208         if (mask.isEmpty())
209             mask = maskRle(painter->clipBoundingRect());
210         else
211             mask = mask & inheritMask;
212     }
213
214     for (auto &i : list) {
215         painter->setBrush(i->mBrush);
216         if (!mask.isEmpty()) {
217             VRle rle = i->rle() & mask;
218             painter->drawRle(VPoint(), rle);
219         } else {
220             painter->drawRle(VPoint(), i->rle());
221         }
222     }
223 }
224
225 VRle LOTLayerItem::maskRle(const VRect &clipRect)
226 {
227     VRle rle;
228     for (auto &i : mMasks) {
229         switch (i->maskMode()) {
230         case LOTMaskData::Mode::Add: {
231             rle = rle + i->rle();
232             break;
233         }
234         case LOTMaskData::Mode::Substarct: {
235             if (rle.isEmpty() && !clipRect.isEmpty())
236                 rle = VRle::toRle(clipRect);
237             rle = rle - i->rle();
238             break;
239         }
240         case LOTMaskData::Mode::Intersect: {
241             rle = rle & i->rle();
242             break;
243         }
244         default:
245             break;
246         }
247     }
248     return rle;
249 }
250
251 LOTLayerItem::LOTLayerItem(LOTLayerData *layerData)
252     : mLayerData(layerData),
253       mParentLayer(nullptr),
254       mPrecompLayer(nullptr),
255       mCombinedAlpha(0.0f),
256       mFrameNo(-1),
257       mDirtyFlag(DirtyFlagBit::All)
258 {
259     if (mLayerData->mHasMask) {
260         for (auto &i : mLayerData->mMasks) {
261             mMasks.push_back(std::make_unique<LOTMaskItem>(i.get()));
262         }
263     }
264 }
265
266 void LOTLayerItem::updateStaticProperty()
267 {
268     if (mParentLayer) mParentLayer->updateStaticProperty();
269
270     mStatic = mLayerData->isStatic();
271     mStatic = mParentLayer ? (mStatic & mParentLayer->isStatic()) : mStatic;
272     mStatic = mPrecompLayer ? (mStatic & mPrecompLayer->isStatic()) : mStatic;
273 }
274
275 void LOTLayerItem::update(int frameNo, const VMatrix &parentMatrix,
276                           float parentAlpha)
277 {
278     mFrameNo = frameNo;
279     // 1. check if the layer is part of the current frame
280     if (!visible()) return;
281
282     // 2. calculate the parent matrix and alpha
283     VMatrix m = matrix(frameNo);
284     m *= parentMatrix;
285     float alpha = parentAlpha * opacity(frameNo);
286
287     // 6. update the mask
288     if (hasMask()) {
289         for (auto &i : mMasks) i->update(frameNo, m, alpha, mDirtyFlag);
290     }
291
292     // 3. update the dirty flag based on the change
293     if (!mCombinedMatrix.fuzzyCompare(m)) {
294         mDirtyFlag |= DirtyFlagBit::Matrix;
295     }
296     if (!vCompare(mCombinedAlpha, alpha)) {
297         mDirtyFlag |= DirtyFlagBit::Alpha;
298     }
299     mCombinedMatrix = m;
300     mCombinedAlpha = alpha;
301
302     // 4. if no parent property change and layer is static then nothing to do.
303     if ((flag() & DirtyFlagBit::None) && isStatic()) return;
304
305     // 5. update the content of the layer
306     updateContent();
307
308     // 6. reset the dirty flag
309     mDirtyFlag = DirtyFlagBit::None;
310 }
311
312 float LOTLayerItem::opacity(int frameNo) const
313 {
314     return mLayerData->mTransform->opacity(frameNo);
315 }
316
317 VMatrix LOTLayerItem::matrix(int frameNo) const
318 {
319     if (mParentLayer)
320         return mLayerData->mTransform->matrix(frameNo) *
321                mParentLayer->matrix(frameNo);
322     else
323         return mLayerData->mTransform->matrix(frameNo);
324 }
325
326 bool LOTLayerItem::visible() const
327 {
328     if (frameNo() >= mLayerData->inFrame() &&
329         frameNo() < mLayerData->outFrame())
330         return true;
331     else
332         return false;
333 }
334
335 LOTCompLayerItem::LOTCompLayerItem(LOTLayerData *layerModel)
336     : LOTLayerItem(layerModel)
337 {
338     for (auto &i : mLayerData->mChildren) {
339         LOTLayerData *layerModel = dynamic_cast<LOTLayerData *>(i.get());
340         if (layerModel) {
341             LOTLayerItem *layerItem = LOTCompItem::createLayerItem(layerModel);
342             if (layerItem) {
343                 mLayers.push_back(layerItem);
344                 mLayerMap[layerItem->id()] = layerItem;
345             }
346         }
347     }
348
349     // 2. update parent layer
350     for (auto &i : mLayers) {
351         int id = i->parentId();
352         if (id >= 0) {
353             auto search = mLayerMap.find(id);
354             if (search != mLayerMap.end()) {
355                 LOTLayerItem *parentLayer = search->second;
356                 i->setParentLayer(parentLayer);
357             }
358         }
359         i->setPrecompLayer(this);
360     }
361 }
362
363 void LOTCompLayerItem::updateStaticProperty()
364 {
365     LOTLayerItem::updateStaticProperty();
366
367     for (auto &i : mLayers) {
368         i->updateStaticProperty();
369     }
370 }
371
372 void LOTCompLayerItem::render(VPainter *painter, const VRle &inheritMask)
373 {
374     VRle mask = inheritMask;
375
376     if (hasMask()) {
377         if (mask.isEmpty())
378             mask = maskRle(painter->clipBoundingRect());
379         else
380             mask = mask & inheritMask;
381     }
382
383     for (auto &i : mLayers) {
384         i->render(painter, mask);
385     }
386 }
387
388 LOTCompLayerItem::~LOTCompLayerItem()
389 {
390     for (auto &i : mLayers) {
391         delete i;
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         LOTLayerItem *layer = *i;
400         layer->update(frameNo(), combinedMatrix(), combinedAlpha());
401     }
402 }
403
404 void LOTCompLayerItem::renderList(std::vector<VDrawable *> &list)
405 {
406     if (!visible()) return;
407
408     // update the layer from back to front
409     for (auto i = mLayers.rbegin(); i != mLayers.rend(); ++i) {
410         LOTLayerItem *layer = *i;
411         layer->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
914     drawable->setStrokeInfo(mCap, mJoin, mMiterLimit,
915                             mWidth * getScale(mParentMatrix));
916     if (mDashArraySize) {
917         drawable->setDashInfo(mDashArray, mDashArraySize);
918     }
919 }
920
921 LOTGStrokeItem::LOTGStrokeItem(LOTGStrokeData *data)
922     : LOTPaintDataItem(data->isStatic()), mData(data)
923 {
924     mDashArraySize = 0;
925 }
926
927 void LOTGStrokeItem::updateContent(int frameNo)
928 {
929     mData->update(mGradient, frameNo);
930     mGradient->mMatrix = mParentMatrix;
931     mCap = mData->capStyle();
932     mJoin = mData->joinStyle();
933     mMiterLimit = mData->meterLimit();
934     mWidth = mData->width(frameNo);
935     if (mData->hasDashInfo()) {
936         mDashArraySize = mData->getDashInfo(frameNo, mDashArray);
937     }
938 }
939
940 void LOTGStrokeItem::updateRenderNode(LOTPathDataItem *pathNode,
941                                       VDrawable *drawable, bool sameParent)
942 {
943     drawable->setBrush(VBrush(mGradient.get()));
944     drawable->setStrokeInfo(mCap, mJoin, mMiterLimit,
945                             mWidth * getScale(mParentMatrix));
946     if (mDashArraySize) {
947         drawable->setDashInfo(mDashArray, mDashArraySize);
948     }
949 }
950
951 LOTTrimItem::LOTTrimItem(LOTTrimData *data) : mData(data) {}
952
953 void LOTTrimItem::update(int frameNo, const VMatrix &parentMatrix,
954                          float parentAlpha, const DirtyFlag &flag)
955 {
956 }
957
958 LOTRepeaterItem::LOTRepeaterItem(LOTRepeaterData *data) : mData(data) {}
959
960 void LOTRepeaterItem::update(int frameNo, const VMatrix &parentMatrix,
961                              float parentAlpha, const DirtyFlag &flag)
962 {
963 }
964
965 void LOTRepeaterItem::renderList(std::vector<VDrawable *> &list) {}
966
967 void LOTDrawable::sync()
968 {
969     mCNode.mFlag = ChangeFlagNone;
970     if (mFlag & DirtyState::None) return;
971
972     if (mFlag & DirtyState::Path) {
973         const std::vector<VPath::Element> &elm = mPath.elements();
974         const std::vector<VPointF> &       pts = mPath.points();
975         const float *ptPtr = reinterpret_cast<const float *>(pts.data());
976         const char * elmPtr = reinterpret_cast<const char *>(elm.data());
977         mCNode.mPath.elmPtr = elmPtr;
978         mCNode.mPath.elmCount = elm.size();
979         mCNode.mPath.ptPtr = ptPtr;
980         mCNode.mPath.ptCount = 2 * pts.size();
981         mCNode.mFlag |= ChangeFlagPath;
982     }
983
984     if (mStroke.enable) {
985         mCNode.mStroke.width = mStroke.width;
986         mCNode.mStroke.meterLimit = mStroke.meterLimit;
987         mCNode.mStroke.enable = 1;
988
989         switch (mFillRule) {
990         case FillRule::EvenOdd:
991             mCNode.mFillRule = LOTNode::EvenOdd;
992             break;
993         default:
994             mCNode.mFillRule = LOTNode::Winding;
995             break;
996         }
997
998         switch (mStroke.cap) {
999         case CapStyle::Flat:
1000             mCNode.mStroke.cap = LOTNode::FlatCap;
1001             break;
1002         case CapStyle::Square:
1003             mCNode.mStroke.cap = LOTNode::SquareCap;
1004             break;
1005         case CapStyle::Round:
1006             mCNode.mStroke.cap = LOTNode::RoundCap;
1007             break;
1008         default:
1009             mCNode.mStroke.cap = LOTNode::FlatCap;
1010             break;
1011         }
1012
1013         switch (mStroke.join) {
1014         case JoinStyle::Miter:
1015             mCNode.mStroke.join = LOTNode::MiterJoin;
1016             break;
1017         case JoinStyle::Bevel:
1018             mCNode.mStroke.join = LOTNode::BevelJoin;
1019             break;
1020         case JoinStyle::Round:
1021             mCNode.mStroke.join = LOTNode::RoundJoin;
1022             break;
1023         default:
1024             mCNode.mStroke.join = LOTNode::MiterJoin;
1025             break;
1026         }
1027
1028         mCNode.mStroke.dashArray = mStroke.dashArray;
1029         mCNode.mStroke.dashArraySize = mStroke.dashArraySize;
1030
1031     } else {
1032         mCNode.mStroke.enable = 0;
1033     }
1034
1035     switch (mBrush.type()) {
1036     case VBrush::Type::Solid:
1037         mCNode.mType = LOTNode::BrushSolid;
1038         mCNode.mColor.r = mBrush.mColor.r;
1039         mCNode.mColor.g = mBrush.mColor.g;
1040         mCNode.mColor.b = mBrush.mColor.b;
1041         mCNode.mColor.a = mBrush.mColor.a;
1042         break;
1043     case VBrush::Type::LinearGradient:
1044         mCNode.mType = LOTNode::BrushGradient;
1045         mCNode.mGradient.type = LOTNode::Gradient::Linear;
1046         mCNode.mGradient.start.x = mBrush.mGradient->linear.x1;
1047         mCNode.mGradient.start.y = mBrush.mGradient->linear.y1;
1048         mCNode.mGradient.end.x = mBrush.mGradient->linear.x2;
1049         mCNode.mGradient.end.y = mBrush.mGradient->linear.y2;
1050         break;
1051     case VBrush::Type::RadialGradient:
1052         mCNode.mType = LOTNode::BrushGradient;
1053         mCNode.mGradient.type = LOTNode::Gradient::Radial;
1054         mCNode.mGradient.center.x = mBrush.mGradient->radial.cx;
1055         mCNode.mGradient.center.y = mBrush.mGradient->radial.cy;
1056         mCNode.mGradient.focal.x = mBrush.mGradient->radial.fx;
1057         mCNode.mGradient.focal.y = mBrush.mGradient->radial.fy;
1058         mCNode.mGradient.cradius = mBrush.mGradient->radial.cradius;
1059         mCNode.mGradient.fradius = mBrush.mGradient->radial.fradius;
1060         break;
1061     default:
1062         break;
1063     }
1064 }