From a55a591e5ab2a89baea017d987a497570e6803d6 Mon Sep 17 00:00:00 2001 From: Florin Malita Date: Wed, 24 Jan 2018 19:07:59 -0500 Subject: [skottie] Cache the last keyframe We can avoid searching on every tick. TBR= Change-Id: Ifc3ff40f1f5ec2bf865c09a8e784223aa8a96674 Reviewed-on: https://skia-review.googlesource.com/99580 Reviewed-by: Florin Malita Commit-Queue: Florin Malita --- experimental/skottie/SkottieAnimator.h | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'experimental') diff --git a/experimental/skottie/SkottieAnimator.h b/experimental/skottie/SkottieAnimator.h index 7ea4929280..8320ec499e 100644 --- a/experimental/skottie/SkottieAnimator.h +++ b/experimental/skottie/SkottieAnimator.h @@ -130,10 +130,12 @@ public: } void onTick(float t) override { - const auto& frame = this->findFrame(t); + if (!fCurrentFrame || !fCurrentFrame->contains(t)) { + fCurrentFrame = this->findFrame(t); + } T val; - frame.eval(t, &val); + fCurrentFrame->eval(t, &val); fFunc(val); } @@ -143,18 +145,17 @@ private: : fFrames(std::move(frames)) , fFunc(std::move(applyFunc)) {} - const KeyframeInterval& findFrame(float t) const; + const KeyframeInterval* findFrame(float t) const; const SkTArray, true> fFrames; - const ApplyFuncT fFunc; + const ApplyFuncT fFunc; + const KeyframeInterval* fCurrentFrame = nullptr; }; template -const KeyframeInterval& Animator::findFrame(float t) const { +const KeyframeInterval* Animator::findFrame(float t) const { SkASSERT(!fFrames.empty()); - // TODO: cache last/current frame? - auto f0 = fFrames.begin(), f1 = fFrames.end() - 1; @@ -162,11 +163,11 @@ const KeyframeInterval& Animator::findFrame(float t) const { SkASSERT(f1->isValid()); if (t < f0->t0()) { - return *f0; + return f0; } if (t > f1->t1()) { - return *f1; + return f1; } while (f0 != f1) { @@ -186,7 +187,7 @@ const KeyframeInterval& Animator::findFrame(float t) const { SkASSERT(f0 == f1); SkASSERT(f0->contains(t)); - return *f0; + return f0; } } // namespace skottie -- cgit v1.2.3