|
| 1 | +// Page motion blur through a real multi-worker render (#6967): outside the trail of everything |
| 2 | +// that moved, a blur-on render must equal the blur-off render frame by frame. A PNG sequence is |
| 3 | +// lossless and does not encode while capturing, so each worker captures one contiguous |
| 4 | +// block of frames and every block but the first starts mid-film. Needs Chrome + ffmpeg: integration lane. |
| 5 | +// The suites share this module and split the fixtures between two files to stay inside the flake gate's run limit. |
| 6 | + |
| 7 | +import { spawnSync } from "node:child_process"; |
| 8 | +import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; |
| 9 | +import { tmpdir } from "node:os"; |
| 10 | +import { join } from "node:path"; |
| 11 | +import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 12 | +import { decodePng, distributeFrames, getFfmpegBinary, resolveConfig } from "@hyperframes/engine"; |
| 13 | +import { |
| 14 | + compareTiles, |
| 15 | + trailMask, |
| 16 | + type Differential, |
| 17 | + type Rgba, |
| 18 | +} from "../../../../../engine/src/services/__fixtures__/blurDifferential.js"; |
| 19 | +import { |
| 20 | + FIXTURES, |
| 21 | + type BlurFixture, |
| 22 | +} from "../../../../../engine/src/services/__fixtures__/motionBlur/fixtures.js"; |
| 23 | +import { |
| 24 | + INVARIANTS, |
| 25 | + RENDER_OUTSIDE_MAX_MAE, |
| 26 | +} from "../../../../../engine/src/services/__fixtures__/motionBlur/thresholds.js"; |
| 27 | +import { createRenderJob, executeRenderJob } from "../../renderOrchestrator.js"; |
| 28 | + |
| 29 | +const FFMPEG = getFfmpegBinary(); |
| 30 | +const HAS_FFMPEG = spawnSync(FFMPEG, ["-version"], { encoding: "utf-8" }).status === 0; |
| 31 | +const ON_CI = Boolean(process.env.CI); |
| 32 | + |
| 33 | +const FPS = 30; |
| 34 | +const WORKERS = 4; |
| 35 | +/** |
| 36 | + * `callbacks` calls at frames 15, 16 and 30. A worker whose block starts strictly between the |
| 37 | + * calls shows any call state its scan leaves behind; one starting on a call only by timing (#6967). |
| 38 | + * 7 workers over 60 frames start at 0, 9, 18, 27, 36, 45 and 54. |
| 39 | + */ |
| 40 | +const WORKERS_BY_FIXTURE = new Map<string, number>([ |
| 41 | + ["callbacks", 7], |
| 42 | + ["once-guard-label", 7], |
| 43 | + ["stateful-callback", 7], |
| 44 | +]); |
| 45 | +/** Block starts the partition must have, at least `count` of them in [from, to]. */ |
| 46 | +const REQUIRED_STARTS = new Map<string, { from: number; to: number; count: number }>([ |
| 47 | + ["callbacks", { from: 18, to: 28, count: 2 }], |
| 48 | + ["scroll-list", { from: 28, to: Infinity, count: 2 }], |
| 49 | +]); |
| 50 | +const MIN_SECONDS = 1.2; |
| 51 | +const RENDER_TIMEOUT_MS = 600_000; |
| 52 | + |
| 53 | +interface Renders { |
| 54 | + off: Rgba[]; |
| 55 | + off2: Rgba[]; |
| 56 | + on: Rgba[]; |
| 57 | +} |
| 58 | + |
| 59 | +interface FrameResult { |
| 60 | + frame: number; |
| 61 | + outside: Differential; |
| 62 | + noise: Differential; |
| 63 | + insideMae: number; |
| 64 | +} |
| 65 | + |
| 66 | +let root = ""; |
| 67 | + |
| 68 | +const fixtureNamed = (name: string): BlurFixture => { |
| 69 | + const f = FIXTURES.find((x) => x.name === name); |
| 70 | + if (!f) throw new Error(`missing fixture ${name}`); |
| 71 | + return f; |
| 72 | +}; |
| 73 | + |
| 74 | +async function renderFixture(f: BlurFixture, blur: "on" | "off", tag: string): Promise<Rgba[]> { |
| 75 | + const projectDir = join(root, `${f.name}-${tag}`); |
| 76 | + const outputDir = join(projectDir, "frames"); |
| 77 | + mkdirSync(projectDir, { recursive: true }); |
| 78 | + writeFileSync(join(projectDir, "index.html"), f.html(blur)); |
| 79 | + const job = createRenderJob({ |
| 80 | + fps: FPS, |
| 81 | + format: "png-sequence", |
| 82 | + quality: "standard", |
| 83 | + hdrMode: "force-sdr", |
| 84 | + useGpu: false, |
| 85 | + workers: workersFor(f), |
| 86 | + logger: { info() {}, warn() {}, error() {}, debug() {} }, |
| 87 | + producerConfig: resolveConfig({ browserGpuMode: "software" }), |
| 88 | + }); |
| 89 | + await executeRenderJob(job, projectDir, outputDir); |
| 90 | + const frames = readdirSync(outputDir) |
| 91 | + .filter((file) => file.endsWith(".png")) |
| 92 | + .sort() |
| 93 | + .map((file) => decodePng(readFileSync(join(outputDir, file)))); |
| 94 | + rmSync(projectDir, { recursive: true, force: true }); |
| 95 | + return frames; |
| 96 | +} |
| 97 | + |
| 98 | +async function renderAll(f: BlurFixture): Promise<Renders> { |
| 99 | + const off = await renderFixture(f, "off", "off"); |
| 100 | + const off2 = await renderFixture(f, "off", "off2"); |
| 101 | + const on = await renderFixture(f, "on", "on"); |
| 102 | + return { off, off2, on }; |
| 103 | +} |
| 104 | + |
| 105 | +const workersFor = (f: BlurFixture): number => WORKERS_BY_FIXTURE.get(f.name) ?? WORKERS; |
| 106 | + |
| 107 | +function blockStarts(f: BlurFixture): number[] { |
| 108 | + const frames = Math.round(f.durationSeconds * FPS); |
| 109 | + return distributeFrames(frames, workersFor(f), "", 0).map((task) => task.startFrame); |
| 110 | +} |
| 111 | + |
| 112 | +const radiusPx = (f: BlurFixture): number => Math.ceil(2 * f.dpr); |
| 113 | + |
| 114 | +/** Frames whose `radiusFrames` neighbours on both sides exist. */ |
| 115 | +function comparedFrames(count: number): number[] { |
| 116 | + const { radiusFrames } = INVARIANTS; |
| 117 | + return Array.from({ length: Math.max(0, count - 2 * radiusFrames) }, (_, k) => k + radiusFrames); |
| 118 | +} |
| 119 | + |
| 120 | +function at(frames: readonly Rgba[], n: number): Rgba { |
| 121 | + const frame = frames[n]; |
| 122 | + if (!frame) throw new Error(`missing frame ${n}`); |
| 123 | + return frame; |
| 124 | +} |
| 125 | + |
| 126 | +function compareFrame(f: BlurFixture, r: Renders, n: number): FrameResult { |
| 127 | + const { radiusFrames, block } = INVARIANTS; |
| 128 | + const neighbours = r.off.slice(n - radiusFrames, n + radiusFrames + 1); |
| 129 | + const mask = trailMask(neighbours, radiusPx(f)); |
| 130 | + const options = { block, maxMae: RENDER_OUTSIDE_MAX_MAE }; |
| 131 | + return { |
| 132 | + frame: n, |
| 133 | + outside: compareTiles(at(r.off, n), at(r.on, n), mask, "outside", options), |
| 134 | + noise: compareTiles(at(r.off, n), at(r.off2, n), mask, "outside", options), |
| 135 | + insideMae: compareTiles(at(r.off, n), at(r.on, n), mask, "inside", options).worstMae, |
| 136 | + }; |
| 137 | +} |
| 138 | + |
| 139 | +function compareFixture(f: BlurFixture, r: Renders): FrameResult[] { |
| 140 | + expect(r.off).toHaveLength(Math.round(f.durationSeconds * FPS)); |
| 141 | + expect(r.off2).toHaveLength(r.off.length); |
| 142 | + expect(r.on).toHaveLength(r.off.length); |
| 143 | + return comparedFrames(r.off.length).map((n) => compareFrame(f, r, n)); |
| 144 | +} |
| 145 | + |
| 146 | +function firstBad(results: readonly FrameResult[]): string { |
| 147 | + return JSON.stringify( |
| 148 | + results |
| 149 | + .filter((x) => x.outside.bad.length > 0) |
| 150 | + .slice(0, 5) |
| 151 | + .map((x) => ({ frame: x.frame, bad: x.outside.bad.slice(0, 3) })), |
| 152 | + ); |
| 153 | +} |
| 154 | + |
| 155 | +function expectEqualOutsideTrail(f: BlurFixture, results: readonly FrameResult[]): void { |
| 156 | + expect( |
| 157 | + results.flatMap((x) => x.outside.bad), |
| 158 | + `${f.name} ${firstBad(results)}`, |
| 159 | + ).toEqual([]); |
| 160 | +} |
| 161 | + |
| 162 | +function defineFixtureCases(f: BlurFixture): void { |
| 163 | + describe(f.name, () => { |
| 164 | + let results: FrameResult[] = []; |
| 165 | + |
| 166 | + beforeAll(async () => { |
| 167 | + expect(f.durationSeconds).toBeGreaterThanOrEqual(MIN_SECONDS); |
| 168 | + results = compareFixture(f, await renderAll(f)); |
| 169 | + }, RENDER_TIMEOUT_MS); |
| 170 | + |
| 171 | + const required = REQUIRED_STARTS.get(f.name); |
| 172 | + if (required) { |
| 173 | + it("partitions the frames so workers start where the bug shows", () => { |
| 174 | + const starts = blockStarts(f).filter((n) => n >= required.from && n <= required.to); |
| 175 | + expect(starts.length, `block starts ${blockStarts(f)}`).toBeGreaterThanOrEqual( |
| 176 | + required.count, |
| 177 | + ); |
| 178 | + }); |
| 179 | + } |
| 180 | + |
| 181 | + it("renders blur off the same way twice", () => { |
| 182 | + expect(Math.max(...results.map((x) => x.noise.worstMae))).toBeLessThanOrEqual( |
| 183 | + RENDER_OUTSIDE_MAX_MAE, |
| 184 | + ); |
| 185 | + }); |
| 186 | + |
| 187 | + it("checks most of every frame", () => { |
| 188 | + for (const x of results) { |
| 189 | + expect(x.outside.checkedFraction, `frame ${x.frame}`).toBeGreaterThanOrEqual( |
| 190 | + INVARIANTS.minCheckedFraction, |
| 191 | + ); |
| 192 | + } |
| 193 | + }); |
| 194 | + |
| 195 | + it("outside the trail, blur on equals blur off in every frame", () => |
| 196 | + expectEqualOutsideTrail(f, results)); |
| 197 | + |
| 198 | + if (f.moving.length > 0) { |
| 199 | + it("blurs what moves, so the comparison is not vacuous", () => { |
| 200 | + expect(Math.max(...results.map((x) => x.insideMae))).toBeGreaterThan( |
| 201 | + INVARIANTS.insideMaxMae, |
| 202 | + ); |
| 203 | + }); |
| 204 | + } |
| 205 | + }); |
| 206 | +} |
| 207 | + |
| 208 | +export function defineBlurRenderSuite(title: string, fixtureNames: readonly string[]): void { |
| 209 | + describe.skipIf(!HAS_FFMPEG && !ON_CI)(title, () => { |
| 210 | + let priorFontconfig: string | undefined; |
| 211 | + |
| 212 | + beforeAll(() => { |
| 213 | + if (!HAS_FFMPEG) |
| 214 | + throw new Error(`the blur render tests cannot run on CI: no ffmpeg at ${FFMPEG}`); |
| 215 | + root = mkdtempSync(join(tmpdir(), "hf-blur-differential-")); |
| 216 | + // A host with fontconfig subpixel text colours DOM glyphs but not the capture canvas. That is a |
| 217 | + // host setting, not blur, so both arms draw grayscale text. |
| 218 | + priorFontconfig = process.env.FONTCONFIG_FILE; |
| 219 | + const conf = join(root, "fonts.conf"); |
| 220 | + writeFileSync( |
| 221 | + conf, |
| 222 | + `<?xml version="1.0"?><!DOCTYPE fontconfig SYSTEM "fonts.dtd"><fontconfig> |
| 223 | +<include ignore_missing="yes">/etc/fonts/fonts.conf</include> |
| 224 | +<match target="font"><edit name="rgba" mode="assign"><const>none</const></edit></match> |
| 225 | +</fontconfig>`, |
| 226 | + ); |
| 227 | + process.env.FONTCONFIG_FILE = conf; |
| 228 | + }); |
| 229 | + |
| 230 | + afterAll(() => { |
| 231 | + if (priorFontconfig === undefined) delete process.env.FONTCONFIG_FILE; |
| 232 | + else process.env.FONTCONFIG_FILE = priorFontconfig; |
| 233 | + if (root) rmSync(root, { recursive: true, force: true }); |
| 234 | + }); |
| 235 | + |
| 236 | + for (const name of fixtureNames) defineFixtureCases(fixtureNamed(name)); |
| 237 | + }); |
| 238 | +} |
0 commit comments