commit 80db2585ffb03dba19bb04391adb5e0f663eda83
parent 25fb492525f1abaa7c34d0a05a29281053c475ff
Author: anon <anon@example.com>
Date: Sat, 17 Oct 2026 18:46:08 +0000
Drive browser presentation with animation frames
Diffstat:
5 files changed, 87 insertions(+), 74 deletions(-)
diff --git a/src/Makefile.wasm b/src/Makefile.wasm
@@ -51,7 +51,7 @@ build/wasm/web/suspension-test.html: tests/browser_suspension.c runtime/browser_
build/wasm/web/pacing-test.html: tests/browser_pacing.c runtime/linux_graphics.c runtime/linux_window.c runtime/gl_renderer.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/browser_host.js Makefile.wasm
@mkdir -p $(@D)
- $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) --js-library runtime/browser_host.js -sUSE_SDL=2 -sJSPI -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=2 -sALLOW_BLOCKING_ON_MAIN_THREAD=0 -sALLOW_MEMORY_GROWTH -sOFFSCREENCANVAS_SUPPORT -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -Wl,--wrap=clock_gettime,--wrap=clock_nanosleep -o $@
+ $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) --js-library runtime/browser_host.js -sUSE_SDL=2 -sJSPI -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=2 -sALLOW_BLOCKING_ON_MAIN_THREAD=0 -sALLOW_MEMORY_GROWTH -sOFFSCREENCANVAS_SUPPORT -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -Wl,--wrap=clock_nanosleep -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,15 +1,6 @@
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_present_yield: () => new Promise(resolve => requestAnimationFrame(resolve)),
$AOSoundSizes: {},
ao_asset_manifest__deps: ['$stringToNewUTF8', '$UTF8ToString', '$AOSoundSizes'],
ao_asset_manifest: (base) => {
diff --git a/src/runtime/linux_graphics.c b/src/runtime/linux_graphics.c
@@ -279,36 +279,24 @@ static void wait_for_vertical_blank(void)
g_esp += 16;
g_eax = 0x80070057u;
if (flags != 1 || event) return;
- /* Browsers and disconnected displays may not report a refresh rate. */
- unsigned refresh_rate = 60;
#ifndef __EMSCRIPTEN__
+ /* Disconnected displays may not report a refresh rate. */
+ unsigned refresh_rate = 60;
SDL_DisplayMode mode;
if (SDL_GetCurrentDisplayMode(SDL_GetWindowDisplayIndex(window), &mode)) {
fprintf(stderr, "Cannot determine native display refresh rate\n"); exit(3);
}
if (mode.refresh_rate > 0) refresh_rate = mode.refresh_rate;
-#endif
/* Pace a virtual refresh clock where there is no retrace wait API. */
struct timespec now;
if (clock_gettime(CLOCK_MONOTONIC, &now)) abort();
uint64_t period = 1000000000u / refresh_rate;
uint64_t ticks = (uint64_t)now.tv_sec * 1000000000u + now.tv_nsec;
-#ifdef __EMSCRIPTEN__
- /* Cap the worker at 60 Hz without delaying an already late frame. */
- static uint64_t last_refresh;
- uint64_t next = last_refresh + period;
- if (next < ticks) next = ticks;
-#else
uint64_t next = ticks + period - ticks % period;
-#endif
struct timespec deadline = {next / 1000000000u, next % 1000000000u};
int error;
do { error = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &deadline, NULL); } while (error == EINTR);
if (error) { fprintf(stderr, "Native refresh wait failed: %d\n", error); abort(); }
-#ifdef __EMSCRIPTEN__
- /* An overslept wait starts a new interval rather than a catch-up burst. */
- if (clock_gettime(CLOCK_MONOTONIC, &now)) abort();
- last_refresh = (uint64_t)now.tv_sec * 1000000000u + now.tv_nsec;
#endif
g_eax = 0;
}
@@ -1401,7 +1389,7 @@ static void surface_blit(void)
if (primary) {
native = linux_window_native(target->clipper->window);
if (!native) { g_eax = 0x80004005u; return; }
- if (SDL_GetWindowFlags(native) & (SDL_WINDOW_HIDDEN | SDL_WINDOW_MINIMIZED)) { g_eax = 0; return; }
+ if (SDL_GetWindowFlags(native) & (SDL_WINDOW_HIDDEN | SDL_WINDOW_MINIMIZED)) goto done;
int x, y, width, height;
SDL_GetWindowPosition(native, &x, &y);
SDL_GetWindowSize(native, &width, &height);
@@ -1411,7 +1399,7 @@ static void surface_blit(void)
client[3] > (int32_t)target->height ? (int32_t)target->height : client[3]};
for (unsigned i = 0; i < 4; ++i)
if (i < 2 ? clipped[i] < to[i] : clipped[i] > to[i]) clipped[i] = to[i];
- if (clipped[0] >= clipped[2] || clipped[1] >= clipped[3]) { g_eax = 0; return; }
+ if (clipped[0] >= clipped[2] || clipped[1] >= clipped[3]) goto done;
for (unsigned i = 0; i < 4; ++i) {
from[i] += clipped[i] - to[i];
to[i] = clipped[i];
@@ -1430,15 +1418,19 @@ static void surface_blit(void)
SDL_GL_GetDrawableSize(native, &width, &height);
#endif
gl_target_present(target->target, client, width, height);
-#ifdef __EMSCRIPTEN__
- /* A queued message yields the worker so OffscreenCanvas can present. */
- extern void ao_present_yield(void);
- ao_present_yield();
-#else
+#ifndef __EMSCRIPTEN__
SDL_GL_SwapWindow(native);
#endif
}
+done:
g_eax = 0;
+#ifdef __EMSCRIPTEN__
+ /* Commit completed pixels before yielding; hidden/clipped windows still pace. */
+ if (primary) {
+ extern void ao_present_yield(void);
+ ao_present_yield();
+ }
+#endif
}
static struct device_object *find_device(uint32_t address)
diff --git a/src/tests/browser_pacing.c b/src/tests/browser_pacing.c
@@ -1,40 +1,37 @@
#include "linux_graphics.h"
+#include "linux_window.h"
#include "guest_memory.h"
+#include <SDL2/SDL.h>
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
+#include <emscripten.h>
+#include <emscripten/threading.h>
-static _Thread_local int fake_clock;
-static uint64_t current_time, oversleep, deadlines[5];
-static unsigned waits;
+static _Thread_local int checking_wait;
-int __real_clock_gettime(clockid_t clock, struct timespec *value);
int __real_clock_nanosleep(clockid_t clock, int flags, const struct timespec *deadline, struct timespec *remaining);
-int __wrap_clock_gettime(clockid_t clock, struct timespec *value)
+int __wrap_clock_nanosleep(clockid_t clock, int flags, const struct timespec *deadline, struct timespec *remaining)
{
- if (!fake_clock) return __real_clock_gettime(clock, value);
- assert(clock == CLOCK_MONOTONIC);
- value->tv_sec = current_time / 1000000000;
- value->tv_nsec = current_time % 1000000000;
- return 0;
+ assert(!checking_wait);
+ return __real_clock_nanosleep(clock, flags, deadline, remaining);
}
-int __wrap_clock_nanosleep(clockid_t clock, int flags, const struct timespec *deadline, struct timespec *remaining)
+recomp_func_t recomp_lookup(uint32_t address)
{
- if (!fake_clock) return __real_clock_nanosleep(clock, flags, deadline, remaining);
- assert(clock == CLOCK_MONOTONIC && flags == TIMER_ABSTIME && !remaining);
- assert(waits < 5);
- deadlines[waits] = (uint64_t)deadline->tv_sec * 1000000000 + deadline->tv_nsec;
- current_time = deadlines[waits++] + oversleep;
- return 0;
+ assert(address == 0x11110000);
+ return linux_window_lookup("user32.dll", "DefWindowProcA");
}
-
-recomp_func_t recomp_lookup(uint32_t address) { assert(!"Unexpected guest callback"); return NULL; }
recomp_func_t recomp_lookup_import(uint32_t address) { return recomp_lookup(address); }
+static recomp_func_t virtual_method(uint32_t object, unsigned slot)
+{
+ return linux_graphics_virtual(MEM32(MEM32(object) + slot * 4));
+}
+
static uint32_t invoke(recomp_func_t function, unsigned count, const uint32_t *arguments)
{
uint32_t stack = g_esp, caller = g_cur_func;
@@ -52,39 +49,72 @@ static uint32_t invoke(recomp_func_t function, unsigned count, const uint32_t *a
int main(void)
{
assert(guest_map("browser pacing test", 0x50000000, 0x10000));
+ assert(!emscripten_is_main_browser_thread());
g_esp = 0x5000ff00;
const uint32_t draw7[] = {0x15e65ec0, 0x11d23b9c, 0x60002fb9, 0x5bea9797};
memcpy((void *)ADDR(0x50001000), draw7, sizeof(draw7));
const uint32_t create[] = {0, 0x50001010, 0x50001000, 0};
assert(!invoke(linux_graphics_lookup("DirectDrawCreateEx"), 4, create));
uint32_t object = MEM32(0x50001010);
+ assert(!invoke(virtual_method(object, 20), 3, (uint32_t[]){object, 0, 0x808}));
uint32_t arguments[] = {object, 1, 0};
- recomp_func_t wait = linux_graphics_virtual(MEM32(MEM32(object) + 22 * 4));
+ recomp_func_t wait = virtual_method(object, 22);
assert(wait);
- fake_clock = 1;
- current_time = 1000000000;
- assert(!invoke(wait, 3, arguments));
- current_time = 1005000000;
- assert(!invoke(wait, 3, arguments));
- current_time = 1040000000;
- assert(!invoke(wait, 3, arguments));
- current_time = 1041000000;
- oversleep = 3000000;
- assert(!invoke(wait, 3, arguments));
- current_time = 1060000000;
- oversleep = 0;
- assert(!invoke(wait, 3, arguments));
- fake_clock = 0;
+ /* A real hidden guest window and primary blit exercise the skipped-copy path. */
+ uint32_t record = 0x50002000, text = 0x50002100, desc = 0x50002200;
+ MEM32(record + 4) = 0x11110000;
+ MEM32(record + 16) = 1;
+ MEM32(record + 36) = text;
+ strcpy((void *)ADDR(text), "AO pacing test");
+ uint32_t atom = invoke(linux_window_lookup("user32.dll", "RegisterClassA"), 1, &record);
+ assert(atom);
+ uint32_t window_arguments[] = {0, atom, text, 0, 0, 0, 32, 16, 0, 0, 1, 0};
+ uint32_t window = invoke(linux_window_lookup("user32.dll", "CreateWindowExA"), 12, window_arguments);
+ assert(window && (SDL_GetWindowFlags(linux_window_native(window)) & SDL_WINDOW_HIDDEN));
+ assert(!invoke(virtual_method(object, 4), 4, (uint32_t[]){object, 0, 0x50001010, 0}));
+ uint32_t clipper = MEM32(0x50001010);
+ assert(!invoke(virtual_method(clipper, 8), 3, (uint32_t[]){clipper, 0, window}));
+
+ MEM32(desc) = 124;
+ MEM32(desc + 4) = 1;
+ MEM32(desc + 104) = 0x200;
+ assert(!invoke(virtual_method(object, 6), 4, (uint32_t[]){object, desc, 0x50001010, 0}));
+ uint32_t primary = MEM32(0x50001010);
+ assert(!invoke(virtual_method(primary, 28), 2, (uint32_t[]){primary, clipper}));
+ MEM32(desc + 4) = 7;
+ MEM32(desc + 8) = 16;
+ MEM32(desc + 12) = 32;
+ MEM32(desc + 104) = 0x840;
+ assert(!invoke(virtual_method(object, 6), 4, (uint32_t[]){object, desc, 0x50001010, 0}));
+ uint32_t source = MEM32(0x50001010), rectangle = 0x50002300;
+ memcpy((void *)ADDR(rectangle), (uint32_t[]){0, 0, 32, 16}, 16);
+ EM_ASM({
+ Module.refreshTimes = [];
+ const refresh = globalThis.requestAnimationFrame;
+ globalThis.requestAnimationFrame = callback => refresh(time => {
+ Module.refreshTimes.push(time);
+ callback(time);
+ });
+ });
+
+ for (unsigned i = 0; i < 5; ++i) {
+ if (i == 2)
+ invoke(linux_window_lookup("user32.dll", "ShowWindow"), 2, (uint32_t[]){window, 1});
+ if (i == 4)
+ memcpy((void *)ADDR(rectangle), (uint32_t[]){10000, 10000, 10032, 10016}, 16);
+ checking_wait = 1;
+ assert(!invoke(wait, 3, arguments));
+ checking_wait = 0;
+ assert(EM_ASM_INT({ return Module.refreshTimes.length; }) == i);
+ assert(!invoke(virtual_method(primary, 5), 6,
+ (uint32_t[]){primary, rectangle, source, 0, 0x200, 0}));
+ assert(EM_ASM_INT({ return Module.refreshTimes.length; }) == i + 1);
+ }
- assert(waits == 5);
- assert(deadlines[2] == 1040000000);
- assert(deadlines[0] == 1000000000);
- assert(deadlines[1] == 1016666666);
- assert(deadlines[3] == 1056666666);
- assert(deadlines[4] == 1076333332);
+ assert(EM_ASM_INT({ return Module.refreshTimes.every((time, i, times) => !i || time > times[i - 1]); }));
arguments[1] = 0;
assert(invoke(wait, 3, arguments) == 0x80070057);
linux_graphics_shutdown();
- puts("PASS: browser pacing waits for fast frames, releases late frames, and avoids catch-up after oversleep; guest ABI preserved");
+ puts("PASS: hidden, visible and clipped primary blits await animation frames without an extra timer wait; guest ABI preserved");
}
diff --git a/src/tests/browser_suspension.c b/src/tests/browser_suspension.c
@@ -24,5 +24,5 @@ int main(void)
if (!result) yield_and_return();
assert(result == 7 && resumed == 1);
- puts("PASS: presentation yields reuse their message channel and resume through a Wasm longjmp landing pad");
+ puts("PASS: animation-frame presentation yields resume through a Wasm longjmp landing pad");
}