From 67739ae3fd4063a4876801db9ea7cf93d8ed1540 Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 14 May 2019 13:04:33 +0300 Subject: [PATCH] Don't try to keep up if animation got late. --- .../SourceFiles/lottie/lottie_frame_renderer.cpp | 15 ++++++++++----- .../SourceFiles/lottie/lottie_frame_renderer.h | 4 ++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Telegram/SourceFiles/lottie/lottie_frame_renderer.cpp b/Telegram/SourceFiles/lottie/lottie_frame_renderer.cpp index 21c8bb2a0..c5648d09d 100644 --- a/Telegram/SourceFiles/lottie/lottie_frame_renderer.cpp +++ b/Telegram/SourceFiles/lottie/lottie_frame_renderer.cpp @@ -219,6 +219,10 @@ void SharedState::renderFrame( void SharedState::init(QImage cover) { Expects(!initialized()); + _frameRate = _scene.frameRate(); + _framesCount = _scene.endFrame() - _scene.startFrame(); + _duration = crl::time(1000) * _framesCount / _frameRate; + _frames[0].original = std::move(cover); _frames[0].position = 0; @@ -242,12 +246,11 @@ bool IsRendered(not_null frame) { void SharedState::renderNextFrame( not_null frame, const FrameRequest &request) { - const auto framesCount = (_scene.endFrame() - _scene.startFrame()); - Assert(framesCount > 0); + Expects(_framesCount > 0); - renderFrame(frame->original, request, (++_frameIndex) % framesCount); + renderFrame(frame->original, request, (++_frameIndex) % _framesCount); PrepareFrameByRequest(frame); - frame->position = crl::time(1000) * _frameIndex / _scene.frameRate(); + frame->position = crl::time(1000) * _frameIndex / _frameRate; frame->displayed = kTimeUnknown; } @@ -269,7 +272,7 @@ bool SharedState::renderNextFrame(const FrameRequest &request) { if (!IsRendered(frame)) { renderNextFrame(frame, request); } - frame->display = _started + frame->position; + frame->display = _started + _accumulatedDelayMs + frame->position; // Release this frame to the main thread for rendering. _counter.store( @@ -365,6 +368,8 @@ crl::time SharedState::markFrameDisplayed(crl::time now) { Assert(frame->displayed == kTimeUnknown); frame->displayed = now; + _accumulatedDelayMs += (frame->displayed - frame->display); + return frame->position; }; diff --git a/Telegram/SourceFiles/lottie/lottie_frame_renderer.h b/Telegram/SourceFiles/lottie/lottie_frame_renderer.h index 5dcdaa838..5e1d98fa4 100644 --- a/Telegram/SourceFiles/lottie/lottie_frame_renderer.h +++ b/Telegram/SourceFiles/lottie/lottie_frame_renderer.h @@ -79,7 +79,11 @@ private: base::weak_ptr _owner; crl::time _started = kTimeUnknown; + crl::time _duration = kTimeUnknown; int _frameIndex = 0; + int _framesCount = 0; + int _frameRate; + std::atomic _accumulatedDelayMs = 0; };