From: subhransu mohanty Date: Tue, 23 Jul 2019 02:34:41 +0000 (+0900) Subject: rlottie/cache: Added a free function configureModelCacheSize() for runtime configurat... X-Git-Tag: submit/tizen/20190812.090759~23 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f391feefa9b60be177eac306d65c2312da70c407;p=platform%2Fcore%2Fuifw%2Flottie-player.git rlottie/cache: Added a free function configureModelCacheSize() for runtime configuration of Model Cache Policy --- diff --git a/inc/rlottie.h b/inc/rlottie.h index 06c727b..894e892 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 e052895..d6826a2 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 fffd349..55bece7 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 d7228bb..28cacc3 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();