blob: f8c5ea021cecccdf1efcb7e4f436aa0d7968a680 [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 Malita54f65c42018-01-16 17:04:30 -050011#include "SkottieAnimator.h"
12#include "SkottiePriv.h"
13#include "SkottieProperties.h"
Florin Malita094ccde2017-12-30 12:27:00 -050014#include "SkData.h"
Florin Malita49328072018-01-08 12:51:12 -050015#include "SkImage.h"
Florin Malita094ccde2017-12-30 12:27:00 -050016#include "SkMakeUnique.h"
Florin Malita49328072018-01-08 12:51:12 -050017#include "SkOSPath.h"
Florin Malita094ccde2017-12-30 12:27:00 -050018#include "SkPaint.h"
Florin Malita0e66fba2018-01-09 17:10:18 -050019#include "SkParse.h"
Florin Malita094ccde2017-12-30 12:27:00 -050020#include "SkPath.h"
21#include "SkPoint.h"
22#include "SkSGColor.h"
23#include "SkSGDraw.h"
Florin Malita6aaee592018-01-12 12:25:09 -050024#include "SkSGGradient.h"
Florin Malita094ccde2017-12-30 12:27:00 -050025#include "SkSGGroup.h"
Florin Malita49328072018-01-08 12:51:12 -050026#include "SkSGImage.h"
27#include "SkSGInvalidationController.h"
Florin Malita5f9102f2018-01-10 13:36:22 -050028#include "SkSGMaskEffect.h"
Florin Malitae6345d92018-01-03 23:37:54 -050029#include "SkSGMerge.h"
Florin Malitac0034172018-01-08 16:42:59 -050030#include "SkSGOpacityEffect.h"
Florin Malita094ccde2017-12-30 12:27:00 -050031#include "SkSGPath.h"
Florin Malita2e1d7e22018-01-02 10:40:00 -050032#include "SkSGRect.h"
Florin Malita094ccde2017-12-30 12:27:00 -050033#include "SkSGTransform.h"
Florin Malita51b8c892018-01-07 08:54:24 -050034#include "SkSGTrimEffect.h"
Florin Malita094ccde2017-12-30 12:27:00 -050035#include "SkStream.h"
36#include "SkTArray.h"
37#include "SkTHash.h"
38
39#include <cmath>
Florin Malita18eafd92018-01-04 21:11:55 -050040#include <unordered_map>
Florin Malitae6345d92018-01-03 23:37:54 -050041#include <vector>
42
Florin Malita094ccde2017-12-30 12:27:00 -050043#include "stdlib.h"
44
Florin Malita54f65c42018-01-16 17:04:30 -050045namespace skottie {
Florin Malita094ccde2017-12-30 12:27:00 -050046
47namespace {
48
49using AssetMap = SkTHashMap<SkString, const Json::Value*>;
50
51struct AttachContext {
Florin Malita49328072018-01-08 12:51:12 -050052 const ResourceProvider& fResources;
Florin Malita094ccde2017-12-30 12:27:00 -050053 const AssetMap& fAssets;
54 SkTArray<std::unique_ptr<AnimatorBase>>& fAnimators;
55};
56
57bool LogFail(const Json::Value& json, const char* msg) {
58 const auto dump = json.toStyledString();
59 LOG("!! %s: %s", msg, dump.c_str());
60 return false;
61}
62
63// This is the workhorse for binding properties: depending on whether the property is animated,
64// it will either apply immediately or instantiate and attach a keyframe animator.
Florin Malitaf9590922018-01-09 11:56:09 -050065template <typename ValT, typename NodeT>
66bool BindProperty(const Json::Value& jprop, AttachContext* ctx, const sk_sp<NodeT>& node,
67 typename Animator<ValT, NodeT>::ApplyFuncT&& apply) {
Florin Malita094ccde2017-12-30 12:27:00 -050068 if (!jprop.isObject())
69 return false;
70
Florin Malita95448a92018-01-08 10:15:12 -050071 const auto& jpropA = jprop["a"];
72 const auto& jpropK = jprop["k"];
73
74 // Older Json versions don't have an "a" animation marker.
75 // For those, we attempt to parse both ways.
76 if (jpropA.isNull() || !ParseBool(jpropA, "false")) {
Florin Malitaf9590922018-01-09 11:56:09 -050077 ValT val;
78 if (ValueTraits<ValT>::Parse(jpropK, &val)) {
Florin Malita95448a92018-01-08 10:15:12 -050079 // Static property.
Florin Malitaf9590922018-01-09 11:56:09 -050080 apply(node.get(), val);
Florin Malita95448a92018-01-08 10:15:12 -050081 return true;
Florin Malita094ccde2017-12-30 12:27:00 -050082 }
83
Florin Malita95448a92018-01-08 10:15:12 -050084 if (!jpropA.isNull()) {
85 return LogFail(jprop, "Could not parse (explicit) static property");
Florin Malita094ccde2017-12-30 12:27:00 -050086 }
Florin Malita094ccde2017-12-30 12:27:00 -050087 }
88
Florin Malita95448a92018-01-08 10:15:12 -050089 // Keyframe property.
Florin Malitaf9590922018-01-09 11:56:09 -050090 using AnimatorT = Animator<ValT, NodeT>;
91 auto animator = AnimatorT::Make(ParseFrames<ValT>(jpropK), node, std::move(apply));
Florin Malita95448a92018-01-08 10:15:12 -050092
93 if (!animator) {
94 return LogFail(jprop, "Could not parse keyframed property");
95 }
96
97 ctx->fAnimators.push_back(std::move(animator));
98
Florin Malita094ccde2017-12-30 12:27:00 -050099 return true;
100}
101
Florin Malita18eafd92018-01-04 21:11:55 -0500102sk_sp<sksg::Matrix> AttachMatrix(const Json::Value& t, AttachContext* ctx,
103 sk_sp<sksg::Matrix> parentMatrix) {
104 if (!t.isObject())
105 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500106
Florin Malita18eafd92018-01-04 21:11:55 -0500107 auto matrix = sksg::Matrix::Make(SkMatrix::I(), std::move(parentMatrix));
108 auto composite = sk_make_sp<CompositeTransform>(matrix);
Florin Malitaf9590922018-01-09 11:56:09 -0500109 auto anchor_attached = BindProperty<VectorValue>(t["a"], ctx, composite,
110 [](CompositeTransform* node, const VectorValue& a) {
111 node->setAnchorPoint(ValueTraits<VectorValue>::As<SkPoint>(a));
Florin Malita094ccde2017-12-30 12:27:00 -0500112 });
Florin Malitaf9590922018-01-09 11:56:09 -0500113 auto position_attached = BindProperty<VectorValue>(t["p"], ctx, composite,
114 [](CompositeTransform* node, const VectorValue& p) {
115 node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
Florin Malita094ccde2017-12-30 12:27:00 -0500116 });
Florin Malitaf9590922018-01-09 11:56:09 -0500117 auto scale_attached = BindProperty<VectorValue>(t["s"], ctx, composite,
118 [](CompositeTransform* node, const VectorValue& s) {
119 node->setScale(ValueTraits<VectorValue>::As<SkVector>(s));
Florin Malita094ccde2017-12-30 12:27:00 -0500120 });
Florin Malitaf9590922018-01-09 11:56:09 -0500121 auto rotation_attached = BindProperty<ScalarValue>(t["r"], ctx, composite,
122 [](CompositeTransform* node, const ScalarValue& r) {
Florin Malita094ccde2017-12-30 12:27:00 -0500123 node->setRotation(r);
124 });
Florin Malitaf9590922018-01-09 11:56:09 -0500125 auto skew_attached = BindProperty<ScalarValue>(t["sk"], ctx, composite,
126 [](CompositeTransform* node, const ScalarValue& sk) {
Florin Malita094ccde2017-12-30 12:27:00 -0500127 node->setSkew(sk);
128 });
Florin Malitaf9590922018-01-09 11:56:09 -0500129 auto skewaxis_attached = BindProperty<ScalarValue>(t["sa"], ctx, composite,
130 [](CompositeTransform* node, const ScalarValue& sa) {
Florin Malita094ccde2017-12-30 12:27:00 -0500131 node->setSkewAxis(sa);
132 });
133
134 if (!anchor_attached &&
135 !position_attached &&
136 !scale_attached &&
137 !rotation_attached &&
138 !skew_attached &&
139 !skewaxis_attached) {
140 LogFail(t, "Could not parse transform");
Florin Malita18eafd92018-01-04 21:11:55 -0500141 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500142 }
143
Florin Malita18eafd92018-01-04 21:11:55 -0500144 return matrix;
Florin Malita094ccde2017-12-30 12:27:00 -0500145}
146
Florin Malitac0034172018-01-08 16:42:59 -0500147sk_sp<sksg::RenderNode> AttachOpacity(const Json::Value& jtransform, AttachContext* ctx,
148 sk_sp<sksg::RenderNode> childNode) {
149 if (!jtransform.isObject() || !childNode)
150 return childNode;
151
152 // This is more peeky than other attachers, because we want to avoid redundant opacity
153 // nodes for the extremely common case of static opaciy == 100.
154 const auto& opacity = jtransform["o"];
155 if (opacity.isObject() &&
156 !ParseBool(opacity["a"], true) &&
157 ParseScalar(opacity["k"], -1) == 100) {
158 // Ignoring static full opacity.
159 return childNode;
160 }
161
162 auto opacityNode = sksg::OpacityEffect::Make(childNode);
Florin Malitaf9590922018-01-09 11:56:09 -0500163 BindProperty<ScalarValue>(opacity, ctx, opacityNode,
164 [](sksg::OpacityEffect* node, const ScalarValue& o) {
Florin Malitac0034172018-01-08 16:42:59 -0500165 // BM opacity is [0..100]
166 node->setOpacity(o * 0.01f);
167 });
168
169 return opacityNode;
170}
171
Florin Malita094ccde2017-12-30 12:27:00 -0500172sk_sp<sksg::RenderNode> AttachShape(const Json::Value&, AttachContext* ctx);
173sk_sp<sksg::RenderNode> AttachComposition(const Json::Value&, AttachContext* ctx);
174
175sk_sp<sksg::RenderNode> AttachShapeGroup(const Json::Value& jgroup, AttachContext* ctx) {
176 SkASSERT(jgroup.isObject());
177
178 return AttachShape(jgroup["it"], ctx);
179}
180
181sk_sp<sksg::GeometryNode> AttachPathGeometry(const Json::Value& jpath, AttachContext* ctx) {
182 SkASSERT(jpath.isObject());
183
184 auto path_node = sksg::Path::Make();
Florin Malitaf9590922018-01-09 11:56:09 -0500185 auto path_attached = BindProperty<ShapeValue>(jpath["ks"], ctx, path_node,
186 [](sksg::Path* node, const ShapeValue& p) { node->setPath(p); });
Florin Malita094ccde2017-12-30 12:27:00 -0500187
188 if (path_attached)
189 LOG("** Attached path geometry - verbs: %d\n", path_node->getPath().countVerbs());
190
191 return path_attached ? path_node : nullptr;
192}
193
Florin Malita2e1d7e22018-01-02 10:40:00 -0500194sk_sp<sksg::GeometryNode> AttachRRectGeometry(const Json::Value& jrect, AttachContext* ctx) {
195 SkASSERT(jrect.isObject());
196
197 auto rect_node = sksg::RRect::Make();
198 auto composite = sk_make_sp<CompositeRRect>(rect_node);
199
Florin Malitaf9590922018-01-09 11:56:09 -0500200 auto p_attached = BindProperty<VectorValue>(jrect["p"], ctx, composite,
201 [](CompositeRRect* node, const VectorValue& p) {
202 node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
203 });
204 auto s_attached = BindProperty<VectorValue>(jrect["s"], ctx, composite,
205 [](CompositeRRect* node, const VectorValue& s) {
206 node->setSize(ValueTraits<VectorValue>::As<SkSize>(s));
207 });
208 auto r_attached = BindProperty<ScalarValue>(jrect["r"], ctx, composite,
209 [](CompositeRRect* node, const ScalarValue& r) {
210 node->setRadius(SkSize::Make(r, r));
211 });
Florin Malita2e1d7e22018-01-02 10:40:00 -0500212
213 if (!p_attached && !s_attached && !r_attached) {
214 return nullptr;
215 }
216
Florin Malitafbc13f12018-01-04 10:26:35 -0500217 LOG("** Attached (r)rect geometry\n");
218
219 return rect_node;
220}
221
222sk_sp<sksg::GeometryNode> AttachEllipseGeometry(const Json::Value& jellipse, AttachContext* ctx) {
223 SkASSERT(jellipse.isObject());
224
225 auto rect_node = sksg::RRect::Make();
226 auto composite = sk_make_sp<CompositeRRect>(rect_node);
227
Florin Malitaf9590922018-01-09 11:56:09 -0500228 auto p_attached = BindProperty<VectorValue>(jellipse["p"], ctx, composite,
229 [](CompositeRRect* node, const VectorValue& p) {
230 node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
231 });
232 auto s_attached = BindProperty<VectorValue>(jellipse["s"], ctx, composite,
233 [](CompositeRRect* node, const VectorValue& s) {
234 const auto sz = ValueTraits<VectorValue>::As<SkSize>(s);
235 node->setSize(sz);
236 node->setRadius(SkSize::Make(sz.width() / 2, sz.height() / 2));
237 });
Florin Malitafbc13f12018-01-04 10:26:35 -0500238
239 if (!p_attached && !s_attached) {
240 return nullptr;
241 }
242
243 LOG("** Attached ellipse geometry\n");
244
Florin Malita2e1d7e22018-01-02 10:40:00 -0500245 return rect_node;
246}
247
Florin Malita02a32b02018-01-04 11:27:09 -0500248sk_sp<sksg::GeometryNode> AttachPolystarGeometry(const Json::Value& jstar, AttachContext* ctx) {
249 SkASSERT(jstar.isObject());
250
251 static constexpr CompositePolyStar::Type gTypes[] = {
252 CompositePolyStar::Type::kStar, // "sy": 1
253 CompositePolyStar::Type::kPoly, // "sy": 2
254 };
255
256 const auto type = ParseInt(jstar["sy"], 0) - 1;
257 if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gTypes))) {
258 LogFail(jstar, "Unknown polystar type");
259 return nullptr;
260 }
261
262 auto path_node = sksg::Path::Make();
263 auto composite = sk_make_sp<CompositePolyStar>(path_node, gTypes[type]);
264
Florin Malitaf9590922018-01-09 11:56:09 -0500265 BindProperty<VectorValue>(jstar["p"], ctx, composite,
266 [](CompositePolyStar* node, const VectorValue& p) {
267 node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
268 });
269 BindProperty<ScalarValue>(jstar["pt"], ctx, composite,
270 [](CompositePolyStar* node, const ScalarValue& pt) {
271 node->setPointCount(pt);
272 });
273 BindProperty<ScalarValue>(jstar["ir"], ctx, composite,
274 [](CompositePolyStar* node, const ScalarValue& ir) {
275 node->setInnerRadius(ir);
276 });
277 BindProperty<ScalarValue>(jstar["or"], ctx, composite,
278 [](CompositePolyStar* node, const ScalarValue& otr) {
Florin Malita9661b982018-01-06 14:25:49 -0500279 node->setOuterRadius(otr);
280 });
Florin Malitaf9590922018-01-09 11:56:09 -0500281 BindProperty<ScalarValue>(jstar["is"], ctx, composite,
282 [](CompositePolyStar* node, const ScalarValue& is) {
Florin Malita9661b982018-01-06 14:25:49 -0500283 node->setInnerRoundness(is);
284 });
Florin Malitaf9590922018-01-09 11:56:09 -0500285 BindProperty<ScalarValue>(jstar["os"], ctx, composite,
286 [](CompositePolyStar* node, const ScalarValue& os) {
Florin Malita9661b982018-01-06 14:25:49 -0500287 node->setOuterRoundness(os);
288 });
Florin Malitaf9590922018-01-09 11:56:09 -0500289 BindProperty<ScalarValue>(jstar["r"], ctx, composite,
290 [](CompositePolyStar* node, const ScalarValue& r) {
291 node->setRotation(r);
292 });
Florin Malita02a32b02018-01-04 11:27:09 -0500293
294 return path_node;
295}
296
Florin Malita6aaee592018-01-12 12:25:09 -0500297sk_sp<sksg::Color> AttachColor(const Json::Value& obj, AttachContext* ctx) {
Florin Malita094ccde2017-12-30 12:27:00 -0500298 SkASSERT(obj.isObject());
299
300 auto color_node = sksg::Color::Make(SK_ColorBLACK);
Florin Malita1586d852018-01-12 14:27:39 -0500301 auto color_attached = BindProperty<VectorValue>(obj["c"], ctx, color_node,
302 [](sksg::Color* node, const VectorValue& c) {
Florin Malitaf9590922018-01-09 11:56:09 -0500303 node->setColor(ValueTraits<VectorValue>::As<SkColor>(c));
304 });
Florin Malita094ccde2017-12-30 12:27:00 -0500305
Florin Malita1586d852018-01-12 14:27:39 -0500306 return color_attached ? color_node : nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500307}
308
Florin Malita6aaee592018-01-12 12:25:09 -0500309sk_sp<sksg::Gradient> AttachGradient(const Json::Value& obj, AttachContext* ctx) {
310 SkASSERT(obj.isObject());
Florin Malita094ccde2017-12-30 12:27:00 -0500311
Florin Malita6aaee592018-01-12 12:25:09 -0500312 const auto& stops = obj["g"];
313 if (!stops.isObject())
Florin Malita094ccde2017-12-30 12:27:00 -0500314 return nullptr;
315
Florin Malita6aaee592018-01-12 12:25:09 -0500316 const auto stopCount = ParseInt(stops["p"], -1);
317 if (stopCount < 0)
318 return nullptr;
319
320 sk_sp<sksg::Gradient> gradient_node;
321 sk_sp<CompositeGradient> composite;
322
323 if (ParseInt(obj["t"], 1) == 1) {
324 auto linear_node = sksg::LinearGradient::Make();
325 composite = sk_make_sp<CompositeLinearGradient>(linear_node, stopCount);
326 gradient_node = std::move(linear_node);
327 } else {
328 auto radial_node = sksg::RadialGradient::Make();
329 composite = sk_make_sp<CompositeRadialGradient>(radial_node, stopCount);
330
331 // TODO: highlight, angle
332 gradient_node = std::move(radial_node);
333 }
334
335 BindProperty<VectorValue>(stops["k"], ctx, composite,
336 [](CompositeGradient* node, const VectorValue& stops) {
337 node->setColorStops(stops);
338 });
339 BindProperty<VectorValue>(obj["s"], ctx, composite,
340 [](CompositeGradient* node, const VectorValue& s) {
341 node->setStartPoint(ValueTraits<VectorValue>::As<SkPoint>(s));
342 });
343 BindProperty<VectorValue>(obj["e"], ctx, composite,
344 [](CompositeGradient* node, const VectorValue& e) {
345 node->setEndPoint(ValueTraits<VectorValue>::As<SkPoint>(e));
346 });
347
348 return gradient_node;
349}
350
Florin Malita1586d852018-01-12 14:27:39 -0500351sk_sp<sksg::PaintNode> AttachPaint(const Json::Value& jpaint, AttachContext* ctx,
Florin Malita6aaee592018-01-12 12:25:09 -0500352 sk_sp<sksg::PaintNode> paint_node) {
353 if (paint_node) {
354 paint_node->setAntiAlias(true);
355
Florin Malita1586d852018-01-12 14:27:39 -0500356 BindProperty<ScalarValue>(jpaint["o"], ctx, paint_node,
357 [](sksg::PaintNode* node, const ScalarValue& o) {
358 // BM opacity is [0..100]
359 node->setOpacity(o * 0.01f);
360 });
Florin Malita6aaee592018-01-12 12:25:09 -0500361 }
362
363 return paint_node;
364}
365
366sk_sp<sksg::PaintNode> AttachStroke(const Json::Value& jstroke, AttachContext* ctx,
367 sk_sp<sksg::PaintNode> stroke_node) {
368 SkASSERT(jstroke.isObject());
369
370 if (!stroke_node)
371 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500372
373 stroke_node->setStyle(SkPaint::kStroke_Style);
374
Florin Malitaf9590922018-01-09 11:56:09 -0500375 auto width_attached = BindProperty<ScalarValue>(jstroke["w"], ctx, stroke_node,
Florin Malita6aaee592018-01-12 12:25:09 -0500376 [](sksg::PaintNode* node, const ScalarValue& w) {
Florin Malitaf9590922018-01-09 11:56:09 -0500377 node->setStrokeWidth(w);
378 });
Florin Malita094ccde2017-12-30 12:27:00 -0500379 if (!width_attached)
380 return nullptr;
381
382 stroke_node->setStrokeMiter(ParseScalar(jstroke["ml"], 4));
383
384 static constexpr SkPaint::Join gJoins[] = {
385 SkPaint::kMiter_Join,
386 SkPaint::kRound_Join,
387 SkPaint::kBevel_Join,
388 };
389 stroke_node->setStrokeJoin(gJoins[SkTPin<int>(ParseInt(jstroke["lj"], 1) - 1,
390 0, SK_ARRAY_COUNT(gJoins) - 1)]);
391
392 static constexpr SkPaint::Cap gCaps[] = {
393 SkPaint::kButt_Cap,
394 SkPaint::kRound_Cap,
395 SkPaint::kSquare_Cap,
396 };
397 stroke_node->setStrokeCap(gCaps[SkTPin<int>(ParseInt(jstroke["lc"], 1) - 1,
398 0, SK_ARRAY_COUNT(gCaps) - 1)]);
399
400 return stroke_node;
401}
402
Florin Malita6aaee592018-01-12 12:25:09 -0500403sk_sp<sksg::PaintNode> AttachColorFill(const Json::Value& jfill, AttachContext* ctx) {
404 SkASSERT(jfill.isObject());
405
406 return AttachPaint(jfill, ctx, AttachColor(jfill, ctx));
407}
408
409sk_sp<sksg::PaintNode> AttachGradientFill(const Json::Value& jfill, AttachContext* ctx) {
410 SkASSERT(jfill.isObject());
411
412 return AttachPaint(jfill, ctx, AttachGradient(jfill, ctx));
413}
414
415sk_sp<sksg::PaintNode> AttachColorStroke(const Json::Value& jstroke, AttachContext* ctx) {
416 SkASSERT(jstroke.isObject());
417
418 return AttachStroke(jstroke, ctx, AttachPaint(jstroke, ctx, AttachColor(jstroke, ctx)));
419}
420
421sk_sp<sksg::PaintNode> AttachGradientStroke(const Json::Value& jstroke, AttachContext* ctx) {
422 SkASSERT(jstroke.isObject());
423
424 return AttachStroke(jstroke, ctx, AttachPaint(jstroke, ctx, AttachGradient(jstroke, ctx)));
425}
426
Florin Malitae6345d92018-01-03 23:37:54 -0500427std::vector<sk_sp<sksg::GeometryNode>> AttachMergeGeometryEffect(
428 const Json::Value& jmerge, AttachContext* ctx, std::vector<sk_sp<sksg::GeometryNode>>&& geos) {
429 std::vector<sk_sp<sksg::GeometryNode>> merged;
430
431 static constexpr sksg::Merge::Mode gModes[] = {
432 sksg::Merge::Mode::kMerge, // "mm": 1
433 sksg::Merge::Mode::kUnion, // "mm": 2
434 sksg::Merge::Mode::kDifference, // "mm": 3
435 sksg::Merge::Mode::kIntersect, // "mm": 4
436 sksg::Merge::Mode::kXOR , // "mm": 5
437 };
438
Florin Malita51b8c892018-01-07 08:54:24 -0500439 const auto mode = gModes[SkTPin<int>(ParseInt(jmerge["mm"], 1) - 1,
440 0, SK_ARRAY_COUNT(gModes) - 1)];
Florin Malitae6345d92018-01-03 23:37:54 -0500441 merged.push_back(sksg::Merge::Make(std::move(geos), mode));
442
443 LOG("** Attached merge path effect, mode: %d\n", mode);
444
445 return merged;
446}
447
Florin Malita51b8c892018-01-07 08:54:24 -0500448std::vector<sk_sp<sksg::GeometryNode>> AttachTrimGeometryEffect(
449 const Json::Value& jtrim, AttachContext* ctx, std::vector<sk_sp<sksg::GeometryNode>>&& geos) {
450
451 enum class Mode {
452 kMerged, // "m": 1
453 kSeparate, // "m": 2
454 } gModes[] = { Mode::kMerged, Mode::kSeparate };
455
456 const auto mode = gModes[SkTPin<int>(ParseInt(jtrim["m"], 1) - 1,
457 0, SK_ARRAY_COUNT(gModes) - 1)];
458
459 std::vector<sk_sp<sksg::GeometryNode>> inputs;
460 if (mode == Mode::kMerged) {
461 inputs.push_back(sksg::Merge::Make(std::move(geos), sksg::Merge::Mode::kMerge));
462 } else {
463 inputs = std::move(geos);
464 }
465
466 std::vector<sk_sp<sksg::GeometryNode>> trimmed;
467 trimmed.reserve(inputs.size());
468 for (const auto& i : inputs) {
469 const auto trim = sksg::TrimEffect::Make(i);
470 trimmed.push_back(trim);
Florin Malitaf9590922018-01-09 11:56:09 -0500471 BindProperty<ScalarValue>(jtrim["s"], ctx, trim,
472 [](sksg::TrimEffect* node, const ScalarValue& s) {
Florin Malita51b8c892018-01-07 08:54:24 -0500473 node->setStart(s * 0.01f);
474 });
Florin Malitaf9590922018-01-09 11:56:09 -0500475 BindProperty<ScalarValue>(jtrim["e"], ctx, trim,
476 [](sksg::TrimEffect* node, const ScalarValue& e) {
Florin Malita51b8c892018-01-07 08:54:24 -0500477 node->setEnd(e * 0.01f);
478 });
479 // TODO: "offset" doesn't currently work the same as BM - figure out what's going on.
Florin Malitaf9590922018-01-09 11:56:09 -0500480 BindProperty<ScalarValue>(jtrim["o"], ctx, trim,
481 [](sksg::TrimEffect* node, const ScalarValue& o) {
Florin Malita51b8c892018-01-07 08:54:24 -0500482 node->setOffset(o * 0.01f);
483 });
484 }
485
486 return trimmed;
487}
488
Florin Malita094ccde2017-12-30 12:27:00 -0500489using GeometryAttacherT = sk_sp<sksg::GeometryNode> (*)(const Json::Value&, AttachContext*);
490static constexpr GeometryAttacherT gGeometryAttachers[] = {
491 AttachPathGeometry,
Florin Malita2e1d7e22018-01-02 10:40:00 -0500492 AttachRRectGeometry,
Florin Malitafbc13f12018-01-04 10:26:35 -0500493 AttachEllipseGeometry,
Florin Malita02a32b02018-01-04 11:27:09 -0500494 AttachPolystarGeometry,
Florin Malita094ccde2017-12-30 12:27:00 -0500495};
496
497using PaintAttacherT = sk_sp<sksg::PaintNode> (*)(const Json::Value&, AttachContext*);
498static constexpr PaintAttacherT gPaintAttachers[] = {
Florin Malita6aaee592018-01-12 12:25:09 -0500499 AttachColorFill,
500 AttachColorStroke,
501 AttachGradientFill,
502 AttachGradientStroke,
Florin Malita094ccde2017-12-30 12:27:00 -0500503};
504
505using GroupAttacherT = sk_sp<sksg::RenderNode> (*)(const Json::Value&, AttachContext*);
506static constexpr GroupAttacherT gGroupAttachers[] = {
507 AttachShapeGroup,
508};
509
Florin Malitae6345d92018-01-03 23:37:54 -0500510using GeometryEffectAttacherT =
511 std::vector<sk_sp<sksg::GeometryNode>> (*)(const Json::Value&,
512 AttachContext*,
513 std::vector<sk_sp<sksg::GeometryNode>>&&);
514static constexpr GeometryEffectAttacherT gGeometryEffectAttachers[] = {
515 AttachMergeGeometryEffect,
Florin Malita51b8c892018-01-07 08:54:24 -0500516 AttachTrimGeometryEffect,
Florin Malitae6345d92018-01-03 23:37:54 -0500517};
518
Florin Malita094ccde2017-12-30 12:27:00 -0500519enum class ShapeType {
520 kGeometry,
Florin Malitae6345d92018-01-03 23:37:54 -0500521 kGeometryEffect,
Florin Malita094ccde2017-12-30 12:27:00 -0500522 kPaint,
523 kGroup,
Florin Malitadacc02b2017-12-31 09:12:31 -0500524 kTransform,
Florin Malita094ccde2017-12-30 12:27:00 -0500525};
526
527struct ShapeInfo {
528 const char* fTypeString;
529 ShapeType fShapeType;
530 uint32_t fAttacherIndex; // index into respective attacher tables
531};
532
533const ShapeInfo* FindShapeInfo(const Json::Value& shape) {
534 static constexpr ShapeInfo gShapeInfo[] = {
Florin Malitafbc13f12018-01-04 10:26:35 -0500535 { "el", ShapeType::kGeometry , 2 }, // ellipse -> AttachEllipseGeometry
Florin Malita6aaee592018-01-12 12:25:09 -0500536 { "fl", ShapeType::kPaint , 0 }, // fill -> AttachColorFill
537 { "gf", ShapeType::kPaint , 2 }, // gfill -> AttachGradientFill
Florin Malitae6345d92018-01-03 23:37:54 -0500538 { "gr", ShapeType::kGroup , 0 }, // group -> AttachShapeGroup
Florin Malita6aaee592018-01-12 12:25:09 -0500539 { "gs", ShapeType::kPaint , 3 }, // gstroke -> AttachGradientStroke
Florin Malitae6345d92018-01-03 23:37:54 -0500540 { "mm", ShapeType::kGeometryEffect, 0 }, // merge -> AttachMergeGeometryEffect
Florin Malita02a32b02018-01-04 11:27:09 -0500541 { "rc", ShapeType::kGeometry , 1 }, // rrect -> AttachRRectGeometry
Florin Malitae6345d92018-01-03 23:37:54 -0500542 { "sh", ShapeType::kGeometry , 0 }, // shape -> AttachPathGeometry
Florin Malita02a32b02018-01-04 11:27:09 -0500543 { "sr", ShapeType::kGeometry , 3 }, // polystar -> AttachPolyStarGeometry
Florin Malita6aaee592018-01-12 12:25:09 -0500544 { "st", ShapeType::kPaint , 1 }, // stroke -> AttachColorStroke
Florin Malita51b8c892018-01-07 08:54:24 -0500545 { "tm", ShapeType::kGeometryEffect, 1 }, // trim -> AttachTrimGeometryEffect
Florin Malita18eafd92018-01-04 21:11:55 -0500546 { "tr", ShapeType::kTransform , 0 }, // transform -> In-place handler
Florin Malita094ccde2017-12-30 12:27:00 -0500547 };
548
549 if (!shape.isObject())
550 return nullptr;
551
552 const auto& type = shape["ty"];
553 if (!type.isString())
554 return nullptr;
555
556 const auto* info = bsearch(type.asCString(),
557 gShapeInfo,
558 SK_ARRAY_COUNT(gShapeInfo),
559 sizeof(ShapeInfo),
560 [](const void* key, const void* info) {
561 return strcmp(static_cast<const char*>(key),
562 static_cast<const ShapeInfo*>(info)->fTypeString);
563 });
564
565 return static_cast<const ShapeInfo*>(info);
566}
567
568sk_sp<sksg::RenderNode> AttachShape(const Json::Value& shapeArray, AttachContext* ctx) {
569 if (!shapeArray.isArray())
570 return nullptr;
571
Florin Malita2a8275b2018-01-02 12:52:43 -0500572 // (https://helpx.adobe.com/after-effects/using/overview-shape-layers-paths-vector.html#groups_and_render_order_for_shapes_and_shape_attributes)
573 //
574 // Render order for shapes within a shape layer
575 //
576 // The rules for rendering a shape layer are similar to the rules for rendering a composition
577 // that contains nested compositions:
578 //
579 // * Within a group, the shape at the bottom of the Timeline panel stacking order is rendered
580 // first.
581 //
582 // * All path operations within a group are performed before paint operations. This means,
583 // for example, that the stroke follows the distortions in the path made by the Wiggle Paths
584 // path operation. Path operations within a group are performed from top to bottom.
585 //
586 // * Paint operations within a group are performed from the bottom to the top in the Timeline
587 // panel stacking order. This means, for example, that a stroke is rendered on top of
588 // (in front of) a stroke that appears after it in the Timeline panel.
589 //
Florin Malitadacc02b2017-12-31 09:12:31 -0500590 sk_sp<sksg::Group> shape_group = sksg::Group::Make();
591 sk_sp<sksg::RenderNode> xformed_group = shape_group;
Florin Malita094ccde2017-12-30 12:27:00 -0500592
Florin Malitae6345d92018-01-03 23:37:54 -0500593 std::vector<sk_sp<sksg::GeometryNode>> geos;
594 std::vector<sk_sp<sksg::RenderNode>> draws;
Florin Malita094ccde2017-12-30 12:27:00 -0500595
596 for (const auto& s : shapeArray) {
597 const auto* info = FindShapeInfo(s);
598 if (!info) {
599 LogFail(s.isObject() ? s["ty"] : s, "Unknown shape");
600 continue;
601 }
602
603 switch (info->fShapeType) {
604 case ShapeType::kGeometry: {
605 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGeometryAttachers));
606 if (auto geo = gGeometryAttachers[info->fAttacherIndex](s, ctx)) {
607 geos.push_back(std::move(geo));
608 }
609 } break;
Florin Malitae6345d92018-01-03 23:37:54 -0500610 case ShapeType::kGeometryEffect: {
611 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGeometryEffectAttachers));
612 geos = gGeometryEffectAttachers[info->fAttacherIndex](s, ctx, std::move(geos));
613 } break;
Florin Malita094ccde2017-12-30 12:27:00 -0500614 case ShapeType::kPaint: {
615 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gPaintAttachers));
616 if (auto paint = gPaintAttachers[info->fAttacherIndex](s, ctx)) {
Florin Malita2a8275b2018-01-02 12:52:43 -0500617 for (const auto& geo : geos) {
618 draws.push_back(sksg::Draw::Make(geo, paint));
619 }
Florin Malita094ccde2017-12-30 12:27:00 -0500620 }
621 } break;
622 case ShapeType::kGroup: {
623 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGroupAttachers));
624 if (auto group = gGroupAttachers[info->fAttacherIndex](s, ctx)) {
Florin Malita2a8275b2018-01-02 12:52:43 -0500625 draws.push_back(std::move(group));
Florin Malita094ccde2017-12-30 12:27:00 -0500626 }
627 } break;
Florin Malitadacc02b2017-12-31 09:12:31 -0500628 case ShapeType::kTransform: {
Florin Malita2a8275b2018-01-02 12:52:43 -0500629 // TODO: BM appears to transform the geometry, not the draw op itself.
Florin Malita18eafd92018-01-04 21:11:55 -0500630 if (auto matrix = AttachMatrix(s, ctx, nullptr)) {
631 xformed_group = sksg::Transform::Make(std::move(xformed_group),
632 std::move(matrix));
633 }
Florin Malitac0034172018-01-08 16:42:59 -0500634 xformed_group = AttachOpacity(s, ctx, std::move(xformed_group));
Florin Malitadacc02b2017-12-31 09:12:31 -0500635 } break;
Florin Malita094ccde2017-12-30 12:27:00 -0500636 }
637 }
638
Florin Malita2a8275b2018-01-02 12:52:43 -0500639 if (draws.empty()) {
640 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500641 }
642
Florin Malitae6345d92018-01-03 23:37:54 -0500643 for (auto draw = draws.rbegin(); draw != draws.rend(); ++draw) {
644 shape_group->addChild(std::move(*draw));
Florin Malita2a8275b2018-01-02 12:52:43 -0500645 }
646
Florin Malitae6345d92018-01-03 23:37:54 -0500647 LOG("** Attached shape: %zd draws.\n", draws.size());
Florin Malitadacc02b2017-12-31 09:12:31 -0500648 return xformed_group;
Florin Malita094ccde2017-12-30 12:27:00 -0500649}
650
651sk_sp<sksg::RenderNode> AttachCompLayer(const Json::Value& layer, AttachContext* ctx) {
652 SkASSERT(layer.isObject());
653
654 auto refId = ParseString(layer["refId"], "");
655 if (refId.isEmpty()) {
656 LOG("!! Comp layer missing refId\n");
657 return nullptr;
658 }
659
660 const auto* comp = ctx->fAssets.find(refId);
661 if (!comp) {
662 LOG("!! Pre-comp not found: '%s'\n", refId.c_str());
663 return nullptr;
664 }
665
666 // TODO: cycle detection
667 return AttachComposition(**comp, ctx);
668}
669
Florin Malita0e66fba2018-01-09 17:10:18 -0500670sk_sp<sksg::RenderNode> AttachSolidLayer(const Json::Value& jlayer, AttachContext*) {
671 SkASSERT(jlayer.isObject());
Florin Malita094ccde2017-12-30 12:27:00 -0500672
Florin Malita0e66fba2018-01-09 17:10:18 -0500673 const auto size = SkSize::Make(ParseScalar(jlayer["sw"], -1),
674 ParseScalar(jlayer["sh"], -1));
675 const auto hex = ParseString(jlayer["sc"], "");
676 uint32_t c;
677 if (size.isEmpty() ||
678 !hex.startsWith("#") ||
679 !SkParse::FindHex(hex.c_str() + 1, &c)) {
680 LogFail(jlayer, "Could not parse solid layer");
681 return nullptr;
682 }
683
684 const SkColor color = 0xff000000 | c;
685
686 return sksg::Draw::Make(sksg::Rect::Make(SkRect::MakeSize(size)),
687 sksg::Color::Make(color));
Florin Malita094ccde2017-12-30 12:27:00 -0500688}
689
Florin Malita49328072018-01-08 12:51:12 -0500690sk_sp<sksg::RenderNode> AttachImageAsset(const Json::Value& jimage, AttachContext* ctx) {
691 SkASSERT(jimage.isObject());
692
693 const auto name = ParseString(jimage["p"], ""),
694 path = ParseString(jimage["u"], "");
695 if (name.isEmpty())
696 return nullptr;
697
698 // TODO: plumb resource paths explicitly to ResourceProvider?
699 const auto resName = path.isEmpty() ? name : SkOSPath::Join(path.c_str(), name.c_str());
700 const auto resStream = ctx->fResources.openStream(resName.c_str());
701 if (!resStream || !resStream->hasLength()) {
702 LOG("!! Could not load image resource: %s\n", resName.c_str());
703 return nullptr;
704 }
705
706 // TODO: non-intrisic image sizing
707 return sksg::Image::Make(
708 SkImage::MakeFromEncoded(SkData::MakeFromStream(resStream.get(), resStream->getLength())));
709}
710
711sk_sp<sksg::RenderNode> AttachImageLayer(const Json::Value& layer, AttachContext* ctx) {
Florin Malita094ccde2017-12-30 12:27:00 -0500712 SkASSERT(layer.isObject());
713
Florin Malita49328072018-01-08 12:51:12 -0500714 auto refId = ParseString(layer["refId"], "");
715 if (refId.isEmpty()) {
716 LOG("!! Image layer missing refId\n");
717 return nullptr;
718 }
719
720 const auto* jimage = ctx->fAssets.find(refId);
721 if (!jimage) {
722 LOG("!! Image asset not found: '%s'\n", refId.c_str());
723 return nullptr;
724 }
725
726 return AttachImageAsset(**jimage, ctx);
Florin Malita094ccde2017-12-30 12:27:00 -0500727}
728
729sk_sp<sksg::RenderNode> AttachNullLayer(const Json::Value& layer, AttachContext*) {
730 SkASSERT(layer.isObject());
731
Florin Malita18eafd92018-01-04 21:11:55 -0500732 // Null layers are used solely to drive dependent transforms,
733 // but we use free-floating sksg::Matrices for that purpose.
Florin Malita094ccde2017-12-30 12:27:00 -0500734 return nullptr;
735}
736
737sk_sp<sksg::RenderNode> AttachShapeLayer(const Json::Value& layer, AttachContext* ctx) {
738 SkASSERT(layer.isObject());
739
740 LOG("** Attaching shape layer ind: %d\n", ParseInt(layer["ind"], 0));
741
742 return AttachShape(layer["shapes"], ctx);
743}
744
745sk_sp<sksg::RenderNode> AttachTextLayer(const Json::Value& layer, AttachContext*) {
746 SkASSERT(layer.isObject());
747
748 LOG("?? Text layer stub\n");
749 return nullptr;
750}
751
Florin Malita18eafd92018-01-04 21:11:55 -0500752struct AttachLayerContext {
753 AttachLayerContext(const Json::Value& jlayers, AttachContext* ctx)
754 : fLayerList(jlayers), fCtx(ctx) {}
755
756 const Json::Value& fLayerList;
757 AttachContext* fCtx;
758 std::unordered_map<const Json::Value*, sk_sp<sksg::Matrix>> fLayerMatrixCache;
759 std::unordered_map<int, const Json::Value*> fLayerIndexCache;
Florin Malita5f9102f2018-01-10 13:36:22 -0500760 sk_sp<sksg::RenderNode> fCurrentMatte;
Florin Malita18eafd92018-01-04 21:11:55 -0500761
762 const Json::Value* findLayer(int index) {
763 SkASSERT(fLayerList.isArray());
764
765 if (index < 0) {
766 return nullptr;
767 }
768
769 const auto cached = fLayerIndexCache.find(index);
770 if (cached != fLayerIndexCache.end()) {
771 return cached->second;
772 }
773
774 for (const auto& l : fLayerList) {
775 if (!l.isObject()) {
776 continue;
777 }
778
779 if (ParseInt(l["ind"], -1) == index) {
780 fLayerIndexCache.insert(std::make_pair(index, &l));
781 return &l;
782 }
783 }
784
785 return nullptr;
786 }
787
788 sk_sp<sksg::Matrix> AttachLayerMatrix(const Json::Value& jlayer) {
789 SkASSERT(jlayer.isObject());
790
791 const auto cached = fLayerMatrixCache.find(&jlayer);
792 if (cached != fLayerMatrixCache.end()) {
793 return cached->second;
794 }
795
796 const auto* parentLayer = this->findLayer(ParseInt(jlayer["parent"], -1));
797
798 // TODO: cycle detection?
799 auto parentMatrix = (parentLayer && parentLayer != &jlayer)
800 ? this->AttachLayerMatrix(*parentLayer) : nullptr;
801
802 auto layerMatrix = AttachMatrix(jlayer["ks"], fCtx, std::move(parentMatrix));
803 fLayerMatrixCache.insert(std::make_pair(&jlayer, layerMatrix));
804
805 return layerMatrix;
806 }
807};
808
809sk_sp<sksg::RenderNode> AttachLayer(const Json::Value& jlayer,
810 AttachLayerContext* layerCtx) {
811 if (!jlayer.isObject())
Florin Malita094ccde2017-12-30 12:27:00 -0500812 return nullptr;
813
814 using LayerAttacher = sk_sp<sksg::RenderNode> (*)(const Json::Value&, AttachContext*);
815 static constexpr LayerAttacher gLayerAttachers[] = {
816 AttachCompLayer, // 'ty': 0
817 AttachSolidLayer, // 'ty': 1
818 AttachImageLayer, // 'ty': 2
819 AttachNullLayer, // 'ty': 3
820 AttachShapeLayer, // 'ty': 4
821 AttachTextLayer, // 'ty': 5
822 };
823
Florin Malita18eafd92018-01-04 21:11:55 -0500824 int type = ParseInt(jlayer["ty"], -1);
Florin Malita094ccde2017-12-30 12:27:00 -0500825 if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gLayerAttachers))) {
826 return nullptr;
827 }
828
Florin Malita71cba8f2018-01-09 08:07:14 -0500829 // Layer content.
830 auto layer = gLayerAttachers[type](jlayer, layerCtx->fCtx);
831 if (auto layerMatrix = layerCtx->AttachLayerMatrix(jlayer)) {
832 // Optional layer transform.
833 layer = sksg::Transform::Make(std::move(layer), std::move(layerMatrix));
834 }
835 // Optional layer opacity.
836 layer = AttachOpacity(jlayer["ks"], layerCtx->fCtx, std::move(layer));
Florin Malita18eafd92018-01-04 21:11:55 -0500837
Florin Malita71cba8f2018-01-09 08:07:14 -0500838 // TODO: we should also disable related/inactive animators.
839 class Activator final : public AnimatorBase {
840 public:
841 Activator(sk_sp<sksg::OpacityEffect> controlNode, float in, float out)
842 : fControlNode(std::move(controlNode))
843 , fIn(in)
844 , fOut(out) {}
845
Florin Malitaa6dd7522018-01-09 08:46:52 -0500846 void tick(float t) override {
Florin Malita71cba8f2018-01-09 08:07:14 -0500847 // Keep the layer fully transparent except for its [in..out] lifespan.
848 // (note: opacity == 0 disables rendering, while opacity == 1 is a noop)
849 fControlNode->setOpacity(t >= fIn && t <= fOut ? 1 : 0);
850 }
851
852 private:
853 const sk_sp<sksg::OpacityEffect> fControlNode;
854 const float fIn,
855 fOut;
856 };
857
858 auto layerControl = sksg::OpacityEffect::Make(std::move(layer));
859 const auto in = ParseScalar(jlayer["ip"], 0),
860 out = ParseScalar(jlayer["op"], in);
861
862 if (in >= out || ! layerControl)
863 return nullptr;
864
865 layerCtx->fCtx->fAnimators.push_back(skstd::make_unique<Activator>(layerControl, in, out));
866
Florin Malita5f9102f2018-01-10 13:36:22 -0500867 if (ParseBool(jlayer["td"], false)) {
868 // This layer is a matte. We apply it as a mask to the next layer.
869 layerCtx->fCurrentMatte = std::move(layerControl);
870 return nullptr;
871 }
872
873 if (layerCtx->fCurrentMatte) {
874 // There is a pending matte. Apply and reset.
875 return sksg::MaskEffect::Make(std::move(layerControl), std::move(layerCtx->fCurrentMatte));
876 }
877
Florin Malita71cba8f2018-01-09 08:07:14 -0500878 return layerControl;
Florin Malita094ccde2017-12-30 12:27:00 -0500879}
880
881sk_sp<sksg::RenderNode> AttachComposition(const Json::Value& comp, AttachContext* ctx) {
882 if (!comp.isObject())
883 return nullptr;
884
Florin Malita18eafd92018-01-04 21:11:55 -0500885 const auto& jlayers = comp["layers"];
886 if (!jlayers.isArray())
887 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500888
Florin Malita18eafd92018-01-04 21:11:55 -0500889 SkSTArray<16, sk_sp<sksg::RenderNode>, true> layers;
890 AttachLayerContext layerCtx(jlayers, ctx);
891
892 for (const auto& l : jlayers) {
893 if (auto layer_fragment = AttachLayer(l, &layerCtx)) {
Florin Malita2a8275b2018-01-02 12:52:43 -0500894 layers.push_back(std::move(layer_fragment));
Florin Malita094ccde2017-12-30 12:27:00 -0500895 }
896 }
897
Florin Malita2a8275b2018-01-02 12:52:43 -0500898 if (layers.empty()) {
899 return nullptr;
900 }
901
902 // Layers are painted in bottom->top order.
903 auto comp_group = sksg::Group::Make();
904 for (int i = layers.count() - 1; i >= 0; --i) {
905 comp_group->addChild(std::move(layers[i]));
906 }
907
908 LOG("** Attached composition '%s': %d layers.\n",
909 ParseString(comp["id"], "").c_str(), layers.count());
910
Florin Malita094ccde2017-12-30 12:27:00 -0500911 return comp_group;
912}
913
914} // namespace
915
Florin Malita49328072018-01-08 12:51:12 -0500916std::unique_ptr<Animation> Animation::Make(SkStream* stream, const ResourceProvider& res) {
Florin Malita094ccde2017-12-30 12:27:00 -0500917 if (!stream->hasLength()) {
918 // TODO: handle explicit buffering?
919 LOG("!! cannot parse streaming content\n");
920 return nullptr;
921 }
922
923 Json::Value json;
924 {
925 auto data = SkData::MakeFromStream(stream, stream->getLength());
926 if (!data) {
927 LOG("!! could not read stream\n");
928 return nullptr;
929 }
930
931 Json::Reader reader;
932
933 auto dataStart = static_cast<const char*>(data->data());
934 if (!reader.parse(dataStart, dataStart + data->size(), json, false) || !json.isObject()) {
935 LOG("!! failed to parse json: %s\n", reader.getFormattedErrorMessages().c_str());
936 return nullptr;
937 }
938 }
939
940 const auto version = ParseString(json["v"], "");
941 const auto size = SkSize::Make(ParseScalar(json["w"], -1), ParseScalar(json["h"], -1));
942 const auto fps = ParseScalar(json["fr"], -1);
943
944 if (size.isEmpty() || version.isEmpty() || fps < 0) {
945 LOG("!! invalid animation params (version: %s, size: [%f %f], frame rate: %f)",
946 version.c_str(), size.width(), size.height(), fps);
947 return nullptr;
948 }
949
Florin Malita49328072018-01-08 12:51:12 -0500950 return std::unique_ptr<Animation>(new Animation(res, std::move(version), size, fps, json));
Florin Malita094ccde2017-12-30 12:27:00 -0500951}
952
Florin Malita49328072018-01-08 12:51:12 -0500953std::unique_ptr<Animation> Animation::MakeFromFile(const char path[], const ResourceProvider* res) {
954 class DirectoryResourceProvider final : public ResourceProvider {
955 public:
956 explicit DirectoryResourceProvider(SkString dir) : fDir(std::move(dir)) {}
957
958 std::unique_ptr<SkStream> openStream(const char resource[]) const override {
959 const auto resPath = SkOSPath::Join(fDir.c_str(), resource);
960 return SkStream::MakeFromFile(resPath.c_str());
961 }
962
963 private:
964 const SkString fDir;
965 };
966
967 const auto jsonStream = SkStream::MakeFromFile(path);
968 if (!jsonStream)
969 return nullptr;
970
971 std::unique_ptr<ResourceProvider> defaultProvider;
972 if (!res) {
973 defaultProvider = skstd::make_unique<DirectoryResourceProvider>(SkOSPath::Dirname(path));
974 }
975
976 return Make(jsonStream.get(), res ? *res : *defaultProvider);
977}
978
979Animation::Animation(const ResourceProvider& resources,
980 SkString version, const SkSize& size, SkScalar fps, const Json::Value& json)
Florin Malita094ccde2017-12-30 12:27:00 -0500981 : fVersion(std::move(version))
982 , fSize(size)
983 , fFrameRate(fps)
984 , fInPoint(ParseScalar(json["ip"], 0))
985 , fOutPoint(SkTMax(ParseScalar(json["op"], SK_ScalarMax), fInPoint)) {
986
987 AssetMap assets;
988 for (const auto& asset : json["assets"]) {
989 if (!asset.isObject()) {
990 continue;
991 }
992
993 assets.set(ParseString(asset["id"], ""), &asset);
994 }
995
Florin Malita49328072018-01-08 12:51:12 -0500996 AttachContext ctx = { resources, assets, fAnimators };
Florin Malita094ccde2017-12-30 12:27:00 -0500997 fDom = AttachComposition(json, &ctx);
998
Florin Malitadb385732018-01-09 12:19:32 -0500999 // In case the client calls render before the first tick.
1000 this->animationTick(0);
1001
Florin Malita094ccde2017-12-30 12:27:00 -05001002 LOG("** Attached %d animators\n", fAnimators.count());
1003}
1004
1005Animation::~Animation() = default;
1006
Mike Reed29859872018-01-08 08:25:27 -05001007void Animation::render(SkCanvas* canvas, const SkRect* dstR) const {
Florin Malita094ccde2017-12-30 12:27:00 -05001008 if (!fDom)
1009 return;
1010
1011 sksg::InvalidationController ic;
1012 fDom->revalidate(&ic, SkMatrix::I());
1013
1014 // TODO: proper inval
Mike Reed29859872018-01-08 08:25:27 -05001015 SkAutoCanvasRestore restore(canvas, true);
1016 const SkRect srcR = SkRect::MakeSize(this->size());
1017 if (dstR) {
1018 canvas->concat(SkMatrix::MakeRectToRect(srcR, *dstR, SkMatrix::kCenter_ScaleToFit));
1019 }
1020 canvas->clipRect(srcR);
Florin Malita094ccde2017-12-30 12:27:00 -05001021 fDom->render(canvas);
1022
1023 if (!fShowInval)
1024 return;
1025
1026 SkPaint fill, stroke;
1027 fill.setAntiAlias(true);
1028 fill.setColor(0x40ff0000);
1029 stroke.setAntiAlias(true);
1030 stroke.setColor(0xffff0000);
1031 stroke.setStyle(SkPaint::kStroke_Style);
1032
1033 for (const auto& r : ic) {
1034 canvas->drawRect(r, fill);
1035 canvas->drawRect(r, stroke);
1036 }
1037}
1038
1039void Animation::animationTick(SkMSec ms) {
1040 // 't' in the BM model really means 'frame #'
1041 auto t = static_cast<float>(ms) * fFrameRate / 1000;
1042
1043 t = fInPoint + std::fmod(t, fOutPoint - fInPoint);
1044
1045 // TODO: this can be optimized quite a bit with some sorting/state tracking.
1046 for (const auto& a : fAnimators) {
1047 a->tick(t);
1048 }
1049}
1050
Florin Malita54f65c42018-01-16 17:04:30 -05001051} // namespace skottie