| 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 |
|
| 8 |
#include "modules/skottie/include/Skottie.h" |
| 9 |
|
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkData.h" |
| 12 |
#include "include/core/SkFontMgr.h" |
| 13 |
#include "include/core/SkMatrix.h" |
| 14 |
#include "include/core/SkRect.h" |
| 15 |
#include "include/core/SkStream.h" |
| 16 |
#include "include/private/base/SkDebug.h" |
| 17 |
#include "include/private/base/SkFloatingPoint.h" |
| 18 |
#include "include/private/base/SkTPin.h" |
| 19 |
#include "include/private/base/SkTo.h" |
| 20 |
#include "modules/jsonreader/SkJSONReader.h" |
| 21 |
#include "modules/skottie/include/ExternalLayer.h" |
| 22 |
#include "modules/skottie/include/SkottieProperty.h" |
| 23 |
#include "modules/skottie/include/SlotManager.h" |
| 24 |
#include "modules/skottie/src/Adapter.h" |
| 25 |
#include "modules/skottie/src/Composition.h" |
| 26 |
#include "modules/skottie/src/SkottieJson.h" |
| 27 |
#include "modules/skottie/src/SkottiePriv.h" |
| 28 |
#include "modules/skottie/src/SkottieValue.h" |
| 29 |
#include "modules/skottie/src/Transform.h" // IWYU pragma: keep |
| 30 |
#include "modules/skottie/src/animator/Animator.h" |
| 31 |
#include "modules/skottie/src/text/TextAdapter.h" |
| 32 |
#include "modules/sksg/include/SkSGOpacityEffect.h" |
| 33 |
#include "modules/sksg/include/SkSGRenderNode.h" |
| 34 |
#include "modules/skshaper/include/SkShaper_factory.h" |
| 35 |
#include "src/core/SkTHash.h" |
| 36 |
#include "src/core/SkTraceEvent.h" |
| 37 |
|
| 38 |
#include <algorithm> |
| 39 |
#include <chrono> |
| 40 |
#include <cmath> |
| 41 |
#include <cstdarg> |
| 42 |
#include <cstdio> |
| 43 |
#include <cstring> |
| 44 |
#include <functional> |
| 45 |
#include <memory> |
| 46 |
#include <ratio> |
| 47 |
#include <utility> |
| 48 |
|
| 49 |
#if !defined(SK_DISABLE_LEGACY_SHAPER_FACTORY) |
| 50 |
#include "modules/skshaper/utils/FactoryHelpers.h" |
| 51 |
#endif |
| 52 |
|
| 53 |
namespace sksg { |
| 54 |
class Color; |
| 55 |
} |
| 56 |
|
| 57 |
namespace skottie { |
| 58 |
|
| 59 |
namespace internal { |
| 60 |
|
| 61 |
void SceneGraphRevalidator::setRoot(sk_sp<sksg::RenderNode> root) { |
| 62 |
fRoot = std::move(root); |
| 63 |
} |
| 64 |
|
| 65 |
void SceneGraphRevalidator::revalidate() { |
| 66 |
if (fRoot) { |
| 67 |
fRoot->revalidate(nullptr, SkMatrix::I()); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
void AnimationBuilder::log(Logger::Level lvl, const skjson::Value* json, |
| 72 |
const char fmt[], ...) const { |
| 73 |
if (!fLogger) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
char buff[1024]; |
| 78 |
va_list va; |
| 79 |
va_start(va, fmt); |
| 80 |
const auto len = vsnprintf(buff, sizeof(buff), fmt, va); |
| 81 |
va_end(va); |
| 82 |
|
| 83 |
if (len < 0) { |
| 84 |
SkDebugf("!! Could not format log message !!\n"); |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
if (len >= SkToInt(sizeof(buff))) { |
| 89 |
static constexpr char kEllipsesStr[] = "..."; |
| 90 |
strcpy(buff + sizeof(buff) - sizeof(kEllipsesStr), kEllipsesStr); |
| 91 |
} |
| 92 |
|
| 93 |
SkString jsonstr = json ? json->toString() : SkString(); |
| 94 |
|
| 95 |
fLogger->log(lvl, buff, jsonstr.c_str()); |
| 96 |
} |
| 97 |
|
| 98 |
class OpacityAdapter final : public DiscardableAdapterBase<OpacityAdapter, sksg::OpacityEffect> { |
| 99 |
public: |
| 100 |
OpacityAdapter(const skjson::ObjectValue& jobject, |
| 101 |
sk_sp<sksg::RenderNode> child, |
| 102 |
const AnimationBuilder& abuilder) |
| 103 |
: INHERITED(sksg::OpacityEffect::Make(std::move(child))) { |
| 104 |
this->bind(abuilder, jobject["o"], fOpacity); |
| 105 |
} |
| 106 |
|
| 107 |
private: |
| 108 |
void onSync() override { |
| 109 |
this->node()->setOpacity(fOpacity * 0.01f); |
| 110 |
} |
| 111 |
|
| 112 |
ScalarValue fOpacity = 100; |
| 113 |
|
| 114 |
using INHERITED = DiscardableAdapterBase<OpacityAdapter, sksg::OpacityEffect>; |
| 115 |
}; |
| 116 |
|
| 117 |
|
| 118 |
sk_sp<sksg::RenderNode> AnimationBuilder::attachOpacity(const skjson::ObjectValue& jobject, |
| 119 |
sk_sp<sksg::RenderNode> child_node) const { |
| 120 |
if (!child_node) |
| 121 |
return nullptr; |
| 122 |
|
| 123 |
auto adapter = OpacityAdapter::Make(jobject, child_node, *this); |
| 124 |
if (adapter->isStatic()) { |
| 125 |
adapter->seek(0); |
| 126 |
} |
| 127 |
auto dispatched = this->dispatchOpacityProperty(adapter->node()); |
| 128 |
if (adapter->isStatic()) { |
| 129 |
if (!dispatched && adapter->node()->getOpacity() >= 1) { |
| 130 |
// No obeservable effects - we can discard. |
| 131 |
return child_node; |
| 132 |
} |
| 133 |
} else { |
| 134 |
fCurrentAnimatorScope->push_back(adapter); |
| 135 |
} |
| 136 |
|
| 137 |
return adapter->node(); |
| 138 |
} |
| 139 |
|
| 140 |
AnimationBuilder::AnimationBuilder(sk_sp<ResourceProvider> rp, sk_sp<SkFontMgr> fontmgr, |
| 141 |
sk_sp<PropertyObserver> pobserver, sk_sp<Logger> logger, |
| 142 |
sk_sp<MarkerObserver> mobserver, sk_sp<PrecompInterceptor> pi, |
| 143 |
sk_sp<ExpressionManager> expressionmgr, |
| 144 |
sk_sp<SkShapers::Factory> shapingFactory, |
| 145 |
Animation::Builder::Stats* stats, |
| 146 |
const SkSize& comp_size, float duration, float framerate, |
| 147 |
uint32_t flags) |
| 148 |
: fResourceProvider(std::move(rp)) |
| 149 |
, fFontMgr(std::move(fontmgr)) |
| 150 |
, fPropertyObserver(std::move(pobserver)) |
| 151 |
, fLogger(std::move(logger)) |
| 152 |
, fMarkerObserver(std::move(mobserver)) |
| 153 |
, fPrecompInterceptor(std::move(pi)) |
| 154 |
, fExpressionManager(std::move(expressionmgr)) |
| 155 |
, fShapingFactory(std::move(shapingFactory)) |
| 156 |
, fRevalidator(sk_make_sp<SceneGraphRevalidator>()) |
| 157 |
, fSlotManager(sk_make_sp<SlotManager>(fRevalidator)) |
| 158 |
, fStats(stats) |
| 159 |
, fCompSize(comp_size) |
| 160 |
, fDuration(duration) |
| 161 |
, fFrameRate(framerate) |
| 162 |
, fFlags(flags) |
| 163 |
, fHasNontrivialBlending(false) {} |
| 164 |
|
| 165 |
AnimationBuilder::AnimationInfo AnimationBuilder::parse(const skjson::ObjectValue& jroot) { |
| 166 |
this->dispatchMarkers(jroot["markers"]); |
| 167 |
|
| 168 |
AutoScope ascope(this); |
| 169 |
AutoPropertyTracker apt(this, jroot, PropertyObserver::NodeType::COMPOSITION); |
| 170 |
|
| 171 |
this->parseAssets(jroot["assets"]); |
| 172 |
this->parseFonts(jroot["fonts"], jroot["chars"]); |
| 173 |
fSlotsRoot = jroot["slots"]; |
| 174 |
|
| 175 |
auto root = CompositionBuilder(*this, fCompSize, jroot).build(*this); |
| 176 |
|
| 177 |
auto animators = ascope.release(); |
| 178 |
fStats->fAnimatorCount = animators.size(); |
| 179 |
|
| 180 |
// Point the revalidator to our final root, and perform initial revalidation. |
| 181 |
fRevalidator->setRoot(root); |
| 182 |
fRevalidator->revalidate(); |
| 183 |
|
| 184 |
return { std::move(root), std::move(animators), std::move(fSlotManager), std::move(fLayerInfo)}; |
| 185 |
} |
| 186 |
|
| 187 |
void AnimationBuilder::parseAssets(const skjson::ArrayValue* jassets) { |
| 188 |
if (!jassets) { |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
for (const skjson::ObjectValue* asset : *jassets) { |
| 193 |
if (asset) { |
| 194 |
fAssets.set(ParseDefault<SkString>((*asset)["id"], SkString()), { asset, false }); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
void AnimationBuilder::dispatchMarkers(const skjson::ArrayValue* jmarkers) const { |
| 200 |
if (!fMarkerObserver || !jmarkers) { |
| 201 |
return; |
| 202 |
} |
| 203 |
|
| 204 |
// For frame-number -> t conversions. |
| 205 |
const auto frameRatio = 1 / (fFrameRate * fDuration); |
| 206 |
|
| 207 |
for (const skjson::ObjectValue* m : *jmarkers) { |
| 208 |
if (!m) continue; |
| 209 |
|
| 210 |
const skjson::StringValue* name = (*m)["cm"]; |
| 211 |
const auto time = ParseDefault((*m)["tm"], -1.0f), |
| 212 |
duration = ParseDefault((*m)["dr"], -1.0f); |
| 213 |
|
| 214 |
if (name && time >= 0 && duration >= 0) { |
| 215 |
fMarkerObserver->onMarker( |
| 216 |
name->begin(), |
| 217 |
// "tm" is in frames |
| 218 |
time * frameRatio, |
| 219 |
// ... as is "dr" |
| 220 |
(time + duration) * frameRatio |
| 221 |
); |
| 222 |
} else { |
| 223 |
this->log(Logger::Level::kWarning, m, "Ignoring unexpected marker."); |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
bool AnimationBuilder::dispatchColorProperty(const sk_sp<sksg::Color>& c) const { |
| 229 |
bool dispatched = false; |
| 230 |
if (fPropertyObserver) { |
| 231 |
const char * node_name = fPropertyObserverContext; |
| 232 |
fPropertyObserver->onColorProperty(node_name, |
| 233 |
[&]() { |
| 234 |
dispatched = true; |
| 235 |
return std::make_unique<ColorPropertyHandle>(c, fRevalidator); |
| 236 |
}); |
| 237 |
} |
| 238 |
|
| 239 |
return dispatched; |
| 240 |
} |
| 241 |
|
| 242 |
bool AnimationBuilder::dispatchOpacityProperty(const sk_sp<sksg::OpacityEffect>& o) const { |
| 243 |
bool dispatched = false; |
| 244 |
|
| 245 |
if (fPropertyObserver) { |
| 246 |
fPropertyObserver->onOpacityProperty(fPropertyObserverContext, |
| 247 |
[&]() { |
| 248 |
dispatched = true; |
| 249 |
return std::make_unique<OpacityPropertyHandle>(o, fRevalidator); |
| 250 |
}); |
| 251 |
} |
| 252 |
|
| 253 |
return dispatched; |
| 254 |
} |
| 255 |
|
| 256 |
bool AnimationBuilder::dispatchTextProperty(const sk_sp<TextAdapter>& t, |
| 257 |
const skjson::ObjectValue* jtext) const { |
| 258 |
bool dispatched = false; |
| 259 |
|
| 260 |
if (jtext) { |
| 261 |
if (const skjson::StringValue* slotID = (*jtext)["sid"]) { |
| 262 |
fSlotManager->trackTextValue(SkString(slotID->begin()), t); |
| 263 |
dispatched = true; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
if (fPropertyObserver) { |
| 268 |
fPropertyObserver->onTextProperty(fPropertyObserverContext, |
| 269 |
[&]() { |
| 270 |
dispatched = true; |
| 271 |
return std::make_unique<TextPropertyHandle>(t, fRevalidator); |
| 272 |
}); |
| 273 |
} |
| 274 |
|
| 275 |
return dispatched; |
| 276 |
} |
| 277 |
|
| 278 |
bool AnimationBuilder::dispatchTransformProperty(const sk_sp<TransformAdapter2D>& t) const { |
| 279 |
bool dispatched = false; |
| 280 |
|
| 281 |
if (fPropertyObserver) { |
| 282 |
fPropertyObserver->onTransformProperty(fPropertyObserverContext, |
| 283 |
[&]() { |
| 284 |
dispatched = true; |
| 285 |
return std::make_unique<TransformPropertyHandle>(t, fRevalidator); |
| 286 |
}); |
| 287 |
} |
| 288 |
|
| 289 |
return dispatched; |
| 290 |
} |
| 291 |
|
| 292 |
sk_sp<ExpressionManager> AnimationBuilder::expression_manager() const { |
| 293 |
return fExpressionManager; |
| 294 |
} |
| 295 |
|
| 296 |
void AnimationBuilder::AutoPropertyTracker::updateContext(PropertyObserver* observer, |
| 297 |
const skjson::ObjectValue& obj) { |
| 298 |
const skjson::StringValue* name = obj["nm"]; |
| 299 |
fBuilder->fPropertyObserverContext = name ? name->begin() : fPrevContext; |
| 300 |
} |
| 301 |
|
| 302 |
} // namespace internal |
| 303 |
|
| 304 |
Animation::Builder::Builder(uint32_t flags) : fFlags(flags) {} |
| 305 |
Animation::Builder::Builder(const Builder&) = default; |
| 306 |
Animation::Builder::Builder(Builder&&) = default; |
| 307 |
Animation::Builder::~Builder() = default; |
| 308 |
|
| 309 |
Animation::Builder& Animation::Builder::setResourceProvider(sk_sp<ResourceProvider> rp) { |
| 310 |
fResourceProvider = std::move(rp); |
| 311 |
return *this; |
| 312 |
} |
| 313 |
|
| 314 |
Animation::Builder& Animation::Builder::setFontManager(sk_sp<SkFontMgr> fmgr) { |
| 315 |
fFontMgr = std::move(fmgr); |
| 316 |
return *this; |
| 317 |
} |
| 318 |
|
| 319 |
Animation::Builder& Animation::Builder::setPropertyObserver(sk_sp<PropertyObserver> pobserver) { |
| 320 |
fPropertyObserver = std::move(pobserver); |
| 321 |
return *this; |
| 322 |
} |
| 323 |
|
| 324 |
Animation::Builder& Animation::Builder::setLogger(sk_sp<Logger> logger) { |
| 325 |
fLogger = std::move(logger); |
| 326 |
return *this; |
| 327 |
} |
| 328 |
|
| 329 |
Animation::Builder& Animation::Builder::setMarkerObserver(sk_sp<MarkerObserver> mobserver) { |
| 330 |
fMarkerObserver = std::move(mobserver); |
| 331 |
return *this; |
| 332 |
} |
| 333 |
|
| 334 |
Animation::Builder& Animation::Builder::setPrecompInterceptor(sk_sp<PrecompInterceptor> pi) { |
| 335 |
fPrecompInterceptor = std::move(pi); |
| 336 |
return *this; |
| 337 |
} |
| 338 |
|
| 339 |
Animation::Builder& Animation::Builder::setExpressionManager(sk_sp<ExpressionManager> em) { |
| 340 |
fExpressionManager = std::move(em); |
| 341 |
return *this; |
| 342 |
} |
| 343 |
|
| 344 |
Animation::Builder& Animation::Builder::setTextShapingFactory(sk_sp<SkShapers::Factory> factory) { |
| 345 |
fShapingFactory = std::move(factory); |
| 346 |
return *this; |
| 347 |
} |
| 348 |
|
| 349 |
sk_sp<Animation> Animation::Builder::make(SkStream* stream) { |
| 350 |
if (!stream->hasLength()) { |
| 351 |
// TODO: handle explicit buffering? |
| 352 |
if (fLogger) { |
| 353 |
fLogger->log(Logger::Level::kError, "Cannot parse streaming content.\n"); |
| 354 |
} |
| 355 |
return nullptr; |
| 356 |
} |
| 357 |
|
| 358 |
auto data = SkData::MakeFromStream(stream, stream->getLength()); |
| 359 |
if (!data) { |
| 360 |
if (fLogger) { |
| 361 |
fLogger->log(Logger::Level::kError, "Failed to read the input stream.\n"); |
| 362 |
} |
| 363 |
return nullptr; |
| 364 |
} |
| 365 |
|
| 366 |
return this->make(static_cast<const char*>(data->data()), data->size()); |
| 367 |
} |
| 368 |
|
| 369 |
sk_sp<Animation> Animation::Builder::make(const char* data, size_t data_len) { |
| 370 |
TRACE_EVENT0("skottie", TRACE_FUNC); |
| 371 |
|
| 372 |
// Sanitize factory args. |
| 373 |
class NullResourceProvider final : public ResourceProvider { |
| 374 |
sk_sp<SkData> load(const char[], const char[]) const override { return nullptr; } |
| 375 |
}; |
| 376 |
auto resolvedProvider = fResourceProvider |
| 377 |
? fResourceProvider : sk_make_sp<NullResourceProvider>(); |
| 378 |
|
| 379 |
fStats = Stats{}; |
| 380 |
|
| 381 |
fStats.fJsonSize = data_len; |
| 382 |
const auto t0 = std::chrono::steady_clock::now(); |
| 383 |
|
| 384 |
const skjson::DOM dom(data, data_len); |
| 385 |
if (!dom.root().is<skjson::ObjectValue>()) { |
| 386 |
// TODO: more error info. |
| 387 |
if (fLogger) { |
| 388 |
fLogger->log(Logger::Level::kError, "Failed to parse JSON input.\n"); |
| 389 |
} |
| 390 |
return nullptr; |
| 391 |
} |
| 392 |
const auto& json = dom.root().as<skjson::ObjectValue>(); |
| 393 |
|
| 394 |
const auto t1 = std::chrono::steady_clock::now(); |
| 395 |
fStats.fJsonParseTimeMS = std::chrono::duration<float, std::milli>{t1-t0}.count(); |
| 396 |
|
| 397 |
const auto version = ParseDefault<SkString>(json["v"], SkString()); |
| 398 |
const auto size = SkSize::Make(ParseDefault<float>(json["w"], 0.0f), |
| 399 |
ParseDefault<float>(json["h"], 0.0f)); |
| 400 |
const auto fps = ParseDefault<float>(json["fr"], -1.0f), |
| 401 |
inPoint = ParseDefault<float>(json["ip"], 0.0f), |
| 402 |
outPoint = std::max(ParseDefault<float>(json["op"], SK_ScalarMax), inPoint), |
| 403 |
duration = sk_ieee_float_divide(outPoint - inPoint, fps); |
| 404 |
|
| 405 |
if (size.isEmpty() || fps <= 0 || |
| 406 |
!SkIsFinite(inPoint, outPoint, duration)) { |
| 407 |
if (fLogger) { |
| 408 |
const auto msg = SkStringPrintf( |
| 409 |
"Invalid animation params (size: [%f %f], frame rate: %f, " |
| 410 |
"in-point: %f, out-point: %f)\n", |
| 411 |
size.width(), size.height(), fps, inPoint, outPoint); |
| 412 |
fLogger->log(Logger::Level::kError, msg.c_str()); |
| 413 |
} |
| 414 |
return nullptr; |
| 415 |
} |
| 416 |
|
| 417 |
#if defined(SK_DISABLE_LEGACY_SHAPER_FACTORY) |
| 418 |
auto factory = fShapingFactory ? fShapingFactory : ::SkShapers::Primitive::Factory(); |
| 419 |
#else |
| 420 |
auto factory = fShapingFactory ? fShapingFactory : ::SkShapers::BestAvailable(); |
| 421 |
#endif |
| 422 |
SkASSERT(resolvedProvider); |
| 423 |
internal::AnimationBuilder builder(std::move(resolvedProvider), fFontMgr, |
| 424 |
std::move(fPropertyObserver), |
| 425 |
std::move(fLogger), |
| 426 |
std::move(fMarkerObserver), |
| 427 |
std::move(fPrecompInterceptor), |
| 428 |
std::move(fExpressionManager), |
| 429 |
std::move(factory), |
| 430 |
&fStats, size, duration, fps, fFlags); |
| 431 |
auto ainfo = builder.parse(json); |
| 432 |
|
| 433 |
fSlotManager = ainfo.fSlotManager; |
| 434 |
fLayerInfo = std::move(ainfo.fLayerInfo); |
| 435 |
|
| 436 |
const auto t2 = std::chrono::steady_clock::now(); |
| 437 |
fStats.fSceneParseTimeMS = std::chrono::duration<float, std::milli>{t2-t1}.count(); |
| 438 |
fStats.fTotalLoadTimeMS = std::chrono::duration<float, std::milli>{t2-t0}.count(); |
| 439 |
|
| 440 |
if (!ainfo.fSceneRoot && fLogger) { |
| 441 |
fLogger->log(Logger::Level::kError, "Could not parse animation.\n"); |
| 442 |
} |
| 443 |
|
| 444 |
uint32_t flags = 0; |
| 445 |
if (builder.hasNontrivialBlending()) { |
| 446 |
flags |= Animation::Flags::kRequiresTopLevelIsolation; |
| 447 |
} |
| 448 |
|
| 449 |
return sk_sp<Animation>(new Animation(std::move(ainfo.fSceneRoot), |
| 450 |
std::move(ainfo.fAnimators), |
| 451 |
std::move(version), |
| 452 |
size, |
| 453 |
inPoint, |
| 454 |
outPoint, |
| 455 |
duration, |
| 456 |
fps, |
| 457 |
flags)); |
| 458 |
} |
| 459 |
|
| 460 |
sk_sp<Animation> Animation::Builder::makeFromFile(const char path[]) { |
| 461 |
const auto data = SkData::MakeFromFileName(path); |
| 462 |
|
| 463 |
return data ? this->make(static_cast<const char*>(data->data()), data->size()) |
| 464 |
: nullptr; |
| 465 |
} |
| 466 |
|
| 467 |
Animation::Animation(sk_sp<sksg::RenderNode> scene_root, |
| 468 |
std::vector<sk_sp<internal::Animator>>&& animators, |
| 469 |
SkString version, const SkSize& size, |
| 470 |
double inPoint, double outPoint, double duration, double fps, uint32_t flags) |
| 471 |
: fSceneRoot(std::move(scene_root)) |
| 472 |
, fAnimators(std::move(animators)) |
| 473 |
, fVersion(std::move(version)) |
| 474 |
, fSize(size) |
| 475 |
, fInPoint(inPoint) |
| 476 |
, fOutPoint(outPoint) |
| 477 |
, fDuration(duration) |
| 478 |
, fFPS(fps) |
| 479 |
, fFlags(flags) {} |
| 480 |
|
| 481 |
Animation::~Animation() = default; |
| 482 |
|
| 483 |
void Animation::render(SkCanvas* canvas, const SkRect* dstR) const { |
| 484 |
this->render(canvas, dstR, 0); |
| 485 |
} |
| 486 |
|
| 487 |
void Animation::render(SkCanvas* canvas, const SkRect* dstR, RenderFlags renderFlags) const { |
| 488 |
TRACE_EVENT0("skottie", TRACE_FUNC); |
| 489 |
|
| 490 |
if (!fSceneRoot) |
| 491 |
return; |
| 492 |
|
| 493 |
SkAutoCanvasRestore restore(canvas, true); |
| 494 |
|
| 495 |
const SkRect srcR = SkRect::MakeSize(this->size()); |
| 496 |
if (dstR) { |
| 497 |
canvas->concat(SkMatrix::RectToRectOrIdentity(srcR, *dstR, SkMatrix::kCenter_ScaleToFit)); |
| 498 |
} |
| 499 |
|
| 500 |
if (!(renderFlags & RenderFlag::kDisableTopLevelClipping)) { |
| 501 |
canvas->clipRect(srcR); |
| 502 |
} |
| 503 |
|
| 504 |
if ((fFlags & Flags::kRequiresTopLevelIsolation) && |
| 505 |
!(renderFlags & RenderFlag::kSkipTopLevelIsolation)) { |
| 506 |
// The animation uses non-trivial blending, and needs |
| 507 |
// to be rendered into a separate/transparent layer. |
| 508 |
canvas->saveLayer(srcR, nullptr); |
| 509 |
} |
| 510 |
|
| 511 |
fSceneRoot->render(canvas); |
| 512 |
} |
| 513 |
|
| 514 |
void Animation::seekFrame(double t, sksg::InvalidationController* ic) { |
| 515 |
TRACE_EVENT0("skottie", TRACE_FUNC); |
| 516 |
|
| 517 |
if (!fSceneRoot) |
| 518 |
return; |
| 519 |
|
| 520 |
// Per AE/Lottie semantics out_point is exclusive. |
| 521 |
const auto kLastValidFrame = std::nextafterf(fOutPoint, fInPoint), |
| 522 |
comp_time = SkTPin<float>(fInPoint + t, fInPoint, kLastValidFrame); |
| 523 |
|
| 524 |
for (const auto& anim : fAnimators) { |
| 525 |
anim->seek(comp_time); |
| 526 |
} |
| 527 |
|
| 528 |
fSceneRoot->revalidate(ic, SkMatrix::I()); |
| 529 |
} |
| 530 |
|
| 531 |
void Animation::seekFrameTime(double t, sksg::InvalidationController* ic) { |
| 532 |
this->seekFrame(t * fFPS, ic); |
| 533 |
} |
| 534 |
|
| 535 |
sk_sp<Animation> Animation::Make(const char* data, size_t length) { |
| 536 |
return Builder().make(data, length); |
| 537 |
} |
| 538 |
|
| 539 |
sk_sp<Animation> Animation::Make(SkStream* stream) { |
| 540 |
return Builder().make(stream); |
| 541 |
} |
| 542 |
|
| 543 |
sk_sp<Animation> Animation::MakeFromFile(const char path[]) { |
| 544 |
return Builder().makeFromFile(path); |
| 545 |
} |
| 546 |
|
| 547 |
} // namespace skottie |