rlottie/cache: Added cache configuration support for indivisual resource.
authorsubhransu mohanty <sub.mohanty@samsung.com>
Tue, 23 Jul 2019 03:06:56 +0000 (12:06 +0900)
committerHermet Park <hermetpark@gmail.com>
Fri, 9 Aug 2019 08:54:06 +0000 (17:54 +0900)
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
src/lottie/lottieanimation.cpp
src/lottie/lottieloader.cpp
src/lottie/lottieloader.h

index 894e892..6066c62 100644 (file)
@@ -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<Animation>
-    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<Animation>
-    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.
index d6826a2..be4d93e 100644 (file)
@@ -240,7 +240,7 @@ std::future<Surface> AnimationImpl::renderAsync(size_t    frameNo,
  */
 std::unique_ptr<Animation> 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> Animation::loadFromData(
 
     LottieLoader loader;
     if (loader.loadFromData(std::move(jsonData), key,
-                            (resourcePath.empty() ? " " : resourcePath))) {
+                            (resourcePath.empty() ? " " : resourcePath), cachePolicy)) {
         auto animation = std::unique_ptr<Animation>(new Animation);
         animation->d->init(loader.model());
         return animation;
@@ -257,7 +257,8 @@ std::unique_ptr<Animation> Animation::loadFromData(
     return nullptr;
 }
 
-std::unique_ptr<Animation> Animation::loadFromFile(const std::string &path)
+std::unique_ptr<Animation>
+Animation::loadFromFile(const std::string &path, bool cachePolicy)
 {
     if (path.empty()) {
         vWarning << "File path is empty";
@@ -265,7 +266,7 @@ std::unique_ptr<Animation> Animation::loadFromFile(const std::string &path)
     }
 
     LottieLoader loader;
-    if (loader.load(path)) {
+    if (loader.load(path, cachePolicy)) {
         auto animation = std::unique_ptr<Animation>(new Animation);
         animation->d->init(loader.model());
         return animation;
index 55bece7..5fc7c8f 100644 (file)
@@ -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<char *>(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;
 }
index 28cacc3..4d4646d 100644 (file)
@@ -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<LOTModel> model();
-private:
+private:  
    std::shared_ptr<LOTModel>    mModel;
 };