8000 fix(preview): image lanes render — report bitmap dims to store by johannboehme · Pull Request #19 · johannboehme/tk-1 · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 161 additions & 0 deletions frontend/src/editor/render/preview-runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
} from "./video-element-pool";
import type { Clip, ImageClip, VideoClip } from "../types";
import type { EditorStoreSnapshot } from "./build-descriptor";
import { useEditorStore } from "../store";

function videoClip(id: string, more: Partial<VideoClip> = {}): VideoClip {
return {
Expand Down Expand Up @@ -291,6 +292,166 @@ describe("PreviewRuntime — image bitmap cache", () => {
});
});

describe("PreviewRuntime — image clips without pre-set dims", () => {
// Background: image clips arrive in the store without `displayW`/`displayH`
// (the V1 `<img>`-overlay that previously reported `naturalWidth/Height`
// is gone since the V1 preview path was dropped). `buildPreviewLayers`
// early-exits when dims are missing, so the image never enters
// `descriptor.layers`, so `buildSourcesMap`'s lazy `ensureImageBitmap`
// is never called — chicken-and-egg lock that leaves the preview black.
// The runtime must (a) trigger bitmap loads for image clips up front,
// independent of the descriptor, and (b) report `bm.width`/`bm.height`
// back to the store so the next frame can build the layer.

function imageClipNoDims(id: string): ImageClip {
// Helper builds an image clip with displayW/H deliberately unset —
// this is the production state for a freshly-added image, before
// anything has reported its natural dims.
const c = imageClip(id);
delete (c as { displayW?: number }).displayW;
delete (c as { displayH?: number }).displayH;
return c;
}

it("kicks off bitmap load even when image has no displayW/H (no chicken-and-egg)", async () => {
const fakeBitmap = { width: 800, height: 600, close: vi.fn() } as unknown as ImageBitmap;
const loadBitmap = vi.fn(async () => fakeBitmap);
const backend = makeBackend();
const { rt } = makeRuntime({
snap: snapshot({ clips: [imageClipNoDims("img1")] }),
cams: { img1: { videoUrl: "img1.png" } },
backend,
loadBitmap,
});
await rt.init();
// First tick — even though the image is dropped from the descriptor
// (no displayW/H → buildPreviewLayers early-exits), the load must
// still start.
rt.tick();
expect(loadBitmap).toHaveBeenCalledWith("img1.png");
});

it("reports bm.width/height to the store via setClipDisplayDims after load", async () => {
const fakeBitmap = { width: 1234, height: 567, close: vi.fn() } as unknown as ImageBitmap;
const loadBitmap = vi.fn(async () => fakeBitmap);
const setSpy = vi.fn();
const getStateSpy = vi
.spyOn(useEditorStore, "getState")
.mockReturnValue({ setClipDisplayDims: setSpy } as unknown as ReturnType<
typeof useEditorStore.getState
>);
try {
const { rt } = makeRuntime({
snap: snapshot({ clips: [imageClipNoDims("img1")] }),
cams: { img1: { videoUrl: "img1.png" } },
loadBitmap,
});
await rt.init();
rt.tick();
// Let the loadBitmap promise resolve and its .then run.
await Promise.resolve();
await Promise.resolve();
expect(setSpy).toHaveBeenCalledWith("img1", 1234, 567);
} finally {
getStateSpy.mockRestore();
}
});

it("does NOT report dims when bitmap load fails", async () => {
const loadBitmap = vi.fn(async () => {
throw new Error("nope");
});
const setSpy = vi.fn();
const getStateSpy = vi
.spyOn(useEditorStore, "getState")
.mockReturnValue({ setClipDisplayDims: setSpy } as unknown as ReturnType<
typeof useEditorStore.getState
>);
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
try {
const { rt } = makeRuntime({
snap: snapshot({ clips: [imageClipNoDims("img1")] }),
cams: { img1: { videoUrl: "img1.png" } },
loadBitmap,
});
await rt.init();
rt.tick();
await Promise.resolve();
await Promise.resolve();
await Promise.resolve();
expect(setSpy).not.toHaveBeenCalled();
expect(warnSpy).toHaveBeenCalled();
} finally {
getStateSpy.mockRestore();
warnSpy.mockRestore();
}
});

it("reports dims exactly once per image — no per-frame churn", async () => {
const fakeBitmap = { width: 800, height: 600, close: vi.fn() } as unknown as ImageBitmap;
const loadBitmap = vi.fn(async () => fakeBitmap);
const setSpy = vi.fn();
const getStateSpy = vi
.spyOn(useEditorStore, "getState")
.mockReturnValue({ setClipDisplayDims: setSpy } as unknown as ReturnType<
typeof useEditorStore.getState
>);
try {
const { rt } = makeRuntime({
snap: snapshot({ clips: [imageClipNoDims("img1")] }),
cams: { img1: { videoUrl: "img1.png" } },
loadBitmap,
});
await rt.init();
// Run many ticks; the load resolves once, the dim-report fires once.
for (let i = 0; i < 30; i++) {
rt.tick();
await Promise.resolve();
}
expect(setSpy).toHaveBeenCalledTimes(1);
expect(loadBitmap).toHaveBeenCalledTimes(1);
} finally {
getStateSpy.mockRestore();
}
});

it("kicks off bitmap load for an image added live via setCams (no init-time clip)", async () => {
// Repro of the live + Media flow: editor opens with no clips, user
// drops an image, addClip + setCams fire. The runtime must catch
// the dim-less image on the reconcile path, not just at init().
const fakeBitmap = { width: 800, height: 600, close: vi.fn() } as unknown as ImageBitmap;
const loadBitmap = vi.fn(async () => fakeBitmap);
const backend = makeBackend();
const pool = makePool();
const playback = { currentTime: 0, isPlaying: false };
const snap: { value: EditorStoreSnapshot } = { value: snapshot({ clips: [] }) };
const canvas = document.createElement("canvas");
const rt = new PreviewRuntime({
canvas,
cams: {},
capabilities: { webgl2: false, webgpu: false },
cssW: 100,
cssH: 50,
dpr: 1,
initialScale: 1,
createBackendFn: vi.fn(async () => backend),
createPool: () => pool,
raf: () => 0,
cancelRaf: () => undefined,
readSnapshot: () => snap.value,
readPlayback: () => playback,
loadBitmap,
});
await rt.init();

// Live add: new clip with NO displayW/H + cams URL update.
snap.value = snapshot({ clips: [imageClipNoDims("img1")] });
rt.setCams({ img1: { videoUrl: "img1.png" } });
rt.tick();
expect(loadBitmap).toHaveBeenCalledWith("img1.png");
});
});

