lottie/optimization: move the rle object to rletask thread to reuse
[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             mLocalPath = mData->mShape.value(frameNo).toPath();
176         }
177     } else {
178         mLocalPath = mData->mShape.value(frameNo).toPath();
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(path, std::move(mRle));
188 }
189
190 VRle LOTMaskItem::rle()
191 {
192     if (mRleTask.valid()) {
193         mRle = mRleTask.get();
194         if (!vCompare(mCombinedAlpha, 1.0f))
195             mRle = mRle * (mCombinedAlpha * 255);
196         if (mData->mInv) mRle = ~mRle;
197     }
198     return mRle;
199 }
200
201 void LOTLayerItem::render(VPainter *painter, const VRle &inheritMask)
202 {
203     std::vector<VDrawable *> list;
204     renderList(list);
205     VRle mask = inheritMask;
206     if (hasMask()) {
207         if (mask.isEmpty())
208             mask = maskRle(painter->clipBoundingRect());
209         else
210             mask = mask & inheritMask;
211     }
212
213     for (auto i : list) {
214         painter->setBrush(i->mBrush);
215         if (!mask.isEmpty()) {
216             VRle rle = i->rle() & mask;
217             painter->drawRle(VPoint(), rle);
218         } else {
219             painter->drawRle(VPoint(), i->rle());
220         }
221     }
222 }
223
224 VRle LOTLayerItem::maskRle(const VRect &clipRect)
225 {
226     VRle rle;
227     for (auto &i : mMasks) {
228         switch (i->maskMode()) {
229         case LOTMaskData::Mode::Add: {
230             rle = rle + i->rle();
231             break;
232         }
233         case LOTMaskData::Mode::Substarct: {
234             if (rle.isEmpty() && !clipRect.isEmpty())
235                 rle = VRle::toRle(clipRect);
236             rle = rle - i->rle();
237             break;
238         }
239         case LOTMaskData::Mode::Intersect: {
240             rle = rle & i->rle();
241             break;
242         }
243         default:
244             break;
245         }
246     }
247     return rle;
248 }
249
250 LOTLayerItem::LOTLayerItem(LOTLayerData *layerData)
251     : mLayerData(layerData),
252       mParentLayer(nullptr),
253       mPrecompLayer(nullptr),
254       mCombinedAlpha(0.0f),
255       mFrameNo(-1),
256       mDirtyFlag(DirtyFlagBit::All)
257 {
258     if (mLayerData->mHasMask) {
259         for (auto i : mLayerData->mMasks) {
260             mMasks.push_back(std::make_unique<LOTMaskItem>(i.get()));
261         }
262     }
263 }
264
265 void LOTLayerItem::updateStaticProperty()
266 {
267     if (mParentLayer) mParentLayer->updateStaticProperty();
268
269     mStatic = mLayerData->isStatic();
270     mStatic = mParentLayer ? (mStatic & mParentLayer->isStatic()) : mStatic;
271     mStatic = mPrecompLayer ? (mStatic & mPrecompLayer->isStatic()) : mStatic;
272 }
273
274 void LOTLayerItem::update(int frameNo, const VMatrix &parentMatrix,
275                           float parentAlpha)
276 {
277     mFrameNo = frameNo;
278     // 1. check if the layer is part of the current frame
279     if (!visible()) return;
280
281     // 2. calculate the parent matrix and alpha
282     VMatrix m = matrix(frameNo);
283     m *= parentMatrix;
284     float alpha = parentAlpha * opacity(frameNo);
285
286     // 6. update the mask
287     if (hasMask()) {
288         for (auto &i : mMasks) i->update(frameNo, m, alpha, mDirtyFlag);
289     }
290
291     // 3. update the dirty flag based on the change
292     if (!mCombinedMatrix.fuzzyCompare(m)) {
293         mDirtyFlag |= DirtyFlagBit::Matrix;
294     }
295     if (!vCompare(mCombinedAlpha, alpha)) {
296         mDirtyFlag |= DirtyFlagBit::Alpha;
297     }
298     mCombinedMatrix = m;
299     mCombinedAlpha = alpha;
300
301     // 4. if no parent property change and layer is static then nothing to do.
302     if ((flag() & DirtyFlagBit::None) && isStatic()) return;
303
304     // 5. update the content of the layer
305     updateContent();
306
307     // 6. reset the dirty flag
308     mDirtyFlag = DirtyFlagBit::None;
309 }
310
311 float LOTLayerItem::opacity(int frameNo) const
312 {
313     return mLayerData->mTransform->opacity(frameNo);
314 }
315
316 VMatrix LOTLayerItem::matrix(int frameNo) const
317 {
318     if (mParentLayer)
319         return mLayerData->mTransform->matrix(frameNo) *
320                mParentLayer->matrix(frameNo);
321     else
322         return mLayerData->mTransform->matrix(frameNo);
323 }
324
325 bool LOTLayerItem::visible() const
326 {
327     if (frameNo() >= mLayerData->inFrame() &&
328         frameNo() < mLayerData->outFrame())
329         return true;
330     else
331         return false;
332 }
333
334 LOTCompLayerItem::LOTCompLayerItem(LOTLayerData *layerModel)
335     : LOTLayerItem(layerModel)
336 {
337     for (auto i : mLayerData->mChildren) {
338         LOTLayerData *layerModel = dynamic_cast<LOTLayerData *>(i.get());
339         if (layerModel) {
340             LOTLayerItem *layerItem = LOTCompItem::createLayerItem(layerModel);
341             if (layerItem) {
342                 mLayers.push_back(layerItem);
343                 mLayerMap[layerItem->id()] = layerItem;
344             }
345         }
346     }
347
348     // 2. update parent layer
349     for (auto i : mLayers) {
350         int id = i->parentId();
351         if (id >= 0) {
352             auto search = mLayerMap.find(id);
353             if (search != mLayerMap.end()) {
354                 LOTLayerItem *parentLayer = search->second;
355                 i->setParentLayer(parentLayer);
356             }
357         }
358         i->setPrecompLayer(this);
359     }
360 }
361
362 void LOTCompLayerItem::updateStaticProperty()
363 {
364     LOTLayerItem::updateStaticProperty();
365
366     for (auto i : mLayers) {
367         i->updateStaticProperty();
368     }
369 }
370
371 void LOTCompLayerItem::render(VPainter *painter, const VRle &inheritMask)
372 {
373     VRle mask = inheritMask;
374
375     if (hasMask()) {
376         if (mask.isEmpty())
377             mask = maskRle(painter->clipBoundingRect());
378         else
379             mask = mask & inheritMask;
380     }
381
382     for (auto i : mLayers) {
383         i->render(painter, mask);
384     }
385 }
386
387 LOTCompLayerItem::~LOTCompLayerItem()
388 {
389     for (auto i : mLayers) {
390         delete i;
391     }
392 }
393
394 void LOTCompLayerItem::updateContent()
395 {
396     // update the layer from back to front
397     for (auto i = mLayers.rbegin(); i != mLayers.rend(); ++i) {
398         LOTLayerItem *layer = *i;
399         layer->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         LOTLayerItem *layer = *i;
410         layer->renderList(list);
411     }
412 }
413
414 LOTSolidLayerItem::LOTSolidLayerItem(LOTLayerData *layerData)
415     : LOTLayerItem(layerData)
416 {
417 }
418
419 void LOTSolidLayerItem::updateContent()
420 {
421     if (!mRenderNode) {
422         mRenderNode = std::make_unique<LOTDrawable>();
423         mRenderNode->mType = VDrawable::Type::Fill;
424         mRenderNode->mFlag |= VDrawable::DirtyState::All;
425     }
426
427     if (flag() & DirtyFlagBit::Matrix) {
428         VPath path;
429         path.addRect(
430             VRectF(0, 0, mLayerData->solidWidth(), mLayerData->solidHeight()));
431         path.transform(combinedMatrix());
432         mRenderNode->mFlag |= VDrawable::DirtyState::Path;
433         mRenderNode->mPath = path;
434     }
435     if (flag() & DirtyFlagBit::Alpha) {
436         LottieColor color = mLayerData->solidColor();
437         VBrush      brush(color.toColor(combinedAlpha()));
438         mRenderNode->setBrush(brush);
439         mRenderNode->mFlag |= VDrawable::DirtyState::Brush;
440     }
441 }
442
443 void LOTSolidLayerItem::renderList(std::vector<VDrawable *> &list)
444 {
445     if (!visible()) return;
446
447     list.push_back(mRenderNode.get());
448 }
449
450 LOTNullLayerItem::LOTNullLayerItem(LOTLayerData *layerData)
451     : LOTLayerItem(layerData)
452 {
453 }
454 void LOTNullLayerItem::updateContent() {}
455
456 LOTShapeLayerItem::LOTShapeLayerItem(LOTLayerData *layerData)
457     : LOTLayerItem(layerData)
458 {
459     mRoot = new LOTContentGroupItem(nullptr);
460     mRoot->addChildren(layerData);
461     mRoot->processPaintOperation();
462     if (layerData->hasPathOperator()) mRoot->processTrimOperation();
463 }
464
465 LOTShapeLayerItem::~LOTShapeLayerItem()
466 {
467     delete mRoot;
468 }
469
470 LOTContentItem *LOTShapeLayerItem::createContentItem(LOTData *contentData)
471 {
472     switch (contentData->type()) {
473     case LOTData::Type::ShapeGroup: {
474         return new LOTContentGroupItem(
475             static_cast<LOTShapeGroupData *>(contentData));
476         break;
477     }
478     case LOTData::Type::Rect: {
479         return new LOTRectItem(static_cast<LOTRectData *>(contentData));
480         break;
481     }
482     case LOTData::Type::Ellipse: {
483         return new LOTEllipseItem(static_cast<LOTEllipseData *>(contentData));
484         break;
485     }
486     case LOTData::Type::Shape: {
487         return new LOTShapeItem(static_cast<LOTShapeData *>(contentData));
488         break;
489     }
490     case LOTData::Type::Polystar: {
491         return new LOTPolystarItem(static_cast<LOTPolystarData *>(contentData));
492         break;
493     }
494     case LOTData::Type::Fill: {
495         return new LOTFillItem(static_cast<LOTFillData *>(contentData));
496         break;
497     }
498     case LOTData::Type::GFill: {
499         return new LOTGFillItem(static_cast<LOTGFillData *>(contentData));
500         break;
501     }
502     case LOTData::Type::Stroke: {
503         return new LOTStrokeItem(static_cast<LOTStrokeData *>(contentData));
504         break;
505     }
506     case LOTData::Type::GStroke: {
507         return new LOTGStrokeItem(static_cast<LOTGStrokeData *>(contentData));
508         break;
509     }
510     case LOTData::Type::Repeater: {
511         return new LOTRepeaterItem(static_cast<LOTRepeaterData *>(contentData));
512         break;
513     }
514     case LOTData::Type::Trim: {
515         return new LOTTrimItem(static_cast<LOTTrimData *>(contentData));
516         break;
517     }
518     default:
519         return nullptr;
520         break;
521     }
522 }
523
524 void LOTShapeLayerItem::updateContent()
525 {
526     mRoot->update(frameNo(), combinedMatrix(), combinedAlpha(), flag());
527 }
528
529 void LOTShapeLayerItem::renderList(std::vector<VDrawable *> &list)
530 {
531     if (!visible()) return;
532     mRoot->renderList(list);
533 }
534
535 LOTContentGroupItem::LOTContentGroupItem(LOTShapeGroupData *data) : mData(data)
536 {
537     addChildren(mData);
538 }
539
540 void LOTContentGroupItem::addChildren(LOTGroupData *data)
541 {
542     if (!data) return;
543
544     for (auto i : data->mChildren) {
545         LOTData *       data = i.get();
546         LOTContentItem *content = LOTShapeLayerItem::createContentItem(data);
547         if (content) mContents.push_back(content);
548     }
549 }
550
551 LOTContentGroupItem::~LOTContentGroupItem()
552 {
553     for (auto i : mContents) {
554         delete i;
555     }
556 }
557
558 void LOTContentGroupItem::update(int frameNo, const VMatrix &parentMatrix,
559                                  float parentAlpha, const DirtyFlag &flag)
560 {
561     VMatrix   m = parentMatrix;
562     float     alpha = parentAlpha;
563     DirtyFlag newFlag = flag;
564
565     if (mData) {
566         // update the matrix and the flag
567         if ((flag & DirtyFlagBit::Matrix) ||
568             !mData->mTransform->staticMatrix()) {
569             newFlag |= DirtyFlagBit::Matrix;
570         }
571         m = mData->mTransform->matrix(frameNo);
572         m *= parentMatrix;
573         alpha *= mData->mTransform->opacity(frameNo);
574
575         if (!vCompare(alpha, parentAlpha)) {
576             newFlag |= DirtyFlagBit::Alpha;
577         }
578     }
579
580     for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
581         (*i)->update(frameNo, m, alpha, newFlag);
582     }
583 }
584
585 void LOTContentGroupItem::renderList(std::vector<VDrawable *> &list)
586 {
587     for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
588         (*i)->renderList(list);
589     }
590 }
591
592 void LOTContentGroupItem::processPaintOperation()
593 {
594     std::vector<LOTPaintDataItem *> list;
595     paintOperationHelper(list);
596 }
597
598 void LOTContentGroupItem::paintOperationHelper(
599     std::vector<LOTPaintDataItem *> &list)
600 {
601     int curOpCount = list.size();
602     for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
603         auto child = *i;
604         if (auto pathNode = dynamic_cast<LOTPathDataItem *>(child)) {
605             // the node is a path data node add the paint operation list to it.
606             pathNode->addPaintOperation(list, curOpCount);
607         } else if (auto paintNode = dynamic_cast<LOTPaintDataItem *>(child)) {
608             // add it to the paint operation list
609             list.push_back(paintNode);
610         } else if (auto groupNode =
611                        dynamic_cast<LOTContentGroupItem *>(child)) {
612             // update the groups node with current list
613             groupNode->paintOperationHelper(list);
614         }
615     }
616     list.erase(list.begin() + curOpCount, list.end());
617 }
618
619 void LOTPathDataItem::addPaintOperation(std::vector<LOTPaintDataItem *> &list,
620                                         int externalCount)
621 {
622     for (auto paintItem : list) {
623         bool sameGroup = (externalCount-- > 0) ? false : true;
624         mNodeList.push_back(std::make_unique<LOTDrawable>());
625         mRenderList.push_back(
626             LOTRenderNode(this, paintItem, mNodeList.back().get(), sameGroup));
627     }
628 }
629
630 void LOTContentGroupItem::processTrimOperation()
631 {
632     std::vector<LOTTrimItem *> list;
633     trimOperationHelper(list);
634 }
635
636 void LOTContentGroupItem::trimOperationHelper(std::vector<LOTTrimItem *> &list)
637 {
638     int curOpCount = list.size();
639     for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
640         auto child = *i;
641         if (auto pathNode = dynamic_cast<LOTPathDataItem *>(child)) {
642             // the node is a path data node add the trim operation list to it.
643             pathNode->addTrimOperation(list);
644         } else if (auto trimNode = dynamic_cast<LOTTrimItem *>(child)) {
645             // add it to the trim operation list
646             list.push_back(trimNode);
647         } else if (auto groupNode =
648                        dynamic_cast<LOTContentGroupItem *>(child)) {
649             // update the groups node with current list
650             groupNode->trimOperationHelper(list);
651         }
652     }
653     list.erase(list.begin() + curOpCount, list.end());
654 }
655
656 void LOTPathDataItem::addTrimOperation(std::vector<LOTTrimItem *> &list)
657 {
658     for (auto trimItem : list) {
659         mTrimNodeRefs.push_back(trimItem);
660     }
661 }
662
663 void LOTPathDataItem::update(int frameNo, const VMatrix &parentMatrix,
664                              float parentAlpha, const DirtyFlag &flag)
665 {
666     VPath tempPath;
667
668     mPathChanged = false;
669     mCombinedAlpha = parentAlpha;
670
671     // 1. update the local path if needed
672     if (!(mInit && mStaticPath)) {
673         mLocalPath = getPath(frameNo);
674         mInit = true;
675         mPathChanged = true;
676     }
677
678     tempPath = mLocalPath;
679
680     // 2. apply path operation if needed
681     if (mTrimNodeRefs.size() > 0) {
682         // TODO apply more than one trim path if necessary
683         VPathMesure pm;
684         float       s = mTrimNodeRefs.front()->getStart(frameNo) / 100.0f;
685         float       e = mTrimNodeRefs.front()->getEnd(frameNo) / 100.0f;
686
687         pm.setOffset(s, e);
688         tempPath = pm.trim(tempPath);
689         mPathChanged = true;
690     }
691
692     // 3. compute the final path with parentMatrix
693
694     if ((flag & DirtyFlagBit::Matrix) || mPathChanged) {
695         mFinalPath = tempPath;
696         mFinalPath.transform(parentMatrix);
697         mPathChanged = true;
698     }
699
700     // 2. update the rendernode list
701     for (const auto &i : mRenderList) {
702         i.drawable->mFlag = VDrawable::DirtyState::None;
703         i.paintNodeRef->updateRenderNode(i.pathNodeRef, i.drawable,
704                                          i.sameGroup);
705         if (mPathChanged) {
706             i.drawable->mPath = mFinalPath;
707             i.drawable->mFlag |= VDrawable::DirtyState::Path;
708         }
709     }
710 }
711
712 void LOTPathDataItem::renderList(std::vector<VDrawable *> &list)
713 {
714     for (const auto &i : mRenderList) {
715         list.push_back(i.drawable);
716     }
717 }
718
719 VPath LOTPathDataItem::path() const
720 {
721     return mFinalPath;
722 }
723
724 LOTRectItem::LOTRectItem(LOTRectData *data)
725     : LOTPathDataItem(data->isStatic()), mData(data)
726 {
727 }
728
729 VPath LOTRectItem::getPath(int frameNo)
730 {
731     VPointF pos = mData->mPos.value(frameNo);
732     VPointF size = mData->mSize.value(frameNo);
733     float   radius = mData->mRound.value(frameNo);
734     VRectF  r(pos.x() - size.x() / 2, pos.y() - size.y() / 2, size.x(),
735              size.y());
736
737     VPath path;
738     path.addRoundRect(r, radius, radius, mData->direction());
739
740     return path;
741 }
742
743 LOTEllipseItem::LOTEllipseItem(LOTEllipseData *data)
744     : LOTPathDataItem(data->isStatic()), mData(data)
745 {
746 }
747
748 VPath LOTEllipseItem::getPath(int frameNo)
749 {
750     VPointF pos = mData->mPos.value(frameNo);
751     VPointF size = mData->mSize.value(frameNo);
752     VRectF  r(pos.x() - size.x() / 2, pos.y() - size.y() / 2, size.x(),
753              size.y());
754
755     VPath path;
756     path.addOval(r, mData->direction());
757
758     return path;
759 }
760
761 LOTShapeItem::LOTShapeItem(LOTShapeData *data)
762     : LOTPathDataItem(data->isStatic()), mData(data)
763 {
764 }
765
766 VPath LOTShapeItem::getPath(int frameNo)
767 {
768     return mData->mShape.value(frameNo).toPath();
769 }
770
771 LOTPolystarItem::LOTPolystarItem(LOTPolystarData *data)
772     : LOTPathDataItem(data->isStatic()), mData(data)
773 {
774 }
775
776 VPath LOTPolystarItem::getPath(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     VPath   path;
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
801     return path;
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 }