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