Skip to content

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.

fix(sdk): a lost context could never have come back

esengine3a2f786ed6e3
diff --git a/desktop/scripts/headless-verify.mjs b/desktop/scripts/headless-verify.mjs
index f74d4ff51..7a8b7d34c 100644
--- a/desktop/scripts/headless-verify.mjs
+++ b/desktop/scripts/headless-verify.mjs
@@ -224,15 +224,19 @@ app.whenReady().then(async () => {
const out = { supported: d.lose() };
if (!out.supported) return out;
- // The browser reports the loss to JS asynchronously; step so the engine's
- // own poll sees it too, then confirm it refuses to submit.
- await new Promise((r) => setTimeout(r, 100));
- api.step(3, 1 / 60);
+ // The browser reports the loss asynchronously and the engine polls on its
+ // own frame, so both are waited for rather than assumed.
+ for (let i = 0; i < 30 && d.status() === 0; i++) {
+ api.step(1, 1 / 60);
+ await new Promise((r) => setTimeout(r, 50));
+ }
out.statusAfterLoss = d.status();
out.reportAfterLoss = d.report();
+ out.glLostBeforeRestore = d.contextLost();
d.restore();
- await new Promise((r) => setTimeout(r, 300));
+ await new Promise((r) => setTimeout(r, 500));
+ out.glLostAfterRestore = d.contextLost();
// A context comes back when the browser is ready, not when asked.
out.recovered = false;
diff --git a/desktop/src/headless.ts b/desktop/src/headless.ts
index 51c983547..36a5ab5e4 100644
--- a/desktop/src/headless.ts
+++ b/desktop/src/headless.ts
@@ -32,6 +32,7 @@ declare global {
recover(): boolean;
finishRecovery(): void;
lose(): boolean;
+ contextLost(): boolean | null;
restore(): boolean;
};
};
@@ -69,6 +70,10 @@ window.__estellaHeadless = {
report: () => getDeviceLostReport(),
recover: () => recoverDevice(),
finishRecovery: () => finishDeviceRecovery(),
+ contextLost: () => {
+ const gl = EngineHost.canvas?.getContext('webgl2') as WebGL2RenderingContext | null;
+ return gl ? gl.isContextLost() : null;
+ },
lose: () => {
const ext = loseContextExtension();
if (!ext) return false;
diff --git a/sdk/src/platform/web.ts b/sdk/src/platform/web.ts
index 8be6eca8c..c0cee4f53 100644
--- a/sdk/src/platform/web.ts
+++ b/sdk/src/platform/web.ts
@@ -336,7 +336,13 @@ class WebPlatformAdapter implements PlatformAdapter {
*/
onContextLost(callback: () => void): () => void {
if (typeof window === 'undefined') return () => {};
- const onLost = (): void => { callback(); };
+ const onLost = (e: Event): void => {
+ // Without this the browser's default action is to abandon the context
+ // permanently: `webglcontextrestored` never fires, and no amount of
+ // recovery code downstream gets a chance to matter.
+ e.preventDefault();
+ callback();
+ };
window.addEventListener('webglcontextlost', onLost, true);
return () => window.removeEventListener('webglcontextlost', onLost, true);
}
diff --git a/sdk/src/render/renderer.ts b/sdk/src/render/renderer.ts
index 624150be7..dcf4599db 100644
--- a/sdk/src/render/renderer.ts
+++ b/sdk/src/render/renderer.ts
@@ -4,6 +4,7 @@ import type { ESEngineModule, CppRegistry } from '../wasm';
import { CoreApiBridge } from '../wasm/CoreApiBridge';
import { handleWasmError } from '../wasm/wasmError';
import { requireResourceManager } from '../wasm/resourceManager';
+import { platformOnContextLost } from '../platform';
import { decodeFrameCapture, replayToDrawCall as replayToDrawCallImpl, getSnapshotImageData as getSnapshotImpl, type FrameCaptureData } from './frameCapture';
export enum RenderStage {
@@ -202,10 +203,28 @@ export function watchWebGPUDeviceLoss(wasmModule: ESEngineModule): void {
});
}
+let lossGuardInstalled = false;
+
+/**
+ * Subscribe to context loss unconditionally, at renderer init.
+ *
+ * The subscription is what calls preventDefault, and without that the browser
+ * abandons the context for good. That decision belongs to the renderer, not to
+ * whether a diagnostics plugin happens to be installed.
+ */
+function installContextLossGuard(): void {
+ if (lossGuardInstalled) return;
+ lossGuardInstalled = true;
+ platformOnContextLost(() => {
+ reportDeviceLost(DeviceLostReason.ContextLost, 'The host reported the rendering context was lost');
+ });
+}
+
/** @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();
viewProjectionPtr = module._malloc(16 * 4);
backend = wasmBackend(module);
}