describe("PreviewRuntime — last-good-frame cache (hides black flash on seek/wrap)", () => {
// Background: when the user scrubs the playhead OR a loop wraps to
// its start, the pool re-sets `<video>.currentTime`. The browser's
Expand Down
23 changes: 23 additions & 0 deletions frontend/src/editor/render/preview-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ export class PreviewRuntime {
// with so the first tick doesn't redundantly re-reconcile.
this.lastReconciledClipsRef = snapshot.clips;
this.lastReconciledCamsRef = this.opts.cams;
// Kick off bitmap loads for any image clips already in the store.
// The reconcile gate in tick() won't fire on the first tick (refs
// match what we seeded above), so the eager start has to happen
// here — otherwise an image present at editor open never loads.
this.ensureAllImageBitmaps(snapshot.clips);
const caps = this.computeCaps();
this.backend = await this.opts.createBackendFn(this.opts.canvas, caps, this.opts.capabilities);
await this.backend.warmup();
Expand Down Expand Up @@ -300,6 +305,12 @@ export class PreviewRuntime {
this.lastReconciledClipsRef = snapshot.clips;
this.lastReconciledCamsRef = this.opts.cams;
this.evictStaleFrames(snapshot.clips);
// Image symmetric to the video pool: a newly-added image (live
// "+ Media" flow) needs its bitmap fetched. We can't rely on
// buildSourcesMap's lazy trigger because the image has no
// displayW/H yet, so buildPreviewLayers drops it from
// descriptor.layers and that loop never sees it.
this.ensureAllImageBitmaps(snapshot.clips);
}
< 990A /td>
// Time the actual GPU/CPU work so the adaptive scaler sees real
Expand Down Expand Up @@ -512,12 +523,24 @@ export class PreviewRuntime {
return;
}
this.bitmaps.set(clipId, bm);
// Symmetric to VideoElementPool.onDimsReport for videos: tell
// the store the image's natural dims so buildPreviewLayers
// stops early-exiting on the missing displayW/H. Store setter
// is a no-op when dims are unchanged, so this fires once per
// image and doesn't churn the clips ref.
useEditorStore.getState().setClipDisplayDims(clipId, bm.width, bm.height);
})
.catch((err) => {
this.bitmapsLoading.delete(clipId);
console.warn(`[compositor] image bitmap load failed for ${clipId}:`, err);
});
}

private ensureAllImageBitmaps(clips: readonly Clip[]): void {
for (const c of clips) {
if (isImageClip(c)) this.ensureImageBitmap(c.id, clips);
}
}
}

// ---- defaults ----
Expand Down
Loading
0