blob: 5a04e5a719983c25545fb4f7c4707aeb0f648a58 [file] [log] [blame]
Florin Malita094ccde2017-12-30 12:27:00 -05001/*
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
Florin Malita54f65c42018-01-16 17:04:30 -05008#include "Skottie.h"
Florin Malita094ccde2017-12-30 12:27:00 -05009
10#include "SkCanvas.h"
Florin Malitafc807c82018-01-25 22:35:09 -050011#include "SkJSONCPP.h"
Florin Malita54f65c42018-01-16 17:04:30 -050012#include "SkottieAnimator.h"
Florin Malitacf8ed522018-01-25 15:27:33 -050013#include "SkottieParser.h"
Florin Malita54f65c42018-01-16 17:04:30 -050014#include "SkottieProperties.h"
Florin Malita094ccde2017-12-30 12:27:00 -050015#include "SkData.h"
Florin Malita49328072018-01-08 12:51:12 -050016#include "SkImage.h"
Florin Malita094ccde2017-12-30 12:27:00 -050017#include "SkMakeUnique.h"
Florin Malita49328072018-01-08 12:51:12 -050018#include "SkOSPath.h"
Florin Malita094ccde2017-12-30 12:27:00 -050019#include "SkPaint.h"
Florin Malita0e66fba2018-01-09 17:10:18 -050020#include "SkParse.h"
Florin Malita094ccde2017-12-30 12:27:00 -050021#include "SkPoint.h"
Florin Malita38ea40e2018-01-29 16:31:14 -050022#include "SkSGClipEffect.h"
Florin Malita094ccde2017-12-30 12:27:00 -050023#include "SkSGColor.h"
24#include "SkSGDraw.h"
Florin Malita16d0ad02018-01-19 15:07:29 -050025#include "SkSGGeometryTransform.h"
Florin Malita6aaee592018-01-12 12:25:09 -050026#include "SkSGGradient.h"
Florin Malita094ccde2017-12-30 12:27:00 -050027#include "SkSGGroup.h"
Florin Malita49328072018-01-08 12:51:12 -050028#include "SkSGImage.h"
29#include "SkSGInvalidationController.h"
Florin Malita5f9102f2018-01-10 13:36:22 -050030#include "SkSGMaskEffect.h"
Florin Malitae6345d92018-01-03 23:37:54 -050031#include "SkSGMerge.h"
Florin Malitac0034172018-01-08 16:42:59 -050032#include "SkSGOpacityEffect.h"
Florin Malita094ccde2017-12-30 12:27:00 -050033#include "SkSGPath.h"
Florin Malita2e1d7e22018-01-02 10:40:00 -050034#include "SkSGRect.h"
Florin Malita35efaa82018-01-22 12:57:06 -050035#include "SkSGScene.h"
Florin Malita094ccde2017-12-30 12:27:00 -050036#include "SkSGTransform.h"
Florin Malita51b8c892018-01-07 08:54:24 -050037#include "SkSGTrimEffect.h"
Florin Malita094ccde2017-12-30 12:27:00 -050038#include "SkStream.h"
39#include "SkTArray.h"
40#include "SkTHash.h"
41
42#include <cmath>
Florin Malitae6345d92018-01-03 23:37:54 -050043#include <vector>
44
Florin Malita094ccde2017-12-30 12:27:00 -050045#include "stdlib.h"
46
Florin Malita54f65c42018-01-16 17:04:30 -050047namespace skottie {
Florin Malita094ccde2017-12-30 12:27:00 -050048
Florin Malitacf8ed522018-01-25 15:27:33 -050049#define LOG SkDebugf
50
Florin Malita094ccde2017-12-30 12:27:00 -050051namespace {
52
Florin Malitadd22cf962018-01-29 15:42:01 +000053using AssetMap = SkTHashMap<SkString, const Json::Value*>;
Florin Malita094ccde2017-12-30 12:27:00 -050054
55struct AttachContext {
Florin Malitacca86f32018-01-29 10:49:49 -050056 const ResourceProvider& fResources;
57 const AssetMap& fAssets;
Florin Malita1022f742018-02-23 11:10:22 -050058 const float fFrameRate;
Florin Malitacca86f32018-01-29 10:49:49 -050059 sksg::AnimatorList& fAnimators;
Florin Malita094ccde2017-12-30 12:27:00 -050060};
61
62bool LogFail(const Json::Value& json, const char* msg) {
63 const auto dump = json.toStyledString();
64 LOG("!! %s: %s", msg, dump.c_str());
65 return false;
66}
67
Florin Malita18eafd92018-01-04 21:11:55 -050068sk_sp<sksg::Matrix> AttachMatrix(const Json::Value& t, AttachContext* ctx,
69 sk_sp<sksg::Matrix> parentMatrix) {
70 if (!t.isObject())
71 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -050072
Florin Malita18eafd92018-01-04 21:11:55 -050073 auto matrix = sksg::Matrix::Make(SkMatrix::I(), std::move(parentMatrix));
74 auto composite = sk_make_sp<CompositeTransform>(matrix);
Florin Malitafc807c82018-01-25 22:35:09 -050075 auto anchor_attached = BindProperty<VectorValue>(t["a"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -050076 [composite](const VectorValue& a) {
77 composite->setAnchorPoint(ValueTraits<VectorValue>::As<SkPoint>(a));
Florin Malita094ccde2017-12-30 12:27:00 -050078 });
Florin Malitafc807c82018-01-25 22:35:09 -050079 auto position_attached = BindProperty<VectorValue>(t["p"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -050080 [composite](const VectorValue& p) {
81 composite->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
Florin Malita094ccde2017-12-30 12:27:00 -050082 });
Florin Malitafc807c82018-01-25 22:35:09 -050083 auto scale_attached = BindProperty<VectorValue>(t["s"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -050084 [composite](const VectorValue& s) {
85 composite->setScale(ValueTraits<VectorValue>::As<SkVector>(s));
Florin Malita094ccde2017-12-30 12:27:00 -050086 });
Florin Malita1eb98db2018-01-26 15:03:38 -050087
88 auto* jrotation = &t["r"];
89 if (jrotation->isNull()) {
90 // 3d rotations have separate rx,ry,rz components. While we don't fully support them,
91 // we can still make use of rz.
92 jrotation = &t["rz"];
93 }
94 auto rotation_attached = BindProperty<ScalarValue>(*jrotation, &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -050095 [composite](const ScalarValue& r) {
96 composite->setRotation(r);
Florin Malita094ccde2017-12-30 12:27:00 -050097 });
Florin Malitafc807c82018-01-25 22:35:09 -050098 auto skew_attached = BindProperty<ScalarValue>(t["sk"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -050099 [composite](const ScalarValue& sk) {
100 composite->setSkew(sk);
Florin Malita094ccde2017-12-30 12:27:00 -0500101 });
Florin Malitafc807c82018-01-25 22:35:09 -0500102 auto skewaxis_attached = BindProperty<ScalarValue>(t["sa"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -0500103 [composite](const ScalarValue& sa) {
104 composite->setSkewAxis(sa);
Florin Malita094ccde2017-12-30 12:27:00 -0500105 });
106
107 if (!anchor_attached &&
108 !position_attached &&
109 !scale_attached &&
110 !rotation_attached &&
111 !skew_attached &&
112 !skewaxis_attached) {
113 LogFail(t, "Could not parse transform");
Florin Malita18eafd92018-01-04 21:11:55 -0500114 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500115 }
116
Florin Malita18eafd92018-01-04 21:11:55 -0500117 return matrix;
Florin Malita094ccde2017-12-30 12:27:00 -0500118}
119
Florin Malitac0034172018-01-08 16:42:59 -0500120sk_sp<sksg::RenderNode> AttachOpacity(const Json::Value& jtransform, AttachContext* ctx,
121 sk_sp<sksg::RenderNode> childNode) {
122 if (!jtransform.isObject() || !childNode)
123 return childNode;
124
125 // This is more peeky than other attachers, because we want to avoid redundant opacity
126 // nodes for the extremely common case of static opaciy == 100.
127 const auto& opacity = jtransform["o"];
128 if (opacity.isObject() &&
Florin Malitacf8ed522018-01-25 15:27:33 -0500129 !ParseDefault(opacity["a"], true) &&
130 ParseDefault(opacity["k"], -1) == 100) {
Florin Malitac0034172018-01-08 16:42:59 -0500131 // Ignoring static full opacity.
132 return childNode;
133 }
134
135 auto opacityNode = sksg::OpacityEffect::Make(childNode);
Florin Malitafc807c82018-01-25 22:35:09 -0500136 BindProperty<ScalarValue>(opacity, &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -0500137 [opacityNode](const ScalarValue& o) {
Florin Malitac0034172018-01-08 16:42:59 -0500138 // BM opacity is [0..100]
Florin Malita2518a0a2018-01-24 18:29:00 -0500139 opacityNode->setOpacity(o * 0.01f);
Florin Malitac0034172018-01-08 16:42:59 -0500140 });
141
142 return opacityNode;
143}
144
Florin Malita094ccde2017-12-30 12:27:00 -0500145sk_sp<sksg::RenderNode> AttachComposition(const Json::Value&, AttachContext* ctx);
146
Florin Malita25366fa2018-01-23 13:37:59 -0500147sk_sp<sksg::Path> AttachPath(const Json::Value& jpath, AttachContext* ctx) {
148 auto path_node = sksg::Path::Make();
Florin Malitafc807c82018-01-25 22:35:09 -0500149 return BindProperty<ShapeValue>(jpath, &ctx->fAnimators,
150 [path_node](const ShapeValue& p) { path_node->setPath(p); })
Florin Malita25366fa2018-01-23 13:37:59 -0500151 ? path_node
152 : nullptr;
153}
154
Florin Malita094ccde2017-12-30 12:27:00 -0500155sk_sp<sksg::GeometryNode> AttachPathGeometry(const Json::Value& jpath, AttachContext* ctx) {
156 SkASSERT(jpath.isObject());
157
Florin Malita25366fa2018-01-23 13:37:59 -0500158 return AttachPath(jpath["ks"], ctx);
Florin Malita094ccde2017-12-30 12:27:00 -0500159}
160
Florin Malita2e1d7e22018-01-02 10:40:00 -0500161sk_sp<sksg::GeometryNode> AttachRRectGeometry(const Json::Value& jrect, AttachContext* ctx) {
162 SkASSERT(jrect.isObject());
163
164 auto rect_node = sksg::RRect::Make();
165 auto composite = sk_make_sp<CompositeRRect>(rect_node);
166
Florin Malitafc807c82018-01-25 22:35:09 -0500167 auto p_attached = BindProperty<VectorValue>(jrect["p"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -0500168 [composite](const VectorValue& p) {
169 composite->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
Florin Malitaf9590922018-01-09 11:56:09 -0500170 });
Florin Malitafc807c82018-01-25 22:35:09 -0500171 auto s_attached = BindProperty<VectorValue>(jrect["s"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -0500172 [composite](const VectorValue& s) {
173 composite->setSize(ValueTraits<VectorValue>::As<SkSize>(s));
Florin Malitaf9590922018-01-09 11:56:09 -0500174 });
Florin Malitafc807c82018-01-25 22:35:09 -0500175 auto r_attached = BindProperty<ScalarValue>(jrect["r"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -0500176 [composite](const ScalarValue& r) {
177 composite->setRadius(SkSize::Make(r, r));
Florin Malitaf9590922018-01-09 11:56:09 -0500178 });
Florin Malita2e1d7e22018-01-02 10:40:00 -0500179
180 if (!p_attached && !s_attached && !r_attached) {
181 return nullptr;
182 }
183
Florin Malitafbc13f12018-01-04 10:26:35 -0500184 return rect_node;
185}
186
187sk_sp<sksg::GeometryNode> AttachEllipseGeometry(const Json::Value& jellipse, AttachContext* ctx) {
188 SkASSERT(jellipse.isObject());
189
190 auto rect_node = sksg::RRect::Make();
191 auto composite = sk_make_sp<CompositeRRect>(rect_node);
192
Florin Malitafc807c82018-01-25 22:35:09 -0500193 auto p_attached = BindProperty<VectorValue>(jellipse["p"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -0500194 [composite](const VectorValue& p) {
195 composite->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
Florin Malitaf9590922018-01-09 11:56:09 -0500196 });
Florin Malitafc807c82018-01-25 22:35:09 -0500197 auto s_attached = BindProperty<VectorValue>(jellipse["s"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -0500198 [composite](const VectorValue& s) {
Florin Malitaf9590922018-01-09 11:56:09 -0500199 const auto sz = ValueTraits<VectorValue>::As<SkSize>(s);
Florin Malita2518a0a2018-01-24 18:29:00 -0500200 composite->setSize(sz);
201 composite->setRadius(SkSize::Make(sz.width() / 2, sz.height() / 2));
Florin Malitaf9590922018-01-09 11:56:09 -0500202 });
Florin Malitafbc13f12018-01-04 10:26:35 -0500203
204 if (!p_attached && !s_attached) {
205 return nullptr;
206 }
207
Florin Malita2e1d7e22018-01-02 10:40:00 -0500208 return rect_node;
209}
210
Florin Malita02a32b02018-01-04 11:27:09 -0500211sk_sp<sksg::GeometryNode> AttachPolystarGeometry(const Json::Value& jstar, AttachContext* ctx) {
212 SkASSERT(jstar.isObject());
213
214 static constexpr CompositePolyStar::Type gTypes[] = {
215 CompositePolyStar::Type::kStar, // "sy": 1
216 CompositePolyStar::Type::kPoly, // "sy": 2
217 };
218
Florin Malitacf8ed522018-01-25 15:27:33 -0500219 const auto type = ParseDefault(jstar["sy"], 0) - 1;
Florin Malita02a32b02018-01-04 11:27:09 -0500220 if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gTypes))) {
221 LogFail(jstar, "Unknown polystar type");
222 return nullptr;
223 }
224
225 auto path_node = sksg::Path::Make();
226 auto composite = sk_make_sp<CompositePolyStar>(path_node, gTypes[type]);
227
Florin Malitafc807c82018-01-25 22:35:09 -0500228 BindProperty<VectorValue>(jstar["p"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -0500229 [composite](const VectorValue& p) {
230 composite->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
Florin Malitaf9590922018-01-09 11:56:09 -0500231 });
Florin Malitafc807c82018-01-25 22:35:09 -0500232 BindProperty<ScalarValue>(jstar["pt"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -0500233 [composite](const ScalarValue& pt) {
234 composite->setPointCount(pt);
Florin Malitaf9590922018-01-09 11:56:09 -0500235 });
Florin Malitafc807c82018-01-25 22:35:09 -0500236 BindProperty<ScalarValue>(jstar["ir"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -0500237 [composite](const ScalarValue& ir) {
238 composite->setInnerRadius(ir);
Florin Malitaf9590922018-01-09 11:56:09 -0500239 });
Florin Malitafc807c82018-01-25 22:35:09 -0500240 BindProperty<ScalarValue>(jstar["or"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -0500241 [composite](const ScalarValue& otr) {
242 composite->setOuterRadius(otr);
Florin Malita9661b982018-01-06 14:25:49 -0500243 });
Florin Malitafc807c82018-01-25 22:35:09 -0500244 BindProperty<ScalarValue>(jstar["is"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -0500245 [composite](const ScalarValue& is) {
246 composite->setInnerRoundness(is);
Florin Malita9661b982018-01-06 14:25:49 -0500247 });
Florin Malitafc807c82018-01-25 22:35:09 -0500248 BindProperty<ScalarValue>(jstar["os"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -0500249 [composite](const ScalarValue& os) {
250 composite->setOuterRoundness(os);
Florin Malita9661b982018-01-06 14:25:49 -0500251 });
Florin Malitafc807c82018-01-25 22:35:09 -0500252 BindProperty<ScalarValue>(jstar["r"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -0500253 [composite](const ScalarValue& r) {
254 composite->setRotation(r);
Florin Malitaf9590922018-01-09 11:56:09 -0500255 });
Florin Malita02a32b02018-01-04 11:27:09 -0500256
257 return path_node;
258}
259
Florin Malita6aaee592018-01-12 12:25:09 -0500260sk_sp<sksg::Color> AttachColor(const Json::Value& obj, AttachContext* ctx) {
Florin Malita094ccde2017-12-30 12:27:00 -0500261 SkASSERT(obj.isObject());
262
263 auto color_node = sksg::Color::Make(SK_ColorBLACK);
Florin Malitafc807c82018-01-25 22:35:09 -0500264 auto color_attached = BindProperty<VectorValue>(obj["c"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -0500265 [color_node](const VectorValue& c) {
266 color_node->setColor(ValueTraits<VectorValue>::As<SkColor>(c));
Florin Malitaf9590922018-01-09 11:56:09 -0500267 });
Florin Malita094ccde2017-12-30 12:27:00 -0500268
Florin Malita1586d852018-01-12 14:27:39 -0500269 return color_attached ? color_node : nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500270}
271
Florin Malita6aaee592018-01-12 12:25:09 -0500272sk_sp<sksg::Gradient> AttachGradient(const Json::Value& obj, AttachContext* ctx) {
273 SkASSERT(obj.isObject());
Florin Malita094ccde2017-12-30 12:27:00 -0500274
Florin Malita6aaee592018-01-12 12:25:09 -0500275 const auto& stops = obj["g"];
276 if (!stops.isObject())
Florin Malita094ccde2017-12-30 12:27:00 -0500277 return nullptr;
278
Florin Malitacf8ed522018-01-25 15:27:33 -0500279 const auto stopCount = ParseDefault(stops["p"], -1);
Florin Malita6aaee592018-01-12 12:25:09 -0500280 if (stopCount < 0)
281 return nullptr;
282
283 sk_sp<sksg::Gradient> gradient_node;
284 sk_sp<CompositeGradient> composite;
285
Florin Malitacf8ed522018-01-25 15:27:33 -0500286 if (ParseDefault(obj["t"], 1) == 1) {
Florin Malita6aaee592018-01-12 12:25:09 -0500287 auto linear_node = sksg::LinearGradient::Make();
288 composite = sk_make_sp<CompositeLinearGradient>(linear_node, stopCount);
289 gradient_node = std::move(linear_node);
290 } else {
291 auto radial_node = sksg::RadialGradient::Make();
292 composite = sk_make_sp<CompositeRadialGradient>(radial_node, stopCount);
293
294 // TODO: highlight, angle
295 gradient_node = std::move(radial_node);
296 }
297
Florin Malitafc807c82018-01-25 22:35:09 -0500298 BindProperty<VectorValue>(stops["k"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -0500299 [composite](const VectorValue& stops) {
300 composite->setColorStops(stops);
Florin Malita6aaee592018-01-12 12:25:09 -0500301 });
Florin Malitafc807c82018-01-25 22:35:09 -0500302 BindProperty<VectorValue>(obj["s"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -0500303 [composite](const VectorValue& s) {
304 composite->setStartPoint(ValueTraits<VectorValue>::As<SkPoint>(s));
Florin Malita6aaee592018-01-12 12:25:09 -0500305 });
Florin Malitafc807c82018-01-25 22:35:09 -0500306 BindProperty<VectorValue>(obj["e"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -0500307 [composite](const VectorValue& e) {
308 composite->setEndPoint(ValueTraits<VectorValue>::As<SkPoint>(e));
Florin Malita6aaee592018-01-12 12:25:09 -0500309 });
310
311 return gradient_node;
312}
313
Florin Malita1586d852018-01-12 14:27:39 -0500314sk_sp<sksg::PaintNode> AttachPaint(const Json::Value& jpaint, AttachContext* ctx,
Florin Malita6aaee592018-01-12 12:25:09 -0500315 sk_sp<sksg::PaintNode> paint_node) {
316 if (paint_node) {
317 paint_node->setAntiAlias(true);
318
Florin Malitafc807c82018-01-25 22:35:09 -0500319 BindProperty<ScalarValue>(jpaint["o"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -0500320 [paint_node](const ScalarValue& o) {
Florin Malita1586d852018-01-12 14:27:39 -0500321 // BM opacity is [0..100]
Florin Malita2518a0a2018-01-24 18:29:00 -0500322 paint_node->setOpacity(o * 0.01f);
Florin Malita1586d852018-01-12 14:27:39 -0500323 });
Florin Malita6aaee592018-01-12 12:25:09 -0500324 }
325
326 return paint_node;
327}
328
329sk_sp<sksg::PaintNode> AttachStroke(const Json::Value& jstroke, AttachContext* ctx,
330 sk_sp<sksg::PaintNode> stroke_node) {
331 SkASSERT(jstroke.isObject());
332
333 if (!stroke_node)
334 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500335
336 stroke_node->setStyle(SkPaint::kStroke_Style);
337
Florin Malitafc807c82018-01-25 22:35:09 -0500338 auto width_attached = BindProperty<ScalarValue>(jstroke["w"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -0500339 [stroke_node](const ScalarValue& w) {
340 stroke_node->setStrokeWidth(w);
Florin Malitaf9590922018-01-09 11:56:09 -0500341 });
Florin Malita094ccde2017-12-30 12:27:00 -0500342 if (!width_attached)
343 return nullptr;
344
Florin Malitacf8ed522018-01-25 15:27:33 -0500345 stroke_node->setStrokeMiter(ParseDefault(jstroke["ml"], 4.0f));
Florin Malita094ccde2017-12-30 12:27:00 -0500346
347 static constexpr SkPaint::Join gJoins[] = {
348 SkPaint::kMiter_Join,
349 SkPaint::kRound_Join,
350 SkPaint::kBevel_Join,
351 };
Florin Malitacf8ed522018-01-25 15:27:33 -0500352 stroke_node->setStrokeJoin(gJoins[SkTPin<int>(ParseDefault(jstroke["lj"], 1) - 1,
Florin Malita094ccde2017-12-30 12:27:00 -0500353 0, SK_ARRAY_COUNT(gJoins) - 1)]);
354
355 static constexpr SkPaint::Cap gCaps[] = {
356 SkPaint::kButt_Cap,
357 SkPaint::kRound_Cap,
358 SkPaint::kSquare_Cap,
359 };
Florin Malitacf8ed522018-01-25 15:27:33 -0500360 stroke_node->setStrokeCap(gCaps[SkTPin<int>(ParseDefault(jstroke["lc"], 1) - 1,
Florin Malita094ccde2017-12-30 12:27:00 -0500361 0, SK_ARRAY_COUNT(gCaps) - 1)]);
362
363 return stroke_node;
364}
365
Florin Malita6aaee592018-01-12 12:25:09 -0500366sk_sp<sksg::PaintNode> AttachColorFill(const Json::Value& jfill, AttachContext* ctx) {
367 SkASSERT(jfill.isObject());
368
369 return AttachPaint(jfill, ctx, AttachColor(jfill, ctx));
370}
371
372sk_sp<sksg::PaintNode> AttachGradientFill(const Json::Value& jfill, AttachContext* ctx) {
373 SkASSERT(jfill.isObject());
374
375 return AttachPaint(jfill, ctx, AttachGradient(jfill, ctx));
376}
377
378sk_sp<sksg::PaintNode> AttachColorStroke(const Json::Value& jstroke, AttachContext* ctx) {
379 SkASSERT(jstroke.isObject());
380
381 return AttachStroke(jstroke, ctx, AttachPaint(jstroke, ctx, AttachColor(jstroke, ctx)));
382}
383
384sk_sp<sksg::PaintNode> AttachGradientStroke(const Json::Value& jstroke, AttachContext* ctx) {
385 SkASSERT(jstroke.isObject());
386
387 return AttachStroke(jstroke, ctx, AttachPaint(jstroke, ctx, AttachGradient(jstroke, ctx)));
388}
389
Florin Malitae6345d92018-01-03 23:37:54 -0500390std::vector<sk_sp<sksg::GeometryNode>> AttachMergeGeometryEffect(
391 const Json::Value& jmerge, AttachContext* ctx, std::vector<sk_sp<sksg::GeometryNode>>&& geos) {
392 std::vector<sk_sp<sksg::GeometryNode>> merged;
393
394 static constexpr sksg::Merge::Mode gModes[] = {
395 sksg::Merge::Mode::kMerge, // "mm": 1
396 sksg::Merge::Mode::kUnion, // "mm": 2
397 sksg::Merge::Mode::kDifference, // "mm": 3
398 sksg::Merge::Mode::kIntersect, // "mm": 4
399 sksg::Merge::Mode::kXOR , // "mm": 5
400 };
401
Florin Malitacf8ed522018-01-25 15:27:33 -0500402 const auto mode = gModes[SkTPin<int>(ParseDefault(jmerge["mm"], 1) - 1,
Florin Malita51b8c892018-01-07 08:54:24 -0500403 0, SK_ARRAY_COUNT(gModes) - 1)];
Florin Malitae6345d92018-01-03 23:37:54 -0500404 merged.push_back(sksg::Merge::Make(std::move(geos), mode));
405
Florin Malitae6345d92018-01-03 23:37:54 -0500406 return merged;
407}
408
Florin Malita51b8c892018-01-07 08:54:24 -0500409std::vector<sk_sp<sksg::GeometryNode>> AttachTrimGeometryEffect(
410 const Json::Value& jtrim, AttachContext* ctx, std::vector<sk_sp<sksg::GeometryNode>>&& geos) {
411
412 enum class Mode {
413 kMerged, // "m": 1
414 kSeparate, // "m": 2
415 } gModes[] = { Mode::kMerged, Mode::kSeparate };
416
Florin Malitacf8ed522018-01-25 15:27:33 -0500417 const auto mode = gModes[SkTPin<int>(ParseDefault(jtrim["m"], 1) - 1,
Florin Malita51b8c892018-01-07 08:54:24 -0500418 0, SK_ARRAY_COUNT(gModes) - 1)];
419
420 std::vector<sk_sp<sksg::GeometryNode>> inputs;
421 if (mode == Mode::kMerged) {
422 inputs.push_back(sksg::Merge::Make(std::move(geos), sksg::Merge::Mode::kMerge));
423 } else {
424 inputs = std::move(geos);
425 }
426
427 std::vector<sk_sp<sksg::GeometryNode>> trimmed;
428 trimmed.reserve(inputs.size());
429 for (const auto& i : inputs) {
Florin Malita69526b02018-03-22 12:20:02 -0400430 const auto trimEffect = sksg::TrimEffect::Make(i);
431 trimmed.push_back(trimEffect);
432
433 const auto trimComposite = sk_make_sp<CompositeTrimEffect>(std::move(trimEffect));
Florin Malitafc807c82018-01-25 22:35:09 -0500434 BindProperty<ScalarValue>(jtrim["s"], &ctx->fAnimators,
Florin Malita69526b02018-03-22 12:20:02 -0400435 [trimComposite](const ScalarValue& s) {
436 trimComposite->setStart(s);
Florin Malita51b8c892018-01-07 08:54:24 -0500437 });
Florin Malitafc807c82018-01-25 22:35:09 -0500438 BindProperty<ScalarValue>(jtrim["e"], &ctx->fAnimators,
Florin Malita69526b02018-03-22 12:20:02 -0400439 [trimComposite](const ScalarValue& e) {
440 trimComposite->setEnd(e);
Florin Malita51b8c892018-01-07 08:54:24 -0500441 });
Florin Malitafc807c82018-01-25 22:35:09 -0500442 BindProperty<ScalarValue>(jtrim["o"], &ctx->fAnimators,
Florin Malita69526b02018-03-22 12:20:02 -0400443 [trimComposite](const ScalarValue& o) {
444 trimComposite->setOffset(o);
Florin Malita51b8c892018-01-07 08:54:24 -0500445 });
446 }
447
448 return trimmed;
449}
450
Florin Malita094ccde2017-12-30 12:27:00 -0500451using GeometryAttacherT = sk_sp<sksg::GeometryNode> (*)(const Json::Value&, AttachContext*);
452static constexpr GeometryAttacherT gGeometryAttachers[] = {
453 AttachPathGeometry,
Florin Malita2e1d7e22018-01-02 10:40:00 -0500454 AttachRRectGeometry,
Florin Malitafbc13f12018-01-04 10:26:35 -0500455 AttachEllipseGeometry,
Florin Malita02a32b02018-01-04 11:27:09 -0500456 AttachPolystarGeometry,
Florin Malita094ccde2017-12-30 12:27:00 -0500457};
458
459using PaintAttacherT = sk_sp<sksg::PaintNode> (*)(const Json::Value&, AttachContext*);
460static constexpr PaintAttacherT gPaintAttachers[] = {
Florin Malita6aaee592018-01-12 12:25:09 -0500461 AttachColorFill,
462 AttachColorStroke,
463 AttachGradientFill,
464 AttachGradientStroke,
Florin Malita094ccde2017-12-30 12:27:00 -0500465};
466
Florin Malitae6345d92018-01-03 23:37:54 -0500467using GeometryEffectAttacherT =
468 std::vector<sk_sp<sksg::GeometryNode>> (*)(const Json::Value&,
469 AttachContext*,
470 std::vector<sk_sp<sksg::GeometryNode>>&&);
471static constexpr GeometryEffectAttacherT gGeometryEffectAttachers[] = {
472 AttachMergeGeometryEffect,
Florin Malita51b8c892018-01-07 08:54:24 -0500473 AttachTrimGeometryEffect,
Florin Malitae6345d92018-01-03 23:37:54 -0500474};
475
Florin Malita094ccde2017-12-30 12:27:00 -0500476enum class ShapeType {
477 kGeometry,
Florin Malitae6345d92018-01-03 23:37:54 -0500478 kGeometryEffect,
Florin Malita094ccde2017-12-30 12:27:00 -0500479 kPaint,
480 kGroup,
Florin Malitadacc02b2017-12-31 09:12:31 -0500481 kTransform,
Florin Malita094ccde2017-12-30 12:27:00 -0500482};
483
484struct ShapeInfo {
485 const char* fTypeString;
486 ShapeType fShapeType;
487 uint32_t fAttacherIndex; // index into respective attacher tables
488};
489
490const ShapeInfo* FindShapeInfo(const Json::Value& shape) {
491 static constexpr ShapeInfo gShapeInfo[] = {
Florin Malitafbc13f12018-01-04 10:26:35 -0500492 { "el", ShapeType::kGeometry , 2 }, // ellipse -> AttachEllipseGeometry
Florin Malita6aaee592018-01-12 12:25:09 -0500493 { "fl", ShapeType::kPaint , 0 }, // fill -> AttachColorFill
494 { "gf", ShapeType::kPaint , 2 }, // gfill -> AttachGradientFill
Florin Malita16d0ad02018-01-19 15:07:29 -0500495 { "gr", ShapeType::kGroup , 0 }, // group -> Inline handler
Florin Malita6aaee592018-01-12 12:25:09 -0500496 { "gs", ShapeType::kPaint , 3 }, // gstroke -> AttachGradientStroke
Florin Malitae6345d92018-01-03 23:37:54 -0500497 { "mm", ShapeType::kGeometryEffect, 0 }, // merge -> AttachMergeGeometryEffect
Florin Malita02a32b02018-01-04 11:27:09 -0500498 { "rc", ShapeType::kGeometry , 1 }, // rrect -> AttachRRectGeometry
Florin Malitae6345d92018-01-03 23:37:54 -0500499 { "sh", ShapeType::kGeometry , 0 }, // shape -> AttachPathGeometry
Florin Malita02a32b02018-01-04 11:27:09 -0500500 { "sr", ShapeType::kGeometry , 3 }, // polystar -> AttachPolyStarGeometry
Florin Malita6aaee592018-01-12 12:25:09 -0500501 { "st", ShapeType::kPaint , 1 }, // stroke -> AttachColorStroke
Florin Malita51b8c892018-01-07 08:54:24 -0500502 { "tm", ShapeType::kGeometryEffect, 1 }, // trim -> AttachTrimGeometryEffect
Florin Malita16d0ad02018-01-19 15:07:29 -0500503 { "tr", ShapeType::kTransform , 0 }, // transform -> Inline handler
Florin Malita094ccde2017-12-30 12:27:00 -0500504 };
505
506 if (!shape.isObject())
507 return nullptr;
508
509 const auto& type = shape["ty"];
510 if (!type.isString())
511 return nullptr;
512
513 const auto* info = bsearch(type.asCString(),
514 gShapeInfo,
515 SK_ARRAY_COUNT(gShapeInfo),
516 sizeof(ShapeInfo),
517 [](const void* key, const void* info) {
518 return strcmp(static_cast<const char*>(key),
519 static_cast<const ShapeInfo*>(info)->fTypeString);
520 });
521
522 return static_cast<const ShapeInfo*>(info);
523}
524
Florin Malita16d0ad02018-01-19 15:07:29 -0500525struct GeometryEffectRec {
526 const Json::Value& fJson;
527 GeometryEffectAttacherT fAttach;
528};
529
Florin Malitaca4439f2018-01-23 10:31:59 -0500530struct AttachShapeContext {
531 AttachShapeContext(AttachContext* ctx,
532 std::vector<sk_sp<sksg::GeometryNode>>* geos,
533 std::vector<GeometryEffectRec>* effects,
534 size_t committedAnimators)
535 : fCtx(ctx)
536 , fGeometryStack(geos)
537 , fGeometryEffectStack(effects)
538 , fCommittedAnimators(committedAnimators) {}
539
540 AttachContext* fCtx;
541 std::vector<sk_sp<sksg::GeometryNode>>* fGeometryStack;
542 std::vector<GeometryEffectRec>* fGeometryEffectStack;
543 size_t fCommittedAnimators;
544};
545
546sk_sp<sksg::RenderNode> AttachShape(const Json::Value& jshape, AttachShapeContext* shapeCtx) {
Florin Malita16d0ad02018-01-19 15:07:29 -0500547 if (!jshape.isArray())
Florin Malita094ccde2017-12-30 12:27:00 -0500548 return nullptr;
549
Florin Malitaca4439f2018-01-23 10:31:59 -0500550 SkDEBUGCODE(const auto initialGeometryEffects = shapeCtx->fGeometryEffectStack->size();)
Florin Malita094ccde2017-12-30 12:27:00 -0500551
Florin Malita16d0ad02018-01-19 15:07:29 -0500552 sk_sp<sksg::Group> shape_group = sksg::Group::Make();
553 sk_sp<sksg::RenderNode> shape_wrapper = shape_group;
554 sk_sp<sksg::Matrix> shape_matrix;
Florin Malita094ccde2017-12-30 12:27:00 -0500555
Florin Malita16d0ad02018-01-19 15:07:29 -0500556 struct ShapeRec {
557 const Json::Value& fJson;
558 const ShapeInfo& fInfo;
559 };
560
561 // First pass (bottom->top):
562 //
563 // * pick up the group transform and opacity
564 // * push local geometry effects onto the stack
565 // * store recs for next pass
566 //
567 std::vector<ShapeRec> recs;
568 for (Json::ArrayIndex i = 0; i < jshape.size(); ++i) {
569 const auto& s = jshape[jshape.size() - 1 - i];
Florin Malita094ccde2017-12-30 12:27:00 -0500570 const auto* info = FindShapeInfo(s);
571 if (!info) {
572 LogFail(s.isObject() ? s["ty"] : s, "Unknown shape");
573 continue;
574 }
575
Florin Malita16d0ad02018-01-19 15:07:29 -0500576 recs.push_back({ s, *info });
577
Florin Malita094ccde2017-12-30 12:27:00 -0500578 switch (info->fShapeType) {
Florin Malita16d0ad02018-01-19 15:07:29 -0500579 case ShapeType::kTransform:
Florin Malitaca4439f2018-01-23 10:31:59 -0500580 if ((shape_matrix = AttachMatrix(s, shapeCtx->fCtx, nullptr))) {
Florin Malita16d0ad02018-01-19 15:07:29 -0500581 shape_wrapper = sksg::Transform::Make(std::move(shape_wrapper), shape_matrix);
582 }
Florin Malitaca4439f2018-01-23 10:31:59 -0500583 shape_wrapper = AttachOpacity(s, shapeCtx->fCtx, std::move(shape_wrapper));
Florin Malita16d0ad02018-01-19 15:07:29 -0500584 break;
585 case ShapeType::kGeometryEffect:
586 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGeometryEffectAttachers));
Florin Malitaca4439f2018-01-23 10:31:59 -0500587 shapeCtx->fGeometryEffectStack->push_back(
Florin Malita16d0ad02018-01-19 15:07:29 -0500588 { s, gGeometryEffectAttachers[info->fAttacherIndex] });
589 break;
590 default:
591 break;
592 }
593 }
594
595 // Second pass (top -> bottom, after 2x reverse):
596 //
597 // * track local geometry
598 // * emit local paints
599 //
600 std::vector<sk_sp<sksg::GeometryNode>> geos;
601 std::vector<sk_sp<sksg::RenderNode >> draws;
602 for (auto rec = recs.rbegin(); rec != recs.rend(); ++rec) {
603 switch (rec->fInfo.fShapeType) {
Florin Malita094ccde2017-12-30 12:27:00 -0500604 case ShapeType::kGeometry: {
Florin Malita16d0ad02018-01-19 15:07:29 -0500605 SkASSERT(rec->fInfo.fAttacherIndex < SK_ARRAY_COUNT(gGeometryAttachers));
Florin Malitaca4439f2018-01-23 10:31:59 -0500606 if (auto geo = gGeometryAttachers[rec->fInfo.fAttacherIndex](rec->fJson,
607 shapeCtx->fCtx)) {
Florin Malita094ccde2017-12-30 12:27:00 -0500608 geos.push_back(std::move(geo));
609 }
610 } break;
Florin Malitae6345d92018-01-03 23:37:54 -0500611 case ShapeType::kGeometryEffect: {
Florin Malita16d0ad02018-01-19 15:07:29 -0500612 // Apply the current effect and pop from the stack.
613 SkASSERT(rec->fInfo.fAttacherIndex < SK_ARRAY_COUNT(gGeometryEffectAttachers));
Florin Malitaee6de4b2018-01-21 11:13:17 -0500614 if (!geos.empty()) {
615 geos = gGeometryEffectAttachers[rec->fInfo.fAttacherIndex](rec->fJson,
Florin Malitaca4439f2018-01-23 10:31:59 -0500616 shapeCtx->fCtx,
Florin Malitaee6de4b2018-01-21 11:13:17 -0500617 std::move(geos));
618 }
Florin Malita16d0ad02018-01-19 15:07:29 -0500619
Florin Malitaca4439f2018-01-23 10:31:59 -0500620 SkASSERT(shapeCtx->fGeometryEffectStack->back().fJson == rec->fJson);
621 SkASSERT(shapeCtx->fGeometryEffectStack->back().fAttach ==
Florin Malita16d0ad02018-01-19 15:07:29 -0500622 gGeometryEffectAttachers[rec->fInfo.fAttacherIndex]);
Florin Malitaca4439f2018-01-23 10:31:59 -0500623 shapeCtx->fGeometryEffectStack->pop_back();
Florin Malita094ccde2017-12-30 12:27:00 -0500624 } break;
625 case ShapeType::kGroup: {
Florin Malitaca4439f2018-01-23 10:31:59 -0500626 AttachShapeContext groupShapeCtx(shapeCtx->fCtx,
627 &geos,
628 shapeCtx->fGeometryEffectStack,
629 shapeCtx->fCommittedAnimators);
630 if (auto subgroup = AttachShape(rec->fJson["it"], &groupShapeCtx)) {
Florin Malita16d0ad02018-01-19 15:07:29 -0500631 draws.push_back(std::move(subgroup));
Florin Malitaca4439f2018-01-23 10:31:59 -0500632 SkASSERT(groupShapeCtx.fCommittedAnimators >= shapeCtx->fCommittedAnimators);
633 shapeCtx->fCommittedAnimators = groupShapeCtx.fCommittedAnimators;
Florin Malita094ccde2017-12-30 12:27:00 -0500634 }
635 } break;
Florin Malita16d0ad02018-01-19 15:07:29 -0500636 case ShapeType::kPaint: {
637 SkASSERT(rec->fInfo.fAttacherIndex < SK_ARRAY_COUNT(gPaintAttachers));
Florin Malitaca4439f2018-01-23 10:31:59 -0500638 auto paint = gPaintAttachers[rec->fInfo.fAttacherIndex](rec->fJson, shapeCtx->fCtx);
Florin Malita16d0ad02018-01-19 15:07:29 -0500639 if (!paint || geos.empty())
640 break;
641
642 auto drawGeos = geos;
643
644 // Apply all pending effects from the stack.
Florin Malitaca4439f2018-01-23 10:31:59 -0500645 for (auto it = shapeCtx->fGeometryEffectStack->rbegin();
646 it != shapeCtx->fGeometryEffectStack->rend(); ++it) {
647 drawGeos = it->fAttach(it->fJson, shapeCtx->fCtx, std::move(drawGeos));
Florin Malita18eafd92018-01-04 21:11:55 -0500648 }
Florin Malita16d0ad02018-01-19 15:07:29 -0500649
650 // If we still have multiple geos, reduce using 'merge'.
651 auto geo = drawGeos.size() > 1
652 ? sksg::Merge::Make(std::move(drawGeos), sksg::Merge::Mode::kMerge)
653 : drawGeos[0];
654
655 SkASSERT(geo);
656 draws.push_back(sksg::Draw::Make(std::move(geo), std::move(paint)));
Florin Malitaca4439f2018-01-23 10:31:59 -0500657 shapeCtx->fCommittedAnimators = shapeCtx->fCtx->fAnimators.size();
Florin Malitadacc02b2017-12-31 09:12:31 -0500658 } break;
Florin Malita16d0ad02018-01-19 15:07:29 -0500659 default:
660 break;
Florin Malita094ccde2017-12-30 12:27:00 -0500661 }
662 }
663
Florin Malita16d0ad02018-01-19 15:07:29 -0500664 // By now we should have popped all local geometry effects.
Florin Malitaca4439f2018-01-23 10:31:59 -0500665 SkASSERT(shapeCtx->fGeometryEffectStack->size() == initialGeometryEffects);
Florin Malita16d0ad02018-01-19 15:07:29 -0500666
667 // Push transformed local geometries to parent list, for subsequent paints.
668 for (const auto& geo : geos) {
Florin Malitaca4439f2018-01-23 10:31:59 -0500669 shapeCtx->fGeometryStack->push_back(shape_matrix
Florin Malita16d0ad02018-01-19 15:07:29 -0500670 ? sksg::GeometryTransform::Make(std::move(geo), shape_matrix)
671 : std::move(geo));
Florin Malita094ccde2017-12-30 12:27:00 -0500672 }
673
Florin Malita16d0ad02018-01-19 15:07:29 -0500674 // Emit local draws reversed (bottom->top, per spec).
675 for (auto it = draws.rbegin(); it != draws.rend(); ++it) {
676 shape_group->addChild(std::move(*it));
Florin Malita2a8275b2018-01-02 12:52:43 -0500677 }
678
Florin Malita16d0ad02018-01-19 15:07:29 -0500679 return draws.empty() ? nullptr : shape_wrapper;
Florin Malita094ccde2017-12-30 12:27:00 -0500680}
681
Florin Malita1022f742018-02-23 11:10:22 -0500682sk_sp<sksg::RenderNode> AttachNestedAnimation(const char* path, AttachContext* ctx) {
683 class SkottieSGAdapter final : public sksg::RenderNode {
684 public:
685 explicit SkottieSGAdapter(sk_sp<Animation> animation)
686 : fAnimation(std::move(animation)) {
687 SkASSERT(fAnimation);
688 }
689
690 protected:
691 SkRect onRevalidate(sksg::InvalidationController*, const SkMatrix&) override {
692 return SkRect::MakeSize(fAnimation->size());
693 }
694
695 void onRender(SkCanvas* canvas) const override {
696 fAnimation->render(canvas);
697 }
698
699 private:
700 const sk_sp<Animation> fAnimation;
701 };
702
703 class SkottieAnimatorAdapter final : public sksg::Animator {
704 public:
705 SkottieAnimatorAdapter(sk_sp<Animation> animation, float frameRate)
706 : fAnimation(std::move(animation))
707 , fFrameRate(frameRate) {
708 SkASSERT(fAnimation);
709 SkASSERT(fFrameRate > 0);
710 }
711
712 protected:
713 void onTick(float t) {
714 // map back from frame # to ms.
715 const auto t_ms = t * 1000 / fFrameRate;
716 fAnimation->animationTick(t_ms);
717 }
718
719 private:
720 const sk_sp<Animation> fAnimation;
721 const float fFrameRate;
722 };
723
724 const auto resStream = ctx->fResources.openStream(path);
725 if (!resStream || !resStream->hasLength()) {
726 LOG("!! Could not open: %s\n", path);
727 return nullptr;
728 }
729
730 auto animation = Animation::Make(resStream.get(), ctx->fResources);
731 if (!animation) {
732 LOG("!! Could not load nested animation: %s\n", path);
733 return nullptr;
734 }
735
736 ctx->fAnimators.push_back(skstd::make_unique<SkottieAnimatorAdapter>(animation,
737 ctx->fFrameRate));
738
739 return sk_make_sp<SkottieSGAdapter>(std::move(animation));
740}
741
Florin Malitaeb87d672018-01-29 15:28:24 -0500742sk_sp<sksg::RenderNode> AttachCompLayer(const Json::Value& jlayer, AttachContext* ctx,
743 float* time_bias, float* time_scale) {
744 SkASSERT(jlayer.isObject());
Florin Malita094ccde2017-12-30 12:27:00 -0500745
Florin Malitacf8ed522018-01-25 15:27:33 -0500746 SkString refId;
Florin Malitaeb87d672018-01-29 15:28:24 -0500747 if (!Parse(jlayer["refId"], &refId) || refId.isEmpty()) {
Florin Malitadd22cf962018-01-29 15:42:01 +0000748 LOG("!! Comp layer missing refId\n");
Florin Malita094ccde2017-12-30 12:27:00 -0500749 return nullptr;
750 }
751
Florin Malitaeb87d672018-01-29 15:28:24 -0500752 const auto start_time = ParseDefault(jlayer["st"], 0.0f),
753 stretch_time = ParseDefault(jlayer["sr"], 1.0f);
754
755 *time_bias = -start_time;
756 *time_scale = 1 / stretch_time;
757 if (SkScalarIsNaN(*time_scale)) {
758 *time_scale = 1;
759 }
760
Florin Malita1022f742018-02-23 11:10:22 -0500761 if (refId.startsWith("$")) {
762 return AttachNestedAnimation(refId.c_str() + 1, ctx);
763 }
764
765 const auto* comp = ctx->fAssets.find(refId);
766 if (!comp) {
767 LOG("!! Pre-comp not found: '%s'\n", refId.c_str());
768 return nullptr;
769 }
770
Florin Malitadd22cf962018-01-29 15:42:01 +0000771 // TODO: cycle detection
772 return AttachComposition(**comp, ctx);
Florin Malita094ccde2017-12-30 12:27:00 -0500773}
774
Florin Malitaeb87d672018-01-29 15:28:24 -0500775sk_sp<sksg::RenderNode> AttachSolidLayer(const Json::Value& jlayer, AttachContext*,
776 float*, float*) {
Florin Malita0e66fba2018-01-09 17:10:18 -0500777 SkASSERT(jlayer.isObject());
Florin Malita094ccde2017-12-30 12:27:00 -0500778
Florin Malitacf8ed522018-01-25 15:27:33 -0500779 const auto size = SkSize::Make(ParseDefault(jlayer["sw"], 0.0f),
780 ParseDefault(jlayer["sh"], 0.0f));
781 const auto hex = ParseDefault(jlayer["sc"], SkString());
Florin Malita0e66fba2018-01-09 17:10:18 -0500782 uint32_t c;
783 if (size.isEmpty() ||
784 !hex.startsWith("#") ||
785 !SkParse::FindHex(hex.c_str() + 1, &c)) {
786 LogFail(jlayer, "Could not parse solid layer");
787 return nullptr;
788 }
789
790 const SkColor color = 0xff000000 | c;
791
792 return sksg::Draw::Make(sksg::Rect::Make(SkRect::MakeSize(size)),
793 sksg::Color::Make(color));
Florin Malita094ccde2017-12-30 12:27:00 -0500794}
795
Florin Malita49328072018-01-08 12:51:12 -0500796sk_sp<sksg::RenderNode> AttachImageAsset(const Json::Value& jimage, AttachContext* ctx) {
797 SkASSERT(jimage.isObject());
798
Florin Malitacf8ed522018-01-25 15:27:33 -0500799 const auto name = ParseDefault(jimage["p"], SkString()),
800 path = ParseDefault(jimage["u"], SkString());
Florin Malita49328072018-01-08 12:51:12 -0500801 if (name.isEmpty())
802 return nullptr;
803
804 // TODO: plumb resource paths explicitly to ResourceProvider?
805 const auto resName = path.isEmpty() ? name : SkOSPath::Join(path.c_str(), name.c_str());
806 const auto resStream = ctx->fResources.openStream(resName.c_str());
807 if (!resStream || !resStream->hasLength()) {
808 LOG("!! Could not load image resource: %s\n", resName.c_str());
809 return nullptr;
810 }
811
812 // TODO: non-intrisic image sizing
813 return sksg::Image::Make(
814 SkImage::MakeFromEncoded(SkData::MakeFromStream(resStream.get(), resStream->getLength())));
815}
816
Florin Malitaeb87d672018-01-29 15:28:24 -0500817sk_sp<sksg::RenderNode> AttachImageLayer(const Json::Value& layer, AttachContext* ctx,
818 float*, float*) {
Florin Malitadd22cf962018-01-29 15:42:01 +0000819 SkASSERT(layer.isObject());
820
821 SkString refId;
822 if (!Parse(layer["refId"], &refId) || refId.isEmpty()) {
823 LOG("!! Image layer missing refId\n");
824 return nullptr;
825 }
826
827 const auto* jimage = ctx->fAssets.find(refId);
828 if (!jimage) {
829 LOG("!! Image asset not found: '%s'\n", refId.c_str());
830 return nullptr;
831 }
832
833 return AttachImageAsset(**jimage, ctx);
Florin Malita094ccde2017-12-30 12:27:00 -0500834}
835
Florin Malitaeb87d672018-01-29 15:28:24 -0500836sk_sp<sksg::RenderNode> AttachNullLayer(const Json::Value& layer, AttachContext*, float*, float*) {
Florin Malita094ccde2017-12-30 12:27:00 -0500837 SkASSERT(layer.isObject());
838
Florin Malita18eafd92018-01-04 21:11:55 -0500839 // Null layers are used solely to drive dependent transforms,
840 // but we use free-floating sksg::Matrices for that purpose.
Florin Malita094ccde2017-12-30 12:27:00 -0500841 return nullptr;
842}
843
Florin Malitaeb87d672018-01-29 15:28:24 -0500844sk_sp<sksg::RenderNode> AttachShapeLayer(const Json::Value& layer, AttachContext* ctx,
845 float*, float*) {
Florin Malita094ccde2017-12-30 12:27:00 -0500846 SkASSERT(layer.isObject());
847
Florin Malita16d0ad02018-01-19 15:07:29 -0500848 std::vector<sk_sp<sksg::GeometryNode>> geometryStack;
849 std::vector<GeometryEffectRec> geometryEffectStack;
Florin Malitaca4439f2018-01-23 10:31:59 -0500850 AttachShapeContext shapeCtx(ctx, &geometryStack, &geometryEffectStack, ctx->fAnimators.size());
851 auto shapeNode = AttachShape(layer["shapes"], &shapeCtx);
852
853 // Trim uncommitted animators: AttachShape consumes effects on the fly, and greedily attaches
854 // geometries => at the end, we can end up with unused geometries, which are nevertheless alive
855 // due to attached animators. To avoid this, we track committed animators and discard the
856 // orphans here.
857 SkASSERT(shapeCtx.fCommittedAnimators <= ctx->fAnimators.size());
858 ctx->fAnimators.resize(shapeCtx.fCommittedAnimators);
859
860 return shapeNode;
Florin Malita094ccde2017-12-30 12:27:00 -0500861}
862
Florin Malitaeb87d672018-01-29 15:28:24 -0500863sk_sp<sksg::RenderNode> AttachTextLayer(const Json::Value& layer, AttachContext*, float*, float*) {
Florin Malita094ccde2017-12-30 12:27:00 -0500864 SkASSERT(layer.isObject());
865
866 LOG("?? Text layer stub\n");
867 return nullptr;
868}
869
Florin Malita18eafd92018-01-04 21:11:55 -0500870struct AttachLayerContext {
871 AttachLayerContext(const Json::Value& jlayers, AttachContext* ctx)
872 : fLayerList(jlayers), fCtx(ctx) {}
873
Florin Malita4a490682018-01-28 14:27:51 -0500874 const Json::Value& fLayerList;
875 AttachContext* fCtx;
876 SkTHashMap<int, sk_sp<sksg::Matrix>> fLayerMatrixMap;
877 sk_sp<sksg::RenderNode> fCurrentMatte;
Florin Malita18eafd92018-01-04 21:11:55 -0500878
Florin Malita4a490682018-01-28 14:27:51 -0500879 sk_sp<sksg::Matrix> AttachParentLayerMatrix(const Json::Value& jlayer) {
880 SkASSERT(jlayer.isObject());
Florin Malita18eafd92018-01-04 21:11:55 -0500881 SkASSERT(fLayerList.isArray());
882
Florin Malita4a490682018-01-28 14:27:51 -0500883 const auto parent_index = ParseDefault(jlayer["parent"], -1);
884 if (parent_index < 0)
Florin Malita18eafd92018-01-04 21:11:55 -0500885 return nullptr;
Florin Malita18eafd92018-01-04 21:11:55 -0500886
Florin Malita4a490682018-01-28 14:27:51 -0500887 if (auto* m = fLayerMatrixMap.find(parent_index))
888 return *m;
Florin Malita18eafd92018-01-04 21:11:55 -0500889
890 for (const auto& l : fLayerList) {
891 if (!l.isObject()) {
892 continue;
893 }
894
Florin Malita4a490682018-01-28 14:27:51 -0500895 if (ParseDefault(l["ind"], -1) == parent_index) {
896 return this->AttachLayerMatrix(l);
Florin Malita18eafd92018-01-04 21:11:55 -0500897 }
898 }
899
900 return nullptr;
901 }
902
903 sk_sp<sksg::Matrix> AttachLayerMatrix(const Json::Value& jlayer) {
904 SkASSERT(jlayer.isObject());
905
Florin Malita4a490682018-01-28 14:27:51 -0500906 const auto layer_index = ParseDefault(jlayer["ind"], -1);
907 if (layer_index < 0)
908 return nullptr;
Florin Malita18eafd92018-01-04 21:11:55 -0500909
Florin Malita4a490682018-01-28 14:27:51 -0500910 if (auto* m = fLayerMatrixMap.find(layer_index))
911 return *m;
Florin Malita18eafd92018-01-04 21:11:55 -0500912
Florin Malita4a490682018-01-28 14:27:51 -0500913 // Add a stub entry to break recursion cycles.
914 fLayerMatrixMap.set(layer_index, nullptr);
Florin Malita18eafd92018-01-04 21:11:55 -0500915
Florin Malita4a490682018-01-28 14:27:51 -0500916 auto parent_matrix = this->AttachParentLayerMatrix(jlayer);
Florin Malita18eafd92018-01-04 21:11:55 -0500917
Florin Malita4a490682018-01-28 14:27:51 -0500918 return *fLayerMatrixMap.set(layer_index,
919 AttachMatrix(jlayer["ks"],
920 fCtx,
921 this->AttachParentLayerMatrix(jlayer)));
Florin Malita18eafd92018-01-04 21:11:55 -0500922 }
923};
924
Florin Malita25366fa2018-01-23 13:37:59 -0500925SkBlendMode MaskBlendMode(char mode) {
926 switch (mode) {
927 case 'a': return SkBlendMode::kSrcOver; // Additive
928 case 's': return SkBlendMode::kExclusion; // Subtract
929 case 'i': return SkBlendMode::kDstIn; // Intersect
930 case 'l': return SkBlendMode::kLighten; // Lighten
931 case 'd': return SkBlendMode::kDarken; // Darken
932 case 'f': return SkBlendMode::kDifference; // Difference
933 default: break;
934 }
935
936 return SkBlendMode::kSrcOver;
937}
938
939sk_sp<sksg::RenderNode> AttachMask(const Json::Value& jmask,
940 AttachContext* ctx,
941 sk_sp<sksg::RenderNode> childNode) {
942 if (!jmask.isArray())
943 return childNode;
944
945 auto mask_group = sksg::Group::Make();
946
947 for (const auto& m : jmask) {
948 if (!m.isObject())
949 continue;
950
Florin Malitacf8ed522018-01-25 15:27:33 -0500951 const auto inverted = ParseDefault(m["inv"], false);
Florin Malita25366fa2018-01-23 13:37:59 -0500952 // TODO
953 if (inverted) {
954 LogFail(m, "Unsupported inverse mask");
955 continue;
956 }
957
958 auto mask_path = AttachPath(m["pt"], ctx);
959 if (!mask_path) {
960 LogFail(m, "Could not parse mask path");
961 continue;
962 }
963
Florin Malitacf8ed522018-01-25 15:27:33 -0500964 SkString mode;
965 if (!Parse(m["mode"], &mode) ||
966 mode.size() != 1 ||
967 !strcmp(mode.c_str(), "n")) { // "None" masks have no effect.
Florin Malita25366fa2018-01-23 13:37:59 -0500968 continue;
Florin Malitacf8ed522018-01-25 15:27:33 -0500969 }
Florin Malita25366fa2018-01-23 13:37:59 -0500970
971 auto mask_paint = sksg::Color::Make(SK_ColorBLACK);
Florin Malitaaa71c892018-01-30 09:27:01 -0500972 mask_paint->setAntiAlias(true);
Florin Malita25366fa2018-01-23 13:37:59 -0500973 mask_paint->setBlendMode(MaskBlendMode(mode.c_str()[0]));
Florin Malitafc807c82018-01-25 22:35:09 -0500974 BindProperty<ScalarValue>(m["o"], &ctx->fAnimators,
Florin Malita2518a0a2018-01-24 18:29:00 -0500975 [mask_paint](const ScalarValue& o) { mask_paint->setOpacity(o * 0.01f); });
Florin Malita25366fa2018-01-23 13:37:59 -0500976
977 mask_group->addChild(sksg::Draw::Make(std::move(mask_path), std::move(mask_paint)));
978 }
979
980 return mask_group->empty()
981 ? childNode
982 : sksg::MaskEffect::Make(std::move(childNode), std::move(mask_group));
983}
984
Florin Malita18eafd92018-01-04 21:11:55 -0500985sk_sp<sksg::RenderNode> AttachLayer(const Json::Value& jlayer,
986 AttachLayerContext* layerCtx) {
987 if (!jlayer.isObject())
Florin Malita094ccde2017-12-30 12:27:00 -0500988 return nullptr;
989
Florin Malitaeb87d672018-01-29 15:28:24 -0500990 using LayerAttacher = sk_sp<sksg::RenderNode> (*)(const Json::Value&, AttachContext*,
991 float* time_bias, float* time_scale);
Florin Malita094ccde2017-12-30 12:27:00 -0500992 static constexpr LayerAttacher gLayerAttachers[] = {
993 AttachCompLayer, // 'ty': 0
994 AttachSolidLayer, // 'ty': 1
995 AttachImageLayer, // 'ty': 2
996 AttachNullLayer, // 'ty': 3
997 AttachShapeLayer, // 'ty': 4
998 AttachTextLayer, // 'ty': 5
999 };
1000
Florin Malitacf8ed522018-01-25 15:27:33 -05001001 int type = ParseDefault(jlayer["ty"], -1);
Florin Malita094ccde2017-12-30 12:27:00 -05001002 if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gLayerAttachers))) {
1003 return nullptr;
1004 }
1005
Florin Malitacca86f32018-01-29 10:49:49 -05001006 sksg::AnimatorList layer_animators;
Florin Malita1022f742018-02-23 11:10:22 -05001007 AttachContext local_ctx = { layerCtx->fCtx->fResources,
1008 layerCtx->fCtx->fAssets,
1009 layerCtx->fCtx->fFrameRate,
1010 layer_animators};
Florin Malitacca86f32018-01-29 10:49:49 -05001011
Florin Malitaeb87d672018-01-29 15:28:24 -05001012 // Layer attachers may adjust these.
1013 float time_bias = 0,
1014 time_scale = 1;
1015
Florin Malita71cba8f2018-01-09 08:07:14 -05001016 // Layer content.
Florin Malitaeb87d672018-01-29 15:28:24 -05001017 auto layer = gLayerAttachers[type](jlayer, &local_ctx, &time_bias, &time_scale);
Florin Malita38ea40e2018-01-29 16:31:14 -05001018
1019 // Clip layers with explicit dimensions.
1020 float w, h;
1021 if (Parse(jlayer["w"], &w) && Parse(jlayer["h"], &h)) {
1022 layer = sksg::ClipEffect::Make(std::move(layer),
1023 sksg::Rect::Make(SkRect::MakeWH(w, h)),
1024 true);
1025 }
1026
Florin Malita25366fa2018-01-23 13:37:59 -05001027 // Optional layer mask.
Florin Malitacca86f32018-01-29 10:49:49 -05001028 layer = AttachMask(jlayer["masksProperties"], &local_ctx, std::move(layer));
Florin Malita38ea40e2018-01-29 16:31:14 -05001029
Florin Malita25366fa2018-01-23 13:37:59 -05001030 // Optional layer transform.
Florin Malita71cba8f2018-01-09 08:07:14 -05001031 if (auto layerMatrix = layerCtx->AttachLayerMatrix(jlayer)) {
Florin Malita71cba8f2018-01-09 08:07:14 -05001032 layer = sksg::Transform::Make(std::move(layer), std::move(layerMatrix));
1033 }
Florin Malita38ea40e2018-01-29 16:31:14 -05001034
Florin Malita71cba8f2018-01-09 08:07:14 -05001035 // Optional layer opacity.
Florin Malitacca86f32018-01-29 10:49:49 -05001036 layer = AttachOpacity(jlayer["ks"], &local_ctx, std::move(layer));
Florin Malita18eafd92018-01-04 21:11:55 -05001037
Florin Malitaeb87d672018-01-29 15:28:24 -05001038 class LayerController final : public sksg::GroupAnimator {
Florin Malita71cba8f2018-01-09 08:07:14 -05001039 public:
Florin Malitaeb87d672018-01-29 15:28:24 -05001040 LayerController(sksg::AnimatorList&& layer_animators,
1041 sk_sp<sksg::OpacityEffect> controlNode,
1042 float in, float out,
1043 float time_bias, float time_scale)
Florin Malitacca86f32018-01-29 10:49:49 -05001044 : INHERITED(std::move(layer_animators))
1045 , fControlNode(std::move(controlNode))
Florin Malita71cba8f2018-01-09 08:07:14 -05001046 , fIn(in)
Florin Malitaeb87d672018-01-29 15:28:24 -05001047 , fOut(out)
1048 , fTimeBias(time_bias)
1049 , fTimeScale(time_scale) {}
Florin Malita71cba8f2018-01-09 08:07:14 -05001050
Florin Malita35efaa82018-01-22 12:57:06 -05001051 void onTick(float t) override {
Florin Malitacca86f32018-01-29 10:49:49 -05001052 const auto active = (t >= fIn && t <= fOut);
1053
Florin Malita71cba8f2018-01-09 08:07:14 -05001054 // Keep the layer fully transparent except for its [in..out] lifespan.
1055 // (note: opacity == 0 disables rendering, while opacity == 1 is a noop)
Florin Malitacca86f32018-01-29 10:49:49 -05001056 fControlNode->setOpacity(active ? 1 : 0);
1057
1058 // Dispatch ticks only while active.
1059 if (active)
Florin Malitaeb87d672018-01-29 15:28:24 -05001060 this->INHERITED::onTick((t + fTimeBias) * fTimeScale);
Florin Malita71cba8f2018-01-09 08:07:14 -05001061 }
1062
1063 private:
1064 const sk_sp<sksg::OpacityEffect> fControlNode;
1065 const float fIn,
Florin Malitaeb87d672018-01-29 15:28:24 -05001066 fOut,
1067 fTimeBias,
1068 fTimeScale;
Florin Malitacca86f32018-01-29 10:49:49 -05001069
1070 using INHERITED = sksg::GroupAnimator;
Florin Malita71cba8f2018-01-09 08:07:14 -05001071 };
1072
Florin Malitaeb87d672018-01-29 15:28:24 -05001073 auto controller_node = sksg::OpacityEffect::Make(std::move(layer));
1074 const auto in = ParseDefault(jlayer["ip"], 0.0f),
1075 out = ParseDefault(jlayer["op"], in);
Florin Malita71cba8f2018-01-09 08:07:14 -05001076
Florin Malitaeb87d672018-01-29 15:28:24 -05001077 if (!jlayer["tm"].isNull()) {
1078 LogFail(jlayer["tm"], "Unsupported time remapping");
1079 }
1080
1081 if (in >= out || !controller_node)
Florin Malita71cba8f2018-01-09 08:07:14 -05001082 return nullptr;
1083
Florin Malitacca86f32018-01-29 10:49:49 -05001084 layerCtx->fCtx->fAnimators.push_back(
Florin Malitaeb87d672018-01-29 15:28:24 -05001085 skstd::make_unique<LayerController>(std::move(layer_animators),
1086 controller_node,
1087 in,
1088 out,
1089 time_bias,
1090 time_scale));
Florin Malita71cba8f2018-01-09 08:07:14 -05001091
Florin Malitacf8ed522018-01-25 15:27:33 -05001092 if (ParseDefault(jlayer["td"], false)) {
Florin Malita5f9102f2018-01-10 13:36:22 -05001093 // This layer is a matte. We apply it as a mask to the next layer.
Florin Malitaeb87d672018-01-29 15:28:24 -05001094 layerCtx->fCurrentMatte = std::move(controller_node);
Florin Malita5f9102f2018-01-10 13:36:22 -05001095 return nullptr;
1096 }
1097
1098 if (layerCtx->fCurrentMatte) {
1099 // There is a pending matte. Apply and reset.
Florin Malitaa016be92018-03-05 14:01:41 -05001100 static constexpr sksg::MaskEffect::Mode gMaskModes[] = {
1101 sksg::MaskEffect::Mode::kNormal, // tt: 1
1102 sksg::MaskEffect::Mode::kInvert, // tt: 2
1103 };
1104 const auto matteType = ParseDefault(jlayer["tt"], 1) - 1;
1105
1106 if (matteType >= 0 && matteType < SkTo<int>(SK_ARRAY_COUNT(gMaskModes))) {
1107 return sksg::MaskEffect::Make(std::move(controller_node),
1108 std::move(layerCtx->fCurrentMatte),
1109 gMaskModes[matteType]);
1110 }
1111 layerCtx->fCurrentMatte.reset();
Florin Malita5f9102f2018-01-10 13:36:22 -05001112 }
1113
Florin Malitaeb87d672018-01-29 15:28:24 -05001114 return controller_node;
Florin Malita094ccde2017-12-30 12:27:00 -05001115}
1116
1117sk_sp<sksg::RenderNode> AttachComposition(const Json::Value& comp, AttachContext* ctx) {
1118 if (!comp.isObject())
1119 return nullptr;
1120
Florin Malita18eafd92018-01-04 21:11:55 -05001121 const auto& jlayers = comp["layers"];
1122 if (!jlayers.isArray())
1123 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -05001124
Florin Malita18eafd92018-01-04 21:11:55 -05001125 SkSTArray<16, sk_sp<sksg::RenderNode>, true> layers;
1126 AttachLayerContext layerCtx(jlayers, ctx);
1127
1128 for (const auto& l : jlayers) {
1129 if (auto layer_fragment = AttachLayer(l, &layerCtx)) {
Florin Malita2a8275b2018-01-02 12:52:43 -05001130 layers.push_back(std::move(layer_fragment));
Florin Malita094ccde2017-12-30 12:27:00 -05001131 }
1132 }
1133
Florin Malita2a8275b2018-01-02 12:52:43 -05001134 if (layers.empty()) {
1135 return nullptr;
1136 }
1137
1138 // Layers are painted in bottom->top order.
1139 auto comp_group = sksg::Group::Make();
1140 for (int i = layers.count() - 1; i >= 0; --i) {
1141 comp_group->addChild(std::move(layers[i]));
1142 }
1143
Florin Malita094ccde2017-12-30 12:27:00 -05001144 return comp_group;
1145}
1146
1147} // namespace
1148
Florin Malita1022f742018-02-23 11:10:22 -05001149sk_sp<Animation> Animation::Make(SkStream* stream, const ResourceProvider& res) {
Florin Malita094ccde2017-12-30 12:27:00 -05001150 if (!stream->hasLength()) {
1151 // TODO: handle explicit buffering?
1152 LOG("!! cannot parse streaming content\n");
1153 return nullptr;
1154 }
1155
1156 Json::Value json;
1157 {
1158 auto data = SkData::MakeFromStream(stream, stream->getLength());
1159 if (!data) {
1160 LOG("!! could not read stream\n");
1161 return nullptr;
1162 }
1163
1164 Json::Reader reader;
1165
1166 auto dataStart = static_cast<const char*>(data->data());
1167 if (!reader.parse(dataStart, dataStart + data->size(), json, false) || !json.isObject()) {
1168 LOG("!! failed to parse json: %s\n", reader.getFormattedErrorMessages().c_str());
1169 return nullptr;
1170 }
1171 }
1172
Florin Malitacf8ed522018-01-25 15:27:33 -05001173 const auto version = ParseDefault(json["v"], SkString());
1174 const auto size = SkSize::Make(ParseDefault(json["w"], 0.0f),
1175 ParseDefault(json["h"], 0.0f));
1176 const auto fps = ParseDefault(json["fr"], -1.0f);
Florin Malita094ccde2017-12-30 12:27:00 -05001177
Florin Malita1022f742018-02-23 11:10:22 -05001178 if (size.isEmpty() || version.isEmpty() || fps <= 0) {
Florin Malita094ccde2017-12-30 12:27:00 -05001179 LOG("!! invalid animation params (version: %s, size: [%f %f], frame rate: %f)",
1180 version.c_str(), size.width(), size.height(), fps);
1181 return nullptr;
1182 }
1183
Florin Malita1022f742018-02-23 11:10:22 -05001184 return sk_sp<Animation>(new Animation(res, std::move(version), size, fps, json));
Florin Malita094ccde2017-12-30 12:27:00 -05001185}
1186
Florin Malita1022f742018-02-23 11:10:22 -05001187sk_sp<Animation> Animation::MakeFromFile(const char path[], const ResourceProvider* res) {
Florin Malita49328072018-01-08 12:51:12 -05001188 class DirectoryResourceProvider final : public ResourceProvider {
1189 public:
1190 explicit DirectoryResourceProvider(SkString dir) : fDir(std::move(dir)) {}
1191
1192 std::unique_ptr<SkStream> openStream(const char resource[]) const override {
1193 const auto resPath = SkOSPath::Join(fDir.c_str(), resource);
1194 return SkStream::MakeFromFile(resPath.c_str());
1195 }
1196
1197 private:
1198 const SkString fDir;
1199 };
1200
1201 const auto jsonStream = SkStream::MakeFromFile(path);
1202 if (!jsonStream)
1203 return nullptr;
1204
1205 std::unique_ptr<ResourceProvider> defaultProvider;
1206 if (!res) {
1207 defaultProvider = skstd::make_unique<DirectoryResourceProvider>(SkOSPath::Dirname(path));
1208 }
1209
1210 return Make(jsonStream.get(), res ? *res : *defaultProvider);
1211}
1212
1213Animation::Animation(const ResourceProvider& resources,
1214 SkString version, const SkSize& size, SkScalar fps, const Json::Value& json)
Florin Malita094ccde2017-12-30 12:27:00 -05001215 : fVersion(std::move(version))
1216 , fSize(size)
1217 , fFrameRate(fps)
Florin Malitacf8ed522018-01-25 15:27:33 -05001218 , fInPoint(ParseDefault(json["ip"], 0.0f))
1219 , fOutPoint(SkTMax(ParseDefault(json["op"], SK_ScalarMax), fInPoint)) {
Florin Malita094ccde2017-12-30 12:27:00 -05001220
1221 AssetMap assets;
1222 for (const auto& asset : json["assets"]) {
1223 if (!asset.isObject()) {
1224 continue;
1225 }
1226
Florin Malitadd22cf962018-01-29 15:42:01 +00001227 assets.set(ParseDefault(asset["id"], SkString()), &asset);
Florin Malita094ccde2017-12-30 12:27:00 -05001228 }
1229
Florin Malitacca86f32018-01-29 10:49:49 -05001230 sksg::AnimatorList animators;
Florin Malita1022f742018-02-23 11:10:22 -05001231 AttachContext ctx = { resources, assets, fFrameRate, animators };
Florin Malita35efaa82018-01-22 12:57:06 -05001232 auto root = AttachComposition(json, &ctx);
1233
1234 LOG("** Attached %d animators\n", animators.size());
1235
1236 fScene = sksg::Scene::Make(std::move(root), std::move(animators));
Florin Malita094ccde2017-12-30 12:27:00 -05001237
Florin Malitadb385732018-01-09 12:19:32 -05001238 // In case the client calls render before the first tick.
1239 this->animationTick(0);
Florin Malita094ccde2017-12-30 12:27:00 -05001240}
1241
1242Animation::~Animation() = default;
1243
Florin Malita35efaa82018-01-22 12:57:06 -05001244void Animation::setShowInval(bool show) {
1245 if (fScene) {
1246 fScene->setShowInval(show);
1247 }
1248}
1249
Mike Reed29859872018-01-08 08:25:27 -05001250void Animation::render(SkCanvas* canvas, const SkRect* dstR) const {
Florin Malita35efaa82018-01-22 12:57:06 -05001251 if (!fScene)
Florin Malita094ccde2017-12-30 12:27:00 -05001252 return;
1253
Mike Reed29859872018-01-08 08:25:27 -05001254 SkAutoCanvasRestore restore(canvas, true);
1255 const SkRect srcR = SkRect::MakeSize(this->size());
1256 if (dstR) {
1257 canvas->concat(SkMatrix::MakeRectToRect(srcR, *dstR, SkMatrix::kCenter_ScaleToFit));
1258 }
1259 canvas->clipRect(srcR);
Florin Malita35efaa82018-01-22 12:57:06 -05001260 fScene->render(canvas);
Florin Malita094ccde2017-12-30 12:27:00 -05001261}
1262
1263void Animation::animationTick(SkMSec ms) {
Florin Malita35efaa82018-01-22 12:57:06 -05001264 if (!fScene)
1265 return;
1266
Florin Malita094ccde2017-12-30 12:27:00 -05001267 // 't' in the BM model really means 'frame #'
1268 auto t = static_cast<float>(ms) * fFrameRate / 1000;
1269
1270 t = fInPoint + std::fmod(t, fOutPoint - fInPoint);
1271
Florin Malita35efaa82018-01-22 12:57:06 -05001272 fScene->animate(t);
Florin Malita094ccde2017-12-30 12:27:00 -05001273}
1274
Florin Malita54f65c42018-01-16 17:04:30 -05001275} // namespace skottie