Draw on the GPU: the desktop with OpenGL ES, apps through linux-dmabuf

The host's rasteriser has a GPU half (raster_gpu.c): the same primitives, drawn the same way, on
machines with a real GPU and OpenGL ES 3, straight into the display's buffers in a login session.
EGL and GLES are loaded at run time; without them everything is drawn on the CPU as before
(CHANDRA_GPU=0 does that too). zwp_linux_dmabuf_v1 lets apps draw on the GPU as well.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Opus 5.5 committed 2026-11-30 16:42:22 -08:00
1 parent d81003f176
commit cfb25fe699
26 files changed
+2485 -105

No files matched your search

+296
View File
@@ -0,0 +1,296 @@
///| zwp_linux_dmabuf_v1 (version 4): apps that draw on the GPU hand over the buffer they drew into,
/// 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.
///| What the server needs of the host's GPU (a fake one in tests).
pub(all) struct DmabufHost {
/// The dev_t of the GPU clients should draw on, and the (fourcc, modifier) pairs it reads; no
/// pairs when there is none.
support : () -> (UInt64, Array[(UInt, UInt64)])
/// (planes as (fd, offset, stride), modifier, width, height, fourcc, upside down) -> id, 0 when
/// the GPU can't read it. The fds stay the caller's.
import_buffer : (Array[(Int, UInt, UInt)], UInt64, Int, Int, UInt, Bool) -> UInt
/// What the buffer holds now, as a host surface id; 0 on failure.
capture : (UInt) -> UInt
release : (UInt) -> Unit
}
///|
fn DmabufHost::system() -> DmabufHost {
{
support: () => @sys.dmabuf_support(),
import_buffer: (p, m, w, h, f, y) => @sys.dmabuf_import(p, m, w, h, f, y),
capture: id => @sys.dmabuf_capture(id),
release: id => @sys.dmabuf_release(id),
}
}
///| A zwp_linux_buffer_params_v1: the planes added so far (fd, plane, offset, stride).
struct DmabufParams {
planes : Array[(Int, UInt, UInt, UInt)]
mut modifier : UInt64
mut used : Bool
}
///| A wl_buffer made from dmabufs. `id` is the host's (0: the GPU can't read it, it shows nothing).
struct DmabufBuffer {
id : UInt
width : Int
height : Int
opaque : Bool
}
///| DRM_FORMAT_XRGB8888 and XBGR8888: no alpha.
fn fourcc_opaque(f : UInt) -> Bool {
f == 0x34325258U || f == 0x34324258U
}
///|
fn DmabufParams::close_fds(self : DmabufParams) -> Unit {
for p in self.planes {
@sys.close_fd(p.0)
}
self.planes.clear()
}
///|
fn ClientConn::drop_dmabufs(self : ClientConn, host : DmabufHost) -> Unit {
for _, p in self.dmabuf_params {
p.close_fds()
}
for _, b in self.dmabuf_buffers {
if b.id != 0U {
(host.release)(b.id)
}
}
self.dmabuf_params.clear()
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)]) {
match self.dmabuf_support {
Some(s) => s
None => {
let s = (self.dmabuf_host.support)()
self.dmabuf_support = Some(s)
s
}
}
}
///| What a client that bound the global learns: before version 4 the formats right away (after it
/// a feedback object tells, below).
fn WaylandServer::dmabuf_bound(self : WaylandServer, c : ClientConn, id : UInt, ver : UInt) -> Unit {
if ver >= 4U {
return
}
let told : Map[UInt, Bool] = {}
for f in self.dmabuf_formats().1 {
if ver >= 3U {
let w = WireWriter::new()
w.u32(f.0)
w.u32((f.1 >> 32).to_uint())
w.u32((f.1 & 0xffffffffUL).to_uint())
c.send_msg(w.finish_msg(id, 1U)) // modifier
} else if !told.contains(f.0) {
told.set(f.0, true)
let w = WireWriter::new()
w.u32(f.0)
c.send_msg(w.finish_msg(id, 0U)) // format
}
}
}
///|
fn dev_array(dev : UInt64) -> Array[Byte] {
Array::makei(8, i => ((dev >> (i * 8)) & 0xffUL).to_byte())
}
///| zwp_linux_dmabuf_feedback_v1: the GPU to draw on and the formats to draw in, one tranche. The
/// same for every surface.
fn WaylandServer::dmabuf_feedback(self : WaylandServer, c : ClientConn, id : UInt) -> Unit {
let (dev, formats) = self.dmabuf_formats()
let table : Array[Byte] = []
let indices : Array[Byte] = []
for i, f in formats {
for k = 0; k < 4; k = k + 1 {
table.push(((f.0 >> (k * 8)) & 0xffU).to_byte())
}
for _ in 0..<4 {
table.push(b'\x00')
}
table.append(dev_array(f.1))
indices.push((i & 0xff).to_byte())
indices.push((i >> 8).to_byte())
}
let fd = @sys.memfd_create("chandra-dmabuf-formats", table.length())
if fd < 0 {
return
}
ignore(@sys.pwrite_bytes(fd, Bytes::from_array(table), 0))
let w = WireWriter::new()
w.u32(table.length().reinterpret_as_uint())
if c.alive {
ignore(@sys.sendmsg_fd(c.fd, w.finish_msg(id, 1U), fd)) // format_table
}
@sys.close_fd(fd)
let w = WireWriter::new()
w.array(dev_array(dev))
c.send_msg(w.finish_msg(id, 2U)) // main_device
let w = WireWriter::new()
w.array(dev_array(dev))
c.send_msg(w.finish_msg(id, 4U)) // tranche_target_device
let w = WireWriter::new()
w.u32(0U)
c.send_msg(w.finish_msg(id, 6U)) // tranche_flags
let w = WireWriter::new()
w.array(indices)
c.send_msg(w.finish_msg(id, 5U)) // tranche_formats
c.send_msg(WireWriter::new().finish_msg(id, 3U)) // tranche_done
c.send_msg(WireWriter::new().finish_msg(id, 0U)) // done
}
///| A request on the zwp_linux_dmabuf_v1 `obj_id`.
fn WaylandServer::dmabuf_request(
self : WaylandServer,
c : ClientConn,
obj_id : UInt,
opcode : UInt,
reader : WireReader,
) -> Unit {
match opcode {
0U => {
c.objects.remove(obj_id)
send_delete_id(c, obj_id)
}
1U => {
// create_params(id)
let id = reader.u32()
c.objects.set(id, ObjDmabufParams(id))
c.dmabuf_params.set(id, { planes: [], modifier: 0UL, used: false })
}
2U | 3U => {
// get_default_feedback(id), get_surface_feedback(id, surface)
let id = reader.u32()
c.objects.set(id, ObjDmabufFeedback)
self.dmabuf_feedback(c, id)
}
_ => ()
}
}
///| A request on the zwp_linux_buffer_params_v1 `id`.
fn WaylandServer::dmabuf_params_request(
self : WaylandServer,
c : ClientConn,
id : UInt,
opcode : UInt,
reader : WireReader,
) -> Unit {
guard c.dmabuf_params.get(id) is Some(p) else { return }
match opcode {
0U => {
p.close_fds()
c.dmabuf_params.remove(id)
c.objects.remove(id)
send_delete_id(c, id)
}
1U => {
// add(fd: out-of-band, plane_idx, offset, stride, modifier_hi, modifier_lo)
let fd = c.take_fd()
let plane = reader.u32()
let offset = reader.u32()
let stride = reader.u32()
let hi = reader.u32()
let lo = reader.u32()
if fd < 0 {
return
}
if p.used || plane > 3U || p.planes.iter().any(q => q.1 == plane) {
@sys.close_fd(fd)
return
}
p.planes.push((fd, plane, offset, stride))
p.modifier = (hi.to_uint64() << 32) | lo.to_uint64()
}
2U | 3U => {
// create(width, height, format, flags), create_immed(buffer_id, width, height, format, flags)
let immed = opcode == 3U
let want = if immed { reader.u32() } else { 0U }
let width = reader.i32()
let height = reader.i32()
let fourcc = reader.u32()
let flags = reader.u32()
p.planes.sort_by((a, b) => a.1.compare(b.1))
let planes = p.planes.map(q => (q.0, q.2, q.3))
let contiguous = p.planes.length() > 0 &&
p.planes[p.planes.length() - 1].1.reinterpret_as_int() == p.planes.length() - 1
// flags: 1 upside down, 2 interlaced, 4 bottom field first.
let got = if p.used || !contiguous || width <= 0 || height <= 0 || (flags & 6U) != 0U {
0U
} else {
(self.dmabuf_host.import_buffer)(planes, p.modifier, width, height, fourcc, (flags & 1U) != 0U)
}
p.used = true
p.close_fds()
if got == 0U {
@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)",
)
if !immed {
c.send_msg(WireWriter::new().finish_msg(id, 1U)) // failed
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 !immed {
let w = WireWriter::new()
w.u32(buf)
c.send_msg(w.finish_msg(id, 0U)) // created
}
}
_ => ()
}
}
///| The wl_buffer `buf_id` was destroyed.
fn WaylandServer::dmabuf_buffer_gone(self : WaylandServer, c : ClientConn, buf_id : UInt) -> Unit {
match c.dmabuf_buffers.get(buf_id) {
Some(b) => {
if b.id != 0U {
(self.dmabuf_host.release)(b.id)
}
c.dmabuf_buffers.remove(buf_id)
}
None => ()
}
}
///| What the dmabuf buffer `b` holds now, as `snapshot_buffer` gives it.
fn WaylandServer::snapshot_dmabuf(self : WaylandServer, b : DmabufBuffer) -> @scene.ShmBufferSnapshot? {
let id = if b.id == 0U { 0U } else { (self.dmabuf_host.capture)(b.id) }
if id == 0U {
return None
}
Some({
width: b.width,
height: b.height,
stride: b.width * 4,
format: if b.opaque { 1 } else { 0 },
bytes: Bytes::make(0, b'\x00'),
host_id: id,
})
}
+1 -1
View File
@@ -509,7 +509,7 @@ fn WaylandServer::dnd_icon_commit(
}
match surf.attached_buffer {
Some(bid) =>
match snapshot_shm_buffer(c, bid) {
match self.snapshot_buffer(c, bid) {
Some(snap) => events.push(DragIcon(Some(snap), d.icon_x, d.icon_y))
None => ()
}
+40 -6
View File
@@ -106,6 +106,10 @@ enum ObjKind {
/// zwp_text_input_manager_v3 and its zwp_text_input_v3 objects (text_input.mbt).
ObjTextInputMgr
ObjTextInput
/// zwp_linux_dmabuf_v1, a zwp_linux_buffer_params_v1 and a feedback object (dmabuf.mbt).
ObjDmabuf
ObjDmabufParams(UInt)
ObjDmabufFeedback
}
///|
@@ -223,6 +227,8 @@ struct ClientConn {
surfaces : Map[UInt, SurfaceState]
shm_pools : Map[UInt, ShmPoolState]
shm_buffers : Map[UInt, ShmBufferState]
dmabuf_params : Map[UInt, DmabufParams]
dmabuf_buffers : Map[UInt, DmabufBuffer]
xdg_surfaces : Map[UInt, XdgSurfaceState]
subsurfaces : Map[UInt, SubState]
toplevels : Map[UInt64, ToplevelState]
@@ -267,6 +273,8 @@ fn ClientConn::new(client_id : UInt, fd : Int) -> ClientConn {
surfaces: {},
shm_pools: {},
shm_buffers: {},
dmabuf_params: {},
dmabuf_buffers: {},
xdg_surfaces: {},
subsurfaces: {},
toplevels: {},
@@ -384,6 +392,9 @@ pub(all) struct WaylandServer {
/// so clients stop drawing frames nobody sees: (client id, callback id), sent on release.
mut hold_frames : Bool
held_frames : Array[(UInt, UInt)]
/// Client buffers on the GPU (dmabuf.mbt): the host's side, and what it supports once asked.
mut dmabuf_host : DmabufHost
mut dmabuf_support : (UInt64, Array[(UInt, UInt64)])?
}
///|
@@ -422,6 +433,8 @@ pub fn WaylandServer::listen(
drop_pending: None,
hold_frames: false,
held_frames: [],
dmabuf_host: DmabufHost::system(),
dmabuf_support: None,
})
}
@@ -456,6 +469,7 @@ pub fn WaylandServer::close(self : WaylandServer) -> Unit {
}
self.transfers.clear()
for c in self.clients {
c.drop_dmabufs(self.dmabuf_host)
c.close_all()
}
self.clients.clear()
@@ -471,7 +485,7 @@ fn WaylandServer::next_serial(self : WaylandServer) -> UInt {
}
///|
fn emit_registry_globals(c : ClientConn, reg_id : UInt) -> Unit {
fn emit_registry_globals(c : ClientConn, reg_id : UInt, dmabuf : Bool) -> Unit {
let globals : Array[(UInt, String, UInt)] = [
(1U, "wl_compositor", 5U),
(2U, "wl_shm", 1U),
@@ -486,6 +500,9 @@ fn emit_registry_globals(c : ClientConn, reg_id : UInt) -> Unit {
(11U, "zwp_primary_selection_device_manager_v1", 1U),
(12U, "zwp_text_input_manager_v3", 1U),
]
if dmabuf {
globals.push((13U, "zwp_linux_dmabuf_v1", 4U))
}
for g in globals {
let w = WireWriter::new()
w.u32(g.0)
@@ -602,10 +619,15 @@ fn send_decoration_mode(c : ClientConn, deco_id : UInt, mode : UInt) -> Unit {
}
///|
fn snapshot_shm_buffer(
fn WaylandServer::snapshot_buffer(
self : WaylandServer,
c : ClientConn,
buf_id : UInt,
) -> @scene.ShmBufferSnapshot? {
match c.dmabuf_buffers.get(buf_id) {
Some(b) => return self.snapshot_dmabuf(b)
None => ()
}
let buf = match c.shm_buffers.get(buf_id) {
Some(b) => b
None => return None
@@ -676,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)
emit_registry_globals(c, reg_id, self.dmabuf_formats().1.length() > 0)
}
ObjRegistry =>
if opcode == 0U {
@@ -713,6 +735,9 @@ fn WaylandServer::dispatch_msg(
c.objects.set(new_id, ObjPrimaryManager)
} else if iface == "zwp_text_input_manager_v3" {
c.objects.set(new_id, ObjTextInputMgr)
} else if iface == "zwp_linux_dmabuf_v1" {
c.objects.set(new_id, ObjDmabuf)
self.dmabuf_bound(c, new_id, ver)
} else if iface == "wl_subcompositor" {
c.objects.set(new_id, ObjSubcompositor)
} else if iface == "wl_output" {
@@ -798,7 +823,7 @@ fn WaylandServer::dispatch_msg(
} else {
match surf.attached_buffer {
Some(bid) =>
match snapshot_shm_buffer(c, bid) {
match self.snapshot_buffer(c, bid) {
Some(snap) => events.push(SubsurfaceCommitted(wid, sid, ax, ay, Some(snap)))
None => ()
}
@@ -815,7 +840,7 @@ fn WaylandServer::dispatch_msg(
Some(tl) =>
if tl.popup_parent is Some(_) {
let snap = match surf.attached_buffer {
Some(bid) => snapshot_shm_buffer(c, bid)
Some(bid) => self.snapshot_buffer(c, bid)
None => None
}
events.push(PopupCommitted(wid, snap))
@@ -832,7 +857,7 @@ fn WaylandServer::dispatch_msg(
)
} else {
let snap = match surf.attached_buffer {
Some(bid) => snapshot_shm_buffer(c, bid)
Some(bid) => self.snapshot_buffer(c, bid)
None => None
}
if snap is Some(_) {
@@ -1213,6 +1238,7 @@ fn WaylandServer::dispatch_msg(
None => None
}
c.shm_buffers.remove(buf_id)
self.dmabuf_buffer_gone(c, buf_id)
c.objects.remove(buf_id)
send_delete_id(c, buf_id)
match pool_id {
@@ -1220,6 +1246,13 @@ fn WaylandServer::dispatch_msg(
None => ()
}
}
ObjDmabuf => self.dmabuf_request(c, obj_id, opcode, reader)
ObjDmabufParams(id) => self.dmabuf_params_request(c, id, opcode, reader)
ObjDmabufFeedback =>
if opcode == 0U {
c.objects.remove(obj_id)
send_delete_id(c, obj_id)
}
ObjXdgWmBase =>
if opcode == 0U {
c.objects.remove(obj_id)
@@ -1723,6 +1756,7 @@ pub fn WaylandServer::poll_events(
self.win_to_client.remove(wid)
events.push(ToplevelClosed(wid))
}
c.drop_dmabufs(self.dmabuf_host)
c.close_all()
self.dnd_client_gone(c.client_id, events)
if self.kbd_client == Some(c.client_id) {