Skip to content

Commit a2301dc

Browse files
jrusso1020claude
andcommitted
fix(core): motion blur notices a WebGL canvas the Studio page touched first
When a same-origin host page (Studio) reaches a preview element before the scene does, for example through a MutationObserver record, Chrome gives that element the host's prototype. The scene's getContext("webgl") then ran the host's unpatched getContext, so the canvas never reached the accelerated canvas set, capture motion blur stayed on, and the WebGL canvas froze during preview playback. The tracker now records a canvas in the set of the canvas's own window, and installs itself in a same-origin parent too, compiled with the parent's own Function. Cross-origin parents are skipped. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent d03bdea commit a2301dc

3 files changed

Lines changed: 102 additions & 2 deletions

File tree

‎packages/core/src/pageMotionBlur.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,25 @@ describe("accelerated canvas tracker", () => {
5656
canvas.getContext("webgl");
5757
expect(isAcceleratedCanvas(canvas)).toBe(true);
5858
});
59+
60+
it("tracks a scene canvas that has the host page's prototype", () => {
61+
const frame = document.createElement("iframe");
62+
document.body.append(frame);
63+
try {
64+
const scene = frame.contentWindow as (Window & typeof globalThis) | null;
65+
if (!scene) throw new Error("no iframe window");
66+
const sceneProto = scene.HTMLCanvasElement.prototype as unknown as Proto;
67+
expect(sceneProto === proto).toBe(false);
68+
sceneProto.getContext = vi.fn(() => ({}));
69+
scene.eval(acceleratedCanvasTrackerSource());
70+
const canvas = scene.document.createElement("canvas");
71+
// Chrome gives a canvas the host's prototype when the host page reaches it first.
72+
Object.setPrototypeOf(canvas, HTMLCanvasElement.prototype);
73+
canvas.getContext("webgl2");
74+
const tracked: unknown = Reflect.get(scene, "__hfAcceleratedCanvases");
75+
expect((tracked as WeakSet<object>).has(canvas)).toBe(true);
76+
} finally {
77+
frame.remove();
78+
}
79+
});
5980
});

‎packages/core/src/pageMotionBlur.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,26 @@ export function installAcceleratedCanvasTracker(): void {
3535
Object.defineProperty(tracked, "__hfAccelTracked", { value: true });
3636
Object.defineProperty(proto, name, { value: tracked, writable: true, configurable: true });
3737
};
38+
const ownWindowSet = (canvas: object): WeakSet<object> => {
39+
const view = (canvas as Node).ownerDocument?.defaultView;
40+
const own: unknown = view ? Reflect.get(view, key) : undefined;
41+
return own && typeof (own as WeakSet<object>).add === "function"
42+
? (own as WeakSet<object>)
43+
: seen;
44+
};
3845
wrap("getContext", (canvas, args, ctx) => {
39-
if (ctx && accelerated.includes(String(args[0]))) seen.add(canvas);
46+
if (ctx && accelerated.includes(String(args[0]))) ownWindowSet(canvas).add(canvas);
4047
});
4148
wrap("transferControlToOffscreen", (canvas) => {
42-
seen.add(canvas);
49+
ownWindowSet(canvas).add(canvas);
4350
});
51+
// A same-origin host that reaches a canvas first gives it the host's prototype: track there too.
52+
try {
53+
if (window.parent !== window) {
54+
const HostFunction = Reflect.get(window.parent, "Function") as FunctionConstructor;
55+
new HostFunction(`(${installAcceleratedCanvasTracker.toString()})();`)();
56+
}
57+
} catch {}
4458
}
4559

