Skip to content

Commit c586989

Browse files
committed
Add native WebAssembly backend
This introduces a lightweight WASM backend without SDL dependency, achieving 60% binary size reduction (891K→357K) compared to SDL-based approach. - Add backend/wasm.c: Native WASM backend using Canvas 2D API directly * Direct Canvas rendering via EM_ASM (no SDL overhead) * Browser-native image decoding (JPEG/PNG) * Optimized for minimal runtime with MINIMAL_RUNTIME=1 * Framebuffer accessor functions for zero-copy updates - Remove twin_dispatch() from API and implementation * Eliminated unused function across all backends * All backends now use twin_dispatch_once() consistently - Make SDL backend native-only * Removed Emscripten-specific code paths from backend/sdl.c * SDL now exclusively for native development/testing
1 parent 16193dc commit c586989

File tree

11 files changed

+994
-351
lines changed

11 files changed

+994
-351
lines changed

Makefile

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ ifneq ($(findstring emcc,$(CC_VERSION)),)
1616
CC_IS_EMCC := 1
1717
endif
1818

19-
# Enforce SDL backend for Emscripten builds (skip during config targets)
19+
# Enforce compatible backend for Emscripten builds (skip during config targets)
2020
ifeq ($(filter $(check_goal),config defconfig),)
2121
ifeq ($(CC_IS_EMCC),1)
22-
ifneq ($(CONFIG_BACKEND_SDL),y)
23-
$(error Emscripten (WebAssembly) builds require SDL backend. Please run: env CC=emcc make defconfig)
22+
ifneq ($(CONFIG_BACKEND_WASM),y)
23+
$(error Emscripten (WebAssembly) builds require WASM backend. SDL backend is native-only.)
2424
endif
2525
endif
2626
endif
@@ -155,15 +155,8 @@ BACKEND := none
155155
ifeq ($(CONFIG_BACKEND_SDL), y)
156156
BACKEND = sdl
157157
libtwin.a_files-y += backend/sdl.c
158-
# Emscripten uses ports system for SDL2
159-
ifneq ($(CC_IS_EMCC), 1)
160158
libtwin.a_cflags-y += $(shell sdl2-config --cflags)
161159
TARGET_LIBS += $(shell sdl2-config --libs)
162-
else
163-
# Emscripten SDL2 port - flags needed for both compile and link
164-
libtwin.a_cflags-y += -sUSE_SDL=2
165-
TARGET_LIBS += -sUSE_SDL=2
166-
endif
167160
endif
168161

169162
ifeq ($(CONFIG_BACKEND_FBDEV), y)
@@ -186,6 +179,12 @@ BACKEND = headless
186179
libtwin.a_files-y += backend/headless.c
187180
endif
188181

182+
ifeq ($(CONFIG_BACKEND_WASM), y)
183+
BACKEND = wasm
184+
libtwin.a_files-y += backend/wasm.c
185+
# WASM backend uses Emscripten directly, no external libraries needed
186+
endif
187+
189188
# Performance tester
190189
ifeq ($(CONFIG_PERF_TEST), y)
191190
target-$(CONFIG_PERF_TEST) += mado-perf
@@ -212,22 +211,35 @@ demo-$(BACKEND)_ldflags-y := \
212211

213212
# Emscripten-specific linker flags for WebAssembly builds
214213
ifeq ($(CC_IS_EMCC), 1)
214+
# Base Emscripten flags for all backends
215215
demo-$(BACKEND)_ldflags-y += \
216216
-sINITIAL_MEMORY=33554432 \
217217
-sALLOW_MEMORY_GROWTH=1 \
218218
-sSTACK_SIZE=1048576 \
219-
-sUSE_SDL=2 \
220-
-sMINIMAL_RUNTIME=0 \
221219
-sDYNAMIC_EXECUTION=0 \
222220
-sASSERTIONS=0 \
223221
-sEXPORTED_FUNCTIONS=_main,_malloc,_free \
224222
-sEXPORTED_RUNTIME_METHODS=ccall,cwrap \
225223
-sNO_EXIT_RUNTIME=1 \
226-
-Oz \
227-
--preload-file assets \
228-
--exclude-file assets/web
224+
-Oz
225+
226+
# Note: MINIMAL_RUNTIME incompatible with --preload-file, so we skip asset preloading
227+
# Assets can be loaded via HTTP requests instead
228+
ifeq ($(CONFIG_BACKEND_WASM), y)
229+
demo-$(BACKEND)_cflags-y += -flto
230+
demo-$(BACKEND)_ldflags-y += \
231+
-sMINIMAL_RUNTIME=1 \
232+
-sMALLOC=emmalloc \
233+
-sFILESYSTEM=0 \
234+
-sDISABLE_EXCEPTION_CATCHING=1 \
235+
-sEXPORT_ES6=0 \
236+
-sMODULARIZE=0 \
237+
-sENVIRONMENT=web \
238+
-sSUPPORT_ERRNO=0 \
239+
-flto
229240
endif
230241
endif
242+
endif # CONFIG_DEMO_APPLICATIONS
231243

232244
# Font editor tool
233245
# Tools should not be built for WebAssembly

0 commit comments

Comments
 (0)