blob: 2199f51f2abedd3227ebe49f0a16e99bf40e017d [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 Malita35efaa82018-01-22 12:57:06 -050034#include "SkSGScene.h"
Florin Malita094ccde2017-12-30 12:27:00 -050035#include "SkSGTransform.h"
Florin Malita51b8c892018-01-07 08:54:24 -050036#include "SkSGTrimEffect.h"
Florin Malita094ccde2017-12-30 12:27:00 -050037#include "SkStream.h"
38#include "SkTArray.h"
39#include "SkTHash.h"
40
41#include <cmath>
Florin Malita18eafd92018-01-04 21:11:55 -050042#include <unordered_map>
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
49namespace {
50
51using AssetMap = SkTHashMap<SkString, const Json::Value*>;
52
53struct AttachContext {
Florin Malita35efaa82018-01-22 12:57:06 -050054 const ResourceProvider& fResources;
55 const AssetMap& fAssets;
56 sksg::Scene::AnimatorList& fAnimators;
Florin Malita094ccde2017-12-30 12:27:00 -050057};
58
59bool LogFail(const Json::Value& json, const char* msg) {
60 const auto dump = json.toStyledString();
61 LOG("!! %s: %s", msg, dump.c_str());
62 return false;
63}
64
65// This is the workhorse for binding properties: depending on whether the property is animated,
66// it will either apply immediately or instantiate and attach a keyframe animator.
Florin Malitaf9590922018-01-09 11:56:09 -050067template <typename ValT, typename NodeT>
68bool BindProperty(const Json::Value& jprop, AttachContext* ctx, const sk_sp<NodeT>& node,
69 typename Animator<ValT, NodeT>::ApplyFuncT&& apply) {
Florin Malita094ccde2017-12-30 12:27:00 -050070 if (!jprop.isObject())
71 return false;
72
Florin Malita95448a92018-01-08 10:15:12 -050073 const auto& jpropA = jprop["a"];
74 const auto& jpropK = jprop["k"];
75
76 // Older Json versions don't have an "a" animation marker.
77 // For those, we attempt to parse both ways.
78 if (jpropA.isNull() || !ParseBool(jpropA, "false")) {
Florin Malitaf9590922018-01-09 11:56:09 -050079 ValT val;
80 if (ValueTraits<ValT>::Parse(jpropK, &val)) {
Florin Malita95448a92018-01-08 10:15:12 -050081 // Static property.
Florin Malitaf9590922018-01-09 11:56:09 -050082 apply(node.get(), val);
Florin Malita95448a92018-01-08 10:15:12 -050083 return true;
Florin Malita094ccde2017-12-30 12:27:00 -050084 }
85
Florin Malita95448a92018-01-08 10:15:12 -050086 if (!jpropA.isNull()) {
87 return LogFail(jprop, "Could not parse (explicit) static property");
Florin Malita094ccde2017-12-30 12:27:00 -050088 }
Florin Malita094ccde2017-12-30 12:27:00 -050089 }
90
Florin Malita95448a92018-01-08 10:15:12 -050091 // Keyframe property.
Florin Malitaf9590922018-01-09 11:56:09 -050092 using AnimatorT = Animator<ValT, NodeT>;
93 auto animator = AnimatorT::Make(ParseFrames<ValT>(jpropK), node, std::move(apply));
Florin Malita95448a92018-01-08 10:15:12 -050094
95 if (!animator) {
96 return LogFail(jprop, "Could not parse keyframed property");
97 }
98
99 ctx->fAnimators.push_back(std::move(animator));
100
Florin Malita094ccde2017-12-30 12:27:00 -0500101 return true;
102}
103
Florin Malita18eafd92018-01-04 21:11:55 -0500104sk_sp<sksg::Matrix> AttachMatrix(const Json::Value& t, AttachContext* ctx,
105 sk_sp<sksg::Matrix> parentMatrix) {
106 if (!t.isObject())
107 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500108
Florin Malita18eafd92018-01-04 21:11:55 -0500109 auto matrix = sksg::Matrix::Make(SkMatrix::I(), std::move(parentMatrix));
110 auto composite = sk_make_sp<CompositeTransform>(matrix);
Florin Malitaf9590922018-01-09 11:56:09 -0500111 auto anchor_attached = BindProperty<VectorValue>(t["a"], ctx, composite,
112 [](CompositeTransform* node, const VectorValue& a) {
113 node->setAnchorPoint(ValueTraits<VectorValue>::As<SkPoint>(a));
Florin Malita094ccde2017-12-30 12:27:00 -0500114 });
Florin Malitaf9590922018-01-09 11:56:09 -0500115 auto position_attached = BindProperty<VectorValue>(t["p"], ctx, composite,
116 [](CompositeTransform* node, const VectorValue& p) {
117 node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
Florin Malita094ccde2017-12-30 12:27:00 -0500118 });
Florin Malitaf9590922018-01-09 11:56:09 -0500119 auto scale_attached = BindProperty<VectorValue>(t["s"], ctx, composite,
120 [](CompositeTransform* node, const VectorValue& s) {
121 node->setScale(ValueTraits<VectorValue>::As<SkVector>(s));
Florin Malita094ccde2017-12-30 12:27:00 -0500122 });
Florin Malitaf9590922018-01-09 11:56:09 -0500123 auto rotation_attached = BindProperty<ScalarValue>(t["r"], ctx, composite,
124 [](CompositeTransform* node, const ScalarValue& r) {
Florin Malita094ccde2017-12-30 12:27:00 -0500125 node->setRotation(r);
126 });
Florin Malitaf9590922018-01-09 11:56:09 -0500127 auto skew_attached = BindProperty<ScalarValue>(t["sk"], ctx, composite,
128 [](CompositeTransform* node, const ScalarValue& sk) {
Florin Malita094ccde2017-12-30 12:27:00 -0500129 node->setSkew(sk);
130 });
Florin Malitaf9590922018-01-09 11:56:09 -0500131 auto skewaxis_attached = BindProperty<ScalarValue>(t["sa"], ctx, composite,
132 [](CompositeTransform* node, const ScalarValue& sa) {
Florin Malita094ccde2017-12-30 12:27:00 -0500133 node->setSkewAxis(sa);
134 });
135
136 if (!anchor_attached &&
137 !position_attached &&
138 !scale_attached &&
139 !rotation_attached &&
140 !skew_attached &&
141 !skewaxis_attached) {
142 LogFail(t, "Could not parse transform");
Florin Malita18eafd92018-01-04 21:11:55 -0500143 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500144 }
145
Florin Malita18eafd92018-01-04 21:11:55 -0500146 return matrix;
Florin Malita094ccde2017-12-30 12:27:00 -0500147}
148
Florin Malitac0034172018-01-08 16:42:59 -0500149sk_sp<sksg::RenderNode> AttachOpacity(const Json::Value& jtransform, AttachContext* ctx,
150 sk_sp<sksg::RenderNode> childNode) {
151 if (!jtransform.isObject() || !childNode)
152 return childNode;
153
154 // This is more peeky than other attachers, because we want to avoid redundant opacity
155 // nodes for the extremely common case of static opaciy == 100.
156 const auto& opacity = jtransform["o"];
157 if (opacity.isObject() &&
158 !ParseBool(opacity["a"], true) &&
159 ParseScalar(opacity["k"], -1) == 100) {
160 // Ignoring static full opacity.
161 return childNode;
162 }
163
164 auto opacityNode = sksg::OpacityEffect::Make(childNode);
Florin Malitaf9590922018-01-09 11:56:09 -0500165 BindProperty<ScalarValue>(opacity, ctx, opacityNode,
166 [](sksg::OpacityEffect* node, const ScalarValue& o) {
Florin Malitac0034172018-01-08 16:42:59 -0500167 // BM opacity is [0..100]
168 node->setOpacity(o * 0.01f);
169 });
170
171 return opacityNode;
172}
173
Florin Malita094ccde2017-12-30 12:27:00 -0500174sk_sp<sksg::RenderNode> AttachComposition(const Json::Value&, AttachContext* ctx);
175
Florin Malita25366fa2018-01-23 13:37:59 -0500176sk_sp<sksg::Path> AttachPath(const Json::Value& jpath, AttachContext* ctx) {
177 auto path_node = sksg::Path::Make();
178 return BindProperty<ShapeValue>(jpath, ctx, path_node,
179 [](sksg::Path* node, const ShapeValue& p) { node->setPath(p); })
180 ? path_node
181 : nullptr;
182}
183
Florin Malita094ccde2017-12-30 12:27:00 -0500184sk_sp<sksg::GeometryNode> AttachPathGeometry(const Json::Value& jpath, AttachContext* ctx) {
185 SkASSERT(jpath.isObject());
186
Florin Malita25366fa2018-01-23 13:37:59 -0500187 return AttachPath(jpath["ks"], ctx);
Florin Malita094ccde2017-12-30 12:27:00 -0500188}
189
Florin Malita2e1d7e22018-01-02 10:40:00 -0500190sk_sp<sksg::GeometryNode> AttachRRectGeometry(const Json::Value& jrect, AttachContext* ctx) {
191 SkASSERT(jrect.isObject());
192
193 auto rect_node = sksg::RRect::Make();
194 auto composite = sk_make_sp<CompositeRRect>(rect_node);
195
Florin Malitaf9590922018-01-09 11:56:09 -0500196 auto p_attached = BindProperty<VectorValue>(jrect["p"], ctx, composite,
197 [](CompositeRRect* node, const VectorValue& p) {
198 node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
199 });
200 auto s_attached = BindProperty<VectorValue>(jrect["s"], ctx, composite,
201 [](CompositeRRect* node, const VectorValue& s) {
202 node->setSize(ValueTraits<VectorValue>::As<SkSize>(s));
203 });
204 auto r_attached = BindProperty<ScalarValue>(jrect["r"], ctx, composite,
205 [](CompositeRRect* node, const ScalarValue& r) {
206 node->setRadius(SkSize::Make(r, r));
207 });
Florin Malita2e1d7e22018-01-02 10:40:00 -0500208
209 if (!p_attached && !s_attached && !r_attached) {
210 return nullptr;
211 }
212
Florin Malitafbc13f12018-01-04 10:26:35 -0500213 LOG("** Attached (r)rect geometry\n");
214
215 return rect_node;
216}
217
218sk_sp<sksg::GeometryNode> AttachEllipseGeometry(const Json::Value& jellipse, AttachContext* ctx) {
219 SkASSERT(jellipse.isObject());
220
221 auto rect_node = sksg::RRect::Make();
222 auto composite = sk_make_sp<CompositeRRect>(rect_node);
223
Florin Malitaf9590922018-01-09 11:56:09 -0500224 auto p_attached = BindProperty<VectorValue>(jellipse["p"], ctx, composite,
225 [](CompositeRRect* node, const VectorValue& p) {
226 node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
227 });
228 auto s_attached = BindProperty<VectorValue>(jellipse["s"], ctx, composite,
229 [](CompositeRRect* node, const VectorValue& s) {
230 const auto sz = ValueTraits<VectorValue>::As<SkSize>(s);
231 node->setSize(sz);
232 node->setRadius(SkSize::Make(sz.width() / 2, sz.height() / 2));
233 });
Florin Malitafbc13f12018-01-04 10:26:35 -0500234
235 if (!p_attached && !s_attached) {
236 return nullptr;
237 }
238
239 LOG("** Attached ellipse geometry\n");
240
Florin Malita2e1d7e22018-01-02 10:40:00 -0500241 return rect_node;
242}
243
Florin Malita02a32b02018-01-04 11:27:09 -0500244sk_sp<sksg::GeometryNode> AttachPolystarGeometry(const Json::Value& jstar, AttachContext* ctx) {
245 SkASSERT(jstar.isObject());
246
247 static constexpr CompositePolyStar::Type gTypes[] = {
248 CompositePolyStar::Type::kStar, // "sy": 1
249 CompositePolyStar::Type::kPoly, // "sy": 2
250 };
251
252 const auto type = ParseInt(jstar["sy"], 0) - 1;
253 if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gTypes))) {
254 LogFail(jstar, "Unknown polystar type");
255 return nullptr;
256 }
257
258 auto path_node = sksg::Path::Make();
259 auto composite = sk_make_sp<CompositePolyStar>(path_node, gTypes[type]);
260
Florin Malitaf9590922018-01-09 11:56:09 -0500261 BindProperty<VectorValue>(jstar["p"], ctx, composite,
262 [](CompositePolyStar* node, const VectorValue& p) {
263 node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
264 });
265 BindProperty<ScalarValue>(jstar["pt"], ctx, composite,
266 [](CompositePolyStar* node, const ScalarValue& pt) {
267 node->setPointCount(pt);
268 });
269 BindProperty<ScalarValue>(jstar["ir"], ctx, composite,
270 [](CompositePolyStar* node, const ScalarValue& ir) {
271 node->setInnerRadius(ir);
272 });
273 BindProperty<ScalarValue>(jstar["or"], ctx, composite,
274 [](CompositePolyStar* node, const ScalarValue& otr) {
Florin Malita9661b982018-01-06 14:25:49 -0500275 node->setOuterRadius(otr);
276 });
Florin Malitaf9590922018-01-09 11:56:09 -0500277 BindProperty<ScalarValue>(jstar["is"], ctx, composite,
278 [](CompositePolyStar* node, const ScalarValue& is) {
Florin Malita9661b982018-01-06 14:25:49 -0500279 node->setInnerRoundness(is);
280 });
Florin Malitaf9590922018-01-09 11:56:09 -0500281 BindProperty<ScalarValue>(jstar["os"], ctx, composite,
282 [](CompositePolyStar* node, const ScalarValue& os) {
Florin Malita9661b982018-01-06 14:25:49 -0500283 node->setOuterRoundness(os);
284 });
Florin Malitaf9590922018-01-09 11:56:09 -0500285 BindProperty<ScalarValue>(jstar["r"], ctx, composite,
286 [](CompositePolyStar* node, const ScalarValue& r) {
287 node->setRotation(r);
288 });
Florin Malita02a32b02018-01-04 11:27:09 -0500289
290 return path_node;
291}
292
Florin Malita6aaee592018-01-12 12:25:09 -0500293sk_sp<sksg::Color> AttachColor(const Json::Value& obj, AttachContext* ctx) {
Florin Malita094ccde2017-12-30 12:27:00 -0500294 SkASSERT(obj.isObject());
295
296 auto color_node = sksg::Color::Make(SK_ColorBLACK);
Florin Malita1586d852018-01-12 14:27:39 -0500297 auto color_attached = BindProperty<VectorValue>(obj["c"], ctx, color_node,
298 [](sksg::Color* node, const VectorValue& c) {
Florin Malitaf9590922018-01-09 11:56:09 -0500299 node->setColor(ValueTraits<VectorValue>::As<SkColor>(c));
300 });
Florin Malita094ccde2017-12-30 12:27:00 -0500301
Florin Malita1586d852018-01-12 14:27:39 -0500302 return color_attached ? color_node : nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500303}
304
Florin Malita6aaee592018-01-12 12:25:09 -0500305sk_sp<sksg::Gradient> AttachGradient(const Json::Value& obj, AttachContext* ctx) {
306 SkASSERT(obj.isObject());
Florin Malita094ccde2017-12-30 12:27:00 -0500307
Florin Malita6aaee592018-01-12 12:25:09 -0500308 const auto& stops = obj["g"];
309 if (!stops.isObject())
Florin Malita094ccde2017-12-30 12:27:00 -0500310 return nullptr;
311
Florin Malita6aaee592018-01-12 12:25:09 -0500312 const auto stopCount = ParseInt(stops["p"], -1);
313 if (stopCount < 0)
314 return nullptr;
315
316 sk_sp<sksg::Gradient> gradient_node;
317 sk_sp<CompositeGradient> composite;
318
319 if (ParseInt(obj["t"], 1) == 1) {
320 auto linear_node = sksg::LinearGradient::Make();
321 composite = sk_make_sp<CompositeLinearGradient>(linear_node, stopCount);
322 gradient_node = std::move(linear_node);
323 } else {
324 auto radial_node = sksg::RadialGradient::Make();
325 composite = sk_make_sp<CompositeRadialGradient>(radial_node, stopCount);
326
327 // TODO: highlight, angle
328 gradient_node = std::move(radial_node);
329 }
330
331 BindProperty<VectorValue>(stops["k"], ctx, composite,
332 [](CompositeGradient* node, const VectorValue& stops) {
333 node->setColorStops(stops);
334 });
335 BindProperty<VectorValue>(obj["s"], ctx, composite,
336 [](CompositeGradient* node, const VectorValue& s) {
337 node->setStartPoint(ValueTraits<VectorValue>::As<SkPoint>(s));
338 });
339 BindProperty<VectorValue>(obj["e"], ctx, composite,
340 [](CompositeGradient* node, const VectorValue& e) {
341 node->setEndPoint(ValueTraits<VectorValue>::As<SkPoint>(e));
342 });
343
344 return gradient_node;
345}
346
Florin Malita1586d852018-01-12 14:27:39 -0500347sk_sp<sksg::PaintNode> AttachPaint(const Json::Value& jpaint, AttachContext* ctx,
Florin Malita6aaee592018-01-12 12:25:09 -0500348 sk_sp<sksg::PaintNode> paint_node) {
349 if (paint_node) {
350 paint_node->setAntiAlias(true);
351
Florin Malita1586d852018-01-12 14:27:39 -0500352 BindProperty<ScalarValue>(jpaint["o"], ctx, paint_node,
353 [](sksg::PaintNode* node, const ScalarValue& o) {
354 // BM opacity is [0..100]
355 node->setOpacity(o * 0.01f);
356 });
Florin Malita6aaee592018-01-12 12:25:09 -0500357 }
358
359 return paint_node;
360}
361
362sk_sp<sksg::PaintNode> AttachStroke(const Json::Value& jstroke, AttachContext* ctx,
363 sk_sp<sksg::PaintNode> stroke_node) {
364 SkASSERT(jstroke.isObject());
365
366 if (!stroke_node)
367 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500368
369 stroke_node->setStyle(SkPaint::kStroke_Style);
370
Florin Malitaf9590922018-01-09 11:56:09 -0500371 auto width_attached = BindProperty<ScalarValue>(jstroke["w"], ctx, stroke_node,
Florin Malita6aaee592018-01-12 12:25:09 -0500372 [](sksg::PaintNode* node, const ScalarValue& w) {
Florin Malitaf9590922018-01-09 11:56:09 -0500373 node->setStrokeWidth(w);
374 });
Florin Malita094ccde2017-12-30 12:27:00 -0500375 if (!width_attached)
376 return nullptr;
377
378 stroke_node->setStrokeMiter(ParseScalar(jstroke["ml"], 4));
379
380 static constexpr SkPaint::Join gJoins[] = {
381 SkPaint::kMiter_Join,
382 SkPaint::kRound_Join,
383 SkPaint::kBevel_Join,
384 };
385 stroke_node->setStrokeJoin(gJoins[SkTPin<int>(ParseInt(jstroke["lj"], 1) - 1,
386 0, SK_ARRAY_COUNT(gJoins) - 1)]);
387
388 static constexpr SkPaint::Cap gCaps[] = {
389 SkPaint::kButt_Cap,
390 SkPaint::kRound_Cap,
391 SkPaint::kSquare_Cap,
392 };
393 stroke_node->setStrokeCap(gCaps[SkTPin<int>(ParseInt(jstroke["lc"], 1) - 1,
394 0, SK_ARRAY_COUNT(gCaps) - 1)]);
395
396 return stroke_node;
397}
398
Florin Malita6aaee592018-01-12 12:25:09 -0500399sk_sp<sksg::PaintNode> AttachColorFill(const Json::Value& jfill, AttachContext* ctx) {
400 SkASSERT(jfill.isObject());
401
402 return AttachPaint(jfill, ctx, AttachColor(jfill, ctx));
403}
404
405sk_sp<sksg::PaintNode> AttachGradientFill(const Json::Value& jfill, AttachContext* ctx) {
406 SkASSERT(jfill.isObject());
407
408 return AttachPaint(jfill, ctx, AttachGradient(jfill, ctx));
409}
410
411sk_sp<sksg::PaintNode> AttachColorStroke(const Json::Value& jstroke, AttachContext* ctx) {
412 SkASSERT(jstroke.isObject());
413
414 return AttachStroke(jstroke, ctx, AttachPaint(jstroke, ctx, AttachColor(jstroke, ctx)));
415}
416
417sk_sp<sksg::PaintNode> AttachGradientStroke(const Json::Value& jstroke, AttachContext* ctx) {
418 SkASSERT(jstroke.isObject());
419
420 return AttachStroke(jstroke, ctx, AttachPaint(jstroke, ctx, AttachGradient(jstroke, ctx)));
421}
422
Florin Malitae6345d92018-01-03 23:37:54 -0500423std::vector<sk_sp<sksg::GeometryNode>> AttachMergeGeometryEffect(
424 const Json::Value& jmerge, AttachContext* ctx, std::vector<sk_sp<sksg::GeometryNode>>&& geos) {
425 std::vector<sk_sp<sksg::GeometryNode>> merged;
426
427 static constexpr sksg::Merge::Mode gModes[] = {
428 sksg::Merge::Mode::kMerge, // "mm": 1
429 sksg::Merge::Mode::kUnion, // "mm": 2
430 sksg::Merge::Mode::kDifference, // "mm": 3
431 sksg::Merge::Mode::kIntersect, // "mm": 4
432 sksg::Merge::Mode::kXOR , // "mm": 5
433 };
434
Florin Malita51b8c892018-01-07 08:54:24 -0500435 const auto mode = gModes[SkTPin<int>(ParseInt(jmerge["mm"], 1) - 1,
436 0, SK_ARRAY_COUNT(gModes) - 1)];
Florin Malitae6345d92018-01-03 23:37:54 -0500437 merged.push_back(sksg::Merge::Make(std::move(geos), mode));
438
439 LOG("** Attached merge path effect, mode: %d\n", mode);
440
441 return merged;
442}
443
Florin Malita51b8c892018-01-07 08:54:24 -0500444std::vector<sk_sp<sksg::GeometryNode>> AttachTrimGeometryEffect(
445 const Json::Value& jtrim, AttachContext* ctx, std::vector<sk_sp<sksg::GeometryNode>>&& geos) {
446
447 enum class Mode {
448 kMerged, // "m": 1
449 kSeparate, // "m": 2
450 } gModes[] = { Mode::kMerged, Mode::kSeparate };
451
452 const auto mode = gModes[SkTPin<int>(ParseInt(jtrim["m"], 1) - 1,
453 0, SK_ARRAY_COUNT(gModes) - 1)];
454
455 std::vector<sk_sp<sksg::GeometryNode>> inputs;
456 if (mode == Mode::kMerged) {
457 inputs.push_back(sksg::Merge::Make(std::move(geos), sksg::Merge::Mode::kMerge));
458 } else {
459 inputs = std::move(geos);
460 }
461
462 std::vector<sk_sp<sksg::GeometryNode>> trimmed;
463 trimmed.reserve(inputs.size());
464 for (const auto& i : inputs) {
465 const auto trim = sksg::TrimEffect::Make(i);
466 trimmed.push_back(trim);
Florin Malitaf9590922018-01-09 11:56:09 -0500467 BindProperty<ScalarValue>(jtrim["s"], ctx, trim,
468 [](sksg::TrimEffect* node, const ScalarValue& s) {
Florin Malita51b8c892018-01-07 08:54:24 -0500469 node->setStart(s * 0.01f);
470 });
Florin Malitaf9590922018-01-09 11:56:09 -0500471 BindProperty<ScalarValue>(jtrim["e"], ctx, trim,
472 [](sksg::TrimEffect* node, const ScalarValue& e) {
Florin Malita51b8c892018-01-07 08:54:24 -0500473 node->setEnd(e * 0.01f);
474 });
Florin Malitaf9590922018-01-09 11:56:09 -0500475 BindProperty<ScalarValue>(jtrim["o"], ctx, trim,
476 [](sksg::TrimEffect* node, const ScalarValue& o) {
Florin Malita055b0f52018-01-22 23:13:10 -0500477 node->setOffset(o / 360);
Florin Malita51b8c892018-01-07 08:54:24 -0500478 });
479 }
480
481 return trimmed;
482}
483
Florin Malita094ccde2017-12-30 12:27:00 -0500484using GeometryAttacherT = sk_sp<sksg::GeometryNode> (*)(const Json::Value&, AttachContext*);
485static constexpr GeometryAttacherT gGeometryAttachers[] = {
486 AttachPathGeometry,
Florin Malita2e1d7e22018-01-02 10:40:00 -0500487 AttachRRectGeometry,
Florin Malitafbc13f12018-01-04 10:26:35 -0500488 AttachEllipseGeometry,
Florin Malita02a32b02018-01-04 11:27:09 -0500489 AttachPolystarGeometry,
Florin Malita094ccde2017-12-30 12:27:00 -0500490};
491
492using PaintAttacherT = sk_sp<sksg::PaintNode> (*)(const Json::Value&, AttachContext*);
493static constexpr PaintAttacherT gPaintAttachers[] = {
Florin Malita6aaee592018-01-12 12:25:09 -0500494 AttachColorFill,
495 AttachColorStroke,
496 AttachGradientFill,
497 AttachGradientStroke,
Florin Malita094ccde2017-12-30 12:27:00 -0500498};
499
Florin Malitae6345d92018-01-03 23:37:54 -0500500using GeometryEffectAttacherT =
501 std::vector<sk_sp<sksg::GeometryNode>> (*)(const Json::Value&,
502 AttachContext*,
503 std::vector<sk_sp<sksg::GeometryNode>>&&);
504static constexpr GeometryEffectAttacherT gGeometryEffectAttachers[] = {
505 AttachMergeGeometryEffect,
Florin Malita51b8c892018-01-07 08:54:24 -0500506 AttachTrimGeometryEffect,
Florin Malitae6345d92018-01-03 23:37:54 -0500507};
508
Florin Malita094ccde2017-12-30 12:27:00 -0500509enum class ShapeType {
510 kGeometry,
Florin Malitae6345d92018-01-03 23:37:54 -0500511 kGeometryEffect,
Florin Malita094ccde2017-12-30 12:27:00 -0500512 kPaint,
513 kGroup,
Florin Malitadacc02b2017-12-31 09:12:31 -0500514 kTransform,
Florin Malita094ccde2017-12-30 12:27:00 -0500515};
516
517struct ShapeInfo {
518 const char* fTypeString;
519 ShapeType fShapeType;
520 uint32_t fAttacherIndex; // index into respective attacher tables
521};
522
523const ShapeInfo* FindShapeInfo(const Json::Value& shape) {
524 static constexpr ShapeInfo gShapeInfo[] = {
Florin Malitafbc13f12018-01-04 10:26:35 -0500525 { "el", ShapeType::kGeometry , 2 }, // ellipse -> AttachEllipseGeometry
Florin Malita6aaee592018-01-12 12:25:09 -0500526 { "fl", ShapeType::kPaint , 0 }, // fill -> AttachColorFill
527 { "gf", ShapeType::kPaint , 2 }, // gfill -> AttachGradientFill
Florin Malita16d0ad02018-01-19 15:07:29 -0500528 { "gr", ShapeType::kGroup , 0 }, // group -> Inline handler
Florin Malita6aaee592018-01-12 12:25:09 -0500529 { "gs", ShapeType::kPaint , 3 }, // gstroke -> AttachGradientStroke
Florin Malitae6345d92018-01-03 23:37:54 -0500530 { "mm", ShapeType::kGeometryEffect, 0 }, // merge -> AttachMergeGeometryEffect
Florin Malita02a32b02018-01-04 11:27:09 -0500531 { "rc", ShapeType::kGeometry , 1 }, // rrect -> AttachRRectGeometry
Florin Malitae6345d92018-01-03 23:37:54 -0500532 { "sh", ShapeType::kGeometry , 0 }, // shape -> AttachPathGeometry
Florin Malita02a32b02018-01-04 11:27:09 -0500533 { "sr", ShapeType::kGeometry , 3 }, // polystar -> AttachPolyStarGeometry
Florin Malita6aaee592018-01-12 12:25:09 -0500534 { "st", ShapeType::kPaint , 1 }, // stroke -> AttachColorStroke
Florin Malita51b8c892018-01-07 08:54:24 -0500535 { "tm", ShapeType::kGeometryEffect, 1 }, // trim -> AttachTrimGeometryEffect
Florin Malita16d0ad02018-01-19 15:07:29 -0500536 { "tr", ShapeType::kTransform , 0 }, // transform -> Inline handler
Florin Malita094ccde2017-12-30 12:27:00 -0500537 };
538
539 if (!shape.isObject())
540 return nullptr;
541
542 const auto& type = shape["ty"];
543 if (!type.isString())
544 return nullptr;
545
546 const auto* info = bsearch(type.asCString(),
547 gShapeInfo,
548 SK_ARRAY_COUNT(gShapeInfo),
549 sizeof(ShapeInfo),
550 [](const void* key, const void* info) {
551 return strcmp(static_cast<const char*>(key),
552 static_cast<const ShapeInfo*>(info)->fTypeString);
553 });
554
555 return static_cast<const ShapeInfo*>(info);
556}
557
Florin Malita16d0ad02018-01-19 15:07:29 -0500558struct GeometryEffectRec {
559 const Json::Value& fJson;
560 GeometryEffectAttacherT fAttach;
561};
562
Florin Malitaca4439f2018-01-23 10:31:59 -0500563struct AttachShapeContext {
564 AttachShapeContext(AttachContext* ctx,
565 std::vector<sk_sp<sksg::GeometryNode>>* geos,
566 std::vector<GeometryEffectRec>* effects,
567 size_t committedAnimators)
568 : fCtx(ctx)
569 , fGeometryStack(geos)
570 , fGeometryEffectStack(effects)
571 , fCommittedAnimators(committedAnimators) {}
572
573 AttachContext* fCtx;
574 std::vector<sk_sp<sksg::GeometryNode>>* fGeometryStack;
575 std::vector<GeometryEffectRec>* fGeometryEffectStack;
576 size_t fCommittedAnimators;
577};
578
579sk_sp<sksg::RenderNode> AttachShape(const Json::Value& jshape, AttachShapeContext* shapeCtx) {
Florin Malita16d0ad02018-01-19 15:07:29 -0500580 if (!jshape.isArray())
Florin Malita094ccde2017-12-30 12:27:00 -0500581 return nullptr;
582
Florin Malitaca4439f2018-01-23 10:31:59 -0500583 SkDEBUGCODE(const auto initialGeometryEffects = shapeCtx->fGeometryEffectStack->size();)
Florin Malita094ccde2017-12-30 12:27:00 -0500584
Florin Malita16d0ad02018-01-19 15:07:29 -0500585 sk_sp<sksg::Group> shape_group = sksg::Group::Make();
586 sk_sp<sksg::RenderNode> shape_wrapper = shape_group;
587 sk_sp<sksg::Matrix> shape_matrix;
Florin Malita094ccde2017-12-30 12:27:00 -0500588
Florin Malita16d0ad02018-01-19 15:07:29 -0500589 struct ShapeRec {
590 const Json::Value& fJson;
591 const ShapeInfo& fInfo;
592 };
593
594 // First pass (bottom->top):
595 //
596 // * pick up the group transform and opacity
597 // * push local geometry effects onto the stack
598 // * store recs for next pass
599 //
600 std::vector<ShapeRec> recs;
601 for (Json::ArrayIndex i = 0; i < jshape.size(); ++i) {
602 const auto& s = jshape[jshape.size() - 1 - i];
Florin Malita094ccde2017-12-30 12:27:00 -0500603 const auto* info = FindShapeInfo(s);
604 if (!info) {
605 LogFail(s.isObject() ? s["ty"] : s, "Unknown shape");
606 continue;
607 }
608
Florin Malita16d0ad02018-01-19 15:07:29 -0500609 recs.push_back({ s, *info });
610
Florin Malita094ccde2017-12-30 12:27:00 -0500611 switch (info->fShapeType) {
Florin Malita16d0ad02018-01-19 15:07:29 -0500612 case ShapeType::kTransform:
Florin Malitaca4439f2018-01-23 10:31:59 -0500613 if ((shape_matrix = AttachMatrix(s, shapeCtx->fCtx, nullptr))) {
Florin Malita16d0ad02018-01-19 15:07:29 -0500614 shape_wrapper = sksg::Transform::Make(std::move(shape_wrapper), shape_matrix);
615 }
Florin Malitaca4439f2018-01-23 10:31:59 -0500616 shape_wrapper = AttachOpacity(s, shapeCtx->fCtx, std::move(shape_wrapper));
Florin Malita16d0ad02018-01-19 15:07:29 -0500617 break;
618 case ShapeType::kGeometryEffect:
619 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGeometryEffectAttachers));
Florin Malitaca4439f2018-01-23 10:31:59 -0500620 shapeCtx->fGeometryEffectStack->push_back(
Florin Malita16d0ad02018-01-19 15:07:29 -0500621 { s, gGeometryEffectAttachers[info->fAttacherIndex] });
622 break;
623 default:
624 break;
625 }
626 }
627
628 // Second pass (top -> bottom, after 2x reverse):
629 //
630 // * track local geometry
631 // * emit local paints
632 //
633 std::vector<sk_sp<sksg::GeometryNode>> geos;
634 std::vector<sk_sp<sksg::RenderNode >> draws;
635 for (auto rec = recs.rbegin(); rec != recs.rend(); ++rec) {
636 switch (rec->fInfo.fShapeType) {
Florin Malita094ccde2017-12-30 12:27:00 -0500637 case ShapeType::kGeometry: {
Florin Malita16d0ad02018-01-19 15:07:29 -0500638 SkASSERT(rec->fInfo.fAttacherIndex < SK_ARRAY_COUNT(gGeometryAttachers));
Florin Malitaca4439f2018-01-23 10:31:59 -0500639 if (auto geo = gGeometryAttachers[rec->fInfo.fAttacherIndex](rec->fJson,
640 shapeCtx->fCtx)) {
Florin Malita094ccde2017-12-30 12:27:00 -0500641 geos.push_back(std::move(geo));
642 }
643 } break;
Florin Malitae6345d92018-01-03 23:37:54 -0500644 case ShapeType::kGeometryEffect: {
Florin Malita16d0ad02018-01-19 15:07:29 -0500645 // Apply the current effect and pop from the stack.
646 SkASSERT(rec->fInfo.fAttacherIndex < SK_ARRAY_COUNT(gGeometryEffectAttachers));
Florin Malitaee6de4b2018-01-21 11:13:17 -0500647 if (!geos.empty()) {
648 geos = gGeometryEffectAttachers[rec->fInfo.fAttacherIndex](rec->fJson,
Florin Malitaca4439f2018-01-23 10:31:59 -0500649 shapeCtx->fCtx,
Florin Malitaee6de4b2018-01-21 11:13:17 -0500650 std::move(geos));
651 }
Florin Malita16d0ad02018-01-19 15:07:29 -0500652
Florin Malitaca4439f2018-01-23 10:31:59 -0500653 SkASSERT(shapeCtx->fGeometryEffectStack->back().fJson == rec->fJson);
654 SkASSERT(shapeCtx->fGeometryEffectStack->back().fAttach ==
Florin Malita16d0ad02018-01-19 15:07:29 -0500655 gGeometryEffectAttachers[rec->fInfo.fAttacherIndex]);
Florin Malitaca4439f2018-01-23 10:31:59 -0500656 shapeCtx->fGeometryEffectStack->pop_back();
Florin Malita094ccde2017-12-30 12:27:00 -0500657 } break;
658 case ShapeType::kGroup: {
Florin Malitaca4439f2018-01-23 10:31:59 -0500659 AttachShapeContext groupShapeCtx(shapeCtx->fCtx,
660 &geos,
661 shapeCtx->fGeometryEffectStack,
662 shapeCtx->fCommittedAnimators);
663 if (auto subgroup = AttachShape(rec->fJson["it"], &groupShapeCtx)) {
Florin Malita16d0ad02018-01-19 15:07:29 -0500664 draws.push_back(std::move(subgroup));
Florin Malitaca4439f2018-01-23 10:31:59 -0500665 SkASSERT(groupShapeCtx.fCommittedAnimators >= shapeCtx->fCommittedAnimators);
666 shapeCtx->fCommittedAnimators = groupShapeCtx.fCommittedAnimators;
Florin Malita094ccde2017-12-30 12:27:00 -0500667 }
668 } break;
Florin Malita16d0ad02018-01-19 15:07:29 -0500669 case ShapeType::kPaint: {
670 SkASSERT(rec->fInfo.fAttacherIndex < SK_ARRAY_COUNT(gPaintAttachers));
Florin Malitaca4439f2018-01-23 10:31:59 -0500671 auto paint = gPaintAttachers[rec->fInfo.fAttacherIndex](rec->fJson, shapeCtx->fCtx);
Florin Malita16d0ad02018-01-19 15:07:29 -0500672 if (!paint || geos.empty())
673 break;
674
675 auto drawGeos = geos;
676
677 // Apply all pending effects from the stack.
Florin Malitaca4439f2018-01-23 10:31:59 -0500678 for (auto it = shapeCtx->fGeometryEffectStack->rbegin();
679 it != shapeCtx->fGeometryEffectStack->rend(); ++it) {
680 drawGeos = it->fAttach(it->fJson, shapeCtx->fCtx, std::move(drawGeos));
Florin Malita18eafd92018-01-04 21:11:55 -0500681 }
Florin Malita16d0ad02018-01-19 15:07:29 -0500682
683 // If we still have multiple geos, reduce using 'merge'.
684 auto geo = drawGeos.size() > 1
685 ? sksg::Merge::Make(std::move(drawGeos), sksg::Merge::Mode::kMerge)
686 : drawGeos[0];
687
688 SkASSERT(geo);
689 draws.push_back(sksg::Draw::Make(std::move(geo), std::move(paint)));
Florin Malitaca4439f2018-01-23 10:31:59 -0500690 shapeCtx->fCommittedAnimators = shapeCtx->fCtx->fAnimators.size();
Florin Malitadacc02b2017-12-31 09:12:31 -0500691 } break;
Florin Malita16d0ad02018-01-19 15:07:29 -0500692 default:
693 break;
Florin Malita094ccde2017-12-30 12:27:00 -0500694 }
695 }
696
Florin Malita16d0ad02018-01-19 15:07:29 -0500697 // By now we should have popped all local geometry effects.
Florin Malitaca4439f2018-01-23 10:31:59 -0500698 SkASSERT(shapeCtx->fGeometryEffectStack->size() == initialGeometryEffects);
Florin Malita16d0ad02018-01-19 15:07:29 -0500699
700 // Push transformed local geometries to parent list, for subsequent paints.
701 for (const auto& geo : geos) {
Florin Malitaca4439f2018-01-23 10:31:59 -0500702 shapeCtx->fGeometryStack->push_back(shape_matrix
Florin Malita16d0ad02018-01-19 15:07:29 -0500703 ? sksg::GeometryTransform::Make(std::move(geo), shape_matrix)
704 : std::move(geo));
Florin Malita094ccde2017-12-30 12:27:00 -0500705 }
706
Florin Malita16d0ad02018-01-19 15:07:29 -0500707 // Emit local draws reversed (bottom->top, per spec).
708 for (auto it = draws.rbegin(); it != draws.rend(); ++it) {
709 shape_group->addChild(std::move(*it));
Florin Malita2a8275b2018-01-02 12:52:43 -0500710 }
711
Florin Malita16d0ad02018-01-19 15:07:29 -0500712 return draws.empty() ? nullptr : shape_wrapper;
Florin Malita094ccde2017-12-30 12:27:00 -0500713}
714
715sk_sp<sksg::RenderNode> AttachCompLayer(const Json::Value& layer, AttachContext* ctx) {
716 SkASSERT(layer.isObject());
717
718 auto refId = ParseString(layer["refId"], "");
719 if (refId.isEmpty()) {
720 LOG("!! Comp layer missing refId\n");
721 return nullptr;
722 }
723
724 const auto* comp = ctx->fAssets.find(refId);
725 if (!comp) {
726 LOG("!! Pre-comp not found: '%s'\n", refId.c_str());
727 return nullptr;
728 }
729
730 // TODO: cycle detection
731 return AttachComposition(**comp, ctx);
732}
733
Florin Malita0e66fba2018-01-09 17:10:18 -0500734sk_sp<sksg::RenderNode> AttachSolidLayer(const Json::Value& jlayer, AttachContext*) {
735 SkASSERT(jlayer.isObject());
Florin Malita094ccde2017-12-30 12:27:00 -0500736
Florin Malita0e66fba2018-01-09 17:10:18 -0500737 const auto size = SkSize::Make(ParseScalar(jlayer["sw"], -1),
738 ParseScalar(jlayer["sh"], -1));
739 const auto hex = ParseString(jlayer["sc"], "");
740 uint32_t c;
741 if (size.isEmpty() ||
742 !hex.startsWith("#") ||
743 !SkParse::FindHex(hex.c_str() + 1, &c)) {
744 LogFail(jlayer, "Could not parse solid layer");
745 return nullptr;
746 }
747
748 const SkColor color = 0xff000000 | c;
749
750 return sksg::Draw::Make(sksg::Rect::Make(SkRect::MakeSize(size)),
751 sksg::Color::Make(color));
Florin Malita094ccde2017-12-30 12:27:00 -0500752}
753
Florin Malita49328072018-01-08 12:51:12 -0500754sk_sp<sksg::RenderNode> AttachImageAsset(const Json::Value& jimage, AttachContext* ctx) {
755 SkASSERT(jimage.isObject());
756
757 const auto name = ParseString(jimage["p"], ""),
758 path = ParseString(jimage["u"], "");
759 if (name.isEmpty())
760 return nullptr;
761
762 // TODO: plumb resource paths explicitly to ResourceProvider?
763 const auto resName = path.isEmpty() ? name : SkOSPath::Join(path.c_str(), name.c_str());
764 const auto resStream = ctx->fResources.openStream(resName.c_str());
765 if (!resStream || !resStream->hasLength()) {
766 LOG("!! Could not load image resource: %s\n", resName.c_str());
767 return nullptr;
768 }
769
770 // TODO: non-intrisic image sizing
771 return sksg::Image::Make(
772 SkImage::MakeFromEncoded(SkData::MakeFromStream(resStream.get(), resStream->getLength())));
773}
774
775sk_sp<sksg::RenderNode> AttachImageLayer(const Json::Value& layer, AttachContext* ctx) {
Florin Malita094ccde2017-12-30 12:27:00 -0500776 SkASSERT(layer.isObject());
777
Florin Malita49328072018-01-08 12:51:12 -0500778 auto refId = ParseString(layer["refId"], "");
779 if (refId.isEmpty()) {
780 LOG("!! Image layer missing refId\n");
781 return nullptr;
782 }
783
784 const auto* jimage = ctx->fAssets.find(refId);
785 if (!jimage) {
786 LOG("!! Image asset not found: '%s'\n", refId.c_str());
787 return nullptr;
788 }
789
790 return AttachImageAsset(**jimage, ctx);
Florin Malita094ccde2017-12-30 12:27:00 -0500791}
792
793sk_sp<sksg::RenderNode> AttachNullLayer(const Json::Value& layer, AttachContext*) {
794 SkASSERT(layer.isObject());
795
Florin Malita18eafd92018-01-04 21:11:55 -0500796 // Null layers are used solely to drive dependent transforms,
797 // but we use free-floating sksg::Matrices for that purpose.
Florin Malita094ccde2017-12-30 12:27:00 -0500798 return nullptr;
799}
800
801sk_sp<sksg::RenderNode> AttachShapeLayer(const Json::Value& layer, AttachContext* ctx) {
802 SkASSERT(layer.isObject());
803
804 LOG("** Attaching shape layer ind: %d\n", ParseInt(layer["ind"], 0));
805
Florin Malita16d0ad02018-01-19 15:07:29 -0500806 std::vector<sk_sp<sksg::GeometryNode>> geometryStack;
807 std::vector<GeometryEffectRec> geometryEffectStack;
Florin Malitaca4439f2018-01-23 10:31:59 -0500808 AttachShapeContext shapeCtx(ctx, &geometryStack, &geometryEffectStack, ctx->fAnimators.size());
809 auto shapeNode = AttachShape(layer["shapes"], &shapeCtx);
810
811 // Trim uncommitted animators: AttachShape consumes effects on the fly, and greedily attaches
812 // geometries => at the end, we can end up with unused geometries, which are nevertheless alive
813 // due to attached animators. To avoid this, we track committed animators and discard the
814 // orphans here.
815 SkASSERT(shapeCtx.fCommittedAnimators <= ctx->fAnimators.size());
816 ctx->fAnimators.resize(shapeCtx.fCommittedAnimators);
817
818 return shapeNode;
Florin Malita094ccde2017-12-30 12:27:00 -0500819}
820
821sk_sp<sksg::RenderNode> AttachTextLayer(const Json::Value& layer, AttachContext*) {
822 SkASSERT(layer.isObject());
823
824 LOG("?? Text layer stub\n");
825 return nullptr;
826}
827
Florin Malita18eafd92018-01-04 21:11:55 -0500828struct AttachLayerContext {
829 AttachLayerContext(const Json::Value& jlayers, AttachContext* ctx)
830 : fLayerList(jlayers), fCtx(ctx) {}
831
832 const Json::Value& fLayerList;
833 AttachContext* fCtx;
834 std::unordered_map<const Json::Value*, sk_sp<sksg::Matrix>> fLayerMatrixCache;
835 std::unordered_map<int, const Json::Value*> fLayerIndexCache;
Florin Malita5f9102f2018-01-10 13:36:22 -0500836 sk_sp<sksg::RenderNode> fCurrentMatte;
Florin Malita18eafd92018-01-04 21:11:55 -0500837
838 const Json::Value* findLayer(int index) {
839 SkASSERT(fLayerList.isArray());
840
841 if (index < 0) {
842 return nullptr;
843 }
844
845 const auto cached = fLayerIndexCache.find(index);
846 if (cached != fLayerIndexCache.end()) {
847 return cached->second;
848 }
849
850 for (const auto& l : fLayerList) {
851 if (!l.isObject()) {
852 continue;
853 }
854
855 if (ParseInt(l["ind"], -1) == index) {
856 fLayerIndexCache.insert(std::make_pair(index, &l));
857 return &l;
858 }
859 }
860
861 return nullptr;
862 }
863
864 sk_sp<sksg::Matrix> AttachLayerMatrix(const Json::Value& jlayer) {
865 SkASSERT(jlayer.isObject());
866
867 const auto cached = fLayerMatrixCache.find(&jlayer);
868 if (cached != fLayerMatrixCache.end()) {
869 return cached->second;
870 }
871
872 const auto* parentLayer = this->findLayer(ParseInt(jlayer["parent"], -1));
873
874 // TODO: cycle detection?
875 auto parentMatrix = (parentLayer && parentLayer != &jlayer)
876 ? this->AttachLayerMatrix(*parentLayer) : nullptr;
877
878 auto layerMatrix = AttachMatrix(jlayer["ks"], fCtx, std::move(parentMatrix));
879 fLayerMatrixCache.insert(std::make_pair(&jlayer, layerMatrix));
880
881 return layerMatrix;
882 }
883};
884
Florin Malita25366fa2018-01-23 13:37:59 -0500885SkBlendMode MaskBlendMode(char mode) {
886 switch (mode) {
887 case 'a': return SkBlendMode::kSrcOver; // Additive
888 case 's': return SkBlendMode::kExclusion; // Subtract
889 case 'i': return SkBlendMode::kDstIn; // Intersect
890 case 'l': return SkBlendMode::kLighten; // Lighten
891 case 'd': return SkBlendMode::kDarken; // Darken
892 case 'f': return SkBlendMode::kDifference; // Difference
893 default: break;
894 }
895
896 return SkBlendMode::kSrcOver;
897}
898
899sk_sp<sksg::RenderNode> AttachMask(const Json::Value& jmask,
900 AttachContext* ctx,
901 sk_sp<sksg::RenderNode> childNode) {
902 if (!jmask.isArray())
903 return childNode;
904
905 auto mask_group = sksg::Group::Make();
906
907 for (const auto& m : jmask) {
908 if (!m.isObject())
909 continue;
910
911 const auto inverted = ParseBool(m["inv"], false);
912 // TODO
913 if (inverted) {
914 LogFail(m, "Unsupported inverse mask");
915 continue;
916 }
917
918 auto mask_path = AttachPath(m["pt"], ctx);
919 if (!mask_path) {
920 LogFail(m, "Could not parse mask path");
921 continue;
922 }
923
924 auto mode = ParseString(m["mode"], "");
925 if (mode.size() != 1 || !strcmp(mode.c_str(), "n")) // "None" masks have no effect.
926 continue;
927
928 auto mask_paint = sksg::Color::Make(SK_ColorBLACK);
929 mask_paint->setBlendMode(MaskBlendMode(mode.c_str()[0]));
930 BindProperty<ScalarValue>(m["o"], ctx, mask_paint,
931 [](sksg::Color* node, const ScalarValue& o) { node->setOpacity(o * 0.01f); });
932
933 mask_group->addChild(sksg::Draw::Make(std::move(mask_path), std::move(mask_paint)));
934 }
935
936 return mask_group->empty()
937 ? childNode
938 : sksg::MaskEffect::Make(std::move(childNode), std::move(mask_group));
939}
940
Florin Malita18eafd92018-01-04 21:11:55 -0500941sk_sp<sksg::RenderNode> AttachLayer(const Json::Value& jlayer,
942 AttachLayerContext* layerCtx) {
943 if (!jlayer.isObject())
Florin Malita094ccde2017-12-30 12:27:00 -0500944 return nullptr;
945
946 using LayerAttacher = sk_sp<sksg::RenderNode> (*)(const Json::Value&, AttachContext*);
947 static constexpr LayerAttacher gLayerAttachers[] = {
948 AttachCompLayer, // 'ty': 0
949 AttachSolidLayer, // 'ty': 1
950 AttachImageLayer, // 'ty': 2
951 AttachNullLayer, // 'ty': 3
952 AttachShapeLayer, // 'ty': 4
953 AttachTextLayer, // 'ty': 5
954 };
955
Florin Malita18eafd92018-01-04 21:11:55 -0500956 int type = ParseInt(jlayer["ty"], -1);
Florin Malita094ccde2017-12-30 12:27:00 -0500957 if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gLayerAttachers))) {
958 return nullptr;
959 }
960
Florin Malita71cba8f2018-01-09 08:07:14 -0500961 // Layer content.
962 auto layer = gLayerAttachers[type](jlayer, layerCtx->fCtx);
Florin Malita25366fa2018-01-23 13:37:59 -0500963 // Optional layer mask.
964 layer = AttachMask(jlayer["masksProperties"], layerCtx->fCtx, std::move(layer));
965 // Optional layer transform.
Florin Malita71cba8f2018-01-09 08:07:14 -0500966 if (auto layerMatrix = layerCtx->AttachLayerMatrix(jlayer)) {
Florin Malita71cba8f2018-01-09 08:07:14 -0500967 layer = sksg::Transform::Make(std::move(layer), std::move(layerMatrix));
968 }
969 // Optional layer opacity.
970 layer = AttachOpacity(jlayer["ks"], layerCtx->fCtx, std::move(layer));
Florin Malita18eafd92018-01-04 21:11:55 -0500971
Florin Malita71cba8f2018-01-09 08:07:14 -0500972 // TODO: we should also disable related/inactive animators.
Florin Malita35efaa82018-01-22 12:57:06 -0500973 class Activator final : public sksg::Animator {
Florin Malita71cba8f2018-01-09 08:07:14 -0500974 public:
975 Activator(sk_sp<sksg::OpacityEffect> controlNode, float in, float out)
976 : fControlNode(std::move(controlNode))
977 , fIn(in)
978 , fOut(out) {}
979
Florin Malita35efaa82018-01-22 12:57:06 -0500980 void onTick(float t) override {
Florin Malita71cba8f2018-01-09 08:07:14 -0500981 // Keep the layer fully transparent except for its [in..out] lifespan.
982 // (note: opacity == 0 disables rendering, while opacity == 1 is a noop)
983 fControlNode->setOpacity(t >= fIn && t <= fOut ? 1 : 0);
984 }
985
986 private:
987 const sk_sp<sksg::OpacityEffect> fControlNode;
988 const float fIn,
989 fOut;
990 };
991
992 auto layerControl = sksg::OpacityEffect::Make(std::move(layer));
993 const auto in = ParseScalar(jlayer["ip"], 0),
994 out = ParseScalar(jlayer["op"], in);
995
996 if (in >= out || ! layerControl)
997 return nullptr;
998
999 layerCtx->fCtx->fAnimators.push_back(skstd::make_unique<Activator>(layerControl, in, out));
1000
Florin Malita5f9102f2018-01-10 13:36:22 -05001001 if (ParseBool(jlayer["td"], false)) {
1002 // This layer is a matte. We apply it as a mask to the next layer.
1003 layerCtx->fCurrentMatte = std::move(layerControl);
1004 return nullptr;
1005 }
1006
1007 if (layerCtx->fCurrentMatte) {
1008 // There is a pending matte. Apply and reset.
1009 return sksg::MaskEffect::Make(std::move(layerControl), std::move(layerCtx->fCurrentMatte));
1010 }
1011
Florin Malita71cba8f2018-01-09 08:07:14 -05001012 return layerControl;
Florin Malita094ccde2017-12-30 12:27:00 -05001013}
1014
1015sk_sp<sksg::RenderNode> AttachComposition(const Json::Value& comp, AttachContext* ctx) {
1016 if (!comp.isObject())
1017 return nullptr;
1018
Florin Malita18eafd92018-01-04 21:11:55 -05001019 const auto& jlayers = comp["layers"];
1020 if (!jlayers.isArray())
1021 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -05001022
Florin Malita18eafd92018-01-04 21:11:55 -05001023 SkSTArray<16, sk_sp<sksg::RenderNode>, true> layers;
1024 AttachLayerContext layerCtx(jlayers, ctx);
1025
1026 for (const auto& l : jlayers) {
1027 if (auto layer_fragment = AttachLayer(l, &layerCtx)) {
Florin Malita2a8275b2018-01-02 12:52:43 -05001028 layers.push_back(std::move(layer_fragment));
Florin Malita094ccde2017-12-30 12:27:00 -05001029 }
1030 }
1031
Florin Malita2a8275b2018-01-02 12:52:43 -05001032 if (layers.empty()) {
1033 return nullptr;
1034 }
1035
1036 // Layers are painted in bottom->top order.
1037 auto comp_group = sksg::Group::Make();
1038 for (int i = layers.count() - 1; i >= 0; --i) {
1039 comp_group->addChild(std::move(layers[i]));
1040 }
1041
1042 LOG("** Attached composition '%s': %d layers.\n",
1043 ParseString(comp["id"], "").c_str(), layers.count());
1044
Florin Malita094ccde2017-12-30 12:27:00 -05001045 return comp_group;
1046}
1047
1048} // namespace
1049
Florin Malita49328072018-01-08 12:51:12 -05001050std::unique_ptr<Animation> Animation::Make(SkStream* stream, const ResourceProvider& res) {
Florin Malita094ccde2017-12-30 12:27:00 -05001051 if (!stream->hasLength()) {
1052 // TODO: handle explicit buffering?
1053 LOG("!! cannot parse streaming content\n");
1054 return nullptr;
1055 }
1056
1057 Json::Value json;
1058 {
1059 auto data = SkData::MakeFromStream(stream, stream->getLength());
1060 if (!data) {
1061 LOG("!! could not read stream\n");
1062 return nullptr;
1063 }
1064
1065 Json::Reader reader;
1066
1067 auto dataStart = static_cast<const char*>(data->data());
1068 if (!reader.parse(dataStart, dataStart + data->size(), json, false) || !json.isObject()) {
1069 LOG("!! failed to parse json: %s\n", reader.getFormattedErrorMessages().c_str());
1070 return nullptr;
1071 }
1072 }
1073
1074 const auto version = ParseString(json["v"], "");
1075 const auto size = SkSize::Make(ParseScalar(json["w"], -1), ParseScalar(json["h"], -1));
1076 const auto fps = ParseScalar(json["fr"], -1);
1077
1078 if (size.isEmpty() || version.isEmpty() || fps < 0) {
1079 LOG("!! invalid animation params (version: %s, size: [%f %f], frame rate: %f)",
1080 version.c_str(), size.width(), size.height(), fps);
1081 return nullptr;
1082 }
1083
Florin Malita49328072018-01-08 12:51:12 -05001084 return std::unique_ptr<Animation>(new Animation(res, std::move(version), size, fps, json));
Florin Malita094ccde2017-12-30 12:27:00 -05001085}
1086
Florin Malita49328072018-01-08 12:51:12 -05001087std::unique_ptr<Animation> Animation::MakeFromFile(const char path[], const ResourceProvider* res) {
1088 class DirectoryResourceProvider final : public ResourceProvider {
1089 public:
1090 explicit DirectoryResourceProvider(SkString dir) : fDir(std::move(dir)) {}
1091
1092 std::unique_ptr<SkStream> openStream(const char resource[]) const override {
1093 const auto resPath = SkOSPath::Join(fDir.c_str(), resource);
1094 return SkStream::MakeFromFile(resPath.c_str());
1095 }
1096
1097 private:
1098 const SkString fDir;
1099 };
1100
1101 const auto jsonStream = SkStream::MakeFromFile(path);
1102 if (!jsonStream)
1103 return nullptr;
1104
1105 std::unique_ptr<ResourceProvider> defaultProvider;
1106 if (!res) {
1107 defaultProvider = skstd::make_unique<DirectoryResourceProvider>(SkOSPath::Dirname(path));
1108 }
1109
1110 return Make(jsonStream.get(), res ? *res : *defaultProvider);
1111}
1112
1113Animation::Animation(const ResourceProvider& resources,
1114 SkString version, const SkSize& size, SkScalar fps, const Json::Value& json)
Florin Malita094ccde2017-12-30 12:27:00 -05001115 : fVersion(std::move(version))
1116 , fSize(size)
1117 , fFrameRate(fps)
1118 , fInPoint(ParseScalar(json["ip"], 0))
1119 , fOutPoint(SkTMax(ParseScalar(json["op"], SK_ScalarMax), fInPoint)) {
1120
1121 AssetMap assets;
1122 for (const auto& asset : json["assets"]) {
1123 if (!asset.isObject()) {
1124 continue;
1125 }
1126
1127 assets.set(ParseString(asset["id"], ""), &asset);
1128 }
1129
Florin Malita35efaa82018-01-22 12:57:06 -05001130 sksg::Scene::AnimatorList animators;
1131 AttachContext ctx = { resources, assets, animators };
1132 auto root = AttachComposition(json, &ctx);
1133
1134 LOG("** Attached %d animators\n", animators.size());
1135
1136 fScene = sksg::Scene::Make(std::move(root), std::move(animators));
Florin Malita094ccde2017-12-30 12:27:00 -05001137
Florin Malitadb385732018-01-09 12:19:32 -05001138 // In case the client calls render before the first tick.
1139 this->animationTick(0);
Florin Malita094ccde2017-12-30 12:27:00 -05001140}
1141
1142Animation::~Animation() = default;
1143
Florin Malita35efaa82018-01-22 12:57:06 -05001144void Animation::setShowInval(bool show) {
1145 if (fScene) {
1146 fScene->setShowInval(show);
1147 }
1148}
1149
Mike Reed29859872018-01-08 08:25:27 -05001150void Animation::render(SkCanvas* canvas, const SkRect* dstR) const {
Florin Malita35efaa82018-01-22 12:57:06 -05001151 if (!fScene)
Florin Malita094ccde2017-12-30 12:27:00 -05001152 return;
1153
Mike Reed29859872018-01-08 08:25:27 -05001154 SkAutoCanvasRestore restore(canvas, true);
1155 const SkRect srcR = SkRect::MakeSize(this->size());
1156 if (dstR) {
1157 canvas->concat(SkMatrix::MakeRectToRect(srcR, *dstR, SkMatrix::kCenter_ScaleToFit));
1158 }
1159 canvas->clipRect(srcR);
Florin Malita35efaa82018-01-22 12:57:06 -05001160 fScene->render(canvas);
Florin Malita094ccde2017-12-30 12:27:00 -05001161}
1162
1163void Animation::animationTick(SkMSec ms) {
Florin Malita35efaa82018-01-22 12:57:06 -05001164 if (!fScene)
1165 return;
1166
Florin Malita094ccde2017-12-30 12:27:00 -05001167 // 't' in the BM model really means 'frame #'
1168 auto t = static_cast<float>(ms) * fFrameRate / 1000;
1169
1170 t = fInPoint + std::fmod(t, fOutPoint - fInPoint);
1171
Florin Malita35efaa82018-01-22 12:57:06 -05001172 fScene->animate(t);
Florin Malita094ccde2017-12-30 12:27:00 -05001173}
1174
Florin Malita54f65c42018-01-16 17:04:30 -05001175} // namespace skottie