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>
This commit is contained in:
nandiandClaude Opus 5.5 committed 2026-11-30 16:42:22 -08:00
1 parent d81003f176
commit cfb25fe699
26 files changed
+2485 -105

No files matched your search

+49 -2
View File
@@ -130,6 +130,7 @@ struct fbuf {
uint32_t handle, fb, pitch;
size_t size;
uint8_t *map;
int dmabuf; /* 1 + its fd as a dmabuf (for a GPU to draw into), 0 not exported yet, -1 can't be */
};
static int g_drm = -1;
@@ -792,6 +793,9 @@ static void copy_rows(struct fbuf *b, const uint8_t *src, int w, int x0, int y0,
MOONBIT_FFI_EXPORT
int32_t mp_kms_present(moonbit_bytes_t pixels, int32_t w, int32_t h);
/* Show the back buffer (below). */
int32_t mp_kms_flip(void);
/* 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
@@ -810,10 +814,53 @@ int32_t mp_kms_present_damage(moonbit_bytes_t pixels, int32_t w, int32_t h, int3
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);
return mp_kms_flip();
}
/* ---- frames drawn by the GPU straight into the scanout buffers (the wasm host's rasteriser) ---- */
/* The frame changed there: every buffer is now missing it. */
MOONBIT_FFI_EXPORT
void mp_kms_add_damage(int32_t x, int32_t y, int32_t w, int32_t h) {
if (w > 0 && h > 0) stale_add(x, y, x + w, y + h);
}
/* The buffer to draw the next w x h frame into. Returns its index (0 or 1), -1 when no frame can be
* shown now, -2 when the display's buffers can't be shared with a GPU. info: dmabuf fd, pitch,
* width and height in pixels, scale, rotation; missing: x0, y0, x1, y1 of the frame it lacks. */
MOONBIT_FFI_EXPORT
int32_t mp_kms_back_buffer(int32_t w, int32_t h, int32_t *info, int32_t *missing) {
if (w != g_width || h != g_height) return -1;
if (!g_seat_enabled || g_flip_pending || g_blank) return -1;
int back = 1 - g_front;
struct fbuf *b = &g_fb[back];
if (b->dmabuf == 0) {
int fd = -1;
if (drmPrimeHandleToFD(g_drm, b->handle, DRM_CLOEXEC | DRM_RDWR, &fd) < 0) {
fprintf(stderr, "kms: cannot share the framebuffers with the GPU: %s\n", strerror(errno));
b->dmabuf = -1;
} else {
b->dmabuf = fd + 1;
}
}
if (b->dmabuf < 0) return -2;
info[0] = b->dmabuf - 1; info[1] = (int32_t)b->pitch; info[2] = g_phys_w; info[3] = g_phys_h;
info[4] = g_scale > 1 ? g_scale : 1; info[5] = g_rot;
const int *s = g_stale[back];
missing[0] = s[0] < 0 ? 0 : s[0]; missing[1] = s[1] < 0 ? 0 : s[1];
missing[2] = s[2] > w ? w : s[2]; missing[3] = s[3] > h ? h : s[3];
return back;
}
/* Show the buffer mp_kms_back_buffer gave. */
MOONBIT_FFI_EXPORT
int32_t mp_kms_flip(void) {
int back = 1 - g_front;
int *s = g_stale[back];
s[0] = s[1] = s[2] = s[3] = 0;
if (drmModePageFlip(g_drm, g_crtc, b->fb, DRM_MODE_PAGE_FLIP_EVENT, NULL) < 0) {
if (drmModePageFlip(g_drm, g_crtc, g_fb[back].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;
if (drmModeSetCrtc(g_drm, g_crtc, g_fb[back].fb, 0, 0, &g_conn, 1, &g_mode) < 0) return -1;
} else {
g_flip_pending = 1;
}