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