lottie: pass matteSource and bitmap to layer's render function
[platform/core/uifw/lottie-player.git] / src / lottie / lottieitem.h
1 #ifndef LOTTIEITEM_H
2 #define LOTTIEITEM_H
3
4 #include<lottiemodel.h>
5 #include<sstream>
6 #include<memory>
7
8 #include"vmatrix.h"
9 #include"vpath.h"
10 #include"vpoint.h"
11 #include"vpathmesure.h"
12 #include"lottieplayer.h"
13 #include"vpainter.h"
14 #include"vdrawable.h"
15
16 V_USE_NAMESPACE
17
18 enum class DirtyFlagBit : uchar
19 {
20    None   = 0x00,
21    Matrix = 0x01,
22    Alpha  = 0x02,
23    All    = (Matrix | Alpha)
24 };
25
26 class LOTLayerItem;
27 class LOTMaskItem;
28 class VDrawable;
29
30 class LOTCompItem
31 {
32 public:
33    LOTCompItem(LOTModel *model);
34    static LOTLayerItem * createLayerItem(LOTLayerData *layerData);
35    bool update(int frameNo);
36    void resize(const VSize &size);
37    VSize size() const;
38    const std::vector<LOTNode *>& renderList()const;
39    void buildRenderList();
40    bool render(const LOTBuffer &buffer);
41 private:
42    VMatrix                                    mScaleMatrix;
43    VSize                                      mViewSize;
44    LOTModel                                   *mRootModel;
45    LOTCompositionData                         *mCompData;
46    std::unique_ptr<LOTLayerItem>               mRootLayer;
47    bool                                        mUpdateViewBox;
48    int                                         mCurFrameNo;
49    std::vector<LOTNode *>                      mRenderList;
50    std::vector<VDrawable *>                    mDrawableList;
51 };
52
53 typedef vFlag<DirtyFlagBit> DirtyFlag;
54 class LOTLayerItem
55 {
56 public:
57    LOTLayerItem(LOTLayerData *layerData);
58    virtual ~LOTLayerItem(){}
59    int id() const {return mLayerData->id();}
60    int parentId() const {return mLayerData->parentId();}
61    void setParentLayer(LOTLayerItem *parent){mParentLayer = parent;}
62    void setPrecompLayer(LOTLayerItem *precomp){mPrecompLayer = precomp;}
63    virtual void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha);
64    VMatrix matrix(int frameNo) const;
65    virtual void renderList(std::vector<VDrawable *> &list){}
66    virtual void updateStaticProperty();
67    virtual void render(VPainter *painter, const VRle &mask, LOTLayerItem *matteSource);
68    bool hasMatte() { if (mLayerData->mMatteType == MatteType::None) return false; return true; }
69
70 protected:
71    virtual void updateContent() = 0;
72    inline VMatrix combinedMatrix() const {return mCombinedMatrix;}
73    inline int frameNo() const {return mFrameNo;}
74    inline float combinedAlpha() const {return mCombinedAlpha;}
75    inline bool isStatic() const {return mStatic;}
76    float opacity(int frameNo) const;
77    bool visible() const;
78    inline DirtyFlag flag() const {return mDirtyFlag;}
79    VRle maskRle(const VRect &clipRect);
80    bool hasMask() const {return !mMasks.empty();}
81 protected:
82    std::vector<std::unique_ptr<LOTMaskItem>>   mMasks;
83    LOTLayerData                               *mLayerData;
84    LOTLayerItem                               *mParentLayer;
85    LOTLayerItem                               *mPrecompLayer;
86    VMatrix                                    mCombinedMatrix;
87    float                                       mCombinedAlpha;
88    int                                         mFrameNo;
89    DirtyFlag                                   mDirtyFlag;
90    bool                                        mVisible;
91    bool                                        mStatic;
92 };
93
94 class LOTCompLayerItem: public LOTLayerItem
95 {
96 public:
97    ~LOTCompLayerItem();
98    LOTCompLayerItem(LOTLayerData *layerData);
99    void renderList(std::vector<VDrawable *> &list)final;
100    void updateStaticProperty() final;
101    void render(VPainter *painter, const VRle &mask, LOTLayerItem *matteSource) final;
102 protected:
103    void updateContent() final;
104 private:
105    std::vector<LOTLayerItem *>                  mLayers;
106    std::unordered_map<int, LOTLayerItem *>      mLayerMap;
107    int                                          mLastFrame;
108 };
109
110 class LOTSolidLayerItem: public LOTLayerItem
111 {
112 public:
113    LOTSolidLayerItem(LOTLayerData *layerData);
114 protected:
115    void updateContent() final;
116    void renderList(std::vector<VDrawable *> &list) final;
117 private:
118    std::unique_ptr<VDrawable>   mRenderNode;
119 };
120
121 class LOTContentItem;
122 class LOTContentGroupItem;
123 class LOTShapeLayerItem: public LOTLayerItem
124 {
125 public:
126    ~LOTShapeLayerItem();
127    LOTShapeLayerItem(LOTLayerData *layerData);
128    static LOTContentItem * createContentItem(LOTData *contentData);
129    void renderList(std::vector<VDrawable *> &list)final;
130 protected:
131    void updateContent() final;
132    LOTContentGroupItem       *mRoot;
133 };
134
135 class LOTNullLayerItem: public LOTLayerItem
136 {
137 public:
138    LOTNullLayerItem(LOTLayerData *layerData);
139 protected:
140    void updateContent() final;
141 };
142
143 class LOTMaskItem
144 {
145 public:
146     LOTMaskItem(LOTMaskData *data): mData(data), mCombinedAlpha(0){}
147     void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag);
148     LOTMaskData::Mode maskMode() const { return mData->mMode;}
149     VRle rle();
150 public:
151     LOTMaskData             *mData;
152     float                    mCombinedAlpha;
153     VMatrix                  mCombinedMatrix;
154     VPath                    mLocalPath;
155     std::future<VRle>        mRleTask;
156     VRle                     mRle;
157 };
158
159 class LOTDrawable : public VDrawable
160 {
161 public:
162     void sync();
163 public:
164     LOTNode           mCNode;
165 };
166
167 class LOTNode;
168 class LOTPathDataItem;
169 class LOTPaintDataItem;
170 class LOTTrimItem;
171 struct LOTRenderNode
172 {
173    LOTRenderNode(LOTPathDataItem *path, LOTPaintDataItem *paint, VDrawable *render, bool sameG)
174                   :pathNodeRef(path), paintNodeRef(paint), drawable(render), sameGroup(sameG){}
175    LOTPathDataItem  *pathNodeRef;
176    LOTPaintDataItem *paintNodeRef;
177    VDrawable        *drawable;
178    bool              sameGroup;
179 };
180
181 class LOTContentItem
182 {
183 public:
184    LOTContentItem(){}
185    virtual ~LOTContentItem(){}
186    virtual void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag) = 0;
187    virtual void renderList(std::vector<VDrawable *> &list){}
188 };
189
190 class LOTContentGroupItem: public LOTContentItem
191 {
192 public:
193    ~LOTContentGroupItem();
194    LOTContentGroupItem(LOTShapeGroupData *data);
195    void addChildren(LOTGroupData *data);
196    void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag) final;
197    void processPaintOperation();
198    void processTrimOperation();
199    void renderList(std::vector<VDrawable *> &list) final;
200 private:
201    void paintOperationHelper(std::vector<LOTPaintDataItem *> &list);
202    void trimOperationHelper(std::vector<LOTTrimItem *> &list);
203    LOTShapeGroupData                 *mData;
204    std::vector<LOTContentItem *>      mContents;
205 };
206
207 class LOTPathDataItem : public LOTContentItem
208 {
209 public:
210    LOTPathDataItem(bool staticPath):mInit(false), mStaticPath(staticPath){}
211    void addPaintOperation(std::vector<LOTPaintDataItem *> &list, int externalCount);
212    void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag) final;
213    VPath path() const;
214    void addTrimOperation(std::vector<LOTTrimItem *> &list);
215    inline float combinedAlpha() const{ return mCombinedAlpha;}
216    void renderList(std::vector<VDrawable *> &list) final;
217 private:
218    std::vector<LOTTrimItem *>              mTrimNodeRefs;
219    std::vector<LOTRenderNode>              mRenderList;
220    std::vector<std::unique_ptr<VDrawable>> mNodeList;
221    bool                                    mInit;
222    bool                                    mStaticPath;
223    VPath                                  mLocalPath;
224    VPath                                  mFinalPath;
225    bool                                    mPathChanged;
226    float                                   mCombinedAlpha;
227 protected:
228    virtual void updatePath(VPath& path, int frameNo) = 0;
229    virtual bool hasChanged(int frameNo) = 0;
230 };
231
232 class LOTRectItem: public LOTPathDataItem
233 {
234 public:
235    LOTRectItem(LOTRectData *data);
236 protected:
237    void updatePath(VPath& path, int frameNo) final;
238    LOTRectData           *mData;
239
240    struct Cache {
241         int                  mFrameNo{-1};
242         VPointF              mPos;
243         VPointF              mSize;
244         float                mRoundness;
245    };
246    Cache                     mCache;
247
248    void updateCache(int frameNo, VPointF pos, VPointF size, float roundness) {
249         mCache.mFrameNo = frameNo;
250         mCache.mPos = pos;
251         mCache.mSize = size;
252         mCache.mRoundness = roundness;
253    }
254    bool hasChanged(int frameNo) final {
255         if (mCache.mFrameNo == frameNo) return false;
256
257         VPointF pos = mData->mPos.value(frameNo);
258         VPointF size = mData->mSize.value(frameNo);
259         float   roundness = mData->mRound.value(frameNo);
260
261         if (vCompare(mCache.mPos.x(), pos.x()) && vCompare(mCache.mPos.y(), pos.y()) &&
262             vCompare(mCache.mSize.x(), size.x()) && vCompare(mCache.mSize.y(), size.y()) &&
263             vCompare(mCache.mRoundness, roundness))
264           return false;
265
266         return true;
267    }
268 };
269
270 class LOTEllipseItem: public LOTPathDataItem
271 {
272 public:
273    LOTEllipseItem(LOTEllipseData *data);
274 private:
275    void updatePath(VPath& path, int frameNo) final;
276    LOTEllipseData           *mData;
277
278    struct Cache {
279         int                  mFrameNo{-1};
280         VPointF              mPos;
281         VPointF              mSize;
282    };
283    Cache                     mCache;
284
285    void updateCache(int frameNo, VPointF pos, VPointF size) {
286         mCache.mFrameNo = frameNo;
287         mCache.mPos = pos;
288         mCache.mSize = size;
289    }
290    bool hasChanged(int frameNo) final {
291         if (mCache.mFrameNo == frameNo) return false;
292
293         VPointF pos = mData->mPos.value(frameNo);
294         VPointF size = mData->mSize.value(frameNo);
295
296         if (vCompare(mCache.mPos.x(), pos.x()) && vCompare(mCache.mPos.y(), pos.y()) &&
297             vCompare(mCache.mSize.x(), size.x()) && vCompare(mCache.mSize.y(), size.y()))
298           return false;
299
300         return true;
301    }
302 };
303
304 class LOTShapeItem: public LOTPathDataItem
305 {
306 public:
307    LOTShapeItem(LOTShapeData *data);
308 private:
309    void updatePath(VPath& path, int frameNo) final;
310    LOTShapeData             *mData;
311    bool hasChanged(int frameNo) final { return true; }
312 };
313
314 class LOTPolystarItem: public LOTPathDataItem
315 {
316 public:
317    LOTPolystarItem(LOTPolystarData *data);
318 private:
319    void updatePath(VPath& path, int frameNo) final;
320    LOTPolystarData             *mData;
321
322    struct Cache {
323         int                     mFrameNo{-1};
324         VPointF                 mPos;
325         float                   mPoints{0};
326         float                   mInnerRadius{0};
327         float                   mOuterRadius{0};
328         float                   mInnerRoundness{0};
329         float                   mOuterRoundness{0};
330         float                   mRotation{0};
331    };
332    Cache                        mCache;
333
334    void updateCache(int frameNo, VPointF pos, float points, float innerRadius, float outerRadius,
335                     float innerRoundness, float outerRoundness, float rotation) {
336         mCache.mFrameNo = frameNo;
337         mCache.mPos = pos;
338         mCache.mPoints = points;
339         mCache.mInnerRadius = innerRadius;
340         mCache.mOuterRadius = outerRadius;
341         mCache.mInnerRoundness = innerRoundness;
342         mCache.mOuterRoundness = outerRoundness;
343         mCache.mRotation = rotation;
344    }
345    bool hasChanged(int frameNo) final {
346         if (mCache.mFrameNo == frameNo) return false;
347
348         VPointF pos = mData->mPos.value(frameNo);
349         float   points = mData->mPointCount.value(frameNo);
350         float   innerRadius = mData->mInnerRadius.value(frameNo);
351         float   outerRadius = mData->mOuterRadius.value(frameNo);
352         float   innerRoundness = mData->mInnerRoundness.value(frameNo);
353         float   outerRoundness = mData->mOuterRoundness.value(frameNo);
354         float   rotation = mData->mRotation.value(frameNo);
355
356         if (vCompare(mCache.mPos.x(), pos.x()) && vCompare(mCache.mPos.y(), pos.y()) &&
357             vCompare(mCache.mPoints, points) && vCompare(mCache.mRotation, rotation) &&
358             vCompare(mCache.mInnerRadius, innerRadius) && vCompare(mCache.mOuterRadius, outerRadius) &&
359             vCompare(mCache.mInnerRoundness, innerRoundness) && vCompare(mCache.mOuterRoundness, outerRoundness))
360           return false;
361
362         return true;
363    }
364 };
365
366
367
368 class LOTPaintDataItem : public LOTContentItem
369 {
370 public:
371    LOTPaintDataItem(bool staticContent):mInit(false), mStaticContent(staticContent){}
372    virtual void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag);
373    virtual void updateRenderNode(LOTPathDataItem *pathNode, VDrawable *renderer, bool sameParent) = 0;
374 protected:
375    virtual void updateContent(int frameNo) = 0;
376    inline float parentAlpha() const {return mParentAlpha;}
377    inline bool  contentChanged() const{return mContentChanged;}
378 public:
379    float         mParentAlpha;
380    VMatrix      mParentMatrix;
381    DirtyFlag     mFlag;
382    int           mFrameNo;
383    bool          mInit;
384    bool          mStaticContent;
385    bool          mContentChanged;
386 };
387
388 class LOTFillItem : public LOTPaintDataItem
389 {
390 public:
391    LOTFillItem(LOTFillData *data);
392 protected:
393    void updateContent(int frameNo) final;
394    void updateRenderNode(LOTPathDataItem *pathNode, VDrawable *renderer, bool sameParent) final;
395 private:
396    LOTFillData             *mData;
397    VColor                  mColor;
398    FillRule                mFillRule;
399 };
400
401 class LOTGFillItem : public LOTPaintDataItem
402 {
403 public:
404    LOTGFillItem(LOTGFillData *data);
405 protected:
406    void updateContent(int frameNo) final;
407    void updateRenderNode(LOTPathDataItem *pathNode, VDrawable *renderer, bool sameParent) final;
408 private:
409    LOTGFillData                 *mData;
410    std::unique_ptr<VGradient>    mGradient;
411    FillRule                      mFillRule;
412 };
413
414 class LOTStrokeItem : public LOTPaintDataItem
415 {
416 public:
417    LOTStrokeItem(LOTStrokeData *data);
418 protected:
419    void updateContent(int frameNo) final;
420    void updateRenderNode(LOTPathDataItem *pathNode, VDrawable *renderer, bool sameParent) final;
421 private:
422    LOTStrokeData             *mData;
423    CapStyle                  mCap;
424    JoinStyle                 mJoin;
425    float                     mMiterLimit;
426    VColor                    mColor;
427    float                     mWidth;
428    float                     mDashArray[6];
429    int                       mDashArraySize;
430 };
431
432 class LOTGStrokeItem : public LOTPaintDataItem
433 {
434 public:
435    LOTGStrokeItem(LOTGStrokeData *data);
436 protected:
437    void updateContent(int frameNo) final;
438    void updateRenderNode(LOTPathDataItem *pathNode, VDrawable *renderer, bool sameParent) final;
439 private:
440    LOTGStrokeData               *mData;
441    std::unique_ptr<VGradient>    mGradient;
442    CapStyle                      mCap;
443    JoinStyle                     mJoin;
444    float                         mMiterLimit;
445    VColor                        mColor;
446    float                         mWidth;
447    float                         mDashArray[6];
448    int                           mDashArraySize;
449 };
450
451
452 // Trim Item
453
454 class LOTTrimItem : public LOTContentItem
455 {
456 public:
457    LOTTrimItem(LOTTrimData *data);
458    void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag) final;
459    float getStart(int frameNo) {return mData->mStart.value(frameNo);}
460    float getEnd(int frameNo) {return mData->mEnd.value(frameNo);}
461 private:
462    LOTTrimData             *mData;
463 };
464
465 class LOTRepeaterItem : public LOTContentItem
466 {
467 public:
468    LOTRepeaterItem(LOTRepeaterData *data);
469    virtual void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag) final;
470    virtual void renderList(std::vector<VDrawable *> &list) final;
471 private:
472    LOTRepeaterData             *mData;
473 };
474
475
476 #endif // LOTTIEITEM_H
477
478