wasm: Revise the wasm binding for lottie-player

- removed and replaced `tvgWasm`
This commit is contained in:
Jinny You 2023-12-11 11:56:20 +09:00 committed by Hermet Park
parent 93f0e493ac
commit 99a841b404
2 changed files with 62 additions and 80 deletions

View file

@ -1,5 +1,5 @@
if (cc.get_id() == 'emscripten') if (cc.get_id() == 'emscripten')
source_file = files('tvgWasm.cpp') source_file = files('tvgWasmLottieAnimation.cpp')
thorvg_wasm_dep = declare_dependency(include_directories thorvg_wasm_dep = declare_dependency(include_directories
: include_directories('.'), sources : include_directories('.'), sources
: source_file) : source_file)

View file

@ -29,18 +29,18 @@ using namespace tvg;
static const char* NoError = "None"; static const char* NoError = "None";
class __attribute__((visibility("default"))) TvgWasm class __attribute__((visibility("default"))) TvgLottieAnimation
{ {
public: public:
~TvgWasm() ~TvgLottieAnimation()
{ {
free(buffer); free(buffer);
Initializer::term(); Initializer::term();
} }
static unique_ptr<TvgWasm> create() static unique_ptr<TvgLottieAnimation> create()
{ {
return unique_ptr<TvgWasm>(new TvgWasm()); return unique_ptr<TvgLottieAnimation>(new TvgLottieAnimation());
} }
string error() string error()
@ -48,7 +48,32 @@ public:
return errorMsg; return errorMsg;
} }
bool load(string data, string mimetype, int width, int height) // Getter methods
val size()
{
return val(typed_memory_view(2, psize));
}
val duration()
{
if (!canvas || !animation) return val(0);
return val(animation->duration());
}
val totalFrame()
{
if (!canvas || !animation) return val(0);
return val(animation->totalFrame());
}
val curFrame()
{
if (!canvas || !animation) return val(0);
return val(animation->curFrame());
}
// Render methods
bool load(string data, string mimetype, int width, int height, string rpath = "")
{ {
errorMsg = NoError; errorMsg = NoError;
@ -63,7 +88,7 @@ public:
animation = Animation::gen(); animation = Animation::gen();
if (animation->picture()->load(data.c_str(), data.size(), mimetype, false) != Result::Success) { if (animation->picture()->load(data.c_str(), data.size(), mimetype, rpath, false) != Result::Success) {
errorMsg = "load() fail"; errorMsg = "load() fail";
return false; return false;
} }
@ -86,20 +111,6 @@ public:
return true; return true;
} }
bool update()
{
if (!updated) return true;
errorMsg = NoError;
if (canvas->update() != Result::Success) {
errorMsg = "update() fail";
return false;
}
return true;
}
val render() val render()
{ {
errorMsg = NoError; errorMsg = NoError;
@ -120,21 +131,18 @@ public:
return val(typed_memory_view(width * height * 4, buffer)); return val(typed_memory_view(width * height * 4, buffer));
} }
val size() bool update()
{ {
return val(typed_memory_view(2, psize)); if (!updated) return true;
}
val duration() errorMsg = NoError;
{
if (!canvas || !animation) return val(0);
return val(animation->duration());
}
val totalFrame() if (canvas->update() != Result::Success) {
{ errorMsg = "update() fail";
if (!canvas || !animation) return val(0); return false;
return val(animation->totalFrame()); }
return true;
} }
bool frame(float no) bool frame(float no)
@ -173,35 +181,7 @@ public:
updated = true; updated = true;
} }
bool save2Tvg() // Saver methods
{
errorMsg = NoError;
if (!animation) return false;
auto duplicate = cast<Picture>(animation->picture()->duplicate());
if (!duplicate) {
errorMsg = "duplicate(), fail";
return false;
}
auto saver = Saver::gen();
if (!saver) {
errorMsg = "Invalid saver";
return false;
}
if (saver->save(std::move(duplicate), "output.tvg") != tvg::Result::Success) {
errorMsg = "save(), fail";
return false;
}
saver->sync();
return true;
}
bool save2Gif(string data, string mimetype, int width, int height, int fps) bool save2Gif(string data, string mimetype, int width, int height, int fps)
{ {
errorMsg = NoError; errorMsg = NoError;
@ -213,7 +193,7 @@ public:
return false; return false;
} }
if (animation->picture()->load(data.c_str(), data.size(), mimetype, false) != Result::Success) { if (animation->picture()->load(data.c_str(), data.size(), mimetype, "", false) != Result::Success) {
errorMsg = "load() fail"; errorMsg = "load() fail";
return false; return false;
} }
@ -247,8 +227,10 @@ public:
return true; return true;
} }
// TODO: Advanced APIs wrt Interactivty & theme methods...
private: private:
explicit TvgWasm() explicit TvgLottieAnimation()
{ {
errorMsg = NoError; errorMsg = NoError;
@ -275,19 +257,19 @@ private:
bool updated = false; bool updated = false;
}; };
EMSCRIPTEN_BINDINGS(thorvg_bindings)
EMSCRIPTEN_BINDINGS(thorvg_bindings) { {
class_<TvgWasm>("TvgWasm") class_<TvgLottieAnimation>("TvgLottieAnimation")
.constructor(&TvgWasm::create) .constructor(&TvgLottieAnimation ::create)
.function("error", &TvgWasm::error, allow_raw_pointers()) .function("error", &TvgLottieAnimation ::error, allow_raw_pointers())
.function("load", &TvgWasm::load) .function("size", &TvgLottieAnimation ::size)
.function("update", &TvgWasm::update) .function("duration", &TvgLottieAnimation ::duration)
.function("resize", &TvgWasm::resize) .function("totalFrame", &TvgLottieAnimation ::totalFrame)
.function("render", &TvgWasm::render) .function("curFrame", &TvgLottieAnimation ::curFrame)
.function("size", &TvgWasm::size) .function("render", &TvgLottieAnimation::render)
.function("duration", &TvgWasm::duration) .function("load", &TvgLottieAnimation ::load)
.function("totalFrame", &TvgWasm::totalFrame) .function("update", &TvgLottieAnimation ::update)
.function("frame", &TvgWasm::frame) .function("frame", &TvgLottieAnimation ::frame)
.function("save2Tvg", &TvgWasm::save2Tvg) .function("resize", &TvgLottieAnimation ::resize)
.function("save2Gif", &TvgWasm::save2Gif); .function("save2Gif", &TvgLottieAnimation ::save2Gif);
} }