rlottie/cache: Added a free function configureModelCacheSize() for runtime configuration of Model Cache Policy

This commit is contained in:
subhransu mohanty
2019-07-23 11:34:41 +09:00
committed by Subhransu
parent c15a86985a
commit 5b4eb59cd8
4 changed files with 47 additions and 0 deletions

View 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){}

View File

@@ -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<Surface> sender;

View 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(), '/');

View 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();