1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
|
/*
* Copyright (c) 2020 Samsung Electronics Co., Ltd. All rights reserved.
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef LOTTIEITEM_H
#define LOTTIEITEM_H
#include <memory>
#include <sstream>
#include "lottie_lottiekeypath.h"
#include "lottie_lottiefiltermodel.h"
#include "rlottie.h"
#include "rlottiecommon.h"
#include "vector_varenaalloc.h"
#include "vector_vdrawable.h"
#include "vector_vmatrix.h"
#include "vector_vpainter.h"
#include "vector_vpath.h"
#include "vector_vpathmesure.h"
#include "vector_vpoint.h"
V_USE_NAMESPACE
namespace rlottie {
namespace internal {
template <class T>
class VSpan {
public:
using reference = T &;
using pointer = T *;
using const_pointer = T const *;
using const_reference = T const &;
using index_type = size_t;
using iterator = pointer;
using const_iterator = const_pointer;
VSpan() = default;
VSpan(pointer data, index_type size) : _data(data), _size(size) {}
constexpr pointer data() const noexcept { return _data; }
constexpr index_type size() const noexcept { return _size; }
constexpr bool empty() const noexcept { return size() == 0; }
constexpr iterator begin() const noexcept { return data(); }
constexpr iterator end() const noexcept { return data() + size(); }
constexpr const_iterator cbegin() const noexcept { return data(); }
constexpr const_iterator cend() const noexcept { return data() + size(); }
constexpr reference operator[](index_type idx) const
{
return *(data() + idx);
}
private:
pointer _data{nullptr};
index_type _size{0};
};
namespace renderer {
using DrawableList = VSpan<VDrawable *>;
enum class DirtyFlagBit : uchar {
None = 0x00,
Matrix = 0x01,
Alpha = 0x02,
All = (Matrix | Alpha)
};
typedef vFlag<DirtyFlagBit> DirtyFlag;
class SurfaceCache {
public:
SurfaceCache() { mCache.reserve(10); }
VBitmap make_surface(
size_t width, size_t height,
VBitmap::Format format = VBitmap::Format::ARGB32_Premultiplied)
{
if (mCache.empty()) return {width, height, format};
auto surface = mCache.back();
surface.reset(width, height, format);
mCache.pop_back();
return surface;
}
void release_surface(VBitmap &surface) { mCache.push_back(surface); }
private:
std::vector<VBitmap> mCache;
};
class Drawable final : public VDrawable {
public:
void sync();
public:
std::unique_ptr<LOTNode> mCNode{nullptr};
~Drawable() noexcept
{
if (mCNode && mCNode->mGradient.stopPtr)
free(mCNode->mGradient.stopPtr);
}
};
struct CApiData {
CApiData();
LOTLayerNode mLayer;
std::vector<LOTMask> mMasks;
std::vector<LOTLayerNode *> mLayers;
std::vector<LOTNode *> mCNodeList;
};
class Clipper {
public:
explicit Clipper(VSize size) : mSize(size) {}
void update(const VMatrix &matrix);
void preprocess(const VRect &clip);
VRle rle(const VRle &mask);
public:
VSize mSize;
VPath mPath;
VRle mMaskedRle;
VRasterizer mRasterizer;
bool mRasterRequest{false};
};
class Mask {
public:
explicit Mask(model::Mask *data) : mData(data) {}
void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha,
const DirtyFlag &flag);
model::Mask::Mode maskMode() const { return mData->mMode; }
VRle rle();
void preprocess(const VRect &clip);
bool inverted() const { return mData->mInv; }
public:
model::Mask *mData{nullptr};
VPath mLocalPath;
VPath mFinalPath;
VRasterizer mRasterizer;
float mCombinedAlpha{0};
bool mRasterRequest{false};
};
/*
* Handels mask property of a layer item
*/
class LayerMask {
public:
explicit LayerMask(model::Layer *layerData);
void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha,
const DirtyFlag &flag);
bool isStatic() const { return mStatic; }
VRle maskRle(const VRect &clipRect);
void preprocess(const VRect &clip);
public:
std::vector<Mask> mMasks;
VRle mRle;
bool mStatic{true};
bool mDirty{true};
};
class Layer;
class Composition {
public:
explicit Composition(std::shared_ptr<model::Composition> composition);
bool update(int frameNo, const VSize &size, bool keepAspectRatio);
VSize size() const { return mViewSize; }
void buildRenderTree();
const LOTLayerNode *renderTree() const;
bool render(const rlottie::Surface &surface);
void setValue(const std::string &keypath, LOTVariant &value);
private:
SurfaceCache mSurfaceCache;
VBitmap mSurface;
VMatrix mScaleMatrix;
VSize mViewSize;
std::shared_ptr<model::Composition> mModel;
Layer * mRootLayer{nullptr};
VArenaAlloc mAllocator{2048};
int mCurFrameNo;
bool mKeepAspectRatio{true};
};
class Layer {
public:
virtual ~Layer() = default;
Layer &operator=(Layer &&) noexcept = delete;
Layer(model::Layer *layerData);
int id() const { return mLayerData->id(); }
int parentId() const { return mLayerData->parentId(); }
void setParentLayer(Layer *parent) { mParentLayer = parent; }
void setComplexContent(bool value) { mComplexContent = value; }
bool complexContent() const { return mComplexContent; }
virtual void update(int frameNo, const VMatrix &parentMatrix,
float parentAlpha);
VMatrix matrix(int frameNo) const;
void preprocess(const VRect &clip);
virtual DrawableList renderList() { return {}; }
virtual void render(VPainter *painter, const VRle &mask,
const VRle &matteRle, SurfaceCache &cache);
bool hasMatte()
{
if (mLayerData->mMatteType == model::MatteType::None) return false;
return true;
}
model::MatteType matteType() const { return mLayerData->mMatteType; }
bool visible() const;
virtual void buildLayerNode();
LOTLayerNode & clayer() { return mCApiData->mLayer; }
std::vector<LOTLayerNode *> &clayers() { return mCApiData->mLayers; }
std::vector<LOTMask> & cmasks() { return mCApiData->mMasks; }
std::vector<LOTNode *> & cnodes() { return mCApiData->mCNodeList; }
const char * name() const { return mLayerData->name(); }
virtual bool resolveKeyPath(LOTKeyPath &keyPath, uint depth,
LOTVariant &value);
protected:
virtual void preprocessStage(const VRect &clip) = 0;
virtual void updateContent() = 0;
inline VMatrix combinedMatrix() const { return mCombinedMatrix; }
inline int frameNo() const { return mFrameNo; }
inline float combinedAlpha() const { return mCombinedAlpha; }
inline bool isStatic() const { return mLayerData->isStatic(); }
float opacity(int frameNo) const { return mLayerData->opacity(frameNo); }
inline DirtyFlag flag() const { return mDirtyFlag; }
bool skipRendering() const
{
return (!visible() || vIsZero(combinedAlpha()));
}
protected:
std::unique_ptr<LayerMask> mLayerMask;
model::Layer * mLayerData{nullptr};
Layer * mParentLayer{nullptr};
VMatrix mCombinedMatrix;
float mCombinedAlpha{0.0};
int mFrameNo{-1};
DirtyFlag mDirtyFlag{DirtyFlagBit::All};
bool mComplexContent{false};
std::unique_ptr<CApiData> mCApiData;
};
class CompLayer final : public Layer {
public:
explicit CompLayer(model::Layer *layerData, VArenaAlloc *allocator);
void render(VPainter *painter, const VRle &mask, const VRle &matteRle,
SurfaceCache &cache) final;
void buildLayerNode() final;
bool resolveKeyPath(LOTKeyPath &keyPath, uint depth,
LOTVariant &value) override;
protected:
void preprocessStage(const VRect &clip) final;
void updateContent() final;
private:
void renderHelper(VPainter *painter, const VRle &mask, const VRle &matteRle,
SurfaceCache &cache);
void renderMatteLayer(VPainter *painter, const VRle &inheritMask,
const VRle &matteRle, Layer *layer, Layer *src,
SurfaceCache &cache);
private:
std::vector<Layer *> mLayers;
std::unique_ptr<Clipper> mClipper;
};
class SolidLayer final : public Layer {
public:
explicit SolidLayer(model::Layer *layerData);
void buildLayerNode() final;
DrawableList renderList() final;
protected:
void preprocessStage(const VRect &clip) final;
void updateContent() final;
private:
Drawable mRenderNode;
VPath mPath;
VDrawable *mDrawableList{nullptr}; // to work with the Span api
};
class Group;
class ShapeLayer final : public Layer {
public:
explicit ShapeLayer(model::Layer *layerData, VArenaAlloc *allocator);
DrawableList renderList() final;
void buildLayerNode() final;
bool resolveKeyPath(LOTKeyPath &keyPath, uint depth,
LOTVariant &value) override;
protected:
void preprocessStage(const VRect &clip) final;
void updateContent() final;
std::vector<VDrawable *> mDrawableList;
Group * mRoot{nullptr};
};
class NullLayer final : public Layer {
public:
explicit NullLayer(model::Layer *layerData);
protected:
void preprocessStage(const VRect &) final {}
void updateContent() final;
};
class ImageLayer final : public Layer {
public:
explicit ImageLayer(model::Layer *layerData);
void buildLayerNode() final;
DrawableList renderList() final;
protected:
void preprocessStage(const VRect &clip) final;
void updateContent() final;
private:
Drawable mRenderNode;
VTexture mTexture;
VPath mPath;
VDrawable *mDrawableList{nullptr}; // to work with the Span api
};
class Object {
public:
enum class Type : uchar { Unknown, Group, Shape, Paint, Trim };
virtual ~Object() = default;
Object & operator=(Object &&) noexcept = delete;
virtual void update(int frameNo, const VMatrix &parentMatrix,
float parentAlpha, const DirtyFlag &flag) = 0;
virtual void renderList(std::vector<VDrawable *> &) {}
virtual bool resolveKeyPath(LOTKeyPath &, uint, LOTVariant &)
{
return false;
}
virtual Object::Type type() const { return Object::Type::Unknown; }
};
class Shape;
class Group : public Object {
public:
Group() = default;
explicit Group(model::Group *data, VArenaAlloc *allocator);
void addChildren(model::Group *data, VArenaAlloc *allocator);
void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha,
const DirtyFlag &flag) override;
void applyTrim();
void processTrimItems(std::vector<Shape *> &list);
void processPaintItems(std::vector<Shape *> &list);
void renderList(std::vector<VDrawable *> &list) override;
Object::Type type() const final { return Object::Type::Group; }
const VMatrix &matrix() const { return mMatrix; }
const char * name() const
{
static const char *TAG = "__";
return mModel.hasModel() ? mModel.name() : TAG;
}
bool resolveKeyPath(LOTKeyPath &keyPath, uint depth,
LOTVariant &value) override;
protected:
std::vector<Object *> mContents;
VMatrix mMatrix;
private:
model::Filter<model::Group> mModel;
};
class Shape : public Object {
public:
Shape(bool staticPath) : mStaticPath(staticPath) {}
void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha,
const DirtyFlag &flag) final;
Object::Type type() const final { return Object::Type::Shape; }
bool dirty() const { return mDirtyPath; }
const VPath &localPath() const { return mTemp; }
void finalPath(VPath &result);
void updatePath(const VPath &path)
{
mTemp = path;
mDirtyPath = true;
}
bool staticPath() const { return mStaticPath; }
void setParent(Group *parent) { mParent = parent; }
Group *parent() const { return mParent; }
protected:
virtual void updatePath(VPath &path, int frameNo) = 0;
virtual bool hasChanged(int prevFrame, int curFrame) = 0;
private:
bool hasChanged(int frameNo)
{
int prevFrame = mFrameNo;
mFrameNo = frameNo;
if (prevFrame == -1) return true;
if (mStaticPath || (prevFrame == frameNo)) return false;
return hasChanged(prevFrame, frameNo);
}
Group *mParent{nullptr};
VPath mLocalPath;
VPath mTemp;
int mFrameNo{-1};
bool mDirtyPath{true};
bool mStaticPath;
};
class Rect final : public Shape {
public:
explicit Rect(model::Rect *data);
protected:
void updatePath(VPath &path, int frameNo) final;
model::Rect *mData{nullptr};
bool hasChanged(int prevFrame, int curFrame) final
{
return (mData->mPos.changed(prevFrame, curFrame) ||
mData->mSize.changed(prevFrame, curFrame) ||
mData->roundnessChanged(prevFrame, curFrame));
}
};
class Ellipse final : public Shape {
public:
explicit Ellipse(model::Ellipse *data);
private:
void updatePath(VPath &path, int frameNo) final;
model::Ellipse *mData{nullptr};
bool hasChanged(int prevFrame, int curFrame) final
{
return (mData->mPos.changed(prevFrame, curFrame) ||
mData->mSize.changed(prevFrame, curFrame));
}
};
class Path final : public Shape {
public:
explicit Path(model::Path *data);
private:
void updatePath(VPath &path, int frameNo) final;
model::Path *mData{nullptr};
bool hasChanged(int prevFrame, int curFrame) final
{
return mData->mShape.changed(prevFrame, curFrame);
}
};
class Polystar final : public Shape {
public:
explicit Polystar(model::Polystar *data);
private:
void updatePath(VPath &path, int frameNo) final;
model::Polystar *mData{nullptr};
bool hasChanged(int prevFrame, int curFrame) final
{
return (mData->mPos.changed(prevFrame, curFrame) ||
mData->mPointCount.changed(prevFrame, curFrame) ||
mData->mInnerRadius.changed(prevFrame, curFrame) ||
mData->mOuterRadius.changed(prevFrame, curFrame) ||
mData->mInnerRoundness.changed(prevFrame, curFrame) ||
mData->mOuterRoundness.changed(prevFrame, curFrame) ||
mData->mRotation.changed(prevFrame, curFrame));
}
};
class Paint : public Object {
public:
Paint(bool staticContent);
void addPathItems(std::vector<Shape *> &list, size_t startOffset);
void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha,
const DirtyFlag &flag) override;
void renderList(std::vector<VDrawable *> &list) final;
Object::Type type() const final { return Object::Type::Paint; }
protected:
virtual bool updateContent(int frameNo, const VMatrix &matrix,
float alpha) = 0;
private:
void updateRenderNode();
protected:
std::vector<Shape *> mPathItems;
Drawable mDrawable;
VPath mPath;
DirtyFlag mFlag;
bool mStaticContent;
bool mRenderNodeUpdate{true};
bool mContentToRender{true};
};
class Fill final : public Paint {
public:
explicit Fill(model::Fill *data);
protected:
bool updateContent(int frameNo, const VMatrix &matrix, float alpha) final;
bool resolveKeyPath(LOTKeyPath &keyPath, uint depth,
LOTVariant &value) final;
private:
model::Filter<model::Fill> mModel;
};
class GradientFill final : public Paint {
public:
explicit GradientFill(model::GradientFill *data);
protected:
bool updateContent(int frameNo, const VMatrix &matrix, float alpha) final;
private:
model::GradientFill * mData{nullptr};
std::unique_ptr<VGradient> mGradient;
};
class Stroke : public Paint {
public:
explicit Stroke(model::Stroke *data);
protected:
bool updateContent(int frameNo, const VMatrix &matrix, float alpha) final;
bool resolveKeyPath(LOTKeyPath &keyPath, uint depth,
LOTVariant &value) final;
private:
model::Filter<model::Stroke> mModel;
};
class GradientStroke final : public Paint {
public:
explicit GradientStroke(model::GradientStroke *data);
protected:
bool updateContent(int frameNo, const VMatrix &matrix, float alpha) final;
private:
model::GradientStroke * mData{nullptr};
std::unique_ptr<VGradient> mGradient;
};
class Trim final : public Object {
public:
explicit Trim(model::Trim *data) : mData(data) {}
void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha,
const DirtyFlag &flag) final;
Object::Type type() const final { return Object::Type::Trim; }
void update();
void addPathItems(std::vector<Shape *> &list, size_t startOffset);
private:
bool pathDirty() const
{
for (auto &i : mPathItems) {
if (i->dirty()) return true;
}
return false;
}
struct Cache {
int mFrameNo{-1};
model::Trim::Segment mSegment{};
};
Cache mCache;
std::vector<Shape *> mPathItems;
model::Trim * mData{nullptr};
VPathMesure mPathMesure;
bool mDirty{true};
};
class Repeater final : public Group {
public:
explicit Repeater(model::Repeater *data, VArenaAlloc *allocator);
void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha,
const DirtyFlag &flag) final;
void renderList(std::vector<VDrawable *> &list) final;
private:
model::Repeater *mRepeaterData{nullptr};
bool mHidden{false};
int mCopies{0};
};
} // namespace renderer
} // namespace internal
} // namespace rlottie
#endif // LOTTIEITEM_H
|