From df43f916447f09288251caa61ce9b20a5c53499b Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Sat, 12 Aug 2023 00:31:54 +0900 Subject: [PATCH] loader/lottie: optimize the header task. To achieve full threading efficiency, the header should return the necessary properties: view size, framerate, and duration. This commit adds duration parsing. Also don't do the header task in case of single thread because there wouldn't be parallel effects. --- src/loaders/lottie/tvgLottieLoader.cpp | 44 ++++++++++++++++++++------ src/loaders/lottie/tvgLottieLoader.h | 3 +- src/loaders/lottie/tvgLottieModel.cpp | 4 --- src/loaders/lottie/tvgLottieModel.h | 2 +- 4 files changed, 38 insertions(+), 15 deletions(-) diff --git a/src/loaders/lottie/tvgLottieLoader.cpp b/src/loaders/lottie/tvgLottieLoader.cpp index af9c3531..548ef72e 100644 --- a/src/loaders/lottie/tvgLottieLoader.cpp +++ b/src/loaders/lottie/tvgLottieLoader.cpp @@ -67,8 +67,9 @@ void LottieLoader::run(unsigned tid) //initial loading } else { LottieParser parser(content, dirName); - parser.parse(); + if (!parser.parse()) return; comp = parser.comp; + if (!comp) return; builder->build(comp); } } @@ -99,11 +100,16 @@ LottieLoader::~LottieLoader() bool LottieLoader::header() { - //Quickly validate the given Lottie file without parsing in order to get the animation info. - auto p = content; + //A single thread doesn't need to perform intensive tasks. + if (TaskScheduler::threads() == 0) return true; + //Quickly validate the given Lottie file without parsing in order to get the animation info. + auto startFrame = 0.0f; + auto endFrame = 0.0f; uint32_t depth = 0; + auto p = content; + while (*p != '\0') { if (*p == '{') { ++depth; @@ -124,6 +130,7 @@ bool LottieLoader::header() p += 4; continue; } + //framerate if (!strncmp(p, "\"fr\":", 5)) { p += 5; @@ -133,7 +140,26 @@ bool LottieLoader::header() p = e; continue; } - //TODO: need a duration time in advance. + + //start frame + if (!strncmp(p, "\"ip\":", 5)) { + p += 5; + auto e = strstr(p, ","); + if (!e) e = strstr(p, "}"); + startFrame = _str2float(p, e - p); + p = e; + continue; + } + + //end frame + if (!strncmp(p, "\"op\":", 5)) { + p += 5; + auto e = strstr(p, ","); + if (!e) e = strstr(p, "}"); + endFrame = _str2float(p, e - p); + p = e; + continue; + } //width if (!strncmp(p, "\"w\":", 4)) { @@ -155,7 +181,10 @@ bool LottieLoader::header() } ++p; } - TVGLOG("LOTTIE", "info: fr = %d, size = %d x %d", frameRate, (int)w, (int)h); + + frameDuration = (endFrame - startFrame) / frameRate; + + TVGLOG("LOTTIE", "info: frame rate = %d, duration = %f size = %d x %d", frameRate, frameDuration, (int)w, (int)h); return true; } @@ -303,10 +332,7 @@ uint32_t LottieLoader::curFrame() float LottieLoader::duration() { - this->done(); - - if (!comp) return 0; - return comp->duration(); + return frameDuration; } diff --git a/src/loaders/lottie/tvgLottieLoader.h b/src/loaders/lottie/tvgLottieLoader.h index 87427082..ce84b85e 100644 --- a/src/loaders/lottie/tvgLottieLoader.h +++ b/src/loaders/lottie/tvgLottieLoader.h @@ -35,8 +35,9 @@ class LottieLoader : public FrameModule, public Task public: const char* content = nullptr; //lottie file data uint32_t size = 0; //lottie data size - uint32_t frameRate; uint32_t frameNo = 0; //current frame number + uint32_t frameRate; + float frameDuration; LottieBuilder* builder = nullptr; LottieComposition* comp = nullptr; diff --git a/src/loaders/lottie/tvgLottieModel.cpp b/src/loaders/lottie/tvgLottieModel.cpp index 2ec67bc2..6afd9832 100644 --- a/src/loaders/lottie/tvgLottieModel.cpp +++ b/src/loaders/lottie/tvgLottieModel.cpp @@ -20,10 +20,6 @@ * SOFTWARE. */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - #include "tvgLottieModel.h" diff --git a/src/loaders/lottie/tvgLottieModel.h b/src/loaders/lottie/tvgLottieModel.h index 84f452c2..2398fb84 100644 --- a/src/loaders/lottie/tvgLottieModel.h +++ b/src/loaders/lottie/tvgLottieModel.h @@ -445,7 +445,7 @@ struct LottieComposition char* version = nullptr; char* name = nullptr; uint32_t w, h; - long startFrame, endFrame; + int32_t startFrame, endFrame; float frameRate; Array assets; Array interpolators;