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): give a shared frame's slot back 40 ms after Chromium lets go

EtienneLescotc5d3dcde2fea
diff --git a/electron/native-bridge/services/compositorViewService.test.ts b/electron/native-bridge/services/compositorViewService.test.ts
index 40987fe62..a7791c58f 100644
--- a/electron/native-bridge/services/compositorViewService.test.ts
+++ b/electron/native-bridge/services/compositorViewService.test.ts
@@ -10,6 +10,7 @@ import {
CompositorViewService,
ffmpegSharedBinCandidates,
resolveSceneAssetPaths,
+ SHARED_SLOT_RELEASE_DELAY_MS,
} from "./compositorViewService";
describe("native GIF cancellation capability", () => {
@@ -185,13 +186,22 @@ describe("CompositorViewService frames handed over as shared GPU textures", () =
expect(addon.releaseSharedFrame).not.toHaveBeenCalled();
});
- it("hands the slot back once Chromium has let go of the texture everywhere", async () => {
- const { addon, id, releaseEverywhere, service } = setup();
- await service.readFrame(id, 0, target);
+ it("hands the slot back a while after Chromium has let go of the texture everywhere", async () => {
+ vi.useFakeTimers();
+ try {
+ const { addon, id, releaseEverywhere, service } = setup();
+ await service.readFrame(id, 0, target);
- releaseEverywhere();
+ releaseEverywhere();
+ // Not at once: the GPU copy into the canvas may still be queued behind the release.
+ vi.advanceTimersByTime(SHARED_SLOT_RELEASE_DELAY_MS - 1);
+ expect(addon.releaseSharedFrame).not.toHaveBeenCalled();
- expect(addon.releaseSharedFrame).toHaveBeenCalledWith(id, 1, 3);
+ vi.advanceTimersByTime(1);
+ expect(addon.releaseSharedFrame).toHaveBeenCalledWith(id, 1, 3);
+ } finally {
+ vi.useRealTimers();
+ }
});
it("goes back to read-back when a delivery fails", async () => {
diff --git a/electron/native-bridge/services/compositorViewService.ts b/electron/native-bridge/services/compositorViewService.ts
index 34f4004d8..078f53877 100644
--- a/electron/native-bridge/services/compositorViewService.ts
+++ b/electron/native-bridge/services/compositorViewService.ts
@@ -233,6 +233,17 @@ export interface CompositorViewServiceOptions {
gpuCompositing?: () => boolean;
}
+/**
+ * How long a shared frame's ring slot stays out of the render thread's reach after Chromium has
+ * let go of it. Chromium lets go when the renderer drops its references, which is as soon as it
+ * has issued the draw into the canvas, not once the GPU has run it: the copy runs when the frame
+ * carrying it is drawn, up to a vsync later. A slot written again before that copy put a later
+ * frame on the canvas, then the one in between, so the picture and the cursor stepped back a frame
+ * (tens of times in 18 s at 60 Hz, a few at 280 Hz). 40 ms is more than two vsyncs at 60 Hz; it
+ * took the steps back to none at both rates, and the four slots still keep up with 60 fps.
+ */
+export const SHARED_SLOT_RELEASE_DELAY_MS = 40;
+
/** The two calls a shared preview frame needs from Electron's `sharedTexture` module. */
export type SharedTextureApi = Pick<
Electron.SharedTexture,
@@ -746,8 +757,14 @@ export class CompositorViewService {
codedSize: { width: frame.width, height: frame.height },
handle: { ntHandle: frame.handle },
},
- // Chromium is done with the texture in every process: the slot may be written again.
- allReferencesReleased: () => addon.releaseSharedFrame?.(id, frame.slot, frame.gen),
+ // Chromium is done with the texture in every process: after a grace period, the slot
+ // may be written again (SHARED_SLOT_RELEASE_DELAY_MS says why it waits).
+ allReferencesReleased: () => {
+ setTimeout(
+ () => addon.releaseSharedFrame?.(id, frame.slot, frame.gen),
+ SHARED_SLOT_RELEASE_DELAY_MS,
+ );
+ },
});
await api.sendSharedTexture({ frame: target, importedSharedTexture: imported }, meta);
return { ...meta, shared: true };