Skip to content

Commit b96d3c8

Browse files
fix(core): a multi-worker render replays overlapping tweens behind the timeline stand-in (heygen-com#7231)
* fix(core): a multi-worker render replays overlapping tweens behind the timeline stand-in The overlap and landing-mark walks compared tween parents with the stand-in the producer's GSAP stub hands out, never met it, and lost the overlaps and step times. Both now compare with the real timeline behind the stand-in. * fix(core): drop the moved helper's comment so the file's comment share holds
1 parent 659ef56 commit b96d3c8

5 files changed

Lines changed: 64 additions & 21 deletions

File tree

‎packages/core/src/runtime/adapters/gsap.order.test.ts‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
import gsap from "gsap";
22
import { describe, expect, it } from "vitest";
33
import { seekGsapTimeline } from "./gsap";
4+
import { producerStandIn } from "./gsapStandIn.test-helpers";
45
import type { RuntimeTimelineLike } from "../types";
56

67
const FPS = 60;
78
const FRAMES = 150;
89

910
// A zoom punch still running when a cut sets the zoom and a slower move starts from wherever the zoom is.
10-
function overlappingZoom() {
11+
function overlappingZoom(standIn = false) {
1112
const params = { z: 1 };
1213
const timeline = gsap.timeline({ paused: true });
1314
timeline.to(params, { z: 1.45, duration: 0.3, ease: "power4.out" }, 0.573);
1415
timeline.set(params, { z: 1.04 }, 0.8);
1516
timeline.to(params, { z: 1.12, duration: 1.39, ease: "power1.in" }, 0.869);
16-
return { params, timeline: timeline as unknown as RuntimeTimelineLike };
17+
return {
18+
params,
19+
timeline: standIn ? producerStandIn(timeline) : (timeline as unknown as RuntimeTimelineLike),
20+
};
1721
}
1822

1923
/** The zoom at each frame a renderer captures, in order; a scene's clock starts at `hostStart`. */
20-
function capture(frames: number[], hostStart = 0, seekFirst?: number): Map<number, number> {
21-
const { params, timeline } = overlappingZoom();
24+
function capture(
25+
frames: number[],
26+
hostStart = 0,
27+
seekFirst?: number,
28+
standIn = false,
29+
): Map<number, number> {
30+
const { params, timeline } = overlappingZoom(standIn);
2231
const zoom = new Map<number, number>();
2332
if (seekFirst !== undefined) seekGsapTimeline(timeline, seekFirst, true, undefined, FPS);
2433
for (const frame of frames) {
@@ -42,6 +51,14 @@ describe("a render that captures frames out of order", () => {
4251
}
4352
});
4453

54+
it("gives every frame the same value when the producer hands out a stand-in for the timeline", () => {
55+
for (let worker = 0; worker < 8; worker++) {
56+
for (const [frame, z] of capture(range(worker, FRAMES, 8), 0, undefined, true)) {
57+
expect([frame, z]).toEqual([frame, inOrder.get(frame)]);
58+
}
59+
}
60+
});
61+
4562
it("gives every frame the same value when a chunk starts inside the overlap", () => {
4663
for (const [frame, z] of capture(range(53, FRAMES))) {
4764
expect([frame, z]).toEqual([frame, inOrder.get(frame)]);

‎packages/core/src/runtime/adapters/gsap.steps.test.ts‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
seekGsapTimeline,
1010
} from "./gsap";
1111
import type { GsapStructureKey } from "./gsap";
12+
import { producerStandIn } from "./gsapStandIn.test-helpers";
1213
import type { RuntimeTimelineLike } from "../types";
1314

1415
// Every 0.1 s: hide all three frames, then show one, as frame-by-frame films do.
@@ -646,18 +647,24 @@ describe("seekGsapTimeline", () => {
646647
expect(scene.field.style.visibility).toBe("visible");
647648
});
648649

649-
it("shows two steps on one later time in authored order after the playhead comes back onto it", () => {
650-
const word = document.body.appendChild(document.createElement("span"));
651-
const timeline = gsap.timeline({ paused: true });
652-
timeline.set(word, { visibility: "hidden" }, 1).set(word, { visibility: "visible" }, 1);
653-
timeline.to({}, { duration: 3 }, 0);
654-
const scene = timeline as unknown as RuntimeTimelineLike;
655-
sceneSeek(scene, 2);
656-
sceneSeek(scene, 1);
657-
expect(word.style.visibility).toBe("visible");
658-
sceneSeek(scene, 1.2);
659-
expect(word.style.visibility).toBe("visible");
660-
});
650+
it.each([
651+
["a timeline", (timeline: gsap.core.Timeline) => timeline as unknown as RuntimeTimelineLike],
652+
["the producer's stand-in for a timeline", producerStandIn],
653+
])(
654+
"shows two steps on one later time in authored order after the playhead comes back onto %s",
655+
(_, seekable) => {
656+
const word = document.body.appendChild(document.createElement("span"));
657+
const timeline = gsap.timeline({ paused: true });
658+
timeline.set(word, { visibility: "hidden" }, 1).set(word, { visibility: "visible" }, 1);
659+
timeline.to({}, { duration: 3 }, 0);
660+
const scene = seekable(timeline);
661+
sceneSeek(scene, 2);
662+
sceneSeek(scene, 1);
663+
expect(word.style.visibility).toBe("visible");
664+
sceneSeek(scene, 1.2);
665+
expect(word.style.visibility).toBe("visible");
666+
},
667+
);
661668

662669
it("keeps a scene's steps at 0 through the root's redraw at 0 on every seek", () => {
663670
const scene = revealedAtZero();

‎packages/core/src/runtime/adapters/gsap.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,14 @@ function spanOnParent(
159159
return [Number(parent._start) + from / Math.abs(speed), span / Math.abs(speed)];
160160
}
161161

162+
export function gsapTimelineOf(timeline: object): object {
163+
return (timeline as { __hfReal?: object }).__hfReal ?? timeline;
164+
}
165+
162166
function writeOf(tween: PlacedTween, timeline: RuntimeTimelineLike): PropertyWrite | null {
167+
const owner = gsapTimelineOf(timeline);
163168
let span: [number, number] | null = [Number(tween._start), Number(tween.totalDuration?.() ?? 0)];
164-
for (let parent = tween.parent; span && parent && parent !== timeline; parent = parent.parent) {
169+
for (let parent = tween.parent; span && parent && parent !== owner; parent = parent.parent) {
165170
span = spanOnParent(span, parent);
166171
}
167172
return (
@@ -355,8 +360,9 @@ type GsapPlaced = {
355360
};
356361

357362
function startOnOwner(child: GsapPlaced, owner: object): number | null {
363+
const timeline = gsapTimelineOf(owner);
358364
let at = Number(child._start);
359-
for (let parent = child.parent; parent && parent !== owner; parent = parent.parent) {
365+
for (let parent = child.parent; parent && parent !== timeline; parent = parent.parent) {
360366
if (parent._repeat || !parent._ts) return null;
361367
at = Number(parent._start) + at / parent._ts;
362368
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type gsap from "gsap";
2+
import type { RuntimeTimelineLike } from "../types";
3+
4+
/** The shape the producer's GSAP stub hands a composition instead of the timeline it created. */
5+
export function producerStandIn(real: gsap.core.Timeline): RuntimeTimelineLike {
6+
const standIn = {
7+
__hfReal: real,
8+
__hfIsProxy: true,
9+
totalTime: (...args: [number, boolean?]) =>
10+
args.length > 0 ? (real.totalTime(...args), standIn) : real.totalTime(),
11+
totalDuration: () => real.totalDuration(),
12+
getChildren: (...args: Parameters<typeof real.getChildren>) => real.getChildren(...args),
13+
};
14+
return standIn as unknown as RuntimeTimelineLike;
15+
}

‎packages/core/src/runtime/init.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import {
5757
createGsapAdapter,
5858
holdFrameMarks,
5959
rerenderGsapTimelineAt,
60+
gsapTimelineOf,
6061
seekGsapTimeline,
6162
type GsapStructureKey,
6263
} from "./adapters/gsap";
@@ -1641,9 +1642,6 @@ export function initSandboxRuntimeModular(): void {
16411642

16421643
// Sub-composition timelines the runtime nested into the root: the host composition id, and where.
16431644
const autoNested = new WeakMap<object, { hostId: string; parent: unknown; at: number }>();
1644-
// The producer's GSAP stub registers stand-ins; GSAP's tree holds the timelines behind them.
1645-
const gsapTimelineOf = (timeline: object): object =>
1646-
(timeline as { __hfReal?: object }).__hfReal ?? timeline;
16471645
// The composite that holds the children of a root with no timeline, and how many it holds.
16481646
let missingRootComposite: { timeline: RuntimeTimelineLike; size: number } | null = null;
16491647
// Roots the runtime built. One it no longer holds would stay on GSAP's global timeline with all it last held.

0 commit comments

Comments
 (0)