lottie/example: Added image_test.json(resource with image resource )
[platform/core/uifw/lottie-player.git] / src / lottie / lottieitem.h
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<lottiemodel.h>
23 #include<sstream>
24 #include<memory>
25
26 #include"vmatrix.h"
27 #include"vpath.h"
28 #include"vpoint.h"
29 #include"vpathmesure.h"
30 #include"lottiecommon.h"
31 #include"lottieanimation.h"
32 #include"vpainter.h"
33 #include"vdrawable.h"
34
35 V_USE_NAMESPACE
36
37 enum class DirtyFlagBit : uchar
38 {
39    None   = 0x00,
40    Matrix = 0x01,
41    Alpha  = 0x02,
42    All    = (Matrix | Alpha)
43 };
44
45 class LOTLayerItem;
46 class LOTMaskItem;
47 class VDrawable;
48
49 class LOTCompItem
50 {
51 public:
52    LOTCompItem(LOTModel *model);
53    static std::unique_ptr<LOTLayerItem> createLayerItem(LOTLayerData *layerData);
54    bool update(int frameNo);
55    void resize(const VSize &size);
56    VSize size() const;
57    void buildRenderTree();
58    const LOTLayerNode * renderTree()const;
59    bool render(const lottie::Surface &surface);
60 private:
61    VMatrix                                    mScaleMatrix;
62    VSize                                      mViewSize;
63    LOTModel                                   *mRootModel;
64    LOTCompositionData                         *mCompData;
65    std::unique_ptr<LOTLayerItem>               mRootLayer;
66    bool                                        mUpdateViewBox;
67    int                                         mCurFrameNo;
68    std::vector<LOTNode *>                      mRenderList;
69    std::vector<VDrawable *>                    mDrawableList;
70 };
71
72 class LOTLayerMaskItem;
73
74 class LOTClipperItem
75 {
76 public:
77     LOTClipperItem(VSize size): mSize(size){}
78     void update(const VMatrix &matrix);
79     VRle rle();
80 public:
81     VSize                    mSize;
82     VPath                    mPath;
83     RleShare                 mRleFuture;
84     VRle                     mRle;
85 };
86
87 typedef vFlag<DirtyFlagBit> DirtyFlag;
88 class LOTLayerItem
89 {
90 public:
91    LOTLayerItem(LOTLayerData *layerData);
92    virtual ~LOTLayerItem()= default;
93    int id() const {return mLayerData->id();}
94    int parentId() const {return mLayerData->parentId();}
95    void setParentLayer(LOTLayerItem *parent){mParentLayer = parent;}
96    void setPrecompLayer(LOTLayerItem *precomp){mPrecompLayer = precomp;}
97    virtual void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha);
98    VMatrix matrix(int frameNo) const;
99    virtual void renderList(std::vector<VDrawable *> &){}
100    virtual void updateStaticProperty();
101    virtual void render(VPainter *painter, const VRle &mask, const VRle &matteRle);
102    bool hasMatte() { if (mLayerData->mMatteType == MatteType::None) return false; return true; }
103    MatteType matteType() const { return mLayerData->mMatteType;}
104    bool visible() const;
105    virtual void buildLayerNode();
106    LOTLayerNode * layerNode() const {return mLayerCNode.get();}
107 protected:
108    virtual void updateContent() = 0;
109    inline VMatrix combinedMatrix() const {return mCombinedMatrix;}
110    inline int frameNo() const {return mFrameNo;}
111    inline float combinedAlpha() const {return mCombinedAlpha;}
112    inline bool isStatic() const {return mStatic;}
113    float opacity(int frameNo) const;
114    inline DirtyFlag flag() const {return mDirtyFlag;}
115 protected:
116    std::vector<LOTMask>                        mMasksCNode;
117    std::unique_ptr<LOTLayerNode>               mLayerCNode;
118    std::vector<VDrawable *>                    mDrawableList;
119    std::unique_ptr<LOTLayerMaskItem>           mLayerMask;
120    LOTLayerData                               *mLayerData{nullptr};
121    LOTLayerItem                               *mParentLayer{nullptr};
122    LOTLayerItem                               *mPrecompLayer{nullptr};
123    VMatrix                                     mCombinedMatrix;
124    float                                       mCombinedAlpha{0.0};
125    int                                         mFrameNo{-1};
126    DirtyFlag                                   mDirtyFlag{DirtyFlagBit::All};
127    bool                                        mStatic{false};
128 };
129
130 class LOTCompLayerItem: public LOTLayerItem
131 {
132 public:
133    LOTCompLayerItem(LOTLayerData *layerData);
134    void renderList(std::vector<VDrawable *> &list)final;
135    void updateStaticProperty() final;
136    void render(VPainter *painter, const VRle &mask, const VRle &matteRle) final;
137    void buildLayerNode() final;
138 protected:
139    void updateContent() final;
140 private:
141     void renderMatteLayer(VPainter *painter, const VRle &inheritMask, const VRle &matteRle,
142                           LOTLayerItem *layer, LOTLayerItem *src);
143 private:
144    std::vector<LOTLayerNode *>                  mLayersCNode;
145    std::vector<std::unique_ptr<LOTLayerItem>>   mLayers;
146    std::unique_ptr<LOTClipperItem>              mClipper;
147 };
148
149 class LOTSolidLayerItem: public LOTLayerItem
150 {
151 public:
152    LOTSolidLayerItem(LOTLayerData *layerData);
153    void buildLayerNode() final;
154 protected:
155    void updateContent() final;
156    void renderList(std::vector<VDrawable *> &list) final;
157 private:
158    std::vector<LOTNode *>       mCNodeList;
159    std::unique_ptr<VDrawable>   mRenderNode;
160 };
161
162 class LOTContentItem;
163 class LOTContentGroupItem;
164 class LOTShapeLayerItem: public LOTLayerItem
165 {
166 public:
167    LOTShapeLayerItem(LOTLayerData *layerData);
168    static std::unique_ptr<LOTContentItem> createContentItem(LOTData *contentData);
169    void renderList(std::vector<VDrawable *> &list)final;
170    void buildLayerNode() final;
171 protected:
172    void updateContent() final;
173    std::vector<LOTNode *>               mCNodeList;
174    std::unique_ptr<LOTContentGroupItem> mRoot;
175 };
176
177 class LOTNullLayerItem: public LOTLayerItem
178 {
179 public:
180    LOTNullLayerItem(LOTLayerData *layerData);
181 protected:
182    void updateContent() final;
183 };
184
185 class LOTImageLayerItem: public LOTLayerItem
186 {
187 public:
188    LOTImageLayerItem(LOTLayerData *layerData);
189    void buildLayerNode() final;
190 protected:
191    void updateContent() final;
192    void renderList(std::vector<VDrawable *> &list) final;
193 private:
194    std::vector<LOTNode *>       mCNodeList;
195    std::unique_ptr<VDrawable>   mRenderNode;
196 };
197
198 class LOTMaskItem
199 {
200 public:
201     LOTMaskItem(LOTMaskData *data): mData(data), mCombinedAlpha(0){}
202     void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag);
203     LOTMaskData::Mode maskMode() const { return mData->mMode;}
204     VRle rle();
205 public:
206     LOTMaskData             *mData;
207     float                    mCombinedAlpha;
208     VMatrix                  mCombinedMatrix;
209     VPath                    mLocalPath;
210     VPath                    mFinalPath;
211     RleShare                 mRleFuture;
212     VRle                     mRle;
213 };
214
215 /*
216  * Handels mask property of a layer item
217  */
218 class LOTLayerMaskItem
219 {
220 public:
221     LOTLayerMaskItem(LOTLayerData *layerData);
222     void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag);
223     bool isStatic() const {return mStatic;}
224     VRle maskRle(const VRect &clipRect);
225 public:
226     std::vector<LOTMaskItem>   mMasks;
227     VRle                       mRle;
228     bool                       mStatic{true};
229     bool                       mDirty{true};
230 };
231
232 class LOTDrawable : public VDrawable
233 {
234 public:
235     void sync();
236 public:
237     std::unique_ptr<LOTNode>  mCNode;
238
239     ~LOTDrawable() {
240         if (mCNode && mCNode->mGradient.stopPtr)
241           free(mCNode->mGradient.stopPtr);
242     }
243 };
244
245 class LOTPathDataItem;
246 class LOTPaintDataItem;
247 class LOTTrimItem;
248
249 class LOTContentItem
250 {
251 public:
252    virtual ~LOTContentItem()= default;
253    virtual void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag) = 0;
254    virtual void renderList(std::vector<VDrawable *> &){}
255    void setParent(LOTContentItem *parent) {mParent = parent;}
256    LOTContentItem *parent() const {return mParent;}
257 private:
258    LOTContentItem *mParent{nullptr};
259 };
260
261 class LOTContentGroupItem: public LOTContentItem
262 {
263 public:
264     LOTContentGroupItem(){}
265    LOTContentGroupItem(LOTGroupData *data);
266    void addChildren(LOTGroupData *data);
267    void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag);
268    void applyTrim();
269    void processTrimItems(std::vector<LOTPathDataItem *> &list);
270    void processPaintItems(std::vector<LOTPathDataItem *> &list);
271    void renderList(std::vector<VDrawable *> &list) final;
272    const VMatrix & matrix() const { return mMatrix;}
273 protected:
274    LOTGroupData                                  *mData{nullptr};
275    std::vector<std::unique_ptr<LOTContentItem>>   mContents;
276    VMatrix                                        mMatrix;
277 };
278
279 class LOTPathDataItem : public LOTContentItem
280 {
281 public:
282    LOTPathDataItem(bool staticPath): mStaticPath(staticPath){}
283    void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag) final;
284    bool dirty() const {return mPathChanged;}
285    const VPath &localPath() const {return mTemp;}
286    const VPath &finalPath();
287    void updatePath(const VPath &path) {mTemp = path; mPathChanged = true; mNeedUpdate = true;}
288    bool staticPath() const { return mStaticPath; }
289 protected:
290    virtual void updatePath(VPath& path, int frameNo) = 0;
291    virtual bool hasChanged(int prevFrame, int curFrame) = 0;
292 private:
293    bool hasChanged(int frameNo) {
294        int prevFrame = mFrameNo;
295        mFrameNo = frameNo;
296        if (prevFrame == -1) return true;
297        if (mStaticPath ||
298            (prevFrame == frameNo)) return false;
299        return hasChanged(prevFrame, frameNo);
300    }
301    VPath                                   mLocalPath;
302    VPath                                   mTemp;
303    VPath                                   mFinalPath;
304    int                                     mFrameNo{-1};
305    bool                                    mPathChanged{true};
306    bool                                    mNeedUpdate{true};
307    bool                                    mStaticPath;
308 };
309
310 class LOTRectItem: public LOTPathDataItem
311 {
312 public:
313    LOTRectItem(LOTRectData *data);
314 protected:
315    void updatePath(VPath& path, int frameNo) final;
316    LOTRectData           *mData;
317
318    bool hasChanged(int prevFrame, int curFrame) final {
319        return (mData->mPos.changed(prevFrame, curFrame) ||
320                mData->mSize.changed(prevFrame, curFrame) ||
321                mData->mRound.changed(prevFrame, curFrame)) ? true : false;
322    }
323 };
324
325 class LOTEllipseItem: public LOTPathDataItem
326 {
327 public:
328    LOTEllipseItem(LOTEllipseData *data);
329 private:
330    void updatePath(VPath& path, int frameNo) final;
331    LOTEllipseData           *mData;
332    bool hasChanged(int prevFrame, int curFrame) final {
333        return (mData->mPos.changed(prevFrame, curFrame) ||
334                mData->mSize.changed(prevFrame, curFrame)) ? true : false;
335    }
336 };
337
338 class LOTShapeItem: public LOTPathDataItem
339 {
340 public:
341    LOTShapeItem(LOTShapeData *data);
342 private:
343    void updatePath(VPath& path, int frameNo) final;
344    LOTShapeData             *mData;
345    bool hasChanged(int prevFrame, int curFrame) final {
346        return mData->mShape.changed(prevFrame, curFrame);
347    }
348 };
349
350 class LOTPolystarItem: public LOTPathDataItem
351 {
352 public:
353    LOTPolystarItem(LOTPolystarData *data);
354 private:
355    void updatePath(VPath& path, int frameNo) final;
356    LOTPolystarData             *mData;
357
358    bool hasChanged(int prevFrame, int curFrame) final {
359        return (mData->mPos.changed(prevFrame, curFrame) ||
360                mData->mPointCount.changed(prevFrame, curFrame) ||
361                mData->mInnerRadius.changed(prevFrame, curFrame) ||
362                mData->mOuterRadius.changed(prevFrame, curFrame) ||
363                mData->mInnerRoundness.changed(prevFrame, curFrame) ||
364                mData->mOuterRoundness.changed(prevFrame, curFrame) ||
365                mData->mRotation.changed(prevFrame, curFrame)) ? true : false;
366    }
367 };
368
369
370
371 class LOTPaintDataItem : public LOTContentItem
372 {
373 public:
374    LOTPaintDataItem(bool staticContent);
375    void addPathItems(std::vector<LOTPathDataItem *> &list, int startOffset);
376    void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag) override;
377    void renderList(std::vector<VDrawable *> &list) final;
378 protected:
379    virtual void updateContent(int frameNo) = 0;
380    virtual void updateRenderNode();
381    inline float parentAlpha() const {return mParentAlpha;}
382 public:
383    float                            mParentAlpha{1.0f};
384    VPath                            mPath;
385    DirtyFlag                        mFlag;
386    int                              mFrameNo{-1};
387    std::vector<LOTPathDataItem *>   mPathItems;
388    std::unique_ptr<VDrawable>       mDrawable;
389    bool                             mStaticContent;
390    bool                             mRenderNodeUpdate{true};
391 };
392
393 class LOTFillItem : public LOTPaintDataItem
394 {
395 public:
396    LOTFillItem(LOTFillData *data);
397 protected:
398    void updateContent(int frameNo) final;
399    void updateRenderNode() final;
400 private:
401    LOTFillData             *mData;
402    VColor                  mColor;
403    FillRule                mFillRule{FillRule::Winding};
404 };
405
406 class LOTGFillItem : public LOTPaintDataItem
407 {
408 public:
409    LOTGFillItem(LOTGFillData *data);
410 protected:
411    void updateContent(int frameNo) final;
412    void updateRenderNode() final;
413 private:
414    LOTGFillData                 *mData;
415    std::unique_ptr<VGradient>    mGradient;
416    FillRule                      mFillRule{FillRule::Winding};
417 };
418
419 class LOTStrokeItem : public LOTPaintDataItem
420 {
421 public:
422    LOTStrokeItem(LOTStrokeData *data);
423 protected:
424    void updateContent(int frameNo) final;
425    void updateRenderNode() final;
426 private:
427    LOTStrokeData             *mData;
428    CapStyle                  mCap{CapStyle::Flat};
429    JoinStyle                 mJoin{JoinStyle::Miter};
430    float                     mMiterLimit{0};
431    VColor                    mColor;
432    float                     mWidth{0};
433    float                     mDashArray[6];
434    int                       mDashArraySize{0};
435 };
436
437 class LOTGStrokeItem : public LOTPaintDataItem
438 {
439 public:
440    LOTGStrokeItem(LOTGStrokeData *data);
441 protected:
442    void updateContent(int frameNo) final;
443    void updateRenderNode() final;
444 private:
445    LOTGStrokeData               *mData;
446    std::unique_ptr<VGradient>    mGradient;
447    CapStyle                      mCap{CapStyle::Flat};
448    JoinStyle                     mJoin{JoinStyle::Miter};
449    float                         mMiterLimit{0};
450    VColor                        mColor;
451    float                         mWidth{0};
452    float                         mDashArray[6];
453    int                           mDashArraySize{0};
454 };
455
456
457 // Trim Item
458
459 class LOTTrimItem : public LOTContentItem
460 {
461 public:
462    LOTTrimItem(LOTTrimData *data);
463    void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag) final;
464    void update();
465    void addPathItems(std::vector<LOTPathDataItem *> &list, int startOffset);
466 private:
467    bool pathDirty() const {
468        for (auto &i : mPathItems) {
469            if (i->dirty())
470                return true;
471        }
472        return false;
473    }
474    struct Cache {
475         int                     mFrameNo{-1};
476         LOTTrimData::Segment    mSegment{};
477    };
478    Cache                            mCache;
479    std::vector<LOTPathDataItem *>   mPathItems;
480    LOTTrimData                     *mData;
481    bool                             mDirty{true};
482 };
483
484 class LOTRepeaterItem : public LOTContentGroupItem
485 {
486 public:
487    LOTRepeaterItem(LOTRepeaterData *data);
488    void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha, const DirtyFlag &flag) final;
489 private:
490    LOTRepeaterData             *mData;
491 };
492
493
494 #endif // LOTTIEITEM_H
495
496