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.
This commit is contained in:
subhransu mohanty
2019-07-23 12:06:56 +09:00
committed by Subhransu
parent 5b4eb59cd8
commit f95f60f8ed
4 changed files with 34 additions and 17 deletions

View 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.

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

View 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;
}

View 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;
};