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