voelin-media: fix Wayland screen capture delivering no frames at all

Measured on a real 2560x1440 Wayland desktop through the ScreenCast portal,
which nothing here had ever run against: every captured frame was dropped
and the viewer got nothing.

Two faults, one behind the other.

The mapping length came from `mapoffset + maxsize`. Those describe a
mapping, which only shared memory has; a DMA-BUF's size lives in its buffer
object, and this compositor leaves both fields 0 (the file descriptor and
the 10240-byte stride were fine). The length was therefore 0, every map was
refused, and the capture delivered nothing for as long as it ran. Derive the
length from the rows the negotiated format describes, which is exactly what
is read afterwards.

With frames flowing, the second fault showed: reading a LINEAR DMA-BUF from
this GPU's memory took 11.95 ms per frame and 19.5 CPU cores at 1440p60,
against 0.27 ms and 0.41 cores for the same capture over shared memory — 44
times the time, 48 times the CPU. The guard that exists for this switches at
6.0 ns per pixel and the DMA-BUF path only reaches 3.24, so it never fired.
Put the threshold between the two measurements, an order of magnitude above
the shared-memory figure so a GPU whose buffers the CPU can read keeps them.

After both: 2560x1440 at 60.0 fps on 0.55 cores, no dropped frames, the
bitrate honoured. A DMA-BUF that cannot be mapped at all now also falls back
to shared memory after a few failures instead of dropping frames forever.

`stream bench` was reporting the layer size from --res, which is not what a
real source encodes; it now prints the size that was really encoded.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
faumarayandClaude Opus 5 committed 2026-09-29 23:50:52 +03:00
1 parent 91bca224d7
commit 9b09e7bde2
2 files changed
+56 -4

No files matched your search

+49 -3
View File
@@ -248,9 +248,23 @@ const MODIFIER_LINEAR: i64 = 0;
/// Reading DMA-BUFs slower than this (per pixel, conversion included) means
/// the buffers live in memory the CPU reads uncached (e.g. a discrete GPU's
/// VRAM); shared memory is faster then.
const SLOW_DMABUF_NS_PER_PIXEL: f64 = 6.0;
///
/// Measured on a Radeon RX 7900 GRE capturing a 2560x1440 desktop at 60 fps,
/// the same run through both paths: a LINEAR DMA-BUF took 11.95 ms per frame
/// (3.24 ns per pixel) and 19.5 CPU cores, shared memory 0.27 ms (0.07 ns
/// per pixel) and 0.41 cores — 44 times the time and 48 times the CPU. The
/// threshold was 6.0, which that does not reach, so the switch never
/// happened and a discrete GPU burned twenty cores on screen sharing. It
/// sits between the two figures, an order of magnitude above the fast path,
/// so a GPU whose buffers the CPU can read (anything with unified memory)
/// keeps them.
const SLOW_DMABUF_NS_PER_PIXEL: f64 = 1.0;
/// DMA-BUF frames timed before deciding.
const DMABUF_PROBE_FRAMES: u32 = 30;
/// DMA-BUFs that failed to map before shared memory is offered instead. A
/// few failures can be a buffer being replaced; more means this compositor's
/// buffers cannot be read this way at all.
const DMABUF_FAILURES_BEFORE_SHM: u32 = 5;
/// What was negotiated.
#[derive(Clone, Copy, Debug)]
@@ -275,6 +289,9 @@ struct VideoState {
/// Time spent handing DMA-BUF frames to the sink, and how many.
dmabuf_time: Duration,
dmabuf_frames: u32,
/// DMA-BUFs that could not be mapped (see
/// [`DMABUF_FAILURES_BEFORE_SHM`]).
dmabuf_failures: u32,
logged: Option<bool>,
mainloop: pw::main_loop::MainLoopWeak,
}
@@ -303,6 +320,7 @@ fn video_stream(
maps: Vec::new(),
dmabuf_time: Duration::ZERO,
dmabuf_frames: 0,
dmabuf_failures: 0,
logged: None,
mainloop: mainloop.downgrade(),
};
@@ -600,14 +618,42 @@ fn process(stream: &pw::stream::Stream, state: &mut VideoState) {
let started = Instant::now();
let more = if is_dmabuf {
let raw = data.as_raw();
let len = (raw.mapoffset + raw.maxsize) as usize;
// A DMA-BUF's size lives in its buffer object, not in `maxsize`:
// `maxsize` describes a mapping, which only shared memory has, and
// compositors leave it 0 (measured on a 2560x1440 Wayland desktop:
// fd valid, stride 10240, `mapoffset` and `maxsize` both 0). Mapping
// `mapoffset + maxsize` bytes then asks for a zero-length mapping
// and every frame is dropped, so fall back to the rows the
// negotiated format describes, which is exactly what is read below.
let len = match (raw.mapoffset + raw.maxsize) as usize {
0 => raw.mapoffset as usize + needed,
size => size,
};
let fd = data.fd();
if !state.maps.iter().any(|m| m.fd() == fd && m.len() >= len) {
state.maps.retain(|m| m.fd() != fd);
match DmaBufMap::new(fd, len) {
Ok(map) => state.maps.push(map),
Err(e) => {
warn!("cannot map a screen capture DMA-BUF: {e}");
warn!(
fd,
len,
mapoffset = raw.mapoffset,
maxsize = raw.maxsize,
stride,
height = format.height,
"cannot map a screen capture DMA-BUF: {e}"
);
// Dropping every frame would leave the viewer a black
// screen; shared memory always works.
state.dmabuf_failures += 1;
if state.dmabuf_failures >= DMABUF_FAILURES_BEFORE_SHM && state.dmabuf {
tracing::info!(
"screen capture DMA-BUFs cannot be mapped; switching to shared memory"
);
state.dmabuf = false;
renegotiate(stream, state.fps, false);
}
return;
}
}
+7 -1
View File
@@ -423,7 +423,13 @@ fn bench(args: BenchArgs) -> Result<()> {
let frames = b.frames - a.frames;
output_frames += frames;
let spec = layers.iter().find(|l| l.id == *id);
let size = spec.map_or((width, height), |l| l.output_size(width, height));
let layer_stats = stats.layers.iter().find(|l| l.id == *id);
// What was really encoded: with a real source the size comes from the
// capture, not from --res.
let size = match layer_stats.filter(|l| l.width > 0) {
Some(l) => (l.width, l.height),
None => spec.map_or((width, height), |l| l.output_size(width, height)),
};
let dropped = |s: &Sample| s.dropped.iter().find(|d| d.0 == *id).map_or(0, |d| d.1);
let layer = stats.layers.iter().find(|l| l.id == *id);
println!(