From 06a34a4c65ee339982320b0913c1dbf2f3f4890f Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Thu, 4 Apr 2024 12:33:05 +0900 Subject: [PATCH] renderer/loader: ensure unique scene instances for each Lottie loader The Lottie loader now utilizes an individual scene instance per loader. This guarantees that each instance retains a unique frame number, essential for maintaining the integrity of animation states. As a result, sharing loaders between Lottie animations is prohibited to uphold frame context isolation. This may be subject to optimization review in the future. --- src/renderer/tvgLoader.cpp | 47 ++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/src/renderer/tvgLoader.cpp b/src/renderer/tvgLoader.cpp index ddcf5c6e..a7801619 100644 --- a/src/renderer/tvgLoader.cpp +++ b/src/renderer/tvgLoader.cpp @@ -294,15 +294,24 @@ LoadModule* LoaderMgr::loader(const string& path, bool* invalid) { *invalid = false; - if (auto loader = _findFromCache(path)) return loader; + //TODO: lottie is not sharable. + auto allowCache = true; + auto ext = path.substr(path.find_last_of(".") + 1); + if (!ext.compare("json")) allowCache = false; + + if (allowCache) { + if (auto loader = _findFromCache(path)) return loader; + } if (auto loader = _findByPath(path)) { if (loader->open(path)) { - loader->hashpath = strdup(path.c_str()); - loader->pathcache = true; - { - ScopedLock lock(key); - _activeLoaders.back(loader); + if (allowCache) { + loader->hashpath = strdup(path.c_str()); + loader->pathcache = true; + { + ScopedLock lock(key); + _activeLoaders.back(loader); + } } return loader; } @@ -312,11 +321,13 @@ LoadModule* LoaderMgr::loader(const string& path, bool* invalid) for (int i = 0; i < static_cast(FileType::Raw); i++) { if (auto loader = _find(static_cast(i))) { if (loader->open(path)) { - loader->hashpath = strdup(path.c_str()); - loader->pathcache = true; - { - ScopedLock lock(key); - _activeLoaders.back(loader); + if (allowCache) { + loader->hashpath = strdup(path.c_str()); + loader->pathcache = true; + { + ScopedLock lock(key); + _activeLoaders.back(loader); + } } return loader; } @@ -353,7 +364,15 @@ LoadModule* LoaderMgr::loader(const char* data, uint32_t size, const string& mim { //Note that users could use the same data pointer with the different content. //Thus caching is only valid for shareable. - if (!copy) { + auto allowCache = !copy; + + //TODO: lottie is not sharable. + if (allowCache) { + auto type = _convert(mimeType); + if (type == FileType::Lottie) allowCache = false; + } + + if (allowCache) { if (auto loader = _findFromCache(data, size, mimeType)) return loader; } @@ -361,7 +380,7 @@ LoadModule* LoaderMgr::loader(const char* data, uint32_t size, const string& mim if (!mimeType.empty()) { if (auto loader = _findByType(mimeType)) { if (loader->open(data, size, rpath, copy)) { - if (!copy) { + if (allowCache) { loader->hashkey = HASH_KEY(data); ScopedLock lock(key); _activeLoaders.back(loader); @@ -378,7 +397,7 @@ LoadModule* LoaderMgr::loader(const char* data, uint32_t size, const string& mim auto loader = _find(static_cast(i)); if (loader) { if (loader->open(data, size, rpath, copy)) { - if (!copy) { + if (allowCache) { loader->hashkey = HASH_KEY(data); ScopedLock lock(key); _activeLoaders.back(loader);