[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 <fmalita@chromium.org>
Commit-Queue: Florin Malita <fmalita@chromium.org>
This commit is contained in:
Florin Malita 2018-01-24 19:07:59 -05:00 committed by Skia Commit-Bot
parent 531b3c0a4e
commit a55a591e5a

View File

@ -130,10 +130,12 @@ public:
} }
void onTick(float t) override { void onTick(float t) override {
const auto& frame = this->findFrame(t); if (!fCurrentFrame || !fCurrentFrame->contains(t)) {
fCurrentFrame = this->findFrame(t);
}
T val; T val;
frame.eval(t, &val); fCurrentFrame->eval(t, &val);
fFunc(val); fFunc(val);
} }
@ -143,18 +145,17 @@ private:
: fFrames(std::move(frames)) : fFrames(std::move(frames))
, fFunc(std::move(applyFunc)) {} , fFunc(std::move(applyFunc)) {}
const KeyframeInterval<T>& findFrame(float t) const; const KeyframeInterval<T>* findFrame(float t) const;
const SkTArray<KeyframeInterval<T>, true> fFrames; const SkTArray<KeyframeInterval<T>, true> fFrames;
const ApplyFuncT fFunc; const ApplyFuncT fFunc;
const KeyframeInterval<T>* fCurrentFrame = nullptr;
}; };
template <typename T> template <typename T>
const KeyframeInterval<T>& Animator<T>::findFrame(float t) const { const KeyframeInterval<T>* Animator<T>::findFrame(float t) const {
SkASSERT(!fFrames.empty()); SkASSERT(!fFrames.empty());
// TODO: cache last/current frame?
auto f0 = fFrames.begin(), auto f0 = fFrames.begin(),
f1 = fFrames.end() - 1; f1 = fFrames.end() - 1;
@ -162,11 +163,11 @@ const KeyframeInterval<T>& Animator<T>::findFrame(float t) const {
SkASSERT(f1->isValid()); SkASSERT(f1->isValid());
if (t < f0->t0()) { if (t < f0->t0()) {
return *f0; return f0;
} }
if (t > f1->t1()) { if (t > f1->t1()) {
return *f1; return f1;
} }
while (f0 != f1) { while (f0 != f1) {
@ -186,7 +187,7 @@ const KeyframeInterval<T>& Animator<T>::findFrame(float t) const {
SkASSERT(f0 == f1); SkASSERT(f0 == f1);
SkASSERT(f0->contains(t)); SkASSERT(f0->contains(t));
return *f0; return f0;
} }
} // namespace skottie } // namespace skottie