blob: 9a14ac141e75da9e806f0ef6c302405d841bd9df [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 Malitac0034172018-01-08 16:42:59 -050027#include "SkSGOpacityEffect.h"
Florin Malita094ccde2017-12-30 12:27:00 -050028#include "SkSGPath.h"
Florin Malita2e1d7e22018-01-02 10:40:00 -050029#include "SkSGRect.h"
Florin Malita094ccde2017-12-30 12:27:00 -050030#include "SkSGTransform.h"
Florin Malita51b8c892018-01-07 08:54:24 -050031#include "SkSGTrimEffect.h"
Florin Malita094ccde2017-12-30 12:27:00 -050032#include "SkStream.h"
33#include "SkTArray.h"
34#include "SkTHash.h"
35
36#include <cmath>
Florin Malita18eafd92018-01-04 21:11:55 -050037#include <unordered_map>
Florin Malitae6345d92018-01-03 23:37:54 -050038#include <vector>
39
Florin Malita094ccde2017-12-30 12:27:00 -050040#include "stdlib.h"
41
42namespace skotty {
43
44namespace {
45
46using AssetMap = SkTHashMap<SkString, const Json::Value*>;
47
48struct AttachContext {
Florin Malita49328072018-01-08 12:51:12 -050049 const ResourceProvider& fResources;
Florin Malita094ccde2017-12-30 12:27:00 -050050 const AssetMap& fAssets;
51 SkTArray<std::unique_ptr<AnimatorBase>>& fAnimators;
52};
53
54bool LogFail(const Json::Value& json, const char* msg) {
55 const auto dump = json.toStyledString();
56 LOG("!! %s: %s", msg, dump.c_str());
57 return false;
58}
59
60// This is the workhorse for binding properties: depending on whether the property is animated,
61// it will either apply immediately or instantiate and attach a keyframe animator.
Florin Malita9661b982018-01-06 14:25:49 -050062template <typename ValueT, typename AttrT, typename NodeT>
Florin Malita094ccde2017-12-30 12:27:00 -050063bool AttachProperty(const Json::Value& jprop, AttachContext* ctx, const sk_sp<NodeT>& node,
Florin Malita9661b982018-01-06 14:25:49 -050064 typename Animator<ValueT, AttrT, NodeT>::ApplyFuncT&& apply) {
Florin Malita094ccde2017-12-30 12:27:00 -050065 if (!jprop.isObject())
66 return false;
67
Florin Malita95448a92018-01-08 10:15:12 -050068 const auto& jpropA = jprop["a"];
69 const auto& jpropK = jprop["k"];
70
71 // Older Json versions don't have an "a" animation marker.
72 // For those, we attempt to parse both ways.
73 if (jpropA.isNull() || !ParseBool(jpropA, "false")) {
Florin Malita094ccde2017-12-30 12:27:00 -050074 ValueT val;
Florin Malita9e1c58c2018-01-08 19:23:08 -050075 if (ValueTraits<ValueT>::Parse(jpropK, &val)) {
Florin Malita95448a92018-01-08 10:15:12 -050076 // Static property.
Florin Malita9e1c58c2018-01-08 19:23:08 -050077 apply(node, ValueTraits<ValueT>::template As<AttrT>(val));
Florin Malita95448a92018-01-08 10:15:12 -050078 return true;
Florin Malita094ccde2017-12-30 12:27:00 -050079 }
80
Florin Malita95448a92018-01-08 10:15:12 -050081 if (!jpropA.isNull()) {
82 return LogFail(jprop, "Could not parse (explicit) static property");
Florin Malita094ccde2017-12-30 12:27:00 -050083 }
Florin Malita094ccde2017-12-30 12:27:00 -050084 }
85
Florin Malita95448a92018-01-08 10:15:12 -050086 // Keyframe property.
87 using AnimatorT = Animator<ValueT, AttrT, NodeT>;
88 auto animator = AnimatorT::Make(jpropK, node, std::move(apply));
89
90 if (!animator) {
91 return LogFail(jprop, "Could not parse keyframed property");
92 }
93
94 ctx->fAnimators.push_back(std::move(animator));
95
Florin Malita094ccde2017-12-30 12:27:00 -050096 return true;
97}
98
Florin Malita18eafd92018-01-04 21:11:55 -050099sk_sp<sksg::Matrix> AttachMatrix(const Json::Value& t, AttachContext* ctx,
100 sk_sp<sksg::Matrix> parentMatrix) {
101 if (!t.isObject())
102 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500103
Florin Malita18eafd92018-01-04 21:11:55 -0500104 auto matrix = sksg::Matrix::Make(SkMatrix::I(), std::move(parentMatrix));
105 auto composite = sk_make_sp<CompositeTransform>(matrix);
106 auto anchor_attached = AttachProperty<VectorValue, SkPoint>(t["a"], ctx, composite,
Florin Malita094ccde2017-12-30 12:27:00 -0500107 [](const sk_sp<CompositeTransform>& node, const SkPoint& a) {
108 node->setAnchorPoint(a);
109 });
Florin Malita18eafd92018-01-04 21:11:55 -0500110 auto position_attached = AttachProperty<VectorValue, SkPoint>(t["p"], ctx, composite,
Florin Malita094ccde2017-12-30 12:27:00 -0500111 [](const sk_sp<CompositeTransform>& node, const SkPoint& p) {
112 node->setPosition(p);
113 });
Florin Malita18eafd92018-01-04 21:11:55 -0500114 auto scale_attached = AttachProperty<VectorValue, SkVector>(t["s"], ctx, composite,
Florin Malita094ccde2017-12-30 12:27:00 -0500115 [](const sk_sp<CompositeTransform>& node, const SkVector& s) {
116 node->setScale(s);
117 });
Florin Malita18eafd92018-01-04 21:11:55 -0500118 auto rotation_attached = AttachProperty<ScalarValue, SkScalar>(t["r"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500119 [](const sk_sp<CompositeTransform>& node, const SkScalar& r) {
Florin Malita094ccde2017-12-30 12:27:00 -0500120 node->setRotation(r);
121 });
Florin Malita18eafd92018-01-04 21:11:55 -0500122 auto skew_attached = AttachProperty<ScalarValue, SkScalar>(t["sk"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500123 [](const sk_sp<CompositeTransform>& node, const SkScalar& sk) {
Florin Malita094ccde2017-12-30 12:27:00 -0500124 node->setSkew(sk);
125 });
Florin Malita18eafd92018-01-04 21:11:55 -0500126 auto skewaxis_attached = AttachProperty<ScalarValue, SkScalar>(t["sa"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500127 [](const sk_sp<CompositeTransform>& node, const SkScalar& sa) {
Florin Malita094ccde2017-12-30 12:27:00 -0500128 node->setSkewAxis(sa);
129 });
130
131 if (!anchor_attached &&
132 !position_attached &&
133 !scale_attached &&
134 !rotation_attached &&
135 !skew_attached &&
136 !skewaxis_attached) {
137 LogFail(t, "Could not parse transform");
Florin Malita18eafd92018-01-04 21:11:55 -0500138 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500139 }
140
Florin Malita18eafd92018-01-04 21:11:55 -0500141 return matrix;
Florin Malita094ccde2017-12-30 12:27:00 -0500142}
143
Florin Malitac0034172018-01-08 16:42:59 -0500144sk_sp<sksg::RenderNode> AttachOpacity(const Json::Value& jtransform, AttachContext* ctx,
145 sk_sp<sksg::RenderNode> childNode) {
146 if (!jtransform.isObject() || !childNode)
147 return childNode;
148
149 // This is more peeky than other attachers, because we want to avoid redundant opacity
150 // nodes for the extremely common case of static opaciy == 100.
151 const auto& opacity = jtransform["o"];
152 if (opacity.isObject() &&
153 !ParseBool(opacity["a"], true) &&
154 ParseScalar(opacity["k"], -1) == 100) {
155 // Ignoring static full opacity.
156 return childNode;
157 }
158
159 auto opacityNode = sksg::OpacityEffect::Make(childNode);
160 AttachProperty<ScalarValue, SkScalar>(opacity, ctx, opacityNode,
161 [](const sk_sp<sksg::OpacityEffect>& node, const SkScalar& o) {
162 // BM opacity is [0..100]
163 node->setOpacity(o * 0.01f);
164 });
165
166 return opacityNode;
167}
168
Florin Malita094ccde2017-12-30 12:27:00 -0500169sk_sp<sksg::RenderNode> AttachShape(const Json::Value&, AttachContext* ctx);
170sk_sp<sksg::RenderNode> AttachComposition(const Json::Value&, AttachContext* ctx);
171
172sk_sp<sksg::RenderNode> AttachShapeGroup(const Json::Value& jgroup, AttachContext* ctx) {
173 SkASSERT(jgroup.isObject());
174
175 return AttachShape(jgroup["it"], ctx);
176}
177
178sk_sp<sksg::GeometryNode> AttachPathGeometry(const Json::Value& jpath, AttachContext* ctx) {
179 SkASSERT(jpath.isObject());
180
181 auto path_node = sksg::Path::Make();
182 auto path_attached = AttachProperty<ShapeValue, SkPath>(jpath["ks"], ctx, path_node,
183 [](const sk_sp<sksg::Path>& node, const SkPath& p) { node->setPath(p); });
184
185 if (path_attached)
186 LOG("** Attached path geometry - verbs: %d\n", path_node->getPath().countVerbs());
187
188 return path_attached ? path_node : nullptr;
189}
190
Florin Malita2e1d7e22018-01-02 10:40:00 -0500191sk_sp<sksg::GeometryNode> AttachRRectGeometry(const Json::Value& jrect, AttachContext* ctx) {
192 SkASSERT(jrect.isObject());
193
194 auto rect_node = sksg::RRect::Make();
195 auto composite = sk_make_sp<CompositeRRect>(rect_node);
196
197 auto p_attached = AttachProperty<VectorValue, SkPoint>(jrect["p"], ctx, composite,
198 [](const sk_sp<CompositeRRect>& node, const SkPoint& pos) { node->setPosition(pos); });
199 auto s_attached = AttachProperty<VectorValue, SkSize>(jrect["s"], ctx, composite,
200 [](const sk_sp<CompositeRRect>& node, const SkSize& sz) { node->setSize(sz); });
201 auto r_attached = AttachProperty<ScalarValue, SkScalar>(jrect["r"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500202 [](const sk_sp<CompositeRRect>& node, const SkScalar& radius) {
Florin Malitafbc13f12018-01-04 10:26:35 -0500203 node->setRadius(SkSize::Make(radius, radius));
204 });
Florin Malita2e1d7e22018-01-02 10:40:00 -0500205
206 if (!p_attached && !s_attached && !r_attached) {
207 return nullptr;
208 }
209
Florin Malitafbc13f12018-01-04 10:26:35 -0500210 LOG("** Attached (r)rect geometry\n");
211
212 return rect_node;
213}
214
215sk_sp<sksg::GeometryNode> AttachEllipseGeometry(const Json::Value& jellipse, AttachContext* ctx) {
216 SkASSERT(jellipse.isObject());
217
218 auto rect_node = sksg::RRect::Make();
219 auto composite = sk_make_sp<CompositeRRect>(rect_node);
220
221 auto p_attached = AttachProperty<VectorValue, SkPoint>(jellipse["p"], ctx, composite,
222 [](const sk_sp<CompositeRRect>& node, const SkPoint& pos) { node->setPosition(pos); });
223 auto s_attached = AttachProperty<VectorValue, SkSize>(jellipse["s"], ctx, composite,
224 [](const sk_sp<CompositeRRect>& node, const SkSize& sz) {
225 node->setSize(sz);
226 node->setRadius(SkSize::Make(sz.width() / 2, sz.height() / 2));
227 });
228
229 if (!p_attached && !s_attached) {
230 return nullptr;
231 }
232
233 LOG("** Attached ellipse geometry\n");
234
Florin Malita2e1d7e22018-01-02 10:40:00 -0500235 return rect_node;
236}
237
Florin Malita02a32b02018-01-04 11:27:09 -0500238sk_sp<sksg::GeometryNode> AttachPolystarGeometry(const Json::Value& jstar, AttachContext* ctx) {
239 SkASSERT(jstar.isObject());
240
241 static constexpr CompositePolyStar::Type gTypes[] = {
242 CompositePolyStar::Type::kStar, // "sy": 1
243 CompositePolyStar::Type::kPoly, // "sy": 2
244 };
245
246 const auto type = ParseInt(jstar["sy"], 0) - 1;
247 if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gTypes))) {
248 LogFail(jstar, "Unknown polystar type");
249 return nullptr;
250 }
251
252 auto path_node = sksg::Path::Make();
253 auto composite = sk_make_sp<CompositePolyStar>(path_node, gTypes[type]);
254
255 AttachProperty<VectorValue, SkPoint>(jstar["p"], ctx, composite,
256 [](const sk_sp<CompositePolyStar>& node, const SkPoint& p) { node->setPosition(p); });
257 AttachProperty<ScalarValue, SkScalar>(jstar["pt"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500258 [](const sk_sp<CompositePolyStar>& node, const SkScalar& pt) { node->setPointCount(pt); });
Florin Malita02a32b02018-01-04 11:27:09 -0500259 AttachProperty<ScalarValue, SkScalar>(jstar["ir"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500260 [](const sk_sp<CompositePolyStar>& node, const SkScalar& ir) { node->setInnerRadius(ir); });
Florin Malita02a32b02018-01-04 11:27:09 -0500261 AttachProperty<ScalarValue, SkScalar>(jstar["or"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500262 [](const sk_sp<CompositePolyStar>& node, const SkScalar& otr) {
263 node->setOuterRadius(otr);
264 });
Florin Malita02a32b02018-01-04 11:27:09 -0500265 AttachProperty<ScalarValue, SkScalar>(jstar["is"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500266 [](const sk_sp<CompositePolyStar>& node, const SkScalar& is) {
267 node->setInnerRoundness(is);
268 });
Florin Malita02a32b02018-01-04 11:27:09 -0500269 AttachProperty<ScalarValue, SkScalar>(jstar["os"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500270 [](const sk_sp<CompositePolyStar>& node, const SkScalar& os) {
271 node->setOuterRoundness(os);
272 });
Florin Malita02a32b02018-01-04 11:27:09 -0500273 AttachProperty<ScalarValue, SkScalar>(jstar["r"], ctx, composite,
Florin Malita9661b982018-01-06 14:25:49 -0500274 [](const sk_sp<CompositePolyStar>& node, const SkScalar& r) { node->setRotation(r); });
Florin Malita02a32b02018-01-04 11:27:09 -0500275
276 return path_node;
277}
278
Florin Malita094ccde2017-12-30 12:27:00 -0500279sk_sp<sksg::Color> AttachColorPaint(const Json::Value& obj, AttachContext* ctx) {
280 SkASSERT(obj.isObject());
281
282 auto color_node = sksg::Color::Make(SK_ColorBLACK);
283 color_node->setAntiAlias(true);
284
285 auto color_attached = AttachProperty<VectorValue, SkColor>(obj["c"], ctx, color_node,
Florin Malita9661b982018-01-06 14:25:49 -0500286 [](const sk_sp<sksg::Color>& node, const SkColor& c) { node->setColor(c); });
Florin Malita094ccde2017-12-30 12:27:00 -0500287
288 return color_attached ? color_node : nullptr;
289}
290
291sk_sp<sksg::PaintNode> AttachFillPaint(const Json::Value& jfill, AttachContext* ctx) {
292 SkASSERT(jfill.isObject());
293
294 auto color = AttachColorPaint(jfill, ctx);
295 if (color) {
296 LOG("** Attached color fill: 0x%x\n", color->getColor());
297 }
298 return color;
299}
300
301sk_sp<sksg::PaintNode> AttachStrokePaint(const Json::Value& jstroke, AttachContext* ctx) {
302 SkASSERT(jstroke.isObject());
303
304 auto stroke_node = AttachColorPaint(jstroke, ctx);
305 if (!stroke_node)
306 return nullptr;
307
308 LOG("** Attached color stroke: 0x%x\n", stroke_node->getColor());
309
310 stroke_node->setStyle(SkPaint::kStroke_Style);
311
312 auto width_attached = AttachProperty<ScalarValue, SkScalar>(jstroke["w"], ctx, stroke_node,
Florin Malita9661b982018-01-06 14:25:49 -0500313 [](const sk_sp<sksg::Color>& node, const SkScalar& width) { node->setStrokeWidth(width); });
Florin Malita094ccde2017-12-30 12:27:00 -0500314 if (!width_attached)
315 return nullptr;
316
317 stroke_node->setStrokeMiter(ParseScalar(jstroke["ml"], 4));
318
319 static constexpr SkPaint::Join gJoins[] = {
320 SkPaint::kMiter_Join,
321 SkPaint::kRound_Join,
322 SkPaint::kBevel_Join,
323 };
324 stroke_node->setStrokeJoin(gJoins[SkTPin<int>(ParseInt(jstroke["lj"], 1) - 1,
325 0, SK_ARRAY_COUNT(gJoins) - 1)]);
326
327 static constexpr SkPaint::Cap gCaps[] = {
328 SkPaint::kButt_Cap,
329 SkPaint::kRound_Cap,
330 SkPaint::kSquare_Cap,
331 };
332 stroke_node->setStrokeCap(gCaps[SkTPin<int>(ParseInt(jstroke["lc"], 1) - 1,
333 0, SK_ARRAY_COUNT(gCaps) - 1)]);
334
335 return stroke_node;
336}
337
Florin Malitae6345d92018-01-03 23:37:54 -0500338std::vector<sk_sp<sksg::GeometryNode>> AttachMergeGeometryEffect(
339 const Json::Value& jmerge, AttachContext* ctx, std::vector<sk_sp<sksg::GeometryNode>>&& geos) {
340 std::vector<sk_sp<sksg::GeometryNode>> merged;
341
342 static constexpr sksg::Merge::Mode gModes[] = {
343 sksg::Merge::Mode::kMerge, // "mm": 1
344 sksg::Merge::Mode::kUnion, // "mm": 2
345 sksg::Merge::Mode::kDifference, // "mm": 3
346 sksg::Merge::Mode::kIntersect, // "mm": 4
347 sksg::Merge::Mode::kXOR , // "mm": 5
348 };
349
Florin Malita51b8c892018-01-07 08:54:24 -0500350 const auto mode = gModes[SkTPin<int>(ParseInt(jmerge["mm"], 1) - 1,
351 0, SK_ARRAY_COUNT(gModes) - 1)];
Florin Malitae6345d92018-01-03 23:37:54 -0500352 merged.push_back(sksg::Merge::Make(std::move(geos), mode));
353
354 LOG("** Attached merge path effect, mode: %d\n", mode);
355
356 return merged;
357}
358
Florin Malita51b8c892018-01-07 08:54:24 -0500359std::vector<sk_sp<sksg::GeometryNode>> AttachTrimGeometryEffect(
360 const Json::Value& jtrim, AttachContext* ctx, std::vector<sk_sp<sksg::GeometryNode>>&& geos) {
361
362 enum class Mode {
363 kMerged, // "m": 1
364 kSeparate, // "m": 2
365 } gModes[] = { Mode::kMerged, Mode::kSeparate };
366
367 const auto mode = gModes[SkTPin<int>(ParseInt(jtrim["m"], 1) - 1,
368 0, SK_ARRAY_COUNT(gModes) - 1)];
369
370 std::vector<sk_sp<sksg::GeometryNode>> inputs;
371 if (mode == Mode::kMerged) {
372 inputs.push_back(sksg::Merge::Make(std::move(geos), sksg::Merge::Mode::kMerge));
373 } else {
374 inputs = std::move(geos);
375 }
376
377 std::vector<sk_sp<sksg::GeometryNode>> trimmed;
378 trimmed.reserve(inputs.size());
379 for (const auto& i : inputs) {
380 const auto trim = sksg::TrimEffect::Make(i);
381 trimmed.push_back(trim);
382 AttachProperty<ScalarValue, SkScalar>(jtrim["s"], ctx, trim,
383 [](const sk_sp<sksg::TrimEffect>& node, const SkScalar& s) {
384 node->setStart(s * 0.01f);
385 });
386 AttachProperty<ScalarValue, SkScalar>(jtrim["e"], ctx, trim,
387 [](const sk_sp<sksg::TrimEffect>& node, const SkScalar& e) {
388 node->setEnd(e * 0.01f);
389 });
390 // TODO: "offset" doesn't currently work the same as BM - figure out what's going on.
391 AttachProperty<ScalarValue, SkScalar>(jtrim["o"], ctx, trim,
392 [](const sk_sp<sksg::TrimEffect>& node, const SkScalar& o) {
393 node->setOffset(o * 0.01f);
394 });
395 }
396
397 return trimmed;
398}
399
Florin Malita094ccde2017-12-30 12:27:00 -0500400using GeometryAttacherT = sk_sp<sksg::GeometryNode> (*)(const Json::Value&, AttachContext*);
401static constexpr GeometryAttacherT gGeometryAttachers[] = {
402 AttachPathGeometry,
Florin Malita2e1d7e22018-01-02 10:40:00 -0500403 AttachRRectGeometry,
Florin Malitafbc13f12018-01-04 10:26:35 -0500404 AttachEllipseGeometry,
Florin Malita02a32b02018-01-04 11:27:09 -0500405 AttachPolystarGeometry,
Florin Malita094ccde2017-12-30 12:27:00 -0500406};
407
408using PaintAttacherT = sk_sp<sksg::PaintNode> (*)(const Json::Value&, AttachContext*);
409static constexpr PaintAttacherT gPaintAttachers[] = {
410 AttachFillPaint,
411 AttachStrokePaint,
412};
413
414using GroupAttacherT = sk_sp<sksg::RenderNode> (*)(const Json::Value&, AttachContext*);
415static constexpr GroupAttacherT gGroupAttachers[] = {
416 AttachShapeGroup,
417};
418
Florin Malitae6345d92018-01-03 23:37:54 -0500419using GeometryEffectAttacherT =
420 std::vector<sk_sp<sksg::GeometryNode>> (*)(const Json::Value&,
421 AttachContext*,
422 std::vector<sk_sp<sksg::GeometryNode>>&&);
423static constexpr GeometryEffectAttacherT gGeometryEffectAttachers[] = {
424 AttachMergeGeometryEffect,
Florin Malita51b8c892018-01-07 08:54:24 -0500425 AttachTrimGeometryEffect,
Florin Malitae6345d92018-01-03 23:37:54 -0500426};
427
Florin Malita094ccde2017-12-30 12:27:00 -0500428enum class ShapeType {
429 kGeometry,
Florin Malitae6345d92018-01-03 23:37:54 -0500430 kGeometryEffect,
Florin Malita094ccde2017-12-30 12:27:00 -0500431 kPaint,
432 kGroup,
Florin Malitadacc02b2017-12-31 09:12:31 -0500433 kTransform,
Florin Malita094ccde2017-12-30 12:27:00 -0500434};
435
436struct ShapeInfo {
437 const char* fTypeString;
438 ShapeType fShapeType;
439 uint32_t fAttacherIndex; // index into respective attacher tables
440};
441
442const ShapeInfo* FindShapeInfo(const Json::Value& shape) {
443 static constexpr ShapeInfo gShapeInfo[] = {
Florin Malitafbc13f12018-01-04 10:26:35 -0500444 { "el", ShapeType::kGeometry , 2 }, // ellipse -> AttachEllipseGeometry
Florin Malitae6345d92018-01-03 23:37:54 -0500445 { "fl", ShapeType::kPaint , 0 }, // fill -> AttachFillPaint
446 { "gr", ShapeType::kGroup , 0 }, // group -> AttachShapeGroup
447 { "mm", ShapeType::kGeometryEffect, 0 }, // merge -> AttachMergeGeometryEffect
Florin Malita02a32b02018-01-04 11:27:09 -0500448 { "rc", ShapeType::kGeometry , 1 }, // rrect -> AttachRRectGeometry
Florin Malitae6345d92018-01-03 23:37:54 -0500449 { "sh", ShapeType::kGeometry , 0 }, // shape -> AttachPathGeometry
Florin Malita02a32b02018-01-04 11:27:09 -0500450 { "sr", ShapeType::kGeometry , 3 }, // polystar -> AttachPolyStarGeometry
Florin Malitae6345d92018-01-03 23:37:54 -0500451 { "st", ShapeType::kPaint , 1 }, // stroke -> AttachStrokePaint
Florin Malita51b8c892018-01-07 08:54:24 -0500452 { "tm", ShapeType::kGeometryEffect, 1 }, // trim -> AttachTrimGeometryEffect
Florin Malita18eafd92018-01-04 21:11:55 -0500453 { "tr", ShapeType::kTransform , 0 }, // transform -> In-place handler
Florin Malita094ccde2017-12-30 12:27:00 -0500454 };
455
456 if (!shape.isObject())
457 return nullptr;
458
459 const auto& type = shape["ty"];
460 if (!type.isString())
461 return nullptr;
462
463 const auto* info = bsearch(type.asCString(),
464 gShapeInfo,
465 SK_ARRAY_COUNT(gShapeInfo),
466 sizeof(ShapeInfo),
467 [](const void* key, const void* info) {
468 return strcmp(static_cast<const char*>(key),
469 static_cast<const ShapeInfo*>(info)->fTypeString);
470 });
471
472 return static_cast<const ShapeInfo*>(info);
473}
474
475sk_sp<sksg::RenderNode> AttachShape(const Json::Value& shapeArray, AttachContext* ctx) {
476 if (!shapeArray.isArray())
477 return nullptr;
478
Florin Malita2a8275b2018-01-02 12:52:43 -0500479 // (https://helpx.adobe.com/after-effects/using/overview-shape-layers-paths-vector.html#groups_and_render_order_for_shapes_and_shape_attributes)
480 //
481 // Render order for shapes within a shape layer
482 //
483 // The rules for rendering a shape layer are similar to the rules for rendering a composition
484 // that contains nested compositions:
485 //
486 // * Within a group, the shape at the bottom of the Timeline panel stacking order is rendered
487 // first.
488 //
489 // * All path operations within a group are performed before paint operations. This means,
490 // for example, that the stroke follows the distortions in the path made by the Wiggle Paths
491 // path operation. Path operations within a group are performed from top to bottom.
492 //
493 // * Paint operations within a group are performed from the bottom to the top in the Timeline
494 // panel stacking order. This means, for example, that a stroke is rendered on top of
495 // (in front of) a stroke that appears after it in the Timeline panel.
496 //
Florin Malitadacc02b2017-12-31 09:12:31 -0500497 sk_sp<sksg::Group> shape_group = sksg::Group::Make();
498 sk_sp<sksg::RenderNode> xformed_group = shape_group;
Florin Malita094ccde2017-12-30 12:27:00 -0500499
Florin Malitae6345d92018-01-03 23:37:54 -0500500 std::vector<sk_sp<sksg::GeometryNode>> geos;
501 std::vector<sk_sp<sksg::RenderNode>> draws;
Florin Malita094ccde2017-12-30 12:27:00 -0500502
503 for (const auto& s : shapeArray) {
504 const auto* info = FindShapeInfo(s);
505 if (!info) {
506 LogFail(s.isObject() ? s["ty"] : s, "Unknown shape");
507 continue;
508 }
509
510 switch (info->fShapeType) {
511 case ShapeType::kGeometry: {
512 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGeometryAttachers));
513 if (auto geo = gGeometryAttachers[info->fAttacherIndex](s, ctx)) {
514 geos.push_back(std::move(geo));
515 }
516 } break;
Florin Malitae6345d92018-01-03 23:37:54 -0500517 case ShapeType::kGeometryEffect: {
518 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGeometryEffectAttachers));
519 geos = gGeometryEffectAttachers[info->fAttacherIndex](s, ctx, std::move(geos));
520 } break;
Florin Malita094ccde2017-12-30 12:27:00 -0500521 case ShapeType::kPaint: {
522 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gPaintAttachers));
523 if (auto paint = gPaintAttachers[info->fAttacherIndex](s, ctx)) {
Florin Malita2a8275b2018-01-02 12:52:43 -0500524 for (const auto& geo : geos) {
525 draws.push_back(sksg::Draw::Make(geo, paint));
526 }
Florin Malita094ccde2017-12-30 12:27:00 -0500527 }
528 } break;
529 case ShapeType::kGroup: {
530 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGroupAttachers));
531 if (auto group = gGroupAttachers[info->fAttacherIndex](s, ctx)) {
Florin Malita2a8275b2018-01-02 12:52:43 -0500532 draws.push_back(std::move(group));
Florin Malita094ccde2017-12-30 12:27:00 -0500533 }
534 } break;
Florin Malitadacc02b2017-12-31 09:12:31 -0500535 case ShapeType::kTransform: {
Florin Malita2a8275b2018-01-02 12:52:43 -0500536 // TODO: BM appears to transform the geometry, not the draw op itself.
Florin Malita18eafd92018-01-04 21:11:55 -0500537 if (auto matrix = AttachMatrix(s, ctx, nullptr)) {
538 xformed_group = sksg::Transform::Make(std::move(xformed_group),
539 std::move(matrix));
540 }
Florin Malitac0034172018-01-08 16:42:59 -0500541 xformed_group = AttachOpacity(s, ctx, std::move(xformed_group));
Florin Malitadacc02b2017-12-31 09:12:31 -0500542 } break;
Florin Malita094ccde2017-12-30 12:27:00 -0500543 }
544 }
545
Florin Malita2a8275b2018-01-02 12:52:43 -0500546 if (draws.empty()) {
547 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500548 }
549
Florin Malitae6345d92018-01-03 23:37:54 -0500550 for (auto draw = draws.rbegin(); draw != draws.rend(); ++draw) {
551 shape_group->addChild(std::move(*draw));
Florin Malita2a8275b2018-01-02 12:52:43 -0500552 }
553
Florin Malitae6345d92018-01-03 23:37:54 -0500554 LOG("** Attached shape: %zd draws.\n", draws.size());
Florin Malitadacc02b2017-12-31 09:12:31 -0500555 return xformed_group;
Florin Malita094ccde2017-12-30 12:27:00 -0500556}
557
558sk_sp<sksg::RenderNode> AttachCompLayer(const Json::Value& layer, AttachContext* ctx) {
559 SkASSERT(layer.isObject());
560
561 auto refId = ParseString(layer["refId"], "");
562 if (refId.isEmpty()) {
563 LOG("!! Comp layer missing refId\n");
564 return nullptr;
565 }
566
567 const auto* comp = ctx->fAssets.find(refId);
568 if (!comp) {
569 LOG("!! Pre-comp not found: '%s'\n", refId.c_str());
570 return nullptr;
571 }
572
573 // TODO: cycle detection
574 return AttachComposition(**comp, ctx);
575}
576
577sk_sp<sksg::RenderNode> AttachSolidLayer(const Json::Value& layer, AttachContext*) {
578 SkASSERT(layer.isObject());
579
580 LOG("?? Solid layer stub\n");
581 return nullptr;
582}
583
Florin Malita49328072018-01-08 12:51:12 -0500584sk_sp<sksg::RenderNode> AttachImageAsset(const Json::Value& jimage, AttachContext* ctx) {
585 SkASSERT(jimage.isObject());
586
587 const auto name = ParseString(jimage["p"], ""),
588 path = ParseString(jimage["u"], "");
589 if (name.isEmpty())
590 return nullptr;
591
592 // TODO: plumb resource paths explicitly to ResourceProvider?
593 const auto resName = path.isEmpty() ? name : SkOSPath::Join(path.c_str(), name.c_str());
594 const auto resStream = ctx->fResources.openStream(resName.c_str());
595 if (!resStream || !resStream->hasLength()) {
596 LOG("!! Could not load image resource: %s\n", resName.c_str());
597 return nullptr;
598 }
599
600 // TODO: non-intrisic image sizing
601 return sksg::Image::Make(
602 SkImage::MakeFromEncoded(SkData::MakeFromStream(resStream.get(), resStream->getLength())));
603}
604
605sk_sp<sksg::RenderNode> AttachImageLayer(const Json::Value& layer, AttachContext* ctx) {
Florin Malita094ccde2017-12-30 12:27:00 -0500606 SkASSERT(layer.isObject());
607
Florin Malita49328072018-01-08 12:51:12 -0500608 auto refId = ParseString(layer["refId"], "");
609 if (refId.isEmpty()) {
610 LOG("!! Image layer missing refId\n");
611 return nullptr;
612 }
613
614 const auto* jimage = ctx->fAssets.find(refId);
615 if (!jimage) {
616 LOG("!! Image asset not found: '%s'\n", refId.c_str());
617 return nullptr;
618 }
619
620 return AttachImageAsset(**jimage, ctx);
Florin Malita094ccde2017-12-30 12:27:00 -0500621}
622
623sk_sp<sksg::RenderNode> AttachNullLayer(const Json::Value& layer, AttachContext*) {
624 SkASSERT(layer.isObject());
625
Florin Malita18eafd92018-01-04 21:11:55 -0500626 // Null layers are used solely to drive dependent transforms,
627 // but we use free-floating sksg::Matrices for that purpose.
Florin Malita094ccde2017-12-30 12:27:00 -0500628 return nullptr;
629}
630
631sk_sp<sksg::RenderNode> AttachShapeLayer(const Json::Value& layer, AttachContext* ctx) {
632 SkASSERT(layer.isObject());
633
634 LOG("** Attaching shape layer ind: %d\n", ParseInt(layer["ind"], 0));
635
636 return AttachShape(layer["shapes"], ctx);
637}
638
639sk_sp<sksg::RenderNode> AttachTextLayer(const Json::Value& layer, AttachContext*) {
640 SkASSERT(layer.isObject());
641
642 LOG("?? Text layer stub\n");
643 return nullptr;
644}
645
Florin Malita18eafd92018-01-04 21:11:55 -0500646struct AttachLayerContext {
647 AttachLayerContext(const Json::Value& jlayers, AttachContext* ctx)
648 : fLayerList(jlayers), fCtx(ctx) {}
649
650 const Json::Value& fLayerList;
651 AttachContext* fCtx;
652 std::unordered_map<const Json::Value*, sk_sp<sksg::Matrix>> fLayerMatrixCache;
653 std::unordered_map<int, const Json::Value*> fLayerIndexCache;
654
655 const Json::Value* findLayer(int index) {
656 SkASSERT(fLayerList.isArray());
657
658 if (index < 0) {
659 return nullptr;
660 }
661
662 const auto cached = fLayerIndexCache.find(index);
663 if (cached != fLayerIndexCache.end()) {
664 return cached->second;
665 }
666
667 for (const auto& l : fLayerList) {
668 if (!l.isObject()) {
669 continue;
670 }
671
672 if (ParseInt(l["ind"], -1) == index) {
673 fLayerIndexCache.insert(std::make_pair(index, &l));
674 return &l;
675 }
676 }
677
678 return nullptr;
679 }
680
681 sk_sp<sksg::Matrix> AttachLayerMatrix(const Json::Value& jlayer) {
682 SkASSERT(jlayer.isObject());
683
684 const auto cached = fLayerMatrixCache.find(&jlayer);
685 if (cached != fLayerMatrixCache.end()) {
686 return cached->second;
687 }
688
689 const auto* parentLayer = this->findLayer(ParseInt(jlayer["parent"], -1));
690
691 // TODO: cycle detection?
692 auto parentMatrix = (parentLayer && parentLayer != &jlayer)
693 ? this->AttachLayerMatrix(*parentLayer) : nullptr;
694
695 auto layerMatrix = AttachMatrix(jlayer["ks"], fCtx, std::move(parentMatrix));
696 fLayerMatrixCache.insert(std::make_pair(&jlayer, layerMatrix));
697
698 return layerMatrix;
699 }
700};
701
702sk_sp<sksg::RenderNode> AttachLayer(const Json::Value& jlayer,
703 AttachLayerContext* layerCtx) {
704 if (!jlayer.isObject())
Florin Malita094ccde2017-12-30 12:27:00 -0500705 return nullptr;
706
707 using LayerAttacher = sk_sp<sksg::RenderNode> (*)(const Json::Value&, AttachContext*);
708 static constexpr LayerAttacher gLayerAttachers[] = {
709 AttachCompLayer, // 'ty': 0
710 AttachSolidLayer, // 'ty': 1
711 AttachImageLayer, // 'ty': 2
712 AttachNullLayer, // 'ty': 3
713 AttachShapeLayer, // 'ty': 4
714 AttachTextLayer, // 'ty': 5
715 };
716
Florin Malita18eafd92018-01-04 21:11:55 -0500717 int type = ParseInt(jlayer["ty"], -1);
Florin Malita094ccde2017-12-30 12:27:00 -0500718 if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gLayerAttachers))) {
719 return nullptr;
720 }
721
Florin Malita71cba8f2018-01-09 08:07:14 -0500722 // Layer content.
723 auto layer = gLayerAttachers[type](jlayer, layerCtx->fCtx);
724 if (auto layerMatrix = layerCtx->AttachLayerMatrix(jlayer)) {
725 // Optional layer transform.
726 layer = sksg::Transform::Make(std::move(layer), std::move(layerMatrix));
727 }
728 // Optional layer opacity.
729 layer = AttachOpacity(jlayer["ks"], layerCtx->fCtx, std::move(layer));
Florin Malita18eafd92018-01-04 21:11:55 -0500730
Florin Malita71cba8f2018-01-09 08:07:14 -0500731 // TODO: we should also disable related/inactive animators.
732 class Activator final : public AnimatorBase {
733 public:
734 Activator(sk_sp<sksg::OpacityEffect> controlNode, float in, float out)
735 : fControlNode(std::move(controlNode))
736 , fIn(in)
737 , fOut(out) {}
738
739 void tick(SkMSec t) override {
740 // Keep the layer fully transparent except for its [in..out] lifespan.
741 // (note: opacity == 0 disables rendering, while opacity == 1 is a noop)
742 fControlNode->setOpacity(t >= fIn && t <= fOut ? 1 : 0);
743 }
744
745 private:
746 const sk_sp<sksg::OpacityEffect> fControlNode;
747 const float fIn,
748 fOut;
749 };
750
751 auto layerControl = sksg::OpacityEffect::Make(std::move(layer));
752 const auto in = ParseScalar(jlayer["ip"], 0),
753 out = ParseScalar(jlayer["op"], in);
754
755 if (in >= out || ! layerControl)
756 return nullptr;
757
758 layerCtx->fCtx->fAnimators.push_back(skstd::make_unique<Activator>(layerControl, in, out));
759
760 return layerControl;
Florin Malita094ccde2017-12-30 12:27:00 -0500761}
762
763sk_sp<sksg::RenderNode> AttachComposition(const Json::Value& comp, AttachContext* ctx) {
764 if (!comp.isObject())
765 return nullptr;
766
Florin Malita18eafd92018-01-04 21:11:55 -0500767 const auto& jlayers = comp["layers"];
768 if (!jlayers.isArray())
769 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500770
Florin Malita18eafd92018-01-04 21:11:55 -0500771 SkSTArray<16, sk_sp<sksg::RenderNode>, true> layers;
772 AttachLayerContext layerCtx(jlayers, ctx);
773
774 for (const auto& l : jlayers) {
775 if (auto layer_fragment = AttachLayer(l, &layerCtx)) {
Florin Malita2a8275b2018-01-02 12:52:43 -0500776 layers.push_back(std::move(layer_fragment));
Florin Malita094ccde2017-12-30 12:27:00 -0500777 }
778 }
779
Florin Malita2a8275b2018-01-02 12:52:43 -0500780 if (layers.empty()) {
781 return nullptr;
782 }
783
784 // Layers are painted in bottom->top order.
785 auto comp_group = sksg::Group::Make();
786 for (int i = layers.count() - 1; i >= 0; --i) {
787 comp_group->addChild(std::move(layers[i]));
788 }
789
790 LOG("** Attached composition '%s': %d layers.\n",
791 ParseString(comp["id"], "").c_str(), layers.count());
792
Florin Malita094ccde2017-12-30 12:27:00 -0500793 return comp_group;
794}
795
796} // namespace
797
Florin Malita49328072018-01-08 12:51:12 -0500798std::unique_ptr<Animation> Animation::Make(SkStream* stream, const ResourceProvider& res) {
Florin Malita094ccde2017-12-30 12:27:00 -0500799 if (!stream->hasLength()) {
800 // TODO: handle explicit buffering?
801 LOG("!! cannot parse streaming content\n");
802 return nullptr;
803 }
804
805 Json::Value json;
806 {
807 auto data = SkData::MakeFromStream(stream, stream->getLength());
808 if (!data) {
809 LOG("!! could not read stream\n");
810 return nullptr;
811 }
812
813 Json::Reader reader;
814
815 auto dataStart = static_cast<const char*>(data->data());
816 if (!reader.parse(dataStart, dataStart + data->size(), json, false) || !json.isObject()) {
817 LOG("!! failed to parse json: %s\n", reader.getFormattedErrorMessages().c_str());
818 return nullptr;
819 }
820 }
821
822 const auto version = ParseString(json["v"], "");
823 const auto size = SkSize::Make(ParseScalar(json["w"], -1), ParseScalar(json["h"], -1));
824 const auto fps = ParseScalar(json["fr"], -1);
825
826 if (size.isEmpty() || version.isEmpty() || fps < 0) {
827 LOG("!! invalid animation params (version: %s, size: [%f %f], frame rate: %f)",
828 version.c_str(), size.width(), size.height(), fps);
829 return nullptr;
830 }
831
Florin Malita49328072018-01-08 12:51:12 -0500832 return std::unique_ptr<Animation>(new Animation(res, std::move(version), size, fps, json));
Florin Malita094ccde2017-12-30 12:27:00 -0500833}
834
Florin Malita49328072018-01-08 12:51:12 -0500835std::unique_ptr<Animation> Animation::MakeFromFile(const char path[], const ResourceProvider* res) {
836 class DirectoryResourceProvider final : public ResourceProvider {
837 public:
838 explicit DirectoryResourceProvider(SkString dir) : fDir(std::move(dir)) {}
839
840 std::unique_ptr<SkStream> openStream(const char resource[]) const override {
841 const auto resPath = SkOSPath::Join(fDir.c_str(), resource);
842 return SkStream::MakeFromFile(resPath.c_str());
843 }
844
845 private:
846 const SkString fDir;
847 };
848
849 const auto jsonStream = SkStream::MakeFromFile(path);
850 if (!jsonStream)
851 return nullptr;
852
853 std::unique_ptr<ResourceProvider> defaultProvider;
854 if (!res) {
855 defaultProvider = skstd::make_unique<DirectoryResourceProvider>(SkOSPath::Dirname(path));
856 }
857
858 return Make(jsonStream.get(), res ? *res : *defaultProvider);
859}
860
861Animation::Animation(const ResourceProvider& resources,
862 SkString version, const SkSize& size, SkScalar fps, const Json::Value& json)
Florin Malita094ccde2017-12-30 12:27:00 -0500863 : fVersion(std::move(version))
864 , fSize(size)
865 , fFrameRate(fps)
866 , fInPoint(ParseScalar(json["ip"], 0))
867 , fOutPoint(SkTMax(ParseScalar(json["op"], SK_ScalarMax), fInPoint)) {
868
869 AssetMap assets;
870 for (const auto& asset : json["assets"]) {
871 if (!asset.isObject()) {
872 continue;
873 }
874
875 assets.set(ParseString(asset["id"], ""), &asset);
876 }
877
Florin Malita49328072018-01-08 12:51:12 -0500878 AttachContext ctx = { resources, assets, fAnimators };
Florin Malita094ccde2017-12-30 12:27:00 -0500879 fDom = AttachComposition(json, &ctx);
880
881 LOG("** Attached %d animators\n", fAnimators.count());
882}
883
884Animation::~Animation() = default;
885
Mike Reed29859872018-01-08 08:25:27 -0500886void Animation::render(SkCanvas* canvas, const SkRect* dstR) const {
Florin Malita094ccde2017-12-30 12:27:00 -0500887 if (!fDom)
888 return;
889
890 sksg::InvalidationController ic;
891 fDom->revalidate(&ic, SkMatrix::I());
892
893 // TODO: proper inval
Mike Reed29859872018-01-08 08:25:27 -0500894 SkAutoCanvasRestore restore(canvas, true);
895 const SkRect srcR = SkRect::MakeSize(this->size());
896 if (dstR) {
897 canvas->concat(SkMatrix::MakeRectToRect(srcR, *dstR, SkMatrix::kCenter_ScaleToFit));
898 }
899 canvas->clipRect(srcR);
Florin Malita094ccde2017-12-30 12:27:00 -0500900 fDom->render(canvas);
901
902 if (!fShowInval)
903 return;
904
905 SkPaint fill, stroke;
906 fill.setAntiAlias(true);
907 fill.setColor(0x40ff0000);
908 stroke.setAntiAlias(true);
909 stroke.setColor(0xffff0000);
910 stroke.setStyle(SkPaint::kStroke_Style);
911
912 for (const auto& r : ic) {
913 canvas->drawRect(r, fill);
914 canvas->drawRect(r, stroke);
915 }
916}
917
918void Animation::animationTick(SkMSec ms) {
919 // 't' in the BM model really means 'frame #'
920 auto t = static_cast<float>(ms) * fFrameRate / 1000;
921
922 t = fInPoint + std::fmod(t, fOutPoint - fInPoint);
923
924 // TODO: this can be optimized quite a bit with some sorting/state tracking.
925 for (const auto& a : fAnimators) {
926 a->tick(t);
927 }
928}
929
930} // namespace skotty