CPU, round two: cheaper client captures, content-only redraws, damage-limited KMS and nested output
CI / test (push) Failing after 19s

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
This commit is contained in:
Claude committed 2026-11-25 21:16:41 +00:00
1 parent 99c6c66726
commit 442f9477b4
11 files changed
+455 -61

No files matched your search

+53 -14
View File
@@ -32,6 +32,23 @@
#define KEY_F11_CODE 87
#define KEY_F12_CODE 88
/* ---- damage ---- */
/* What each scanout buffer is missing of the frame: the part changed since it was last written
* (x0, y0, x1, y1; empty when x0 >= x1). Both start (and after a seat switch, are) fully stale. */
static int g_stale[2][4];
static void stale_add(int x0, int y0, int x1, int y1) {
for (int i = 0; i < 2; i++) {
int *s = g_stale[i];
if (s[0] >= s[2] || s[1] >= s[3]) { s[0] = x0; s[1] = y0; s[2] = x1; s[3] = y1; continue; }
if (x0 < s[0]) s[0] = x0;
if (y0 < s[1]) s[1] = y0;
if (x1 > s[2]) s[2] = x1;
if (y1 > s[3]) s[3] = y1;
}
}
/* ---- seat ---------------------------------------------------------------- */
static struct libseat *g_seat;
@@ -42,6 +59,7 @@ static int g_suspended; /* VT switched away; resume when the seat is enabled aga
static void seat_enable(struct libseat *s, void *u) {
(void)s; (void)u;
g_seat_enabled = 1;
stale_add(0, 0, 1 << 16, 1 << 16); /* another session may have drawn in the buffers */
}
static void seat_disable(struct libseat *s, void *u) {
@@ -401,6 +419,7 @@ int32_t mp_kms_open(void) {
return -4;
}
g_front = 0;
stale_add(0, 0, 1 << 16, 1 << 16);
do_modeset();
/* Hardware cursor (optional: fall back to no cursor if unsupported). */
@@ -487,25 +506,40 @@ int32_t mp_kms_frame_ready(void) {
return g_seat_enabled && !g_flip_pending && !g_blank;
}
/* RGBA8888 rows -> the scanout buffer's XRGB8888 (B, G, R, X), one pixel as one 32-bit word. */
static void copy_rows(struct fbuf *b, const uint8_t *src, int w, int x0, int y0, int x1, int y1) {
for (int y = y0; y < y1; y++) {
uint32_t *d = (uint32_t *)(b->map + (size_t)y * b->pitch);
const uint32_t *s = (const uint32_t *)(src + (size_t)y * (size_t)w * 4);
for (int x = x0; x < x1; x++) {
uint32_t v = s[x];
d[x] = 0xff000000u | (v & 0x0000ff00u) | ((v >> 16) & 0xffu) | ((v & 0xffu) << 16);
}
}
}
/* Present an RGBA8888 frame of w x h (must match the mode). */
MOONBIT_FFI_EXPORT
int32_t mp_kms_present(moonbit_bytes_t pixels, int32_t w, int32_t h) {
int32_t mp_kms_present(moonbit_bytes_t pixels, int32_t w, int32_t h);
/* Show an RGBA8888 frame of which only (dx, dy, dw, dh) changed since the last call: only what the
* back buffer is missing is copied into it (the change now, and the one shown last time). */
MOONBIT_FFI_EXPORT
int32_t mp_kms_present_damage(moonbit_bytes_t pixels, int32_t w, int32_t h, int32_t dx, int32_t dy, int32_t dw,
int32_t dh) {
int cw = w < g_width ? w : g_width;
int ch = h < g_height ? h : g_height;
int x0 = dx < 0 ? 0 : dx, y0 = dy < 0 ? 0 : dy;
int x1 = dx + dw > cw ? cw : dx + dw, y1 = dy + dh > ch ? ch : dy + dh;
if (x0 < x1 && y0 < y1) stale_add(x0, y0, x1, y1);
if (!g_seat_enabled || g_flip_pending || g_blank) return 0;
int back = 1 - g_front;
struct fbuf *b = &g_fb[back];
int cw = w < g_width ? w : g_width;
int ch = h < g_height ? h : g_height;
const uint8_t *src = (const uint8_t *)pixels;
for (int y = 0; y < ch; y++) {
uint8_t *dst = b->map + (size_t)y * b->pitch;
const uint8_t *s = src + (size_t)y * (size_t)w * 4;
for (int x = 0; x < cw; x++) {
dst[x * 4 + 0] = s[x * 4 + 2]; /* B */
dst[x * 4 + 1] = s[x * 4 + 1]; /* G */
dst[x * 4 + 2] = s[x * 4 + 0]; /* R */
dst[x * 4 + 3] = 0xff;
}
}
int *s = g_stale[back];
int sx0 = s[0] < 0 ? 0 : s[0], sy0 = s[1] < 0 ? 0 : s[1];
int sx1 = s[2] > cw ? cw : s[2], sy1 = s[3] > ch ? ch : s[3];
if (sx0 < sx1 && sy0 < sy1) copy_rows(b, (const uint8_t *)pixels, w, sx0, sy0, sx1, sy1);
s[0] = s[1] = s[2] = s[3] = 0;
if (drmModePageFlip(g_drm, g_crtc, b->fb, DRM_MODE_PAGE_FLIP_EVENT, NULL) < 0) {
/* Fall back to a synchronous set. */
if (drmModeSetCrtc(g_drm, g_crtc, b->fb, 0, 0, &g_conn, 1, &g_mode) < 0) return -1;
@@ -516,6 +550,11 @@ int32_t mp_kms_present(moonbit_bytes_t pixels, int32_t w, int32_t h) {
return 1;
}
MOONBIT_FFI_EXPORT
int32_t mp_kms_present(moonbit_bytes_t pixels, int32_t w, int32_t h) {
return mp_kms_present_damage(pixels, w, h, 0, 0, w, h);
}
/* Cursor image by wp_cursor_shape_v1 value (4 pointer hand, 9 text, else arrow). */
MOONBIT_FFI_EXPORT
void mp_kms_set_cursor_shape(int32_t shape) {
+3 -1
View File
@@ -15,7 +15,9 @@ options(
"native-stub": [ "kms_stub.c" ],
link: {
"native": {
"stub-cc-flags": "-I/usr/include/libdrm",
// -O2: setting stub flags drops moon's default ones, and the per-frame pixel copy into the
// scanout buffer is several times slower unoptimised.
"stub-cc-flags": "-O2 -I/usr/include/libdrm",
},
},
)