commit ef732d268a1b784401f0d2cd55d500e961d2429d
parent 89273f9047923b291e935e6c7eda62e540715005
Author: anon <anon@example.com>
Date: Sat, 17 Oct 2026 04:41:36 +0000
Avoid browser timer delay after frame presentation
Diffstat:
4 files changed, 19 insertions(+), 6 deletions(-)
diff --git a/src/Makefile.wasm b/src/Makefile.wasm
@@ -45,9 +45,9 @@ build/wasm/web/storage-test.html: tests/browser_storage.c runtime/browser_host.c
@mkdir -p $(@D)
$(CC) $(CFLAGS) $(filter %.c,$^) -sWASMFS -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=2 -sALLOW_BLOCKING_ON_MAIN_THREAD=0 -sALLOW_MEMORY_GROWTH --js-library runtime/browser_host.js -o $@
-build/wasm/web/suspension-test.html: tests/browser_suspension.c Makefile.wasm
+build/wasm/web/suspension-test.html: tests/browser_suspension.c runtime/browser_host.js Makefile.wasm
@mkdir -p $(@D)
- $(CC) $(CFLAGS) $< -sJSPI -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=2 -sALLOW_BLOCKING_ON_MAIN_THREAD=0 -o $@
+ $(CC) $(CFLAGS) $< --js-library runtime/browser_host.js -sJSPI -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=2 -sALLOW_BLOCKING_ON_MAIN_THREAD=0 -o $@
build/wasm/web/audio-output-test.html: tests/browser_audio.html build/wasm/web/audio-output-test.js
cp $< $@
diff --git a/src/runtime/browser_host.js b/src/runtime/browser_host.js
@@ -1,4 +1,15 @@
addToLibrary({
+ $AOPresentation: {channel: null, resume: null},
+ ao_present_yield__deps: ['$AOPresentation'],
+ ao_present_yield__async: 'auto',
+ ao_present_yield: () => new Promise(resolve => {
+ if (!AOPresentation.channel) {
+ AOPresentation.channel = new MessageChannel();
+ AOPresentation.channel.port1.onmessage = () => AOPresentation.resume();
+ }
+ AOPresentation.resume = resolve;
+ AOPresentation.channel.port2.postMessage(0);
+ }),
ao_asset_manifest__deps: ['$stringToNewUTF8', '$UTF8ToString'],
ao_asset_manifest: (base) => {
// This call runs on the game worker, where synchronous XHR may block safely.
diff --git a/src/runtime/linux_graphics.c b/src/runtime/linux_graphics.c
@@ -1418,8 +1418,9 @@ static void surface_blit(void)
#endif
gl_target_present(target->target, client, width, height);
#ifdef __EMSCRIPTEN__
- /* JSPI yields the worker so OffscreenCanvas can present this frame. */
- emscripten_sleep(0);
+ /* A queued message yields the worker so OffscreenCanvas can present. */
+ extern void ao_present_yield(void);
+ ao_present_yield();
#else
SDL_GL_SwapWindow(native);
#endif
diff --git a/src/tests/browser_suspension.c b/src/tests/browser_suspension.c
@@ -6,10 +6,11 @@
static jmp_buf landing;
static int resumed;
+void ao_present_yield(void);
static void yield_and_return(void)
{
- emscripten_sleep(0);
+ for (unsigned i = 0; i < 3; ++i) ao_present_yield();
resumed = 1;
longjmp(landing, 7);
}
@@ -23,5 +24,5 @@ int main(void)
if (!result) yield_and_return();
assert(result == 7 && resumed == 1);
- puts("PASS: JSPI yields through a Wasm longjmp landing pad on the game worker");
+ puts("PASS: presentation yields reuse their message channel and resume through a Wasm longjmp landing pad");
}