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:
1 parent
d81003f176
commit
cfb25fe699
26 files changed
+2485
-105
No files matched your search
@@ -0,0 +1,106 @@
|
||||
///| zwp_linux_dmabuf_v1: what an app that draws on the GPU asks for, against a pretend GPU.
|
||||
|
||||
///|
|
||||
struct FakeGpu {
|
||||
imported : Array[(Int, UInt64, Int, Int, UInt, Bool)]
|
||||
captured : Array[UInt]
|
||||
released : Array[UInt]
|
||||
}
|
||||
|
||||
///|
|
||||
const XR24 : UInt = 0x34325258U
|
||||
|
||||
///|
|
||||
fn with_fake_gpu(comp : @host.ChandraCompositor) -> FakeGpu {
|
||||
let g : FakeGpu = { imported: [], captured: [], released: [] }
|
||||
comp.server.dmabuf_host = {
|
||||
support: () => (0xe280UL, [(XR24, 0UL), (XR24, 0x0100000000000001UL)]),
|
||||
import_buffer: (planes, modifier, w, h, fourcc, flip) => {
|
||||
g.imported.push((planes.length(), modifier, w, h, fourcc, flip))
|
||||
if fourcc == XR24 { 40U + g.imported.length().reinterpret_as_uint() } else { 0U }
|
||||
},
|
||||
capture: id => {
|
||||
g.captured.push(id)
|
||||
7000U + g.captured.length().reinterpret_as_uint()
|
||||
},
|
||||
release: id => g.released.push(id),
|
||||
}
|
||||
g
|
||||
}
|
||||
|
||||
///| create_params, add one plane, create_immed: the wl_buffer.
|
||||
fn dmabuf_buffer(c : TestWaylandClient, dmabuf : UInt, fourcc : UInt, flags : UInt) -> UInt {
|
||||
let params = c.alloc_id()
|
||||
let a = @wayland.WireWriter::new()
|
||||
a.u32(params)
|
||||
c.send(a.finish_msg(dmabuf, 1U))
|
||||
let fd = @sys.memfd_create("plane", 4096)
|
||||
let p = @wayland.WireWriter::new()
|
||||
p.u32(0U)
|
||||
p.u32(0U)
|
||||
p.u32(1280U)
|
||||
p.u32(0x01000000U)
|
||||
p.u32(1U)
|
||||
c.send(p.finish_msg(params, 1U), send_fd=fd)
|
||||
@sys.close_fd(fd)
|
||||
let buf = c.alloc_id()
|
||||
let b = @wayland.WireWriter::new()
|
||||
b.u32(buf)
|
||||
b.i32(320)
|
||||
b.i32(200)
|
||||
b.u32(fourcc)
|
||||
b.u32(flags)
|
||||
c.send(b.finish_msg(params, 3U))
|
||||
c.send(@wayland.WireWriter::new().finish_msg(params, 0U))
|
||||
buf
|
||||
}
|
||||
|
||||
///|
|
||||
test "dmabuf buffers show their window and are released at once" {
|
||||
let socket_path = "/tmp/wayland-moon-dmabuf-" + @sys.getpid().to_string() + ".sock"
|
||||
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap()
|
||||
let gpu = with_fake_gpu(comp)
|
||||
let (c, shm, surf, xdg, _tl, _deco) = open_and_configure_client(comp, socket_path, "T", "t")
|
||||
commit_shm_frame(comp, c, shm, surf, xdg, 1UL)
|
||||
let dmabuf = bind_global(c, 2U, "zwp_linux_dmabuf_v1", 4U)
|
||||
let buf = dmabuf_buffer(c, dmabuf, XR24, 1U)
|
||||
attach_commit(comp, c, surf, Some(buf))
|
||||
pump(comp, 3)
|
||||
assert_eq(gpu.imported.length(), 1)
|
||||
let (planes, modifier, w, h, fourcc, flip) = gpu.imported[0]
|
||||
assert_eq(planes, 1)
|
||||
assert_eq(modifier, 0x0100000000000001UL)
|
||||
assert_eq((w, h), (320, 200))
|
||||
assert_eq(fourcc, XR24)
|
||||
assert_true(flip)
|
||||
assert_eq(gpu.captured, [41U])
|
||||
let s = comp.windows.get(1UL).unwrap().surface.unwrap()
|
||||
assert_eq((s.width, s.height, s.format, s.host_id), (320, 200, 1, 7001U))
|
||||
// Destroying the wl_buffer forgets the import.
|
||||
c.send(@wayland.WireWriter::new().finish_msg(buf, 0U))
|
||||
pump(comp, 2)
|
||||
assert_eq(gpu.released, [41U])
|
||||
// A buffer the GPU can't read shows nothing and breaks nothing.
|
||||
let bad = dmabuf_buffer(c, dmabuf, 0x3231564eU, 0U)
|
||||
attach_commit(comp, c, surf, Some(bad))
|
||||
pump(comp, 3)
|
||||
assert_eq(gpu.imported.length(), 2)
|
||||
assert_eq(gpu.captured.length(), 1)
|
||||
assert_eq(comp.windows.get(1UL).unwrap().surface.unwrap().host_id, 7001U)
|
||||
// The client going away releases what it left.
|
||||
ignore(dmabuf_buffer(c, dmabuf, XR24, 0U))
|
||||
pump(comp, 2)
|
||||
assert_eq(gpu.imported.length(), 3)
|
||||
@sys.close_fd(c.fd)
|
||||
pump(comp, 3)
|
||||
assert_eq(gpu.released, [41U, 43U])
|
||||
comp.close()
|
||||
}
|
||||
|
||||
///|
|
||||
test "no dmabuf global without a GPU" {
|
||||
let socket_path = "/tmp/wayland-moon-nodmabuf-" + @sys.getpid().to_string() + ".sock"
|
||||
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap()
|
||||
assert_eq(comp.server.dmabuf_formats().1.length(), 0)
|
||||
comp.close()
|
||||
}
|
||||
@@ -130,6 +130,7 @@ struct fbuf {
|
||||
uint32_t handle, fb, pitch;
|
||||
size_t size;
|
||||
uint8_t *map;
|
||||
int dmabuf; /* 1 + its fd as a dmabuf (for a GPU to draw into), 0 not exported yet, -1 can't be */
|
||||
};
|
||||
|
||||
static int g_drm = -1;
|
||||
@@ -792,6 +793,9 @@ static void copy_rows(struct fbuf *b, const uint8_t *src, int w, int x0, int y0,
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_kms_present(moonbit_bytes_t pixels, int32_t w, int32_t h);
|
||||
|
||||
/* Show the back buffer (below). */
|
||||
int32_t mp_kms_flip(void);
|
||||
|
||||
/* Show an RGBA8888 frame of which only (dx, dy, dw, dh) changed since the last call: only what the
|
||||
* back buffer is missing is copied into it (the change now, and the one shown last time). */
|
||||
MOONBIT_FFI_EXPORT
|
||||
@@ -810,10 +814,53 @@ int32_t mp_kms_present_damage(moonbit_bytes_t pixels, int32_t w, int32_t h, int3
|
||||
int sx0 = s[0] < 0 ? 0 : s[0], sy0 = s[1] < 0 ? 0 : s[1];
|
||||
int sx1 = s[2] > cw ? cw : s[2], sy1 = s[3] > ch ? ch : s[3];
|
||||
if (sx0 < sx1 && sy0 < sy1) copy_rows(b, (const uint8_t *)pixels, w, sx0, sy0, sx1, sy1);
|
||||
return mp_kms_flip();
|
||||
}
|
||||
|
||||
/* ---- frames drawn by the GPU straight into the scanout buffers (the wasm host's rasteriser) ---- */
|
||||
|
||||
/* The frame changed there: every buffer is now missing it. */
|
||||
MOONBIT_FFI_EXPORT
|
||||
void mp_kms_add_damage(int32_t x, int32_t y, int32_t w, int32_t h) {
|
||||
if (w > 0 && h > 0) stale_add(x, y, x + w, y + h);
|
||||
}
|
||||
|
||||
/* The buffer to draw the next w x h frame into. Returns its index (0 or 1), -1 when no frame can be
|
||||
* shown now, -2 when the display's buffers can't be shared with a GPU. info: dmabuf fd, pitch,
|
||||
* width and height in pixels, scale, rotation; missing: x0, y0, x1, y1 of the frame it lacks. */
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_kms_back_buffer(int32_t w, int32_t h, int32_t *info, int32_t *missing) {
|
||||
if (w != g_width || h != g_height) return -1;
|
||||
if (!g_seat_enabled || g_flip_pending || g_blank) return -1;
|
||||
int back = 1 - g_front;
|
||||
struct fbuf *b = &g_fb[back];
|
||||
if (b->dmabuf == 0) {
|
||||
int fd = -1;
|
||||
if (drmPrimeHandleToFD(g_drm, b->handle, DRM_CLOEXEC | DRM_RDWR, &fd) < 0) {
|
||||
fprintf(stderr, "kms: cannot share the framebuffers with the GPU: %s\n", strerror(errno));
|
||||
b->dmabuf = -1;
|
||||
} else {
|
||||
b->dmabuf = fd + 1;
|
||||
}
|
||||
}
|
||||
if (b->dmabuf < 0) return -2;
|
||||
info[0] = b->dmabuf - 1; info[1] = (int32_t)b->pitch; info[2] = g_phys_w; info[3] = g_phys_h;
|
||||
info[4] = g_scale > 1 ? g_scale : 1; info[5] = g_rot;
|
||||
const int *s = g_stale[back];
|
||||
missing[0] = s[0] < 0 ? 0 : s[0]; missing[1] = s[1] < 0 ? 0 : s[1];
|
||||
missing[2] = s[2] > w ? w : s[2]; missing[3] = s[3] > h ? h : s[3];
|
||||
return back;
|
||||
}
|
||||
|
||||
/* Show the buffer mp_kms_back_buffer gave. */
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_kms_flip(void) {
|
||||
int back = 1 - g_front;
|
||||
int *s = g_stale[back];
|
||||
s[0] = s[1] = s[2] = s[3] = 0;
|
||||
if (drmModePageFlip(g_drm, g_crtc, b->fb, DRM_MODE_PAGE_FLIP_EVENT, NULL) < 0) {
|
||||
if (drmModePageFlip(g_drm, g_crtc, g_fb[back].fb, DRM_MODE_PAGE_FLIP_EVENT, NULL) < 0) {
|
||||
/* Fall back to a synchronous set. */
|
||||
if (drmModeSetCrtc(g_drm, g_crtc, b->fb, 0, 0, &g_conn, 1, &g_mode) < 0) return -1;
|
||||
if (drmModeSetCrtc(g_drm, g_crtc, g_fb[back].fb, 0, 0, &g_conn, 1, &g_mode) < 0) return -1;
|
||||
} else {
|
||||
g_flip_pending = 1;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
///| Software or hardware rendering: whether this machine has a GPU that apps can draw with.
|
||||
///
|
||||
/// Chandra itself always draws the desktop on the CPU (the host's rasteriser), so the GPU never
|
||||
/// makes the compositor's own frames cheaper. What it changes is how much CPU is left over: with
|
||||
/// a GPU, apps (browsers, video, games) draw there; without one they draw with llvmpipe or their
|
||||
/// own software paths on the same cores as Chandra. So in software mode the desktop asks for
|
||||
/// The host draws the desktop on the GPU when it can (wasmhost/bin/raster_gpu.c decides that on its
|
||||
/// own, docs/rendering.md) and apps draw there too; without one both are software on the same
|
||||
/// cores. So in software mode the desktop asks for
|
||||
/// less: window previews are remade less often while they are on screen, and a desktop module
|
||||
/// (Live Starfield) gets at most 30 frames a second.
|
||||
///
|
||||
|
||||
@@ -353,6 +353,33 @@ pub fn surface_capture(_fd : Int, _offset : Int, _stride : Int, _width : Int, _h
|
||||
0U
|
||||
}
|
||||
|
||||
///| No GPU buffers on the native build, which draws on the CPU.
|
||||
pub fn dmabuf_support() -> (UInt64, Array[(UInt, UInt64)]) {
|
||||
(0UL, [])
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn dmabuf_import(
|
||||
_planes : Array[(Int, UInt, UInt)],
|
||||
_modifier : UInt64,
|
||||
_width : Int,
|
||||
_height : Int,
|
||||
_fourcc : UInt,
|
||||
_y_invert : Bool,
|
||||
) -> UInt {
|
||||
0U
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn dmabuf_capture(_id : UInt) -> UInt {
|
||||
0U
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn dmabuf_release(_id : UInt) -> Unit {
|
||||
()
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn surface_release(_id : UInt) -> Unit {
|
||||
()
|
||||
|
||||
@@ -255,6 +255,37 @@ pub fn surface_capture(fd : Int, offset : Int, stride : Int, width : Int, height
|
||||
@hostraster.capture(fd, offset, stride, width, height, format)
|
||||
}
|
||||
|
||||
///| The GPU clients should draw on (its dev_t) and the buffers (DRM fourcc, modifier) the host takes
|
||||
/// from it; none when the host draws on the CPU.
|
||||
pub fn dmabuf_support() -> (UInt64, Array[(UInt, UInt64)]) {
|
||||
let s = @hostraster.dmabufs()
|
||||
(s.device, s.formats.map(f => (f.fourcc, f.modifier)))
|
||||
}
|
||||
|
||||
///| Import a client's GPU buffer (planes: fd, offset, stride; the fds stay the caller's). 0 when the
|
||||
/// GPU can't read it.
|
||||
pub fn dmabuf_import(
|
||||
planes : Array[(Int, UInt, UInt)],
|
||||
modifier : UInt64,
|
||||
width : Int,
|
||||
height : Int,
|
||||
fourcc : UInt,
|
||||
y_invert : Bool,
|
||||
) -> UInt {
|
||||
let planes : Array[@hostraster.DmabufPlane] = planes.map(p => { fd: p.0, offset: p.1, stride: p.2 })
|
||||
@hostraster.import_dmabuf(planes, modifier, width, height, fourcc, y_invert)
|
||||
}
|
||||
|
||||
///| What an imported buffer holds now, as a surface (like `surface_capture`). 0 on failure.
|
||||
pub fn dmabuf_capture(id : UInt) -> UInt {
|
||||
@hostraster.capture_dmabuf(id)
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn dmabuf_release(id : UInt) -> Unit {
|
||||
@hostraster.release_dmabuf(id)
|
||||
}
|
||||
|
||||
///| Forget a captured surface.
|
||||
pub fn surface_release(id : UInt) -> Unit {
|
||||
@hostraster.release(id)
|
||||
|
||||
@@ -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,
|
||||
})
|
||||
}
|
||||
@@ -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 => ()
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user