Smoother animations without a GPU: faster rasteriser, frame pacing, software/hardware mode
CI / test (push) Failing after 3m2s

Measured headless at 1920x1080 with CHANDRA_FRAME_TRACE (new: one log line per drawn frame):
- Overview open: 46 -> 62 fps, worst frame gap 69 -> 36 ms, raster 14 -> 5 ms a frame.
- Live Starfield: 19 -> 30 fps (its target), raster 49 -> 13 ms, CPU 97% -> 46%.

Rasteriser (MoonBit host; circles and previews also in the Rust host):
- Blending works on two channels at a time in 16-bit lanes with an exact /255: same pixels,
  about 3x quicker on big translucent fills (the overview's dim, the starfield's nebulae).
- Filled circles are drawn as row spans, rings skip their empty middle (a sqrt per pixel of the
  bounding box before). Same pixels; a Rust test checks it against the old loop.
- Window previews gliding in the overview are sampled straight from the preview at each frame's
  size instead of filtered and cached at a size used once; the MoonBit host also gets the
  one-large-size cache and 4096 px draw limit the Rust host had (it kept every size and didn't
  draw previews past 1024 px).
- Image scaling sums each output row's columns once, with 32-bit sums and division.

Frame pacing: frames are drawn only when the output can take them (no page flip in flight on
KMS; the previous frame shown nested and on X11), with a frame every 250 ms for an output that
never gets ready. The KMS loop runs at 125 Hz, so animations used to rasterise frames that were
never shown.

Software or hardware mode (compositor/shell/render_mode.mbt, docs/rendering.md): detected from
the DRM render nodes' drivers (and NVIDIA's proprietary driver, LIBGL_ALWAYS_SOFTWARE),
overridden by CHANDRA_RENDER=software|hardware or Settings > Session "Lighter animations"
(state.edn :render, saved only when it differs from what was detected). Software mode remakes
changing previews every 200 ms instead of 100 ms and caps desktop modules at ~30 fps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DPL5hkTQp5uN5FtwpzrXUA
This commit is contained in:
Claude committed 2026-11-26 01:55:50 +00:00
1 parent 1aabccba5d
commit 71f0b858d6
16 files changed
+719 -97

No files matched your search

