aorecomp

Anarchy Online classic in the browser

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


commit a894a2d4d488ee2c5ebe3e0171157805d602a901
parent f97e81083641b505095b06bfcb943b884f1c3c28
Author: anon <anon@example.com>
Date:   Tue, 20 Oct 2026 17:03:27 +0000

Fall back to page-thread WebGL when worker graphics is unavailable

Diffstat:
Msrc/Makefile.wasm | 8++++----
Msrc/browser/index.html | 40++++++++++++++++++++++------------------
Msrc/runtime/browser_host.js | 34++++++++++++++++++++++++++++++----
Msrc/runtime/linux_graphics.c | 20++++++++++++++------
Msrc/tests/browser_context.c | 53++++++++++++++++++++++++++++++++++++++---------------
Msrc/tests/browser_context.mjs | 37++++++++++++++++++++++++++++++++++---
Msrc/tests/browser_loading.mjs | 41+++++++++++++++++++++++++++++++++--------
7 files changed, 175 insertions(+), 58 deletions(-)

diff --git a/src/Makefile.wasm b/src/Makefile.wasm @@ -69,18 +69,18 @@ build/wasm/browser-network-timeout.js: tests/browser_network_timeout.c game/er.d build/wasm/web/devices-test.html: tests/devices.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_profile.c runtime/browser_host.js runtime/browser_profile.js Makefile.wasm @mkdir -p $(@D) - $(CC) -sUSE_ZLIB=1 $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -sUSE_SDL=2 -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 -sEXIT_RUNTIME --js-library runtime/browser_host.js --js-library runtime/browser_profile.js -o $@ + $(CC) -sUSE_ZLIB=1 $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -sUSE_SDL=2 -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=2 -sALLOW_BLOCKING_ON_MAIN_THREAD=0 -sALLOW_MEMORY_GROWTH -sOFFSCREENCANVAS_SUPPORT -sOFFSCREEN_FRAMEBUFFER -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sEXIT_RUNTIME --js-library runtime/browser_host.js --js-library runtime/browser_profile.js -o $@ build/wasm/web/context-test.html: tests/browser_context.html build/wasm/web/context-test.js cp $< $@ build/wasm/web/context-test.js: tests/browser_context.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_profile.c runtime/browser_host.js runtime/browser_profile.js Makefile.wasm @mkdir -p $(@D) - $(CC) -sUSE_ZLIB=1 $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -sUSE_SDL=2 -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 -sEXIT_RUNTIME --js-library runtime/browser_host.js --js-library runtime/browser_profile.js -o $@ + $(CC) -sUSE_ZLIB=1 $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -sUSE_SDL=2 -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=2 -sALLOW_BLOCKING_ON_MAIN_THREAD=0 -sALLOW_MEMORY_GROWTH -sOFFSCREENCANVAS_SUPPORT -sOFFSCREEN_FRAMEBUFFER -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sEXIT_RUNTIME --js-library runtime/browser_host.js --js-library runtime/browser_profile.js -o $@ build/wasm/web/game.js build/wasm/web/game.wasm &: $(OBJECTS) runtime/browser_network.js runtime/browser_host.js runtime/browser_fetch.js runtime/browser_audio.js runtime/browser_profile.js ../local/wasm/deps/lib/libsndfile.a ../local/wasm/deps/lib/libmp3lame.a Makefile.wasm @mkdir -p $(@D) - $(CC) $(CFLAGS) $(PORT_FLAGS) $(OBJECTS) ../local/wasm/deps/lib/libsndfile.a ../local/wasm/deps/lib/libmp3lame.a -sUSE_MPG123=1 -sWASMFS -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=8 -sALLOW_BLOCKING_ON_MAIN_THREAD=0 -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sOFFSCREENCANVAS_SUPPORT -sALLOW_MEMORY_GROWTH -sINITIAL_MEMORY=268435456 -sSTACK_SIZE=1048576 --js-library runtime/browser_network.js --js-library runtime/browser_host.js --js-library runtime/browser_fetch.js --js-library runtime/browser_audio.js --js-library runtime/browser_profile.js -sBINARYEN_EXTRA_PASSES=merge-similar-functions -Wl,--thinlto-jobs=8 -Wl,--thinlto-cache-dir=build/wasm/lto-cache --emit-symbol-map -o build/wasm/web/game.js + $(CC) $(CFLAGS) $(PORT_FLAGS) $(OBJECTS) ../local/wasm/deps/lib/libsndfile.a ../local/wasm/deps/lib/libmp3lame.a -sUSE_MPG123=1 -sWASMFS -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=8 -sALLOW_BLOCKING_ON_MAIN_THREAD=0 -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sOFFSCREENCANVAS_SUPPORT -sOFFSCREEN_FRAMEBUFFER -sALLOW_MEMORY_GROWTH -sINITIAL_MEMORY=268435456 -sSTACK_SIZE=1048576 --js-library runtime/browser_network.js --js-library runtime/browser_host.js --js-library runtime/browser_fetch.js --js-library runtime/browser_audio.js --js-library runtime/browser_profile.js -sBINARYEN_EXTRA_PASSES=merge-similar-functions -Wl,--thinlto-jobs=8 -Wl,--thinlto-cache-dir=build/wasm/lto-cache --emit-symbol-map -o build/wasm/web/game.js build/wasm/web/index.html: browser/index.html @mkdir -p $(@D) @@ -121,7 +121,7 @@ build/wasm/web/loop-test.html: tests/browser_loop.c runtime/browser_loop.c runti 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 runtime/browser_profile.c runtime/browser_profile.js @mkdir -p $(@D) - $(CC) -sUSE_ZLIB=1 $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) --js-library runtime/browser_host.js -sUSE_SDL=2 -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 $@ --js-library runtime/browser_profile.js + $(CC) -sUSE_ZLIB=1 $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) --js-library runtime/browser_host.js -sUSE_SDL=2 -sPROXY_TO_PTHREAD -sPTHREAD_POOL_SIZE=2 -sALLOW_BLOCKING_ON_MAIN_THREAD=0 -sALLOW_MEMORY_GROWTH -sOFFSCREENCANVAS_SUPPORT -sOFFSCREEN_FRAMEBUFFER -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -Wl,--wrap=clock_nanosleep -o $@ --js-library runtime/browser_profile.js build/wasm/web/audio-output-test.html: tests/browser_audio.html build/wasm/web/audio-output-test.js build/wasm/web/audio-worklet.js cp $< $@ diff --git a/src/browser/index.html b/src/browser/index.html @@ -287,26 +287,30 @@ show(); }; async function startGame() { if (!crossOriginIsolated) { status.textContent = 'Shared memory is unavailable'; return; } - if (!canvas.transferControlToOffscreen) { status.textContent = 'This game requires OffscreenCanvas, which this browser does not support'; return; } - // Some browsers expose OffscreenCanvas without supporting WebGL in workers. - const probeURL = URL.createObjectURL(new Blob([ - "postMessage(!!new OffscreenCanvas(1, 1).getContext('webgl2', {alpha: false, antialias: false}));" - ], {type: 'text/javascript'})); - const probe = new Worker(probeURL); - let graphicsReady; - try { - graphicsReady = await new Promise((resolve, reject) => { - probe.onmessage = event => resolve(event.data); - probe.onerror = event => reject(new Error(event.message)); - }); - } finally { - probe.terminate(); - URL.revokeObjectURL(probeURL); + let workerGraphics = false; + if (canvas.transferControlToOffscreen) { + const probeURL = URL.createObjectURL(new Blob([ + "postMessage(!!new OffscreenCanvas(1, 1).getContext('webgl2', {alpha: false, antialias: false}));" + ], {type: 'text/javascript'})); + const probe = new Worker(probeURL); + try { + workerGraphics = await new Promise((resolve, reject) => { + probe.onmessage = event => resolve(event.data); + probe.onerror = event => reject(new Error(event.message)); + }); + } finally { + probe.terminate(); + URL.revokeObjectURL(probeURL); + } } - if (!graphicsReady) { - status.textContent = 'This browser could not initialize WebGL 2 in a worker. Check graphics settings or try another browser.'; - return; + if (!workerGraphics) { + if (!canvas.getContext('webgl2', {alpha: false, antialias: false})) { + status.textContent = 'This browser could not initialize WebGL 2. Check graphics settings or try another browser.'; + return; + } + // Keep this canvas on the page when Emscripten starts the game worker. + canvas.transferControlToOffscreen = undefined; } Module.audioContext = new AudioContext(); // Load immediately; browsers unlock audio on the first game interaction. diff --git a/src/runtime/browser_host.js b/src/runtime/browser_host.js @@ -25,13 +25,39 @@ addToLibrary({ } }, $AOPresented: false, - ao_graphics_initialize__deps: ['$GL', 'browser_graphics_lost'], - ao_graphics_initialize: () => { - // OffscreenCanvas events belong to the worker that owns the WebGL context. - GL.currentContext.GLctx.canvas.addEventListener('webglcontextlost', event => { + $AOInitializeGraphics__deps: ['$GL', 'browser_graphics_lost'], + $AOInitializeGraphics: context => { + // Context events run on the thread that owns the actual WebGL object. + context.GLctx.canvas.addEventListener('webglcontextlost', event => { event.preventDefault(); _browser_graphics_lost(); }); + context.GLctx.canvas.addEventListener('webglcontextrestored', () => { + if (context.defaultFbo) GL.createOffscreenFramebuffer(context); + }); + }, + ao_graphics_initialize_main__deps: ['$GL', '$AOInitializeGraphics'], + ao_graphics_initialize_main__proxy: 'sync', + ao_graphics_initialize_main: (context) => AOInitializeGraphics(GL.getContext(context)), + ao_graphics_initialize__deps: ['$GL', '$AOInitializeGraphics', 'ao_graphics_initialize_main'], + ao_graphics_initialize: () => { + if (GL.currentContextIsProxied) _ao_graphics_initialize_main(GL.currentContext); + else AOInitializeGraphics(GL.currentContext); + }, + // Context state and destruction need explicit forwarding to their owning thread. + ao_graphics_context_lost_main__deps: ['$GL'], + ao_graphics_context_lost_main__proxy: 'sync', + ao_graphics_context_lost_main: (context) => !GL.getContext(context) || GL.getContext(context).GLctx.isContextLost(), + emscripten_is_webgl_context_lost__deps: ['$GL', 'ao_graphics_context_lost_main'], + emscripten_is_webgl_context_lost: (context) => GL.getContext(context) + ? GL.getContext(context).GLctx.isContextLost() : _ao_graphics_context_lost_main(context), + ao_graphics_destroy_main__deps: ['$GL'], + ao_graphics_destroy_main__proxy: 'sync', + ao_graphics_destroy_main: (context) => { GL.deleteContext(context); }, + emscripten_webgl_destroy_context__deps: ['$GL', 'ao_graphics_destroy_main'], + emscripten_webgl_destroy_context: (context) => { + if (GL.getContext(context)) GL.deleteContext(context); + else _ao_graphics_destroy_main(context); }, ao_graphics_status__proxy: 'async', ao_graphics_status: (lost) => { Module.onGraphicsStatus?.(lost); }, diff --git a/src/runtime/linux_graphics.c b/src/runtime/linux_graphics.c @@ -8,6 +8,7 @@ #include <GLES3/gl3.h> #include <emscripten.h> #include <emscripten/html5.h> +#include <stdatomic.h> #else #include <SDL2/SDL_opengl.h> #endif @@ -22,7 +23,8 @@ static GLint maximum_texture_size; #ifdef __EMSCRIPTEN__ static EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context; static SDL_DisplayMode browser_display; -static int graphics_lost; +static _Atomic int graphics_lost; +static int proxied_context; void ao_graphics_initialize(void); void ao_graphics_status(int lost); @@ -35,10 +37,9 @@ EMSCRIPTEN_KEEPALIVE void browser_graphics_lost(void) int browser_graphics_ready(void) { if (context && emscripten_is_webgl_context_lost(context)) return 0; - if (graphics_lost) { + if (atomic_exchange(&graphics_lost, 0)) { gl_renderer_restore(); glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maximum_texture_size); - graphics_lost = 0; ao_graphics_status(0); fprintf(stderr, "[browser-graphics] WebGL resources restored\n"); } @@ -126,6 +127,7 @@ void linux_graphics_shutdown(void) #ifdef __EMSCRIPTEN__ if (context) emscripten_webgl_destroy_context(context); graphics_lost = 0; + proxied_context = 0; #else SDL_GL_DeleteContext(context); #endif @@ -162,16 +164,20 @@ static int initialize_graphics(void) ); if (!window) goto failed; #ifdef __EMSCRIPTEN__ - /* Own WebGL on the game worker; SDL still handles window/input events. */ + /* Keep rendering local when the canvas was transferred to this worker. */ + proxied_context = EM_ASM_INT({ return !Module.canvas; }); EmscriptenWebGLContextAttributes attributes; emscripten_webgl_init_context_attributes(&attributes); attributes.majorVersion = 2; attributes.alpha = attributes.antialias = 0; - attributes.proxyContextToMainThread = EMSCRIPTEN_WEBGL_CONTEXT_PROXY_DISALLOW; + attributes.proxyContextToMainThread = proxied_context ? + EMSCRIPTEN_WEBGL_CONTEXT_PROXY_ALWAYS : EMSCRIPTEN_WEBGL_CONTEXT_PROXY_DISALLOW; + attributes.explicitSwapControl = attributes.renderViaOffscreenBackBuffer = proxied_context; context = emscripten_webgl_create_context("#canvas", &attributes); if (context <= 0) { context = 0; goto failed; } if (emscripten_webgl_make_context_current(context) != EMSCRIPTEN_RESULT_SUCCESS) goto failed; ao_graphics_initialize(); + fprintf(stderr, "[browser-graphics] WebGL on %s\n", proxied_context ? "page thread" : "game worker"); #else context = SDL_GL_CreateContext(window); if (!context) goto failed; @@ -1507,7 +1513,9 @@ static void surface_blit(void) SDL_GL_GetDrawableSize(native, &width, &height); #endif gl_target_present(target->target, client, width, height); -#ifndef __EMSCRIPTEN__ +#ifdef __EMSCRIPTEN__ + if (proxied_context && emscripten_webgl_commit_frame() != EMSCRIPTEN_RESULT_SUCCESS) abort(); +#else SDL_GL_SwapWindow(native); #endif } diff --git a/src/tests/browser_context.c b/src/tests/browser_context.c @@ -4,6 +4,8 @@ #include <assert.h> #include <emscripten.h> #include <emscripten/html5.h> +#include <emscripten/threading.h> +#include <stdatomic.h> #include <stdio.h> #include <string.h> @@ -11,20 +13,31 @@ static struct SDL_Window *window; static struct gl_target *image, *first, *second; static struct gl_depth *depth; static unsigned iteration; +static atomic_int restored; -EM_JS(void, lose_context, (void), { - Module.testRestored = false; - const context = GL.currentContext.GLctx; - const extension = context.getExtension('WEBGL_lose_context'); - if (!extension) throw new Error('WEBGL_lose_context is required for this test'); - context.canvas.addEventListener('webglcontextlost', () => { - setTimeout(() => extension.restoreContext(), 50); - }, {once: true}); - context.canvas.addEventListener('webglcontextrestored', () => { - Module.testRestored = true; - }, {once: true}); - extension.loseContext(); -}); +static void lose_context_on_owner(void) +{ + EM_ASM({ + Atomics.store(HEAP32, $0 >> 2, 0); + const context = GL.currentContext.GLctx; + const extension = context.getExtension('WEBGL_lose_context'); + if (!extension) throw new Error('WEBGL_lose_context is required for this test'); + context.canvas.addEventListener('webglcontextlost', () => { + setTimeout(() => extension.restoreContext(), 50); + }, {once: true}); + context.canvas.addEventListener('webglcontextrestored', () => { + Atomics.store(HEAP32, $0 >> 2, 1); + }, {once: true}); + extension.loseContext(); + }, &restored); +} + +static void lose_context(void) +{ + if (EM_ASM_INT({ return !!GL.currentContextIsProxied; })) + emscripten_sync_run_in_main_runtime_thread(EM_FUNC_SIG_V, lose_context_on_owner); + else lose_context_on_owner(); +} static bool restored_frame(double timestamp, void *argument); @@ -64,7 +77,7 @@ static bool restored_frame(double timestamp, void *argument) { (void)timestamp; (void)argument; - if (!EM_ASM_INT({ return Module.testRestored; })) return true; + if (!atomic_load(&restored)) return true; const unsigned char expected[] = {0, 0, 255, 255, 255, 255, 255, 255, 255, 0, 0, 255, 0, 255, 0, 255}; const struct gl_vertex vertices[] = { @@ -95,6 +108,16 @@ static bool restored_frame(double timestamp, void *argument) assert(!memcmp(actual, expected, sizeof(expected))); gl_target_read(second, actual); assert(!memcmp(actual, expected, sizeof(expected))); + + /* Resize and present through the default framebuffer after every restoration. */ + assert(emscripten_set_canvas_element_size("#canvas", 2 + iteration, 2 + iteration) == EMSCRIPTEN_RESULT_SUCCESS); + gl_target_present(first, clip, 2, 2); + glReadPixels(0, 0, 2, 2, GL_RGBA, GL_UNSIGNED_BYTE, actual); + const unsigned char presented[] = {255, 0, 0, 255, 0, 255, 0, 255, + 0, 0, 255, 255, 255, 255, 255, 255}; + assert(!memcmp(actual, presented, sizeof(presented))); + if (EM_ASM_INT({ return !!GL.currentContextIsProxied; })) + assert(emscripten_webgl_commit_frame() == EMSCRIPTEN_RESULT_SUCCESS); assert(glGetError() == GL_NO_ERROR); if (++iteration < 3) { lose_context(); @@ -105,6 +128,6 @@ static bool restored_frame(double timestamp, void *argument) gl_target_destroy(second); gl_depth_destroy(depth); linux_graphics_shutdown(); - puts("PASS: repeated worker context loss restores textures, shared depth, shaders and drawing without JSPI"); + puts("PASS: repeated context loss restores textures, shared depth, shaders, resizing and presentation without JSPI"); return false; } diff --git a/src/tests/browser_context.mjs b/src/tests/browser_context.mjs @@ -3,11 +3,24 @@ import fs from 'node:fs'; import vm from 'node:vm'; import test from 'node:test'; -function graphics() { +function graphics(proxied = false) { const canvas = new EventTarget(); - let lost = false, notifications = 0, library; + let lost = false, notifications = 0, framebuffers = 0, queries = 0, library; + const context = {defaultFbo: proxied ? {} : null, GLctx: {canvas, isContextLost: () => lost}}; vm.runInNewContext(fs.readFileSync(new URL('../runtime/browser_host.js', import.meta.url), 'utf8'), { - GL: {currentContext: {GLctx: {canvas, isContextLost: () => lost}}}, + GL: {currentContext: proxied ? 7 : context, currentContextIsProxied: proxied, + getContext: handle => !proxied && handle === 7 ? context : undefined, + createOffscreenFramebuffer: () => ++framebuffers}, + AOInitializeGraphics: context => library.$AOInitializeGraphics(context), + _ao_graphics_initialize_main: handle => { + assert.equal(handle, 7); + library.$AOInitializeGraphics(context); + }, + _ao_graphics_context_lost_main: handle => { + assert.equal(handle, 7); + ++queries; + return lost; + }, _browser_graphics_lost: () => ++notifications, addToLibrary: value => { library = value; } }); @@ -15,6 +28,8 @@ function graphics() { return { library, notifications: () => notifications, + framebuffers: () => framebuffers, + queries: () => queries, lose() { lost = true; const event = new Event('webglcontextlost', {cancelable: true}); @@ -38,3 +53,19 @@ test('context loss allows restoration and notifies the game on every loss', () = assert.equal(game.notifications(), 3); assert.equal(game.library.ao_graphics_wait, undefined); }); + +test('proxied context loss reaches the page and rebuilds its presentation framebuffer', () => { + const game = graphics(true); + + const event = game.lose(); + const lost = game.library.emscripten_is_webgl_context_lost(7); + game.restore(); + const restored = game.library.emscripten_is_webgl_context_lost(7); + + assert.equal(event.defaultPrevented, true); + assert.equal(game.notifications(), 1); + assert.equal(lost, true); + assert.equal(restored, false); + assert.equal(game.framebuffers(), 1); + assert.equal(game.queries(), 2); +}); diff --git a/src/tests/browser_loading.mjs b/src/tests/browser_loading.mjs @@ -5,7 +5,7 @@ import vm from 'node:vm'; function fixture(response) { const progress = []; const loading = {hidden: false, set textContent(value) { progress.push(value); }}; - const canvas = {addEventListener() {}}; + const canvas = {addEventListener() {}, getContext() { return null; }}; const context = vm.createContext({ console, WebAssembly, Response, TransformStream, crossOriginIsolated: true, navigator: {userAgent: 'Mozilla/5.0 (X11; Linux x86_64)'}, @@ -55,16 +55,15 @@ async function rejectsFailedClientDownload() { /Client download: HTTP 404/); } -async function reportsMissingOffscreenCanvas() { +async function reportsMissingWebGL() { const test = fixture(); - test.context.WebAssembly = {}; await test.context.startGame(); - assert.deepEqual(test.progress, ['This game requires OffscreenCanvas, which this browser does not support']); + assert.deepEqual(test.progress, ['This browser could not initialize WebGL 2. Check graphics settings or try another browser.']); } -async function reportsUnavailableWorkerWebGLBeforeDownloading() { +async function reportsUnavailableWebGLOnBothThreadsBeforeDownloading() { const test = fixture(); test.context.document.querySelector('#canvas').transferControlToOffscreen = () => {}; let terminated = false, revoked = null; @@ -84,14 +83,40 @@ async function reportsUnavailableWorkerWebGLBeforeDownloading() { await test.context.startGame(); assert.deepEqual(test.progress, [ - 'This browser could not initialize WebGL 2 in a worker. Check graphics settings or try another browser.' + 'This browser could not initialize WebGL 2. Check graphics settings or try another browser.' ]); assert.equal(terminated, true); assert.equal(revoked, 'blob:graphics-probe'); } +async function startsWithMainThreadGraphicsWithoutOffscreenCanvas() { + const test = fixture(); + const canvas = test.context.document.querySelector('#canvas'); + canvas.getContext = type => { assert.equal(type, 'webgl2'); return {}; }; + canvas.clientWidth = 1024; + canvas.clientHeight = 768; + const scripts = []; + test.context.AudioContext = class {}; + test.context.document.addEventListener = () => {}; + test.context.document.createElement = () => ({}); + test.context.document.body = {append: script => scripts.push(script.src)}; + test.context.fetch = async (url, options) => { + assert.equal(url, '/browser-launch'); + assert.equal(options.method, 'POST'); + return new Response('Clientr.exe localtest localtest 73'); + }; + + await test.context.startGame(); + + assert.deepEqual(scripts, ['/wasm/game.js']); + assert.equal(canvas.transferControlToOffscreen, undefined); + assert.equal(canvas.width, 1024); + assert.equal(canvas.height, 768); +} + await streamsDecodedClientAndTracksProgress(); await rejectsFailedClientDownload(); -await reportsMissingOffscreenCanvas(); -await reportsUnavailableWorkerWebGLBeforeDownloading(); +await reportsMissingWebGL(); +await reportsUnavailableWebGLOnBothThreadsBeforeDownloading(); +await startsWithMainThreadGraphicsWithoutOffscreenCanvas(); console.log('PASS: loading progress, streaming compilation, first frame, download failure and missing browser graphics');