From f95f60f8eddf60fdfc48240e5c2eb41d8404edec Mon Sep 17 00:00:00 2001 From: subhransu mohanty Date: Tue, 23 Jul 2019 12:06:56 +0900 Subject: [PATCH] rlottie/cache: Added cache configuration support for indivisual resource. For configuring Model Cache at runtime used configureModelCacheSize(). For configuring Model Cache at build time use cache flag to enable or disable. --- inc/rlottie.h | 13 +++++++++++-- src/lottie/lottieanimation.cpp | 9 +++++---- src/lottie/lottieloader.cpp | 22 ++++++++++++++-------- src/lottie/lottieloader.h | 7 ++++--- 4 files changed, 34 insertions(+), 17 deletions(-) diff --git a/inc/rlottie.h b/inc/rlottie.h index 894e892ffd..6066c628dd 100644 --- a/inc/rlottie.h +++ b/inc/rlottie.h @@ -260,6 +260,10 @@ public: * @brief Constructs an animation object from file path. * * @param[in] path Lottie resource file path + * @param[in] cachePolicy whether to cache or not the model data. + * use only when need to explicit disabl caching for a + * particular resource. To disable caching at library level + * use @see configureModelCacheSize() instead. * * @return Animation object that can render the contents of the * Lottie resource represented by file path. @@ -267,7 +271,7 @@ public: * @internal */ static std::unique_ptr - loadFromFile(const std::string &path); + loadFromFile(const std::string &path, bool cachePolicy=true); /** * @brief Constructs an animation object from JSON string data. @@ -275,6 +279,10 @@ public: * @param[in] jsonData The JSON string data. * @param[in] key the string that will be used to cache the JSON string data. * @param[in] resourcePath the path will be used to search for external resource. + * @param[in] cachePolicy whether to cache or not the model data. + * use only when need to explicit disabl caching for a + * particular resource. To disable caching at library level + * use @see configureModelCacheSize() instead. * * @return Animation object that can render the contents of the * Lottie resource represented by JSON string data. @@ -282,7 +290,8 @@ public: * @internal */ static std::unique_ptr - loadFromData(std::string jsonData, const std::string &key, const std::string &resourcePath=""); + loadFromData(std::string jsonData, const std::string &key, + const std::string &resourcePath="", bool cachePolicy=true); /** * @brief Returns default framerate of the Lottie resource. diff --git a/src/lottie/lottieanimation.cpp b/src/lottie/lottieanimation.cpp index d6826a2139..be4d93eff8 100644 --- a/src/lottie/lottieanimation.cpp +++ b/src/lottie/lottieanimation.cpp @@ -240,7 +240,7 @@ std::future AnimationImpl::renderAsync(size_t frameNo, */ std::unique_ptr Animation::loadFromData( std::string jsonData, const std::string &key, - const std::string &resourcePath) + const std::string &resourcePath, bool cachePolicy) { if (jsonData.empty()) { vWarning << "jason data is empty"; @@ -249,7 +249,7 @@ std::unique_ptr Animation::loadFromData( LottieLoader loader; if (loader.loadFromData(std::move(jsonData), key, - (resourcePath.empty() ? " " : resourcePath))) { + (resourcePath.empty() ? " " : resourcePath), cachePolicy)) { auto animation = std::unique_ptr(new Animation); animation->d->init(loader.model()); return animation; @@ -257,7 +257,8 @@ std::unique_ptr Animation::loadFromData( return nullptr; } -std::unique_ptr Animation::loadFromFile(const std::string &path) +std::unique_ptr +Animation::loadFromFile(const std::string &path, bool cachePolicy) { if (path.empty()) { vWarning << "File path is empty"; @@ -265,7 +266,7 @@ std::unique_ptr Animation::loadFromFile(const std::string &path) } LottieLoader loader; - if (loader.load(path)) { + if (loader.load(path, cachePolicy)) { auto animation = std::unique_ptr(new Animation); animation->d->init(loader.model()); return animation; diff --git a/src/lottie/lottieloader.cpp b/src/lottie/lottieloader.cpp index 55bece7e0d..5fc7c8f895 100644 --- a/src/lottie/lottieloader.cpp +++ b/src/lottie/lottieloader.cpp @@ -105,10 +105,12 @@ static std::string dirname(const std::string &path) return std::string(path, 0, len); } -bool LottieLoader::load(const std::string &path) +bool LottieLoader::load(const std::string &path, bool cachePolicy) { - mModel = LottieModelCache::instance().find(path); - if (mModel) return true; + if (cachePolicy) { + mModel = LottieModelCache::instance().find(path); + if (mModel) return true; + } std::ifstream f; f.open(path); @@ -126,7 +128,8 @@ bool LottieLoader::load(const std::string &path) if (!mModel) return false; - LottieModelCache::instance().add(path, mModel); + if (cachePolicy) + LottieModelCache::instance().add(path, mModel); f.close(); } @@ -135,10 +138,12 @@ bool LottieLoader::load(const std::string &path) } bool LottieLoader::loadFromData(std::string &&jsonData, const std::string &key, - const std::string &resourcePath) + const std::string &resourcePath, bool cachePolicy) { - mModel = LottieModelCache::instance().find(key); - if (mModel) return true; + if (cachePolicy) { + mModel = LottieModelCache::instance().find(key); + if (mModel) return true; + } LottieParser parser(const_cast(jsonData.c_str()), resourcePath.c_str()); @@ -146,7 +151,8 @@ bool LottieLoader::loadFromData(std::string &&jsonData, const std::string &key, if (!mModel) return false; - LottieModelCache::instance().add(key, mModel); + if (cachePolicy) + LottieModelCache::instance().add(key, mModel); return true; } diff --git a/src/lottie/lottieloader.h b/src/lottie/lottieloader.h index 28cacc3f0f..4d4646dbb4 100644 --- a/src/lottie/lottieloader.h +++ b/src/lottie/lottieloader.h @@ -27,10 +27,11 @@ 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); + bool load(const std::string &filePath, bool cachePolicy); + bool loadFromData(std::string &&jsonData, const std::string &key, + const std::string &resourcePath, bool cachePolicy); std::shared_ptr model(); -private: +private: std::shared_ptr mModel; };