Files
chandra/compositor/kms/kms_native.mbt
T
nandiandClaude Sonnet 5.5 c6e31b4a8f
CI / test (push) Failing after 29s
Draw in the host: the wasm compositor sends draw ops, the host rasterises natively
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>
2026-11-25 04:39:40 -08:00

127 lines
3.5 KiB
MoonBit

///| Native display backend for running moon-plasma as a login session: DRM/KMS
/// output (dumb buffers, page flips, hardware cursor), libinput keyboard and
/// pointer, xkbcommon keymap, libseat for device access and VT switching.
/// All state lives in the C shim (kms_stub.c); this wrapper exposes it with the
/// same shape as `@nested.NestedWindow` so the main loop can drive either.
///|
extern "C" fn c_open() -> Int = "mp_kms_open"
///|
extern "C" fn c_width() -> Int = "mp_kms_width"
///|
extern "C" fn c_height() -> Int = "mp_kms_height"
///|
extern "C" fn c_quit_requested() -> Int = "mp_kms_quit_requested"
///|
extern "C" fn c_poll() -> Int = "mp_kms_poll"
///|
extern "C" fn c_frame_ready() -> Int = "mp_kms_frame_ready"
///|
extern "C" fn c_present(pixels : FixedArray[Byte], w : Int, h : Int) -> Int = "mp_kms_present"
///|
extern "C" fn c_set_cursor_shape(shape : Int) = "mp_kms_set_cursor_shape"
///|
extern "C" fn c_next_event() -> Int = "mp_kms_next_event"
///|
extern "C" fn c_arg(i : Int) -> Int = "mp_kms_arg"
///|
extern "C" fn c_keymap_len() -> Int = "mp_kms_keymap_len"
///|
extern "C" fn c_keymap_copy(out : FixedArray[Byte]) -> Int = "mp_kms_keymap_copy"
///|
extern "C" fn c_close() = "mp_kms_close"
///|
pub(all) struct Kms {
width : Int
height : Int
keymap : Bytes
}
///| Open the seat, the first connected DRM output and libinput. Returns None
/// (after printing why to stderr) when not running on an active login VT.
pub fn Kms::open() -> Kms? {
if c_open() != 0 {
c_close()
return None
}
let n = c_keymap_len()
let buf : FixedArray[Byte] = FixedArray::make(n, b'\x00')
let got = if n > 0 { c_keymap_copy(buf) } else { 0 }
let keymap = Bytes::from_fixedarray(buf, len=got)
Some({ width: c_width(), height: c_height(), keymap })
}
///| Service the seat, DRM and input fds. False once Ctrl+Alt+Backspace was pressed.
pub fn Kms::poll(_self : Kms) -> Bool {
ignore(c_poll())
c_quit_requested() == 0
}
///| True when a new frame can be presented (seat active, no page flip in flight).
pub fn Kms::frame_ready(_self : Kms) -> Bool {
c_frame_ready() != 0
}
///| Present a composed frame (its size must match the output mode).
pub fn Kms::present(_self : Kms, pixmap : @renderer.Pixmap) -> Unit {
ignore(c_present(pixmap.data, pixmap.width, pixmap.height))
}
///| Show the cursor for a wp_cursor_shape_v1 value.
pub fn Kms::set_cursor_shape(_self : Kms, shape : UInt) -> Unit {
c_set_cursor_shape(shape.reinterpret_as_int())
}
///| Input queued since the last call, in the compositor's event type.
pub fn Kms::take_input(_self : Kms) -> Array[@nested.InputEvent] {
let out : Array[@nested.InputEvent] = []
while true {
let t = c_next_event()
if t == 0 {
break
}
let a = c_arg(1)
let b = c_arg(2)
match t {
1 => out.push(PointerMove(a.to_double(), b.to_double()))
2 => out.push(PointerButton(a.reinterpret_as_uint(), b != 0))
3 => out.push(Key(a.reinterpret_as_uint(), b != 0))
4 =>
out.push(
Modifiers(
a.reinterpret_as_uint(),
b.reinterpret_as_uint(),
c_arg(3).reinterpret_as_uint(),
c_arg(4).reinterpret_as_uint(),
),
)
_ => ()
}
}
out
}
///| Restore the previous video mode and release the seat.
pub fn Kms::close(_self : Kms) -> Unit {
c_close()
}
///| The native build draws with the software renderer and uses `present`; there is no host
/// framebuffer to show.
pub fn Kms::present_frame(_self : Kms) -> Unit {
abort("present_frame is only available to the wasm compositor")
}