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 bool LOTCompItem::render(const lottie::Surface &surface)
111 VBitmap bitmap((uchar *)surface.buffer(), surface.width(), surface.height(),
112 surface.bytesPerLine(), VBitmap::Format::ARGB32_Premultiplied,
115 /* schedule all preprocess task for this frame at once.
117 mDrawableList.clear();
118 mRootLayer->renderList(mDrawableList);
119 VRect clip(0, 0, surface.width(), surface.height());
120 for (auto &e : mDrawableList) {
124 VPainter painter(&bitmap);
125 mRootLayer->render(&painter, {}, {}, nullptr);
130 void LOTMaskItem::update(int frameNo, const VMatrix &parentMatrix,
131 float parentAlpha, const DirtyFlag &/*flag*/)
133 if (mData->mShape.isStatic()) {
134 if (mLocalPath.empty()) {
135 mData->mShape.value(frameNo).toPath(mLocalPath);
138 mData->mShape.value(frameNo).toPath(mLocalPath);
140 float opacity = mData->opacity(frameNo);
141 opacity = opacity * parentAlpha;
142 mCombinedAlpha = opacity;
144 VPath path = mLocalPath;
145 path.transform(parentMatrix);
147 mRleTask = VRaster::generateFillInfo(std::move(path), std::move(mRle));
151 VRle LOTMaskItem::rle()
153 if (mRleTask.valid()) {
154 mRle = mRleTask.get();
155 if (!vCompare(mCombinedAlpha, 1.0f))
156 mRle *= (mCombinedAlpha * 255);
157 if (mData->mInv) mRle.invert();
162 void LOTLayerItem::render(VPainter *painter, const VRle &inheritMask, const VRle &inheritMatte, LOTLayerItem *matteSource)
166 mDrawableList.clear();
167 matteSource->renderList(mDrawableList);
168 for (auto &i : mDrawableList) {
169 matteRle = matteRle + i->rle();
172 if (!inheritMatte.empty())
173 matteRle = matteRle & inheritMatte;
175 matteRle = inheritMatte;
177 mDrawableList.clear();
178 renderList(mDrawableList);
182 mask = maskRle(painter->clipBoundingRect());
183 if (!inheritMask.empty())
184 mask = mask & inheritMask;
185 // if resulting mask is empty then return.
192 for (auto &i : mDrawableList) {
193 painter->setBrush(i->mBrush);
195 if (!mask.empty()) rle = rle & mask;
197 if (rle.empty()) continue;
199 if (!matteRle.empty()) {
200 if (mLayerData->mMatteType == MatteType::AlphaInv) {
201 rle = rle - matteRle;
203 rle = rle & matteRle;
206 painter->drawRle(VPoint(), rle);
210 VRle LOTLayerItem::maskRle(const VRect &clipRect)
213 for (auto &i : mMasks) {
214 switch (i->maskMode()) {
215 case LOTMaskData::Mode::Add: {
216 rle = rle + i->rle();
219 case LOTMaskData::Mode::Substarct: {
220 if (rle.empty() && !clipRect.empty())
221 rle = VRle::toRle(clipRect);
222 rle = rle - i->rle();
225 case LOTMaskData::Mode::Intersect: {
226 rle = rle & i->rle();
229 case LOTMaskData::Mode::Difference: {
230 rle = rle ^ i->rle();
240 LOTLayerItem::LOTLayerItem(LOTLayerData *layerData): mLayerData(layerData)
242 if (mLayerData->mHasMask) {
243 for (auto &i : mLayerData->mMasks) {
244 mMasks.push_back(std::make_unique<LOTMaskItem>(i.get()));
249 void LOTLayerItem::updateStaticProperty()
251 if (mParentLayer) mParentLayer->updateStaticProperty();
253 mStatic = mLayerData->isStatic();
254 mStatic = mParentLayer ? (mStatic & mParentLayer->isStatic()) : mStatic;
255 mStatic = mPrecompLayer ? (mStatic & mPrecompLayer->isStatic()) : mStatic;
258 void LOTLayerItem::update(int frameNumber, const VMatrix &parentMatrix,
261 mFrameNo = frameNumber;
262 // 1. check if the layer is part of the current frame
263 if (!visible()) return;
265 // 2. calculate the parent matrix and alpha
266 VMatrix m = matrix(frameNo());
268 float alpha = parentAlpha * opacity(frameNo());
270 // 6. update the mask
272 for (auto &i : mMasks) i->update(frameNo(), m, alpha, mDirtyFlag);
275 // 3. update the dirty flag based on the change
276 if (!mCombinedMatrix.fuzzyCompare(m)) {
277 mDirtyFlag |= DirtyFlagBit::Matrix;
279 if (!vCompare(mCombinedAlpha, alpha)) {
280 mDirtyFlag |= DirtyFlagBit::Alpha;
283 mCombinedAlpha = alpha;
285 // 4. if no parent property change and layer is static then nothing to do.
286 if ((flag() & DirtyFlagBit::None) && isStatic()) return;
288 // 5. update the content of the layer
291 // 6. reset the dirty flag
292 mDirtyFlag = DirtyFlagBit::None;
295 float LOTLayerItem::opacity(int frameNo) const
297 return mLayerData->mTransform->opacity(frameNo);
300 VMatrix LOTLayerItem::matrix(int frameNo) const
303 return mLayerData->mTransform->matrix(frameNo, mLayerData->autoOrient()) *
304 mParentLayer->matrix(frameNo);
306 return mLayerData->mTransform->matrix(frameNo, mLayerData->autoOrient());
309 bool LOTLayerItem::visible() const
311 if (frameNo() >= mLayerData->inFrame() &&
312 frameNo() < mLayerData->outFrame())
318 LOTCompLayerItem::LOTCompLayerItem(LOTLayerData *layerModel)
319 : LOTLayerItem(layerModel)
321 // 1. create layer item
322 for (auto &i : mLayerData->mChildren) {
323 LOTLayerData *layerModel = static_cast<LOTLayerData *>(i.get());
324 auto layerItem = LOTCompItem::createLayerItem(layerModel);
325 if (layerItem) mLayers.push_back(std::move(layerItem));
328 // 2. update parent layer
329 for (const auto &layer : mLayers) {
330 int id = layer->parentId();
332 auto search = std::find_if(mLayers.begin(), mLayers.end(),
333 [id](const auto& val){ return val->id() == id;});
334 if (search != mLayers.end()) layer->setParentLayer((*search).get());
336 // update the precomp layer if its not the root layer.
337 if (!layerModel->root()) layer->setPrecompLayer(this);
340 // 3. keep the layer in back-to-front order.
341 // as lottie model keeps the data in front-toback-order.
342 std::reverse(mLayers.begin(), mLayers.end());
345 void LOTCompLayerItem::updateStaticProperty()
347 LOTLayerItem::updateStaticProperty();
349 for (const auto &layer : mLayers) {
350 layer->updateStaticProperty();
354 void LOTCompLayerItem::render(VPainter *painter, const VRle &inheritMask, const VRle &inheritMatte, LOTLayerItem *matteSource)
358 mDrawableList.clear();
359 matteSource->renderList(mDrawableList);
360 for (auto &i : mDrawableList) {
361 matteRle = matteRle + i->rle();
364 if (!inheritMatte.empty())
365 matteRle = matteRle & inheritMatte;
367 matteRle = inheritMatte;
372 mask = maskRle(painter->clipBoundingRect());
373 if (!inheritMask.empty())
374 mask = mask & inheritMask;
375 // if resulting mask is empty then return.
382 LOTLayerItem *matteLayer = nullptr;
383 for (const auto &layer : mLayers) {
384 if (!matteLayer && layer->hasMatte()) {
385 matteLayer = layer.get();
390 if (matteLayer->visible() && layer->visible())
391 matteLayer->render(painter, mask, matteRle, layer.get());
392 matteLayer = nullptr;
394 if (layer->visible())
395 layer->render(painter, mask, matteRle, nullptr);
400 void LOTCompLayerItem::updateContent()
402 for (const auto &layer : mLayers) {
403 layer->update( mLayerData->timeRemap(frameNo()) - mLayerData->startFrame(),
404 combinedMatrix(), combinedAlpha());
408 void LOTCompLayerItem::renderList(std::vector<VDrawable *> &list)
410 if (!visible()) return;
412 for (const auto &layer : mLayers) {
413 layer->renderList(list);
417 LOTSolidLayerItem::LOTSolidLayerItem(LOTLayerData *layerData)
418 : LOTLayerItem(layerData)
422 void LOTSolidLayerItem::updateContent()
425 mRenderNode = std::make_unique<LOTDrawable>();
426 mRenderNode->mType = VDrawable::Type::Fill;
427 mRenderNode->mFlag |= VDrawable::DirtyState::All;
430 if (flag() & DirtyFlagBit::Matrix) {
433 VRectF(0, 0, mLayerData->solidWidth(), mLayerData->solidHeight()));
434 path.transform(combinedMatrix());
435 mRenderNode->mFlag |= VDrawable::DirtyState::Path;
436 mRenderNode->mPath = path;
438 if (flag() & DirtyFlagBit::Alpha) {
439 LottieColor color = mLayerData->solidColor();
440 VBrush brush(color.toColor(combinedAlpha()));
441 mRenderNode->setBrush(brush);
442 mRenderNode->mFlag |= VDrawable::DirtyState::Brush;
446 void LOTSolidLayerItem::renderList(std::vector<VDrawable *> &list)
448 if (!visible()) return;
450 list.push_back(mRenderNode.get());
453 LOTNullLayerItem::LOTNullLayerItem(LOTLayerData *layerData)
454 : LOTLayerItem(layerData)
457 void LOTNullLayerItem::updateContent() {}
459 LOTShapeLayerItem::LOTShapeLayerItem(LOTLayerData *layerData)
460 : LOTLayerItem(layerData)
462 mRoot = std::make_unique<LOTContentGroupItem>(nullptr);
463 mRoot->addChildren(layerData);
465 std::vector<LOTPathDataItem *> list;
466 mRoot->processPaintItems(list);
468 if (layerData->hasPathOperator()) {
470 mRoot->processTrimItems(list);
474 std::unique_ptr<LOTContentItem>
475 LOTShapeLayerItem::createContentItem(LOTData *contentData)
477 switch (contentData->type()) {
478 case LOTData::Type::ShapeGroup: {
479 return std::make_unique<LOTContentGroupItem>(
480 static_cast<LOTShapeGroupData *>(contentData));
483 case LOTData::Type::Rect: {
484 return std::make_unique<LOTRectItem>(static_cast<LOTRectData *>(contentData));
487 case LOTData::Type::Ellipse: {
488 return std::make_unique<LOTEllipseItem>(static_cast<LOTEllipseData *>(contentData));
491 case LOTData::Type::Shape: {
492 return std::make_unique<LOTShapeItem>(static_cast<LOTShapeData *>(contentData));
495 case LOTData::Type::Polystar: {
496 return std::make_unique<LOTPolystarItem>(static_cast<LOTPolystarData *>(contentData));
499 case LOTData::Type::Fill: {
500 return std::make_unique<LOTFillItem>(static_cast<LOTFillData *>(contentData));
503 case LOTData::Type::GFill: {
504 return std::make_unique<LOTGFillItem>(static_cast<LOTGFillData *>(contentData));
507 case LOTData::Type::Stroke: {
508 return std::make_unique<LOTStrokeItem>(static_cast<LOTStrokeData *>(contentData));
511 case LOTData::Type::GStroke: {
512 return std::make_unique<LOTGStrokeItem>(static_cast<LOTGStrokeData *>(contentData));
515 case LOTData::Type::Repeater: {
516 return std::make_unique<LOTRepeaterItem>(static_cast<LOTRepeaterData *>(contentData));
519 case LOTData::Type::Trim: {
520 return std::make_unique<LOTTrimItem>(static_cast<LOTTrimData *>(contentData));
529 void LOTShapeLayerItem::updateContent()
531 mRoot->update(frameNo(), combinedMatrix(), combinedAlpha(), flag());
533 if (mLayerData->hasPathOperator()) {
538 void LOTShapeLayerItem::renderList(std::vector<VDrawable *> &list)
540 if (!visible()) return;
541 mRoot->renderList(list);
544 LOTContentGroupItem::LOTContentGroupItem(LOTShapeGroupData *data) : mData(data)
549 void LOTContentGroupItem::addChildren(LOTGroupData *data)
553 for (auto &i : data->mChildren) {
554 auto content = LOTShapeLayerItem::createContentItem(i.get());
556 content->setParent(this);
557 mContents.push_back(std::move(content));
561 // keep the content in back-to-front order.
562 std::reverse(mContents.begin(), mContents.end());
565 void LOTContentGroupItem::update(int frameNo, const VMatrix &parentMatrix,
566 float parentAlpha, const DirtyFlag &flag)
568 VMatrix m = parentMatrix;
569 float alpha = parentAlpha;
570 DirtyFlag newFlag = flag;
573 // update the matrix and the flag
574 if ((flag & DirtyFlagBit::Matrix) ||
575 !mData->mTransform->staticMatrix()) {
576 newFlag |= DirtyFlagBit::Matrix;
578 m = mData->mTransform->matrix(frameNo);
580 alpha *= mData->mTransform->opacity(frameNo);
582 if (!vCompare(alpha, parentAlpha)) {
583 newFlag |= DirtyFlagBit::Alpha;
589 for (const auto &content : mContents) {
590 content->update(frameNo, m, alpha, newFlag);
594 void LOTContentGroupItem::applyTrim()
596 for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
597 auto content = (*i).get();
598 if (auto trim = dynamic_cast<LOTTrimItem *>(content)) {
600 } else if (auto group = dynamic_cast<LOTContentGroupItem *>(content)) {
606 void LOTContentGroupItem::renderList(std::vector<VDrawable *> &list)
608 for (const auto &content : mContents) {
609 content->renderList(list);
613 void LOTContentGroupItem::processPaintItems(
614 std::vector<LOTPathDataItem *> &list)
616 int curOpCount = list.size();
617 for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
618 auto content = (*i).get();
619 if (auto pathNode = dynamic_cast<LOTPathDataItem *>(content)) {
620 // add it to the list
621 list.push_back(pathNode);
622 } else if (auto paintNode = dynamic_cast<LOTPaintDataItem *>(content)) {
623 // the node is a paint data node update the path list of the paint item.
624 paintNode->addPathItems(list, curOpCount);
625 } else if (auto groupNode =
626 dynamic_cast<LOTContentGroupItem *>(content)) {
627 // update the groups node with current list
628 groupNode->processPaintItems(list);
633 void LOTContentGroupItem::processTrimItems(
634 std::vector<LOTPathDataItem *> &list)
636 int curOpCount = list.size();
637 for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
638 auto content = (*i).get();
639 if (auto pathNode = dynamic_cast<LOTPathDataItem *>(content)) {
640 // add it to the list
641 list.push_back(pathNode);
642 } else if (auto trimNode = dynamic_cast<LOTTrimItem *>(content)) {
643 // the node is a paint data node update the path list of the paint item.
644 trimNode->addPathItems(list, curOpCount);
645 } else if (auto groupNode =
646 dynamic_cast<LOTContentGroupItem *>(content)) {
647 // update the groups node with current list
648 groupNode->processTrimItems(list);
653 void LOTPathDataItem::update(int frameNo, const VMatrix &,
654 float, const DirtyFlag &flag)
656 mPathChanged = false;
658 // 1. update the local path if needed
659 if (hasChanged(frameNo)) {
660 updatePath(mLocalPath, frameNo);
667 // 3. compute the final path with parentMatrix
668 if ((flag & DirtyFlagBit::Matrix) || mPathChanged) {
673 const VPath & LOTPathDataItem::finalPath()
675 if (mPathChanged || mNeedUpdate) {
676 mFinalPath.clone(mTemp);
677 mFinalPath.transform(static_cast<LOTContentGroupItem *>(parent())->matrix());
682 LOTRectItem::LOTRectItem(LOTRectData *data)
683 : LOTPathDataItem(data->isStatic()), mData(data)
687 void LOTRectItem::updatePath(VPath& path, int frameNo)
689 VPointF pos = mData->mPos.value(frameNo);
690 VPointF size = mData->mSize.value(frameNo);
691 float roundness = mData->mRound.value(frameNo);
692 VRectF r(pos.x() - size.x() / 2, pos.y() - size.y() / 2, size.x(),
696 path.addRoundRect(r, roundness, mData->direction());
697 updateCache(frameNo, pos, size, roundness);
700 LOTEllipseItem::LOTEllipseItem(LOTEllipseData *data)
701 : LOTPathDataItem(data->isStatic()), mData(data)
705 void LOTEllipseItem::updatePath(VPath& path, int frameNo)
707 VPointF pos = mData->mPos.value(frameNo);
708 VPointF size = mData->mSize.value(frameNo);
709 VRectF r(pos.x() - size.x() / 2, pos.y() - size.y() / 2, size.x(),
713 path.addOval(r, mData->direction());
714 updateCache(frameNo, pos, size);
717 LOTShapeItem::LOTShapeItem(LOTShapeData *data)
718 : LOTPathDataItem(data->isStatic()), mData(data)
722 void LOTShapeItem::updatePath(VPath& path, int frameNo)
724 mData->mShape.value(frameNo).toPath(path);
727 LOTPolystarItem::LOTPolystarItem(LOTPolystarData *data)
728 : LOTPathDataItem(data->isStatic()), mData(data)
732 void LOTPolystarItem::updatePath(VPath& path, int frameNo)
734 VPointF pos = mData->mPos.value(frameNo);
735 float points = mData->mPointCount.value(frameNo);
736 float innerRadius = mData->mInnerRadius.value(frameNo);
737 float outerRadius = mData->mOuterRadius.value(frameNo);
738 float innerRoundness = mData->mInnerRoundness.value(frameNo);
739 float outerRoundness = mData->mOuterRoundness.value(frameNo);
740 float rotation = mData->mRotation.value(frameNo);
745 if (mData->mType == LOTPolystarData::PolyType::Star) {
746 path.addPolystar(points, innerRadius, outerRadius, innerRoundness,
747 outerRoundness, 0.0, 0.0, 0.0, mData->direction());
749 path.addPolygon(points, outerRadius, outerRoundness, 0.0, 0.0, 0.0,
753 m.translate(pos.x(), pos.y()).rotate(rotation);
756 updateCache(frameNo, pos, points, innerRadius, outerRadius,
757 innerRoundness, outerRoundness, rotation);
761 * PaintData Node handling
764 LOTPaintDataItem::LOTPaintDataItem(bool staticContent):mDrawable(std::make_unique<LOTDrawable>()),
765 mStaticContent(staticContent){}
767 void LOTPaintDataItem::update(int frameNo, const VMatrix &parentMatrix,
768 float parentAlpha, const DirtyFlag &flag)
770 mRenderNodeUpdate = true;
771 mParentAlpha = parentAlpha;
775 updateContent(frameNo);
778 void LOTPaintDataItem::updateRenderNode()
781 for (auto &i : mPathItems) {
791 for (auto &i : mPathItems) {
792 mPath.addPath(i->finalPath());
794 mDrawable->setPath(mPath);
796 if (mDrawable->mFlag & VDrawable::DirtyState::Path)
797 mDrawable->mPath = mPath;
801 void LOTPaintDataItem::renderList(std::vector<VDrawable *> &list)
803 if (mRenderNodeUpdate) {
805 LOTPaintDataItem::updateRenderNode();
806 mRenderNodeUpdate = false;
808 list.push_back(mDrawable.get());
812 void LOTPaintDataItem::addPathItems(std::vector<LOTPathDataItem *> &list, int startOffset)
814 std::copy(list.begin() + startOffset, list.end(), back_inserter(mPathItems));
818 LOTFillItem::LOTFillItem(LOTFillData *data)
819 : LOTPaintDataItem(data->isStatic()), mData(data)
823 void LOTFillItem::updateContent(int frameNo)
825 LottieColor c = mData->mColor.value(frameNo);
826 float opacity = mData->opacity(frameNo);
827 mColor = c.toColor(opacity);
828 mFillRule = mData->fillRule();
831 void LOTFillItem::updateRenderNode()
833 VColor color = mColor;
835 color.setAlpha(color.a * parentAlpha());
837 mDrawable->setBrush(brush);
838 mDrawable->setFillRule(mFillRule);
841 LOTGFillItem::LOTGFillItem(LOTGFillData *data)
842 : LOTPaintDataItem(data->isStatic()), mData(data)
846 void LOTGFillItem::updateContent(int frameNo)
848 mData->update(mGradient, frameNo);
849 mGradient->mMatrix = static_cast<LOTContentGroupItem *>(parent())->matrix();
850 mFillRule = mData->fillRule();
853 void LOTGFillItem::updateRenderNode()
855 mGradient->setAlpha(parentAlpha());
856 mDrawable->setBrush(VBrush(mGradient.get()));
857 mDrawable->setFillRule(mFillRule);
860 LOTStrokeItem::LOTStrokeItem(LOTStrokeData *data)
861 : LOTPaintDataItem(data->isStatic()), mData(data)
866 void LOTStrokeItem::updateContent(int frameNo)
868 LottieColor c = mData->mColor.value(frameNo);
869 float opacity = mData->opacity(frameNo);
870 mColor = c.toColor(opacity);
871 mCap = mData->capStyle();
872 mJoin = mData->joinStyle();
873 mMiterLimit = mData->meterLimit();
874 mWidth = mData->width(frameNo);
875 if (mData->hasDashInfo()) {
876 mDashArraySize = mData->getDashInfo(frameNo, mDashArray);
880 static float getScale(const VMatrix &matrix)
882 constexpr float SQRT_2 = 1.41421;
884 VPointF p2(SQRT_2, SQRT_2);
887 VPointF final = p2 - p1;
889 return std::sqrt(final.x() * final.x() + final.y() * final.y()) / 2.0;
892 void LOTStrokeItem::updateRenderNode()
894 VColor color = mColor;
896 color.setAlpha(color.a * parentAlpha());
898 mDrawable->setBrush(brush);
899 float scale = getScale(static_cast<LOTContentGroupItem *>(parent())->matrix());
900 mDrawable->setStrokeInfo(mCap, mJoin, mMiterLimit,
902 if (mDashArraySize) {
903 for (int i = 0 ; i < mDashArraySize ; i++)
904 mDashArray[i] *= scale;
905 mDrawable->setDashInfo(mDashArray, mDashArraySize);
909 LOTGStrokeItem::LOTGStrokeItem(LOTGStrokeData *data)
910 : LOTPaintDataItem(data->isStatic()), mData(data)
915 void LOTGStrokeItem::updateContent(int frameNo)
917 mData->update(mGradient, frameNo);
918 mGradient->mMatrix = static_cast<LOTContentGroupItem *>(parent())->matrix();
919 mCap = mData->capStyle();
920 mJoin = mData->joinStyle();
921 mMiterLimit = mData->meterLimit();
922 mWidth = mData->width(frameNo);
923 if (mData->hasDashInfo()) {
924 mDashArraySize = mData->getDashInfo(frameNo, mDashArray);
928 void LOTGStrokeItem::updateRenderNode()
930 float scale = getScale(mGradient->mMatrix);
931 mGradient->setAlpha(parentAlpha());
932 mDrawable->setBrush(VBrush(mGradient.get()));
933 mDrawable->setStrokeInfo(mCap, mJoin, mMiterLimit,
935 if (mDashArraySize) {
936 for (int i = 0 ; i < mDashArraySize ; i++)
937 mDashArray[i] *= scale;
938 mDrawable->setDashInfo(mDashArray, mDashArraySize);
942 LOTTrimItem::LOTTrimItem(LOTTrimData *data) : mData(data) {}
944 void LOTTrimItem::update(int frameNo, const VMatrix &/*parentMatrix*/,
945 float /*parentAlpha*/, const DirtyFlag &/*flag*/)
949 if (mCache.mFrameNo == frameNo) return;
951 float start = mData->start(frameNo);
952 float end = mData->end(frameNo);
953 float offset = mData->offset(frameNo);
955 if (!(vCompare(mCache.mStart, start) && vCompare(mCache.mEnd, end) &&
956 vCompare(mCache.mOffset, offset))) {
958 mCache.mStart = start;
960 mCache.mOffset = offset;
962 mCache.mFrameNo = frameNo;
965 void LOTTrimItem::update()
967 // when both path and trim are not dirty
968 if (!(mDirty || pathDirty())) return;
970 if (vCompare(std::fabs(mCache.mStart - mCache.mEnd) , 1)) return;
972 if (vCompare(mCache.mStart, mCache.mEnd)) {
973 for (auto &i : mPathItems) {
974 i->updatePath(VPath());
980 if (mData->type() == LOTTrimData::TrimType::Simultaneously) {
981 for (auto &i : mPathItems) {
983 pm.setStart(mCache.mStart);
984 pm.setEnd(mCache.mEnd);
985 pm.setOffset(mCache.mOffset);
986 i->updatePath(pm.trim(i->localPath()));
988 } else { // LOTTrimData::TrimType::Individually
989 float totalLength = 0.0;
990 for (auto &i : mPathItems) {
991 totalLength += i->localPath().length();
993 float offset = totalLength * mCache.mOffset;
994 float start = totalLength * mCache.mStart;
995 float end = totalLength * mCache.mEnd;
998 // normalize start, end value to 0 - totalLength
999 if (fabs(start) > totalLength) start = fmod(start, totalLength);
1000 if (fabs(end) > totalLength) end = fmod(end, totalLength);
1002 if (start >= 0 && end >= 0) {
1003 if (start > end) std::swap(start, end);
1004 } else if ( start < 0 && end < 0) {
1005 start += totalLength;
1007 if (start > end) std::swap(start, end);
1009 // one is +ve and one is -ve so the
1010 // segment will be wrapped from end segment to start segment.
1011 // we need to split it in two segment.
1018 for (auto &i : mPathItems) {
1020 // update with empty path.
1021 i->updatePath(VPath());
1024 float len = i->localPath().length();
1026 if (curLen < start && curLen + len < start) {
1028 // update with empty path.
1029 i->updatePath(VPath());
1031 } else if (start <= curLen && end >= curLen + len) {
1036 float local_start = start > curLen ? start - curLen : 0;
1038 float local_end = curLen + len < end ? len : end - curLen;
1041 pm.setStart(local_start);
1042 pm.setEnd(local_end);
1043 VPath p = pm.trim(i->localPath());
1054 void LOTTrimItem::addPathItems(std::vector<LOTPathDataItem *> &list, int startOffset)
1056 std::copy(list.begin() + startOffset, list.end(), back_inserter(mPathItems));
1060 LOTRepeaterItem::LOTRepeaterItem(LOTRepeaterData *data) : mData(data) {}
1062 void LOTRepeaterItem::update(int /*frameNo*/, const VMatrix &/*parentMatrix*/,
1063 float /*parentAlpha*/, const DirtyFlag &/*flag*/)
1067 void LOTRepeaterItem::renderList(std::vector<VDrawable *> &/*list*/) {}
1069 static void updateGStops(LOTNode *n, const VGradient *grad)
1071 if (grad->mStops.size() != n->mGradient.stopCount) {
1072 if (n->mGradient.stopCount)
1073 free(n->mGradient.stopPtr);
1074 n->mGradient.stopCount = grad->mStops.size();
1075 n->mGradient.stopPtr = (LOTGradientStop *) malloc(n->mGradient.stopCount * sizeof(LOTGradientStop));
1078 LOTGradientStop *ptr = n->mGradient.stopPtr;
1079 for (const auto &i : grad->mStops) {
1081 ptr->a = i.second.alpha() * grad->alpha();
1082 ptr->r = i.second.red();
1083 ptr->g = i.second.green();
1084 ptr->b = i.second.blue();
1090 void LOTDrawable::sync()
1093 mCNode = std::make_unique<LOTNode>();
1094 mCNode->mGradient.stopPtr = nullptr;
1095 mCNode->mGradient.stopCount = 0;
1098 mCNode->mFlag = ChangeFlagNone;
1099 if (mFlag & DirtyState::None) return;
1101 if (mFlag & DirtyState::Path) {
1102 const std::vector<VPath::Element> &elm = mPath.elements();
1103 const std::vector<VPointF> & pts = mPath.points();
1104 const float *ptPtr = reinterpret_cast<const float *>(pts.data());
1105 const char * elmPtr = reinterpret_cast<const char *>(elm.data());
1106 mCNode->mPath.elmPtr = elmPtr;
1107 mCNode->mPath.elmCount = elm.size();
1108 mCNode->mPath.ptPtr = ptPtr;
1109 mCNode->mPath.ptCount = 2 * pts.size();
1110 mCNode->mFlag |= ChangeFlagPath;
1113 if (mStroke.enable) {
1114 mCNode->mStroke.width = mStroke.width;
1115 mCNode->mStroke.meterLimit = mStroke.meterLimit;
1116 mCNode->mStroke.enable = 1;
1118 switch (mFillRule) {
1119 case FillRule::EvenOdd:
1120 mCNode->mFillRule = LOTFillRule::FillEvenOdd;
1123 mCNode->mFillRule = LOTFillRule::FillWinding;
1127 switch (mStroke.cap) {
1128 case CapStyle::Flat:
1129 mCNode->mStroke.cap = LOTCapStyle::CapFlat;
1131 case CapStyle::Square:
1132 mCNode->mStroke.cap = LOTCapStyle::CapSquare;
1134 case CapStyle::Round:
1135 mCNode->mStroke.cap = LOTCapStyle::CapRound;
1138 mCNode->mStroke.cap = LOTCapStyle::CapFlat;
1142 switch (mStroke.join) {
1143 case JoinStyle::Miter:
1144 mCNode->mStroke.join = LOTJoinStyle::JoinMiter;
1146 case JoinStyle::Bevel:
1147 mCNode->mStroke.join = LOTJoinStyle::JoinBevel;
1149 case JoinStyle::Round:
1150 mCNode->mStroke.join = LOTJoinStyle::JoinRound;
1153 mCNode->mStroke.join = LOTJoinStyle::JoinMiter;
1157 mCNode->mStroke.dashArray = mStroke.mDash.data();
1158 mCNode->mStroke.dashArraySize = mStroke.mDash.size();
1161 mCNode->mStroke.enable = 0;
1164 switch (mBrush.type()) {
1165 case VBrush::Type::Solid:
1166 mCNode->mBrushType = LOTBrushType::BrushSolid;
1167 mCNode->mColor.r = mBrush.mColor.r;
1168 mCNode->mColor.g = mBrush.mColor.g;
1169 mCNode->mColor.b = mBrush.mColor.b;
1170 mCNode->mColor.a = mBrush.mColor.a;
1172 case VBrush::Type::LinearGradient: {
1173 mCNode->mBrushType = LOTBrushType::BrushGradient;
1174 mCNode->mGradient.type = LOTGradientType::GradientLinear;
1175 VPointF s = mBrush.mGradient->mMatrix.map({mBrush.mGradient->linear.x1,
1176 mBrush.mGradient->linear.y1});
1177 VPointF e = mBrush.mGradient->mMatrix.map({mBrush.mGradient->linear.x2,
1178 mBrush.mGradient->linear.y2});
1179 mCNode->mGradient.start.x = s.x();
1180 mCNode->mGradient.start.y = s.y();
1181 mCNode->mGradient.end.x = e.x();
1182 mCNode->mGradient.end.y = e.y();
1183 updateGStops(mCNode.get(), mBrush.mGradient);
1186 case VBrush::Type::RadialGradient: {
1187 mCNode->mBrushType = LOTBrushType::BrushGradient;
1188 mCNode->mGradient.type = LOTGradientType::GradientRadial;
1189 VPointF c = mBrush.mGradient->mMatrix.map({mBrush.mGradient->radial.cx,
1190 mBrush.mGradient->radial.cy});
1191 VPointF f = mBrush.mGradient->mMatrix.map({mBrush.mGradient->radial.fx,
1192 mBrush.mGradient->radial.fy});
1193 mCNode->mGradient.center.x = c.x();
1194 mCNode->mGradient.center.y = c.y();
1195 mCNode->mGradient.focal.x = f.x();
1196 mCNode->mGradient.focal.y = f.y();
1198 float scale = getScale(mBrush.mGradient->mMatrix);
1199 mCNode->mGradient.cradius = mBrush.mGradient->radial.cradius * scale;
1200 mCNode->mGradient.fradius = mBrush.mGradient->radial.fradius * scale;
1201 updateGStops(mCNode.get(), mBrush.mGradient);