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