Files
hyperframes/packages/engine/src/services/webglRelease.ts
T
Miguel Ángel 26d6e3eef1 fix(engine): a scene that has ended gives up its WebGL context so the next scene is not slowed (#6984)
* fix(engine): a scene that has ended gives up its WebGL context so the next scene is not slowed

* build(engine): raise the engine package size budget for the WebGL release module

* chore(engine): shorten the WebGL release comment

* fix(engine): a scene gives up its WebGL context only when the runtime says its window ended

* chore(engine): drop a comment the type already says

* chore(engine): shorten a comment line
2026-11-02 07:50:16 +00:00

51 lines
1.9 KiB
TypeScript

import type { Page } from "puppeteer-core";
type RecordedContext = { canvas: HTMLCanvasElement; ctx: WebGLRenderingContext; lost?: boolean };
type GlWindow = Window & { __hf_gl_contexts?: RecordedContext[] };
/** Records each WebGL context a page creates. Register with page.evaluateOnNewDocument before navigation. */
export function recordWebglContexts(): void {
const w = window as GlWindow;
w.__hf_gl_contexts = [];
const canvasProto = HTMLCanvasElement.prototype as unknown as {
getContext: (
this: HTMLCanvasElement,
type: string,
attrs?: unknown,
) => WebGLRenderingContext | null;
};
const orig = canvasProto.getContext;
canvasProto.getContext = function (type, attrs) {
const ctx = orig.call(this, type, attrs);
if (ctx && (type === "webgl" || type === "webgl2" || type === "experimental-webgl")) {
const list = w.__hf_gl_contexts ?? [];
if (!list.some((r) => r.ctx === ctx)) list.push({ canvas: this, ctx });
w.__hf_gl_contexts = list;
}
return ctx;
};
}
/** Releases the WebGL context of each canvas in a hidden clip whose runtime-resolved window ended by `time`. */
export async function releaseEndedWebglContexts(page: Page, time: number): Promise<number> {
return page.evaluate((t: number) => {
const clipWindow = window.__hf?.clipWindow;
if (!clipWindow) return 0;
const hasEnded = (el: Element): boolean => {
for (let up: Element | null = el; up; up = up.parentElement) {
const span = up.hasAttribute("data-hf-clip-hidden") ? clipWindow(up) : null;
if (span && Number.isFinite(span.end) && span.end <= t) return true;
}
return false;
};
const live = ((window as GlWindow).__hf_gl_contexts ?? []).filter(
(r) => !r.lost && hasEnded(r.canvas),
);
for (const rec of live) {
rec.lost = true;
rec.ctx.getExtension("WEBGL_lose_context")?.loseContext();
}
return live.length;
}, time);
}