Skip to content

Write HTML. Render video. Built for agents.

perf(core): preview runtime scales with what plays, not with the film's clip count (#5674)

Vance Ingallsd05bc4215d57
diff --git a/packages/core/src/audioGroups.ts b/packages/core/src/audioGroups.ts
index 1482eba48a..149566adff 100644
--- a/packages/core/src/audioGroups.ts
+++ b/packages/core/src/audioGroups.ts
@@ -133,10 +133,8 @@ export function isMemberGroupHidden(
* a sub-composition's root is skipped, so repeating one cannot stack chains.
*/
export function resolveMasterBusElement(root: ParentNode): Element | null {
- for (const el of root.querySelectorAll("[data-composition-id]")) {
- if (!el.parentElement?.closest("[data-composition-id]")) return el;
- }
- return null;
+ const first = root.querySelector("[data-composition-id]");
+ return first && !first.parentElement?.closest("[data-composition-id]") ? first : null;
}
/** The master bus chain: the top-level root's `data-fx-chain`, if it carries one. */
diff --git a/packages/core/src/runtime/adapters/gsap.steps.test.ts b/packages/core/src/runtime/adapters/gsap.steps.test.ts
index a04a1e5d9c..93b148d2ca 100644
--- a/packages/core/src/runtime/adapters/gsap.steps.test.ts
+++ b/packages/core/src/runtime/adapters/gsap.steps.test.ts
@@ -579,3 +579,55 @@ describe("seekGsapTimeline", () => {
expect(film.calls()).toBe(1);
});
});
+
+describe("rerenderGsapTimelineAt with a structure key", () => {
+ // Steps at the root and inside a nested part that starts at 1 s, the shape of a caption track.
+ function film() {
+ const frames = [0, 1, 2].map(() => document.body.appendChild(document.createElement("div")));
+ let calls = 0;
+ const timeline = gsap.timeline({ paused: true });
+ for (let k = 0; k < 20; k++) timeline.set(frames[k % 3]!, { opacity: k % 2 }, k * 0.1);
+ const part = gsap.timeline();
+ for (let k = 0; k < 20; k++) part.set(frames[(k + 1) % 3]!, { x: k }, k * 0.05);
+ part.call(() => void calls++, [], 0.5);
+ timeline.add(part, 1);
+ const state = () => ({
+ dom: frames.map((frame) => frame.getAttribute("style")),
+ calls,
+ marks: (timeline.getChildren(true, true, true) as unknown as GsapMarks[]).map((child) => [
+ child.ratio,
+ child._zTime,
+ child._act,
+ ]),
+ });
+ return { timeline, state };
+ }
+ type GsapMarks = { ratio: number; _zTime?: number; _act?: number };
+
+ it("leaves the film exactly as a re-render that walks every child", () => {
+ const plain = film();
+ const keyed = film();
+ const key = { owner: keyed.timeline, version: "1" };
+ for (let frame = 0; frame <= 150; frame++) {
+ const t = frame / 60;
+ for (const [one, structure] of [
+ [plain, undefined],
+ [keyed, key],
+ ] as const) {
+ one.timeline.totalTime(t, false);
+ rerenderGsapTimelineAt(
+ {
+ totalTime: one.timeline.totalTime.bind(one.timeline),
+ totalDuration: one.timeline.totalDuration.bind(one.timeline),
+ getChildren: one.timeline.getChildren.bind(
+ one.timeline,
+ ) as RuntimeTimelineLike["getChildren"],
+ },
+ t,
+ structure,
+ );
+ }
+ expect(keyed.state()).toEqual(plain.state());
+ }
+ });
+});
diff --git a/packages/core/src/runtime/adapters/gsap.ts b/packages/core/src/runtime/adapters/gsap.ts
index b2c7b5cd45..6b4e9114b7 100644
--- a/packages/core/src/runtime/adapters/gsap.ts
+++ b/packages/core/src/runtime/adapters/gsap.ts
@@ -15,10 +15,11 @@ export function rerenderGsapTimelineAt(
getChildren?: RuntimeTimelineLike["getChildren"];
},
t: number,
+ structure?: GsapStructureKey,
): void {
timeline.totalDuration?.();
const children = timeline.getChildren?.(false, true, true) ?? [];
- const marked = childrenWithLandingMarksIn(timeline).map(
+ const marked = landingMarksOf(timeline, structure, t).map(
(child) => [child, child.ratio, child._zTime, child._act] as const,
);
const skipped = childrenStartingAfter(children, t).map((child) => [child, child._ts] as const);
@@ -52,7 +53,13 @@ export function rerenderGsapTimelineAt(
type GsapRenderState = { _initted?: number };
function gsapNeverRendered(timeline: RuntimeTimelineLike): boolean {
- const [child] = (timeline.getChildren?.(false, true, true) ?? []) as { parent?: unknown }[];
+ const own = timeline as { _first?: { parent?: unknown } | null };
+ const [child] =
+ "_first" in own
+ ? own._first
+ ? [own._first]
+ : []
+ : ((timeline.getChildren?.(false, true, true) ?? []) as { parent?: unknown }[]);
const rendered = child?.parent as GsapRenderState | undefined;
return rendered !== undefined && !rendered._initted;
}
@@ -121,6 +128,83 @@ function childrenWithLandingMarksIn(timeline: {
}) as unknown as GsapLandingMarks[];
}
+export type GsapStructureKey = { owner: object; version: string };
+
+type PlacedMarks = {
+ version: string;
+ always: GsapLandingMarks[];
+ points: { at: number; mark: GsapLandingMarks }[];
+};
+
+const landingMarksCache = new WeakMap<object, PlacedMarks>();
+
+const LANDING_MARK_REACH = 0.05;
+
+type GsapPlaced = {
+ _start?: number;
+ _ts?: number;
+ _repeat?: number;
+ parent?: GsapPlaced | null;
+ getChildren?: unknown;
+};
+
+function startOnOwner(child: GsapPlaced, owner: object): number | null {
+ let at = Number(child._start);
+ for (let parent = child.parent; parent && parent !== owner; parent = parent.parent) {
+ if (parent._repeat || !parent._ts) return null;
+ at = Number(parent._start) + at / parent._ts;
+ }
+ return Number.isFinite(at) ? at : null;
+}
+
+function placeLandingMarks(
+ timeline: { getChildren?: RuntimeTimelineLike["getChildren"] },
+ structure: GsapStructureKey,
+): PlacedMarks {
+ const placed: PlacedMarks = { version: structure.version, always: [], points: [] };
+ for (const mark of childrenWithLandingMarksIn(timeline)) {
+ const child = mark as unknown as GsapPlaced;
+ const at =
+ typeof child.getChildren === "function" ? null : startOnOwner(child, structure.owner);
+ if (at === null) placed.always.push(mark);
+ else placed.points.push({ at, mark });
+ }
+ placed.points.sort((a, b) => a.at - b.at);
+ return placed;
+}
+
+function firstPointFrom(points: PlacedMarks["points"], time: number): number {
+ let lo = 0;
+ let hi = points.length;
+ while (lo < hi) {
+ const mid = (lo + hi) >> 1;
+ if (points[mid]!.at < time) lo = mid + 1;
+ else hi = mid;
+ }
+ return lo;
+}
+
+function landingMarksOf(
+ timeline: { getChildren?: RuntimeTimelineLike["getChildren"] },
+ structure: GsapStructureKey | undefined,
+ t: number,
+): GsapLandingMarks[] {
+ if (!structure) return childrenWithLandingMarksIn(timeline);
+ let placed = landingMarksCache.get(structure.owner);
+ if (placed?.version !== structure.version) {
+ placed = placeLandingMarks(timeline, structure);
+ landingMarksCache.set(structure.owner, placed);
+ }
+ const { points } = placed;
+ const near = [...placed.always];
+ const last = t + LANDING_MARK_REACH;
+ for (let i = firstPointFrom(points, t - LANDING_MARK_REACH); i < points.length; i++) {
+ if (points[i]!.at > last) break;
+ near.push(points[i]!.mark);
+ }
+ return near;
+}
+
type GsapAnimation = {
startTime: () => number;
timeScale: () => number;
diff --git a/packages/core/src/runtime/init.test.ts b/packages/core/src/runtime/init.test.ts
index 2a56463e91..c13c0e68b0 100644
--- a/packages/core/src/runtime/init.test.ts
+++ b/packages/core/src/runtime/init.test.ts
@@ -5365,6 +5365,47 @@ describe("initSandboxRuntimeModular", () => {
}) as typeof window.getComputedStyle);
};
+ it("lays a clip out again only once its layout inputs change", async () => {
+ const raf = createManualRaf();
+ vi.spyOn(performance, "now").mockImplementation(() => raf.now());
+ window.requestAnimationFrame =
+ raf.requestAnimationFrame as typeof window.requestAnimationFrame;
+ window.cancelAnimationFrame = raf.cancelAnimationFrame as typeof window.cancelAnimationFrame;
+ const root = makeRoot();
+ const clip = document.createElement("div");
+ clip.setAttribute("data-start", "0");
+ root.appendChild(clip);
+ const reads = vi.spyOn(window, "getComputedStyle");
+ const readsOfClip = () => reads.mock.calls.filter(([el]) => el === clip).length;
+ const frames = async (n: number) => {
+ for (let i = 0; i < n; i++) {
+ raf.step(1000 / 60);
+ await Promise.resolve();
+ }
+ };
+ const posts = vi.spyOn(window.parent, "postMessage");
+ const timelinePosts = () =>
+ posts.mock.calls.filter(([m]) => (m as { type?: string })?.type === "stage-size").length;
+
+ window.__timelines = { main: createMockTimeline(10) };
+ initSandboxRuntimeModular();
+ await frames(5);
+ const settled = readsOfClip();
+ const postedBefore = timelinePosts();
+ expect(settled).toBeGreaterThan(0);
+
+ // A timing edit posts the timeline again; the clip's layout inputs did not move.
+ clip.setAttribute("data-duration", "5");
+ await frames(60);
+ expect(timelinePosts()).toBeGreaterThan(postedBefore);
+ expect(readsOfClip()).toBe(settled);
+
+ clip.setAttribute("data-width", "640");
+ await frames(60);
+ expect(readsOfClip()).toBeGreaterThan(settled);
+ expect(clip.style.width).toBe("640px");
+ });
+
it("force-absolutizes an authored data-start clip and leaves its position to CSS", () => {
const root = makeRoot();
const clip = document.createElement("div");
diff --git a/packages/core/src/runtime/init.ts b/packages/core/src/runtime/init.ts
index 40c4eb8e0a..63be3b3fe5 100644
--- a/packages/core/src/runtime/init.ts
+++ b/packages/core/src/runtime/init.ts
@@ -22,7 +22,12 @@ import {
import { initRuntimeAnalytics, emitAnalyticsEvent } from "./analytics";
import { injectCompositionCssVariables } from "./getVariables";
import { createCssAdapter } from "./adapters/css";
-import { createGsapAdapter, rerenderGsapTimelineAt, seekGsapTimeline } from "./adapters/gsap";
+import {
+ createGsapAdapter,
+ rerenderGsapTimelineAt,
+ seekGsapTimeline,
+ type GsapStructureKey,
+} from "./adapters/gsap";
import { createAnimeJsAdapter } from "./adapters/animejs";
import { createLottieAdapter } from "./adapters/lottie";
import { createThreeAdapter } from "./adapters/three";
@@ -63,7 +68,12 @@ import {
maybeProxyProactively,
setProxyDisplayScale,
} from "./mediaProxy";
-import { probeAndCacheElementVolume, type VolumeKeyframe } from "./mediaVolumeEnvelope.js";
+import {
+ probeAndCacheElementVolume,
+ volumeMoversOf,
+ type VolumeKeyframe,
+ type VolumeMovers,
+} from "./mediaVolumeEnvelope.js";
import { createPickerModule } from "./picker";
import { createRuntimePlayer, resolveRenderSeekTime, type RuntimePlayerTransport } from "./player";
import { createRuntimeState } from "./state";
@@ -765,7 +775,22 @@ export function initSandboxRuntimeModular(): void {
return parsed === null ? null : `${parsed}px`;
};
- const resolveRootCompositionElement = (): HTMLElement | null => findRootCompositionElement();
+ let rootCompositionCache: { revision: number; el: HTMLElement | null } | null = null;
+ let rootCompositionCacheReady = false;
+ const resolveRootCompositionElement = (): HTMLElement | null => {
+ if (!rootCompositionCacheReady) return findRootCompositionElement();
+ watchCompositionTimingInputs();
+ if (
+ compositionTimingObserver &&
+ !renderCaptureSeekStarted &&
+ rootCompositionCache?.revision === compositionTimingRevision &&
+ (rootCompositionCache.el === null || rootCompositionCache.el.isConnected)
+ )
+ return rootCompositionCache.el;
+ const el = findRootCompositionElement();
+ rootCompositionCache = { revision: compositionTimingRevision, el };
+ return el;
+ };
const applyCompositionSizing = () => {
const rootEl = resolveRootCompositionElement();
@@ -825,6 +850,20 @@ export function initSandboxRuntimeModular(): void {
}
};
+ const clipLayoutInputs = new WeakMap<Element, string>();
+ const clipLayoutKey = (el: HTMLElement) =>
+ [
+ el.className,
+ el.style.position,
+ el.style.top,
+ el.style.left,
+ el.style.width,
+ el.style.height,
+ el.style.transform,
+ el.getAttribute("data-width"),
+ el.getAttribute("data-height"),
+ ].join("|");
+
const applyClipLayout = () => {
const rootEl = resolveRootCompositionElement();
if (!rootEl) return;
@@ -848,7 +887,8 @@ export function initSandboxRuntimeModular(): void {
// `justify-content: space-between` clusters in the top-left. Leave them in
// flow so the preview matches the rendered video, which never stamps
// (production renders run as the top-level page, not in an iframe).
- return !el.hasAttribute(RUNTIME_STAMPED_ATTR);
+ if (el.hasAttribute(RUNTIME_STAMPED_ATTR)) return false;
+ return clipLayoutInputs.get(el) !== clipLayoutKey(el);
});
const displayNoneLiftedToMeasureShown = clips
.filter((el) => el.style.getPropertyValue("display") === "none")
@@ -924,6 +964,8 @@ export function initSandboxRuntimeModular(): void {
for (const { el, priority } of displayNoneLiftedToMeasureShown) {
el.style.setProperty("display", "none", priority);
}
+ if (document.readyState === "complete")
+ for (const el of clips) clipLayoutInputs.set(el, clipLayoutKey(el));
};
const createTimingResolver = (includeAuthoredTimingAttrs: boolean) =>
@@ -1281,6 +1323,18 @@ export function initSandboxRuntimeModular(): void {
// second cache checking for itself would be told nothing changed.
let compositionTimingRevision = 0;
+ const queryCache = new Map<string, { revision: number; nodes: Element[] }>();
+ const queryForTransport = (selector: string): Element[] => {
+ if (!compositionTimingObserver || renderCaptureSeekStarted)
+ return Array.from(document.querySelectorAll(selector));
+ if (compositionTimingObserver.takeRecords().length > 0) invalidateCompositionTimingCaches();
+ const cached = queryCache.get(selector);
+ if (cached?.revision === compositionTimingRevision) return cached.nodes;
+ const nodes = Array.from(document.querySelectorAll(selector));
+ queryCache.set(selector, { revision: compositionTimingRevision, nodes });
+ return nodes;
+ };
+
// Restarts the transport loop when it has parked itself (see
// `scheduleNextTransportFrame`). Held as a reassignable binding because the
// invalidation hooks below are installed long before the transport exists,
@@ -1312,6 +1366,20 @@ export function initSandboxRuntimeModular(): void {
return signature;
};
+ const invalidateOnMediaTimingEvent = (event: Event) => {
+ const target = event.target;
+ if (isMediaElement(target) && lengthIsAuthored(target)) return;
+ invalidateCompositionTimingCaches();
+ };
+
+ const withoutOwnTimingRecords = (el: Element, write: () => void) => {
+ const pending = compositionTimingObserver?.takeRecords() ?? [];
+ write();
+ const ours = compositionTimingObserver?.takeRecords() ?? [];
+ if (pending.length > 0 || ours.some((record) => record.target !== el))
+ invalidateCompositionTimingCaches();
+ };
+
const watchCompositionTimingInputs = () => {
if (compositionTimingObserver || typeof MutationObserver === "undefined") return;
compositionTimingObserver = new MutationObserver(invalidateCompositionTimingCaches);
@@ -1326,17 +1394,18 @@ export function initSandboxRuntimeModular(): void {
// they do not bubble — so listen in the capture phase, which reaches every
// media element including ones added long after this binds.
for (const eventType of COMPOSITION_TIMING_MEDIA_EVENTS) {
- document.addEventListener(eventType, invalidateCompositionTimingCaches, true);
+ document.addEventListener(eventType, invalidateOnMediaTimingEvent, true);
}
runtimeCleanupCallbacks.push(() => {
compositionTimingObserver?.disconnect();
compositionTimingObserver = null;
invalidateCompositionTimingCaches();
for (const eventType of COMPOSITION_TIMING_MEDIA_EVENTS) {
- document.removeEventListener(eventType, invalidateCompositionTimingCaches, true);
+ document.removeEventListener(eventType, invalidateOnMediaTimingEvent, true);
}
});
};
+ rootCompositionCacheReady = true;
/**
* The current revision, after taking every signal that could have moved it.
@@ -2112,9 +2181,10 @@ export function initSandboxRuntimeModular(): void {
// (Re-)probe all already-bound media elements against the new timeline.
// Clear the cache first so elements probed against a prior timeline get fresh keyframes.
+ const rebindMovers = passMovers();
for (const el of metadataBoundMedia) {
volumeKeyframeCache.delete(el);
- probeAndCacheVolumeKeyframes(el);
+ probeAndCacheVolumeKeyframes(el, rebindMovers);
}
return true;
};
@@ -2366,6 +2436,10 @@ export function initSandboxRuntimeModular(): void {
let metadataRebindApplied = false;
const metadataBoundMedia = new Set<HTMLMediaElement>();
const volumeKeyframeCache = new WeakMap<HTMLMediaElement, VolumeKeyframe[]>();
+ const passMovers = (): (() => VolumeMovers) => {
+ let movers: VolumeMovers | undefined;
+ return () => (movers ??= volumeMoversOf(state.capturedTimeline ?? {}));
+ };
const scheduleMetadataDurationHydration = () => {
if (state.tornDown) return;
@@ -2498,6 +2572,7 @@ export function initSandboxRuntimeModular(): void {
if (state.tornDown) return;
const mediaEls = Array.from(document.querySelectorAll("video, audio")) as HTMLMediaElement[];
const nearPlayheadPending: HTMLMediaElement[] = [];
+ const bindMovers = passMovers();
for (const mediaEl of mediaEls) {
if (metadataBoundMedia.has(mediaEl)) continue;
metadataBoundMedia.add(mediaEl);
@@ -2541,13 +2616,13 @@ export function initSandboxRuntimeModular(): void {
// Runs only when the timeline is already captured; elements bound before
// the timeline is ready are re-probed the first time bindMediaMetadataListeners
// fires after the timeline has been captured (every 30 transport ticks).
- probeAndCacheVolumeKeyframes(mediaEl);
+ probeAndCacheVolumeKeyframes(mediaEl, bindMovers);
}
if (nearPlayheadPending.length > 0)
syncTimedElementVisibility(state.currentTime, nearPlayheadPending);
};
- const probeAndCacheVolumeKeyframes = (mediaEl: HTMLMediaElement) => {
+ const probeAndCacheVolumeKeyframes = (mediaEl: HTMLMediaElement, movers?: () => VolumeMovers) => {
if (volumeKeyframeCache.has(mediaEl)) return;
probeAndCacheElementVolume(
mediaEl,
@@ -2557,6 +2632,7 @@ export function initSandboxRuntimeModular(): void {
{
allowLiveTimelineSeek: !(window as Window & { __HF_RENDER_CAPTURE_MODE?: boolean })
.__HF_RENDER_CAPTURE_MODE,
+ ...(movers && { movers }),
},
);
};
@@ -2666,7 +2742,7 @@ export function initSandboxRuntimeModular(): void {
/** The bus gain owns a group's mute; true when an unmute leaves a member still to play outside the graph. */
const syncAudioGroupMute = (currentTime: number): boolean => {
let needsCapture = false;
- for (const groupEl of document.querySelectorAll(HF_AUDIO_GROUP_TAG)) {
+ for (const groupEl of queryForTransport(HF_AUDIO_GROUP_TAG)) {
const hidden = groupEl.hasAttribute("data-hidden");
const last = groupHiddenLast.get(groupEl);
if (last === hidden) continue;
@@ -2785,7 +2861,7 @@ export function initSandboxRuntimeModular(): void {
if (el.preload !== "none") {
el.preload = "none";
// Frees a clip the window armed; on one the parser started, it would only restart the fetch.
- if (decided) releaseMedia(el);
+ if (decided) withoutOwnTimingRecords(el, () => releaseMedia(el));
}
} else if (!visible || decided === undefined) {
// Not a clip a jump lands on: the media sync arms that one, and load() would undo its seek.
@@ -2805,14 +2881,54 @@ export function initSandboxRuntimeModular(): void {
(visibleAt(el, loopStartSeconds) ||
dueSoon(el, visibleAt, loopStartSeconds, RELEASE_LOOKAHEAD_SECONDS)));
+ const visibilityDecided = new WeakMap<Element, boolean>();
+ const mediaWindows = new WeakMap<object, Map<Element, { start: number; end: number }>>();
+ const VISIBILITY_REACH_SECONDS = RELEASE_LOOKAHEAD_SECONDS + LOOKAHEAD_SECONDS + 1;
+ const PLAYED_STEP_MAX_SECONDS = 1;
+ const unchangedSincePlayedFrom = (
+ from: number,
+ to: number,
+ rootComp: HTMLElement | null,
+ ): ((node: Element) => boolean) | null => {
+ if (!rootComp || !(Math.abs(to - from) <= PLAYED_STEP_MAX_SECONDS)) return null;
+ const index = resolveMediaClipIndex();
+ let windows = mediaWindows.get(index);
+ if (!windows) {
+ windows = new Map(index.clips.map((clip) => [clip.el, clip]));
+ mediaWindows.set(index, windows);
+ }
+ const lo = Math.min(from, to);
+ const hi = Math.max(from, to);
+ const near = (start: number, end: number, a: number, b: number) =>
+ start - VISIBILITY_REACH_SECONDS <= b && end + VISIBILITY_REACH_SECONDS >= a;
+ return (node) => {
+ if (!isMediaElement(node) || node.parentElement !== rootComp) return false;
+ if (visibilityDecided.get(node) !== false || mediaNearPlayhead.get(node) === true)
+ return false;
+ if (armedBy.has(node) || armedFrom.has(node)) return false;
+ const span = windows.get(node);
+ if (!span || !Number.isFinite(span.start)) return false;
+ const end = Number.isFinite(span.end) ? span.end : Infinity;
+ if (near(span.start, end, lo, hi)) return false;
+ return (
+ loopStartSeconds === null || !near(span.start, end, loopStartSeconds, loopStartSeconds)
+ );
+ };
+ };
+
const applyTimedElementVisibility = (
currentTime: number,
visibilityNodes: Element[],
timingRevision?: number,
+ playedFromSeconds?: number,
) => {
const rootComp = resolveRootCompositionElement();
let decidedTimedClip = false;
const visibleAt = timedVisibilityAt(timingRevision);
+ const unchanged =
+ playedFromSeconds === undefined
+ ? null
+ : unchangedSincePlayedFrom(playedFromSeconds, currentTime, rootComp);
for (const rawNode of visibilityNodes) {
if (!isStylableElement(rawNode)) continue;
@@ -2834,12 +2950,15 @@ export function initSandboxRuntimeModular(): void {
if (nodeAffectsAudio(rawNode)) hiddenAudioDirty = true;
}
+ if (unchanged?.(rawNode)) continue;
+
// Descendants must not override a hidden ancestor clip. CSS visibility can
// otherwise leak child pixels through inactive scenes because a descendant
// with visibility:visible escapes an ancestor's visibility:hidden.
const isVisibleNow =
visibleAt(rawNode, currentTime) &&
clipChainVisibleAt(rawNode.parentElement, currentTime, visibleAt, rootComp);
+ visibilityDecided.set(rawNode, isVisibleNow);
const isRuntimeStamped = stampsForStudio && rawNode.hasAttribute(RUNTIME_STAMPED_ATTR);
if (!isRuntimeStamped) rawNode.style.visibility = isVisibleNow ? "visible" : "hidden";
rawNode.toggleAttribute(CLIP_HIDDEN_ATTR, !isRuntimeStamped && !isVisibleNow);
@@ -2886,11 +3005,12 @@ export function initSandboxRuntimeModular(): void {
// authored length, and it never awaits, so no duration the cache read can change.
const syncTimedElementVisibility = (
currentTime: number,
- visibilityNodes: Element[] = Array.from(document.querySelectorAll("[data-start]")),
+ visibilityNodes: Element[] = queryForTransport("[data-start]"),
timingRevision?: number,
+ playedFromSeconds?: number,
) =>
withTimingResolver(() =>
- applyTimedElementVisibility(currentTime, visibilityNodes, timingRevision),
+ applyTimedElementVisibility(currentTime, visibilityNodes, timingRevision, playedFromSeconds),
);
// Images a seek to `time` would reveal undecoded: skipped with their hidden clip, or still loading.
@@ -3158,6 +3278,7 @@ export function initSandboxRuntimeModular(): void {
document.removeEventListener("play", onMediaPlayWakeTransport, true);
});
+ let lastVisibilityPassSeconds: number | null = null;
const syncMediaForCurrentState = (timingRevision?: number) => {
// Scope 1 of 3 (see `withTimingResolver`). Closes before `syncRuntimeMedia`,
// which may call `el.load()` and invalidate every cached duration.
@@ -3237,7 +3358,12 @@ export function initSandboxRuntimeModular(): void {
mediaClipsInWindow = [];
}
}
- syncTimedElementVisibility(state.currentTime, undefined, timingRevision);
+ const playedFrom =
+ indexed && state.isPlaying && !forceSync && lastVisibilityPassSeconds !== null
+ ? lastVisibilityPassSeconds
+ : undefined;
+ syncTimedElementVisibility(state.currentTime, undefined, timingRevision, playedFrom);
+ lastVisibilityPassSeconds = state.currentTime;
};
const postState = (force: boolean) => {
@@ -3784,10 +3910,11 @@ export function initSandboxRuntimeModular(): void {
childrenBound = false;
bindRootTimelineIfAvailable();
// Probed before the scene was nested, so the new media found no volume envelope.
+ const swapMovers = passMovers();
for (const host of swappedHosts) {
for (const el of host.querySelectorAll<HTMLMediaElement>("video, audio")) {
volumeKeyframeCache.delete(el);
- probeAndCacheVolumeKeyframes(el);
+ probeAndCacheVolumeKeyframes(el, swapMovers);
}
}
const duration = getSafeTimelineDurationSeconds(state.capturedTimeline, 0);
@@ -4277,13 +4404,26 @@ export function initSandboxRuntimeModular(): void {
}
};
+ const standaloneHosts = new Map<string, Element | { missingAt: number }>();
+ const standaloneHostOf = (compositionId: string): Element | null => {
+ const known = standaloneHosts.get(compositionId);
+ if (!renderCaptureSeekStarted && known) {
+ if (!isElementNode(known)) {
+ if (known.missingAt === compositionTimingRevision) return null;
+ } else if (known.isConnected && known.getAttribute("data-composition-id") === compositionId)
+ return known;
+ }
+ const node = document.querySelector(`[data-composition-id="${CSS.escape(compositionId)}"]`);
+ standaloneHosts.set(compositionId, node ?? { missingAt: compositionTimingRevision });
+ return node;
+ };
const seekStandaloneRegisteredTimelines = (timeSeconds: number, options?: RuntimeSeekOptions) => {
const timelines = (window.__timelines ?? {}) as Record<string, RuntimeTimelineLike | undefined>;
const rootCompositionId =
resolveRootCompositionElement()?.getAttribute("data-composition-id") ?? null;
for (const [compositionId, timeline] of Object.entries(timelines)) {
if (!timeline || compositionId === rootCompositionId) continue;
- const node = document.querySelector(`[data-composition-id="${CSS.escape(compositionId)}"]`);
+ const node = standaloneHostOf(compositionId);
if (!node) continue;
const start = resolveStartForElement(node, 0);
if (!Number.isFinite(start)) continue;
@@ -4348,7 +4488,19 @@ export function initSandboxRuntimeModular(): void {
}
};
- const hasZeroDurationCallbackTween = (timeline: RuntimeTimelineLike): boolean => {
+ const zeroDurationCallbackCache = new WeakMap<object, { version: string; value: boolean }>();
+ const hasZeroDurationCallbackTween = (
+ timeline: RuntimeTimelineLike,
+ structure?: GsapStructureKey,
+ ): boolean => {
+ if (!structure) return findZeroDurationCallbackTween(timeline);
+ const cached = zeroDurationCallbackCache.get(structure.owner);
+ if (cached?.version === structure.version) return cached.value;
+ const value = findZeroDurationCallbackTween(timeline);
+ zeroDurationCallbackCache.set(structure.owner, { version: structure.version, value });
+ return value;
+ };
+ const findZeroDurationCallbackTween = (timeline: RuntimeTimelineLike): boolean => {
if (!("getChildren" in timeline) || typeof timeline.getChildren !== "function") {
return false;
}
@@ -4433,7 +4585,13 @@ export function initSandboxRuntimeModular(): void {
try {
if (typeof tl.totalTime === "function") {
const redrawn = seekGsapTimeline(tl, tlSeekTime, suppressEvents);
- if (!redrawn && !suppressEvents && !hasZeroDurationCallbackTween(tl)) {
+ const structure = renderCaptureSeekStarted
+ ? undefined
+ : {
+ owner: tl,
+ version: `${compositionTimingRevision}:${tl.getChildren?.(false, true, true).length ?? 0}`,
+ };
+ if (!redrawn && !suppressEvents && !hasZeroDurationCallbackTween(tl, structure)) {
// The first seek is the only eventful one; the re-render only refreshes styles.
rerenderGsapTimelineAt(
{
@@ -4442,6 +4600,7 @@ export function initSandboxRuntimeModular(): void {
getChildren: tl.getChildren?.bind(tl),
},
tlSeekTime,
+ structure,
);
}
} else {
@@ -4620,7 +4779,7 @@ export function initSandboxRuntimeModular(): void {
const followedOrLongestRunningAudio = (
followed: HTMLMediaElement | null,
): { el: HTMLMediaElement; start: number } | null => {
- const audioEls = document.querySelectorAll("audio[data-start]");
+ const audioEls = queryForTransport("audio[data-start]");
let longest: { el: HTMLMediaElement; start: number; runsUntil: number } | null = null;
for (const el of followed ? [followed, ...audioEls] : audioEls) {
if (!isMediaElement(el) || !el.isConnected) continue;
diff --git a/packages/core/src/runtime/mediaVolumeEnvelope.test.ts b/packages/core/src/runtime/mediaVolumeEnvelope.test.ts
index 53093e4c52..29101f98e5 100644
--- a/packages/core/src/runtime/mediaVolumeEnvelope.test.ts
+++ b/packages/core/src/runtime/mediaVolumeEnvelope.test.ts
@@ -5,6 +5,7 @@ import {
interpolateVolumeGain,
probeAndCacheElementVolume,
probeElementVolumeKeyframes,
+ volumeMoversOf,
} from "./mediaVolumeEnvelope";
describe("probeElementVolumeKeyframes", () => {
@@ -114,7 +115,69 @@ describe("probeElementVolumeKeyframes", () => {
});
});
+describe("volumeMoversOf", () => {
+ it("lists the elements a tween names volume on, and only those", () => {
+ const [a, b, title] = [
+ document.createElement("audio"),
+ document.createElement("audio"),
+ document.createElement("h1"),
+ ];
+ const movers = volumeMoversOf({
+ getChildren: () => [
+ { targets: () => [a], vars: { volume: 0.2 } },
+ { targets: () => [b], vars: { playbackRate: 2 } },
+ { targets: () => [title], vars: { opacity: 1 } },
+ ],
+ });
+ expect([...movers.elements]).toEqual([a]);
+ expect(movers.anySetter).toBe(false);
+ expect(movers.unknown).toBe(false);
+ });
+
+ it("marks a tweened setter on a plain object, and a timeline that cannot list its tweens", () => {
+ const gain = { set level(_v: number) {} };
+ expect(
+ volumeMoversOf({ getChildren: () => [{ targets: () => [gain], vars: { level: 0 } }] })
+ .anySetter,
+ ).toBe(true);
+ expect(volumeMoversOf({}).unknown).toBe(true);
+ expect(
+ volumeMoversOf({
+ getChildren: () => {
+ throw new Error("no");
+ },
+ }).unknown,
+ ).toBe(true);
+ });
+});
+
describe("probeAndCacheElementVolume", () => {
+ it("walks the timeline's tweens once for a pass that shares its movers", () => {
+ let walks = 0;
+ const els = Array.from({ length: 50 }, () => {
+ const audio = document.createElement("audio");
+ audio.dataset.start = "0";
+ audio.dataset.duration = "5";
+ document.body.append(audio);
+ return audio;
+ });
+ const timeline = {
+ totalTime: () => 0,
+ getChildren: () => {
+ walks += 1;
+ return [{ targets: () => [document.createElement("h1")], vars: { opacity: 1 } }];
+ },
+ };
+ const cache = new WeakMap<HTMLMediaElement, { time: number; volume: number }[]>();
+ let movers: ReturnType<typeof volumeMoversOf> | undefined;
+ const shared = () => (movers ??= volumeMoversOf(timeline));
+ for (const el of els) probeAndCacheElementVolume(el, timeline, 5, cache, { movers: shared });
+ expect(walks).toBe(1);
+ // without a shared index each probe walks on its own, as before
+ for (const el of els) probeAndCacheElementVolume(el, timeline, 5, cache);
+ expect(walks).toBe(51);
+ });
+
it("does not seek or cache when live timeline probing is disabled", () => {
const audio = document.createElement("audio");
audio.dataset.volume = "1";
diff --git a/packages/core/src/runtime/mediaVolumeEnvelope.ts b/packages/core/src/runtime/mediaVolumeEnvelope.ts
index a4d8bbeea2..be853c24f2 100644
--- a/packages/core/src/runtime/mediaVolumeEnvelope.ts
+++ b/packages/core/src/runtime/mediaVolumeEnvelope.ts
@@ -216,6 +216,7 @@ export interface VolumeProbeOptions {
* Preview callers omit this option and retain live automation discovery.
*/
allowLiveTimelineSeek?: boolean;
+ movers?: () => VolumeMovers;
}
function namesKey(vars: unknown, matches: (key: string) => boolean, depth = 0): boolean {
@@ -238,26 +239,36 @@ function hasSetter(target: unknown, key: string): boolean {
return false;
}
-/**
- * Whether seeking `timeline` can move `el.volume`: a tween on the element that names `volume`, or a tween
- * on a non-DOM object whose tweened property is a setter (a gain proxy). Spacers, calls and counters skip.
- */
-function timelineCanMoveVolume(timeline: RuntimeTimelineRef, el: HTMLMediaElement): boolean {
- if (typeof timeline.getChildren !== "function") return true;
+export interface VolumeMovers {
+ elements: Set<unknown>;
+ anySetter: boolean;
+ unknown: boolean;
+}
+
+export function volumeMoversOf(timeline: RuntimeTimelineRef): VolumeMovers {
+ const unknown: VolumeMovers = { elements: new Set(), anySetter: false, unknown: true };
+ if (typeof timeline.getChildren !== "function") return unknown;
try {
- return timeline.getChildren(true, true, false).some((tween) => {
+ const movers: VolumeMovers = { elements: new Set(), anySetter: false, unknown: false };
+ for (const tween of timeline.getChildren(true, true, false)) {
const targets: unknown[] = tween.targets?.() ?? [];
- return targets.some((target) =>
- isDomNode(target)
- ? target === el && namesKey(tween.vars, (key) => key === "volume")
- : namesKey(tween.vars, (key) => hasSetter(target, key)),
- );
- });
+ for (const target of targets) {
+ if (isDomNode(target)) {
+ if (namesKey(tween.vars, (key) => key === "volume")) movers.elements.add(target);
+ } else if (!movers.anySetter && namesKey(tween.vars, (key) => hasSetter(target, key))) {
+ movers.anySetter = true;
+ }
+ }
+ }
+ return movers;
} catch {
- return true;
+ return unknown;
}
}
+const canMoveVolume = (movers: VolumeMovers, el: HTMLMediaElement): boolean =>
+ movers.unknown || movers.anySetter || movers.elements.has(el);
+
/**
* Probe a media element and, if volume automation is detected, store a
* NORMALISED envelope in `cache`. Safe to call with a null timeline — returns
@@ -281,7 +292,7 @@ export function probeAndCacheElementVolume(
if (!isMediaElement(mediaEl)) return;
if (compositionDuration <= 0) return;
// Sampling is ~60 whole-timeline seeks per second of clip, paid again on every rebind.
- if (!timelineCanMoveVolume(timeline, mediaEl)) return;
+ if (!canMoveVolume(options.movers?.() ?? volumeMoversOf(timeline), mediaEl)) return;
const seekFn = (t: number) => {
try {
diff --git a/packages/core/src/runtime/previewRasterHints.test.ts b/packages/core/src/runtime/previewRasterHints.test.ts
index c5ef01e714..513a0d2623 100644
--- a/packages/core/src/runtime/previewRasterHints.test.ts
+++ b/packages/core/src/runtime/previewRasterHints.test.ts
@@ -175,4 +175,20 @@ describe("setPreviewRasterScale", () => {
sheet.remove();
link.remove();
});
+
+ it("checks each element once for a batch of attribute changes inside one subtree", async () => {
+ const group = layer(POSITIONED);
+ const words = Array.from({ length: 20 }, () => layer("position: absolute", group));
+ setPreviewRasterScale(0.275);
+ const reads = vi.spyOn(window, "getComputedStyle");
+ group.setAttribute("data-state", "a");
+ for (const word of words) word.setAttribute("data-state", "a");
+ group.setAttribute("data-state", "b");
+ await new Promise((resolve) => setTimeout(resolve, 0));
+ const ownReads = reads.mock.calls.filter(([el, pseudo]) => el === words[0] && !pseudo);
+ // Its own check, the group's look at its descendants, and its holders: once each, not per record.
+ expect(ownReads.length).toBeLessThanOrEqual(3);
+ expect(marked(group)).toBe(true);
+ reads.mockRestore();
+ });
});
diff --git a/packages/core/src/runtime/previewRasterHints.ts b/packages/core/src/runtime/previewRasterHints.ts
index e31c3cdf26..36406b7a58 100644
--- a/packages/core/src/runtime/previewRasterHints.ts
+++ b/packages/core/src/runtime/previewRasterHints.ts
@@ -75,13 +75,17 @@ function unmarkHolders(element: Element): void {
}
}
-function followTree(root: Element): void {
+function followTree(root: Element, batch: FollowBatch): void {
for (const element of [root, ...root.querySelectorAll("*")]) {
- check(element, element === root);
+ if (batch.checked.has(element)) continue;
+ batch.checked.add(element);
+ check(element, element === root || batch.changed.has(element));
unmarkHolders(element);
}
}
+type FollowBatch = { changed: Set<Element>; checked: Set<Element> };
+
// Scripts restyle inline every frame, so only what can break a mark is looked at.
function followStyle(element: Element): void {
if (element.hasAttribute(PREVIEW_RASTER_ATTR)) {
@@ -106,11 +110,11 @@ function markLayers(): void {
for (const layer of layers) layer.setAttribute(PREVIEW_RASTER_ATTR, "");
}
-function followAttribute(record: MutationRecord): void {
+function followAttribute(record: MutationRecord, batch: FollowBatch): void {
if (!isElementNode(record.target) || record.attributeName === PREVIEW_RASTER_ATTR) return;
// Any attribute can match a selector; inline style is the per-frame case, so it is cheaper.
if (record.attributeName === "style") followStyle(record.target);
- else followTree(record.target);
+ else followTree(record.target, batch);
}
// New rules can restyle anything, and a linked sheet only applies once it loads.
@@ -121,15 +125,26 @@ function addedRules(record: MutationRecord): Element[] {
);
}
+function ownAttributeChanges(records: MutationRecord[]): Set<Element> {
+ const changed = new Set<Element>();
+ for (const record of records) {
+ const name = record.attributeName;
+ if (name === null || name === "style" || name === PREVIEW_RASTER_ATTR) continue;
+ if (isElementNode(record.target)) changed.add(record.target);
+ }
+ return changed;
+}
+
function follow(records: MutationRecord[]): void {
const rules = records.flatMap(addedRules);
for (const rule of rules) {
rule.addEventListener("load", () => observer && markLayers(), { once: true });
}
if (rules.length) return markLayers();
+ const batch: FollowBatch = { changed: ownAttributeChanges(records), checked: new Set() };
for (const record of records) {
- if (record.type === "attributes") followAttribute(record);
- else for (const node of record.addedNodes) if (isElementNode(node)) followTree(node);
+ if (record.type === "attributes") followAttribute(record, batch);
+ else for (const node of record.addedNodes) if (isElementNode(node)) followTree(node, batch);
}
}
diff --git a/packages/core/src/runtime/stackingContext.test.ts b/packages/core/src/runtime/stackingContext.test.ts
index 6df58e250f..04a3640caf 100644
--- a/packages/core/src/runtime/stackingContext.test.ts
+++ b/packages/core/src/runtime/stackingContext.test.ts
@@ -1,4 +1,4 @@
-import { afterEach, describe, expect, it } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
import { PREVIEW_RASTER_ATTR } from "../studioPreviewMark";
import { ROOT_CSS_STACKING_CONTEXT_ID, resolveCssStackingContextId } from "./stackingContext";
@@ -18,3 +18,20 @@ describe("resolveCssStackingContextId", () => {
expect(resolveCssStackingContextId(clip)).toBe(ROOT_CSS_STACKING_CONTEXT_ID);
});
});
+
+describe("resolveCssStackingContextId with a pass memo", () => {
+ it("answers siblings as alone and reads their ancestors' style once", () => {
+ document.body.innerHTML =
+ `<div id="ctx" style="position:relative;z-index:1"><div id="row">` +
+ `<video></video><video></video><video></video></div></div><p><span></span></p>`;
+ const nodes = [...document.querySelectorAll("video, span")];
+ const alone = nodes.map((node) => resolveCssStackingContextId(node));
+ const reads = vi.spyOn(window, "getComputedStyle");
+ const byParent = new Map<Element | null, string>();
+ expect(nodes.map((node) => resolveCssStackingContextId(node, byParent))).toEqual(alone);
+ expect(reads.mock.calls.filter(([el]) => el === document.getElementById("row"))).toHaveLength(
+ 1,
+ );
+ reads.mockRestore();
+ });
+});
diff --git a/packages/core/src/runtime/stackingContext.ts b/packages/core/src/runtime/stackingContext.ts
index 9aa226bb59..8b24144b70 100644
--- a/packages/core/src/runtime/stackingContext.ts
+++ b/packages/core/src/runtime/stackingContext.ts
@@ -93,7 +93,18 @@ function cssStackingContextPath(element: Element): string {
* the leaf itself: an element that establishes a context still compares its own
* z-index with siblings in its parent's context.
*/
-export function resolveCssStackingContextId(node: Element): string {
+export function resolveCssStackingContextId(
+ node: Element,
+ byParent?: Map<Element | null, string>,
+): string {
+ const known = byParent?.get(node.parentElement);
+ if (known !== undefined) return known;
+ const id = nearestCssStackingContextId(node);
+ byParent?.set(node.parentElement, id);
+ return id;
+}
+
+function nearestCssStackingContextId(node: Element): string {
const win = node.ownerDocument.defaultView;
if (!win) return ROOT_CSS_STACKING_CONTEXT_ID;
let cursor = node.parentElement;
diff --git a/packages/core/src/runtime/timeline.ts b/packages/core/src/runtime/timeline.ts
index e27f836bb5..83e28000b0 100644
--- a/packages/core/src/runtime/timeline.ts
+++ b/packages/core/src/runtime/timeline.ts
@@ -249,6 +249,7 @@ function buildTimelineClipLabel(node: Element, kind: RuntimeTimelineClip["kind"]
export function collectRuntimeTimelinePayload(params: {
canonicalFps: number;
}): RuntimeTimelineMessage {
+ const stackingByParent = new Map<Element | null, string>();
const runtimeWindow = window as Window & {
__timelines?: Record<string, RuntimeTimelineLike | undefined>;
};
@@ -477,7 +478,7 @@ export function collectRuntimeTimelinePayload(params: {
duration,
track: parseAuthoredTrack(node, i),
zIndex: readInlineZIndex(node),
- stackingContextId: resolveCssStackingContextId(node),
+ stackingContextId: resolveCssStackingContextId(node, stackingByParent),
kind,
tagName: tag,
compositionId: node.getAttribute("data-composition-id"),
@@ -588,7 +589,7 @@ export function collectRuntimeTimelinePayload(params: {
duration: clampedDuration,
track: parseAuthoredTrack(el, gsapTrack),
zIndex: readInlineZIndex(el),
- stackingContextId: resolveCssStackingContextId(el),
+ stackingContextId: resolveCssStackingContextId(el, stackingByParent),
kind: "element",
tagName: el.tagName.toLowerCase(),
compositionId: el.getAttribute("data-composition-id"),
@@ -645,7 +646,7 @@ export function collectRuntimeTimelinePayload(params: {
duration: clampedDuration,
track: parseAuthoredTrack(el, overlayTrack),
zIndex: readInlineZIndex(el),
- stackingContextId: resolveCssStackingContextId(el),
+ stackingContextId: resolveCssStackingContextId(el, stackingByParent),
kind: "element",
tagName: tag,
compositionId: el.getAttribute("data-composition-id"),