Idle desktop sleeps instead of redrawing; only changed parts of the screen are redrawn
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:
Claude committed 2026-11-25 19:21:39 +00:00
1 parent 2d9f4e34c5
commit ba82d3c59b
19 files changed
+542 -68

No files matched your search

+63 -11
View File
@@ -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 }
}