blob: 7a08cec9dfaa901ae83e3eae46a0f8fea0c4d3ab [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 Malita16d0ad02018-01-19 15:07:29 -050024#include "SkSGGeometryTransform.h"
Florin Malita6aaee592018-01-12 12:25:09 -050025#include "SkSGGradient.h"
Florin Malita094ccde2017-12-30 12:27:00 -050026#include "SkSGGroup.h"
Florin Malita49328072018-01-08 12:51:12 -050027#include "SkSGImage.h"
28#include "SkSGInvalidationController.h"
Florin Malita5f9102f2018-01-10 13:36:22 -050029#include "SkSGMaskEffect.h"
Florin Malitae6345d92018-01-03 23:37:54 -050030#include "SkSGMerge.h"
Florin Malitac0034172018-01-08 16:42:59 -050031#include "SkSGOpacityEffect.h"
Florin Malita094ccde2017-12-30 12:27:00 -050032#include "SkSGPath.h"
Florin Malita2e1d7e22018-01-02 10:40:00 -050033#include "SkSGRect.h"
Florin Malita094ccde2017-12-30 12:27:00 -050034#include "SkSGTransform.h"
Florin Malita51b8c892018-01-07 08:54:24 -050035#include "SkSGTrimEffect.h"
Florin Malita094ccde2017-12-30 12:27:00 -050036#include "SkStream.h"
37#include "SkTArray.h"
38#include "SkTHash.h"
39
40#include <cmath>
Florin Malita18eafd92018-01-04 21:11:55 -050041#include <unordered_map>
Florin Malitae6345d92018-01-03 23:37:54 -050042#include <vector>
43
Florin Malita094ccde2017-12-30 12:27:00 -050044#include "stdlib.h"
45
Florin Malita54f65c42018-01-16 17:04:30 -050046namespace skottie {
Florin Malita094ccde2017-12-30 12:27:00 -050047
48namespace {
49
50using AssetMap = SkTHashMap<SkString, const Json::Value*>;
51
52struct AttachContext {
Florin Malita49328072018-01-08 12:51:12 -050053 const ResourceProvider& fResources;
Florin Malita094ccde2017-12-30 12:27:00 -050054 const AssetMap& fAssets;
55 SkTArray<std::unique_ptr<AnimatorBase>>& fAnimators;
56};
57
58bool LogFail(const Json::Value& json, const char* msg) {
59 const auto dump = json.toStyledString();
60 LOG("!! %s: %s", msg, dump.c_str());
61 return false;
62}
63
64// This is the workhorse for binding properties: depending on whether the property is animated,
65// it will either apply immediately or instantiate and attach a keyframe animator.
Florin Malitaf9590922018-01-09 11:56:09 -050066template <typename ValT, typename NodeT>
67bool BindProperty(const Json::Value& jprop, AttachContext* ctx, const sk_sp<NodeT>& node,
68 typename Animator<ValT, NodeT>::ApplyFuncT&& apply) {
Florin Malita094ccde2017-12-30 12:27:00 -050069 if (!jprop.isObject())
70 return false;
71
Florin Malita95448a92018-01-08 10:15:12 -050072 const auto& jpropA = jprop["a"];
73 const auto& jpropK = jprop["k"];
74
75 // Older Json versions don't have an "a" animation marker.
76 // For those, we attempt to parse both ways.
77 if (jpropA.isNull() || !ParseBool(jpropA, "false")) {
Florin Malitaf9590922018-01-09 11:56:09 -050078 ValT val;
79 if (ValueTraits<ValT>::Parse(jpropK, &val)) {
Florin Malita95448a92018-01-08 10:15:12 -050080 // Static property.
Florin Malitaf9590922018-01-09 11:56:09 -050081 apply(node.get(), val);
Florin Malita95448a92018-01-08 10:15:12 -050082 return true;
Florin Malita094ccde2017-12-30 12:27:00 -050083 }
84
Florin Malita95448a92018-01-08 10:15:12 -050085 if (!jpropA.isNull()) {
86 return LogFail(jprop, "Could not parse (explicit) static property");
Florin Malita094ccde2017-12-30 12:27:00 -050087 }
Florin Malita094ccde2017-12-30 12:27:00 -050088 }
89
Florin Malita95448a92018-01-08 10:15:12 -050090 // Keyframe property.
Florin Malitaf9590922018-01-09 11:56:09 -050091 using AnimatorT = Animator<ValT, NodeT>;
92 auto animator = AnimatorT::Make(ParseFrames<ValT>(jpropK), node, std::move(apply));
Florin Malita95448a92018-01-08 10:15:12 -050093
94 if (!animator) {
95 return LogFail(jprop, "Could not parse keyframed property");
96 }
97
98 ctx->fAnimators.push_back(std::move(animator));
99
Florin Malita094ccde2017-12-30 12:27:00 -0500100 return true;
101}
102
Florin Malita18eafd92018-01-04 21:11:55 -0500103sk_sp<sksg::Matrix> AttachMatrix(const Json::Value& t, AttachContext* ctx,
104 sk_sp<sksg::Matrix> parentMatrix) {
105 if (!t.isObject())
106 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500107
Florin Malita18eafd92018-01-04 21:11:55 -0500108 auto matrix = sksg::Matrix::Make(SkMatrix::I(), std::move(parentMatrix));
109 auto composite = sk_make_sp<CompositeTransform>(matrix);
Florin Malitaf9590922018-01-09 11:56:09 -0500110 auto anchor_attached = BindProperty<VectorValue>(t["a"], ctx, composite,
111 [](CompositeTransform* node, const VectorValue& a) {
112 node->setAnchorPoint(ValueTraits<VectorValue>::As<SkPoint>(a));
Florin Malita094ccde2017-12-30 12:27:00 -0500113 });
Florin Malitaf9590922018-01-09 11:56:09 -0500114 auto position_attached = BindProperty<VectorValue>(t["p"], ctx, composite,
115 [](CompositeTransform* node, const VectorValue& p) {
116 node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
Florin Malita094ccde2017-12-30 12:27:00 -0500117 });
Florin Malitaf9590922018-01-09 11:56:09 -0500118 auto scale_attached = BindProperty<VectorValue>(t["s"], ctx, composite,
119 [](CompositeTransform* node, const VectorValue& s) {
120 node->setScale(ValueTraits<VectorValue>::As<SkVector>(s));
Florin Malita094ccde2017-12-30 12:27:00 -0500121 });
Florin Malitaf9590922018-01-09 11:56:09 -0500122 auto rotation_attached = BindProperty<ScalarValue>(t["r"], ctx, composite,
123 [](CompositeTransform* node, const ScalarValue& r) {
Florin Malita094ccde2017-12-30 12:27:00 -0500124 node->setRotation(r);
125 });
Florin Malitaf9590922018-01-09 11:56:09 -0500126 auto skew_attached = BindProperty<ScalarValue>(t["sk"], ctx, composite,
127 [](CompositeTransform* node, const ScalarValue& sk) {
Florin Malita094ccde2017-12-30 12:27:00 -0500128 node->setSkew(sk);
129 });
Florin Malitaf9590922018-01-09 11:56:09 -0500130 auto skewaxis_attached = BindProperty<ScalarValue>(t["sa"], ctx, composite,
131 [](CompositeTransform* node, const ScalarValue& sa) {
Florin Malita094ccde2017-12-30 12:27:00 -0500132 node->setSkewAxis(sa);
133 });
134
135 if (!anchor_attached &&
136 !position_attached &&
137 !scale_attached &&
138 !rotation_attached &&
139 !skew_attached &&
140 !skewaxis_attached) {
141 LogFail(t, "Could not parse transform");
Florin Malita18eafd92018-01-04 21:11:55 -0500142 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500143 }
144
Florin Malita18eafd92018-01-04 21:11:55 -0500145 return matrix;
Florin Malita094ccde2017-12-30 12:27:00 -0500146}
147
Florin Malitac0034172018-01-08 16:42:59 -0500148sk_sp<sksg::RenderNode> AttachOpacity(const Json::Value& jtransform, AttachContext* ctx,
149 sk_sp<sksg::RenderNode> childNode) {
150 if (!jtransform.isObject() || !childNode)
151 return childNode;
152
153 // This is more peeky than other attachers, because we want to avoid redundant opacity
154 // nodes for the extremely common case of static opaciy == 100.
155 const auto& opacity = jtransform["o"];
156 if (opacity.isObject() &&
157 !ParseBool(opacity["a"], true) &&
158 ParseScalar(opacity["k"], -1) == 100) {
159 // Ignoring static full opacity.
160 return childNode;
161 }
162
163 auto opacityNode = sksg::OpacityEffect::Make(childNode);
Florin Malitaf9590922018-01-09 11:56:09 -0500164 BindProperty<ScalarValue>(opacity, ctx, opacityNode,
165 [](sksg::OpacityEffect* node, const ScalarValue& o) {
Florin Malitac0034172018-01-08 16:42:59 -0500166 // BM opacity is [0..100]
167 node->setOpacity(o * 0.01f);
168 });
169
170 return opacityNode;
171}
172
Florin Malita094ccde2017-12-30 12:27:00 -0500173sk_sp<sksg::RenderNode> AttachComposition(const Json::Value&, AttachContext* ctx);
174
Florin Malita094ccde2017-12-30 12:27:00 -0500175sk_sp<sksg::GeometryNode> AttachPathGeometry(const Json::Value& jpath, AttachContext* ctx) {
176 SkASSERT(jpath.isObject());
177
178 auto path_node = sksg::Path::Make();
Florin Malitaf9590922018-01-09 11:56:09 -0500179 auto path_attached = BindProperty<ShapeValue>(jpath["ks"], ctx, path_node,
180 [](sksg::Path* node, const ShapeValue& p) { node->setPath(p); });
Florin Malita094ccde2017-12-30 12:27:00 -0500181
182 if (path_attached)
183 LOG("** Attached path geometry - verbs: %d\n", path_node->getPath().countVerbs());
184
185 return path_attached ? path_node : nullptr;
186}
187
Florin Malita2e1d7e22018-01-02 10:40:00 -0500188sk_sp<sksg::GeometryNode> AttachRRectGeometry(const Json::Value& jrect, AttachContext* ctx) {
189 SkASSERT(jrect.isObject());
190
191 auto rect_node = sksg::RRect::Make();
192 auto composite = sk_make_sp<CompositeRRect>(rect_node);
193
Florin Malitaf9590922018-01-09 11:56:09 -0500194 auto p_attached = BindProperty<VectorValue>(jrect["p"], ctx, composite,
195 [](CompositeRRect* node, const VectorValue& p) {
196 node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
197 });
198 auto s_attached = BindProperty<VectorValue>(jrect["s"], ctx, composite,
199 [](CompositeRRect* node, const VectorValue& s) {
200 node->setSize(ValueTraits<VectorValue>::As<SkSize>(s));
201 });
202 auto r_attached = BindProperty<ScalarValue>(jrect["r"], ctx, composite,
203 [](CompositeRRect* node, const ScalarValue& r) {
204 node->setRadius(SkSize::Make(r, r));
205 });
Florin Malita2e1d7e22018-01-02 10:40:00 -0500206
207 if (!p_attached && !s_attached && !r_attached) {
208 return nullptr;
209 }
210
Florin Malitafbc13f12018-01-04 10:26:35 -0500211 LOG("** Attached (r)rect geometry\n");
212
213 return rect_node;
214}
215
216sk_sp<sksg::GeometryNode> AttachEllipseGeometry(const Json::Value& jellipse, AttachContext* ctx) {
217 SkASSERT(jellipse.isObject());
218
219 auto rect_node = sksg::RRect::Make();
220 auto composite = sk_make_sp<CompositeRRect>(rect_node);
221
Florin Malitaf9590922018-01-09 11:56:09 -0500222 auto p_attached = BindProperty<VectorValue>(jellipse["p"], ctx, composite,
223 [](CompositeRRect* node, const VectorValue& p) {
224 node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
225 });
226 auto s_attached = BindProperty<VectorValue>(jellipse["s"], ctx, composite,
227 [](CompositeRRect* node, const VectorValue& s) {
228 const auto sz = ValueTraits<VectorValue>::As<SkSize>(s);
229 node->setSize(sz);
230 node->setRadius(SkSize::Make(sz.width() / 2, sz.height() / 2));
231 });
Florin Malitafbc13f12018-01-04 10:26:35 -0500232
233 if (!p_attached && !s_attached) {
234 return nullptr;
235 }
236
237 LOG("** Attached ellipse geometry\n");
238
Florin Malita2e1d7e22018-01-02 10:40:00 -0500239 return rect_node;
240}
241
Florin Malita02a32b02018-01-04 11:27:09 -0500242sk_sp<sksg::GeometryNode> AttachPolystarGeometry(const Json::Value& jstar, AttachContext* ctx) {
243 SkASSERT(jstar.isObject());
244
245 static constexpr CompositePolyStar::Type gTypes[] = {
246 CompositePolyStar::Type::kStar, // "sy": 1
247 CompositePolyStar::Type::kPoly, // "sy": 2
248 };
249
250 const auto type = ParseInt(jstar["sy"], 0) - 1;
251 if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gTypes))) {
252 LogFail(jstar, "Unknown polystar type");
253 return nullptr;
254 }
255
256 auto path_node = sksg::Path::Make();
257 auto composite = sk_make_sp<CompositePolyStar>(path_node, gTypes[type]);
258
Florin Malitaf9590922018-01-09 11:56:09 -0500259 BindProperty<VectorValue>(jstar["p"], ctx, composite,
260 [](CompositePolyStar* node, const VectorValue& p) {
261 node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
262 });
263 BindProperty<ScalarValue>(jstar["pt"], ctx, composite,
264 [](CompositePolyStar* node, const ScalarValue& pt) {
265 node->setPointCount(pt);
266 });
267 BindProperty<ScalarValue>(jstar["ir"], ctx, composite,
268 [](CompositePolyStar* node, const ScalarValue& ir) {
269 node->setInnerRadius(ir);
270 });
271 BindProperty<ScalarValue>(jstar["or"], ctx, composite,
272 [](CompositePolyStar* node, const ScalarValue& otr) {
Florin Malita9661b982018-01-06 14:25:49 -0500273 node->setOuterRadius(otr);
274 });
Florin Malitaf9590922018-01-09 11:56:09 -0500275 BindProperty<ScalarValue>(jstar["is"], ctx, composite,
276 [](CompositePolyStar* node, const ScalarValue& is) {
Florin Malita9661b982018-01-06 14:25:49 -0500277 node->setInnerRoundness(is);
278 });
Florin Malitaf9590922018-01-09 11:56:09 -0500279 BindProperty<ScalarValue>(jstar["os"], ctx, composite,
280 [](CompositePolyStar* node, const ScalarValue& os) {
Florin Malita9661b982018-01-06 14:25:49 -0500281 node->setOuterRoundness(os);
282 });
Florin Malitaf9590922018-01-09 11:56:09 -0500283 BindProperty<ScalarValue>(jstar["r"], ctx, composite,
284 [](CompositePolyStar* node, const ScalarValue& r) {
285 node->setRotation(r);
286 });
Florin Malita02a32b02018-01-04 11:27:09 -0500287
288 return path_node;
289}
290
Florin Malita6aaee592018-01-12 12:25:09 -0500291sk_sp<sksg::Color> AttachColor(const Json::Value& obj, AttachContext* ctx) {
Florin Malita094ccde2017-12-30 12:27:00 -0500292 SkASSERT(obj.isObject());
293
294 auto color_node = sksg::Color::Make(SK_ColorBLACK);
Florin Malita1586d852018-01-12 14:27:39 -0500295 auto color_attached = BindProperty<VectorValue>(obj["c"], ctx, color_node,
296 [](sksg::Color* node, const VectorValue& c) {
Florin Malitaf9590922018-01-09 11:56:09 -0500297 node->setColor(ValueTraits<VectorValue>::As<SkColor>(c));
298 });
Florin Malita094ccde2017-12-30 12:27:00 -0500299
Florin Malita1586d852018-01-12 14:27:39 -0500300 return color_attached ? color_node : nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500301}
302
Florin Malita6aaee592018-01-12 12:25:09 -0500303sk_sp<sksg::Gradient> AttachGradient(const Json::Value& obj, AttachContext* ctx) {
304 SkASSERT(obj.isObject());
Florin Malita094ccde2017-12-30 12:27:00 -0500305
Florin Malita6aaee592018-01-12 12:25:09 -0500306 const auto& stops = obj["g"];
307 if (!stops.isObject())
Florin Malita094ccde2017-12-30 12:27:00 -0500308 return nullptr;
309
Florin Malita6aaee592018-01-12 12:25:09 -0500310 const auto stopCount = ParseInt(stops["p"], -1);
311 if (stopCount < 0)
312 return nullptr;
313
314 sk_sp<sksg::Gradient> gradient_node;
315 sk_sp<CompositeGradient> composite;
316
317 if (ParseInt(obj["t"], 1) == 1) {
318 auto linear_node = sksg::LinearGradient::Make();
319 composite = sk_make_sp<CompositeLinearGradient>(linear_node, stopCount);
320 gradient_node = std::move(linear_node);
321 } else {
322 auto radial_node = sksg::RadialGradient::Make();
323 composite = sk_make_sp<CompositeRadialGradient>(radial_node, stopCount);
324
325 // TODO: highlight, angle
326 gradient_node = std::move(radial_node);
327 }
328
329 BindProperty<VectorValue>(stops["k"], ctx, composite,
330 [](CompositeGradient* node, const VectorValue& stops) {
331 node->setColorStops(stops);
332 });
333 BindProperty<VectorValue>(obj["s"], ctx, composite,
334 [](CompositeGradient* node, const VectorValue& s) {
335 node->setStartPoint(ValueTraits<VectorValue>::As<SkPoint>(s));
336 });
337 BindProperty<VectorValue>(obj["e"], ctx, composite,
338 [](CompositeGradient* node, const VectorValue& e) {
339 node->setEndPoint(ValueTraits<VectorValue>::As<SkPoint>(e));
340 });
341
342 return gradient_node;
343}
344
Florin Malita1586d852018-01-12 14:27:39 -0500345sk_sp<sksg::PaintNode> AttachPaint(const Json::Value& jpaint, AttachContext* ctx,
Florin Malita6aaee592018-01-12 12:25:09 -0500346 sk_sp<sksg::PaintNode> paint_node) {
347 if (paint_node) {
348 paint_node->setAntiAlias(true);
349
Florin Malita1586d852018-01-12 14:27:39 -0500350 BindProperty<ScalarValue>(jpaint["o"], ctx, paint_node,
351 [](sksg::PaintNode* node, const ScalarValue& o) {
352 // BM opacity is [0..100]
353 node->setOpacity(o * 0.01f);
354 });
Florin Malita6aaee592018-01-12 12:25:09 -0500355 }
356
357 return paint_node;
358}
359
360sk_sp<sksg::PaintNode> AttachStroke(const Json::Value& jstroke, AttachContext* ctx,
361 sk_sp<sksg::PaintNode> stroke_node) {
362 SkASSERT(jstroke.isObject());
363
364 if (!stroke_node)
365 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500366
367 stroke_node->setStyle(SkPaint::kStroke_Style);
368
Florin Malitaf9590922018-01-09 11:56:09 -0500369 auto width_attached = BindProperty<ScalarValue>(jstroke["w"], ctx, stroke_node,
Florin Malita6aaee592018-01-12 12:25:09 -0500370 [](sksg::PaintNode* node, const ScalarValue& w) {
Florin Malitaf9590922018-01-09 11:56:09 -0500371 node->setStrokeWidth(w);
372 });
Florin Malita094ccde2017-12-30 12:27:00 -0500373 if (!width_attached)
374 return nullptr;
375
376 stroke_node->setStrokeMiter(ParseScalar(jstroke["ml"], 4));
377
378 static constexpr SkPaint::Join gJoins[] = {
379 SkPaint::kMiter_Join,
380 SkPaint::kRound_Join,
381 SkPaint::kBevel_Join,
382 };
383 stroke_node->setStrokeJoin(gJoins[SkTPin<int>(ParseInt(jstroke["lj"], 1) - 1,
384 0, SK_ARRAY_COUNT(gJoins) - 1)]);
385
386 static constexpr SkPaint::Cap gCaps[] = {
387 SkPaint::kButt_Cap,
388 SkPaint::kRound_Cap,
389 SkPaint::kSquare_Cap,
390 };
391 stroke_node->setStrokeCap(gCaps[SkTPin<int>(ParseInt(jstroke["lc"], 1) - 1,
392 0, SK_ARRAY_COUNT(gCaps) - 1)]);
393
394 return stroke_node;
395}
396
Florin Malita6aaee592018-01-12 12:25:09 -0500397sk_sp<sksg::PaintNode> AttachColorFill(const Json::Value& jfill, AttachContext* ctx) {
398 SkASSERT(jfill.isObject());
399
400 return AttachPaint(jfill, ctx, AttachColor(jfill, ctx));
401}
402
403sk_sp<sksg::PaintNode> AttachGradientFill(const Json::Value& jfill, AttachContext* ctx) {
404 SkASSERT(jfill.isObject());
405
406 return AttachPaint(jfill, ctx, AttachGradient(jfill, ctx));
407}
408
409sk_sp<sksg::PaintNode> AttachColorStroke(const Json::Value& jstroke, AttachContext* ctx) {
410 SkASSERT(jstroke.isObject());
411
412 return AttachStroke(jstroke, ctx, AttachPaint(jstroke, ctx, AttachColor(jstroke, ctx)));
413}
414
415sk_sp<sksg::PaintNode> AttachGradientStroke(const Json::Value& jstroke, AttachContext* ctx) {
416 SkASSERT(jstroke.isObject());
417
418 return AttachStroke(jstroke, ctx, AttachPaint(jstroke, ctx, AttachGradient(jstroke, ctx)));
419}
420
Florin Malitae6345d92018-01-03 23:37:54 -0500421std::vector<sk_sp<sksg::GeometryNode>> AttachMergeGeometryEffect(
422 const Json::Value& jmerge, AttachContext* ctx, std::vector<sk_sp<sksg::GeometryNode>>&& geos) {
423 std::vector<sk_sp<sksg::GeometryNode>> merged;
424
425 static constexpr sksg::Merge::Mode gModes[] = {
426 sksg::Merge::Mode::kMerge, // "mm": 1
427 sksg::Merge::Mode::kUnion, // "mm": 2
428 sksg::Merge::Mode::kDifference, // "mm": 3
429 sksg::Merge::Mode::kIntersect, // "mm": 4
430 sksg::Merge::Mode::kXOR , // "mm": 5
431 };
432
Florin Malita51b8c892018-01-07 08:54:24 -0500433 const auto mode = gModes[SkTPin<int>(ParseInt(jmerge["mm"], 1) - 1,
434 0, SK_ARRAY_COUNT(gModes) - 1)];
Florin Malitae6345d92018-01-03 23:37:54 -0500435 merged.push_back(sksg::Merge::Make(std::move(geos), mode));
436
437 LOG("** Attached merge path effect, mode: %d\n", mode);
438
439 return merged;
440}
441
Florin Malita51b8c892018-01-07 08:54:24 -0500442std::vector<sk_sp<sksg::GeometryNode>> AttachTrimGeometryEffect(
443 const Json::Value& jtrim, AttachContext* ctx, std::vector<sk_sp<sksg::GeometryNode>>&& geos) {
444
445 enum class Mode {
446 kMerged, // "m": 1
447 kSeparate, // "m": 2
448 } gModes[] = { Mode::kMerged, Mode::kSeparate };
449
450 const auto mode = gModes[SkTPin<int>(ParseInt(jtrim["m"], 1) - 1,
451 0, SK_ARRAY_COUNT(gModes) - 1)];
452
453 std::vector<sk_sp<sksg::GeometryNode>> inputs;
454 if (mode == Mode::kMerged) {
455 inputs.push_back(sksg::Merge::Make(std::move(geos), sksg::Merge::Mode::kMerge));
456 } else {
457 inputs = std::move(geos);
458 }
459
460 std::vector<sk_sp<sksg::GeometryNode>> trimmed;
461 trimmed.reserve(inputs.size());
462 for (const auto& i : inputs) {
463 const auto trim = sksg::TrimEffect::Make(i);
464 trimmed.push_back(trim);
Florin Malitaf9590922018-01-09 11:56:09 -0500465 BindProperty<ScalarValue>(jtrim["s"], ctx, trim,
466 [](sksg::TrimEffect* node, const ScalarValue& s) {
Florin Malita51b8c892018-01-07 08:54:24 -0500467 node->setStart(s * 0.01f);
468 });
Florin Malitaf9590922018-01-09 11:56:09 -0500469 BindProperty<ScalarValue>(jtrim["e"], ctx, trim,
470 [](sksg::TrimEffect* node, const ScalarValue& e) {
Florin Malita51b8c892018-01-07 08:54:24 -0500471 node->setEnd(e * 0.01f);
472 });
473 // TODO: "offset" doesn't currently work the same as BM - figure out what's going on.
Florin Malitaf9590922018-01-09 11:56:09 -0500474 BindProperty<ScalarValue>(jtrim["o"], ctx, trim,
475 [](sksg::TrimEffect* node, const ScalarValue& o) {
Florin Malita51b8c892018-01-07 08:54:24 -0500476 node->setOffset(o * 0.01f);
477 });
478 }
479
480 return trimmed;
481}
482
Florin Malita094ccde2017-12-30 12:27:00 -0500483using GeometryAttacherT = sk_sp<sksg::GeometryNode> (*)(const Json::Value&, AttachContext*);
484static constexpr GeometryAttacherT gGeometryAttachers[] = {
485 AttachPathGeometry,
Florin Malita2e1d7e22018-01-02 10:40:00 -0500486 AttachRRectGeometry,
Florin Malitafbc13f12018-01-04 10:26:35 -0500487 AttachEllipseGeometry,
Florin Malita02a32b02018-01-04 11:27:09 -0500488 AttachPolystarGeometry,
Florin Malita094ccde2017-12-30 12:27:00 -0500489};
490
491using PaintAttacherT = sk_sp<sksg::PaintNode> (*)(const Json::Value&, AttachContext*);
492static constexpr PaintAttacherT gPaintAttachers[] = {
Florin Malita6aaee592018-01-12 12:25:09 -0500493 AttachColorFill,
494 AttachColorStroke,
495 AttachGradientFill,
496 AttachGradientStroke,
Florin Malita094ccde2017-12-30 12:27:00 -0500497};
498
Florin Malitae6345d92018-01-03 23:37:54 -0500499using GeometryEffectAttacherT =
500 std::vector<sk_sp<sksg::GeometryNode>> (*)(const Json::Value&,
501 AttachContext*,
502 std::vector<sk_sp<sksg::GeometryNode>>&&);
503static constexpr GeometryEffectAttacherT gGeometryEffectAttachers[] = {
504 AttachMergeGeometryEffect,
Florin Malita51b8c892018-01-07 08:54:24 -0500505 AttachTrimGeometryEffect,
Florin Malitae6345d92018-01-03 23:37:54 -0500506};
507
Florin Malita094ccde2017-12-30 12:27:00 -0500508enum class ShapeType {
509 kGeometry,
Florin Malitae6345d92018-01-03 23:37:54 -0500510 kGeometryEffect,
Florin Malita094ccde2017-12-30 12:27:00 -0500511 kPaint,
512 kGroup,
Florin Malitadacc02b2017-12-31 09:12:31 -0500513 kTransform,
Florin Malita094ccde2017-12-30 12:27:00 -0500514};
515
516struct ShapeInfo {
517 const char* fTypeString;
518 ShapeType fShapeType;
519 uint32_t fAttacherIndex; // index into respective attacher tables
520};
521
522const ShapeInfo* FindShapeInfo(const Json::Value& shape) {
523 static constexpr ShapeInfo gShapeInfo[] = {
Florin Malitafbc13f12018-01-04 10:26:35 -0500524 { "el", ShapeType::kGeometry , 2 }, // ellipse -> AttachEllipseGeometry
Florin Malita6aaee592018-01-12 12:25:09 -0500525 { "fl", ShapeType::kPaint , 0 }, // fill -> AttachColorFill
526 { "gf", ShapeType::kPaint , 2 }, // gfill -> AttachGradientFill
Florin Malita16d0ad02018-01-19 15:07:29 -0500527 { "gr", ShapeType::kGroup , 0 }, // group -> Inline handler
Florin Malita6aaee592018-01-12 12:25:09 -0500528 { "gs", ShapeType::kPaint , 3 }, // gstroke -> AttachGradientStroke
Florin Malitae6345d92018-01-03 23:37:54 -0500529 { "mm", ShapeType::kGeometryEffect, 0 }, // merge -> AttachMergeGeometryEffect
Florin Malita02a32b02018-01-04 11:27:09 -0500530 { "rc", ShapeType::kGeometry , 1 }, // rrect -> AttachRRectGeometry
Florin Malitae6345d92018-01-03 23:37:54 -0500531 { "sh", ShapeType::kGeometry , 0 }, // shape -> AttachPathGeometry
Florin Malita02a32b02018-01-04 11:27:09 -0500532 { "sr", ShapeType::kGeometry , 3 }, // polystar -> AttachPolyStarGeometry
Florin Malita6aaee592018-01-12 12:25:09 -0500533 { "st", ShapeType::kPaint , 1 }, // stroke -> AttachColorStroke
Florin Malita51b8c892018-01-07 08:54:24 -0500534 { "tm", ShapeType::kGeometryEffect, 1 }, // trim -> AttachTrimGeometryEffect
Florin Malita16d0ad02018-01-19 15:07:29 -0500535 { "tr", ShapeType::kTransform , 0 }, // transform -> Inline handler
Florin Malita094ccde2017-12-30 12:27:00 -0500536 };
537
538 if (!shape.isObject())
539 return nullptr;
540
541 const auto& type = shape["ty"];
542 if (!type.isString())
543 return nullptr;
544
545 const auto* info = bsearch(type.asCString(),
546 gShapeInfo,
547 SK_ARRAY_COUNT(gShapeInfo),
548 sizeof(ShapeInfo),
549 [](const void* key, const void* info) {
550 return strcmp(static_cast<const char*>(key),
551 static_cast<const ShapeInfo*>(info)->fTypeString);
552 });
553
554 return static_cast<const ShapeInfo*>(info);
555}
556
Florin Malita16d0ad02018-01-19 15:07:29 -0500557struct GeometryEffectRec {
558 const Json::Value& fJson;
559 GeometryEffectAttacherT fAttach;
560};
561
562sk_sp<sksg::RenderNode> AttachShape(const Json::Value& jshape, AttachContext* ctx,
563 std::vector<sk_sp<sksg::GeometryNode>>* geometryStack,
564 std::vector<GeometryEffectRec>* geometryEffectStack) {
565 if (!jshape.isArray())
Florin Malita094ccde2017-12-30 12:27:00 -0500566 return nullptr;
567
Florin Malita16d0ad02018-01-19 15:07:29 -0500568 SkDEBUGCODE(const auto initialGeometryEffects = geometryEffectStack->size();)
Florin Malita094ccde2017-12-30 12:27:00 -0500569
Florin Malita16d0ad02018-01-19 15:07:29 -0500570 sk_sp<sksg::Group> shape_group = sksg::Group::Make();
571 sk_sp<sksg::RenderNode> shape_wrapper = shape_group;
572 sk_sp<sksg::Matrix> shape_matrix;
Florin Malita094ccde2017-12-30 12:27:00 -0500573
Florin Malita16d0ad02018-01-19 15:07:29 -0500574 struct ShapeRec {
575 const Json::Value& fJson;
576 const ShapeInfo& fInfo;
577 };
578
579 // First pass (bottom->top):
580 //
581 // * pick up the group transform and opacity
582 // * push local geometry effects onto the stack
583 // * store recs for next pass
584 //
585 std::vector<ShapeRec> recs;
586 for (Json::ArrayIndex i = 0; i < jshape.size(); ++i) {
587 const auto& s = jshape[jshape.size() - 1 - i];
Florin Malita094ccde2017-12-30 12:27:00 -0500588 const auto* info = FindShapeInfo(s);
589 if (!info) {
590 LogFail(s.isObject() ? s["ty"] : s, "Unknown shape");
591 continue;
592 }
593
Florin Malita16d0ad02018-01-19 15:07:29 -0500594 recs.push_back({ s, *info });
595
Florin Malita094ccde2017-12-30 12:27:00 -0500596 switch (info->fShapeType) {
Florin Malita16d0ad02018-01-19 15:07:29 -0500597 case ShapeType::kTransform:
598 if ((shape_matrix = AttachMatrix(s, ctx, nullptr))) {
599 shape_wrapper = sksg::Transform::Make(std::move(shape_wrapper), shape_matrix);
600 }
601 shape_wrapper = AttachOpacity(s, ctx, std::move(shape_wrapper));
602 break;
603 case ShapeType::kGeometryEffect:
604 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGeometryEffectAttachers));
605 geometryEffectStack->push_back(
606 { s, gGeometryEffectAttachers[info->fAttacherIndex] });
607 break;
608 default:
609 break;
610 }
611 }
612
613 // Second pass (top -> bottom, after 2x reverse):
614 //
615 // * track local geometry
616 // * emit local paints
617 //
618 std::vector<sk_sp<sksg::GeometryNode>> geos;
619 std::vector<sk_sp<sksg::RenderNode >> draws;
620 for (auto rec = recs.rbegin(); rec != recs.rend(); ++rec) {
621 switch (rec->fInfo.fShapeType) {
Florin Malita094ccde2017-12-30 12:27:00 -0500622 case ShapeType::kGeometry: {
Florin Malita16d0ad02018-01-19 15:07:29 -0500623 SkASSERT(rec->fInfo.fAttacherIndex < SK_ARRAY_COUNT(gGeometryAttachers));
624 if (auto geo = gGeometryAttachers[rec->fInfo.fAttacherIndex](rec->fJson, ctx)) {
Florin Malita094ccde2017-12-30 12:27:00 -0500625 geos.push_back(std::move(geo));
626 }
627 } break;
Florin Malitae6345d92018-01-03 23:37:54 -0500628 case ShapeType::kGeometryEffect: {
Florin Malita16d0ad02018-01-19 15:07:29 -0500629 // Apply the current effect and pop from the stack.
630 SkASSERT(rec->fInfo.fAttacherIndex < SK_ARRAY_COUNT(gGeometryEffectAttachers));
Florin Malitaee6de4b2018-01-21 11:13:17 -0500631 if (!geos.empty()) {
632 geos = gGeometryEffectAttachers[rec->fInfo.fAttacherIndex](rec->fJson,
633 ctx,
634 std::move(geos));
635 }
Florin Malita16d0ad02018-01-19 15:07:29 -0500636
637 SkASSERT(geometryEffectStack->back().fJson == rec->fJson);
638 SkASSERT(geometryEffectStack->back().fAttach ==
639 gGeometryEffectAttachers[rec->fInfo.fAttacherIndex]);
640 geometryEffectStack->pop_back();
Florin Malita094ccde2017-12-30 12:27:00 -0500641 } break;
642 case ShapeType::kGroup: {
Florin Malita16d0ad02018-01-19 15:07:29 -0500643 if (auto subgroup = AttachShape(rec->fJson["it"], ctx, &geos, geometryEffectStack)) {
644 draws.push_back(std::move(subgroup));
Florin Malita094ccde2017-12-30 12:27:00 -0500645 }
646 } break;
Florin Malita16d0ad02018-01-19 15:07:29 -0500647 case ShapeType::kPaint: {
648 SkASSERT(rec->fInfo.fAttacherIndex < SK_ARRAY_COUNT(gPaintAttachers));
649 auto paint = gPaintAttachers[rec->fInfo.fAttacherIndex](rec->fJson, ctx);
650 if (!paint || geos.empty())
651 break;
652
653 auto drawGeos = geos;
654
655 // Apply all pending effects from the stack.
656 for (auto it = geometryEffectStack->rbegin(); it != geometryEffectStack->rend(); ++it) {
657 drawGeos = it->fAttach(it->fJson, ctx, std::move(drawGeos));
Florin Malita18eafd92018-01-04 21:11:55 -0500658 }
Florin Malita16d0ad02018-01-19 15:07:29 -0500659
660 // If we still have multiple geos, reduce using 'merge'.
661 auto geo = drawGeos.size() > 1
662 ? sksg::Merge::Make(std::move(drawGeos), sksg::Merge::Mode::kMerge)
663 : drawGeos[0];
664
665 SkASSERT(geo);
666 draws.push_back(sksg::Draw::Make(std::move(geo), std::move(paint)));
Florin Malitadacc02b2017-12-31 09:12:31 -0500667 } break;
Florin Malita16d0ad02018-01-19 15:07:29 -0500668 default:
669 break;
Florin Malita094ccde2017-12-30 12:27:00 -0500670 }
671 }
672
Florin Malita16d0ad02018-01-19 15:07:29 -0500673 // By now we should have popped all local geometry effects.
674 SkASSERT(geometryEffectStack->size() == initialGeometryEffects);
675
676 // Push transformed local geometries to parent list, for subsequent paints.
677 for (const auto& geo : geos) {
678 geometryStack->push_back(shape_matrix
679 ? sksg::GeometryTransform::Make(std::move(geo), shape_matrix)
680 : std::move(geo));
Florin Malita094ccde2017-12-30 12:27:00 -0500681 }
682
Florin Malita16d0ad02018-01-19 15:07:29 -0500683 // Emit local draws reversed (bottom->top, per spec).
684 for (auto it = draws.rbegin(); it != draws.rend(); ++it) {
685 shape_group->addChild(std::move(*it));
Florin Malita2a8275b2018-01-02 12:52:43 -0500686 }
687
Florin Malita16d0ad02018-01-19 15:07:29 -0500688 return draws.empty() ? nullptr : shape_wrapper;
Florin Malita094ccde2017-12-30 12:27:00 -0500689}
690
691sk_sp<sksg::RenderNode> AttachCompLayer(const Json::Value& layer, AttachContext* ctx) {
692 SkASSERT(layer.isObject());
693
694 auto refId = ParseString(layer["refId"], "");
695 if (refId.isEmpty()) {
696 LOG("!! Comp layer missing refId\n");
697 return nullptr;
698 }
699
700 const auto* comp = ctx->fAssets.find(refId);
701 if (!comp) {
702 LOG("!! Pre-comp not found: '%s'\n", refId.c_str());
703 return nullptr;
704 }
705
706 // TODO: cycle detection
707 return AttachComposition(**comp, ctx);
708}
709
Florin Malita0e66fba2018-01-09 17:10:18 -0500710sk_sp<sksg::RenderNode> AttachSolidLayer(const Json::Value& jlayer, AttachContext*) {
711 SkASSERT(jlayer.isObject());
Florin Malita094ccde2017-12-30 12:27:00 -0500712
Florin Malita0e66fba2018-01-09 17:10:18 -0500713 const auto size = SkSize::Make(ParseScalar(jlayer["sw"], -1),
714 ParseScalar(jlayer["sh"], -1));
715 const auto hex = ParseString(jlayer["sc"], "");
716 uint32_t c;
717 if (size.isEmpty() ||
718 !hex.startsWith("#") ||
719 !SkParse::FindHex(hex.c_str() + 1, &c)) {
720 LogFail(jlayer, "Could not parse solid layer");
721 return nullptr;
722 }
723
724 const SkColor color = 0xff000000 | c;
725
726 return sksg::Draw::Make(sksg::Rect::Make(SkRect::MakeSize(size)),
727 sksg::Color::Make(color));
Florin Malita094ccde2017-12-30 12:27:00 -0500728}
729
Florin Malita49328072018-01-08 12:51:12 -0500730sk_sp<sksg::RenderNode> AttachImageAsset(const Json::Value& jimage, AttachContext* ctx) {
731 SkASSERT(jimage.isObject());
732
733 const auto name = ParseString(jimage["p"], ""),
734 path = ParseString(jimage["u"], "");
735 if (name.isEmpty())
736 return nullptr;
737
738 // TODO: plumb resource paths explicitly to ResourceProvider?
739 const auto resName = path.isEmpty() ? name : SkOSPath::Join(path.c_str(), name.c_str());
740 const auto resStream = ctx->fResources.openStream(resName.c_str());
741 if (!resStream || !resStream->hasLength()) {
742 LOG("!! Could not load image resource: %s\n", resName.c_str());
743 return nullptr;
744 }
745
746 // TODO: non-intrisic image sizing
747 return sksg::Image::Make(
748 SkImage::MakeFromEncoded(SkData::MakeFromStream(resStream.get(), resStream->getLength())));
749}
750
751sk_sp<sksg::RenderNode> AttachImageLayer(const Json::Value& layer, AttachContext* ctx) {
Florin Malita094ccde2017-12-30 12:27:00 -0500752 SkASSERT(layer.isObject());
753
Florin Malita49328072018-01-08 12:51:12 -0500754 auto refId = ParseString(layer["refId"], "");
755 if (refId.isEmpty()) {
756 LOG("!! Image layer missing refId\n");
757 return nullptr;
758 }
759
760 const auto* jimage = ctx->fAssets.find(refId);
761 if (!jimage) {
762 LOG("!! Image asset not found: '%s'\n", refId.c_str());
763 return nullptr;
764 }
765
766 return AttachImageAsset(**jimage, ctx);
Florin Malita094ccde2017-12-30 12:27:00 -0500767}
768
769sk_sp<sksg::RenderNode> AttachNullLayer(const Json::Value& layer, AttachContext*) {
770 SkASSERT(layer.isObject());
771
Florin Malita18eafd92018-01-04 21:11:55 -0500772 // Null layers are used solely to drive dependent transforms,
773 // but we use free-floating sksg::Matrices for that purpose.
Florin Malita094ccde2017-12-30 12:27:00 -0500774 return nullptr;
775}
776
777sk_sp<sksg::RenderNode> AttachShapeLayer(const Json::Value& layer, AttachContext* ctx) {
778 SkASSERT(layer.isObject());
779
780 LOG("** Attaching shape layer ind: %d\n", ParseInt(layer["ind"], 0));
781
Florin Malita16d0ad02018-01-19 15:07:29 -0500782 std::vector<sk_sp<sksg::GeometryNode>> geometryStack;
783 std::vector<GeometryEffectRec> geometryEffectStack;
784 return AttachShape(layer["shapes"], ctx, &geometryStack, &geometryEffectStack);
Florin Malita094ccde2017-12-30 12:27:00 -0500785}
786
787sk_sp<sksg::RenderNode> AttachTextLayer(const Json::Value& layer, AttachContext*) {
788 SkASSERT(layer.isObject());
789
790 LOG("?? Text layer stub\n");
791 return nullptr;
792}
793
Florin Malita18eafd92018-01-04 21:11:55 -0500794struct AttachLayerContext {
795 AttachLayerContext(const Json::Value& jlayers, AttachContext* ctx)
796 : fLayerList(jlayers), fCtx(ctx) {}
797
798 const Json::Value& fLayerList;
799 AttachContext* fCtx;
800 std::unordered_map<const Json::Value*, sk_sp<sksg::Matrix>> fLayerMatrixCache;
801 std::unordered_map<int, const Json::Value*> fLayerIndexCache;
Florin Malita5f9102f2018-01-10 13:36:22 -0500802 sk_sp<sksg::RenderNode> fCurrentMatte;
Florin Malita18eafd92018-01-04 21:11:55 -0500803
804 const Json::Value* findLayer(int index) {
805 SkASSERT(fLayerList.isArray());
806
807 if (index < 0) {
808 return nullptr;
809 }
810
811 const auto cached = fLayerIndexCache.find(index);
812 if (cached != fLayerIndexCache.end()) {
813 return cached->second;
814 }
815
816 for (const auto& l : fLayerList) {
817 if (!l.isObject()) {
818 continue;
819 }
820
821 if (ParseInt(l["ind"], -1) == index) {
822 fLayerIndexCache.insert(std::make_pair(index, &l));
823 return &l;
824 }
825 }
826
827 return nullptr;
828 }
829
830 sk_sp<sksg::Matrix> AttachLayerMatrix(const Json::Value& jlayer) {
831 SkASSERT(jlayer.isObject());
832
833 const auto cached = fLayerMatrixCache.find(&jlayer);
834 if (cached != fLayerMatrixCache.end()) {
835 return cached->second;
836 }
837
838 const auto* parentLayer = this->findLayer(ParseInt(jlayer["parent"], -1));
839
840 // TODO: cycle detection?
841 auto parentMatrix = (parentLayer && parentLayer != &jlayer)
842 ? this->AttachLayerMatrix(*parentLayer) : nullptr;
843
844 auto layerMatrix = AttachMatrix(jlayer["ks"], fCtx, std::move(parentMatrix));
845 fLayerMatrixCache.insert(std::make_pair(&jlayer, layerMatrix));
846
847 return layerMatrix;
848 }
849};
850
851sk_sp<sksg::RenderNode> AttachLayer(const Json::Value& jlayer,
852 AttachLayerContext* layerCtx) {
853 if (!jlayer.isObject())
Florin Malita094ccde2017-12-30 12:27:00 -0500854 return nullptr;
855
856 using LayerAttacher = sk_sp<sksg::RenderNode> (*)(const Json::Value&, AttachContext*);
857 static constexpr LayerAttacher gLayerAttachers[] = {
858 AttachCompLayer, // 'ty': 0
859 AttachSolidLayer, // 'ty': 1
860 AttachImageLayer, // 'ty': 2
861 AttachNullLayer, // 'ty': 3
862 AttachShapeLayer, // 'ty': 4
863 AttachTextLayer, // 'ty': 5
864 };
865
Florin Malita18eafd92018-01-04 21:11:55 -0500866 int type = ParseInt(jlayer["ty"], -1);
Florin Malita094ccde2017-12-30 12:27:00 -0500867 if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gLayerAttachers))) {
868 return nullptr;
869 }
870
Florin Malita71cba8f2018-01-09 08:07:14 -0500871 // Layer content.
872 auto layer = gLayerAttachers[type](jlayer, layerCtx->fCtx);
873 if (auto layerMatrix = layerCtx->AttachLayerMatrix(jlayer)) {
874 // Optional layer transform.
875 layer = sksg::Transform::Make(std::move(layer), std::move(layerMatrix));
876 }
877 // Optional layer opacity.
878 layer = AttachOpacity(jlayer["ks"], layerCtx->fCtx, std::move(layer));
Florin Malita18eafd92018-01-04 21:11:55 -0500879
Florin Malita71cba8f2018-01-09 08:07:14 -0500880 // TODO: we should also disable related/inactive animators.
881 class Activator final : public AnimatorBase {
882 public:
883 Activator(sk_sp<sksg::OpacityEffect> controlNode, float in, float out)
884 : fControlNode(std::move(controlNode))
885 , fIn(in)
886 , fOut(out) {}
887
Florin Malitaa6dd7522018-01-09 08:46:52 -0500888 void tick(float t) override {
Florin Malita71cba8f2018-01-09 08:07:14 -0500889 // Keep the layer fully transparent except for its [in..out] lifespan.
890 // (note: opacity == 0 disables rendering, while opacity == 1 is a noop)
891 fControlNode->setOpacity(t >= fIn && t <= fOut ? 1 : 0);
892 }
893
894 private:
895 const sk_sp<sksg::OpacityEffect> fControlNode;
896 const float fIn,
897 fOut;
898 };
899
900 auto layerControl = sksg::OpacityEffect::Make(std::move(layer));
901 const auto in = ParseScalar(jlayer["ip"], 0),
902 out = ParseScalar(jlayer["op"], in);
903
904 if (in >= out || ! layerControl)
905 return nullptr;
906
907 layerCtx->fCtx->fAnimators.push_back(skstd::make_unique<Activator>(layerControl, in, out));
908
Florin Malita5f9102f2018-01-10 13:36:22 -0500909 if (ParseBool(jlayer["td"], false)) {
910 // This layer is a matte. We apply it as a mask to the next layer.
911 layerCtx->fCurrentMatte = std::move(layerControl);
912 return nullptr;
913 }
914
915 if (layerCtx->fCurrentMatte) {
916 // There is a pending matte. Apply and reset.
917 return sksg::MaskEffect::Make(std::move(layerControl), std::move(layerCtx->fCurrentMatte));
918 }
919
Florin Malita71cba8f2018-01-09 08:07:14 -0500920 return layerControl;
Florin Malita094ccde2017-12-30 12:27:00 -0500921}
922
923sk_sp<sksg::RenderNode> AttachComposition(const Json::Value& comp, AttachContext* ctx) {
924 if (!comp.isObject())
925 return nullptr;
926
Florin Malita18eafd92018-01-04 21:11:55 -0500927 const auto& jlayers = comp["layers"];
928 if (!jlayers.isArray())
929 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500930
Florin Malita18eafd92018-01-04 21:11:55 -0500931 SkSTArray<16, sk_sp<sksg::RenderNode>, true> layers;
932 AttachLayerContext layerCtx(jlayers, ctx);
933
934 for (const auto& l : jlayers) {
935 if (auto layer_fragment = AttachLayer(l, &layerCtx)) {
Florin Malita2a8275b2018-01-02 12:52:43 -0500936 layers.push_back(std::move(layer_fragment));
Florin Malita094ccde2017-12-30 12:27:00 -0500937 }
938 }
939
Florin Malita2a8275b2018-01-02 12:52:43 -0500940 if (layers.empty()) {
941 return nullptr;
942 }
943
944 // Layers are painted in bottom->top order.
945 auto comp_group = sksg::Group::Make();
946 for (int i = layers.count() - 1; i >= 0; --i) {
947 comp_group->addChild(std::move(layers[i]));
948 }
949
950 LOG("** Attached composition '%s': %d layers.\n",
951 ParseString(comp["id"], "").c_str(), layers.count());
952
Florin Malita094ccde2017-12-30 12:27:00 -0500953 return comp_group;
954}
955
956} // namespace
957
Florin Malita49328072018-01-08 12:51:12 -0500958std::unique_ptr<Animation> Animation::Make(SkStream* stream, const ResourceProvider& res) {
Florin Malita094ccde2017-12-30 12:27:00 -0500959 if (!stream->hasLength()) {
960 // TODO: handle explicit buffering?
961 LOG("!! cannot parse streaming content\n");
962 return nullptr;
963 }
964
965 Json::Value json;
966 {
967 auto data = SkData::MakeFromStream(stream, stream->getLength());
968 if (!data) {
969 LOG("!! could not read stream\n");
970 return nullptr;
971 }
972
973 Json::Reader reader;
974
975 auto dataStart = static_cast<const char*>(data->data());
976 if (!reader.parse(dataStart, dataStart + data->size(), json, false) || !json.isObject()) {
977 LOG("!! failed to parse json: %s\n", reader.getFormattedErrorMessages().c_str());
978 return nullptr;
979 }
980 }
981
982 const auto version = ParseString(json["v"], "");
983 const auto size = SkSize::Make(ParseScalar(json["w"], -1), ParseScalar(json["h"], -1));
984 const auto fps = ParseScalar(json["fr"], -1);
985
986 if (size.isEmpty() || version.isEmpty() || fps < 0) {
987 LOG("!! invalid animation params (version: %s, size: [%f %f], frame rate: %f)",
988 version.c_str(), size.width(), size.height(), fps);
989 return nullptr;
990 }
991
Florin Malita49328072018-01-08 12:51:12 -0500992 return std::unique_ptr<Animation>(new Animation(res, std::move(version), size, fps, json));
Florin Malita094ccde2017-12-30 12:27:00 -0500993}
994
Florin Malita49328072018-01-08 12:51:12 -0500995std::unique_ptr<Animation> Animation::MakeFromFile(const char path[], const ResourceProvider* res) {
996 class DirectoryResourceProvider final : public ResourceProvider {
997 public:
998 explicit DirectoryResourceProvider(SkString dir) : fDir(std::move(dir)) {}
999
1000 std::unique_ptr<SkStream> openStream(const char resource[]) const override {
1001 const auto resPath = SkOSPath::Join(fDir.c_str(), resource);
1002 return SkStream::MakeFromFile(resPath.c_str());
1003 }
1004
1005 private:
1006 const SkString fDir;
1007 };
1008
1009 const auto jsonStream = SkStream::MakeFromFile(path);
1010 if (!jsonStream)
1011 return nullptr;
1012
1013 std::unique_ptr<ResourceProvider> defaultProvider;
1014 if (!res) {
1015 defaultProvider = skstd::make_unique<DirectoryResourceProvider>(SkOSPath::Dirname(path));
1016 }
1017
1018 return Make(jsonStream.get(), res ? *res : *defaultProvider);
1019}
1020
1021Animation::Animation(const ResourceProvider& resources,
1022 SkString version, const SkSize& size, SkScalar fps, const Json::Value& json)
Florin Malita094ccde2017-12-30 12:27:00 -05001023 : fVersion(std::move(version))
1024 , fSize(size)
1025 , fFrameRate(fps)
1026 , fInPoint(ParseScalar(json["ip"], 0))
1027 , fOutPoint(SkTMax(ParseScalar(json["op"], SK_ScalarMax), fInPoint)) {
1028
1029 AssetMap assets;
1030 for (const auto& asset : json["assets"]) {
1031 if (!asset.isObject()) {
1032 continue;
1033 }
1034
1035 assets.set(ParseString(asset["id"], ""), &asset);
1036 }
1037
Florin Malita49328072018-01-08 12:51:12 -05001038 AttachContext ctx = { resources, assets, fAnimators };
Florin Malita094ccde2017-12-30 12:27:00 -05001039 fDom = AttachComposition(json, &ctx);
1040
Florin Malitadb385732018-01-09 12:19:32 -05001041 // In case the client calls render before the first tick.
1042 this->animationTick(0);
1043
Florin Malita094ccde2017-12-30 12:27:00 -05001044 LOG("** Attached %d animators\n", fAnimators.count());
1045}
1046
1047Animation::~Animation() = default;
1048
Mike Reed29859872018-01-08 08:25:27 -05001049void Animation::render(SkCanvas* canvas, const SkRect* dstR) const {
Florin Malita094ccde2017-12-30 12:27:00 -05001050 if (!fDom)
1051 return;
1052
1053 sksg::InvalidationController ic;
1054 fDom->revalidate(&ic, SkMatrix::I());
1055
1056 // TODO: proper inval
Mike Reed29859872018-01-08 08:25:27 -05001057 SkAutoCanvasRestore restore(canvas, true);
1058 const SkRect srcR = SkRect::MakeSize(this->size());
1059 if (dstR) {
1060 canvas->concat(SkMatrix::MakeRectToRect(srcR, *dstR, SkMatrix::kCenter_ScaleToFit));
1061 }
1062 canvas->clipRect(srcR);
Florin Malita094ccde2017-12-30 12:27:00 -05001063 fDom->render(canvas);
1064
1065 if (!fShowInval)
1066 return;
1067
1068 SkPaint fill, stroke;
1069 fill.setAntiAlias(true);
1070 fill.setColor(0x40ff0000);
1071 stroke.setAntiAlias(true);
1072 stroke.setColor(0xffff0000);
1073 stroke.setStyle(SkPaint::kStroke_Style);
1074
1075 for (const auto& r : ic) {
1076 canvas->drawRect(r, fill);
1077 canvas->drawRect(r, stroke);
1078 }
1079}
1080
1081void Animation::animationTick(SkMSec ms) {
1082 // 't' in the BM model really means 'frame #'
1083 auto t = static_cast<float>(ms) * fFrameRate / 1000;
1084
1085 t = fInPoint + std::fmod(t, fOutPoint - fInPoint);
1086
1087 // TODO: this can be optimized quite a bit with some sorting/state tracking.
1088 for (const auto& a : fAnimators) {
1089 a->tick(t);
1090 }
1091}
1092
Florin Malita54f65c42018-01-16 17:04:30 -05001093} // namespace skottie