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