Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(preview): drop a frame whose bitmap lands after a newer one's

EtienneLescot4ed5efaa2a4e
diff --git a/src/native/hooks/useNativeCompositorView.test.ts b/src/native/hooks/useNativeCompositorView.test.ts
index a5b81b3b1..199a1ee67 100644
--- a/src/native/hooks/useNativeCompositorView.test.ts
+++ b/src/native/hooks/useNativeCompositorView.test.ts
@@ -232,6 +232,51 @@ describe("useNativeCompositorView", () => {
}
});
+ // `inFlight` orders the reads, not the bitmap decodes: an older frame's bitmap can land after
+ // a newer one's. Painting it would bring back its pixels and its buffer size.
+ it("drops a frame whose bitmap lands after a newer one's", async () => {
+ vi.stubGlobal(
+ "ImageData",
+ class {
+ constructor(
+ public data: Uint8ClampedArray,
+ public width: number,
+ public height: number,
+ ) {}
+ },
+ );
+ const decodes: Array<(bitmap: { close: () => void }) => void> = [];
+ vi.stubGlobal(
+ "createImageBitmap",
+ vi.fn(
+ () =>
+ new Promise<{ close: () => void }>((resolve) => {
+ decodes.push(resolve);
+ }),
+ ),
+ );
+ try {
+ mocks.createCompositorView.mockResolvedValue({ id: 1 });
+ mocks.readCompositorFrame
+ .mockResolvedValueOnce({ gen: 1, width: 4, height: 2, data: new Uint8Array(32) })
+ .mockResolvedValueOnce({ gen: 2, width: 4, height: 3, data: new Uint8Array(48) })
+ .mockResolvedValue(null);
+ const ref = stubCanvasRef();
+ const canvas = ref.current as HTMLCanvasElement;
+ renderHook(() => useNativeCompositorView(ref, { sources: { screenPath: "rec.mp4" } }));
+ await waitFor(() => expect(decodes).toHaveLength(2));
+
+ decodes[1]({ close: vi.fn() });
+ await waitFor(() => expect(canvas.height).toBe(3));
+ decodes[0]({ close: vi.fn() });
+ await new Promise((resolve) => setTimeout(resolve, 20));
+
+ expect(canvas.height).toBe(3);
+ } finally {
+ vi.unstubAllGlobals();
+ }
+ });
+
it("stays quiet without an Electron bridge — no view id, so nothing is ever polled", async () => {
mocks.createCompositorView.mockRejectedValue(new Error("Native bridge unavailable."));
mocks.readCompositorFrame.mockResolvedValue(null);
diff --git a/src/native/hooks/useNativeCompositorView.ts b/src/native/hooks/useNativeCompositorView.ts
index 9af037ae3..222ffd754 100644
--- a/src/native/hooks/useNativeCompositorView.ts
+++ b/src/native/hooks/useNativeCompositorView.ts
@@ -177,6 +177,10 @@ export function useNativeCompositorView(
// redundant IPC and removes any chance of two responses landing out of order
// and rewinding `lastGen` (which would re-deliver an already-painted frame).
let inFlight = false;
+ // `inFlight` orders the reads, not the paints: a bitmap still decoding can finish after
+ // the next frame's. The generation actually on the canvas lets an older one be dropped,
+ // instead of bringing back its pixels and its buffer size until native sends another.
+ let paintedGen = 0;
/** rAF pull loop: throttle to ~30fps and repaint ONLY when native reports a
* newer generation. The returned packet is self-describing (`gen` + dims +
@@ -232,11 +236,12 @@ export function useNativeCompositorView(
// An Auto format reshapes that box on every padding tick, so an early resize
// blinked the footage out and back while the slider moved.
const paint = (draw: () => void) => {
- if (disposed) {
+ if (disposed || gen < paintedGen) {
return;
}
setBufferSize(width, height);
draw();
+ paintedGen = gen;
markPainted();
};
// `createImageBitmap` decodes off the main thread (keeps UI at 60/120fps)