blob: 284b37704a802e7e6707a466f69e0fb1865ccb09 [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"
Florin Malita0e66fba2018-01-09 17:10:18 -050019#include "SkParse.h"
Florin Malita094ccde2017-12-30 12:27:00 -050020#include "SkPath.h"
21#include "SkPoint.h"
22#include "SkSGColor.h"
23#include "SkSGDraw.h"
Florin Malita094ccde2017-12-30 12:27:00 -050024#include "SkSGGroup.h"
Florin Malita49328072018-01-08 12:51:12 -050025#include "SkSGImage.h"
26#include "SkSGInvalidationController.h"
Florin Malita5f9102f2018-01-10 13:36:22 -050027#include "SkSGMaskEffect.h"
Florin Malitae6345d92018-01-03 23:37:54 -050028#include "SkSGMerge.h"
Florin Malitac0034172018-01-08 16:42:59 -050029#include "SkSGOpacityEffect.h"
Florin Malita094ccde2017-12-30 12:27:00 -050030#include "SkSGPath.h"
Florin Malita2e1d7e22018-01-02 10:40:00 -050031#include "SkSGRect.h"
Florin Malita094ccde2017-12-30 12:27:00 -050032#include "SkSGTransform.h"
Florin Malita51b8c892018-01-07 08:54:24 -050033#include "SkSGTrimEffect.h"
Florin Malita094ccde2017-12-30 12:27:00 -050034#include "SkStream.h"
35#include "SkTArray.h"
36#include "SkTHash.h"
37
38#include <cmath>
Florin Malita18eafd92018-01-04 21:11:55 -050039#include <unordered_map>
Florin Malitae6345d92018-01-03 23:37:54 -050040#include <vector>
41
Florin Malita094ccde2017-12-30 12:27:00 -050042#include "stdlib.h"
43
44namespace skotty {
45
46namespace {
47
48using AssetMap = SkTHashMap<SkString, const Json::Value*>;
49
50struct AttachContext {
Florin Malita49328072018-01-08 12:51:12 -050051 const ResourceProvider& fResources;
Florin Malita094ccde2017-12-30 12:27:00 -050052 const AssetMap& fAssets;
53 SkTArray<std::unique_ptr<AnimatorBase>>& fAnimators;
54};
55
56bool LogFail(const Json::Value& json, const char* msg) {
57 const auto dump = json.toStyledString();
58 LOG("!! %s: %s", msg, dump.c_str());
59 return false;
60}
61
62// This is the workhorse for binding properties: depending on whether the property is animated,
63// it will either apply immediately or instantiate and attach a keyframe animator.
Florin Malitaf9590922018-01-09 11:56:09 -050064template <typename ValT, typename NodeT>
65bool BindProperty(const Json::Value& jprop, AttachContext* ctx, const sk_sp<NodeT>& node,
66 typename Animator<ValT, NodeT>::ApplyFuncT&& apply) {
Florin Malita094ccde2017-12-30 12:27:00 -050067 if (!jprop.isObject())
68 return false;
69
Florin Malita95448a92018-01-08 10:15:12 -050070 const auto& jpropA = jprop["a"];
71 const auto& jpropK = jprop["k"];
72
73 // Older Json versions don't have an "a" animation marker.
74 // For those, we attempt to parse both ways.
75 if (jpropA.isNull() || !ParseBool(jpropA, "false")) {
Florin Malitaf9590922018-01-09 11:56:09 -050076 ValT val;
77 if (ValueTraits<ValT>::Parse(jpropK, &val)) {
Florin Malita95448a92018-01-08 10:15:12 -050078 // Static property.
Florin Malitaf9590922018-01-09 11:56:09 -050079 apply(node.get(), val);
Florin Malita95448a92018-01-08 10:15:12 -050080 return true;
Florin Malita094ccde2017-12-30 12:27:00 -050081 }
82
Florin Malita95448a92018-01-08 10:15:12 -050083 if (!jpropA.isNull()) {
84 return LogFail(jprop, "Could not parse (explicit) static property");
Florin Malita094ccde2017-12-30 12:27:00 -050085 }
Florin Malita094ccde2017-12-30 12:27:00 -050086 }
87
Florin Malita95448a92018-01-08 10:15:12 -050088 // Keyframe property.
Florin Malitaf9590922018-01-09 11:56:09 -050089 using AnimatorT = Animator<ValT, NodeT>;
90 auto animator = AnimatorT::Make(ParseFrames<ValT>(jpropK), node, std::move(apply));
Florin Malita95448a92018-01-08 10:15:12 -050091
92 if (!animator) {
93 return LogFail(jprop, "Could not parse keyframed property");
94 }
95
96 ctx->fAnimators.push_back(std::move(animator));
97
Florin Malita094ccde2017-12-30 12:27:00 -050098 return true;
99}
100
Florin Malita18eafd92018-01-04 21:11:55 -0500101sk_sp<sksg::Matrix> AttachMatrix(const Json::Value& t, AttachContext* ctx,
102 sk_sp<sksg::Matrix> parentMatrix) {
103 if (!t.isObject())
104 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500105
Florin Malita18eafd92018-01-04 21:11:55 -0500106 auto matrix = sksg::Matrix::Make(SkMatrix::I(), std::move(parentMatrix));
107 auto composite = sk_make_sp<CompositeTransform>(matrix);
Florin Malitaf9590922018-01-09 11:56:09 -0500108 auto anchor_attached = BindProperty<VectorValue>(t["a"], ctx, composite,
109 [](CompositeTransform* node, const VectorValue& a) {
110 node->setAnchorPoint(ValueTraits<VectorValue>::As<SkPoint>(a));
Florin Malita094ccde2017-12-30 12:27:00 -0500111 });
Florin Malitaf9590922018-01-09 11:56:09 -0500112 auto position_attached = BindProperty<VectorValue>(t["p"], ctx, composite,
113 [](CompositeTransform* node, const VectorValue& p) {
114 node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
Florin Malita094ccde2017-12-30 12:27:00 -0500115 });
Florin Malitaf9590922018-01-09 11:56:09 -0500116 auto scale_attached = BindProperty<VectorValue>(t["s"], ctx, composite,
117 [](CompositeTransform* node, const VectorValue& s) {
118 node->setScale(ValueTraits<VectorValue>::As<SkVector>(s));
Florin Malita094ccde2017-12-30 12:27:00 -0500119 });
Florin Malitaf9590922018-01-09 11:56:09 -0500120 auto rotation_attached = BindProperty<ScalarValue>(t["r"], ctx, composite,
121 [](CompositeTransform* node, const ScalarValue& r) {
Florin Malita094ccde2017-12-30 12:27:00 -0500122 node->setRotation(r);
123 });
Florin Malitaf9590922018-01-09 11:56:09 -0500124 auto skew_attached = BindProperty<ScalarValue>(t["sk"], ctx, composite,
125 [](CompositeTransform* node, const ScalarValue& sk) {
Florin Malita094ccde2017-12-30 12:27:00 -0500126 node->setSkew(sk);
127 });
Florin Malitaf9590922018-01-09 11:56:09 -0500128 auto skewaxis_attached = BindProperty<ScalarValue>(t["sa"], ctx, composite,
129 [](CompositeTransform* node, const ScalarValue& sa) {
Florin Malita094ccde2017-12-30 12:27:00 -0500130 node->setSkewAxis(sa);
131 });
132
133 if (!anchor_attached &&
134 !position_attached &&
135 !scale_attached &&
136 !rotation_attached &&
137 !skew_attached &&
138 !skewaxis_attached) {
139 LogFail(t, "Could not parse transform");
Florin Malita18eafd92018-01-04 21:11:55 -0500140 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500141 }
142
Florin Malita18eafd92018-01-04 21:11:55 -0500143 return matrix;
Florin Malita094ccde2017-12-30 12:27:00 -0500144}
145
Florin Malitac0034172018-01-08 16:42:59 -0500146sk_sp<sksg::RenderNode> AttachOpacity(const Json::Value& jtransform, AttachContext* ctx,
147 sk_sp<sksg::RenderNode> childNode) {
148 if (!jtransform.isObject() || !childNode)
149 return childNode;
150
151 // This is more peeky than other attachers, because we want to avoid redundant opacity
152 // nodes for the extremely common case of static opaciy == 100.
153 const auto& opacity = jtransform["o"];
154 if (opacity.isObject() &&
155 !ParseBool(opacity["a"], true) &&
156 ParseScalar(opacity["k"], -1) == 100) {
157 // Ignoring static full opacity.
158 return childNode;
159 }
160
161 auto opacityNode = sksg::OpacityEffect::Make(childNode);
Florin Malitaf9590922018-01-09 11:56:09 -0500162 BindProperty<ScalarValue>(opacity, ctx, opacityNode,
163 [](sksg::OpacityEffect* node, const ScalarValue& o) {
Florin Malitac0034172018-01-08 16:42:59 -0500164 // BM opacity is [0..100]
165 node->setOpacity(o * 0.01f);
166 });
167
168 return opacityNode;
169}
170
Florin Malita094ccde2017-12-30 12:27:00 -0500171sk_sp<sksg::RenderNode> AttachShape(const Json::Value&, AttachContext* ctx);
172sk_sp<sksg::RenderNode> AttachComposition(const Json::Value&, AttachContext* ctx);
173
174sk_sp<sksg::RenderNode> AttachShapeGroup(const Json::Value& jgroup, AttachContext* ctx) {
175 SkASSERT(jgroup.isObject());
176
177 return AttachShape(jgroup["it"], ctx);
178}
179
180sk_sp<sksg::GeometryNode> AttachPathGeometry(const Json::Value& jpath, AttachContext* ctx) {
181 SkASSERT(jpath.isObject());
182
183 auto path_node = sksg::Path::Make();
Florin Malitaf9590922018-01-09 11:56:09 -0500184 auto path_attached = BindProperty<ShapeValue>(jpath["ks"], ctx, path_node,
185 [](sksg::Path* node, const ShapeValue& p) { node->setPath(p); });
Florin Malita094ccde2017-12-30 12:27:00 -0500186
187 if (path_attached)
188 LOG("** Attached path geometry - verbs: %d\n", path_node->getPath().countVerbs());
189
190 return path_attached ? path_node : nullptr;
191}
192
Florin Malita2e1d7e22018-01-02 10:40:00 -0500193sk_sp<sksg::GeometryNode> AttachRRectGeometry(const Json::Value& jrect, AttachContext* ctx) {
194 SkASSERT(jrect.isObject());
195
196 auto rect_node = sksg::RRect::Make();
197 auto composite = sk_make_sp<CompositeRRect>(rect_node);
198
Florin Malitaf9590922018-01-09 11:56:09 -0500199 auto p_attached = BindProperty<VectorValue>(jrect["p"], ctx, composite,
200 [](CompositeRRect* node, const VectorValue& p) {
201 node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
202 });
203 auto s_attached = BindProperty<VectorValue>(jrect["s"], ctx, composite,
204 [](CompositeRRect* node, const VectorValue& s) {
205 node->setSize(ValueTraits<VectorValue>::As<SkSize>(s));
206 });
207 auto r_attached = BindProperty<ScalarValue>(jrect["r"], ctx, composite,
208 [](CompositeRRect* node, const ScalarValue& r) {
209 node->setRadius(SkSize::Make(r, r));
210 });
Florin Malita2e1d7e22018-01-02 10:40:00 -0500211
212 if (!p_attached && !s_attached && !r_attached) {
213 return nullptr;
214 }
215
Florin Malitafbc13f12018-01-04 10:26:35 -0500216 LOG("** Attached (r)rect geometry\n");
217
218 return rect_node;
219}
220
221sk_sp<sksg::GeometryNode> AttachEllipseGeometry(const Json::Value& jellipse, AttachContext* ctx) {
222 SkASSERT(jellipse.isObject());
223
224 auto rect_node = sksg::RRect::Make();
225 auto composite = sk_make_sp<CompositeRRect>(rect_node);
226
Florin Malitaf9590922018-01-09 11:56:09 -0500227 auto p_attached = BindProperty<VectorValue>(jellipse["p"], ctx, composite,
228 [](CompositeRRect* node, const VectorValue& p) {
229 node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
230 });
231 auto s_attached = BindProperty<VectorValue>(jellipse["s"], ctx, composite,
232 [](CompositeRRect* node, const VectorValue& s) {
233 const auto sz = ValueTraits<VectorValue>::As<SkSize>(s);
234 node->setSize(sz);
235 node->setRadius(SkSize::Make(sz.width() / 2, sz.height() / 2));
236 });
Florin Malitafbc13f12018-01-04 10:26:35 -0500237
238 if (!p_attached && !s_attached) {
239 return nullptr;
240 }
241
242 LOG("** Attached ellipse geometry\n");
243
Florin Malita2e1d7e22018-01-02 10:40:00 -0500244 return rect_node;
245}
246
Florin Malita02a32b02018-01-04 11:27:09 -0500247sk_sp<sksg::GeometryNode> AttachPolystarGeometry(const Json::Value& jstar, AttachContext* ctx) {
248 SkASSERT(jstar.isObject());
249
250 static constexpr CompositePolyStar::Type gTypes[] = {
251 CompositePolyStar::Type::kStar, // "sy": 1
252 CompositePolyStar::Type::kPoly, // "sy": 2
253 };
254
255 const auto type = ParseInt(jstar["sy"], 0) - 1;
256 if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gTypes))) {
257 LogFail(jstar, "Unknown polystar type");
258 return nullptr;
259 }
260
261 auto path_node = sksg::Path::Make();
262 auto composite = sk_make_sp<CompositePolyStar>(path_node, gTypes[type]);
263
Florin Malitaf9590922018-01-09 11:56:09 -0500264 BindProperty<VectorValue>(jstar["p"], ctx, composite,
265 [](CompositePolyStar* node, const VectorValue& p) {
266 node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p));
267 });
268 BindProperty<ScalarValue>(jstar["pt"], ctx, composite,
269 [](CompositePolyStar* node, const ScalarValue& pt) {
270 node->setPointCount(pt);
271 });
272 BindProperty<ScalarValue>(jstar["ir"], ctx, composite,
273 [](CompositePolyStar* node, const ScalarValue& ir) {
274 node->setInnerRadius(ir);
275 });
276 BindProperty<ScalarValue>(jstar["or"], ctx, composite,
277 [](CompositePolyStar* node, const ScalarValue& otr) {
Florin Malita9661b982018-01-06 14:25:49 -0500278 node->setOuterRadius(otr);
279 });
Florin Malitaf9590922018-01-09 11:56:09 -0500280 BindProperty<ScalarValue>(jstar["is"], ctx, composite,
281 [](CompositePolyStar* node, const ScalarValue& is) {
Florin Malita9661b982018-01-06 14:25:49 -0500282 node->setInnerRoundness(is);
283 });
Florin Malitaf9590922018-01-09 11:56:09 -0500284 BindProperty<ScalarValue>(jstar["os"], ctx, composite,
285 [](CompositePolyStar* node, const ScalarValue& os) {
Florin Malita9661b982018-01-06 14:25:49 -0500286 node->setOuterRoundness(os);
287 });
Florin Malitaf9590922018-01-09 11:56:09 -0500288 BindProperty<ScalarValue>(jstar["r"], ctx, composite,
289 [](CompositePolyStar* node, const ScalarValue& r) {
290 node->setRotation(r);
291 });
Florin Malita02a32b02018-01-04 11:27:09 -0500292
293 return path_node;
294}
295
Florin Malita094ccde2017-12-30 12:27:00 -0500296sk_sp<sksg::Color> AttachColorPaint(const Json::Value& obj, AttachContext* ctx) {
297 SkASSERT(obj.isObject());
298
299 auto color_node = sksg::Color::Make(SK_ColorBLACK);
300 color_node->setAntiAlias(true);
301
Florin Malitadcbb2db2018-01-09 13:11:56 -0500302 auto composite = sk_make_sp<CompositeColor>(color_node);
303 auto color_attached = BindProperty<VectorValue>(obj["c"], ctx, composite,
304 [](CompositeColor* node, const VectorValue& c) {
Florin Malitaf9590922018-01-09 11:56:09 -0500305 node->setColor(ValueTraits<VectorValue>::As<SkColor>(c));
306 });
Florin Malitadcbb2db2018-01-09 13:11:56 -0500307 auto opacity_attached = BindProperty<ScalarValue>(obj["o"], ctx, composite,
308 [](CompositeColor* node, const ScalarValue& o) {
309 node->setOpacity(o);
310 });
Florin Malita094ccde2017-12-30 12:27:00 -0500311
Florin Malitadcbb2db2018-01-09 13:11:56 -0500312 return (color_attached || opacity_attached) ? color_node : nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500313}
314
315sk_sp<sksg::PaintNode> AttachFillPaint(const Json::Value& jfill, AttachContext* ctx) {
316 SkASSERT(jfill.isObject());
317
318 auto color = AttachColorPaint(jfill, ctx);
319 if (color) {
320 LOG("** Attached color fill: 0x%x\n", color->getColor());
321 }
322 return color;
323}
324
325sk_sp<sksg::PaintNode> AttachStrokePaint(const Json::Value& jstroke, AttachContext* ctx) {
326 SkASSERT(jstroke.isObject());
327
328 auto stroke_node = AttachColorPaint(jstroke, ctx);
329 if (!stroke_node)
330 return nullptr;
331
332 LOG("** Attached color stroke: 0x%x\n", stroke_node->getColor());
333
334 stroke_node->setStyle(SkPaint::kStroke_Style);
335
Florin Malitaf9590922018-01-09 11:56:09 -0500336 auto width_attached = BindProperty<ScalarValue>(jstroke["w"], ctx, stroke_node,
337 [](sksg::Color* node, const ScalarValue& w) {
338 node->setStrokeWidth(w);
339 });
Florin Malita094ccde2017-12-30 12:27:00 -0500340 if (!width_attached)
341 return nullptr;
342
343 stroke_node->setStrokeMiter(ParseScalar(jstroke["ml"], 4));
344
345 static constexpr SkPaint::Join gJoins[] = {
346 SkPaint::kMiter_Join,
347 SkPaint::kRound_Join,
348 SkPaint::kBevel_Join,
349 };
350 stroke_node->setStrokeJoin(gJoins[SkTPin<int>(ParseInt(jstroke["lj"], 1) - 1,
351 0, SK_ARRAY_COUNT(gJoins) - 1)]);
352
353 static constexpr SkPaint::Cap gCaps[] = {
354 SkPaint::kButt_Cap,
355 SkPaint::kRound_Cap,
356 SkPaint::kSquare_Cap,
357 };
358 stroke_node->setStrokeCap(gCaps[SkTPin<int>(ParseInt(jstroke["lc"], 1) - 1,
359 0, SK_ARRAY_COUNT(gCaps) - 1)]);
360
361 return stroke_node;
362}
363
Florin Malitae6345d92018-01-03 23:37:54 -0500364std::vector<sk_sp<sksg::GeometryNode>> AttachMergeGeometryEffect(
365 const Json::Value& jmerge, AttachContext* ctx, std::vector<sk_sp<sksg::GeometryNode>>&& geos) {
366 std::vector<sk_sp<sksg::GeometryNode>> merged;
367
368 static constexpr sksg::Merge::Mode gModes[] = {
369 sksg::Merge::Mode::kMerge, // "mm": 1
370 sksg::Merge::Mode::kUnion, // "mm": 2
371 sksg::Merge::Mode::kDifference, // "mm": 3
372 sksg::Merge::Mode::kIntersect, // "mm": 4
373 sksg::Merge::Mode::kXOR , // "mm": 5
374 };
375
Florin Malita51b8c892018-01-07 08:54:24 -0500376 const auto mode = gModes[SkTPin<int>(ParseInt(jmerge["mm"], 1) - 1,
377 0, SK_ARRAY_COUNT(gModes) - 1)];
Florin Malitae6345d92018-01-03 23:37:54 -0500378 merged.push_back(sksg::Merge::Make(std::move(geos), mode));
379
380 LOG("** Attached merge path effect, mode: %d\n", mode);
381
382 return merged;
383}
384
Florin Malita51b8c892018-01-07 08:54:24 -0500385std::vector<sk_sp<sksg::GeometryNode>> AttachTrimGeometryEffect(
386 const Json::Value& jtrim, AttachContext* ctx, std::vector<sk_sp<sksg::GeometryNode>>&& geos) {
387
388 enum class Mode {
389 kMerged, // "m": 1
390 kSeparate, // "m": 2
391 } gModes[] = { Mode::kMerged, Mode::kSeparate };
392
393 const auto mode = gModes[SkTPin<int>(ParseInt(jtrim["m"], 1) - 1,
394 0, SK_ARRAY_COUNT(gModes) - 1)];
395
396 std::vector<sk_sp<sksg::GeometryNode>> inputs;
397 if (mode == Mode::kMerged) {
398 inputs.push_back(sksg::Merge::Make(std::move(geos), sksg::Merge::Mode::kMerge));
399 } else {
400 inputs = std::move(geos);
401 }
402
403 std::vector<sk_sp<sksg::GeometryNode>> trimmed;
404 trimmed.reserve(inputs.size());
405 for (const auto& i : inputs) {
406 const auto trim = sksg::TrimEffect::Make(i);
407 trimmed.push_back(trim);
Florin Malitaf9590922018-01-09 11:56:09 -0500408 BindProperty<ScalarValue>(jtrim["s"], ctx, trim,
409 [](sksg::TrimEffect* node, const ScalarValue& s) {
Florin Malita51b8c892018-01-07 08:54:24 -0500410 node->setStart(s * 0.01f);
411 });
Florin Malitaf9590922018-01-09 11:56:09 -0500412 BindProperty<ScalarValue>(jtrim["e"], ctx, trim,
413 [](sksg::TrimEffect* node, const ScalarValue& e) {
Florin Malita51b8c892018-01-07 08:54:24 -0500414 node->setEnd(e * 0.01f);
415 });
416 // TODO: "offset" doesn't currently work the same as BM - figure out what's going on.
Florin Malitaf9590922018-01-09 11:56:09 -0500417 BindProperty<ScalarValue>(jtrim["o"], ctx, trim,
418 [](sksg::TrimEffect* node, const ScalarValue& o) {
Florin Malita51b8c892018-01-07 08:54:24 -0500419 node->setOffset(o * 0.01f);
420 });
421 }
422
423 return trimmed;
424}
425
Florin Malita094ccde2017-12-30 12:27:00 -0500426using GeometryAttacherT = sk_sp<sksg::GeometryNode> (*)(const Json::Value&, AttachContext*);
427static constexpr GeometryAttacherT gGeometryAttachers[] = {
428 AttachPathGeometry,
Florin Malita2e1d7e22018-01-02 10:40:00 -0500429 AttachRRectGeometry,
Florin Malitafbc13f12018-01-04 10:26:35 -0500430 AttachEllipseGeometry,
Florin Malita02a32b02018-01-04 11:27:09 -0500431 AttachPolystarGeometry,
Florin Malita094ccde2017-12-30 12:27:00 -0500432};
433
434using PaintAttacherT = sk_sp<sksg::PaintNode> (*)(const Json::Value&, AttachContext*);
435static constexpr PaintAttacherT gPaintAttachers[] = {
436 AttachFillPaint,
437 AttachStrokePaint,
438};
439
440using GroupAttacherT = sk_sp<sksg::RenderNode> (*)(const Json::Value&, AttachContext*);
441static constexpr GroupAttacherT gGroupAttachers[] = {
442 AttachShapeGroup,
443};
444
Florin Malitae6345d92018-01-03 23:37:54 -0500445using GeometryEffectAttacherT =
446 std::vector<sk_sp<sksg::GeometryNode>> (*)(const Json::Value&,
447 AttachContext*,
448 std::vector<sk_sp<sksg::GeometryNode>>&&);
449static constexpr GeometryEffectAttacherT gGeometryEffectAttachers[] = {
450 AttachMergeGeometryEffect,
Florin Malita51b8c892018-01-07 08:54:24 -0500451 AttachTrimGeometryEffect,
Florin Malitae6345d92018-01-03 23:37:54 -0500452};
453
Florin Malita094ccde2017-12-30 12:27:00 -0500454enum class ShapeType {
455 kGeometry,
Florin Malitae6345d92018-01-03 23:37:54 -0500456 kGeometryEffect,
Florin Malita094ccde2017-12-30 12:27:00 -0500457 kPaint,
458 kGroup,
Florin Malitadacc02b2017-12-31 09:12:31 -0500459 kTransform,
Florin Malita094ccde2017-12-30 12:27:00 -0500460};
461
462struct ShapeInfo {
463 const char* fTypeString;
464 ShapeType fShapeType;
465 uint32_t fAttacherIndex; // index into respective attacher tables
466};
467
468const ShapeInfo* FindShapeInfo(const Json::Value& shape) {
469 static constexpr ShapeInfo gShapeInfo[] = {
Florin Malitafbc13f12018-01-04 10:26:35 -0500470 { "el", ShapeType::kGeometry , 2 }, // ellipse -> AttachEllipseGeometry
Florin Malitae6345d92018-01-03 23:37:54 -0500471 { "fl", ShapeType::kPaint , 0 }, // fill -> AttachFillPaint
472 { "gr", ShapeType::kGroup , 0 }, // group -> AttachShapeGroup
473 { "mm", ShapeType::kGeometryEffect, 0 }, // merge -> AttachMergeGeometryEffect
Florin Malita02a32b02018-01-04 11:27:09 -0500474 { "rc", ShapeType::kGeometry , 1 }, // rrect -> AttachRRectGeometry
Florin Malitae6345d92018-01-03 23:37:54 -0500475 { "sh", ShapeType::kGeometry , 0 }, // shape -> AttachPathGeometry
Florin Malita02a32b02018-01-04 11:27:09 -0500476 { "sr", ShapeType::kGeometry , 3 }, // polystar -> AttachPolyStarGeometry
Florin Malitae6345d92018-01-03 23:37:54 -0500477 { "st", ShapeType::kPaint , 1 }, // stroke -> AttachStrokePaint
Florin Malita51b8c892018-01-07 08:54:24 -0500478 { "tm", ShapeType::kGeometryEffect, 1 }, // trim -> AttachTrimGeometryEffect
Florin Malita18eafd92018-01-04 21:11:55 -0500479 { "tr", ShapeType::kTransform , 0 }, // transform -> In-place handler
Florin Malita094ccde2017-12-30 12:27:00 -0500480 };
481
482 if (!shape.isObject())
483 return nullptr;
484
485 const auto& type = shape["ty"];
486 if (!type.isString())
487 return nullptr;
488
489 const auto* info = bsearch(type.asCString(),
490 gShapeInfo,
491 SK_ARRAY_COUNT(gShapeInfo),
492 sizeof(ShapeInfo),
493 [](const void* key, const void* info) {
494 return strcmp(static_cast<const char*>(key),
495 static_cast<const ShapeInfo*>(info)->fTypeString);
496 });
497
498 return static_cast<const ShapeInfo*>(info);
499}
500
501sk_sp<sksg::RenderNode> AttachShape(const Json::Value& shapeArray, AttachContext* ctx) {
502 if (!shapeArray.isArray())
503 return nullptr;
504
Florin Malita2a8275b2018-01-02 12:52:43 -0500505 // (https://helpx.adobe.com/after-effects/using/overview-shape-layers-paths-vector.html#groups_and_render_order_for_shapes_and_shape_attributes)
506 //
507 // Render order for shapes within a shape layer
508 //
509 // The rules for rendering a shape layer are similar to the rules for rendering a composition
510 // that contains nested compositions:
511 //
512 // * Within a group, the shape at the bottom of the Timeline panel stacking order is rendered
513 // first.
514 //
515 // * All path operations within a group are performed before paint operations. This means,
516 // for example, that the stroke follows the distortions in the path made by the Wiggle Paths
517 // path operation. Path operations within a group are performed from top to bottom.
518 //
519 // * Paint operations within a group are performed from the bottom to the top in the Timeline
520 // panel stacking order. This means, for example, that a stroke is rendered on top of
521 // (in front of) a stroke that appears after it in the Timeline panel.
522 //
Florin Malitadacc02b2017-12-31 09:12:31 -0500523 sk_sp<sksg::Group> shape_group = sksg::Group::Make();
524 sk_sp<sksg::RenderNode> xformed_group = shape_group;
Florin Malita094ccde2017-12-30 12:27:00 -0500525
Florin Malitae6345d92018-01-03 23:37:54 -0500526 std::vector<sk_sp<sksg::GeometryNode>> geos;
527 std::vector<sk_sp<sksg::RenderNode>> draws;
Florin Malita094ccde2017-12-30 12:27:00 -0500528
529 for (const auto& s : shapeArray) {
530 const auto* info = FindShapeInfo(s);
531 if (!info) {
532 LogFail(s.isObject() ? s["ty"] : s, "Unknown shape");
533 continue;
534 }
535
536 switch (info->fShapeType) {
537 case ShapeType::kGeometry: {
538 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGeometryAttachers));
539 if (auto geo = gGeometryAttachers[info->fAttacherIndex](s, ctx)) {
540 geos.push_back(std::move(geo));
541 }
542 } break;
Florin Malitae6345d92018-01-03 23:37:54 -0500543 case ShapeType::kGeometryEffect: {
544 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGeometryEffectAttachers));
545 geos = gGeometryEffectAttachers[info->fAttacherIndex](s, ctx, std::move(geos));
546 } break;
Florin Malita094ccde2017-12-30 12:27:00 -0500547 case ShapeType::kPaint: {
548 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gPaintAttachers));
549 if (auto paint = gPaintAttachers[info->fAttacherIndex](s, ctx)) {
Florin Malita2a8275b2018-01-02 12:52:43 -0500550 for (const auto& geo : geos) {
551 draws.push_back(sksg::Draw::Make(geo, paint));
552 }
Florin Malita094ccde2017-12-30 12:27:00 -0500553 }
554 } break;
555 case ShapeType::kGroup: {
556 SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGroupAttachers));
557 if (auto group = gGroupAttachers[info->fAttacherIndex](s, ctx)) {
Florin Malita2a8275b2018-01-02 12:52:43 -0500558 draws.push_back(std::move(group));
Florin Malita094ccde2017-12-30 12:27:00 -0500559 }
560 } break;
Florin Malitadacc02b2017-12-31 09:12:31 -0500561 case ShapeType::kTransform: {
Florin Malita2a8275b2018-01-02 12:52:43 -0500562 // TODO: BM appears to transform the geometry, not the draw op itself.
Florin Malita18eafd92018-01-04 21:11:55 -0500563 if (auto matrix = AttachMatrix(s, ctx, nullptr)) {
564 xformed_group = sksg::Transform::Make(std::move(xformed_group),
565 std::move(matrix));
566 }
Florin Malitac0034172018-01-08 16:42:59 -0500567 xformed_group = AttachOpacity(s, ctx, std::move(xformed_group));
Florin Malitadacc02b2017-12-31 09:12:31 -0500568 } break;
Florin Malita094ccde2017-12-30 12:27:00 -0500569 }
570 }
571
Florin Malita2a8275b2018-01-02 12:52:43 -0500572 if (draws.empty()) {
573 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500574 }
575
Florin Malitae6345d92018-01-03 23:37:54 -0500576 for (auto draw = draws.rbegin(); draw != draws.rend(); ++draw) {
577 shape_group->addChild(std::move(*draw));
Florin Malita2a8275b2018-01-02 12:52:43 -0500578 }
579
Florin Malitae6345d92018-01-03 23:37:54 -0500580 LOG("** Attached shape: %zd draws.\n", draws.size());
Florin Malitadacc02b2017-12-31 09:12:31 -0500581 return xformed_group;
Florin Malita094ccde2017-12-30 12:27:00 -0500582}
583
584sk_sp<sksg::RenderNode> AttachCompLayer(const Json::Value& layer, AttachContext* ctx) {
585 SkASSERT(layer.isObject());
586
587 auto refId = ParseString(layer["refId"], "");
588 if (refId.isEmpty()) {
589 LOG("!! Comp layer missing refId\n");
590 return nullptr;
591 }
592
593 const auto* comp = ctx->fAssets.find(refId);
594 if (!comp) {
595 LOG("!! Pre-comp not found: '%s'\n", refId.c_str());
596 return nullptr;
597 }
598
599 // TODO: cycle detection
600 return AttachComposition(**comp, ctx);
601}
602
Florin Malita0e66fba2018-01-09 17:10:18 -0500603sk_sp<sksg::RenderNode> AttachSolidLayer(const Json::Value& jlayer, AttachContext*) {
604 SkASSERT(jlayer.isObject());
Florin Malita094ccde2017-12-30 12:27:00 -0500605
Florin Malita0e66fba2018-01-09 17:10:18 -0500606 const auto size = SkSize::Make(ParseScalar(jlayer["sw"], -1),
607 ParseScalar(jlayer["sh"], -1));
608 const auto hex = ParseString(jlayer["sc"], "");
609 uint32_t c;
610 if (size.isEmpty() ||
611 !hex.startsWith("#") ||
612 !SkParse::FindHex(hex.c_str() + 1, &c)) {
613 LogFail(jlayer, "Could not parse solid layer");
614 return nullptr;
615 }
616
617 const SkColor color = 0xff000000 | c;
618
619 return sksg::Draw::Make(sksg::Rect::Make(SkRect::MakeSize(size)),
620 sksg::Color::Make(color));
Florin Malita094ccde2017-12-30 12:27:00 -0500621}
622
Florin Malita49328072018-01-08 12:51:12 -0500623sk_sp<sksg::RenderNode> AttachImageAsset(const Json::Value& jimage, AttachContext* ctx) {
624 SkASSERT(jimage.isObject());
625
626 const auto name = ParseString(jimage["p"], ""),
627 path = ParseString(jimage["u"], "");
628 if (name.isEmpty())
629 return nullptr;
630
631 // TODO: plumb resource paths explicitly to ResourceProvider?
632 const auto resName = path.isEmpty() ? name : SkOSPath::Join(path.c_str(), name.c_str());
633 const auto resStream = ctx->fResources.openStream(resName.c_str());
634 if (!resStream || !resStream->hasLength()) {
635 LOG("!! Could not load image resource: %s\n", resName.c_str());
636 return nullptr;
637 }
638
639 // TODO: non-intrisic image sizing
640 return sksg::Image::Make(
641 SkImage::MakeFromEncoded(SkData::MakeFromStream(resStream.get(), resStream->getLength())));
642}
643
644sk_sp<sksg::RenderNode> AttachImageLayer(const Json::Value& layer, AttachContext* ctx) {
Florin Malita094ccde2017-12-30 12:27:00 -0500645 SkASSERT(layer.isObject());
646
Florin Malita49328072018-01-08 12:51:12 -0500647 auto refId = ParseString(layer["refId"], "");
648 if (refId.isEmpty()) {
649 LOG("!! Image layer missing refId\n");
650 return nullptr;
651 }
652
653 const auto* jimage = ctx->fAssets.find(refId);
654 if (!jimage) {
655 LOG("!! Image asset not found: '%s'\n", refId.c_str());
656 return nullptr;
657 }
658
659 return AttachImageAsset(**jimage, ctx);
Florin Malita094ccde2017-12-30 12:27:00 -0500660}
661
662sk_sp<sksg::RenderNode> AttachNullLayer(const Json::Value& layer, AttachContext*) {
663 SkASSERT(layer.isObject());
664
Florin Malita18eafd92018-01-04 21:11:55 -0500665 // Null layers are used solely to drive dependent transforms,
666 // but we use free-floating sksg::Matrices for that purpose.
Florin Malita094ccde2017-12-30 12:27:00 -0500667 return nullptr;
668}
669
670sk_sp<sksg::RenderNode> AttachShapeLayer(const Json::Value& layer, AttachContext* ctx) {
671 SkASSERT(layer.isObject());
672
673 LOG("** Attaching shape layer ind: %d\n", ParseInt(layer["ind"], 0));
674
675 return AttachShape(layer["shapes"], ctx);
676}
677
678sk_sp<sksg::RenderNode> AttachTextLayer(const Json::Value& layer, AttachContext*) {
679 SkASSERT(layer.isObject());
680
681 LOG("?? Text layer stub\n");
682 return nullptr;
683}
684
Florin Malita18eafd92018-01-04 21:11:55 -0500685struct AttachLayerContext {
686 AttachLayerContext(const Json::Value& jlayers, AttachContext* ctx)
687 : fLayerList(jlayers), fCtx(ctx) {}
688
689 const Json::Value& fLayerList;
690 AttachContext* fCtx;
691 std::unordered_map<const Json::Value*, sk_sp<sksg::Matrix>> fLayerMatrixCache;
692 std::unordered_map<int, const Json::Value*> fLayerIndexCache;
Florin Malita5f9102f2018-01-10 13:36:22 -0500693 sk_sp<sksg::RenderNode> fCurrentMatte;
Florin Malita18eafd92018-01-04 21:11:55 -0500694
695 const Json::Value* findLayer(int index) {
696 SkASSERT(fLayerList.isArray());
697
698 if (index < 0) {
699 return nullptr;
700 }
701
702 const auto cached = fLayerIndexCache.find(index);
703 if (cached != fLayerIndexCache.end()) {
704 return cached->second;
705 }
706
707 for (const auto& l : fLayerList) {
708 if (!l.isObject()) {
709 continue;
710 }
711
712 if (ParseInt(l["ind"], -1) == index) {
713 fLayerIndexCache.insert(std::make_pair(index, &l));
714 return &l;
715 }
716 }
717
718 return nullptr;
719 }
720
721 sk_sp<sksg::Matrix> AttachLayerMatrix(const Json::Value& jlayer) {
722 SkASSERT(jlayer.isObject());
723
724 const auto cached = fLayerMatrixCache.find(&jlayer);
725 if (cached != fLayerMatrixCache.end()) {
726 return cached->second;
727 }
728
729 const auto* parentLayer = this->findLayer(ParseInt(jlayer["parent"], -1));
730
731 // TODO: cycle detection?
732 auto parentMatrix = (parentLayer && parentLayer != &jlayer)
733 ? this->AttachLayerMatrix(*parentLayer) : nullptr;
734
735 auto layerMatrix = AttachMatrix(jlayer["ks"], fCtx, std::move(parentMatrix));
736 fLayerMatrixCache.insert(std::make_pair(&jlayer, layerMatrix));
737
738 return layerMatrix;
739 }
740};
741
742sk_sp<sksg::RenderNode> AttachLayer(const Json::Value& jlayer,
743 AttachLayerContext* layerCtx) {
744 if (!jlayer.isObject())
Florin Malita094ccde2017-12-30 12:27:00 -0500745 return nullptr;
746
747 using LayerAttacher = sk_sp<sksg::RenderNode> (*)(const Json::Value&, AttachContext*);
748 static constexpr LayerAttacher gLayerAttachers[] = {
749 AttachCompLayer, // 'ty': 0
750 AttachSolidLayer, // 'ty': 1
751 AttachImageLayer, // 'ty': 2
752 AttachNullLayer, // 'ty': 3
753 AttachShapeLayer, // 'ty': 4
754 AttachTextLayer, // 'ty': 5
755 };
756
Florin Malita18eafd92018-01-04 21:11:55 -0500757 int type = ParseInt(jlayer["ty"], -1);
Florin Malita094ccde2017-12-30 12:27:00 -0500758 if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gLayerAttachers))) {
759 return nullptr;
760 }
761
Florin Malita71cba8f2018-01-09 08:07:14 -0500762 // Layer content.
763 auto layer = gLayerAttachers[type](jlayer, layerCtx->fCtx);
764 if (auto layerMatrix = layerCtx->AttachLayerMatrix(jlayer)) {
765 // Optional layer transform.
766 layer = sksg::Transform::Make(std::move(layer), std::move(layerMatrix));
767 }
768 // Optional layer opacity.
769 layer = AttachOpacity(jlayer["ks"], layerCtx->fCtx, std::move(layer));
Florin Malita18eafd92018-01-04 21:11:55 -0500770
Florin Malita71cba8f2018-01-09 08:07:14 -0500771 // TODO: we should also disable related/inactive animators.
772 class Activator final : public AnimatorBase {
773 public:
774 Activator(sk_sp<sksg::OpacityEffect> controlNode, float in, float out)
775 : fControlNode(std::move(controlNode))
776 , fIn(in)
777 , fOut(out) {}
778
Florin Malitaa6dd7522018-01-09 08:46:52 -0500779 void tick(float t) override {
Florin Malita71cba8f2018-01-09 08:07:14 -0500780 // Keep the layer fully transparent except for its [in..out] lifespan.
781 // (note: opacity == 0 disables rendering, while opacity == 1 is a noop)
782 fControlNode->setOpacity(t >= fIn && t <= fOut ? 1 : 0);
783 }
784
785 private:
786 const sk_sp<sksg::OpacityEffect> fControlNode;
787 const float fIn,
788 fOut;
789 };
790
791 auto layerControl = sksg::OpacityEffect::Make(std::move(layer));
792 const auto in = ParseScalar(jlayer["ip"], 0),
793 out = ParseScalar(jlayer["op"], in);
794
795 if (in >= out || ! layerControl)
796 return nullptr;
797
798 layerCtx->fCtx->fAnimators.push_back(skstd::make_unique<Activator>(layerControl, in, out));
799
Florin Malita5f9102f2018-01-10 13:36:22 -0500800 if (ParseBool(jlayer["td"], false)) {
801 // This layer is a matte. We apply it as a mask to the next layer.
802 layerCtx->fCurrentMatte = std::move(layerControl);
803 return nullptr;
804 }
805
806 if (layerCtx->fCurrentMatte) {
807 // There is a pending matte. Apply and reset.
808 return sksg::MaskEffect::Make(std::move(layerControl), std::move(layerCtx->fCurrentMatte));
809 }
810
Florin Malita71cba8f2018-01-09 08:07:14 -0500811 return layerControl;
Florin Malita094ccde2017-12-30 12:27:00 -0500812}
813
814sk_sp<sksg::RenderNode> AttachComposition(const Json::Value& comp, AttachContext* ctx) {
815 if (!comp.isObject())
816 return nullptr;
817
Florin Malita18eafd92018-01-04 21:11:55 -0500818 const auto& jlayers = comp["layers"];
819 if (!jlayers.isArray())
820 return nullptr;
Florin Malita094ccde2017-12-30 12:27:00 -0500821
Florin Malita18eafd92018-01-04 21:11:55 -0500822 SkSTArray<16, sk_sp<sksg::RenderNode>, true> layers;
823 AttachLayerContext layerCtx(jlayers, ctx);
824
825 for (const auto& l : jlayers) {
826 if (auto layer_fragment = AttachLayer(l, &layerCtx)) {
Florin Malita2a8275b2018-01-02 12:52:43 -0500827 layers.push_back(std::move(layer_fragment));
Florin Malita094ccde2017-12-30 12:27:00 -0500828 }
829 }
830
Florin Malita2a8275b2018-01-02 12:52:43 -0500831 if (layers.empty()) {
832 return nullptr;
833 }
834
835 // Layers are painted in bottom->top order.
836 auto comp_group = sksg::Group::Make();
837 for (int i = layers.count() - 1; i >= 0; --i) {
838 comp_group->addChild(std::move(layers[i]));
839 }
840
841 LOG("** Attached composition '%s': %d layers.\n",
842 ParseString(comp["id"], "").c_str(), layers.count());
843
Florin Malita094ccde2017-12-30 12:27:00 -0500844 return comp_group;
845}
846
847} // namespace
848
Florin Malita49328072018-01-08 12:51:12 -0500849std::unique_ptr<Animation> Animation::Make(SkStream* stream, const ResourceProvider& res) {
Florin Malita094ccde2017-12-30 12:27:00 -0500850 if (!stream->hasLength()) {
851 // TODO: handle explicit buffering?
852 LOG("!! cannot parse streaming content\n");
853 return nullptr;
854 }
855
856 Json::Value json;
857 {
858 auto data = SkData::MakeFromStream(stream, stream->getLength());
859 if (!data) {
860 LOG("!! could not read stream\n");
861 return nullptr;
862 }
863
864 Json::Reader reader;
865
866 auto dataStart = static_cast<const char*>(data->data());
867 if (!reader.parse(dataStart, dataStart + data->size(), json, false) || !json.isObject()) {
868 LOG("!! failed to parse json: %s\n", reader.getFormattedErrorMessages().c_str());
869 return nullptr;
870 }
871 }
872
873 const auto version = ParseString(json["v"], "");
874 const auto size = SkSize::Make(ParseScalar(json["w"], -1), ParseScalar(json["h"], -1));
875 const auto fps = ParseScalar(json["fr"], -1);
876
877 if (size.isEmpty() || version.isEmpty() || fps < 0) {
878 LOG("!! invalid animation params (version: %s, size: [%f %f], frame rate: %f)",
879 version.c_str(), size.width(), size.height(), fps);
880 return nullptr;
881 }
882
Florin Malita49328072018-01-08 12:51:12 -0500883 return std::unique_ptr<Animation>(new Animation(res, std::move(version), size, fps, json));
Florin Malita094ccde2017-12-30 12:27:00 -0500884}
885
Florin Malita49328072018-01-08 12:51:12 -0500886std::unique_ptr<Animation> Animation::MakeFromFile(const char path[], const ResourceProvider* res) {
887 class DirectoryResourceProvider final : public ResourceProvider {
888 public:
889 explicit DirectoryResourceProvider(SkString dir) : fDir(std::move(dir)) {}
890
891 std::unique_ptr<SkStream> openStream(const char resource[]) const override {
892 const auto resPath = SkOSPath::Join(fDir.c_str(), resource);
893 return SkStream::MakeFromFile(resPath.c_str());
894 }
895
896 private:
897 const SkString fDir;
898 };
899
900 const auto jsonStream = SkStream::MakeFromFile(path);
901 if (!jsonStream)
902 return nullptr;
903
904 std::unique_ptr<ResourceProvider> defaultProvider;
905 if (!res) {
906 defaultProvider = skstd::make_unique<DirectoryResourceProvider>(SkOSPath::Dirname(path));
907 }
908
909 return Make(jsonStream.get(), res ? *res : *defaultProvider);
910}
911
912Animation::Animation(const ResourceProvider& resources,
913 SkString version, const SkSize& size, SkScalar fps, const Json::Value& json)
Florin Malita094ccde2017-12-30 12:27:00 -0500914 : fVersion(std::move(version))
915 , fSize(size)
916 , fFrameRate(fps)
917 , fInPoint(ParseScalar(json["ip"], 0))
918 , fOutPoint(SkTMax(ParseScalar(json["op"], SK_ScalarMax), fInPoint)) {
919
920 AssetMap assets;
921 for (const auto& asset : json["assets"]) {
922 if (!asset.isObject()) {
923 continue;
924 }
925
926 assets.set(ParseString(asset["id"], ""), &asset);
927 }
928
Florin Malita49328072018-01-08 12:51:12 -0500929 AttachContext ctx = { resources, assets, fAnimators };
Florin Malita094ccde2017-12-30 12:27:00 -0500930 fDom = AttachComposition(json, &ctx);
931
Florin Malitadb385732018-01-09 12:19:32 -0500932 // In case the client calls render before the first tick.
933 this->animationTick(0);
934
Florin Malita094ccde2017-12-30 12:27:00 -0500935 LOG("** Attached %d animators\n", fAnimators.count());
936}
937
938Animation::~Animation() = default;
939
Mike Reed29859872018-01-08 08:25:27 -0500940void Animation::render(SkCanvas* canvas, const SkRect* dstR) const {
Florin Malita094ccde2017-12-30 12:27:00 -0500941 if (!fDom)
942 return;
943
944 sksg::InvalidationController ic;
945 fDom->revalidate(&ic, SkMatrix::I());
946
947 // TODO: proper inval
Mike Reed29859872018-01-08 08:25:27 -0500948 SkAutoCanvasRestore restore(canvas, true);
949 const SkRect srcR = SkRect::MakeSize(this->size());
950 if (dstR) {
951 canvas->concat(SkMatrix::MakeRectToRect(srcR, *dstR, SkMatrix::kCenter_ScaleToFit));
952 }
953 canvas->clipRect(srcR);
Florin Malita094ccde2017-12-30 12:27:00 -0500954 fDom->render(canvas);
955
956 if (!fShowInval)
957 return;
958
959 SkPaint fill, stroke;
960 fill.setAntiAlias(true);
961 fill.setColor(0x40ff0000);
962 stroke.setAntiAlias(true);
963 stroke.setColor(0xffff0000);
964 stroke.setStyle(SkPaint::kStroke_Style);
965
966 for (const auto& r : ic) {
967 canvas->drawRect(r, fill);
968 canvas->drawRect(r, stroke);
969 }
970}
971
972void Animation::animationTick(SkMSec ms) {
973 // 't' in the BM model really means 'frame #'
974 auto t = static_cast<float>(ms) * fFrameRate / 1000;
975
976 t = fInPoint + std::fmod(t, fOutPoint - fInPoint);
977
978 // TODO: this can be optimized quite a bit with some sorting/state tracking.
979 for (const auto& a : fAnimators) {
980 a->tick(t);
981 }
982}
983
984} // namespace skotty