From 5b4eb59cd8b2152d4847bcab8676c138d8b4c712 Mon Sep 17 00:00:00 2001 From: subhransu mohanty Date: Tue, 23 Jul 2019 11:34:41 +0900 Subject: [PATCH] rlottie/cache: Added a free function configureModelCacheSize() for runtime configuration of Model Cache Policy --- inc/rlottie.h | 17 +++++++++++++++++ src/lottie/lottieanimation.cpp | 5 +++++ src/lottie/lottieloader.cpp | 24 ++++++++++++++++++++++++ src/lottie/lottieloader.h | 1 + 4 files changed, 47 insertions(+) diff --git a/inc/rlottie.h b/inc/rlottie.h index 06c727b52e..894e892ffd 100644 --- a/inc/rlottie.h +++ b/inc/rlottie.h @@ -43,6 +43,23 @@ struct LOTLayerNode; namespace rlottie { +/** + * @brief Configures rlottie model cache policy. + * + * Provides Library level control to configure model cache + * policy. Setting it to 0 will disable + * the cache as well as flush all the previously cached content. + * + * @param[in] cacheSize Maximum Model Cache size. + * + * @note to disable Caching configure with 0 size. + * @note to flush the current Cache content configure it with 0 and + * then reconfigure with the new size. + * + * @internal + */ +LOT_EXPORT void configureModelCacheSize(size_t cacheSize); + struct Color { Color() = default; Color(float r, float g , float b):_r(r), _g(g), _b(b){} diff --git a/src/lottie/lottieanimation.cpp b/src/lottie/lottieanimation.cpp index e052895ee2..d6826a2139 100644 --- a/src/lottie/lottieanimation.cpp +++ b/src/lottie/lottieanimation.cpp @@ -25,6 +25,11 @@ using namespace rlottie; +LOT_EXPORT void configureModelCacheSize(size_t cacheSize) +{ + LottieLoader::configureModelCacheSize(cacheSize); +} + struct RenderTask { RenderTask() { receiver = sender.get_future(); } std::promise sender; diff --git a/src/lottie/lottieloader.cpp b/src/lottie/lottieloader.cpp index fffd349191..55bece7e0d 100644 --- a/src/lottie/lottieloader.cpp +++ b/src/lottie/lottieloader.cpp @@ -38,6 +38,8 @@ public: { std::lock_guard guard(mMutex); + if (!mcacheSize) return nullptr; + auto search = mHash.find(key); return (search != mHash.end()) ? search->second : nullptr; @@ -46,14 +48,30 @@ public: void add(const std::string &key, std::shared_ptr value) { std::lock_guard guard(mMutex); + + if (!mcacheSize) return; + + //@TODO just remove the 1st element + // not the best of LRU logic + if (mcacheSize == mHash.size()) mHash.erase(mHash.cbegin()); + mHash[key] = std::move(value); } + void configureCacheSize(size_t cacheSize) + { + std::lock_guard guard(mMutex); + mcacheSize = cacheSize; + + if (!mcacheSize) mHash.clear(); + } + private: LottieModelCache() = default; std::unordered_map> mHash; std::mutex mMutex; + size_t mcacheSize{10}; }; #else @@ -67,10 +85,16 @@ public: } std::shared_ptr find(const std::string &) { return nullptr; } void add(const std::string &, std::shared_ptr) {} + void configureCacheSize(size_t) {} }; #endif +void LottieLoader::configureModelCacheSize(size_t cacheSize) +{ + LottieModelCache::instance().configureCacheSize(cacheSize); +} + static std::string dirname(const std::string &path) { const char *ptr = strrchr(path.c_str(), '/'); diff --git a/src/lottie/lottieloader.h b/src/lottie/lottieloader.h index d7228bbce1..28cacc3f0f 100644 --- a/src/lottie/lottieloader.h +++ b/src/lottie/lottieloader.h @@ -26,6 +26,7 @@ class LOTModel; class LottieLoader { public: + static void configureModelCacheSize(size_t cacheSize); bool load(const std::string &filePath); bool loadFromData(std::string &&jsonData, const std::string &key, const std::string &resourcePath); std::shared_ptr model();