Merge main: one linux-dmabuf, imported on the GPU or mapped without one

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Opus 5.5 committed 2026-11-30 16:47:03 -08:00
commit d478f58d9b
54 files changed
+1110 -376

No files matched your search

+80 -17
View File
@@ -2,9 +2,10 @@
/// and never copy a frame through shared memory. Without it Mesa's EGL and Vulkan fall back to
/// drawing on the CPU, whatever GPU the machine has.
///
/// The buffers live in the host (`@sys.dmabuf_*`); here are only ids. At each commit the host
/// copies the buffer on the GPU and it is released at once, as an shm buffer is. The global is
/// only advertised when the host draws on a GPU that reads such buffers.
/// When the host draws on a GPU, buffers of any layout it samples are imported there; here are only
/// ids. At each commit the host copies the buffer on the GPU and it is released at once, as an shm
/// buffer is. When it draws on the CPU (and on the native build), only linear ARGB8888 and
/// XRGB8888 buffers are taken: they are read by mapping them, like a one-buffer shm pool.
///| What the server needs of the host's GPU (a fake one in tests).
pub(all) struct DmabufHost {
@@ -71,12 +72,43 @@ fn ClientConn::drop_dmabufs(self : ClientConn, host : DmabufHost) -> Unit {
self.dmabuf_buffers.clear()
}
///| The formats the GPU reads, asked of the host once.
pub fn WaylandServer::dmabuf_formats(self : WaylandServer) -> (UInt64, Array[(UInt, UInt64)]) {
///| The render node's dev_t, from /sys/class/drm/renderD*/dev ("226:128"); 0 when there is none.
fn render_node_dev() -> UInt64 {
for minor = 128; minor < 192; minor = minor + 1 {
let txt = @sys.read_proc_bytes("/sys/class/drm/renderD" + minor.to_string() + "/dev")
if txt.length() == 0 {
continue
}
let mut major = 0
let mut mnr = 0
let mut after = false
for b in txt {
let b = b.to_int()
if b == 58 {
after = true
} else if b >= 48 && b <= 57 {
if after {
mnr = mnr * 10 + (b - 48)
} else {
major = major * 10 + (b - 48)
}
}
}
// glibc makedev() for a 12-bit major and an 8-bit minor (a render node's).
return (((major & 0xfff) << 8) | (mnr & 0xff)).to_uint64()
}
0UL
}
///| The formats the GPU reads, asked of the host once; without one those read by mapping.
fn WaylandServer::dmabuf_formats(self : WaylandServer) -> (UInt64, Array[(UInt, UInt64)]) {
match self.dmabuf_support {
Some(s) => s
None => {
let s = (self.dmabuf_host.support)()
let s = match (self.dmabuf_host.support)() {
(_, []) => (render_node_dev(), [(0x34325241U, 0UL), (0x34325258U, 0UL)])
gpu => gpu
}
self.dmabuf_support = Some(s)
s
}
@@ -140,10 +172,11 @@ fn WaylandServer::dmabuf_feedback(self : WaylandServer, c : ClientConn, id : UIn
}
@sys.close_fd(fd)
let w = WireWriter::new()
w.array(dev_array(dev))
let dev = if dev == 0UL { [] } else { dev_array(dev) }
w.array(dev)
c.send_msg(w.finish_msg(id, 2U)) // main_device
let w = WireWriter::new()
w.array(dev_array(dev))
w.array(dev)
c.send_msg(w.finish_msg(id, 4U)) // tranche_target_device
let w = WireWriter::new()
w.u32(0U)
@@ -184,6 +217,33 @@ fn WaylandServer::dmabuf_request(
}
}
///| Take a linear buffer to be read by mapping it: it owns the fd, like a one-buffer pool that is
/// already destroyed. False when it isn't one.
fn ClientConn::map_dmabuf(
self : ClientConn,
p : DmabufParams,
buf : UInt,
width : Int,
height : Int,
fourcc : UInt,
) -> Bool {
guard p.planes is [(fd, 0U, offset, stride)] else { return false }
let format = match fourcc {
0x34325241U => 0
0x34325258U => 1
_ => return false
}
let offset = offset.reinterpret_as_int()
let stride = stride.reinterpret_as_int()
if p.modifier != 0UL || width <= 0 || height <= 0 || offset < 0 || stride < width * 4 {
return false
}
p.planes.clear()
self.shm_pools.set(buf, { fd, size: offset + stride * height, destroyed: true })
self.shm_buffers.set(buf, { pool_id: buf, offset, width, height, stride, format })
true
}
///| A request on the zwp_linux_buffer_params_v1 `id`.
fn WaylandServer::dmabuf_params_request(
self : WaylandServer,
@@ -236,9 +296,17 @@ fn WaylandServer::dmabuf_params_request(
} else {
(self.dmabuf_host.import_buffer)(planes, p.modifier, width, height, fourcc, (flags & 1U) != 0U)
}
let buf = if immed {
want
} else {
let b = c.next_offer // any id of the server's
c.next_offer = b + 1U
b
}
let mapped = got == 0U && !p.used && flags == 0U && c.map_dmabuf(p, buf, width, height, fourcc)
p.used = true
p.close_fds()
if got == 0U {
if got == 0U && !mapped {
@sys.log(
"dmabuf: can't use a client's buffer (\{width}x\{height}, format 0x\{fourcc.to_string(radix=16)}, modifier 0x\{p.modifier.to_string(radix=16)}, \{planes.length()} planes)",
)
@@ -247,15 +315,10 @@ fn WaylandServer::dmabuf_params_request(
return
}
}
let buf = if immed {
want
} else {
let b = c.next_offer // any id of the server's
c.next_offer = b + 1U
b
}
c.objects.set(buf, ObjBuffer(buf))
c.dmabuf_buffers.set(buf, { id: got, width, height, opaque: fourcc_opaque(fourcc) })
if !mapped {
c.dmabuf_buffers.set(buf, { id: got, width, height, opaque: fourcc_opaque(fourcc) })
}
if !immed {
let w = WireWriter::new()
w.u32(buf)