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

+200 -29
View File
@@ -4,6 +4,10 @@
* this file owns the framebuffer, the captured client surfaces, the images and the glyph cache,
* and draws one primitive per call, inside the current clip.
*
* With a GPU (raster_gpu.c) every primitive is drawn there instead, into a frame that stays on the
* GPU; `fb` is then only a copy, read back (fb_sync) when something needs the pixels here:
* screenshots, recordings, the remote mirror, a nested or X11 window.
*
* Glyphs come from stb_truetype through compositor/font's shim (mp_font_*); SVG icons from
* nanosvg (third_party/nanosvg). */
#define _GNU_SOURCE
@@ -23,6 +27,7 @@
#include <unistd.h>
#include "moonbit.h"
#include "../../remote/chandra_fb_shm.h"
#include "raster_gpu.h"
#define NANOSVG_IMPLEMENTATION
#define NANOSVGRAST_IMPLEMENTATION
@@ -38,7 +43,8 @@ int32_t mp_font_has_glyph(int32_t slot, int32_t cp);
/* ------------------------------------------------------------------ canvas */
static int32_t fb_w, fb_h;
static uint8_t *fb; /* RGBA8888 */
static uint8_t *fb; /* RGBA8888; with a GPU a copy, see fb_sync */
static int canvas_bad; /* the frame is too big for the GPU */
static int clip_on;
static int32_t clip_x, clip_y, clip_w, clip_h;
@@ -47,11 +53,26 @@ static inline int clamp_u8(int v) { return v < 0 ? 0 : v > 255 ? 255 : v; }
static inline int imin(int a, int b) { return a < b ? a : b; }
static inline int imax(int a, int b) { return a > b ? a : b; }
/* Find the GPU, before anything is drawn or captured. 1 when there is one to draw on. */
int32_t rs_gpu_init(int32_t session) { return gpu_init(session); }
static void gpu_stale_all(void);
/* Resize the framebuffer (zeroed). 1 when the size changed. */
int32_t rs_resize(int32_t w, int32_t h) {
if (w < 1) w = 1;
if (h < 1) h = 1;
if (fb && w == fb_w && h == fb_h) return 0;
if (w == fb_w && h == fb_h && (fb || gpu_on)) return 0;
if (gpu_on) {
free(fb);
fb = NULL;
fb_w = w;
fb_h = h;
canvas_bad = !gpu_resize(w, h);
if (canvas_bad) fprintf(stderr, "gpu: no %d x %d frame\n", w, h);
gpu_stale_all();
return 1;
}
free(fb);
fb = calloc((size_t)w * h, 4);
fb_w = w;
@@ -59,11 +80,15 @@ int32_t rs_resize(int32_t w, int32_t h) {
return 1;
}
void rs_clear(void) { if (fb) memset(fb, 0, (size_t)fb_w * fb_h * 4); }
void rs_clear(void) {
if (gpu_on) { gpu_clear(); return; }
if (fb) memset(fb, 0, (size_t)fb_w * fb_h * 4);
}
void rs_set_clip(int32_t on, int32_t x, int32_t y, int32_t w, int32_t h) {
clip_on = on;
clip_x = x; clip_y = y; clip_w = w; clip_h = h;
if (gpu_on) gpu_clip(on, x, y, w, h);
}
/* Colour (r, g, b) at alpha a over the RGBA8888 pixel at p: two channels at a time in 16-bit
@@ -138,6 +163,7 @@ static int visible(int x, int y, int w, int h, int *x0, int *y0, int *x1, int *y
void rs_fill_rect(int32_t x, int32_t y, int32_t w, int32_t h, int32_t cr, int32_t cg, int32_t cb, int32_t ca,
int32_t radius) {
if (gpu_on) { gpu_fill(x, y, w, h, cr, cg, cb, ca, radius); return; }
if (!fb || w <= 0 || h <= 0 || ca == 0) return;
int x0, y0, x1, y1;
if (!visible(x, y, w, h, &x0, &y0, &x1, &y1)) return;
@@ -166,6 +192,7 @@ void rs_fill_rect(int32_t x, int32_t y, int32_t w, int32_t h, int32_t cr, int32_
void rs_stroke_rect(int32_t x, int32_t y, int32_t w, int32_t h, int32_t cr, int32_t cg, int32_t cb, int32_t ca,
int32_t width, int32_t radius) {
if (gpu_on) { gpu_stroke(x, y, w, h, cr, cg, cb, ca, width, radius); return; }
if (!fb || w <= 0 || h <= 0 || width <= 0 || ca == 0) return;
int x0 = imax(x, 0), y0 = imax(y, 0), x1 = imin(x + w, fb_w), y1 = imin(y + h, fb_h);
int inner_x = x + width, inner_y = y + width, inner_w = w - width * 2, inner_h = h - width * 2;
@@ -193,13 +220,14 @@ void rs_stroke_rect(int32_t x, int32_t y, int32_t w, int32_t h, int32_t cr, int3
void rs_line(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t cr, int32_t cg, int32_t cb, int32_t ca,
int32_t width) {
if (!fb) return;
if (!fb && !gpu_on) return;
int half = width <= 1 ? 0 : width / 2;
int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
int err = dx + dy;
for (;;) {
for (int oy = -half; oy <= half; oy++)
if (gpu_on) gpu_fill(x0 - half, y0 - half, 2 * half + 1, 2 * half + 1, cr, cg, cb, ca, 0);
else for (int oy = -half; oy <= half; oy++)
for (int ox = -half; ox <= half; ox++) blend_pixel(x0 + ox, y0 + oy, cr, cg, cb, ca);
if (x0 == x1 && y0 == y1) break;
int e2 = 2 * err;
@@ -226,6 +254,7 @@ static inline double circle_cov(int dx, int dy, double rad, int filled) {
* 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 (gpu_on) { gpu_circle(cx, cy, radius, cr, cg, cb, ca, filled); return; }
if (!fb || radius <= 0) return;
double rad = radius;
/* Fully covered (filled) when dist <= rad - 0.5; certainly empty (ring) when dist <= rad - 2. */
@@ -263,6 +292,8 @@ typedef struct Glyph {
int32_t slot, cp, size;
int32_t w, h, xoff, yoff, advance;
uint8_t *coverage;
int ax, ay; /* its place in the GPU's glyph atlas, */
uint32_t gen; /* when this is the atlas's generation */
struct Glyph *next;
} Glyph;
@@ -336,8 +367,14 @@ void rs_glyph_coverage(int32_t slot, int32_t cp, int32_t size, moonbit_bytes_t o
void rs_draw_glyph(int32_t slot, int32_t cp, int32_t size, int32_t pen, int32_t baseline, int32_t cr, int32_t cg,
int32_t cb, int32_t ca, int32_t embolden) {
if (!fb) return;
if (!fb && !gpu_on) return;
Glyph *g = glyph(slot, cp, size);
if (gpu_on) {
if (!g->coverage) return;
for (int k = 0; k <= (embolden ? 1 : 0); k++)
gpu_glyph(g->coverage, g->w, g->h, &g->ax, &g->ay, &g->gen, pen + g->xoff + k, baseline + g->yoff, cr, cg, cb, ca);
return;
}
for (int row = 0; row < g->h; row++) {
for (int col = 0; col < g->w; col++) {
int cov = g->coverage[row * g->w + col];
@@ -354,11 +391,12 @@ void rs_draw_glyph(int32_t slot, int32_t cp, int32_t size, int32_t pen, int32_t
#define MAX_IMAGE 16384
typedef struct Sized { int32_t w, h; uint8_t *rgba; struct Sized *next; } Sized;
typedef struct Sized { int32_t w, h; uint8_t *rgba; uint32_t tex; struct Sized *next; } Sized;
typedef struct {
int used;
int32_t w, h;
uint8_t *rgba; /* pixels, or NULL for an SVG */
uint8_t *rgba; /* pixels, or NULL for an SVG (or a preview made on the GPU) */
uint32_t tex; /* the pixels on the GPU, once drawn there */
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) */
@@ -414,10 +452,17 @@ static uint8_t *scale_rgba(const uint8_t *src, int sw, int sh, int w, int h) {
return out;
}
static void sized_free(Sized *s) {
gpu_tex_free(s->tex, s->w, s->h);
free(s->rgba);
free(s);
}
static void image_reset(Image *im) {
free(im->rgba);
gpu_tex_free(im->tex, im->w, im->h);
if (im->svg) nsvgDelete(im->svg);
for (Sized *s = im->sized; s;) { Sized *n = s->next; free(s->rgba); free(s); s = n; }
for (Sized *s = im->sized; s;) { Sized *n = s->next; sized_free(s); s = n; }
memset(im, 0, sizeof *im);
}
@@ -594,12 +639,12 @@ static const uint8_t *image_sized(int32_t handle, int w, int h) {
/* 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); }
if (big_size(s->w, s->h)) { *pp = s->next; sized_free(s); }
else pp = &s->next;
}
}
Sized *s = malloc(sizeof *s);
s->w = w; s->h = h; s->rgba = px; s->next = im->sized;
s->w = w; s->h = h; s->rgba = px; s->tex = 0; s->next = im->sized;
im->sized = s;
return px;
}
@@ -624,7 +669,28 @@ static void blend_image(const uint8_t *px, int stride, int x0, int y0, int w, in
}
}
static void draw_image_gpu(int32_t handle, int x0, int y0, int w, int h) {
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) return;
if (im->preview) { gpu_image(im->tex, x0, y0, w, h, 1); return; } /* filtered at any size */
const uint8_t *px = image_sized(handle, w, h);
if (px && px != im->rgba) {
Sized *s = im->sized;
while (s && s->rgba != px) s = s->next;
if (!s) return;
if (!s->tex) s->tex = gpu_tex_rgba(s->rgba, w, h);
gpu_image(s->tex, x0, y0, w, h, 0);
return;
}
/* At its own size, or a large one that is moving: the GPU scales it. */
if (!im->rgba) return;
if (!im->tex) im->tex = gpu_tex_rgba(im->rgba, im->w, im->h);
gpu_image(im->tex, x0, y0, w, h, 0);
}
void rs_draw_image(int32_t handle, int32_t x0, int32_t y0, int32_t w, int32_t h) {
if (gpu_on) { draw_image_gpu(handle, x0, y0, w, h); return; }
if (!fb) return;
const uint8_t *px = image_sized(handle, w, h);
if (px) { blend_image(px, w, x0, y0, w, h, NULL, NULL); return; }
@@ -644,7 +710,8 @@ void rs_draw_image(int32_t handle, int32_t x0, int32_t y0, int32_t w, int32_t h)
typedef struct Surface {
uint32_t id;
int32_t w, h;
uint8_t *rgba;
uint8_t *rgba; /* NULL with a GPU: */
uint32_t tex; /* it is there */
int opaque; /* every pixel has alpha 255: blits copy rows */
struct Surface *next;
} Surface;
@@ -759,6 +826,15 @@ static const uint8_t *pool_map(int fd, const struct stat *st) {
return p;
}
static uint32_t surface_add(int32_t w, int32_t h, uint8_t *rgba, uint32_t tex, int opaque) {
do { next_surface++; } while (next_surface == 0 || surface(next_surface));
Surface *sf = malloc(sizeof *sf);
sf->id = next_surface; sf->w = w; sf->h = h; sf->rgba = rgba; sf->tex = tex; sf->opaque = opaque;
sf->next = surfaces[sf->id % SURF_BUCKETS];
surfaces[sf->id % SURF_BUCKETS] = sf;
return sf->id;
}
/* Copy a client buffer (stride * height bytes at `offset` of the file behind fd, little-endian
* B, G, R, A/X) into a surface. 0 on failure. */
uint32_t rs_capture(int32_t fd, int32_t offset, int32_t stride, int32_t w, int32_t h, int32_t format) {
@@ -768,15 +844,57 @@ uint32_t rs_capture(int32_t fd, int32_t offset, int32_t stride, int32_t w, int32
if (fstat(fd, &st) < 0 || st.st_size < end) return 0;
const uint8_t *p = pool_map(fd, &st);
if (!p) return 0;
if (gpu_on) {
/* Whether ARGB pixels are all opaque is not worth reading them for: the GPU blends. */
uint32_t tex = gpu_tex_shm(p + offset, stride, w, h, format == 1);
return tex ? surface_add(w, h, NULL, tex, format == 1) : 0;
}
uint8_t *rgba = px_alloc((size_t)w * h * 4);
if (!rgba) return 0;
int opaque = swizzle_rows(p + offset, stride, rgba, w, h, format == 1);
do { next_surface++; } while (next_surface == 0 || surface(next_surface));
Surface *sf = malloc(sizeof *sf);
sf->id = next_surface; sf->w = w; sf->h = h; sf->rgba = rgba; sf->opaque = opaque;
sf->next = surfaces[sf->id % SURF_BUCKETS];
surfaces[sf->id % SURF_BUCKETS] = sf;
return sf->id;
return surface_add(w, h, rgba, 0, opaque);
}
/* ---- client buffers on the GPU (linux-dmabuf) ---- */
/* The device clients should draw on (its dev_t), 0 when there are no GPU buffers here. */
uint64_t rs_dmabuf_device(void) { return gpu_device(); }
#define DMABUF_FORMATS 512
/* Format i as fourcc, modifier low, modifier high into `out`; 0 past the last one. */
int32_t rs_dmabuf_format(int32_t i, int32_t *out) {
static uint32_t fourcc[DMABUF_FORMATS];
static uint64_t modifier[DMABUF_FORMATS];
static int n = -1;
if (n < 0) n = gpu_dmabuf_formats(fourcc, modifier, DMABUF_FORMATS);
if (i < 0 || i >= n) return 0;
out[0] = (int32_t)fourcc[i];
out[1] = (int32_t)(uint32_t)modifier[i];
out[2] = (int32_t)(uint32_t)(modifier[i] >> 32);
return 1;
}
/* planes: fd, offset, stride of each of `n`. */
uint32_t rs_dmabuf_import(int32_t n, const int32_t *planes, uint64_t modifier, int32_t w, int32_t h, uint32_t fourcc,
int32_t y_invert) {
int fd[4];
uint32_t offset[4], stride[4];
if (n < 1 || n > 4) return 0;
for (int i = 0; i < n; i++) {
fd[i] = planes[i * 3];
offset[i] = (uint32_t)planes[i * 3 + 1];
stride[i] = (uint32_t)planes[i * 3 + 2];
}
return gpu_dmabuf_import(n, fd, offset, stride, modifier, w, h, fourcc, y_invert);
}
void rs_dmabuf_release(uint32_t id) { gpu_dmabuf_free(id); }
/* A dmabuf as it is now, as a surface (a copy on the GPU, so its client can have it back at once). */
uint32_t rs_dmabuf_capture(uint32_t id) {
int w, h, opaque;
uint32_t tex = gpu_dmabuf_copy(id, &w, &h, &opaque);
return tex ? surface_add(w, h, NULL, tex, opaque) : 0;
}
void rs_release(uint32_t id) {
@@ -785,6 +903,7 @@ void rs_release(uint32_t id) {
if ((*p)->id == id) {
Surface *s = *p; *p = s->next;
px_free(s->rgba, (size_t)s->w * s->h * 4);
gpu_tex_free(s->tex, s->w, s->h);
free(s);
return;
}
@@ -818,9 +937,11 @@ int32_t rs_thumbnail(uint32_t id, int32_t w, int32_t h, int32_t reuse) {
if (w <= 0 || h <= 0 || w > MAX_IMAGE || h > MAX_IMAGE) return 0;
Surface *s = surface(id);
if (!s) return 0;
uint8_t *px = scale_rgba(s->rgba, s->w, s->h, w, h);
uint8_t *px = gpu_on ? NULL : scale_rgba(s->rgba, s->w, s->h, w, h);
uint32_t tex = gpu_on ? gpu_tex_scaled(s->tex, s->w, s->h, w, h) : 0;
if (!px && !tex) return 0;
int32_t img = image_slot(reuse);
images[img - 1].w = w; images[img - 1].h = h; images[img - 1].rgba = px;
images[img - 1].w = w; images[img - 1].h = h; images[img - 1].rgba = px; images[img - 1].tex = tex;
images[img - 1].changed = img == reuse;
images[img - 1].preview = 1;
return img;
@@ -832,6 +953,7 @@ int32_t rs_thumbnail(uint32_t id, int32_t w, int32_t h, int32_t reuse) {
int32_t rs_blit_surface(uint32_t id, int32_t dx, int32_t dy, int32_t dw, int32_t dh) {
Surface *s = surface(id);
if (!s) return 0;
if (gpu_on) { gpu_surface(s->tex, s->w, s->h, dx, dy, dw, dh); return 1; }
if (!fb || s->w <= 0 || s->h <= 0 || dw <= 0 || dh <= 0) return 1;
if (s->w < dw || s->h < dh) {
const uint8_t *o = s->rgba + ((size_t)(s->h - 1) * s->w + (s->w - 1)) * 4;
@@ -915,6 +1037,7 @@ static int32_t backdrop_w, backdrop_h;
/* Keep the framebuffer as the backdrop (wallpaper and desktop card). */
void rs_save_backdrop(void) {
if (gpu_on) { gpu_save_backdrop(); backdrop_w = fb_w; backdrop_h = fb_h; return; }
free(backdrop);
backdrop = malloc((size_t)fb_w * fb_h * 4);
memcpy(backdrop, fb, (size_t)fb_w * fb_h * 4);
@@ -923,11 +1046,12 @@ void rs_save_backdrop(void) {
}
/* 1 when a backdrop of the framebuffer's size is kept. */
int32_t rs_has_backdrop(void) { return backdrop && backdrop_w == fb_w && backdrop_h == fb_h; }
int32_t rs_has_backdrop(void) { return (backdrop || gpu_on) && backdrop_w == fb_w && backdrop_h == fb_h; }
/* Copy the backdrop back into the clip (or everywhere). */
void rs_copy_backdrop(void) {
if (!rs_has_backdrop()) return;
if (gpu_on) { gpu_copy_backdrop(); return; }
if (!clip_on) { memcpy(fb, backdrop, (size_t)fb_w * fb_h * 4); return; }
int x0 = imax(clip_x, 0), x1 = imin(clip_x + clip_w, fb_w);
int y0 = imax(clip_y, 0), y1 = imin(clip_y + clip_h, fb_h);
@@ -940,12 +1064,13 @@ void rs_copy_backdrop(void) {
int32_t rs_width(void) { return fb_w; }
int32_t rs_height(void) { return fb_h; }
uint8_t *rs_pixels(void) { return fb; }
static int fb_sync(void);
/* The framebuffer as RGBA8888 into `out` (w * h * 4 bytes). */
void rs_read_frame(moonbit_bytes_t out) {
size_t n = (size_t)fb_w * fb_h * 4;
if (fb && (size_t)Moonbit_array_length(out) >= n) memcpy(out, fb, n);
if (fb_sync() && (size_t)Moonbit_array_length(out) >= n) memcpy(out, fb, n);
}
/* ---- damage: what changed in the framebuffer since each output last took it ---- */
@@ -965,6 +1090,27 @@ static Box box_fb(Box b) {
return (Box){ imax(b.x0, 0), imax(b.y0, 0), imin(b.x1, fb_w), imin(b.y1, fb_h) };
}
/* What the CPU's copy of a frame on the GPU is missing. */
static Box cpu_stale;
static void gpu_stale_all(void) { cpu_stale = (Box){ 0, 0, fb_w, fb_h }; }
/* Have the frame's pixels in `fb`: with a GPU, read back what changed since the last time. 0 when
* there is no frame. */
static int fb_sync(void) {
if (!gpu_on) return fb != NULL;
if (fb_w <= 0 || fb_h <= 0 || canvas_bad) return 0;
if (!fb) {
fb = calloc((size_t)fb_w * fb_h, 4);
if (!fb) return 0;
gpu_stale_all();
}
Box b = box_fb(cpu_stale);
if (b.x0 < b.x1 && b.y0 < b.y1) gpu_read(fb, fb_w, b.x0, b.y0, b.x1, b.y1);
cpu_stale = (Box){ 0, 0, 0, 0 };
return 1;
}
#define EXPORT_SLOTS 3
static Box kms_damage;
static Box slot_damage[EXPORT_SLOTS];
@@ -974,6 +1120,7 @@ static Box shm_pub_dmg;
/* The frame just drawn changed (x, y, w, h) of the framebuffer. */
void rs_damage(int32_t x, int32_t y, int32_t w, int32_t h) {
box_add(&kms_damage, x, y, x + w, y + h);
box_add(&cpu_stale, x, y, x + w, y + h);
for (int i = 0; i < EXPORT_SLOTS; i++) box_add(&slot_damage[i], x, y, x + w, y + h);
box_add(&shm_slot_dmg[0], x, y, x + w, y + h);
box_add(&shm_slot_dmg[1], x, y, x + w, y + h);
@@ -1014,7 +1161,7 @@ static void slots_drop(void) {
/* A memfd holding the framebuffer as little-endian ARGB8888 (B, G, R, 255), for a wl_shm pool;
* the caller closes it (it is a duplicate of the slot's own). -1 on failure. */
int32_t rs_export_memfd(void) {
if (!fb) return -1;
if (!fb_sync()) return -1;
size_t n = (size_t)fb_w * fb_h * 4;
if (slot_w != fb_w || slot_h != fb_h) {
slots_drop();
@@ -1041,10 +1188,32 @@ int32_t rs_export_memfd(void) {
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);
/* compositor/kms/kms_stub.c: the buffer to draw the next frame into (its index, -1 when no frame
* can be shown now), as a dmabuf with its layout, and the part of the frame it is missing; then
* show it. */
int32_t mp_kms_back_buffer(int32_t w, int32_t h, int32_t *info, int32_t *missing);
int32_t mp_kms_flip(void);
void mp_kms_add_damage(int32_t x, int32_t y, int32_t w, int32_t h);
/* 1 when the GPU drew the frame into the display's own buffer (or nothing can be shown now). */
static int present_gpu(void) {
int32_t info[6], m[4];
int32_t i = mp_kms_back_buffer(fb_w, fb_h, info, m);
if (i < 0) return i != -2;
if (!gpu_scanout(i, info[0], (uint32_t)info[1], info[2], info[3], info[4], info[5], m[0], m[1], m[2], m[3])) return 0;
mp_kms_flip();
return 1;
}
void rs_present_kms(void) {
if (!fb) return;
if (!fb && !gpu_on) return;
Box b = box_fb(kms_damage);
if (b.x0 >= b.x1 || b.y0 >= b.y1) b = (Box){ 0, 0, 0, 0 };
if (gpu_on && !canvas_bad) {
mp_kms_add_damage(b.x0, b.y0, b.x1 - b.x0, b.y1 - b.y0);
if (present_gpu()) { kms_damage = (Box){ 0, 0, 0, 0 }; return; }
}
if (!fb_sync()) return;
/* The display keeps what it was not given yet, so the damage is handed over either way. */
mp_kms_present_damage((moonbit_bytes_t)fb, fb_w, fb_h, b.x0, b.y0, b.x1 - b.x0, b.y1 - b.y0);
kms_damage = (Box){ 0, 0, 0, 0 };
@@ -1154,12 +1323,12 @@ static void *rec_writer(void *arg) {
}
int32_t rs_grab_recording(int32_t x, int32_t y, int32_t w, int32_t h, int32_t pointer, int32_t px, int32_t py) {
if (!fb || w <= 0 || h <= 0 || x < 0 || y < 0 || x + w > fb_w || y + h > fb_h) return 0;
if (w <= 0 || h <= 0 || x < 0 || y < 0 || x + w > fb_w || y + h > fb_h) return 0;
pthread_mutex_lock(&rec_mu);
int full = rec_count == REC_SLOTS || rec_failed;
int i = (rec_first + rec_count) % REC_SLOTS;
pthread_mutex_unlock(&rec_mu);
if (full) return 0;
if (full || !fb_sync()) return 0;
/* Slot i is not in the queue, so the writer doesn't touch it while it is filled. */
size_t n = (size_t)w * h * 4;
if (rec_cap[i] < REC_HEAD + n) {
@@ -1231,7 +1400,7 @@ static uint8_t *snap;
static int32_t snap_w, snap_h;
int32_t rs_snapshot(void) {
if (!fb) return 0;
if (!fb_sync()) return 0;
size_t n = (size_t)fb_w * fb_h * 4;
if (!snap || snap_w != fb_w || snap_h != fb_h) { free(snap); snap = malloc(n); snap_w = fb_w; snap_h = fb_h; }
memcpy(snap, fb, n);
@@ -1318,7 +1487,8 @@ static void shm_ensure_open(void) {
}
void rs_publish_frame(void) {
if (!fb || fb_w <= 0 || fb_h <= 0) return;
if (gpu_on) gpu_submit(); /* the frame is drawn: start the GPU on it */
if ((!fb && !gpu_on) || fb_w <= 0 || fb_h <= 0) return;
if ((size_t)fb_w * (size_t)fb_h * 4 > CHANDRA_FB_SLOT_BYTES) return;
shm_ensure_open();
if (!shm_hdr) return;
@@ -1338,6 +1508,7 @@ void rs_publish_frame(void) {
if (cur_seq == 0) pb = (Box){ 0, 0, fb_w, fb_h };
else return;
}
if (!fb_sync()) return;
uint32_t next = 1u - (__atomic_load_n(&shm_hdr->active_slot, __ATOMIC_RELAXED) & 1u);
uint8_t *dst = chandra_fb_slot_ptr(shm_hdr, next, shm_hdr->slot_bytes);
Box sb = box_fb(shm_slot_dmg[next]);