Idle desktop sleeps instead of redrawing; only changed parts of the screen are redrawn
CI / test (push) Failing after 15s
CI / test (push) Failing after 15s
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
This commit is contained in:
19 files changed
+542
-68
No files matched your search
+63
-11
@@ -182,6 +182,7 @@ pub fn run(args : Array[String]) -> Int {
|
||||
ignore(comp.restore_session()) // reopen the windows the last session ended with
|
||||
}
|
||||
let mut frames = 0
|
||||
let idle = IdleClock::new()
|
||||
// A changed frame waits here until the display can take it (no page flip in flight).
|
||||
let mut need_present = true
|
||||
// --profile: every 3 s print how long each frame part took on average.
|
||||
@@ -198,7 +199,8 @@ pub fn run(args : Array[String]) -> Int {
|
||||
break
|
||||
}
|
||||
let now_ms = @sys.monotonic_ms().to_uint()
|
||||
for ev in k.take_input() {
|
||||
let input = k.take_input()
|
||||
for ev in input {
|
||||
comp.handle_input(ev, now_ms)
|
||||
}
|
||||
comp.step()
|
||||
@@ -206,14 +208,15 @@ pub fn run(args : Array[String]) -> Int {
|
||||
k.set_cursor_shape(comp.desired_cursor())
|
||||
// The host draws; only a changed desktop is sent, and the frame is shown by the host
|
||||
// straight from its framebuffer.
|
||||
match comp.compose_scene() {
|
||||
let changed = match comp.compose_scene() {
|
||||
Some(sg) => {
|
||||
let r0 = @sys.monotonic_ms()
|
||||
@raster.submit(sg)
|
||||
comp.prof_raster_ms = comp.prof_raster_ms + (@sys.monotonic_ms() - r0)
|
||||
need_present = true
|
||||
true
|
||||
}
|
||||
None => ()
|
||||
None => false
|
||||
}
|
||||
if need_present && k.frame_ready() {
|
||||
k.present_frame()
|
||||
@@ -234,9 +237,16 @@ pub fn run(args : Array[String]) -> Int {
|
||||
comp.prof_raster_ms = 0UL
|
||||
}
|
||||
}
|
||||
// Sleep out what is left of the frame budget (8 ms), not a fixed 8 ms on top of the work.
|
||||
let spent = (@sys.monotonic_ms() - f0).to_int()
|
||||
@sys.sleep_ms(if spent >= 8 { 1 } else { 8 - spent })
|
||||
if input.length() == 0 && !changed && !need_present && !comp.has_timed_work() {
|
||||
// Idle: sleep until a client, the seat or the clock has something.
|
||||
let fds = comp.server.wait_fds()
|
||||
fds.append(k.wait_fds())
|
||||
ignore(@sys.wait_readable(fds, idle.timeout()))
|
||||
} else {
|
||||
// Sleep out what is left of the frame budget (8 ms), not a fixed 8 ms on top of the work.
|
||||
let spent = (@sys.monotonic_ms() - f0).to_int()
|
||||
@sys.sleep_ms(if spent >= 8 { 1 } else { 8 - spent })
|
||||
}
|
||||
continue
|
||||
}
|
||||
None => ()
|
||||
@@ -246,6 +256,9 @@ pub fn run(args : Array[String]) -> Int {
|
||||
if comp.quit_requested {
|
||||
break
|
||||
}
|
||||
// Whether this pass had anything to do; an idle pass sleeps on the fds instead of a frame.
|
||||
let mut busy = false
|
||||
let wait_fds = comp.server.wait_fds()
|
||||
match nested {
|
||||
Some(n) => {
|
||||
if !n.poll() {
|
||||
@@ -262,11 +275,13 @@ pub fn run(args : Array[String]) -> Int {
|
||||
height = nh
|
||||
comp.resize(nw, nh)
|
||||
need_present = true
|
||||
busy = true
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
let now_ms = @sys.monotonic_ms().to_uint()
|
||||
for ev in n.take_input() {
|
||||
let input = n.take_input()
|
||||
for ev in input {
|
||||
comp.handle_input(ev, now_ms)
|
||||
}
|
||||
n.set_cursor_shape(comp.desired_cursor())
|
||||
@@ -276,6 +291,7 @@ pub fn run(args : Array[String]) -> Int {
|
||||
@raster.submit(sg)
|
||||
comp.prof_raster_ms = comp.prof_raster_ms + (@sys.monotonic_ms() - r0)
|
||||
need_present = true
|
||||
busy = true
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
@@ -283,6 +299,8 @@ pub fn run(args : Array[String]) -> Int {
|
||||
n.present_frame(width, height)
|
||||
need_present = false
|
||||
}
|
||||
busy = busy || input.length() > 0 || need_present
|
||||
wait_fds.push(n.fd)
|
||||
}
|
||||
None => {
|
||||
comp.handle_gfx_events(gfx_surface)
|
||||
@@ -291,7 +309,8 @@ pub fn run(args : Array[String]) -> Int {
|
||||
let r0 = @sys.monotonic_ms()
|
||||
@raster.submit(sg)
|
||||
comp.prof_raster_ms = comp.prof_raster_ms + (@sys.monotonic_ms() - r0)
|
||||
need_present = true
|
||||
need_present = serve && gfx_out.length() > 0
|
||||
busy = true
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
@@ -300,6 +319,7 @@ pub fn run(args : Array[String]) -> Int {
|
||||
ignore(@sys.write_file_bytes(gfx_out, @renderer.encode_png(@raster.read_frame(width, height))))
|
||||
need_present = false
|
||||
}
|
||||
busy = busy || need_present
|
||||
}
|
||||
}
|
||||
if profile {
|
||||
@@ -317,9 +337,13 @@ pub fn run(args : Array[String]) -> Int {
|
||||
comp.prof_raster_ms = 0UL
|
||||
}
|
||||
}
|
||||
// About 60 frames a second: sleep what is left of 16 ms after the work.
|
||||
let spent = (@sys.monotonic_ms() - f0).to_int()
|
||||
@sys.sleep_ms(if spent >= 16 { 1 } else { 16 - spent })
|
||||
if busy || comp.has_timed_work() {
|
||||
// About 60 frames a second: sleep what is left of 16 ms after the work.
|
||||
let spent = (@sys.monotonic_ms() - f0).to_int()
|
||||
@sys.sleep_ms(if spent >= 16 { 1 } else { 16 - spent })
|
||||
} else {
|
||||
ignore(@sys.wait_readable(wait_fds, idle.timeout()))
|
||||
}
|
||||
}
|
||||
comp.save_session() // what is open now is reopened at the next start
|
||||
match kms {
|
||||
@@ -331,3 +355,31 @@ pub fn run(args : Array[String]) -> Int {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| How long an idle main loop may sleep. Nothing on screen changes without a client, the seat or
|
||||
/// the display waking the loop, except what follows the wall clock (clocks, the desktop card,
|
||||
/// system stats, notices), which changes on whole seconds. So an idle loop sleeps until just
|
||||
/// before the next second turns, then checks every few ms until it has: one or two wakeups a
|
||||
/// second instead of 60-125, and a clock that turns within a few tens of ms of the real one.
|
||||
struct IdleClock {
|
||||
/// The wall clock's seconds field last seen, and the monotonic time it was first seen.
|
||||
mut sec : Int
|
||||
mut sec_at : UInt64
|
||||
}
|
||||
|
||||
///|
|
||||
fn IdleClock::new() -> IdleClock {
|
||||
{ sec: -1, sec_at: 0UL }
|
||||
}
|
||||
|
||||
///| Milliseconds to sleep from now.
|
||||
fn IdleClock::timeout(self : IdleClock) -> Int {
|
||||
let now = @sys.monotonic_ms()
|
||||
let s = @sys.wall_time_field(5)
|
||||
if s != self.sec {
|
||||
self.sec = s
|
||||
self.sec_at = now
|
||||
}
|
||||
let since = (now - self.sec_at).to_int()
|
||||
if since < 980 { 980 - since } else { 5 }
|
||||
}
|
||||
@@ -260,7 +260,6 @@ fn release_snapshot(s : @scene.ShmBufferSnapshot?) -> Unit {
|
||||
}
|
||||
|
||||
///| What a frame was drawn from, compared with the next frame's to see whether it changed.
|
||||
#derive(Eq)
|
||||
pub(all) struct FrameSig {
|
||||
bg : Array[@scene.DrawOp]
|
||||
panel : Array[@scene.DrawOp]
|
||||
@@ -268,6 +267,15 @@ pub(all) struct FrameSig {
|
||||
popups : Array[@scene.Rect]
|
||||
}
|
||||
|
||||
///| The background ops are usually the very same (cached) array as last frame's: that is
|
||||
/// checked first, instead of comparing its ~2000 ops one by one every frame.
|
||||
pub impl Eq for FrameSig with equal(a, b) {
|
||||
(physical_equal(a.bg, b.bg) || a.bg == b.bg) &&
|
||||
a.panel == b.panel &&
|
||||
a.wins == b.wins &&
|
||||
a.popups == b.popups
|
||||
}
|
||||
|
||||
///|
|
||||
fn ChandraCompositor::apply_actions(
|
||||
self : ChandraCompositor,
|
||||
@@ -529,6 +537,16 @@ pub fn ChandraCompositor::step(self : ChandraCompositor) -> Unit {
|
||||
self.recording_tick()
|
||||
}
|
||||
|
||||
///| True while something runs on a timer rather than on input or client messages (a recording,
|
||||
/// a pending screenshot, a toast counting down): the main loop keeps its frame pace then instead
|
||||
/// of sleeping until an fd wakes it.
|
||||
pub fn ChandraCompositor::has_timed_work(self : ChandraCompositor) -> Bool {
|
||||
self.rec is Some(_) ||
|
||||
self.shot_request is Some(_) ||
|
||||
self.rec_request is Some(_) ||
|
||||
self.shot_toast is Some(_)
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn ChandraCompositor::set_layout_mode(
|
||||
self : ChandraCompositor,
|
||||
|
||||
@@ -43,6 +43,9 @@ 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
|
||||
@@ -132,3 +135,15 @@ pub fn Kms::close(_self : Kms) -> Unit {
|
||||
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
|
||||
}
|
||||
@@ -434,6 +434,19 @@ int32_t mp_kms_poll(void) {
|
||||
return g_seat_enabled;
|
||||
}
|
||||
|
||||
/* The fds mp_kms_poll services (0 seat, 1 DRM, 2 libinput), for an idle loop to block on;
|
||||
-1 when not open. */
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_kms_fd(int32_t i) {
|
||||
if (!g_seat) return -1;
|
||||
switch (i) {
|
||||
case 0: return libseat_get_fd(g_seat);
|
||||
case 1: return g_drm;
|
||||
case 2: return g_li ? libinput_get_fd(g_li) : -1;
|
||||
default: return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/* True when a new frame may be presented (seat active, no flip in flight). */
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_kms_frame_ready(void) {
|
||||
|
||||
@@ -71,3 +71,9 @@ pub fn Kms::take_input(_self : Kms) -> Array[@nested.InputEvent] {
|
||||
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] {
|
||||
[]
|
||||
}
|
||||
@@ -71,6 +71,7 @@ pub fn Rgba::from_hex(s : String) -> Rgba {
|
||||
}
|
||||
|
||||
///|
|
||||
#derive(Eq)
|
||||
pub(all) struct ThemePalette {
|
||||
desktop_top : Rgba
|
||||
desktop_bottom : Rgba
|
||||
@@ -85,6 +86,7 @@ pub(all) struct ThemePalette {
|
||||
}
|
||||
|
||||
///|
|
||||
#derive(Eq)
|
||||
pub(all) struct ThemeTokens {
|
||||
name : String
|
||||
palette : ThemePalette
|
||||
|
||||
@@ -223,6 +223,10 @@ pub(all) struct DesktopShell {
|
||||
mut wallpaper_len : Int
|
||||
/// Which wallpaper is drawn (an index into `@libshell.wallpaper_names`); the wallpaper module sets it.
|
||||
mut wallpaper_sel : Int
|
||||
/// The background ops (wallpaper + desktop card) and what they were built from: rebuilt only
|
||||
/// when that changes (about once a minute), not every frame.
|
||||
mut bg_cache : Array[@scene.DrawOp]
|
||||
mut bg_key : BgKey?
|
||||
/// Session settings (the session settings module flips them; saved in state.edn): reopen the
|
||||
/// last session's windows at the next start, and remember where floating windows were left.
|
||||
mut restore_session : Bool
|
||||
@@ -326,6 +330,8 @@ pub fn DesktopShell::new(display : String) -> DesktopShell {
|
||||
screen_w: 0,
|
||||
wallpaper_len: 0,
|
||||
wallpaper_sel: 0,
|
||||
bg_cache: [],
|
||||
bg_key: None,
|
||||
restore_session: true,
|
||||
screenshot_state: 2,
|
||||
remember_windows: true,
|
||||
@@ -800,6 +806,55 @@ pub fn DesktopShell::take_actions(self : DesktopShell) -> Array[ShellAction] {
|
||||
out
|
||||
}
|
||||
|
||||
///| What the background (wallpaper + desktop card) is drawn from.
|
||||
#derive(Eq)
|
||||
struct BgKey {
|
||||
w : Int
|
||||
h : Int
|
||||
theme : @scene.ThemeTokens
|
||||
wallpaper : Int
|
||||
x : Int
|
||||
y : Int
|
||||
date : (UInt, Byte, Byte, Byte)
|
||||
hour : Byte
|
||||
minute : Byte
|
||||
}
|
||||
|
||||
///| The wallpaper and the desktop card. Building them costs most of an idle frame (the card's
|
||||
/// moon disc alone is ~1800 ops), so the last ops are kept, the same array, until something they
|
||||
/// are drawn from changes: the card shows minutes, so that is about once a minute.
|
||||
fn DesktopShell::background(
|
||||
self : DesktopShell,
|
||||
w : Int,
|
||||
h : Int,
|
||||
theme : @scene.ThemeTokens,
|
||||
tokens : @theme.Tokens,
|
||||
) -> Array[@scene.DrawOp] {
|
||||
let at = now()
|
||||
let (dx, dy) = self.desk_pos()
|
||||
let key : BgKey = {
|
||||
w,
|
||||
h,
|
||||
theme,
|
||||
wallpaper: self.wallpaper_sel,
|
||||
x: dx,
|
||||
y: dy,
|
||||
date: (at.year, at.month, at.day, at.weekday),
|
||||
hour: at.hour,
|
||||
minute: at.minute,
|
||||
}
|
||||
if self.bg_key == Some(key) {
|
||||
return self.bg_cache
|
||||
}
|
||||
let bg : Array[@scene.DrawOp] = []
|
||||
translate(@libshell.render_wallpaper(@ui.size(w, h), tokens, self.wallpaper_sel), 0, 0, bg)
|
||||
self.wallpaper_len = bg.length()
|
||||
render_desktop_widget(dx, dy, tokens, at, bg)
|
||||
self.bg_cache = bg
|
||||
self.bg_key = Some(key)
|
||||
bg
|
||||
}
|
||||
|
||||
///| Build the (background, panel) render-lists for the current frame.
|
||||
pub fn DesktopShell::render(
|
||||
self : DesktopShell,
|
||||
@@ -823,11 +878,7 @@ pub fn DesktopShell::render(
|
||||
if self.widgets.iter().any(w => w.has_cap("icons")) {
|
||||
self.publish_icons()
|
||||
}
|
||||
let bg : Array[@scene.DrawOp] = []
|
||||
translate(@libshell.render_wallpaper(@ui.size(w, h), tokens, self.wallpaper_sel), 0, 0, bg)
|
||||
self.wallpaper_len = bg.length()
|
||||
let (dx, dy) = self.desk_pos()
|
||||
render_desktop_widget(dx, dy, tokens, now(), bg)
|
||||
let bg = self.background(w, h, theme, tokens)
|
||||
|
||||
let panel_ops : Array[@scene.DrawOp] = []
|
||||
self.comps.clear()
|
||||
|
||||
@@ -133,6 +133,16 @@ pub fn poll_read(fd : Int, timeout_ms : Int) -> Int {
|
||||
c_poll_read(fd, timeout_ms)
|
||||
}
|
||||
|
||||
///|
|
||||
#borrow(fds)
|
||||
extern "C" fn c_wait_readable(fds : FixedArray[Int], n : Int, timeout_ms : Int) -> Int = "mp_wait_readable"
|
||||
|
||||
///| Block until one of `fds` is readable or `timeout_ms` passes (how an idle loop sleeps). The
|
||||
/// number of ready fds, 0 on timeout.
|
||||
pub fn wait_readable(fds : Array[Int], timeout_ms : Int) -> Int {
|
||||
c_wait_readable(FixedArray::from_array(fds), fds.length(), timeout_ms)
|
||||
}
|
||||
|
||||
///| Receive bytes and every SCM_RIGHTS fd that came with them, in order. Returns
|
||||
/// (bytes_read, fds); bytes_read == -1 means EAGAIN, -2 an error, 0 EOF.
|
||||
pub fn recvmsg_fds(fd : Int, buf : FixedArray[Byte], max_len : Int) -> (Int, Array[Int]) {
|
||||
|
||||
@@ -119,6 +119,21 @@ int32_t mp_poll_read(int32_t fd, int32_t timeout_ms) {
|
||||
return (pfd.revents & (POLLIN | POLLHUP | POLLERR)) ? 1 : 0;
|
||||
}
|
||||
|
||||
/* Block until one of fds[0..n) is readable or timeout_ms passes; poll(2)'s result. */
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_wait_readable(int32_t *fds, int32_t n, int32_t timeout_ms) {
|
||||
struct pollfd pfd[64];
|
||||
int k = 0;
|
||||
for (int i = 0; i < n && k < 64; i++) {
|
||||
if (fds[i] < 0) continue;
|
||||
pfd[k].fd = fds[i];
|
||||
pfd[k].events = POLLIN;
|
||||
pfd[k].revents = 0;
|
||||
k++;
|
||||
}
|
||||
return poll(pfd, (nfds_t)k, timeout_ms);
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t mp_recvmsg_fd(int32_t fd, moonbit_bytes_t buf, int32_t max_len, int32_t *out_fd) {
|
||||
/* out_fd[0] = number of fds received, out_fd[1..] the fds. A client flushes up to 28 fds
|
||||
|
||||
@@ -70,6 +70,13 @@ pub fn poll_read(fd : Int, timeout_ms : Int) -> Int {
|
||||
@hostsys.poll_read(fd, timeout_ms)
|
||||
}
|
||||
|
||||
///| Block until one of `fds` is readable or `timeout_ms` passes (how an idle loop sleeps). The
|
||||
/// host also wakes on its login-session display's seat, DRM and input fds while that is open.
|
||||
/// The number of ready fds, 0 on timeout.
|
||||
pub fn wait_readable(fds : Array[Int], timeout_ms : Int) -> Int {
|
||||
@hostsys.wait_readable(FixedArray::from_array(fds), timeout_ms)
|
||||
}
|
||||
|
||||
///| Receive bytes and an optional SCM_RIGHTS fd into `buf`. Returns (bytes_read, received_fd);
|
||||
/// bytes_read == -1 means EAGAIN, -2 an error, 0 EOF.
|
||||
pub fn recvmsg_fd(fd : Int, buf : FixedArray[Byte], max_len : Int) -> (Int, Int) {
|
||||
|
||||
@@ -282,9 +282,22 @@ pub fn publish(which : HostTable, t : Table) -> Unit {
|
||||
SessionSettings => 6U
|
||||
Icons => 7U
|
||||
}
|
||||
// Most tables are republished every frame unchanged: skip the copy into the host then.
|
||||
let slot = id.reinterpret_as_int()
|
||||
while published.length() <= slot {
|
||||
published.push(None)
|
||||
}
|
||||
match published[slot] {
|
||||
Some(prev) => if prev == out { return }
|
||||
None => ()
|
||||
}
|
||||
published[slot] = Some(out)
|
||||
@hostpl.publish_table(id, out)
|
||||
}
|
||||
|
||||
///| The bytes last sent for each table, by table id.
|
||||
let published : Array[FixedArray[Byte]?] = []
|
||||
|
||||
///|
|
||||
pub fn publish_state(layout_mode : Int, inspector_on : Bool) -> Unit {
|
||||
@hostpl.publish_state(layout_mode, inspector_on)
|
||||
|
||||
@@ -1135,6 +1135,17 @@ fn WaylandServer::dispatch_msg(
|
||||
}
|
||||
}
|
||||
|
||||
///| The listening socket and every connected client: what an idle compositor waits on.
|
||||
pub fn WaylandServer::wait_fds(self : WaylandServer) -> Array[Int] {
|
||||
let out = [self.listen_fd]
|
||||
for c in self.clients {
|
||||
if c.alive {
|
||||
out.push(c.fd)
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
///| Accept new clients, read incoming Wayland messages + SCM_RIGHTS fds, and emit CompositorEvents.
|
||||
pub fn WaylandServer::poll_events(
|
||||
self : WaylandServer,
|
||||
|
||||
Reference in new issue
Block a user