From cd7e93baa0b83dbc6536490a876d00a51ba236aa Mon Sep 17 00:00:00 2001 From: Vance Ingalls Date: Fri, 6 Nov 2026 04:15:22 +0000 Subject: [PATCH] fix(core): blur quiets discrete callbacks instead of undoing what they wrote (#7271) * fix(core): blur quiets discrete callbacks instead of undoing what they wrote #7191 undid the DOM a callback wrote during the blur scan with a mutation journal. That regressed once-guarded and memoized timeline callbacks: the callback ran, its guard flipped, and the journal put the DOM back, so the real seek never wrote it again. Mute GSAP's onStart, onComplete, onReverseComplete and onRepeat for the scan and for eventful pose reads, then restore them. onUpdate keeps firing, so movers driven from it are still found. muteDiscreteCallbacks replaces a callback with a no-op rather than clearing it (hasZeroDurationCallbackTween reads typeof vars.onComplete), walks tween.timeline for staggers and keyframes, and skips an animation already muted because scene timelines nest in the root. The journal (journalDom, landThenUndoDom, putBack) is deleted. The loud rewind through 0 stays. Measured in the spike (mute-spike-report.md, "Probe matrix and fresh worker"): every GSAP pattern matches blur off in label and fire count; 8 of 300 comparisons differ, all the non-GSAP row, against 248 of 300 on main. This supersedes the limit #7191 accepted. Mute and restore over 2002 animations is 0.2 ms. Limits: - A mover that exists only because a muted callback set it up is found only if that callback fired before the scan. A worker starting before the call leaves that mover sharp while a later worker blurs it; positions stay right. - tl.call(() => gsap.to(...)) movers are never found and stay sharp. - A non-GSAP adapter that ignores suppressEvents (adapters/animejs.ts) and writes DOM on a once-only completion shows it early. Pinned by a KNOWN LIMIT test. Co-Authored-By: Claude Sonnet 5.5 * test(producer): drop the once-guard-label block-start guard that pins a block that cannot fail The guard required a block start in [1, 15], the worker at frame 9, and the comment said it paints on both sides of the call at 16. Its frames after the call (16, 17) sit inside the trail mask at the label, so that block cannot show the bug. The outside-the-trail case on the 7-worker partition is what catches the mutant; the fixture stays in FIXTURE_NAMES. Co-Authored-By: Claude Sonnet 5.5 * test(engine): split the callbacks matrix in three and share one blur-off run per row The matrix file took 247 s on an idle Mac and timed out at the flake gate's 120 s in all 10 reruns. Each row now compares the arms against one blur-off page painted in order and one fresh page, not a new blur-off page per arm and sequence; the in-order sequence is the first paints of the page that goes back to the first time; rows run their pages together; the paint skips the full-frame PNG that only the pixel tests read. Only the two arms with an onUpdate mover stay (both tiers): they run the scan's mute and the per-frame mute, which the arms without a mover do not. The rows and harness move to __fixtures__/motionBlur/callbackRows.ts and the matrix is split over three files so each run fits the gate. Rows a to y, the three sequences, both tiers, row y and the KNOWN LIMIT test are all kept; no assertion changed. Co-Authored-By: Claude Sonnet 5.5 --------- Co-authored-by: Claude Sonnet 5.5 --- .../src/runtime/adapters/gsap.mute.test.ts | 134 +++++++ packages/core/src/runtime/adapters/gsap.ts | 37 ++ .../src/runtime/captureMotionBlur/discover.ts | 83 +--- .../runtime/captureMotionBlur/filterTier.ts | 33 +- .../runtime/captureMotionBlur/index.test.ts | 48 ++- .../src/runtime/captureMotionBlur/index.ts | 36 +- .../src/runtime/captureMotionBlur/tier.ts | 1 + packages/core/src/runtime/init.ts | 6 + .../__fixtures__/motionBlur/callbackRows.ts | 354 ++++++++++++++++++ .../__fixtures__/motionBlur/fixtures.ts | 8 + ...aptureMotionBlur.callbacks.browser.test.ts | 39 ++ ...ptureMotionBlur.callbacks2.browser.test.ts | 32 ++ ...ptureMotionBlur.callbacks3.browser.test.ts | 42 +++ .../blurDifferential.integration.test.ts | 6 +- 14 files changed, 740 insertions(+), 119 deletions(-) create mode 100644 packages/core/src/runtime/adapters/gsap.mute.test.ts create mode 100644 packages/engine/src/services/__fixtures__/motionBlur/callbackRows.ts create mode 100644 packages/engine/src/services/captureMotionBlur.callbacks.browser.test.ts create mode 100644 packages/engine/src/services/captureMotionBlur.callbacks2.browser.test.ts create mode 100644 packages/engine/src/services/captureMotionBlur.callbacks3.browser.test.ts diff --git a/packages/core/src/runtime/adapters/gsap.mute.test.ts b/packages/core/src/runtime/adapters/gsap.mute.test.ts new file mode 100644 index 0000000000..e0d7caf795 --- /dev/null +++ b/packages/core/src/runtime/adapters/gsap.mute.test.ts @@ -0,0 +1,134 @@ +// @vitest-environment happy-dom +import gsap from "gsap"; +import { describe, expect, it, vi } from "vitest"; +import { muteDiscreteCallbacks } from "./gsap"; +import type { RuntimeTimelineLike } from "../types"; + +const asRuntime = (tl: gsap.core.Timeline): RuntimeTimelineLike => + tl as unknown as RuntimeTimelineLike; + +function sweep(tl: gsap.core.Timeline, ...times: number[]): void { + for (const t of times) tl.seek(t, false); +} + +describe("muteDiscreteCallbacks", () => { + it("quiets onStart, onComplete, onReverseComplete and onRepeat, and leaves onUpdate firing", () => { + const fired = { start: 0, complete: 0, reverse: 0, repeat: 0, update: 0 }; + const tl = gsap.timeline({ paused: true }); + tl.to( + { v: 0 }, + { + v: 1, + duration: 0.2, + repeat: 1, + onStart: () => void fired.start++, + onComplete: () => void fired.complete++, + onReverseComplete: () => void fired.reverse++, + onRepeat: () => void fired.repeat++, + onUpdate: () => void fired.update++, + }, + 0.1, + ); + + const restore = muteDiscreteCallbacks([asRuntime(tl)]); + sweep(tl, 0, 0.2, 0.6, 0); + expect(fired.start + fired.complete + fired.reverse + fired.repeat).toBe(0); + expect(fired.update).toBeGreaterThan(0); + + restore(); + sweep(tl, 0.2, 0.6, 0); + expect(fired.start).toBeGreaterThan(0); + expect(fired.complete).toBeGreaterThan(0); + expect(fired.reverse).toBeGreaterThan(0); + expect(fired.repeat).toBeGreaterThan(0); + }); + + it("reaches a stagger's and a keyframes tween's own tweens", () => { + const staggered = vi.fn(); + const keyed = vi.fn(); + const tl = gsap.timeline({ paused: true }); + tl.to( + [{ v: 0 }, { v: 0 }], + { + v: 1, + duration: 0.05, + stagger: { each: 0.05, onComplete: staggered }, + }, + 0.1, + ); + tl.to( + { v: 0 }, + { + keyframes: [ + { v: 0.5, duration: 0.05 }, + { v: 1, duration: 0.05, onComplete: keyed }, + ], + }, + 0.4, + ); + + const restore = muteDiscreteCallbacks([asRuntime(tl)]); + sweep(tl, 0, 1); + expect(staggered).not.toHaveBeenCalled(); + expect(keyed).not.toHaveBeenCalled(); + + restore(); + sweep(tl, 0, 1); + expect(staggered).toHaveBeenCalled(); + expect(keyed).toHaveBeenCalled(); + }); + + it("restores a callback reached through two timelines", () => { + const fn = vi.fn(); + const root = gsap.timeline({ paused: true }); + const scene = gsap.timeline(); + scene.call(fn, [], 0.5); + root.add(scene, 0); + + const restore = muteDiscreteCallbacks([asRuntime(scene), asRuntime(root)]); + sweep(root, 0, 1); + expect(fn).not.toHaveBeenCalled(); + + restore(); + sweep(root, 0, 1); + expect(fn).toHaveBeenCalledTimes(1); + }); + + it("restores the outer mute's callbacks when mutes nest", () => { + const fn = vi.fn(); + const tl = gsap.timeline({ paused: true }); + tl.call(fn, [], 0.5); + + const outer = muteDiscreteCallbacks([asRuntime(tl)]); + const inner = muteDiscreteCallbacks([asRuntime(tl)]); + inner(); + sweep(tl, 0, 1); + expect(fn).not.toHaveBeenCalled(); + + outer(); + sweep(tl, 0, 1); + expect(fn).toHaveBeenCalledTimes(1); + }); + + it("keeps a replaced function's params and scope", () => { + const scope = { name: "scope" }; + const seen: Array<{ args: unknown[]; scope: unknown }> = []; + const fn = function (this: unknown, ...args: unknown[]): void { + seen.push({ args, scope: this }); + }; + const tl = gsap.timeline({ paused: true }); + tl.call(fn, ["x"], 0.5); + const call = tl.getChildren(false, true, false)[0] as gsap.core.Tween; + call.vars.callbackScope = scope; + + const restore = muteDiscreteCallbacks([asRuntime(tl)]); + sweep(tl, 0, 1); + expect(seen).toEqual([]); + + restore(); + sweep(tl, 0, 1); + expect(seen).toHaveLength(1); + expect(seen[0]?.args).toEqual(["x"]); + expect(seen[0]?.scope).toBe(scope); + }); +}); diff --git a/packages/core/src/runtime/adapters/gsap.ts b/packages/core/src/runtime/adapters/gsap.ts index 2b6682be15..39d74505cc 100644 --- a/packages/core/src/runtime/adapters/gsap.ts +++ b/packages/core/src/runtime/adapters/gsap.ts @@ -482,6 +482,43 @@ export function holdFrameMarks({ }; } +type GsapCallbackHolder = { + vars?: Record; + timeline?: { getChildren?: RuntimeTimelineLike["getChildren"] }; + getChildren?: RuntimeTimelineLike["getChildren"]; +}; + +const DISCRETE_CALLBACKS = ["onStart", "onComplete", "onReverseComplete", "onRepeat"] as const; + +const noOpInPlaceOfCallback = (): void => {}; + +function eachAnimationIn( + animation: GsapCallbackHolder, + visit: (a: GsapCallbackHolder) => void, +): void { + visit(animation); + for (const child of (animation.getChildren?.(false, true, true) ?? []) as GsapCallbackHolder[]) + eachAnimationIn(child, visit); + if (animation.timeline) eachAnimationIn(animation.timeline as GsapCallbackHolder, visit); +} + +export function muteDiscreteCallbacks(timelines: RuntimeTimelineLike[]): () => void { + const held: Array<[Record, string, unknown]> = []; + for (const timeline of timelines) { + eachAnimationIn(gsapTimelineOf(timeline) as GsapCallbackHolder, ({ vars }) => { + if (!vars) return; + for (const name of DISCRETE_CALLBACKS) { + if (typeof vars[name] !== "function" || vars[name] === noOpInPlaceOfCallback) continue; + held.push([vars, name, vars[name]]); + vars[name] = noOpInPlaceOfCallback; + } + }); + } + return () => { + for (const [vars, name, callback] of held) vars[name] = callback; + }; +} + function landingMarksOf( timeline: { getChildren?: RuntimeTimelineLike["getChildren"] }, structure: GsapStructureKey | undefined, diff --git a/packages/core/src/runtime/captureMotionBlur/discover.ts b/packages/core/src/runtime/captureMotionBlur/discover.ts index 63e9f2d9b8..b88e820c6f 100644 --- a/packages/core/src/runtime/captureMotionBlur/discover.ts +++ b/packages/core/src/runtime/captureMotionBlur/discover.ts @@ -292,80 +292,17 @@ function boxesKey(boxes: ReadonlyArray): number { return h; } -type FirstValue = [namespace: string | null, name: string, old: string | null]; - -type DomJournal = { flush: () => void; undo: () => void; stop: () => void }; - -function journalDom(doc: Document): DomJournal { - const firstValues = new Map>(); - const trees: MutationRecord[] = []; - const fold = (records: MutationRecord[]): void => { - for (const r of records) { - if (r.type === "childList") { - trees.push(r); - continue; - } - const own = firstValues.get(r.target) ?? new Map(); - firstValues.set(r.target, own); - const key = `${r.attributeNamespace}:${r.attributeName}`; - if (!own.has(key)) own.set(key, [r.attributeNamespace, r.attributeName ?? "", r.oldValue]); - } - }; - const observer = new MutationObserver(fold); - observer.observe(doc, { - subtree: true, - childList: true, - attributes: true, - attributeOldValue: true, - characterData: true, - characterDataOldValue: true, - }); - return { - flush: () => fold(observer.takeRecords()), - stop: () => observer.disconnect(), - undo: () => { - fold(observer.takeRecords()); - observer.disconnect(); - for (const r of trees.reverse()) { - r.addedNodes.forEach((n) => n.parentNode === r.target && r.target.removeChild(n)); - r.removedNodes.forEach((n) => r.target.insertBefore(n, r.nextSibling)); - } - for (const [node, values] of firstValues) { - for (const [ns, name, old] of values.values()) { - putBack(node, ns, name, old); - } - } - }, - }; -} - -const isElement = (node: Node): node is Element => node.nodeType === 1; - -function putBack(node: Node, ns: string | null, name: string, old: string | null): void { - if (!isElement(node)) { - const text = old ?? ""; - if ((node as CharacterData).data !== text) (node as CharacterData).data = text; - return; +export function whileCallbacksMuted( + host: { muteCallbacks?: () => () => void }, + muted: boolean, + run: () => T, +): T { + const unmute = muted ? host.muteCallbacks?.() : undefined; + try { + return run(); + } finally { + unmute?.(); } - if (node.getAttributeNS(ns, name) === old) return; - if (old === null) node.removeAttributeNS(ns, name); - else node.setAttributeNS(ns, name, old); -} - -// A callback a loud seek fired cannot be un-fired: land with callbacks on, then put the DOM back. -export function landThenUndoDom( - seek: (t: number) => void, - doc: Document, -): Omit & { land: (t: number) => void } { - const journal = journalDom(doc); - return { - flush: journal.flush, - stop: journal.stop, - land: (t) => { - seek(t); - journal.undo(); - }, - }; } /** Discovery, and whether pose reads need callbacks: when they change an element's style or box. */ diff --git a/packages/core/src/runtime/captureMotionBlur/filterTier.ts b/packages/core/src/runtime/captureMotionBlur/filterTier.ts index 7395ae62e7..f0e7014c02 100644 --- a/packages/core/src/runtime/captureMotionBlur/filterTier.ts +++ b/packages/core/src/runtime/captureMotionBlur/filterTier.ts @@ -6,7 +6,7 @@ */ import { swallow } from "../diagnostics"; -import { landThenUndoDom, scanMovers } from "./discover"; +import { scanMovers, whileCallbacksMuted } from "./discover"; import { planFilter, type FilterPlan } from "./filterPlan"; import { readMoverFrames, shutterTimes } from "./frames"; import { createPoseReader } from "./pose"; @@ -123,21 +123,17 @@ export function createFilterTier( function discover(restoreTo: number): void { scanned = host.timeline(); const s0 = performance.now(); - const landing = config.events ? landThenUndoDom(seek, document) : null; - let found: ReturnType; - try { - found = scanMovers({ + const found = whileCallbacksMuted(host, config.events, () => { + const scan = scanMovers({ scene: root, durationSeconds: host.durationSeconds(), fps: host.fps, seek: host.seek, events: config.events, - onStep: landing?.flush, }); - (landing?.land ?? seek)(restoreTo); - } finally { - landing?.stop(); - } + seek(restoreTo); + return scan; + }); readEvents = found.readEvents; movers = found.movers; pads = found.pads; @@ -186,14 +182,13 @@ export function createFilterTier( function plansAt(t: number, engineMode: boolean): Array<[HTMLElement, FilterPlan]> { if (!reader || host.timeline() !== scanned) discover(t); if (!reader) return []; - const landing = readEvents ? landThenUndoDom(readSeek, document) : null; - let read: ReturnType; - try { - read = readMoverFrames({ + const poseReader = reader; + const read = whileCallbacksMuted(host, readEvents, () => + readMoverFrames({ movers, - reader, + reader: poseReader, seek: readSeek, - restore: landing?.land ?? host.holdFrame?.(), + restore: readEvents ? undefined : host.holdFrame?.(), t, reads: engineMode ? RENDER_READS : PREVIEW_READS, ...shutterTimes(config.shutterDeg, config.phaseDeg, host.fps), @@ -201,10 +196,8 @@ export function createFilterTier( frameW: width, scene: root, frameH: height, - }); - } finally { - landing?.stop(); - } + }), + ); const plans: Array<[HTMLElement, FilterPlan]> = []; read.els.forEach((el, i) => { const frame = read.frames[i]; diff --git a/packages/core/src/runtime/captureMotionBlur/index.test.ts b/packages/core/src/runtime/captureMotionBlur/index.test.ts index a9caea4ab7..f93c391acb 100644 --- a/packages/core/src/runtime/captureMotionBlur/index.test.ts +++ b/packages/core/src/runtime/captureMotionBlur/index.test.ts @@ -1,6 +1,6 @@ import gsap from "gsap"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -import { createGsapAdapter } from "../adapters/gsap"; +import { createGsapAdapter, muteDiscreteCallbacks } from "../adapters/gsap"; import type { RuntimeTimelineLike } from "../types"; import { type CaptureMotionBlurHost, @@ -184,7 +184,7 @@ function gsapCallsFilm(movesSlider = false) { tl.to(el, { x: 300, duration: 1, ease: "none" }, 0) .call(() => void (label.textContent = "B"), [], 0.5) .call(() => void label.classList.toggle("on"), [], 0.6); - if (movesSlider) tl.call(() => void (el.style.left = el.style.left ? "" : "40px"), [], 0.2); + if (movesSlider) tl.eventCallback("onUpdate", () => void (el.style.left = `${tl.time() * 40}px`)); const x = () => Number(gsap.getProperty(el, "x")) + (parseFloat(el.style.left) || 0); el.getBoundingClientRect = () => ({ left: x(), top: 0, right: x() + 10, bottom: 10, width: 10, height: 10 }) as DOMRect; @@ -192,7 +192,11 @@ function gsapCallsFilm(movesSlider = false) { const seek = vi.fn((t: number, suppressEvents: boolean) => adapter.seek({ time: t, suppressEvents }), ); - initCaptureMotionBlur({ ...h, seek }); + initCaptureMotionBlur({ + ...h, + seek, + muteCallbacks: () => muteDiscreteCallbacks([tl as unknown as RuntimeTimelineLike]), + }); const paintAt = (t: number): string => { seek(t, false); void paintCaptureMotionBlur(t, PLAY); @@ -1183,6 +1187,44 @@ describe("painting", () => { expect(paintAt.seek.mock.calls.some(([t, quiet]) => t > 0.5 && t < 0.51 && !quiet)).toBe(true); }); + it("restores callbacks when onUpdate throws during the scan", () => { + installHtmlInCanvas(); + const { el, h } = movingHost({ + "data-motion-blur": "on", + "data-motion-blur-events": "on", + }); + const called = vi.fn(); + let thrown = false; + const tl = gsap.timeline({ + paused: true, + onUpdate: () => { + if (thrown || tl.time() < 0.4) return; + thrown = true; + throw new Error("onUpdate threw mid-scan"); + }, + }); + tl.to(el, { x: 300, duration: 1, ease: "none" }, 0).call(called, [], 0.5); + el.getBoundingClientRect = () => { + const x = Number(gsap.getProperty(el, "x")); + return { left: x, top: 0, right: x + 10, bottom: 10, width: 10, height: 10 } as DOMRect; + }; + const adapter = createGsapAdapter({ getTimeline: () => tl as unknown as RuntimeTimelineLike }); + initCaptureMotionBlur({ + ...h, + seek: (t, suppressEvents) => adapter.seek({ time: t, suppressEvents }), + muteCallbacks: () => muteDiscreteCallbacks([tl as unknown as RuntimeTimelineLike]), + }); + try { + void paintCaptureMotionBlur(0.3, PLAY); + } catch { + // The scan may rethrow; what matters is that the callbacks are back. + } + expect(thrown).toBe(true); + expect(called).not.toHaveBeenCalled(); + tl.seek(0.7, false); + expect(called).toHaveBeenCalledTimes(1); + }); + it("fires timeline callbacks during discovery only when the root allows events", () => { for (const events of [true, false]) { installHtmlInCanvas(); diff --git a/packages/core/src/runtime/captureMotionBlur/index.ts b/packages/core/src/runtime/captureMotionBlur/index.ts index b4335eeac8..7529e241eb 100644 --- a/packages/core/src/runtime/captureMotionBlur/index.ts +++ b/packages/core/src/runtime/captureMotionBlur/index.ts @@ -36,7 +36,7 @@ import { import { swallow } from "../diagnostics"; import { createCompositor, type MoverFrame } from "./composite"; import { readMotionBlurSettings } from "../../motionBlurSettings"; -import { landThenUndoDom, scanMovers } from "./discover"; +import { scanMovers, whileCallbacksMuted } from "./discover"; import { createFilterTier } from "./filterTier"; import { readMoverFrames, shutterTimes } from "./frames"; import { createPoseReader } from "./pose"; @@ -258,24 +258,18 @@ function createInstance(host: CaptureMotionBlurHost, config: Config, form: HtmlI const duration = host.durationSeconds(); const defects = createDefectScan(root, isAccelerated); const s0 = performance.now(); - const landing = config.events ? landThenUndoDom(seek, document) : null; - let found: ReturnType; - try { - found = scanMovers({ + const found = whileCallbacksMuted(host, config.events, () => { + const scan = scanMovers({ scene: root, durationSeconds: duration, fps: host.fps, seek: host.seek, events: config.events, - onStep: (i, steps) => { - landing?.flush(); - defects.sample(i, steps, (i / steps) * duration); - }, + onStep: (i, steps) => defects.sample(i, steps, (i / steps) * duration), }); - (landing?.land ?? seek)(restoreTo); - } finally { - landing?.stop(); - } + seek(restoreTo); + return scan; + }); readEvents = found.readEvents; scanDefect = defects.result(); faceNow = defects.facesNow; @@ -288,13 +282,13 @@ function createInstance(host: CaptureMotionBlurHost, config: Config, form: HtmlI function frames(t: number, reads: number): { els: HTMLElement[]; frames: MoverFrame[] } { if (!reader) return { els: [], frames: [] }; - const landing = readEvents ? landThenUndoDom(readSeek, document) : null; - try { - return readMoverFrames({ + const poseReader = reader; + return whileCallbacksMuted(host, readEvents, () => + readMoverFrames({ movers, - reader, + reader: poseReader, seek: readSeek, - restore: landing?.land ?? host.holdFrame?.(), + restore: readEvents ? undefined : host.holdFrame?.(), t, reads, ...shutterTimes(config.shutterDeg, config.phaseDeg, host.fps), @@ -303,10 +297,8 @@ function createInstance(host: CaptureMotionBlurHost, config: Config, form: HtmlI frameW: width, scene: root, frameH: height, - }); - } finally { - landing?.stop(); - } + }), + ); } const capture = (): void => { diff --git a/packages/core/src/runtime/captureMotionBlur/tier.ts b/packages/core/src/runtime/captureMotionBlur/tier.ts index 4da2f9cd83..1307e4e751 100644 --- a/packages/core/src/runtime/captureMotionBlur/tier.ts +++ b/packages/core/src/runtime/captureMotionBlur/tier.ts @@ -12,6 +12,7 @@ export interface CaptureMotionBlurHost { /** Seek the timeline and adapters (not media) for a pose read. */ seek: (t: number, suppressEvents: boolean) => void; holdFrame?: () => (t: number) => void; + muteCallbacks?: () => () => void; ignoreMutations?: (write: () => void) => void; } diff --git a/packages/core/src/runtime/init.ts b/packages/core/src/runtime/init.ts index a166240ad6..e8d379f5a6 100644 --- a/packages/core/src/runtime/init.ts +++ b/packages/core/src/runtime/init.ts @@ -56,6 +56,7 @@ import { createCssAdapter } from "./adapters/css"; import { createGsapAdapter, holdFrameMarks, + muteDiscreteCallbacks, rerenderGsapTimelineAt, gsapTimelineOf, seekGsapTimeline, @@ -4141,6 +4142,11 @@ export function initSandboxRuntimeModular(): void { seekRootChildrenAndAdapters(state.capturedTimeline, t, { suppressEvents: true }), ); }, + muteCallbacks: () => + muteDiscreteCallbacks([ + ...sceneTimelines().map(([, timeline]) => timeline), + ...(state.capturedTimeline ? [state.capturedTimeline] : []), + ]), ignoreMutations: withoutOwnCompositionRecords, }); } diff --git a/packages/engine/src/services/__fixtures__/motionBlur/callbackRows.ts b/packages/engine/src/services/__fixtures__/motionBlur/callbackRows.ts new file mode 100644 index 0000000000..6c51ec0522 --- /dev/null +++ b/packages/engine/src/services/__fixtures__/motionBlur/callbackRows.ts @@ -0,0 +1,354 @@ +import { readFileSync } from "node:fs"; +import { createRequire } from "node:module"; +import { resolve } from "node:path"; +import type { Browser, Page } from "puppeteer-core"; +import { expect } from "vitest"; +import { waitForPendingSeekCompletion } from "../../frameCapture.js"; +import { openFixture, type BlurWindow } from "./blurPage.js"; + +// Blur quiets a timeline's discrete callbacks while it scans and reads poses. Whatever a callback +// writes, a frame painted with blur on must show what blur off shows, and fire it as often. + +const repoRoot = resolve(import.meta.dirname, "../../../../../.."); +const GSAP = readFileSync( + createRequire(resolve(repoRoot, "packages/core/package.json")).resolve("gsap/dist/gsap.min.js"), + "utf8", +); + +const WIDTH = 480; +const HEIGHT = 180; +export const ROW_TIMEOUT_MS = 180_000; +const frame = (k: number): number => k / 30; + +export interface Row { + id: string; + name: string; + setup: string; + read?: string; + extra?: string; + seq?: number[]; + fresh?: number[]; +} + +const SCENE = `
`; + +// Each setup runs with the root timeline `tl`, the `label`, a `fire` wrapper that counts calls, and +// `updates`, which the root timeline's onUpdate runs. +export const GSAP_ROWS: Row[] = [ + { + id: "a", + name: "classList.toggle, DOM only", + setup: `tl.call(fire(()=>label.classList.toggle("on")),[],0.5)`, + read: `label.className||"-"`, + }, + { + id: "b", + name: "textContent=, DOM only", + setup: `tl.call(fire(()=>{label.textContent="B"}),[],0.5)`, + }, + { + id: "c", + name: "JS flag toggle", + setup: `let on=false;tl.call(fire(()=>{on=!on;label.textContent=on?"B":"A"}),[],0.5)`, + }, + { + id: "d", + name: "once-guard", + setup: `let done=false;tl.call(fire(()=>{if(done)return;done=true;label.textContent="B"}),[],0.5)`, + }, + { + id: "e", + name: "memo in onUpdate", + setup: `const st={word:"A"};let shown="A";tl.call(fire(()=>{st.word="B"}),[],0.5);updates.push(()=>{if(st.word!==shown){label.textContent=st.word;shown=st.word}})`, + }, + { + id: "f", + name: "i++; text=words[i]", + setup: `const words=["A","B","C","D","E","F","G","H","I","J","K","L"];let i=0;tl.call(fire(()=>{i++;label.textContent=words[i]}),[],0.5)`, + }, + { + id: "g", + name: "call starts gsap.to(label)", + setup: `label.style.opacity="0.2";tl.call(fire(()=>{gsap.to(label,{opacity:1})}),[],0.5)`, + read: `"tweensOf="+gsap.getTweensOf(label).length`, + }, + { + id: "h", + name: "call writes into a shadow root", + extra: `
`, + setup: `const sr=document.getElementById("sh").attachShadow({mode:"open"});sr.innerHTML='A';tl.call(fire(()=>{sr.getElementById("s").textContent="B"}),[],0.5)`, + read: `document.getElementById("sh").shadowRoot.getElementById("s").textContent`, + }, + { + id: "i", + name: "non-zero tween onComplete", + setup: `const p={v:0};tl.to(p,{v:1,duration:0.1,onComplete:fire(()=>{label.textContent="B"})},0.4)`, + }, + { + id: "j", + name: "call exactly on a painted frame", + setup: `let on=false;tl.call(fire(()=>{on=!on;label.textContent=on?"B":"A"}),[],0.5)`, + seq: [frame(14), frame(15), frame(16)], + fresh: [frame(15)], + }, + { + id: "k", + name: "stagger's per-target onComplete", + setup: `const ps=[{v:0},{v:0}];tl.to(ps,{v:1,duration:0.05,stagger:{each:0.05,onComplete:fire(()=>{label.textContent=label.textContent+"+"})}},0.4)`, + }, + { + id: "l", + name: "keyframe-level onComplete", + setup: `const p={v:0};tl.to(p,{keyframes:[{v:0.5,duration:0.05},{v:1,duration:0.05,onComplete:fire(()=>{label.textContent="B"})}]},0.4)`, + }, + { + id: "m", + name: "sub-timeline call", + setup: `const sub=gsap.timeline();sub.call(fire(()=>{label.textContent="B"}),[],0.2);tl.add(sub,0.3)`, + }, + { + id: "n", + name: "sub-timeline onComplete", + setup: `const sub=gsap.timeline({onComplete:fire(()=>{label.textContent="B"})});sub.to({v:0},{v:1,duration:0.1});tl.add(sub,0.4)`, + }, + { + id: "o", + name: "root timeline onComplete", + setup: `tl.eventCallback("onComplete",fire(()=>{label.textContent="END"}))`, + }, + { + id: "p", + name: "tween onStart", + setup: `const p={v:0};tl.to(p,{v:1,duration:0.2,onStart:fire(()=>{label.textContent="B"})},0.5)`, + }, + { + id: "q", + name: "tween onRepeat", + setup: `const p={v:0};let r=0;tl.to(p,{v:1,duration:0.1,repeat:1,onRepeat:fire(()=>{r++;label.textContent="R"+r})},0.4)`, + }, + { + id: "r", + name: "call with params and scope", + setup: `const o={w:"B"};const fn=fire(function(x){label.textContent=this.w+x});tl.call(fn,["!"],0.5);tl.getChildren(false,true,false).find(c=>c.vars.onComplete===fn).vars.callbackScope=o`, + }, + { + id: "s", + name: "scene timeline call", + extra: SCENE, + setup: `const st=gsap.timeline({paused:true});st.call(fire(()=>{label.textContent="B"}),[],0.5);st.to({v:0},{v:1,duration:2},0);window.__timelines.scene=st`, + }, + { + id: "t", + name: "tween onUpdate prints its value", + setup: `const p={v:0};label.textContent="$0.0T";tl.to(p,{v:1,duration:0.2,ease:"none",onUpdate(){label.textContent="$"+p.v.toFixed(1)+"T"}},0.5)`, + }, + { + id: "u", + name: "onStart sets up what onUpdate uses", + setup: `const p={v:0};let rig;tl.to(p,{v:1,duration:0.5,onStart:fire(()=>{rig=label}),onUpdate(){rig.dataset.v=p.v.toFixed(1);rig.textContent="v"+rig.dataset.v}},0.2)`, + seq: [0.1, 0.3, 0.7], + fresh: [0.7], + }, + { + id: "v", + name: "root onUpdate prints the time, painted at 0", + setup: `updates.push(()=>{label.textContent="$"+tl.time().toFixed(1)+"T"})`, + seq: [0, 0.7], + fresh: [0], + }, + { + id: "w", + name: "tween at 0 onUpdate prints its value, painted at 0", + setup: `const p={v:0};tl.to(p,{v:1,duration:0.2,ease:"none",onUpdate(){label.textContent="$"+p.v.toFixed(1)+"T"}},0)`, + seq: [0, 0.1, 0.7], + fresh: [0], + }, +]; + +// A stand-in shaped like adapters/animejs.ts: seek(ms) runs whatever the page wrote, whether or not +// the caller asked to suppress events. Anime.js itself is not installed in this repo. +export const ONCE_ONLY_ADAPTER: Row = { + id: "x", + name: "non-GSAP adapter's once-only completion writes DOM", + setup: `let d=false;window.__hfAnime=[{seek(ms){if(!d&&ms>=500){d=true;window.__fires++;label.textContent="B"}},pause(){},play(){}}]`, +}; +export const STATELESS_ADAPTER: Row = { + id: "y", + name: "non-GSAP adapter's stateless write", + setup: `window.__hfAnime=[{seek(ms){label.textContent=ms>=500?"B":"A"},pause(){},play(){}}]`, +}; +export const MATRIX_ROWS: Row[] = [...GSAP_ROWS, STATELESS_ADAPTER]; + +const page = ( + row: Row, + attrs: string, + withUpdateMover: boolean, +): string => ` +
+
A
${withUpdateMover ? '
' : ""}${row.extra ?? ""}
+`; + +interface Arm { + name: string; + attrs: string; + tier: "capture" | "filter" | null; + /** An onUpdate that moves an element makes pose reads run callbacks, so the per-frame mute runs too. */ + withUpdateMover: boolean; +} + +const BLUR = 'data-motion-blur="on" data-motion-blur-events="on"'; +const OFF: Arm = { + name: "off", + attrs: 'data-motion-blur="off"', + tier: null, + withUpdateMover: false, +}; +// Every GSAP row paints the same in all four arms (the spike measured it). The two arms with a mover +// run both mutes, the scan's and the per-frame one, so they stand in for the two without. +export const BLUR_ARMS: Arm[] = [ + { + name: "capture with an onUpdate mover", + attrs: `${BLUR} data-motion-blur-tier="capture"`, + tier: "capture", + withUpdateMover: true, + }, + { + name: "filter with an onUpdate mover", + attrs: `${BLUR} data-motion-blur-tier="filter"`, + tier: "filter", + withUpdateMover: true, + }, +]; + +interface Painted { + read: string; + fires: number; + state: string; + tier: string | null; +} + +type Win = BlurWindow & { __read: () => unknown; __fires: number }; + +async function paintFrame(p: Page, t: number): Promise { + const pending = await p.evaluate((time) => { + const w = window as BlurWindow; + w.__player?.renderSeek?.(time); + return w.__hf_page_composite_pending === true; + }, t); + await waitForPendingSeekCompletion(p); + if (!pending) return; + await p.evaluate(async () => { + await (window as BlurWindow).__hf_page_composite_prepare?.(); + }); + const cdp = await p.createCDPSession(); + await cdp.send("Page.captureScreenshot", { + format: "jpeg", + quality: 1, + clip: { x: 0, y: 0, width: 1, height: 1, scale: 1 }, + }); + await cdp.detach(); + await p.evaluate(() => (window as BlurWindow).__hf_page_composite_resolve?.()); +} + +async function paint( + browser: Browser, + row: Row, + arm: Arm, + times: readonly number[], +): Promise { + const p = await openFixture(browser, page(row, arm.attrs, arm.withUpdateMover), 1, { + width: WIDTH, + height: HEIGHT, + }); + const out: Painted[] = []; + try { + for (const t of times) { + await paintFrame(p, t); + out.push( + await p.evaluate(() => { + const w = window as unknown as Win; + const report = w.__hfMotionBlur?.report(); + return { + read: String(w.__read()), + fires: w.__fires, + state: report?.state ?? "none", + tier: report?.tier ?? null, + }; + }), + ); + } + } finally { + await p.close(); + } + return out; +} + +const shown = (paints: Painted[]): Array> => + paints.map(({ read, fires }) => ({ read, fires })); + +/** The matrix is split over three test files so each fits the flake gate's per-run timeout. */ +export const ROW_GROUPS = [ + ["a", "b", "c", "d", "e", "f", "g", "h", "i"], + ["j", "k", "l", "m", "n", "o", "p", "q"], + ["r", "s", "t", "u", "v", "w", "y"], +] as const; + +export const rowsOfGroup = (group: number): Row[] => + MATRIX_ROWS.filter((row) => ROW_GROUPS[group]?.some((id) => id === row.id)); + +export async function expectRowMatchesBlurOff(browser: Browser, row: Row): Promise { + const seq = row.seq ?? [0.3, 0.7]; + const back = [...seq, seq[0] ?? 0]; + const fresh = row.fresh ?? [0.7]; + // One page paints `seq` and then goes back to its first time, so its first paints are the + // in-order sequence and all of them are the one that goes back. Blur off is the same for every arm. + const [offBack = [], offFresh = [], ...armed] = await Promise.all([ + paint(browser, row, OFF, back), + paint(browser, row, OFF, fresh), + ...BLUR_ARMS.flatMap((arm) => [ + paint(browser, row, arm, back), + paint(browser, row, arm, fresh), + ]), + ]); + BLUR_ARMS.forEach((arm, k) => { + const onBack = armed[2 * k] ?? []; + const onFresh = armed[2 * k + 1] ?? []; + const sequences: Array<[string, Painted[], Painted[]]> = [ + ["painted in order", onBack.slice(0, seq.length), offBack.slice(0, seq.length)], + ["a fresh worker", onFresh, offFresh], + ["painted in order, then back to the first time", onBack, offBack], + ]; + for (const [label, on, off] of sequences) { + expect( + on.map(({ state, tier }) => ({ state, tier })), + `${arm.name}, ${label}: blur engaged`, + ).toEqual(on.map(() => ({ state: "on", tier: arm.tier }))); + expect(shown(on), `${arm.name}, ${label}`).toEqual(shown(off)); + } + }); +} + +export async function expectOnceOnlyCompletionShowsEarly(browser: Browser): Promise { + const painted = await Promise.all( + BLUR_ARMS.map((arm) => paint(browser, ONCE_ONLY_ADAPTER, arm, [0.3, 0.7])), + ); + BLUR_ARMS.forEach((arm, k) => { + expect( + (painted[k] ?? []).map(({ read }) => read), + arm.name, + ).toEqual(["B", "B"]); + }); +} diff --git a/packages/engine/src/services/__fixtures__/motionBlur/fixtures.ts b/packages/engine/src/services/__fixtures__/motionBlur/fixtures.ts index 10c4e4dcfe..0bd365510c 100644 --- a/packages/engine/src/services/__fixtures__/motionBlur/fixtures.ts +++ b/packages/engine/src/services/__fixtures__/motionBlur/fixtures.ts @@ -183,6 +183,14 @@ export const FIXTURES: readonly BlurFixture[] = [ tl.call(write("B"), null, 0.5).call(write("A"), null, 0.5333) .call(write("first"), null, 1).call(write("second"), null, 1);`, }), + fixture({ + name: "once-guard-label", + checkpoints: frames(21, 24, 36), + extra: `
A
`, + script: `const label = document.getElementById("label"); + let done = false; + tl.call(() => { if (done) return; done = true; label.textContent = "B" }, null, ${frame(16)});`, + }), fixture({ name: "stateful-callback", extra: `
0
`, diff --git a/packages/engine/src/services/captureMotionBlur.callbacks.browser.test.ts b/packages/engine/src/services/captureMotionBlur.callbacks.browser.test.ts new file mode 100644 index 0000000000..907b4f97f0 --- /dev/null +++ b/packages/engine/src/services/captureMotionBlur.callbacks.browser.test.ts @@ -0,0 +1,39 @@ +import type { Browser } from "puppeteer-core"; +import { afterAll, beforeAll, describe, expect, it } from "vitest"; +import { closeTestChrome } from "./__fixtures__/loopback.js"; +import { hasBrowser, launch } from "./__fixtures__/motionBlur/blurPage.js"; +import { + ROW_GROUPS, + ROW_TIMEOUT_MS, + MATRIX_ROWS, + expectRowMatchesBlurOff, + rowsOfGroup, +} from "./__fixtures__/motionBlur/callbackRows.js"; + +// Rows of the callbacks matrix, group 1 of 3: see callbackRows.ts. +describe.skipIf(!hasBrowser)( + "capture motion blur leaves timeline callbacks as blur off has them (1 of 3)", + () => { + let browser: Browser | undefined; + let supported = false; + + beforeAll(async () => { + ({ browser, supported } = await launch()); + }, 30_000); + afterAll(() => closeTestChrome(browser)); + + it("splits every row of the matrix into exactly one of the three files", () => { + const ids = ROW_GROUPS.flat().sort(); + expect(ids).toEqual(MATRIX_ROWS.map((row) => row.id).sort()); + }); + + it.each(rowsOfGroup(0))( + "row $id, $name: every paint shows what blur off shows, with as many fires", + async (row, ctx) => { + if (!supported || !browser) ctx.skip(); + await expectRowMatchesBlurOff(browser as Browser, row); + }, + ROW_TIMEOUT_MS, + ); + }, +); diff --git a/packages/engine/src/services/captureMotionBlur.callbacks2.browser.test.ts b/packages/engine/src/services/captureMotionBlur.callbacks2.browser.test.ts new file mode 100644 index 0000000000..b28ea198cc --- /dev/null +++ b/packages/engine/src/services/captureMotionBlur.callbacks2.browser.test.ts @@ -0,0 +1,32 @@ +import type { Browser } from "puppeteer-core"; +import { afterAll, beforeAll, describe, it } from "vitest"; +import { closeTestChrome } from "./__fixtures__/loopback.js"; +import { hasBrowser, launch } from "./__fixtures__/motionBlur/blurPage.js"; +import { + ROW_TIMEOUT_MS, + expectRowMatchesBlurOff, + rowsOfGroup, +} from "./__fixtures__/motionBlur/callbackRows.js"; + +// Rows of the callbacks matrix, group 2 of 3: see callbackRows.ts. +describe.skipIf(!hasBrowser)( + "capture motion blur leaves timeline callbacks as blur off has them (2 of 3)", + () => { + let browser: Browser | undefined; + let supported = false; + + beforeAll(async () => { + ({ browser, supported } = await launch()); + }, 30_000); + afterAll(() => closeTestChrome(browser)); + + it.each(rowsOfGroup(1))( + "row $id, $name: every paint shows what blur off shows, with as many fires", + async (row, ctx) => { + if (!supported || !browser) ctx.skip(); + await expectRowMatchesBlurOff(browser as Browser, row); + }, + ROW_TIMEOUT_MS, + ); + }, +); diff --git a/packages/engine/src/services/captureMotionBlur.callbacks3.browser.test.ts b/packages/engine/src/services/captureMotionBlur.callbacks3.browser.test.ts new file mode 100644 index 0000000000..66e74b487d --- /dev/null +++ b/packages/engine/src/services/captureMotionBlur.callbacks3.browser.test.ts @@ -0,0 +1,42 @@ +import type { Browser } from "puppeteer-core"; +import { afterAll, beforeAll, describe, it } from "vitest"; +import { closeTestChrome } from "./__fixtures__/loopback.js"; +import { hasBrowser, launch } from "./__fixtures__/motionBlur/blurPage.js"; +import { + ROW_TIMEOUT_MS, + expectOnceOnlyCompletionShowsEarly, + expectRowMatchesBlurOff, + rowsOfGroup, +} from "./__fixtures__/motionBlur/callbackRows.js"; + +// Rows of the callbacks matrix, group 3 of 3: see callbackRows.ts. +describe.skipIf(!hasBrowser)( + "capture motion blur leaves timeline callbacks as blur off has them (3 of 3)", + () => { + let browser: Browser | undefined; + let supported = false; + + beforeAll(async () => { + ({ browser, supported } = await launch()); + }, 30_000); + afterAll(() => closeTestChrome(browser)); + + it( + "KNOWN LIMIT: a non-GSAP adapter's once-only completion shows early, as adapters/animejs.ts ignores suppressEvents", + async (ctx) => { + if (!supported || !browser) ctx.skip(); + await expectOnceOnlyCompletionShowsEarly(browser as Browser); + }, + ROW_TIMEOUT_MS, + ); + + it.each(rowsOfGroup(2))( + "row $id, $name: every paint shows what blur off shows, with as many fires", + async (row, ctx) => { + if (!supported || !browser) ctx.skip(); + await expectRowMatchesBlurOff(browser as Browser, row); + }, + ROW_TIMEOUT_MS, + ); + }, +); diff --git a/packages/producer/src/services/render/blurDifferential.integration.test.ts b/packages/producer/src/services/render/blurDifferential.integration.test.ts index d5a65a91e5..4258b4b486 100644 --- a/packages/producer/src/services/render/blurDifferential.integration.test.ts +++ b/packages/producer/src/services/render/blurDifferential.integration.test.ts @@ -35,7 +35,10 @@ const WORKERS = 4; * calls shows any call state its scan leaves behind; one starting on a call only by timing (#6967). * 7 workers over 60 frames start at 0, 9, 18, 27, 36, 45 and 54. */ -const WORKERS_BY_FIXTURE = new Map([["callbacks", 7]]); +const WORKERS_BY_FIXTURE = new Map([ + ["callbacks", 7], + ["once-guard-label", 7], +]); /** Block starts the partition must have, at least `count` of them in [from, to]. */ const REQUIRED_STARTS = new Map([ ["callbacks", { from: 18, to: 28, count: 2 }], @@ -46,6 +49,7 @@ const RENDER_TIMEOUT_MS = 600_000; const FIXTURE_NAMES = [ "callbacks", + "once-guard-label", "scroll-list", "nested-canvas-offset", "held-card",