Files
chandra/compositor/kms/kms_native.mbt
T
Claude ba82d3c59b
CI / test (push) Failing after 15s
Idle desktop sleeps instead of redrawing; only changed parts of the screen are redrawn
Measured headless and nested at 1080p (host-wasm), against main:
- Idle: ~9.4% CPU per compositor -> ~0.35%. Idle frames cost ~1.3 ms, and the loop
  woke 60-125 times a second to find nothing had changed.
- One 800x600 floating client animating at 60 fps: 33% -> 16% CPU; raster per frame
  ~4.5 ms -> ~0.9 ms. A full-screen tiled client: 65% -> 54% (its cost is now mostly
  copying the 8 MB buffer out of the client's shared memory).

Changes:
- shell: the wallpaper and desktop card ops (~1900, the moon disc is drawn per pixel)
  are kept and reused until what they are drawn from changes (size, theme, wallpaper,
  card position, date, hour:minute). This was ~75% of an idle frame.
- FrameSig compares the background by identity first instead of op by op.
- Host tables (windows, layouts, ...) are only sent to the host when their bytes change.
- Main loop: a pass with no input, no changed frame and nothing waiting to be shown
  blocks in the new sys.wait-readable on the Wayland socket and clients (plus the
  nested host connection; the login session's seat, DRM and libinput fds are added by
  the host) until just before the wall clock's next second, then polls every 5 ms
  until it turns, so clocks still tick on time. Recording, pending screenshots and
  toasts keep the frame pace (ChandraCompositor::has_timed_work).
- Host raster: works out what changed from the last frame. When only windows changed
  (a client drew, a window moved, focus moved) it redraws just their footprint
  (decoration and shadow included, old and new place), copying back only that part
  of the cached backdrop; anything else still redraws everything. A test checks the
  partial redraw is pixel-identical to a full one for a client redraw, a move and a
  focus change with stacking swapped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hku78wdzXtY9QsR9NW
2026-11-25 19:21:39 +00:00

150 lines
4.0 KiB
MoonBit

///| Native display backend for running chandra 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"
///|
extern "C" fn c_fd(i : Int) -> Int = "mp_kms_fd"
///|
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(),
),
)
5 =>
out.push(
Scroll(
a.reinterpret_as_uint(),
b.to_double() / 256.0,
c_arg(3),
),
)
_ => ()
}
}
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")
}
///| The seat, DRM and input fds `poll` services, for an idle loop to block on.
pub fn Kms::wait_fds(_self : Kms) -> Array[Int] {
let out : Array[Int] = []
for i = 0; i < 3; i = i + 1 {
let fd = c_fd(i)
if fd >= 0 {
out.push(fd)
}
}
out
}