• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
16 
17 #include <memory>
18 
19 class SkCanvas;
20 class SkData;
21 class SkImage;
22 struct SkRect;
23 class SkStream;
24 
25 namespace skjson { class ObjectValue; }
26 
27 namespace sksg {
28 
29 class InvalidationController;
30 class Scene;
31 
32 } // namespace sksg
33 
34 namespace skottie {
35 
36 class PropertyObserver;
37 
38 /**
39  * Image asset proxy interface.
40  */
41 class SK_API ImageAsset : public SkRefCnt {
42 public:
43     /**
44      * Returns true if the image asset is animated.
45      */
46     virtual bool isMultiFrame() = 0;
47 
48     /**
49      * Returns the SkImage for a given frame.
50      *
51      * If the image asset is static, getImage() is only called once, at animation load time.
52      * Otherwise, this gets invoked every time the animation time is adjusted (on every seek).
53      *
54      * Embedders should cache and serve the same SkImage whenever possible, for efficiency.
55      *
56      * @param t   Frame time code, in seconds, relative to the image layer timeline origin
57      *            (in-point).
58      */
59     virtual sk_sp<SkImage> getFrame(float t) = 0;
60 };
61 
62 /**
63  * ResourceProvider allows Skottie embedders to control loading of external
64  * Skottie resources -- e.g. images, fonts, nested animations.
65  */
66 class SK_API ResourceProvider : public SkRefCnt {
67 public:
68     /**
69      * Load a generic resource (currently only nested animations) specified by |path| + |name|,
70      * and return as an SkData.
71      */
72     virtual sk_sp<SkData> load(const char resource_path[],
73                                const char resource_name[]) const;
74 
75     /**
76      * Load an image asset specified by |path| + |name|, and returns the corresponding
77      * ImageAsset proxy.
78      */
79     virtual sk_sp<ImageAsset> loadImageAsset(const char resource_path[],
80                                              const char resource_name[],
81                                              const char resource_id[]) const;
82 
83     /**
84      * Load an external font and return as SkData.
85      *
86      * @param name  font name    ("fName" Lottie property)
87      * @param url   web font URL ("fPath" Lottie property)
88      *
89      * -- Note --
90      *
91      *   This mechanism assumes monolithic fonts (single data blob).  Some web font providers may
92      *   serve multiple font blobs, segmented for various unicode ranges, depending on user agent
93      *   capabilities (woff, woff2).  In that case, the embedder would need to advertise no user
94      *   agent capabilities when fetching the URL, in order to receive full font data.
95      */
96     virtual sk_sp<SkData> loadFont(const char name[], const char url[]) const;
97 };
98 
99 /**
100  * A Logger subclass can be used to receive Animation::Builder parsing errors and warnings.
101  */
102 class SK_API Logger : public SkRefCnt {
103 public:
104     enum class Level {
105         kWarning,
106         kError,
107     };
108 
109     virtual void log(Level, const char message[], const char* json = nullptr);
110 };
111 
112 /**
113  * Interface for receiving AE composition markers at Animation build time.
114  */
115 class SK_API MarkerObserver : public SkRefCnt {
116 public:
117     // t0,t1 are in the Animation::seek() domain.
118     virtual void onMarker(const char name[], float t0, float t1) = 0;
119 };
120 
121 class SK_API Animation : public SkNVRefCnt<Animation> {
122 public:
123 
124     class Builder final {
125     public:
126         Builder();
127         ~Builder();
128 
129         struct Stats {
130             float  fTotalLoadTimeMS  = 0, // Total animation instantiation time.
131                    fJsonParseTimeMS  = 0, // Time spent building a JSON DOM.
132                    fSceneParseTimeMS = 0; // Time spent constructing the animation scene graph.
133             size_t fJsonSize         = 0, // Input JSON size.
134                    fAnimatorCount    = 0; // Number of dynamically animated properties.
135         };
136 
137         /**
138          * Returns various animation build stats.
139          *
140          * @return Stats (see above).
141          */
getStats()142         const Stats& getStats() const { return fStats; }
143 
144         /**
145          * Specify a loader for external resources (images, etc.).
146          */
147         Builder& setResourceProvider(sk_sp<ResourceProvider>);
148 
149         /**
150          * Specify a font manager for loading animation fonts.
151          */
152         Builder& setFontManager(sk_sp<SkFontMgr>);
153 
154         /**
155          * Specify a PropertyObserver to receive callbacks during parsing.
156          *
157          * See SkottieProperty.h for more details.
158          *
159          */
160         Builder& setPropertyObserver(sk_sp<PropertyObserver>);
161 
162         /**
163          * Register a Logger with this builder.
164          */
165         Builder& setLogger(sk_sp<Logger>);
166 
167         /**
168          * Register a MarkerObserver with this builder.
169          */
170         Builder& setMarkerObserver(sk_sp<MarkerObserver>);
171 
172         /**
173          * Animation factories.
174          */
175         sk_sp<Animation> make(SkStream*);
176         sk_sp<Animation> make(const char* data, size_t length);
177         sk_sp<Animation> makeFromFile(const char path[]);
178 
179     private:
180         sk_sp<ResourceProvider> fResourceProvider;
181         sk_sp<SkFontMgr>        fFontMgr;
182         sk_sp<PropertyObserver> fPropertyObserver;
183         sk_sp<Logger>           fLogger;
184         sk_sp<MarkerObserver>   fMarkerObserver;
185         Stats                   fStats;
186     };
187 
188     /**
189      * Animation factories.
190      *
191      * Use the Builder helper above for more options/control.
192      */
193     static sk_sp<Animation> Make(const char* data, size_t length);
194     static sk_sp<Animation> Make(SkStream*);
195     static sk_sp<Animation> MakeFromFile(const char path[]);
196 
197     ~Animation();
198 
199     enum RenderFlag : uint32_t {
200         // When rendering into a known transparent buffer, clients can pass
201         // this flag to avoid some unnecessary compositing overhead for
202         // animations using layer blend modes.
203         kSkipTopLevelIsolation = 0x01,
204     };
205     using RenderFlags = uint32_t;
206 
207     /**
208      * Draws the current animation frame.
209      *
210      * @param canvas   destination canvas
211      * @param dst      optional destination rect
212      * @param flags    optional RenderFlags
213      */
214     void render(SkCanvas* canvas, const SkRect* dst = nullptr) const;
215     void render(SkCanvas* canvas, const SkRect* dst, RenderFlags) const;
216 
217     /**
218      * Updates the animation state for |t|.
219      *
220      * @param t   normalized [0..1] frame selector (0 -> first frame, 1 -> final frame)
221      * @param ic  optional invalidation controller (dirty region tracking)
222      *
223      */
224     void seek(SkScalar t, sksg::InvalidationController* ic = nullptr);
225 
226     /** Update the animation state to match t, specifed in frame time
227      *  i.e. relative to duration().
228      */
229     void seekFrameTime(double t, sksg::InvalidationController* = nullptr);
230 
231     /**
232      * Returns the animation duration in seconds.
233      */
duration()234     SkScalar duration() const { return fDuration; }
235 
version()236     const SkString& version() const { return fVersion;   }
size()237     const SkSize&      size() const { return fSize;      }
238 
239 private:
240     enum Flags : uint32_t {
241         kRequiresTopLevelIsolation = 1 << 0, // Needs to draw into a layer due to layer blending.
242     };
243 
244     Animation(std::unique_ptr<sksg::Scene>, SkString ver, const SkSize& size,
245               SkScalar inPoint, SkScalar outPoint, SkScalar duration, uint32_t flags = 0);
246 
247     std::unique_ptr<sksg::Scene> fScene;
248     const SkString               fVersion;
249     const SkSize                 fSize;
250     const SkScalar               fInPoint,
251                                  fOutPoint,
252                                  fDuration;
253     const uint32_t               fFlags;
254 
255     typedef SkNVRefCnt<Animation> INHERITED;
256 };
257 
258 } // namespace skottie
259 
260 #endif // Skottie_DEFINED
261