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