CI / test (push) Failing after 29s
The wasm software renderer cost ~10-20 ns/pixel, so any changed frame took ~100+ ms at 1080p and the desktop swung between idle and very slow. The compositor still builds the scene (windows, decorations, panel, wallpaper) but a new moonplasma:host/raster interface turns it into pixels in the host: - host-wasm/src/raster.rs: a port of compositor/renderer (same blend arithmetic: fills, strokes, lines, circles, text, clips, shadows, surface blits), a wallpaper+desktop-card cache, and captured client surfaces (copied out of the shared memory once at commit, converted to RGBA, solid ones blit as row copies). 9 unit tests; a full frame matches the native software renderer except for text antialiasing (0.3% of pixels). - compositor/raster sends a SceneGraph as ops; compositor.compose_scene() returns it only when something changed. The login session shows the host framebuffer directly (display.present-frame), the nested window gets it as a memfd (raster.export-memfd), --gfx-out reads it (read-frame). - Client buffers never enter wasm: snapshot_shm_buffer captures in the host and keeps an id. - Loop pacing sleeps the rest of the frame budget instead of a fixed 16/8 ms. Full-screen terminal flooding output at 1080p: ~2 fps / 94% CPU -> ~57 fps; idle ~9%. The native build keeps the software renderer. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
506 lines
14 KiB
MoonBit
506 lines
14 KiB
MoonBit
///| Nested backend: shows the compositor's frames in a window of the host
|
|
/// Wayland session. The compositor is a plain Wayland *client* here (wl_shm
|
|
/// buffers over SCM_RIGHTS + xdg_toplevel), reusing the same wire code and C
|
|
/// shim as the server side, and queues host pointer/keyboard input for the compositor.
|
|
|
|
///| Input forwarded from the host session (pointer in window pixels; key = evdev code).
|
|
pub(all) enum InputEvent {
|
|
PointerMove(Double, Double)
|
|
PointerButton(UInt, Bool)
|
|
/// Vertical wheel scroll in wl_fixed units / 256 (positive = down).
|
|
Scroll(Double)
|
|
Key(UInt, Bool)
|
|
Modifiers(UInt, UInt, UInt, UInt)
|
|
}
|
|
|
|
///|
|
|
pub(all) struct NestedWindow {
|
|
input : Array[InputEvent]
|
|
fds : Array[Int]
|
|
mut keymap : Bytes
|
|
mut keyboard_id : UInt
|
|
mut shape_mgr_id : UInt
|
|
mut shape_dev_id : UInt
|
|
mut enter_serial : UInt
|
|
mut shape_applied : UInt
|
|
fd : Int
|
|
rx : Array[Byte]
|
|
globals : Map[String, (UInt, UInt)]
|
|
live_buffers : Map[UInt, Bool]
|
|
mut next_id : UInt
|
|
mut comp_id : UInt
|
|
mut shm_id : UInt
|
|
mut wm_id : UInt
|
|
mut seat_id : UInt
|
|
mut surf_id : UInt
|
|
mut xdg_surf_id : UInt
|
|
mut toplevel_id : UInt
|
|
mut pointer_id : UInt
|
|
mut sync_cb : UInt
|
|
mut sync_done : Bool
|
|
mut frame_cb : UInt
|
|
mut frame_pending : Bool
|
|
mut configured : Bool
|
|
mut closed : Bool
|
|
mut px : Double
|
|
mut py : Double
|
|
width : Int
|
|
height : Int
|
|
}
|
|
|
|
///|
|
|
fn NestedWindow::alloc(self : NestedWindow) -> UInt {
|
|
let id = self.next_id
|
|
self.next_id = id + 1U
|
|
id
|
|
}
|
|
|
|
///|
|
|
fn NestedWindow::send(self : NestedWindow, msg : Bytes, send_fd? : Int = -1) -> Unit {
|
|
ignore(@sys.sendmsg_fd(self.fd, msg, send_fd))
|
|
}
|
|
|
|
///|
|
|
fn NestedWindow::bind(self : NestedWindow, iface : String, ver : UInt) -> UInt {
|
|
match self.globals.get(iface) {
|
|
None => 0U
|
|
Some((name, adv)) => {
|
|
let id = self.alloc()
|
|
let w = @wayland.WireWriter::new()
|
|
w.u32(name)
|
|
w.str(iface)
|
|
w.u32(if ver < adv { ver } else { adv })
|
|
w.u32(id)
|
|
self.send(w.finish_msg(2U, 0U))
|
|
id
|
|
}
|
|
}
|
|
}
|
|
|
|
///| Read and dispatch everything pending on the host socket (non-blocking).
|
|
fn NestedWindow::pump_events(self : NestedWindow) -> Unit {
|
|
let scratch : FixedArray[Byte] = FixedArray::make(16384, b'\x00')
|
|
while @sys.poll_read(self.fd, 0) > 0 {
|
|
let (n, rfd) = @sys.recvmsg_fd(self.fd, scratch, 16384)
|
|
if rfd >= 0 {
|
|
self.fds.push(rfd)
|
|
}
|
|
if n <= 0 {
|
|
self.closed = true
|
|
return
|
|
}
|
|
for i = 0; i < n; i = i + 1 {
|
|
self.rx.push(scratch[i])
|
|
}
|
|
}
|
|
let mut off = 0
|
|
while off + 8 <= self.rx.length() {
|
|
let obj = @wayland.read_u32_le(self.rx, off)
|
|
let word2 = @wayland.read_u32_le(self.rx, off + 4)
|
|
let size = ((word2 >> 16) & 0xFFFFU).reinterpret_as_int()
|
|
let op = word2 & 0xFFFFU
|
|
if size < 8 || off + size > self.rx.length() {
|
|
break
|
|
}
|
|
let r = @wayland.WireReader::from_array(self.rx, off + 8)
|
|
self.dispatch(obj, op, r)
|
|
off = off + size
|
|
}
|
|
if off > 0 {
|
|
let rest : Array[Byte] = []
|
|
for i = off; i < self.rx.length(); i = i + 1 {
|
|
rest.push(self.rx[i])
|
|
}
|
|
self.rx.clear()
|
|
for b in rest {
|
|
self.rx.push(b)
|
|
}
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn NestedWindow::dispatch(
|
|
self : NestedWindow,
|
|
obj : UInt,
|
|
op : UInt,
|
|
r : @wayland.WireReader,
|
|
) -> Unit {
|
|
if obj == 1U {
|
|
if op == 0U {
|
|
let _id = r.u32()
|
|
let _code = r.u32()
|
|
@sys.log("nested: host wl_display error: " + r.str())
|
|
self.closed = true
|
|
}
|
|
} else if obj == 2U {
|
|
if op == 0U {
|
|
let name = r.u32()
|
|
let iface = r.str()
|
|
let ver = r.u32()
|
|
self.globals.set(iface, (name, ver))
|
|
}
|
|
} else if self.sync_cb != 0U && obj == self.sync_cb {
|
|
self.sync_done = true
|
|
} else if self.frame_cb != 0U && obj == self.frame_cb {
|
|
self.frame_pending = false
|
|
self.frame_cb = 0U
|
|
} else if obj == self.wm_id {
|
|
if op == 0U {
|
|
// xdg_wm_base.ping(serial) -> pong
|
|
let serial = r.u32()
|
|
let w = @wayland.WireWriter::new()
|
|
w.u32(serial)
|
|
self.send(w.finish_msg(self.wm_id, 3U))
|
|
}
|
|
} else if obj == self.xdg_surf_id {
|
|
if op == 0U {
|
|
// configure(serial) -> ack_configure
|
|
let serial = r.u32()
|
|
let w = @wayland.WireWriter::new()
|
|
w.u32(serial)
|
|
self.send(w.finish_msg(self.xdg_surf_id, 4U))
|
|
self.configured = true
|
|
}
|
|
} else if obj == self.toplevel_id {
|
|
if op == 1U {
|
|
self.closed = true
|
|
}
|
|
} else if obj == self.seat_id {
|
|
if op == 0U {
|
|
let caps = r.u32()
|
|
if (caps & 1U) != 0U && self.pointer_id == 0U {
|
|
let id = self.alloc()
|
|
let w = @wayland.WireWriter::new()
|
|
w.u32(id)
|
|
self.send(w.finish_msg(self.seat_id, 0U))
|
|
self.pointer_id = id
|
|
if self.shape_mgr_id != 0U {
|
|
let dev = self.alloc()
|
|
let wd = @wayland.WireWriter::new()
|
|
wd.u32(dev)
|
|
wd.u32(id)
|
|
self.send(wd.finish_msg(self.shape_mgr_id, 1U))
|
|
self.shape_dev_id = dev
|
|
}
|
|
}
|
|
if (caps & 2U) != 0U && self.keyboard_id == 0U {
|
|
let id = self.alloc()
|
|
let w = @wayland.WireWriter::new()
|
|
w.u32(id)
|
|
self.send(w.finish_msg(self.seat_id, 1U))
|
|
self.keyboard_id = id
|
|
}
|
|
}
|
|
} else if self.keyboard_id != 0U && obj == self.keyboard_id {
|
|
match op {
|
|
0U => {
|
|
// keymap(format, fd, size)
|
|
let _format = r.u32()
|
|
let size = r.u32().reinterpret_as_int()
|
|
if self.fds.length() > 0 {
|
|
let fd = self.fds.remove(0)
|
|
self.keymap = @sys.mmap_read(fd, size)
|
|
@sys.close_fd(fd)
|
|
}
|
|
}
|
|
3U => {
|
|
let _serial = r.u32()
|
|
let _time = r.u32()
|
|
let key = r.u32()
|
|
let state = r.u32()
|
|
self.input.push(Key(key, state == 1U))
|
|
}
|
|
4U => {
|
|
let _serial = r.u32()
|
|
let dep = r.u32()
|
|
let lat = r.u32()
|
|
let lock = r.u32()
|
|
let group = r.u32()
|
|
self.input.push(Modifiers(dep, lat, lock, group))
|
|
}
|
|
_ => ()
|
|
}
|
|
} else if self.pointer_id != 0U && obj == self.pointer_id {
|
|
match op {
|
|
0U => {
|
|
self.enter_serial = r.u32()
|
|
self.shape_applied = 0U // host resets the cursor on enter
|
|
let _surf = r.u32()
|
|
self.px = r.i32().to_double() / 256.0
|
|
self.py = r.i32().to_double() / 256.0
|
|
self.input.push(PointerMove(self.px, self.py))
|
|
}
|
|
2U => {
|
|
let _time = r.u32()
|
|
self.px = r.i32().to_double() / 256.0
|
|
self.py = r.i32().to_double() / 256.0
|
|
self.input.push(PointerMove(self.px, self.py))
|
|
}
|
|
3U => {
|
|
let _serial = r.u32()
|
|
let _time = r.u32()
|
|
let button = r.u32()
|
|
let state = r.u32()
|
|
self.input.push(PointerButton(button, state == 1U))
|
|
}
|
|
4U => {
|
|
let _time = r.u32()
|
|
let axis = r.u32()
|
|
let value = r.i32().to_double() / 256.0
|
|
if axis == 0U {
|
|
self.input.push(Scroll(value))
|
|
}
|
|
}
|
|
_ => ()
|
|
}
|
|
} else if self.live_buffers.contains(obj) {
|
|
if op == 0U {
|
|
// wl_buffer.release -> destroy it
|
|
self.live_buffers.remove(obj)
|
|
let w = @wayland.WireWriter::new()
|
|
self.send(w.finish_msg(obj, 0U))
|
|
}
|
|
}
|
|
}
|
|
|
|
///| wl_display.sync and wait for the callback.
|
|
fn NestedWindow::roundtrip(self : NestedWindow) -> Unit {
|
|
let cb = self.alloc()
|
|
self.sync_cb = cb
|
|
self.sync_done = false
|
|
let w = @wayland.WireWriter::new()
|
|
w.u32(cb)
|
|
self.send(w.finish_msg(1U, 0U))
|
|
let mut tries = 0
|
|
while !self.sync_done && !self.closed && tries < 500 {
|
|
ignore(@sys.poll_read(self.fd, 10))
|
|
self.pump_events()
|
|
tries = tries + 1
|
|
}
|
|
self.sync_cb = 0U
|
|
}
|
|
|
|
///| Connect to the host compositor and open a window of `width` x `height`.
|
|
/// `display` is a socket name (relative to `runtime_dir`) or an absolute path.
|
|
pub fn NestedWindow::open(
|
|
display : String,
|
|
runtime_dir : String,
|
|
title : String,
|
|
app_id : String,
|
|
width : Int,
|
|
height : Int,
|
|
) -> NestedWindow? {
|
|
let path = if display.length() > 0 && display[0] == '/' {
|
|
display
|
|
} else {
|
|
runtime_dir + "/" + display
|
|
}
|
|
let fd = @sys.unix_connect(path)
|
|
if fd < 0 {
|
|
return None
|
|
}
|
|
let self : NestedWindow = {
|
|
fd,
|
|
input: [],
|
|
fds: [],
|
|
keymap: Bytes::from_array([]),
|
|
keyboard_id: 0U,
|
|
shape_mgr_id: 0U,
|
|
shape_dev_id: 0U,
|
|
enter_serial: 0U,
|
|
shape_applied: 0U,
|
|
rx: [],
|
|
globals: {},
|
|
live_buffers: {},
|
|
next_id: 3U,
|
|
comp_id: 0U,
|
|
shm_id: 0U,
|
|
wm_id: 0U,
|
|
seat_id: 0U,
|
|
surf_id: 0U,
|
|
xdg_surf_id: 0U,
|
|
toplevel_id: 0U,
|
|
pointer_id: 0U,
|
|
sync_cb: 0U,
|
|
sync_done: false,
|
|
frame_cb: 0U,
|
|
frame_pending: false,
|
|
configured: false,
|
|
closed: false,
|
|
px: 0.0,
|
|
py: 0.0,
|
|
width,
|
|
height,
|
|
}
|
|
let w = @wayland.WireWriter::new()
|
|
w.u32(2U)
|
|
self.send(w.finish_msg(1U, 1U)) // get_registry
|
|
self.roundtrip()
|
|
self.comp_id = self.bind("wl_compositor", 4U)
|
|
self.shm_id = self.bind("wl_shm", 1U)
|
|
self.wm_id = self.bind("xdg_wm_base", 2U)
|
|
self.seat_id = self.bind("wl_seat", 5U)
|
|
self.shape_mgr_id = self.bind("wp_cursor_shape_manager_v1", 1U)
|
|
if self.comp_id == 0U || self.shm_id == 0U || self.wm_id == 0U {
|
|
@sys.log("nested: host compositor lacks wl_compositor/wl_shm/xdg_wm_base")
|
|
return None
|
|
}
|
|
self.surf_id = self.alloc()
|
|
let w1 = @wayland.WireWriter::new()
|
|
w1.u32(self.surf_id)
|
|
self.send(w1.finish_msg(self.comp_id, 0U))
|
|
self.xdg_surf_id = self.alloc()
|
|
let w2 = @wayland.WireWriter::new()
|
|
w2.u32(self.xdg_surf_id)
|
|
w2.u32(self.surf_id)
|
|
self.send(w2.finish_msg(self.wm_id, 2U))
|
|
self.toplevel_id = self.alloc()
|
|
let w3 = @wayland.WireWriter::new()
|
|
w3.u32(self.toplevel_id)
|
|
self.send(w3.finish_msg(self.xdg_surf_id, 1U))
|
|
let w4 = @wayland.WireWriter::new()
|
|
w4.str(title)
|
|
self.send(w4.finish_msg(self.toplevel_id, 2U))
|
|
let w5 = @wayland.WireWriter::new()
|
|
w5.str(app_id)
|
|
self.send(w5.finish_msg(self.toplevel_id, 3U))
|
|
// Fixed size: min == max == output size.
|
|
let w6 = @wayland.WireWriter::new()
|
|
w6.i32(width)
|
|
w6.i32(height)
|
|
self.send(w6.finish_msg(self.toplevel_id, 7U)) // set_max_size
|
|
let w7 = @wayland.WireWriter::new()
|
|
w7.i32(width)
|
|
w7.i32(height)
|
|
self.send(w7.finish_msg(self.toplevel_id, 8U)) // set_min_size
|
|
self.send(@wayland.WireWriter::new().finish_msg(self.surf_id, 6U)) // commit
|
|
let mut tries = 0
|
|
while !self.configured && !self.closed && tries < 500 {
|
|
ignore(@sys.poll_read(self.fd, 10))
|
|
self.pump_events()
|
|
tries = tries + 1
|
|
}
|
|
if !self.configured {
|
|
@sys.log("nested: host never sent xdg_surface.configure")
|
|
return None
|
|
}
|
|
Some(self)
|
|
}
|
|
|
|
///| Pump host events (input is queued; see take_input). False once the
|
|
/// window was closed or the host went away.
|
|
pub fn NestedWindow::poll(self : NestedWindow) -> Bool {
|
|
self.pump_events()
|
|
!self.closed
|
|
}
|
|
|
|
///| Take all input received since the last call.
|
|
pub fn NestedWindow::take_input(self : NestedWindow) -> Array[InputEvent] {
|
|
let out = self.input.copy()
|
|
self.input.clear()
|
|
out
|
|
}
|
|
|
|
///| Show `pixmap` (RGBA8888) in the window. Skipped while the host has not yet
|
|
/// consumed the previous frame, so a slow host never builds a backlog.
|
|
pub fn NestedWindow::present(self : NestedWindow, pixmap : @renderer.Pixmap) -> Unit {
|
|
if self.frame_pending || self.closed {
|
|
return
|
|
}
|
|
let w = pixmap.width
|
|
let h = pixmap.height
|
|
let len = w * 4 * h
|
|
// RGBA -> little-endian ARGB8888 (B, G, R, A).
|
|
let out : FixedArray[Byte] = FixedArray::make(len, b'\x00')
|
|
for i = 0; i < w * h; i = i + 1 {
|
|
let o = i * 4
|
|
out[o] = pixmap.data[o + 2]
|
|
out[o + 1] = pixmap.data[o + 1]
|
|
out[o + 2] = pixmap.data[o]
|
|
out[o + 3] = b'\xff'
|
|
}
|
|
let memfd = @sys.memfd_create("moon-plasma-frame", len)
|
|
if memfd < 0 {
|
|
return
|
|
}
|
|
ignore(@sys.pwrite_bytes(memfd, Bytes::from_fixedarray(out), 0))
|
|
self.send_frame(memfd, w, h)
|
|
}
|
|
|
|
///| Show the host's framebuffer (drawn by the host rasteriser) in the window: the host fills a
|
|
/// memfd with it and this hands that to the host compositor, so no pixels pass through here.
|
|
pub fn NestedWindow::present_frame(self : NestedWindow, w : Int, h : Int) -> Unit {
|
|
if self.frame_pending || self.closed {
|
|
return
|
|
}
|
|
let memfd = @sys.frame_memfd()
|
|
if memfd < 0 {
|
|
return
|
|
}
|
|
self.send_frame(memfd, w, h)
|
|
}
|
|
|
|
///| Attach the ARGB8888 frame in `memfd` (w x h) to the window and ask for a frame callback.
|
|
/// Takes the descriptor: it is closed here once sent.
|
|
fn NestedWindow::send_frame(self : NestedWindow, memfd : Int, w : Int, h : Int) -> Unit {
|
|
let stride = w * 4
|
|
let len = stride * h
|
|
let pool = self.alloc()
|
|
let wp = @wayland.WireWriter::new()
|
|
wp.u32(pool)
|
|
wp.i32(len)
|
|
self.send(wp.finish_msg(self.shm_id, 0U), send_fd=memfd)
|
|
@sys.close_fd(memfd)
|
|
let buf = self.alloc()
|
|
let wb = @wayland.WireWriter::new()
|
|
wb.u32(buf)
|
|
wb.i32(0)
|
|
wb.i32(w)
|
|
wb.i32(h)
|
|
wb.i32(stride)
|
|
wb.u32(0U)
|
|
self.send(wb.finish_msg(pool, 0U))
|
|
self.send(@wayland.WireWriter::new().finish_msg(pool, 1U)) // pool.destroy
|
|
self.live_buffers.set(buf, true)
|
|
let wa = @wayland.WireWriter::new()
|
|
wa.u32(buf)
|
|
wa.i32(0)
|
|
wa.i32(0)
|
|
self.send(wa.finish_msg(self.surf_id, 1U))
|
|
let wd = @wayland.WireWriter::new()
|
|
wd.i32(0)
|
|
wd.i32(0)
|
|
wd.i32(w)
|
|
wd.i32(h)
|
|
self.send(wd.finish_msg(self.surf_id, 9U))
|
|
self.frame_cb = self.alloc()
|
|
let wf = @wayland.WireWriter::new()
|
|
wf.u32(self.frame_cb)
|
|
self.send(wf.finish_msg(self.surf_id, 3U))
|
|
self.frame_pending = true
|
|
self.send(@wayland.WireWriter::new().finish_msg(self.surf_id, 6U))
|
|
}
|
|
|
|
///|
|
|
pub fn NestedWindow::close(self : NestedWindow) -> Unit {
|
|
@sys.close_fd(self.fd)
|
|
}
|
|
|
|
///| True when the host is ready for a new frame (avoids rendering frames that would be dropped).
|
|
pub fn NestedWindow::frame_ready(self : NestedWindow) -> Bool {
|
|
!self.frame_pending && !self.closed
|
|
}
|
|
|
|
///| Ask the host to show cursor `shape` (wp_cursor_shape_v1 enum value) over our
|
|
/// window. No-op if unchanged, unsupported by the host, or the pointer is outside.
|
|
pub fn NestedWindow::set_cursor_shape(self : NestedWindow, shape : UInt) -> Unit {
|
|
if self.shape_dev_id == 0U || self.enter_serial == 0U || shape == self.shape_applied {
|
|
return
|
|
}
|
|
let w = @wayland.WireWriter::new()
|
|
w.u32(self.enter_serial)
|
|
w.u32(shape)
|
|
self.send(w.finish_msg(self.shape_dev_id, 1U))
|
|
self.shape_applied = shape
|
|
}
|