aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit 7bc50381cc778015b911665a5be605a0aa082a44
parent dea58a30b640c83d782a3b1da57f1db2864408f1
Author: anon <anon@example.com>
Date:   Sat, 17 Oct 2026 05:32:03 +0000

Avoid delaying late browser frames by another refresh period

Diffstat:
Msrc/Makefile.wasm | 4++++
Msrc/runtime/linux_graphics.c | 12++++++++++++
Asrc/tests/browser_pacing.c | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 106 insertions(+), 0 deletions(-)

diff --git a/src/Makefile.wasm b/src/Makefile.wasm @@ -49,6 +49,10 @@ build/wasm/web/suspension-test.html: tests/browser_suspension.c runtime/browser_ @mkdir -p $(@D) $(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/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 $@ + build/wasm/web/audio-output-test.html: tests/browser_audio.html build/wasm/web/audio-output-test.js cp $< $@ diff --git a/src/runtime/linux_graphics.c b/src/runtime/linux_graphics.c @@ -292,11 +292,23 @@ static void wait_for_vertical_blank(void) 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; } diff --git a/src/tests/browser_pacing.c b/src/tests/browser_pacing.c @@ -0,0 +1,90 @@ +#include "linux_graphics.h" +#include "guest_memory.h" +#include <assert.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include <time.h> + +static _Thread_local int fake_clock; +static uint64_t current_time, oversleep, deadlines[5]; +static unsigned waits; + +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) +{ + 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; +} + +int __wrap_clock_nanosleep(clockid_t clock, int flags, const struct timespec *deadline, struct timespec *remaining) +{ + 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; +} + +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 uint32_t invoke(recomp_func_t function, unsigned count, const uint32_t *arguments) +{ + uint32_t stack = g_esp, caller = g_cur_func; + for (unsigned i = count; i; --i) PUSH32(g_esp, arguments[i - 1]); + PUSH32(g_esp, RECOMP_RETADDR); + g_ebx = 0x12345678; g_esi = 0x23456789; + g_edi = 0x3456789a; g_ebp = 0x456789ab; + function(); + assert(g_esp == stack && g_cur_func == caller); + assert(g_ebx == 0x12345678 && g_esi == 0x23456789); + assert(g_edi == 0x3456789a && g_ebp == 0x456789ab); + return g_eax; +} + +int main(void) +{ + assert(guest_map("browser pacing test", 0x50000000, 0x10000)); + 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); + uint32_t arguments[] = {object, 1, 0}; + recomp_func_t wait = linux_graphics_virtual(MEM32(MEM32(object) + 22 * 4)); + 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; + + assert(waits == 5); + assert(deadlines[2] == 1040000000); + assert(deadlines[0] == 1000000000); + assert(deadlines[1] == 1016666666); + assert(deadlines[3] == 1056666666); + assert(deadlines[4] == 1076333332); + 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"); +}