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