Files
Claude 442f9477b4
CI / test (push) Failing after 19s
CPU, round two: cheaper client captures, content-only redraws, damage-limited KMS and nested output
Measured on the MoonBit host, 1080p, cloud container (two runs each, before -> after):
- full-screen (tiled) client redrawing at 60 fps: 43/39% -> 31/35%
- 800x600 floating client at 60 fps: 15.5/13.2% -> 10.4/11.9%
- client inside the nested window: nested 18% -> 12%, outer 17% -> 11.6%
- Alt+Tab open over two animating clients: 36.6% -> 25.4%
- idle (headless / nested): 0.2-0.6% -> 0.1-0.3%

wasmhost raster:
- capture keeps client pools mapped (keyed by dev/inode/size, dropped after 1 s unused) and
  reuses released surface buffers, swizzling a word at a time: 3.2 -> 2.1 ms per 1080p commit.
- a window whose only change is a new buffer damages just its client area, and the topmost
  opaque buffer that covers the damage is drawn first (no backdrop copy, desktop card, lower
  windows or its own decoration under it): submit 3.2 -> 0.8 ms per full-screen frame.
- the framebuffer tracks what changed since each output took it. The nested window's frames
  are a ring of three mapped memfds, each updated only where the screen changed, instead of
  a new memfd with a full 8 MB conversion and write every frame.
- KMS: present copies only the rows the back buffer is missing (this frame's change and the
  last one's), and kms_stub.c is compiled with -O2 (its stub-cc-flags dropped moon's default,
  so the per-frame copy ran unoptimised).
- images re-filled under the same handle (Alt+Tab previews) damage where the panel draws them:
  before, a live preview only repainted where it overlapped the window that redrew.

compositor:
- Alt+Tab previews of a window that keeps redrawing are remade at most every 100 ms (scaling a
  1080p buffer takes ~2 ms); the loop keeps its pace while one is pending.
- set-tokens is sent to the host only when the theme changes (it was 9 calls a frame).
- /proc is sampled only when a module imports `system`; a machine without a battery looks for
  one every 30 s instead of probing eight sysfs paths every second.

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

105 lines
4.0 KiB
MoonBit

///|
test "thumb_fit keeps the aspect ratio and never enlarges" {
assert_eq(@host.thumb_fit(1280, 720, 220, 138), (220, 123))
assert_eq(@host.thumb_fit(600, 1200, 220, 138), (69, 138))
assert_eq(@host.thumb_fit(100, 50, 220, 138), (100, 50))
assert_eq(@host.thumb_fit(4000, 1, 220, 138), (220, 1))
assert_eq(@host.thumb_fit(0, 50, 220, 138), (0, 0))
}
///|
fn find_image(ops : Array[@scene.DrawOp], handle : UInt) -> @scene.Rect? {
for op in ops {
match op {
Image(r, h) if h == handle => return Some(r)
_ => ()
}
}
None
}
///|
fn has_text(ops : Array[@scene.DrawOp], s : String) -> Bool {
ops.iter().any(op => op is Text(_, _, t, _, _, _, _) && t == s)
}
///|
test "alt-tab shows a preview of each window's last frame, made only when needed" {
let socket_path = "/tmp/wayland-moon-thumb-" + @sys.getpid().to_string() + ".sock"
let theme = @scene.ThemeTokens::moon_night()
let comp = @host.ChandraCompositor::new(socket_path, theme, 1280, 720).unwrap()
let (c1, shm1, surf1, xdg1, _, _) = open_and_configure_client(comp, socket_path, "A", "a")
commit_shm_frame(comp, c1, shm1, surf1, xdg1, 1UL)
let (c2, shm2, surf2, xdg2, _, _) = open_and_configure_client(comp, socket_path, "B", "b")
commit_shm_frame(comp, c2, shm2, surf2, xdg2, 2UL)
// C maps no buffer at all.
ignore(open_and_configure_client(comp, socket_path, "C", "c"))
for _i = 0; _i < 10; _i = _i + 1 {
comp.step()
}
ignore(comp.compose_scene())
// Nothing is scaled while the switcher is closed.
assert_eq(comp.thumbs.length(), 0)
let key = fn(code : UInt, down : Bool, t : UInt) {
comp.handle_input(@nested.InputEvent::Key(code, down), t)
}
key(56U, true, 1U)
key(15U, true, 2U)
assert_true(comp.switcher_open())
let sg = comp.compose_scene().unwrap()
let t1 = comp.thumbs.get(1UL).unwrap()
assert_true(t1.handle != 0U)
assert_false(t1.stale)
// The aspect ratio of the window's buffer is kept.
let buf = comp.windows.get(1UL).unwrap().surface.unwrap()
assert_true((t1.w * buf.height - t1.h * buf.width).abs() <= buf.width)
assert_true(comp.thumbs.get(2UL).unwrap().handle != 0U)
assert_true(comp.thumbs.get(1UL).unwrap().handle != comp.thumbs.get(2UL).unwrap().handle)
// No buffer yet: no preview (an icon or letter tile stands in).
assert_true(comp.thumbs.get(3UL) is None)
// The preview shows the window's pixels: the test frame has a bright square in its middle.
let r = find_image(sg.panel_ops, t1.handle).unwrap()
assert_eq((r.w, r.h), (t1.w, t1.h))
let px = @renderer.render_scene(sg)
match px.pixel(r.x + r.w / 2, r.y + r.h / 2) {
Some(c) => assert_eq((c.r, c.g, c.b), (b'\x38', b'\xbd', b'\xf8'))
None => abort("no pixel")
}
match px.pixel(r.x + 2, r.y + 2) {
Some(c) => assert_eq((c.r, c.g, c.b), (b'\x1e', b'\x29', b'\x3b'))
None => abort("no pixel")
}
// A new frame from the window marks its preview stale; it's remade into the same handle.
let h1 = t1.handle
commit_shm_frame(comp, c1, shm1, surf1, xdg1, 1UL)
for _i = 0; _i < 5; _i = _i + 1 {
comp.step()
}
assert_true(comp.thumbs.get(1UL).unwrap().stale)
// It is remade at most every 100 ms (a video would otherwise cost a scale every frame); until
// then the main loop keeps its frame pace for it.
assert_true(comp.has_timed_work())
@sys.sleep_ms(110)
ignore(comp.compose_scene())
assert_false(comp.thumbs.get(1UL).unwrap().stale)
assert_false(comp.has_timed_work())
assert_eq(comp.thumbs.get(1UL).unwrap().handle, h1)
key(1U, true, 3U)
key(56U, false, 4U)
assert_false(comp.switcher_open())
// Minimize everything (Super+D): minimized windows keep their last frame as the preview.
key(125U, true, 10U)
key(32U, true, 11U)
key(32U, false, 12U)
key(125U, false, 13U)
assert_true(comp.wm.windows.get(1UL).unwrap().minimized)
key(56U, true, 20U)
key(15U, true, 21U)
assert_true(comp.switcher_open())
let sg = comp.compose_scene().unwrap()
assert_true(find_image(sg.panel_ops, h1) is Some(_))
assert_true(has_text(sg.panel_ops, "minimized"))
key(1U, true, 22U)
key(56U, false, 23U)
}