From ca7aec5fff4f91029be1e090034e4bb8831b9cdc Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Tue, 9 Apr 2024 15:52:23 +0900 Subject: [PATCH] lottie: rectify the frame updates. Currently, tvg ignores the frame value if the difference is less than 0.001. In most cases, updating the frames when the change is less than 1ms is just an unnecessary burden on the system. Instead, this ensures that the perfect last frame is reached. issue: https://github.com/thorvg/thorvg/issues/2147 --- inc/thorvg.h | 3 +++ src/bindings/capi/thorvg_capi.h | 2 ++ src/loaders/lottie/tvgLottieLoader.cpp | 9 +++++++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/inc/thorvg.h b/inc/thorvg.h index f51b2c81..ed0e13c3 100644 --- a/inc/thorvg.h +++ b/inc/thorvg.h @@ -1853,6 +1853,9 @@ public: * @retval Result::InsufficientCondition if the given @p no is the same as the current frame value. * @retval Result::NonSupport The current Picture data does not support animations. * + * @note For efficiency, ThorVG ignores updates to the new frame value if the difference from the current frame value + * is less than 0.001. In such cases, it returns @c Result::InsufficientCondition. + * * @see totalFrame() * */ diff --git a/src/bindings/capi/thorvg_capi.h b/src/bindings/capi/thorvg_capi.h index 8dcc9927..c92b61fc 100644 --- a/src/bindings/capi/thorvg_capi.h +++ b/src/bindings/capi/thorvg_capi.h @@ -2253,6 +2253,8 @@ TVG_API Tvg_Animation* tvg_animation_new(); * \retval TVG_RESULT_INSUFFICIENT_CONDITION No animatable data loaded from the Picture. * \retval TVG_RESULT_NOT_SUPPORTED The picture data does not support animations. * +* \note For efficiency, ThorVG ignores updates to the new frame value if the difference from the current frame value +* is less than 0.001. In such cases, it returns @c Result::InsufficientCondition. * \see tvg_animation_get_total_frame() */ TVG_API Tvg_Result tvg_animation_set_frame(Tvg_Animation* animation, float no); diff --git a/src/loaders/lottie/tvgLottieLoader.cpp b/src/loaders/lottie/tvgLottieLoader.cpp index 25b548b2..8d327663 100644 --- a/src/loaders/lottie/tvgLottieLoader.cpp +++ b/src/loaders/lottie/tvgLottieLoader.cpp @@ -315,11 +315,16 @@ bool LottieLoader::override(const char* slot) bool LottieLoader::frame(float no) { - //no meaing to update if frame diff is less then 1ms - if (fabsf(this->frameNo - no) < 0.001f) return false; + //Skip update if frame diff is too small. + if (fabsf(this->frameNo - no) < 0.0009f) return false; this->done(); + //This ensures that the perfect last frame is reached. + no *= 1000.0f; + no = roundf(no); + no *= 0.001f; + this->frameNo = no; TaskScheduler::request(this);