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