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 "Skottie.h"
9 
10 #include "SkCanvas.h"
11 #include "SkData.h"
12 #include "SkFontMgr.h"
13 #include "SkImage.h"
14 #include "SkMakeUnique.h"
15 #include "SkPaint.h"
16 #include "SkPoint.h"
17 #include "SkSGColor.h"
18 #include "SkSGInvalidationController.h"
19 #include "SkSGOpacityEffect.h"
20 #include "SkSGPath.h"
21 #include "SkSGScene.h"
22 #include "SkSGTransform.h"
23 #include "SkStream.h"
24 #include "SkTArray.h"
25 #include "SkTime.h"
26 #include "SkTo.h"
27 #include "SkottieAdapter.h"
28 #include "SkottieJson.h"
29 #include "SkottiePriv.h"
30 #include "SkottieProperty.h"
31 #include "SkottieValue.h"
32 #include "SkTraceEvent.h"
33 
34 #include <cmath>
35 
36 #include "stdlib.h"
37 
38 namespace skottie {
39 
40 namespace internal {
41 
log(Logger::Level lvl,const skjson::Value * json,const char fmt[],...) const42 void AnimationBuilder::log(Logger::Level lvl, const skjson::Value* json,
43                            const char fmt[], ...) const {
44     if (!fLogger) {
45         return;
46     }
47 
48     char buff[1024];
49     va_list va;
50     va_start(va, fmt);
51     const auto len = vsprintf(buff, fmt, va);
52     va_end(va);
53 
54     if (len < 0 || len >= SkToInt(sizeof(buff))) {
55         SkDebugf("!! Could not format log message !!\n");
56         return;
57     }
58 
59     SkString jsonstr = json ? json->toString() : SkString();
60 
61     fLogger->log(lvl, buff, jsonstr.c_str());
62 }
63 
attachMatrix2D(const skjson::ObjectValue & t,AnimatorScope * ascope,sk_sp<sksg::Transform> parent) const64 sk_sp<sksg::Transform> AnimationBuilder::attachMatrix2D(const skjson::ObjectValue& t,
65                                                         AnimatorScope* ascope,
66                                                         sk_sp<sksg::Transform> parent) const {
67     static const VectorValue g_default_vec_0   = {  0,   0},
68                              g_default_vec_100 = {100, 100};
69 
70     auto matrix = sksg::Matrix<SkMatrix>::Make(SkMatrix::I());
71     auto adapter = sk_make_sp<TransformAdapter2D>(matrix);
72 
73     auto bound = this->bindProperty<VectorValue>(t["a"], ascope,
74             [adapter](const VectorValue& a) {
75                 adapter->setAnchorPoint(ValueTraits<VectorValue>::As<SkPoint>(a));
76             }, g_default_vec_0);
77     bound |= this->bindProperty<VectorValue>(t["p"], ascope,
78             [adapter](const VectorValue& p) {
79                 adapter->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
80             }, g_default_vec_0);
81     bound |= this->bindProperty<VectorValue>(t["s"], ascope,
82             [adapter](const VectorValue& s) {
83                 adapter->setScale(ValueTraits<VectorValue>::As<SkVector>(s));
84             }, g_default_vec_100);
85 
86     const auto* jrotation = &t["r"];
87     if (jrotation->is<skjson::NullValue>()) {
88         // 3d rotations have separate rx,ry,rz components.  While we don't fully support them,
89         // we can still make use of rz.
90         jrotation = &t["rz"];
91     }
92     bound |= this->bindProperty<ScalarValue>(*jrotation, ascope,
93             [adapter](const ScalarValue& r) {
94                 adapter->setRotation(r);
95             }, 0.0f);
96     bound |= this->bindProperty<ScalarValue>(t["sk"], ascope,
97             [adapter](const ScalarValue& sk) {
98                 adapter->setSkew(sk);
99             }, 0.0f);
100     bound |= this->bindProperty<ScalarValue>(t["sa"], ascope,
101             [adapter](const ScalarValue& sa) {
102                 adapter->setSkewAxis(sa);
103             }, 0.0f);
104 
105     const auto dispatched = this->dispatchTransformProperty(adapter);
106 
107     return (bound || dispatched)
108         ? sksg::Transform::MakeConcat(std::move(parent), std::move(matrix))
109         : parent;
110 }
111 
attachMatrix3D(const skjson::ObjectValue & t,AnimatorScope * ascope,sk_sp<sksg::Transform> parent) const112 sk_sp<sksg::Transform> AnimationBuilder::attachMatrix3D(const skjson::ObjectValue& t,
113                                                         AnimatorScope* ascope,
114                                                         sk_sp<sksg::Transform> parent) const {
115     static const VectorValue g_default_vec_0   = {  0,   0,   0},
116                              g_default_vec_100 = {100, 100, 100};
117 
118     auto matrix = sksg::Matrix<SkMatrix44>::Make(SkMatrix::I());
119     auto adapter = sk_make_sp<TransformAdapter3D>(matrix);
120 
121     auto bound = this->bindProperty<VectorValue>(t["a"], ascope,
122             [adapter](const VectorValue& a) {
123                 adapter->setAnchorPoint(TransformAdapter3D::Vec3(a));
124             }, g_default_vec_0);
125     bound |= this->bindProperty<VectorValue>(t["p"], ascope,
126             [adapter](const VectorValue& p) {
127                 adapter->setPosition(TransformAdapter3D::Vec3(p));
128             }, g_default_vec_0);
129     bound |= this->bindProperty<VectorValue>(t["s"], ascope,
130             [adapter](const VectorValue& s) {
131                 adapter->setScale(TransformAdapter3D::Vec3(s));
132             }, g_default_vec_100);
133 
134     // Orientation and rx/ry/rz are mapped to the same rotation property -- the difference is
135     // in how they get interpolated (vector vs. scalar/decomposed interpolation).
136     bound |= this->bindProperty<VectorValue>(t["or"], ascope,
137             [adapter](const VectorValue& o) {
138                 adapter->setRotation(TransformAdapter3D::Vec3(o));
139             }, g_default_vec_0);
140 
141     bound |= this->bindProperty<ScalarValue>(t["rx"], ascope,
142             [adapter](const ScalarValue& rx) {
143                 const auto& r = adapter->getRotation();
144                 adapter->setRotation(TransformAdapter3D::Vec3({rx, r.fY, r.fZ}));
145             }, 0.0f);
146 
147     bound |= this->bindProperty<ScalarValue>(t["ry"], ascope,
148             [adapter](const ScalarValue& ry) {
149                 const auto& r = adapter->getRotation();
150                 adapter->setRotation(TransformAdapter3D::Vec3({r.fX, ry, r.fZ}));
151             }, 0.0f);
152 
153     bound |= this->bindProperty<ScalarValue>(t["rz"], ascope,
154             [adapter](const ScalarValue& rz) {
155                 const auto& r = adapter->getRotation();
156                 adapter->setRotation(TransformAdapter3D::Vec3({r.fX, r.fY, rz}));
157             }, 0.0f);
158 
159     // TODO: dispatch 3D transform properties
160 
161     return (bound)
162         ? sksg::Transform::MakeConcat(std::move(parent), std::move(matrix))
163         : parent;
164 }
165 
attachOpacity(const skjson::ObjectValue & jtransform,AnimatorScope * ascope,sk_sp<sksg::RenderNode> childNode) const166 sk_sp<sksg::RenderNode> AnimationBuilder::attachOpacity(const skjson::ObjectValue& jtransform,
167                                                         AnimatorScope* ascope,
168                                                         sk_sp<sksg::RenderNode> childNode) const {
169     if (!childNode)
170         return nullptr;
171 
172     auto opacityNode = sksg::OpacityEffect::Make(childNode);
173 
174     const auto bound = this->bindProperty<ScalarValue>(jtransform["o"], ascope,
175         [opacityNode](const ScalarValue& o) {
176             // BM opacity is [0..100]
177             opacityNode->setOpacity(o * 0.01f);
178         }, 100.0f);
179     const auto dispatched = this->dispatchOpacityProperty(opacityNode);
180 
181     // We can ignore constant full opacity.
182     return (bound || dispatched) ? std::move(opacityNode) : childNode;
183 }
184 
attachPath(const skjson::Value & jpath,AnimatorScope * ascope) const185 sk_sp<sksg::Path> AnimationBuilder::attachPath(const skjson::Value& jpath,
186                                                AnimatorScope* ascope) const {
187     auto path_node = sksg::Path::Make();
188     return this->bindProperty<ShapeValue>(jpath, ascope,
189         [path_node](const ShapeValue& p) {
190             // FillType is tracked in the SG node, not in keyframes -- make sure we preserve it.
191             auto path = ValueTraits<ShapeValue>::As<SkPath>(p);
192             path.setFillType(path_node->getFillType());
193             path_node->setPath(path);
194         })
195         ? path_node
196         : nullptr;
197 }
198 
attachColor(const skjson::ObjectValue & jcolor,AnimatorScope * ascope,const char prop_name[]) const199 sk_sp<sksg::Color> AnimationBuilder::attachColor(const skjson::ObjectValue& jcolor,
200                                                  AnimatorScope* ascope,
201                                                  const char prop_name[]) const {
202     auto color_node = sksg::Color::Make(SK_ColorBLACK);
203 
204     this->bindProperty<VectorValue>(jcolor[prop_name], ascope,
205         [color_node](const VectorValue& c) {
206             color_node->setColor(ValueTraits<VectorValue>::As<SkColor>(c));
207         });
208     this->dispatchColorProperty(color_node);
209 
210     return color_node;
211 }
212 
AnimationBuilder(sk_sp<ResourceProvider> rp,sk_sp<SkFontMgr> fontmgr,sk_sp<PropertyObserver> pobserver,sk_sp<Logger> logger,sk_sp<MarkerObserver> mobserver,Animation::Builder::Stats * stats,float duration,float framerate)213 AnimationBuilder::AnimationBuilder(sk_sp<ResourceProvider> rp, sk_sp<SkFontMgr> fontmgr,
214                                    sk_sp<PropertyObserver> pobserver, sk_sp<Logger> logger,
215                                    sk_sp<MarkerObserver> mobserver,
216                                    Animation::Builder::Stats* stats,
217                                    float duration, float framerate)
218     : fResourceProvider(std::move(rp))
219     , fLazyFontMgr(std::move(fontmgr))
220     , fPropertyObserver(std::move(pobserver))
221     , fLogger(std::move(logger))
222     , fMarkerObserver(std::move(mobserver))
223     , fStats(stats)
224     , fDuration(duration)
225     , fFrameRate(framerate) {}
226 
parse(const skjson::ObjectValue & jroot)227 std::unique_ptr<sksg::Scene> AnimationBuilder::parse(const skjson::ObjectValue& jroot) {
228     this->dispatchMarkers(jroot["markers"]);
229 
230     this->parseAssets(jroot["assets"]);
231     this->parseFonts(jroot["fonts"], jroot["chars"]);
232 
233     AnimatorScope animators;
234     auto root = this->attachComposition(jroot, &animators);
235 
236     fStats->fAnimatorCount = animators.size();
237 
238     return sksg::Scene::Make(std::move(root), std::move(animators));
239 }
240 
parseAssets(const skjson::ArrayValue * jassets)241 void AnimationBuilder::parseAssets(const skjson::ArrayValue* jassets) {
242     if (!jassets) {
243         return;
244     }
245 
246     for (const skjson::ObjectValue* asset : *jassets) {
247         if (asset) {
248             fAssets.set(ParseDefault<SkString>((*asset)["id"], SkString()), { asset, false });
249         }
250     }
251 }
252 
dispatchMarkers(const skjson::ArrayValue * jmarkers) const253 void AnimationBuilder::dispatchMarkers(const skjson::ArrayValue* jmarkers) const {
254     if (!fMarkerObserver || !jmarkers) {
255         return;
256     }
257 
258     // For frame-number -> t conversions.
259     const auto frameRatio = 1 / (fFrameRate * fDuration);
260 
261     for (const skjson::ObjectValue* m : *jmarkers) {
262         if (!m) continue;
263 
264         const skjson::StringValue* name = (*m)["cm"];
265         const auto time = ParseDefault((*m)["tm"], -1.0f),
266                duration = ParseDefault((*m)["dr"], -1.0f);
267 
268         if (name && time >= 0 && duration >= 0) {
269             fMarkerObserver->onMarker(
270                         name->begin(),
271                         // "tm" is in frames
272                         time * frameRatio,
273                         // ... as is "dr"
274                         (time + duration) * frameRatio
275             );
276         } else {
277             this->log(Logger::Level::kWarning, m, "Ignoring unexpected marker.");
278         }
279     }
280 }
281 
dispatchColorProperty(const sk_sp<sksg::Color> & c) const282 bool AnimationBuilder::dispatchColorProperty(const sk_sp<sksg::Color>& c) const {
283     bool dispatched = false;
284 
285     if (fPropertyObserver) {
286         fPropertyObserver->onColorProperty(fPropertyObserverContext,
287             [&]() {
288                 dispatched = true;
289                 return std::unique_ptr<ColorPropertyHandle>(new ColorPropertyHandle(c));
290             });
291     }
292 
293     return dispatched;
294 }
295 
dispatchOpacityProperty(const sk_sp<sksg::OpacityEffect> & o) const296 bool AnimationBuilder::dispatchOpacityProperty(const sk_sp<sksg::OpacityEffect>& o) const {
297     bool dispatched = false;
298 
299     if (fPropertyObserver) {
300         fPropertyObserver->onOpacityProperty(fPropertyObserverContext,
301             [&]() {
302                 dispatched = true;
303                 return std::unique_ptr<OpacityPropertyHandle>(new OpacityPropertyHandle(o));
304             });
305     }
306 
307     return dispatched;
308 }
309 
dispatchTransformProperty(const sk_sp<TransformAdapter2D> & t) const310 bool AnimationBuilder::dispatchTransformProperty(const sk_sp<TransformAdapter2D>& t) const {
311     bool dispatched = false;
312 
313     if (fPropertyObserver) {
314         fPropertyObserver->onTransformProperty(fPropertyObserverContext,
315             [&]() {
316                 dispatched = true;
317                 return std::unique_ptr<TransformPropertyHandle>(new TransformPropertyHandle(t));
318             });
319     }
320 
321     return dispatched;
322 }
323 
updateContext(PropertyObserver * observer,const skjson::ObjectValue & obj)324 void AnimationBuilder::AutoPropertyTracker::updateContext(PropertyObserver* observer,
325                                                           const skjson::ObjectValue& obj) {
326 
327     const skjson::StringValue* name = obj["nm"];
328 
329     fBuilder->fPropertyObserverContext = name ? name->begin() : nullptr;
330 }
331 
332 } // namespace internal
333 
load(const char[],const char[]) const334 sk_sp<SkData> ResourceProvider::load(const char[], const char[]) const {
335     return nullptr;
336 }
337 
loadImageAsset(const char path[],const char name[]) const338 sk_sp<ImageAsset> ResourceProvider::loadImageAsset(const char path[], const char name[]) const {
339     return nullptr;
340 }
341 
loadFont(const char[],const char[]) const342 sk_sp<SkData> ResourceProvider::loadFont(const char[], const char[]) const {
343     return nullptr;
344 }
345 
log(Level,const char[],const char *)346 void Logger::log(Level, const char[], const char*) {}
347 
348 Animation::Builder::Builder()  = default;
349 Animation::Builder::~Builder() = default;
350 
setResourceProvider(sk_sp<ResourceProvider> rp)351 Animation::Builder& Animation::Builder::setResourceProvider(sk_sp<ResourceProvider> rp) {
352     fResourceProvider = std::move(rp);
353     return *this;
354 }
355 
setFontManager(sk_sp<SkFontMgr> fmgr)356 Animation::Builder& Animation::Builder::setFontManager(sk_sp<SkFontMgr> fmgr) {
357     fFontMgr = std::move(fmgr);
358     return *this;
359 }
360 
setPropertyObserver(sk_sp<PropertyObserver> pobserver)361 Animation::Builder& Animation::Builder::setPropertyObserver(sk_sp<PropertyObserver> pobserver) {
362     fPropertyObserver = std::move(pobserver);
363     return *this;
364 }
365 
setLogger(sk_sp<Logger> logger)366 Animation::Builder& Animation::Builder::setLogger(sk_sp<Logger> logger) {
367     fLogger = std::move(logger);
368     return *this;
369 }
370 
setMarkerObserver(sk_sp<MarkerObserver> mobserver)371 Animation::Builder& Animation::Builder::setMarkerObserver(sk_sp<MarkerObserver> mobserver) {
372     fMarkerObserver = std::move(mobserver);
373     return *this;
374 }
375 
make(SkStream * stream)376 sk_sp<Animation> Animation::Builder::make(SkStream* stream) {
377     if (!stream->hasLength()) {
378         // TODO: handle explicit buffering?
379         if (fLogger) {
380             fLogger->log(Logger::Level::kError, "Cannot parse streaming content.\n");
381         }
382         return nullptr;
383     }
384 
385     auto data = SkData::MakeFromStream(stream, stream->getLength());
386     if (!data) {
387         if (fLogger) {
388             fLogger->log(Logger::Level::kError, "Failed to read the input stream.\n");
389         }
390         return nullptr;
391     }
392 
393     return this->make(static_cast<const char*>(data->data()), data->size());
394 }
395 
make(const char * data,size_t data_len)396 sk_sp<Animation> Animation::Builder::make(const char* data, size_t data_len) {
397     TRACE_EVENT0("skottie", TRACE_FUNC);
398 
399     // Sanitize factory args.
400     class NullResourceProvider final : public ResourceProvider {
401         sk_sp<SkData> load(const char[], const char[]) const override { return nullptr; }
402     };
403     auto resolvedProvider = fResourceProvider
404             ? fResourceProvider : sk_make_sp<NullResourceProvider>();
405 
406     memset(&fStats, 0, sizeof(struct Stats));
407 
408     fStats.fJsonSize = data_len;
409     const auto t0 = SkTime::GetMSecs();
410 
411     const skjson::DOM dom(data, data_len);
412     if (!dom.root().is<skjson::ObjectValue>()) {
413         // TODO: more error info.
414         if (fLogger) {
415             fLogger->log(Logger::Level::kError, "Failed to parse JSON input.\n");
416         }
417         return nullptr;
418     }
419     const auto& json = dom.root().as<skjson::ObjectValue>();
420 
421     const auto t1 = SkTime::GetMSecs();
422     fStats.fJsonParseTimeMS = t1 - t0;
423 
424     const auto version  = ParseDefault<SkString>(json["v"], SkString());
425     const auto size     = SkSize::Make(ParseDefault<float>(json["w"], 0.0f),
426                                        ParseDefault<float>(json["h"], 0.0f));
427     const auto fps      = ParseDefault<float>(json["fr"], -1.0f),
428                inPoint  = ParseDefault<float>(json["ip"], 0.0f),
429                outPoint = SkTMax(ParseDefault<float>(json["op"], SK_ScalarMax), inPoint),
430                duration = sk_ieee_float_divide(outPoint - inPoint, fps);
431 
432     if (size.isEmpty() || version.isEmpty() || fps <= 0 ||
433         !SkScalarIsFinite(inPoint) || !SkScalarIsFinite(outPoint) || !SkScalarIsFinite(duration)) {
434         if (fLogger) {
435             const auto msg = SkStringPrintf(
436                          "Invalid animation params (version: %s, size: [%f %f], frame rate: %f, "
437                          "in-point: %f, out-point: %f)\n",
438                          version.c_str(), size.width(), size.height(), fps, inPoint, outPoint);
439             fLogger->log(Logger::Level::kError, msg.c_str());
440         }
441         return nullptr;
442     }
443 
444     SkASSERT(resolvedProvider);
445     internal::AnimationBuilder builder(std::move(resolvedProvider), fFontMgr,
446                                        std::move(fPropertyObserver),
447                                        std::move(fLogger),
448                                        std::move(fMarkerObserver),
449                                        &fStats, duration, fps);
450     auto scene = builder.parse(json);
451 
452     const auto t2 = SkTime::GetMSecs();
453     fStats.fSceneParseTimeMS = t2 - t1;
454     fStats.fTotalLoadTimeMS  = t2 - t0;
455 
456     if (!scene && fLogger) {
457         fLogger->log(Logger::Level::kError, "Could not parse animation.\n");
458     }
459 
460     return sk_sp<Animation>(
461         new Animation(std::move(scene), std::move(version), size, inPoint, outPoint, duration));
462 }
463 
makeFromFile(const char path[])464 sk_sp<Animation> Animation::Builder::makeFromFile(const char path[]) {
465     const auto data = SkData::MakeFromFileName(path);
466 
467     return data ? this->make(static_cast<const char*>(data->data()), data->size())
468                 : nullptr;
469 }
470 
Animation(std::unique_ptr<sksg::Scene> scene,SkString version,const SkSize & size,SkScalar inPoint,SkScalar outPoint,SkScalar duration)471 Animation::Animation(std::unique_ptr<sksg::Scene> scene, SkString version, const SkSize& size,
472                      SkScalar inPoint, SkScalar outPoint, SkScalar duration)
473     : fScene(std::move(scene))
474     , fVersion(std::move(version))
475     , fSize(size)
476     , fInPoint(inPoint)
477     , fOutPoint(outPoint)
478     , fDuration(duration) {
479 
480     // In case the client calls render before the first tick.
481     this->seek(0);
482 }
483 
484 Animation::~Animation() = default;
485 
setShowInval(bool show)486 void Animation::setShowInval(bool show) {
487     if (fScene) {
488         fScene->setShowInval(show);
489     }
490 }
491 
render(SkCanvas * canvas,const SkRect * dstR) const492 void Animation::render(SkCanvas* canvas, const SkRect* dstR) const {
493     TRACE_EVENT0("skottie", TRACE_FUNC);
494 
495     if (!fScene)
496         return;
497 
498     SkAutoCanvasRestore restore(canvas, true);
499     const SkRect srcR = SkRect::MakeSize(this->size());
500     if (dstR) {
501         canvas->concat(SkMatrix::MakeRectToRect(srcR, *dstR, SkMatrix::kCenter_ScaleToFit));
502     }
503     canvas->clipRect(srcR);
504     fScene->render(canvas);
505 }
506 
seek(SkScalar t)507 void Animation::seek(SkScalar t) {
508     TRACE_EVENT0("skottie", TRACE_FUNC);
509 
510     if (!fScene)
511         return;
512 
513     fScene->animate(fInPoint + SkTPin(t, 0.0f, 1.0f) * (fOutPoint - fInPoint));
514 }
515 
Make(const char * data,size_t length)516 sk_sp<Animation> Animation::Make(const char* data, size_t length) {
517     return Builder().make(data, length);
518 }
519 
Make(SkStream * stream)520 sk_sp<Animation> Animation::Make(SkStream* stream) {
521     return Builder().make(stream);
522 }
523 
MakeFromFile(const char path[])524 sk_sp<Animation> Animation::MakeFromFile(const char path[]) {
525     return Builder().makeFromFile(path);
526 }
527 
528 } // namespace skottie
529