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)
+14 -17
View File
@@ -137,7 +137,9 @@ struct SurfaceState {
/// without a new attach (a client changing only its regions or geometry) neither shows the
/// old buffer again nor releases it a second time: the client may already be drawing into it.
mut attached_buffer : UInt?
mut pending_frame_cb : UInt?
/// Every wl_surface.frame callback since the last commit; a client may queue several (Zed
/// and Mesa's EGL each do) and waits on all of them.
pending_frame_cbs : Array[UInt]
mut xdg_surface_id : UInt?
mut window_id : UInt64?
/// wl_surface.set_buffer_scale: the buffer is this many times the window's size.
@@ -485,7 +487,7 @@ fn WaylandServer::next_serial(self : WaylandServer) -> UInt {
}
///|
fn emit_registry_globals(c : ClientConn, reg_id : UInt, dmabuf : Bool) -> Unit {
fn emit_registry_globals(c : ClientConn, reg_id : UInt) -> Unit {
let globals : Array[(UInt, String, UInt)] = [
(1U, "wl_compositor", 5U),
(2U, "wl_shm", 1U),
@@ -499,10 +501,8 @@ fn emit_registry_globals(c : ClientConn, reg_id : UInt, dmabuf : Bool) -> Unit {
(10U, "org_kde_kwin_server_decoration_manager", 1U),
(11U, "zwp_primary_selection_device_manager_v1", 1U),
(12U, "zwp_text_input_manager_v3", 1U),
(13U, "zwp_linux_dmabuf_v1", 4U),
]
if dmabuf {
globals.push((13U, "zwp_linux_dmabuf_v1", 4U))
}
for g in globals {
let w = WireWriter::new()
w.u32(g.0)
@@ -698,7 +698,7 @@ fn WaylandServer::dispatch_msg(
// get_registry(registry: new_id)
let reg_id = reader.u32()
c.objects.set(reg_id, ObjRegistry)
emit_registry_globals(c, reg_id, self.dmabuf_formats().1.length() > 0)
emit_registry_globals(c, reg_id)
}
ObjRegistry =>
if opcode == 0U {
@@ -752,7 +752,7 @@ fn WaylandServer::dispatch_msg(
c.objects.set(sid, ObjSurface(sid))
c.surfaces.set(sid, {
attached_buffer: None,
pending_frame_cb: None,
pending_frame_cbs: [],
xdg_surface_id: None,
window_id: None,
buffer_scale: 1,
@@ -801,7 +801,7 @@ fn WaylandServer::dispatch_msg(
} else if opcode == 3U {
// frame(callback)
let cb_id = reader.u32()
surf.pending_frame_cb = Some(cb_id)
surf.pending_frame_cbs.push(cb_id)
} else if opcode == 10U {
// offset(x, y) (version 5): moves the surface; for a drag icon, its hotspot.
surf.pend_dx = surf.pend_dx + reader.i32()
@@ -898,17 +898,14 @@ fn WaylandServer::dispatch_msg(
None => ()
}
surf.attached_buffer = None
match surf.pending_frame_cb {
Some(cb_id) => {
surf.pending_frame_cb = None
if self.hold_frames {
self.held_frames.push((c.client_id, cb_id))
} else {
self.frame_done(c, cb_id)
}
for cb_id in surf.pending_frame_cbs {
if self.hold_frames {
self.held_frames.push((c.client_id, cb_id))
} else {
self.frame_done(c, cb_id)
}
None => ()
}
surf.pending_frame_cbs.clear()
}
}
ObjSubcompositor =>