blob: 8c48d1f871c0e8673951ea1fed821d0a7f7d3d0d [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"
27#include "SkStream.h"
28#include "SkTArray.h"
29#include "SkTHash.h"
30
31#include <cmath>
Florin Malita18eafd92018-01-04 21:11:55 -050032#include <unordered_map>
Florin Malitae6345d92018-01-03 23:37:54 -050033#include <vector>
34
Florin Malita094ccde2017-12-30 12:27:00 -050035#include "stdlib.h"
36
37namespace skotty {
38
39namespace {
40
41using AssetMap = SkTHashMap<SkString, const Json::Value*>;
42
43struct AttachContext {
44 const AssetMap& fAssets;
45 SkTArray<std::unique_ptr<AnimatorBase>>& fAnimators;
46};
47
48bool LogFail(const Json::Value& json, const char* msg) {
49 const auto dump = json.toStyledString();
50 LOG("!! %s: %s", msg, dump.c_str());
51 return false;
52}
53
54// This is the workhorse for binding properties: depending on whether the property is animated,
55// it will either apply immediately or instantiate and attach a keyframe animator.
Florin Malita9661b982018-01-06 14:25:49 -050056template <typename ValueT, typename AttrT, typename NodeT>
Florin Malita094ccde2017-12-30 12:27:00 -050057bool AttachProperty(const Json::Value& jprop, AttachContext* ctx, const sk_sp<NodeT>& node,
Florin Malita9661b982018-01-06 14:25:49 -050058 typename Animator<ValueT, AttrT, NodeT>::ApplyFuncT&& apply) {
Florin Malita094ccde2017-12-30 12:27:00 -050059 if (!jprop.isObject())
60 return false;
61
62 if (!ParseBool(jprop["a"], false)) {
63 // Static property.
64 ValueT val;
65 if (!ValueT::Parse(jprop["k"], &val)) {
66 return LogFail(jprop, "Could not parse static property");
67 }
68
69 apply(node, val.template as<AttrT>());
70 } else {
71 // Keyframe property.
72 using AnimatorT = Animator<ValueT, AttrT, NodeT>;
73 auto animator = AnimatorT::Make(jprop["k"], node, std::move(apply));
74
75 if (!animator) {
76 return LogFail(jprop, "Could not instantiate keyframe animator");
77 }
78
79 ctx->fAnimators.push_back(std::move(animator));
80 }
81
82 return true;
83}
84
Florin Malita18eafd92018-01-04 21:11:55 -050085sk_sp<sksg::Matrix> AttachMatrix(const Json::Value& t, AttachContext* ctx,
86 sk_sp<sksg::Matrix> parentMatrix) {
87 if (!t.isObject())
88 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -050089
Florin Malita18eafd92018-01-04 21:11:55 -050090 auto matrix = sksg::Matrix::Make(SkMatrix::I(), std::move(parentMatrix));
91 auto composite = sk_make_sp<CompositeTransform>(matrix);
92 auto anchor_attached = AttachProperty<VectorValue, SkPoint>(t["a"], ctx, composite,
Florin Malita094ccde2017-12-30 12:27:00 -050093 [](const sk_sp<CompositeTransform>& node, const SkPoint& a) {
94 node->setAnchorPoint(a);
95 });
Florin Malita18eafd92018-01-04 21:11:55 -050096 auto position_attached = AttachProperty<VectorValue, SkPoint>(t["p"], ctx, composite,
Florin Malita094ccde2017-12-30 12:27:00 -050097 [](const sk_sp<CompositeTransform>& node, const SkPoint& p) {
98 node->setPosition(p);
99 });
Florin Malita18eafd92018-01-04 21:11:55 -0500100 auto scale_attached = AttachProperty<VectorValue, SkVector>(t["s"], ctx, composite,
Florin Malita094ccde2017-12-30 12:27:00 -0500101 [](const sk_sp<CompositeTransform>& node, const SkVector& s) {
102 node->setScale(s);
103 });
Florin Malita18eafd92018-01-04 21:11:55 -0500104 auto rotation_attached = AttachProperty<ScalarValue, SkScalar>(t["r"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500105 [](const sk_sp<CompositeTransform>& node, const SkScalar& r) {
Florin Malita094ccde2017-12-30 12:27:00 -0500106 node->setRotation(r);
107 });
Florin Malita18eafd92018-01-04 21:11:55 -0500108 auto skew_attached = AttachProperty<ScalarValue, SkScalar>(t["sk"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500109 [](const sk_sp<CompositeTransform>& node, const SkScalar& sk) {
Florin Malita094ccde2017-12-30 12:27:00 -0500110 node->setSkew(sk);
111 });
Florin Malita18eafd92018-01-04 21:11:55 -0500112 auto skewaxis_attached = AttachProperty<ScalarValue, SkScalar>(t["sa"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500113 [](const sk_sp<CompositeTransform>& node, const SkScalar& sa) {
Florin Malita094ccde2017-12-30 12:27:00 -0500114 node->setSkewAxis(sa);
115 });
116
117 if (!anchor_attached &&
118 !position_attached &&
119 !scale_attached &&
120 !rotation_attached &&
121 !skew_attached &&
122 !skewaxis_attached) {
123 LogFail(t, "Could not parse transform");
Florin Malita18eafd92018-01-04 21:11:55 -0500124 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500125 }
126
Florin Malita18eafd92018-01-04 21:11:55 -0500127 return matrix;
Florin Malita094ccde2017-12-30 12:27:00 -0500128}
129
130sk_sp<sksg::RenderNode> AttachShape(const Json::Value&, AttachContext* ctx);
131sk_sp<sksg::RenderNode> AttachComposition(const Json::Value&, AttachContext* ctx);
132
133sk_sp<sksg::RenderNode> AttachShapeGroup(const Json::Value& jgroup, AttachContext* ctx) {
134 SkASSERT(jgroup.isObject());
135
136 return AttachShape(jgroup["it"], ctx);
137}
138
139sk_sp<sksg::GeometryNode> AttachPathGeometry(const Json::Value& jpath, AttachContext* ctx) {
140 SkASSERT(jpath.isObject());
141
142 auto path_node = sksg::Path::Make();
143 auto path_attached = AttachProperty<ShapeValue, SkPath>(jpath["ks"], ctx, path_node,
144 [](const sk_sp<sksg::Path>& node, const SkPath& p) { node->setPath(p); });
145
146 if (path_attached)
147 LOG("** Attached path geometry - verbs: %d\n", path_node->getPath().countVerbs());
148
149 return path_attached ? path_node : nullptr;
150}
151
Florin Malita2e1d7e22018-01-02 10:40:00 -0500152sk_sp<sksg::GeometryNode> AttachRRectGeometry(const Json::Value& jrect, AttachContext* ctx) {
153 SkASSERT(jrect.isObject());
154
155 auto rect_node = sksg::RRect::Make();
156 auto composite = sk_make_sp<CompositeRRect>(rect_node);
157
158 auto p_attached = AttachProperty<VectorValue, SkPoint>(jrect["p"], ctx, composite,
159 [](const sk_sp<CompositeRRect>& node, const SkPoint& pos) { node->setPosition(pos); });
160 auto s_attached = AttachProperty<VectorValue, SkSize>(jrect["s"], ctx, composite,
161 [](const sk_sp<CompositeRRect>& node, const SkSize& sz) { node->setSize(sz); });
162 auto r_attached = AttachProperty<ScalarValue, SkScalar>(jrect["r"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500163 [](const sk_sp<CompositeRRect>& node, const SkScalar& radius) {
Florin Malitafbc13f12018-01-04 10:26:35 -0500164 node->setRadius(SkSize::Make(radius, radius));
165 });
Florin Malita2e1d7e22018-01-02 10:40:00 -0500166
167 if (!p_attached && !s_attached && !r_attached) {
168 return nullptr;
169 }
170
Florin Malitafbc13f12018-01-04 10:26:35 -0500171 LOG("** Attached (r)rect geometry\n");
172
173 return rect_node;
174}
175
176sk_sp<sksg::GeometryNode> AttachEllipseGeometry(const Json::Value& jellipse, AttachContext* ctx) {
177 SkASSERT(jellipse.isObject());
178
179 auto rect_node = sksg::RRect::Make();
180 auto composite = sk_make_sp<CompositeRRect>(rect_node);
181
182 auto p_attached = AttachProperty<VectorValue, SkPoint>(jellipse["p"], ctx, composite,
183 [](const sk_sp<CompositeRRect>& node, const SkPoint& pos) { node->setPosition(pos); });
184 auto s_attached = AttachProperty<VectorValue, SkSize>(jellipse["s"], ctx, composite,
185 [](const sk_sp<CompositeRRect>& node, const SkSize& sz) {
186 node->setSize(sz);
187 node->setRadius(SkSize::Make(sz.width() / 2, sz.height() / 2));
188 });
189
190 if (!p_attached && !s_attached) {
191 return nullptr;
192 }
193
194 LOG("** Attached ellipse geometry\n");
195
Florin Malita2e1d7e22018-01-02 10:40:00 -0500196 return rect_node;
197}
198
Florin Malita02a32b02018-01-04 11:27:09 -0500199sk_sp<sksg::GeometryNode> AttachPolystarGeometry(const Json::Value& jstar, AttachContext* ctx) {
200 SkASSERT(jstar.isObject());
201
202 static constexpr CompositePolyStar::Type gTypes[] = {
203 CompositePolyStar::Type::kStar, // "sy": 1
204 CompositePolyStar::Type::kPoly, // "sy": 2
205 };
206
207 const auto type = ParseInt(jstar["sy"], 0) - 1;
208 if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gTypes))) {
209 LogFail(jstar, "Unknown polystar type");
210 return nullptr;
211 }
212
213 auto path_node = sksg::Path::Make();
214 auto composite = sk_make_sp<CompositePolyStar>(path_node, gTypes[type]);
215
216 AttachProperty<VectorValue, SkPoint>(jstar["p"], ctx, composite,
217 [](const sk_sp<CompositePolyStar>& node, const SkPoint& p) { node->setPosition(p); });
218 AttachProperty<ScalarValue, SkScalar>(jstar["pt"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500219 [](const sk_sp<CompositePolyStar>& node, const SkScalar& pt) { node->setPointCount(pt); });
Florin Malita02a32b02018-01-04 11:27:09 -0500220 AttachProperty<ScalarValue, SkScalar>(jstar["ir"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500221 [](const sk_sp<CompositePolyStar>& node, const SkScalar& ir) { node->setInnerRadius(ir); });
Florin Malita02a32b02018-01-04 11:27:09 -0500222 AttachProperty<ScalarValue, SkScalar>(jstar["or"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500223 [](const sk_sp<CompositePolyStar>& node, const SkScalar& otr) {
224 node->setOuterRadius(otr);
225 });
Florin Malita02a32b02018-01-04 11:27:09 -0500226 AttachProperty<ScalarValue, SkScalar>(jstar["is"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500227 [](const sk_sp<CompositePolyStar>& node, const SkScalar& is) {
228 node->setInnerRoundness(is);
229 });
Florin Malita02a32b02018-01-04 11:27:09 -0500230 AttachProperty<ScalarValue, SkScalar>(jstar["os"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500231 [](const sk_sp<CompositePolyStar>& node, const SkScalar& os) {
232 node->setOuterRoundness(os);
233 });
Florin Malita02a32b02018-01-04 11:27:09 -0500234 AttachProperty<ScalarValue, SkScalar>(jstar["r"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500235 [](const sk_sp<CompositePolyStar>& node, const SkScalar& r) { node->setRotation(r); });
Florin Malita02a32b02018-01-04 11:27:09 -0500236
237 return path_node;
238}
239
Florin Malita094ccde2017-12-30 12:27:00 -0500240sk_sp<sksg::Color> AttachColorPaint(const Json::Value& obj, AttachContext* ctx) {
241 SkASSERT(obj.isObject());
242
243 auto color_node = sksg::Color::Make(SK_ColorBLACK);
244 color_node->setAntiAlias(true);
245
246 auto color_attached = AttachProperty<VectorValue, SkColor>(obj["c"], ctx, color_node,
Florin Malita9661b982018-01-06 14:25:49 -0500247 [](const sk_sp<sksg::Color>& node, const SkColor& c) { node->setColor(c); });
Florin Malita094ccde2017-12-30 12:27:00 -0500248
249 return color_attached ? color_node : nullptr;
250}
251
252sk_sp<sksg::PaintNode> AttachFillPaint(const Json::Value& jfill, AttachContext* ctx) {
253 SkASSERT(jfill.isObject());
254
255 auto color = AttachColorPaint(jfill, ctx);
256 if (color) {
257 LOG("** Attached color fill: 0x%x\n", color->getColor());
258 }
259 return color;
260}
261
262sk_sp<sksg::PaintNode> AttachStrokePaint(const Json::Value& jstroke, AttachContext* ctx) {
263 SkASSERT(jstroke.isObject());
264
265 auto stroke_node = AttachColorPaint(jstroke, ctx);
266 if (!stroke_node)
267 return nullptr;
268
269 LOG("** Attached color stroke: 0x%x\n", stroke_node->getColor());
270
271 stroke_node->setStyle(SkPaint::kStroke_Style);
272
273 auto width_attached = AttachProperty<ScalarValue, SkScalar>(jstroke["w"], ctx, stroke_node,
Florin Malita9661b982018-01-06 14:25:49 -0500274 [](const sk_sp<sksg::Color>& node, const SkScalar& width) { node->setStrokeWidth(width); });
Florin Malita094ccde2017-12-30 12:27:00 -0500275 if (!width_attached)
276 return nullptr;
277
278 stroke_node->setStrokeMiter(ParseScalar(jstroke["ml"], 4));
279
280 static constexpr SkPaint::Join gJoins[] = {
281 SkPaint::kMiter_Join,
282 SkPaint::kRound_Join,
283 SkPaint::kBevel_Join,
284 };
285 stroke_node->setStrokeJoin(gJoins[SkTPin<int>(ParseInt(jstroke["lj"], 1) - 1,
286 0, SK_ARRAY_COUNT(gJoins) - 1)]);
287
288 static constexpr SkPaint::Cap gCaps[] = {
289 SkPaint::kButt_Cap,
290 SkPaint::kRound_Cap,
291 SkPaint::kSquare_Cap,
292 };
293 stroke_node->setStrokeCap(gCaps[SkTPin<int>(ParseInt(jstroke["lc"], 1) - 1,
294 0, SK_ARRAY_COUNT(gCaps) - 1)]);
295
296 return stroke_node;
297}
298
Florin Malitae6345d92018-01-03 23:37:54 -0500299std::vector<sk_sp<sksg::GeometryNode>> AttachMergeGeometryEffect(
300 const Json::Value& jmerge, AttachContext* ctx, std::vector<sk_sp<sksg::GeometryNode>>&& geos) {
301 std::vector<sk_sp<sksg::GeometryNode>> merged;
302
303 static constexpr sksg::Merge::Mode gModes[] = {
304 sksg::Merge::Mode::kMerge, // "mm": 1
305 sksg::Merge::Mode::kUnion, // "mm": 2
306 sksg::Merge::Mode::kDifference, // "mm": 3
307 sksg::Merge::Mode::kIntersect, // "mm": 4
308 sksg::Merge::Mode::kXOR , // "mm": 5
309 };
310
311 const auto mode = gModes[SkTPin<int>(ParseInt(jmerge["mm"], 1) - 1, 0, SK_ARRAY_COUNT(gModes))];
312 merged.push_back(sksg::Merge::Make(std::move(geos), mode));
313
314 LOG("** Attached merge path effect, mode: %d\n", mode);
315
316 return merged;
317}
318
Florin Malita094ccde2017-12-30 12:27:00 -0500319using GeometryAttacherT = sk_sp<sksg::GeometryNode> (*)(const Json::Value&, AttachContext*);
320static constexpr GeometryAttacherT gGeometryAttachers[] = {
321 AttachPathGeometry,
Florin Malita2e1d7e22018-01-02 10:40:00 -0500322 AttachRRectGeometry,
Florin Malitafbc13f12018-01-04 10:26:35 -0500323 AttachEllipseGeometry,
Florin Malita02a32b02018-01-04 11:27:09 -0500324 AttachPolystarGeometry,
Florin Malita094ccde2017-12-30 12:27:00 -0500325};
326
327using PaintAttacherT = sk_sp<sksg::PaintNode> (*)(const Json::Value&, AttachContext*);
328static constexpr PaintAttacherT gPaintAttachers[] = {
329 AttachFillPaint,
330 AttachStrokePaint,
331};
332
333using GroupAttacherT = sk_sp<sksg::RenderNode> (*)(const Json::Value&, AttachContext*);
334static constexpr GroupAttacherT gGroupAttachers[] = {
335 AttachShapeGroup,
336};
337
Florin Malitae6345d92018-01-03 23:37:54 -0500338using GeometryEffectAttacherT =
339 std::vector<sk_sp<sksg::GeometryNode>> (*)(const Json::Value&,
340 AttachContext*,
341 std::vector<sk_sp<sksg::GeometryNode>>&&);
342static constexpr GeometryEffectAttacherT gGeometryEffectAttachers[] = {
343 AttachMergeGeometryEffect,
344};
345
Florin Malita094ccde2017-12-30 12:27:00 -0500346enum class ShapeType {
347 kGeometry,
Florin Malitae6345d92018-01-03 23:37:54 -0500348 kGeometryEffect,
Florin Malita094ccde2017-12-30 12:27:00 -0500349 kPaint,
350 kGroup,
Florin Malitadacc02b2017-12-31 09:12:31 -0500351 kTransform,
Florin Malita094ccde2017-12-30 12:27:00 -0500352};
353
354struct ShapeInfo {
355 const char* fTypeString;
356 ShapeType fShapeType;
357 uint32_t fAttacherIndex; // index into respective attacher tables
358};
359
360const ShapeInfo* FindShapeInfo(const Json::Value& shape) {
361 static constexpr ShapeInfo gShapeInfo[] = {
Florin Malitafbc13f12018-01-04 10:26:35 -0500362 { "el", ShapeType::kGeometry , 2 }, // ellipse -> AttachEllipseGeometry
Florin Malitae6345d92018-01-03 23:37:54 -0500363 { "fl", ShapeType::kPaint , 0 }, // fill -> AttachFillPaint
364 { "gr", ShapeType::kGroup , 0 }, // group -> AttachShapeGroup
365 { "mm", ShapeType::kGeometryEffect, 0 }, // merge -> AttachMergeGeometryEffect
Florin Malita02a32b02018-01-04 11:27:09 -0500366 { "rc", ShapeType::kGeometry , 1 }, // rrect -> AttachRRectGeometry
Florin Malitae6345d92018-01-03 23:37:54 -0500367 { "sh", ShapeType::kGeometry , 0 }, // shape -> AttachPathGeometry
Florin Malita02a32b02018-01-04 11:27:09 -0500368 { "sr", ShapeType::kGeometry , 3 }, // polystar -> AttachPolyStarGeometry
Florin Malitae6345d92018-01-03 23:37:54 -0500369 { "st", ShapeType::kPaint , 1 }, // stroke -> AttachStrokePaint
Florin Malita18eafd92018-01-04 21:11:55 -0500370 { "tr", ShapeType::kTransform , 0 }, // transform -> In-place handler
Florin Malita094ccde2017-12-30 12:27:00 -0500371 };
372
373 if (!shape.isObject())
374 return nullptr;
375
376 const auto& type = shape["ty"];
377 if (!type.isString())
378 return nullptr;
379
380 const auto* info = bsearch(type.asCString(),
381 gShapeInfo,
382 SK_ARRAY_COUNT(gShapeInfo),
383 sizeof(ShapeInfo),
384 [](const void* key, const void* info) {
385 return strcmp(static_cast<const char*>(key),
386 static_cast<const ShapeInfo*>(info)->fTypeString);
387 });
388
389 return static_cast<const ShapeInfo*>(info);
390}
391
392sk_sp<sksg::RenderNode> AttachShape(const Json::Value& shapeArray, AttachContext* ctx) {
393 if (!shapeArray.isArray())
394 return nullptr;
395
Florin Malita2a8275b2018-01-02 12:52:43 -0500396 // (https://helpx.adobe.com/after-effects/using/overview-shape-layers-paths-vector.html#groups_and_render_order_for_shapes_and_shape_attributes)
397 //
398 // Render order for shapes within a shape layer
399 //
400 // The rules for rendering a shape layer are similar to the rules for rendering a composition
401 // that contains nested compositions:
402 //
403 // * Within a group, the shape at the bottom of the Timeline panel stacking order is rendered
404 // first.
405 //
406 // * All path operations within a group are performed before paint operations. This means,
407 // for example, that the stroke follows the distortions in the path made by the Wiggle Paths
408 // path operation. Path operations within a group are performed from top to bottom.
409 //
410 // * Paint operations within a group are performed from the bottom to the top in the Timeline
411 // panel stacking order. This means, for example, that a stroke is rendered on top of
412 // (in front of) a stroke that appears after it in the Timeline panel.
413 //
Florin Malitadacc02b2017-12-31 09:12:31 -0500414 sk_sp<sksg::Group> shape_group = sksg::Group::Make();
415 sk_sp<sksg::RenderNode> xformed_group = shape_group;
Florin Malita094ccde2017-12-30 12:27:00 -0500416
Florin Malitae6345d92018-01-03 23:37:54 -0500417 std::vector<sk_sp<sksg::GeometryNode>> geos;
418 std::vector<sk_sp<sksg::RenderNode>> draws;
Florin Malita094ccde2017-12-30 12:27:00 -0500419
420 for (const auto& s : shapeArray) {
421 const auto* info = FindShapeInfo(s);
422 if (!info) {
423 LogFail(s.isObject() ? s["ty"] : s, "Unknown shape");
424 continue;
425 }
426
427 switch (info->fShapeType) {
428 case ShapeType::kGeometry: {
429 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGeometryAttachers));
430 if (auto geo = gGeometryAttachers[info->fAttacherIndex](s, ctx)) {
431 geos.push_back(std::move(geo));
432 }
433 } break;
Florin Malitae6345d92018-01-03 23:37:54 -0500434 case ShapeType::kGeometryEffect: {
435 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGeometryEffectAttachers));
436 geos = gGeometryEffectAttachers[info->fAttacherIndex](s, ctx, std::move(geos));
437 } break;
Florin Malita094ccde2017-12-30 12:27:00 -0500438 case ShapeType::kPaint: {
439 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gPaintAttachers));
440 if (auto paint = gPaintAttachers[info->fAttacherIndex](s, ctx)) {
Florin Malita2a8275b2018-01-02 12:52:43 -0500441 for (const auto& geo : geos) {
442 draws.push_back(sksg::Draw::Make(geo, paint));
443 }
Florin Malita094ccde2017-12-30 12:27:00 -0500444 }
445 } break;
446 case ShapeType::kGroup: {
447 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGroupAttachers));
448 if (auto group = gGroupAttachers[info->fAttacherIndex](s, ctx)) {
Florin Malita2a8275b2018-01-02 12:52:43 -0500449 draws.push_back(std::move(group));
Florin Malita094ccde2017-12-30 12:27:00 -0500450 }
451 } break;
Florin Malitadacc02b2017-12-31 09:12:31 -0500452 case ShapeType::kTransform: {
Florin Malita2a8275b2018-01-02 12:52:43 -0500453 // TODO: BM appears to transform the geometry, not the draw op itself.
Florin Malita18eafd92018-01-04 21:11:55 -0500454 if (auto matrix = AttachMatrix(s, ctx, nullptr)) {
455 xformed_group = sksg::Transform::Make(std::move(xformed_group),
456 std::move(matrix));
457 }
Florin Malitadacc02b2017-12-31 09:12:31 -0500458 } break;
Florin Malita094ccde2017-12-30 12:27:00 -0500459 }
460 }
461
Florin Malita2a8275b2018-01-02 12:52:43 -0500462 if (draws.empty()) {
463 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500464 }
465
Florin Malitae6345d92018-01-03 23:37:54 -0500466 for (auto draw = draws.rbegin(); draw != draws.rend(); ++draw) {
467 shape_group->addChild(std::move(*draw));
Florin Malita2a8275b2018-01-02 12:52:43 -0500468 }
469
Florin Malitae6345d92018-01-03 23:37:54 -0500470 LOG("** Attached shape: %zd draws.\n", draws.size());
Florin Malitadacc02b2017-12-31 09:12:31 -0500471 return xformed_group;
Florin Malita094ccde2017-12-30 12:27:00 -0500472}
473
474sk_sp<sksg::RenderNode> AttachCompLayer(const Json::Value& layer, AttachContext* ctx) {
475 SkASSERT(layer.isObject());
476
477 auto refId = ParseString(layer["refId"], "");
478 if (refId.isEmpty()) {
479 LOG("!! Comp layer missing refId\n");
480 return nullptr;
481 }
482
483 const auto* comp = ctx->fAssets.find(refId);
484 if (!comp) {
485 LOG("!! Pre-comp not found: '%s'\n", refId.c_str());
486 return nullptr;
487 }
488
489 // TODO: cycle detection
490 return AttachComposition(**comp, ctx);
491}
492
493sk_sp<sksg::RenderNode> AttachSolidLayer(const Json::Value& layer, AttachContext*) {
494 SkASSERT(layer.isObject());
495
496 LOG("?? Solid layer stub\n");
497 return nullptr;
498}
499
500sk_sp<sksg::RenderNode> AttachImageLayer(const Json::Value& layer, AttachContext*) {
501 SkASSERT(layer.isObject());
502
503 LOG("?? Image layer stub\n");
504 return nullptr;
505}
506
507sk_sp<sksg::RenderNode> AttachNullLayer(const Json::Value& layer, AttachContext*) {
508 SkASSERT(layer.isObject());
509
Florin Malita18eafd92018-01-04 21:11:55 -0500510 // Null layers are used solely to drive dependent transforms,
511 // but we use free-floating sksg::Matrices for that purpose.
Florin Malita094ccde2017-12-30 12:27:00 -0500512 return nullptr;
513}
514
515sk_sp<sksg::RenderNode> AttachShapeLayer(const Json::Value& layer, AttachContext* ctx) {
516 SkASSERT(layer.isObject());
517
518 LOG("** Attaching shape layer ind: %d\n", ParseInt(layer["ind"], 0));
519
520 return AttachShape(layer["shapes"], ctx);
521}
522
523sk_sp<sksg::RenderNode> AttachTextLayer(const Json::Value& layer, AttachContext*) {
524 SkASSERT(layer.isObject());
525
526 LOG("?? Text layer stub\n");
527 return nullptr;
528}
529
Florin Malita18eafd92018-01-04 21:11:55 -0500530struct AttachLayerContext {
531 AttachLayerContext(const Json::Value& jlayers, AttachContext* ctx)
532 : fLayerList(jlayers), fCtx(ctx) {}
533
534 const Json::Value& fLayerList;
535 AttachContext* fCtx;
536 std::unordered_map<const Json::Value*, sk_sp<sksg::Matrix>> fLayerMatrixCache;
537 std::unordered_map<int, const Json::Value*> fLayerIndexCache;
538
539 const Json::Value* findLayer(int index) {
540 SkASSERT(fLayerList.isArray());
541
542 if (index < 0) {
543 return nullptr;
544 }
545
546 const auto cached = fLayerIndexCache.find(index);
547 if (cached != fLayerIndexCache.end()) {
548 return cached->second;
549 }
550
551 for (const auto& l : fLayerList) {
552 if (!l.isObject()) {
553 continue;
554 }
555
556 if (ParseInt(l["ind"], -1) == index) {
557 fLayerIndexCache.insert(std::make_pair(index, &l));
558 return &l;
559 }
560 }
561
562 return nullptr;
563 }
564
565 sk_sp<sksg::Matrix> AttachLayerMatrix(const Json::Value& jlayer) {
566 SkASSERT(jlayer.isObject());
567
568 const auto cached = fLayerMatrixCache.find(&jlayer);
569 if (cached != fLayerMatrixCache.end()) {
570 return cached->second;
571 }
572
573 const auto* parentLayer = this->findLayer(ParseInt(jlayer["parent"], -1));
574
575 // TODO: cycle detection?
576 auto parentMatrix = (parentLayer && parentLayer != &jlayer)
577 ? this->AttachLayerMatrix(*parentLayer) : nullptr;
578
579 auto layerMatrix = AttachMatrix(jlayer["ks"], fCtx, std::move(parentMatrix));
580 fLayerMatrixCache.insert(std::make_pair(&jlayer, layerMatrix));
581
582 return layerMatrix;
583 }
584};
585
586sk_sp<sksg::RenderNode> AttachLayer(const Json::Value& jlayer,
587 AttachLayerContext* layerCtx) {
588 if (!jlayer.isObject())
Florin Malita094ccde2017-12-30 12:27:00 -0500589 return nullptr;
590
591 using LayerAttacher = sk_sp<sksg::RenderNode> (*)(const Json::Value&, AttachContext*);
592 static constexpr LayerAttacher gLayerAttachers[] = {
593 AttachCompLayer, // 'ty': 0
594 AttachSolidLayer, // 'ty': 1
595 AttachImageLayer, // 'ty': 2
596 AttachNullLayer, // 'ty': 3
597 AttachShapeLayer, // 'ty': 4
598 AttachTextLayer, // 'ty': 5
599 };
600
Florin Malita18eafd92018-01-04 21:11:55 -0500601 int type = ParseInt(jlayer["ty"], -1);
Florin Malita094ccde2017-12-30 12:27:00 -0500602 if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gLayerAttachers))) {
603 return nullptr;
604 }
605
Florin Malita18eafd92018-01-04 21:11:55 -0500606 auto layer = gLayerAttachers[type](jlayer, layerCtx->fCtx);
607 auto layerMatrix = layerCtx->AttachLayerMatrix(jlayer);
608
609 return layerMatrix
610 ? sksg::Transform::Make(std::move(layer), std::move(layerMatrix))
611 : layer;
Florin Malita094ccde2017-12-30 12:27:00 -0500612}
613
614sk_sp<sksg::RenderNode> AttachComposition(const Json::Value& comp, AttachContext* ctx) {
615 if (!comp.isObject())
616 return nullptr;
617
Florin Malita18eafd92018-01-04 21:11:55 -0500618 const auto& jlayers = comp["layers"];
619 if (!jlayers.isArray())
620 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500621
Florin Malita18eafd92018-01-04 21:11:55 -0500622 SkSTArray<16, sk_sp<sksg::RenderNode>, true> layers;
623 AttachLayerContext layerCtx(jlayers, ctx);
624
625 for (const auto& l : jlayers) {
626 if (auto layer_fragment = AttachLayer(l, &layerCtx)) {
Florin Malita2a8275b2018-01-02 12:52:43 -0500627 layers.push_back(std::move(layer_fragment));
Florin Malita094ccde2017-12-30 12:27:00 -0500628 }
629 }
630
Florin Malita2a8275b2018-01-02 12:52:43 -0500631 if (layers.empty()) {
632 return nullptr;
633 }
634
635 // Layers are painted in bottom->top order.
636 auto comp_group = sksg::Group::Make();
637 for (int i = layers.count() - 1; i >= 0; --i) {
638 comp_group->addChild(std::move(layers[i]));
639 }
640
641 LOG("** Attached composition '%s': %d layers.\n",
642 ParseString(comp["id"], "").c_str(), layers.count());
643
Florin Malita094ccde2017-12-30 12:27:00 -0500644 return comp_group;
645}
646
647} // namespace
648
649std::unique_ptr<Animation> Animation::Make(SkStream* stream) {
650 if (!stream->hasLength()) {
651 // TODO: handle explicit buffering?
652 LOG("!! cannot parse streaming content\n");
653 return nullptr;
654 }
655
656 Json::Value json;
657 {
658 auto data = SkData::MakeFromStream(stream, stream->getLength());
659 if (!data) {
660 LOG("!! could not read stream\n");
661 return nullptr;
662 }
663
664 Json::Reader reader;
665
666 auto dataStart = static_cast<const char*>(data->data());
667 if (!reader.parse(dataStart, dataStart + data->size(), json, false) || !json.isObject()) {
668 LOG("!! failed to parse json: %s\n", reader.getFormattedErrorMessages().c_str());
669 return nullptr;
670 }
671 }
672
673 const auto version = ParseString(json["v"], "");
674 const auto size = SkSize::Make(ParseScalar(json["w"], -1), ParseScalar(json["h"], -1));
675 const auto fps = ParseScalar(json["fr"], -1);
676
677 if (size.isEmpty() || version.isEmpty() || fps < 0) {
678 LOG("!! invalid animation params (version: %s, size: [%f %f], frame rate: %f)",
679 version.c_str(), size.width(), size.height(), fps);
680 return nullptr;
681 }
682
683 return std::unique_ptr<Animation>(new Animation(std::move(version), size, fps, json));
684}
685
686Animation::Animation(SkString version, const SkSize& size, SkScalar fps, const Json::Value& json)
687 : fVersion(std::move(version))
688 , fSize(size)
689 , fFrameRate(fps)
690 , fInPoint(ParseScalar(json["ip"], 0))
691 , fOutPoint(SkTMax(ParseScalar(json["op"], SK_ScalarMax), fInPoint)) {
692
693 AssetMap assets;
694 for (const auto& asset : json["assets"]) {
695 if (!asset.isObject()) {
696 continue;
697 }
698
699 assets.set(ParseString(asset["id"], ""), &asset);
700 }
701
702 AttachContext ctx = { assets, fAnimators };
703 fDom = AttachComposition(json, &ctx);
704
705 LOG("** Attached %d animators\n", fAnimators.count());
706}
707
708Animation::~Animation() = default;
709
710void Animation::render(SkCanvas* canvas) const {
711 if (!fDom)
712 return;
713
714 sksg::InvalidationController ic;
715 fDom->revalidate(&ic, SkMatrix::I());
716
717 // TODO: proper inval
718 fDom->render(canvas);
719
720 if (!fShowInval)
721 return;
722
723 SkPaint fill, stroke;
724 fill.setAntiAlias(true);
725 fill.setColor(0x40ff0000);
726 stroke.setAntiAlias(true);
727 stroke.setColor(0xffff0000);
728 stroke.setStyle(SkPaint::kStroke_Style);
729
730 for (const auto& r : ic) {
731 canvas->drawRect(r, fill);
732 canvas->drawRect(r, stroke);
733 }
734}
735
736void Animation::animationTick(SkMSec ms) {
737 // 't' in the BM model really means 'frame #'
738 auto t = static_cast<float>(ms) * fFrameRate / 1000;
739
740 t = fInPoint + std::fmod(t, fOutPoint - fInPoint);
741
742 // TODO: this can be optimized quite a bit with some sorting/state tracking.
743 for (const auto& a : fAnimators) {
744 a->tick(t);
745 }
746}
747
748} // namespace skotty