From 50707482ba2db3af5a5ac066e36d240d2cb10598 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 fcf28467..685fdf89 100644 --- a/inc/thorvg.h +++ b/inc/thorvg.h @@ -1802,6 +1802,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 704cbcf4..7845bb2d 100644 --- a/src/bindings/capi/thorvg_capi.h +++ b/src/bindings/capi/thorvg_capi.h @@ -2250,6 +2250,8 @@ TVG_API Tvg_Animation* tvg_animation_new(); * \retval TVG_RESULT_INSUFFICIENT_CONDITION if the given @p no is the same as the current frame value. * \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 2f879285..78a5fc52 100644 --- a/src/loaders/lottie/tvgLottieLoader.cpp +++ b/src/loaders/lottie/tvgLottieLoader.cpp @@ -319,11 +319,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);