blob: 7fcd9ef79a03153efaff10f898cf8a0b60735529 [file] [log] [blame]
Florin Malitaa6e30f72018-03-23 13:41:58 -04001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkottieAdapter_DEFINED
9#define SkottieAdapter_DEFINED
10
11#include "SkPoint.h"
12#include "SkRefCnt.h"
13#include "SkSize.h"
Mike Reed9adff1a2018-08-07 15:43:17 -040014#include "SkottieValue.h"
Florin Malitaa6e30f72018-03-23 13:41:58 -040015
16namespace sksg {
17
Florin Malitaa720d762019-03-07 15:11:27 -050018class BlurImageFilter;
Florin Malita9402c7d2018-08-26 22:06:55 -040019class Color;
20class Draw;
Florin Malitadafd6522019-02-10 01:49:46 +000021class DropShadowImageFilter;
Florin Malitaa6e30f72018-03-23 13:41:58 -040022class Gradient;
Florin Malita9402c7d2018-08-26 22:06:55 -040023class Group;
Florin Malitaa6e30f72018-03-23 13:41:58 -040024class LinearGradient;
Florin Malita760a0522019-01-10 15:24:15 -050025template <typename>
Florin Malitaa6e30f72018-03-23 13:41:58 -040026class Matrix;
27class Path;
28class RadialGradient;
Florin Malita8ec9a602019-02-01 17:59:25 -050029class RenderNode;
Florin Malitaa6e30f72018-03-23 13:41:58 -040030class RRect;
Florin Malita3d846c92019-04-02 09:43:18 -040031class ShaderEffect;
Florin Malita0a8b4e12018-11-01 11:16:18 -040032class TextBlob;
Florin Malitae3e8da52019-03-19 13:26:42 -040033class Transform;
Florin Malita8ec9a602019-02-01 17:59:25 -050034class TransformEffect;
Florin Malitaa6e30f72018-03-23 13:41:58 -040035class TrimEffect;
36
37};
38
Florin Malitae3e8da52019-03-19 13:26:42 -040039namespace skjson {
40 class ObjectValue;
41}
42
Florin Malitaa6e30f72018-03-23 13:41:58 -040043namespace skottie {
44
45#define ADAPTER_PROPERTY(p_name, p_type, p_default) \
Florin Malitaa85f3a12018-09-24 17:24:59 -040046 const p_type& get##p_name() const { \
47 return f##p_name; \
48 } \
Florin Malitaa6e30f72018-03-23 13:41:58 -040049 void set##p_name(const p_type& p) { \
50 if (p == f##p_name) return; \
51 f##p_name = p; \
52 this->apply(); \
53 } \
54 private: \
55 p_type f##p_name = p_default; \
56 public:
57
Florin Malita93563932019-01-02 09:26:13 -050058class RRectAdapter final : public SkNVRefCnt<RRectAdapter> {
Florin Malitaa6e30f72018-03-23 13:41:58 -040059public:
60 explicit RRectAdapter(sk_sp<sksg::RRect>);
Florin Malita93563932019-01-02 09:26:13 -050061 ~RRectAdapter();
Florin Malitaa6e30f72018-03-23 13:41:58 -040062
63 ADAPTER_PROPERTY(Position, SkPoint , SkPoint::Make(0, 0))
64 ADAPTER_PROPERTY(Size , SkSize , SkSize::Make(0, 0))
65 ADAPTER_PROPERTY(Radius , SkSize , SkSize::Make(0, 0))
66
67private:
68 void apply();
69
70 sk_sp<sksg::RRect> fRRectNode;
Florin Malitaa6e30f72018-03-23 13:41:58 -040071};
72
Florin Malita93563932019-01-02 09:26:13 -050073class PolyStarAdapter final : public SkNVRefCnt<PolyStarAdapter> {
Florin Malitaa6e30f72018-03-23 13:41:58 -040074public:
75 enum class Type {
76 kStar, kPoly,
77 };
78
79 PolyStarAdapter(sk_sp<sksg::Path>, Type);
Florin Malita93563932019-01-02 09:26:13 -050080 ~PolyStarAdapter();
Florin Malitaa6e30f72018-03-23 13:41:58 -040081
82 ADAPTER_PROPERTY(Position , SkPoint , SkPoint::Make(0, 0))
83 ADAPTER_PROPERTY(PointCount , SkScalar, 0)
84 ADAPTER_PROPERTY(InnerRadius , SkScalar, 0)
85 ADAPTER_PROPERTY(OuterRadius , SkScalar, 0)
86 ADAPTER_PROPERTY(InnerRoundness, SkScalar, 0)
87 ADAPTER_PROPERTY(OuterRoundness, SkScalar, 0)
88 ADAPTER_PROPERTY(Rotation , SkScalar, 0)
89
90private:
91 void apply();
92
93 sk_sp<sksg::Path> fPathNode;
94 Type fType;
Florin Malitaa6e30f72018-03-23 13:41:58 -040095};
96
Florin Malitad2a18532019-01-02 10:34:49 -050097class TransformAdapter2D final : public SkNVRefCnt<TransformAdapter2D> {
Florin Malitaa6e30f72018-03-23 13:41:58 -040098public:
Florin Malita760a0522019-01-10 15:24:15 -050099 explicit TransformAdapter2D(sk_sp<sksg::Matrix<SkMatrix>>);
Florin Malitad2a18532019-01-02 10:34:49 -0500100 ~TransformAdapter2D();
Florin Malitaa6e30f72018-03-23 13:41:58 -0400101
102 ADAPTER_PROPERTY(AnchorPoint, SkPoint , SkPoint::Make(0, 0))
103 ADAPTER_PROPERTY(Position , SkPoint , SkPoint::Make(0, 0))
104 ADAPTER_PROPERTY(Scale , SkVector, SkPoint::Make(100, 100))
105 ADAPTER_PROPERTY(Rotation , SkScalar, 0)
106 ADAPTER_PROPERTY(Skew , SkScalar, 0)
107 ADAPTER_PROPERTY(SkewAxis , SkScalar, 0)
108
Florin Malitaa85f3a12018-09-24 17:24:59 -0400109 SkMatrix totalMatrix() const;
110
Florin Malitaa6e30f72018-03-23 13:41:58 -0400111private:
112 void apply();
113
Florin Malita760a0522019-01-10 15:24:15 -0500114 sk_sp<sksg::Matrix<SkMatrix>> fMatrixNode;
Florin Malitaa6e30f72018-03-23 13:41:58 -0400115};
116
Florin Malitae3e8da52019-03-19 13:26:42 -0400117class TransformAdapter3D : public SkRefCnt {
Florin Malitad2a18532019-01-02 10:34:49 -0500118public:
Florin Malitae3e8da52019-03-19 13:26:42 -0400119 TransformAdapter3D();
120 ~TransformAdapter3D() override;
Florin Malitad2a18532019-01-02 10:34:49 -0500121
122 struct Vec3 {
123 float fX, fY, fZ;
124
125 explicit Vec3(const VectorValue&);
126
127 bool operator==(const Vec3& other) const {
128 return fX == other.fX && fY == other.fY && fZ == other.fZ;
129 }
130 bool operator!=(const Vec3& other) const { return !(*this == other); }
131 };
132
133 ADAPTER_PROPERTY(AnchorPoint, Vec3, Vec3({ 0, 0, 0}))
134 ADAPTER_PROPERTY(Position , Vec3, Vec3({ 0, 0, 0}))
135 ADAPTER_PROPERTY(Rotation , Vec3, Vec3({ 0, 0, 0}))
136 ADAPTER_PROPERTY(Scale , Vec3, Vec3({100, 100, 100}))
137
Florin Malitae3e8da52019-03-19 13:26:42 -0400138 sk_sp<sksg::Transform> refTransform() const;
Florin Malitad2a18532019-01-02 10:34:49 -0500139
Florin Malitae3e8da52019-03-19 13:26:42 -0400140protected:
Florin Malitad2a18532019-01-02 10:34:49 -0500141 void apply();
142
Florin Malitae3e8da52019-03-19 13:26:42 -0400143private:
144 virtual SkMatrix44 totalMatrix() const;
145
Florin Malita760a0522019-01-10 15:24:15 -0500146 sk_sp<sksg::Matrix<SkMatrix44>> fMatrixNode;
Florin Malitae3e8da52019-03-19 13:26:42 -0400147
148 using INHERITED = SkRefCnt;
149};
150
151class CameraAdapter final : public TransformAdapter3D {
152public:
153 explicit CameraAdapter(const SkSize& viewport_size);
154 ~CameraAdapter() override;
155
156 ADAPTER_PROPERTY(Zoom, SkScalar, 0)
157
158private:
159 SkMatrix44 totalMatrix() const override;
160
161 const SkSize fViewportSize;
162
163 using INHERITED = TransformAdapter3D;
Florin Malitad2a18532019-01-02 10:34:49 -0500164};
165
Florin Malita8ec9a602019-02-01 17:59:25 -0500166class RepeaterAdapter final : public SkNVRefCnt<RepeaterAdapter> {
167public:
168 enum class Composite { kAbove, kBelow };
169
170 RepeaterAdapter(sk_sp<sksg::RenderNode>, Composite);
171 ~RepeaterAdapter();
172
173 // Repeater props
174 ADAPTER_PROPERTY(Count , SkScalar, 0)
175 ADAPTER_PROPERTY(Offset , SkScalar, 0)
176
177 // Transform props
178 ADAPTER_PROPERTY(AnchorPoint , SkPoint , SkPoint::Make(0, 0))
179 ADAPTER_PROPERTY(Position , SkPoint , SkPoint::Make(0, 0))
180 ADAPTER_PROPERTY(Scale , SkVector, SkPoint::Make(100, 100))
181 ADAPTER_PROPERTY(Rotation , SkScalar, 0)
182 ADAPTER_PROPERTY(StartOpacity, SkScalar, 100)
183 ADAPTER_PROPERTY(EndOpacity , SkScalar, 100)
184
185 const sk_sp<sksg::Group>& root() const { return fRoot; }
186
187private:
188 void apply();
189
190 const sk_sp<sksg::RenderNode> fRepeaterNode;
191 const Composite fComposite;
192
193 sk_sp<sksg::Group> fRoot;
194};
195
Florin Malitaa6e30f72018-03-23 13:41:58 -0400196class GradientAdapter : public SkRefCnt {
197public:
Mike Reed9adff1a2018-08-07 15:43:17 -0400198 ADAPTER_PROPERTY(StartPoint, SkPoint , SkPoint::Make(0, 0) )
199 ADAPTER_PROPERTY(EndPoint , SkPoint , SkPoint::Make(0, 0) )
200 ADAPTER_PROPERTY(ColorStops, VectorValue , VectorValue() )
Florin Malitaa6e30f72018-03-23 13:41:58 -0400201
202protected:
203 GradientAdapter(sk_sp<sksg::Gradient>, size_t stopCount);
204
205 const SkPoint& startPoint() const { return fStartPoint; }
206 const SkPoint& endPoint() const { return fEndPoint; }
207
208 sk_sp<sksg::Gradient> fGradient;
209 size_t fStopCount;
210
211 virtual void onApply() = 0;
212
213private:
214 void apply();
Florin Malitaa6e30f72018-03-23 13:41:58 -0400215};
216
217class LinearGradientAdapter final : public GradientAdapter {
218public:
219 LinearGradientAdapter(sk_sp<sksg::LinearGradient>, size_t stopCount);
220
221private:
222 void onApply() override;
223
224 using INHERITED = GradientAdapter;
225};
226
227class RadialGradientAdapter final : public GradientAdapter {
228public:
229 RadialGradientAdapter(sk_sp<sksg::RadialGradient>, size_t stopCount);
230
231private:
232 void onApply() override;
233
234 using INHERITED = GradientAdapter;
235};
236
Florin Malita3d846c92019-04-02 09:43:18 -0400237class GradientRampEffectAdapter final : public SkNVRefCnt<GradientRampEffectAdapter> {
238public:
239 explicit GradientRampEffectAdapter(sk_sp<sksg::RenderNode> child);
240 ~GradientRampEffectAdapter();
241
242 ADAPTER_PROPERTY(StartPoint, SkPoint , SkPoint::Make(0, 0))
243 ADAPTER_PROPERTY(EndPoint , SkPoint , SkPoint::Make(0, 0))
244 ADAPTER_PROPERTY(StartColor, SkColor , SK_ColorBLACK)
245 ADAPTER_PROPERTY(EndColor , SkColor , SK_ColorBLACK)
246 ADAPTER_PROPERTY(Blend , SkScalar, 0)
247 ADAPTER_PROPERTY(Scatter , SkScalar, 0)
248
249 // Really an enum: 1 -> linear, 7 -> radial (?!)
250 ADAPTER_PROPERTY(Shape , SkScalar, 0)
251
252 const sk_sp<sksg::ShaderEffect>& root() const { return fRoot; }
253
254private:
255 enum class InstanceType {
256 kNone,
257 kLinear,
258 kRadial,
259 };
260
261 void apply();
262
263 sk_sp<sksg::ShaderEffect> fRoot;
264 sk_sp<sksg::Gradient> fGradient;
265 InstanceType fInstanceType = InstanceType::kNone;
266};
267
Florin Malita93563932019-01-02 09:26:13 -0500268class TrimEffectAdapter final : public SkNVRefCnt<TrimEffectAdapter> {
Florin Malitaa6e30f72018-03-23 13:41:58 -0400269public:
270 explicit TrimEffectAdapter(sk_sp<sksg::TrimEffect>);
Florin Malita93563932019-01-02 09:26:13 -0500271 ~TrimEffectAdapter();
Florin Malitaa6e30f72018-03-23 13:41:58 -0400272
273 ADAPTER_PROPERTY(Start , SkScalar, 0)
274 ADAPTER_PROPERTY(End , SkScalar, 100)
275 ADAPTER_PROPERTY(Offset, SkScalar, 0)
276
277private:
278 void apply();
279
280 sk_sp<sksg::TrimEffect> fTrimEffect;
Florin Malitaa6e30f72018-03-23 13:41:58 -0400281};
282
Florin Malitadafd6522019-02-10 01:49:46 +0000283class DropShadowEffectAdapter final : public SkNVRefCnt<DropShadowEffectAdapter> {
284public:
285 explicit DropShadowEffectAdapter(sk_sp<sksg::DropShadowImageFilter>);
286 ~DropShadowEffectAdapter();
287
288 ADAPTER_PROPERTY(Color , SkColor , SK_ColorBLACK)
289 ADAPTER_PROPERTY(Opacity , SkScalar, 255)
290 ADAPTER_PROPERTY(Direction , SkScalar, 0)
291 ADAPTER_PROPERTY(Distance , SkScalar, 0)
292 ADAPTER_PROPERTY(Softness , SkScalar, 0)
293 ADAPTER_PROPERTY(ShadowOnly, bool , false)
294
295private:
296 void apply();
297
298 const sk_sp<sksg::DropShadowImageFilter> fDropShadow;
299};
300
Florin Malitaa720d762019-03-07 15:11:27 -0500301class GaussianBlurEffectAdapter final : public SkNVRefCnt<GaussianBlurEffectAdapter> {
302public:
303 explicit GaussianBlurEffectAdapter(sk_sp<sksg::BlurImageFilter>);
304 ~GaussianBlurEffectAdapter();
305
306 // AE/BM model properties. These are all animatable/interpolatable.
307
308 // Controls the blur sigma.
309 ADAPTER_PROPERTY(Blurriness, SkScalar, 0)
310
311 // Enum selecting the blur dimensionality:
312 //
313 // 1 -> horizontal & vertical
314 // 2 -> horizontal
315 // 3 -> vertical
316 //
317 ADAPTER_PROPERTY(Dimensions, SkScalar, 1)
318
319 // Enum selecting edge behavior:
320 //
321 // 0 -> clamp
322 // 1 -> repeat
323 //
324 ADAPTER_PROPERTY(RepeatEdge, SkScalar, 0)
325
326private:
327 void apply();
328
329 const sk_sp<sksg::BlurImageFilter> fBlur;
330};
331
Florin Malita93563932019-01-02 09:26:13 -0500332class TextAdapter final : public SkNVRefCnt<TextAdapter> {
Florin Malita9402c7d2018-08-26 22:06:55 -0400333public:
334 explicit TextAdapter(sk_sp<sksg::Group> root);
Florin Malita93563932019-01-02 09:26:13 -0500335 ~TextAdapter();
Florin Malita9402c7d2018-08-26 22:06:55 -0400336
337 ADAPTER_PROPERTY(Text, TextValue, TextValue())
338
339 const sk_sp<sksg::Group>& root() const { return fRoot; }
340
341private:
342 void apply();
343
Florin Malita0a8b4e12018-11-01 11:16:18 -0400344 sk_sp<sksg::Group> fRoot;
345 sk_sp<sksg::TextBlob> fTextNode;
346 sk_sp<sksg::Color> fFillColor,
347 fStrokeColor;
348 sk_sp<sksg::Draw> fFillNode,
349 fStrokeNode;
Florin Malita9402c7d2018-08-26 22:06:55 -0400350
Florin Malita0a8b4e12018-11-01 11:16:18 -0400351 bool fHadFill : 1, // - state cached from the prev apply()
352 fHadStroke : 1; // /
Florin Malita9402c7d2018-08-26 22:06:55 -0400353};
354
Florin Malitaa6e30f72018-03-23 13:41:58 -0400355#undef ADAPTER_PROPERTY
356
357} // namespace skottie
358
359#endif // SkottieAdapter_DEFINED