Stop redrawing an unchanged desktop; faster fills and blits; --profile
CI / test (push) Failing after 31s

Profiling the wasm compositor at 1080p idle: ~90 ms per frame in the software rasteriser, run
every loop iteration (92% of a core, stuttery). Now:
- compose_frame compares what the frame is made from (shell ops, decorations, popups, client
  damage) with the last one and returns the previous pixels when nothing changed; the DRM and
  nested loops only present a changed frame (held until the display is ready).
- fill_rounded_rect fills spans (circle test only in the corner bands) and blit_shm_surface
  copies rows without per-pixel calls; tests check both against the old per-pixel code.
- snapshot_shm_buffer sliced buffers byte by byte; now a slice copy.
- --profile prints per-3s step / shell / raster timings.
Idle CPU at 1080p: 92% -> ~10%.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
nandiandClaude Sonnet 5.5 committed 2026-11-25 03:35:06 -08:00
1 parent 235f17cd70
commit 095bbc5906
6 files changed
+301 -34

No files matched your search

+54 -4
View File
@@ -47,6 +47,7 @@ pub fn run(args : Array[String]) -> Int {
let mut gfx_out = ""
let mut windowed = false
let mut serve = false
let mut profile = false
let mut drm = false
let mut width = 1280
let mut height = 720
@@ -66,6 +67,9 @@ pub fn run(args : Array[String]) -> Int {
} else if a == "--drm" {
drm = true
i = i + 1
} else if a == "--profile" {
profile = true
i = i + 1
} else if a == "--serve" {
serve = true
i = i + 1
@@ -166,8 +170,16 @@ pub fn run(args : Array[String]) -> Int {
}
@sys.log("moon-plasma compositor listening on " + socket_path)
let mut frames = 0
// 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.
let mut p_t0 = @sys.monotonic_ms()
let mut p_n = 0
let mut p_step = 0UL
let mut p_comp = 0UL
while true {
frames = frames + 1
let f0 = @sys.monotonic_ms()
match kms {
Some(k) => {
if !k.poll() || comp.quit_requested {
@@ -178,9 +190,28 @@ pub fn run(args : Array[String]) -> Int {
comp.handle_input(ev, now_ms)
}
comp.step()
let f1 = @sys.monotonic_ms()
k.set_cursor_shape(comp.desired_cursor())
if k.frame_ready() {
k.present(comp.present_to_gfx(gfx_ctx))
let px = comp.compose_frame()
need_present = need_present || comp.frame_changed
if need_present && k.frame_ready() {
k.present(px)
need_present = false
}
if profile {
p_n = p_n + 1
p_step = p_step + (f1 - f0)
p_comp = p_comp + (@sys.monotonic_ms() - f1)
let now = @sys.monotonic_ms()
if now - p_t0 >= 3000UL {
@sys.log("profile: " + p_n.to_string() + " frames/3s step " + p_step.to_string() + " ms compose+present " + p_comp.to_string() + " ms (shell " + comp.prof_shell_ms.to_string() + " ms, raster " + comp.prof_raster_ms.to_string() + " ms)")
p_t0 = now
p_n = 0
p_step = 0UL
p_comp = 0UL
comp.prof_shell_ms = 0UL
comp.prof_raster_ms = 0UL
}
}
@sys.sleep_ms(8)
continue
@@ -188,6 +219,7 @@ pub fn run(args : Array[String]) -> Int {
None => ()
}
comp.step()
let f1 = @sys.monotonic_ms()
if comp.quit_requested {
break
}
@@ -204,8 +236,11 @@ pub fn run(args : Array[String]) -> Int {
comp.handle_input(ev, now_ms)
}
n.set_cursor_shape(comp.desired_cursor())
if n.frame_ready() {
n.present(comp.present_to_gfx(gfx_ctx))
let px = comp.compose_frame()
need_present = need_present || comp.frame_changed
if need_present && n.frame_ready() {
n.present(px)
need_present = false
}
}
None => {
@@ -216,6 +251,21 @@ pub fn run(args : Array[String]) -> Int {
}
}
}
if profile {
p_n = p_n + 1
p_step = p_step + (f1 - f0)
p_comp = p_comp + (@sys.monotonic_ms() - f1)
let now = @sys.monotonic_ms()
if now - p_t0 >= 3000UL {
@sys.log("profile: " + p_n.to_string() + " frames/3s step " + p_step.to_string() + " ms compose+present " + p_comp.to_string() + " ms (shell " + comp.prof_shell_ms.to_string() + " ms, raster " + comp.prof_raster_ms.to_string() + " ms)")
p_t0 = now
p_n = 0
p_step = 0UL
p_comp = 0UL
comp.prof_shell_ms = 0UL
comp.prof_raster_ms = 0UL
}
}
@sys.sleep_ms(16)
}
match kms {
+50 -2
View File
@@ -53,6 +53,14 @@ pub(all) struct MoonPlasmaCompositor {
mut layout_mode : @wm.LayoutMode
/// Set when the user asked to end the session (the logout button); the main loop then exits.
mut quit_requested : Bool
/// Milliseconds spent so far in the shell (panel modules) and in the rasteriser, for --profile.
/// The last composed frame and what it was made from: an unchanged desktop is not redrawn.
mut last_frame : @renderer.Pixmap?
mut last_sig : FrameSig?
/// True when the frame returned by the latest compose_frame differs from the one before it.
mut frame_changed : Bool
mut prof_shell_ms : UInt64
mut prof_raster_ms : UInt64
}
///|
@@ -102,6 +110,11 @@ pub fn MoonPlasmaCompositor::new(
super_solo: false,
layout_mode: @wm.LayoutMode::Tiling,
quit_requested: false,
last_frame: None,
last_sig: None,
frame_changed: true,
prof_shell_ms: 0UL,
prof_raster_ms: 0UL,
})
}
@@ -111,6 +124,15 @@ pub fn MoonPlasmaCompositor::close(self : MoonPlasmaCompositor) -> Unit {
self.server.close()
}
///| 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]
wins : Array[(UInt64, @scene.Rect, Bool, Bool, Array[@scene.DrawOp])]
popups : Array[@scene.Rect]
}
///|
fn MoonPlasmaCompositor::apply_actions(
self : MoonPlasmaCompositor,
@@ -433,12 +455,36 @@ pub fn MoonPlasmaCompositor::compose_frame(
}
}
self.shell.win_list = entries
let t0 = @sys.monotonic_ms()
let (bg_ops, panel_ops) = self.shell.render(self.output_w, self.output_h, self.theme)
self.run_shell_actions()
scene.background_ops = bg_ops
scene.panel_ops = panel_ops
ignore(self.damage.take_damage(self.output_w, self.output_h))
@renderer.render_scene(scene)
let damaged = self.damage.take_damage(self.output_w, self.output_h).length() > 0
let sig : FrameSig = {
bg: bg_ops,
panel: panel_ops,
wins: scene.windows.map(w => (w.id, w.geometry, w.focused, w.fullscreen, w.deco_ops)),
popups: scene.popups.map(p => p.0),
}
// Nothing changed since the last frame (no client commit, same shell and decoration ops):
// hand back the previous pixels instead of rasterising 2 million pixels again.
match (self.last_frame, self.last_sig) {
(Some(prev), Some(ps)) =>
if !damaged && ps == sig {
self.frame_changed = false
return prev
}
_ => ()
}
self.frame_changed = true
self.last_sig = Some(sig)
let t1 = @sys.monotonic_ms()
let px = @renderer.render_scene(scene)
self.last_frame = Some(px)
self.prof_shell_ms = self.prof_shell_ms + (t1 - t0)
self.prof_raster_ms = self.prof_raster_ms + (@sys.monotonic_ms() - t1)
px
}
///|
@@ -449,7 +495,9 @@ pub fn MoonPlasmaCompositor::present_to_gfx(
ctx : @gfx.SurfaceFrameBufferContext,
) -> @renderer.Pixmap {
let pixmap = self.compose_frame()
if self.frame_changed {
@gfx.present_pixmap(ctx, pixmap)
}
pixmap
}
+78
View File
@@ -0,0 +1,78 @@
///| The old per-pixel fill, kept as the reference the span-based fill must match exactly.
fn reference_fill(dst : Pixmap, rect : @scene.Rect, color : @scene.Rgba, radius : Int, clip : @scene.Rect?) -> Unit {
let x0 = if rect.x < 0 { 0 } else { rect.x }
let y0 = if rect.y < 0 { 0 } else { rect.y }
let x1 = if rect.x + rect.w > dst.width { dst.width } else { rect.x + rect.w }
let y1 = if rect.y + rect.h > dst.height { dst.height } else { rect.y + rect.h }
for y = y0; y < y1; y = y + 1 {
for x = x0; x < x1; x = x + 1 {
if radius == 0 || in_rounded_rect(x, y, rect.x, rect.y, rect.w, rect.h, radius) {
blend_pixel(dst, x, y, color.r.to_int(), color.g.to_int(), color.b.to_int(), color.a.to_int(), clip)
}
}
}
}
///|
test "span-based rounded fills match the per-pixel reference" {
let rects = [
(0, 0, 40, 30), (5, 7, 33, 21), (-6, -4, 30, 20), (30, 20, 40, 30), (10, 10, 3, 3),
(2, 2, 1, 9), (12, 3, 26, 2), (-10, 15, 80, 8), (20, 5, 7, 40),
]
let colors = [
@scene.Rgba::new(200, 40, 90, 255), @scene.Rgba::new(10, 200, 120, 128), @scene.Rgba::new(255, 255, 255, 17),
]
let clips : Array[@scene.Rect?] = [None, Some(@scene.Rect::new(8, 6, 25, 20)), Some(@scene.Rect::new(-5, -5, 20, 20))]
for rc in rects {
for radius in [0, 1, 4, 9, 40] {
for col in colors {
for clip in clips {
let a = Pixmap::new(64, 48)
let b = Pixmap::new(64, 48)
for i = 0; i < a.data.length(); i = i + 1 {
let v = ((i * 37 + 11) % 251).to_byte()
a.data[i] = v
b.data[i] = v
}
let r = @scene.Rect::new(rc.0, rc.1, rc.2, rc.3)
fill_rounded_rect(a, r, col, radius, clip)
if col.a.to_int() > 0 {
reference_fill(b, r, col, radius, clip)
}
assert_eq(a.data, b.data)
}
}
}
}
}
///|
test "blitting a client buffer matches per-pixel blending, even partly off screen" {
let w = 20
let h = 12
let bytes = FixedArray::make(w * h * 4, b'\x00')
for i = 0; i < bytes.length(); i = i + 1 {
bytes[i] = ((i * 53 + 7) % 256).to_byte()
}
let raw = Bytes::from_fixedarray(bytes)
for format in [0, 1] {
for pos in [(0, 0), (5, 4), (-7, -3), (50, 40), (60, 30)] {
let buf : @scene.ShmBufferSnapshot = { width: w, height: h, stride: w * 4, format, bytes: raw }
let a = Pixmap::new(64, 48)
let b = Pixmap::new(64, 48)
for i = 0; i < a.data.length(); i = i + 1 {
a.data[i] = ((i * 29 + 3) % 253).to_byte()
b.data[i] = a.data[i]
}
blit_shm_surface(a, buf, @scene.Rect::new(pos.0, pos.1, w, h))
for row = 0; row < h; row = row + 1 {
for col = 0; col < w; col = col + 1 {
let o = row * w * 4 + col * 4
let al = if format == 1 { 255 } else { raw[o + 3].to_int() }
blend_pixel(b, pos.0 + col, pos.1 + row, raw[o + 2].to_int(), raw[o + 1].to_int(), raw[o].to_int(), al, None)
}
}
assert_eq(a.data, b.data)
}
}
}
+115 -23
View File
@@ -115,6 +115,41 @@ fn in_rounded_rect(
}
}
///| Blend a colour over the pixels [xa, xb) of row `y`. The caller has already clipped the span to
/// the pixmap and the clip rectangle, so this is a bare loop over the buffer.
fn blend_span(dst : Pixmap, y : Int, xa : Int, xb : Int, r : Int, g : Int, b : Int, a : Int) -> Unit {
if xa >= xb || a <= 0 {
return
}
let data = dst.data
let mut off = (y * dst.width + xa) * 4
let end = (y * dst.width + xb) * 4
if a >= 255 {
let rb = r.to_byte()
let gb = g.to_byte()
let bb = b.to_byte()
while off < end {
data[off] = rb
data[off + 1] = gb
data[off + 2] = bb
data[off + 3] = b'\xff'
off = off + 4
}
return
}
let inv = 255 - a
let ra = r * a + 127
let ga = g * a + 127
let ba = b * a + 127
while off < end {
data[off] = ((ra + data[off].to_int() * inv) / 255).to_byte()
data[off + 1] = ((ga + data[off + 1].to_int() * inv) / 255).to_byte()
data[off + 2] = ((ba + data[off + 2].to_int() * inv) / 255).to_byte()
data[off + 3] = (a + (data[off + 3].to_int() * inv + 127) / 255).clamp(min=0, max=255).to_byte()
off = off + 4
}
}
///|
fn fill_rounded_rect(
dst : Pixmap,
@@ -126,24 +161,64 @@ fn fill_rounded_rect(
if rect.w <= 0 || rect.h <= 0 || color.a.to_int() == 0 {
return
}
let x0 = if rect.x < 0 { 0 } else { rect.x }
let y0 = if rect.y < 0 { 0 } else { rect.y }
let x1 = if rect.x + rect.w > dst.width { dst.width } else { rect.x + rect.w }
let y1 = if rect.y + rect.h > dst.height {
dst.height
} else {
rect.y + rect.h
// Visible part of the rectangle: the pixmap, intersected with the clip.
let mut x0 = if rect.x < 0 { 0 } else { rect.x }
let mut y0 = if rect.y < 0 { 0 } else { rect.y }
let mut x1 = if rect.x + rect.w > dst.width { dst.width } else { rect.x + rect.w }
let mut y1 = if rect.y + rect.h > dst.height { dst.height } else { rect.y + rect.h }
match clip {
Some(c) => {
if c.x > x0 { x0 = c.x }
if c.y > y0 { y0 = c.y }
if c.x + c.w < x1 { x1 = c.x + c.w }
if c.y + c.h < y1 { y1 = c.y + c.h }
}
None => ()
}
if x0 >= x1 || y0 >= y1 {
return
}
let cr = color.r.to_int()
let cg = color.g.to_int()
let cb = color.b.to_int()
let ca = color.a.to_int()
if radius <= 0 {
for y = y0; y < y1; y = y + 1 {
for x = x0; x < x1; x = x + 1 {
if radius == 0 ||
in_rounded_rect(x, y, rect.x, rect.y, rect.w, rect.h, radius) {
blend_pixel(dst, x, y, cr, cg, cb, ca, clip)
blend_span(dst, y, x0, x1, cr, cg, cb, ca)
}
return
}
// Rounded: the rows outside the corner bands are plain spans; inside a band only the pixels
// within `r` of a side need the circle test (exactly the test `in_rounded_rect` applies).
let max_r = (if rect.w < rect.h { rect.w } else { rect.h }) / 2
let r = if radius > max_r { max_r } else { radius }
let x_left = rect.x + r
let x_right = rect.x + rect.w - r - 1
let y_top = rect.y + r
let y_bot = rect.y + rect.h - r - 1
for y = y0; y < y1; y = y + 1 {
if y >= y_top && y <= y_bot {
blend_span(dst, y, x0, x1, cr, cg, cb, ca)
continue
}
// Corner band: left corner pixels, middle span, right corner pixels.
let mut x = x0
while x < x1 && x < x_left {
if in_rounded_rect(x, y, rect.x, rect.y, rect.w, rect.h, radius) {
blend_span(dst, y, x, x + 1, cr, cg, cb, ca)
}
x = x + 1
}
let mid_a = if x_left > x0 { x_left } else { x0 }
let mid_b = if x_right + 1 < x1 { x_right + 1 } else { x1 }
blend_span(dst, y, mid_a, mid_b, cr, cg, cb, ca)
x = if x_right + 1 > x0 { x_right + 1 } else { x0 }
if x < mid_a { x = mid_a }
while x < x1 {
if in_rounded_rect(x, y, rect.x, rect.y, rect.w, rect.h, radius) {
blend_span(dst, y, x, x + 1, cr, cg, cb, ca)
}
x = x + 1
}
}
}
@@ -617,25 +692,42 @@ pub fn blit_shm_surface(
let copy_w = if buf.width < dst_rect.w { buf.width } else { dst_rect.w }
let copy_h = if buf.height < dst_rect.h { buf.height } else { dst_rect.h }
let is_xrgb = buf.format == 1
// Visible columns/rows only (the window may hang off the screen).
let c0 = if dst_rect.x < 0 { 0 - dst_rect.x } else { 0 }
let c1 = if dst_rect.x + copy_w > dst.width { dst.width - dst_rect.x } else { copy_w }
let src = buf.bytes
let data = dst.data
for row = 0; row < copy_h; row = row + 1 {
let dy = dst_rect.y + row
if dy < 0 || dy >= dst.height {
continue
}
for col = 0; col < copy_w; col = col + 1 {
let dx = dst_rect.x + col
if dx < 0 || dx >= dst.width {
continue
let mut col = c0
let mut so = row * buf.stride + c0 * 4
let mut d = (dy * dst.width + dst_rect.x + c0) * 4
// Never read past the buffer, even if a client lied about its stride.
let last = if row * buf.stride + c1 * 4 > src.length() {
(src.length() - row * buf.stride) / 4
} else {
c1
}
let src_off = row * buf.stride + col * 4
if src_off + 3 >= buf.bytes.length() {
continue
while col < last {
let a = if is_xrgb { 255 } else { src[so + 3].to_int() }
if a >= 255 {
data[d] = src[so + 2]
data[d + 1] = src[so + 1]
data[d + 2] = src[so]
data[d + 3] = b'\xff'
} else if a > 0 {
let inv = 255 - a
data[d] = ((src[so + 2].to_int() * a + data[d].to_int() * inv + 127) / 255).to_byte()
data[d + 1] = ((src[so + 1].to_int() * a + data[d + 1].to_int() * inv + 127) / 255).to_byte()
data[d + 2] = ((src[so].to_int() * a + data[d + 2].to_int() * inv + 127) / 255).to_byte()
data[d + 3] = (a + (data[d + 3].to_int() * inv + 127) / 255).clamp(min=0, max=255).to_byte()
}
let b = buf.bytes[src_off].to_int()
let g = buf.bytes[src_off + 1].to_int()
let r = buf.bytes[src_off + 2].to_int()
let a = if is_xrgb { 255 } else { buf.bytes[src_off + 3].to_int() }
blend_pixel(dst, dx, dy, r, g, b, a, None)
col = col + 1
so = so + 4
d = d + 4
}
}
}
+1
View File
@@ -218,6 +218,7 @@ pub(all) enum TextAlign {
}
///|
#derive(Eq)
pub(all) enum DrawOp {
FillRect(Rect, Rgba, Int)
StrokeRect(Rect, Rgba, Int, Int)
+3 -5
View File
@@ -390,14 +390,12 @@ fn snapshot_shm_buffer(
return None
}
let byte_len = buf.stride * buf.height
// The buffer's bytes within the pool: a slice copy, not a byte-by-byte loop (a full-screen
// buffer is 8 MB and this runs on every commit).
let slice = if buf.offset == 0 && raw.length() == byte_len {
raw
} else {
let arr : Array[Byte] = Array(capacity=byte_len)
for i = 0; i < byte_len; i = i + 1 {
arr.push(raw[buf.offset + i])
}
Bytes::from_array(arr)
raw[buf.offset:buf.offset + byte_len].to_bytes()
}
Some({
width: buf.width,