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 for (auto &e : mDrawableList) {
123 VPainter painter(&bitmap);
124 mRootLayer->render(&painter, {}, {}, nullptr);
129 void LOTMaskItem::update(int frameNo, const VMatrix &parentMatrix,
130 float parentAlpha, const DirtyFlag &/*flag*/)
132 if (mData->mShape.isStatic()) {
133 if (mLocalPath.empty()) {
134 mData->mShape.value(frameNo).toPath(mLocalPath);
137 mData->mShape.value(frameNo).toPath(mLocalPath);
139 float opacity = mData->opacity(frameNo);
140 opacity = opacity * parentAlpha;
141 mCombinedAlpha = opacity;
143 VPath path = mLocalPath;
144 path.transform(parentMatrix);
146 mRleTask = VRaster::generateFillInfo(std::move(path), std::move(mRle));
150 VRle LOTMaskItem::rle()
152 if (mRleTask.valid()) {
153 mRle = mRleTask.get();
154 if (!vCompare(mCombinedAlpha, 1.0f))
155 mRle *= (mCombinedAlpha * 255);
156 if (mData->mInv) mRle.invert();
161 void LOTLayerItem::render(VPainter *painter, const VRle &inheritMask, const VRle &inheritMatte, LOTLayerItem *matteSource)
165 mDrawableList.clear();
166 matteSource->renderList(mDrawableList);
167 for (auto &i : mDrawableList) {
168 matteRle = matteRle + i->rle();
171 if (!inheritMatte.empty())
172 matteRle = matteRle & inheritMatte;
174 matteRle = inheritMatte;
176 mDrawableList.clear();
177 renderList(mDrawableList);
181 mask = maskRle(painter->clipBoundingRect());
182 if (!inheritMask.empty())
183 mask = mask & inheritMask;
184 // if resulting mask is empty then return.
191 for (auto &i : mDrawableList) {
192 painter->setBrush(i->mBrush);
194 if (!mask.empty()) rle = rle & mask;
196 if (rle.empty()) continue;
198 if (!matteRle.empty()) {
199 if (mLayerData->mMatteType == MatteType::AlphaInv) {
200 rle = rle - matteRle;
202 rle = rle & matteRle;
205 painter->drawRle(VPoint(), rle);
209 VRle LOTLayerItem::maskRle(const VRect &clipRect)
212 for (auto &i : mMasks) {
213 switch (i->maskMode()) {
214 case LOTMaskData::Mode::Add: {
215 rle = rle + i->rle();
218 case LOTMaskData::Mode::Substarct: {
219 if (rle.empty() && !clipRect.empty())
220 rle = VRle::toRle(clipRect);
221 rle = rle - i->rle();
224 case LOTMaskData::Mode::Intersect: {
225 rle = rle & i->rle();
228 case LOTMaskData::Mode::Difference: {
229 rle = rle ^ i->rle();
239 LOTLayerItem::LOTLayerItem(LOTLayerData *layerData): mLayerData(layerData)
241 if (mLayerData->mHasMask) {
242 for (auto &i : mLayerData->mMasks) {
243 mMasks.push_back(std::make_unique<LOTMaskItem>(i.get()));
248 void LOTLayerItem::updateStaticProperty()
250 if (mParentLayer) mParentLayer->updateStaticProperty();
252 mStatic = mLayerData->isStatic();
253 mStatic = mParentLayer ? (mStatic & mParentLayer->isStatic()) : mStatic;
254 mStatic = mPrecompLayer ? (mStatic & mPrecompLayer->isStatic()) : mStatic;
257 void LOTLayerItem::update(int frameNumber, const VMatrix &parentMatrix,
260 mFrameNo = frameNumber;
261 // 1. check if the layer is part of the current frame
262 if (!visible()) return;
264 // 2. calculate the parent matrix and alpha
265 VMatrix m = matrix(frameNo());
267 float alpha = parentAlpha * opacity(frameNo());
269 // 6. update the mask
271 for (auto &i : mMasks) i->update(frameNo(), m, alpha, mDirtyFlag);
274 // 3. update the dirty flag based on the change
275 if (!mCombinedMatrix.fuzzyCompare(m)) {
276 mDirtyFlag |= DirtyFlagBit::Matrix;
278 if (!vCompare(mCombinedAlpha, alpha)) {
279 mDirtyFlag |= DirtyFlagBit::Alpha;
282 mCombinedAlpha = alpha;
284 // 4. if no parent property change and layer is static then nothing to do.
285 if ((flag() & DirtyFlagBit::None) && isStatic()) return;
287 // 5. update the content of the layer
290 // 6. reset the dirty flag
291 mDirtyFlag = DirtyFlagBit::None;
294 float LOTLayerItem::opacity(int frameNo) const
296 return mLayerData->mTransform->opacity(frameNo);
299 VMatrix LOTLayerItem::matrix(int frameNo) const
302 return mLayerData->mTransform->matrix(frameNo) *
303 mParentLayer->matrix(frameNo);
305 return mLayerData->mTransform->matrix(frameNo);
308 bool LOTLayerItem::visible() const
310 if (frameNo() >= mLayerData->inFrame() &&
311 frameNo() < mLayerData->outFrame())
317 LOTCompLayerItem::LOTCompLayerItem(LOTLayerData *layerModel)
318 : LOTLayerItem(layerModel)
320 // 1. create layer item
321 for (auto &i : mLayerData->mChildren) {
322 LOTLayerData *layerModel = static_cast<LOTLayerData *>(i.get());
323 auto layerItem = LOTCompItem::createLayerItem(layerModel);
324 if (layerItem) mLayers.push_back(std::move(layerItem));
327 // 2. update parent layer
328 for (const auto &layer : mLayers) {
329 int id = layer->parentId();
331 auto search = std::find_if(mLayers.begin(), mLayers.end(),
332 [id](const auto& val){ return val->id() == id;});
333 if (search != mLayers.end()) layer->setParentLayer((*search).get());
335 // update the precomp layer if its not the root layer.
336 if (!layerModel->root()) layer->setPrecompLayer(this);
339 // 3. keep the layer in back-to-front order.
340 // as lottie model keeps the data in front-toback-order.
341 std::reverse(mLayers.begin(), mLayers.end());
344 void LOTCompLayerItem::updateStaticProperty()
346 LOTLayerItem::updateStaticProperty();
348 for (const auto &layer : mLayers) {
349 layer->updateStaticProperty();
353 void LOTCompLayerItem::render(VPainter *painter, const VRle &inheritMask, const VRle &inheritMatte, LOTLayerItem *matteSource)
357 mDrawableList.clear();
358 matteSource->renderList(mDrawableList);
359 for (auto &i : mDrawableList) {
360 matteRle = matteRle + i->rle();
363 if (!inheritMatte.empty())
364 matteRle = matteRle & inheritMatte;
366 matteRle = inheritMatte;
371 mask = maskRle(painter->clipBoundingRect());
372 if (!inheritMask.empty())
373 mask = mask & inheritMask;
374 // if resulting mask is empty then return.
381 LOTLayerItem *matteLayer = nullptr;
382 for (const auto &layer : mLayers) {
383 if (!matteLayer && layer->hasMatte()) {
384 matteLayer = layer.get();
389 if (matteLayer->visible() && layer->visible())
390 matteLayer->render(painter, mask, matteRle, layer.get());
391 matteLayer = nullptr;
393 if (layer->visible())
394 layer->render(painter, mask, matteRle, nullptr);
399 void LOTCompLayerItem::updateContent()
401 for (const auto &layer : mLayers) {
402 layer->update( mLayerData->timeRemap(frameNo()) - mLayerData->startFrame(),
403 combinedMatrix(), combinedAlpha());
407 void LOTCompLayerItem::renderList(std::vector<VDrawable *> &list)
409 if (!visible()) return;
411 for (const auto &layer : mLayers) {
412 layer->renderList(list);
416 LOTSolidLayerItem::LOTSolidLayerItem(LOTLayerData *layerData)
417 : LOTLayerItem(layerData)
421 void LOTSolidLayerItem::updateContent()
424 mRenderNode = std::make_unique<LOTDrawable>();
425 mRenderNode->mType = VDrawable::Type::Fill;
426 mRenderNode->mFlag |= VDrawable::DirtyState::All;
429 if (flag() & DirtyFlagBit::Matrix) {
432 VRectF(0, 0, mLayerData->solidWidth(), mLayerData->solidHeight()));
433 path.transform(combinedMatrix());
434 mRenderNode->mFlag |= VDrawable::DirtyState::Path;
435 mRenderNode->mPath = path;
437 if (flag() & DirtyFlagBit::Alpha) {
438 LottieColor color = mLayerData->solidColor();
439 VBrush brush(color.toColor(combinedAlpha()));
440 mRenderNode->setBrush(brush);
441 mRenderNode->mFlag |= VDrawable::DirtyState::Brush;
445 void LOTSolidLayerItem::renderList(std::vector<VDrawable *> &list)
447 if (!visible()) return;
449 list.push_back(mRenderNode.get());
452 LOTNullLayerItem::LOTNullLayerItem(LOTLayerData *layerData)
453 : LOTLayerItem(layerData)
456 void LOTNullLayerItem::updateContent() {}
458 LOTShapeLayerItem::LOTShapeLayerItem(LOTLayerData *layerData)
459 : LOTLayerItem(layerData)
461 mRoot = std::make_unique<LOTContentGroupItem>(nullptr);
462 mRoot->addChildren(layerData);
464 std::vector<LOTPathDataItem *> list;
465 mRoot->processPaintItems(list);
467 if (layerData->hasPathOperator()) {
469 mRoot->processTrimItems(list);
473 std::unique_ptr<LOTContentItem>
474 LOTShapeLayerItem::createContentItem(LOTData *contentData)
476 switch (contentData->type()) {
477 case LOTData::Type::ShapeGroup: {
478 return std::make_unique<LOTContentGroupItem>(
479 static_cast<LOTShapeGroupData *>(contentData));
482 case LOTData::Type::Rect: {
483 return std::make_unique<LOTRectItem>(static_cast<LOTRectData *>(contentData));
486 case LOTData::Type::Ellipse: {
487 return std::make_unique<LOTEllipseItem>(static_cast<LOTEllipseData *>(contentData));
490 case LOTData::Type::Shape: {
491 return std::make_unique<LOTShapeItem>(static_cast<LOTShapeData *>(contentData));
494 case LOTData::Type::Polystar: {
495 return std::make_unique<LOTPolystarItem>(static_cast<LOTPolystarData *>(contentData));
498 case LOTData::Type::Fill: {
499 return std::make_unique<LOTFillItem>(static_cast<LOTFillData *>(contentData));
502 case LOTData::Type::GFill: {
503 return std::make_unique<LOTGFillItem>(static_cast<LOTGFillData *>(contentData));
506 case LOTData::Type::Stroke: {
507 return std::make_unique<LOTStrokeItem>(static_cast<LOTStrokeData *>(contentData));
510 case LOTData::Type::GStroke: {
511 return std::make_unique<LOTGStrokeItem>(static_cast<LOTGStrokeData *>(contentData));
514 case LOTData::Type::Repeater: {
515 return std::make_unique<LOTRepeaterItem>(static_cast<LOTRepeaterData *>(contentData));
518 case LOTData::Type::Trim: {
519 return std::make_unique<LOTTrimItem>(static_cast<LOTTrimData *>(contentData));
528 void LOTShapeLayerItem::updateContent()
530 mRoot->update(frameNo(), combinedMatrix(), combinedAlpha(), flag());
532 if (mLayerData->hasPathOperator()) {
537 void LOTShapeLayerItem::renderList(std::vector<VDrawable *> &list)
539 if (!visible()) return;
540 mRoot->renderList(list);
543 LOTContentGroupItem::LOTContentGroupItem(LOTShapeGroupData *data) : mData(data)
548 void LOTContentGroupItem::addChildren(LOTGroupData *data)
552 for (auto &i : data->mChildren) {
553 auto content = LOTShapeLayerItem::createContentItem(i.get());
555 content->setParent(this);
556 mContents.push_back(std::move(content));
560 // keep the content in back-to-front order.
561 std::reverse(mContents.begin(), mContents.end());
564 void LOTContentGroupItem::update(int frameNo, const VMatrix &parentMatrix,
565 float parentAlpha, const DirtyFlag &flag)
567 VMatrix m = parentMatrix;
568 float alpha = parentAlpha;
569 DirtyFlag newFlag = flag;
572 // update the matrix and the flag
573 if ((flag & DirtyFlagBit::Matrix) ||
574 !mData->mTransform->staticMatrix()) {
575 newFlag |= DirtyFlagBit::Matrix;
577 m = mData->mTransform->matrix(frameNo);
579 alpha *= mData->mTransform->opacity(frameNo);
581 if (!vCompare(alpha, parentAlpha)) {
582 newFlag |= DirtyFlagBit::Alpha;
588 for (const auto &content : mContents) {
589 content->update(frameNo, m, alpha, newFlag);
593 void LOTContentGroupItem::applyTrim()
595 for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
596 auto content = (*i).get();
597 if (auto trim = dynamic_cast<LOTTrimItem *>(content)) {
599 } else if (auto group = dynamic_cast<LOTContentGroupItem *>(content)) {
605 void LOTContentGroupItem::renderList(std::vector<VDrawable *> &list)
607 for (const auto &content : mContents) {
608 content->renderList(list);
612 void LOTContentGroupItem::processPaintItems(
613 std::vector<LOTPathDataItem *> &list)
615 int curOpCount = list.size();
616 for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
617 auto content = (*i).get();
618 if (auto pathNode = dynamic_cast<LOTPathDataItem *>(content)) {
619 // add it to the list
620 list.push_back(pathNode);
621 } else if (auto paintNode = dynamic_cast<LOTPaintDataItem *>(content)) {
622 // the node is a paint data node update the path list of the paint item.
623 paintNode->addPathItems(list, curOpCount);
624 } else if (auto groupNode =
625 dynamic_cast<LOTContentGroupItem *>(content)) {
626 // update the groups node with current list
627 groupNode->processPaintItems(list);
632 void LOTContentGroupItem::processTrimItems(
633 std::vector<LOTPathDataItem *> &list)
635 int curOpCount = list.size();
636 for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
637 auto content = (*i).get();
638 if (auto pathNode = dynamic_cast<LOTPathDataItem *>(content)) {
639 // add it to the list
640 list.push_back(pathNode);
641 } else if (auto trimNode = dynamic_cast<LOTTrimItem *>(content)) {
642 // the node is a paint data node update the path list of the paint item.
643 trimNode->addPathItems(list, curOpCount);
644 } else if (auto groupNode =
645 dynamic_cast<LOTContentGroupItem *>(content)) {
646 // update the groups node with current list
647 groupNode->processTrimItems(list);
652 void LOTPathDataItem::update(int frameNo, const VMatrix &,
653 float, const DirtyFlag &flag)
655 mPathChanged = false;
657 // 1. update the local path if needed
658 if (hasChanged(frameNo)) {
659 updatePath(mLocalPath, frameNo);
666 // 3. compute the final path with parentMatrix
667 if ((flag & DirtyFlagBit::Matrix) || mPathChanged) {
672 const VPath & LOTPathDataItem::finalPath()
674 if (mPathChanged || mNeedUpdate) {
675 mFinalPath.clone(mTemp);
676 mFinalPath.transform(static_cast<LOTContentGroupItem *>(parent())->matrix());
681 LOTRectItem::LOTRectItem(LOTRectData *data)
682 : LOTPathDataItem(data->isStatic()), mData(data)
686 void LOTRectItem::updatePath(VPath& path, int frameNo)
688 VPointF pos = mData->mPos.value(frameNo);
689 VPointF size = mData->mSize.value(frameNo);
690 float roundness = mData->mRound.value(frameNo);
691 VRectF r(pos.x() - size.x() / 2, pos.y() - size.y() / 2, size.x(),
695 path.addRoundRect(r, roundness, mData->direction());
696 updateCache(frameNo, pos, size, roundness);
699 LOTEllipseItem::LOTEllipseItem(LOTEllipseData *data)
700 : LOTPathDataItem(data->isStatic()), mData(data)
704 void LOTEllipseItem::updatePath(VPath& path, int frameNo)
706 VPointF pos = mData->mPos.value(frameNo);
707 VPointF size = mData->mSize.value(frameNo);
708 VRectF r(pos.x() - size.x() / 2, pos.y() - size.y() / 2, size.x(),
712 path.addOval(r, mData->direction());
713 updateCache(frameNo, pos, size);
716 LOTShapeItem::LOTShapeItem(LOTShapeData *data)
717 : LOTPathDataItem(data->isStatic()), mData(data)
721 void LOTShapeItem::updatePath(VPath& path, int frameNo)
723 mData->mShape.value(frameNo).toPath(path);
726 LOTPolystarItem::LOTPolystarItem(LOTPolystarData *data)
727 : LOTPathDataItem(data->isStatic()), mData(data)
731 void LOTPolystarItem::updatePath(VPath& path, int frameNo)
733 VPointF pos = mData->mPos.value(frameNo);
734 float points = mData->mPointCount.value(frameNo);
735 float innerRadius = mData->mInnerRadius.value(frameNo);
736 float outerRadius = mData->mOuterRadius.value(frameNo);
737 float innerRoundness = mData->mInnerRoundness.value(frameNo);
738 float outerRoundness = mData->mOuterRoundness.value(frameNo);
739 float rotation = mData->mRotation.value(frameNo);
744 if (mData->mType == LOTPolystarData::PolyType::Star) {
745 path.addPolystar(points, innerRadius, outerRadius, innerRoundness,
746 outerRoundness, 0.0, 0.0, 0.0, mData->direction());
748 path.addPolygon(points, outerRadius, outerRoundness, 0.0, 0.0, 0.0,
752 m.translate(pos.x(), pos.y()).rotate(rotation);
755 updateCache(frameNo, pos, points, innerRadius, outerRadius,
756 innerRoundness, outerRoundness, rotation);
760 * PaintData Node handling
763 LOTPaintDataItem::LOTPaintDataItem(bool staticContent):mDrawable(std::make_unique<LOTDrawable>()),
764 mStaticContent(staticContent){}
766 void LOTPaintDataItem::update(int frameNo, const VMatrix &parentMatrix,
767 float parentAlpha, const DirtyFlag &flag)
769 mRenderNodeUpdate = true;
770 mParentAlpha = parentAlpha;
774 updateContent(frameNo);
777 void LOTPaintDataItem::updateRenderNode()
780 for (auto &i : mPathItems) {
790 for (auto &i : mPathItems) {
791 mPath.addPath(i->finalPath());
793 mDrawable->setPath(mPath);
795 if (mDrawable->mFlag & VDrawable::DirtyState::Path)
796 mDrawable->mPath = mPath;
800 void LOTPaintDataItem::renderList(std::vector<VDrawable *> &list)
802 if (mRenderNodeUpdate) {
804 LOTPaintDataItem::updateRenderNode();
805 mRenderNodeUpdate = false;
807 list.push_back(mDrawable.get());
811 void LOTPaintDataItem::addPathItems(std::vector<LOTPathDataItem *> &list, int startOffset)
813 std::copy(list.begin() + startOffset, list.end(), back_inserter(mPathItems));
817 LOTFillItem::LOTFillItem(LOTFillData *data)
818 : LOTPaintDataItem(data->isStatic()), mData(data)
822 void LOTFillItem::updateContent(int frameNo)
824 LottieColor c = mData->mColor.value(frameNo);
825 float opacity = mData->opacity(frameNo);
826 mColor = c.toColor(opacity);
827 mFillRule = mData->fillRule();
830 void LOTFillItem::updateRenderNode()
832 VColor color = mColor;
834 color.setAlpha(color.a * parentAlpha());
836 mDrawable->setBrush(brush);
837 mDrawable->setFillRule(mFillRule);
840 LOTGFillItem::LOTGFillItem(LOTGFillData *data)
841 : LOTPaintDataItem(data->isStatic()), mData(data)
845 void LOTGFillItem::updateContent(int frameNo)
847 mData->update(mGradient, frameNo);
848 mGradient->mMatrix = static_cast<LOTContentGroupItem *>(parent())->matrix();
849 mFillRule = mData->fillRule();
852 void LOTGFillItem::updateRenderNode()
854 mGradient->setAlpha(parentAlpha());
855 mDrawable->setBrush(VBrush(mGradient.get()));
856 mDrawable->setFillRule(mFillRule);
859 LOTStrokeItem::LOTStrokeItem(LOTStrokeData *data)
860 : LOTPaintDataItem(data->isStatic()), mData(data)
865 void LOTStrokeItem::updateContent(int frameNo)
867 LottieColor c = mData->mColor.value(frameNo);
868 float opacity = mData->opacity(frameNo);
869 mColor = c.toColor(opacity);
870 mCap = mData->capStyle();
871 mJoin = mData->joinStyle();
872 mMiterLimit = mData->meterLimit();
873 mWidth = mData->width(frameNo);
874 if (mData->hasDashInfo()) {
875 mDashArraySize = mData->getDashInfo(frameNo, mDashArray);
879 static float getScale(const VMatrix &matrix)
881 constexpr float SQRT_2 = 1.41421;
883 VPointF p2(SQRT_2, SQRT_2);
886 VPointF final = p2 - p1;
888 return std::sqrt(final.x() * final.x() + final.y() * final.y()) / 2.0;
891 void LOTStrokeItem::updateRenderNode()
893 VColor color = mColor;
895 color.setAlpha(color.a * parentAlpha());
897 mDrawable->setBrush(brush);
898 float scale = getScale(static_cast<LOTContentGroupItem *>(parent())->matrix());
899 mDrawable->setStrokeInfo(mCap, mJoin, mMiterLimit,
901 if (mDashArraySize) {
902 for (int i = 0 ; i < mDashArraySize ; i++)
903 mDashArray[i] *= scale;
904 mDrawable->setDashInfo(mDashArray, mDashArraySize);
908 LOTGStrokeItem::LOTGStrokeItem(LOTGStrokeData *data)
909 : LOTPaintDataItem(data->isStatic()), mData(data)
914 void LOTGStrokeItem::updateContent(int frameNo)
916 mData->update(mGradient, frameNo);
917 mGradient->mMatrix = static_cast<LOTContentGroupItem *>(parent())->matrix();
918 mCap = mData->capStyle();
919 mJoin = mData->joinStyle();
920 mMiterLimit = mData->meterLimit();
921 mWidth = mData->width(frameNo);
922 if (mData->hasDashInfo()) {
923 mDashArraySize = mData->getDashInfo(frameNo, mDashArray);
927 void LOTGStrokeItem::updateRenderNode()
929 float scale = getScale(mGradient->mMatrix);
930 mGradient->setAlpha(parentAlpha());
931 mDrawable->setBrush(VBrush(mGradient.get()));
932 mDrawable->setStrokeInfo(mCap, mJoin, mMiterLimit,
934 if (mDashArraySize) {
935 for (int i = 0 ; i < mDashArraySize ; i++)
936 mDashArray[i] *= scale;
937 mDrawable->setDashInfo(mDashArray, mDashArraySize);
941 LOTTrimItem::LOTTrimItem(LOTTrimData *data) : mData(data) {}
943 void LOTTrimItem::update(int frameNo, const VMatrix &/*parentMatrix*/,
944 float /*parentAlpha*/, const DirtyFlag &/*flag*/)
948 if (mCache.mFrameNo == frameNo) return;
950 float start = mData->start(frameNo);
951 float end = mData->end(frameNo);
952 float offset = mData->offset(frameNo);
954 if (!(vCompare(mCache.mStart, start) && vCompare(mCache.mEnd, end) &&
955 vCompare(mCache.mOffset, offset))) {
957 mCache.mStart = start;
959 mCache.mOffset = offset;
961 mCache.mFrameNo = frameNo;
964 void LOTTrimItem::update()
966 // when both path and trim are not dirty
967 if (!(mDirty || pathDirty())) return;
969 //@TODO take the offset and trim type into account.
970 for (auto &i : mPathItems) {
972 pm.setStart(mCache.mStart);
973 pm.setEnd(mCache.mEnd);
974 pm.setOffset(mCache.mOffset);
975 i->updatePath(pm.trim(i->localPath()));
980 void LOTTrimItem::addPathItems(std::vector<LOTPathDataItem *> &list, int startOffset)
982 std::copy(list.begin() + startOffset, list.end(), back_inserter(mPathItems));
986 LOTRepeaterItem::LOTRepeaterItem(LOTRepeaterData *data) : mData(data) {}
988 void LOTRepeaterItem::update(int /*frameNo*/, const VMatrix &/*parentMatrix*/,
989 float /*parentAlpha*/, const DirtyFlag &/*flag*/)
993 void LOTRepeaterItem::renderList(std::vector<VDrawable *> &/*list*/) {}
995 static void updateGStops(LOTNode *n, const VGradient *grad)
997 if (grad->mStops.size() != n->mGradient.stopCount) {
998 if (n->mGradient.stopCount)
999 free(n->mGradient.stopPtr);
1000 n->mGradient.stopCount = grad->mStops.size();
1001 n->mGradient.stopPtr = (LOTGradientStop *) malloc(n->mGradient.stopCount * sizeof(LOTGradientStop));
1004 LOTGradientStop *ptr = n->mGradient.stopPtr;
1005 for (const auto &i : grad->mStops) {
1007 ptr->a = i.second.alpha() * grad->alpha();
1008 ptr->r = i.second.red();
1009 ptr->g = i.second.green();
1010 ptr->b = i.second.blue();
1016 void LOTDrawable::sync()
1019 mCNode = std::make_unique<LOTNode>();
1020 mCNode->mGradient.stopPtr = nullptr;
1021 mCNode->mGradient.stopCount = 0;
1024 mCNode->mFlag = ChangeFlagNone;
1025 if (mFlag & DirtyState::None) return;
1027 if (mFlag & DirtyState::Path) {
1028 const std::vector<VPath::Element> &elm = mPath.elements();
1029 const std::vector<VPointF> & pts = mPath.points();
1030 const float *ptPtr = reinterpret_cast<const float *>(pts.data());
1031 const char * elmPtr = reinterpret_cast<const char *>(elm.data());
1032 mCNode->mPath.elmPtr = elmPtr;
1033 mCNode->mPath.elmCount = elm.size();
1034 mCNode->mPath.ptPtr = ptPtr;
1035 mCNode->mPath.ptCount = 2 * pts.size();
1036 mCNode->mFlag |= ChangeFlagPath;
1039 if (mStroke.enable) {
1040 mCNode->mStroke.width = mStroke.width;
1041 mCNode->mStroke.meterLimit = mStroke.meterLimit;
1042 mCNode->mStroke.enable = 1;
1044 switch (mFillRule) {
1045 case FillRule::EvenOdd:
1046 mCNode->mFillRule = LOTFillRule::FillEvenOdd;
1049 mCNode->mFillRule = LOTFillRule::FillWinding;
1053 switch (mStroke.cap) {
1054 case CapStyle::Flat:
1055 mCNode->mStroke.cap = LOTCapStyle::CapFlat;
1057 case CapStyle::Square:
1058 mCNode->mStroke.cap = LOTCapStyle::CapSquare;
1060 case CapStyle::Round:
1061 mCNode->mStroke.cap = LOTCapStyle::CapRound;
1064 mCNode->mStroke.cap = LOTCapStyle::CapFlat;
1068 switch (mStroke.join) {
1069 case JoinStyle::Miter:
1070 mCNode->mStroke.join = LOTJoinStyle::JoinMiter;
1072 case JoinStyle::Bevel:
1073 mCNode->mStroke.join = LOTJoinStyle::JoinBevel;
1075 case JoinStyle::Round:
1076 mCNode->mStroke.join = LOTJoinStyle::JoinRound;
1079 mCNode->mStroke.join = LOTJoinStyle::JoinMiter;
1083 mCNode->mStroke.dashArray = mStroke.mDash.data();
1084 mCNode->mStroke.dashArraySize = mStroke.mDash.size();
1087 mCNode->mStroke.enable = 0;
1090 switch (mBrush.type()) {
1091 case VBrush::Type::Solid:
1092 mCNode->mBrushType = LOTBrushType::BrushSolid;
1093 mCNode->mColor.r = mBrush.mColor.r;
1094 mCNode->mColor.g = mBrush.mColor.g;
1095 mCNode->mColor.b = mBrush.mColor.b;
1096 mCNode->mColor.a = mBrush.mColor.a;
1098 case VBrush::Type::LinearGradient: {
1099 mCNode->mBrushType = LOTBrushType::BrushGradient;
1100 mCNode->mGradient.type = LOTGradientType::GradientLinear;
1101 VPointF s = mBrush.mGradient->mMatrix.map({mBrush.mGradient->linear.x1,
1102 mBrush.mGradient->linear.y1});
1103 VPointF e = mBrush.mGradient->mMatrix.map({mBrush.mGradient->linear.x2,
1104 mBrush.mGradient->linear.y2});
1105 mCNode->mGradient.start.x = s.x();
1106 mCNode->mGradient.start.y = s.y();
1107 mCNode->mGradient.end.x = e.x();
1108 mCNode->mGradient.end.y = e.y();
1109 updateGStops(mCNode.get(), mBrush.mGradient);
1112 case VBrush::Type::RadialGradient:
1113 mCNode->mBrushType = LOTBrushType::BrushGradient;
1114 mCNode->mGradient.type = LOTGradientType::GradientRadial;
1115 mCNode->mGradient.center.x = mBrush.mGradient->radial.cx;
1116 mCNode->mGradient.center.y = mBrush.mGradient->radial.cy;
1117 mCNode->mGradient.focal.x = mBrush.mGradient->radial.fx;
1118 mCNode->mGradient.focal.y = mBrush.mGradient->radial.fy;
1119 mCNode->mGradient.cradius = mBrush.mGradient->radial.cradius;
1120 mCNode->mGradient.fradius = mBrush.mGradient->radial.fradius;
1121 updateGStops(mCNode.get(), mBrush.mGradient);