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:
1 parent
d81003f176
commit
cfb25fe699
26 files changed
+2485
-105
No files matched your search
+3
-1
@@ -22,7 +22,9 @@ build/wasmhost build/compositor.wasm --socket PATH --gfx-out frame.png
|
||||
(`snapshot-*`, `*-recording-frame`, `write-frame-png` in `wit/host/raster.wit`).
|
||||
- `bin/`: the interfaces of `wit/host`. Most of it is the native compositor's own packages:
|
||||
`sys` is compositor/sys, glyphs come from compositor/font (stb_truetype). The rasteriser
|
||||
(`raster.mbt`, pixel work in `raster_stub.c`) is a port of compositor/renderer; SVG icons
|
||||
(`raster.mbt`, pixel work in `raster_stub.c`) is a port of compositor/renderer; with a GPU the
|
||||
same calls draw on it (`raster_gpu.c`, OpenGL ES 3 loaded at run time; `scripts/test-gpu.sh`
|
||||
compares the two; docs/rendering.md). SVG icons
|
||||
use nanosvg (`third_party/nanosvg`, patched for clip paths and masks). `plasmoids.mbt` runs
|
||||
panel modules through compositor/wasm (wasm.h, which libwasmtime provides); `display.mbt` is the login session's DRM/KMS output and input (compositor/kms).
|
||||
|
||||
|
||||
@@ -70,6 +70,7 @@ fn main {
|
||||
args.remove(0) |> ignore
|
||||
}
|
||||
c_ignore_sigchld()
|
||||
ignore(rs_gpu_init(if args.contains("--drm") { 1 } else { 0 }))
|
||||
match @wt.load(path) {
|
||||
Ok(_) => ()
|
||||
Err(e) => {
|
||||
|
||||
@@ -24,7 +24,7 @@ pkgtype(kind: "executable")
|
||||
// looks for it there too (the rpath is relative to build/), not through LD_LIBRARY_PATH, which
|
||||
// apps started by the compositor would inherit.
|
||||
options(
|
||||
"native-stub": [ "host_stub.c", "raster_stub.c" ],
|
||||
"native-stub": [ "host_stub.c", "raster_stub.c", "raster_gpu.c" ],
|
||||
link: {
|
||||
"native": {
|
||||
"cc-link-flags": "-l:libwasmtime.a -lpthread -ldl -lm -ldrm -linput -lseat -lxkbcommon -ludev -Wl,-rpath,$ORIGIN/../.pixi/envs/default/lib",
|
||||
|
||||
@@ -2,6 +2,76 @@
|
||||
/// surface ids; the pixels live here (raster_stub.c) and never cross the component boundary.
|
||||
/// What to draw where (layers, the wallpaper cache, damage) is decided here.
|
||||
|
||||
///| Look for a GPU to draw on (raster_gpu.c): 1 when there is one. Before anything else here.
|
||||
/// `session` is 1 for a login session (--drm).
|
||||
extern "C" fn rs_gpu_init(session : Int) -> Int = "rs_gpu_init"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_dmabuf_device() -> UInt64 = "rs_dmabuf_device"
|
||||
|
||||
///|
|
||||
#borrow(out)
|
||||
extern "C" fn rs_dmabuf_format(i : Int, out : FixedArray[Int]) -> Int = "rs_dmabuf_format"
|
||||
|
||||
///|
|
||||
#borrow(planes)
|
||||
extern "C" fn rs_dmabuf_import(
|
||||
n : Int,
|
||||
planes : FixedArray[Int],
|
||||
modifier : UInt64,
|
||||
w : Int,
|
||||
h : Int,
|
||||
fourcc : UInt,
|
||||
y_invert : Int,
|
||||
) -> UInt = "rs_dmabuf_import"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_dmabuf_release(id : UInt) = "rs_dmabuf_release"
|
||||
|
||||
///|
|
||||
extern "C" fn rs_dmabuf_capture(id : UInt) -> UInt = "rs_dmabuf_capture"
|
||||
|
||||
///| `dmabufs`: the GPU clients should draw on and the buffers it reads.
|
||||
fn dmabuf_support() -> @wt.Val {
|
||||
let formats : Array[@wt.Val] = []
|
||||
let f = FixedArray::make(3, 0)
|
||||
while rs_dmabuf_format(formats.length(), f) != 0 {
|
||||
let modifier = f[1].reinterpret_as_uint().to_uint64() |
|
||||
(f[2].reinterpret_as_uint().to_uint64() << 32)
|
||||
formats.push(
|
||||
Record([
|
||||
("fourcc", U32(f[0].reinterpret_as_uint())),
|
||||
("modifier", U64(modifier)),
|
||||
]),
|
||||
)
|
||||
}
|
||||
Record([("device", U64(rs_dmabuf_device())), ("formats", List(formats))])
|
||||
}
|
||||
|
||||
///|
|
||||
fn import_dmabuf(a : Array[@wt.Val]) -> UInt {
|
||||
let planes = a[0].to_list()
|
||||
if planes.length() < 1 || planes.length() > 4 {
|
||||
return 0U
|
||||
}
|
||||
let flat = FixedArray::make(planes.length() * 3, 0)
|
||||
for i, p in planes {
|
||||
flat[i * 3] = p.field("fd").to_int()
|
||||
flat[i * 3 + 1] = p.field("offset").to_uint().reinterpret_as_int()
|
||||
flat[i * 3 + 2] = p.field("stride").to_uint().reinterpret_as_int()
|
||||
}
|
||||
guard a[1] is U64(modifier) else { return 0U }
|
||||
rs_dmabuf_import(
|
||||
planes.length(),
|
||||
flat,
|
||||
modifier,
|
||||
a[2].to_int(),
|
||||
a[3].to_int(),
|
||||
a[4].to_uint(),
|
||||
if a[5].to_bool() { 1 } else { 0 },
|
||||
)
|
||||
}
|
||||
|
||||
///|
|
||||
extern "C" fn rs_resize(w : Int, h : Int) -> Int = "rs_resize"
|
||||
|
||||
@@ -991,6 +1061,16 @@ fn raster_funcs() -> Array[(String, (Array[@wt.Val]) -> Array[@wt.Val])] {
|
||||
none
|
||||
},
|
||||
),
|
||||
("dmabufs", _ => [dmabuf_support()]),
|
||||
("import-dmabuf", a => [U32(import_dmabuf(a))]),
|
||||
("capture-dmabuf", a => [U32(rs_dmabuf_capture(a[0].to_uint()))]),
|
||||
(
|
||||
"release-dmabuf",
|
||||
a => {
|
||||
rs_dmabuf_release(a[0].to_uint())
|
||||
none
|
||||
},
|
||||
),
|
||||
("load-image", a => [U32(load_image(a[0].to_string()).reinterpret_as_uint())]),
|
||||
(
|
||||
"emoji-font",
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,54 @@
|
||||
/* The GPU half of the host's rasteriser (raster_stub.c calls these when gpu_on). */
|
||||
#ifndef CHANDRA_RASTER_GPU_H
|
||||
#define CHANDRA_RASTER_GPU_H
|
||||
#include <stdint.h>
|
||||
|
||||
/* 1 once gpu_init found a GPU: every drawing call then goes to it. */
|
||||
extern int gpu_on;
|
||||
/* `session`: for a login session on the display itself (the crash guard). */
|
||||
int gpu_init(int session);
|
||||
|
||||
/* The frame: a texture of w x h that keeps its contents between frames (so damage works). */
|
||||
int gpu_resize(int w, int h); /* 0 when the GPU can't */
|
||||
void gpu_clear(void);
|
||||
void gpu_clip(int on, int x, int y, int w, int h);
|
||||
void gpu_flush(void); /* send what was batched to GL */
|
||||
void gpu_submit(void); /* and start the GPU on it */
|
||||
void gpu_read(uint8_t *fb, int stride_px, int x0, int y0, int x1, int y1); /* RGBA rows, at their place in fb */
|
||||
|
||||
void gpu_fill(int x, int y, int w, int h, int r, int g, int b, int a, int radius);
|
||||
void gpu_stroke(int x, int y, int w, int h, int r, int g, int b, int a, int width, int radius);
|
||||
void gpu_circle(int cx, int cy, int radius, int r, int g, int b, int a, int filled);
|
||||
/* A glyph's coverage (w * h bytes). *ax, *ay, *gen: where the atlas holds it, kept by the caller. */
|
||||
void gpu_glyph(const uint8_t *cov, int w, int h, int *ax, int *ay, uint32_t *gen, int x, int y, int r, int g, int b,
|
||||
int a);
|
||||
|
||||
/* Textures. Straight-alpha RGBA pixels, or (gpu_tex_shm) a client's B, G, R, A/X rows. */
|
||||
uint32_t gpu_tex_rgba(const uint8_t *px, int w, int h);
|
||||
uint32_t gpu_tex_shm(const uint8_t *px, int stride, int w, int h, int opaque);
|
||||
void gpu_tex_free(uint32_t tex, int w, int h); /* w, h: its size, to reuse it */
|
||||
/* A copy of texture `src` (sw x sh, premultiplied) scaled to w x h. */
|
||||
uint32_t gpu_tex_scaled(uint32_t src, int sw, int sh, int w, int h);
|
||||
/* Draw `tex` stretched over (x, y, w, h). */
|
||||
void gpu_image(uint32_t tex, int x, int y, int w, int h, int premultiplied);
|
||||
/* Draw `tex` (tw x th, premultiplied) unscaled at (x, y), cut to w x h; the part past the
|
||||
* texture takes its bottom-right pixel. */
|
||||
void gpu_surface(uint32_t tex, int tw, int th, int x, int y, int w, int h);
|
||||
|
||||
void gpu_save_backdrop(void);
|
||||
void gpu_copy_backdrop(void);
|
||||
|
||||
/* Client buffers on the GPU (linux-dmabuf). */
|
||||
uint64_t gpu_device(void); /* the render node's dev_t; 0 without dmabuf support */
|
||||
int gpu_dmabuf_formats(uint32_t *fourcc, uint64_t *modifier, int cap); /* how many there are */
|
||||
uint32_t gpu_dmabuf_import(int n, const int *fd, const uint32_t *offset, const uint32_t *stride, uint64_t modifier,
|
||||
int w, int h, uint32_t fourcc, int y_invert);
|
||||
void gpu_dmabuf_free(uint32_t id);
|
||||
/* A copy of the buffer as it is now, as a texture. *opaque: its format has no alpha. */
|
||||
uint32_t gpu_dmabuf_copy(uint32_t id, int *w, int *h, int *opaque);
|
||||
|
||||
/* Draw the frame into scanout buffer `slot` (0 or 1), a dmabuf of pw x ph XRGB8888: the frame's
|
||||
* x0..y1, at `scale` and turned by `rot` quarters. `fd` is only used the first time (and not
|
||||
* closed). 0 when the GPU can't draw there. */
|
||||
int gpu_scanout(int slot, int fd, uint32_t pitch, int pw, int ph, int scale, int rot, int x0, int y0, int x1, int y1);
|
||||
#endif
|
||||
+200
-29
@@ -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]);
|
||||
|
||||
@@ -0,0 +1,300 @@
|
||||
/* 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;
|
||||
}
|
||||
Reference in new issue
Block a user