CI / test (push) Failing after 17s
New chandra:host/raster functions, on both hosts: write-frame-png (--gfx-out), grab-recording-frame / send-recording-frame (the recorder streams frames from the host to ffmpeg) and snapshot-frame / snapshot-png / snapshot-pixels (screenshots freeze, crop and encode the screen there; the magnifier and the notification's thumbnail read small scaled copies). The wasm compositor sets them up through frame_recorder and frame_shots; the native compositor keeps its pixel path. Moving whole frames as list<u8> is cheap for the Rust host but costs about 0.4 s a 720p frame through the wasmtime C API. A full-screen screenshot now takes 45 ms on the MoonBit host (was 472) and 8 ms on the Rust host (was 112); recording runs at 30 fps on both. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5hiNfjrw2edja7LiRLB
698 lines
26 KiB
C
698 lines
26 KiB
C
/* The host's rasteriser (chandra:host/raster), pixel work in C.
|
|
*
|
|
* A port of host-wasm/src/raster.rs, itself a port of compositor/renderer, so a frame comes out
|
|
* pixel for pixel the same. raster.mbt decides what to draw (layers, damage, the wallpaper cache);
|
|
* 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.
|
|
*
|
|
* Glyphs come from stb_truetype through compositor/font's shim (mp_font_*); SVG icons from
|
|
* nanosvg (third_party/nanosvg). */
|
|
#define _GNU_SOURCE
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <math.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/mman.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
#include "moonbit.h"
|
|
|
|
#define NANOSVG_IMPLEMENTATION
|
|
#define NANOSVGRAST_IMPLEMENTATION
|
|
#include "../../third_party/nanosvg/nanosvg.h"
|
|
#include "../../third_party/nanosvg/nanosvgrast.h"
|
|
|
|
/* compositor/font/font_stub.c */
|
|
int32_t mp_font_load(int32_t slot, moonbit_bytes_t path_bytes);
|
|
void mp_font_metrics(int32_t slot, int32_t cp, int32_t size, int32_t *info);
|
|
void mp_font_bitmap(int32_t slot, int32_t cp, int32_t size, int32_t w, int32_t h, moonbit_bytes_t buf);
|
|
|
|
/* ------------------------------------------------------------------ canvas */
|
|
|
|
static int32_t fb_w, fb_h;
|
|
static uint8_t *fb; /* RGBA8888 */
|
|
|
|
static int clip_on;
|
|
static int32_t clip_x, clip_y, clip_w, clip_h;
|
|
|
|
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; }
|
|
|
|
/* 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;
|
|
free(fb);
|
|
fb = calloc((size_t)w * h, 4);
|
|
fb_w = w;
|
|
fb_h = h;
|
|
return 1;
|
|
}
|
|
|
|
void rs_clear(void) { 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;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
/* 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;
|
|
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; }
|
|
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);
|
|
}
|
|
}
|
|
|
|
static int in_rounded_rect(int px, int py, int rx, int ry, int rw, int rh, int rad) {
|
|
if (px < rx || py < ry || px >= rx + rw || py >= ry + rh) return 0;
|
|
if (rad <= 0) return 1;
|
|
int r = imin(rad, imin(rw, rh) / 2);
|
|
if (r <= 0) return 1;
|
|
int x_left = rx + r, x_right = rx + rw - r - 1, y_top = ry + r, y_bot = ry + rh - r - 1;
|
|
#define CORNER(dx, dy) ((dx) * (dx) + (dy) * (dy) <= r * r)
|
|
if (px < x_left && py < y_top) return CORNER(x_left - px, y_top - py);
|
|
if (px > x_right && py < y_top) return CORNER(px - x_right, y_top - py);
|
|
if (px < x_left && py > y_bot) return CORNER(x_left - px, py - y_bot);
|
|
if (px > x_right && py > y_bot) return CORNER(px - x_right, py - y_bot);
|
|
#undef CORNER
|
|
return 1;
|
|
}
|
|
|
|
/* The part of the rect on the canvas and inside the clip. 0 when empty. */
|
|
static int visible(int x, int y, int w, int h, int *x0, int *y0, int *x1, int *y1) {
|
|
*x0 = imax(x, 0); *y0 = imax(y, 0);
|
|
*x1 = imin(x + w, fb_w); *y1 = imin(y + h, fb_h);
|
|
if (clip_on) {
|
|
*x0 = imax(*x0, clip_x); *y0 = imax(*y0, clip_y);
|
|
*x1 = imin(*x1, clip_x + clip_w); *y1 = imin(*y1, clip_y + clip_h);
|
|
}
|
|
return *x0 < *x1 && *y0 < *y1;
|
|
}
|
|
|
|
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 (!fb || w <= 0 || h <= 0 || ca == 0) return;
|
|
int x0, y0, x1, y1;
|
|
if (!visible(x, y, w, h, &x0, &y0, &x1, &y1)) return;
|
|
if (radius <= 0) {
|
|
for (int yy = y0; yy < y1; yy++) blend_span(yy, x0, x1, cr, cg, cb, ca);
|
|
return;
|
|
}
|
|
int r = imin(radius, imin(w, h) / 2);
|
|
int x_left = x + r, x_right = x + w - r - 1, y_top = y + r, y_bot = y + h - r - 1;
|
|
for (int yy = y0; yy < y1; yy++) {
|
|
if (yy >= y_top && yy <= y_bot) { blend_span(yy, x0, x1, cr, cg, cb, ca); continue; }
|
|
int xx = x0;
|
|
while (xx < x1 && xx < x_left) {
|
|
if (in_rounded_rect(xx, yy, x, y, w, h, radius)) blend_span(yy, xx, xx + 1, cr, cg, cb, ca);
|
|
xx++;
|
|
}
|
|
int mid_a = imax(x_left, x0), mid_b = imin(x_right + 1, x1);
|
|
blend_span(yy, mid_a, mid_b, cr, cg, cb, ca);
|
|
xx = imax(imax(x_right + 1, x0), mid_a);
|
|
while (xx < x1) {
|
|
if (in_rounded_rect(xx, yy, x, y, w, h, radius)) blend_span(yy, xx, xx + 1, cr, cg, cb, ca);
|
|
xx++;
|
|
}
|
|
}
|
|
}
|
|
|
|
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 (!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;
|
|
int inner_r = radius > width ? radius - width : 0;
|
|
int cx0 = clip_on ? clip_x : 0, cx1 = clip_on ? clip_x + clip_w : fb_w;
|
|
int cy0 = clip_on ? clip_y : 0, cy1 = clip_on ? clip_y + clip_h : fb_h;
|
|
int mid_a = inner_y + inner_r, mid_b = inner_y + inner_h - inner_r;
|
|
for (int yy = y0; yy < y1; yy++) {
|
|
if (inner_w > 0 && inner_h > 0 && yy >= mid_a && yy < mid_b) {
|
|
if (yy >= cy0 && yy < cy1) {
|
|
int l0 = imax(x0, cx0), l1 = imin(imin(inner_x, x1), cx1);
|
|
blend_span(yy, l0, l1, cr, cg, cb, ca);
|
|
int r0 = imax(imax(inner_x + inner_w, x0), cx0);
|
|
blend_span(yy, r0, imin(x1, cx1), cr, cg, cb, ca);
|
|
}
|
|
continue;
|
|
}
|
|
for (int xx = x0; xx < x1; xx++) {
|
|
int in_outer = in_rounded_rect(xx, yy, x, y, w, h, radius);
|
|
int in_inner = inner_w > 0 && inner_h > 0 && in_rounded_rect(xx, yy, inner_x, inner_y, inner_w, inner_h, inner_r);
|
|
if (in_outer && !in_inner) blend_pixel(xx, yy, cr, cg, cb, ca);
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
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++)
|
|
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;
|
|
if (e2 >= dy) { err += dy; x0 += sx; }
|
|
if (e2 <= dx) { err += dx; y0 += sy; }
|
|
}
|
|
}
|
|
|
|
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;
|
|
for (int dy = -radius - 1; dy <= radius + 1; dy++) {
|
|
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 (cov > 0.02) blend_pixel(cx + dx, cy + dy, cr, cg, cb, (int)round(ca * cov));
|
|
}
|
|
}
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ glyphs */
|
|
|
|
typedef struct Glyph {
|
|
int32_t slot, cp, size;
|
|
int32_t w, h, xoff, yoff, advance;
|
|
uint8_t *coverage;
|
|
struct Glyph *next;
|
|
} Glyph;
|
|
|
|
#define GLYPH_BUCKETS 4096
|
|
static Glyph *glyphs[GLYPH_BUCKETS];
|
|
static int font_loaded[4];
|
|
|
|
static Glyph *glyph(int32_t slot, int32_t cp, int32_t size) {
|
|
uint32_t hsh = ((uint32_t)slot * 2654435761u) ^ ((uint32_t)cp * 40503u) ^ ((uint32_t)size * 977u);
|
|
Glyph **b = &glyphs[hsh % GLYPH_BUCKETS];
|
|
for (Glyph *g = *b; g; g = g->next)
|
|
if (g->slot == slot && g->cp == cp && g->size == size) return g;
|
|
Glyph *g = calloc(1, sizeof *g);
|
|
g->slot = slot; g->cp = cp; g->size = size;
|
|
int32_t info[5] = {0};
|
|
if (slot >= 0 && slot < 4 && font_loaded[slot]) mp_font_metrics(slot, cp, size, info);
|
|
g->w = info[0]; g->h = info[1]; g->xoff = info[2]; g->yoff = info[3]; g->advance = info[4];
|
|
if (g->w > 0 && g->h > 0) {
|
|
/* The shim checks the buffer's MoonBit length: hand it a real MoonBit byte string. */
|
|
moonbit_bytes_t buf = moonbit_make_bytes(g->w * g->h, 0);
|
|
mp_font_bitmap(slot, cp, size, g->w, g->h, buf);
|
|
g->coverage = malloc((size_t)g->w * g->h);
|
|
memcpy(g->coverage, buf, (size_t)g->w * g->h);
|
|
moonbit_decref(buf);
|
|
} else {
|
|
g->w = g->h = 0;
|
|
}
|
|
g->next = *b;
|
|
*b = g;
|
|
return g;
|
|
}
|
|
|
|
static void drop_glyphs(int32_t slot) {
|
|
for (int i = 0; i < GLYPH_BUCKETS; i++) {
|
|
Glyph **p = &glyphs[i];
|
|
while (*p) {
|
|
Glyph *g = *p;
|
|
if (g->slot == slot) { *p = g->next; free(g->coverage); free(g); }
|
|
else p = &g->next;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Slot `slot` now holds a newly loaded face (raster.mbt loads it with mp_font_load, which also
|
|
* links compositor/font's shim in). */
|
|
void rs_font_loaded(int32_t slot) {
|
|
if (slot < 0 || slot >= 4) return;
|
|
font_loaded[slot] = 1;
|
|
drop_glyphs(slot);
|
|
}
|
|
|
|
int32_t rs_font_has(int32_t slot) { return slot >= 0 && slot < 4 && font_loaded[slot]; }
|
|
|
|
/* Metrics into info: width, height, xoff, yoff, advance. */
|
|
void rs_glyph_info(int32_t slot, int32_t cp, int32_t size, int32_t *info) {
|
|
Glyph *g = glyph(slot, cp, size);
|
|
info[0] = g->w; info[1] = g->h; info[2] = g->xoff; info[3] = g->yoff; info[4] = g->advance;
|
|
}
|
|
|
|
/* The coverage bitmap (w * h bytes) into `out`. */
|
|
void rs_glyph_coverage(int32_t slot, int32_t cp, int32_t size, moonbit_bytes_t out) {
|
|
Glyph *g = glyph(slot, cp, size);
|
|
size_t n = (size_t)g->w * g->h, cap = (size_t)Moonbit_array_length(out);
|
|
if (g->coverage && n <= cap) memcpy(out, g->coverage, n);
|
|
}
|
|
|
|
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;
|
|
Glyph *g = glyph(slot, cp, size);
|
|
for (int row = 0; row < g->h; row++) {
|
|
for (int col = 0; col < g->w; col++) {
|
|
int cov = g->coverage[row * g->w + col];
|
|
if (cov > 0) {
|
|
int px = pen + g->xoff + col, py = baseline + g->yoff + row;
|
|
blend_pixel(px, py, cr, cg, cb, ca * cov / 255);
|
|
if (embolden) blend_pixel(px + 1, py, cr, cg, cb, ca * cov / 255);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ images */
|
|
|
|
#define MAX_IMAGE 1024
|
|
|
|
typedef struct Sized { int32_t w, h; uint8_t *rgba; struct Sized *next; } Sized;
|
|
typedef struct {
|
|
int used;
|
|
int32_t w, h;
|
|
uint8_t *rgba; /* pixels, or NULL for an SVG */
|
|
NSVGimage *svg;
|
|
Sized *sized; /* the image at the sizes it was drawn at */
|
|
} 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. */
|
|
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);
|
|
for (int y = 0; y < h; y++) {
|
|
int y0 = y * sh / h, y1 = imax((y + 1) * sh / h, y0 + 1);
|
|
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++;
|
|
}
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
static void image_reset(Image *im) {
|
|
free(im->rgba);
|
|
if (im->svg) nsvgDelete(im->svg);
|
|
for (Sized *s = im->sized; s;) { Sized *n = s->next; free(s->rgba); free(s); s = n; }
|
|
memset(im, 0, sizeof *im);
|
|
}
|
|
|
|
static int32_t image_slot(int32_t reuse) {
|
|
if (reuse > 0 && reuse <= n_images) {
|
|
image_reset(&images[reuse - 1]);
|
|
images[reuse - 1].used = 1;
|
|
return reuse;
|
|
}
|
|
if (n_images == cap_images) {
|
|
cap_images = cap_images ? cap_images * 2 : 64;
|
|
images = realloc(images, sizeof(Image) * (size_t)cap_images);
|
|
}
|
|
memset(&images[n_images], 0, sizeof(Image));
|
|
images[n_images].used = 1;
|
|
return ++n_images;
|
|
}
|
|
|
|
/* Store w x h RGBA pixels as an image: overwrite `reuse` when it is an earlier handle, else add one. */
|
|
int32_t rs_image_put(int32_t reuse, int32_t w, int32_t h, moonbit_bytes_t rgba) {
|
|
if (w <= 0 || h <= 0 || w > MAX_IMAGE || h > MAX_IMAGE) return 0;
|
|
if ((size_t)Moonbit_array_length(rgba) < (size_t)w * h * 4) return 0;
|
|
int32_t id = image_slot(reuse);
|
|
Image *im = &images[id - 1];
|
|
im->w = w; im->h = h;
|
|
im->rgba = malloc((size_t)w * h * 4);
|
|
memcpy(im->rgba, rgba, (size_t)w * h * 4);
|
|
return id;
|
|
}
|
|
|
|
/* An SVG document (its bytes) as an image, rendered sharp at each size it is drawn at. 0 when it
|
|
* doesn't parse. */
|
|
int32_t rs_image_svg(moonbit_bytes_t data) {
|
|
size_t n = (size_t)Moonbit_array_length(data);
|
|
char *text = malloc(n + 1);
|
|
memcpy(text, data, n);
|
|
text[n] = 0;
|
|
NSVGimage *svg = nsvgParse(text, "px", 96.0f);
|
|
free(text);
|
|
if (!svg) return 0;
|
|
if (svg->width <= 0 || svg->height <= 0 || !svg->shapes) { nsvgDelete(svg); return 0; }
|
|
int32_t id = image_slot(0);
|
|
images[id - 1].svg = svg;
|
|
return id;
|
|
}
|
|
|
|
static uint8_t *render_svg(NSVGimage *svg, int w, int h) {
|
|
static NSVGrasterizer *rast;
|
|
if (!rast) rast = nsvgCreateRasterizer();
|
|
uint8_t *out = calloc((size_t)w * h, 4);
|
|
float k = fminf((float)w / svg->width, (float)h / svg->height);
|
|
float dx = ((float)w - svg->width * k) / 2.0f, dy = ((float)h - svg->height * k) / 2.0f;
|
|
nsvgRasterize(rast, svg, dx, dy, k, out, w, h, w * 4); /* straight (not premultiplied) RGBA */
|
|
return out;
|
|
}
|
|
|
|
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 (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;
|
|
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;
|
|
Sized *s = malloc(sizeof *s);
|
|
s->w = w; s->h = h; s->rgba = px; s->next = im->sized;
|
|
im->sized = s;
|
|
return px;
|
|
}
|
|
|
|
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]);
|
|
}
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ surfaces */
|
|
|
|
typedef struct Surface {
|
|
uint32_t id;
|
|
int32_t w, h;
|
|
uint8_t *rgba;
|
|
int opaque; /* every pixel has alpha 255: blits copy rows */
|
|
struct Surface *next;
|
|
} Surface;
|
|
|
|
#define SURF_BUCKETS 256
|
|
static Surface *surfaces[SURF_BUCKETS];
|
|
static uint32_t next_surface;
|
|
|
|
static Surface *surface(uint32_t id) {
|
|
for (Surface *s = surfaces[id % SURF_BUCKETS]; s; s = s->next)
|
|
if (s->id == id) return s;
|
|
return NULL;
|
|
}
|
|
|
|
/* 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) {
|
|
if (fd < 0 || offset < 0 || stride <= 0 || w <= 0 || h <= 0 || stride < w * 4) return 0;
|
|
int64_t len = (int64_t)stride * h, end = offset + len;
|
|
struct stat st;
|
|
if (fstat(fd, &st) < 0 || st.st_size < end) return 0;
|
|
/* Mapping a large pool only reserves address space; just the pages copied are touched. */
|
|
uint8_t *p = mmap(NULL, (size_t)end, PROT_READ, MAP_SHARED, fd, 0);
|
|
if (p == MAP_FAILED) return 0;
|
|
const uint8_t *src = p + offset;
|
|
uint8_t *rgba = malloc((size_t)w * h * 4);
|
|
int opaque = 1, xrgb = format == 1;
|
|
for (int y = 0; y < h; y++) {
|
|
const uint8_t *s = src + (size_t)y * stride;
|
|
uint8_t *d = rgba + (size_t)y * w * 4;
|
|
for (int x = 0; x < w; x++, s += 4, d += 4) {
|
|
d[0] = s[2]; d[1] = s[1]; d[2] = s[0];
|
|
d[3] = xrgb ? 0xff : s[3];
|
|
opaque &= d[3] == 0xff;
|
|
}
|
|
}
|
|
munmap(p, (size_t)end);
|
|
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;
|
|
}
|
|
|
|
void rs_release(uint32_t id) {
|
|
Surface **p = &surfaces[id % SURF_BUCKETS];
|
|
while (*p) {
|
|
if ((*p)->id == id) { Surface *s = *p; *p = s->next; free(s->rgba); free(s); return; }
|
|
p = &(*p)->next;
|
|
}
|
|
}
|
|
|
|
int32_t rs_surface_count(void) {
|
|
int n = 0;
|
|
for (int i = 0; i < SURF_BUCKETS; i++) for (Surface *s = surfaces[i]; s; s = s->next) n++;
|
|
return n;
|
|
}
|
|
|
|
/* A w x h scaled copy of surface `id` as an image (see rs_image_put for `reuse`). */
|
|
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);
|
|
int32_t img = image_slot(reuse);
|
|
images[img - 1].w = w; images[img - 1].h = h; images[img - 1].rgba = px;
|
|
return img;
|
|
}
|
|
|
|
/* Draw surface `id` into (x, y, w, h). While a client catches up with a resize its buffer is
|
|
* smaller than the window: the buffer's bottom-right colour is extended over the rest.
|
|
* 0 when there is no such surface. */
|
|
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 (!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;
|
|
rs_fill_rect(dx, dy, dw, dh, o[0], o[1], o[2], o[3], 0);
|
|
}
|
|
int copy_w = imin(s->w, dw), copy_h = imin(s->h, dh);
|
|
int c0 = imax(-dx, 0), c1 = imin(copy_w, fb_w - dx);
|
|
int r0 = imax(-dy, 0), r1 = imin(copy_h, fb_h - dy);
|
|
if (clip_on) {
|
|
c0 = imax(c0, clip_x - dx); c1 = imin(c1, clip_x + clip_w - dx);
|
|
r0 = imax(r0, clip_y - dy); r1 = imin(r1, clip_y + clip_h - dy);
|
|
}
|
|
if (c0 >= c1 || r0 >= r1) return 1;
|
|
for (int row = r0; row < r1; row++) {
|
|
const uint8_t *src = s->rgba + ((size_t)row * s->w + c0) * 4;
|
|
uint8_t *dst = fb + ((size_t)(dy + row) * fb_w + dx + c0) * 4;
|
|
size_t n = (size_t)(c1 - c0) * 4;
|
|
if (s->opaque) { memcpy(dst, src, n); continue; }
|
|
for (size_t i = 0; i < n; i += 4) {
|
|
int a = src[i + 3];
|
|
if (a >= 255) memcpy(dst + i, src + i, 4);
|
|
else if (a > 0) {
|
|
/* wl_shm ARGB8888 is premultiplied: the colour already carries its alpha. */
|
|
int inv = 255 - a;
|
|
dst[i] = (uint8_t)clamp_u8(src[i] + (dst[i] * inv + 127) / 255);
|
|
dst[i + 1] = (uint8_t)clamp_u8(src[i + 1] + (dst[i + 1] * inv + 127) / 255);
|
|
dst[i + 2] = (uint8_t)clamp_u8(src[i + 2] + (dst[i + 2] * inv + 127) / 255);
|
|
dst[i + 3] = (uint8_t)clamp_u8(a + (dst[i + 3] * inv + 127) / 255);
|
|
}
|
|
}
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
/* ------------------------------------------------------------------ backdrop and output */
|
|
|
|
static uint8_t *backdrop;
|
|
static int32_t backdrop_w, backdrop_h;
|
|
|
|
/* Keep the framebuffer as the backdrop (wallpaper and desktop card). */
|
|
void rs_save_backdrop(void) {
|
|
free(backdrop);
|
|
backdrop = malloc((size_t)fb_w * fb_h * 4);
|
|
memcpy(backdrop, fb, (size_t)fb_w * fb_h * 4);
|
|
backdrop_w = fb_w;
|
|
backdrop_h = fb_h;
|
|
}
|
|
|
|
/* 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; }
|
|
|
|
/* Copy the backdrop back into the clip (or everywhere). */
|
|
void rs_copy_backdrop(void) {
|
|
if (!rs_has_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);
|
|
if (x0 >= x1) return;
|
|
for (int y = y0; y < y1; y++) {
|
|
size_t a = ((size_t)y * fb_w + x0) * 4;
|
|
memcpy(fb + a, backdrop + a, (size_t)(x1 - x0) * 4);
|
|
}
|
|
}
|
|
|
|
int32_t rs_width(void) { return fb_w; }
|
|
int32_t rs_height(void) { return fb_h; }
|
|
uint8_t *rs_pixels(void) { return fb; }
|
|
|
|
/* 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);
|
|
}
|
|
|
|
/* A new memfd holding the framebuffer as little-endian ARGB8888 (B, G, R, 255). -1 on failure. */
|
|
int32_t rs_export_memfd(void) {
|
|
if (!fb) return -1;
|
|
size_t n = (size_t)fb_w * fb_h * 4;
|
|
uint8_t *data = malloc(n);
|
|
for (size_t i = 0; i < n; i += 4) {
|
|
data[i] = fb[i + 2]; data[i + 1] = fb[i + 1]; data[i + 2] = fb[i]; data[i + 3] = 0xff;
|
|
}
|
|
int fd = memfd_create("chandra-frame", MFD_CLOEXEC);
|
|
if (fd < 0) { free(data); return -1; }
|
|
size_t done = 0;
|
|
while (done < n) {
|
|
ssize_t w = write(fd, data + done, n - done);
|
|
if (w <= 0) { close(fd); free(data); return -1; }
|
|
done += (size_t)w;
|
|
}
|
|
free(data);
|
|
return fd;
|
|
}
|
|
|
|
/* compositor/kms/kms_stub.c: show an RGBA8888 frame on the login session's output. */
|
|
int32_t mp_kms_present(moonbit_bytes_t pixels, int32_t w, int32_t h);
|
|
|
|
void rs_present_kms(void) {
|
|
if (fb) mp_kms_present((moonbit_bytes_t)fb, fb_w, fb_h);
|
|
}
|
|
|
|
/* ---- screen recording: the frame in flight stays here ---- */
|
|
|
|
static uint8_t *rec_px;
|
|
static size_t rec_len, rec_sent;
|
|
|
|
static void rec_fill(int32_t w, int32_t h, int32_t x, int32_t y, int32_t rw, int32_t rh, uint8_t v) {
|
|
for (int32_t j = imax(y, 0); j < imin(y + rh, h); j++)
|
|
for (int32_t i = imax(x, 0); i < imin(x + rw, w); i++) {
|
|
uint8_t *p = rec_px + ((size_t)j * w + i) * 4;
|
|
p[0] = p[1] = p[2] = v; p[3] = 0xff;
|
|
}
|
|
}
|
|
|
|
/* The arrow pointer with its tip at (x, y), as recorder.mbt's draw_pointer draws it: a black body
|
|
* widening over 13 rows and a short tail, then the white inside. */
|
|
static void rec_pointer(int32_t w, int32_t h, int32_t x, int32_t y) {
|
|
for (int i = 0; i < 17; i++) {
|
|
int l = i < 13 ? 0 : 3 + (i - 13), bw = i < 13 ? i * 2 / 3 + 1 : 3;
|
|
rec_fill(w, h, x + l - 1, y + i - 1, bw + 2, 3, 0);
|
|
}
|
|
for (int i = 1; i < 16; i++) {
|
|
int l = i < 13 ? 0 : 4 + (i - 13), bw = i < 13 ? i * 2 / 3 - 1 : 1;
|
|
if (bw > 0) rec_fill(w, h, x + l + 1, y + i, bw, 1, 0xff);
|
|
}
|
|
}
|
|
|
|
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;
|
|
size_t n = (size_t)w * h * 4;
|
|
if (n != rec_len) { free(rec_px); rec_px = malloc(n); rec_len = n; }
|
|
for (int32_t j = 0; j < h; j++) memcpy(rec_px + (size_t)j * w * 4, fb + ((size_t)(y + j) * fb_w + x) * 4, (size_t)w * 4);
|
|
for (size_t i = 3; i < n; i += 4) rec_px[i] = 0xff;
|
|
if (pointer) rec_pointer(w, h, px - x, py - y);
|
|
rec_sent = 0;
|
|
return 1;
|
|
}
|
|
|
|
int32_t rs_send_recording(int32_t fd) {
|
|
if (!rec_px) return 0;
|
|
int fl = fcntl(fd, F_GETFL);
|
|
if (fl >= 0 && !(fl & O_NONBLOCK)) fcntl(fd, F_SETFL, fl | O_NONBLOCK);
|
|
while (rec_sent < rec_len) {
|
|
ssize_t w = send(fd, rec_px + rec_sent, rec_len - rec_sent, MSG_NOSIGNAL);
|
|
if (w < 0 && errno == ENOTSOCK) w = write(fd, rec_px + rec_sent, rec_len - rec_sent);
|
|
if (w < 0) {
|
|
if (errno == EINTR) continue;
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) break;
|
|
return -1;
|
|
}
|
|
rec_sent += (size_t)w;
|
|
}
|
|
size_t left = rec_len - rec_sent;
|
|
return left > 0x7fffffff ? 0x7fffffff : (int32_t)left;
|
|
}
|
|
|
|
/* ---- screenshots: the frozen screen stays here ---- */
|
|
|
|
static uint8_t *snap;
|
|
static int32_t snap_w, snap_h;
|
|
|
|
int32_t rs_snapshot(void) {
|
|
if (!fb) 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);
|
|
return 1;
|
|
}
|
|
|
|
int32_t rs_snapshot_w(void) { return snap ? snap_w : 0; }
|
|
int32_t rs_snapshot_h(void) { return snap ? snap_h : 0; }
|
|
|
|
/* `x`, `y`, `w` x `h` of the snapshot scaled (nearest) to `ow` x `oh` into `out` (ow * oh * 4
|
|
* bytes), opaque; off the snapshot is black. */
|
|
void rs_snapshot_pixels(int32_t x, int32_t y, int32_t w, int32_t h, int32_t ow, int32_t oh, moonbit_bytes_t out) {
|
|
if ((size_t)Moonbit_array_length(out) < (size_t)ow * oh * 4 || ow <= 0 || oh <= 0) return;
|
|
for (int32_t ty = 0; ty < oh; ty++) {
|
|
int32_t sy = y + (int32_t)((int64_t)ty * h / oh);
|
|
for (int32_t tx = 0; tx < ow; tx++) {
|
|
int32_t sx = x + (int32_t)((int64_t)tx * w / ow);
|
|
uint8_t *d = out + ((size_t)ty * ow + tx) * 4;
|
|
if (snap && sx >= 0 && sy >= 0 && sx < snap_w && sy < snap_h) memcpy(d, snap + ((size_t)sy * snap_w + sx) * 4, 3);
|
|
else d[0] = d[1] = d[2] = 0;
|
|
d[3] = 0xff;
|
|
}
|
|
}
|
|
}
|