Skia
2D Graphics Library
Skottie.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 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 Skottie_DEFINED
9 #define Skottie_DEFINED
10 
11 #include "include/core/SkFontMgr.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkSize.h"
14 #include "include/core/SkString.h"
15 #include "include/core/SkTypes.h"
19 #include "modules/skresources/include/SkResources.h"
20 
21 #include <memory>
22 #include <vector>
23 
24 class SkCanvas;
25 struct SkRect;
26 class SkStream;
27 
28 namespace skjson { class ObjectValue; }
29 
30 namespace sksg {
31 
33 class RenderNode;
34 
35 } // namespace sksg
36 
37 namespace skottie {
38 
39 namespace internal { class Animator; }
40 
43 
47 class SK_API Logger : public SkRefCnt {
48 public:
49  enum class Level {
50  kWarning,
51  kError,
52  };
53 
54  virtual void log(Level, const char message[], const char* json = nullptr);
55 };
56 
57 // Evaluates AE expressions.
58 template <class T>
59 class SK_API ExpressionEvaluator : public SkRefCnt {
60 public:
61  // Evaluate the expression at the current time.
62  virtual T evaluate(float t) = 0;
63 };
64 
68 class SK_API ExpressionManager : public SkRefCnt {
69 public:
71  const char expression[]) = 0;
72 
74  const char expression[]) = 0;
75 
77  const char expression[]) = 0;
78 };
79 
83 class SK_API MarkerObserver : public SkRefCnt {
84 public:
85  // t0,t1 are in the Animation::seek() domain.
86  virtual void onMarker(const char name[], float t0, float t1) = 0;
87 };
88 
89 class SK_API Animation : public SkNVRefCnt<Animation> {
90 public:
91  class SK_API Builder final {
92  public:
93  enum Flags : uint32_t {
94  kDeferImageLoading = 0x01, // Normally, all static image frames are resolved at
95  // load time via ImageAsset::getFrame(0). With this flag,
96  // frames are only resolved when needed, at seek() time.
97  kPreferEmbeddedFonts = 0x02, // Attempt to use the embedded fonts (glyph paths,
98  // normally used as fallback) over native Skia typefaces.
99  };
100 
101  explicit Builder(uint32_t flags = 0);
103 
104  struct Stats {
105  float fTotalLoadTimeMS = 0, // Total animation instantiation time.
106  fJsonParseTimeMS = 0, // Time spent building a JSON DOM.
107  fSceneParseTimeMS = 0; // Time spent constructing the animation scene graph.
108  size_t fJsonSize = 0, // Input JSON size.
109  fAnimatorCount = 0; // Number of dynamically animated properties.
110  };
111 
117  const Stats& getStats() const { return fStats; }
118 
123 
128 
136 
141 
146 
152 
158 
163  sk_sp<Animation> make(const char* data, size_t length);
164  sk_sp<Animation> makeFromFile(const char path[]);
165 
169  const sk_sp<SlotManager>& getSlotManager() const {return fSlotManager;}
170 
171  private:
172  const uint32_t fFlags;
173 
174  sk_sp<ResourceProvider> fResourceProvider;
175  sk_sp<SkFontMgr> fFontMgr;
176  sk_sp<PropertyObserver> fPropertyObserver;
177  sk_sp<Logger> fLogger;
178  sk_sp<MarkerObserver > fMarkerObserver;
179  sk_sp<PrecompInterceptor> fPrecompInterceptor;
180  sk_sp<ExpressionManager> fExpressionManager;
181  sk_sp<SlotManager> fSlotManager;
182  Stats fStats;
183  };
184 
190  static sk_sp<Animation> Make(const char* data, size_t length);
192  static sk_sp<Animation> MakeFromFile(const char path[]);
193 
195 
196  enum RenderFlag : uint32_t {
197  // When rendering into a known transparent buffer, clients can pass
198  // this flag to avoid some unnecessary compositing overhead for
199  // animations using layer blend modes.
200  kSkipTopLevelIsolation = 0x01,
201  // By default, content is clipped to the intrinsic animation
202  // bounds (as determined by its size). If this flag is set,
203  // then the animation can draw outside of the bounds.
204  kDisableTopLevelClipping = 0x02,
205  };
206  using RenderFlags = uint32_t;
207 
218  void render(SkCanvas* canvas, const SkRect* dst = nullptr) const;
219  void render(SkCanvas* canvas, const SkRect* dst, RenderFlags) const;
220 
230  void seek(SkScalar t, sksg::InvalidationController* ic = nullptr) {
231  this->seekFrameTime(t * this->duration(), ic);
232  }
233 
244  void seekFrame(double t, sksg::InvalidationController* ic = nullptr);
245 
249  void seekFrameTime(double t, sksg::InvalidationController* = nullptr);
250 
254  double duration() const { return fDuration; }
255 
259  double fps() const { return fFPS; }
260 
264  double inPoint() const { return fInPoint; }
265 
269  double outPoint() const { return fOutPoint; }
270 
271  const SkString& version() const { return fVersion; }
272  const SkSize& size() const { return fSize; }
273 
274 private:
275  enum Flags : uint32_t {
276  kRequiresTopLevelIsolation = 1 << 0, // Needs to draw into a layer due to layer blending.
277  };
278 
279  Animation(sk_sp<sksg::RenderNode>,
280  std::vector<sk_sp<internal::Animator>>&&,
281  SkString ver, const SkSize& size,
282  double inPoint, double outPoint, double duration, double fps, uint32_t flags);
283 
284  const sk_sp<sksg::RenderNode> fSceneRoot;
285  const std::vector<sk_sp<internal::Animator>> fAnimators;
286  const SkString fVersion;
287  const SkSize fSize;
288  const double fInPoint,
289  fOutPoint,
290  fDuration,
291  fFPS;
292  const uint32_t fFlags;
293 
294  using INHERITED = SkNVRefCnt<Animation>;
295 };
296 
297 } // namespace skottie
298 
299 #endif // Skottie_DEFINED
float SkScalar
Definition: SkScalar.h:14
SkCanvas provides an interface for drawing, and how the drawing is clipped and transformed.
Definition: SkCanvas.h:99
Definition: SkRefCnt.h:160
Definition: SkRefCnt.h:119
SkStream – abstraction for a source of bytes.
Definition: SkStream.h:29
Light weight class for managing strings.
Definition: SkString.h:118
Shared pointer class to wrap classes that support a ref()/unref() interface.
Definition: SkRefCnt.h:220
Definition: Skottie.h:91
sk_sp< Animation > make(SkStream *)
Animation factories.
const Stats & getStats() const
Returns various animation build stats.
Definition: Skottie.h:117
Builder(uint32_t flags=0)
sk_sp< Animation > make(const char *data, size_t length)
Builder & setMarkerObserver(sk_sp< MarkerObserver >)
Register a MarkerObserver with this builder.
Flags
Definition: Skottie.h:93
Builder & setFontManager(sk_sp< SkFontMgr >)
Specify a font manager for loading animation fonts.
Builder & setPrecompInterceptor(sk_sp< PrecompInterceptor >)
Register a precomp layer interceptor.
Builder & setResourceProvider(sk_sp< ResourceProvider >)
Specify a loader for external resources (images, etc.).
Builder & setLogger(sk_sp< Logger >)
Register a Logger with this builder.
sk_sp< Animation > makeFromFile(const char path[])
Builder & setExpressionManager(sk_sp< ExpressionManager >)
Registers an ExpressionManager to evaluate AE expressions.
Builder & setPropertyObserver(sk_sp< PropertyObserver >)
Specify a PropertyObserver to receive callbacks during parsing.
const sk_sp< SlotManager > & getSlotManager() const
Get handle for SlotManager after animation is built.
Definition: Skottie.h:169
Definition: Skottie.h:89
const SkSize & size() const
Definition: Skottie.h:272
RenderFlag
Definition: Skottie.h:196
void seekFrame(double t, sksg::InvalidationController *ic=nullptr)
Update the animation state to match |t|, specified as a frame index i.e.
const SkString & version() const
Definition: Skottie.h:271
double inPoint() const
Animation in point, in frame index units.
Definition: Skottie.h:264
double fps() const
Returns the animation frame rate (frames / second).
Definition: Skottie.h:259
static sk_sp< Animation > MakeFromFile(const char path[])
static sk_sp< Animation > Make(const char *data, size_t length)
Animation factories.
void render(SkCanvas *canvas, const SkRect *dst, RenderFlags) const
void seekFrameTime(double t, sksg::InvalidationController *=nullptr)
Update the animation state to match t, specifed in frame time i.e.
double outPoint() const
Animation out point, in frame index units.
Definition: Skottie.h:269
double duration() const
Returns the animation duration in seconds.
Definition: Skottie.h:254
void render(SkCanvas *canvas, const SkRect *dst=nullptr) const
Draws the current animation frame.
void seek(SkScalar t, sksg::InvalidationController *ic=nullptr)
[Deprecated: use one of the other versions.
Definition: Skottie.h:230
uint32_t RenderFlags
Definition: Skottie.h:206
static sk_sp< Animation > Make(SkStream *)
Definition: Skottie.h:59
virtual T evaluate(float t)=0
Creates ExpressionEvaluators to evaluate AE expressions and return the results.
Definition: Skottie.h:68
virtual sk_sp< ExpressionEvaluator< float > > createNumberExpressionEvaluator(const char expression[])=0
virtual sk_sp< ExpressionEvaluator< std::vector< float > > > createArrayExpressionEvaluator(const char expression[])=0
virtual sk_sp< ExpressionEvaluator< SkString > > createStringExpressionEvaluator(const char expression[])=0
A Logger subclass can be used to receive Animation::Builder parsing errors and warnings.
Definition: Skottie.h:47
virtual void log(Level, const char message[], const char *json=nullptr)
Level
Definition: Skottie.h:49
Interface for receiving AE composition markers at Animation build time.
Definition: Skottie.h:83
virtual void onMarker(const char name[], float t0, float t1)=0
Receiver for invalidation events.
Definition: SkSGInvalidationController.h:25
Base class for nodes which can render to a canvas.
Definition: SkSGRenderNode.h:27
Definition: Skottie.h:28
Definition: ExternalLayer.h:16
skresources::ResourceProvider ResourceProvider
Definition: Skottie.h:42
skresources::ImageAsset ImageAsset
Definition: Skottie.h:41
Definition: Skottie.h:30
SkRect holds four float coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:582
Definition: SkSize.h:51
Definition: Skottie.h:104