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 | #include "modules/skottie/include/Skottie.h" |
9 | |
10 | #include "include/core/SkCanvas.h" |
11 | #include "include/core/SkData.h" |
12 | #include "include/core/SkFontMgr.h" |
13 | #include "include/core/SkImage.h" |
14 | #include "include/core/SkPaint.h" |
15 | #include "include/core/SkPoint.h" |
16 | #include "include/core/SkStream.h" |
17 | #include "include/private/SkTArray.h" |
18 | #include "include/private/SkTo.h" |
19 | #include "modules/skottie/include/ExternalLayer.h" |
20 | #include "modules/skottie/include/SkottieProperty.h" |
21 | #include "modules/skottie/src/Composition.h" |
22 | #include "modules/skottie/src/SkottieJson.h" |
23 | #include "modules/skottie/src/SkottiePriv.h" |
24 | #include "modules/skottie/src/SkottieValue.h" |
25 | #include "modules/skottie/src/Transform.h" |
26 | #include "modules/skottie/src/text/TextAdapter.h" |
27 | #include "modules/sksg/include/SkSGInvalidationController.h" |
28 | #include "modules/sksg/include/SkSGOpacityEffect.h" |
29 | #include "modules/sksg/include/SkSGPaint.h" |
30 | #include "modules/sksg/include/SkSGPath.h" |
31 | #include "modules/sksg/include/SkSGRenderEffect.h" |
32 | #include "modules/sksg/include/SkSGScene.h" |
33 | #include "modules/sksg/include/SkSGTransform.h" |
34 | #include "src/core/SkTraceEvent.h" |
35 | |
36 | #include <chrono> |
37 | #include <cmath> |
38 | #include <memory> |
39 | |
40 | #include "stdlib.h" |
41 | |
42 | namespace skottie { |
43 | |
44 | namespace internal { |
45 | |
46 | void AnimationBuilder::log(Logger::Level lvl, const skjson::Value* json, |
47 | const char fmt[], ...) const { |
48 | if (!fLogger) { |
49 | return; |
50 | } |
51 | |
52 | char buff[1024]; |
53 | va_list va; |
54 | va_start(va, fmt); |
55 | const auto len = vsnprintf(buff, sizeof(buff), fmt, va); |
56 | va_end(va); |
57 | |
58 | if (len < 0) { |
59 | SkDebugf("!! Could not format log message !!\n" ); |
60 | return; |
61 | } |
62 | |
63 | if (len >= SkToInt(sizeof(buff))) { |
64 | static constexpr char kEllipsesStr[] = "..." ; |
65 | strcpy(buff + sizeof(buff) - sizeof(kEllipsesStr), kEllipsesStr); |
66 | } |
67 | |
68 | SkString jsonstr = json ? json->toString() : SkString(); |
69 | |
70 | fLogger->log(lvl, buff, jsonstr.c_str()); |
71 | } |
72 | |
73 | namespace { |
74 | |
75 | class OpacityAdapter final : public DiscardableAdapterBase<OpacityAdapter, sksg::OpacityEffect> { |
76 | public: |
77 | OpacityAdapter(const skjson::ObjectValue& jobject, |
78 | sk_sp<sksg::RenderNode> child, |
79 | const AnimationBuilder& abuilder) |
80 | : INHERITED(sksg::OpacityEffect::Make(child)) { |
81 | this->bind(abuilder, jobject["o" ], fOpacity); |
82 | } |
83 | |
84 | private: |
85 | void onSync() override { |
86 | this->node()->setOpacity(fOpacity * 0.01f); |
87 | } |
88 | |
89 | ScalarValue fOpacity = 100; |
90 | |
91 | using INHERITED = DiscardableAdapterBase<OpacityAdapter, sksg::OpacityEffect>; |
92 | }; |
93 | |
94 | static SkBlendMode GetBlendMode(const skjson::ObjectValue& jobject, |
95 | const AnimationBuilder* abuilder) { |
96 | static constexpr SkBlendMode kBlendModeMap[] = { |
97 | SkBlendMode::kSrcOver, // 0:'normal' |
98 | SkBlendMode::kMultiply, // 1:'multiply' |
99 | SkBlendMode::kScreen, // 2:'screen' |
100 | SkBlendMode::kOverlay, // 3:'overlay |
101 | SkBlendMode::kDarken, // 4:'darken' |
102 | SkBlendMode::kLighten, // 5:'lighten' |
103 | SkBlendMode::kColorDodge, // 6:'color-dodge' |
104 | SkBlendMode::kColorBurn, // 7:'color-burn' |
105 | SkBlendMode::kHardLight, // 8:'hard-light' |
106 | SkBlendMode::kSoftLight, // 9:'soft-light' |
107 | SkBlendMode::kDifference, // 10:'difference' |
108 | SkBlendMode::kExclusion, // 11:'exclusion' |
109 | SkBlendMode::kHue, // 12:'hue' |
110 | SkBlendMode::kSaturation, // 13:'saturation' |
111 | SkBlendMode::kColor, // 14:'color' |
112 | SkBlendMode::kLuminosity, // 15:'luminosity' |
113 | SkBlendMode::kPlus, // 16:'add' |
114 | }; |
115 | |
116 | const auto bm_index = ParseDefault<size_t>(jobject["bm" ], 0); |
117 | if (bm_index >= SK_ARRAY_COUNT(kBlendModeMap)) { |
118 | abuilder->log(Logger::Level::kWarning, &jobject, |
119 | "Unsupported blend mode %lu\n" , bm_index); |
120 | return SkBlendMode::kSrcOver; |
121 | } |
122 | |
123 | return kBlendModeMap[bm_index]; |
124 | } |
125 | |
126 | } // namespace |
127 | |
128 | sk_sp<sksg::RenderNode> AnimationBuilder::attachOpacity(const skjson::ObjectValue& jobject, |
129 | sk_sp<sksg::RenderNode> child_node) const { |
130 | if (!child_node) |
131 | return nullptr; |
132 | |
133 | auto adapter = OpacityAdapter::Make(jobject, child_node, *this); |
134 | const auto dispatched = this->dispatchOpacityProperty(adapter->node()); |
135 | |
136 | if (adapter->isStatic()) { |
137 | adapter->seek(0); |
138 | if (!dispatched && adapter->node()->getOpacity() >= 1) { |
139 | // No obeservable effects - we can discard. |
140 | return child_node; |
141 | } |
142 | } else { |
143 | fCurrentAnimatorScope->push_back(adapter); |
144 | } |
145 | |
146 | return adapter->node(); |
147 | } |
148 | |
149 | sk_sp<sksg::RenderNode> AnimationBuilder::attachBlendMode(const skjson::ObjectValue& jobject, |
150 | sk_sp<sksg::RenderNode> child) const { |
151 | const auto bm = GetBlendMode(jobject, this); |
152 | if (bm != SkBlendMode::kSrcOver) { |
153 | fHasNontrivialBlending = true; |
154 | child = sksg::BlendModeEffect::Make(std::move(child), bm); |
155 | } |
156 | |
157 | return child; |
158 | } |
159 | |
160 | AnimationBuilder::AnimationBuilder(sk_sp<ResourceProvider> rp, sk_sp<SkFontMgr> fontmgr, |
161 | sk_sp<PropertyObserver> pobserver, sk_sp<Logger> logger, |
162 | sk_sp<MarkerObserver> mobserver, sk_sp<PrecompInterceptor> pi, |
163 | Animation::Builder::Stats* stats, |
164 | const SkSize& comp_size, float duration, float framerate, |
165 | uint32_t flags) |
166 | : fResourceProvider(std::move(rp)) |
167 | , fLazyFontMgr(std::move(fontmgr)) |
168 | , fPropertyObserver(std::move(pobserver)) |
169 | , fLogger(std::move(logger)) |
170 | , fMarkerObserver(std::move(mobserver)) |
171 | , fPrecompInterceptor(std::move(pi)) |
172 | , fStats(stats) |
173 | , fCompSize(comp_size) |
174 | , fDuration(duration) |
175 | , fFrameRate(framerate) |
176 | , fFlags(flags) |
177 | , fHasNontrivialBlending(false) {} |
178 | |
179 | AnimationBuilder::AnimationInfo AnimationBuilder::parse(const skjson::ObjectValue& jroot) { |
180 | this->dispatchMarkers(jroot["markers" ]); |
181 | |
182 | this->parseAssets(jroot["assets" ]); |
183 | this->parseFonts(jroot["fonts" ], jroot["chars" ]); |
184 | |
185 | AutoScope ascope(this); |
186 | auto root = CompositionBuilder(*this, fCompSize, jroot).build(*this); |
187 | |
188 | auto animators = ascope.release(); |
189 | fStats->fAnimatorCount = animators.size(); |
190 | |
191 | return { sksg::Scene::Make(std::move(root)), std::move(animators) }; |
192 | } |
193 | |
194 | void AnimationBuilder::parseAssets(const skjson::ArrayValue* jassets) { |
195 | if (!jassets) { |
196 | return; |
197 | } |
198 | |
199 | for (const skjson::ObjectValue* asset : *jassets) { |
200 | if (asset) { |
201 | fAssets.set(ParseDefault<SkString>((*asset)["id" ], SkString()), { asset, false }); |
202 | } |
203 | } |
204 | } |
205 | |
206 | void AnimationBuilder::dispatchMarkers(const skjson::ArrayValue* jmarkers) const { |
207 | if (!fMarkerObserver || !jmarkers) { |
208 | return; |
209 | } |
210 | |
211 | // For frame-number -> t conversions. |
212 | const auto frameRatio = 1 / (fFrameRate * fDuration); |
213 | |
214 | for (const skjson::ObjectValue* m : *jmarkers) { |
215 | if (!m) continue; |
216 | |
217 | const skjson::StringValue* name = (*m)["cm" ]; |
218 | const auto time = ParseDefault((*m)["tm" ], -1.0f), |
219 | duration = ParseDefault((*m)["dr" ], -1.0f); |
220 | |
221 | if (name && time >= 0 && duration >= 0) { |
222 | fMarkerObserver->onMarker( |
223 | name->begin(), |
224 | // "tm" is in frames |
225 | time * frameRatio, |
226 | // ... as is "dr" |
227 | (time + duration) * frameRatio |
228 | ); |
229 | } else { |
230 | this->log(Logger::Level::kWarning, m, "Ignoring unexpected marker." ); |
231 | } |
232 | } |
233 | } |
234 | |
235 | bool AnimationBuilder::dispatchColorProperty(const sk_sp<sksg::Color>& c) const { |
236 | bool dispatched = false; |
237 | |
238 | if (fPropertyObserver) { |
239 | fPropertyObserver->onColorProperty(fPropertyObserverContext, |
240 | [&]() { |
241 | dispatched = true; |
242 | return std::make_unique<ColorPropertyHandle>(c); |
243 | }); |
244 | } |
245 | |
246 | return dispatched; |
247 | } |
248 | |
249 | bool AnimationBuilder::dispatchOpacityProperty(const sk_sp<sksg::OpacityEffect>& o) const { |
250 | bool dispatched = false; |
251 | |
252 | if (fPropertyObserver) { |
253 | fPropertyObserver->onOpacityProperty(fPropertyObserverContext, |
254 | [&]() { |
255 | dispatched = true; |
256 | return std::make_unique<OpacityPropertyHandle>(o); |
257 | }); |
258 | } |
259 | |
260 | return dispatched; |
261 | } |
262 | |
263 | bool AnimationBuilder::dispatchTextProperty(const sk_sp<TextAdapter>& t) const { |
264 | bool dispatched = false; |
265 | |
266 | if (fPropertyObserver) { |
267 | fPropertyObserver->onTextProperty(fPropertyObserverContext, |
268 | [&]() { |
269 | dispatched = true; |
270 | return std::make_unique<TextPropertyHandle>(t); |
271 | }); |
272 | } |
273 | |
274 | return dispatched; |
275 | } |
276 | |
277 | bool AnimationBuilder::dispatchTransformProperty(const sk_sp<TransformAdapter2D>& t) const { |
278 | bool dispatched = false; |
279 | |
280 | if (fPropertyObserver) { |
281 | fPropertyObserver->onTransformProperty(fPropertyObserverContext, |
282 | [&]() { |
283 | dispatched = true; |
284 | return std::make_unique<TransformPropertyHandle>(t); |
285 | }); |
286 | } |
287 | |
288 | return dispatched; |
289 | } |
290 | |
291 | void AnimationBuilder::AutoPropertyTracker::updateContext(PropertyObserver* observer, |
292 | const skjson::ObjectValue& obj) { |
293 | |
294 | const skjson::StringValue* name = obj["nm" ]; |
295 | |
296 | fBuilder->fPropertyObserverContext = name ? name->begin() : nullptr; |
297 | } |
298 | |
299 | } // namespace internal |
300 | |
301 | void Logger::log(Level, const char[], const char*) {} |
302 | |
303 | Animation::Builder::Builder(uint32_t flags) : fFlags(flags) {} |
304 | Animation::Builder::~Builder() = default; |
305 | |
306 | Animation::Builder& Animation::Builder::setResourceProvider(sk_sp<ResourceProvider> rp) { |
307 | fResourceProvider = std::move(rp); |
308 | return *this; |
309 | } |
310 | |
311 | Animation::Builder& Animation::Builder::setFontManager(sk_sp<SkFontMgr> fmgr) { |
312 | fFontMgr = std::move(fmgr); |
313 | return *this; |
314 | } |
315 | |
316 | Animation::Builder& Animation::Builder::setPropertyObserver(sk_sp<PropertyObserver> pobserver) { |
317 | fPropertyObserver = std::move(pobserver); |
318 | return *this; |
319 | } |
320 | |
321 | Animation::Builder& Animation::Builder::setLogger(sk_sp<Logger> logger) { |
322 | fLogger = std::move(logger); |
323 | return *this; |
324 | } |
325 | |
326 | Animation::Builder& Animation::Builder::setMarkerObserver(sk_sp<MarkerObserver> mobserver) { |
327 | fMarkerObserver = std::move(mobserver); |
328 | return *this; |
329 | } |
330 | |
331 | Animation::Builder& Animation::Builder::setPrecompInterceptor(sk_sp<PrecompInterceptor> pi) { |
332 | fPrecompInterceptor = std::move(pi); |
333 | return *this; |
334 | } |
335 | |
336 | sk_sp<Animation> Animation::Builder::make(SkStream* stream) { |
337 | if (!stream->hasLength()) { |
338 | // TODO: handle explicit buffering? |
339 | if (fLogger) { |
340 | fLogger->log(Logger::Level::kError, "Cannot parse streaming content.\n" ); |
341 | } |
342 | return nullptr; |
343 | } |
344 | |
345 | auto data = SkData::MakeFromStream(stream, stream->getLength()); |
346 | if (!data) { |
347 | if (fLogger) { |
348 | fLogger->log(Logger::Level::kError, "Failed to read the input stream.\n" ); |
349 | } |
350 | return nullptr; |
351 | } |
352 | |
353 | return this->make(static_cast<const char*>(data->data()), data->size()); |
354 | } |
355 | |
356 | sk_sp<Animation> Animation::Builder::make(const char* data, size_t data_len) { |
357 | TRACE_EVENT0("skottie" , TRACE_FUNC); |
358 | |
359 | // Sanitize factory args. |
360 | class NullResourceProvider final : public ResourceProvider { |
361 | sk_sp<SkData> load(const char[], const char[]) const override { return nullptr; } |
362 | }; |
363 | auto resolvedProvider = fResourceProvider |
364 | ? fResourceProvider : sk_make_sp<NullResourceProvider>(); |
365 | |
366 | fStats = Stats{}; |
367 | |
368 | fStats.fJsonSize = data_len; |
369 | const auto t0 = std::chrono::steady_clock::now(); |
370 | |
371 | const skjson::DOM dom(data, data_len); |
372 | if (!dom.root().is<skjson::ObjectValue>()) { |
373 | // TODO: more error info. |
374 | if (fLogger) { |
375 | fLogger->log(Logger::Level::kError, "Failed to parse JSON input.\n" ); |
376 | } |
377 | return nullptr; |
378 | } |
379 | const auto& json = dom.root().as<skjson::ObjectValue>(); |
380 | |
381 | const auto t1 = std::chrono::steady_clock::now(); |
382 | fStats.fJsonParseTimeMS = std::chrono::duration<float, std::milli>{t1-t0}.count(); |
383 | |
384 | const auto version = ParseDefault<SkString>(json["v" ], SkString()); |
385 | const auto size = SkSize::Make(ParseDefault<float>(json["w" ], 0.0f), |
386 | ParseDefault<float>(json["h" ], 0.0f)); |
387 | const auto fps = ParseDefault<float>(json["fr" ], -1.0f), |
388 | inPoint = ParseDefault<float>(json["ip" ], 0.0f), |
389 | outPoint = std::max(ParseDefault<float>(json["op" ], SK_ScalarMax), inPoint), |
390 | duration = sk_ieee_float_divide(outPoint - inPoint, fps); |
391 | |
392 | if (size.isEmpty() || version.isEmpty() || fps <= 0 || |
393 | !SkScalarIsFinite(inPoint) || !SkScalarIsFinite(outPoint) || !SkScalarIsFinite(duration)) { |
394 | if (fLogger) { |
395 | const auto msg = SkStringPrintf( |
396 | "Invalid animation params (version: %s, size: [%f %f], frame rate: %f, " |
397 | "in-point: %f, out-point: %f)\n" , |
398 | version.c_str(), size.width(), size.height(), fps, inPoint, outPoint); |
399 | fLogger->log(Logger::Level::kError, msg.c_str()); |
400 | } |
401 | return nullptr; |
402 | } |
403 | |
404 | SkASSERT(resolvedProvider); |
405 | internal::AnimationBuilder builder(std::move(resolvedProvider), fFontMgr, |
406 | std::move(fPropertyObserver), |
407 | std::move(fLogger), |
408 | std::move(fMarkerObserver), |
409 | std::move(fPrecompInterceptor), |
410 | &fStats, size, duration, fps, fFlags); |
411 | auto ainfo = builder.parse(json); |
412 | |
413 | const auto t2 = std::chrono::steady_clock::now(); |
414 | fStats.fSceneParseTimeMS = std::chrono::duration<float, std::milli>{t2-t1}.count(); |
415 | fStats.fTotalLoadTimeMS = std::chrono::duration<float, std::milli>{t2-t0}.count(); |
416 | |
417 | if (!ainfo.fScene && fLogger) { |
418 | fLogger->log(Logger::Level::kError, "Could not parse animation.\n" ); |
419 | } |
420 | |
421 | uint32_t flags = 0; |
422 | if (builder.hasNontrivialBlending()) { |
423 | flags |= Animation::Flags::kRequiresTopLevelIsolation; |
424 | } |
425 | |
426 | return sk_sp<Animation>(new Animation(std::move(ainfo.fScene), |
427 | std::move(ainfo.fAnimators), |
428 | std::move(version), |
429 | size, |
430 | inPoint, |
431 | outPoint, |
432 | duration, |
433 | fps, |
434 | flags)); |
435 | } |
436 | |
437 | sk_sp<Animation> Animation::Builder::makeFromFile(const char path[]) { |
438 | const auto data = SkData::MakeFromFileName(path); |
439 | |
440 | return data ? this->make(static_cast<const char*>(data->data()), data->size()) |
441 | : nullptr; |
442 | } |
443 | |
444 | Animation::Animation(std::unique_ptr<sksg::Scene> scene, |
445 | std::vector<sk_sp<internal::Animator>>&& animators, |
446 | SkString version, const SkSize& size, |
447 | double inPoint, double outPoint, double duration, double fps, uint32_t flags) |
448 | : fScene(std::move(scene)) |
449 | , fAnimators(std::move(animators)) |
450 | , fVersion(std::move(version)) |
451 | , fSize(size) |
452 | , fInPoint(inPoint) |
453 | , fOutPoint(outPoint) |
454 | , fDuration(duration) |
455 | , fFPS(fps) |
456 | , fFlags(flags) {} |
457 | |
458 | Animation::~Animation() = default; |
459 | |
460 | void Animation::render(SkCanvas* canvas, const SkRect* dstR) const { |
461 | this->render(canvas, dstR, 0); |
462 | } |
463 | |
464 | void Animation::render(SkCanvas* canvas, const SkRect* dstR, RenderFlags renderFlags) const { |
465 | TRACE_EVENT0("skottie" , TRACE_FUNC); |
466 | |
467 | if (!fScene) |
468 | return; |
469 | |
470 | SkAutoCanvasRestore restore(canvas, true); |
471 | |
472 | const SkRect srcR = SkRect::MakeSize(this->size()); |
473 | if (dstR) { |
474 | canvas->concat(SkMatrix::MakeRectToRect(srcR, *dstR, SkMatrix::kCenter_ScaleToFit)); |
475 | } |
476 | |
477 | if (!(renderFlags & RenderFlag::kDisableTopLevelClipping)) { |
478 | canvas->clipRect(srcR); |
479 | } |
480 | |
481 | if ((fFlags & Flags::kRequiresTopLevelIsolation) && |
482 | !(renderFlags & RenderFlag::kSkipTopLevelIsolation)) { |
483 | // The animation uses non-trivial blending, and needs |
484 | // to be rendered into a separate/transparent layer. |
485 | canvas->saveLayer(srcR, nullptr); |
486 | } |
487 | |
488 | fScene->render(canvas); |
489 | } |
490 | |
491 | void Animation::seekFrame(double t, sksg::InvalidationController* ic) { |
492 | TRACE_EVENT0("skottie" , TRACE_FUNC); |
493 | |
494 | if (!fScene) |
495 | return; |
496 | |
497 | // Per AE/Lottie semantics out_point is exclusive. |
498 | const auto kLastValidFrame = std::nextafterf(fOutPoint, fInPoint), |
499 | comp_time = SkTPin<float>(fInPoint + t, fInPoint, kLastValidFrame); |
500 | |
501 | for (const auto& anim : fAnimators) { |
502 | anim->seek(comp_time); |
503 | } |
504 | |
505 | fScene->revalidate(ic); |
506 | } |
507 | |
508 | void Animation::seekFrameTime(double t, sksg::InvalidationController* ic) { |
509 | this->seekFrame(t * fFPS, ic); |
510 | } |
511 | |
512 | sk_sp<Animation> Animation::Make(const char* data, size_t length) { |
513 | return Builder().make(data, length); |
514 | } |
515 | |
516 | sk_sp<Animation> Animation::Make(SkStream* stream) { |
517 | return Builder().make(stream); |
518 | } |
519 | |
520 | sk_sp<Animation> Animation::MakeFromFile(const char path[]) { |
521 | return Builder().makeFromFile(path); |
522 | } |
523 | |
524 | } // namespace skottie |
525 | |