Debian Codegraph @

modules/skottie/src/SkottiePriv.h

raw
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 SkottiePriv_DEFINED
9 #define SkottiePriv_DEFINED
10
11 #include "include/core/SkRefCnt.h"
12 #include "modules/skottie/include/Skottie.h"
13
14 #include "include/core/SkFontMgr.h"
15 #include "include/core/SkFontStyle.h"
16 #include "include/core/SkString.h"
17 #include "include/core/SkTypeface.h"
18 #include "modules/skottie/include/ExternalLayer.h"
19 #include "modules/skottie/include/SkottieProperty.h"
20 #include "modules/skottie/include/SlotManager.h"
21 #include "modules/skottie/src/animator/Animator.h"
22 #include "modules/skottie/src/text/Font.h"
23 #include "src/base/SkUTF.h"
24 #include "src/core/SkTHash.h"
25
26 #include "modules/skshaper/include/SkShaper_factory.h"
27
28 #include <string>
29 #include <vector>
30
31 namespace skjson {
32 class ArrayValue;
33 class ObjectValue;
34 class Value;
35 } // namespace skjson
36
37 namespace sksg {
38 class Color;
39 class Path;
40 class RenderNode;
41 class Transform;
42 } // namespace sksg
43
44 namespace skottie {
45 namespace internal {
46
47 // Close-enough to AE.
48 static constexpr float kBlurSizeToSigma = 0.3f;
49
50 class TextAdapter;
51 class TransformAdapter2D;
52 class TransformAdapter3D;
53 class OpacityAdapter;
54
55
56 using AnimatorScope = std::vector<sk_sp<Animator>>;
57
58 class SceneGraphRevalidator final : public SkNVRefCnt<SceneGraphRevalidator> {
59 public:
60 void revalidate();
61 void setRoot(sk_sp<sksg::RenderNode>);
62
63 private:
64 sk_sp<sksg::RenderNode> fRoot;
65 };
66
67 class AnimationBuilder final : public SkNoncopyable {
68 public:
69 AnimationBuilder(sk_sp<ResourceProvider>, sk_sp<SkFontMgr>, sk_sp<PropertyObserver>,
70 sk_sp<Logger>, sk_sp<MarkerObserver>, sk_sp<PrecompInterceptor>,
71 sk_sp<ExpressionManager>, sk_sp<SkShapers::Factory>,
72 Animation::Builder::Stats*, const SkSize& comp_size,
73 float duration, float framerate, uint32_t flags);
74
75 struct AnimationInfo {
76 sk_sp<sksg::RenderNode> fSceneRoot;
77 AnimatorScope fAnimators;
78 sk_sp<SlotManager> fSlotManager;
79 std::vector<LayerInfo> fLayerInfo;
80 };
81
82 AnimationInfo parse(const skjson::ObjectValue&);
83
84 struct FontInfo {
85 SkString fFamily,
86 fStyle,
87 fPath;
88 SkScalar fAscentPct;
89 sk_sp<SkTypeface> fTypeface;
90 CustomFont::Builder fCustomFontBuilder;
91
92 bool matches(const char family[], const char style[]) const;
93 };
94 const FontInfo* findFont(const SkString& name) const;
95
96 void log(Logger::Level, const skjson::Value*, const char fmt[], ...) const SK_PRINTF_LIKE(4, 5);
97
98 sk_sp<sksg::Transform> attachMatrix2D(const skjson::ObjectValue&, sk_sp<sksg::Transform>,
99 bool auto_orient = false) const;
100 sk_sp<sksg::Transform> attachMatrix3D(const skjson::ObjectValue&, sk_sp<sksg::Transform>,
101 bool auto_orient = false) const;
102
103 sk_sp<sksg::Transform> attachCamera(const skjson::ObjectValue& jlayer,
104 const skjson::ObjectValue& jtransform,
105 sk_sp<sksg::Transform>,
106 const SkSize&) const;
107
108 sk_sp<sksg::RenderNode> attachOpacity(const skjson::ObjectValue&,
109 sk_sp<sksg::RenderNode>) const;
110 sk_sp<sksg::Path> attachPath(const skjson::Value&) const;
111
112 bool hasNontrivialBlending() const { return fHasNontrivialBlending; }
113
114 class AutoScope final {
115 public:
116 explicit AutoScope(const AnimationBuilder* builder) : AutoScope(builder, AnimatorScope()) {}
117
118 AutoScope(const AnimationBuilder* builder, AnimatorScope&& scope)
119 : fBuilder(builder)
120 , fCurrentScope(std::move(scope))
121 , fPrevScope(fBuilder->fCurrentAnimatorScope) {
122 fBuilder->fCurrentAnimatorScope = &fCurrentScope;
123 }
124
125 AnimatorScope release() {
126 fBuilder->fCurrentAnimatorScope = fPrevScope;
127 SkDEBUGCODE(fBuilder = nullptr);
128
129 return std::move(fCurrentScope);
130 }
131
132 ~AutoScope() { SkASSERT(!fBuilder); }
133
134 private:
135 const AnimationBuilder* fBuilder;
136 AnimatorScope fCurrentScope;
137 AnimatorScope* fPrevScope;
138 };
139
140 template <typename T>
141 void attachDiscardableAdapter(sk_sp<T> adapter) const {
142 if (adapter->isStatic()) {
143 // Fire off a synthetic tick to force a single SG sync before discarding.
144 adapter->seek(0);
145 } else {
146 fCurrentAnimatorScope->push_back(std::move(adapter));
147 }
148 }
149
150 template <typename T, typename... Args>
151 auto attachDiscardableAdapter(Args&&... args) const ->
152 typename std::decay<decltype(T::Make(std::forward<Args>(args)...)->node())>::type
153 {
154 using NodeType =
155 typename std::decay<decltype(T::Make(std::forward<Args>(args)...)->node())>::type;
156
157 NodeType node;
158 if (auto adapter = T::Make(std::forward<Args>(args)...)) {
159 node = adapter->node();
160 this->attachDiscardableAdapter(std::move(adapter));
161 }
162 return node;
163 }
164
165 class AutoPropertyTracker {
166 public:
167 AutoPropertyTracker(const AnimationBuilder* builder, const skjson::ObjectValue& obj, const PropertyObserver::NodeType node_type)
168 : fBuilder(builder)
169 , fPrevContext(builder->fPropertyObserverContext), fNodeType(node_type) {
170 if (fBuilder->fPropertyObserver) {
171 auto observer = builder->fPropertyObserver.get();
172 this->updateContext(observer, obj);
173 observer->onEnterNode(fBuilder->fPropertyObserverContext, fNodeType);
174 }
175 }
176
177 ~AutoPropertyTracker() {
178 if (fBuilder->fPropertyObserver) {
179 fBuilder->fPropertyObserver->onLeavingNode(fBuilder->fPropertyObserverContext, fNodeType);
180 fBuilder->fPropertyObserverContext = fPrevContext;
181 }
182 }
183 private:
184 void updateContext(PropertyObserver*, const skjson::ObjectValue&);
185
186 const AnimationBuilder* fBuilder;
187 const char* fPrevContext;
188 const PropertyObserver::NodeType fNodeType;
189 };
190
191 bool dispatchColorProperty(const sk_sp<sksg::Color>&) const;
192 bool dispatchOpacityProperty(const sk_sp<sksg::OpacityEffect>&) const;
193 bool dispatchTextProperty(const sk_sp<TextAdapter>&,
194 const skjson::ObjectValue* jtext) const;
195 bool dispatchTransformProperty(const sk_sp<TransformAdapter2D>&) const;
196
197 sk_sp<ExpressionManager> expression_manager() const;
198
199 const skjson::ObjectValue* getSlotsRoot() const {
200 return fSlotsRoot;
201 }
202
203 void parseFonts (const skjson::ObjectValue* jfonts, const skjson::ArrayValue* jchars);
204
205 private:
206 friend class CompositionBuilder;
207 friend class CustomFont;
208 friend class LayerBuilder;
209 friend class AnimatablePropertyContainer;
210 friend class SkSLEffectBase;
211
212 struct AttachLayerContext;
213 struct AttachShapeContext;
214 struct FootageAssetInfo;
215
216 void parseAssets(const skjson::ArrayValue*);
217
218 // Return true iff all fonts were resolved.
219 bool resolveNativeTypefaces();
220 bool resolveEmbeddedTypefaces(const skjson::ArrayValue& jchars);
221
222 void dispatchMarkers(const skjson::ArrayValue*) const;
223
224 sk_sp<sksg::RenderNode> attachBlendMode(const skjson::ObjectValue&,
225 sk_sp<sksg::RenderNode>) const;
226
227 sk_sp<sksg::RenderNode> attachShape(const skjson::ArrayValue*, AttachShapeContext*,
228 bool suppress_draws = false) const;
229 const FootageAssetInfo* loadFootageAsset(const skjson::ObjectValue&) const;
230 sk_sp<sksg::RenderNode> attachFootageAsset(const skjson::ObjectValue&, LayerInfo*) const;
231
232 sk_sp<sksg::RenderNode> attachExternalPrecompLayer(const skjson::ObjectValue&,
233 const LayerInfo&) const;
234
235 sk_sp<sksg::RenderNode> attachFootageLayer(const skjson::ObjectValue&, LayerInfo*) const;
236 sk_sp<sksg::RenderNode> attachNullLayer (const skjson::ObjectValue&, LayerInfo*) const;
237 sk_sp<sksg::RenderNode> attachPrecompLayer(const skjson::ObjectValue&, LayerInfo*) const;
238 sk_sp<sksg::RenderNode> attachShapeLayer (const skjson::ObjectValue&, LayerInfo*) const;
239 sk_sp<sksg::RenderNode> attachSolidLayer (const skjson::ObjectValue&, LayerInfo*) const;
240 sk_sp<sksg::RenderNode> attachTextLayer (const skjson::ObjectValue&, LayerInfo*) const;
241 sk_sp<sksg::RenderNode> attachAudioLayer (const skjson::ObjectValue&, LayerInfo*) const;
242
243 void trackLayerInfo(const LayerInfo& info) const {fLayerInfo.emplace_back(info);}
244
245 sk_sp<ResourceProvider> fResourceProvider;
246 sk_sp<SkFontMgr> fFontMgr;
247 sk_sp<PropertyObserver> fPropertyObserver;
248 sk_sp<Logger> fLogger;
249 sk_sp<MarkerObserver> fMarkerObserver;
250 sk_sp<PrecompInterceptor> fPrecompInterceptor;
251 sk_sp<ExpressionManager> fExpressionManager;
252 sk_sp<SkShapers::Factory> fShapingFactory;
253 sk_sp<SceneGraphRevalidator> fRevalidator;
254 sk_sp<SlotManager> fSlotManager;
255 Animation::Builder::Stats* fStats;
256 const SkSize fCompSize;
257 const float fDuration,
258 fFrameRate;
259 const uint32_t fFlags;
260 mutable std::vector<LayerInfo> fLayerInfo;
261 mutable AnimatorScope* fCurrentAnimatorScope;
262 mutable const char* fPropertyObserverContext = nullptr;
263 mutable bool fHasNontrivialBlending : 1;
264 struct AssetInfo {
265 const skjson::ObjectValue* fAsset;
266 mutable bool fIsAttaching; // Used for cycle detection
267 };
268
269 struct FootageAssetInfo {
270 sk_sp<ImageAsset> fAsset;
271 SkISize fSize;
272 };
273
274 class ScopedAssetRef {
275 public:
276 ScopedAssetRef(const AnimationBuilder* abuilder, const skjson::ObjectValue& jlayer);
277
278 ~ScopedAssetRef() {
279 if (fInfo) {
280 fInfo->fIsAttaching = false;
281 }
282 }
283
284 explicit operator bool() const { return !!fInfo; }
285
286 const skjson::ObjectValue& operator*() const { return *fInfo->fAsset; }
287
288 private:
289 const AssetInfo* fInfo = nullptr;
290 };
291
292 skia_private::THashMap<SkString, AssetInfo> fAssets;
293 skia_private::THashMap<SkString, FontInfo> fFonts;
294 sk_sp<CustomFont::GlyphCompMapper> fCustomGlyphMapper;
295 mutable skia_private::THashMap<SkString, FootageAssetInfo> fImageAssetCache;
296
297 // Handle to "slots" JSON Object, used to grab slot values while building
298 const skjson::ObjectValue* fSlotsRoot;
299
300 using INHERITED = SkNoncopyable;
301 };
302
303 } // namespace internal
304 } // namespace skottie
305
306 #endif // SkottiePriv_DEFINED