Stop redrawing an unchanged desktop; faster fills and blits; --profile
CI / test (push) Failing after 31s
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:
1 parent
235f17cd70
commit
095bbc5906
6 files changed
+303
-36
No files matched your search
@@ -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,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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user