FFFF fix(preview): hold last-good frame to hide black flash on seek/wrap by johannboehme · Pull Request #17 · johannboehme/tk-1 · GitHub
[go: up one dir, main page]

Skip to content
10000
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
13 changes: 12 additions & 1 deletion frontend/src/editor/render/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,18 @@ import type { FrameDescriptor } from "./frame-descriptor";
* time. A missing entry means the source isn't ready yet — the
* backend MAY skip that layer or substitute background. */
export type LayerSource =
| { kind: "video"; element: HTMLVideoElement }
| {
kind: "video";
element: HTMLVideoElement;
/** Last-good frame snapshot held by the runtime. When the
* `<video>` is mid-seek (`seeking || readyState < 2`) the
* runtime sets `preferFallback=true` so the backend draws this
* bitmap instead of the empty/transparent video element —
* hides the black flash that would otherwise show the
* background-clear through during loop wraps and scrubs. */
fallback?: ImageBitmap;
preferFallback?: boolean;
}
| { kind: "videoframe"; frame: VideoFrame }
| { kind: "image"; bitmap: ImageBitmap | HTMLImageElement }
| { kind: "test-pattern" };
Expand Down
84 changes: 84 additions & 0 deletions frontend/src/editor/render/canvas2d-backend.browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,90 @@ describe("Canvas2DBackend — letterbox / pillarbox pixel parity", () => {
});
});

describe("Canvas2DBackend — last-good-frame fallback", () => {
// The runtime hands the backend a `video` source with
// `preferFallback=true` + a cached ImageBitmap during the few decode
// frames after a seek/loop-wrap. The backend must paint pixels from
// that bitmap, not from the stale/empty <video> — otherwise the
// black background-clear shows through and the user sees a flash.

it("draws the fallback bitmap when preferFallback=true", async () => {
// Use a small distinct bitmap so we can prove the fallback path,
// not the global 4-quadrant fixture.
const off = new OffscreenCanvas(20, 20);
const fctx = off.getContext("2d")!;
fctx.fillStyle = "rgb(0, 200, 100)"; // teal-ish, unique vs fixture
fctx.fillRect(0, 0, 20, 20);
const fallback = await createImageBitmap(off);

const canvas = document.createElement("canvas");
const b = new Canvas2DBackend();
await b.init(canvas, { pixelW: 100, pixelH: 100 });

// Real <video> element — readyState is 0, so without the fallback
// the layer would draw nothing and the bg-clear (#000) would win.
const videoEl = document.createElement("video");
b.drawFrame(
descriptor([videoLayer()]),
new Map([
[
"a",
{
kind: "video",
element: videoEl,
fallback,
preferFallback: true,
},
],
]),
);
const ctx = canvas.getContext("2d")!;
const px = ctx.getImageData(50, 50, 1, 1).data;
expect(Math.abs(px[0] - 0)).toBeLessThan(20);
expect(Math.abs(px[1] - 200)).toBeLessThan(20);
expect(Math.abs(px[2] - 100)).toBeLessThan(20);
fallback.close();
});

it("ignores fallback when preferFallback is not set", async () => {
const off = new OffscreenCanvas(20, 20);
const fctx = off.getContext("2d")!;
fctx.fillStyle = "rgb(0, 200, 100)";
fctx.fillRect(0, 0, 20, 20);
const fallback = await createImageBitmap(off);

const canvas = document.createElement("canvas");
const b = new Canvas2DBackend();
await b.init(canvas, { pixelW: 100, pixelH: 100 });

const videoEl = document.createElement("video"); // readyState=0 → empty
b.drawFrame(
descriptor([videoLayer()]),
new Map([
[
"a",
{
kind: "video",
element: videoEl,
fallback, // present but flag is false → must not paint
preferFallback: false,
},
],
]),
);
const ctx = canvas.getContext("2d")!;
const px = ctx.getImageData(50, 50, 1, 1).data;
// Background-clear black wins (or whatever the empty <video> draws).
// Crucially, the teal fallback must NOT be visible.
const isTeal =
Math.abs(px[0] - 0) < 20 &&
Math.abs(px[1] - 200) < 20 &&
Math.abs(px[2] - 100) < 20;
expect(isTeal).toBe(false);
fallback.close();
});
});

describe("Canvas2DBackend — vignette FX pixel parity", () => {
it("corners are darker than center after vignette pass", async () => {
const canvas = document.createElement("canvas");
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/editor/render/canvas2d-backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ function drawLayer(ctx: AnyCtx2D, layer: FrameLayer, src: LayerSource): void {
function sourceToImage(src: LayerSource): CanvasImageSource | null {
switch (src.kind) {
case "video":
// Last-good-frame substitution: while the <video> is mid-seek
// (after a scrub or loop wrap) the runtime hands us the cached
// bitmap to hide the decode-latency black flash.
if (src.preferFallback && src.fallback) return src.fallback;
return src.element;
case "videoframe":
return src.frame as unknown as CanvasImageSource;
Expand Down
Loading
Loading
0