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 | f12d675 | 2019-02-26 13:15:48 -0500 | [diff] [blame] | 21 | #include "SkSGRenderEffect.h" |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 22 | #include "SkSGScene.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 23 | #include "SkSGTransform.h" |
| 24 | #include "SkStream.h" |
| 25 | #include "SkTArray.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 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 | 9d0b306 | 2018-12-13 11:08:39 -0500 | [diff] [blame] | 32 | #include "SkTraceEvent.h" |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 33 | |
Florin Malita | 4025710 | 2019-03-07 10:10:38 -0500 | [diff] [blame^] | 34 | #include <chrono> |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 35 | #include <cmath> |
Florin Malita | e6345d9 | 2018-01-03 23:37:54 -0500 | [diff] [blame] | 36 | |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 37 | #include "stdlib.h" |
| 38 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 39 | namespace skottie { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 40 | |
Florin Malita | f9c5063 | 2018-08-17 12:29:45 -0400 | [diff] [blame] | 41 | namespace internal { |
| 42 | |
Florin Malita | 57b9d40 | 2018-10-02 12:48:00 -0400 | [diff] [blame] | 43 | void AnimationBuilder::log(Logger::Level lvl, const skjson::Value* json, |
| 44 | const char fmt[], ...) const { |
| 45 | if (!fLogger) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | char buff[1024]; |
| 50 | va_list va; |
| 51 | va_start(va, fmt); |
Florin Malita | b26b899 | 2019-02-05 18:57:29 -0500 | [diff] [blame] | 52 | const auto len = vsnprintf(buff, sizeof(buff), fmt, va); |
Florin Malita | 57b9d40 | 2018-10-02 12:48:00 -0400 | [diff] [blame] | 53 | va_end(va); |
| 54 | |
Florin Malita | b26b899 | 2019-02-05 18:57:29 -0500 | [diff] [blame] | 55 | if (len < 0) { |
Florin Malita | 57b9d40 | 2018-10-02 12:48:00 -0400 | [diff] [blame] | 56 | SkDebugf("!! Could not format log message !!\n"); |
| 57 | return; |
| 58 | } |
| 59 | |
Florin Malita | b26b899 | 2019-02-05 18:57:29 -0500 | [diff] [blame] | 60 | if (len >= SkToInt(sizeof(buff))) { |
| 61 | static constexpr char kEllipsesStr[] = "..."; |
| 62 | strcpy(buff + sizeof(buff) - sizeof(kEllipsesStr), kEllipsesStr); |
| 63 | } |
| 64 | |
Florin Malita | 57b9d40 | 2018-10-02 12:48:00 -0400 | [diff] [blame] | 65 | SkString jsonstr = json ? json->toString() : SkString(); |
| 66 | |
| 67 | fLogger->log(lvl, buff, jsonstr.c_str()); |
Florin Malita | f9c5063 | 2018-08-17 12:29:45 -0400 | [diff] [blame] | 68 | } |
| 69 | |
Florin Malita | 919e209 | 2019-01-09 15:37:57 -0500 | [diff] [blame] | 70 | sk_sp<sksg::Transform> AnimationBuilder::attachMatrix2D(const skjson::ObjectValue& t, |
| 71 | AnimatorScope* ascope, |
| 72 | sk_sp<sksg::Transform> parent) const { |
Florin Malita | 3be2e10 | 2018-07-10 10:20:36 -0400 | [diff] [blame] | 73 | static const VectorValue g_default_vec_0 = { 0, 0}, |
| 74 | g_default_vec_100 = {100, 100}; |
| 75 | |
Florin Malita | 760a052 | 2019-01-10 15:24:15 -0500 | [diff] [blame] | 76 | auto matrix = sksg::Matrix<SkMatrix>::Make(SkMatrix::I()); |
Florin Malita | d2a1853 | 2019-01-02 10:34:49 -0500 | [diff] [blame] | 77 | auto adapter = sk_make_sp<TransformAdapter2D>(matrix); |
Florin Malita | 3be2e10 | 2018-07-10 10:20:36 -0400 | [diff] [blame] | 78 | |
Florin Malita | 471a946 | 2018-08-25 20:22:34 -0400 | [diff] [blame] | 79 | auto bound = this->bindProperty<VectorValue>(t["a"], ascope, |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 80 | [adapter](const VectorValue& a) { |
| 81 | adapter->setAnchorPoint(ValueTraits<VectorValue>::As<SkPoint>(a)); |
Florin Malita | 3be2e10 | 2018-07-10 10:20:36 -0400 | [diff] [blame] | 82 | }, g_default_vec_0); |
Florin Malita | 471a946 | 2018-08-25 20:22:34 -0400 | [diff] [blame] | 83 | bound |= this->bindProperty<VectorValue>(t["p"], ascope, |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 84 | [adapter](const VectorValue& p) { |
| 85 | adapter->setPosition(ValueTraits<VectorValue>::As<SkPoint>(p)); |
Florin Malita | 3be2e10 | 2018-07-10 10:20:36 -0400 | [diff] [blame] | 86 | }, g_default_vec_0); |
Florin Malita | 471a946 | 2018-08-25 20:22:34 -0400 | [diff] [blame] | 87 | bound |= this->bindProperty<VectorValue>(t["s"], ascope, |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 88 | [adapter](const VectorValue& s) { |
| 89 | adapter->setScale(ValueTraits<VectorValue>::As<SkVector>(s)); |
Florin Malita | 3be2e10 | 2018-07-10 10:20:36 -0400 | [diff] [blame] | 90 | }, g_default_vec_100); |
Florin Malita | 1eb98db | 2018-01-26 15:03:38 -0500 | [diff] [blame] | 91 | |
Florin Malita | 7d42c44 | 2018-06-14 16:16:01 -0400 | [diff] [blame] | 92 | const auto* jrotation = &t["r"]; |
| 93 | if (jrotation->is<skjson::NullValue>()) { |
Florin Malita | 1eb98db | 2018-01-26 15:03:38 -0500 | [diff] [blame] | 94 | // 3d rotations have separate rx,ry,rz components. While we don't fully support them, |
| 95 | // we can still make use of rz. |
Florin Malita | 7d42c44 | 2018-06-14 16:16:01 -0400 | [diff] [blame] | 96 | jrotation = &t["rz"]; |
Florin Malita | 1eb98db | 2018-01-26 15:03:38 -0500 | [diff] [blame] | 97 | } |
Florin Malita | 471a946 | 2018-08-25 20:22:34 -0400 | [diff] [blame] | 98 | bound |= this->bindProperty<ScalarValue>(*jrotation, ascope, |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 99 | [adapter](const ScalarValue& r) { |
| 100 | adapter->setRotation(r); |
Florin Malita | 3be2e10 | 2018-07-10 10:20:36 -0400 | [diff] [blame] | 101 | }, 0.0f); |
Florin Malita | 471a946 | 2018-08-25 20:22:34 -0400 | [diff] [blame] | 102 | bound |= this->bindProperty<ScalarValue>(t["sk"], ascope, |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 103 | [adapter](const ScalarValue& sk) { |
| 104 | adapter->setSkew(sk); |
Florin Malita | 3be2e10 | 2018-07-10 10:20:36 -0400 | [diff] [blame] | 105 | }, 0.0f); |
Florin Malita | 471a946 | 2018-08-25 20:22:34 -0400 | [diff] [blame] | 106 | bound |= this->bindProperty<ScalarValue>(t["sa"], ascope, |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 107 | [adapter](const ScalarValue& sa) { |
| 108 | adapter->setSkewAxis(sa); |
Florin Malita | 3be2e10 | 2018-07-10 10:20:36 -0400 | [diff] [blame] | 109 | }, 0.0f); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 110 | |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 111 | const auto dispatched = this->dispatchTransformProperty(adapter); |
| 112 | |
Florin Malita | 919e209 | 2019-01-09 15:37:57 -0500 | [diff] [blame] | 113 | return (bound || dispatched) |
| 114 | ? sksg::Transform::MakeConcat(std::move(parent), std::move(matrix)) |
| 115 | : parent; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 116 | } |
| 117 | |
Florin Malita | 919e209 | 2019-01-09 15:37:57 -0500 | [diff] [blame] | 118 | sk_sp<sksg::Transform> AnimationBuilder::attachMatrix3D(const skjson::ObjectValue& t, |
| 119 | AnimatorScope* ascope, |
| 120 | sk_sp<sksg::Transform> parent) const { |
Florin Malita | d2a1853 | 2019-01-02 10:34:49 -0500 | [diff] [blame] | 121 | static const VectorValue g_default_vec_0 = { 0, 0, 0}, |
| 122 | g_default_vec_100 = {100, 100, 100}; |
| 123 | |
Florin Malita | 760a052 | 2019-01-10 15:24:15 -0500 | [diff] [blame] | 124 | auto matrix = sksg::Matrix<SkMatrix44>::Make(SkMatrix::I()); |
Florin Malita | d2a1853 | 2019-01-02 10:34:49 -0500 | [diff] [blame] | 125 | auto adapter = sk_make_sp<TransformAdapter3D>(matrix); |
| 126 | |
| 127 | auto bound = this->bindProperty<VectorValue>(t["a"], ascope, |
| 128 | [adapter](const VectorValue& a) { |
| 129 | adapter->setAnchorPoint(TransformAdapter3D::Vec3(a)); |
| 130 | }, g_default_vec_0); |
| 131 | bound |= this->bindProperty<VectorValue>(t["p"], ascope, |
| 132 | [adapter](const VectorValue& p) { |
| 133 | adapter->setPosition(TransformAdapter3D::Vec3(p)); |
| 134 | }, g_default_vec_0); |
| 135 | bound |= this->bindProperty<VectorValue>(t["s"], ascope, |
| 136 | [adapter](const VectorValue& s) { |
| 137 | adapter->setScale(TransformAdapter3D::Vec3(s)); |
| 138 | }, g_default_vec_100); |
| 139 | |
| 140 | // Orientation and rx/ry/rz are mapped to the same rotation property -- the difference is |
| 141 | // in how they get interpolated (vector vs. scalar/decomposed interpolation). |
| 142 | bound |= this->bindProperty<VectorValue>(t["or"], ascope, |
| 143 | [adapter](const VectorValue& o) { |
| 144 | adapter->setRotation(TransformAdapter3D::Vec3(o)); |
| 145 | }, g_default_vec_0); |
| 146 | |
| 147 | bound |= this->bindProperty<ScalarValue>(t["rx"], ascope, |
| 148 | [adapter](const ScalarValue& rx) { |
| 149 | const auto& r = adapter->getRotation(); |
| 150 | adapter->setRotation(TransformAdapter3D::Vec3({rx, r.fY, r.fZ})); |
| 151 | }, 0.0f); |
| 152 | |
| 153 | bound |= this->bindProperty<ScalarValue>(t["ry"], ascope, |
| 154 | [adapter](const ScalarValue& ry) { |
| 155 | const auto& r = adapter->getRotation(); |
| 156 | adapter->setRotation(TransformAdapter3D::Vec3({r.fX, ry, r.fZ})); |
| 157 | }, 0.0f); |
| 158 | |
| 159 | bound |= this->bindProperty<ScalarValue>(t["rz"], ascope, |
| 160 | [adapter](const ScalarValue& rz) { |
| 161 | const auto& r = adapter->getRotation(); |
| 162 | adapter->setRotation(TransformAdapter3D::Vec3({r.fX, r.fY, rz})); |
| 163 | }, 0.0f); |
| 164 | |
| 165 | // TODO: dispatch 3D transform properties |
| 166 | |
Florin Malita | 919e209 | 2019-01-09 15:37:57 -0500 | [diff] [blame] | 167 | return (bound) |
| 168 | ? sksg::Transform::MakeConcat(std::move(parent), std::move(matrix)) |
| 169 | : parent; |
Florin Malita | d2a1853 | 2019-01-02 10:34:49 -0500 | [diff] [blame] | 170 | } |
| 171 | |
Florin Malita | 471a946 | 2018-08-25 20:22:34 -0400 | [diff] [blame] | 172 | sk_sp<sksg::RenderNode> AnimationBuilder::attachOpacity(const skjson::ObjectValue& jtransform, |
| 173 | AnimatorScope* ascope, |
| 174 | sk_sp<sksg::RenderNode> childNode) const { |
Florin Malita | 7d42c44 | 2018-06-14 16:16:01 -0400 | [diff] [blame] | 175 | if (!childNode) |
| 176 | return nullptr; |
Florin Malita | c003417 | 2018-01-08 16:42:59 -0500 | [diff] [blame] | 177 | |
Florin Malita | c003417 | 2018-01-08 16:42:59 -0500 | [diff] [blame] | 178 | auto opacityNode = sksg::OpacityEffect::Make(childNode); |
Florin Malita | 7ac2e3b | 2018-05-09 14:54:39 -0400 | [diff] [blame] | 179 | |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 180 | const auto bound = this->bindProperty<ScalarValue>(jtransform["o"], ascope, |
Florin Malita | 2518a0a | 2018-01-24 18:29:00 -0500 | [diff] [blame] | 181 | [opacityNode](const ScalarValue& o) { |
Florin Malita | c003417 | 2018-01-08 16:42:59 -0500 | [diff] [blame] | 182 | // BM opacity is [0..100] |
Florin Malita | 2518a0a | 2018-01-24 18:29:00 -0500 | [diff] [blame] | 183 | opacityNode->setOpacity(o * 0.01f); |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 184 | }, 100.0f); |
| 185 | const auto dispatched = this->dispatchOpacityProperty(opacityNode); |
Florin Malita | c003417 | 2018-01-08 16:42:59 -0500 | [diff] [blame] | 186 | |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 187 | // We can ignore constant full opacity. |
| 188 | return (bound || dispatched) ? std::move(opacityNode) : childNode; |
Florin Malita | c003417 | 2018-01-08 16:42:59 -0500 | [diff] [blame] | 189 | } |
| 190 | |
Florin Malita | f12d675 | 2019-02-26 13:15:48 -0500 | [diff] [blame] | 191 | namespace { |
| 192 | |
| 193 | static SkBlendMode GetBlendMode(const skjson::ObjectValue& jobject, |
| 194 | const AnimationBuilder* abuilder) { |
| 195 | static constexpr SkBlendMode kBlendModeMap[] = { |
| 196 | SkBlendMode::kSrcOver, // 0:'normal' |
| 197 | SkBlendMode::kMultiply, // 1:'multiply' |
| 198 | SkBlendMode::kScreen, // 2:'screen' |
| 199 | SkBlendMode::kOverlay, // 3:'overlay |
| 200 | SkBlendMode::kDarken, // 4:'darken' |
| 201 | SkBlendMode::kLighten, // 5:'lighten' |
| 202 | SkBlendMode::kColorDodge, // 6:'color-dodge' |
| 203 | SkBlendMode::kColorBurn, // 7:'color-burn' |
| 204 | SkBlendMode::kHardLight, // 8:'hard-light' |
| 205 | SkBlendMode::kSoftLight, // 9:'soft-light' |
| 206 | SkBlendMode::kDifference, // 10:'difference' |
| 207 | SkBlendMode::kExclusion, // 11:'exclusion' |
| 208 | SkBlendMode::kHue, // 12:'hue' |
| 209 | SkBlendMode::kSaturation, // 13:'saturation' |
| 210 | SkBlendMode::kColor, // 14:'color' |
| 211 | SkBlendMode::kLuminosity, // 15:'luminosity' |
| 212 | }; |
| 213 | |
| 214 | const auto bm_index = ParseDefault<size_t>(jobject["bm"], 0); |
| 215 | if (bm_index >= SK_ARRAY_COUNT(kBlendModeMap)) { |
| 216 | abuilder->log(Logger::Level::kWarning, &jobject, |
| 217 | "Unsupported blend mode %lu\n", bm_index); |
| 218 | return SkBlendMode::kSrcOver; |
| 219 | } |
| 220 | |
| 221 | return kBlendModeMap[bm_index]; |
| 222 | } |
| 223 | |
| 224 | } // namespace |
| 225 | |
| 226 | sk_sp<sksg::RenderNode> AnimationBuilder::attachBlendMode(const skjson::ObjectValue& jobject, |
| 227 | sk_sp<sksg::RenderNode> child) const { |
| 228 | const auto bm = GetBlendMode(jobject, this); |
| 229 | if (bm != SkBlendMode::kSrcOver) { |
| 230 | fHasNontrivialBlending = true; |
| 231 | child = sksg::BlendModeEffect::Make(std::move(child), bm); |
| 232 | } |
| 233 | |
| 234 | return child; |
| 235 | } |
| 236 | |
Florin Malita | 471a946 | 2018-08-25 20:22:34 -0400 | [diff] [blame] | 237 | sk_sp<sksg::Path> AnimationBuilder::attachPath(const skjson::Value& jpath, |
| 238 | AnimatorScope* ascope) const { |
Florin Malita | 25366fa | 2018-01-23 13:37:59 -0500 | [diff] [blame] | 239 | auto path_node = sksg::Path::Make(); |
Florin Malita | 471a946 | 2018-08-25 20:22:34 -0400 | [diff] [blame] | 240 | return this->bindProperty<ShapeValue>(jpath, ascope, |
Florin Malita | c353ee2 | 2018-04-30 21:49:41 -0400 | [diff] [blame] | 241 | [path_node](const ShapeValue& p) { |
Florin Malita | 502c3ff | 2018-07-03 18:10:39 -0400 | [diff] [blame] | 242 | // FillType is tracked in the SG node, not in keyframes -- make sure we preserve it. |
| 243 | auto path = ValueTraits<ShapeValue>::As<SkPath>(p); |
| 244 | path.setFillType(path_node->getFillType()); |
| 245 | path_node->setPath(path); |
Florin Malita | c353ee2 | 2018-04-30 21:49:41 -0400 | [diff] [blame] | 246 | }) |
Florin Malita | 25366fa | 2018-01-23 13:37:59 -0500 | [diff] [blame] | 247 | ? path_node |
| 248 | : nullptr; |
| 249 | } |
| 250 | |
Florin Malita | 471a946 | 2018-08-25 20:22:34 -0400 | [diff] [blame] | 251 | sk_sp<sksg::Color> AnimationBuilder::attachColor(const skjson::ObjectValue& jcolor, |
| 252 | AnimatorScope* ascope, |
| 253 | const char prop_name[]) const { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 254 | auto color_node = sksg::Color::Make(SK_ColorBLACK); |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 255 | |
Florin Malita | 471a946 | 2018-08-25 20:22:34 -0400 | [diff] [blame] | 256 | this->bindProperty<VectorValue>(jcolor[prop_name], ascope, |
Florin Malita | 2518a0a | 2018-01-24 18:29:00 -0500 | [diff] [blame] | 257 | [color_node](const VectorValue& c) { |
| 258 | color_node->setColor(ValueTraits<VectorValue>::As<SkColor>(c)); |
Florin Malita | f959092 | 2018-01-09 11:56:09 -0500 | [diff] [blame] | 259 | }); |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 260 | this->dispatchColorProperty(color_node); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 261 | |
Florin Malita | c45a5c5 | 2018-07-16 14:20:51 -0400 | [diff] [blame] | 262 | return color_node; |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 263 | } |
| 264 | |
Florin Malita | 906cdf3 | 2018-08-23 14:49:05 -0400 | [diff] [blame] | 265 | AnimationBuilder::AnimationBuilder(sk_sp<ResourceProvider> rp, sk_sp<SkFontMgr> fontmgr, |
Florin Malita | 57b9d40 | 2018-10-02 12:48:00 -0400 | [diff] [blame] | 266 | sk_sp<PropertyObserver> pobserver, sk_sp<Logger> logger, |
Florin Malita | 91af8d8 | 2018-11-30 16:46:45 -0500 | [diff] [blame] | 267 | sk_sp<MarkerObserver> mobserver, |
Florin Malita | 906cdf3 | 2018-08-23 14:49:05 -0400 | [diff] [blame] | 268 | Animation::Builder::Stats* stats, |
| 269 | float duration, float framerate) |
| 270 | : fResourceProvider(std::move(rp)) |
Florin Malita | be5e865 | 2018-08-31 12:54:18 -0400 | [diff] [blame] | 271 | , fLazyFontMgr(std::move(fontmgr)) |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 272 | , fPropertyObserver(std::move(pobserver)) |
Florin Malita | 57b9d40 | 2018-10-02 12:48:00 -0400 | [diff] [blame] | 273 | , fLogger(std::move(logger)) |
Florin Malita | 91af8d8 | 2018-11-30 16:46:45 -0500 | [diff] [blame] | 274 | , fMarkerObserver(std::move(mobserver)) |
Florin Malita | fa0441b | 2018-08-21 13:17:27 -0400 | [diff] [blame] | 275 | , fStats(stats) |
| 276 | , fDuration(duration) |
Florin Malita | bd64f18 | 2019-02-25 09:55:04 -0500 | [diff] [blame] | 277 | , fFrameRate(framerate) |
| 278 | , fHasNontrivialBlending(false) {} |
Florin Malita | fa0441b | 2018-08-21 13:17:27 -0400 | [diff] [blame] | 279 | |
| 280 | std::unique_ptr<sksg::Scene> AnimationBuilder::parse(const skjson::ObjectValue& jroot) { |
Florin Malita | 91af8d8 | 2018-11-30 16:46:45 -0500 | [diff] [blame] | 281 | this->dispatchMarkers(jroot["markers"]); |
Florin Malita | d9c56b4 | 2018-10-09 14:33:08 -0400 | [diff] [blame] | 282 | |
Florin Malita | fa0441b | 2018-08-21 13:17:27 -0400 | [diff] [blame] | 283 | this->parseAssets(jroot["assets"]); |
| 284 | this->parseFonts(jroot["fonts"], jroot["chars"]); |
| 285 | |
| 286 | AnimatorScope animators; |
| 287 | auto root = this->attachComposition(jroot, &animators); |
| 288 | |
| 289 | fStats->fAnimatorCount = animators.size(); |
| 290 | |
| 291 | return sksg::Scene::Make(std::move(root), std::move(animators)); |
| 292 | } |
| 293 | |
| 294 | void AnimationBuilder::parseAssets(const skjson::ArrayValue* jassets) { |
| 295 | if (!jassets) { |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | for (const skjson::ObjectValue* asset : *jassets) { |
| 300 | if (asset) { |
| 301 | fAssets.set(ParseDefault<SkString>((*asset)["id"], SkString()), { asset, false }); |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
Florin Malita | 91af8d8 | 2018-11-30 16:46:45 -0500 | [diff] [blame] | 306 | void AnimationBuilder::dispatchMarkers(const skjson::ArrayValue* jmarkers) const { |
| 307 | if (!fMarkerObserver || !jmarkers) { |
Florin Malita | d9c56b4 | 2018-10-09 14:33:08 -0400 | [diff] [blame] | 308 | return; |
| 309 | } |
| 310 | |
Florin Malita | 91af8d8 | 2018-11-30 16:46:45 -0500 | [diff] [blame] | 311 | // For frame-number -> t conversions. |
| 312 | const auto frameRatio = 1 / (fFrameRate * fDuration); |
| 313 | |
| 314 | for (const skjson::ObjectValue* m : *jmarkers) { |
| 315 | if (!m) continue; |
| 316 | |
| 317 | const skjson::StringValue* name = (*m)["cm"]; |
| 318 | const auto time = ParseDefault((*m)["tm"], -1.0f), |
| 319 | duration = ParseDefault((*m)["dr"], -1.0f); |
| 320 | |
| 321 | if (name && time >= 0 && duration >= 0) { |
| 322 | fMarkerObserver->onMarker( |
| 323 | name->begin(), |
| 324 | // "tm" is in frames |
| 325 | time * frameRatio, |
| 326 | // ... as is "dr" |
| 327 | (time + duration) * frameRatio |
| 328 | ); |
Florin Malita | d9c56b4 | 2018-10-09 14:33:08 -0400 | [diff] [blame] | 329 | } else { |
Florin Malita | 91af8d8 | 2018-11-30 16:46:45 -0500 | [diff] [blame] | 330 | this->log(Logger::Level::kWarning, m, "Ignoring unexpected marker."); |
Florin Malita | d9c56b4 | 2018-10-09 14:33:08 -0400 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 335 | bool AnimationBuilder::dispatchColorProperty(const sk_sp<sksg::Color>& c) const { |
| 336 | bool dispatched = false; |
| 337 | |
| 338 | if (fPropertyObserver) { |
| 339 | fPropertyObserver->onColorProperty(fPropertyObserverContext, |
| 340 | [&]() { |
| 341 | dispatched = true; |
| 342 | return std::unique_ptr<ColorPropertyHandle>(new ColorPropertyHandle(c)); |
| 343 | }); |
| 344 | } |
| 345 | |
| 346 | return dispatched; |
| 347 | } |
| 348 | |
| 349 | bool AnimationBuilder::dispatchOpacityProperty(const sk_sp<sksg::OpacityEffect>& o) const { |
| 350 | bool dispatched = false; |
| 351 | |
| 352 | if (fPropertyObserver) { |
| 353 | fPropertyObserver->onOpacityProperty(fPropertyObserverContext, |
| 354 | [&]() { |
| 355 | dispatched = true; |
| 356 | return std::unique_ptr<OpacityPropertyHandle>(new OpacityPropertyHandle(o)); |
| 357 | }); |
| 358 | } |
| 359 | |
| 360 | return dispatched; |
| 361 | } |
| 362 | |
Florin Malita | d2a1853 | 2019-01-02 10:34:49 -0500 | [diff] [blame] | 363 | bool AnimationBuilder::dispatchTransformProperty(const sk_sp<TransformAdapter2D>& t) const { |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 364 | bool dispatched = false; |
| 365 | |
| 366 | if (fPropertyObserver) { |
| 367 | fPropertyObserver->onTransformProperty(fPropertyObserverContext, |
| 368 | [&]() { |
| 369 | dispatched = true; |
| 370 | return std::unique_ptr<TransformPropertyHandle>(new TransformPropertyHandle(t)); |
| 371 | }); |
| 372 | } |
| 373 | |
| 374 | return dispatched; |
| 375 | } |
| 376 | |
| 377 | void AnimationBuilder::AutoPropertyTracker::updateContext(PropertyObserver* observer, |
| 378 | const skjson::ObjectValue& obj) { |
| 379 | |
| 380 | const skjson::StringValue* name = obj["nm"]; |
| 381 | |
| 382 | fBuilder->fPropertyObserverContext = name ? name->begin() : nullptr; |
| 383 | } |
| 384 | |
Florin Malita | fa0441b | 2018-08-21 13:17:27 -0400 | [diff] [blame] | 385 | } // namespace internal |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 386 | |
Florin Malita | 4054351 | 2018-09-19 21:43:29 -0400 | [diff] [blame] | 387 | sk_sp<SkData> ResourceProvider::load(const char[], const char[]) const { |
| 388 | return nullptr; |
| 389 | } |
| 390 | |
Florin Malita | 62c6bd9 | 2018-10-03 14:39:56 -0400 | [diff] [blame] | 391 | sk_sp<ImageAsset> ResourceProvider::loadImageAsset(const char path[], const char name[]) const { |
Florin Malita | 9059c23 | 2018-10-05 10:47:31 -0400 | [diff] [blame] | 392 | return nullptr; |
Florin Malita | 62c6bd9 | 2018-10-03 14:39:56 -0400 | [diff] [blame] | 393 | } |
| 394 | |
Florin Malita | 97b150e | 2018-09-27 10:59:52 -0400 | [diff] [blame] | 395 | sk_sp<SkData> ResourceProvider::loadFont(const char[], const char[]) const { |
Florin Malita | 4054351 | 2018-09-19 21:43:29 -0400 | [diff] [blame] | 396 | return nullptr; |
| 397 | } |
| 398 | |
Florin Malita | 57b9d40 | 2018-10-02 12:48:00 -0400 | [diff] [blame] | 399 | void Logger::log(Level, const char[], const char*) {} |
| 400 | |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 401 | Animation::Builder::Builder() = default; |
| 402 | Animation::Builder::~Builder() = default; |
| 403 | |
Florin Malita | 906cdf3 | 2018-08-23 14:49:05 -0400 | [diff] [blame] | 404 | Animation::Builder& Animation::Builder::setResourceProvider(sk_sp<ResourceProvider> rp) { |
| 405 | fResourceProvider = std::move(rp); |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 406 | return *this; |
| 407 | } |
| 408 | |
| 409 | Animation::Builder& Animation::Builder::setFontManager(sk_sp<SkFontMgr> fmgr) { |
| 410 | fFontMgr = std::move(fmgr); |
| 411 | return *this; |
| 412 | } |
| 413 | |
Florin Malita | a85f3a1 | 2018-09-24 17:24:59 -0400 | [diff] [blame] | 414 | Animation::Builder& Animation::Builder::setPropertyObserver(sk_sp<PropertyObserver> pobserver) { |
| 415 | fPropertyObserver = std::move(pobserver); |
| 416 | return *this; |
| 417 | } |
| 418 | |
Florin Malita | 57b9d40 | 2018-10-02 12:48:00 -0400 | [diff] [blame] | 419 | Animation::Builder& Animation::Builder::setLogger(sk_sp<Logger> logger) { |
| 420 | fLogger = std::move(logger); |
| 421 | return *this; |
| 422 | } |
| 423 | |
Florin Malita | 91af8d8 | 2018-11-30 16:46:45 -0500 | [diff] [blame] | 424 | Animation::Builder& Animation::Builder::setMarkerObserver(sk_sp<MarkerObserver> mobserver) { |
| 425 | fMarkerObserver = std::move(mobserver); |
Florin Malita | d9c56b4 | 2018-10-09 14:33:08 -0400 | [diff] [blame] | 426 | return *this; |
| 427 | } |
| 428 | |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 429 | sk_sp<Animation> Animation::Builder::make(SkStream* stream) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 430 | if (!stream->hasLength()) { |
| 431 | // TODO: handle explicit buffering? |
Florin Malita | 57b9d40 | 2018-10-02 12:48:00 -0400 | [diff] [blame] | 432 | if (fLogger) { |
| 433 | fLogger->log(Logger::Level::kError, "Cannot parse streaming content.\n"); |
| 434 | } |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 435 | return nullptr; |
| 436 | } |
| 437 | |
Florin Malita | 7d42c44 | 2018-06-14 16:16:01 -0400 | [diff] [blame] | 438 | auto data = SkData::MakeFromStream(stream, stream->getLength()); |
| 439 | if (!data) { |
Florin Malita | 57b9d40 | 2018-10-02 12:48:00 -0400 | [diff] [blame] | 440 | if (fLogger) { |
| 441 | fLogger->log(Logger::Level::kError, "Failed to read the input stream.\n"); |
| 442 | } |
Florin Malita | fa7e9a8 | 2018-05-04 15:10:54 -0400 | [diff] [blame] | 443 | return nullptr; |
Florin Malita | 7d42c44 | 2018-06-14 16:16:01 -0400 | [diff] [blame] | 444 | } |
| 445 | |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 446 | return this->make(static_cast<const char*>(data->data()), data->size()); |
Florin Malita | 6e487e6 | 2018-08-06 11:07:05 -0400 | [diff] [blame] | 447 | } |
| 448 | |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 449 | sk_sp<Animation> Animation::Builder::make(const char* data, size_t data_len) { |
Florin Malita | 9d0b306 | 2018-12-13 11:08:39 -0500 | [diff] [blame] | 450 | TRACE_EVENT0("skottie", TRACE_FUNC); |
| 451 | |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 452 | // Sanitize factory args. |
| 453 | class NullResourceProvider final : public ResourceProvider { |
| 454 | sk_sp<SkData> load(const char[], const char[]) const override { return nullptr; } |
| 455 | }; |
Florin Malita | 906cdf3 | 2018-08-23 14:49:05 -0400 | [diff] [blame] | 456 | auto resolvedProvider = fResourceProvider |
| 457 | ? fResourceProvider : sk_make_sp<NullResourceProvider>(); |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 458 | |
| 459 | memset(&fStats, 0, sizeof(struct Stats)); |
| 460 | |
| 461 | fStats.fJsonSize = data_len; |
Florin Malita | 4025710 | 2019-03-07 10:10:38 -0500 | [diff] [blame^] | 462 | const auto t0 = std::chrono::steady_clock::now(); |
Florin Malita | 6e487e6 | 2018-08-06 11:07:05 -0400 | [diff] [blame] | 463 | |
| 464 | const skjson::DOM dom(data, data_len); |
Florin Malita | 7d42c44 | 2018-06-14 16:16:01 -0400 | [diff] [blame] | 465 | if (!dom.root().is<skjson::ObjectValue>()) { |
| 466 | // TODO: more error info. |
Florin Malita | 57b9d40 | 2018-10-02 12:48:00 -0400 | [diff] [blame] | 467 | if (fLogger) { |
| 468 | fLogger->log(Logger::Level::kError, "Failed to parse JSON input.\n"); |
| 469 | } |
Florin Malita | 7d42c44 | 2018-06-14 16:16:01 -0400 | [diff] [blame] | 470 | return nullptr; |
| 471 | } |
| 472 | const auto& json = dom.root().as<skjson::ObjectValue>(); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 473 | |
Florin Malita | 4025710 | 2019-03-07 10:10:38 -0500 | [diff] [blame^] | 474 | const auto t1 = std::chrono::steady_clock::now(); |
| 475 | fStats.fJsonParseTimeMS = std::chrono::duration<float, std::milli>{t1-t0}.count(); |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 476 | |
Florin Malita | cc9c45d | 2018-08-20 10:02:12 -0400 | [diff] [blame] | 477 | const auto version = ParseDefault<SkString>(json["v"], SkString()); |
| 478 | const auto size = SkSize::Make(ParseDefault<float>(json["w"], 0.0f), |
| 479 | ParseDefault<float>(json["h"], 0.0f)); |
| 480 | const auto fps = ParseDefault<float>(json["fr"], -1.0f), |
| 481 | inPoint = ParseDefault<float>(json["ip"], 0.0f), |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 482 | outPoint = SkTMax(ParseDefault<float>(json["op"], SK_ScalarMax), inPoint), |
Florin Malita | 217d4a5 | 2018-09-18 10:05:30 -0400 | [diff] [blame] | 483 | duration = sk_ieee_float_divide(outPoint - inPoint, fps); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 484 | |
Florin Malita | cc9c45d | 2018-08-20 10:02:12 -0400 | [diff] [blame] | 485 | if (size.isEmpty() || version.isEmpty() || fps <= 0 || |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 486 | !SkScalarIsFinite(inPoint) || !SkScalarIsFinite(outPoint) || !SkScalarIsFinite(duration)) { |
Florin Malita | 57b9d40 | 2018-10-02 12:48:00 -0400 | [diff] [blame] | 487 | if (fLogger) { |
| 488 | const auto msg = SkStringPrintf( |
| 489 | "Invalid animation params (version: %s, size: [%f %f], frame rate: %f, " |
| 490 | "in-point: %f, out-point: %f)\n", |
| 491 | version.c_str(), size.width(), size.height(), fps, inPoint, outPoint); |
| 492 | fLogger->log(Logger::Level::kError, msg.c_str()); |
| 493 | } |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 494 | return nullptr; |
| 495 | } |
| 496 | |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 497 | SkASSERT(resolvedProvider); |
Florin Malita | be5e865 | 2018-08-31 12:54:18 -0400 | [diff] [blame] | 498 | internal::AnimationBuilder builder(std::move(resolvedProvider), fFontMgr, |
Florin Malita | 57b9d40 | 2018-10-02 12:48:00 -0400 | [diff] [blame] | 499 | std::move(fPropertyObserver), |
| 500 | std::move(fLogger), |
Florin Malita | 91af8d8 | 2018-11-30 16:46:45 -0500 | [diff] [blame] | 501 | std::move(fMarkerObserver), |
Florin Malita | 57b9d40 | 2018-10-02 12:48:00 -0400 | [diff] [blame] | 502 | &fStats, duration, fps); |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 503 | auto scene = builder.parse(json); |
Florin Malita | c83a0de | 2018-05-31 12:17:55 -0400 | [diff] [blame] | 504 | |
Florin Malita | 4025710 | 2019-03-07 10:10:38 -0500 | [diff] [blame^] | 505 | const auto t2 = std::chrono::steady_clock::now(); |
| 506 | fStats.fSceneParseTimeMS = std::chrono::duration<float, std::milli>{t2-t1}.count(); |
| 507 | fStats.fTotalLoadTimeMS = std::chrono::duration<float, std::milli>{t2-t0}.count(); |
Florin Malita | 6eb85a1 | 2018-04-30 10:32:18 -0400 | [diff] [blame] | 508 | |
Florin Malita | 57b9d40 | 2018-10-02 12:48:00 -0400 | [diff] [blame] | 509 | if (!scene && fLogger) { |
| 510 | fLogger->log(Logger::Level::kError, "Could not parse animation.\n"); |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 511 | } |
| 512 | |
Florin Malita | bd64f18 | 2019-02-25 09:55:04 -0500 | [diff] [blame] | 513 | uint32_t flags = 0; |
| 514 | if (builder.hasNontrivialBlending()) { |
| 515 | flags |= Flags::kRequiresTopLevelIsolation; |
| 516 | } |
| 517 | |
| 518 | return sk_sp<Animation>(new Animation(std::move(scene), |
| 519 | std::move(version), |
| 520 | size, |
| 521 | inPoint, |
| 522 | outPoint, |
| 523 | duration, |
| 524 | flags)); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 525 | } |
| 526 | |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 527 | sk_sp<Animation> Animation::Builder::makeFromFile(const char path[]) { |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 528 | const auto data = SkData::MakeFromFileName(path); |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 529 | |
Florin Malita | a831655 | 2018-11-09 16:19:44 -0500 | [diff] [blame] | 530 | return data ? this->make(static_cast<const char*>(data->data()), data->size()) |
| 531 | : nullptr; |
Florin Malita | 4932807 | 2018-01-08 12:51:12 -0500 | [diff] [blame] | 532 | } |
| 533 | |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 534 | Animation::Animation(std::unique_ptr<sksg::Scene> scene, SkString version, const SkSize& size, |
Florin Malita | bd64f18 | 2019-02-25 09:55:04 -0500 | [diff] [blame] | 535 | SkScalar inPoint, SkScalar outPoint, SkScalar duration, uint32_t flags) |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 536 | : fScene(std::move(scene)) |
| 537 | , fVersion(std::move(version)) |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 538 | , fSize(size) |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 539 | , fInPoint(inPoint) |
| 540 | , fOutPoint(outPoint) |
Florin Malita | bd64f18 | 2019-02-25 09:55:04 -0500 | [diff] [blame] | 541 | , fDuration(duration) |
| 542 | , fFlags(flags) { |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 543 | |
Florin Malita | db38573 | 2018-01-09 12:19:32 -0500 | [diff] [blame] | 544 | // In case the client calls render before the first tick. |
Florin Malita | a33447d | 2018-05-29 13:46:54 -0400 | [diff] [blame] | 545 | this->seek(0); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | Animation::~Animation() = default; |
| 549 | |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 550 | void Animation::setShowInval(bool show) { |
| 551 | if (fScene) { |
| 552 | fScene->setShowInval(show); |
| 553 | } |
| 554 | } |
| 555 | |
Mike Reed | 2985987 | 2018-01-08 08:25:27 -0500 | [diff] [blame] | 556 | void Animation::render(SkCanvas* canvas, const SkRect* dstR) const { |
Florin Malita | bd64f18 | 2019-02-25 09:55:04 -0500 | [diff] [blame] | 557 | this->render(canvas, dstR, 0); |
| 558 | } |
| 559 | |
| 560 | void Animation::render(SkCanvas* canvas, const SkRect* dstR, RenderFlags renderFlags) const { |
Florin Malita | 9d0b306 | 2018-12-13 11:08:39 -0500 | [diff] [blame] | 561 | TRACE_EVENT0("skottie", TRACE_FUNC); |
| 562 | |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 563 | if (!fScene) |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 564 | return; |
| 565 | |
Mike Reed | 2985987 | 2018-01-08 08:25:27 -0500 | [diff] [blame] | 566 | SkAutoCanvasRestore restore(canvas, true); |
Florin Malita | bd64f18 | 2019-02-25 09:55:04 -0500 | [diff] [blame] | 567 | |
Mike Reed | 2985987 | 2018-01-08 08:25:27 -0500 | [diff] [blame] | 568 | const SkRect srcR = SkRect::MakeSize(this->size()); |
| 569 | if (dstR) { |
| 570 | canvas->concat(SkMatrix::MakeRectToRect(srcR, *dstR, SkMatrix::kCenter_ScaleToFit)); |
| 571 | } |
Florin Malita | bd64f18 | 2019-02-25 09:55:04 -0500 | [diff] [blame] | 572 | |
| 573 | if ((fFlags & Flags::kRequiresTopLevelIsolation) && |
| 574 | !(renderFlags & RenderFlag::kSkipTopLevelIsolation)) { |
| 575 | // The animation uses non-trivial blending, and needs |
| 576 | // to be rendered into a separate/transparent layer. |
| 577 | canvas->saveLayer(srcR, nullptr); |
| 578 | } |
| 579 | |
Mike Reed | 2985987 | 2018-01-08 08:25:27 -0500 | [diff] [blame] | 580 | canvas->clipRect(srcR); |
Florin Malita | bd64f18 | 2019-02-25 09:55:04 -0500 | [diff] [blame] | 581 | |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 582 | fScene->render(canvas); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 583 | } |
| 584 | |
Florin Malita | a33447d | 2018-05-29 13:46:54 -0400 | [diff] [blame] | 585 | void Animation::seek(SkScalar t) { |
Florin Malita | 9d0b306 | 2018-12-13 11:08:39 -0500 | [diff] [blame] | 586 | TRACE_EVENT0("skottie", TRACE_FUNC); |
| 587 | |
Florin Malita | 35efaa8 | 2018-01-22 12:57:06 -0500 | [diff] [blame] | 588 | if (!fScene) |
| 589 | return; |
| 590 | |
Florin Malita | a33447d | 2018-05-29 13:46:54 -0400 | [diff] [blame] | 591 | fScene->animate(fInPoint + SkTPin(t, 0.0f, 1.0f) * (fOutPoint - fInPoint)); |
Florin Malita | 094ccde | 2017-12-30 12:27:00 -0500 | [diff] [blame] | 592 | } |
| 593 | |
Florin Malita | 40c3742 | 2018-08-22 20:37:04 -0400 | [diff] [blame] | 594 | sk_sp<Animation> Animation::Make(const char* data, size_t length) { |
| 595 | return Builder().make(data, length); |
| 596 | } |
| 597 | |
| 598 | sk_sp<Animation> Animation::Make(SkStream* stream) { |
| 599 | return Builder().make(stream); |
| 600 | } |
| 601 | |
| 602 | sk_sp<Animation> Animation::MakeFromFile(const char path[]) { |
| 603 | return Builder().makeFromFile(path); |
| 604 | } |
| 605 | |
Florin Malita | 54f65c4 | 2018-01-16 17:04:30 -0500 | [diff] [blame] | 606 | } // namespace skottie |