blob: a13a04c03c6a65d28e1b47e0d050a323fc94c896 [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
8#include "Skotty.h"
9
10#include "SkCanvas.h"
11#include "SkottyAnimator.h"
12#include "SkottyPriv.h"
13#include "SkottyProperties.h"
14#include "SkData.h"
15#include "SkMakeUnique.h"
16#include "SkPaint.h"
17#include "SkPath.h"
18#include "SkPoint.h"
19#include "SkSGColor.h"
20#include "SkSGDraw.h"
21#include "SkSGInvalidationController.h"
22#include "SkSGGroup.h"
Florin Malitae6345d92018-01-03 23:37:54 -050023#include "SkSGMerge.h"
Florin Malita094ccde2017-12-30 12:27:00 -050024#include "SkSGPath.h"
Florin Malita2e1d7e22018-01-02 10:40:00 -050025#include "SkSGRect.h"
Florin Malita094ccde2017-12-30 12:27:00 -050026#include "SkSGTransform.h"
Florin Malita51b8c892018-01-07 08:54:24 -050027#include "SkSGTrimEffect.h"
Florin Malita094ccde2017-12-30 12:27:00 -050028#include "SkStream.h"
29#include "SkTArray.h"
30#include "SkTHash.h"
31
32#include <cmath>
Florin Malita18eafd92018-01-04 21:11:55 -050033#include <unordered_map>
Florin Malitae6345d92018-01-03 23:37:54 -050034#include <vector>
35
Florin Malita094ccde2017-12-30 12:27:00 -050036#include "stdlib.h"
37
38namespace skotty {
39
40namespace {
41
42using AssetMap = SkTHashMap<SkString, const Json::Value*>;
43
44struct AttachContext {
45 const AssetMap& fAssets;
46 SkTArray<std::unique_ptr<AnimatorBase>>& fAnimators;
47};
48
49bool LogFail(const Json::Value& json, const char* msg) {
50 const auto dump = json.toStyledString();
51 LOG("!! %s: %s", msg, dump.c_str());
52 return false;
53}
54
55// This is the workhorse for binding properties: depending on whether the property is animated,
56// it will either apply immediately or instantiate and attach a keyframe animator.
Florin Malita9661b982018-01-06 14:25:49 -050057template <typename ValueT, typename AttrT, typename NodeT>
Florin Malita094ccde2017-12-30 12:27:00 -050058bool AttachProperty(const Json::Value& jprop, AttachContext* ctx, const sk_sp<NodeT>& node,
Florin Malita9661b982018-01-06 14:25:49 -050059 typename Animator<ValueT, AttrT, NodeT>::ApplyFuncT&& apply) {
Florin Malita094ccde2017-12-30 12:27:00 -050060 if (!jprop.isObject())
61 return false;
62
63 if (!ParseBool(jprop["a"], false)) {
64 // Static property.
65 ValueT val;
66 if (!ValueT::Parse(jprop["k"], &val)) {
67 return LogFail(jprop, "Could not parse static property");
68 }
69
70 apply(node, val.template as<AttrT>());
71 } else {
72 // Keyframe property.
73 using AnimatorT = Animator<ValueT, AttrT, NodeT>;
74 auto animator = AnimatorT::Make(jprop["k"], node, std::move(apply));
75
76 if (!animator) {
77 return LogFail(jprop, "Could not instantiate keyframe animator");
78 }
79
80 ctx->fAnimators.push_back(std::move(animator));
81 }
82
83 return true;
84}
85
Florin Malita18eafd92018-01-04 21:11:55 -050086sk_sp<sksg::Matrix> AttachMatrix(const Json::Value& t, AttachContext* ctx,
87 sk_sp<sksg::Matrix> parentMatrix) {
88 if (!t.isObject())
89 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -050090
Florin Malita18eafd92018-01-04 21:11:55 -050091 auto matrix = sksg::Matrix::Make(SkMatrix::I(), std::move(parentMatrix));
92 auto composite = sk_make_sp<CompositeTransform>(matrix);
93 auto anchor_attached = AttachProperty<VectorValue, SkPoint>(t["a"], ctx, composite,
Florin Malita094ccde2017-12-30 12:27:00 -050094 [](const sk_sp<CompositeTransform>& node, const SkPoint& a) {
95 node->setAnchorPoint(a);
96 });
Florin Malita18eafd92018-01-04 21:11:55 -050097 auto position_attached = AttachProperty<VectorValue, SkPoint>(t["p"], ctx, composite,
Florin Malita094ccde2017-12-30 12:27:00 -050098 [](const sk_sp<CompositeTransform>& node, const SkPoint& p) {
99 node->setPosition(p);
100 });
Florin Malita18eafd92018-01-04 21:11:55 -0500101 auto scale_attached = AttachProperty<VectorValue, SkVector>(t["s"], ctx, composite,
Florin Malita094ccde2017-12-30 12:27:00 -0500102 [](const sk_sp<CompositeTransform>& node, const SkVector& s) {
103 node->setScale(s);
104 });
Florin Malita18eafd92018-01-04 21:11:55 -0500105 auto rotation_attached = AttachProperty<ScalarValue, SkScalar>(t["r"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500106 [](const sk_sp<CompositeTransform>& node, const SkScalar& r) {
Florin Malita094ccde2017-12-30 12:27:00 -0500107 node->setRotation(r);
108 });
Florin Malita18eafd92018-01-04 21:11:55 -0500109 auto skew_attached = AttachProperty<ScalarValue, SkScalar>(t["sk"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500110 [](const sk_sp<CompositeTransform>& node, const SkScalar& sk) {
Florin Malita094ccde2017-12-30 12:27:00 -0500111 node->setSkew(sk);
112 });
Florin Malita18eafd92018-01-04 21:11:55 -0500113 auto skewaxis_attached = AttachProperty<ScalarValue, SkScalar>(t["sa"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500114 [](const sk_sp<CompositeTransform>& node, const SkScalar& sa) {
Florin Malita094ccde2017-12-30 12:27:00 -0500115 node->setSkewAxis(sa);
116 });
117
118 if (!anchor_attached &&
119 !position_attached &&
120 !scale_attached &&
121 !rotation_attached &&
122 !skew_attached &&
123 !skewaxis_attached) {
124 LogFail(t, "Could not parse transform");
Florin Malita18eafd92018-01-04 21:11:55 -0500125 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500126 }
127
Florin Malita18eafd92018-01-04 21:11:55 -0500128 return matrix;
Florin Malita094ccde2017-12-30 12:27:00 -0500129}
130
131sk_sp<sksg::RenderNode> AttachShape(const Json::Value&, AttachContext* ctx);
132sk_sp<sksg::RenderNode> AttachComposition(const Json::Value&, AttachContext* ctx);
133
134sk_sp<sksg::RenderNode> AttachShapeGroup(const Json::Value& jgroup, AttachContext* ctx) {
135 SkASSERT(jgroup.isObject());
136
137 return AttachShape(jgroup["it"], ctx);
138}
139
140sk_sp<sksg::GeometryNode> AttachPathGeometry(const Json::Value& jpath, AttachContext* ctx) {
141 SkASSERT(jpath.isObject());
142
143 auto path_node = sksg::Path::Make();
144 auto path_attached = AttachProperty<ShapeValue, SkPath>(jpath["ks"], ctx, path_node,
145 [](const sk_sp<sksg::Path>& node, const SkPath& p) { node->setPath(p); });
146
147 if (path_attached)
148 LOG("** Attached path geometry - verbs: %d\n", path_node->getPath().countVerbs());
149
150 return path_attached ? path_node : nullptr;
151}
152
Florin Malita2e1d7e22018-01-02 10:40:00 -0500153sk_sp<sksg::GeometryNode> AttachRRectGeometry(const Json::Value& jrect, AttachContext* ctx) {
154 SkASSERT(jrect.isObject());
155
156 auto rect_node = sksg::RRect::Make();
157 auto composite = sk_make_sp<CompositeRRect>(rect_node);
158
159 auto p_attached = AttachProperty<VectorValue, SkPoint>(jrect["p"], ctx, composite,
160 [](const sk_sp<CompositeRRect>& node, const SkPoint& pos) { node->setPosition(pos); });
161 auto s_attached = AttachProperty<VectorValue, SkSize>(jrect["s"], ctx, composite,
162 [](const sk_sp<CompositeRRect>& node, const SkSize& sz) { node->setSize(sz); });
163 auto r_attached = AttachProperty<ScalarValue, SkScalar>(jrect["r"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500164 [](const sk_sp<CompositeRRect>& node, const SkScalar& radius) {
Florin Malitafbc13f12018-01-04 10:26:35 -0500165 node->setRadius(SkSize::Make(radius, radius));
166 });
Florin Malita2e1d7e22018-01-02 10:40:00 -0500167
168 if (!p_attached && !s_attached && !r_attached) {
169 return nullptr;
170 }
171
Florin Malitafbc13f12018-01-04 10:26:35 -0500172 LOG("** Attached (r)rect geometry\n");
173
174 return rect_node;
175}
176
177sk_sp<sksg::GeometryNode> AttachEllipseGeometry(const Json::Value& jellipse, AttachContext* ctx) {
178 SkASSERT(jellipse.isObject());
179
180 auto rect_node = sksg::RRect::Make();
181 auto composite = sk_make_sp<CompositeRRect>(rect_node);
182
183 auto p_attached = AttachProperty<VectorValue, SkPoint>(jellipse["p"], ctx, composite,
184 [](const sk_sp<CompositeRRect>& node, const SkPoint& pos) { node->setPosition(pos); });
185 auto s_attached = AttachProperty<VectorValue, SkSize>(jellipse["s"], ctx, composite,
186 [](const sk_sp<CompositeRRect>& node, const SkSize& sz) {
187 node->setSize(sz);
188 node->setRadius(SkSize::Make(sz.width() / 2, sz.height() / 2));
189 });
190
191 if (!p_attached && !s_attached) {
192 return nullptr;
193 }
194
195 LOG("** Attached ellipse geometry\n");
196
Florin Malita2e1d7e22018-01-02 10:40:00 -0500197 return rect_node;
198}
199
Florin Malita02a32b02018-01-04 11:27:09 -0500200sk_sp<sksg::GeometryNode> AttachPolystarGeometry(const Json::Value& jstar, AttachContext* ctx) {
201 SkASSERT(jstar.isObject());
202
203 static constexpr CompositePolyStar::Type gTypes[] = {
204 CompositePolyStar::Type::kStar, // "sy": 1
205 CompositePolyStar::Type::kPoly, // "sy": 2
206 };
207
208 const auto type = ParseInt(jstar["sy"], 0) - 1;
209 if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gTypes))) {
210 LogFail(jstar, "Unknown polystar type");
211 return nullptr;
212 }
213
214 auto path_node = sksg::Path::Make();
215 auto composite = sk_make_sp<CompositePolyStar>(path_node, gTypes[type]);
216
217 AttachProperty<VectorValue, SkPoint>(jstar["p"], ctx, composite,
218 [](const sk_sp<CompositePolyStar>& node, const SkPoint& p) { node->setPosition(p); });
219 AttachProperty<ScalarValue, SkScalar>(jstar["pt"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500220 [](const sk_sp<CompositePolyStar>& node, const SkScalar& pt) { node->setPointCount(pt); });
Florin Malita02a32b02018-01-04 11:27:09 -0500221 AttachProperty<ScalarValue, SkScalar>(jstar["ir"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500222 [](const sk_sp<CompositePolyStar>& node, const SkScalar& ir) { node->setInnerRadius(ir); });
Florin Malita02a32b02018-01-04 11:27:09 -0500223 AttachProperty<ScalarValue, SkScalar>(jstar["or"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500224 [](const sk_sp<CompositePolyStar>& node, const SkScalar& otr) {
225 node->setOuterRadius(otr);
226 });
Florin Malita02a32b02018-01-04 11:27:09 -0500227 AttachProperty<ScalarValue, SkScalar>(jstar["is"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500228 [](const sk_sp<CompositePolyStar>& node, const SkScalar& is) {
229 node->setInnerRoundness(is);
230 });
Florin Malita02a32b02018-01-04 11:27:09 -0500231 AttachProperty<ScalarValue, SkScalar>(jstar["os"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500232 [](const sk_sp<CompositePolyStar>& node, const SkScalar& os) {
233 node->setOuterRoundness(os);
234 });
Florin Malita02a32b02018-01-04 11:27:09 -0500235 AttachProperty<ScalarValue, SkScalar>(jstar["r"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500236 [](const sk_sp<CompositePolyStar>& node, const SkScalar& r) { node->setRotation(r); });
Florin Malita02a32b02018-01-04 11:27:09 -0500237
238 return path_node;
239}
240
Florin Malita094ccde2017-12-30 12:27:00 -0500241sk_sp<sksg::Color> AttachColorPaint(const Json::Value& obj, AttachContext* ctx) {
242 SkASSERT(obj.isObject());
243
244 auto color_node = sksg::Color::Make(SK_ColorBLACK);
245 color_node->setAntiAlias(true);
246
247 auto color_attached = AttachProperty<VectorValue, SkColor>(obj["c"], ctx, color_node,
Florin Malita9661b982018-01-06 14:25:49 -0500248 [](const sk_sp<sksg::Color>& node, const SkColor& c) { node->setColor(c); });
Florin Malita094ccde2017-12-30 12:27:00 -0500249
250 return color_attached ? color_node : nullptr;
251}
252
253sk_sp<sksg::PaintNode> AttachFillPaint(const Json::Value& jfill, AttachContext* ctx) {
254 SkASSERT(jfill.isObject());
255
256 auto color = AttachColorPaint(jfill, ctx);
257 if (color) {
258 LOG("** Attached color fill: 0x%x\n", color->getColor());
259 }
260 return color;
261}
262
263sk_sp<sksg::PaintNode> AttachStrokePaint(const Json::Value& jstroke, AttachContext* ctx) {
264 SkASSERT(jstroke.isObject());
265
266 auto stroke_node = AttachColorPaint(jstroke, ctx);
267 if (!stroke_node)
268 return nullptr;
269
270 LOG("** Attached color stroke: 0x%x\n", stroke_node->getColor());
271
272 stroke_node->setStyle(SkPaint::kStroke_Style);
273
274 auto width_attached = AttachProperty<ScalarValue, SkScalar>(jstroke["w"], ctx, stroke_node,
Florin Malita9661b982018-01-06 14:25:49 -0500275 [](const sk_sp<sksg::Color>& node, const SkScalar& width) { node->setStrokeWidth(width); });
Florin Malita094ccde2017-12-30 12:27:00 -0500276 if (!width_attached)
277 return nullptr;
278
279 stroke_node->setStrokeMiter(ParseScalar(jstroke["ml"], 4));
280
281 static constexpr SkPaint::Join gJoins[] = {
282 SkPaint::kMiter_Join,
283 SkPaint::kRound_Join,
284 SkPaint::kBevel_Join,
285 };
286 stroke_node->setStrokeJoin(gJoins[SkTPin<int>(ParseInt(jstroke["lj"], 1) - 1,
287 0, SK_ARRAY_COUNT(gJoins) - 1)]);
288
289 static constexpr SkPaint::Cap gCaps[] = {
290 SkPaint::kButt_Cap,
291 SkPaint::kRound_Cap,
292 SkPaint::kSquare_Cap,
293 };
294 stroke_node->setStrokeCap(gCaps[SkTPin<int>(ParseInt(jstroke["lc"], 1) - 1,
295 0, SK_ARRAY_COUNT(gCaps) - 1)]);
296
297 return stroke_node;
298}
299
Florin Malitae6345d92018-01-03 23:37:54 -0500300std::vector<sk_sp<sksg::GeometryNode>> AttachMergeGeometryEffect(
301 const Json::Value& jmerge, AttachContext* ctx, std::vector<sk_sp<sksg::GeometryNode>>&& geos) {
302 std::vector<sk_sp<sksg::GeometryNode>> merged;
303
304 static constexpr sksg::Merge::Mode gModes[] = {
305 sksg::Merge::Mode::kMerge, // "mm": 1
306 sksg::Merge::Mode::kUnion, // "mm": 2
307 sksg::Merge::Mode::kDifference, // "mm": 3
308 sksg::Merge::Mode::kIntersect, // "mm": 4
309 sksg::Merge::Mode::kXOR , // "mm": 5
310 };
311
Florin Malita51b8c892018-01-07 08:54:24 -0500312 const auto mode = gModes[SkTPin<int>(ParseInt(jmerge["mm"], 1) - 1,
313 0, SK_ARRAY_COUNT(gModes) - 1)];
Florin Malitae6345d92018-01-03 23:37:54 -0500314 merged.push_back(sksg::Merge::Make(std::move(geos), mode));
315
316 LOG("** Attached merge path effect, mode: %d\n", mode);
317
318 return merged;
319}
320
Florin Malita51b8c892018-01-07 08:54:24 -0500321std::vector<sk_sp<sksg::GeometryNode>> AttachTrimGeometryEffect(
322 const Json::Value& jtrim, AttachContext* ctx, std::vector<sk_sp<sksg::GeometryNode>>&& geos) {
323
324 enum class Mode {
325 kMerged, // "m": 1
326 kSeparate, // "m": 2
327 } gModes[] = { Mode::kMerged, Mode::kSeparate };
328
329 const auto mode = gModes[SkTPin<int>(ParseInt(jtrim["m"], 1) - 1,
330 0, SK_ARRAY_COUNT(gModes) - 1)];
331
332 std::vector<sk_sp<sksg::GeometryNode>> inputs;
333 if (mode == Mode::kMerged) {
334 inputs.push_back(sksg::Merge::Make(std::move(geos), sksg::Merge::Mode::kMerge));
335 } else {
336 inputs = std::move(geos);
337 }
338
339 std::vector<sk_sp<sksg::GeometryNode>> trimmed;
340 trimmed.reserve(inputs.size());
341 for (const auto& i : inputs) {
342 const auto trim = sksg::TrimEffect::Make(i);
343 trimmed.push_back(trim);
344 AttachProperty<ScalarValue, SkScalar>(jtrim["s"], ctx, trim,
345 [](const sk_sp<sksg::TrimEffect>& node, const SkScalar& s) {
346 node->setStart(s * 0.01f);
347 });
348 AttachProperty<ScalarValue, SkScalar>(jtrim["e"], ctx, trim,
349 [](const sk_sp<sksg::TrimEffect>& node, const SkScalar& e) {
350 node->setEnd(e * 0.01f);
351 });
352 // TODO: "offset" doesn't currently work the same as BM - figure out what's going on.
353 AttachProperty<ScalarValue, SkScalar>(jtrim["o"], ctx, trim,
354 [](const sk_sp<sksg::TrimEffect>& node, const SkScalar& o) {
355 node->setOffset(o * 0.01f);
356 });
357 }
358
359 return trimmed;
360}
361
Florin Malita094ccde2017-12-30 12:27:00 -0500362using GeometryAttacherT = sk_sp<sksg::GeometryNode> (*)(const Json::Value&, AttachContext*);
363static constexpr GeometryAttacherT gGeometryAttachers[] = {
364 AttachPathGeometry,
Florin Malita2e1d7e22018-01-02 10:40:00 -0500365 AttachRRectGeometry,
Florin Malitafbc13f12018-01-04 10:26:35 -0500366 AttachEllipseGeometry,
Florin Malita02a32b02018-01-04 11:27:09 -0500367 AttachPolystarGeometry,
Florin Malita094ccde2017-12-30 12:27:00 -0500368};
369
370using PaintAttacherT = sk_sp<sksg::PaintNode> (*)(const Json::Value&, AttachContext*);
371static constexpr PaintAttacherT gPaintAttachers[] = {
372 AttachFillPaint,
373 AttachStrokePaint,
374};
375
376using GroupAttacherT = sk_sp<sksg::RenderNode> (*)(const Json::Value&, AttachContext*);
377static constexpr GroupAttacherT gGroupAttachers[] = {
378 AttachShapeGroup,
379};
380
Florin Malitae6345d92018-01-03 23:37:54 -0500381using GeometryEffectAttacherT =
382 std::vector<sk_sp<sksg::GeometryNode>> (*)(const Json::Value&,
383 AttachContext*,
384 std::vector<sk_sp<sksg::GeometryNode>>&&);
385static constexpr GeometryEffectAttacherT gGeometryEffectAttachers[] = {
386 AttachMergeGeometryEffect,
Florin Malita51b8c892018-01-07 08:54:24 -0500387 AttachTrimGeometryEffect,
Florin Malitae6345d92018-01-03 23:37:54 -0500388};
389
Florin Malita094ccde2017-12-30 12:27:00 -0500390enum class ShapeType {
391 kGeometry,
Florin Malitae6345d92018-01-03 23:37:54 -0500392 kGeometryEffect,
Florin Malita094ccde2017-12-30 12:27:00 -0500393 kPaint,
394 kGroup,
Florin Malitadacc02b2017-12-31 09:12:31 -0500395 kTransform,
Florin Malita094ccde2017-12-30 12:27:00 -0500396};
397
398struct ShapeInfo {
399 const char* fTypeString;
400 ShapeType fShapeType;
401 uint32_t fAttacherIndex; // index into respective attacher tables
402};
403
404const ShapeInfo* FindShapeInfo(const Json::Value& shape) {
405 static constexpr ShapeInfo gShapeInfo[] = {
Florin Malitafbc13f12018-01-04 10:26:35 -0500406 { "el", ShapeType::kGeometry , 2 }, // ellipse -> AttachEllipseGeometry
Florin Malitae6345d92018-01-03 23:37:54 -0500407 { "fl", ShapeType::kPaint , 0 }, // fill -> AttachFillPaint
408 { "gr", ShapeType::kGroup , 0 }, // group -> AttachShapeGroup
409 { "mm", ShapeType::kGeometryEffect, 0 }, // merge -> AttachMergeGeometryEffect
Florin Malita02a32b02018-01-04 11:27:09 -0500410 { "rc", ShapeType::kGeometry , 1 }, // rrect -> AttachRRectGeometry
Florin Malitae6345d92018-01-03 23:37:54 -0500411 { "sh", ShapeType::kGeometry , 0 }, // shape -> AttachPathGeometry
Florin Malita02a32b02018-01-04 11:27:09 -0500412 { "sr", ShapeType::kGeometry , 3 }, // polystar -> AttachPolyStarGeometry
Florin Malitae6345d92018-01-03 23:37:54 -0500413 { "st", ShapeType::kPaint , 1 }, // stroke -> AttachStrokePaint
Florin Malita51b8c892018-01-07 08:54:24 -0500414 { "tm", ShapeType::kGeometryEffect, 1 }, // trim -> AttachTrimGeometryEffect
Florin Malita18eafd92018-01-04 21:11:55 -0500415 { "tr", ShapeType::kTransform , 0 }, // transform -> In-place handler
Florin Malita094ccde2017-12-30 12:27:00 -0500416 };
417
418 if (!shape.isObject())
419 return nullptr;
420
421 const auto& type = shape["ty"];
422 if (!type.isString())
423 return nullptr;
424
425 const auto* info = bsearch(type.asCString(),
426 gShapeInfo,
427 SK_ARRAY_COUNT(gShapeInfo),
428 sizeof(ShapeInfo),
429 [](const void* key, const void* info) {
430 return strcmp(static_cast<const char*>(key),
431 static_cast<const ShapeInfo*>(info)->fTypeString);
432 });
433
434 return static_cast<const ShapeInfo*>(info);
435}
436
437sk_sp<sksg::RenderNode> AttachShape(const Json::Value& shapeArray, AttachContext* ctx) {
438 if (!shapeArray.isArray())
439 return nullptr;
440
Florin Malita2a8275b2018-01-02 12:52:43 -0500441 // (https://helpx.adobe.com/after-effects/using/overview-shape-layers-paths-vector.html#groups_and_render_order_for_shapes_and_shape_attributes)
442 //
443 // Render order for shapes within a shape layer
444 //
445 // The rules for rendering a shape layer are similar to the rules for rendering a composition
446 // that contains nested compositions:
447 //
448 // * Within a group, the shape at the bottom of the Timeline panel stacking order is rendered
449 // first.
450 //
451 // * All path operations within a group are performed before paint operations. This means,
452 // for example, that the stroke follows the distortions in the path made by the Wiggle Paths
453 // path operation. Path operations within a group are performed from top to bottom.
454 //
455 // * Paint operations within a group are performed from the bottom to the top in the Timeline
456 // panel stacking order. This means, for example, that a stroke is rendered on top of
457 // (in front of) a stroke that appears after it in the Timeline panel.
458 //
Florin Malitadacc02b2017-12-31 09:12:31 -0500459 sk_sp<sksg::Group> shape_group = sksg::Group::Make();
460 sk_sp<sksg::RenderNode> xformed_group = shape_group;
Florin Malita094ccde2017-12-30 12:27:00 -0500461
Florin Malitae6345d92018-01-03 23:37:54 -0500462 std::vector<sk_sp<sksg::GeometryNode>> geos;
463 std::vector<sk_sp<sksg::RenderNode>> draws;
Florin Malita094ccde2017-12-30 12:27:00 -0500464
465 for (const auto& s : shapeArray) {
466 const auto* info = FindShapeInfo(s);
467 if (!info) {
468 LogFail(s.isObject() ? s["ty"] : s, "Unknown shape");
469 continue;
470 }
471
472 switch (info->fShapeType) {
473 case ShapeType::kGeometry: {
474 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGeometryAttachers));
475 if (auto geo = gGeometryAttachers[info->fAttacherIndex](s, ctx)) {
476 geos.push_back(std::move(geo));
477 }
478 } break;
Florin Malitae6345d92018-01-03 23:37:54 -0500479 case ShapeType::kGeometryEffect: {
480 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGeometryEffectAttachers));
481 geos = gGeometryEffectAttachers[info->fAttacherIndex](s, ctx, std::move(geos));
482 } break;
Florin Malita094ccde2017-12-30 12:27:00 -0500483 case ShapeType::kPaint: {
484 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gPaintAttachers));
485 if (auto paint = gPaintAttachers[info->fAttacherIndex](s, ctx)) {
Florin Malita2a8275b2018-01-02 12:52:43 -0500486 for (const auto& geo : geos) {
487 draws.push_back(sksg::Draw::Make(geo, paint));
488 }
Florin Malita094ccde2017-12-30 12:27:00 -0500489 }
490 } break;
491 case ShapeType::kGroup: {
492 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGroupAttachers));
493 if (auto group = gGroupAttachers[info->fAttacherIndex](s, ctx)) {
Florin Malita2a8275b2018-01-02 12:52:43 -0500494 draws.push_back(std::move(group));
Florin Malita094ccde2017-12-30 12:27:00 -0500495 }
496 } break;
Florin Malitadacc02b2017-12-31 09:12:31 -0500497 case ShapeType::kTransform: {
Florin Malita2a8275b2018-01-02 12:52:43 -0500498 // TODO: BM appears to transform the geometry, not the draw op itself.
Florin Malita18eafd92018-01-04 21:11:55 -0500499 if (auto matrix = AttachMatrix(s, ctx, nullptr)) {
500 xformed_group = sksg::Transform::Make(std::move(xformed_group),
501 std::move(matrix));
502 }
Florin Malitadacc02b2017-12-31 09:12:31 -0500503 } break;
Florin Malita094ccde2017-12-30 12:27:00 -0500504 }
505 }
506
Florin Malita2a8275b2018-01-02 12:52:43 -0500507 if (draws.empty()) {
508 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500509 }
510
Florin Malitae6345d92018-01-03 23:37:54 -0500511 for (auto draw = draws.rbegin(); draw != draws.rend(); ++draw) {
512 shape_group->addChild(std::move(*draw));
Florin Malita2a8275b2018-01-02 12:52:43 -0500513 }
514
Florin Malitae6345d92018-01-03 23:37:54 -0500515 LOG("** Attached shape: %zd draws.\n", draws.size());
Florin Malitadacc02b2017-12-31 09:12:31 -0500516 return xformed_group;
Florin Malita094ccde2017-12-30 12:27:00 -0500517}
518
519sk_sp<sksg::RenderNode> AttachCompLayer(const Json::Value& layer, AttachContext* ctx) {
520 SkASSERT(layer.isObject());
521
522 auto refId = ParseString(layer["refId"], "");
523 if (refId.isEmpty()) {
524 LOG("!! Comp layer missing refId\n");
525 return nullptr;
526 }
527
528 const auto* comp = ctx->fAssets.find(refId);
529 if (!comp) {
530 LOG("!! Pre-comp not found: '%s'\n", refId.c_str());
531 return nullptr;
532 }
533
534 // TODO: cycle detection
535 return AttachComposition(**comp, ctx);
536}
537
538sk_sp<sksg::RenderNode> AttachSolidLayer(const Json::Value& layer, AttachContext*) {
539 SkASSERT(layer.isObject());
540
541 LOG("?? Solid layer stub\n");
542 return nullptr;
543}
544
545sk_sp<sksg::RenderNode> AttachImageLayer(const Json::Value& layer, AttachContext*) {
546 SkASSERT(layer.isObject());
547
548 LOG("?? Image layer stub\n");
549 return nullptr;
550}
551
552sk_sp<sksg::RenderNode> AttachNullLayer(const Json::Value& layer, AttachContext*) {
553 SkASSERT(layer.isObject());
554
Florin Malita18eafd92018-01-04 21:11:55 -0500555 // Null layers are used solely to drive dependent transforms,
556 // but we use free-floating sksg::Matrices for that purpose.
Florin Malita094ccde2017-12-30 12:27:00 -0500557 return nullptr;
558}
559
560sk_sp<sksg::RenderNode> AttachShapeLayer(const Json::Value& layer, AttachContext* ctx) {
561 SkASSERT(layer.isObject());
562
563 LOG("** Attaching shape layer ind: %d\n", ParseInt(layer["ind"], 0));
564
565 return AttachShape(layer["shapes"], ctx);
566}
567
568sk_sp<sksg::RenderNode> AttachTextLayer(const Json::Value& layer, AttachContext*) {
569 SkASSERT(layer.isObject());
570
571 LOG("?? Text layer stub\n");
572 return nullptr;
573}
574
Florin Malita18eafd92018-01-04 21:11:55 -0500575struct AttachLayerContext {
576 AttachLayerContext(const Json::Value& jlayers, AttachContext* ctx)
577 : fLayerList(jlayers), fCtx(ctx) {}
578
579 const Json::Value& fLayerList;
580 AttachContext* fCtx;
581 std::unordered_map<const Json::Value*, sk_sp<sksg::Matrix>> fLayerMatrixCache;
582 std::unordered_map<int, const Json::Value*> fLayerIndexCache;
583
584 const Json::Value* findLayer(int index) {
585 SkASSERT(fLayerList.isArray());
586
587 if (index < 0) {
588 return nullptr;
589 }
590
591 const auto cached = fLayerIndexCache.find(index);
592 if (cached != fLayerIndexCache.end()) {
593 return cached->second;
594 }
595
596 for (const auto& l : fLayerList) {
597 if (!l.isObject()) {
598 continue;
599 }
600
601 if (ParseInt(l["ind"], -1) == index) {
602 fLayerIndexCache.insert(std::make_pair(index, &l));
603 return &l;
604 }
605 }
606
607 return nullptr;
608 }
609
610 sk_sp<sksg::Matrix> AttachLayerMatrix(const Json::Value& jlayer) {
611 SkASSERT(jlayer.isObject());
612
613 const auto cached = fLayerMatrixCache.find(&jlayer);
614 if (cached != fLayerMatrixCache.end()) {
615 return cached->second;
616 }
617
618 const auto* parentLayer = this->findLayer(ParseInt(jlayer["parent"], -1));
619
620 // TODO: cycle detection?
621 auto parentMatrix = (parentLayer && parentLayer != &jlayer)
622 ? this->AttachLayerMatrix(*parentLayer) : nullptr;
623
624 auto layerMatrix = AttachMatrix(jlayer["ks"], fCtx, std::move(parentMatrix));
625 fLayerMatrixCache.insert(std::make_pair(&jlayer, layerMatrix));
626
627 return layerMatrix;
628 }
629};
630
631sk_sp<sksg::RenderNode> AttachLayer(const Json::Value& jlayer,
632 AttachLayerContext* layerCtx) {
633 if (!jlayer.isObject())
Florin Malita094ccde2017-12-30 12:27:00 -0500634 return nullptr;
635
636 using LayerAttacher = sk_sp<sksg::RenderNode> (*)(const Json::Value&, AttachContext*);
637 static constexpr LayerAttacher gLayerAttachers[] = {
638 AttachCompLayer, // 'ty': 0
639 AttachSolidLayer, // 'ty': 1
640 AttachImageLayer, // 'ty': 2
641 AttachNullLayer, // 'ty': 3
642 AttachShapeLayer, // 'ty': 4
643 AttachTextLayer, // 'ty': 5
644 };
645
Florin Malita18eafd92018-01-04 21:11:55 -0500646 int type = ParseInt(jlayer["ty"], -1);
Florin Malita094ccde2017-12-30 12:27:00 -0500647 if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gLayerAttachers))) {
648 return nullptr;
649 }
650
Florin Malita18eafd92018-01-04 21:11:55 -0500651 auto layer = gLayerAttachers[type](jlayer, layerCtx->fCtx);
652 auto layerMatrix = layerCtx->AttachLayerMatrix(jlayer);
653
654 return layerMatrix
655 ? sksg::Transform::Make(std::move(layer), std::move(layerMatrix))
656 : layer;
Florin Malita094ccde2017-12-30 12:27:00 -0500657}
658
659sk_sp<sksg::RenderNode> AttachComposition(const Json::Value& comp, AttachContext* ctx) {
660 if (!comp.isObject())
661 return nullptr;
662
Florin Malita18eafd92018-01-04 21:11:55 -0500663 const auto& jlayers = comp["layers"];
664 if (!jlayers.isArray())
665 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500666
Florin Malita18eafd92018-01-04 21:11:55 -0500667 SkSTArray<16, sk_sp<sksg::RenderNode>, true> layers;
668 AttachLayerContext layerCtx(jlayers, ctx);
669
670 for (const auto& l : jlayers) {
671 if (auto layer_fragment = AttachLayer(l, &layerCtx)) {
Florin Malita2a8275b2018-01-02 12:52:43 -0500672 layers.push_back(std::move(layer_fragment));
Florin Malita094ccde2017-12-30 12:27:00 -0500673 }
674 }
675
Florin Malita2a8275b2018-01-02 12:52:43 -0500676 if (layers.empty()) {
677 return nullptr;
678 }
679
680 // Layers are painted in bottom->top order.
681 auto comp_group = sksg::Group::Make();
682 for (int i = layers.count() - 1; i >= 0; --i) {
683 comp_group->addChild(std::move(layers[i]));
684 }
685
686 LOG("** Attached composition '%s': %d layers.\n",
687 ParseString(comp["id"], "").c_str(), layers.count());
688
Florin Malita094ccde2017-12-30 12:27:00 -0500689 return comp_group;
690}
691
692} // namespace
693
694std::unique_ptr<Animation> Animation::Make(SkStream* stream) {
695 if (!stream->hasLength()) {
696 // TODO: handle explicit buffering?
697 LOG("!! cannot parse streaming content\n");
698 return nullptr;
699 }
700
701 Json::Value json;
702 {
703 auto data = SkData::MakeFromStream(stream, stream->getLength());
704 if (!data) {
705 LOG("!! could not read stream\n");
706 return nullptr;
707 }
708
709 Json::Reader reader;
710
711 auto dataStart = static_cast<const char*>(data->data());
712 if (!reader.parse(dataStart, dataStart + data->size(), json, false) || !json.isObject()) {
713 LOG("!! failed to parse json: %s\n", reader.getFormattedErrorMessages().c_str());
714 return nullptr;
715 }
716 }
717
718 const auto version = ParseString(json["v"], "");
719 const auto size = SkSize::Make(ParseScalar(json["w"], -1), ParseScalar(json["h"], -1));
720 const auto fps = ParseScalar(json["fr"], -1);
721
722 if (size.isEmpty() || version.isEmpty() || fps < 0) {
723 LOG("!! invalid animation params (version: %s, size: [%f %f], frame rate: %f)",
724 version.c_str(), size.width(), size.height(), fps);
725 return nullptr;
726 }
727
728 return std::unique_ptr<Animation>(new Animation(std::move(version), size, fps, json));
729}
730
731Animation::Animation(SkString version, const SkSize& size, SkScalar fps, const Json::Value& json)
732 : fVersion(std::move(version))
733 , fSize(size)
734 , fFrameRate(fps)
735 , fInPoint(ParseScalar(json["ip"], 0))
736 , fOutPoint(SkTMax(ParseScalar(json["op"], SK_ScalarMax), fInPoint)) {
737
738 AssetMap assets;
739 for (const auto& asset : json["assets"]) {
740 if (!asset.isObject()) {
741 continue;
742 }
743
744 assets.set(ParseString(asset["id"], ""), &asset);
745 }
746
747 AttachContext ctx = { assets, fAnimators };
748 fDom = AttachComposition(json, &ctx);
749
750 LOG("** Attached %d animators\n", fAnimators.count());
751}
752
753Animation::~Animation() = default;
754
755void Animation::render(SkCanvas* canvas) const {
756 if (!fDom)
757 return;
758
759 sksg::InvalidationController ic;
760 fDom->revalidate(&ic, SkMatrix::I());
761
762 // TODO: proper inval
763 fDom->render(canvas);
764
765 if (!fShowInval)
766 return;
767
768 SkPaint fill, stroke;
769 fill.setAntiAlias(true);
770 fill.setColor(0x40ff0000);
771 stroke.setAntiAlias(true);
772 stroke.setColor(0xffff0000);
773 stroke.setStyle(SkPaint::kStroke_Style);
774
775 for (const auto& r : ic) {
776 canvas->drawRect(r, fill);
777 canvas->drawRect(r, stroke);
778 }
779}
780
781void Animation::animationTick(SkMSec ms) {
782 // 't' in the BM model really means 'frame #'
783 auto t = static_cast<float>(ms) * fFrameRate / 1000;
784
785 t = fInPoint + std::fmod(t, fOutPoint - fInPoint);
786
787 // TODO: this can be optimized quite a bit with some sorting/state tracking.
788 for (const auto& a : fAnimators) {
789 a->tick(t);
790 }
791}
792
793} // namespace skotty