Skip to content

Commit e9181d0

Browse files
fix(cli): check skips WebGL draws on its box-only seeks so Three.js scenes finish (#5825)
1 parent cadd7af commit e9181d0

3 files changed

Lines changed: 159 additions & 23 deletions

File tree

‎packages/cli/src/capture/captureCompositionFrame.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ describe("seekCompositionTimeline", () => {
107107

108108
expect(waitForFunction).not.toHaveBeenCalled();
109109
expect(evaluate).toHaveBeenCalledTimes(4);
110-
expect(evaluate).toHaveBeenNthCalledWith(1, expect.any(Function), 1.25, false, false);
110+
expect(evaluate).toHaveBeenNthCalledWith(1, expect.any(Function), 1.25, false, false, false);
111111
expect(evaluate).toHaveBeenNthCalledWith(2, expect.any(Function), { videoFrames: true });
112112
expect(evaluate.mock.calls[2]?.[0]).toContain("window.setTimeout(finish, 100)");
113113
// Post-seek font settle: a seek can reveal glyphs whose unicode-range
@@ -212,7 +212,7 @@ describe("seekCompositionTimeline", () => {
212212

213213
expect(waitForFunction).toHaveBeenCalledWith(expect.any(Function), { timeout: 500 });
214214
expect(evaluate).toHaveBeenCalledTimes(2);
215-
expect(evaluate).toHaveBeenCalledWith(expect.any(Function), 3, true, false);
215+
expect(evaluate).toHaveBeenCalledWith(expect.any(Function), 3, true, false, false);
216216
});
217217

218218
it("supports layout's ordered double-frame, bounded font, and sleep settles", async () => {
@@ -229,7 +229,7 @@ describe("seekCompositionTimeline", () => {
229229
await pending;
230230

231231
expect(evaluate).toHaveBeenCalledTimes(4);
232-
expect(evaluate).toHaveBeenNthCalledWith(1, expect.any(Function), 4, true, false);
232+
expect(evaluate).toHaveBeenNthCalledWith(1, expect.any(Function), 4, true, false, false);
233233
expect(evaluate).toHaveBeenNthCalledWith(2, expect.any(Function), { videoFrames: true });
234234
expect(evaluate).toHaveBeenNthCalledWith(3, expect.any(Function));
235235
expect(evaluate).toHaveBeenNthCalledWith(4, expect.any(Function), 500);

‎packages/cli/src/capture/captureCompositionFrame.ts‎

Lines changed: 81 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export const DENSE_GEOMETRY_SEEK_OPTIONS = {
4242
waitForFontsMs: 0,
4343
settleMs: 0,
4444
waitForVideoFrames: false,
45+
skipWebglDraws: true,
4546
} as const;
4647

4748
export interface SeekCompositionTimelineOptions {
@@ -52,6 +53,8 @@ export interface SeekCompositionTimelineOptions {
5253
waitForFontsMs?: number;
5354
settleMs?: number;
5455
waitForVideoFrames?: boolean;
56+
// Box-only reads: software GL spends about a second per frame on draws nothing here looks at.
57+
skipWebglDraws?: boolean;
5558
}
5659

5760
type SeekBarrierOptions = { videoFrames: boolean };
@@ -61,14 +64,20 @@ type CompositionPageFunction =
6164
| (() => unknown)
6265
| ((value: number) => unknown)
6366
| ((barrier: SeekBarrierOptions) => unknown)
64-
| ((value: number, fallbackToBridgeAndTimelines: boolean, exactTime: boolean) => unknown);
67+
| ((
68+
value: number,
69+
fallbackToBridgeAndTimelines: boolean,
70+
exactTime: boolean,
71+
skipWebglDraws: boolean,
72+
) => unknown);
6573

6674
export interface CompositionEvaluationPage {
6775
evaluate(
6876
pageFunction: CompositionPageFunction,
6977
value?: number | SeekBarrierOptions,
7078
fallbackToBridgeAndTimelines?: boolean,
7179
exactTime?: boolean,
80+
skipWebglDraws?: boolean,
7281
): Promise<unknown>;
7382
}
7483

@@ -297,7 +306,7 @@ export async function seekCompositionTimeline(
297306
await page.evaluate(
298307
// Serialized into the page; the seek-target cascade must stay one function.
299308
// fallow-ignore-next-line complexity
300-
(t: number, fallbackToBridgeAndTimelines: boolean, exactTime: boolean) => {
309+
(t: number, fallbackToBridgeAndTimelines: boolean, exactTime: boolean, skipDraws: boolean) => {
301310
const getProperty = (target: unknown, key: string): unknown => {
302311
if ((typeof target !== "object" || target === null) && typeof target !== "function") {
303312
return undefined;
@@ -318,33 +327,85 @@ export async function seekCompositionTimeline(
318327
const playerSeek = getProperty(player, "seek");
319328
const hf = Reflect.get(window, "__hf");
320329
const bridgeSeek = getProperty(hf, "seek");
330+
const gsap = Reflect.get(window, "gsap");
331+
const ticker = getProperty(gsap, "ticker");
332+
333+
// fallow-ignore-next-line complexity
334+
const seekTo = (time: number): void => {
335+
// Prefer renderSeek because it also runs the runtime's data-start/data-duration
336+
// visibility sync; raw timeline seeks leave off-window clips visible to audits.
337+
if (call(renderSeek, player, exactTime ? [time, { exact: true }] : [time])) {
338+
// Preferred runtime target handled the seek.
339+
} else if (fallbackToBridgeAndTimelines && call(bridgeSeek, hf, [time])) {
340+
// Producer bridge handled the seek.
341+
} else if (call(playerSeek, player, [time])) {
342+
// Legacy player target handled the seek.
343+
} else if (fallbackToBridgeAndTimelines) {
344+
const timelines = Reflect.get(window, "__timelines");
345+
if (typeof timelines === "object" && timelines !== null) {
346+
for (const key of Object.keys(timelines)) {
347+
const timeline = Reflect.get(timelines, key);
348+
call(getProperty(timeline, "pause"), timeline, []);
349+
call(getProperty(timeline, "seek"), timeline, [time]);
350+
}
351+
}
352+
}
353+
call(getProperty(ticker, "tick"), ticker, []);
354+
};
321355

322-
// Prefer renderSeek because it also runs the runtime's data-start/data-duration
323-
// visibility sync; raw timeline seeks leave off-window clips visible to audits.
324-
if (call(renderSeek, player, exactTime ? [safe, { exact: true }] : [safe])) {
325-
// Preferred runtime target handled the seek.
326-
} else if (fallbackToBridgeAndTimelines && call(bridgeSeek, hf, [safe])) {
327-
// Producer bridge handled the seek.
328-
} else if (call(playerSeek, player, [safe])) {
329-
// Legacy player target handled the seek.
330-
} else if (fallbackToBridgeAndTimelines) {
331-
const timelines = Reflect.get(window, "__timelines");
332-
if (typeof timelines === "object" && timelines !== null) {
333-
for (const key of Object.keys(timelines)) {
334-
const timeline = Reflect.get(timelines, key);
335-
call(getProperty(timeline, "pause"), timeline, []);
336-
call(getProperty(timeline, "seek"), timeline, [safe]);
356+
const gateSlot = Symbol.for("hyperframes.webglDrawGate");
357+
let gate = Reflect.get(window, gateSlot) as { skip: boolean; skipped: boolean } | undefined;
358+
if (skipDraws && !gate) {
359+
const installed = { skip: false, skipped: false };
360+
for (const name of ["WebGLRenderingContext", "WebGL2RenderingContext"]) {
361+
const proto = getProperty(Reflect.get(window, name), "prototype");
362+
if (!proto) continue;
363+
for (const method of [
364+
"clear",
365+
"drawArrays",
366+
"drawElements",
367+
"drawArraysInstanced",
368+
"drawElementsInstanced",
369+
"drawRangeElements",
370+
"blitFramebuffer",
371+
]) {
372+
const draw = getProperty(proto, method);
373+
if (typeof draw !== "function") continue;
374+
Reflect.set(proto, method, function (this: unknown, ...args: unknown[]) {
375+
if (!installed.skip) return Reflect.apply(draw, this, args);
376+
installed.skipped = true;
377+
return undefined;
378+
});
337379
}
338380
}
381+
Reflect.set(window, gateSlot, installed);
382+
gate = installed;
339383
}
384+
const seekWithoutDraws = (time: number): void => {
385+
if (!gate) return seekTo(time);
386+
gate.skip = true;
387+
try {
388+
seekTo(time);
389+
} finally {
390+
gate.skip = false;
391+
}
392+
};
340393

341-
const gsap = Reflect.get(window, "gsap");
342-
const ticker = getProperty(gsap, "ticker");
343-
call(getProperty(ticker, "tick"), ticker, []);
394+
if (skipDraws) {
395+
seekWithoutDraws(safe);
396+
return;
397+
}
398+
// A skipped draw left the canvas stale and a same-time seek redraws nothing: step off first.
399+
if (gate?.skipped) {
400+
seekWithoutDraws(safe >= 0.001 ? safe - 0.001 : safe + 0.001);
401+
gate.skipped = false;
402+
}
403+
seekTo(safe);
344404
},
345405
timeSeconds,
346406
options.fallbackToBridgeAndTimelines === true,
347407
options.exactTime === true,
408+
options.skipWebglDraws === true,
348409
);
349410

350411
await page.evaluate(
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import puppeteer, { type Browser, type Page } from "puppeteer-core";
2+
import { afterAll, beforeAll, expect, it } from "vitest";
3+
import { ensureBrowser } from "../browser/manager.js";
4+
import { DENSE_GEOMETRY_SEEK_OPTIONS, seekCompositionTimeline } from "./captureCompositionFrame.js";
5+
6+
// Red channel = time * 25. Like GSAP, seeking to the current time renders nothing.
7+
const PAGE = `<canvas id="c" width="4" height="4"></canvas>
8+
<script>
9+
const gl = document.getElementById("c").getContext("webgl", { preserveDrawingBuffer: true });
10+
const paint = (t) => {
11+
gl.clearColor((t * 25) / 255, 0, 0, 1);
12+
gl.clear(gl.COLOR_BUFFER_BIT);
13+
};
14+
let now = 0;
15+
window.seeks = [];
16+
window.__player = {
17+
renderSeek: (t) => {
18+
window.seeks.push(t);
19+
if (t === now) return;
20+
now = t;
21+
paint(t);
22+
},
23+
};
24+
window.paint = paint;
25+
window.red = () => {
26+
const px = new Uint8Array(4);
27+
gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, px);
28+
return px[0];
29+
};
30+
paint(0);
31+
</script>`;
32+
33+
const PIXEL_SEEK = { animationFrameSettle: "none", waitForFontsMs: 0 } as const;
34+
35+
let browser: Browser;
36+
let page: Page;
37+
38+
beforeAll(async () => {
39+
const { executablePath } = await ensureBrowser();
40+
browser = await puppeteer.launch({
41+
executablePath,
42+
headless: true,
43+
args: [
44+
"--no-sandbox",
45+
"--use-gl=angle",
46+
"--use-angle=swiftshader",
47+
"--enable-unsafe-swiftshader",
48+
],
49+
});
50+
page = await browser.newPage();
51+
await page.setContent(PAGE);
52+
}, 60_000);
53+
54+
afterAll(async () => {
55+
await browser?.close();
56+
});
57+
58+
const red = () => page.evaluate(() => Reflect.apply(Reflect.get(window, "red"), window, []));
59+
const seeks = () => page.evaluate(() => [...Reflect.get(window, "seeks")]);
60+
61+
it("skips WebGL draws on geometry seeks and repaints on the next pixel seek to the same time", async () => {
62+
await seekCompositionTimeline(page, 4, DENSE_GEOMETRY_SEEK_OPTIONS);
63+
expect(await red()).toBe(0);
64+
65+
await seekCompositionTimeline(page, 4, PIXEL_SEEK);
66+
expect(await red()).toBe(100);
67+
expect(await seeks()).toEqual([4, 3.999, 4]);
68+
69+
await seekCompositionTimeline(page, 6, PIXEL_SEEK);
70+
expect(await red()).toBe(150);
71+
expect(await seeks()).toEqual([4, 3.999, 4, 6]);
72+
73+
await page.evaluate(() => Reflect.apply(Reflect.get(window, "paint"), window, [2]));
74+
expect(await red()).toBe(50);
75+
});

0 commit comments

Comments
 (0)