Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 1 | /* |
| 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 Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame^] | 15 | #include "SkImage.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 16 | #include "SkMakeUnique.h" |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame^] | 17 | #include "SkOSPath.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 18 | #include "SkPaint.h" |
| 19 | #include "SkPath.h" |
| 20 | #include "SkPoint.h" |
| 21 | #include "SkSGColor.h" |
| 22 | #include "SkSGDraw.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 23 | #include "SkSGGroup.h" |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame^] | 24 | #include "SkSGImage.h" |
| 25 | #include "SkSGInvalidationController.h" |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 26 | #include "SkSGMerge.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 27 | #include "SkSGPath.h" |
Florin Malita | 2e1d7e2 | 2018-01-02 10:40:00 -0500 | [diff] [blame] | 28 | #include "SkSGRect.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 29 | #include "SkSGTransform.h" |
Florin Malita | 51b8c89 | 2018-01-07 08:54:24 -0500 | [diff] [blame] | 30 | #include "SkSGTrimEffect.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 31 | #include "SkStream.h" |
| 32 | #include "SkTArray.h" |
| 33 | #include "SkTHash.h" |
| 34 | |
| 35 | #include <cmath> |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 36 | #include <unordered_map> |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 37 | #include <vector> |
| 38 | |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 39 | #include "stdlib.h" |
| 40 | |
| 41 | namespace skotty { |
| 42 | |
| 43 | namespace { |
| 44 | |
| 45 | using AssetMap = SkTHashMap<SkString, const Json::Value*>; |
| 46 | |
| 47 | struct AttachContext { |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame^] | 48 | const ResourceProvider& fResources; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 49 | const AssetMap& fAssets; |
| 50 | SkTArray<std::unique_ptr<AnimatorBase>>& fAnimators; |
| 51 | }; |
| 52 | |
| 53 | bool 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 Malita | 9661b98 | 2018-01-06 14:25:49 -0500 | [diff] [blame] | 61 | template <typename ValueT, typename AttrT, typename NodeT> |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 62 | bool AttachProperty(const Json::Value& jprop, AttachContext* ctx, const sk_sp<NodeT>& node, |
Florin Malita | 9661b98 | 2018-01-06 14:25:49 -0500 | [diff] [blame] | 63 | typename Animator<ValueT, AttrT, NodeT>::ApplyFuncT&& apply) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 64 | if (!jprop.isObject()) |
| 65 | return false; |
| 66 | |
Florin Malita | 95448a9 | 2018-01-08 10:15:12 -0500 | [diff] [blame] | 67 | 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 Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 73 | ValueT val; |
Florin Malita | 95448a9 | 2018-01-08 10:15:12 -0500 | [diff] [blame] | 74 | if (ValueT::Parse(jpropK, &val)) { |
| 75 | // Static property. |
| 76 | apply(node, val.template as<AttrT>()); |
| 77 | return true; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 78 | } |
| 79 | |
Florin Malita | 95448a9 | 2018-01-08 10:15:12 -0500 | [diff] [blame] | 80 | if (!jpropA.isNull()) { |
| 81 | return LogFail(jprop, "Could not parse (explicit) static property"); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 82 | } |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 83 | } |
| 84 | |
Florin Malita | 95448a9 | 2018-01-08 10:15:12 -0500 | [diff] [blame] | 85 | // 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 Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 95 | return true; |
| 96 | } |
| 97 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 98 | sk_sp<sksg::Matrix> AttachMatrix(const Json::Value& t, AttachContext* ctx, |
| 99 | sk_sp<sksg::Matrix> parentMatrix) { |
| 100 | if (!t.isObject()) |
| 101 | return nullptr; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 102 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 103 | 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 Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 106 | [](const sk_sp<CompositeTransform>& node, const SkPoint& a) { |
| 107 | node->setAnchorPoint(a); |
| 108 | }); |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 109 | auto position_attached = AttachProperty<VectorValue, SkPoint>(t["p"], ctx, composite, |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 110 | [](const sk_sp<CompositeTransform>& node, const SkPoint& p) { |
| 111 | node->setPosition(p); |
| 112 | }); |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 113 | auto scale_attached = AttachProperty<VectorValue, SkVector>(t["s"], ctx, composite, |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 114 | [](const sk_sp<CompositeTransform>& node, const SkVector& s) { |
| 115 | node->setScale(s); |
| 116 | }); |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 117 | auto rotation_attached = AttachProperty<ScalarValue, SkScalar>(t["r"], ctx, composite, |
Florin Malita | 9661b98 | 2018-01-06 14:25:49 -0500 | [diff] [blame] | 118 | [](const sk_sp<CompositeTransform>& node, const SkScalar& r) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 119 | node->setRotation(r); |
| 120 | }); |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 121 | auto skew_attached = AttachProperty<ScalarValue, SkScalar>(t["sk"], ctx, composite, |
Florin Malita | 9661b98 | 2018-01-06 14:25:49 -0500 | [diff] [blame] | 122 | [](const sk_sp<CompositeTransform>& node, const SkScalar& sk) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 123 | node->setSkew(sk); |
| 124 | }); |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 125 | auto skewaxis_attached = AttachProperty<ScalarValue, SkScalar>(t["sa"], ctx, composite, |
Florin Malita | 9661b98 | 2018-01-06 14:25:49 -0500 | [diff] [blame] | 126 | [](const sk_sp<CompositeTransform>& node, const SkScalar& sa) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 127 | 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 Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 137 | return nullptr; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 138 | } |
| 139 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 140 | return matrix; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | sk_sp<sksg::RenderNode> AttachShape(const Json::Value&, AttachContext* ctx); |
| 144 | sk_sp<sksg::RenderNode> AttachComposition(const Json::Value&, AttachContext* ctx); |
| 145 | |
| 146 | sk_sp<sksg::RenderNode> AttachShapeGroup(const Json::Value& jgroup, AttachContext* ctx) { |
| 147 | SkASSERT(jgroup.isObject()); |
| 148 | |
| 149 | return AttachShape(jgroup["it"], ctx); |
| 150 | } |
| 151 | |
| 152 | sk_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 Malita | 2e1d7e2 | 2018-01-02 10:40:00 -0500 | [diff] [blame] | 165 | sk_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 Malita | 9661b98 | 2018-01-06 14:25:49 -0500 | [diff] [blame] | 176 | [](const sk_sp<CompositeRRect>& node, const SkScalar& radius) { |
Florin Malita | fbc13f1 | 2018-01-04 10:26:35 -0500 | [diff] [blame] | 177 | node->setRadius(SkSize::Make(radius, radius)); |
| 178 | }); |
Florin Malita | 2e1d7e2 | 2018-01-02 10:40:00 -0500 | [diff] [blame] | 179 | |
| 180 | if (!p_attached && !s_attached && !r_attached) { |
| 181 | return nullptr; |
| 182 | } |
| 183 | |
Florin Malita | fbc13f1 | 2018-01-04 10:26:35 -0500 | [diff] [blame] | 184 | LOG("** Attached (r)rect geometry\n"); |
| 185 | |
| 186 | return rect_node; |
| 187 | } |
| 188 | |
| 189 | sk_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 Malita | 2e1d7e2 | 2018-01-02 10:40:00 -0500 | [diff] [blame] | 209 | return rect_node; |
| 210 | } |
| 211 | |
Florin Malita | 02a32b0 | 2018-01-04 11:27:09 -0500 | [diff] [blame] | 212 | sk_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 Malita | 9661b98 | 2018-01-06 14:25:49 -0500 | [diff] [blame] | 232 | [](const sk_sp<CompositePolyStar>& node, const SkScalar& pt) { node->setPointCount(pt); }); |
Florin Malita | 02a32b0 | 2018-01-04 11:27:09 -0500 | [diff] [blame] | 233 | AttachProperty<ScalarValue, SkScalar>(jstar["ir"], ctx, composite, |
Florin Malita | 9661b98 | 2018-01-06 14:25:49 -0500 | [diff] [blame] | 234 | [](const sk_sp<CompositePolyStar>& node, const SkScalar& ir) { node->setInnerRadius(ir); }); |
Florin Malita | 02a32b0 | 2018-01-04 11:27:09 -0500 | [diff] [blame] | 235 | AttachProperty<ScalarValue, SkScalar>(jstar["or"], ctx, composite, |
Florin Malita | 9661b98 | 2018-01-06 14:25:49 -0500 | [diff] [blame] | 236 | [](const sk_sp<CompositePolyStar>& node, const SkScalar& otr) { |
| 237 | node->setOuterRadius(otr); |
| 238 | }); |
Florin Malita | 02a32b0 | 2018-01-04 11:27:09 -0500 | [diff] [blame] | 239 | AttachProperty<ScalarValue, SkScalar>(jstar["is"], ctx, composite, |
Florin Malita | 9661b98 | 2018-01-06 14:25:49 -0500 | [diff] [blame] | 240 | [](const sk_sp<CompositePolyStar>& node, const SkScalar& is) { |
| 241 | node->setInnerRoundness(is); |
| 242 | }); |
Florin Malita | 02a32b0 | 2018-01-04 11:27:09 -0500 | [diff] [blame] | 243 | AttachProperty<ScalarValue, SkScalar>(jstar["os"], ctx, composite, |
Florin Malita | 9661b98 | 2018-01-06 14:25:49 -0500 | [diff] [blame] | 244 | [](const sk_sp<CompositePolyStar>& node, const SkScalar& os) { |
| 245 | node->setOuterRoundness(os); |
| 246 | }); |
Florin Malita | 02a32b0 | 2018-01-04 11:27:09 -0500 | [diff] [blame] | 247 | AttachProperty<ScalarValue, SkScalar>(jstar["r"], ctx, composite, |
Florin Malita | 9661b98 | 2018-01-06 14:25:49 -0500 | [diff] [blame] | 248 | [](const sk_sp<CompositePolyStar>& node, const SkScalar& r) { node->setRotation(r); }); |
Florin Malita | 02a32b0 | 2018-01-04 11:27:09 -0500 | [diff] [blame] | 249 | |
| 250 | return path_node; |
| 251 | } |
| 252 | |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 253 | sk_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 Malita | 9661b98 | 2018-01-06 14:25:49 -0500 | [diff] [blame] | 260 | [](const sk_sp<sksg::Color>& node, const SkColor& c) { node->setColor(c); }); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 261 | |
| 262 | return color_attached ? color_node : nullptr; |
| 263 | } |
| 264 | |
| 265 | sk_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 | |
| 275 | sk_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 Malita | 9661b98 | 2018-01-06 14:25:49 -0500 | [diff] [blame] | 287 | [](const sk_sp<sksg::Color>& node, const SkScalar& width) { node->setStrokeWidth(width); }); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 288 | 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 Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 312 | std::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 Malita | 51b8c89 | 2018-01-07 08:54:24 -0500 | [diff] [blame] | 324 | const auto mode = gModes[SkTPin<int>(ParseInt(jmerge["mm"], 1) - 1, |
| 325 | 0, SK_ARRAY_COUNT(gModes) - 1)]; |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 326 | 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 Malita | 51b8c89 | 2018-01-07 08:54:24 -0500 | [diff] [blame] | 333 | std::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 Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 374 | using GeometryAttacherT = sk_sp<sksg::GeometryNode> (*)(const Json::Value&, AttachContext*); |
| 375 | static constexpr GeometryAttacherT gGeometryAttachers[] = { |
| 376 | AttachPathGeometry, |
Florin Malita | 2e1d7e2 | 2018-01-02 10:40:00 -0500 | [diff] [blame] | 377 | AttachRRectGeometry, |
Florin Malita | fbc13f1 | 2018-01-04 10:26:35 -0500 | [diff] [blame] | 378 | AttachEllipseGeometry, |
Florin Malita | 02a32b0 | 2018-01-04 11:27:09 -0500 | [diff] [blame] | 379 | AttachPolystarGeometry, |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 380 | }; |
| 381 | |
| 382 | using PaintAttacherT = sk_sp<sksg::PaintNode> (*)(const Json::Value&, AttachContext*); |
| 383 | static constexpr PaintAttacherT gPaintAttachers[] = { |
| 384 | AttachFillPaint, |
| 385 | AttachStrokePaint, |
| 386 | }; |
| 387 | |
| 388 | using GroupAttacherT = sk_sp<sksg::RenderNode> (*)(const Json::Value&, AttachContext*); |
| 389 | static constexpr GroupAttacherT gGroupAttachers[] = { |
| 390 | AttachShapeGroup, |
| 391 | }; |
| 392 | |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 393 | using GeometryEffectAttacherT = |
| 394 | std::vector<sk_sp<sksg::GeometryNode>> (*)(const Json::Value&, |
| 395 | AttachContext*, |
| 396 | std::vector<sk_sp<sksg::GeometryNode>>&&); |
| 397 | static constexpr GeometryEffectAttacherT gGeometryEffectAttachers[] = { |
| 398 | AttachMergeGeometryEffect, |
Florin Malita | 51b8c89 | 2018-01-07 08:54:24 -0500 | [diff] [blame] | 399 | AttachTrimGeometryEffect, |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 400 | }; |
| 401 | |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 402 | enum class ShapeType { |
| 403 | kGeometry, |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 404 | kGeometryEffect, |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 405 | kPaint, |
| 406 | kGroup, |
Florin Malita | dacc02b | 2017-12-31 09:12:31 -0500 | [diff] [blame] | 407 | kTransform, |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 408 | }; |
| 409 | |
| 410 | struct ShapeInfo { |
| 411 | const char* fTypeString; |
| 412 | ShapeType fShapeType; |
| 413 | uint32_t fAttacherIndex; // index into respective attacher tables |
| 414 | }; |
| 415 | |
| 416 | const ShapeInfo* FindShapeInfo(const Json::Value& shape) { |
| 417 | static constexpr ShapeInfo gShapeInfo[] = { |
Florin Malita | fbc13f1 | 2018-01-04 10:26:35 -0500 | [diff] [blame] | 418 | { "el", ShapeType::kGeometry , 2 }, // ellipse -> AttachEllipseGeometry |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 419 | { "fl", ShapeType::kPaint , 0 }, // fill -> AttachFillPaint |
| 420 | { "gr", ShapeType::kGroup , 0 }, // group -> AttachShapeGroup |
| 421 | { "mm", ShapeType::kGeometryEffect, 0 }, // merge -> AttachMergeGeometryEffect |
Florin Malita | 02a32b0 | 2018-01-04 11:27:09 -0500 | [diff] [blame] | 422 | { "rc", ShapeType::kGeometry , 1 }, // rrect -> AttachRRectGeometry |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 423 | { "sh", ShapeType::kGeometry , 0 }, // shape -> AttachPathGeometry |
Florin Malita | 02a32b0 | 2018-01-04 11:27:09 -0500 | [diff] [blame] | 424 | { "sr", ShapeType::kGeometry , 3 }, // polystar -> AttachPolyStarGeometry |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 425 | { "st", ShapeType::kPaint , 1 }, // stroke -> AttachStrokePaint |
Florin Malita | 51b8c89 | 2018-01-07 08:54:24 -0500 | [diff] [blame] | 426 | { "tm", ShapeType::kGeometryEffect, 1 }, // trim -> AttachTrimGeometryEffect |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 427 | { "tr", ShapeType::kTransform , 0 }, // transform -> In-place handler |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 428 | }; |
| 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 | |
| 449 | sk_sp<sksg::RenderNode> AttachShape(const Json::Value& shapeArray, AttachContext* ctx) { |
| 450 | if (!shapeArray.isArray()) |
| 451 | return nullptr; |
| 452 | |
Florin Malita | 2a8275b | 2018-01-02 12:52:43 -0500 | [diff] [blame] | 453 | // (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 Malita | dacc02b | 2017-12-31 09:12:31 -0500 | [diff] [blame] | 471 | sk_sp<sksg::Group> shape_group = sksg::Group::Make(); |
| 472 | sk_sp<sksg::RenderNode> xformed_group = shape_group; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 473 | |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 474 | std::vector<sk_sp<sksg::GeometryNode>> geos; |
| 475 | std::vector<sk_sp<sksg::RenderNode>> draws; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 476 | |
| 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 Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 491 | case ShapeType::kGeometryEffect: { |
| 492 | SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGeometryEffectAttachers)); |
| 493 | geos = gGeometryEffectAttachers[info->fAttacherIndex](s, ctx, std::move(geos)); |
| 494 | } break; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 495 | case ShapeType::kPaint: { |
| 496 | SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gPaintAttachers)); |
| 497 | if (auto paint = gPaintAttachers[info->fAttacherIndex](s, ctx)) { |
Florin Malita | 2a8275b | 2018-01-02 12:52:43 -0500 | [diff] [blame] | 498 | for (const auto& geo : geos) { |
| 499 | draws.push_back(sksg::Draw::Make(geo, paint)); |
| 500 | } |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 501 | } |
| 502 | } break; |
| 503 | case ShapeType::kGroup: { |
| 504 | SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGroupAttachers)); |
| 505 | if (auto group = gGroupAttachers[info->fAttacherIndex](s, ctx)) { |
Florin Malita | 2a8275b | 2018-01-02 12:52:43 -0500 | [diff] [blame] | 506 | draws.push_back(std::move(group)); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 507 | } |
| 508 | } break; |
Florin Malita | dacc02b | 2017-12-31 09:12:31 -0500 | [diff] [blame] | 509 | case ShapeType::kTransform: { |
Florin Malita | 2a8275b | 2018-01-02 12:52:43 -0500 | [diff] [blame] | 510 | // TODO: BM appears to transform the geometry, not the draw op itself. |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 511 | if (auto matrix = AttachMatrix(s, ctx, nullptr)) { |
| 512 | xformed_group = sksg::Transform::Make(std::move(xformed_group), |
| 513 | std::move(matrix)); |
| 514 | } |
Florin Malita | dacc02b | 2017-12-31 09:12:31 -0500 | [diff] [blame] | 515 | } break; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
Florin Malita | 2a8275b | 2018-01-02 12:52:43 -0500 | [diff] [blame] | 519 | if (draws.empty()) { |
| 520 | return nullptr; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 521 | } |
| 522 | |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 523 | for (auto draw = draws.rbegin(); draw != draws.rend(); ++draw) { |
| 524 | shape_group->addChild(std::move(*draw)); |
Florin Malita | 2a8275b | 2018-01-02 12:52:43 -0500 | [diff] [blame] | 525 | } |
| 526 | |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 527 | LOG("** Attached shape: %zd draws.\n", draws.size()); |
Florin Malita | dacc02b | 2017-12-31 09:12:31 -0500 | [diff] [blame] | 528 | return xformed_group; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | sk_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 | |
| 550 | sk_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 Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame^] | 557 | sk_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 | |
| 578 | sk_sp<sksg::RenderNode> AttachImageLayer(const Json::Value& layer, AttachContext* ctx) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 579 | SkASSERT(layer.isObject()); |
| 580 | |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame^] | 581 | 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 Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | sk_sp<sksg::RenderNode> AttachNullLayer(const Json::Value& layer, AttachContext*) { |
| 597 | SkASSERT(layer.isObject()); |
| 598 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 599 | // Null layers are used solely to drive dependent transforms, |
| 600 | // but we use free-floating sksg::Matrices for that purpose. |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 601 | return nullptr; |
| 602 | } |
| 603 | |
| 604 | sk_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 | |
| 612 | sk_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 Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 619 | struct 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 | |
| 675 | sk_sp<sksg::RenderNode> AttachLayer(const Json::Value& jlayer, |
| 676 | AttachLayerContext* layerCtx) { |
| 677 | if (!jlayer.isObject()) |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 678 | 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 Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 690 | int type = ParseInt(jlayer["ty"], -1); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 691 | if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gLayerAttachers))) { |
| 692 | return nullptr; |
| 693 | } |
| 694 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 695 | 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 Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | sk_sp<sksg::RenderNode> AttachComposition(const Json::Value& comp, AttachContext* ctx) { |
| 704 | if (!comp.isObject()) |
| 705 | return nullptr; |
| 706 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 707 | const auto& jlayers = comp["layers"]; |
| 708 | if (!jlayers.isArray()) |
| 709 | return nullptr; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 710 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 711 | 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 Malita | 2a8275b | 2018-01-02 12:52:43 -0500 | [diff] [blame] | 716 | layers.push_back(std::move(layer_fragment)); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | |
Florin Malita | 2a8275b | 2018-01-02 12:52:43 -0500 | [diff] [blame] | 720 | 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 Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 733 | return comp_group; |
| 734 | } |
| 735 | |
| 736 | } // namespace |
| 737 | |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame^] | 738 | std::unique_ptr<Animation> Animation::Make(SkStream* stream, const ResourceProvider& res) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 739 | 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 Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame^] | 772 | return std::unique_ptr<Animation>(new Animation(res, std::move(version), size, fps, json)); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 773 | } |
| 774 | |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame^] | 775 | std::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 | |
| 801 | Animation::Animation(const ResourceProvider& resources, |
| 802 | SkString version, const SkSize& size, SkScalar fps, const Json::Value& json) |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 803 | : 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 Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame^] | 818 | AttachContext ctx = { resources, assets, fAnimators }; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 819 | fDom = AttachComposition(json, &ctx); |
| 820 | |
| 821 | LOG("** Attached %d animators\n", fAnimators.count()); |
| 822 | } |
| 823 | |
| 824 | Animation::~Animation() = default; |
| 825 | |
Mike Reed | 2985987 | 2018-01-08 08:25:27 -0500 | [diff] [blame] | 826 | void Animation::render(SkCanvas* canvas, const SkRect* dstR) const { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 827 | if (!fDom) |
| 828 | return; |
| 829 | |
| 830 | sksg::InvalidationController ic; |
| 831 | fDom->revalidate(&ic, SkMatrix::I()); |
| 832 | |
| 833 | // TODO: proper inval |
Mike Reed | 2985987 | 2018-01-08 08:25:27 -0500 | [diff] [blame] | 834 | 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 Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 840 | 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 | |
| 858 | void 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 |