Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 1 | /* |
| 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 Reed | 9adff1a | 2018-08-07 15:43:17 -0400 | [diff] [blame] | 14 | #include "SkottieValue.h" |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 15 | |
| 16 | namespace sksg { |
| 17 | |
Florin Malita | a720d76 | 2019-03-07 15:11:27 -0500 | [diff] [blame] | 18 | class BlurImageFilter; |
Florin Malita | 9402c7d | 2018-08-26 22:06:55 -0400 | [diff] [blame] | 19 | class Color; |
| 20 | class Draw; |
Florin Malita | dafd652 | 2019-02-10 01:49:46 +0000 | [diff] [blame] | 21 | class DropShadowImageFilter; |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 22 | class Gradient; |
Florin Malita | 9402c7d | 2018-08-26 22:06:55 -0400 | [diff] [blame] | 23 | class Group; |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 24 | class LinearGradient; |
Florin Malita | 760a052 | 2019-01-10 15:24:15 -0500 | [diff] [blame] | 25 | template <typename> |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 26 | class Matrix; |
| 27 | class Path; |
| 28 | class RadialGradient; |
Florin Malita | 8ec9a60 | 2019-02-01 17:59:25 -0500 | [diff] [blame] | 29 | class RenderNode; |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 30 | class RRect; |
Florin Malita | 3d846c9 | 2019-04-02 09:43:18 -0400 | [diff] [blame^] | 31 | class ShaderEffect; |
Florin Malita | 0a8b4e1 | 2018-11-01 11:16:18 -0400 | [diff] [blame] | 32 | class TextBlob; |
Florin Malita | e3e8da5 | 2019-03-19 13:26:42 -0400 | [diff] [blame] | 33 | class Transform; |
Florin Malita | 8ec9a60 | 2019-02-01 17:59:25 -0500 | [diff] [blame] | 34 | class TransformEffect; |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 35 | class TrimEffect; |
| 36 | |
| 37 | }; |
| 38 | |
Florin Malita | e3e8da5 | 2019-03-19 13:26:42 -0400 | [diff] [blame] | 39 | namespace skjson { |
| 40 | class ObjectValue; |
| 41 | } |
| 42 | |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 43 | namespace skottie { |
| 44 | |
| 45 | #define ADAPTER_PROPERTY(p_name, p_type, p_default) \ |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 46 | const p_type& get##p_name() const { \ |
| 47 | return f##p_name; \ |
| 48 | } \ |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 49 | 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 Malita | 9356393 | 2019-01-02 09:26:13 -0500 | [diff] [blame] | 58 | class RRectAdapter final : public SkNVRefCnt<RRectAdapter> { |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 59 | public: |
| 60 | explicit RRectAdapter(sk_sp<sksg::RRect>); |
Florin Malita | 9356393 | 2019-01-02 09:26:13 -0500 | [diff] [blame] | 61 | ~RRectAdapter(); |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 62 | |
| 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 | |
| 67 | private: |
| 68 | void apply(); |
| 69 | |
| 70 | sk_sp<sksg::RRect> fRRectNode; |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 71 | }; |
| 72 | |
Florin Malita | 9356393 | 2019-01-02 09:26:13 -0500 | [diff] [blame] | 73 | class PolyStarAdapter final : public SkNVRefCnt<PolyStarAdapter> { |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 74 | public: |
| 75 | enum class Type { |
| 76 | kStar, kPoly, |
| 77 | }; |
| 78 | |
| 79 | PolyStarAdapter(sk_sp<sksg::Path>, Type); |
Florin Malita | 9356393 | 2019-01-02 09:26:13 -0500 | [diff] [blame] | 80 | ~PolyStarAdapter(); |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 81 | |
| 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 | |
| 90 | private: |
| 91 | void apply(); |
| 92 | |
| 93 | sk_sp<sksg::Path> fPathNode; |
| 94 | Type fType; |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 95 | }; |
| 96 | |
Florin Malita | d2a1853 | 2019-01-02 10:34:49 -0500 | [diff] [blame] | 97 | class TransformAdapter2D final : public SkNVRefCnt<TransformAdapter2D> { |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 98 | public: |
Florin Malita | 760a052 | 2019-01-10 15:24:15 -0500 | [diff] [blame] | 99 | explicit TransformAdapter2D(sk_sp<sksg::Matrix<SkMatrix>>); |
Florin Malita | d2a1853 | 2019-01-02 10:34:49 -0500 | [diff] [blame] | 100 | ~TransformAdapter2D(); |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 101 | |
| 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 Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 109 | SkMatrix totalMatrix() const; |
| 110 | |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 111 | private: |
| 112 | void apply(); |
| 113 | |
Florin Malita | 760a052 | 2019-01-10 15:24:15 -0500 | [diff] [blame] | 114 | sk_sp<sksg::Matrix<SkMatrix>> fMatrixNode; |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 115 | }; |
| 116 | |
Florin Malita | e3e8da5 | 2019-03-19 13:26:42 -0400 | [diff] [blame] | 117 | class TransformAdapter3D : public SkRefCnt { |
Florin Malita | d2a1853 | 2019-01-02 10:34:49 -0500 | [diff] [blame] | 118 | public: |
Florin Malita | e3e8da5 | 2019-03-19 13:26:42 -0400 | [diff] [blame] | 119 | TransformAdapter3D(); |
| 120 | ~TransformAdapter3D() override; |
Florin Malita | d2a1853 | 2019-01-02 10:34:49 -0500 | [diff] [blame] | 121 | |
| 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 Malita | e3e8da5 | 2019-03-19 13:26:42 -0400 | [diff] [blame] | 138 | sk_sp<sksg::Transform> refTransform() const; |
Florin Malita | d2a1853 | 2019-01-02 10:34:49 -0500 | [diff] [blame] | 139 | |
Florin Malita | e3e8da5 | 2019-03-19 13:26:42 -0400 | [diff] [blame] | 140 | protected: |
Florin Malita | d2a1853 | 2019-01-02 10:34:49 -0500 | [diff] [blame] | 141 | void apply(); |
| 142 | |
Florin Malita | e3e8da5 | 2019-03-19 13:26:42 -0400 | [diff] [blame] | 143 | private: |
| 144 | virtual SkMatrix44 totalMatrix() const; |
| 145 | |
Florin Malita | 760a052 | 2019-01-10 15:24:15 -0500 | [diff] [blame] | 146 | sk_sp<sksg::Matrix<SkMatrix44>> fMatrixNode; |
Florin Malita | e3e8da5 | 2019-03-19 13:26:42 -0400 | [diff] [blame] | 147 | |
| 148 | using INHERITED = SkRefCnt; |
| 149 | }; |
| 150 | |
| 151 | class CameraAdapter final : public TransformAdapter3D { |
| 152 | public: |
| 153 | explicit CameraAdapter(const SkSize& viewport_size); |
| 154 | ~CameraAdapter() override; |
| 155 | |
| 156 | ADAPTER_PROPERTY(Zoom, SkScalar, 0) |
| 157 | |
| 158 | private: |
| 159 | SkMatrix44 totalMatrix() const override; |
| 160 | |
| 161 | const SkSize fViewportSize; |
| 162 | |
| 163 | using INHERITED = TransformAdapter3D; |
Florin Malita | d2a1853 | 2019-01-02 10:34:49 -0500 | [diff] [blame] | 164 | }; |
| 165 | |
Florin Malita | 8ec9a60 | 2019-02-01 17:59:25 -0500 | [diff] [blame] | 166 | class RepeaterAdapter final : public SkNVRefCnt<RepeaterAdapter> { |
| 167 | public: |
| 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 | |
| 187 | private: |
| 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 Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 196 | class GradientAdapter : public SkRefCnt { |
| 197 | public: |
Mike Reed | 9adff1a | 2018-08-07 15:43:17 -0400 | [diff] [blame] | 198 | ADAPTER_PROPERTY(StartPoint, SkPoint , SkPoint::Make(0, 0) ) |
| 199 | ADAPTER_PROPERTY(EndPoint , SkPoint , SkPoint::Make(0, 0) ) |
| 200 | ADAPTER_PROPERTY(ColorStops, VectorValue , VectorValue() ) |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 201 | |
| 202 | protected: |
| 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 | |
| 213 | private: |
| 214 | void apply(); |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | class LinearGradientAdapter final : public GradientAdapter { |
| 218 | public: |
| 219 | LinearGradientAdapter(sk_sp<sksg::LinearGradient>, size_t stopCount); |
| 220 | |
| 221 | private: |
| 222 | void onApply() override; |
| 223 | |
| 224 | using INHERITED = GradientAdapter; |
| 225 | }; |
| 226 | |
| 227 | class RadialGradientAdapter final : public GradientAdapter { |
| 228 | public: |
| 229 | RadialGradientAdapter(sk_sp<sksg::RadialGradient>, size_t stopCount); |
| 230 | |
| 231 | private: |
| 232 | void onApply() override; |
| 233 | |
| 234 | using INHERITED = GradientAdapter; |
| 235 | }; |
| 236 | |
Florin Malita | 3d846c9 | 2019-04-02 09:43:18 -0400 | [diff] [blame^] | 237 | class GradientRampEffectAdapter final : public SkNVRefCnt<GradientRampEffectAdapter> { |
| 238 | public: |
| 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 | |
| 254 | private: |
| 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 Malita | 9356393 | 2019-01-02 09:26:13 -0500 | [diff] [blame] | 268 | class TrimEffectAdapter final : public SkNVRefCnt<TrimEffectAdapter> { |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 269 | public: |
| 270 | explicit TrimEffectAdapter(sk_sp<sksg::TrimEffect>); |
Florin Malita | 9356393 | 2019-01-02 09:26:13 -0500 | [diff] [blame] | 271 | ~TrimEffectAdapter(); |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 272 | |
| 273 | ADAPTER_PROPERTY(Start , SkScalar, 0) |
| 274 | ADAPTER_PROPERTY(End , SkScalar, 100) |
| 275 | ADAPTER_PROPERTY(Offset, SkScalar, 0) |
| 276 | |
| 277 | private: |
| 278 | void apply(); |
| 279 | |
| 280 | sk_sp<sksg::TrimEffect> fTrimEffect; |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 281 | }; |
| 282 | |
Florin Malita | dafd652 | 2019-02-10 01:49:46 +0000 | [diff] [blame] | 283 | class DropShadowEffectAdapter final : public SkNVRefCnt<DropShadowEffectAdapter> { |
| 284 | public: |
| 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 | |
| 295 | private: |
| 296 | void apply(); |
| 297 | |
| 298 | const sk_sp<sksg::DropShadowImageFilter> fDropShadow; |
| 299 | }; |
| 300 | |
Florin Malita | a720d76 | 2019-03-07 15:11:27 -0500 | [diff] [blame] | 301 | class GaussianBlurEffectAdapter final : public SkNVRefCnt<GaussianBlurEffectAdapter> { |
| 302 | public: |
| 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 | |
| 326 | private: |
| 327 | void apply(); |
| 328 | |
| 329 | const sk_sp<sksg::BlurImageFilter> fBlur; |
| 330 | }; |
| 331 | |
Florin Malita | 9356393 | 2019-01-02 09:26:13 -0500 | [diff] [blame] | 332 | class TextAdapter final : public SkNVRefCnt<TextAdapter> { |
Florin Malita | 9402c7d | 2018-08-26 22:06:55 -0400 | [diff] [blame] | 333 | public: |
| 334 | explicit TextAdapter(sk_sp<sksg::Group> root); |
Florin Malita | 9356393 | 2019-01-02 09:26:13 -0500 | [diff] [blame] | 335 | ~TextAdapter(); |
Florin Malita | 9402c7d | 2018-08-26 22:06:55 -0400 | [diff] [blame] | 336 | |
| 337 | ADAPTER_PROPERTY(Text, TextValue, TextValue()) |
| 338 | |
| 339 | const sk_sp<sksg::Group>& root() const { return fRoot; } |
| 340 | |
| 341 | private: |
| 342 | void apply(); |
| 343 | |
Florin Malita | 0a8b4e1 | 2018-11-01 11:16:18 -0400 | [diff] [blame] | 344 | 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 Malita | 9402c7d | 2018-08-26 22:06:55 -0400 | [diff] [blame] | 350 | |
Florin Malita | 0a8b4e1 | 2018-11-01 11:16:18 -0400 | [diff] [blame] | 351 | bool fHadFill : 1, // - state cached from the prev apply() |
| 352 | fHadStroke : 1; // / |
Florin Malita | 9402c7d | 2018-08-26 22:06:55 -0400 | [diff] [blame] | 353 | }; |
| 354 | |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 355 | #undef ADAPTER_PROPERTY |
| 356 | |
| 357 | } // namespace skottie |
| 358 | |
| 359 | #endif // SkottieAdapter_DEFINED |