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