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" |
Florin Malita | 0e66fba | 2018-01-09 17:10:18 -0500 | [diff] [blame] | 19 | #include "SkParse.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 20 | #include "SkPath.h" |
| 21 | #include "SkPoint.h" |
| 22 | #include "SkSGColor.h" |
| 23 | #include "SkSGDraw.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 24 | #include "SkSGGroup.h" |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 25 | #include "SkSGImage.h" |
| 26 | #include "SkSGInvalidationController.h" |
Florin Malita | 5f9102f | 2018-01-10 13:36:22 -0500 | [diff] [blame^] | 27 | #include "SkSGMaskEffect.h" |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 28 | #include "SkSGMerge.h" |
Florin Malita | c003417 | 2018-01-08 16:42:59 -0500 | [diff] [blame] | 29 | #include "SkSGOpacityEffect.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 30 | #include "SkSGPath.h" |
Florin Malita | 2e1d7e2 | 2018-01-02 10:40:00 -0500 | [diff] [blame] | 31 | #include "SkSGRect.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 32 | #include "SkSGTransform.h" |
Florin Malita | 51b8c89 | 2018-01-07 08:54:24 -0500 | [diff] [blame] | 33 | #include "SkSGTrimEffect.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 34 | #include "SkStream.h" |
| 35 | #include "SkTArray.h" |
| 36 | #include "SkTHash.h" |
| 37 | |
| 38 | #include <cmath> |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 39 | #include <unordered_map> |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 40 | #include <vector> |
| 41 | |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 42 | #include "stdlib.h" |
| 43 | |
| 44 | namespace skotty { |
| 45 | |
| 46 | namespace { |
| 47 | |
| 48 | using AssetMap = SkTHashMap<SkString, const Json::Value*>; |
| 49 | |
| 50 | struct AttachContext { |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 51 | const ResourceProvider& fResources; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 52 | const AssetMap& fAssets; |
| 53 | SkTArray<std::unique_ptr<AnimatorBase>>& fAnimators; |
| 54 | }; |
| 55 | |
| 56 | bool 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 Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 64 | template <typename ValT, typename NodeT> |
| 65 | bool BindProperty(const Json::Value& jprop, AttachContext* ctx, const sk_sp<NodeT>& node, |
| 66 | typename Animator<ValT, NodeT>::ApplyFuncT&& apply) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 67 | if (!jprop.isObject()) |
| 68 | return false; |
| 69 | |
Florin Malita | 95448a9 | 2018-01-08 10:15:12 -0500 | [diff] [blame] | 70 | 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 Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 76 | ValT val; |
| 77 | if (ValueTraits<ValT>::Parse(jpropK, &val)) { |
Florin Malita | 95448a9 | 2018-01-08 10:15:12 -0500 | [diff] [blame] | 78 | // Static property. |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 79 | apply(node.get(), val); |
Florin Malita | 95448a9 | 2018-01-08 10:15:12 -0500 | [diff] [blame] | 80 | return true; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 81 | } |
| 82 | |
Florin Malita | 95448a9 | 2018-01-08 10:15:12 -0500 | [diff] [blame] | 83 | if (!jpropA.isNull()) { |
| 84 | return LogFail(jprop, "Could not parse (explicit) static property"); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 85 | } |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 86 | } |
| 87 | |
Florin Malita | 95448a9 | 2018-01-08 10:15:12 -0500 | [diff] [blame] | 88 | // Keyframe property. |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 89 | using AnimatorT = Animator<ValT, NodeT>; |
| 90 | auto animator = AnimatorT::Make(ParseFrames<ValT>(jpropK), node, std::move(apply)); |
Florin Malita | 95448a9 | 2018-01-08 10:15:12 -0500 | [diff] [blame] | 91 | |
| 92 | if (!animator) { |
| 93 | return LogFail(jprop, "Could not parse keyframed property"); |
| 94 | } |
| 95 | |
| 96 | ctx->fAnimators.push_back(std::move(animator)); |
| 97 | |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 98 | return true; |
| 99 | } |
| 100 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 101 | sk_sp<sksg::Matrix> AttachMatrix(const Json::Value& t, AttachContext* ctx, |
| 102 | sk_sp<sksg::Matrix> parentMatrix) { |
| 103 | if (!t.isObject()) |
| 104 | return nullptr; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 105 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 106 | auto matrix = sksg::Matrix::Make(SkMatrix::I(), std::move(parentMatrix)); |
| 107 | auto composite = sk_make_sp<CompositeTransform>(matrix); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 108 | auto anchor_attached = BindProperty<VectorValue>(t["a"], ctx, composite, |
| 109 | [](CompositeTransform* node, const VectorValue& a) { |
| 110 | node->setAnchorPoint(ValueTraits<VectorValue>::As<SkPoint>(a)); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 111 | }); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 112 | auto position_attached = BindProperty<VectorValue>(t["p"], ctx, composite, |
| 113 | [](CompositeTransform* node, const VectorValue& p) { |
| 114 | node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p)); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 115 | }); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 116 | auto scale_attached = BindProperty<VectorValue>(t["s"], ctx, composite, |
| 117 | [](CompositeTransform* node, const VectorValue& s) { |
| 118 | node->setScale(ValueTraits<VectorValue>::As<SkVector>(s)); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 119 | }); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 120 | auto rotation_attached = BindProperty<ScalarValue>(t["r"], ctx, composite, |
| 121 | [](CompositeTransform* node, const ScalarValue& r) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 122 | node->setRotation(r); |
| 123 | }); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 124 | auto skew_attached = BindProperty<ScalarValue>(t["sk"], ctx, composite, |
| 125 | [](CompositeTransform* node, const ScalarValue& sk) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 126 | node->setSkew(sk); |
| 127 | }); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 128 | auto skewaxis_attached = BindProperty<ScalarValue>(t["sa"], ctx, composite, |
| 129 | [](CompositeTransform* node, const ScalarValue& sa) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 130 | 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 Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 140 | return nullptr; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 141 | } |
| 142 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 143 | return matrix; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 144 | } |
| 145 | |
Florin Malita | c003417 | 2018-01-08 16:42:59 -0500 | [diff] [blame] | 146 | sk_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 Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 162 | BindProperty<ScalarValue>(opacity, ctx, opacityNode, |
| 163 | [](sksg::OpacityEffect* node, const ScalarValue& o) { |
Florin Malita | c003417 | 2018-01-08 16:42:59 -0500 | [diff] [blame] | 164 | // BM opacity is [0..100] |
| 165 | node->setOpacity(o * 0.01f); |
| 166 | }); |
| 167 | |
| 168 | return opacityNode; |
| 169 | } |
| 170 | |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 171 | sk_sp<sksg::RenderNode> AttachShape(const Json::Value&, AttachContext* ctx); |
| 172 | sk_sp<sksg::RenderNode> AttachComposition(const Json::Value&, AttachContext* ctx); |
| 173 | |
| 174 | sk_sp<sksg::RenderNode> AttachShapeGroup(const Json::Value& jgroup, AttachContext* ctx) { |
| 175 | SkASSERT(jgroup.isObject()); |
| 176 | |
| 177 | return AttachShape(jgroup["it"], ctx); |
| 178 | } |
| 179 | |
| 180 | sk_sp<sksg::GeometryNode> AttachPathGeometry(const Json::Value& jpath, AttachContext* ctx) { |
| 181 | SkASSERT(jpath.isObject()); |
| 182 | |
| 183 | auto path_node = sksg::Path::Make(); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 184 | auto path_attached = BindProperty<ShapeValue>(jpath["ks"], ctx, path_node, |
| 185 | [](sksg::Path* node, const ShapeValue& p) { node->setPath(p); }); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 186 | |
| 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 Malita | 2e1d7e2 | 2018-01-02 10:40:00 -0500 | [diff] [blame] | 193 | sk_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 Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 199 | 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 Malita | 2e1d7e2 | 2018-01-02 10:40:00 -0500 | [diff] [blame] | 211 | |
| 212 | if (!p_attached && !s_attached && !r_attached) { |
| 213 | return nullptr; |
| 214 | } |
| 215 | |
Florin Malita | fbc13f1 | 2018-01-04 10:26:35 -0500 | [diff] [blame] | 216 | LOG("** Attached (r)rect geometry\n"); |
| 217 | |
| 218 | return rect_node; |
| 219 | } |
| 220 | |
| 221 | sk_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 Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 227 | 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 Malita | fbc13f1 | 2018-01-04 10:26:35 -0500 | [diff] [blame] | 237 | |
| 238 | if (!p_attached && !s_attached) { |
| 239 | return nullptr; |
| 240 | } |
| 241 | |
| 242 | LOG("** Attached ellipse geometry\n"); |
| 243 | |
Florin Malita | 2e1d7e2 | 2018-01-02 10:40:00 -0500 | [diff] [blame] | 244 | return rect_node; |
| 245 | } |
| 246 | |
Florin Malita | 02a32b0 | 2018-01-04 11:27:09 -0500 | [diff] [blame] | 247 | sk_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 Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 264 | 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 Malita | 9661b98 | 2018-01-06 14:25:49 -0500 | [diff] [blame] | 278 | node->setOuterRadius(otr); |
| 279 | }); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 280 | BindProperty<ScalarValue>(jstar["is"], ctx, composite, |
| 281 | [](CompositePolyStar* node, const ScalarValue& is) { |
Florin Malita | 9661b98 | 2018-01-06 14:25:49 -0500 | [diff] [blame] | 282 | node->setInnerRoundness(is); |
| 283 | }); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 284 | BindProperty<ScalarValue>(jstar["os"], ctx, composite, |
| 285 | [](CompositePolyStar* node, const ScalarValue& os) { |
Florin Malita | 9661b98 | 2018-01-06 14:25:49 -0500 | [diff] [blame] | 286 | node->setOuterRoundness(os); |
| 287 | }); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 288 | BindProperty<ScalarValue>(jstar["r"], ctx, composite, |
| 289 | [](CompositePolyStar* node, const ScalarValue& r) { |
| 290 | node->setRotation(r); |
| 291 | }); |
Florin Malita | 02a32b0 | 2018-01-04 11:27:09 -0500 | [diff] [blame] | 292 | |
| 293 | return path_node; |
| 294 | } |
| 295 | |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 296 | sk_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 Malita | dcbb2db | 2018-01-09 13:11:56 -0500 | [diff] [blame] | 302 | 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 Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 305 | node->setColor(ValueTraits<VectorValue>::As<SkColor>(c)); |
| 306 | }); |
Florin Malita | dcbb2db | 2018-01-09 13:11:56 -0500 | [diff] [blame] | 307 | auto opacity_attached = BindProperty<ScalarValue>(obj["o"], ctx, composite, |
| 308 | [](CompositeColor* node, const ScalarValue& o) { |
| 309 | node->setOpacity(o); |
| 310 | }); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 311 | |
Florin Malita | dcbb2db | 2018-01-09 13:11:56 -0500 | [diff] [blame] | 312 | return (color_attached || opacity_attached) ? color_node : nullptr; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | sk_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 | |
| 325 | sk_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 Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 336 | auto width_attached = BindProperty<ScalarValue>(jstroke["w"], ctx, stroke_node, |
| 337 | [](sksg::Color* node, const ScalarValue& w) { |
| 338 | node->setStrokeWidth(w); |
| 339 | }); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 340 | 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 Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 364 | std::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 Malita | 51b8c89 | 2018-01-07 08:54:24 -0500 | [diff] [blame] | 376 | const auto mode = gModes[SkTPin<int>(ParseInt(jmerge["mm"], 1) - 1, |
| 377 | 0, SK_ARRAY_COUNT(gModes) - 1)]; |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 378 | 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 Malita | 51b8c89 | 2018-01-07 08:54:24 -0500 | [diff] [blame] | 385 | std::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 Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 408 | BindProperty<ScalarValue>(jtrim["s"], ctx, trim, |
| 409 | [](sksg::TrimEffect* node, const ScalarValue& s) { |
Florin Malita | 51b8c89 | 2018-01-07 08:54:24 -0500 | [diff] [blame] | 410 | node->setStart(s * 0.01f); |
| 411 | }); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 412 | BindProperty<ScalarValue>(jtrim["e"], ctx, trim, |
| 413 | [](sksg::TrimEffect* node, const ScalarValue& e) { |
Florin Malita | 51b8c89 | 2018-01-07 08:54:24 -0500 | [diff] [blame] | 414 | node->setEnd(e * 0.01f); |
| 415 | }); |
| 416 | // TODO: "offset" doesn't currently work the same as BM - figure out what's going on. |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 417 | BindProperty<ScalarValue>(jtrim["o"], ctx, trim, |
| 418 | [](sksg::TrimEffect* node, const ScalarValue& o) { |
Florin Malita | 51b8c89 | 2018-01-07 08:54:24 -0500 | [diff] [blame] | 419 | node->setOffset(o * 0.01f); |
| 420 | }); |
| 421 | } |
| 422 | |
| 423 | return trimmed; |
| 424 | } |
| 425 | |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 426 | using GeometryAttacherT = sk_sp<sksg::GeometryNode> (*)(const Json::Value&, AttachContext*); |
| 427 | static constexpr GeometryAttacherT gGeometryAttachers[] = { |
| 428 | AttachPathGeometry, |
Florin Malita | 2e1d7e2 | 2018-01-02 10:40:00 -0500 | [diff] [blame] | 429 | AttachRRectGeometry, |
Florin Malita | fbc13f1 | 2018-01-04 10:26:35 -0500 | [diff] [blame] | 430 | AttachEllipseGeometry, |
Florin Malita | 02a32b0 | 2018-01-04 11:27:09 -0500 | [diff] [blame] | 431 | AttachPolystarGeometry, |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 432 | }; |
| 433 | |
| 434 | using PaintAttacherT = sk_sp<sksg::PaintNode> (*)(const Json::Value&, AttachContext*); |
| 435 | static constexpr PaintAttacherT gPaintAttachers[] = { |
| 436 | AttachFillPaint, |
| 437 | AttachStrokePaint, |
| 438 | }; |
| 439 | |
| 440 | using GroupAttacherT = sk_sp<sksg::RenderNode> (*)(const Json::Value&, AttachContext*); |
| 441 | static constexpr GroupAttacherT gGroupAttachers[] = { |
| 442 | AttachShapeGroup, |
| 443 | }; |
| 444 | |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 445 | using GeometryEffectAttacherT = |
| 446 | std::vector<sk_sp<sksg::GeometryNode>> (*)(const Json::Value&, |
| 447 | AttachContext*, |
| 448 | std::vector<sk_sp<sksg::GeometryNode>>&&); |
| 449 | static constexpr GeometryEffectAttacherT gGeometryEffectAttachers[] = { |
| 450 | AttachMergeGeometryEffect, |
Florin Malita | 51b8c89 | 2018-01-07 08:54:24 -0500 | [diff] [blame] | 451 | AttachTrimGeometryEffect, |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 452 | }; |
| 453 | |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 454 | enum class ShapeType { |
| 455 | kGeometry, |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 456 | kGeometryEffect, |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 457 | kPaint, |
| 458 | kGroup, |
Florin Malita | dacc02b | 2017-12-31 09:12:31 -0500 | [diff] [blame] | 459 | kTransform, |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 460 | }; |
| 461 | |
| 462 | struct ShapeInfo { |
| 463 | const char* fTypeString; |
| 464 | ShapeType fShapeType; |
| 465 | uint32_t fAttacherIndex; // index into respective attacher tables |
| 466 | }; |
| 467 | |
| 468 | const ShapeInfo* FindShapeInfo(const Json::Value& shape) { |
| 469 | static constexpr ShapeInfo gShapeInfo[] = { |
Florin Malita | fbc13f1 | 2018-01-04 10:26:35 -0500 | [diff] [blame] | 470 | { "el", ShapeType::kGeometry , 2 }, // ellipse -> AttachEllipseGeometry |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 471 | { "fl", ShapeType::kPaint , 0 }, // fill -> AttachFillPaint |
| 472 | { "gr", ShapeType::kGroup , 0 }, // group -> AttachShapeGroup |
| 473 | { "mm", ShapeType::kGeometryEffect, 0 }, // merge -> AttachMergeGeometryEffect |
Florin Malita | 02a32b0 | 2018-01-04 11:27:09 -0500 | [diff] [blame] | 474 | { "rc", ShapeType::kGeometry , 1 }, // rrect -> AttachRRectGeometry |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 475 | { "sh", ShapeType::kGeometry , 0 }, // shape -> AttachPathGeometry |
Florin Malita | 02a32b0 | 2018-01-04 11:27:09 -0500 | [diff] [blame] | 476 | { "sr", ShapeType::kGeometry , 3 }, // polystar -> AttachPolyStarGeometry |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 477 | { "st", ShapeType::kPaint , 1 }, // stroke -> AttachStrokePaint |
Florin Malita | 51b8c89 | 2018-01-07 08:54:24 -0500 | [diff] [blame] | 478 | { "tm", ShapeType::kGeometryEffect, 1 }, // trim -> AttachTrimGeometryEffect |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 479 | { "tr", ShapeType::kTransform , 0 }, // transform -> In-place handler |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 480 | }; |
| 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 | |
| 501 | sk_sp<sksg::RenderNode> AttachShape(const Json::Value& shapeArray, AttachContext* ctx) { |
| 502 | if (!shapeArray.isArray()) |
| 503 | return nullptr; |
| 504 | |
Florin Malita | 2a8275b | 2018-01-02 12:52:43 -0500 | [diff] [blame] | 505 | // (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 Malita | dacc02b | 2017-12-31 09:12:31 -0500 | [diff] [blame] | 523 | sk_sp<sksg::Group> shape_group = sksg::Group::Make(); |
| 524 | sk_sp<sksg::RenderNode> xformed_group = shape_group; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 525 | |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 526 | std::vector<sk_sp<sksg::GeometryNode>> geos; |
| 527 | std::vector<sk_sp<sksg::RenderNode>> draws; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 528 | |
| 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 Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 543 | case ShapeType::kGeometryEffect: { |
| 544 | SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGeometryEffectAttachers)); |
| 545 | geos = gGeometryEffectAttachers[info->fAttacherIndex](s, ctx, std::move(geos)); |
| 546 | } break; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 547 | case ShapeType::kPaint: { |
| 548 | SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gPaintAttachers)); |
| 549 | if (auto paint = gPaintAttachers[info->fAttacherIndex](s, ctx)) { |
Florin Malita | 2a8275b | 2018-01-02 12:52:43 -0500 | [diff] [blame] | 550 | for (const auto& geo : geos) { |
| 551 | draws.push_back(sksg::Draw::Make(geo, paint)); |
| 552 | } |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 553 | } |
| 554 | } break; |
| 555 | case ShapeType::kGroup: { |
| 556 | SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGroupAttachers)); |
| 557 | if (auto group = gGroupAttachers[info->fAttacherIndex](s, ctx)) { |
Florin Malita | 2a8275b | 2018-01-02 12:52:43 -0500 | [diff] [blame] | 558 | draws.push_back(std::move(group)); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 559 | } |
| 560 | } break; |
Florin Malita | dacc02b | 2017-12-31 09:12:31 -0500 | [diff] [blame] | 561 | case ShapeType::kTransform: { |
Florin Malita | 2a8275b | 2018-01-02 12:52:43 -0500 | [diff] [blame] | 562 | // TODO: BM appears to transform the geometry, not the draw op itself. |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 563 | if (auto matrix = AttachMatrix(s, ctx, nullptr)) { |
| 564 | xformed_group = sksg::Transform::Make(std::move(xformed_group), |
| 565 | std::move(matrix)); |
| 566 | } |
Florin Malita | c003417 | 2018-01-08 16:42:59 -0500 | [diff] [blame] | 567 | xformed_group = AttachOpacity(s, ctx, std::move(xformed_group)); |
Florin Malita | dacc02b | 2017-12-31 09:12:31 -0500 | [diff] [blame] | 568 | } break; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
Florin Malita | 2a8275b | 2018-01-02 12:52:43 -0500 | [diff] [blame] | 572 | if (draws.empty()) { |
| 573 | return nullptr; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 574 | } |
| 575 | |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 576 | for (auto draw = draws.rbegin(); draw != draws.rend(); ++draw) { |
| 577 | shape_group->addChild(std::move(*draw)); |
Florin Malita | 2a8275b | 2018-01-02 12:52:43 -0500 | [diff] [blame] | 578 | } |
| 579 | |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 580 | LOG("** Attached shape: %zd draws.\n", draws.size()); |
Florin Malita | dacc02b | 2017-12-31 09:12:31 -0500 | [diff] [blame] | 581 | return xformed_group; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | sk_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 Malita | 0e66fba | 2018-01-09 17:10:18 -0500 | [diff] [blame] | 603 | sk_sp<sksg::RenderNode> AttachSolidLayer(const Json::Value& jlayer, AttachContext*) { |
| 604 | SkASSERT(jlayer.isObject()); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 605 | |
Florin Malita | 0e66fba | 2018-01-09 17:10:18 -0500 | [diff] [blame] | 606 | 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 Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 621 | } |
| 622 | |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 623 | sk_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 | |
| 644 | sk_sp<sksg::RenderNode> AttachImageLayer(const Json::Value& layer, AttachContext* ctx) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 645 | SkASSERT(layer.isObject()); |
| 646 | |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 647 | 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 Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | sk_sp<sksg::RenderNode> AttachNullLayer(const Json::Value& layer, AttachContext*) { |
| 663 | SkASSERT(layer.isObject()); |
| 664 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 665 | // Null layers are used solely to drive dependent transforms, |
| 666 | // but we use free-floating sksg::Matrices for that purpose. |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 667 | return nullptr; |
| 668 | } |
| 669 | |
| 670 | sk_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 | |
| 678 | sk_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 Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 685 | struct 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 Malita | 5f9102f | 2018-01-10 13:36:22 -0500 | [diff] [blame^] | 693 | sk_sp<sksg::RenderNode> fCurrentMatte; |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 694 | |
| 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 | |
| 742 | sk_sp<sksg::RenderNode> AttachLayer(const Json::Value& jlayer, |
| 743 | AttachLayerContext* layerCtx) { |
| 744 | if (!jlayer.isObject()) |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 745 | 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 Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 757 | int type = ParseInt(jlayer["ty"], -1); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 758 | if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gLayerAttachers))) { |
| 759 | return nullptr; |
| 760 | } |
| 761 | |
Florin Malita | 71cba8f | 2018-01-09 08:07:14 -0500 | [diff] [blame] | 762 | // 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 Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 770 | |
Florin Malita | 71cba8f | 2018-01-09 08:07:14 -0500 | [diff] [blame] | 771 | // 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 Malita | a6dd752 | 2018-01-09 08:46:52 -0500 | [diff] [blame] | 779 | void tick(float t) override { |
Florin Malita | 71cba8f | 2018-01-09 08:07:14 -0500 | [diff] [blame] | 780 | // 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 Malita | 5f9102f | 2018-01-10 13:36:22 -0500 | [diff] [blame^] | 800 | 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 Malita | 71cba8f | 2018-01-09 08:07:14 -0500 | [diff] [blame] | 811 | return layerControl; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | sk_sp<sksg::RenderNode> AttachComposition(const Json::Value& comp, AttachContext* ctx) { |
| 815 | if (!comp.isObject()) |
| 816 | return nullptr; |
| 817 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 818 | const auto& jlayers = comp["layers"]; |
| 819 | if (!jlayers.isArray()) |
| 820 | return nullptr; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 821 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 822 | 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 Malita | 2a8275b | 2018-01-02 12:52:43 -0500 | [diff] [blame] | 827 | layers.push_back(std::move(layer_fragment)); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | |
Florin Malita | 2a8275b | 2018-01-02 12:52:43 -0500 | [diff] [blame] | 831 | 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 Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 844 | return comp_group; |
| 845 | } |
| 846 | |
| 847 | } // namespace |
| 848 | |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 849 | std::unique_ptr<Animation> Animation::Make(SkStream* stream, const ResourceProvider& res) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 850 | 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 Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 883 | 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] | 884 | } |
| 885 | |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 886 | std::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 | |
| 912 | Animation::Animation(const ResourceProvider& resources, |
| 913 | SkString version, const SkSize& size, SkScalar fps, const Json::Value& json) |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 914 | : 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 Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 929 | AttachContext ctx = { resources, assets, fAnimators }; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 930 | fDom = AttachComposition(json, &ctx); |
| 931 | |
Florin Malita | db38573 | 2018-01-09 12:19:32 -0500 | [diff] [blame] | 932 | // In case the client calls render before the first tick. |
| 933 | this->animationTick(0); |
| 934 | |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 935 | LOG("** Attached %d animators\n", fAnimators.count()); |
| 936 | } |
| 937 | |
| 938 | Animation::~Animation() = default; |
| 939 | |
Mike Reed | 2985987 | 2018-01-08 08:25:27 -0500 | [diff] [blame] | 940 | void Animation::render(SkCanvas* canvas, const SkRect* dstR) const { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 941 | if (!fDom) |
| 942 | return; |
| 943 | |
| 944 | sksg::InvalidationController ic; |
| 945 | fDom->revalidate(&ic, SkMatrix::I()); |
| 946 | |
| 947 | // TODO: proper inval |
Mike Reed | 2985987 | 2018-01-08 08:25:27 -0500 | [diff] [blame] | 948 | 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 Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 954 | 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 | |
| 972 | void 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 |