From 5d617c08210693860eba6bcc9481a72fa896d97e Mon Sep 17 00:00:00 2001 From: Jinny You Date: Thu, 12 Dec 2024 18:08:44 +0900 Subject: [PATCH] wasm: remove ASYNCIFY to reduce binary size MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The binary size recently increased due to the ASYNCIFY option, which was required for WebGPU initialization. To prevent unnecessary binary size growth, ThorVG will no longer depend on asynchronous processes such as `emscripten_sleep` (ASYNCIFY or JSPI). As a result, the combined WASM binary size has been significantly reduced to less than 1MB. Size comparison: 1559KB → 998KB (-36%) --- cross/wasm32.txt | 2 +- cross/wasm32_wg.txt | 2 +- src/bindings/wasm/tvgWasmLottieAnimation.cpp | 19 ++++++++++++++++--- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/cross/wasm32.txt b/cross/wasm32.txt index e762e4f6..df51bac3 100644 --- a/cross/wasm32.txt +++ b/cross/wasm32.txt @@ -12,7 +12,7 @@ exe_suffix = 'js' [built-in options] cpp_args = ['-Wshift-negative-value', '-flto', '-Os', '-fno-exceptions'] -cpp_link_args = ['-Wshift-negative-value', '-flto', '-Os', '-fno-exceptions', '--bind', '-sWASM=1', '-sALLOW_MEMORY_GROWTH=1', '-sEXPORT_ES6=1', '-sFORCE_FILESYSTEM=1', '-sMODULARIZE=1', '-sEXPORTED_RUNTIME_METHODS=FS', '-sUSE_WEBGPU=1', '-sASYNCIFY=1', '-sSTACK_SIZE=2MB', '-sMAX_WEBGL_VERSION=2', '-sFULL_ES3'] +cpp_link_args = ['-Wshift-negative-value', '-flto', '-Os', '-fno-exceptions', '--bind', '-sWASM=1', '-sALLOW_MEMORY_GROWTH=1', '-sEXPORT_ES6=1', '-sFORCE_FILESYSTEM=1', '-sMODULARIZE=1', '-sEXPORTED_RUNTIME_METHODS=FS', '-sUSE_WEBGPU=1', '-sSTACK_SIZE=2MB', '-sMAX_WEBGL_VERSION=2', '-sFULL_ES3'] [host_machine] system = 'emscripten' diff --git a/cross/wasm32_wg.txt b/cross/wasm32_wg.txt index d8c42ee9..ea04ff17 100644 --- a/cross/wasm32_wg.txt +++ b/cross/wasm32_wg.txt @@ -12,7 +12,7 @@ exe_suffix = 'js' [built-in options] cpp_args = ['-Wshift-negative-value', '-flto', '-Os', '-fno-exceptions'] -cpp_link_args = ['-Wshift-negative-value', '-flto', '-Os', '-fno-exceptions', '--bind', '-sWASM=1', '-sALLOW_MEMORY_GROWTH=1', '-sEXPORT_ES6=1', '-sFORCE_FILESYSTEM=1', '-sMODULARIZE=1', '-sEXPORTED_RUNTIME_METHODS=FS', '-sUSE_WEBGPU=1', '-sASYNCIFY=1', '-sSTACK_SIZE=2MB'] +cpp_link_args = ['-Wshift-negative-value', '-flto', '-Os', '-fno-exceptions', '--bind', '-sWASM=1', '-sALLOW_MEMORY_GROWTH=1', '-sEXPORT_ES6=1', '-sFORCE_FILESYSTEM=1', '-sMODULARIZE=1', '-sEXPORTED_RUNTIME_METHODS=FS', '-sUSE_WEBGPU=1', '-sSTACK_SIZE=2MB'] [host_machine] system = 'emscripten' diff --git a/src/bindings/wasm/tvgWasmLottieAnimation.cpp b/src/bindings/wasm/tvgWasmLottieAnimation.cpp index 72d94518..b076ebff 100644 --- a/src/bindings/wasm/tvgWasmLottieAnimation.cpp +++ b/src/bindings/wasm/tvgWasmLottieAnimation.cpp @@ -41,9 +41,12 @@ static const char* NoError = "None"; static WGPUInstance instance{}; static WGPUAdapter adapter{}; static WGPUDevice device{}; + static bool adapterRequested = false; + static bool deviceRequested = false; #endif -void init() +// 0: success, 1: fail, 2: wait for async request +int init() { #ifdef THORVG_WG_RASTER_SUPPORT //Init WebGPU @@ -51,22 +54,32 @@ void init() // request adapter if (!adapter) { + if (adapterRequested) return 2; + const WGPURequestAdapterOptions requestAdapterOptions { .nextInChain = nullptr, .powerPreference = WGPUPowerPreference_HighPerformance, .forceFallbackAdapter = false }; auto onAdapterRequestEnded = [](WGPURequestAdapterStatus status, WGPUAdapter adapter, char const * message, void * pUserData) { *((WGPUAdapter*)pUserData) = adapter; }; wgpuInstanceRequestAdapter(instance, &requestAdapterOptions, onAdapterRequestEnded, &adapter); - while (!adapter) emscripten_sleep(10); + + adapterRequested = true; + return 2; } // request device + if (deviceRequested) return device == nullptr ? 2 : 0; + WGPUFeatureName featureNames[32]{}; size_t featuresCount = wgpuAdapterEnumerateFeatures(adapter, featureNames); if (!device) { const WGPUDeviceDescriptor deviceDesc { .nextInChain = nullptr, .label = "The device", .requiredFeatureCount = featuresCount, .requiredFeatures = featureNames }; auto onDeviceRequestEnded = [](WGPURequestDeviceStatus status, WGPUDevice device, char const * message, void * pUserData) { *((WGPUDevice*)pUserData) = device; }; wgpuAdapterRequestDevice(adapter, &deviceDesc, onDeviceRequestEnded, &device); - while (!device) emscripten_sleep(10); + + deviceRequested = true; + return 2; } #endif + + return 0; } void term()