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): republish a frame that found no free slot, and drop the fast cadence on read-back

EtienneLescot33a68560866d
diff --git a/crates/compositor/src/live.rs b/crates/compositor/src/live.rs
index cbed49464..8fd6a29e0 100644
--- a/crates/compositor/src/live.rs
+++ b/crates/compositor/src/live.rs
@@ -1472,6 +1472,10 @@ unsafe fn render_thread(
let mut scene_applied = false;
// Textures partagées de la preview, créées au premier besoin (voir `publish_shared`).
let mut ring = Ring::default();
+ // Une frame composée qui n'a trouvé aucune case libre dans l'anneau : le RT la garde, elle
+ // repart dès qu'une case se libère. Sans ça, une frame composée en pause (seek, réglage)
+ // restait invisible jusqu'au changement suivant — en lecture, la suivante la remplace.
+ let mut publish_pending = false;
while !shared.stop.load(Ordering::SeqCst) {
// Le transport vient de changer : le consommateur doit recevoir la frame courante
@@ -1834,13 +1838,19 @@ unsafe fn render_thread(
stepped = true;
}
- if stepped || first {
+ if stepped || first || publish_pending {
if pw > 0 && ph > 0 {
match publish_shared(&shared, &gpu, &comp, &mut ring) {
- SharedPublish::Published => first = false,
- // Chromium tient toutes les cases : la frame est sautée, et la boucle ne
- // tourne pas à vide le temps qu'il en relâche une.
- SharedPublish::NoFreeSlot => std::thread::sleep(Duration::from_millis(2)),
+ SharedPublish::Published => {
+ first = false;
+ publish_pending = false;
+ }
+ // Chromium tient toutes les cases : la frame attend dans le RT, et la boucle
+ // ne tourne pas à vide le temps qu'il en relâche une.
+ SharedPublish::NoFreeSlot => {
+ publish_pending = true;
+ std::thread::sleep(Duration::from_millis(2));
+ }
// Step complet : `compose_frame` (déjà appelé par `step`/`present_frame`/
// `recompose`) a rastérisé le RT à la géométrie de sortie ramenée au panneau.
// On lit ce RT DIRECTEMENT à sa résolution de rendu (`readback_direct` : copy
@@ -1867,6 +1877,7 @@ unsafe fn render_thread(
*slot = Some((next_gen, rw, rh, rgba, comp.footage_quad()));
}
first = false;
+ publish_pending = false;
}
Err(e) => {
eprintln!("[live] readback_direct: {e:#}");
diff --git a/src/native/hooks/useNativeCompositorView.test.ts b/src/native/hooks/useNativeCompositorView.test.ts
index 7d2b0e756..4949bb5b7 100644
--- a/src/native/hooks/useNativeCompositorView.test.ts
+++ b/src/native/hooks/useNativeCompositorView.test.ts
@@ -527,6 +527,50 @@ describe("useNativeCompositorView", () => {
}
});
+ // The service turns shared textures off for a view whose import or send failed: its next
+ // frames are read-back pixels, and the fast cadence must not outlive the transport.
+ it("drops back to ~30 pulls a second once the view falls back to read-back", async () => {
+ const run = manualFrames();
+ vi.stubGlobal(
+ "ImageData",
+ class {
+ constructor(
+ public data: Uint8ClampedArray,
+ public width: number,
+ public height: number,
+ ) {}
+ },
+ );
+ vi.stubGlobal(
+ "createImageBitmap",
+ vi.fn(async () => ({ close: vi.fn() })),
+ );
+ try {
+ let gen = 0;
+ mocks.readCompositorFrame.mockImplementation(async () => ({
+ ...sharedMeta({ gen: ++gen }),
+ shared: true,
+ }));
+ await mountedView();
+ mocks.sharedListener?.(fakeVideoFrame(), sharedMeta({ gen: 1 }));
+ await run(300, 1000 / 280);
+
+ mocks.readCompositorFrame.mockImplementation(async () => ({
+ gen: ++gen,
+ width: 2,
+ height: 1,
+ data: new Uint8Array(8),
+ }));
+ await run(100, 1000 / 280, 300);
+ mocks.readCompositorFrame.mockClear();
+ await run(500, 1000 / 280, 400);
+
+ expect(mocks.readCompositorFrame.mock.calls.length).toBeLessThanOrEqual(17);
+ } finally {
+ vi.unstubAllGlobals();
+ }
+ });
+
// Read-back frames bound the copies: the fast cadence is for shared textures only.
it("keeps read-back frames at ~30 pulls a second even while they keep coming", async () => {
const run = manualFrames();
diff --git a/src/native/hooks/useNativeCompositorView.ts b/src/native/hooks/useNativeCompositorView.ts
index d42fcfa89..94688c2bf 100644
--- a/src/native/hooks/useNativeCompositorView.ts
+++ b/src/native/hooks/useNativeCompositorView.ts
@@ -289,8 +289,12 @@ export function useNativeCompositorView(
readCompositorFrame(id, lastGen)
.then((frame) => {
inFlight = false;
- if (frame) {
+ // A receipt keeps the fast cadence going. Read-back pixels mean the view went back to
+ // read-back (a failed import or send), whose frames are pulled ~30 times a second.
+ if (frame && "shared" in frame) {
lastFrameAt = now;
+ } else if (frame) {
+ sharedTransport = false;
}
// `null` = nothing newer than `lastGen` (idle path — no pixels
// crossed IPC) OR no frame yet. Either way, leave the canvas as-is.