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 | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 11 | #include "SkData.h" |
Florin Malita | f9c5063 | 2018-08-17 12:29:45 -0400 | [diff] [blame] | 12 | #include "SkFontMgr.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 13 | #include "SkMakeUnique.h" |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 14 | #include "SkOSPath.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 15 | #include "SkPaint.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 16 | #include "SkPoint.h" |
| 17 | #include "SkSGColor.h" |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 18 | #include "SkSGInvalidationController.h" |
Florin Malita | c003417 | 2018-01-08 16:42:59 -0500 | [diff] [blame] | 19 | #include "SkSGOpacityEffect.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 20 | #include "SkSGPath.h" |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 21 | #include "SkSGScene.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 22 | #include "SkSGTransform.h" |
| 23 | #include "SkStream.h" |
| 24 | #include "SkTArray.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 25 | #include "SkTime.h" |
| 26 | #include "SkTo.h" |
| 27 | #include "SkottieAdapter.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 28 | #include "SkottieJson.h" |
Florin Malita | f9c5063 | 2018-08-17 12:29:45 -0400 | [diff] [blame] | 29 | #include "SkottiePriv.h" |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 30 | #include "SkottieProperty.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 31 | #include "SkottieValue.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 32 | |
| 33 | #include <cmath> |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 34 | |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 35 | #include "stdlib.h" |
| 36 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 37 | namespace skottie { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 38 | |
Florin Malita | f9c5063 | 2018-08-17 12:29:45 -0400 | [diff] [blame] | 39 | namespace internal { |
| 40 | |
| 41 | void LogJSON(const skjson::Value& json, const char msg[]) { |
| 42 | const auto dump = json.toString(); |
| 43 | LOG("%s: %s\n", msg, dump.c_str()); |
| 44 | } |
| 45 | |
Florin Malita | 471a946 | 2018-08-25 20:22:34 -0400 | [diff] [blame] | 46 | sk_sp<sksg::Matrix> AnimationBuilder::attachMatrix(const skjson::ObjectValue& t, |
| 47 | AnimatorScope* ascope, |
| 48 | sk_sp<sksg::Matrix> parentMatrix) const { |
Florin Malita | 3be2e10 | 2018-07-10 10:20:36 -0400 | [diff] [blame] | 49 | static const VectorValue g_default_vec_0 = { 0, 0}, |
| 50 | g_default_vec_100 = {100, 100}; |
| 51 | |
Florin Malita | 13ac194 | 2018-07-12 17:48:36 -0400 | [diff] [blame] | 52 | auto matrix = sksg::Matrix::Make(SkMatrix::I(), parentMatrix); |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 53 | auto adapter = sk_make_sp<TransformAdapter>(matrix); |
Florin Malita | 3be2e10 | 2018-07-10 10:20:36 -0400 | [diff] [blame] | 54 | |
Florin Malita | 471a946 | 2018-08-25 20:22:34 -0400 | [diff] [blame] | 55 | auto bound = this->bindProperty<VectorValue>(t["a"], ascope, |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 56 | [adapter](const VectorValue& a) { |
| 57 | adapter->setAnchorPoint(ValueTraits<VectorValue>::As<SkPoint>(a)); |
Florin Malita | 3be2e10 | 2018-07-10 10:20:36 -0400 | [diff] [blame] | 58 | }, g_default_vec_0); |
Florin Malita | 471a946 | 2018-08-25 20:22:34 -0400 | [diff] [blame] | 59 | bound |= this->bindProperty<VectorValue>(t["p"], ascope, |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 60 | [adapter](const VectorValue& p) { |
| 61 | adapter->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p)); |
Florin Malita | 3be2e10 | 2018-07-10 10:20:36 -0400 | [diff] [blame] | 62 | }, g_default_vec_0); |
Florin Malita | 471a946 | 2018-08-25 20:22:34 -0400 | [diff] [blame] | 63 | bound |= this->bindProperty<VectorValue>(t["s"], ascope, |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 64 | [adapter](const VectorValue& s) { |
| 65 | adapter->setScale(ValueTraits<VectorValue>::As<SkVector>(s)); |
Florin Malita | 3be2e10 | 2018-07-10 10:20:36 -0400 | [diff] [blame] | 66 | }, g_default_vec_100); |
Florin Malita | 1eb98db | 2018-01-26 15:03:38 -0500 | [diff] [blame] | 67 | |
Florin Malita | 7d42c44 | 2018-06-14 16:16:01 -0400 | [diff] [blame] | 68 | const auto* jrotation = &t["r"]; |
| 69 | if (jrotation->is<skjson::NullValue>()) { |
Florin Malita | 1eb98db | 2018-01-26 15:03:38 -0500 | [diff] [blame] | 70 | // 3d rotations have separate rx,ry,rz components. While we don't fully support them, |
| 71 | // we can still make use of rz. |
Florin Malita | 7d42c44 | 2018-06-14 16:16:01 -0400 | [diff] [blame] | 72 | jrotation = &t["rz"]; |
Florin Malita | 1eb98db | 2018-01-26 15:03:38 -0500 | [diff] [blame] | 73 | } |
Florin Malita | 471a946 | 2018-08-25 20:22:34 -0400 | [diff] [blame] | 74 | bound |= this->bindProperty<ScalarValue>(*jrotation, ascope, |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 75 | [adapter](const ScalarValue& r) { |
| 76 | adapter->setRotation(r); |
Florin Malita | 3be2e10 | 2018-07-10 10:20:36 -0400 | [diff] [blame] | 77 | }, 0.0f); |
Florin Malita | 471a946 | 2018-08-25 20:22:34 -0400 | [diff] [blame] | 78 | bound |= this->bindProperty<ScalarValue>(t["sk"], ascope, |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 79 | [adapter](const ScalarValue& sk) { |
| 80 | adapter->setSkew(sk); |
Florin Malita | 3be2e10 | 2018-07-10 10:20:36 -0400 | [diff] [blame] | 81 | }, 0.0f); |
Florin Malita | 471a946 | 2018-08-25 20:22:34 -0400 | [diff] [blame] | 82 | bound |= this->bindProperty<ScalarValue>(t["sa"], ascope, |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 83 | [adapter](const ScalarValue& sa) { |
| 84 | adapter->setSkewAxis(sa); |
Florin Malita | 3be2e10 | 2018-07-10 10:20:36 -0400 | [diff] [blame] | 85 | }, 0.0f); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 86 | |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 87 | const auto dispatched = this->dispatchTransformProperty(adapter); |
| 88 | |
| 89 | return (bound || dispatched) ? matrix : parentMatrix; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 90 | } |
| 91 | |
Florin Malita | 471a946 | 2018-08-25 20:22:34 -0400 | [diff] [blame] | 92 | sk_sp<sksg::RenderNode> AnimationBuilder::attachOpacity(const skjson::ObjectValue& jtransform, |
| 93 | AnimatorScope* ascope, |
| 94 | sk_sp<sksg::RenderNode> childNode) const { |
Florin Malita | 7d42c44 | 2018-06-14 16:16:01 -0400 | [diff] [blame] | 95 | if (!childNode) |
| 96 | return nullptr; |
Florin Malita | c003417 | 2018-01-08 16:42:59 -0500 | [diff] [blame] | 97 | |
Florin Malita | c003417 | 2018-01-08 16:42:59 -0500 | [diff] [blame] | 98 | auto opacityNode = sksg::OpacityEffect::Make(childNode); |
Florin Malita | 7ac2e3b | 2018-05-09 14:54:39 -0400 | [diff] [blame] | 99 | |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 100 | const auto bound = this->bindProperty<ScalarValue>(jtransform["o"], ascope, |
Florin Malita | 2518a0a | 2018-01-24 18:29:00 -0500 | [diff] [blame] | 101 | [opacityNode](const ScalarValue& o) { |
Florin Malita | c003417 | 2018-01-08 16:42:59 -0500 | [diff] [blame] | 102 | // BM opacity is [0..100] |
Florin Malita | 2518a0a | 2018-01-24 18:29:00 -0500 | [diff] [blame] | 103 | opacityNode->setOpacity(o * 0.01f); |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 104 | }, 100.0f); |
| 105 | const auto dispatched = this->dispatchOpacityProperty(opacityNode); |
Florin Malita | c003417 | 2018-01-08 16:42:59 -0500 | [diff] [blame] | 106 | |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 107 | // We can ignore constant full opacity. |
| 108 | return (bound || dispatched) ? std::move(opacityNode) : childNode; |
Florin Malita | c003417 | 2018-01-08 16:42:59 -0500 | [diff] [blame] | 109 | } |
| 110 | |
Florin Malita | 471a946 | 2018-08-25 20:22:34 -0400 | [diff] [blame] | 111 | sk_sp<sksg::Path> AnimationBuilder::attachPath(const skjson::Value& jpath, |
| 112 | AnimatorScope* ascope) const { |
Florin Malita | 25366fa | 2018-01-23 13:37:59 -0500 | [diff] [blame] | 113 | auto path_node = sksg::Path::Make(); |
Florin Malita | 471a946 | 2018-08-25 20:22:34 -0400 | [diff] [blame] | 114 | return this->bindProperty<ShapeValue>(jpath, ascope, |
Florin Malita | c353ee2 | 2018-04-30 21:49:41 -0400 | [diff] [blame] | 115 | [path_node](const ShapeValue& p) { |
Florin Malita | 502c3ff | 2018-07-03 18:10:39 -0400 | [diff] [blame] | 116 | // FillType is tracked in the SG node, not in keyframes -- make sure we preserve it. |
| 117 | auto path = ValueTraits<ShapeValue>::As<SkPath>(p); |
| 118 | path.setFillType(path_node->getFillType()); |
| 119 | path_node->setPath(path); |
Florin Malita | c353ee2 | 2018-04-30 21:49:41 -0400 | [diff] [blame] | 120 | }) |
Florin Malita | 25366fa | 2018-01-23 13:37:59 -0500 | [diff] [blame] | 121 | ? path_node |
| 122 | : nullptr; |
| 123 | } |
| 124 | |
Florin Malita | 471a946 | 2018-08-25 20:22:34 -0400 | [diff] [blame] | 125 | sk_sp<sksg::Color> AnimationBuilder::attachColor(const skjson::ObjectValue& jcolor, |
| 126 | AnimatorScope* ascope, |
| 127 | const char prop_name[]) const { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 128 | auto color_node = sksg::Color::Make(SK_ColorBLACK); |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 129 | |
Florin Malita | 471a946 | 2018-08-25 20:22:34 -0400 | [diff] [blame] | 130 | this->bindProperty<VectorValue>(jcolor[prop_name], ascope, |
Florin Malita | 2518a0a | 2018-01-24 18:29:00 -0500 | [diff] [blame] | 131 | [color_node](const VectorValue& c) { |
| 132 | color_node->setColor(ValueTraits<VectorValue>::As<SkColor>(c)); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 133 | }); |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 134 | this->dispatchColorProperty(color_node); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 135 | |
Florin Malita | c45a5c5 | 2018-07-16 14:20:51 -0400 | [diff] [blame] | 136 | return color_node; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 137 | } |
| 138 | |
Florin Malita | 906cdf3 | 2018-08-23 14:49:05 -0400 | [diff] [blame] | 139 | AnimationBuilder::AnimationBuilder(sk_sp<ResourceProvider> rp, sk_sp<SkFontMgr> fontmgr, |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 140 | sk_sp<PropertyObserver> pobserver, |
Florin Malita | 906cdf3 | 2018-08-23 14:49:05 -0400 | [diff] [blame] | 141 | Animation::Builder::Stats* stats, |
| 142 | float duration, float framerate) |
| 143 | : fResourceProvider(std::move(rp)) |
Florin Malita | be5e865 | 2018-08-31 12:54:18 -0400 | [diff] [blame] | 144 | , fLazyFontMgr(std::move(fontmgr)) |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 145 | , fPropertyObserver(std::move(pobserver)) |
Florin Malita | fa0441b | 2018-08-21 13:17:27 -0400 | [diff] [blame] | 146 | , fStats(stats) |
| 147 | , fDuration(duration) |
| 148 | , fFrameRate(framerate) {} |
| 149 | |
| 150 | std::unique_ptr<sksg::Scene> AnimationBuilder::parse(const skjson::ObjectValue& jroot) { |
| 151 | this->parseAssets(jroot["assets"]); |
| 152 | this->parseFonts(jroot["fonts"], jroot["chars"]); |
| 153 | |
| 154 | AnimatorScope animators; |
| 155 | auto root = this->attachComposition(jroot, &animators); |
| 156 | |
| 157 | fStats->fAnimatorCount = animators.size(); |
| 158 | |
| 159 | return sksg::Scene::Make(std::move(root), std::move(animators)); |
| 160 | } |
| 161 | |
| 162 | void AnimationBuilder::parseAssets(const skjson::ArrayValue* jassets) { |
| 163 | if (!jassets) { |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | for (const skjson::ObjectValue* asset : *jassets) { |
| 168 | if (asset) { |
| 169 | fAssets.set(ParseDefault<SkString>((*asset)["id"], SkString()), { asset, false }); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 174 | bool AnimationBuilder::dispatchColorProperty(const sk_sp<sksg::Color>& c) const { |
| 175 | bool dispatched = false; |
| 176 | |
| 177 | if (fPropertyObserver) { |
| 178 | fPropertyObserver->onColorProperty(fPropertyObserverContext, |
| 179 | [&]() { |
| 180 | dispatched = true; |
| 181 | return std::unique_ptr<ColorPropertyHandle>(new ColorPropertyHandle(c)); |
| 182 | }); |
| 183 | } |
| 184 | |
| 185 | return dispatched; |
| 186 | } |
| 187 | |
| 188 | bool AnimationBuilder::dispatchOpacityProperty(const sk_sp<sksg::OpacityEffect>& o) const { |
| 189 | bool dispatched = false; |
| 190 | |
| 191 | if (fPropertyObserver) { |
| 192 | fPropertyObserver->onOpacityProperty(fPropertyObserverContext, |
| 193 | [&]() { |
| 194 | dispatched = true; |
| 195 | return std::unique_ptr<OpacityPropertyHandle>(new OpacityPropertyHandle(o)); |
| 196 | }); |
| 197 | } |
| 198 | |
| 199 | return dispatched; |
| 200 | } |
| 201 | |
| 202 | bool AnimationBuilder::dispatchTransformProperty(const sk_sp<TransformAdapter>& t) const { |
| 203 | bool dispatched = false; |
| 204 | |
| 205 | if (fPropertyObserver) { |
| 206 | fPropertyObserver->onTransformProperty(fPropertyObserverContext, |
| 207 | [&]() { |
| 208 | dispatched = true; |
| 209 | return std::unique_ptr<TransformPropertyHandle>(new TransformPropertyHandle(t)); |
| 210 | }); |
| 211 | } |
| 212 | |
| 213 | return dispatched; |
| 214 | } |
| 215 | |
| 216 | void AnimationBuilder::AutoPropertyTracker::updateContext(PropertyObserver* observer, |
| 217 | const skjson::ObjectValue& obj) { |
| 218 | |
| 219 | const skjson::StringValue* name = obj["nm"]; |
| 220 | |
| 221 | fBuilder->fPropertyObserverContext = name ? name->begin() : nullptr; |
| 222 | } |
| 223 | |
Florin Malita | fa0441b | 2018-08-21 13:17:27 -0400 | [diff] [blame] | 224 | } // namespace internal |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 225 | |
Florin Malita | 4054351 | 2018-09-19 21:43:29 -0400 | [diff] [blame] | 226 | sk_sp<SkData> ResourceProvider::load(const char[], const char[]) const { |
| 227 | return nullptr; |
| 228 | } |
| 229 | |
Florin Malita | 97b150e | 2018-09-27 10:59:52 -0400 | [diff] [blame] | 230 | sk_sp<SkData> ResourceProvider::loadFont(const char[], const char[]) const { |
Florin Malita | 4054351 | 2018-09-19 21:43:29 -0400 | [diff] [blame] | 231 | return nullptr; |
| 232 | } |
| 233 | |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 234 | Animation::Builder::Builder() = default; |
| 235 | Animation::Builder::~Builder() = default; |
| 236 | |
Florin Malita | 906cdf3 | 2018-08-23 14:49:05 -0400 | [diff] [blame] | 237 | Animation::Builder& Animation::Builder::setResourceProvider(sk_sp<ResourceProvider> rp) { |
| 238 | fResourceProvider = std::move(rp); |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 239 | return *this; |
| 240 | } |
| 241 | |
| 242 | Animation::Builder& Animation::Builder::setFontManager(sk_sp<SkFontMgr> fmgr) { |
| 243 | fFontMgr = std::move(fmgr); |
| 244 | return *this; |
| 245 | } |
| 246 | |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 247 | Animation::Builder& Animation::Builder::setPropertyObserver(sk_sp<PropertyObserver> pobserver) { |
| 248 | fPropertyObserver = std::move(pobserver); |
| 249 | return *this; |
| 250 | } |
| 251 | |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 252 | sk_sp<Animation> Animation::Builder::make(SkStream* stream) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 253 | if (!stream->hasLength()) { |
| 254 | // TODO: handle explicit buffering? |
| 255 | LOG("!! cannot parse streaming content\n"); |
| 256 | return nullptr; |
| 257 | } |
| 258 | |
Florin Malita | 7d42c44 | 2018-06-14 16:16:01 -0400 | [diff] [blame] | 259 | auto data = SkData::MakeFromStream(stream, stream->getLength()); |
| 260 | if (!data) { |
| 261 | SkDebugf("!! Failed to read the input stream.\n"); |
Florin Malita | fa7e9a8 | 2018-05-04 15:10:54 -0400 | [diff] [blame] | 262 | return nullptr; |
Florin Malita | 7d42c44 | 2018-06-14 16:16:01 -0400 | [diff] [blame] | 263 | } |
| 264 | |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 265 | return this->make(static_cast<const char*>(data->data()), data->size()); |
Florin Malita | 6e487e6 | 2018-08-06 11:07:05 -0400 | [diff] [blame] | 266 | } |
| 267 | |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 268 | sk_sp<Animation> Animation::Builder::make(const char* data, size_t data_len) { |
| 269 | // Sanitize factory args. |
| 270 | class NullResourceProvider final : public ResourceProvider { |
| 271 | sk_sp<SkData> load(const char[], const char[]) const override { return nullptr; } |
| 272 | }; |
Florin Malita | 906cdf3 | 2018-08-23 14:49:05 -0400 | [diff] [blame] | 273 | auto resolvedProvider = fResourceProvider |
| 274 | ? fResourceProvider : sk_make_sp<NullResourceProvider>(); |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 275 | |
| 276 | memset(&fStats, 0, sizeof(struct Stats)); |
| 277 | |
| 278 | fStats.fJsonSize = data_len; |
Florin Malita | 6e487e6 | 2018-08-06 11:07:05 -0400 | [diff] [blame] | 279 | const auto t0 = SkTime::GetMSecs(); |
| 280 | |
| 281 | const skjson::DOM dom(data, data_len); |
Florin Malita | 7d42c44 | 2018-06-14 16:16:01 -0400 | [diff] [blame] | 282 | if (!dom.root().is<skjson::ObjectValue>()) { |
| 283 | // TODO: more error info. |
| 284 | SkDebugf("!! Failed to parse JSON input.\n"); |
| 285 | return nullptr; |
| 286 | } |
| 287 | const auto& json = dom.root().as<skjson::ObjectValue>(); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 288 | |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 289 | const auto t1 = SkTime::GetMSecs(); |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 290 | fStats.fJsonParseTimeMS = t1 - t0; |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 291 | |
Florin Malita | cc9c45d | 2018-08-20 10:02:12 -0400 | [diff] [blame] | 292 | const auto version = ParseDefault<SkString>(json["v"], SkString()); |
| 293 | const auto size = SkSize::Make(ParseDefault<float>(json["w"], 0.0f), |
| 294 | ParseDefault<float>(json["h"], 0.0f)); |
| 295 | const auto fps = ParseDefault<float>(json["fr"], -1.0f), |
| 296 | inPoint = ParseDefault<float>(json["ip"], 0.0f), |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 297 | outPoint = SkTMax(ParseDefault<float>(json["op"], SK_ScalarMax), inPoint), |
Florin Malita | 217d4a5 | 2018-09-18 10:05:30 -0400 | [diff] [blame] | 298 | duration = sk_ieee_float_divide(outPoint - inPoint, fps); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 299 | |
Florin Malita | cc9c45d | 2018-08-20 10:02:12 -0400 | [diff] [blame] | 300 | if (size.isEmpty() || version.isEmpty() || fps <= 0 || |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 301 | !SkScalarIsFinite(inPoint) || !SkScalarIsFinite(outPoint) || !SkScalarIsFinite(duration)) { |
Florin Malita | cc9c45d | 2018-08-20 10:02:12 -0400 | [diff] [blame] | 302 | LOG("!! invalid animation params (version: %s, size: [%f %f], frame rate: %f, " |
| 303 | "in-point: %f, out-point: %f)\n", |
| 304 | version.c_str(), size.width(), size.height(), fps, inPoint, outPoint); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 305 | return nullptr; |
| 306 | } |
| 307 | |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 308 | SkASSERT(resolvedProvider); |
Florin Malita | be5e865 | 2018-08-31 12:54:18 -0400 | [diff] [blame] | 309 | internal::AnimationBuilder builder(std::move(resolvedProvider), fFontMgr, |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 310 | std::move(fPropertyObserver), &fStats, duration, fps); |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 311 | auto scene = builder.parse(json); |
Florin Malita | c83a0de | 2018-05-31 12:17:55 -0400 | [diff] [blame] | 312 | |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 313 | const auto t2 = SkTime::GetMSecs(); |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 314 | fStats.fSceneParseTimeMS = t2 - t1; |
| 315 | fStats.fTotalLoadTimeMS = t2 - t0; |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 316 | |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 317 | if (!scene) { |
| 318 | LOG("!! could not parse animation.\n"); |
| 319 | } |
| 320 | |
| 321 | return sk_sp<Animation>( |
| 322 | new Animation(std::move(scene), std::move(version), size, inPoint, outPoint, duration)); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 323 | } |
| 324 | |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 325 | sk_sp<Animation> Animation::Builder::makeFromFile(const char path[]) { |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 326 | class DirectoryResourceProvider final : public ResourceProvider { |
| 327 | public: |
| 328 | explicit DirectoryResourceProvider(SkString dir) : fDir(std::move(dir)) {} |
| 329 | |
Florin Malita | 0c8dca0 | 2018-08-06 14:13:04 -0400 | [diff] [blame] | 330 | sk_sp<SkData> load(const char resource_path[], const char resource_name[]) const override { |
| 331 | const auto full_dir = SkOSPath::Join(fDir.c_str(), resource_path), |
| 332 | full_path = SkOSPath::Join(full_dir.c_str(), resource_name); |
| 333 | return SkData::MakeFromFileName(full_path.c_str()); |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | private: |
| 337 | const SkString fDir; |
| 338 | }; |
| 339 | |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 340 | const auto data = SkData::MakeFromFileName(path); |
Florin Malita | 6e487e6 | 2018-08-06 11:07:05 -0400 | [diff] [blame] | 341 | if (!data) |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 342 | return nullptr; |
| 343 | |
Florin Malita | 906cdf3 | 2018-08-23 14:49:05 -0400 | [diff] [blame] | 344 | const auto useLocalProvider = !fResourceProvider; |
| 345 | if (useLocalProvider) { |
| 346 | fResourceProvider = sk_make_sp<DirectoryResourceProvider>(SkOSPath::Dirname(path)); |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 347 | } |
| 348 | |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 349 | auto animation = this->make(static_cast<const char*>(data->data()), data->size()); |
| 350 | |
Florin Malita | 906cdf3 | 2018-08-23 14:49:05 -0400 | [diff] [blame] | 351 | if (useLocalProvider) { |
| 352 | fResourceProvider.reset(); |
| 353 | } |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 354 | |
| 355 | return animation; |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 356 | } |
| 357 | |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 358 | Animation::Animation(std::unique_ptr<sksg::Scene> scene, SkString version, const SkSize& size, |
| 359 | SkScalar inPoint, SkScalar outPoint, SkScalar duration) |
| 360 | : fScene(std::move(scene)) |
| 361 | , fVersion(std::move(version)) |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 362 | , fSize(size) |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 363 | , fInPoint(inPoint) |
| 364 | , fOutPoint(outPoint) |
| 365 | , fDuration(duration) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 366 | |
Florin Malita | db38573 | 2018-01-09 12:19:32 -0500 | [diff] [blame] | 367 | // In case the client calls render before the first tick. |
Florin Malita | a33447d | 2018-05-29 13:46:54 -0400 | [diff] [blame] | 368 | this->seek(0); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | Animation::~Animation() = default; |
| 372 | |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 373 | void Animation::setShowInval(bool show) { |
| 374 | if (fScene) { |
| 375 | fScene->setShowInval(show); |
| 376 | } |
| 377 | } |
| 378 | |
Mike Reed | 2985987 | 2018-01-08 08:25:27 -0500 | [diff] [blame] | 379 | void Animation::render(SkCanvas* canvas, const SkRect* dstR) const { |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 380 | if (!fScene) |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 381 | return; |
| 382 | |
Mike Reed | 2985987 | 2018-01-08 08:25:27 -0500 | [diff] [blame] | 383 | SkAutoCanvasRestore restore(canvas, true); |
| 384 | const SkRect srcR = SkRect::MakeSize(this->size()); |
| 385 | if (dstR) { |
| 386 | canvas->concat(SkMatrix::MakeRectToRect(srcR, *dstR, SkMatrix::kCenter_ScaleToFit)); |
| 387 | } |
| 388 | canvas->clipRect(srcR); |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 389 | fScene->render(canvas); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 390 | } |
| 391 | |
Florin Malita | a33447d | 2018-05-29 13:46:54 -0400 | [diff] [blame] | 392 | void Animation::seek(SkScalar t) { |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 393 | if (!fScene) |
| 394 | return; |
| 395 | |
Florin Malita | a33447d | 2018-05-29 13:46:54 -0400 | [diff] [blame] | 396 | fScene->animate(fInPoint + SkTPin(t, 0.0f, 1.0f) * (fOutPoint - fInPoint)); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 397 | } |
| 398 | |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 399 | sk_sp<Animation> Animation::Make(const char* data, size_t length) { |
| 400 | return Builder().make(data, length); |
| 401 | } |
| 402 | |
| 403 | sk_sp<Animation> Animation::Make(SkStream* stream) { |
| 404 | return Builder().make(stream); |
| 405 | } |
| 406 | |
| 407 | sk_sp<Animation> Animation::MakeFromFile(const char path[]) { |
| 408 | return Builder().makeFromFile(path); |
| 409 | } |
| 410 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 411 | } // namespace skottie |