blob: 077640b69586dbc6e76e3ca4f13f4a465aabf616 [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"
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"
19#include "SkPath.h"
20#include "SkPoint.h"
21#include "SkSGColor.h"
22#include "SkSGDraw.h"
Florin Malita094ccde2017-12-30 12:27:00 -050023#include "SkSGGroup.h"
Florin Malita49328072018-01-08 12:51:12 -050024#include "SkSGImage.h"
25#include "SkSGInvalidationController.h"
Florin Malitae6345d92018-01-03 23:37:54 -050026#include "SkSGMerge.h"
Florin Malita094ccde2017-12-30 12:27:00 -050027#include "SkSGPath.h"
Florin Malita2e1d7e22018-01-02 10:40:00 -050028#include "SkSGRect.h"
Florin Malita094ccde2017-12-30 12:27:00 -050029#include "SkSGTransform.h"
Florin Malita51b8c892018-01-07 08:54:24 -050030#include "SkSGTrimEffect.h"
Florin Malita094ccde2017-12-30 12:27:00 -050031#include "SkStream.h"
32#include "SkTArray.h"
33#include "SkTHash.h"
34
35#include <cmath>
Florin Malita18eafd92018-01-04 21:11:55 -050036#include <unordered_map>
Florin Malitae6345d92018-01-03 23:37:54 -050037#include <vector>
38
Florin Malita094ccde2017-12-30 12:27:00 -050039#include "stdlib.h"
40
41namespace skotty {
42
43namespace {
44
45using AssetMap = SkTHashMap<SkString, const Json::Value*>;
46
47struct AttachContext {
Florin Malita49328072018-01-08 12:51:12 -050048 const ResourceProvider& fResources;
Florin Malita094ccde2017-12-30 12:27:00 -050049 const AssetMap& fAssets;
50 SkTArray<std::unique_ptr<AnimatorBase>>& fAnimators;
51};
52
53bool LogFail(const Json::Value& json, const char* msg) {
54 const auto dump = json.toStyledString();
55 LOG("!! %s: %s", msg, dump.c_str());
56 return false;
57}
58
59// This is the workhorse for binding properties: depending on whether the property is animated,
60// it will either apply immediately or instantiate and attach a keyframe animator.
Florin Malita9661b982018-01-06 14:25:49 -050061template <typename ValueT, typename AttrT, typename NodeT>
Florin Malita094ccde2017-12-30 12:27:00 -050062bool AttachProperty(const Json::Value& jprop, AttachContext* ctx, const sk_sp<NodeT>& node,
Florin Malita9661b982018-01-06 14:25:49 -050063 typename Animator<ValueT, AttrT, NodeT>::ApplyFuncT&& apply) {
Florin Malita094ccde2017-12-30 12:27:00 -050064 if (!jprop.isObject())
65 return false;
66
Florin Malita95448a92018-01-08 10:15:12 -050067 const auto& jpropA = jprop["a"];
68 const auto& jpropK = jprop["k"];
69
70 // Older Json versions don't have an "a" animation marker.
71 // For those, we attempt to parse both ways.
72 if (jpropA.isNull() || !ParseBool(jpropA, "false")) {
Florin Malita094ccde2017-12-30 12:27:00 -050073 ValueT val;
Florin Malita95448a92018-01-08 10:15:12 -050074 if (ValueT::Parse(jpropK, &val)) {
75 // Static property.
76 apply(node, val.template as<AttrT>());
77 return true;
Florin Malita094ccde2017-12-30 12:27:00 -050078 }
79
Florin Malita95448a92018-01-08 10:15:12 -050080 if (!jpropA.isNull()) {
81 return LogFail(jprop, "Could not parse (explicit) static property");
Florin Malita094ccde2017-12-30 12:27:00 -050082 }
Florin Malita094ccde2017-12-30 12:27:00 -050083 }
84
Florin Malita95448a92018-01-08 10:15:12 -050085 // Keyframe property.
86 using AnimatorT = Animator<ValueT, AttrT, NodeT>;
87 auto animator = AnimatorT::Make(jpropK, node, std::move(apply));
88
89 if (!animator) {
90 return LogFail(jprop, "Could not parse keyframed property");
91 }
92
93 ctx->fAnimators.push_back(std::move(animator));
94
Florin Malita094ccde2017-12-30 12:27:00 -050095 return true;
96}
97
Florin Malita18eafd92018-01-04 21:11:55 -050098sk_sp<sksg::Matrix> AttachMatrix(const Json::Value& t, AttachContext* ctx,
99 sk_sp<sksg::Matrix> parentMatrix) {
100 if (!t.isObject())
101 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500102
Florin Malita18eafd92018-01-04 21:11:55 -0500103 auto matrix = sksg::Matrix::Make(SkMatrix::I(), std::move(parentMatrix));
104 auto composite = sk_make_sp<CompositeTransform>(matrix);
105 auto anchor_attached = AttachProperty<VectorValue, SkPoint>(t["a"], ctx, composite,
Florin Malita094ccde2017-12-30 12:27:00 -0500106 [](const sk_sp<CompositeTransform>& node, const SkPoint& a) {
107 node->setAnchorPoint(a);
108 });
Florin Malita18eafd92018-01-04 21:11:55 -0500109 auto position_attached = AttachProperty<VectorValue, SkPoint>(t["p"], ctx, composite,
Florin Malita094ccde2017-12-30 12:27:00 -0500110 [](const sk_sp<CompositeTransform>& node, const SkPoint& p) {
111 node->setPosition(p);
112 });
Florin Malita18eafd92018-01-04 21:11:55 -0500113 auto scale_attached = AttachProperty<VectorValue, SkVector>(t["s"], ctx, composite,
Florin Malita094ccde2017-12-30 12:27:00 -0500114 [](const sk_sp<CompositeTransform>& node, const SkVector& s) {
115 node->setScale(s);
116 });
Florin Malita18eafd92018-01-04 21:11:55 -0500117 auto rotation_attached = AttachProperty<ScalarValue, SkScalar>(t["r"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500118 [](const sk_sp<CompositeTransform>& node, const SkScalar& r) {
Florin Malita094ccde2017-12-30 12:27:00 -0500119 node->setRotation(r);
120 });
Florin Malita18eafd92018-01-04 21:11:55 -0500121 auto skew_attached = AttachProperty<ScalarValue, SkScalar>(t["sk"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500122 [](const sk_sp<CompositeTransform>& node, const SkScalar& sk) {
Florin Malita094ccde2017-12-30 12:27:00 -0500123 node->setSkew(sk);
124 });
Florin Malita18eafd92018-01-04 21:11:55 -0500125 auto skewaxis_attached = AttachProperty<ScalarValue, SkScalar>(t["sa"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500126 [](const sk_sp<CompositeTransform>& node, const SkScalar& sa) {
Florin Malita094ccde2017-12-30 12:27:00 -0500127 node->setSkewAxis(sa);
128 });
129
130 if (!anchor_attached &&
131 !position_attached &&
132 !scale_attached &&
133 !rotation_attached &&
134 !skew_attached &&
135 !skewaxis_attached) {
136 LogFail(t, "Could not parse transform");
Florin Malita18eafd92018-01-04 21:11:55 -0500137 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500138 }
139
Florin Malita18eafd92018-01-04 21:11:55 -0500140 return matrix;
Florin Malita094ccde2017-12-30 12:27:00 -0500141}
142
143sk_sp<sksg::RenderNode> AttachShape(const Json::Value&, AttachContext* ctx);
144sk_sp<sksg::RenderNode> AttachComposition(const Json::Value&, AttachContext* ctx);
145
146sk_sp<sksg::RenderNode> AttachShapeGroup(const Json::Value& jgroup, AttachContext* ctx) {
147 SkASSERT(jgroup.isObject());
148
149 return AttachShape(jgroup["it"], ctx);
150}
151
152sk_sp<sksg::GeometryNode> AttachPathGeometry(const Json::Value& jpath, AttachContext* ctx) {
153 SkASSERT(jpath.isObject());
154
155 auto path_node = sksg::Path::Make();
156 auto path_attached = AttachProperty<ShapeValue, SkPath>(jpath["ks"], ctx, path_node,
157 [](const sk_sp<sksg::Path>& node, const SkPath& p) { node->setPath(p); });
158
159 if (path_attached)
160 LOG("** Attached path geometry - verbs: %d\n", path_node->getPath().countVerbs());
161
162 return path_attached ? path_node : nullptr;
163}
164
Florin Malita2e1d7e22018-01-02 10:40:00 -0500165sk_sp<sksg::GeometryNode> AttachRRectGeometry(const Json::Value& jrect, AttachContext* ctx) {
166 SkASSERT(jrect.isObject());
167
168 auto rect_node = sksg::RRect::Make();
169 auto composite = sk_make_sp<CompositeRRect>(rect_node);
170
171 auto p_attached = AttachProperty<VectorValue, SkPoint>(jrect["p"], ctx, composite,
172 [](const sk_sp<CompositeRRect>& node, const SkPoint& pos) { node->setPosition(pos); });
173 auto s_attached = AttachProperty<VectorValue, SkSize>(jrect["s"], ctx, composite,
174 [](const sk_sp<CompositeRRect>& node, const SkSize& sz) { node->setSize(sz); });
175 auto r_attached = AttachProperty<ScalarValue, SkScalar>(jrect["r"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500176 [](const sk_sp<CompositeRRect>& node, const SkScalar& radius) {
Florin Malitafbc13f12018-01-04 10:26:35 -0500177 node->setRadius(SkSize::Make(radius, radius));
178 });
Florin Malita2e1d7e22018-01-02 10:40:00 -0500179
180 if (!p_attached && !s_attached && !r_attached) {
181 return nullptr;
182 }
183
Florin Malitafbc13f12018-01-04 10:26:35 -0500184 LOG("** Attached (r)rect geometry\n");
185
186 return rect_node;
187}
188
189sk_sp<sksg::GeometryNode> AttachEllipseGeometry(const Json::Value& jellipse, AttachContext* ctx) {
190 SkASSERT(jellipse.isObject());
191
192 auto rect_node = sksg::RRect::Make();
193 auto composite = sk_make_sp<CompositeRRect>(rect_node);
194
195 auto p_attached = AttachProperty<VectorValue, SkPoint>(jellipse["p"], ctx, composite,
196 [](const sk_sp<CompositeRRect>& node, const SkPoint& pos) { node->setPosition(pos); });
197 auto s_attached = AttachProperty<VectorValue, SkSize>(jellipse["s"], ctx, composite,
198 [](const sk_sp<CompositeRRect>& node, const SkSize& sz) {
199 node->setSize(sz);
200 node->setRadius(SkSize::Make(sz.width() / 2, sz.height() / 2));
201 });
202
203 if (!p_attached && !s_attached) {
204 return nullptr;
205 }
206
207 LOG("** Attached ellipse geometry\n");
208
Florin Malita2e1d7e22018-01-02 10:40:00 -0500209 return rect_node;
210}
211
Florin Malita02a32b02018-01-04 11:27:09 -0500212sk_sp<sksg::GeometryNode> AttachPolystarGeometry(const Json::Value& jstar, AttachContext* ctx) {
213 SkASSERT(jstar.isObject());
214
215 static constexpr CompositePolyStar::Type gTypes[] = {
216 CompositePolyStar::Type::kStar, // "sy": 1
217 CompositePolyStar::Type::kPoly, // "sy": 2
218 };
219
220 const auto type = ParseInt(jstar["sy"], 0) - 1;
221 if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gTypes))) {
222 LogFail(jstar, "Unknown polystar type");
223 return nullptr;
224 }
225
226 auto path_node = sksg::Path::Make();
227 auto composite = sk_make_sp<CompositePolyStar>(path_node, gTypes[type]);
228
229 AttachProperty<VectorValue, SkPoint>(jstar["p"], ctx, composite,
230 [](const sk_sp<CompositePolyStar>& node, const SkPoint& p) { node->setPosition(p); });
231 AttachProperty<ScalarValue, SkScalar>(jstar["pt"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500232 [](const sk_sp<CompositePolyStar>& node, const SkScalar& pt) { node->setPointCount(pt); });
Florin Malita02a32b02018-01-04 11:27:09 -0500233 AttachProperty<ScalarValue, SkScalar>(jstar["ir"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500234 [](const sk_sp<CompositePolyStar>& node, const SkScalar& ir) { node->setInnerRadius(ir); });
Florin Malita02a32b02018-01-04 11:27:09 -0500235 AttachProperty<ScalarValue, SkScalar>(jstar["or"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500236 [](const sk_sp<CompositePolyStar>& node, const SkScalar& otr) {
237 node->setOuterRadius(otr);
238 });
Florin Malita02a32b02018-01-04 11:27:09 -0500239 AttachProperty<ScalarValue, SkScalar>(jstar["is"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500240 [](const sk_sp<CompositePolyStar>& node, const SkScalar& is) {
241 node->setInnerRoundness(is);
242 });
Florin Malita02a32b02018-01-04 11:27:09 -0500243 AttachProperty<ScalarValue, SkScalar>(jstar["os"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500244 [](const sk_sp<CompositePolyStar>& node, const SkScalar& os) {
245 node->setOuterRoundness(os);
246 });
Florin Malita02a32b02018-01-04 11:27:09 -0500247 AttachProperty<ScalarValue, SkScalar>(jstar["r"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500248 [](const sk_sp<CompositePolyStar>& node, const SkScalar& r) { node->setRotation(r); });
Florin Malita02a32b02018-01-04 11:27:09 -0500249
250 return path_node;
251}
252
Florin Malita094ccde2017-12-30 12:27:00 -0500253sk_sp<sksg::Color> AttachColorPaint(const Json::Value& obj, AttachContext* ctx) {
254 SkASSERT(obj.isObject());
255
256 auto color_node = sksg::Color::Make(SK_ColorBLACK);
257 color_node->setAntiAlias(true);
258
259 auto color_attached = AttachProperty<VectorValue, SkColor>(obj["c"], ctx, color_node,
Florin Malita9661b982018-01-06 14:25:49 -0500260 [](const sk_sp<sksg::Color>& node, const SkColor& c) { node->setColor(c); });
Florin Malita094ccde2017-12-30 12:27:00 -0500261
262 return color_attached ? color_node : nullptr;
263}
264
265sk_sp<sksg::PaintNode> AttachFillPaint(const Json::Value& jfill, AttachContext* ctx) {
266 SkASSERT(jfill.isObject());
267
268 auto color = AttachColorPaint(jfill, ctx);
269 if (color) {
270 LOG("** Attached color fill: 0x%x\n", color->getColor());
271 }
272 return color;
273}
274
275sk_sp<sksg::PaintNode> AttachStrokePaint(const Json::Value& jstroke, AttachContext* ctx) {
276 SkASSERT(jstroke.isObject());
277
278 auto stroke_node = AttachColorPaint(jstroke, ctx);
279 if (!stroke_node)
280 return nullptr;
281
282 LOG("** Attached color stroke: 0x%x\n", stroke_node->getColor());
283
284 stroke_node->setStyle(SkPaint::kStroke_Style);
285
286 auto width_attached = AttachProperty<ScalarValue, SkScalar>(jstroke["w"], ctx, stroke_node,
Florin Malita9661b982018-01-06 14:25:49 -0500287 [](const sk_sp<sksg::Color>& node, const SkScalar& width) { node->setStrokeWidth(width); });
Florin Malita094ccde2017-12-30 12:27:00 -0500288 if (!width_attached)
289 return nullptr;
290
291 stroke_node->setStrokeMiter(ParseScalar(jstroke["ml"], 4));
292
293 static constexpr SkPaint::Join gJoins[] = {
294 SkPaint::kMiter_Join,
295 SkPaint::kRound_Join,
296 SkPaint::kBevel_Join,
297 };
298 stroke_node->setStrokeJoin(gJoins[SkTPin<int>(ParseInt(jstroke["lj"], 1) - 1,
299 0, SK_ARRAY_COUNT(gJoins) - 1)]);
300
301 static constexpr SkPaint::Cap gCaps[] = {
302 SkPaint::kButt_Cap,
303 SkPaint::kRound_Cap,
304 SkPaint::kSquare_Cap,
305 };
306 stroke_node->setStrokeCap(gCaps[SkTPin<int>(ParseInt(jstroke["lc"], 1) - 1,
307 0, SK_ARRAY_COUNT(gCaps) - 1)]);
308
309 return stroke_node;
310}
311
Florin Malitae6345d92018-01-03 23:37:54 -0500312std::vector<sk_sp<sksg::GeometryNode>> AttachMergeGeometryEffect(
313 const Json::Value& jmerge, AttachContext* ctx, std::vector<sk_sp<sksg::GeometryNode>>&& geos) {
314 std::vector<sk_sp<sksg::GeometryNode>> merged;
315
316 static constexpr sksg::Merge::Mode gModes[] = {
317 sksg::Merge::Mode::kMerge, // "mm": 1
318 sksg::Merge::Mode::kUnion, // "mm": 2
319 sksg::Merge::Mode::kDifference, // "mm": 3
320 sksg::Merge::Mode::kIntersect, // "mm": 4
321 sksg::Merge::Mode::kXOR , // "mm": 5
322 };
323
Florin Malita51b8c892018-01-07 08:54:24 -0500324 const auto mode = gModes[SkTPin<int>(ParseInt(jmerge["mm"], 1) - 1,
325 0, SK_ARRAY_COUNT(gModes) - 1)];
Florin Malitae6345d92018-01-03 23:37:54 -0500326 merged.push_back(sksg::Merge::Make(std::move(geos), mode));
327
328 LOG("** Attached merge path effect, mode: %d\n", mode);
329
330 return merged;
331}
332
Florin Malita51b8c892018-01-07 08:54:24 -0500333std::vector<sk_sp<sksg::GeometryNode>> AttachTrimGeometryEffect(
334 const Json::Value& jtrim, AttachContext* ctx, std::vector<sk_sp<sksg::GeometryNode>>&& geos) {
335
336 enum class Mode {
337 kMerged, // "m": 1
338 kSeparate, // "m": 2
339 } gModes[] = { Mode::kMerged, Mode::kSeparate };
340
341 const auto mode = gModes[SkTPin<int>(ParseInt(jtrim["m"], 1) - 1,
342 0, SK_ARRAY_COUNT(gModes) - 1)];
343
344 std::vector<sk_sp<sksg::GeometryNode>> inputs;
345 if (mode == Mode::kMerged) {
346 inputs.push_back(sksg::Merge::Make(std::move(geos), sksg::Merge::Mode::kMerge));
347 } else {
348 inputs = std::move(geos);
349 }
350
351 std::vector<sk_sp<sksg::GeometryNode>> trimmed;
352 trimmed.reserve(inputs.size());
353 for (const auto& i : inputs) {
354 const auto trim = sksg::TrimEffect::Make(i);
355 trimmed.push_back(trim);
356 AttachProperty<ScalarValue, SkScalar>(jtrim["s"], ctx, trim,
357 [](const sk_sp<sksg::TrimEffect>& node, const SkScalar& s) {
358 node->setStart(s * 0.01f);
359 });
360 AttachProperty<ScalarValue, SkScalar>(jtrim["e"], ctx, trim,
361 [](const sk_sp<sksg::TrimEffect>& node, const SkScalar& e) {
362 node->setEnd(e * 0.01f);
363 });
364 // TODO: "offset" doesn't currently work the same as BM - figure out what's going on.
365 AttachProperty<ScalarValue, SkScalar>(jtrim["o"], ctx, trim,
366 [](const sk_sp<sksg::TrimEffect>& node, const SkScalar& o) {
367 node->setOffset(o * 0.01f);
368 });
369 }
370
371 return trimmed;
372}
373
Florin Malita094ccde2017-12-30 12:27:00 -0500374using GeometryAttacherT = sk_sp<sksg::GeometryNode> (*)(const Json::Value&, AttachContext*);
375static constexpr GeometryAttacherT gGeometryAttachers[] = {
376 AttachPathGeometry,
Florin Malita2e1d7e22018-01-02 10:40:00 -0500377 AttachRRectGeometry,
Florin Malitafbc13f12018-01-04 10:26:35 -0500378 AttachEllipseGeometry,
Florin Malita02a32b02018-01-04 11:27:09 -0500379 AttachPolystarGeometry,
Florin Malita094ccde2017-12-30 12:27:00 -0500380};
381
382using PaintAttacherT = sk_sp<sksg::PaintNode> (*)(const Json::Value&, AttachContext*);
383static constexpr PaintAttacherT gPaintAttachers[] = {
384 AttachFillPaint,
385 AttachStrokePaint,
386};
387
388using GroupAttacherT = sk_sp<sksg::RenderNode> (*)(const Json::Value&, AttachContext*);
389static constexpr GroupAttacherT gGroupAttachers[] = {
390 AttachShapeGroup,
391};
392
Florin Malitae6345d92018-01-03 23:37:54 -0500393using GeometryEffectAttacherT =
394 std::vector<sk_sp<sksg::GeometryNode>> (*)(const Json::Value&,
395 AttachContext*,
396 std::vector<sk_sp<sksg::GeometryNode>>&&);
397static constexpr GeometryEffectAttacherT gGeometryEffectAttachers[] = {
398 AttachMergeGeometryEffect,
Florin Malita51b8c892018-01-07 08:54:24 -0500399 AttachTrimGeometryEffect,
Florin Malitae6345d92018-01-03 23:37:54 -0500400};
401
Florin Malita094ccde2017-12-30 12:27:00 -0500402enum class ShapeType {
403 kGeometry,
Florin Malitae6345d92018-01-03 23:37:54 -0500404 kGeometryEffect,
Florin Malita094ccde2017-12-30 12:27:00 -0500405 kPaint,
406 kGroup,
Florin Malitadacc02b2017-12-31 09:12:31 -0500407 kTransform,
Florin Malita094ccde2017-12-30 12:27:00 -0500408};
409
410struct ShapeInfo {
411 const char* fTypeString;
412 ShapeType fShapeType;
413 uint32_t fAttacherIndex; // index into respective attacher tables
414};
415
416const ShapeInfo* FindShapeInfo(const Json::Value& shape) {
417 static constexpr ShapeInfo gShapeInfo[] = {
Florin Malitafbc13f12018-01-04 10:26:35 -0500418 { "el", ShapeType::kGeometry , 2 }, // ellipse -> AttachEllipseGeometry
Florin Malitae6345d92018-01-03 23:37:54 -0500419 { "fl", ShapeType::kPaint , 0 }, // fill -> AttachFillPaint
420 { "gr", ShapeType::kGroup , 0 }, // group -> AttachShapeGroup
421 { "mm", ShapeType::kGeometryEffect, 0 }, // merge -> AttachMergeGeometryEffect
Florin Malita02a32b02018-01-04 11:27:09 -0500422 { "rc", ShapeType::kGeometry , 1 }, // rrect -> AttachRRectGeometry
Florin Malitae6345d92018-01-03 23:37:54 -0500423 { "sh", ShapeType::kGeometry , 0 }, // shape -> AttachPathGeometry
Florin Malita02a32b02018-01-04 11:27:09 -0500424 { "sr", ShapeType::kGeometry , 3 }, // polystar -> AttachPolyStarGeometry
Florin Malitae6345d92018-01-03 23:37:54 -0500425 { "st", ShapeType::kPaint , 1 }, // stroke -> AttachStrokePaint
Florin Malita51b8c892018-01-07 08:54:24 -0500426 { "tm", ShapeType::kGeometryEffect, 1 }, // trim -> AttachTrimGeometryEffect
Florin Malita18eafd92018-01-04 21:11:55 -0500427 { "tr", ShapeType::kTransform , 0 }, // transform -> In-place handler
Florin Malita094ccde2017-12-30 12:27:00 -0500428 };
429
430 if (!shape.isObject())
431 return nullptr;
432
433 const auto& type = shape["ty"];
434 if (!type.isString())
435 return nullptr;
436
437 const auto* info = bsearch(type.asCString(),
438 gShapeInfo,
439 SK_ARRAY_COUNT(gShapeInfo),
440 sizeof(ShapeInfo),
441 [](const void* key, const void* info) {
442 return strcmp(static_cast<const char*>(key),
443 static_cast<const ShapeInfo*>(info)->fTypeString);
444 });
445
446 return static_cast<const ShapeInfo*>(info);
447}
448
449sk_sp<sksg::RenderNode> AttachShape(const Json::Value& shapeArray, AttachContext* ctx) {
450 if (!shapeArray.isArray())
451 return nullptr;
452
Florin Malita2a8275b2018-01-02 12:52:43 -0500453 // (https://helpx.adobe.com/after-effects/using/overview-shape-layers-paths-vector.html#groups_and_render_order_for_shapes_and_shape_attributes)
454 //
455 // Render order for shapes within a shape layer
456 //
457 // The rules for rendering a shape layer are similar to the rules for rendering a composition
458 // that contains nested compositions:
459 //
460 // * Within a group, the shape at the bottom of the Timeline panel stacking order is rendered
461 // first.
462 //
463 // * All path operations within a group are performed before paint operations. This means,
464 // for example, that the stroke follows the distortions in the path made by the Wiggle Paths
465 // path operation. Path operations within a group are performed from top to bottom.
466 //
467 // * Paint operations within a group are performed from the bottom to the top in the Timeline
468 // panel stacking order. This means, for example, that a stroke is rendered on top of
469 // (in front of) a stroke that appears after it in the Timeline panel.
470 //
Florin Malitadacc02b2017-12-31 09:12:31 -0500471 sk_sp<sksg::Group> shape_group = sksg::Group::Make();
472 sk_sp<sksg::RenderNode> xformed_group = shape_group;
Florin Malita094ccde2017-12-30 12:27:00 -0500473
Florin Malitae6345d92018-01-03 23:37:54 -0500474 std::vector<sk_sp<sksg::GeometryNode>> geos;
475 std::vector<sk_sp<sksg::RenderNode>> draws;
Florin Malita094ccde2017-12-30 12:27:00 -0500476
477 for (const auto& s : shapeArray) {
478 const auto* info = FindShapeInfo(s);
479 if (!info) {
480 LogFail(s.isObject() ? s["ty"] : s, "Unknown shape");
481 continue;
482 }
483
484 switch (info->fShapeType) {
485 case ShapeType::kGeometry: {
486 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGeometryAttachers));
487 if (auto geo = gGeometryAttachers[info->fAttacherIndex](s, ctx)) {
488 geos.push_back(std::move(geo));
489 }
490 } break;
Florin Malitae6345d92018-01-03 23:37:54 -0500491 case ShapeType::kGeometryEffect: {
492 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGeometryEffectAttachers));
493 geos = gGeometryEffectAttachers[info->fAttacherIndex](s, ctx, std::move(geos));
494 } break;
Florin Malita094ccde2017-12-30 12:27:00 -0500495 case ShapeType::kPaint: {
496 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gPaintAttachers));
497 if (auto paint = gPaintAttachers[info->fAttacherIndex](s, ctx)) {
Florin Malita2a8275b2018-01-02 12:52:43 -0500498 for (const auto& geo : geos) {
499 draws.push_back(sksg::Draw::Make(geo, paint));
500 }
Florin Malita094ccde2017-12-30 12:27:00 -0500501 }
502 } break;
503 case ShapeType::kGroup: {
504 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGroupAttachers));
505 if (auto group = gGroupAttachers[info->fAttacherIndex](s, ctx)) {
Florin Malita2a8275b2018-01-02 12:52:43 -0500506 draws.push_back(std::move(group));
Florin Malita094ccde2017-12-30 12:27:00 -0500507 }
508 } break;
Florin Malitadacc02b2017-12-31 09:12:31 -0500509 case ShapeType::kTransform: {
Florin Malita2a8275b2018-01-02 12:52:43 -0500510 // TODO: BM appears to transform the geometry, not the draw op itself.
Florin Malita18eafd92018-01-04 21:11:55 -0500511 if (auto matrix = AttachMatrix(s, ctx, nullptr)) {
512 xformed_group = sksg::Transform::Make(std::move(xformed_group),
513 std::move(matrix));
514 }
Florin Malitadacc02b2017-12-31 09:12:31 -0500515 } break;
Florin Malita094ccde2017-12-30 12:27:00 -0500516 }
517 }
518
Florin Malita2a8275b2018-01-02 12:52:43 -0500519 if (draws.empty()) {
520 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500521 }
522
Florin Malitae6345d92018-01-03 23:37:54 -0500523 for (auto draw = draws.rbegin(); draw != draws.rend(); ++draw) {
524 shape_group->addChild(std::move(*draw));
Florin Malita2a8275b2018-01-02 12:52:43 -0500525 }
526
Florin Malitae6345d92018-01-03 23:37:54 -0500527 LOG("** Attached shape: %zd draws.\n", draws.size());
Florin Malitadacc02b2017-12-31 09:12:31 -0500528 return xformed_group;
Florin Malita094ccde2017-12-30 12:27:00 -0500529}
530
531sk_sp<sksg::RenderNode> AttachCompLayer(const Json::Value& layer, AttachContext* ctx) {
532 SkASSERT(layer.isObject());
533
534 auto refId = ParseString(layer["refId"], "");
535 if (refId.isEmpty()) {
536 LOG("!! Comp layer missing refId\n");
537 return nullptr;
538 }
539
540 const auto* comp = ctx->fAssets.find(refId);
541 if (!comp) {
542 LOG("!! Pre-comp not found: '%s'\n", refId.c_str());
543 return nullptr;
544 }
545
546 // TODO: cycle detection
547 return AttachComposition(**comp, ctx);
548}
549
550sk_sp<sksg::RenderNode> AttachSolidLayer(const Json::Value& layer, AttachContext*) {
551 SkASSERT(layer.isObject());
552
553 LOG("?? Solid layer stub\n");
554 return nullptr;
555}
556
Florin Malita49328072018-01-08 12:51:12 -0500557sk_sp<sksg::RenderNode> AttachImageAsset(const Json::Value& jimage, AttachContext* ctx) {
558 SkASSERT(jimage.isObject());
559
560 const auto name = ParseString(jimage["p"], ""),
561 path = ParseString(jimage["u"], "");
562 if (name.isEmpty())
563 return nullptr;
564
565 // TODO: plumb resource paths explicitly to ResourceProvider?
566 const auto resName = path.isEmpty() ? name : SkOSPath::Join(path.c_str(), name.c_str());
567 const auto resStream = ctx->fResources.openStream(resName.c_str());
568 if (!resStream || !resStream->hasLength()) {
569 LOG("!! Could not load image resource: %s\n", resName.c_str());
570 return nullptr;
571 }
572
573 // TODO: non-intrisic image sizing
574 return sksg::Image::Make(
575 SkImage::MakeFromEncoded(SkData::MakeFromStream(resStream.get(), resStream->getLength())));
576}
577
578sk_sp<sksg::RenderNode> AttachImageLayer(const Json::Value& layer, AttachContext* ctx) {
Florin Malita094ccde2017-12-30 12:27:00 -0500579 SkASSERT(layer.isObject());
580
Florin Malita49328072018-01-08 12:51:12 -0500581 auto refId = ParseString(layer["refId"], "");
582 if (refId.isEmpty()) {
583 LOG("!! Image layer missing refId\n");
584 return nullptr;
585 }
586
587 const auto* jimage = ctx->fAssets.find(refId);
588 if (!jimage) {
589 LOG("!! Image asset not found: '%s'\n", refId.c_str());
590 return nullptr;
591 }
592
593 return AttachImageAsset(**jimage, ctx);
Florin Malita094ccde2017-12-30 12:27:00 -0500594}
595
596sk_sp<sksg::RenderNode> AttachNullLayer(const Json::Value& layer, AttachContext*) {
597 SkASSERT(layer.isObject());
598
Florin Malita18eafd92018-01-04 21:11:55 -0500599 // Null layers are used solely to drive dependent transforms,
600 // but we use free-floating sksg::Matrices for that purpose.
Florin Malita094ccde2017-12-30 12:27:00 -0500601 return nullptr;
602}
603
604sk_sp<sksg::RenderNode> AttachShapeLayer(const Json::Value& layer, AttachContext* ctx) {
605 SkASSERT(layer.isObject());
606
607 LOG("** Attaching shape layer ind: %d\n", ParseInt(layer["ind"], 0));
608
609 return AttachShape(layer["shapes"], ctx);
610}
611
612sk_sp<sksg::RenderNode> AttachTextLayer(const Json::Value& layer, AttachContext*) {
613 SkASSERT(layer.isObject());
614
615 LOG("?? Text layer stub\n");
616 return nullptr;
617}
618
Florin Malita18eafd92018-01-04 21:11:55 -0500619struct AttachLayerContext {
620 AttachLayerContext(const Json::Value& jlayers, AttachContext* ctx)
621 : fLayerList(jlayers), fCtx(ctx) {}
622
623 const Json::Value& fLayerList;
624 AttachContext* fCtx;
625 std::unordered_map<const Json::Value*, sk_sp<sksg::Matrix>> fLayerMatrixCache;
626 std::unordered_map<int, const Json::Value*> fLayerIndexCache;
627
628 const Json::Value* findLayer(int index) {
629 SkASSERT(fLayerList.isArray());
630
631 if (index < 0) {
632 return nullptr;
633 }
634
635 const auto cached = fLayerIndexCache.find(index);
636 if (cached != fLayerIndexCache.end()) {
637 return cached->second;
638 }
639
640 for (const auto& l : fLayerList) {
641 if (!l.isObject()) {
642 continue;
643 }
644
645 if (ParseInt(l["ind"], -1) == index) {
646 fLayerIndexCache.insert(std::make_pair(index, &l));
647 return &l;
648 }
649 }
650
651 return nullptr;
652 }
653
654 sk_sp<sksg::Matrix> AttachLayerMatrix(const Json::Value& jlayer) {
655 SkASSERT(jlayer.isObject());
656
657 const auto cached = fLayerMatrixCache.find(&jlayer);
658 if (cached != fLayerMatrixCache.end()) {
659 return cached->second;
660 }
661
662 const auto* parentLayer = this->findLayer(ParseInt(jlayer["parent"], -1));
663
664 // TODO: cycle detection?
665 auto parentMatrix = (parentLayer && parentLayer != &jlayer)
666 ? this->AttachLayerMatrix(*parentLayer) : nullptr;
667
668 auto layerMatrix = AttachMatrix(jlayer["ks"], fCtx, std::move(parentMatrix));
669 fLayerMatrixCache.insert(std::make_pair(&jlayer, layerMatrix));
670
671 return layerMatrix;
672 }
673};
674
675sk_sp<sksg::RenderNode> AttachLayer(const Json::Value& jlayer,
676 AttachLayerContext* layerCtx) {
677 if (!jlayer.isObject())
Florin Malita094ccde2017-12-30 12:27:00 -0500678 return nullptr;
679
680 using LayerAttacher = sk_sp<sksg::RenderNode> (*)(const Json::Value&, AttachContext*);
681 static constexpr LayerAttacher gLayerAttachers[] = {
682 AttachCompLayer, // 'ty': 0
683 AttachSolidLayer, // 'ty': 1
684 AttachImageLayer, // 'ty': 2
685 AttachNullLayer, // 'ty': 3
686 AttachShapeLayer, // 'ty': 4
687 AttachTextLayer, // 'ty': 5
688 };
689
Florin Malita18eafd92018-01-04 21:11:55 -0500690 int type = ParseInt(jlayer["ty"], -1);
Florin Malita094ccde2017-12-30 12:27:00 -0500691 if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gLayerAttachers))) {
692 return nullptr;
693 }
694
Florin Malita18eafd92018-01-04 21:11:55 -0500695 auto layer = gLayerAttachers[type](jlayer, layerCtx->fCtx);
696 auto layerMatrix = layerCtx->AttachLayerMatrix(jlayer);
697
698 return layerMatrix
699 ? sksg::Transform::Make(std::move(layer), std::move(layerMatrix))
700 : layer;
Florin Malita094ccde2017-12-30 12:27:00 -0500701}
702
703sk_sp<sksg::RenderNode> AttachComposition(const Json::Value& comp, AttachContext* ctx) {
704 if (!comp.isObject())
705 return nullptr;
706
Florin Malita18eafd92018-01-04 21:11:55 -0500707 const auto& jlayers = comp["layers"];
708 if (!jlayers.isArray())
709 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500710
Florin Malita18eafd92018-01-04 21:11:55 -0500711 SkSTArray<16, sk_sp<sksg::RenderNode>, true> layers;
712 AttachLayerContext layerCtx(jlayers, ctx);
713
714 for (const auto& l : jlayers) {
715 if (auto layer_fragment = AttachLayer(l, &layerCtx)) {
Florin Malita2a8275b2018-01-02 12:52:43 -0500716 layers.push_back(std::move(layer_fragment));
Florin Malita094ccde2017-12-30 12:27:00 -0500717 }
718 }
719
Florin Malita2a8275b2018-01-02 12:52:43 -0500720 if (layers.empty()) {
721 return nullptr;
722 }
723
724 // Layers are painted in bottom->top order.
725 auto comp_group = sksg::Group::Make();
726 for (int i = layers.count() - 1; i >= 0; --i) {
727 comp_group->addChild(std::move(layers[i]));
728 }
729
730 LOG("** Attached composition '%s': %d layers.\n",
731 ParseString(comp["id"], "").c_str(), layers.count());
732
Florin Malita094ccde2017-12-30 12:27:00 -0500733 return comp_group;
734}
735
736} // namespace
737
Florin Malita49328072018-01-08 12:51:12 -0500738std::unique_ptr<Animation> Animation::Make(SkStream* stream, const ResourceProvider& res) {
Florin Malita094ccde2017-12-30 12:27:00 -0500739 if (!stream->hasLength()) {
740 // TODO: handle explicit buffering?
741 LOG("!! cannot parse streaming content\n");
742 return nullptr;
743 }
744
745 Json::Value json;
746 {
747 auto data = SkData::MakeFromStream(stream, stream->getLength());
748 if (!data) {
749 LOG("!! could not read stream\n");
750 return nullptr;
751 }
752
753 Json::Reader reader;
754
755 auto dataStart = static_cast<const char*>(data->data());
756 if (!reader.parse(dataStart, dataStart + data->size(), json, false) || !json.isObject()) {
757 LOG("!! failed to parse json: %s\n", reader.getFormattedErrorMessages().c_str());
758 return nullptr;
759 }
760 }
761
762 const auto version = ParseString(json["v"], "");
763 const auto size = SkSize::Make(ParseScalar(json["w"], -1), ParseScalar(json["h"], -1));
764 const auto fps = ParseScalar(json["fr"], -1);
765
766 if (size.isEmpty() || version.isEmpty() || fps < 0) {
767 LOG("!! invalid animation params (version: %s, size: [%f %f], frame rate: %f)",
768 version.c_str(), size.width(), size.height(), fps);
769 return nullptr;
770 }
771
Florin Malita49328072018-01-08 12:51:12 -0500772 return std::unique_ptr<Animation>(new Animation(res, std::move(version), size, fps, json));
Florin Malita094ccde2017-12-30 12:27:00 -0500773}
774
Florin Malita49328072018-01-08 12:51:12 -0500775std::unique_ptr<Animation> Animation::MakeFromFile(const char path[], const ResourceProvider* res) {
776 class DirectoryResourceProvider final : public ResourceProvider {
777 public:
778 explicit DirectoryResourceProvider(SkString dir) : fDir(std::move(dir)) {}
779
780 std::unique_ptr<SkStream> openStream(const char resource[]) const override {
781 const auto resPath = SkOSPath::Join(fDir.c_str(), resource);
782 return SkStream::MakeFromFile(resPath.c_str());
783 }
784
785 private:
786 const SkString fDir;
787 };
788
789 const auto jsonStream = SkStream::MakeFromFile(path);
790 if (!jsonStream)
791 return nullptr;
792
793 std::unique_ptr<ResourceProvider> defaultProvider;
794 if (!res) {
795 defaultProvider = skstd::make_unique<DirectoryResourceProvider>(SkOSPath::Dirname(path));
796 }
797
798 return Make(jsonStream.get(), res ? *res : *defaultProvider);
799}
800
801Animation::Animation(const ResourceProvider& resources,
802 SkString version, const SkSize& size, SkScalar fps, const Json::Value& json)
Florin Malita094ccde2017-12-30 12:27:00 -0500803 : fVersion(std::move(version))
804 , fSize(size)
805 , fFrameRate(fps)
806 , fInPoint(ParseScalar(json["ip"], 0))
807 , fOutPoint(SkTMax(ParseScalar(json["op"], SK_ScalarMax), fInPoint)) {
808
809 AssetMap assets;
810 for (const auto& asset : json["assets"]) {
811 if (!asset.isObject()) {
812 continue;
813 }
814
815 assets.set(ParseString(asset["id"], ""), &asset);
816 }
817
Florin Malita49328072018-01-08 12:51:12 -0500818 AttachContext ctx = { resources, assets, fAnimators };
Florin Malita094ccde2017-12-30 12:27:00 -0500819 fDom = AttachComposition(json, &ctx);
820
821 LOG("** Attached %d animators\n", fAnimators.count());
822}
823
824Animation::~Animation() = default;
825
Mike Reed29859872018-01-08 08:25:27 -0500826void Animation::render(SkCanvas* canvas, const SkRect* dstR) const {
Florin Malita094ccde2017-12-30 12:27:00 -0500827 if (!fDom)
828 return;
829
830 sksg::InvalidationController ic;
831 fDom->revalidate(&ic, SkMatrix::I());
832
833 // TODO: proper inval
Mike Reed29859872018-01-08 08:25:27 -0500834 SkAutoCanvasRestore restore(canvas, true);
835 const SkRect srcR = SkRect::MakeSize(this->size());
836 if (dstR) {
837 canvas->concat(SkMatrix::MakeRectToRect(srcR, *dstR, SkMatrix::kCenter_ScaleToFit));
838 }
839 canvas->clipRect(srcR);
Florin Malita094ccde2017-12-30 12:27:00 -0500840 fDom->render(canvas);
841
842 if (!fShowInval)
843 return;
844
845 SkPaint fill, stroke;
846 fill.setAntiAlias(true);
847 fill.setColor(0x40ff0000);
848 stroke.setAntiAlias(true);
849 stroke.setColor(0xffff0000);
850 stroke.setStyle(SkPaint::kStroke_Style);
851
852 for (const auto& r : ic) {
853 canvas->drawRect(r, fill);
854 canvas->drawRect(r, stroke);
855 }
856}
857
858void Animation::animationTick(SkMSec ms) {
859 // 't' in the BM model really means 'frame #'
860 auto t = static_cast<float>(ms) * fFrameRate / 1000;
861
862 t = fInPoint + std::fmod(t, fOutPoint - fInPoint);
863
864 // TODO: this can be optimized quite a bit with some sorting/state tracking.
865 for (const auto& a : fAnimators) {
866 a->tick(t);
867 }
868}
869
870} // namespace skotty