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.
This commit is contained in:
Hermet Park 2023-08-12 00:31:54 +09:00
parent 9e0a3aa678
commit df43f91644
4 changed files with 38 additions and 15 deletions

View file

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

View file

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

View file

@ -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"

View file

@ -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<LottieObject*> assets;
Array<LottieInterpolator*> interpolators;