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