Debian Codegraph @

modules/skottie/include/Skottie.h

raw
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/SkRefCnt.h"
12 #include "include/core/SkScalar.h"
13 #include "include/core/SkSize.h"
14 #include "include/core/SkSpan.h"
15 #include "include/core/SkString.h"
16 #include "include/core/SkTypes.h"
17 #include "modules/skresources/include/SkResources.h"
18
19 // TODO(kjlubick) update clients and then remove the following:
20 #include "include/core/SkFontMgr.h" // IWYU pragma: keep
21 #include "modules/skottie/include/ExternalLayer.h" // IWYU pragma: keep
22 #include "modules/skottie/include/SkottieProperty.h" // IWYU pragma: keep
23 #include "modules/skottie/include/SlotManager.h" // IWYU pragma: keep
24
25 #include <cstddef>
26 #include <cstdint>
27 #include <vector>
28
29 class SkCanvas;
30 class SkStream;
31 struct SkRect;
32
33 namespace sksg {
34
35 class InvalidationController;
36 class RenderNode;
37
38 } // namespace sksg
39
40 namespace SkShapers { class Factory; }
41
42 namespace skottie {
43
44 namespace internal { class Animator; }
45
46 using ImageAsset = skresources::ImageAsset;
47 using ResourceProvider = skresources::ResourceProvider;
48
49 struct LayerInfo {
50 SkString fName;
51 SkSize fSize;
52 const float fInPoint,
53 fOutPoint;
54 };
55
56 /**
57 * A Logger subclass can be used to receive Animation::Builder parsing errors and warnings.
58 */
59 class SK_API Logger : public SkRefCnt {
60 public:
61 enum class Level {
62 kWarning,
63 kError,
64 };
65
66 virtual void log(Level, const char message[], const char* json = nullptr) = 0;
67 };
68
69 // Evaluates AE expressions.
70 template <class T>
71 class SK_API ExpressionEvaluator : public SkRefCnt {
72 public:
73 // Evaluate the expression at the current time.
74 virtual T evaluate(float t) = 0;
75 };
76
77 /**
78 * Creates ExpressionEvaluators to evaluate AE expressions and return the results.
79 */
80 class SK_API ExpressionManager : public SkRefCnt {
81 public:
82 virtual sk_sp<ExpressionEvaluator<float>> createNumberExpressionEvaluator(
83 const char expression[]) = 0;
84
85 virtual sk_sp<ExpressionEvaluator<SkString>> createStringExpressionEvaluator(
86 const char expression[]) = 0;
87
88 virtual sk_sp<ExpressionEvaluator<std::vector<float>>> createArrayExpressionEvaluator(
89 const char expression[]) = 0;
90 };
91
92 /**
93 * Interface for receiving AE composition markers at Animation build time.
94 */
95 class SK_API MarkerObserver : public SkRefCnt {
96 public:
97 // t0,t1 are in the Animation::seek() domain.
98 virtual void onMarker(const char name[], float t0, float t1) = 0;
99 };
100
101 class SK_API Animation : public SkNVRefCnt<Animation> {
102 public:
103 class SK_API Builder final {
104 public:
105 enum Flags : uint32_t {
106 kDeferImageLoading = 0x01, // Normally, all static image frames are resolved at
107 // load time via ImageAsset::getFrame(0). With this flag,
108 // frames are only resolved when needed, at seek() time.
109 kPreferEmbeddedFonts = 0x02, // Attempt to use the embedded fonts (glyph paths,
110 // normally used as fallback) over native Skia typefaces.
111 };
112
113 explicit Builder(uint32_t flags = 0);
114 Builder(const Builder&);
115 Builder(Builder&&);
116 ~Builder();
117
118 struct Stats {
119 float fTotalLoadTimeMS = 0, // Total animation instantiation time.
120 fJsonParseTimeMS = 0, // Time spent building a JSON DOM.
121 fSceneParseTimeMS = 0; // Time spent constructing the animation scene graph.
122 size_t fJsonSize = 0, // Input JSON size.
123 fAnimatorCount = 0; // Number of dynamically animated properties.
124 };
125
126 /**
127 * Returns various animation build stats.
128 *
129 * @return Stats (see above).
130 */
131 const Stats& getStats() const { return fStats; }
132
133 /**
134 * Specify a loader for external resources (images, etc.).
135 */
136 Builder& setResourceProvider(sk_sp<ResourceProvider>);
137
138 /**
139 * Specify a font manager for loading animation fonts.
140 */
141 Builder& setFontManager(sk_sp<SkFontMgr>);
142
143 /**
144 * Specify a PropertyObserver to receive callbacks during parsing.
145 *
146 * See SkottieProperty.h for more details.
147 *
148 */
149 Builder& setPropertyObserver(sk_sp<PropertyObserver>);
150
151 /**
152 * Register a Logger with this builder.
153 */
154 Builder& setLogger(sk_sp<Logger>);
155
156 /**
157 * Register a MarkerObserver with this builder.
158 */
159 Builder& setMarkerObserver(sk_sp<MarkerObserver>);
160
161 /**
162 * Register a precomp layer interceptor.
163 * This allows substituting precomp layers with custom/externally managed content.
164 */
165 Builder& setPrecompInterceptor(sk_sp<PrecompInterceptor>);
166
167 /**
168 * Registers an ExpressionManager to evaluate AE expressions.
169 * If unspecified, expressions in the animation JSON will be ignored.
170 */
171 Builder& setExpressionManager(sk_sp<ExpressionManager>);
172
173 /**
174 * Registers a factory to be used when shaping text.
175 * If unspecified, text will be shaped with primitive shaping.
176 * See //modules/skshaper/utils/FactoryHelpers.h
177 */
178 Builder& setTextShapingFactory(sk_sp<SkShapers::Factory>);
179
180 /**
181 * Animation factories.
182 */
183 sk_sp<Animation> make(SkStream*);
184 sk_sp<Animation> make(const char* data, size_t length);
185 sk_sp<Animation> makeFromFile(const char path[]);
186
187 /**
188 * Get handle for SlotManager after animation is built.
189 */
190 const sk_sp<SlotManager>& getSlotManager() const {return fSlotManager;}
191
192 SkSpan<const LayerInfo> getLayerInfo() const {return SkSpan(fLayerInfo);}
193
194 private:
195 const uint32_t fFlags;
196
197 sk_sp<ResourceProvider> fResourceProvider;
198 sk_sp<SkFontMgr> fFontMgr;
199 sk_sp<PropertyObserver> fPropertyObserver;
200 sk_sp<Logger> fLogger;
201 sk_sp<MarkerObserver > fMarkerObserver;
202 sk_sp<PrecompInterceptor> fPrecompInterceptor;
203 sk_sp<ExpressionManager> fExpressionManager;
204 sk_sp<SkShapers::Factory> fShapingFactory;
205 sk_sp<SlotManager> fSlotManager;
206 Stats fStats;
207 std::vector<LayerInfo> fLayerInfo;
208 };
209
210 /**
211 * Animation factories.
212 *
213 * Use the Builder helper above for more options/control.
214 */
215 static sk_sp<Animation> Make(const char* data, size_t length);
216 static sk_sp<Animation> Make(SkStream*);
217 static sk_sp<Animation> MakeFromFile(const char path[]);
218
219 ~Animation();
220
221 enum RenderFlag : uint32_t {
222 // When rendering into a known transparent buffer, clients can pass
223 // this flag to avoid some unnecessary compositing overhead for
224 // animations using layer blend modes.
225 kSkipTopLevelIsolation = 0x01,
226 // By default, content is clipped to the intrinsic animation
227 // bounds (as determined by its size). If this flag is set,
228 // then the animation can draw outside of the bounds.
229 kDisableTopLevelClipping = 0x02,
230 };
231 using RenderFlags = uint32_t;
232
233 /**
234 * Draws the current animation frame.
235 *
236 * It is undefined behavior to call render() on a newly created Animation
237 * before specifying an initial frame via one of the seek() variants.
238 *
239 * @param canvas destination canvas
240 * @param dst optional destination rect
241 * @param flags optional RenderFlags
242 */
243 void render(SkCanvas* canvas, const SkRect* dst = nullptr) const;
244 void render(SkCanvas* canvas, const SkRect* dst, RenderFlags) const;
245
246 /**
247 * [Deprecated: use one of the other versions.]
248 *
249 * Updates the animation state for |t|.
250 *
251 * @param t normalized [0..1] frame selector (0 -> first frame, 1 -> final frame)
252 * @param ic optional invalidation controller (dirty region tracking)
253 *
254 */
255 void seek(SkScalar t, sksg::InvalidationController* ic = nullptr) {
256 this->seekFrameTime(t * this->duration(), ic);
257 }
258
259 /**
260 * Update the animation state to match |t|, specified as a frame index
261 * i.e. relative to duration() * fps().
262 *
263 * Fractional values are allowed and meaningful - e.g.
264 *
265 * 0.0 -> first frame
266 * 1.0 -> second frame
267 * 0.5 -> halfway between first and second frame
268 */
269 void seekFrame(double t, sksg::InvalidationController* ic = nullptr);
270
271 /** Update the animation state to match t, specifed in frame time
272 * i.e. relative to duration().
273 */
274 void seekFrameTime(double t, sksg::InvalidationController* = nullptr);
275
276 /**
277 * Returns the animation duration in seconds.
278 */
279 double duration() const { return fDuration; }
280
281 /**
282 * Returns the animation frame rate (frames / second).
283 */
284 double fps() const { return fFPS; }
285
286 /**
287 * Animation in point, in frame index units.
288 */
289 double inPoint() const { return fInPoint; }
290
291 /**
292 * Animation out point, in frame index units.
293 */
294 double outPoint() const { return fOutPoint; }
295
296 const SkString& version() const { return fVersion; }
297 const SkSize& size() const { return fSize; }
298
299 private:
300 enum Flags : uint32_t {
301 kRequiresTopLevelIsolation = 1 << 0, // Needs to draw into a layer due to layer blending.
302 };
303
304 Animation(sk_sp<sksg::RenderNode>,
305 std::vector<sk_sp<internal::Animator>>&&,
306 SkString ver, const SkSize& size,
307 double inPoint, double outPoint, double duration, double fps, uint32_t flags);
308
309 const sk_sp<sksg::RenderNode> fSceneRoot;
310 const std::vector<sk_sp<internal::Animator>> fAnimators;
311 const SkString fVersion;
312 const SkSize fSize;
313 const double fInPoint,
314 fOutPoint,
315 fDuration,
316 fFPS;
317 const uint32_t fFlags;
318
319 using INHERITED = SkNVRefCnt<Animation>;
320 };
321
322 } // namespace skottie
323
324 #endif // Skottie_DEFINED