///| 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, }) }