4660
export function isAcceleratedCanvas(canvas: HTMLCanvasElement): boolean {

‎packages/engine/src/services/captureMotionBlur.pixels.browser.test.ts‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type Win = Window & {
3333
__hf_page_composite_resolve?: () => boolean;
3434
__hfMotionBlur?: {
3535
report: () => { state: string; tier: string | null; reasonCode: string | null; movers: number };
36+
probe: () => { state: string; tier: string | null; reasonCode: string | null; movers: number };
3637
};
3738
};
3839

@@ -301,6 +302,70 @@ describe.skipIf(!hasBrowser)("capture motion blur pixels in Chrome", () => {
301302
expect(ink(trail) / ink(row(sharp.frame, 40))).toBeLessThan(1.1);
302303
});
303304

305+
it("falls back for a WebGL canvas the host page reached first, as Studio does", async (ctx) => {
306+
if (!supported) ctx.skip();
307+
// The preview is a same-origin iframe. A scene mounts its markup, the host's MutationObserver
308+
// walks it (Studio's timeline does), and only then does the scene script call getContext.
309+
const scene = composition(
310+
`${BOX}<script>
311+
window.mountScene = () => document.getElementById("root").insertAdjacentHTML("beforeend", '<div id="scene"><canvas id="gl" width="64" height="36"></canvas></div>');
312+
window.startScene = () => document.getElementById("gl").getContext("webgl");
313+
</script>`,
314+
true,
315+
"capture",
316+
).replace("<head>", `<head><script>${acceleratedCanvasTrackerSource()}</script>`);
317+
const page = await browser.newPage();
318+
try {
319+
await page.setContent(`<iframe id="preview" width="${W}" height="${H}"></iframe>`);
320+
await page.$eval(
321+
"#preview",
322+
(el, html) =>
323+
new Promise((done) => {
324+
const frame = el as HTMLIFrameElement;
325+
frame.onload = done;
326+
frame.srcdoc = html;
327+
}),
328+
scene,
329+
);
330+
// The host observes the preview before the runtime boots, as Studio's player does: its
331+
// callback runs first and so makes the wrappers of what the scene mounts.
332+
await page.evaluate(() => {
333+
const frame = document.getElementById("preview") as HTMLIFrameElement;
334+
const reached: Element[] = [];
335+
Object.assign(window, { reached });
336+
new MutationObserver((records) => {
337+
for (const record of records)
338+
for (const node of Array.from(record.addedNodes))
339+
if (node instanceof Element) reached.push(...Array.from(node.children));
340+
}).observe(frame.contentDocument?.getElementById("root") as Element, { childList: true });
341+
});
342+
const preview = page.frames().find((f) => f !== page.mainFrame());
343+
if (!preview) throw new Error("no preview frame");
344+
await preview.addScriptTag({ content: readFileSync(RUNTIME, "utf8") });
345+
await preview.waitForFunction(
346+
() => (window as Win & { __playerReady?: boolean }).__playerReady === true,
347+
);
348+
const result = await page.evaluate(async () => {
349+
type Scene = Win & { mountScene: () => void; startScene: () => unknown };
350+
const frame = document.getElementById("preview") as HTMLIFrameElement;
351+
const view = frame.contentWindow as Scene;
352+
view.mountScene();
353+
await new Promise((done) => setTimeout(done, 0));
354+
const reached = (window as unknown as { reached: Element[] }).reached;
355+
const hostRealm = reached.some(
356+
(el) => el.id === "gl" && Object.getPrototypeOf(el) === HTMLCanvasElement.prototype,
357+
);
358+
view.startScene();
359+
return { hostRealm, report: view.__hfMotionBlur?.probe() ?? null };
360+
});
361+
// The canvas really carries the host's prototype, so its getContext runs in the host's realm.
362+
expect(result.hostRealm).toBe(true);
363+
expect(result.report).toMatchObject({ tier: "filter", reasonCode: "accelerated-canvas" });
364+
} finally {
365+
await page.close();
366+
}
367+
});
368+
304369
it.each(TIERS)(
305370
"blurs a box centred with translate around where it really is, in the %s tier",
306371
async (tier, ctx) => {

0 commit comments

Comments
 (0)