+4
View File
@@ -175,6 +175,9 @@ fn changed_images(panel : @wt.Val) -> R {
d
}
///|
extern "C" fn rs_frame_begin() = "rs_frame_begin"
///|
extern "C" fn rs_damage(x : Int, y : Int, w : Int, h : Int) = "rs_damage"
@@ -658,6 +661,7 @@ fn submit(v : @wt.Val) -> Unit {
}
let w = f.width
let h = f.height
rs_frame_begin()
let resized = rs_resize(w, h) != 0
// The frame starts from the cached backdrop (which covers every pixel), so the buffer is only
// cleared when there is none.
+163 -44
View File
@@ -63,36 +63,48 @@ void rs_set_clip(int32_t on, int32_t x, int32_t y, int32_t w, int32_t h) {
clip_x = x; clip_y = y; clip_w = w; clip_h = h;
}
/* Colour (r, g, b) at alpha a over the RGBA8888 pixel at p: two channels at a time in 16-bit
* lanes, with an exact floor(v / 255). The same pixels as, per channel,
* c' = (c_src * a + c * (255 - a) + 127) / 255 and alpha' = a + (alpha * (255 - a) + 127) / 255
* (the alpha lane adds a * 255 before the division, which is the same thing). */
static inline uint32_t blend32(uint32_t px, uint32_t inv, uint32_t rbadd, uint32_t agadd) {
uint32_t rb = (px & 0x00FF00FFu) * inv + rbadd;
uint32_t ag = ((px >> 8) & 0x00FF00FFu) * inv + agadd;
rb = ((rb + ((rb >> 8) & 0x00FF00FFu) + 0x00010001u) >> 8) & 0x00FF00FFu;
ag = (ag + ((ag >> 8) & 0x00FF00FFu) + 0x00010001u) & 0xFF00FF00u;
return rb | ag;
}
/* The lane constants for colour (r, g, b) at alpha a. */
#define BLEND_ADDS(r, g, b, a) \
uint32_t rbadd = ((uint32_t)((b) * (a) + 127) << 16) | (uint32_t)((r) * (a) + 127); \
uint32_t agadd = ((uint32_t)(255 * (a) + 127) << 16) | (uint32_t)((g) * (a) + 127)
static inline uint32_t opaque32(int r, int g, int b) {
return 0xFF000000u | ((uint32_t)b << 16) | ((uint32_t)g << 8) | (uint32_t)r;
}
static inline void blend_pixel(int x, int y, int r, int g, int b, int a) {
if (x < 0 || y < 0 || x >= fb_w || y >= fb_h || a <= 0) return;
if (clip_on && (x < clip_x || y < clip_y || x >= clip_x + clip_w || y >= clip_y + clip_h)) return;
uint8_t *d = fb + ((size_t)y * fb_w + x) * 4;
if (a >= 255) {
d[0] = (uint8_t)r; d[1] = (uint8_t)g; d[2] = (uint8_t)b; d[3] = 0xff;
return;
}
int inv = 255 - a;
d[0] = (uint8_t)((r * a + d[0] * inv + 127) / 255);
d[1] = (uint8_t)((g * a + d[1] * inv + 127) / 255);
d[2] = (uint8_t)((b * a + d[2] * inv + 127) / 255);
d[3] = (uint8_t)clamp_u8(a + (d[3] * inv + 127) / 255);
uint32_t *d = (uint32_t *)fb + (size_t)y * fb_w + x;
if (a >= 255) { *d = opaque32(r, g, b); return; }
BLEND_ADDS(r, g, b, a);
*d = blend32(*d, 255u - (uint32_t)a, rbadd, agadd);
}
/* Blend a colour over pixels [xa, xb) of row y; the span is already clipped. */
static void blend_span(int y, int xa, int xb, int r, int g, int b, int a) {
if (xa >= xb || a <= 0) return;
uint8_t *p = fb + ((size_t)y * fb_w + xa) * 4, *end = fb + ((size_t)y * fb_w + xb) * 4;
uint32_t *p = (uint32_t *)fb + (size_t)y * fb_w + xa, *end = (uint32_t *)fb + (size_t)y * fb_w + xb;
if (a >= 255) {
for (; p < end; p += 4) { p[0] = (uint8_t)r; p[1] = (uint8_t)g; p[2] = (uint8_t)b; p[3] = 0xff; }
uint32_t c = opaque32(r, g, b);
for (; p < end; p++) *p = c;
return;
}
int inv = 255 - a, ra = r * a + 127, ga = g * a + 127, ba = b * a + 127;
for (; p < end; p += 4) {
p[0] = (uint8_t)((ra + p[0] * inv) / 255);
p[1] = (uint8_t)((ga + p[1] * inv) / 255);
p[2] = (uint8_t)((ba + p[2] * inv) / 255);
p[3] = (uint8_t)clamp_u8(a + (p[3] * inv + 127) / 255);
}
uint32_t inv = 255u - (uint32_t)a;
BLEND_ADDS(r, g, b, a);
for (; p < end; p++) *p = blend32(*p, inv, rbadd, agadd);
}
static int in_rounded_rect(int px, int py, int rx, int ry, int rw, int rh, int rad) {
@@ -193,21 +205,51 @@ void rs_line(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t cr, int32_t
}
}
/* Coverage of pixel (dx, dy) of a circle of radius `rad` (filled, or a 1 px ring). */
static inline double circle_cov(int dx, int dy, double rad, int filled) {
double dist = sqrt((double)(dx * dx + dy * dy));
double outer = rad + 0.5 - dist;
double cov = outer > 1.0 ? 1.0 : outer;
if (!filled) {
double inner = dist - (rad - 1.0 - 0.5);
double ic = inner > 1.0 ? 1.0 : inner;
if (ic < cov) cov = ic;
}
return cov;
}
/* Row by row: the antialiased edge pixel by pixel, a filled circle's solid middle as one span and
* a ring's empty middle skipped (big translucent discs, like the starfield's nebulae, cost a
* sqrt per pixel otherwise). The same pixels as testing every pixel of the bounding box. */
void rs_circle(int32_t cx, int32_t cy, int32_t radius, int32_t cr, int32_t cg, int32_t cb, int32_t ca,
int32_t filled) {
if (!fb || radius <= 0) return;
double rad = radius;
/* Fully covered (filled) when dist <= rad - 0.5; certainly empty (ring) when dist <= rad - 2. */
double solid = filled ? (rad - 0.5) * (rad - 0.5) : (rad >= 2 ? (rad - 2.0) * (rad - 2.0) : -1.0);
for (int dy = -radius - 1; dy <= radius + 1; dy++) {
int y = cy + dy;
if (y < 0 || y >= fb_h || (clip_on && (y < clip_y || y >= clip_y + clip_h))) continue;
/* The middle run |dx| <= m is solid (or empty); m = -1 when there is none. */
int m = -1;
double left = solid - (double)dy * dy;
if (left >= 0) {
m = (int)sqrt(left);
while ((double)(m + 1) * (m + 1) <= left) m++;
while (m >= 0 && (double)m * m > left) m--;
}
for (int dx = -radius - 1; dx <= radius + 1; dx++) {
double dist = sqrt((double)(dx * dx + dy * dy));
double outer = rad + 0.5 - dist;
double cov = outer > 1.0 ? 1.0 : outer;
if (!filled) {
double inner = dist - (rad - 1.0 - 0.5);
double ic = inner > 1.0 ? 1.0 : inner;
if (ic < cov) cov = ic;
if (m >= 0 && dx == -m) {
if (filled) {
int xa = imax(cx - m, 0), xb = imin(cx + m + 1, fb_w);
if (clip_on) { xa = imax(xa, clip_x); xb = imin(xb, clip_x + clip_w); }
blend_span(y, xa, xb, cr, cg, cb, ca);
}
dx = m;
continue;
}
if (cov > 0.02) blend_pixel(cx + dx, cy + dy, cr, cg, cb, (int)round(ca * cov));
double cov = circle_cov(dx, dy, rad, filled);
if (cov > 0.02) blend_pixel(cx + dx, y, cr, cg, cb, (int)round(ca * cov));
}
}
}
@@ -312,33 +354,55 @@ typedef struct {
NSVGimage *svg;
Sized *sized; /* the image at the sizes it was drawn at */
int changed; /* new pixels under an old handle since the last frame (a preview) */
uint32_t resized_at; /* the frame it was last drawn at a new large size */
int preview; /* a window preview (rs_thumbnail): drawn sampled at any other size */
} Image;
static Image *images;
static int32_t n_images, cap_images;
/* Scale RGBA src (sw x sh) to w x h: average of the covered source pixels (alpha weighted), the
* nearest one when enlarging. Same as the software renderer. */
* nearest one when enlarging. Same as the software renderer. Each output row sums its source
* rows per column first (32-bit sums; a column of up to 4096 rows of 255 * 255 fits), then each
* output pixel sums its columns: one pass over the source, row by row. */
static uint8_t *scale_rgba(const uint8_t *src, int sw, int sh, int w, int h) {
uint8_t *out = calloc((size_t)w * h, 4);
uint32_t *col = malloc(sizeof(uint32_t) * 5 * (size_t)sw); /* r*a, g*a, b*a, a, n per column */
for (int y = 0; y < h; y++) {
int y0 = y * sh / h, y1 = imax((y + 1) * sh / h, y0 + 1);
int x_hi = 0; /* columns summed so far for this row (they are needed left to right) */
for (int x = 0; x < w; x++) {
int x0 = x * sw / w, x1 = imax((x + 1) * sw / w, x0 + 1);
int64_t r = 0, g = 0, b = 0, a = 0, n = 0;
for (int sy = y0; sy < y1; sy++) {
for (int sx = x0; sx < x1; sx++) {
const uint8_t *p = src + ((size_t)sy * sw + sx) * 4;
int64_t pa = p[3];
r += p[0] * pa; g += p[1] * pa; b += p[2] * pa; a += pa; n++;
for (; x_hi < x1; x_hi++) {
uint32_t r = 0, g = 0, b = 0, a = 0;
const uint8_t *p = src + ((size_t)y0 * sw + x_hi) * 4;
for (int sy = y0; sy < y1; sy++, p += (size_t)sw * 4) {
uint32_t pa = p[3];
r += p[0] * pa; g += p[1] * pa; b += p[2] * pa; a += pa;
}
uint32_t *c = col + (size_t)x_hi * 5;
c[0] = r; c[1] = g; c[2] = b; c[3] = a; c[4] = (uint32_t)(y1 - y0);
}
if (a > 0) {
uint8_t *o = out + ((size_t)y * w + x) * 4;
o[0] = (uint8_t)(r / a); o[1] = (uint8_t)(g / a); o[2] = (uint8_t)(b / a); o[3] = (uint8_t)(a / n);
uint8_t *o = out + ((size_t)y * w + x) * 4;
if ((int64_t)(x1 - x0) * (y1 - y0) <= 66000) {
/* The sums fit 32 bits (255 * 255 per pixel), and 32-bit division is much quicker. */
uint32_t r = 0, g = 0, b = 0, a = 0, n = 0;
for (int sx = x0; sx < x1; sx++) {
const uint32_t *c = col + (size_t)sx * 5;
r += c[0]; g += c[1]; b += c[2]; a += c[3]; n += c[4];
}
if (a > 0) { o[0] = (uint8_t)(r / a); o[1] = (uint8_t)(g / a); o[2] = (uint8_t)(b / a); o[3] = (uint8_t)(a / n); }
continue;
}
int64_t r = 0, g = 0, b = 0, a = 0, n = 0;
for (int sx = x0; sx < x1; sx++) {
const uint32_t *c = col + (size_t)sx * 5;
r += c[0]; g += c[1]; b += c[2]; a += c[3]; n += c[4];
}
if (a > 0) { o[0] = (uint8_t)(r / a); o[1] = (uint8_t)(g / a); o[2] = (uint8_t)(b / a); o[3] = (uint8_t)(a / n); }
}
}
free(col);
return out;
}
@@ -487,30 +551,84 @@ static uint8_t *render_svg(NSVGimage *svg, int w, int h) {
return out;
}
/* The largest size an image is drawn at (a window preview gliding to its window's size). */
#define MAX_DRAW 4096
/* Sizes past which only one scaled copy of an image is kept (icons are drawn at a few small
* sizes; previews at one large one, or a new one each frame while they move). */
static int big_size(int w, int h) { return w * h > 128 * 128; }
/* Frames submitted so far (rs_frame_begin): tells a moving image from one drawn at a new size once. */
static uint32_t frame_seq = 1;
void rs_frame_begin(void) { frame_seq++; }
/* Image `handle` at w x h, cached. NULL for an unknown handle or a silly size, and for a window
* preview at another size or a large image that was drawn at another new size in the frame
* before: it is moving (the overview's gliding previews), so rs_draw_image samples it instead of
* filtering it and keeping a copy at a size used once. */
static const uint8_t *image_sized(int32_t handle, int w, int h) {
if (w <= 0 || h <= 0 || w > MAX_IMAGE || h > MAX_IMAGE) return NULL;
if (w <= 0 || h <= 0 || w > MAX_DRAW || h > MAX_DRAW) return NULL;
if (handle <= 0 || handle > n_images || !images[handle - 1].used) return NULL;
Image *im = &images[handle - 1];
if (im->rgba && im->w == w && im->h == h) return im->rgba;
for (Sized *s = im->sized; s; s = s->next)
if (s->w == w && s->h == h) return s->rgba;
int big = big_size(w, h);
if (im->preview) return NULL; /* made at the size it settles at; stretched only while moving */
if (big && im->rgba) {
int moving = im->resized_at + 1 >= frame_seq;
im->resized_at = frame_seq;
if (moving) return NULL;
}
uint8_t *px = im->svg ? render_svg(im->svg, w, h) : im->rgba ? scale_rgba(im->rgba, im->w, im->h, w, h) : NULL;
if (!px) return NULL;
if (big) {
/* Keep one large size: drop the other large copies. */
for (Sized **pp = &im->sized; *pp;) {
Sized *s = *pp;
if (big_size(s->w, s->h)) { *pp = s->next; free(s->rgba); free(s); }
else pp = &s->next;
}
}
Sized *s = malloc(sizeof *s);
s->w = w; s->h = h; s->rgba = px; s->next = im->sized;
im->sized = s;
return px;
}
/* Blend straight-RGBA pixels `px` (w x h, row `stride` pixels) at (x0, y0), clipped. When `sx` /
* `sy` are given, destination column x / row y takes source column sx[x] / row sy[y] (sampling). */
static void blend_image(const uint8_t *px, int stride, int x0, int y0, int w, int h, const int *sx, const int *sy) {
int xa, ya, xb, yb;
if (!visible(x0, y0, w, h, &xa, &ya, &xb, &yb)) return;
for (int y = ya; y < yb; y++) {
int row = sy ? sy[y - y0] : y - y0;
const uint8_t *src = px + (size_t)row * stride * 4;
uint8_t *d = fb + ((size_t)y * fb_w + xa) * 4;
for (int x = xa; x < xb; x++, d += 4) {
const uint8_t *p = src + (size_t)(sx ? sx[x - x0] : x - x0) * 4;
int a = p[3];
if (a <= 0) continue;
if (a >= 255) { *(uint32_t *)d = opaque32(p[0], p[1], p[2]); continue; }
BLEND_ADDS(p[0], p[1], p[2], a);
*(uint32_t *)d = blend32(*(uint32_t *)d, 255u - (uint32_t)a, rbadd, agadd);
}
}
}
void rs_draw_image(int32_t handle, int32_t x0, int32_t y0, int32_t w, int32_t h) {
if (!fb) return;
const uint8_t *px = image_sized(handle, w, h);
if (!px) return;
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++) {
const uint8_t *p = px + ((size_t)y * w + x) * 4;
blend_pixel(x0 + x, y0 + y, p[0], p[1], p[2], p[3]);
}
if (px) { blend_image(px, w, x0, y0, w, h, NULL, NULL); return; }
/* A moving large image: each pixel from the nearest source pixel, nothing kept. */
if (w <= 0 || h <= 0 || w > MAX_DRAW || h > MAX_DRAW || handle <= 0 || handle > n_images) return;
Image *im = &images[handle - 1];
if (!im->used || !im->rgba) return;
int *sx = malloc(sizeof(int) * (size_t)(w + h)), *sy = sx + w;
for (int x = 0; x < w; x++) sx[x] = (int)(((int64_t)x * 2 + 1) * im->w / (2 * w));
for (int y = 0; y < h; y++) sy[y] = (int)(((int64_t)y * 2 + 1) * im->h / (2 * h));
blend_image(im->rgba, im->w, x0, y0, w, h, sx, sy);
free(sx);
}
/* ------------------------------------------------------------------ surfaces */
@@ -691,6 +809,7 @@ int32_t rs_thumbnail(uint32_t id, int32_t w, int32_t h, int32_t reuse) {
int32_t img = image_slot(reuse);
images[img - 1].w = w; images[img - 1].h = h; images[img - 1].rgba = px;
images[img - 1].changed = img == reuse;
images[img - 1].preview = 1;
return img;
}