1 #include "lottieitem.h"
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.
16 LOTCompItem::LOTCompItem(LOTModel *model)
17 : mRootModel(model), mUpdateViewBox(false), mCurFrameNo(-1)
19 mCompData = model->mRoot.get();
20 mRootLayer = createLayerItem(mCompData->mRootLayer.get());
21 mRootLayer->updateStaticProperty();
22 mViewSize = mCompData->size();
25 std::unique_ptr<LOTLayerItem>
26 LOTCompItem::createLayerItem(LOTLayerData *layerData)
28 switch (layerData->mLayerType) {
29 case LayerType::Precomp: {
30 return std::make_unique<LOTCompLayerItem>(layerData);
33 case LayerType::Solid: {
34 return std::make_unique<LOTSolidLayerItem>(layerData);
37 case LayerType::Shape: {
38 return std::make_unique<LOTShapeLayerItem>(layerData);
41 case LayerType::Null: {
42 return std::make_unique<LOTNullLayerItem>(layerData);
51 void LOTCompItem::resize(const VSize &size)
53 if (mViewSize == size) return;
55 mUpdateViewBox = true;
58 VSize LOTCompItem::size() const
63 bool LOTCompItem::update(int frameNo)
65 // check if cached frame is same as requested frame.
66 if (!mUpdateViewBox && (mCurFrameNo == frameNo)) return false;
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.
73 VSize viewPort = mViewSize;
74 VSize viewBox = mCompData->size();
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;
83 m.translate(tx, ty).scale(scale, scale);
84 mRootLayer->update(frameNo, m, 1.0);
86 mCurFrameNo = frameNo;
87 mUpdateViewBox = false;
91 void LOTCompItem::buildRenderList()
93 mDrawableList.clear();
94 mRootLayer->renderList(mDrawableList);
97 for (auto &i : mDrawableList) {
98 LOTDrawable *lotDrawable = static_cast<LOTDrawable *>(i);
100 mRenderList.push_back(lotDrawable->mCNode.get());
104 const std::vector<LOTNode *> &LOTCompItem::renderList() const
109 void LOTCompItem::buildRenderTree()
111 mRootLayer->buildLayerNode();
114 const LOTLayerNode * LOTCompItem::renderTree() const
116 return mRootLayer->layerNode();
119 bool LOTCompItem::render(const lottie::Surface &surface)
121 VBitmap bitmap((uchar *)surface.buffer(), surface.width(), surface.height(),
122 surface.bytesPerLine(), VBitmap::Format::ARGB32_Premultiplied,
125 /* schedule all preprocess task for this frame at once.
127 mDrawableList.clear();
128 mRootLayer->renderList(mDrawableList);
129 VRect clip(0, 0, surface.width(), surface.height());
130 for (auto &e : mDrawableList) {
134 VPainter painter(&bitmap);
135 mRootLayer->render(&painter, {}, {}, nullptr);
140 void LOTMaskItem::update(int frameNo, const VMatrix &parentMatrix,
141 float parentAlpha, const DirtyFlag &/*flag*/)
143 if (mData->mShape.isStatic()) {
144 if (mLocalPath.empty()) {
145 mData->mShape.value(frameNo).toPath(mLocalPath);
148 mData->mShape.value(frameNo).toPath(mLocalPath);
150 float opacity = mData->opacity(frameNo);
151 opacity = opacity * parentAlpha;
152 mCombinedAlpha = opacity;
154 VPath path = mLocalPath;
155 path.transform(parentMatrix);
157 mRleTask = VRaster::generateFillInfo(std::move(path), std::move(mRle));
161 VRle LOTMaskItem::rle()
163 if (mRleTask.valid()) {
164 mRle = mRleTask.get();
165 if (!vCompare(mCombinedAlpha, 1.0f))
166 mRle *= (mCombinedAlpha * 255);
167 if (mData->mInv) mRle.invert();
172 void LOTLayerItem::buildLayerNode()
175 mLayerCNode = std::make_unique<LOTLayerNode>();
176 mLayerCNode->mMaskList.ptr = nullptr;
177 mLayerCNode->mMaskList.size = 0;
178 mLayerCNode->mLayerList.ptr = nullptr;
179 mLayerCNode->mLayerList.size = 0;
180 mLayerCNode->mNodeList.ptr = nullptr;
181 mLayerCNode->mNodeList.size = 0;
182 mLayerCNode->mMatte = MatteNone;
183 mLayerCNode->mVisible = 0;
185 mLayerCNode->mVisible = visible();
188 switch (mLayerData->mMatteType) {
189 case MatteType::Alpha:
190 mLayerCNode->mMatte = MatteAlpha;
192 case MatteType::AlphaInv:
193 mLayerCNode->mMatte = MatteAlphaInv;
195 case MatteType::Luma:
196 mLayerCNode->mMatte = MatteLuma;
198 case MatteType::LumaInv:
199 mLayerCNode->mMatte = MatteLumaInv;
202 mLayerCNode->mMatte = MatteNone;
207 //TODO populate mask property
211 void LOTLayerItem::render(VPainter *painter, const VRle &inheritMask, const VRle &inheritMatte, LOTLayerItem *matteSource)
215 mDrawableList.clear();
216 matteSource->renderList(mDrawableList);
217 for (auto &i : mDrawableList) {
218 matteRle = matteRle + i->rle();
221 if (!inheritMatte.empty())
222 matteRle = matteRle & inheritMatte;
224 matteRle = inheritMatte;
226 mDrawableList.clear();
227 renderList(mDrawableList);
231 mask = maskRle(painter->clipBoundingRect());
232 if (!inheritMask.empty())
233 mask = mask & inheritMask;
234 // if resulting mask is empty then return.
241 for (auto &i : mDrawableList) {
242 painter->setBrush(i->mBrush);
244 if (!mask.empty()) rle = rle & mask;
246 if (rle.empty()) continue;
248 if (!matteRle.empty()) {
249 if (mLayerData->mMatteType == MatteType::AlphaInv) {
250 rle = rle - matteRle;
252 rle = rle & matteRle;
255 painter->drawRle(VPoint(), rle);
259 VRle LOTLayerItem::maskRle(const VRect &clipRect)
262 for (auto &i : mMasks) {
263 switch (i->maskMode()) {
264 case LOTMaskData::Mode::Add: {
265 rle = rle + i->rle();
268 case LOTMaskData::Mode::Substarct: {
269 if (rle.empty() && !clipRect.empty())
270 rle = VRle::toRle(clipRect);
271 rle = rle - i->rle();
274 case LOTMaskData::Mode::Intersect: {
275 rle = rle & i->rle();
278 case LOTMaskData::Mode::Difference: {
279 rle = rle ^ i->rle();
289 LOTLayerItem::LOTLayerItem(LOTLayerData *layerData): mLayerData(layerData)
291 if (mLayerData->mHasMask) {
292 for (auto &i : mLayerData->mMasks) {
293 mMasks.push_back(std::make_unique<LOTMaskItem>(i.get()));
298 void LOTLayerItem::updateStaticProperty()
300 if (mParentLayer) mParentLayer->updateStaticProperty();
302 mStatic = mLayerData->isStatic();
303 mStatic = mParentLayer ? (mStatic & mParentLayer->isStatic()) : mStatic;
304 mStatic = mPrecompLayer ? (mStatic & mPrecompLayer->isStatic()) : mStatic;
307 void LOTLayerItem::update(int frameNumber, const VMatrix &parentMatrix,
310 mFrameNo = frameNumber;
311 // 1. check if the layer is part of the current frame
312 if (!visible()) return;
314 // 2. calculate the parent matrix and alpha
315 VMatrix m = matrix(frameNo());
317 float alpha = parentAlpha * opacity(frameNo());
319 // 6. update the mask
321 for (auto &i : mMasks) i->update(frameNo(), m, alpha, mDirtyFlag);
324 // 3. update the dirty flag based on the change
325 if (!mCombinedMatrix.fuzzyCompare(m)) {
326 mDirtyFlag |= DirtyFlagBit::Matrix;
328 if (!vCompare(mCombinedAlpha, alpha)) {
329 mDirtyFlag |= DirtyFlagBit::Alpha;
332 mCombinedAlpha = alpha;
334 // 4. if no parent property change and layer is static then nothing to do.
335 if ((flag() & DirtyFlagBit::None) && isStatic()) return;
337 // 5. update the content of the layer
340 // 6. reset the dirty flag
341 mDirtyFlag = DirtyFlagBit::None;
344 float LOTLayerItem::opacity(int frameNo) const
346 return mLayerData->mTransform->opacity(frameNo);
349 VMatrix LOTLayerItem::matrix(int frameNo) const
352 return mLayerData->mTransform->matrix(frameNo, mLayerData->autoOrient()) *
353 mParentLayer->matrix(frameNo);
355 return mLayerData->mTransform->matrix(frameNo, mLayerData->autoOrient());
358 bool LOTLayerItem::visible() const
360 if (frameNo() >= mLayerData->inFrame() &&
361 frameNo() < mLayerData->outFrame())
367 LOTCompLayerItem::LOTCompLayerItem(LOTLayerData *layerModel)
368 : LOTLayerItem(layerModel)
370 // 1. create layer item
371 for (auto &i : mLayerData->mChildren) {
372 LOTLayerData *layerModel = static_cast<LOTLayerData *>(i.get());
373 auto layerItem = LOTCompItem::createLayerItem(layerModel);
374 if (layerItem) mLayers.push_back(std::move(layerItem));
377 // 2. update parent layer
378 for (const auto &layer : mLayers) {
379 int id = layer->parentId();
381 auto search = std::find_if(mLayers.begin(), mLayers.end(),
382 [id](const auto& val){ return val->id() == id;});
383 if (search != mLayers.end()) layer->setParentLayer((*search).get());
385 // update the precomp layer if its not the root layer.
386 if (!layerModel->root()) layer->setPrecompLayer(this);
389 // 3. keep the layer in back-to-front order.
390 // as lottie model keeps the data in front-toback-order.
391 std::reverse(mLayers.begin(), mLayers.end());
394 void LOTCompLayerItem::updateStaticProperty()
396 LOTLayerItem::updateStaticProperty();
398 for (const auto &layer : mLayers) {
399 layer->updateStaticProperty();
403 void LOTCompLayerItem::buildLayerNode()
405 LOTLayerItem::buildLayerNode();
406 if (mLayers.size() != mLayersCNode.size()) {
407 for (const auto &layer : mLayers) {
408 layer->buildLayerNode();
409 mLayersCNode.push_back(layer->layerNode());
411 layerNode()->mLayerList.ptr = mLayersCNode.data();
412 layerNode()->mLayerList.size = mLayersCNode.size();
414 for (const auto &layer : mLayers) {
415 layer->buildLayerNode();
420 void LOTCompLayerItem::render(VPainter *painter, const VRle &inheritMask, const VRle &inheritMatte, LOTLayerItem *matteSource)
424 mDrawableList.clear();
425 matteSource->renderList(mDrawableList);
426 for (auto &i : mDrawableList) {
427 matteRle = matteRle + i->rle();
430 if (!inheritMatte.empty())
431 matteRle = matteRle & inheritMatte;
433 matteRle = inheritMatte;
438 mask = maskRle(painter->clipBoundingRect());
439 if (!inheritMask.empty())
440 mask = mask & inheritMask;
441 // if resulting mask is empty then return.
448 LOTLayerItem *matteLayer = nullptr;
449 for (const auto &layer : mLayers) {
450 if (!matteLayer && layer->hasMatte()) {
451 matteLayer = layer.get();
456 if (matteLayer->visible() && layer->visible())
457 matteLayer->render(painter, mask, matteRle, layer.get());
458 matteLayer = nullptr;
460 if (layer->visible())
461 layer->render(painter, mask, matteRle, nullptr);
466 void LOTCompLayerItem::updateContent()
468 for (const auto &layer : mLayers) {
469 layer->update( mLayerData->timeRemap(frameNo()) - mLayerData->startFrame(),
470 combinedMatrix(), combinedAlpha());
474 void LOTCompLayerItem::renderList(std::vector<VDrawable *> &list)
476 if (!visible()) return;
478 for (const auto &layer : mLayers) {
479 layer->renderList(list);
483 LOTSolidLayerItem::LOTSolidLayerItem(LOTLayerData *layerData)
484 : LOTLayerItem(layerData)
488 void LOTSolidLayerItem::updateContent()
491 mRenderNode = std::make_unique<LOTDrawable>();
492 mRenderNode->mType = VDrawable::Type::Fill;
493 mRenderNode->mFlag |= VDrawable::DirtyState::All;
496 if (flag() & DirtyFlagBit::Matrix) {
499 VRectF(0, 0, mLayerData->solidWidth(), mLayerData->solidHeight()));
500 path.transform(combinedMatrix());
501 mRenderNode->mFlag |= VDrawable::DirtyState::Path;
502 mRenderNode->mPath = path;
504 if (flag() & DirtyFlagBit::Alpha) {
505 LottieColor color = mLayerData->solidColor();
506 VBrush brush(color.toColor(combinedAlpha()));
507 mRenderNode->setBrush(brush);
508 mRenderNode->mFlag |= VDrawable::DirtyState::Brush;
512 void LOTSolidLayerItem::buildLayerNode()
514 LOTLayerItem::buildLayerNode();
516 mDrawableList.clear();
517 renderList(mDrawableList);
520 for (auto &i : mDrawableList) {
521 LOTDrawable *lotDrawable = static_cast<LOTDrawable *>(i);
523 mCNodeList.push_back(lotDrawable->mCNode.get());
525 layerNode()->mNodeList.ptr = mCNodeList.data();
526 layerNode()->mNodeList.size = mCNodeList.size();
529 void LOTSolidLayerItem::renderList(std::vector<VDrawable *> &list)
531 if (!visible()) return;
533 list.push_back(mRenderNode.get());
536 LOTNullLayerItem::LOTNullLayerItem(LOTLayerData *layerData)
537 : LOTLayerItem(layerData)
540 void LOTNullLayerItem::updateContent() {}
542 LOTShapeLayerItem::LOTShapeLayerItem(LOTLayerData *layerData)
543 : LOTLayerItem(layerData)
545 mRoot = std::make_unique<LOTContentGroupItem>(nullptr);
546 mRoot->addChildren(layerData);
548 std::vector<LOTPathDataItem *> list;
549 mRoot->processPaintItems(list);
551 if (layerData->hasPathOperator()) {
553 mRoot->processTrimItems(list);
557 std::unique_ptr<LOTContentItem>
558 LOTShapeLayerItem::createContentItem(LOTData *contentData)
560 switch (contentData->type()) {
561 case LOTData::Type::ShapeGroup: {
562 return std::make_unique<LOTContentGroupItem>(
563 static_cast<LOTShapeGroupData *>(contentData));
566 case LOTData::Type::Rect: {
567 return std::make_unique<LOTRectItem>(static_cast<LOTRectData *>(contentData));
570 case LOTData::Type::Ellipse: {
571 return std::make_unique<LOTEllipseItem>(static_cast<LOTEllipseData *>(contentData));
574 case LOTData::Type::Shape: {
575 return std::make_unique<LOTShapeItem>(static_cast<LOTShapeData *>(contentData));
578 case LOTData::Type::Polystar: {
579 return std::make_unique<LOTPolystarItem>(static_cast<LOTPolystarData *>(contentData));
582 case LOTData::Type::Fill: {
583 return std::make_unique<LOTFillItem>(static_cast<LOTFillData *>(contentData));
586 case LOTData::Type::GFill: {
587 return std::make_unique<LOTGFillItem>(static_cast<LOTGFillData *>(contentData));
590 case LOTData::Type::Stroke: {
591 return std::make_unique<LOTStrokeItem>(static_cast<LOTStrokeData *>(contentData));
594 case LOTData::Type::GStroke: {
595 return std::make_unique<LOTGStrokeItem>(static_cast<LOTGStrokeData *>(contentData));
598 case LOTData::Type::Repeater: {
599 return std::make_unique<LOTRepeaterItem>(static_cast<LOTRepeaterData *>(contentData));
602 case LOTData::Type::Trim: {
603 return std::make_unique<LOTTrimItem>(static_cast<LOTTrimData *>(contentData));
612 void LOTShapeLayerItem::updateContent()
614 mRoot->update(frameNo(), combinedMatrix(), combinedAlpha(), flag());
616 if (mLayerData->hasPathOperator()) {
621 void LOTShapeLayerItem::buildLayerNode()
623 LOTLayerItem::buildLayerNode();
625 mDrawableList.clear();
626 renderList(mDrawableList);
629 for (auto &i : mDrawableList) {
630 LOTDrawable *lotDrawable = static_cast<LOTDrawable *>(i);
632 mCNodeList.push_back(lotDrawable->mCNode.get());
634 layerNode()->mNodeList.ptr = mCNodeList.data();
635 layerNode()->mNodeList.size = mCNodeList.size();
638 void LOTShapeLayerItem::renderList(std::vector<VDrawable *> &list)
640 if (!visible()) return;
641 mRoot->renderList(list);
644 LOTContentGroupItem::LOTContentGroupItem(LOTShapeGroupData *data) : mData(data)
649 void LOTContentGroupItem::addChildren(LOTGroupData *data)
653 for (auto &i : data->mChildren) {
654 auto content = LOTShapeLayerItem::createContentItem(i.get());
656 content->setParent(this);
657 mContents.push_back(std::move(content));
661 // keep the content in back-to-front order.
662 std::reverse(mContents.begin(), mContents.end());
665 void LOTContentGroupItem::update(int frameNo, const VMatrix &parentMatrix,
666 float parentAlpha, const DirtyFlag &flag)
668 VMatrix m = parentMatrix;
669 float alpha = parentAlpha;
670 DirtyFlag newFlag = flag;
673 // update the matrix and the flag
674 if ((flag & DirtyFlagBit::Matrix) ||
675 !mData->mTransform->staticMatrix()) {
676 newFlag |= DirtyFlagBit::Matrix;
678 m = mData->mTransform->matrix(frameNo);
680 alpha *= mData->mTransform->opacity(frameNo);
682 if (!vCompare(alpha, parentAlpha)) {
683 newFlag |= DirtyFlagBit::Alpha;
689 for (const auto &content : mContents) {
690 content->update(frameNo, m, alpha, newFlag);
694 void LOTContentGroupItem::applyTrim()
696 for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
697 auto content = (*i).get();
698 if (auto trim = dynamic_cast<LOTTrimItem *>(content)) {
700 } else if (auto group = dynamic_cast<LOTContentGroupItem *>(content)) {
706 void LOTContentGroupItem::renderList(std::vector<VDrawable *> &list)
708 for (const auto &content : mContents) {
709 content->renderList(list);
713 void LOTContentGroupItem::processPaintItems(
714 std::vector<LOTPathDataItem *> &list)
716 int curOpCount = list.size();
717 for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
718 auto content = (*i).get();
719 if (auto pathNode = dynamic_cast<LOTPathDataItem *>(content)) {
720 // add it to the list
721 list.push_back(pathNode);
722 } else if (auto paintNode = dynamic_cast<LOTPaintDataItem *>(content)) {
723 // the node is a paint data node update the path list of the paint item.
724 paintNode->addPathItems(list, curOpCount);
725 } else if (auto groupNode =
726 dynamic_cast<LOTContentGroupItem *>(content)) {
727 // update the groups node with current list
728 groupNode->processPaintItems(list);
733 void LOTContentGroupItem::processTrimItems(
734 std::vector<LOTPathDataItem *> &list)
736 int curOpCount = list.size();
737 for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
738 auto content = (*i).get();
739 if (auto pathNode = dynamic_cast<LOTPathDataItem *>(content)) {
740 // add it to the list
741 list.push_back(pathNode);
742 } else if (auto trimNode = dynamic_cast<LOTTrimItem *>(content)) {
743 // the node is a paint data node update the path list of the paint item.
744 trimNode->addPathItems(list, curOpCount);
745 } else if (auto groupNode =
746 dynamic_cast<LOTContentGroupItem *>(content)) {
747 // update the groups node with current list
748 groupNode->processTrimItems(list);
753 void LOTPathDataItem::update(int frameNo, const VMatrix &,
754 float, const DirtyFlag &flag)
756 mPathChanged = false;
758 // 1. update the local path if needed
759 if (hasChanged(frameNo)) {
760 updatePath(mLocalPath, frameNo);
767 // 3. compute the final path with parentMatrix
768 if ((flag & DirtyFlagBit::Matrix) || mPathChanged) {
773 const VPath & LOTPathDataItem::finalPath()
775 if (mPathChanged || mNeedUpdate) {
776 mFinalPath.clone(mTemp);
777 mFinalPath.transform(static_cast<LOTContentGroupItem *>(parent())->matrix());
782 LOTRectItem::LOTRectItem(LOTRectData *data)
783 : LOTPathDataItem(data->isStatic()), mData(data)
787 void LOTRectItem::updatePath(VPath& path, int frameNo)
789 VPointF pos = mData->mPos.value(frameNo);
790 VPointF size = mData->mSize.value(frameNo);
791 float roundness = mData->mRound.value(frameNo);
792 VRectF r(pos.x() - size.x() / 2, pos.y() - size.y() / 2, size.x(),
796 path.addRoundRect(r, roundness, mData->direction());
799 LOTEllipseItem::LOTEllipseItem(LOTEllipseData *data)
800 : LOTPathDataItem(data->isStatic()), mData(data)
804 void LOTEllipseItem::updatePath(VPath& path, int frameNo)
806 VPointF pos = mData->mPos.value(frameNo);
807 VPointF size = mData->mSize.value(frameNo);
808 VRectF r(pos.x() - size.x() / 2, pos.y() - size.y() / 2, size.x(),
812 path.addOval(r, mData->direction());
815 LOTShapeItem::LOTShapeItem(LOTShapeData *data)
816 : LOTPathDataItem(data->isStatic()), mData(data)
820 void LOTShapeItem::updatePath(VPath& path, int frameNo)
822 mData->mShape.value(frameNo).toPath(path);
825 LOTPolystarItem::LOTPolystarItem(LOTPolystarData *data)
826 : LOTPathDataItem(data->isStatic()), mData(data)
830 void LOTPolystarItem::updatePath(VPath& path, int frameNo)
832 VPointF pos = mData->mPos.value(frameNo);
833 float points = mData->mPointCount.value(frameNo);
834 float innerRadius = mData->mInnerRadius.value(frameNo);
835 float outerRadius = mData->mOuterRadius.value(frameNo);
836 float innerRoundness = mData->mInnerRoundness.value(frameNo);
837 float outerRoundness = mData->mOuterRoundness.value(frameNo);
838 float rotation = mData->mRotation.value(frameNo);
843 if (mData->mType == LOTPolystarData::PolyType::Star) {
844 path.addPolystar(points, innerRadius, outerRadius, innerRoundness,
845 outerRoundness, 0.0, 0.0, 0.0, mData->direction());
847 path.addPolygon(points, outerRadius, outerRoundness, 0.0, 0.0, 0.0,
851 m.translate(pos.x(), pos.y()).rotate(rotation);
857 * PaintData Node handling
860 LOTPaintDataItem::LOTPaintDataItem(bool staticContent):mDrawable(std::make_unique<LOTDrawable>()),
861 mStaticContent(staticContent){}
863 void LOTPaintDataItem::update(int frameNo, const VMatrix &parentMatrix,
864 float parentAlpha, const DirtyFlag &flag)
866 mRenderNodeUpdate = true;
867 mParentAlpha = parentAlpha;
871 updateContent(frameNo);
874 void LOTPaintDataItem::updateRenderNode()
877 for (auto &i : mPathItems) {
887 for (auto &i : mPathItems) {
888 mPath.addPath(i->finalPath());
890 mDrawable->setPath(mPath);
892 if (mDrawable->mFlag & VDrawable::DirtyState::Path)
893 mDrawable->mPath = mPath;
897 void LOTPaintDataItem::renderList(std::vector<VDrawable *> &list)
899 if (mRenderNodeUpdate) {
901 LOTPaintDataItem::updateRenderNode();
902 mRenderNodeUpdate = false;
904 list.push_back(mDrawable.get());
908 void LOTPaintDataItem::addPathItems(std::vector<LOTPathDataItem *> &list, int startOffset)
910 std::copy(list.begin() + startOffset, list.end(), back_inserter(mPathItems));
914 LOTFillItem::LOTFillItem(LOTFillData *data)
915 : LOTPaintDataItem(data->isStatic()), mData(data)
919 void LOTFillItem::updateContent(int frameNo)
921 LottieColor c = mData->mColor.value(frameNo);
922 float opacity = mData->opacity(frameNo);
923 mColor = c.toColor(opacity);
924 mFillRule = mData->fillRule();
927 void LOTFillItem::updateRenderNode()
929 VColor color = mColor;
931 color.setAlpha(color.a * parentAlpha());
933 mDrawable->setBrush(brush);
934 mDrawable->setFillRule(mFillRule);
937 LOTGFillItem::LOTGFillItem(LOTGFillData *data)
938 : LOTPaintDataItem(data->isStatic()), mData(data)
942 void LOTGFillItem::updateContent(int frameNo)
944 mData->update(mGradient, frameNo);
945 mGradient->mMatrix = static_cast<LOTContentGroupItem *>(parent())->matrix();
946 mFillRule = mData->fillRule();
949 void LOTGFillItem::updateRenderNode()
951 mGradient->setAlpha(parentAlpha());
952 mDrawable->setBrush(VBrush(mGradient.get()));
953 mDrawable->setFillRule(mFillRule);
956 LOTStrokeItem::LOTStrokeItem(LOTStrokeData *data)
957 : LOTPaintDataItem(data->isStatic()), mData(data)
962 void LOTStrokeItem::updateContent(int frameNo)
964 LottieColor c = mData->mColor.value(frameNo);
965 float opacity = mData->opacity(frameNo);
966 mColor = c.toColor(opacity);
967 mCap = mData->capStyle();
968 mJoin = mData->joinStyle();
969 mMiterLimit = mData->meterLimit();
970 mWidth = mData->width(frameNo);
971 if (mData->hasDashInfo()) {
972 mDashArraySize = mData->getDashInfo(frameNo, mDashArray);
976 static float getScale(const VMatrix &matrix)
978 constexpr float SQRT_2 = 1.41421;
980 VPointF p2(SQRT_2, SQRT_2);
983 VPointF final = p2 - p1;
985 return std::sqrt(final.x() * final.x() + final.y() * final.y()) / 2.0;
988 void LOTStrokeItem::updateRenderNode()
990 VColor color = mColor;
992 color.setAlpha(color.a * parentAlpha());
994 mDrawable->setBrush(brush);
995 float scale = getScale(static_cast<LOTContentGroupItem *>(parent())->matrix());
996 mDrawable->setStrokeInfo(mCap, mJoin, mMiterLimit,
998 if (mDashArraySize) {
999 for (int i = 0 ; i < mDashArraySize ; i++)
1000 mDashArray[i] *= scale;
1001 mDrawable->setDashInfo(mDashArray, mDashArraySize);
1005 LOTGStrokeItem::LOTGStrokeItem(LOTGStrokeData *data)
1006 : LOTPaintDataItem(data->isStatic()), mData(data)
1011 void LOTGStrokeItem::updateContent(int frameNo)
1013 mData->update(mGradient, frameNo);
1014 mGradient->mMatrix = static_cast<LOTContentGroupItem *>(parent())->matrix();
1015 mCap = mData->capStyle();
1016 mJoin = mData->joinStyle();
1017 mMiterLimit = mData->meterLimit();
1018 mWidth = mData->width(frameNo);
1019 if (mData->hasDashInfo()) {
1020 mDashArraySize = mData->getDashInfo(frameNo, mDashArray);
1024 void LOTGStrokeItem::updateRenderNode()
1026 float scale = getScale(mGradient->mMatrix);
1027 mGradient->setAlpha(parentAlpha());
1028 mDrawable->setBrush(VBrush(mGradient.get()));
1029 mDrawable->setStrokeInfo(mCap, mJoin, mMiterLimit,
1031 if (mDashArraySize) {
1032 for (int i = 0 ; i < mDashArraySize ; i++)
1033 mDashArray[i] *= scale;
1034 mDrawable->setDashInfo(mDashArray, mDashArraySize);
1038 LOTTrimItem::LOTTrimItem(LOTTrimData *data) : mData(data) {}
1040 void LOTTrimItem::update(int frameNo, const VMatrix &/*parentMatrix*/,
1041 float /*parentAlpha*/, const DirtyFlag &/*flag*/)
1045 if (mCache.mFrameNo == frameNo) return;
1047 LOTTrimData::Segment segment = mData->segment(frameNo);
1049 if (!(vCompare(mCache.mSegment.start, segment.start) &&
1050 vCompare(mCache.mSegment.end, segment.end))) {
1052 mCache.mSegment = segment;
1054 mCache.mFrameNo = frameNo;
1057 void LOTTrimItem::update()
1059 // when both path and trim are not dirty
1060 if (!(mDirty || pathDirty())) return;
1062 if (vCompare(mCache.mSegment.start, mCache.mSegment.end)) {
1063 for (auto &i : mPathItems) {
1064 i->updatePath(VPath());
1070 if (mData->type() == LOTTrimData::TrimType::Simultaneously) {
1071 for (auto &i : mPathItems) {
1073 pm.setStart(mCache.mSegment.start);
1074 pm.setEnd(mCache.mSegment.end);
1075 i->updatePath(pm.trim(i->localPath()));
1077 } else { // LOTTrimData::TrimType::Individually
1078 float totalLength = 0.0;
1079 for (auto &i : mPathItems) {
1080 totalLength += i->localPath().length();
1082 float start = totalLength * mCache.mSegment.start;
1083 float end = totalLength * mCache.mSegment.end;
1087 for (auto &i : mPathItems) {
1089 // update with empty path.
1090 i->updatePath(VPath());
1093 float len = i->localPath().length();
1095 if (curLen < start && curLen + len < start) {
1097 // update with empty path.
1098 i->updatePath(VPath());
1100 } else if (start <= curLen && end >= curLen + len) {
1105 float local_start = start > curLen ? start - curLen : 0;
1107 float local_end = curLen + len < end ? len : end - curLen;
1110 pm.setStart(local_start);
1111 pm.setEnd(local_end);
1112 VPath p = pm.trim(i->localPath());
1123 void LOTTrimItem::addPathItems(std::vector<LOTPathDataItem *> &list, int startOffset)
1125 std::copy(list.begin() + startOffset, list.end(), back_inserter(mPathItems));
1129 LOTRepeaterItem::LOTRepeaterItem(LOTRepeaterData *data) : mData(data) {}
1131 void LOTRepeaterItem::update(int /*frameNo*/, const VMatrix &/*parentMatrix*/,
1132 float /*parentAlpha*/, const DirtyFlag &/*flag*/)
1136 void LOTRepeaterItem::renderList(std::vector<VDrawable *> &/*list*/) {}
1138 static void updateGStops(LOTNode *n, const VGradient *grad)
1140 if (grad->mStops.size() != n->mGradient.stopCount) {
1141 if (n->mGradient.stopCount)
1142 free(n->mGradient.stopPtr);
1143 n->mGradient.stopCount = grad->mStops.size();
1144 n->mGradient.stopPtr = (LOTGradientStop *) malloc(n->mGradient.stopCount * sizeof(LOTGradientStop));
1147 LOTGradientStop *ptr = n->mGradient.stopPtr;
1148 for (const auto &i : grad->mStops) {
1150 ptr->a = i.second.alpha() * grad->alpha();
1151 ptr->r = i.second.red();
1152 ptr->g = i.second.green();
1153 ptr->b = i.second.blue();
1159 void LOTDrawable::sync()
1162 mCNode = std::make_unique<LOTNode>();
1163 mCNode->mGradient.stopPtr = nullptr;
1164 mCNode->mGradient.stopCount = 0;
1167 mCNode->mFlag = ChangeFlagNone;
1168 if (mFlag & DirtyState::None) return;
1170 if (mFlag & DirtyState::Path) {
1171 const std::vector<VPath::Element> &elm = mPath.elements();
1172 const std::vector<VPointF> & pts = mPath.points();
1173 const float *ptPtr = reinterpret_cast<const float *>(pts.data());
1174 const char * elmPtr = reinterpret_cast<const char *>(elm.data());
1175 mCNode->mPath.elmPtr = elmPtr;
1176 mCNode->mPath.elmCount = elm.size();
1177 mCNode->mPath.ptPtr = ptPtr;
1178 mCNode->mPath.ptCount = 2 * pts.size();
1179 mCNode->mFlag |= ChangeFlagPath;
1182 if (mStroke.enable) {
1183 mCNode->mStroke.width = mStroke.width;
1184 mCNode->mStroke.meterLimit = mStroke.meterLimit;
1185 mCNode->mStroke.enable = 1;
1187 switch (mFillRule) {
1188 case FillRule::EvenOdd:
1189 mCNode->mFillRule = LOTFillRule::FillEvenOdd;
1192 mCNode->mFillRule = LOTFillRule::FillWinding;
1196 switch (mStroke.cap) {
1197 case CapStyle::Flat:
1198 mCNode->mStroke.cap = LOTCapStyle::CapFlat;
1200 case CapStyle::Square:
1201 mCNode->mStroke.cap = LOTCapStyle::CapSquare;
1203 case CapStyle::Round:
1204 mCNode->mStroke.cap = LOTCapStyle::CapRound;
1207 mCNode->mStroke.cap = LOTCapStyle::CapFlat;
1211 switch (mStroke.join) {
1212 case JoinStyle::Miter:
1213 mCNode->mStroke.join = LOTJoinStyle::JoinMiter;
1215 case JoinStyle::Bevel:
1216 mCNode->mStroke.join = LOTJoinStyle::JoinBevel;
1218 case JoinStyle::Round:
1219 mCNode->mStroke.join = LOTJoinStyle::JoinRound;
1222 mCNode->mStroke.join = LOTJoinStyle::JoinMiter;
1226 mCNode->mStroke.dashArray = mStroke.mDash.data();
1227 mCNode->mStroke.dashArraySize = mStroke.mDash.size();
1230 mCNode->mStroke.enable = 0;
1233 switch (mBrush.type()) {
1234 case VBrush::Type::Solid:
1235 mCNode->mBrushType = LOTBrushType::BrushSolid;
1236 mCNode->mColor.r = mBrush.mColor.r;
1237 mCNode->mColor.g = mBrush.mColor.g;
1238 mCNode->mColor.b = mBrush.mColor.b;
1239 mCNode->mColor.a = mBrush.mColor.a;
1241 case VBrush::Type::LinearGradient: {
1242 mCNode->mBrushType = LOTBrushType::BrushGradient;
1243 mCNode->mGradient.type = LOTGradientType::GradientLinear;
1244 VPointF s = mBrush.mGradient->mMatrix.map({mBrush.mGradient->linear.x1,
1245 mBrush.mGradient->linear.y1});
1246 VPointF e = mBrush.mGradient->mMatrix.map({mBrush.mGradient->linear.x2,
1247 mBrush.mGradient->linear.y2});
1248 mCNode->mGradient.start.x = s.x();
1249 mCNode->mGradient.start.y = s.y();
1250 mCNode->mGradient.end.x = e.x();
1251 mCNode->mGradient.end.y = e.y();
1252 updateGStops(mCNode.get(), mBrush.mGradient);
1255 case VBrush::Type::RadialGradient: {
1256 mCNode->mBrushType = LOTBrushType::BrushGradient;
1257 mCNode->mGradient.type = LOTGradientType::GradientRadial;
1258 VPointF c = mBrush.mGradient->mMatrix.map({mBrush.mGradient->radial.cx,
1259 mBrush.mGradient->radial.cy});
1260 VPointF f = mBrush.mGradient->mMatrix.map({mBrush.mGradient->radial.fx,
1261 mBrush.mGradient->radial.fy});
1262 mCNode->mGradient.center.x = c.x();
1263 mCNode->mGradient.center.y = c.y();
1264 mCNode->mGradient.focal.x = f.x();
1265 mCNode->mGradient.focal.y = f.y();
1267 float scale = getScale(mBrush.mGradient->mMatrix);
1268 mCNode->mGradient.cradius = mBrush.mGradient->radial.cradius * scale;
1269 mCNode->mGradient.fradius = mBrush.mGradient->radial.fradius * scale;
1270 updateGStops(mCNode.get(), mBrush.mGradient);