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 "include/core/SkPoint.h" 12 #include "include/core/SkRefCnt.h" 13 #include "include/core/SkSize.h" 14 #include "modules/skottie/src/SkottieValue.h" 15 #include "modules/sksg/include/SkSGScene.h" 16 17 namespace sksg { 18 19 class BlurImageFilter; 20 class Color; 21 class Draw; 22 class DropShadowImageFilter; 23 class ExternalColorFilter; 24 class Gradient; 25 class Group; 26 class LinearGradient; 27 template <typename> 28 class Matrix; 29 class Path; 30 class RadialGradient; 31 class RenderNode; 32 class RRect; 33 class ShaderEffect; 34 class Transform; 35 class TransformEffect; 36 class TrimEffect; 37 38 }; 39 40 namespace skjson { 41 class ObjectValue; 42 } 43 44 namespace skottie { 45 namespace internal { 46 47 class DiscardableAdaptorBase : public sksg::Animator { 48 protected: 49 DiscardableAdaptorBase(); 50 51 void onTick(float t) final; 52 53 virtual void onSync() = 0; 54 55 private: 56 friend class AnimationBuilder; 57 void setAnimators(sksg::AnimatorList&&); 58 59 sksg::AnimatorList fAnimators; 60 61 using INHERITED = sksg::Animator; 62 }; 63 64 65 } // namespace internal 66 67 #define ADAPTER_PROPERTY(p_name, p_type, p_default) \ 68 const p_type& get##p_name() const { \ 69 return f##p_name; \ 70 } \ 71 void set##p_name(const p_type& p) { \ 72 if (p == f##p_name) return; \ 73 f##p_name = p; \ 74 this->apply(); \ 75 } \ 76 private: \ 77 p_type f##p_name = p_default; \ 78 public: 79 80 class RRectAdapter final : public SkNVRefCnt<RRectAdapter> { 81 public: 82 explicit RRectAdapter(sk_sp<sksg::RRect>); 83 ~RRectAdapter(); 84 85 ADAPTER_PROPERTY(Position, SkPoint , SkPoint::Make(0, 0)) 86 ADAPTER_PROPERTY(Size , SkSize , SkSize::Make(0, 0)) 87 ADAPTER_PROPERTY(Radius , SkSize , SkSize::Make(0, 0)) 88 89 private: 90 void apply(); 91 92 sk_sp<sksg::RRect> fRRectNode; 93 }; 94 95 class PolyStarAdapter final : public SkNVRefCnt<PolyStarAdapter> { 96 public: 97 enum class Type { 98 kStar, kPoly, 99 }; 100 101 PolyStarAdapter(sk_sp<sksg::Path>, Type); 102 ~PolyStarAdapter(); 103 104 ADAPTER_PROPERTY(Position , SkPoint , SkPoint::Make(0, 0)) 105 ADAPTER_PROPERTY(PointCount , SkScalar, 0) 106 ADAPTER_PROPERTY(InnerRadius , SkScalar, 0) 107 ADAPTER_PROPERTY(OuterRadius , SkScalar, 0) 108 ADAPTER_PROPERTY(InnerRoundness, SkScalar, 0) 109 ADAPTER_PROPERTY(OuterRoundness, SkScalar, 0) 110 ADAPTER_PROPERTY(Rotation , SkScalar, 0) 111 112 private: 113 void apply(); 114 115 sk_sp<sksg::Path> fPathNode; 116 Type fType; 117 }; 118 119 class TransformAdapter2D final : public SkNVRefCnt<TransformAdapter2D> { 120 public: 121 explicit TransformAdapter2D(sk_sp<sksg::Matrix<SkMatrix>>); 122 ~TransformAdapter2D(); 123 124 ADAPTER_PROPERTY(AnchorPoint, SkPoint , SkPoint::Make(0, 0)) 125 ADAPTER_PROPERTY(Position , SkPoint , SkPoint::Make(0, 0)) 126 ADAPTER_PROPERTY(Scale , SkVector, SkPoint::Make(100, 100)) 127 ADAPTER_PROPERTY(Rotation , SkScalar, 0) 128 ADAPTER_PROPERTY(Skew , SkScalar, 0) 129 ADAPTER_PROPERTY(SkewAxis , SkScalar, 0) 130 131 SkMatrix totalMatrix() const; 132 133 private: 134 void apply(); 135 136 sk_sp<sksg::Matrix<SkMatrix>> fMatrixNode; 137 }; 138 139 class TransformAdapter3D : public SkRefCnt { 140 public: 141 TransformAdapter3D(); 142 ~TransformAdapter3D() override; 143 144 struct Vec3 { 145 float fX, fY, fZ; 146 147 explicit Vec3(const VectorValue&); 148 149 bool operator==(const Vec3& other) const { 150 return fX == other.fX && fY == other.fY && fZ == other.fZ; 151 } 152 bool operator!=(const Vec3& other) const { return !(*this == other); } 153 }; 154 155 ADAPTER_PROPERTY(AnchorPoint, Vec3, Vec3({ 0, 0, 0})) 156 ADAPTER_PROPERTY(Position , Vec3, Vec3({ 0, 0, 0})) 157 ADAPTER_PROPERTY(Rotation , Vec3, Vec3({ 0, 0, 0})) 158 ADAPTER_PROPERTY(Scale , Vec3, Vec3({100, 100, 100})) 159 160 sk_sp<sksg::Transform> refTransform() const; 161 162 protected: 163 void apply(); 164 165 private: 166 virtual SkMatrix44 totalMatrix() const; 167 168 sk_sp<sksg::Matrix<SkMatrix44>> fMatrixNode; 169 170 using INHERITED = SkRefCnt; 171 }; 172 173 class CameraAdapter final : public TransformAdapter3D { 174 public: 175 explicit CameraAdapter(const SkSize& viewport_size); 176 ~CameraAdapter() override; 177 178 ADAPTER_PROPERTY(Zoom, SkScalar, 0) 179 180 private: 181 SkMatrix44 totalMatrix() const override; 182 183 const SkSize fViewportSize; 184 185 using INHERITED = TransformAdapter3D; 186 }; 187 188 class RepeaterAdapter final : public SkNVRefCnt<RepeaterAdapter> { 189 public: 190 enum class Composite { kAbove, kBelow }; 191 192 RepeaterAdapter(sk_sp<sksg::RenderNode>, Composite); 193 ~RepeaterAdapter(); 194 195 // Repeater props 196 ADAPTER_PROPERTY(Count , SkScalar, 0) 197 ADAPTER_PROPERTY(Offset , SkScalar, 0) 198 199 // Transform props 200 ADAPTER_PROPERTY(AnchorPoint , SkPoint , SkPoint::Make(0, 0)) 201 ADAPTER_PROPERTY(Position , SkPoint , SkPoint::Make(0, 0)) 202 ADAPTER_PROPERTY(Scale , SkVector, SkPoint::Make(100, 100)) 203 ADAPTER_PROPERTY(Rotation , SkScalar, 0) 204 ADAPTER_PROPERTY(StartOpacity, SkScalar, 100) 205 ADAPTER_PROPERTY(EndOpacity , SkScalar, 100) 206 root()207 const sk_sp<sksg::Group>& root() const { return fRoot; } 208 209 private: 210 void apply(); 211 212 const sk_sp<sksg::RenderNode> fRepeaterNode; 213 const Composite fComposite; 214 215 sk_sp<sksg::Group> fRoot; 216 }; 217 218 class GradientAdapter : public SkRefCnt { 219 public: 220 ADAPTER_PROPERTY(StartPoint, SkPoint , SkPoint::Make(0, 0) ) 221 ADAPTER_PROPERTY(EndPoint , SkPoint , SkPoint::Make(0, 0) ) 222 ADAPTER_PROPERTY(ColorStops, VectorValue , VectorValue() ) 223 224 protected: 225 GradientAdapter(sk_sp<sksg::Gradient>, size_t stopCount); 226 startPoint()227 const SkPoint& startPoint() const { return fStartPoint; } endPoint()228 const SkPoint& endPoint() const { return fEndPoint; } 229 230 sk_sp<sksg::Gradient> fGradient; 231 size_t fStopCount; 232 233 virtual void onApply() = 0; 234 235 private: 236 void apply(); 237 }; 238 239 class LinearGradientAdapter final : public GradientAdapter { 240 public: 241 LinearGradientAdapter(sk_sp<sksg::LinearGradient>, size_t stopCount); 242 243 private: 244 void onApply() override; 245 246 using INHERITED = GradientAdapter; 247 }; 248 249 class RadialGradientAdapter final : public GradientAdapter { 250 public: 251 RadialGradientAdapter(sk_sp<sksg::RadialGradient>, size_t stopCount); 252 253 private: 254 void onApply() override; 255 256 using INHERITED = GradientAdapter; 257 }; 258 259 class TrimEffectAdapter final : public SkNVRefCnt<TrimEffectAdapter> { 260 public: 261 explicit TrimEffectAdapter(sk_sp<sksg::TrimEffect>); 262 ~TrimEffectAdapter(); 263 264 ADAPTER_PROPERTY(Start , SkScalar, 0) 265 ADAPTER_PROPERTY(End , SkScalar, 100) 266 ADAPTER_PROPERTY(Offset, SkScalar, 0) 267 268 private: 269 void apply(); 270 271 sk_sp<sksg::TrimEffect> fTrimEffect; 272 }; 273 274 } // namespace skottie 275 276 #endif // SkottieAdapter_DEFINED 277