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

+232 -23
View File
@@ -593,10 +593,27 @@ fn draw_image(c: &mut Canvas, imgs: &Images, p: &wt::Picture, ox: i32, oy: i32,
}
/// Draw `ops` offset by (ox, oy), with the clip stack they push and pop.
fn draw_ops(c: &mut Canvas, fonts: &mut Fonts, imgs: &Images, ops: &[wt::Op], ox: i32, oy: i32) {
/// The overlap of two clips (None: no clip).
fn intersect(a: Clip, b: Clip) -> Clip {
match (a, b) {
(None, k) | (k, None) => k,
(Some(a), Some(b)) => {
let (x0, y0) = (a.x.max(b.x), a.y.max(b.y));
let (x1, y1) = ((a.x + a.w).min(b.x + b.w), (a.y + a.h).min(b.y + b.h));
Some(R { x: x0, y: y0, w: (x1 - x0).max(0), h: (y1 - y0).max(0) })
}
}
}
/// Draw `ops` offset by (ox, oy). `base` limits everything (the frame's damaged area); a push-clip
/// op replaces the clip within it.
fn draw_ops(c: &mut Canvas, fonts: &mut Fonts, imgs: &Images, ops: &[wt::Op], ox: i32, oy: i32, base: Clip) {
let mut stack: Vec<R> = Vec::new();
for op in ops {
let clip = stack.last().copied();
let clip = match stack.last() {
Some(k) => intersect(Some(*k), base),
None => base,
};
match op {
wt::Op::FillRect(f) => {
let r = R { x: f.area.x + ox, y: f.area.y + oy, w: f.area.w, h: f.area.h };
@@ -654,26 +671,30 @@ impl Surface {
/// Draw a client surface into `dst`. While the client catches up with a resize its buffer is smaller
/// than the window: the buffer's bottom-right colour is extended over the rest.
fn blit_surface(c: &mut Canvas, s: &Surface, dst: R) {
fn blit_surface(c: &mut Canvas, s: &Surface, dst: R, clip: Clip) {
if s.w <= 0 || s.h <= 0 || dst.w <= 0 || dst.h <= 0 {
return;
}
if s.w < dst.w || s.h < dst.h {
let o = ((s.h - 1) * s.w + (s.w - 1)) as usize * 4;
let col = wt::Color { r: s.rgba[o], g: s.rgba[o + 1], b: s.rgba[o + 2], a: s.rgba[o + 3] };
fill_rounded_rect(c, dst, col, 0, None);
fill_rounded_rect(c, dst, col, 0, clip);
}
let (copy_w, copy_h) = (s.w.min(dst.w), s.h.min(dst.h));
let c0 = (-dst.x).max(0);
let c1 = copy_w.min(c.w - dst.x);
if c0 >= c1 {
// Visible columns [c0, c1) and rows [r0, r1) of the buffer: on the canvas and inside the clip.
let (mut c0, mut c1) = ((-dst.x).max(0), copy_w.min(c.w - dst.x));
let (mut r0, mut r1) = ((-dst.y).max(0), copy_h.min(c.h - dst.y));
if let Some(k) = clip {
c0 = c0.max(k.x - dst.x);
c1 = c1.min(k.x + k.w - dst.x);
r0 = r0.max(k.y - dst.y);
r1 = r1.min(k.y + k.h - dst.y);
}
if c0 >= c1 || r0 >= r1 {
return;
}
for row in 0..copy_h {
for row in r0..r1 {
let dy = dst.y + row;
if dy < 0 || dy >= c.h {
continue;
}
let src = &s.rgba[((row * s.w + c0) * 4) as usize..((row * s.w + c1) * 4) as usize];
let d0 = ((dy * c.w + dst.x + c0) * 4) as usize;
let dst_row = &mut c.data[d0..d0 + src.len()];
@@ -711,6 +732,70 @@ pub struct Raster {
next_id: u32,
/// Images for `draw-image` ops (app icons).
pub images: Images,
/// The last frame drawn, to work out what the next one changes.
prev: Option<wt::Frame>,
/// Pixels redrawn by the last submit (for tests and profiling).
pub last_drawn: usize,
}
/// Room around a window for its drop shadow (4 px right, 6 px down) and a little slack.
const SHADOW_PAD: i32 = 8;
/// What a window covers on screen: its decoration and drop shadow, or just itself when fullscreen.
fn window_footprint(win: &wt::Window) -> R {
let g = R::from(win.geometry);
if win.fullscreen {
return g;
}
R {
x: g.x - win.inset_left - SHADOW_PAD,
y: g.y - win.inset_top - SHADOW_PAD,
w: g.w + win.inset_left + win.inset_right + 2 * SHADOW_PAD,
h: g.h + win.inset_top + win.inset_bottom + 2 * SHADOW_PAD,
}
}
/// The bounding box of two rectangles (an empty one counts for nothing).
fn union(a: R, b: R) -> R {
if a.w <= 0 || a.h <= 0 {
return b;
}
if b.w <= 0 || b.h <= 0 {
return a;
}
let (x0, y0) = (a.x.min(b.x), a.y.min(b.y));
let (x1, y1) = ((a.x + a.w).max(b.x + b.w), (a.y + a.h).max(b.y + b.h));
R { x: x0, y: y0, w: x1 - x0, h: y1 - y0 }
}
/// The part of the screen `f` changes over `prev`, as one rectangle; None when all of it must be
/// redrawn. Only window changes are narrowed down (a client drawing, a window moving, focus moving):
/// any change to the wallpaper, desktop card, panel or the set of windows redraws everything.
fn damage(prev: &wt::Frame, f: &wt::Frame) -> Option<R> {
if prev.width != f.width || prev.height != f.height || prev.windows.len() != f.windows.len() {
return None;
}
if f.background != prev.background || f.panel != prev.panel {
return None;
}
if let Some(ops) = &f.wallpaper {
if prev.wallpaper.as_ref() != Some(ops) {
return None;
}
}
let mut d = R { x: 0, y: 0, w: 0, h: 0 };
for (a, b) in prev.windows.iter().zip(f.windows.iter()) {
if a == b {
continue;
}
d = union(d, union(window_footprint(a), window_footprint(b)));
}
if prev.popups != f.popups {
for p in prev.popups.iter().chain(f.popups.iter()) {
d = union(d, R::from(p.area));
}
}
Some(d)
}
impl Raster {
@@ -739,9 +824,17 @@ impl Raster {
self.surfaces.len()
}
/// `submit_frame` for a borrowed frame (tests).
#[cfg(test)]
pub fn submit(&mut self, f: &wt::Frame, fonts: &mut Fonts) {
self.submit_frame(f.clone(), fonts)
}
/// Draw a frame into the framebuffer: wallpaper (cached), background, windows bottom to top,
/// panel, then client popups. The same layering as the software renderer's `render_scene`.
pub fn submit(&mut self, f: &wt::Frame, fonts: &mut Fonts) {
/// Only the part that differs from the last frame is redrawn (see `damage`): a client drawing
/// in its window costs that window's pixels, not the whole screen.
pub fn submit_frame(&mut self, f: wt::Frame, fonts: &mut Fonts) {
let (w, h) = (f.width.max(1), f.height.max(1));
let resized = w != self.w || h != self.h || self.fb.len() != (w * h * 4) as usize;
if resized {
@@ -756,37 +849,71 @@ impl Raster {
(Some(ops), Some((o, cw, ch, _))) => *cw == w && *ch == h && o == ops,
_ => false,
};
let clip: Clip = match (&self.prev, resized || !backdrop_ready) {
(Some(prev), false) => match damage(prev, &f) {
Some(d) => match intersect(Some(d), Some(R { x: 0, y: 0, w, h })) {
Some(k) if k.w > 0 && k.h > 0 => Some(k),
_ => {
// Nothing on screen changed (a window off screen, say).
self.last_drawn = 0;
self.keep_prev(f);
return;
}
},
None => None,
},
_ => None,
};
if !resized && !backdrop_ready {
self.fb.fill(0);
}
self.last_drawn = match clip {
Some(k) => (k.w * k.h) as usize,
None => (w * h) as usize,
};
let surfaces = &self.surfaces;
let imgs = &self.images;
let mut c = Canvas { w, h, data: &mut self.fb };
// 1. Wallpaper: drawn once, copied back while unchanged.
// 1. Wallpaper: drawn once, copied back while unchanged (only the damaged rows and columns).
let copy_backdrop = |c: &mut Canvas, px: &[u8]| match clip {
None => c.data.copy_from_slice(px),
Some(k) => {
for y in k.y..k.y + k.h {
let a = ((y * w + k.x) * 4) as usize;
let b = a + (k.w * 4) as usize;
c.data[a..b].copy_from_slice(&px[a..b]);
}
}
};
match &f.wallpaper {
Some(ops) => {
let cached = matches!(&self.wallpaper, Some((o, cw, ch, _)) if *cw == w && *ch == h && o == ops);
if cached {
c.data.copy_from_slice(&self.wallpaper.as_ref().unwrap().3);
copy_backdrop(&mut c, &self.wallpaper.as_ref().unwrap().3);
} else {
draw_ops(&mut c, fonts, imgs, ops, 0, 0);
draw_ops(&mut c, fonts, imgs, ops, 0, 0, None);
self.wallpaper = Some((ops.clone(), w, h, c.data.to_vec()));
}
}
None => {
if let Some((_, cw, ch, px)) = &self.wallpaper {
if *cw == w && *ch == h {
c.data.copy_from_slice(px);
copy_backdrop(&mut c, px);
}
}
}
}
draw_ops(&mut c, fonts, imgs, &f.background, 0, 0);
draw_ops(&mut c, fonts, imgs, &f.background, 0, 0, clip);
// 2. Windows, bottom to top.
for win in &f.windows {
let g = R::from(win.geometry);
if let Some(k) = clip {
if intersect(Some(window_footprint(win)), Some(k)).map_or(true, |r| r.w <= 0 || r.h <= 0) {
continue;
}
}
if !win.fullscreen {
let (outer_x, outer_y) = (g.x - win.inset_left, g.y - win.inset_top);
let outer_w = g.w + win.inset_left + win.inset_right;
@@ -801,23 +928,37 @@ impl Raster {
R { x: outer_x, y: outer_y, w: outer_w, h: win.inset_top + 2 },
];
for s in strips {
fill_rounded_rect(&mut c, shadow, col, 8, Some(s));
if let Some(k) = intersect(Some(s), clip) {
if k.w > 0 && k.h > 0 {
fill_rounded_rect(&mut c, shadow, col, 8, Some(k));
}
}
}
draw_ops(&mut c, fonts, imgs, &win.deco, outer_x, outer_y);
draw_ops(&mut c, fonts, imgs, &win.deco, outer_x, outer_y, clip);
}
match surfaces.get(&win.surface) {
Some(s) => blit_surface(&mut c, s, g),
None => fill_rounded_rect(&mut c, g, wt::Color { r: 0x1e, g: 0x29, b: 0x3b, a: 0xff }, 0, None),
Some(s) => blit_surface(&mut c, s, g, clip),
None => fill_rounded_rect(&mut c, g, wt::Color { r: 0x1e, g: 0x29, b: 0x3b, a: 0xff }, 0, clip),
}
}
// 3. Panel (and its popups, menus and notices), then 4. client popups above everything.
draw_ops(&mut c, fonts, imgs, &f.panel, 0, 0);
draw_ops(&mut c, fonts, imgs, &f.panel, 0, 0, clip);
for p in &f.popups {
if let Some(s) = surfaces.get(&p.surface) {
blit_surface(&mut c, s, R::from(p.area));
blit_surface(&mut c, s, R::from(p.area), clip);
}
}
self.keep_prev(f);
}
/// Remember `f` as the last frame. Its wallpaper ops are kept from the frame that sent them, so
/// a later frame that resends the same wallpaper still compares equal.
fn keep_prev(&mut self, mut f: wt::Frame) {
if f.wallpaper.is_none() {
f.wallpaper = self.prev.as_mut().and_then(|p| p.wallpaper.take());
}
self.prev = Some(f);
}
/// The framebuffer as little-endian ARGB8888 (B, G, R, A=255), `stride = w * 4`.
@@ -955,6 +1096,74 @@ mod tests {
assert_eq!(r.surface_count(), 0);
}
/// A window with a title-bar decoration over a patterned backdrop and a panel.
fn scene_window(r: &mut Raster, x: i32, y: i32, shade: u8, focused: bool) -> wt::Window {
let bgra: Vec<u8> = (0..(30 * 20)).flat_map(|i| [shade, (i % 251) as u8, 40, if i % 7 == 0 { 90 } else { 255 }]).collect();
let id = r.add_surface(Surface::from_bgra(&bgra, 30 * 4, 30, 20, false).unwrap());
wt::Window {
geometry: rect(x, y, 30, 20),
inset_top: 8,
inset_right: 2,
inset_bottom: 2,
inset_left: 2,
deco: vec![fill(0, 0, 34, 30, if focused { color(60, 90, 200, 230) } else { color(90, 90, 90, 200) }, 3)],
fullscreen: false,
surface: id,
}
}
fn scene_frame(windows: Vec<wt::Window>) -> wt::Frame {
let mut f = frame(120, 90);
f.wallpaper = Some((0..12).map(|i| fill(i * 10, 0, 10, 90, color((i * 20) as u8, 100, 200 - (i * 10) as u8, 255), 0)).collect());
f.windows = windows;
f.panel = vec![fill(0, 80, 120, 10, color(20, 20, 30, 220), 0), fill(4, 82, 6, 6, color(250, 250, 250, 255), 3)];
f
}
#[test]
fn redrawing_only_the_damage_gives_the_same_pixels_as_a_full_redraw() {
let mut fonts = Fonts::default();
// Frame 1 (full), then steps that only change windows: a client redraw, a move, a focus change.
let mut inc = Raster::default();
let w1 = scene_window(&mut inc, 10, 15, 10, false);
let w2 = scene_window(&mut inc, 30, 25, 200, true);
inc.submit(&scene_frame(vec![w1.clone(), w2.clone()]), &mut fonts);
assert_eq!(inc.last_drawn, 120 * 90);
let mut new_w1 = scene_window(&mut inc, 10, 15, 99, false); // the client drew a new buffer
let steps = vec![
vec![new_w1.clone(), w2.clone()],
{
new_w1.geometry = rect(60, 40, 30, 20); // moved, now overlapping the panel
vec![new_w1.clone(), w2.clone()]
},
{
let mut a = new_w1.clone();
a.deco = vec![fill(0, 0, 34, 30, color(60, 90, 200, 230), 3)];
let mut b = w2.clone();
b.deco = vec![fill(0, 0, 34, 30, color(90, 90, 90, 200), 3)];
vec![b, a] // focus moved and the stacking order swapped
},
];
for (i, wins) in steps.into_iter().enumerate() {
let f = scene_frame(wins);
let mut f_inc = f.clone();
f_inc.wallpaper = None; // the compositor sends an unchanged backdrop as None
inc.submit(&f_inc, &mut fonts);
assert!(inc.last_drawn < 120 * 90, "step {i}: only part of the screen is redrawn");
// The same frame drawn from scratch, with the same surfaces.
let mut full = Raster::default();
for (id, s) in &inc.surfaces {
full.surfaces.insert(*id, Surface { w: s.w, h: s.h, rgba: s.rgba.clone(), opaque: s.opaque });
}
full.submit(&f, &mut fonts);
assert!(full.fb == inc.fb, "step {i}: partial redraw differs from a full one");
}
// An unchanged frame draws nothing.
let f = inc.prev.clone().unwrap();
inc.submit(&f, &mut fonts);
assert_eq!(inc.last_drawn, 0);
}
#[test]
fn translucent_surfaces_blend_over_what_is_behind() {
let mut r = Raster::default();