rlottie/cache: Added a free function configureModelCacheSize() for runtime configurat...
authorsubhransu mohanty <sub.mohanty@samsung.com>
Tue, 23 Jul 2019 02:34:41 +0000 (11:34 +0900)
committerHermet Park <hermetpark@gmail.com>
Fri, 9 Aug 2019 08:54:02 +0000 (17:54 +0900)
inc/rlottie.h
src/lottie/lottieanimation.cpp
src/lottie/lottieloader.cpp
src/lottie/lottieloader.h

index 06c727b..894e892 100644 (file)
@@ -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){}
index e052895..d6826a2 100644 (file)
 
 using namespace rlottie;
 
+LOT_EXPORT void configureModelCacheSize(size_t cacheSize)
+{
+    LottieLoader::configureModelCacheSize(cacheSize);
+}
+
 struct RenderTask {
     RenderTask() { receiver = sender.get_future(); }
     std::promise<Surface> sender;
index fffd349..55bece7 100644 (file)
@@ -38,6 +38,8 @@ public:
     {
         std::lock_guard<std::mutex> 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<LOTModel> value)
     {
         std::lock_guard<std::mutex> 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<std::mutex> guard(mMutex);
+        mcacheSize = cacheSize;
+
+        if (!mcacheSize) mHash.clear();
+    }
+
 private:
     LottieModelCache() = default;
 
     std::unordered_map<std::string, std::shared_ptr<LOTModel>>  mHash;
     std::mutex                                                  mMutex;
+    size_t                                                      mcacheSize{10};
 };
 
 #else
@@ -67,10 +85,16 @@ public:
     }
     std::shared_ptr<LOTModel> find(const std::string &) { return nullptr; }
     void add(const std::string &, std::shared_ptr<LOTModel>) {}
+    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(), '/');
index d7228bb..28cacc3 100644 (file)
@@ -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<LOTModel> model();