Files
chandra/compositor/kms/kms_wasm.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

80 lines
2.5 KiB
MoonBit

///| Wasm display backend: the same API as kms_native.mbt, but the host owns the display and the
/// seat (`chandra:host/display`). The component sends frames out and reads events in.
///|
pub(all) struct Kms {
width : Int
height : Int
keymap : Bytes
}
///| Take over the first connected output and the seat's input. None when this is not an active
/// login VT (the host prints why).
pub fn Kms::open() -> Kms? {
match @hostdisplay.open() {
None => None
Some(o) =>
Some({
width: o.width.reinterpret_as_int(),
height: o.height.reinterpret_as_int(),
keymap: Bytes::from_fixedarray(o.keymap),
})
}
}
///| Service the seat, DRM and input fds. False once Ctrl+Alt+Backspace was pressed.
pub fn Kms::poll(_self : Kms) -> Bool {
@hostdisplay.poll()
}
///| True when a new frame can be presented (seat active, no page flip in flight).
pub fn Kms::frame_ready(_self : Kms) -> Bool {
@hostdisplay.frame_ready()
}
///| Present a composed frame (its size must match the output mode).
pub fn Kms::present(_self : Kms, pixmap : @renderer.Pixmap) -> Unit {
@hostdisplay.present(
pixmap.data,
pixmap.width.reinterpret_as_uint(),
pixmap.height.reinterpret_as_uint(),
)
}
///| Show the host framebuffer (what the host rasteriser drew) on the output: nothing is copied
/// through this program.
pub fn Kms::present_frame(_self : Kms) -> Unit {
@hostdisplay.present_frame()
}
///| Show the cursor for a wp_cursor_shape_v1 value.
pub fn Kms::set_cursor_shape(_self : Kms, shape : UInt) -> Unit {
@hostdisplay.set_cursor_shape(shape)
}
///| 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] = []
for ev in @hostdisplay.take_input() {
match ev {
PointerMove(p) => out.push(PointerMove(p.x, p.y))
PointerButton(k) => out.push(PointerButton(k.code, k.pressed))
Key(k) => out.push(Key(k.code, k.pressed))
Modifiers(m) => out.push(Modifiers(m.depressed, m.latched, m.locked, m.group))
Scroll(d) => out.push(Scroll(d.axis, d.value, d.discrete))
}
}
out
}
///| Restore the previous video mode and release the seat.
pub fn Kms::close(_self : Kms) -> Unit {
@hostdisplay.close()
}
///| The fds an idle loop blocks on for the display: none here, since the host's
/// `sys.wait-readable` adds its own seat, DRM and input fds while the display is open.
pub fn Kms::wait_fds(_self : Kms) -> Array[Int] {
[]
}