A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(sdk): listen for context loss on the canvas, not on window
esengine414528611cb6
diff --git a/desktop/scripts/probe-webgl-restore.mjs b/desktop/scripts/probe-webgl-restore.mjsnew file mode 100644index 000000000..0e8539448--- /dev/null+++ b/desktop/scripts/probe-webgl-restore.mjs@@ -0,0 +1,47 @@+// Does Chromium hand a WebGL2 context back at all, in THIS environment?+// No engine, no wasm — just the extension and the spec's contract, so a red+// result here means the harness is wrong and a green one means the engine is.+import { app, BrowserWindow } from 'electron';++const HTML = `data:text/html,<html><body><canvas id="c" width="64" height="64"></canvas></body></html>`;++++app.whenReady().then(async () => {+ const win = new BrowserWindow({ show: false, width: 200, height: 200 });+ await win.loadURL(HTML);++ const probe = async (listenOn) => win.webContents.executeJavaScript(`(async () => {+ const c = document.getElementById('c');+ const gl = c.getContext('webgl2');+ const ext = gl && gl.getExtension('WEBGL_lose_context');+ const out = { listenOn: ${JSON.stringify(listenOn)}, hasGl: !!gl, hasExt: !!ext };+ if (!ext) return out;++ let restoredFired = false;+ const target = ${listenOn === 'window' ? 'window' : 'c'};+ const capture = ${listenOn === 'window'};+ target.addEventListener('webglcontextlost', (e) => { e.preventDefault(); }, capture);+ target.addEventListener('webglcontextrestored', () => { restoredFired = true; }, capture);++ ext.loseContext();+ await new Promise((r) => setTimeout(r, 200));+ out.lostAfterLose = gl.isContextLost();++ // The spec says restoreContext may only be honoured after the lost event+ // has been dispatched, so this deliberately runs in a later task.+ ext.restoreContext();+ await new Promise((r) => setTimeout(r, 1500));+ out.lostAfterRestore = gl.isContextLost();+ out.restoredEventFired = restoredFired;+ return out;+ })()`, true);++ // A fresh page per variant: a context that already died would poison the next.+ const onCanvas = await probe('canvas');+ await win.loadURL(HTML);+ const onWindow = await probe('window');++ console.log('PROBE ' + JSON.stringify({ onCanvas, onWindow }));+ app.quit();+});diff --git a/sdk/src/render/renderer.ts b/sdk/src/render/renderer.tsindex dcf4599db..dd09a93ae 100644--- a/sdk/src/render/renderer.ts+++ b/sdk/src/render/renderer.ts@@ -5,6 +5,7 @@ import { CoreApiBridge } from '../wasm/CoreApiBridge';import { handleWasmError } from '../wasm/wasmError';import { requireResourceManager } from '../wasm/resourceManager';import { platformOnContextLost } from '../platform';+import { findWebGL2Context } from '../asset/loaders/TextureLoader';import { decodeFrameCapture, replayToDrawCall as replayToDrawCallImpl, getSnapshotImageData as getSnapshotImpl, type FrameCaptureData } from './frameCapture';export enum RenderStage {@@ -212,19 +213,30 @@ let lossGuardInstalled = false;* abandons the context for good. That decision belongs to the renderer, not to* whether a diagnostics plugin happens to be installed.*/-function installContextLossGuard(): void {+function installContextLossGuard(m: ESEngineModule): void {if (lossGuardInstalled) return;lossGuardInstalled = true;- platformOnContextLost(() => {+ const onLost = (e?: Event): void => {+ e?.preventDefault();reportDeviceLost(DeviceLostReason.ContextLost, 'The host reported the rendering context was lost');- });+ };++ // On the CANVAS, not on window: an unattached canvas has no path to window+ // at all — not even a capture phase — and the engine's own canvas is exactly+ // that in a headless host.+ const canvas = findWebGL2Context(m.GL)?.canvas as EventTarget | undefined;+ if (canvas?.addEventListener) {+ canvas.addEventListener('webglcontextlost', onLost as EventListener);+ return;+ }+ platformOnContextLost(() => onLost());}/** @internal Wired by the engine plugins — not part of the public API. */export function initRendererAPI(wasmModule: ESEngineModule): void {bridge.connect(wasmModule);module = bridge.module;- installContextLossGuard();+ installContextLossGuard(module);viewProjectionPtr = module._malloc(16 * 4);backend = wasmBackend(module);}