Debian Codegraph @

src/lottie/lottieitem.h

raw ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #ifndef LOTTIEITEM_H
20 #define LOTTIEITEM_H
21
22 #include<sstream>
23 #include<memory>
24
25 #include"lottieproxymodel.h"
26 #include"vmatrix.h"
27 #include"vpath.h"
28 #include"vpoint.h"
29 #include"vpathmesure.h"
30 #include"rlottiecommon.h"
31 #include"rlottie.h"
32 #include"vpainter.h"
33 #include"vdrawable.h"
34 #include"lottiekeypath.h"
35 #include"varenaalloc.h"
36
37 V_USE_NAMESPACE
38
39 enum class DirtyFlagBit : uchar
40 {
41 None = 0x00,
42 Matrix = 0x01,
43 Alpha = 0x02,
44 All = (Matrix | Alpha)
45 };
46
47 class LOTLayerItem;
48 class LOTMaskItem;
49 class VDrawable;
50
51 class LOTDrawable : public VDrawable
52 {
53 public:
54 void sync();
55 public:
56 std::unique_ptr<LOTNode> mCNode{nullptr};
57
58 ~LOTDrawable() {
59 if (mCNode && mCNode->mGradient.stopPtr)
60 free(mCNode->mGradient.stopPtr);
61 }
62 };
63
64 class LOTCompItem
65 {
66 public:
67 explicit LOTCompItem(LOTModel *model);
68 bool update(int frameNo, const VSize &size, bool keepAspectRatio);
69 VSize size() const { return mViewSize;}
70 void buildRenderTree();
71 const LOTLayerNode * renderTree()const;
72 bool render(const rlottie::Surface &surface);
73 void setValue(const std::string &keypath, LOTVariant &value);
74 private:
75 VBitmap mSurface;
76 VMatrix mScaleMatrix;
77 VSize mViewSize;
78 LOTCompositionData *mCompData{nullptr};
79 LOTLayerItem *mRootLayer{nullptr};
80 VArenaAlloc mAllocator{2048};
81 int mCurFrameNo;
82 bool mKeepAspectRatio{true};
83 };
84
85 class LOTLayerMaskItem;
86
87 class LOTClipperItem
88 {
89 public:
90 explicit LOTClipperItem(VSize size): mSize(size){}
91 void update(const VMatrix &matrix);
92 void preprocess(const VRect &clip);
93 VRle rle(const VRle& mask);
94 public:
95 VSize mSize;
96 VPath mPath;
97 VRle mMaskedRle;
98 VRasterizer mRasterizer;
99 bool mRasterRequest{false};
100 };
101
102 typedef vFlag<DirtyFlagBit> DirtyFlag;
103
104 struct LOTCApiData
105 {
106 LOTCApiData();
107 LOTLayerNode mLayer;
108 std::vector<LOTMask> mMasks;
109 std::vector<LOTLayerNode *> mLayers;
110 std::vector<LOTNode *> mCNodeList;
111 };
112
113 template< class T>
114 class VSpan
115 {
116 public:
117 using reference = T &;
118 using pointer = T *;
119 using const_pointer = T const *;
120 using const_reference = T const &;
121 using index_type = size_t;
122
123 using iterator = pointer;
124 using const_iterator = const_pointer;
125
126 VSpan() = default;
127 VSpan(pointer data, index_type size):_data(data), _size(size){}
128
129 constexpr pointer data() const noexcept {return _data; }
130 constexpr index_type size() const noexcept {return _size; }
131 constexpr bool empty() const noexcept { return size() == 0 ;}
132 constexpr iterator begin() const noexcept { return data(); }
133 constexpr iterator end() const noexcept {return data() + size() ;}
134 constexpr const_iterator cbegin() const noexcept {return data();}
135 constexpr const_iterator cend() const noexcept { return data() + size();}
136 constexpr reference operator[]( index_type idx ) const { return *( data() + idx );}
137
138 private:
139 pointer _data{nullptr};
140 index_type _size{0};
141 };
142
143 using DrawableList = VSpan<VDrawable *>;
144
145 class LOTLayerItem
146 {
147 public:
148 virtual ~LOTLayerItem() = default;
149 LOTLayerItem& operator=(LOTLayerItem&&) noexcept = delete;
150 LOTLayerItem(LOTLayerData *layerData);
151 int id() const {return mLayerData->id();}
152 int parentId() const {return mLayerData->parentId();}
153 LOTLayerItem *resolvedParentLayer() const {return mParentLayer;}
154 void setParentLayer(LOTLayerItem *parent){mParentLayer = parent;}
155 void setComplexContent(bool value) { mComplexContent = value;}
156 bool complexContent() const {return mComplexContent;}
157 virtual void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha);
158 VMatrix matrix(int frameNo) const;
159 void preprocess(const VRect& clip);
160 virtual DrawableList renderList(){ return {};}
161 virtual void render(VPainter *painter, const VRle &mask, const VRle &matteRle);
162 bool hasMatte() { if (mLayerData->mMatteType == MatteType::None) return false; return true; }
163 MatteType matteType() const { return mLayerData->mMatteType;}
164 bool visible() const;
165 virtual void buildLayerNode();
166 LOTLayerNode& clayer() {return mCApiData->mLayer;}
167 std::vector<LOTLayerNode *>& clayers() {return mCApiData->mLayers;}
168 std::vector<LOTMask>& cmasks() {return mCApiData->mMasks;}
169 std::vector<LOTNode *>& cnodes() {return mCApiData->mCNodeList;}
170 const char* name() const {return mLayerData->name();}
171 virtual bool resolveKeyPath(LOTKeyPath &keyPath, uint depth, LOTVariant &value);
172 VBitmap& bitmap() {return mRenderBuffer;}
173 protected:
174 virtual void preprocessStage(const VRect& clip) = 0;
175 virtual void updateContent() = 0;
176 inline VMatrix combinedMatrix() const {return mCombinedMatrix;}
177 inline int frameNo() const {return mFrameNo;}
178 inline float combinedAlpha() const {return mCombinedAlpha;}
179 inline bool isStatic() const {return mLayerData->isStatic();}
180 float opacity(int frameNo) const {return mLayerData->opacity(frameNo);}
181 inline DirtyFlag flag() const {return mDirtyFlag;}
182 bool skipRendering() const {return (!visible() || vIsZero(combinedAlpha()));}
183 protected:
184 std::unique_ptr<LOTLayerMaskItem> mLayerMask;
185 LOTLayerData *mLayerData{nullptr};
186 LOTLayerItem *mParentLayer{nullptr};
187 VMatrix mCombinedMatrix;
188 VBitmap mRenderBuffer;
189 float mCombinedAlpha{0.0};
190 int mFrameNo{-1};
191 DirtyFlag mDirtyFlag{DirtyFlagBit::All};
192 bool mComplexContent{false};
193 std::unique_ptr<LOTCApiData> mCApiData;
194 };
195
196 class LOTCompLayerItem: public LOTLayerItem
197 {
198 public:
199 explicit LOTCompLayerItem(LOTLayerData *layerData, VArenaAlloc* allocator);
200
201 void render(VPainter *painter, const VRle &mask, const VRle &matteRle) final;
202 void buildLayerNode() final;
203 bool resolveKeyPath(LOTKeyPath &keyPath, uint depth, LOTVariant &value) override;
204 protected:
205 void preprocessStage(const VRect& clip) final;
206 void updateContent() final;
207 private:
208 void renderHelper(VPainter *painter, const VRle &mask, const VRle &matteRle);
209 void renderMatteLayer(VPainter *painter, const VRle &inheritMask, const VRle &matteRle,
210 LOTLayerItem *layer, LOTLayerItem *src);
211 private:
212 std::vector<LOTLayerItem*> mLayers;
213 std::unique_ptr<LOTClipperItem> mClipper;
214 };
215
216 class LOTSolidLayerItem: public LOTLayerItem
217 {
218 public:
219 explicit LOTSolidLayerItem(LOTLayerData *layerData);
220 void buildLayerNode() final;
221 DrawableList renderList() final;
222 protected:
223 void preprocessStage(const VRect& clip) final;
224 void updateContent() final;
225 private:
226 LOTDrawable mRenderNode;
227 VDrawable *mDrawableList{nullptr}; //to work with the Span api
228 };
229
230 class LOTContentItem;
231 class LOTContentGroupItem;
232 class LOTShapeLayerItem: public LOTLayerItem
233 {
234 public:
235 explicit LOTShapeLayerItem(LOTLayerData *layerData, VArenaAlloc* allocator);
236 DrawableList renderList() final;
237 void buildLayerNode() final;
238 bool resolveKeyPath(LOTKeyPath &keyPath, uint depth, LOTVariant &value) override;
239 protected:
240 void preprocessStage(const VRect& clip) final;
241 void updateContent() final;
242 std::vector<VDrawable *> mDrawableList;
243 LOTContentGroupItem *mRoot{nullptr};
244 };
245
246 class LOTNullLayerItem: public LOTLayerItem
247 {
248 public:
249 explicit LOTNullLayerItem(LOTLayerData *layerData);
250 protected:
251 void preprocessStage(const VRect&) final {}
252 void updateContent() final;
253 };
254
255 class LOTImageLayerItem: public LOTLayerItem
256 {
257 public:
258 explicit LOTImageLayerItem(LOTLayerData *layerData);
259 void buildLayerNode() final;
260 DrawableList renderList() final;
261 protected:
262 void preprocessStage(const VRect& clip) final;
263 void updateContent() final;
264 private:
265 LOTDrawable mRenderNode;
266 VTexture mTexture;
267 VDrawable *mDrawableList{nullptr}; //to work with the Span api
268 };
269
270 class LOTMaskItem
271 {
272 public:
273 explicit LOTMaskItem(LOTMaskData *data): mData(data){}
274 void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag);
275 LOTMaskData::Mode maskMode() const { return mData->mMode;}
276 VRle rle();
277 void preprocess(const VRect &clip);
278 public:
279 LOTMaskData *mData{nullptr};
280 VPath mLocalPath;
281 VPath mFinalPath;
282 VRasterizer mRasterizer;
283 float mCombinedAlpha{0};
284 bool mRasterRequest{false};
285 };
286
287 /*
288 * Handels mask property of a layer item
289 */
290 class LOTLayerMaskItem
291 {
292 public:
293 explicit LOTLayerMaskItem(LOTLayerData *layerData);
294 void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag);
295 bool isStatic() const {return mStatic;}
296 VRle maskRle(const VRect &clipRect);
297 void preprocess(const VRect &clip);
298 public:
299 std::vector<LOTMaskItem> mMasks;
300 VRle mRle;
301 bool mStatic{true};
302 bool mDirty{true};
303 };
304
305 class LOTPathDataItem;
306 class LOTPaintDataItem;
307 class LOTTrimItem;
308
309 enum class ContentType : uchar
310 {
311 Unknown,
312 Group,
313 Path,
314 Paint,
315 Trim
316 };
317
318 class LOTContentGroupItem;
319 class LOTContentItem
320 {
321 public:
322 virtual ~LOTContentItem() = default;
323 LOTContentItem& operator=(LOTContentItem&&) noexcept = delete;
324 virtual void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag) = 0; virtual void renderList(std::vector<VDrawable *> &){}
325 virtual bool resolveKeyPath(LOTKeyPath &, uint, LOTVariant &) {return false;}
326 virtual ContentType type() const {return ContentType::Unknown;}
327 };
328
329 class LOTContentGroupItem: public LOTContentItem
330 {
331 public:
332 LOTContentGroupItem() = default;
333 explicit LOTContentGroupItem(LOTGroupData *data, VArenaAlloc* allocator);
334 void addChildren(LOTGroupData *data, VArenaAlloc* allocator);
335 void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag) override;
336 void applyTrim();
337 void processTrimItems(std::vector<LOTPathDataItem *> &list);
338 void processPaintItems(std::vector<LOTPathDataItem *> &list);
339 void renderList(std::vector<VDrawable *> &list) override;
340 ContentType type() const final {return ContentType::Group;}
341 const VMatrix & matrix() const { return mMatrix;}
342 const char* name() const
343 {
344 static const char* TAG = "__";
345 return mModel.hasModel() ? mModel.name() : TAG;
346 }
347 bool resolveKeyPath(LOTKeyPath &keyPath, uint depth, LOTVariant &value) override;
348 protected:
349 std::vector<LOTContentItem*> mContents;
350 VMatrix mMatrix;
351 private:
352 LOTProxyModel<LOTGroupData> mModel;
353 };
354
355 class LOTPathDataItem : public LOTContentItem
356 {
357 public:
358 LOTPathDataItem(bool staticPath): mStaticPath(staticPath){}
359 void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag) final;
360 ContentType type() const final {return ContentType::Path;}
361 bool dirty() const {return mDirtyPath;}
362 const VPath &localPath() const {return mTemp;}
363 void finalPath(VPath& result);
364 void updatePath(const VPath &path) {mTemp = path; mDirtyPath = true;}
365 bool staticPath() const { return mStaticPath; }
366 void setParent(LOTContentGroupItem *parent) {mParent = parent;}
367 LOTContentGroupItem *parent() const {return mParent;}
368 protected:
369 virtual void updatePath(VPath& path, int frameNo) = 0;
370 virtual bool hasChanged(int prevFrame, int curFrame) = 0;
371 private:
372 bool hasChanged(int frameNo) {
373 int prevFrame = mFrameNo;
374 mFrameNo = frameNo;
375 if (prevFrame == -1) return true;
376 if (mStaticPath ||
377 (prevFrame == frameNo)) return false;
378 return hasChanged(prevFrame, frameNo);
379 }
380 LOTContentGroupItem *mParent{nullptr};
381 VPath mLocalPath;
382 VPath mTemp;
383 int mFrameNo{-1};
384 bool mDirtyPath{true};
385 bool mStaticPath;
386 };
387
388 class LOTRectItem: public LOTPathDataItem
389 {
390 public:
391 explicit LOTRectItem(LOTRectData *data);
392 protected:
393 void updatePath(VPath& path, int frameNo) final;
394 LOTRectData *mData{nullptr};
395
396 bool hasChanged(int prevFrame, int curFrame) final {
397 return (mData->mPos.changed(prevFrame, curFrame) ||
398 mData->mSize.changed(prevFrame, curFrame) ||
399 mData->mRound.changed(prevFrame, curFrame));
400 }
401 };
402
403 class LOTEllipseItem: public LOTPathDataItem
404 {
405 public:
406 explicit LOTEllipseItem(LOTEllipseData *data);
407 private:
408 void updatePath(VPath& path, int frameNo) final;
409 LOTEllipseData *mData{nullptr};
410 bool hasChanged(int prevFrame, int curFrame) final {
411 return (mData->mPos.changed(prevFrame, curFrame) ||
412 mData->mSize.changed(prevFrame, curFrame));
413 }
414 };
415
416 class LOTShapeItem: public LOTPathDataItem
417 {
418 public:
419 explicit LOTShapeItem(LOTShapeData *data);
420 private:
421 void updatePath(VPath& path, int frameNo) final;
422 LOTShapeData *mData{nullptr};
423 bool hasChanged(int prevFrame, int curFrame) final {
424 return mData->mShape.changed(prevFrame, curFrame);
425 }
426 };
427
428 class LOTPolystarItem: public LOTPathDataItem
429 {
430 public:
431 explicit LOTPolystarItem(LOTPolystarData *data);
432 private:
433 void updatePath(VPath& path, int frameNo) final;
434 LOTPolystarData *mData{nullptr};
435
436 bool hasChanged(int prevFrame, int curFrame) final {
437 return (mData->mPos.changed(prevFrame, curFrame) ||
438 mData->mPointCount.changed(prevFrame, curFrame) ||
439 mData->mInnerRadius.changed(prevFrame, curFrame) ||
440 mData->mOuterRadius.changed(prevFrame, curFrame) ||
441 mData->mInnerRoundness.changed(prevFrame, curFrame) ||
442 mData->mOuterRoundness.changed(prevFrame, curFrame) ||
443 mData->mRotation.changed(prevFrame, curFrame));
444 }
445 };
446
447
448
449 class LOTPaintDataItem : public LOTContentItem
450 {
451 public:
452 LOTPaintDataItem(bool staticContent);
453 void addPathItems(std::vector<LOTPathDataItem *> &list, size_t startOffset);
454 void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag) override;
455 void renderList(std::vector<VDrawable *> &list) final;
456 ContentType type() const final {return ContentType::Paint;}
457 protected:
458 virtual bool updateContent(int frameNo, const VMatrix &matrix, float alpha) = 0;
459 private:
460 void updateRenderNode();
461 protected:
462 std::vector<LOTPathDataItem *> mPathItems;
463 LOTDrawable mDrawable;
464 VPath mPath;
465 DirtyFlag mFlag;
466 bool mStaticContent;
467 bool mRenderNodeUpdate{true};
468 bool mContentToRender{true};
469 };
470
471 class LOTFillItem : public LOTPaintDataItem
472 {
473 public:
474 explicit LOTFillItem(LOTFillData *data);
475 protected:
476 bool updateContent(int frameNo, const VMatrix &matrix, float alpha) final;
477 bool resolveKeyPath(LOTKeyPath &keyPath, uint depth, LOTVariant &value) final;
478 private:
479 LOTProxyModel<LOTFillData> mModel;
480 };
481
482 class LOTGFillItem : public LOTPaintDataItem
483 {
484 public:
485 explicit LOTGFillItem(LOTGFillData *data);
486 protected:
487 bool updateContent(int frameNo, const VMatrix &matrix, float alpha) final;
488 private:
489 LOTGFillData *mData{nullptr};
490 std::unique_ptr<VGradient> mGradient;
491 };
492
493 class LOTStrokeItem : public LOTPaintDataItem
494 {
495 public:
496 explicit LOTStrokeItem(LOTStrokeData *data);
497 protected:
498 bool updateContent(int frameNo, const VMatrix &matrix, float alpha) final;
499 bool resolveKeyPath(LOTKeyPath &keyPath, uint depth, LOTVariant &value) final;
500 private:
501 LOTProxyModel<LOTStrokeData> mModel;
502 };
503
504 class LOTGStrokeItem : public LOTPaintDataItem
505 {
506 public:
507 explicit LOTGStrokeItem(LOTGStrokeData *data);
508 protected:
509 bool updateContent(int frameNo, const VMatrix &matrix, float alpha) final;
510 private:
511 LOTGStrokeData *mData{nullptr};
512 std::unique_ptr<VGradient> mGradient;
513 };
514
515
516 // Trim Item
517
518 class LOTTrimItem : public LOTContentItem
519 {
520 public:
521 explicit LOTTrimItem(LOTTrimData *data);
522 void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag) final;
523 ContentType type() const final {return ContentType::Trim;}
524 void update();
525 void addPathItems(std::vector<LOTPathDataItem *> &list, size_t startOffset);
526 private:
527 bool pathDirty() const {
528 for (auto &i : mPathItems) {
529 if (i->dirty())
530 return true;
531 }
532 return false;
533 }
534 struct Cache {
535 int mFrameNo{-1};
536 LOTTrimData::Segment mSegment{};
537 };
538 Cache mCache;
539 std::vector<LOTPathDataItem *> mPathItems;
540 LOTTrimData *mData{nullptr};
541 VPathMesure mPathMesure;
542 bool mDirty{true};
543 };
544
545 class LOTRepeaterItem : public LOTContentGroupItem
546 {
547 public:
548 explicit LOTRepeaterItem(LOTRepeaterData *data, VArenaAlloc* allocator);
549 void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag) final;
550 void renderList(std::vector<VDrawable *> &list) final;
551 private:
552 LOTRepeaterData *mRepeaterData{nullptr};
553 bool mHidden{false};
554 int mCopies{0};
555 };
556
557
558 #endif // LOTTIEITEM_H
559
560