Files
chandra/wasmhost/test/gpu_test.c
T
nandiandClaude Opus 5.5 cfb25fe699 Draw on the GPU: the desktop with OpenGL ES, apps through linux-dmabuf
The host's rasteriser has a GPU half (raster_gpu.c): the same primitives, drawn the same way, on
machines with a real GPU and OpenGL ES 3, straight into the display's buffers in a login session.
EGL and GLES are loaded at run time; without them everything is drawn on the CPU as before
(CHANDRA_GPU=0 does that too). zwp_linux_dmabuf_v1 lets apps draw on the GPU as well.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 16:42:22 -08:00

301 lines
12 KiB
C

/* The GPU rasteriser against the CPU one: both draw the same scenes in this process and the frames
* are compared; then a frame goes through a display buffer (a DRM dumb buffer shared as a dmabuf,
* which needs no display) at every rotation, and a client's dmabuf is captured.
* scripts/test-gpu.sh exits 77 when there is no GPU */
#include "../bin/raster_stub.c"
#include <sys/ioctl.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
/* ---- what raster_stub.c takes from the rest of the host ---- */
/* MoonBit's runtime, as far as byte strings go. */
moonbit_bytes_t moonbit_make_bytes(int32_t n, int c) {
struct moonbit_object *o = malloc(sizeof *o + (size_t)n + 1);
memset(o, 0, sizeof *o);
o->meta = (uint32_t)n;
memset(o + 1, c, (size_t)n + 1);
return (moonbit_bytes_t)(o + 1);
}
void moonbit_decref(void *p) { free((struct moonbit_object *)p - 1); }
int32_t mp_font_load(int32_t slot, moonbit_bytes_t path) { (void)slot; (void)path; return 0; }
int32_t mp_font_has_glyph(int32_t slot, int32_t cp) { (void)slot; (void)cp; return 1; }
/* Glyphs: a gradient blob as big as the size asked for. */
void mp_font_metrics(int32_t slot, int32_t cp, int32_t size, int32_t *info) {
(void)slot;
info[0] = size / 2 + cp % 7; info[1] = size; info[2] = 1; info[3] = -size; info[4] = size / 2 + 2;
}
void mp_font_bitmap(int32_t slot, int32_t cp, int32_t size, int32_t w, int32_t h, moonbit_bytes_t buf) {
(void)slot; (void)size;
for (int i = 0; i < w * h; i++) buf[i] = (uint8_t)((i * 37 + cp * 11) % 256);
}
int32_t mp_kms_back_buffer(int32_t w, int32_t h, int32_t *info, int32_t *missing) { (void)w; (void)h; (void)info; (void)missing; return -1; }
int32_t mp_kms_flip(void) { return 0; }
void mp_kms_add_damage(int32_t x, int32_t y, int32_t w, int32_t h) { (void)x; (void)y; (void)w; (void)h; }
int32_t mp_kms_present_damage(moonbit_bytes_t p, int32_t w, int32_t h, int32_t x, int32_t y, int32_t dw, int32_t dh) {
(void)p; (void)w; (void)h; (void)x; (void)y; (void)dw; (void)dh; return 0;
}
/* ---- scenes ---- */
#define W 640
#define H 480
static int failures;
static uint32_t rnd_state;
static int rnd(int lo, int hi) { /* [lo, hi] */
rnd_state = rnd_state * 1664525u + 1013904223u;
return lo + (int)((rnd_state >> 8) % (uint32_t)(hi - lo + 1));
}
static moonbit_bytes_t pixels(int w, int h, int alpha) {
moonbit_bytes_t b = moonbit_make_bytes(w * h * 4, 0);
for (int i = 0; i < w * h; i++) {
int a = alpha ? rnd(0, 255) : 255;
b[i * 4] = (uint8_t)rnd(0, 255); b[i * 4 + 1] = (uint8_t)rnd(0, 255); b[i * 4 + 2] = (uint8_t)rnd(0, 255);
b[i * 4 + 3] = (uint8_t)a;
}
return b;
}
static uint32_t capture(int w, int h, int format) {
int stride = w * 4 + 16;
int fd = memfd_create("client", MFD_CLOEXEC);
uint8_t *p = malloc((size_t)stride * h);
for (int i = 0; i < stride * h; i += 4) { /* premultiplied */
int a = rnd(0, 255);
for (int c = 0; c < 3; c++) p[i + c] = (uint8_t)rnd(0, format == 1 ? 255 : a);
p[i + 3] = (uint8_t)a;
}
if (write(fd, p, (size_t)stride * h) < 0) perror("write");
free(p);
uint32_t id = rs_capture(fd, 0, stride, w, h, format);
close(fd);
return id;
}
/* A kind of drawing, repeated with random places and colours (also off the edges). */
static void draw(int kind) {
int x = rnd(-40, W), y = rnd(-40, H), w = rnd(1, 200), h = rnd(1, 150);
int r = rnd(0, 255), g = rnd(0, 255), b = rnd(0, 255), a = rnd(0, 2) ? rnd(1, 254) : 255;
switch (kind) {
case 0: rs_fill_rect(x, y, w, h, r, g, b, a, 0); break;
case 1: rs_fill_rect(x, y, w, h, r, g, b, a, rnd(1, 60)); break;
case 2: rs_stroke_rect(x, y, w, h, r, g, b, a, rnd(1, 6), rnd(0, 40)); break;
case 3: rs_circle(x, y, rnd(1, 80), r, g, b, a, 1); break;
case 4: rs_circle(x, y, rnd(1, 80), r, g, b, a, 0); break;
case 5: rs_line(x, y, x + rnd(-90, 90), y + rnd(-90, 90), r, g, b, a, rnd(1, 5)); break;
case 6: rs_draw_glyph(0, rnd(32, 200), rnd(8, 40), x, y, r, g, b, a, rnd(0, 1)); break;
}
}
static const char *const kinds[] = { "fill", "rounded fill", "stroke", "disc", "ring", "line", "glyph", "icon", "surface" };
static int32_t icon;
static uint32_t surf_a, surf_x;
static void scene(int kind) {
rnd_state = 1234u + (uint32_t)kind;
rs_frame_begin();
rs_resize(W, H);
rs_set_clip(0, 0, 0, 0, 0);
rs_clear();
for (int y = 0; y < H; y += 16) rs_fill_rect(0, y, W, 16, y / 2, 128, 255 - y / 2, 255, 0);
for (int i = 0; i < 60; i++) {
if (i % 4 == 3) rs_set_clip(1, rnd(0, W), rnd(0, H), rnd(1, W), rnd(1, H));
else rs_set_clip(0, 0, 0, 0, 0);
if (kind == 7) { int s = rnd(0, 1) ? 32 : 20; rs_draw_image(icon, rnd(-20, W), rnd(-20, H), s, s); }
else if (kind == 8) {
/* Smaller than its window only when opaque: the corner pixel stretched over the rest is
* blended as straight alpha by the CPU. */
int x = rnd(-60, W), y = rnd(-60, H);
if (rnd(0, 1)) rs_blit_surface(surf_a, x, y, rnd(1, 120), rnd(1, 80));
else rs_blit_surface(surf_x, x, y, rnd(1, 140), rnd(1, 90));
}
else draw(kind);
}
if (kind == 0) { /* the backdrop: kept, drawn over, copied back into a clip */
rs_set_clip(0, 0, 0, 0, 0);
rs_save_backdrop();
rs_fill_rect(0, 0, W, H, 1, 2, 3, 255, 0);
rs_set_clip(1, 50, 60, 300, 200);
rs_copy_backdrop();
}
rs_damage(0, 0, W, H);
rs_publish_frame();
}
static uint8_t *render(int kind, int on) {
gpu_on = on;
free(fb); fb = NULL; fb_w = fb_h = 0;
rnd_state = 99;
icon = rs_image_put(0, 32, 32, pixels(32, 32, 1));
surf_a = capture(120, 80, 0);
surf_x = capture(100, 60, 1);
scene(kind);
uint8_t *out = moonbit_make_bytes(W * H * 4, 0);
rs_read_frame(out);
rs_release(surf_a);
rs_release(surf_x);
return out;
}
static void compare(int kind) {
uint8_t *cpu = render(kind, 0), *gpu = render(kind, 1);
int worst = 0, bad = 0;
for (int i = 0; i < W * H * 4; i++) {
int d = abs(cpu[i] - gpu[i]);
if (d > worst) worst = d;
if (d > 4) bad++; /* rounding; a circle's edge is a float */
}
/* And that something was drawn over the background at all. */
int drawn = 0;
for (int i = 0; i < W * H; i++) drawn += cpu[i * 4 + 1] != 128;
printf("%-12s worst difference %3d, pixels off %d (of %d drawn)\n", kinds[kind], worst, bad, drawn);
if (bad > 0 || drawn < 500) failures++;
}
/* ---- the display's buffers ---- */
typedef struct { int fd; uint32_t handle, pitch; uint8_t *map; } Dumb;
static int dumb_new(int drm, int w, int h, Dumb *d) {
struct drm_mode_create_dumb c = { .width = (uint32_t)w, .height = (uint32_t)h, .bpp = 32 };
if (drmIoctl(drm, DRM_IOCTL_MODE_CREATE_DUMB, &c) < 0) return -1;
struct drm_mode_map_dumb m = { .handle = c.handle };
if (drmPrimeHandleToFD(drm, c.handle, DRM_CLOEXEC | DRM_RDWR, &d->fd) < 0) return -1;
if (drmIoctl(drm, DRM_IOCTL_MODE_MAP_DUMB, &m) < 0) return -1;
d->handle = c.handle; d->pitch = c.pitch;
d->map = mmap(NULL, c.size, PROT_READ | PROT_WRITE, MAP_SHARED, drm, (off_t)m.offset);
return d->map == MAP_FAILED ? -1 : 0;
}
/* What kms_stub.c's copy_rows shows at physical (px, py). */
static const uint8_t *expected(const uint8_t *f, int px, int py, int sc, int rot) {
int lw = W, lh = H, bw = rot & 1 ? lh : lw, bh = rot & 1 ? lw : lh, ux = px / sc, uy = py / sc, x = ux, y = uy;
if (rot == 1) { x = uy; y = bw - 1 - ux; }
else if (rot == 2) { x = bw - 1 - ux; y = bh - 1 - uy; }
else if (rot == 3) { x = bh - 1 - uy; y = ux; }
return f + ((size_t)y * lw + x) * 4;
}
static void scanout(int drm) {
uint8_t *frame = render(1, 1);
for (int rot = 0; rot < 4; rot++)
for (int sc = 1; sc <= 2; sc++) {
int pw = (rot & 1 ? H : W) * sc, ph = (rot & 1 ? W : H) * sc;
Dumb d;
if (dumb_new(drm, pw, ph, &d) < 0) { printf("scanout skipped (no dumb buffers: %s)\n", strerror(errno)); return; }
memset(d.map, 0x55, (size_t)d.pitch * ph);
scan[0].state = 0; /* a new buffer each time */
int ok = gpu_scanout(0, d.fd, d.pitch, pw, ph, sc, rot, 0, 0, W, H);
p_glFinish();
int bad = 0;
for (int y = 0; ok && y < ph; y++)
for (int x = 0; x < pw; x++) {
const uint8_t *e = expected(frame, x, y, sc, rot), *g = d.map + (size_t)y * d.pitch + x * 4;
if (g[0] != e[2] || g[1] != e[1] || g[2] != e[0]) bad++;
}
printf("scanout rotation %3d, scale %d: %s, %d pixels off\n", rot * 90, sc, ok ? "drawn" : "REFUSED", bad);
if (!ok || bad) failures++;
close(d.fd);
}
/* Only the damaged part is touched. */
Dumb d;
if (dumb_new(drm, W, H, &d) < 0) return;
memset(d.map, 0x55, (size_t)d.pitch * H);
scan[0].state = 0;
gpu_scanout(0, d.fd, d.pitch, W, H, 1, 0, 100, 50, 200, 90);
p_glFinish();
int bad = 0;
for (int y = 0; y < H; y++)
for (int x = 0; x < W; x++) {
int in = x >= 100 && x < 200 && y >= 50 && y < 90;
const uint8_t *e = expected(frame, x, y, 1, 0), *g = d.map + (size_t)y * d.pitch + x * 4;
if (in ? g[0] != e[2] || g[1] != e[1] || g[2] != e[0] : g[0] != 0x55 || g[1] != 0x55) bad++;
}
printf("scanout a damaged part only: %d pixels off\n", bad);
if (bad) failures++;
}
static void client_dmabuf(int drm) {
Dumb d;
if (dumb_new(drm, 64, 32, &d) < 0) return;
for (int y = 0; y < 32; y++)
for (int x = 0; x < 64; x++) {
uint8_t *p = d.map + (size_t)y * d.pitch + x * 4;
p[0] = (uint8_t)(x * 4); p[1] = (uint8_t)(y * 8); p[2] = 77; p[3] = 0;
}
int32_t plane[3] = { d.fd, 0, (int32_t)d.pitch };
for (int flip = 0; flip < 2; flip++) {
gpu_on = 1;
uint32_t id = rs_dmabuf_import(1, plane, 0, 64, 32, DRM_XRGB, flip);
uint32_t s = id ? rs_dmabuf_capture(id) : 0;
rs_dmabuf_release(id);
rs_resize(W, H);
rs_set_clip(0, 0, 0, 0, 0);
rs_clear();
if (s) rs_blit_surface(s, 10, 20, 64, 32);
rs_damage(0, 0, W, H);
uint8_t *out = moonbit_make_bytes(W * H * 4, 0);
rs_read_frame(out);
int bad = 0;
for (int y = 0; y < 32; y++)
for (int x = 0; x < 64; x++) {
const uint8_t *p = out + ((size_t)(y + 20) * W + x + 10) * 4;
int sy = flip ? 31 - y : y;
if (p[0] != 77 || p[1] != sy * 8 || p[2] != x * 4 || p[3] != 255) bad++;
}
printf("dmabuf %s: %d pixels off\n", flip ? "upside down" : "upright", bad);
if (!s || bad) failures++;
if (s) rs_release(s);
}
int32_t junk[3] = { open("/dev/null", O_RDWR | O_CLOEXEC), 0, 256 };
if (rs_dmabuf_import(1, junk, 0, 64, 32, DRM_XRGB, 0) || rs_dmabuf_capture(12345)) {
printf("dmabuf a buffer that is none was accepted\n");
failures++;
}
}
int main(void) {
setenv("CHANDRA_TEST_CONFIG", "1", 1);
rs_font_loaded(0);
/* The crash guard: a session that died starting on the GPU keeps the next one off it, once. */
char dir[] = "/tmp/ct-gpu-XXXXXX", sub[64], mark[96];
if (!mkdtemp(dir)) return 1;
snprintf(sub, sizeof sub, "%s/chandra", dir);
snprintf(mark, sizeof mark, "%s/gpu-starting", sub);
mkdir(sub, 0700);
setenv("XDG_STATE_HOME", dir, 1);
const char *forced = getenv("CHANDRA_GPU");
forced = forced ? strdup(forced) : NULL;
unsetenv("CHANDRA_GPU");
close(open(mark, O_CREAT | O_WRONLY, 0600));
int kept_off = !rs_gpu_init(1) && access(mark, F_OK) != 0;
if (forced) setenv("CHANDRA_GPU", forced, 1);
if (!rs_gpu_init(1)) { printf("no GPU: skipped\n"); return 77; }
printf("crash guard %s\n", kept_off ? "ok" : "FAILED");
failures += !kept_off;
for (int k = 0; k < 9; k++) compare(k);
int32_t f[3], n = 0;
while (rs_dmabuf_format(n, f)) n++;
printf("dmabuf %d format/modifier pairs\n", n);
if (!n) failures++;
/* The card node of the GPU. */
int drm = -1;
for (int i = 0; i < 8 && drm < 0; i++) {
char path[32];
snprintf(path, sizeof path, "/dev/dri/card%d", i);
drm = open(path, O_RDWR | O_CLOEXEC);
}
if (drm < 0) printf("scanout skipped (no card to open)\n");
else { client_dmabuf(drm); scanout(drm); }
int marked = access(mark, F_OK) == 0; /* drawing on the display has begun */
if (drm >= 0 && !marked) { printf("crash guard no mark while starting\n"); failures++; }
guard_exit();
rmdir(sub);
rmdir(dir);
printf(failures ? "FAILED: %d\n" : "ok\n", failures);
return failures ? 1 : 0;
}