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 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 8 | #include "Skottie.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 9 | |
| 10 | #include "SkCanvas.h" |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 11 | #include "SkottieAnimator.h" |
| 12 | #include "SkottiePriv.h" |
| 13 | #include "SkottieProperties.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 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 | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 24 | #include "SkSGGeometryTransform.h" |
Florin Malita | 6aaee59 | 2018-01-12 12:25:09 -0500 | [diff] [blame] | 25 | #include "SkSGGradient.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 26 | #include "SkSGGroup.h" |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 27 | #include "SkSGImage.h" |
| 28 | #include "SkSGInvalidationController.h" |
Florin Malita | 5f9102f | 2018-01-10 13:36:22 -0500 | [diff] [blame] | 29 | #include "SkSGMaskEffect.h" |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 30 | #include "SkSGMerge.h" |
Florin Malita | c003417 | 2018-01-08 16:42:59 -0500 | [diff] [blame] | 31 | #include "SkSGOpacityEffect.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 32 | #include "SkSGPath.h" |
Florin Malita | 2e1d7e2 | 2018-01-02 10:40:00 -0500 | [diff] [blame] | 33 | #include "SkSGRect.h" |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 34 | #include "SkSGScene.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 35 | #include "SkSGTransform.h" |
Florin Malita | 51b8c89 | 2018-01-07 08:54:24 -0500 | [diff] [blame] | 36 | #include "SkSGTrimEffect.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 37 | #include "SkStream.h" |
| 38 | #include "SkTArray.h" |
| 39 | #include "SkTHash.h" |
| 40 | |
| 41 | #include <cmath> |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 42 | #include <unordered_map> |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 43 | #include <vector> |
| 44 | |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 45 | #include "stdlib.h" |
| 46 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 47 | namespace skottie { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 48 | |
| 49 | namespace { |
| 50 | |
| 51 | using AssetMap = SkTHashMap<SkString, const Json::Value*>; |
| 52 | |
| 53 | struct AttachContext { |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 54 | const ResourceProvider& fResources; |
| 55 | const AssetMap& fAssets; |
| 56 | sksg::Scene::AnimatorList& fAnimators; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | bool LogFail(const Json::Value& json, const char* msg) { |
| 60 | const auto dump = json.toStyledString(); |
| 61 | LOG("!! %s: %s", msg, dump.c_str()); |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | // This is the workhorse for binding properties: depending on whether the property is animated, |
| 66 | // it will either apply immediately or instantiate and attach a keyframe animator. |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 67 | template <typename ValT, typename NodeT> |
| 68 | bool BindProperty(const Json::Value& jprop, AttachContext* ctx, const sk_sp<NodeT>& node, |
| 69 | typename Animator<ValT, NodeT>::ApplyFuncT&& apply) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 70 | if (!jprop.isObject()) |
| 71 | return false; |
| 72 | |
Florin Malita | 95448a9 | 2018-01-08 10:15:12 -0500 | [diff] [blame] | 73 | const auto& jpropA = jprop["a"]; |
| 74 | const auto& jpropK = jprop["k"]; |
| 75 | |
| 76 | // Older Json versions don't have an "a" animation marker. |
| 77 | // For those, we attempt to parse both ways. |
| 78 | if (jpropA.isNull() || !ParseBool(jpropA, "false")) { |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 79 | ValT val; |
| 80 | if (ValueTraits<ValT>::Parse(jpropK, &val)) { |
Florin Malita | 95448a9 | 2018-01-08 10:15:12 -0500 | [diff] [blame] | 81 | // Static property. |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 82 | apply(node.get(), val); |
Florin Malita | 95448a9 | 2018-01-08 10:15:12 -0500 | [diff] [blame] | 83 | return true; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 84 | } |
| 85 | |
Florin Malita | 95448a9 | 2018-01-08 10:15:12 -0500 | [diff] [blame] | 86 | if (!jpropA.isNull()) { |
| 87 | return LogFail(jprop, "Could not parse (explicit) static property"); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 88 | } |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 89 | } |
| 90 | |
Florin Malita | 95448a9 | 2018-01-08 10:15:12 -0500 | [diff] [blame] | 91 | // Keyframe property. |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 92 | using AnimatorT = Animator<ValT, NodeT>; |
| 93 | auto animator = AnimatorT::Make(ParseFrames<ValT>(jpropK), node, std::move(apply)); |
Florin Malita | 95448a9 | 2018-01-08 10:15:12 -0500 | [diff] [blame] | 94 | |
| 95 | if (!animator) { |
| 96 | return LogFail(jprop, "Could not parse keyframed property"); |
| 97 | } |
| 98 | |
| 99 | ctx->fAnimators.push_back(std::move(animator)); |
| 100 | |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 101 | return true; |
| 102 | } |
| 103 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 104 | sk_sp<sksg::Matrix> AttachMatrix(const Json::Value& t, AttachContext* ctx, |
| 105 | sk_sp<sksg::Matrix> parentMatrix) { |
| 106 | if (!t.isObject()) |
| 107 | return nullptr; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 108 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 109 | auto matrix = sksg::Matrix::Make(SkMatrix::I(), std::move(parentMatrix)); |
| 110 | auto composite = sk_make_sp<CompositeTransform>(matrix); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 111 | auto anchor_attached = BindProperty<VectorValue>(t["a"], ctx, composite, |
| 112 | [](CompositeTransform* node, const VectorValue& a) { |
| 113 | node->setAnchorPoint(ValueTraits<VectorValue>::As<SkPoint>(a)); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 114 | }); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 115 | auto position_attached = BindProperty<VectorValue>(t["p"], ctx, composite, |
| 116 | [](CompositeTransform* node, const VectorValue& p) { |
| 117 | node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p)); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 118 | }); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 119 | auto scale_attached = BindProperty<VectorValue>(t["s"], ctx, composite, |
| 120 | [](CompositeTransform* node, const VectorValue& s) { |
| 121 | node->setScale(ValueTraits<VectorValue>::As<SkVector>(s)); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 122 | }); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 123 | auto rotation_attached = BindProperty<ScalarValue>(t["r"], ctx, composite, |
| 124 | [](CompositeTransform* node, const ScalarValue& r) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 125 | node->setRotation(r); |
| 126 | }); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 127 | auto skew_attached = BindProperty<ScalarValue>(t["sk"], ctx, composite, |
| 128 | [](CompositeTransform* node, const ScalarValue& sk) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 129 | node->setSkew(sk); |
| 130 | }); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 131 | auto skewaxis_attached = BindProperty<ScalarValue>(t["sa"], ctx, composite, |
| 132 | [](CompositeTransform* node, const ScalarValue& sa) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 133 | node->setSkewAxis(sa); |
| 134 | }); |
| 135 | |
| 136 | if (!anchor_attached && |
| 137 | !position_attached && |
| 138 | !scale_attached && |
| 139 | !rotation_attached && |
| 140 | !skew_attached && |
| 141 | !skewaxis_attached) { |
| 142 | LogFail(t, "Could not parse transform"); |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 143 | return nullptr; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 144 | } |
| 145 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 146 | return matrix; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 147 | } |
| 148 | |
Florin Malita | c003417 | 2018-01-08 16:42:59 -0500 | [diff] [blame] | 149 | sk_sp<sksg::RenderNode> AttachOpacity(const Json::Value& jtransform, AttachContext* ctx, |
| 150 | sk_sp<sksg::RenderNode> childNode) { |
| 151 | if (!jtransform.isObject() || !childNode) |
| 152 | return childNode; |
| 153 | |
| 154 | // This is more peeky than other attachers, because we want to avoid redundant opacity |
| 155 | // nodes for the extremely common case of static opaciy == 100. |
| 156 | const auto& opacity = jtransform["o"]; |
| 157 | if (opacity.isObject() && |
| 158 | !ParseBool(opacity["a"], true) && |
| 159 | ParseScalar(opacity["k"], -1) == 100) { |
| 160 | // Ignoring static full opacity. |
| 161 | return childNode; |
| 162 | } |
| 163 | |
| 164 | auto opacityNode = sksg::OpacityEffect::Make(childNode); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 165 | BindProperty<ScalarValue>(opacity, ctx, opacityNode, |
| 166 | [](sksg::OpacityEffect* node, const ScalarValue& o) { |
Florin Malita | c003417 | 2018-01-08 16:42:59 -0500 | [diff] [blame] | 167 | // BM opacity is [0..100] |
| 168 | node->setOpacity(o * 0.01f); |
| 169 | }); |
| 170 | |
| 171 | return opacityNode; |
| 172 | } |
| 173 | |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 174 | sk_sp<sksg::RenderNode> AttachComposition(const Json::Value&, AttachContext* ctx); |
| 175 | |
Florin Malita | 25366fa | 2018-01-23 13:37:59 -0500 | [diff] [blame^] | 176 | sk_sp<sksg::Path> AttachPath(const Json::Value& jpath, AttachContext* ctx) { |
| 177 | auto path_node = sksg::Path::Make(); |
| 178 | return BindProperty<ShapeValue>(jpath, ctx, path_node, |
| 179 | [](sksg::Path* node, const ShapeValue& p) { node->setPath(p); }) |
| 180 | ? path_node |
| 181 | : nullptr; |
| 182 | } |
| 183 | |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 184 | sk_sp<sksg::GeometryNode> AttachPathGeometry(const Json::Value& jpath, AttachContext* ctx) { |
| 185 | SkASSERT(jpath.isObject()); |
| 186 | |
Florin Malita | 25366fa | 2018-01-23 13:37:59 -0500 | [diff] [blame^] | 187 | return AttachPath(jpath["ks"], ctx); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 188 | } |
| 189 | |
Florin Malita | 2e1d7e2 | 2018-01-02 10:40:00 -0500 | [diff] [blame] | 190 | sk_sp<sksg::GeometryNode> AttachRRectGeometry(const Json::Value& jrect, AttachContext* ctx) { |
| 191 | SkASSERT(jrect.isObject()); |
| 192 | |
| 193 | auto rect_node = sksg::RRect::Make(); |
| 194 | auto composite = sk_make_sp<CompositeRRect>(rect_node); |
| 195 | |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 196 | auto p_attached = BindProperty<VectorValue>(jrect["p"], ctx, composite, |
| 197 | [](CompositeRRect* node, const VectorValue& p) { |
| 198 | node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p)); |
| 199 | }); |
| 200 | auto s_attached = BindProperty<VectorValue>(jrect["s"], ctx, composite, |
| 201 | [](CompositeRRect* node, const VectorValue& s) { |
| 202 | node->setSize(ValueTraits<VectorValue>::As<SkSize>(s)); |
| 203 | }); |
| 204 | auto r_attached = BindProperty<ScalarValue>(jrect["r"], ctx, composite, |
| 205 | [](CompositeRRect* node, const ScalarValue& r) { |
| 206 | node->setRadius(SkSize::Make(r, r)); |
| 207 | }); |
Florin Malita | 2e1d7e2 | 2018-01-02 10:40:00 -0500 | [diff] [blame] | 208 | |
| 209 | if (!p_attached && !s_attached && !r_attached) { |
| 210 | return nullptr; |
| 211 | } |
| 212 | |
Florin Malita | fbc13f1 | 2018-01-04 10:26:35 -0500 | [diff] [blame] | 213 | LOG("** Attached (r)rect geometry\n"); |
| 214 | |
| 215 | return rect_node; |
| 216 | } |
| 217 | |
| 218 | sk_sp<sksg::GeometryNode> AttachEllipseGeometry(const Json::Value& jellipse, AttachContext* ctx) { |
| 219 | SkASSERT(jellipse.isObject()); |
| 220 | |
| 221 | auto rect_node = sksg::RRect::Make(); |
| 222 | auto composite = sk_make_sp<CompositeRRect>(rect_node); |
| 223 | |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 224 | auto p_attached = BindProperty<VectorValue>(jellipse["p"], ctx, composite, |
| 225 | [](CompositeRRect* node, const VectorValue& p) { |
| 226 | node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p)); |
| 227 | }); |
| 228 | auto s_attached = BindProperty<VectorValue>(jellipse["s"], ctx, composite, |
| 229 | [](CompositeRRect* node, const VectorValue& s) { |
| 230 | const auto sz = ValueTraits<VectorValue>::As<SkSize>(s); |
| 231 | node->setSize(sz); |
| 232 | node->setRadius(SkSize::Make(sz.width() / 2, sz.height() / 2)); |
| 233 | }); |
Florin Malita | fbc13f1 | 2018-01-04 10:26:35 -0500 | [diff] [blame] | 234 | |
| 235 | if (!p_attached && !s_attached) { |
| 236 | return nullptr; |
| 237 | } |
| 238 | |
| 239 | LOG("** Attached ellipse geometry\n"); |
| 240 | |
Florin Malita | 2e1d7e2 | 2018-01-02 10:40:00 -0500 | [diff] [blame] | 241 | return rect_node; |
| 242 | } |
| 243 | |
Florin Malita | 02a32b0 | 2018-01-04 11:27:09 -0500 | [diff] [blame] | 244 | sk_sp<sksg::GeometryNode> AttachPolystarGeometry(const Json::Value& jstar, AttachContext* ctx) { |
| 245 | SkASSERT(jstar.isObject()); |
| 246 | |
| 247 | static constexpr CompositePolyStar::Type gTypes[] = { |
| 248 | CompositePolyStar::Type::kStar, // "sy": 1 |
| 249 | CompositePolyStar::Type::kPoly, // "sy": 2 |
| 250 | }; |
| 251 | |
| 252 | const auto type = ParseInt(jstar["sy"], 0) - 1; |
| 253 | if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gTypes))) { |
| 254 | LogFail(jstar, "Unknown polystar type"); |
| 255 | return nullptr; |
| 256 | } |
| 257 | |
| 258 | auto path_node = sksg::Path::Make(); |
| 259 | auto composite = sk_make_sp<CompositePolyStar>(path_node, gTypes[type]); |
| 260 | |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 261 | BindProperty<VectorValue>(jstar["p"], ctx, composite, |
| 262 | [](CompositePolyStar* node, const VectorValue& p) { |
| 263 | node->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p)); |
| 264 | }); |
| 265 | BindProperty<ScalarValue>(jstar["pt"], ctx, composite, |
| 266 | [](CompositePolyStar* node, const ScalarValue& pt) { |
| 267 | node->setPointCount(pt); |
| 268 | }); |
| 269 | BindProperty<ScalarValue>(jstar["ir"], ctx, composite, |
| 270 | [](CompositePolyStar* node, const ScalarValue& ir) { |
| 271 | node->setInnerRadius(ir); |
| 272 | }); |
| 273 | BindProperty<ScalarValue>(jstar["or"], ctx, composite, |
| 274 | [](CompositePolyStar* node, const ScalarValue& otr) { |
Florin Malita | 9661b98 | 2018-01-06 14:25:49 -0500 | [diff] [blame] | 275 | node->setOuterRadius(otr); |
| 276 | }); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 277 | BindProperty<ScalarValue>(jstar["is"], ctx, composite, |
| 278 | [](CompositePolyStar* node, const ScalarValue& is) { |
Florin Malita | 9661b98 | 2018-01-06 14:25:49 -0500 | [diff] [blame] | 279 | node->setInnerRoundness(is); |
| 280 | }); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 281 | BindProperty<ScalarValue>(jstar["os"], ctx, composite, |
| 282 | [](CompositePolyStar* node, const ScalarValue& os) { |
Florin Malita | 9661b98 | 2018-01-06 14:25:49 -0500 | [diff] [blame] | 283 | node->setOuterRoundness(os); |
| 284 | }); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 285 | BindProperty<ScalarValue>(jstar["r"], ctx, composite, |
| 286 | [](CompositePolyStar* node, const ScalarValue& r) { |
| 287 | node->setRotation(r); |
| 288 | }); |
Florin Malita | 02a32b0 | 2018-01-04 11:27:09 -0500 | [diff] [blame] | 289 | |
| 290 | return path_node; |
| 291 | } |
| 292 | |
Florin Malita | 6aaee59 | 2018-01-12 12:25:09 -0500 | [diff] [blame] | 293 | sk_sp<sksg::Color> AttachColor(const Json::Value& obj, AttachContext* ctx) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 294 | SkASSERT(obj.isObject()); |
| 295 | |
| 296 | auto color_node = sksg::Color::Make(SK_ColorBLACK); |
Florin Malita | 1586d85 | 2018-01-12 14:27:39 -0500 | [diff] [blame] | 297 | auto color_attached = BindProperty<VectorValue>(obj["c"], ctx, color_node, |
| 298 | [](sksg::Color* node, const VectorValue& c) { |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 299 | node->setColor(ValueTraits<VectorValue>::As<SkColor>(c)); |
| 300 | }); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 301 | |
Florin Malita | 1586d85 | 2018-01-12 14:27:39 -0500 | [diff] [blame] | 302 | return color_attached ? color_node : nullptr; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 303 | } |
| 304 | |
Florin Malita | 6aaee59 | 2018-01-12 12:25:09 -0500 | [diff] [blame] | 305 | sk_sp<sksg::Gradient> AttachGradient(const Json::Value& obj, AttachContext* ctx) { |
| 306 | SkASSERT(obj.isObject()); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 307 | |
Florin Malita | 6aaee59 | 2018-01-12 12:25:09 -0500 | [diff] [blame] | 308 | const auto& stops = obj["g"]; |
| 309 | if (!stops.isObject()) |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 310 | return nullptr; |
| 311 | |
Florin Malita | 6aaee59 | 2018-01-12 12:25:09 -0500 | [diff] [blame] | 312 | const auto stopCount = ParseInt(stops["p"], -1); |
| 313 | if (stopCount < 0) |
| 314 | return nullptr; |
| 315 | |
| 316 | sk_sp<sksg::Gradient> gradient_node; |
| 317 | sk_sp<CompositeGradient> composite; |
| 318 | |
| 319 | if (ParseInt(obj["t"], 1) == 1) { |
| 320 | auto linear_node = sksg::LinearGradient::Make(); |
| 321 | composite = sk_make_sp<CompositeLinearGradient>(linear_node, stopCount); |
| 322 | gradient_node = std::move(linear_node); |
| 323 | } else { |
| 324 | auto radial_node = sksg::RadialGradient::Make(); |
| 325 | composite = sk_make_sp<CompositeRadialGradient>(radial_node, stopCount); |
| 326 | |
| 327 | // TODO: highlight, angle |
| 328 | gradient_node = std::move(radial_node); |
| 329 | } |
| 330 | |
| 331 | BindProperty<VectorValue>(stops["k"], ctx, composite, |
| 332 | [](CompositeGradient* node, const VectorValue& stops) { |
| 333 | node->setColorStops(stops); |
| 334 | }); |
| 335 | BindProperty<VectorValue>(obj["s"], ctx, composite, |
| 336 | [](CompositeGradient* node, const VectorValue& s) { |
| 337 | node->setStartPoint(ValueTraits<VectorValue>::As<SkPoint>(s)); |
| 338 | }); |
| 339 | BindProperty<VectorValue>(obj["e"], ctx, composite, |
| 340 | [](CompositeGradient* node, const VectorValue& e) { |
| 341 | node->setEndPoint(ValueTraits<VectorValue>::As<SkPoint>(e)); |
| 342 | }); |
| 343 | |
| 344 | return gradient_node; |
| 345 | } |
| 346 | |
Florin Malita | 1586d85 | 2018-01-12 14:27:39 -0500 | [diff] [blame] | 347 | sk_sp<sksg::PaintNode> AttachPaint(const Json::Value& jpaint, AttachContext* ctx, |
Florin Malita | 6aaee59 | 2018-01-12 12:25:09 -0500 | [diff] [blame] | 348 | sk_sp<sksg::PaintNode> paint_node) { |
| 349 | if (paint_node) { |
| 350 | paint_node->setAntiAlias(true); |
| 351 | |
Florin Malita | 1586d85 | 2018-01-12 14:27:39 -0500 | [diff] [blame] | 352 | BindProperty<ScalarValue>(jpaint["o"], ctx, paint_node, |
| 353 | [](sksg::PaintNode* node, const ScalarValue& o) { |
| 354 | // BM opacity is [0..100] |
| 355 | node->setOpacity(o * 0.01f); |
| 356 | }); |
Florin Malita | 6aaee59 | 2018-01-12 12:25:09 -0500 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | return paint_node; |
| 360 | } |
| 361 | |
| 362 | sk_sp<sksg::PaintNode> AttachStroke(const Json::Value& jstroke, AttachContext* ctx, |
| 363 | sk_sp<sksg::PaintNode> stroke_node) { |
| 364 | SkASSERT(jstroke.isObject()); |
| 365 | |
| 366 | if (!stroke_node) |
| 367 | return nullptr; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 368 | |
| 369 | stroke_node->setStyle(SkPaint::kStroke_Style); |
| 370 | |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 371 | auto width_attached = BindProperty<ScalarValue>(jstroke["w"], ctx, stroke_node, |
Florin Malita | 6aaee59 | 2018-01-12 12:25:09 -0500 | [diff] [blame] | 372 | [](sksg::PaintNode* node, const ScalarValue& w) { |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 373 | node->setStrokeWidth(w); |
| 374 | }); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 375 | if (!width_attached) |
| 376 | return nullptr; |
| 377 | |
| 378 | stroke_node->setStrokeMiter(ParseScalar(jstroke["ml"], 4)); |
| 379 | |
| 380 | static constexpr SkPaint::Join gJoins[] = { |
| 381 | SkPaint::kMiter_Join, |
| 382 | SkPaint::kRound_Join, |
| 383 | SkPaint::kBevel_Join, |
| 384 | }; |
| 385 | stroke_node->setStrokeJoin(gJoins[SkTPin<int>(ParseInt(jstroke["lj"], 1) - 1, |
| 386 | 0, SK_ARRAY_COUNT(gJoins) - 1)]); |
| 387 | |
| 388 | static constexpr SkPaint::Cap gCaps[] = { |
| 389 | SkPaint::kButt_Cap, |
| 390 | SkPaint::kRound_Cap, |
| 391 | SkPaint::kSquare_Cap, |
| 392 | }; |
| 393 | stroke_node->setStrokeCap(gCaps[SkTPin<int>(ParseInt(jstroke["lc"], 1) - 1, |
| 394 | 0, SK_ARRAY_COUNT(gCaps) - 1)]); |
| 395 | |
| 396 | return stroke_node; |
| 397 | } |
| 398 | |
Florin Malita | 6aaee59 | 2018-01-12 12:25:09 -0500 | [diff] [blame] | 399 | sk_sp<sksg::PaintNode> AttachColorFill(const Json::Value& jfill, AttachContext* ctx) { |
| 400 | SkASSERT(jfill.isObject()); |
| 401 | |
| 402 | return AttachPaint(jfill, ctx, AttachColor(jfill, ctx)); |
| 403 | } |
| 404 | |
| 405 | sk_sp<sksg::PaintNode> AttachGradientFill(const Json::Value& jfill, AttachContext* ctx) { |
| 406 | SkASSERT(jfill.isObject()); |
| 407 | |
| 408 | return AttachPaint(jfill, ctx, AttachGradient(jfill, ctx)); |
| 409 | } |
| 410 | |
| 411 | sk_sp<sksg::PaintNode> AttachColorStroke(const Json::Value& jstroke, AttachContext* ctx) { |
| 412 | SkASSERT(jstroke.isObject()); |
| 413 | |
| 414 | return AttachStroke(jstroke, ctx, AttachPaint(jstroke, ctx, AttachColor(jstroke, ctx))); |
| 415 | } |
| 416 | |
| 417 | sk_sp<sksg::PaintNode> AttachGradientStroke(const Json::Value& jstroke, AttachContext* ctx) { |
| 418 | SkASSERT(jstroke.isObject()); |
| 419 | |
| 420 | return AttachStroke(jstroke, ctx, AttachPaint(jstroke, ctx, AttachGradient(jstroke, ctx))); |
| 421 | } |
| 422 | |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 423 | std::vector<sk_sp<sksg::GeometryNode>> AttachMergeGeometryEffect( |
| 424 | const Json::Value& jmerge, AttachContext* ctx, std::vector<sk_sp<sksg::GeometryNode>>&& geos) { |
| 425 | std::vector<sk_sp<sksg::GeometryNode>> merged; |
| 426 | |
| 427 | static constexpr sksg::Merge::Mode gModes[] = { |
| 428 | sksg::Merge::Mode::kMerge, // "mm": 1 |
| 429 | sksg::Merge::Mode::kUnion, // "mm": 2 |
| 430 | sksg::Merge::Mode::kDifference, // "mm": 3 |
| 431 | sksg::Merge::Mode::kIntersect, // "mm": 4 |
| 432 | sksg::Merge::Mode::kXOR , // "mm": 5 |
| 433 | }; |
| 434 | |
Florin Malita | 51b8c89 | 2018-01-07 08:54:24 -0500 | [diff] [blame] | 435 | const auto mode = gModes[SkTPin<int>(ParseInt(jmerge["mm"], 1) - 1, |
| 436 | 0, SK_ARRAY_COUNT(gModes) - 1)]; |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 437 | merged.push_back(sksg::Merge::Make(std::move(geos), mode)); |
| 438 | |
| 439 | LOG("** Attached merge path effect, mode: %d\n", mode); |
| 440 | |
| 441 | return merged; |
| 442 | } |
| 443 | |
Florin Malita | 51b8c89 | 2018-01-07 08:54:24 -0500 | [diff] [blame] | 444 | std::vector<sk_sp<sksg::GeometryNode>> AttachTrimGeometryEffect( |
| 445 | const Json::Value& jtrim, AttachContext* ctx, std::vector<sk_sp<sksg::GeometryNode>>&& geos) { |
| 446 | |
| 447 | enum class Mode { |
| 448 | kMerged, // "m": 1 |
| 449 | kSeparate, // "m": 2 |
| 450 | } gModes[] = { Mode::kMerged, Mode::kSeparate }; |
| 451 | |
| 452 | const auto mode = gModes[SkTPin<int>(ParseInt(jtrim["m"], 1) - 1, |
| 453 | 0, SK_ARRAY_COUNT(gModes) - 1)]; |
| 454 | |
| 455 | std::vector<sk_sp<sksg::GeometryNode>> inputs; |
| 456 | if (mode == Mode::kMerged) { |
| 457 | inputs.push_back(sksg::Merge::Make(std::move(geos), sksg::Merge::Mode::kMerge)); |
| 458 | } else { |
| 459 | inputs = std::move(geos); |
| 460 | } |
| 461 | |
| 462 | std::vector<sk_sp<sksg::GeometryNode>> trimmed; |
| 463 | trimmed.reserve(inputs.size()); |
| 464 | for (const auto& i : inputs) { |
| 465 | const auto trim = sksg::TrimEffect::Make(i); |
| 466 | trimmed.push_back(trim); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 467 | BindProperty<ScalarValue>(jtrim["s"], ctx, trim, |
| 468 | [](sksg::TrimEffect* node, const ScalarValue& s) { |
Florin Malita | 51b8c89 | 2018-01-07 08:54:24 -0500 | [diff] [blame] | 469 | node->setStart(s * 0.01f); |
| 470 | }); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 471 | BindProperty<ScalarValue>(jtrim["e"], ctx, trim, |
| 472 | [](sksg::TrimEffect* node, const ScalarValue& e) { |
Florin Malita | 51b8c89 | 2018-01-07 08:54:24 -0500 | [diff] [blame] | 473 | node->setEnd(e * 0.01f); |
| 474 | }); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 475 | BindProperty<ScalarValue>(jtrim["o"], ctx, trim, |
| 476 | [](sksg::TrimEffect* node, const ScalarValue& o) { |
Florin Malita | 055b0f5 | 2018-01-22 23:13:10 -0500 | [diff] [blame] | 477 | node->setOffset(o / 360); |
Florin Malita | 51b8c89 | 2018-01-07 08:54:24 -0500 | [diff] [blame] | 478 | }); |
| 479 | } |
| 480 | |
| 481 | return trimmed; |
| 482 | } |
| 483 | |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 484 | using GeometryAttacherT = sk_sp<sksg::GeometryNode> (*)(const Json::Value&, AttachContext*); |
| 485 | static constexpr GeometryAttacherT gGeometryAttachers[] = { |
| 486 | AttachPathGeometry, |
Florin Malita | 2e1d7e2 | 2018-01-02 10:40:00 -0500 | [diff] [blame] | 487 | AttachRRectGeometry, |
Florin Malita | fbc13f1 | 2018-01-04 10:26:35 -0500 | [diff] [blame] | 488 | AttachEllipseGeometry, |
Florin Malita | 02a32b0 | 2018-01-04 11:27:09 -0500 | [diff] [blame] | 489 | AttachPolystarGeometry, |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 490 | }; |
| 491 | |
| 492 | using PaintAttacherT = sk_sp<sksg::PaintNode> (*)(const Json::Value&, AttachContext*); |
| 493 | static constexpr PaintAttacherT gPaintAttachers[] = { |
Florin Malita | 6aaee59 | 2018-01-12 12:25:09 -0500 | [diff] [blame] | 494 | AttachColorFill, |
| 495 | AttachColorStroke, |
| 496 | AttachGradientFill, |
| 497 | AttachGradientStroke, |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 498 | }; |
| 499 | |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 500 | using GeometryEffectAttacherT = |
| 501 | std::vector<sk_sp<sksg::GeometryNode>> (*)(const Json::Value&, |
| 502 | AttachContext*, |
| 503 | std::vector<sk_sp<sksg::GeometryNode>>&&); |
| 504 | static constexpr GeometryEffectAttacherT gGeometryEffectAttachers[] = { |
| 505 | AttachMergeGeometryEffect, |
Florin Malita | 51b8c89 | 2018-01-07 08:54:24 -0500 | [diff] [blame] | 506 | AttachTrimGeometryEffect, |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 507 | }; |
| 508 | |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 509 | enum class ShapeType { |
| 510 | kGeometry, |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 511 | kGeometryEffect, |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 512 | kPaint, |
| 513 | kGroup, |
Florin Malita | dacc02b | 2017-12-31 09:12:31 -0500 | [diff] [blame] | 514 | kTransform, |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 515 | }; |
| 516 | |
| 517 | struct ShapeInfo { |
| 518 | const char* fTypeString; |
| 519 | ShapeType fShapeType; |
| 520 | uint32_t fAttacherIndex; // index into respective attacher tables |
| 521 | }; |
| 522 | |
| 523 | const ShapeInfo* FindShapeInfo(const Json::Value& shape) { |
| 524 | static constexpr ShapeInfo gShapeInfo[] = { |
Florin Malita | fbc13f1 | 2018-01-04 10:26:35 -0500 | [diff] [blame] | 525 | { "el", ShapeType::kGeometry , 2 }, // ellipse -> AttachEllipseGeometry |
Florin Malita | 6aaee59 | 2018-01-12 12:25:09 -0500 | [diff] [blame] | 526 | { "fl", ShapeType::kPaint , 0 }, // fill -> AttachColorFill |
| 527 | { "gf", ShapeType::kPaint , 2 }, // gfill -> AttachGradientFill |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 528 | { "gr", ShapeType::kGroup , 0 }, // group -> Inline handler |
Florin Malita | 6aaee59 | 2018-01-12 12:25:09 -0500 | [diff] [blame] | 529 | { "gs", ShapeType::kPaint , 3 }, // gstroke -> AttachGradientStroke |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 530 | { "mm", ShapeType::kGeometryEffect, 0 }, // merge -> AttachMergeGeometryEffect |
Florin Malita | 02a32b0 | 2018-01-04 11:27:09 -0500 | [diff] [blame] | 531 | { "rc", ShapeType::kGeometry , 1 }, // rrect -> AttachRRectGeometry |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 532 | { "sh", ShapeType::kGeometry , 0 }, // shape -> AttachPathGeometry |
Florin Malita | 02a32b0 | 2018-01-04 11:27:09 -0500 | [diff] [blame] | 533 | { "sr", ShapeType::kGeometry , 3 }, // polystar -> AttachPolyStarGeometry |
Florin Malita | 6aaee59 | 2018-01-12 12:25:09 -0500 | [diff] [blame] | 534 | { "st", ShapeType::kPaint , 1 }, // stroke -> AttachColorStroke |
Florin Malita | 51b8c89 | 2018-01-07 08:54:24 -0500 | [diff] [blame] | 535 | { "tm", ShapeType::kGeometryEffect, 1 }, // trim -> AttachTrimGeometryEffect |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 536 | { "tr", ShapeType::kTransform , 0 }, // transform -> Inline handler |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 537 | }; |
| 538 | |
| 539 | if (!shape.isObject()) |
| 540 | return nullptr; |
| 541 | |
| 542 | const auto& type = shape["ty"]; |
| 543 | if (!type.isString()) |
| 544 | return nullptr; |
| 545 | |
| 546 | const auto* info = bsearch(type.asCString(), |
| 547 | gShapeInfo, |
| 548 | SK_ARRAY_COUNT(gShapeInfo), |
| 549 | sizeof(ShapeInfo), |
| 550 | [](const void* key, const void* info) { |
| 551 | return strcmp(static_cast<const char*>(key), |
| 552 | static_cast<const ShapeInfo*>(info)->fTypeString); |
| 553 | }); |
| 554 | |
| 555 | return static_cast<const ShapeInfo*>(info); |
| 556 | } |
| 557 | |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 558 | struct GeometryEffectRec { |
| 559 | const Json::Value& fJson; |
| 560 | GeometryEffectAttacherT fAttach; |
| 561 | }; |
| 562 | |
Florin Malita | ca4439f | 2018-01-23 10:31:59 -0500 | [diff] [blame] | 563 | struct AttachShapeContext { |
| 564 | AttachShapeContext(AttachContext* ctx, |
| 565 | std::vector<sk_sp<sksg::GeometryNode>>* geos, |
| 566 | std::vector<GeometryEffectRec>* effects, |
| 567 | size_t committedAnimators) |
| 568 | : fCtx(ctx) |
| 569 | , fGeometryStack(geos) |
| 570 | , fGeometryEffectStack(effects) |
| 571 | , fCommittedAnimators(committedAnimators) {} |
| 572 | |
| 573 | AttachContext* fCtx; |
| 574 | std::vector<sk_sp<sksg::GeometryNode>>* fGeometryStack; |
| 575 | std::vector<GeometryEffectRec>* fGeometryEffectStack; |
| 576 | size_t fCommittedAnimators; |
| 577 | }; |
| 578 | |
| 579 | sk_sp<sksg::RenderNode> AttachShape(const Json::Value& jshape, AttachShapeContext* shapeCtx) { |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 580 | if (!jshape.isArray()) |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 581 | return nullptr; |
| 582 | |
Florin Malita | ca4439f | 2018-01-23 10:31:59 -0500 | [diff] [blame] | 583 | SkDEBUGCODE(const auto initialGeometryEffects = shapeCtx->fGeometryEffectStack->size();) |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 584 | |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 585 | sk_sp<sksg::Group> shape_group = sksg::Group::Make(); |
| 586 | sk_sp<sksg::RenderNode> shape_wrapper = shape_group; |
| 587 | sk_sp<sksg::Matrix> shape_matrix; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 588 | |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 589 | struct ShapeRec { |
| 590 | const Json::Value& fJson; |
| 591 | const ShapeInfo& fInfo; |
| 592 | }; |
| 593 | |
| 594 | // First pass (bottom->top): |
| 595 | // |
| 596 | // * pick up the group transform and opacity |
| 597 | // * push local geometry effects onto the stack |
| 598 | // * store recs for next pass |
| 599 | // |
| 600 | std::vector<ShapeRec> recs; |
| 601 | for (Json::ArrayIndex i = 0; i < jshape.size(); ++i) { |
| 602 | const auto& s = jshape[jshape.size() - 1 - i]; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 603 | const auto* info = FindShapeInfo(s); |
| 604 | if (!info) { |
| 605 | LogFail(s.isObject() ? s["ty"] : s, "Unknown shape"); |
| 606 | continue; |
| 607 | } |
| 608 | |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 609 | recs.push_back({ s, *info }); |
| 610 | |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 611 | switch (info->fShapeType) { |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 612 | case ShapeType::kTransform: |
Florin Malita | ca4439f | 2018-01-23 10:31:59 -0500 | [diff] [blame] | 613 | if ((shape_matrix = AttachMatrix(s, shapeCtx->fCtx, nullptr))) { |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 614 | shape_wrapper = sksg::Transform::Make(std::move(shape_wrapper), shape_matrix); |
| 615 | } |
Florin Malita | ca4439f | 2018-01-23 10:31:59 -0500 | [diff] [blame] | 616 | shape_wrapper = AttachOpacity(s, shapeCtx->fCtx, std::move(shape_wrapper)); |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 617 | break; |
| 618 | case ShapeType::kGeometryEffect: |
| 619 | SkASSERT(info->fAttacherIndex < SK_ARRAY_COUNT(gGeometryEffectAttachers)); |
Florin Malita | ca4439f | 2018-01-23 10:31:59 -0500 | [diff] [blame] | 620 | shapeCtx->fGeometryEffectStack->push_back( |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 621 | { s, gGeometryEffectAttachers[info->fAttacherIndex] }); |
| 622 | break; |
| 623 | default: |
| 624 | break; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | // Second pass (top -> bottom, after 2x reverse): |
| 629 | // |
| 630 | // * track local geometry |
| 631 | // * emit local paints |
| 632 | // |
| 633 | std::vector<sk_sp<sksg::GeometryNode>> geos; |
| 634 | std::vector<sk_sp<sksg::RenderNode >> draws; |
| 635 | for (auto rec = recs.rbegin(); rec != recs.rend(); ++rec) { |
| 636 | switch (rec->fInfo.fShapeType) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 637 | case ShapeType::kGeometry: { |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 638 | SkASSERT(rec->fInfo.fAttacherIndex < SK_ARRAY_COUNT(gGeometryAttachers)); |
Florin Malita | ca4439f | 2018-01-23 10:31:59 -0500 | [diff] [blame] | 639 | if (auto geo = gGeometryAttachers[rec->fInfo.fAttacherIndex](rec->fJson, |
| 640 | shapeCtx->fCtx)) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 641 | geos.push_back(std::move(geo)); |
| 642 | } |
| 643 | } break; |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 644 | case ShapeType::kGeometryEffect: { |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 645 | // Apply the current effect and pop from the stack. |
| 646 | SkASSERT(rec->fInfo.fAttacherIndex < SK_ARRAY_COUNT(gGeometryEffectAttachers)); |
Florin Malita | ee6de4b | 2018-01-21 11:13:17 -0500 | [diff] [blame] | 647 | if (!geos.empty()) { |
| 648 | geos = gGeometryEffectAttachers[rec->fInfo.fAttacherIndex](rec->fJson, |
Florin Malita | ca4439f | 2018-01-23 10:31:59 -0500 | [diff] [blame] | 649 | shapeCtx->fCtx, |
Florin Malita | ee6de4b | 2018-01-21 11:13:17 -0500 | [diff] [blame] | 650 | std::move(geos)); |
| 651 | } |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 652 | |
Florin Malita | ca4439f | 2018-01-23 10:31:59 -0500 | [diff] [blame] | 653 | SkASSERT(shapeCtx->fGeometryEffectStack->back().fJson == rec->fJson); |
| 654 | SkASSERT(shapeCtx->fGeometryEffectStack->back().fAttach == |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 655 | gGeometryEffectAttachers[rec->fInfo.fAttacherIndex]); |
Florin Malita | ca4439f | 2018-01-23 10:31:59 -0500 | [diff] [blame] | 656 | shapeCtx->fGeometryEffectStack->pop_back(); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 657 | } break; |
| 658 | case ShapeType::kGroup: { |
Florin Malita | ca4439f | 2018-01-23 10:31:59 -0500 | [diff] [blame] | 659 | AttachShapeContext groupShapeCtx(shapeCtx->fCtx, |
| 660 | &geos, |
| 661 | shapeCtx->fGeometryEffectStack, |
| 662 | shapeCtx->fCommittedAnimators); |
| 663 | if (auto subgroup = AttachShape(rec->fJson["it"], &groupShapeCtx)) { |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 664 | draws.push_back(std::move(subgroup)); |
Florin Malita | ca4439f | 2018-01-23 10:31:59 -0500 | [diff] [blame] | 665 | SkASSERT(groupShapeCtx.fCommittedAnimators >= shapeCtx->fCommittedAnimators); |
| 666 | shapeCtx->fCommittedAnimators = groupShapeCtx.fCommittedAnimators; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 667 | } |
| 668 | } break; |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 669 | case ShapeType::kPaint: { |
| 670 | SkASSERT(rec->fInfo.fAttacherIndex < SK_ARRAY_COUNT(gPaintAttachers)); |
Florin Malita | ca4439f | 2018-01-23 10:31:59 -0500 | [diff] [blame] | 671 | auto paint = gPaintAttachers[rec->fInfo.fAttacherIndex](rec->fJson, shapeCtx->fCtx); |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 672 | if (!paint || geos.empty()) |
| 673 | break; |
| 674 | |
| 675 | auto drawGeos = geos; |
| 676 | |
| 677 | // Apply all pending effects from the stack. |
Florin Malita | ca4439f | 2018-01-23 10:31:59 -0500 | [diff] [blame] | 678 | for (auto it = shapeCtx->fGeometryEffectStack->rbegin(); |
| 679 | it != shapeCtx->fGeometryEffectStack->rend(); ++it) { |
| 680 | drawGeos = it->fAttach(it->fJson, shapeCtx->fCtx, std::move(drawGeos)); |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 681 | } |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 682 | |
| 683 | // If we still have multiple geos, reduce using 'merge'. |
| 684 | auto geo = drawGeos.size() > 1 |
| 685 | ? sksg::Merge::Make(std::move(drawGeos), sksg::Merge::Mode::kMerge) |
| 686 | : drawGeos[0]; |
| 687 | |
| 688 | SkASSERT(geo); |
| 689 | draws.push_back(sksg::Draw::Make(std::move(geo), std::move(paint))); |
Florin Malita | ca4439f | 2018-01-23 10:31:59 -0500 | [diff] [blame] | 690 | shapeCtx->fCommittedAnimators = shapeCtx->fCtx->fAnimators.size(); |
Florin Malita | dacc02b | 2017-12-31 09:12:31 -0500 | [diff] [blame] | 691 | } break; |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 692 | default: |
| 693 | break; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 697 | // By now we should have popped all local geometry effects. |
Florin Malita | ca4439f | 2018-01-23 10:31:59 -0500 | [diff] [blame] | 698 | SkASSERT(shapeCtx->fGeometryEffectStack->size() == initialGeometryEffects); |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 699 | |
| 700 | // Push transformed local geometries to parent list, for subsequent paints. |
| 701 | for (const auto& geo : geos) { |
Florin Malita | ca4439f | 2018-01-23 10:31:59 -0500 | [diff] [blame] | 702 | shapeCtx->fGeometryStack->push_back(shape_matrix |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 703 | ? sksg::GeometryTransform::Make(std::move(geo), shape_matrix) |
| 704 | : std::move(geo)); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 705 | } |
| 706 | |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 707 | // Emit local draws reversed (bottom->top, per spec). |
| 708 | for (auto it = draws.rbegin(); it != draws.rend(); ++it) { |
| 709 | shape_group->addChild(std::move(*it)); |
Florin Malita | 2a8275b | 2018-01-02 12:52:43 -0500 | [diff] [blame] | 710 | } |
| 711 | |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 712 | return draws.empty() ? nullptr : shape_wrapper; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | sk_sp<sksg::RenderNode> AttachCompLayer(const Json::Value& layer, AttachContext* ctx) { |
| 716 | SkASSERT(layer.isObject()); |
| 717 | |
| 718 | auto refId = ParseString(layer["refId"], ""); |
| 719 | if (refId.isEmpty()) { |
| 720 | LOG("!! Comp layer missing refId\n"); |
| 721 | return nullptr; |
| 722 | } |
| 723 | |
| 724 | const auto* comp = ctx->fAssets.find(refId); |
| 725 | if (!comp) { |
| 726 | LOG("!! Pre-comp not found: '%s'\n", refId.c_str()); |
| 727 | return nullptr; |
| 728 | } |
| 729 | |
| 730 | // TODO: cycle detection |
| 731 | return AttachComposition(**comp, ctx); |
| 732 | } |
| 733 | |
Florin Malita | 0e66fba | 2018-01-09 17:10:18 -0500 | [diff] [blame] | 734 | sk_sp<sksg::RenderNode> AttachSolidLayer(const Json::Value& jlayer, AttachContext*) { |
| 735 | SkASSERT(jlayer.isObject()); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 736 | |
Florin Malita | 0e66fba | 2018-01-09 17:10:18 -0500 | [diff] [blame] | 737 | const auto size = SkSize::Make(ParseScalar(jlayer["sw"], -1), |
| 738 | ParseScalar(jlayer["sh"], -1)); |
| 739 | const auto hex = ParseString(jlayer["sc"], ""); |
| 740 | uint32_t c; |
| 741 | if (size.isEmpty() || |
| 742 | !hex.startsWith("#") || |
| 743 | !SkParse::FindHex(hex.c_str() + 1, &c)) { |
| 744 | LogFail(jlayer, "Could not parse solid layer"); |
| 745 | return nullptr; |
| 746 | } |
| 747 | |
| 748 | const SkColor color = 0xff000000 | c; |
| 749 | |
| 750 | return sksg::Draw::Make(sksg::Rect::Make(SkRect::MakeSize(size)), |
| 751 | sksg::Color::Make(color)); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 752 | } |
| 753 | |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 754 | sk_sp<sksg::RenderNode> AttachImageAsset(const Json::Value& jimage, AttachContext* ctx) { |
| 755 | SkASSERT(jimage.isObject()); |
| 756 | |
| 757 | const auto name = ParseString(jimage["p"], ""), |
| 758 | path = ParseString(jimage["u"], ""); |
| 759 | if (name.isEmpty()) |
| 760 | return nullptr; |
| 761 | |
| 762 | // TODO: plumb resource paths explicitly to ResourceProvider? |
| 763 | const auto resName = path.isEmpty() ? name : SkOSPath::Join(path.c_str(), name.c_str()); |
| 764 | const auto resStream = ctx->fResources.openStream(resName.c_str()); |
| 765 | if (!resStream || !resStream->hasLength()) { |
| 766 | LOG("!! Could not load image resource: %s\n", resName.c_str()); |
| 767 | return nullptr; |
| 768 | } |
| 769 | |
| 770 | // TODO: non-intrisic image sizing |
| 771 | return sksg::Image::Make( |
| 772 | SkImage::MakeFromEncoded(SkData::MakeFromStream(resStream.get(), resStream->getLength()))); |
| 773 | } |
| 774 | |
| 775 | sk_sp<sksg::RenderNode> AttachImageLayer(const Json::Value& layer, AttachContext* ctx) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 776 | SkASSERT(layer.isObject()); |
| 777 | |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 778 | auto refId = ParseString(layer["refId"], ""); |
| 779 | if (refId.isEmpty()) { |
| 780 | LOG("!! Image layer missing refId\n"); |
| 781 | return nullptr; |
| 782 | } |
| 783 | |
| 784 | const auto* jimage = ctx->fAssets.find(refId); |
| 785 | if (!jimage) { |
| 786 | LOG("!! Image asset not found: '%s'\n", refId.c_str()); |
| 787 | return nullptr; |
| 788 | } |
| 789 | |
| 790 | return AttachImageAsset(**jimage, ctx); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | sk_sp<sksg::RenderNode> AttachNullLayer(const Json::Value& layer, AttachContext*) { |
| 794 | SkASSERT(layer.isObject()); |
| 795 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 796 | // Null layers are used solely to drive dependent transforms, |
| 797 | // but we use free-floating sksg::Matrices for that purpose. |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 798 | return nullptr; |
| 799 | } |
| 800 | |
| 801 | sk_sp<sksg::RenderNode> AttachShapeLayer(const Json::Value& layer, AttachContext* ctx) { |
| 802 | SkASSERT(layer.isObject()); |
| 803 | |
| 804 | LOG("** Attaching shape layer ind: %d\n", ParseInt(layer["ind"], 0)); |
| 805 | |
Florin Malita | 16d0ad0 | 2018-01-19 15:07:29 -0500 | [diff] [blame] | 806 | std::vector<sk_sp<sksg::GeometryNode>> geometryStack; |
| 807 | std::vector<GeometryEffectRec> geometryEffectStack; |
Florin Malita | ca4439f | 2018-01-23 10:31:59 -0500 | [diff] [blame] | 808 | AttachShapeContext shapeCtx(ctx, &geometryStack, &geometryEffectStack, ctx->fAnimators.size()); |
| 809 | auto shapeNode = AttachShape(layer["shapes"], &shapeCtx); |
| 810 | |
| 811 | // Trim uncommitted animators: AttachShape consumes effects on the fly, and greedily attaches |
| 812 | // geometries => at the end, we can end up with unused geometries, which are nevertheless alive |
| 813 | // due to attached animators. To avoid this, we track committed animators and discard the |
| 814 | // orphans here. |
| 815 | SkASSERT(shapeCtx.fCommittedAnimators <= ctx->fAnimators.size()); |
| 816 | ctx->fAnimators.resize(shapeCtx.fCommittedAnimators); |
| 817 | |
| 818 | return shapeNode; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | sk_sp<sksg::RenderNode> AttachTextLayer(const Json::Value& layer, AttachContext*) { |
| 822 | SkASSERT(layer.isObject()); |
| 823 | |
| 824 | LOG("?? Text layer stub\n"); |
| 825 | return nullptr; |
| 826 | } |
| 827 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 828 | struct AttachLayerContext { |
| 829 | AttachLayerContext(const Json::Value& jlayers, AttachContext* ctx) |
| 830 | : fLayerList(jlayers), fCtx(ctx) {} |
| 831 | |
| 832 | const Json::Value& fLayerList; |
| 833 | AttachContext* fCtx; |
| 834 | std::unordered_map<const Json::Value*, sk_sp<sksg::Matrix>> fLayerMatrixCache; |
| 835 | std::unordered_map<int, const Json::Value*> fLayerIndexCache; |
Florin Malita | 5f9102f | 2018-01-10 13:36:22 -0500 | [diff] [blame] | 836 | sk_sp<sksg::RenderNode> fCurrentMatte; |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 837 | |
| 838 | const Json::Value* findLayer(int index) { |
| 839 | SkASSERT(fLayerList.isArray()); |
| 840 | |
| 841 | if (index < 0) { |
| 842 | return nullptr; |
| 843 | } |
| 844 | |
| 845 | const auto cached = fLayerIndexCache.find(index); |
| 846 | if (cached != fLayerIndexCache.end()) { |
| 847 | return cached->second; |
| 848 | } |
| 849 | |
| 850 | for (const auto& l : fLayerList) { |
| 851 | if (!l.isObject()) { |
| 852 | continue; |
| 853 | } |
| 854 | |
| 855 | if (ParseInt(l["ind"], -1) == index) { |
| 856 | fLayerIndexCache.insert(std::make_pair(index, &l)); |
| 857 | return &l; |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | return nullptr; |
| 862 | } |
| 863 | |
| 864 | sk_sp<sksg::Matrix> AttachLayerMatrix(const Json::Value& jlayer) { |
| 865 | SkASSERT(jlayer.isObject()); |
| 866 | |
| 867 | const auto cached = fLayerMatrixCache.find(&jlayer); |
| 868 | if (cached != fLayerMatrixCache.end()) { |
| 869 | return cached->second; |
| 870 | } |
| 871 | |
| 872 | const auto* parentLayer = this->findLayer(ParseInt(jlayer["parent"], -1)); |
| 873 | |
| 874 | // TODO: cycle detection? |
| 875 | auto parentMatrix = (parentLayer && parentLayer != &jlayer) |
| 876 | ? this->AttachLayerMatrix(*parentLayer) : nullptr; |
| 877 | |
| 878 | auto layerMatrix = AttachMatrix(jlayer["ks"], fCtx, std::move(parentMatrix)); |
| 879 | fLayerMatrixCache.insert(std::make_pair(&jlayer, layerMatrix)); |
| 880 | |
| 881 | return layerMatrix; |
| 882 | } |
| 883 | }; |
| 884 | |
Florin Malita | 25366fa | 2018-01-23 13:37:59 -0500 | [diff] [blame^] | 885 | SkBlendMode MaskBlendMode(char mode) { |
| 886 | switch (mode) { |
| 887 | case 'a': return SkBlendMode::kSrcOver; // Additive |
| 888 | case 's': return SkBlendMode::kExclusion; // Subtract |
| 889 | case 'i': return SkBlendMode::kDstIn; // Intersect |
| 890 | case 'l': return SkBlendMode::kLighten; // Lighten |
| 891 | case 'd': return SkBlendMode::kDarken; // Darken |
| 892 | case 'f': return SkBlendMode::kDifference; // Difference |
| 893 | default: break; |
| 894 | } |
| 895 | |
| 896 | return SkBlendMode::kSrcOver; |
| 897 | } |
| 898 | |
| 899 | sk_sp<sksg::RenderNode> AttachMask(const Json::Value& jmask, |
| 900 | AttachContext* ctx, |
| 901 | sk_sp<sksg::RenderNode> childNode) { |
| 902 | if (!jmask.isArray()) |
| 903 | return childNode; |
| 904 | |
| 905 | auto mask_group = sksg::Group::Make(); |
| 906 | |
| 907 | for (const auto& m : jmask) { |
| 908 | if (!m.isObject()) |
| 909 | continue; |
| 910 | |
| 911 | const auto inverted = ParseBool(m["inv"], false); |
| 912 | // TODO |
| 913 | if (inverted) { |
| 914 | LogFail(m, "Unsupported inverse mask"); |
| 915 | continue; |
| 916 | } |
| 917 | |
| 918 | auto mask_path = AttachPath(m["pt"], ctx); |
| 919 | if (!mask_path) { |
| 920 | LogFail(m, "Could not parse mask path"); |
| 921 | continue; |
| 922 | } |
| 923 | |
| 924 | auto mode = ParseString(m["mode"], ""); |
| 925 | if (mode.size() != 1 || !strcmp(mode.c_str(), "n")) // "None" masks have no effect. |
| 926 | continue; |
| 927 | |
| 928 | auto mask_paint = sksg::Color::Make(SK_ColorBLACK); |
| 929 | mask_paint->setBlendMode(MaskBlendMode(mode.c_str()[0])); |
| 930 | BindProperty<ScalarValue>(m["o"], ctx, mask_paint, |
| 931 | [](sksg::Color* node, const ScalarValue& o) { node->setOpacity(o * 0.01f); }); |
| 932 | |
| 933 | mask_group->addChild(sksg::Draw::Make(std::move(mask_path), std::move(mask_paint))); |
| 934 | } |
| 935 | |
| 936 | return mask_group->empty() |
| 937 | ? childNode |
| 938 | : sksg::MaskEffect::Make(std::move(childNode), std::move(mask_group)); |
| 939 | } |
| 940 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 941 | sk_sp<sksg::RenderNode> AttachLayer(const Json::Value& jlayer, |
| 942 | AttachLayerContext* layerCtx) { |
| 943 | if (!jlayer.isObject()) |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 944 | return nullptr; |
| 945 | |
| 946 | using LayerAttacher = sk_sp<sksg::RenderNode> (*)(const Json::Value&, AttachContext*); |
| 947 | static constexpr LayerAttacher gLayerAttachers[] = { |
| 948 | AttachCompLayer, // 'ty': 0 |
| 949 | AttachSolidLayer, // 'ty': 1 |
| 950 | AttachImageLayer, // 'ty': 2 |
| 951 | AttachNullLayer, // 'ty': 3 |
| 952 | AttachShapeLayer, // 'ty': 4 |
| 953 | AttachTextLayer, // 'ty': 5 |
| 954 | }; |
| 955 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 956 | int type = ParseInt(jlayer["ty"], -1); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 957 | if (type < 0 || type >= SkTo<int>(SK_ARRAY_COUNT(gLayerAttachers))) { |
| 958 | return nullptr; |
| 959 | } |
| 960 | |
Florin Malita | 71cba8f | 2018-01-09 08:07:14 -0500 | [diff] [blame] | 961 | // Layer content. |
| 962 | auto layer = gLayerAttachers[type](jlayer, layerCtx->fCtx); |
Florin Malita | 25366fa | 2018-01-23 13:37:59 -0500 | [diff] [blame^] | 963 | // Optional layer mask. |
| 964 | layer = AttachMask(jlayer["masksProperties"], layerCtx->fCtx, std::move(layer)); |
| 965 | // Optional layer transform. |
Florin Malita | 71cba8f | 2018-01-09 08:07:14 -0500 | [diff] [blame] | 966 | if (auto layerMatrix = layerCtx->AttachLayerMatrix(jlayer)) { |
Florin Malita | 71cba8f | 2018-01-09 08:07:14 -0500 | [diff] [blame] | 967 | layer = sksg::Transform::Make(std::move(layer), std::move(layerMatrix)); |
| 968 | } |
| 969 | // Optional layer opacity. |
| 970 | layer = AttachOpacity(jlayer["ks"], layerCtx->fCtx, std::move(layer)); |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 971 | |
Florin Malita | 71cba8f | 2018-01-09 08:07:14 -0500 | [diff] [blame] | 972 | // TODO: we should also disable related/inactive animators. |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 973 | class Activator final : public sksg::Animator { |
Florin Malita | 71cba8f | 2018-01-09 08:07:14 -0500 | [diff] [blame] | 974 | public: |
| 975 | Activator(sk_sp<sksg::OpacityEffect> controlNode, float in, float out) |
| 976 | : fControlNode(std::move(controlNode)) |
| 977 | , fIn(in) |
| 978 | , fOut(out) {} |
| 979 | |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 980 | void onTick(float t) override { |
Florin Malita | 71cba8f | 2018-01-09 08:07:14 -0500 | [diff] [blame] | 981 | // Keep the layer fully transparent except for its [in..out] lifespan. |
| 982 | // (note: opacity == 0 disables rendering, while opacity == 1 is a noop) |
| 983 | fControlNode->setOpacity(t >= fIn && t <= fOut ? 1 : 0); |
| 984 | } |
| 985 | |
| 986 | private: |
| 987 | const sk_sp<sksg::OpacityEffect> fControlNode; |
| 988 | const float fIn, |
| 989 | fOut; |
| 990 | }; |
| 991 | |
| 992 | auto layerControl = sksg::OpacityEffect::Make(std::move(layer)); |
| 993 | const auto in = ParseScalar(jlayer["ip"], 0), |
| 994 | out = ParseScalar(jlayer["op"], in); |
| 995 | |
| 996 | if (in >= out || ! layerControl) |
| 997 | return nullptr; |
| 998 | |
| 999 | layerCtx->fCtx->fAnimators.push_back(skstd::make_unique<Activator>(layerControl, in, out)); |
| 1000 | |
Florin Malita | 5f9102f | 2018-01-10 13:36:22 -0500 | [diff] [blame] | 1001 | if (ParseBool(jlayer["td"], false)) { |
| 1002 | // This layer is a matte. We apply it as a mask to the next layer. |
| 1003 | layerCtx->fCurrentMatte = std::move(layerControl); |
| 1004 | return nullptr; |
| 1005 | } |
| 1006 | |
| 1007 | if (layerCtx->fCurrentMatte) { |
| 1008 | // There is a pending matte. Apply and reset. |
| 1009 | return sksg::MaskEffect::Make(std::move(layerControl), std::move(layerCtx->fCurrentMatte)); |
| 1010 | } |
| 1011 | |
Florin Malita | 71cba8f | 2018-01-09 08:07:14 -0500 | [diff] [blame] | 1012 | return layerControl; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | sk_sp<sksg::RenderNode> AttachComposition(const Json::Value& comp, AttachContext* ctx) { |
| 1016 | if (!comp.isObject()) |
| 1017 | return nullptr; |
| 1018 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 1019 | const auto& jlayers = comp["layers"]; |
| 1020 | if (!jlayers.isArray()) |
| 1021 | return nullptr; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 1022 | |
Florin Malita | 18eafd9 | 2018-01-04 21:11:55 -0500 | [diff] [blame] | 1023 | SkSTArray<16, sk_sp<sksg::RenderNode>, true> layers; |
| 1024 | AttachLayerContext layerCtx(jlayers, ctx); |
| 1025 | |
| 1026 | for (const auto& l : jlayers) { |
| 1027 | if (auto layer_fragment = AttachLayer(l, &layerCtx)) { |
Florin Malita | 2a8275b | 2018-01-02 12:52:43 -0500 | [diff] [blame] | 1028 | layers.push_back(std::move(layer_fragment)); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | |
Florin Malita | 2a8275b | 2018-01-02 12:52:43 -0500 | [diff] [blame] | 1032 | if (layers.empty()) { |
| 1033 | return nullptr; |
| 1034 | } |
| 1035 | |
| 1036 | // Layers are painted in bottom->top order. |
| 1037 | auto comp_group = sksg::Group::Make(); |
| 1038 | for (int i = layers.count() - 1; i >= 0; --i) { |
| 1039 | comp_group->addChild(std::move(layers[i])); |
| 1040 | } |
| 1041 | |
| 1042 | LOG("** Attached composition '%s': %d layers.\n", |
| 1043 | ParseString(comp["id"], "").c_str(), layers.count()); |
| 1044 | |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 1045 | return comp_group; |
| 1046 | } |
| 1047 | |
| 1048 | } // namespace |
| 1049 | |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 1050 | std::unique_ptr<Animation> Animation::Make(SkStream* stream, const ResourceProvider& res) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 1051 | if (!stream->hasLength()) { |
| 1052 | // TODO: handle explicit buffering? |
| 1053 | LOG("!! cannot parse streaming content\n"); |
| 1054 | return nullptr; |
| 1055 | } |
| 1056 | |
| 1057 | Json::Value json; |
| 1058 | { |
| 1059 | auto data = SkData::MakeFromStream(stream, stream->getLength()); |
| 1060 | if (!data) { |
| 1061 | LOG("!! could not read stream\n"); |
| 1062 | return nullptr; |
| 1063 | } |
| 1064 | |
| 1065 | Json::Reader reader; |
| 1066 | |
| 1067 | auto dataStart = static_cast<const char*>(data->data()); |
| 1068 | if (!reader.parse(dataStart, dataStart + data->size(), json, false) || !json.isObject()) { |
| 1069 | LOG("!! failed to parse json: %s\n", reader.getFormattedErrorMessages().c_str()); |
| 1070 | return nullptr; |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | const auto version = ParseString(json["v"], ""); |
| 1075 | const auto size = SkSize::Make(ParseScalar(json["w"], -1), ParseScalar(json["h"], -1)); |
| 1076 | const auto fps = ParseScalar(json["fr"], -1); |
| 1077 | |
| 1078 | if (size.isEmpty() || version.isEmpty() || fps < 0) { |
| 1079 | LOG("!! invalid animation params (version: %s, size: [%f %f], frame rate: %f)", |
| 1080 | version.c_str(), size.width(), size.height(), fps); |
| 1081 | return nullptr; |
| 1082 | } |
| 1083 | |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 1084 | 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] | 1085 | } |
| 1086 | |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 1087 | std::unique_ptr<Animation> Animation::MakeFromFile(const char path[], const ResourceProvider* res) { |
| 1088 | class DirectoryResourceProvider final : public ResourceProvider { |
| 1089 | public: |
| 1090 | explicit DirectoryResourceProvider(SkString dir) : fDir(std::move(dir)) {} |
| 1091 | |
| 1092 | std::unique_ptr<SkStream> openStream(const char resource[]) const override { |
| 1093 | const auto resPath = SkOSPath::Join(fDir.c_str(), resource); |
| 1094 | return SkStream::MakeFromFile(resPath.c_str()); |
| 1095 | } |
| 1096 | |
| 1097 | private: |
| 1098 | const SkString fDir; |
| 1099 | }; |
| 1100 | |
| 1101 | const auto jsonStream = SkStream::MakeFromFile(path); |
| 1102 | if (!jsonStream) |
| 1103 | return nullptr; |
| 1104 | |
| 1105 | std::unique_ptr<ResourceProvider> defaultProvider; |
| 1106 | if (!res) { |
| 1107 | defaultProvider = skstd::make_unique<DirectoryResourceProvider>(SkOSPath::Dirname(path)); |
| 1108 | } |
| 1109 | |
| 1110 | return Make(jsonStream.get(), res ? *res : *defaultProvider); |
| 1111 | } |
| 1112 | |
| 1113 | Animation::Animation(const ResourceProvider& resources, |
| 1114 | SkString version, const SkSize& size, SkScalar fps, const Json::Value& json) |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 1115 | : fVersion(std::move(version)) |
| 1116 | , fSize(size) |
| 1117 | , fFrameRate(fps) |
| 1118 | , fInPoint(ParseScalar(json["ip"], 0)) |
| 1119 | , fOutPoint(SkTMax(ParseScalar(json["op"], SK_ScalarMax), fInPoint)) { |
| 1120 | |
| 1121 | AssetMap assets; |
| 1122 | for (const auto& asset : json["assets"]) { |
| 1123 | if (!asset.isObject()) { |
| 1124 | continue; |
| 1125 | } |
| 1126 | |
| 1127 | assets.set(ParseString(asset["id"], ""), &asset); |
| 1128 | } |
| 1129 | |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 1130 | sksg::Scene::AnimatorList animators; |
| 1131 | AttachContext ctx = { resources, assets, animators }; |
| 1132 | auto root = AttachComposition(json, &ctx); |
| 1133 | |
| 1134 | LOG("** Attached %d animators\n", animators.size()); |
| 1135 | |
| 1136 | fScene = sksg::Scene::Make(std::move(root), std::move(animators)); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 1137 | |
Florin Malita | db38573 | 2018-01-09 12:19:32 -0500 | [diff] [blame] | 1138 | // In case the client calls render before the first tick. |
| 1139 | this->animationTick(0); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | Animation::~Animation() = default; |
| 1143 | |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 1144 | void Animation::setShowInval(bool show) { |
| 1145 | if (fScene) { |
| 1146 | fScene->setShowInval(show); |
| 1147 | } |
| 1148 | } |
| 1149 | |
Mike Reed | 2985987 | 2018-01-08 08:25:27 -0500 | [diff] [blame] | 1150 | void Animation::render(SkCanvas* canvas, const SkRect* dstR) const { |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 1151 | if (!fScene) |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 1152 | return; |
| 1153 | |
Mike Reed | 2985987 | 2018-01-08 08:25:27 -0500 | [diff] [blame] | 1154 | SkAutoCanvasRestore restore(canvas, true); |
| 1155 | const SkRect srcR = SkRect::MakeSize(this->size()); |
| 1156 | if (dstR) { |
| 1157 | canvas->concat(SkMatrix::MakeRectToRect(srcR, *dstR, SkMatrix::kCenter_ScaleToFit)); |
| 1158 | } |
| 1159 | canvas->clipRect(srcR); |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 1160 | fScene->render(canvas); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | void Animation::animationTick(SkMSec ms) { |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 1164 | if (!fScene) |
| 1165 | return; |
| 1166 | |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 1167 | // 't' in the BM model really means 'frame #' |
| 1168 | auto t = static_cast<float>(ms) * fFrameRate / 1000; |
| 1169 | |
| 1170 | t = fInPoint + std::fmod(t, fOutPoint - fInPoint); |
| 1171 | |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 1172 | fScene->animate(t); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 1173 | } |
| 1174 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 1175 | } // namespace skottie |