diff --git a/packages/engine/src/services/__fixtures__/motionBlur/sweepFixtures.ts b/packages/engine/src/services/__fixtures__/motionBlur/sweepFixtures.ts new file mode 100644 index 0000000000..297ae7dddd --- /dev/null +++ b/packages/engine/src/services/__fixtures__/motionBlur/sweepFixtures.ts @@ -0,0 +1,159 @@ +import { readFileSync } from "node:fs"; +import { createRequire } from "node:module"; +import { resolve } from "node:path"; + +// One composition per defect from the 60-composition motion blur sweep. Each is 640x360, 30 fps, 1 s, +// and its mover #m travels x 0 to 480 between 0.25 s and 0.75 s (32 px a frame). No text anywhere, so +// no pin depends on how a host rasterises glyphs. + +export type Tier = "capture" | "filter"; + +interface SweepFixture { + name: string; + width: 640; + height: 360; + dpr: 1; + durationSeconds: 1; + fps: 30; + html(blur: "on" | "off", tier: Tier): string; +} + +const repoRoot = resolve(import.meta.dirname, "../../../../../.."); +const GSAP = readFileSync( + createRequire(resolve(repoRoot, "packages/core/package.json")).resolve("gsap/dist/gsap.min.js"), + "utf8", +); + +const BASE_CSS = ` + * { margin: 0; padding: 0; box-sizing: border-box } + html, body { width: 640px; height: 360px; overflow: hidden; background: #000 } + #root { position: relative; width: 640px; height: 360px; overflow: hidden; + background: linear-gradient(135deg, #1d3557 0%, #457b9d 50%, #2a9d8f 100%) } + .deco { position: absolute; width: 40px; height: 40px; border-radius: 50%; background: #f1faee } + .m { position: absolute; left: 40px; top: 140px; width: 120px; height: 80px; background: #e76f51; border: 4px solid #fff }`; + +const DECO = Array.from( + { length: 9 }, + (_, i) => `
`, +).join(""); + +const MOVER = `
`; +const SLIDE = `tl.fromTo("#m", { x: 0 }, { x: 480, duration: 0.5, ease: "none" }, 0.25);`; +const STRIPES = `
`; + +interface Spec { + name: string; + css?: string; + body?: string; + script?: string; +} + +const rootAttrs = (blur: "on" | "off", tier: Tier): string => + blur === "off" + ? 'data-motion-blur="off"' + : `data-motion-blur="on" data-motion-blur-tier="${tier}"`; + +function sweepFixture(spec: Spec): SweepFixture { + return { + name: spec.name, + width: 640, + height: 360, + dpr: 1, + durationSeconds: 1, + fps: 30, + html: (blur, tier) => ` +
${DECO}${spec.body ?? MOVER}
+ + `, + }; +} + +const card = (style: string): string => + `
`; + +const SWEEP_FIXTURES = [ + // Control: a plain mover. Both tiers draw it the way the oracle does. + sweepFixture({ name: "plain" }), + // A: a static panel painted over the mover. + sweepFixture({ + name: "behind-panel", + css: ".m { z-index: 1 }", + body: `${MOVER}
`, + }), + // B: the mover blends with what is under it. + sweepFixture({ + name: "blend-difference", + css: ".m { background: #fff; mix-blend-mode: difference }", + body: STRIPES + MOVER, + }), + // C: an ancestor casts a drop shadow from the mover. + sweepFixture({ + name: "ancestor-drop-shadow", + body: `
${MOVER}
`, + }), + // D: generated content drawn outside the mover's box. + sweepFixture({ + name: "pseudo-after", + css: ".m::after { content: ''; position: absolute; left: 130px; top: 10px; width: 60px; height: 60px; background: #ffd166; border-radius: 50% }", + }), + // E: the mover's own clip-path. + sweepFixture({ + name: "own-clip-polygon", + css: ".m { clip-path: polygon(50% 0, 100% 100%, 0 100%) }", + }), + // F: a position:fixed child of a mover that moves through left. + sweepFixture({ + name: "fixed-child-left", + body: `
`, + script: `tl.fromTo("#m", { left: 40 }, { left: 520, duration: 0.5, ease: "none" }, 0.25);`, + }), + // H: an author selector that goes through the root's parent. + sweepFixture({ name: "body-child-selector", css: "body > #root > .m { background: #06d6a0 }" }), + // I: an author selector on the root's last child. + sweepFixture({ + name: "last-child-selector", + css: "#root > :last-child { outline: 6px solid #ffd166 }", + body: `${MOVER}
`, + }), + // J: an overflow:hidden card the mover slides into. + sweepFixture({ name: "overflow-card", body: card("overflow:hidden;border-radius:24px") }), + // K: two movers that cross. + sweepFixture({ + name: "overlapping-movers", + css: ".m2 { position: absolute; left: 480px; top: 150px; width: 120px; height: 80px; background: #2a9d8f; border: 4px solid #000 }", + body: `${MOVER}
`, + script: `${SLIDE} tl.fromTo("#m2", { x: 0 }, { x: -440, duration: 0.5, ease: "none" }, 0.25);`, + }), + // M: movers that rise in from below the frame edge. + sweepFixture({ + name: "rise-from-below", + css: ".r { position: absolute; top: 300px; width: 40px; height: 40px; background: #ffd166; border: 3px solid #000 }", + body: Array.from( + { length: 9 }, + (_, i) => `
`, + ).join(""), + script: `tl.from(".r", { y: 120, duration: 0.3, ease: "none", stagger: 0.012 }, 0.2);`, + }), + // O: the mover is hidden half way through a frame's shutter. + sweepFixture({ + name: "visibility-toggle", + script: `${SLIDE} tl.set("#m", { visibility: "hidden" }, 0.5);`, + }), +] as const satisfies readonly SweepFixture[]; + +export type SweepFixtureName = (typeof SWEEP_FIXTURES)[number]["name"]; + +export function sweepFixtureNamed(name: SweepFixtureName): SweepFixture { + const f = SWEEP_FIXTURES.find((x) => x.name === name); + if (!f) throw new Error(`missing sweep fixture ${name}`); + return f; +} diff --git a/packages/engine/src/services/__fixtures__/motionBlur/thresholds.ts b/packages/engine/src/services/__fixtures__/motionBlur/thresholds.ts index a64e573c6b..774308bfb4 100644 --- a/packages/engine/src/services/__fixtures__/motionBlur/thresholds.ts +++ b/packages/engine/src/services/__fixtures__/motionBlur/thresholds.ts @@ -42,3 +42,30 @@ export const INVARIANTS = { minCheckedFraction: 0.5, shutterSamples: 8, }; + +// Oracle: the engine's 32-sample sub-frame blur. Noise: worst tile MAE on the sweep fixtures' clean frames. +export const MEASURED_ORACLE_NOISE = 3.94; +export const MEASURED_ORACLE_SMALLEST_SYMPTOM = 14.47; + +export const ORACLE = { + samples: 32, + maxMae: geometricMean(MEASURED_ORACLE_NOISE, MEASURED_ORACLE_SMALLEST_SYMPTOM), +}; + +export const SWEEP_SYMPTOMS = { + A: 47.67, + B: 148.63, + C: 73.02, + D_CAPTURE: 107.66, + D_FILTER: 107.66, + E: 27.6, + F_CAPTURE: 40.62, + F_FILTER: 126.54, + H: 135.67, + I: 53.63, + J: 25.79, + K: MEASURED_ORACLE_SMALLEST_SYMPTOM, + M: 18.31, + O_CAPTURE: 50.38, + O_FILTER: 50.38, +} as const; diff --git a/packages/engine/src/services/captureMotionBlur.sweep.browser.test.ts b/packages/engine/src/services/captureMotionBlur.sweep.browser.test.ts new file mode 100644 index 0000000000..8ffe1cede1 --- /dev/null +++ b/packages/engine/src/services/captureMotionBlur.sweep.browser.test.ts @@ -0,0 +1,390 @@ +import type { Browser, Page } from "puppeteer-core"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { + compareTiles, + meanFrame, + tilesOutsideRegion, + trailMask, + withinSymptomBand, + type Differential, + type Rect, + type Rgba, +} from "./__fixtures__/blurDifferential.js"; +import { closeTestChrome } from "./__fixtures__/loopback.js"; +import { + captureFrame, + hasBrowser, + launch, + openFixture, + type BlurWindow, +} from "./__fixtures__/motionBlur/blurPage.js"; +import { + sweepFixtureNamed, + type SweepFixtureName, + type Tier, +} from "./__fixtures__/motionBlur/sweepFixtures.js"; +import { INVARIANTS, ORACLE, SWEEP_SYMPTOMS } from "./__fixtures__/motionBlur/thresholds.js"; +import { motionBlurSampleTimes, resolveMotionBlurPlan } from "./motionBlur.js"; + +const FPS = 30; +const frameTime = (n: number): number => n / FPS; +const SHUTTER_DIVISIONS = 24; +const SHUTTER_SECONDS = 240 / 360 / FPS; +const RADIUS_PX = 2; +const CASE_TIMEOUT_MS = 60_000; + +const ORACLE_PLAN = resolveMotionBlurPlan({ + samplesPerFrame: ORACLE.samples, + shutterAngle: 240, + shutterPhase: -120, + blend: "srgb", +}); + +/** + * outside: blur on equals blur off away from every trail (invariant 1). + * inside: blur on is close to the mean of 8 blur-off shutter samples inside the trail (invariant 2). + * oracle: blur on is close, over the whole frame, to the engine's own 32-sample sub-frame blur. It is + * stricter than the shutter mean and sees defects too small or too spread out for the two invariants. + */ +type Check = "outside" | "inside" | "oracle"; + +interface KnownBug { + finding: string; + reason: string; + /** Every bad tile must touch one of these rects, in CSS px. */ + regions: readonly Rect[]; + measuredMae: number; +} + +interface SweepCase { + fixture: SweepFixtureName; + tier: Tier; + frame: number; + check: Check; + bug?: KnownBug; +} + +const rect = (x: number, y: number, width: number, height: number): Rect => ({ + x, + y, + width, + height, +}); + +const S = SWEEP_SYMPTOMS; +const mover = (left: number): Rect => rect(left, 140, 120, 80); +// A trail: the mover's box widened by the 21 px it travels in one 240 degree shutter. +const trail = (left: number): Rect => rect(left - 21, 140, 162, 80); + +const CASES: readonly SweepCase[] = [ + { fixture: "plain", tier: "capture", frame: 14, check: "oracle" }, + { fixture: "plain", tier: "filter", frame: 14, check: "oracle" }, + { + fixture: "behind-panel", + tier: "capture", + frame: 14, + check: "outside", + bug: { + finding: "A", + reason: "a static panel in front of the mover is painted over by its trail", + regions: [rect(260, 100, 120, 160)], + measuredMae: S.A, + }, + }, + { + fixture: "blend-difference", + tier: "capture", + frame: 11, + check: "inside", + bug: { + finding: "B", + reason: "a mix-blend-mode mover is captured alone, so it blends with nothing", + regions: [trail(152)], + measuredMae: S.B, + }, + }, + { + fixture: "ancestor-drop-shadow", + tier: "capture", + frame: 14, + check: "inside", + bug: { + finding: "C", + reason: "the drop shadow an ancestor casts from the mover is missing", + regions: [rect(227, 140, 176, 100)], + measuredMae: S.C, + }, + }, + ...(["capture", "filter"] as const).map( + (tier): SweepCase => ({ + fixture: "pseudo-after", + tier, + frame: 14, + check: "inside", + bug: { + finding: "D", + reason: "an ::after drawn outside the mover's box is cut off", + regions: [rect(376, 150, 72, 68)], + measuredMae: tier === "capture" ? S.D_CAPTURE : S.D_FILTER, + }, + }), + ), + { + fixture: "own-clip-polygon", + tier: "filter", + frame: 14, + check: "oracle", + bug: { + finding: "E", + reason: "the mover's own clip-path cuts its smear to a hard edge", + regions: [trail(248)], + measuredMae: S.E, + }, + }, + { + fixture: "fixed-child-left", + tier: "capture", + frame: 11, + check: "outside", + bug: { + finding: "F", + reason: "a fixed child of a mover that moves through left is smeared along the path", + regions: [rect(10, 10, 60, 30)], + measuredMae: S.F_CAPTURE, + }, + }, + { + fixture: "fixed-child-left", + tier: "filter", + frame: 11, + check: "outside", + bug: { + finding: "F", + reason: + "the blur filter makes the mover a containing block, so its fixed child jumps into it", + regions: [rect(10, 10, 60, 30), trail(152)], + measuredMae: S.F_FILTER, + }, + }, + { + fixture: "body-child-selector", + tier: "capture", + frame: 4, + check: "outside", + bug: { + finding: "H", + reason: + "a selector through the root's parent stops matching once the root is wrapped, still frames included", + regions: [mover(40)], + measuredMae: S.H, + }, + }, + { + fixture: "last-child-selector", + tier: "filter", + frame: 11, + check: "outside", + bug: { + finding: "I", + reason: "the filter tier's svg becomes the root's last child, so :last-child moves to it", + regions: [rect(0, 0, 0, 0), rect(494, 14, 112, 62)], + measuredMae: S.I, + }, + }, + { + fixture: "overflow-card", + tier: "capture", + frame: 11, + check: "oracle", + bug: { + finding: "J", + reason: "the clip edge of an overflow:hidden card travels with the mover sliding into it", + regions: [rect(200, 140, 0, 80)], + measuredMae: S.J, + }, + }, + { + fixture: "overlapping-movers", + tier: "capture", + frame: 15, + check: "oracle", + bug: { + finding: "K", + reason: "two crossing movers each carry a ghost of the other in their trail", + regions: [rect(260, 150, 140, 70)], + measuredMae: S.K, + }, + }, + { + fixture: "rise-from-below", + tier: "capture", + frame: 14, + check: "oracle", + bug: { + finding: "M", + reason: "content rising in from below the frame leaves a band at the bottom edge", + regions: [rect(0, 350, 640, 10)], + measuredMae: S.M, + }, + }, + ...(["capture", "filter"] as const).map( + (tier): SweepCase => ({ + fixture: "visibility-toggle", + tier, + frame: 15, + check: "inside", + bug: { + finding: "O", + reason: "a mover hidden half way through the shutter is drawn at full strength, not half", + regions: [trail(280)], + measuredMae: tier === "capture" ? S.O_CAPTURE : S.O_FILTER, + }, + }), + ), +]; + +function neighbourTimes(n: number): number[] { + const r = INVARIANTS.radiusFrames; + return Array.from({ length: 2 * r + 1 }, (_, k) => frameTime(n - r + k)); +} + +function shutterTimes(n: number): number[] { + const t = frameTime(n); + const samples = INVARIANTS.shutterSamples; + return Array.from( + { length: samples }, + (_, k) => t - SHUTTER_SECONDS / 2 + ((k + 0.5) * SHUTTER_SECONDS) / samples, + ); +} + +async function captureInOrder( + page: Page, + times: readonly number[], + options?: { subFrameDivisions?: number }, +): Promise { + const frames: Rgba[] = []; + for (const t of times) frames.push(await captureFrame(page, t, options)); + return frames; +} + +async function blurOffReference( + page: Page, + c: SweepCase, +): Promise<{ reference: Rgba; mask: Uint8Array }> { + if (c.check === "outside") { + const frames = await captureInOrder(page, neighbourTimes(c.frame)); + const reference = frames[INVARIANTS.radiusFrames]; + if (!reference) throw new Error("missing reference frame"); + return { reference, mask: trailMask(frames, RADIUS_PX) }; + } + if (c.check === "inside") { + const samples = await captureInOrder(page, shutterTimes(c.frame), { + subFrameDivisions: SHUTTER_DIVISIONS, + }); + return { reference: meanFrame(samples), mask: trailMask(samples, RADIUS_PX) }; + } + if (!ORACLE_PLAN) throw new Error("no oracle plan"); + const samples = await captureInOrder( + page, + motionBlurSampleTimes(ORACLE_PLAN, c.frame, FPS, ORACLE.samples), + { subFrameDivisions: ORACLE_PLAN.subFrameDivisions }, + ); + const reference = meanFrame(samples); + return { reference, mask: new Uint8Array(reference.width * reference.height).fill(1) }; +} + +const MAX_MAE: Record = { + outside: INVARIANTS.outsideMaxMae, + inside: INVARIANTS.insideMaxMae, + oracle: ORACLE.maxMae, +}; + +async function measure( + browser: Browser, + c: SweepCase, +): Promise<{ d: Differential; tier: string | null | undefined }> { + const f = sweepFixtureNamed(c.fixture); + const size = { width: f.width, height: f.height }; + const off = await openFixture(browser, f.html("off", c.tier), f.dpr, size); + let reference: { reference: Rgba; mask: Uint8Array }; + try { + reference = await blurOffReference(off, c); + } finally { + await off.close(); + } + const on = await openFixture(browser, f.html("on", c.tier), f.dpr, size); + try { + const onFrame = await captureFrame(on, frameTime(c.frame)); + const report = await on.evaluate(() => (window as BlurWindow).__hfMotionBlur?.probe()); + const d = compareTiles( + reference.reference, + onFrame, + reference.mask, + c.check === "outside" ? "outside" : "inside", + { + block: INVARIANTS.block, + maxMae: MAX_MAE[c.check], + }, + ); + return { d, tier: report?.tier }; + } finally { + await on.close(); + } +} + +const tilesOutsideRegions = (d: Differential, regions: readonly Rect[]): Differential["bad"] => + regions.reduce((bad, region) => tilesOutsideRegion(bad, INVARIANTS.block, region, 1), d.bad); + +const firstFive = (d: Differential): string => JSON.stringify(d.bad.slice(0, 5)); + +function describeCase(c: SweepCase): string { + const where = `${c.check}, ${c.tier} tier, ${c.fixture} frame ${c.frame}`; + return c.bug + ? `KNOWN BUG on main: ${c.bug.finding}: ${c.bug.reason} (${where})` + : `blur on matches the reference (${where})`; +} + +let launched: Browser | undefined; +let supported = false; + +function chrome(): Browser { + if (!launched) throw new Error("Chrome did not launch"); + return launched; +} + +describe.skipIf(!hasBrowser)("capture motion blur sweep defects in Chrome", () => { + beforeAll(async () => { + ({ browser: launched, supported } = await launch()); + }, 30_000); + afterAll(() => closeTestChrome(launched)); + + for (const c of CASES) { + it( + describeCase(c), + async (ctx) => { + if (!supported) ctx.skip(); + const { d, tier } = await measure(chrome(), c); + expect(tier, `${c.fixture} did not blur on the ${c.tier} tier`).toBe(c.tier); + expect(d.checkedBlocks).toBeGreaterThan(0); + const { bug } = c; + if (!bug) { + expect(d.bad, `${c.fixture} ${firstFive(d)}`).toEqual([]); + return; + } + expect( + d.bad.length, + `${bug.finding} looks fixed: delete this case's \`bug\`, which keeps it as a check that blur on matches the reference`, + ).toBeGreaterThan(0); + expect( + tilesOutsideRegions(d, bug.regions), + `a new defect outside ${JSON.stringify(bug.regions)}: ${firstFive(d)}`, + ).toEqual([]); + expect( + withinSymptomBand(d.worstMae, bug.measuredMae), + `the pinned symptom changed: re-measure (worst MAE ${d.worstMae.toFixed(2)}, pinned at ${bug.measuredMae})`, + ).toBe(true); + }, + CASE_TIMEOUT_MS, + ); + } +});