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
+303 -36

No files matched your search

+116 -24
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 {
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 {
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)
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
}
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 src_off = row * buf.stride + col * 4
if src_off + 3 >= buf.bytes.length() {
continue
}
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
}
}
}