RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
scene graph plan, stage 4b: rounded clip, border and shadow in the owned pass
oxydizer4f97ed6706b8
diff --git a/AGENTS.md b/AGENTS.mdindex 64a28ea..834784a 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -62,6 +62,13 @@ the one for the directory you work in: `src/files`, `src/editor`,The temporary `[compositor] scene_renderer = "owned"` requires GLES2 and arestart. The owned pass reads graph geometry; scene nodes remain resource andfeedback transports until stage 6 (`docs/plans/scene-graph-stage3.md`).+* **Shapes in the owned pass** (`scene_pass.h`): a window's rounded clip, ring+ and shadow are `rediwm_scene_pass_add_*_shaped` calls, device pixels, snapped by+ the caller. The programs cost the driver about 430 KiB (a GL program costs+ 130-150 KiB whatever its size), so `enable_shapes` builds them on request, never+ with the renderer; before it a shaped draw fails the pass. Blend NONE is only+ valid inside `rediwm_clip_opaque`. The distance ramp is not area coverage (up+ to 18 of 208 levels at one pixel of a curved edge): that is expected.* **Idle makes no wakeups:** timers only for real deadlines, workers signal aneventfd, `text.applySize` instead of re-sizing faces, title changes repaintonly their span and taskbar chip. Measure with per-thread context switches.diff --git a/TESTING.md b/TESTING.mdindex 0596f51..0a43393 100644--- a/TESTING.md+++ b/TESTING.md@@ -16,7 +16,7 @@ debugging on the host, see AGENTS.md, "Rendering".| `zig build test` | In-process unit tests: rasterizers vs. full rasters, UI, config, IPC parser/serializer fixtures, glass damage, projection damage and its typed graph (paint order, unrounded edges, list lifetime), lifecycle/allocation failures, Files/desktop logic. No compositor or bus. || `zig build test-glass` | Real GLES2 pixels on a headless backend (needs a DRM render node). || `zig build test-pass-spike` | The owned-pass spike of `docs/plans/scene-graph-stage1.md`: rounded clip, border, shadow, external-OES texture, backdrop blur, buffer-age damage and cost on headless GLES2 (needs a render node; `-Doptimize=ReleaseSafe` for cost). |-| `zig build test-scene-pass` | Stage 3's owned GLES2 drawing backend against wlroots: transforms, crops, filters, premultiplied alpha, damage, external-OES (an RGB EGLImage and a linear NV12 dmabuf; needs gbm), GL/EGL state and acquire/release timelines. Needs a compatible render node (`WLR_RENDER_DRM_DEVICE` on multi-GPU machines); also run with `-Doptimize=ReleaseSafe`. The unavailable-acquire test intentionally logs `drmSyncobjTransfer failed`. |+| `zig build test-scene-pass` | Stage 3's owned GLES2 drawing backend against wlroots: transforms, crops, filters, premultiplied alpha, damage, external-OES (an RGB EGLImage and a linear NV12 dmabuf; needs gbm), GL/EGL state and acquire/release timelines. Needs a compatible render node (`WLR_RENDER_DRM_DEVICE` on multi-GPU machines); also run with `-Doptimize=ReleaseSafe`. The unavailable-acquire test intentionally logs `drmSyncobjTransfer failed`. Stage 4b's rounded-shape gates are in the same fixture: clip against a supersampled reference, piece joins, border/body, premultiplied edges, the opaque cover, shadow, and partial repaint across a clip edge; the 128 parity cases also run through the shaped programs. || `zig build test-lock` | Lock/greeter painter, password editing, PAM transitions (PAM is replaced at link time in test binaries only), greeter↔rediwm-dm conversation. || `zig build test-dm` | rediwm-dm relay state machine and framing. || `zig build test-dbus`, `test-polkit`, `test-polkit-ui`, `test-notifications`, `test-settings-portal`, `test-power`, `test-power-profiles`, `test-tray`, `test-app-activation`, `test-rediwm-session`, `test-layout-autosave`, `test-files`, `test-images` | Subsystem suites; the D-Bus ones re-exec under `dbus-run-session`. |diff --git a/docs/plans/scene-graph-stage4.md b/docs/plans/scene-graph-stage4.mdindex 8a31bf2..3c689d9 100644--- a/docs/plans/scene-graph-stage4.md+++ b/docs/plans/scene-graph-stage4.md@@ -1,6 +1,6 @@# Scene graph plan, stage 4: clipping and primitives-Status: **4a done, 4b to 4e not started**, 2026-10-09. Stage 3 of+Status: **4a and 4b done, 4c to 4e not started**, 2026-10-10. Stage 3 of[scene-graph.md](scene-graph.md) (the owned output pass, radius zero) is doneapart from hardware qualification; this stage is why the plan exists: the userrequires clipped client corners, and wlroots 0.20 cannot mask a client. The five@@ -194,14 +194,82 @@ changes (`else if (item->clip)`) halved it from 1.5 us. A typical scene has afraction of those leaves, so this is far below what any frame-level measure canresolve; it is recorded rather than hidden because the gate said no cost change.-**4b. Clip, border and shadow in the pass (owned path, nothing produces a clip-yet).** The spike's gates 1 to 8, converted to production code in-`test-scene-pass`: rounded clip against an 8x8 supersampled reference at scales 1,-1.25, 1.5, 2 and zoom 1 to .25 (spike: maximum error 12 levels on arcs, zero-fringe); piece joins; border and body equal one shape; premultiplied edges;-external-OES equals 2D; shadow against the `erfc` profile; and buffer-age damage-at a clip edge. Gate: those pass and unclipped draws stay at maximum difference 0-against wlroots (the existing 128 cases).+**4b. Clip, border and shadow in the pass. Done.** `scene_pass.h` gains+`rediwm_pass_shape` (a snapped device box, four radii, a border width and colour),+`rediwm_pass_shadow`, and three calls on a pass from `rediwm_scene_pass_begin`:+`rediwm_scene_pass_add_rect_shaped`, `_add_texture_shaped` and `_add_shadow`. They+take the same options as the wlroots calls, plus the shape; `wlr_render_pass` has+no way to carry one, hence the extension calls. The pass stays free of geometry+policy: the caller snaps the box (`rediwm_edges_snap`) and scales radii and border,+and nothing is rounded in the pass. Unclipped draws do not change at all: they keep+the stage 3 programs, and the shaped ones are separate.++The shader is the spike's, with three changes. The body, the ring and the+shadow are one fragment program with a `mode` uniform (texture, solid, shadow;+every fragment of a draw reads the same mode, so the branch costs nothing), built+twice, for `sampler2D` and for external. The shape's position relative to its+corner is computed in the vertex stage, so the distance function works on+window-sized numbers. And a radius is clamped to what fits, so a bad radius never+produces junk. The backdrop mode of the spike is not ported: glass keeps its own+blur until the stage that replaces it.++**Memory decided the program count.** Measured as anonymous RSS on the Intel node:+a GL program costs the driver 130-150 KiB **whatever its size** (the same for a+one-line solid fill and the full shaped shader). The first version built four+shaped programs with the renderer and added 600 KiB for nothing, on top of the+0.44 MiB stage 3 is already carrying. Now there are two, and they are built by+`rediwm_scene_renderer_enable_shapes` (once, outside a pass; idempotent), which+costs **about 430 KiB when called and nothing otherwise**. 4c calls it only when+the owned presenter will draw analytic frames. Until then a shaped draw fails the+pass at submit (tested), so a missed call cannot draw square corners silently.+If the build fails, plain drawing is unaffected and the caller learns it from the+return value.++Gates, all in `test-scene-pass` and run in Debug and ReleaseSafe:++* **Unclipped draws unchanged:** the 128 parity cases against wlroots stay at+ maximum difference **0**, and the same 128 run through the **shaped** programs+ with a shape that covers the whole buffer are also at 0 (so transform, crop,+ filter, alpha, XRGB, damage clip, no-VAO fallback and the external sampler are all+ exact there), plus external-OES shaped equal to wlroots, and a rounded shape on+ external equal to the same shape on the 2D texture.+* **Clip against an 8x8 supersampled reference** at scales 1, 1.25, 1.5 and 2 and+ zoom 1 to .25, windows built with `rediwm_clip_resolve` and `rediwm_edges_snap`:+ zero fringe, maximum error **18 levels of 208**. The spike's 12 did not include+ scale 1.25; the worst pixel is (2,2) of an 8.75 px radius, where area coverage is+ .33 and the one-pixel distance ramp gives .41. That is the ramp itself, not a+ defect: on a curved 45-degree edge it cannot equal area coverage, and `glass.c`+ already uses it. The bound is 20.+* **Piece joins:** titlebar, client and footer meet with no seam at every scale+ and zoom, and the snapped pieces tile the shape exactly.+* **Border and body are one shape:** three pieces with a ring in the body's colour+ equal one plain body (difference 0), and the ring is its own colour on the outer+ row and the left edge.+* **Premultiplied edges:** half-transparent, XRGB with a junk alpha byte and a+ half-opacity texture, inside and on every edge pixel against area coverage.+* **The opaque cover of 4a, in real pixels:** drawing `rediwm_clip_opaque`'s+ rectangles with blending off and then the window over them changes nothing, at+ every scale and zoom. This is the claim 4c's blend decisions rest on.+* **Shadow** against the `erfc` profile (difference 0 levels), monotonic, kept out+ of the window when asked, and showing through when not.+* **Damage across a clip edge:** a full frame, then a second one painted only+ inside damage that cuts through two corner arcs, the ring and the shadow, equals+ the full repaint exactly (all four channels, 8 scale/zoom cases), with a control+ that the two frames really differ on both sides of the damage edge.++The gates can fail: a coverage ramp offset, a halved ring, a shadow that ignores+`hide`, a shaped texture that ignores the opaque flag, and a ring colour that is+dropped each fail a gate when injected into the shader.++Outside the fixture: Debug and ReleaseSafe builds and `zig build test` pass; the+owned presenter matches the legacy one on all 16 goldens at tolerance 0; and+`explicit_sync`, `direct_scanout`, `screenshot_regressions`, `appearance_chrome`,+`output_scale`, `desktop_damage` and `panel_damage` pass on the owned path.++Left for later, from what the numbers allow: the 128-case result means the shaped+program with no shape is exactly the plain one, so 4e could replace the three+plain programs by the two shaped ones and give back about 410 KiB, if the cost of+the solid fill (every rectangle goes through it) is acceptable. It is not done here.**4c. Analytic frames, owned path only.** When the owned presenter is selected,`Toplevel` sets the group clip, the footer is `frame_border`, the titlebar raster@@ -248,7 +316,7 @@ in `AGENTS.md`. The audit of the ~22 footer sites happens here for the legacy fl| Resize and move frames | Lower: the footer raster and the titlebar arcs go; titlebar re-rasters only on width/title/state. || Output build CPU | Within the stage 3 tolerance of the stage 3 figures. || GPU | A distance-field multiply per fragment of clipped windows only; check `gpu_elapsed`. |-| RAM | Lower after 4e: footer buffers, and with 4d the shadow patch cache, are freed. |+| RAM | Lower after 4e: footer buffers, and with 4d the shadow patch cache, are freed. 4c adds the shaped programs (about 430 KiB, `enable_shapes`) on the owned path only; judge the net of the two. |The live legacy/owned A/B (`tests/scene_ab.py`) stops being meaningful once theframes differ (4c), so record an owned reference with it, and the `clean` baseline,diff --git a/src/scene_pass.c b/src/scene_pass.cindex 365f930..3a5aa7a 100644--- a/src/scene_pass.c+++ b/src/scene_pass.c@@ -7,6 +7,7 @@#include <GLES2/gl2.h>#include <GLES2/gl2ext.h>#include <assert.h>+#include <math.h>#include <stdlib.h>#include <unistd.h>#include <wlr/render/drm_syncobj.h>@@ -18,7 +19,10 @@struct program {GLuint id;+ unsigned bit; // which bit of pass.targets_set tracks this program's target sizeGLint target, dst, color, alpha, opaque, sampler, coords;+ // Shaped programs only (-1 elsewhere): the shape, and the shadow's own terms.+ GLint mode, origin, size, radii, border, bcolor, offset, sigma, hide;};struct attrib_state {GLint enabled, size, type, normalized, stride, buffer;@@ -37,7 +41,11 @@ struct gl_state {struct rediwm_scene_renderer {struct wlr_renderer *renderer;struct program solid, texture, external;- bool active, has_external, has_sync, has_vao;+ // Rounded-rectangle shapes (stage 4b), built by enable_shapes: one program for+ // a 2D texture, a solid colour and the shape's shadow (they differ in a mode+ // uniform, which every fragment of a draw reads the same), one for external.+ struct program shaped, shaped_external;+ bool active, has_external, has_sync, has_vao, has_shapes, shapes_tried;GLuint vertex_array, vertices;PFNGLGENVERTEXARRAYSOESPROC gen_vertex_arrays;PFNGLBINDVERTEXARRAYOESPROC bind_vertex_array;@@ -141,6 +149,53 @@ static const char fragment[] ="precision mediump float; varying vec2 uv; uniform float alpha, opaque;""void main(){vec4 c=texture2D(tex,uv); if(opaque>0.5)c.a=1.; gl_FragColor=c*alpha;}";+// Shaped draws (stage 4b): the same quad and texture coordinates, plus the+// position relative to the shape's corner, evaluated in the vertex stage so the+// fragment's distance function works on window-sized numbers, not on output ones.+static const char shaped_vertex[] =+ "attribute vec2 position; uniform vec2 target, origin; uniform vec4 dst, coords[2];"+ "varying vec2 uv; varying vec2 local;"+ "void main(){ vec4 row=position.y<.5?coords[0]:coords[1];"+ "uv=position.x<.5?row.xy:row.zw; vec2 p=dst.xy+position*dst.zw;"+ "local=p-origin; gl_Position=vec4(p/target*2.-1.,0.,1.); }";+// One distance function for every piece of a window, so titlebar, client, border+// and shadow agree on the arc to the last bit. The radius is picked by quadrant+// and capped to what fits, as the compositor caps it before scaling.+#define SHAPED_HEAD \+ "#ifdef GL_FRAGMENT_PRECISION_HIGH\nprecision highp float;\n#else\nprecision mediump float;\n#endif\n" \+ "varying vec2 uv; varying vec2 local; uniform vec2 size; uniform vec4 radii, bcolor, color;" \+ "uniform float border;" \+ "float sdf(vec2 p, vec4 r){ vec2 q=p-size*.5;" \+ "float rad=q.x<0.?(q.y<0.?r.x:r.w):(q.y<0.?r.y:r.z);" \+ "rad=clamp(rad,0.,min(size.x,size.y)*.5);" \+ "vec2 d=abs(q)-(size*.5-vec2(rad));" \+ "return length(max(d,0.))+min(max(d.x,d.y),0.)-rad; }" \+ "float cov(float d){ return clamp(.5-d,0.,1.); }"+// A border is part of the same pass as its body: the body keeps the inside of the+// inset shape and the ring the rest, both evaluated at this fragment, so their+// coverages add up to the shape's. As two draws they would multiply, and a seam+// would show where each is partly covered.+//+// `mode` 0 draws the texture, 1 the solid colour, 2 the shape's shadow: a+// Gaussian-blurred copy of the shape, offset, approximated through its distance+// field (exact on straight edges, close at corners), kept out of the shape when+// `hide` is set, which matters under translucent windows. One program for the+// three because a program costs the driver 130-150 KiB however small it is.+static const char shaped_fragment[] = SHAPED_HEAD+ "uniform float alpha, opaque, mode, sigma, hide; uniform vec2 offset;"+ // Abramowitz-Stegun 7.1.26, |error| < 1.5e-7.+ "float erf1(float x){ float s=x<0.?-1.:1.; x=abs(x); float t=1./(1.+.3275911*x);"+ "float y=1.-(((((1.061405429*t-1.453152027)*t)+1.421413741)*t-.284496736)*t+.254829592)*t*exp(-x*x);"+ "return s*y; }"+ "void main(){"+ "if(mode>1.5){ float s=.5*(1.-erf1(sdf(local-offset,radii)/(sigma*1.41421356)));"+ "if(hide>.5)s*=1.-cov(sdf(local,radii)); gl_FragColor=color*s; return; }"+ "vec4 c=color; if(mode<.5){ c=texture2D(tex,uv); if(opaque>.5)c.a=1.; c*=alpha; }"+ "float d=sdf(local,radii);"+ "float inner=border>0.?cov(d+border):cov(d);"+ "float ring=border>0.?cov(d)-cov(d+border):0.;"+ "gl_FragColor=c*inner+bcolor*ring; }";+static GLuint shader(GLenum type, const char *prefix, const char *source) {GLuint id = glCreateShader(type);if (!id) return 0;@@ -158,8 +213,10 @@ static GLuint shader(GLenum type, const char *prefix, const char *source) {}return id;}-static bool program_init(struct program *p, const char *prefix, const char *source) {- GLuint vs = shader(GL_VERTEX_SHADER, "", vertex);+static bool program_init(struct program *p, unsigned bit, const char *vertex_source,+ const char *prefix, const char *source) {+ p->bit = bit;+ GLuint vs = shader(GL_VERTEX_SHADER, "", vertex_source);GLuint fs = shader(GL_FRAGMENT_SHADER, prefix, source);if (vs && fs) {p->id = glCreateProgram();@@ -189,8 +246,19 @@ static bool program_init(struct program *p, const char *prefix, const char *sourp->opaque = glGetUniformLocation(p->id, "opaque");p->sampler = glGetUniformLocation(p->id, "tex");p->coords = glGetUniformLocation(p->id, "coords[0]");+ p->mode = glGetUniformLocation(p->id, "mode");+ p->origin = glGetUniformLocation(p->id, "origin");+ p->size = glGetUniformLocation(p->id, "size");+ p->radii = glGetUniformLocation(p->id, "radii");+ p->border = glGetUniformLocation(p->id, "border");+ p->bcolor = glGetUniformLocation(p->id, "bcolor");+ p->offset = glGetUniformLocation(p->id, "offset");+ p->sigma = glGetUniformLocation(p->id, "sigma");+ p->hide = glGetUniformLocation(p->id, "hide");return true;}+static const char ext_prefix[] =+ "#extension GL_OES_EGL_image_external : require\nuniform samplerExternalOES tex;\n";static const GLfloat positions[] = {0, 0, 1, 0, 0, 1, 1, 1};struct rediwm_scene_renderer *rediwm_scene_renderer_create(struct wlr_renderer *renderer) {if (!wlr_renderer_is_gles2(renderer)) return NULL;@@ -211,10 +279,9 @@ struct rediwm_scene_renderer *rediwm_scene_renderer_create(struct wlr_renderer *r->destroy_sync && r->wait_sync && r->dup_fence;struct gl_state state;if (!enter(r, &state)) { free(r); return NULL; }- bool ok = program_init(&r->solid, "", solid) &&- program_init(&r->texture, "uniform sampler2D tex;", fragment) &&- (!r->has_external || program_init(&r->external,- "#extension GL_OES_EGL_image_external : require\nuniform samplerExternalOES tex;", fragment));+ bool ok = program_init(&r->solid, 1, vertex, "", solid) &&+ program_init(&r->texture, 2, vertex, "uniform sampler2D tex;", fragment) &&+ (!r->has_external || program_init(&r->external, 4, vertex, ext_prefix, fragment));if (ok && r->has_vao) {r->gen_vertex_arrays(1, &r->vertex_array);r->bind_vertex_array(r->vertex_array);@@ -246,6 +313,8 @@ void rediwm_scene_renderer_destroy(struct rediwm_scene_renderer *r) {glDeleteProgram(r->solid.id);glDeleteProgram(r->texture.id);glDeleteProgram(r->external.id);+ glDeleteProgram(r->shaped.id);+ glDeleteProgram(r->shaped_external.id);leave(r, &state);}free(r);@@ -257,10 +326,9 @@ static void setup(struct pass *p, struct program *program, struct wlr_box box,glUseProgram(program->id);p->program = program->id;}- unsigned bit = program == &p->renderer->solid ? 1 : program == &p->renderer->texture ? 2 : 4;- if (!(p->targets_set & bit)) {+ if (!(p->targets_set & program->bit)) {glUniform2f(program->target, p->buffer->width, p->buffer->height);- p->targets_set |= bit;+ p->targets_set |= program->bit;}glUniform4f(program->dst, box.x, box.y, box.width, box.height);if (p->blend_mode != (int)blend) {@@ -282,6 +350,22 @@ static void draw(const pixman_region32_t *clip) {glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);}}+// The shape's uniforms. A degenerate or non-finite shape is the caller's bug;+// it fails the pass at submit like any other bad draw.+static bool shape_usable(const struct rediwm_pass_shape *s) {+ if (!isfinite(s->border) || s->border < 0) return false;+ for (int i = 0; i < 4; i++) if (!isfinite(s->radii[i]) || s->radii[i] < 0) return false;+ for (int i = 0; i < 4; i++) if (!isfinite(s->border_color[i])) return false;+ return true;+}+static void set_shape(const struct program *g, const struct rediwm_pass_shape *s) {+ glUniform2f(g->origin, s->box.x, s->box.y);+ glUniform2f(g->size, s->box.width, s->box.height);+ glUniform4f(g->radii, s->radii[0], s->radii[1], s->radii[2], s->radii[3]);+ glUniform1f(g->border, s->border);+ glUniform4f(g->bcolor, s->border_color[0], s->border_color[1], s->border_color[2],+ s->border_color[3]);+}static void add_rect(struct wlr_render_pass *base, const struct wlr_render_rect_options *o) {struct pass *p = wl_container_of(base, p, base);if (p->failed || (o->clip && !pixman_region32_not_empty(o->clip))) return;@@ -291,6 +375,19 @@ static void add_rect(struct wlr_render_pass *base, const struct wlr_render_rect_glUniform4f(p->renderer->solid.color, o->color.r, o->color.g, o->color.b, o->color.a);draw(o->clip);}+static void rect_shaped(struct pass *p, const struct wlr_render_rect_options *o,+ const struct rediwm_pass_shape *shape) {+ struct program *g = &p->renderer->shaped;+ if (p->failed || (o->clip && !pixman_region32_not_empty(o->clip))) return;+ if (!g->id || !shape_usable(shape)) { p->failed = true; return; }+ struct wlr_box box;+ wlr_render_rect_options_get_box(o, p->buffer, &box);+ setup(p, g, box, o->blend_mode);+ glUniform4f(g->color, o->color.r, o->color.g, o->color.b, o->color.a);+ glUniform1f(g->mode, 1.f);+ set_shape(g, shape);+ draw(o->clip);+}static bool wait_point(struct pass *p, struct wlr_drm_syncobj_timeline *timeline, uint64_t point) {if (!timeline) return true;struct rediwm_scene_renderer *r = p->renderer;@@ -306,10 +403,11 @@ static bool wait_point(struct pass *p, struct wlr_drm_syncobj_timeline *timelinereturn ok;}static void texture_draw(struct pass *p, const struct wlr_render_texture_options *o,- struct wlr_gles2_texture_attribs a) {- struct program *program = a.target == GL_TEXTURE_2D ? &p->renderer->texture :- a.target == GL_TEXTURE_EXTERNAL_OES ? &p->renderer->external : NULL;- if (!program || !program->id) { p->failed = true; return; }+ struct wlr_gles2_texture_attribs a, const struct rediwm_pass_shape *shape) {+ struct rediwm_scene_renderer *r = p->renderer;+ struct program *program = a.target == GL_TEXTURE_2D ? (shape ? &r->shaped : &r->texture) :+ a.target == GL_TEXTURE_EXTERNAL_OES ? (shape ? &r->shaped_external : &r->external) : NULL;+ if (!program || !program->id || (shape && !shape_usable(shape))) { p->failed = true; return; }struct wlr_box dst;struct wlr_fbox src;wlr_render_texture_options_get_dst_box(o, &dst);@@ -342,9 +440,14 @@ static void texture_draw(struct pass *p, const struct wlr_render_texture_optionsuv[2 * i + 1] = (src.y + v * src.height) / o->texture->height;}glUniform4fv(program->coords, 2, uv);+ if (shape) {+ glUniform1f(program->mode, 0.f);+ set_shape(program, shape);+ }draw(o->clip);}-static void add_texture(struct wlr_render_pass *base, const struct wlr_render_texture_options *o) {+static void texture_add(struct wlr_render_pass *base, const struct wlr_render_texture_options *o,+ const struct rediwm_pass_shape *shape) {struct pass *p = wl_container_of(base, p, base);if (p->failed || (o->clip && !pixman_region32_not_empty(o->clip))) return;if (!wlr_texture_is_gles2(o->texture) || o->texture->renderer != p->renderer->renderer ||@@ -354,7 +457,10 @@ static void add_texture(struct wlr_render_pass *base, const struct wlr_render_te}struct wlr_gles2_texture_attribs a;wlr_gles2_texture_get_attribs(o->texture, &a);- texture_draw(p, o, a);+ texture_draw(p, o, a, shape);+}+static void add_texture(struct wlr_render_pass *base, const struct wlr_render_texture_options *o) {+ texture_add(base, o, NULL);}static bool submit(struct wlr_render_pass *base) {struct pass *p = wl_container_of(base, p, base);@@ -421,3 +527,67 @@ struct wlr_render_pass *rediwm_scene_pass_begin(struct rediwm_scene_renderer *r,wlr_render_pass_init(&p->base, &impl);return &p->base;}++bool rediwm_scene_renderer_enable_shapes(struct rediwm_scene_renderer *r) {+ assert(!r->active);+ if (r->shapes_tried) return r->has_shapes;+ r->shapes_tried = true;+ struct gl_state state;+ if (!enter(r, &state)) return false;+ r->has_shapes = program_init(&r->shaped, 8, shaped_vertex, "uniform sampler2D tex;\n",+ shaped_fragment) &&+ (!r->has_external || program_init(&r->shaped_external, 16, shaped_vertex, ext_prefix,+ shaped_fragment));+ if (!r->has_shapes) {+ wlr_log(WLR_ERROR, "scene pass: shaped programs failed to build, windows stay square");+ glDeleteProgram(r->shaped.id);+ glDeleteProgram(r->shaped_external.id);+ r->shaped.id = r->shaped_external.id = 0;+ }+ leave(r, &state);+ return r->has_shapes;+}+// Only a pass from rediwm_scene_pass_begin carries these: handing in a wlroots+// pass is a programming error, not something to degrade around.+static struct pass *owned_pass(struct wlr_render_pass *base) {+ assert(base->impl == &impl);+ struct pass *p = wl_container_of(base, p, base);+ return p;+}+void rediwm_scene_pass_add_rect_shaped(struct wlr_render_pass *base,+ const struct wlr_render_rect_options *o, const struct rediwm_pass_shape *shape) {+ rect_shaped(owned_pass(base), o, shape);+}+void rediwm_scene_pass_add_texture_shaped(struct wlr_render_pass *base,+ const struct wlr_render_texture_options *o, const struct rediwm_pass_shape *shape) {+ owned_pass(base);+ texture_add(base, o, shape);+}+void rediwm_scene_pass_add_shadow(struct wlr_render_pass *base,+ const struct rediwm_pass_shape *shape, const struct rediwm_pass_shadow *shadow) {+ struct pass *p = owned_pass(base);+ struct program *g = &p->renderer->shaped;+ if (p->failed || (shadow->clip && !pixman_region32_not_empty(shadow->clip))) return;+ if (!g->id || !shape_usable(shape) || !isfinite(shadow->offset_x) ||+ !isfinite(shadow->offset_y) || !isfinite(shadow->sigma) || shadow->sigma > 1024 ||+ fabsf(shadow->offset_x) > 65536 || fabsf(shadow->offset_y) > 65536) {+ p->failed = true;+ return;+ }+ // A quarter pixel is a hard edge; zero would divide by it.+ const float sigma = shadow->sigma < .25f ? .25f : shadow->sigma;+ // The quad reaches three sigma past the shape, and as far again as the offset.+ const int pad = (int)ceilf(3 * sigma) + (int)ceilf(fmaxf(fabsf(shadow->offset_x), fabsf(shadow->offset_y)));+ const struct wlr_box box = {shape->box.x - pad, shape->box.y - pad,+ shape->box.width + 2 * pad, shape->box.height + 2 * pad};+ setup(p, g, box, WLR_RENDER_BLEND_MODE_PREMULTIPLIED);+ glUniform4f(g->color, shadow->color[0], shadow->color[1], shadow->color[2], shadow->color[3]);+ struct rediwm_pass_shape bare = *shape; // the shadow has no border of its own+ bare.border = 0;+ set_shape(g, &bare);+ glUniform1f(g->mode, 2.f);+ glUniform2f(g->offset, shadow->offset_x, shadow->offset_y);+ glUniform1f(g->sigma, sigma);+ glUniform1f(g->hide, shadow->hide_inside ? 1.f : 0.f);+ draw(shadow->clip);+}diff --git a/src/scene_pass.h b/src/scene_pass.hindex 70fd8dd..ea52442 100644--- a/src/scene_pass.h+++ b/src/scene_pass.h@@ -1,8 +1,10 @@-// Radius-zero GLES2 drawing for the owned scene output pass. The caller owns-// output policy (damage, culling, feedback, swapchain and commit). Using the-// wlroots pass interface also lets software cursors use the same pass.+// GLES2 drawing for the owned scene output pass. The caller owns output policy+// (damage, culling, feedback, swapchain and commit). Using the wlroots pass+// interface also lets software cursors use the same pass.#pragma once+#include <stdbool.h>#include <wlr/render/pass.h>+#include <wlr/util/box.h>struct rediwm_scene_renderer;// The renderer must be GLES2 and outlive this object and all its passes.@@ -14,3 +16,55 @@ void rediwm_scene_renderer_destroy(struct rediwm_scene_renderer *);// are supported; non-NULL requests fail instead of silently ignoring them.struct wlr_render_pass *rediwm_scene_pass_begin(struct rediwm_scene_renderer *,struct wlr_buffer *, const struct wlr_buffer_pass_options *);++// Stage 4b of docs/plans/scene-graph.md: rounded-rectangle shapes. One shape+// clips every piece of a window (titlebar, client, border, shadow), and each+// piece is a draw that names the shape, so they agree on the arc to the last bit+// and there is no per-piece corner approximation. Plain add_rect/add_texture+// calls are unchanged and never touch these programs.+//+// All numbers are device pixels of the destination buffer, in the same space as+// `box` of the draws (y down, row 0 first). The caller snaps the box (see+// rediwm_edges_snap in scene_graph.h) and scales the radii and border; nothing+// is rounded here, so a fractional radius or border is drawn as given.+struct rediwm_pass_shape {+ struct wlr_box box;+ // Top-left, top-right, bottom-right, bottom-left. A radius larger than half+ // the short side is capped to it.+ float radii[4];+ // Width of the ring just inside the edge, drawn in `border_color` by the same+ // fragment that draws the body, so the two add up to the shape's coverage+ // exactly. 0 draws no ring.+ float border;+ float border_color[4]; // premultiplied+};+struct rediwm_pass_shadow {+ float offset_x, offset_y; // how far the shadow is shifted from the shape+ float sigma; // Gaussian standard deviation; below 0.25 it is 0.25+ float color[4]; // premultiplied, at full strength under the shape's edge+ // Keep the shadow out of the shape, so a translucent window does not show it.+ bool hide_inside;+ const pixman_region32_t *clip; // damage, as for any draw; NULL for everything+};++// Builds the shaped programs, which cost the driver about 430 KiB and are+// therefore not made with the renderer. Call it once, outside a pass, before the+// first shaped draw; later calls return the first result. False when they did not+// build: shaped draws then fail the pass at submit, plain drawing is unaffected.+bool rediwm_scene_renderer_enable_shapes(struct rediwm_scene_renderer *);++// These take a pass from rediwm_scene_pass_begin only (a wlroots pass is a+// programming error). `box` of the options is the piece to draw; the shape is+// what clips it, so a piece is usually a part of the shape's box.+//+// Edges are anti-aliased by multiplying the piece's premultiplied colour by the+// shape's pixel coverage, so the blend mode must be PREMULTIPLIED wherever the+// shape's edge crosses the piece. NONE is only right inside the part of the shape+// that is fully covered (see rediwm_clip_opaque in scene_graph.h).+void rediwm_scene_pass_add_rect_shaped(struct wlr_render_pass *,+ const struct wlr_render_rect_options *, const struct rediwm_pass_shape *);+void rediwm_scene_pass_add_texture_shaped(struct wlr_render_pass *,+ const struct wlr_render_texture_options *, const struct rediwm_pass_shape *);+// The shape's shadow. The quad reaches three sigma and the offset past the box.+void rediwm_scene_pass_add_shadow(struct wlr_render_pass *, const struct rediwm_pass_shape *,+ const struct rediwm_pass_shadow *);diff --git a/src/scene_pass_test.c b/src/scene_pass_test.cindex 633f9a6..3947cc3 100644--- a/src/scene_pass_test.c+++ b/src/scene_pass_test.c@@ -1,6 +1,9 @@// Compare the actual owned drawing backend with wlroots, independently of// scene traversal and output policy. A noninstalled, real-GPU fixture.#include "scene_pass.c"+#include "scene_graph.h"+#include <math.h>+#include <stdarg.h>#include <stdio.h>#include <string.h>#include <drm_fourcc.h>@@ -49,19 +52,26 @@ static void clear(struct wlr_render_pass *pass) {.blend_mode = WLR_RENDER_BLEND_MODE_NONE,});}+// With a shape the owned pass draws through its shaped programs; wlroots' pass,+// which has no such thing, is always given NULL.static void paint(struct wlr_render_pass *pass, struct wlr_texture *texture,- int transform, int filter, int crop, float alpha, const pixman_region32_t *clip) {- wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){- .box = {8, -7, 49, 63}, .color = {.3, .06, .17, .5}, .clip = clip,- });- wlr_render_pass_add_texture(pass, &(struct wlr_render_texture_options){+ int transform, int filter, int crop, float alpha, const pixman_region32_t *clip,+ const struct rediwm_pass_shape *shape) {+ struct wlr_render_rect_options rects[2] = {+ {.box = {8, -7, 49, 63}, .color = {.3, .06, .17, .5}, .clip = clip},+ {.box = {41, 29, 37, 53}, .color = {.1, .2, .05, .4}, .clip = clip},+ };+ struct wlr_render_texture_options t = {.texture = texture, .dst_box = {-3, 11, 112, 77},.src_box = crop ? (struct wlr_fbox){2.25, 3.5, 16.5, 11.25} : (struct wlr_fbox){0},.transform = transform, .filter_mode = filter, .alpha = &alpha, .clip = clip,- });- wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){- .box = {41, 29, 37, 53}, .color = {.1, .2, .05, .4}, .clip = clip,- });+ };+ if (shape) rediwm_scene_pass_add_rect_shaped(pass, &rects[0], shape);+ else wlr_render_pass_add_rect(pass, &rects[0]);+ if (shape) rediwm_scene_pass_add_texture_shaped(pass, &t, shape);+ else wlr_render_pass_add_texture(pass, &t);+ if (shape) rediwm_scene_pass_add_rect_shaped(pass, &rects[1], shape);+ else wlr_render_pass_add_rect(pass, &rects[1]);}static struct wlr_texture *texture_create(struct wlr_renderer *renderer, bool opaque) {uint32_t pixels[TW * TH];@@ -76,8 +86,12 @@ static struct wlr_texture *texture_create(struct wlr_renderer *renderer, bool opassert(t);return t;}+// `shaped` runs every case through the shaped programs, with a shape that covers+// the whole buffer and has no corners: it must still equal wlroots exactly.static void parity(struct rediwm_scene_renderer *r, struct wlr_buffer *actual,- struct wlr_buffer *expected) {+ struct wlr_buffer *expected, bool shaped) {+ const struct rediwm_pass_shape cover = {.box = {-50, -50, WIDTH + 100, HEIGHT + 100}};+ const struct rediwm_pass_shape *shape = shaped ? &cover : NULL;pixman_region32_t clip;pixman_region32_init_rect(&clip, -10, 8, 65, 31);pixman_region32_union_rect(&clip, &clip, 71, 43, 81, 80);@@ -90,17 +104,17 @@ static void parity(struct rediwm_scene_renderer *r, struct wlr_buffer *actual,struct wlr_render_pass *pass = wlr_renderer_begin_buffer_pass(r->renderer, expected, NULL);assert(pass);clear(pass);- paint(pass, t, tr, filter, crop, alpha, partial ? &clip : NULL);+ paint(pass, t, tr, filter, crop, alpha, partial ? &clip : NULL, NULL);assert(wlr_render_pass_submit(pass));pass = rediwm_scene_pass_begin(r, actual, NULL);assert(pass);clear(pass);- paint(pass, t, tr, filter, crop, alpha, partial ? &clip : NULL);+ paint(pass, t, tr, filter, crop, alpha, partial ? &clip : NULL, shape);assert(wlr_render_pass_submit(pass));int d = compare(r, actual, expected);if (d > 1) {- fprintf(stderr, "parity: opaque=%d transform=%d filter=%d crop=%d partial=%d diff=%d\n",- opaque, tr, filter, crop, partial, d);+ fprintf(stderr, "parity: shaped=%d opaque=%d transform=%d filter=%d crop=%d partial=%d diff=%d\n",+ shaped, opaque, tr, filter, crop, partial, d);abort();}if (d > worst) worst = d;@@ -109,7 +123,21 @@ static void parity(struct rediwm_scene_renderer *r, struct wlr_buffer *actual,wlr_texture_destroy(t);}pixman_region32_fini(&clip);- printf("PASS: %d texture/transform/crop/filter/alpha/damage cases (max difference %d)\n", cases, worst);+ printf("PASS: %d texture/transform/crop/filter/alpha/damage cases%s (max difference %d)\n", cases,+ shaped ? " through the shaped programs" : "", worst);+}+// Before enable_shapes there are no shaped programs: a shaped draw must fail the+// pass at submit, not draw something else, and the next pass must work.+static void shapes_fail_closed(struct rediwm_scene_renderer *r, struct wlr_buffer *buffer) {+ const struct rediwm_pass_shape shape = {.box = {10, 10, 50, 50}, .radii = {5, 5, 5, 5}};+ struct wlr_render_pass *pass = rediwm_scene_pass_begin(r, buffer, NULL);+ assert(pass);+ rediwm_scene_pass_add_rect_shaped(pass, &(struct wlr_render_rect_options){+ .box = {10, 10, 50, 50}, .color = {1, 0, 0, 1}}, &shape);+ assert(!wlr_render_pass_submit(pass));+ pass = rediwm_scene_pass_begin(r, buffer, NULL);+ assert(pass && wlr_render_pass_submit(pass));+ puts("PASS: a shaped draw before enable_shapes fails the pass and the next pass works");}static void damage(struct rediwm_scene_renderer *r, struct wlr_buffer *actual,struct wlr_buffer *expected) {@@ -125,7 +153,7 @@ static void damage(struct rediwm_scene_renderer *r, struct wlr_buffer *actual,struct wlr_texture *t = texture_create(r->renderer, false);pass = rediwm_scene_pass_begin(r, actual, NULL);assert(pass);- paint(pass, t, 3, 0, 1, .73, &clip);+ paint(pass, t, 3, 0, 1, .73, &clip, NULL);assert(wlr_render_pass_submit(pass));read_pixels(r, actual, after);int changed = 0;@@ -139,7 +167,7 @@ static void damage(struct rediwm_scene_renderer *r, struct wlr_buffer *actual,pixman_region32_clear(&clip);pass = rediwm_scene_pass_begin(r, actual, NULL);assert(pass);- paint(pass, t, 1, 1, 0, 1, &clip);+ paint(pass, t, 1, 1, 0, 1, &clip, NULL);assert(wlr_render_pass_submit(pass));read_pixels(r, actual, before);assert(memcmp(before, after, sizeof(before)) == 0);@@ -196,7 +224,7 @@ static void state_restoration(struct rediwm_scene_renderer *r, struct wlr_bufferassert(pass);assert(!rediwm_scene_pass_begin(r, buffer, NULL)); // no nested owned passesclear(pass);- paint(pass, t, 1, 0, 1, .5, NULL);+ paint(pass, t, 1, 0, 1, .5, NULL, NULL);assert(wlr_render_pass_submit(pass));assert(enter(r, &after));leave(r, &after);@@ -230,7 +258,7 @@ static void external_texture(struct rediwm_scene_renderer *r, struct wlr_allocatstruct wlr_render_pass *base = wlr_renderer_begin_buffer_pass(r->renderer, source, NULL);assert(base);clear(base);- paint(base, pattern, 0, 1, 0, 1, NULL);+ paint(base, pattern, 0, 1, 0, 1, NULL, NULL);assert(wlr_render_pass_submit(base));wlr_texture_destroy(pattern);struct wlr_texture *reference = wlr_texture_from_buffer(r->renderer, source);@@ -281,9 +309,39 @@ static void external_texture(struct rediwm_scene_renderer *r, struct wlr_allocatstruct pass *p = wl_container_of(base, p, base);// Force a real external EGLImage through the same draw routine; this// driver's normal import of an RGB dmabuf chooses a 2D target.- texture_draw(p, &o, a);+ texture_draw(p, &o, a, NULL);assert(wlr_render_pass_submit(base));assert(compare(r, actual, expected) <= 1);+ // The shaped external program with a cover-everything shape is the same draw.+ const struct rediwm_pass_shape cover = {.box = {-50, -50, WIDTH + 100, HEIGHT + 100}};+ base = rediwm_scene_pass_begin(r, actual, NULL);+ assert(base);+ clear(base);+ p = wl_container_of(base, p, base);+ texture_draw(p, &o, a, &cover);+ assert(wlr_render_pass_submit(base));+ assert(compare(r, actual, expected) <= 1);+ // And through a rounded, bordered shape the external texture equals the 2D+ // one of the same pixels: the clip does not depend on the sampler.+ const struct rediwm_pass_shape round = {.box = {12, 14, 100, 70},+ .radii = {18, 9, 25.5f, 3}, .border = 2, .border_color = {.5f, .1f, .1f, .5f}};+ base = rediwm_scene_pass_begin(r, expected, NULL);+ assert(base);+ clear(base);+ rediwm_scene_pass_add_texture_shaped(base, &o, &round);+ assert(wlr_render_pass_submit(base));+ base = rediwm_scene_pass_begin(r, actual, NULL);+ assert(base);+ clear(base);+ p = wl_container_of(base, p, base);+ texture_draw(p, &o, a, &round);+ assert(wlr_render_pass_submit(base));+ assert(compare(r, actual, expected) <= 1);+ // Two blank images would also match: the corner must be cut, the middle drawn.+ uint8_t shown[WIDTH * HEIGHT * 4];+ read_pixels(r, actual, shown);+ assert(memcmp(&shown[4 * (15 * WIDTH + 13)], &shown[0], 4) == 0);+ assert(memcmp(&shown[4 * (50 * WIDTH + 60)], &shown[0], 4) != 0);}assert(enter(r, &state));glDeleteTextures(1, &a.tex);@@ -291,7 +349,7 @@ static void external_texture(struct rediwm_scene_renderer *r, struct wlr_allocatleave(r, &state);wlr_texture_destroy(reference);wlr_buffer_drop(source);- puts("PASS: external-OES EGLImage matches wlroots across all transforms and filters");+ puts("PASS: external-OES EGLImage matches wlroots across all transforms and filters, plain and shaped");}// Driver-converted YUV. A linear NV12 dmabuf is imported by wlroots exactly as a// video client's would be. Mesa reports it external-only, so both passes must@@ -410,6 +468,406 @@ static void synchronization(struct rediwm_scene_renderer *r, struct wlr_buffer *wlr_drm_syncobj_timeline_unref(timeline);puts("PASS: acquire/release timeline points and recovery from unavailable acquire");}++// ---- Stage 4b: rounded shapes (docs/plans/scene-graph-stage4.md) ----------------------+// The stage 1 spike's gates, on the production pass. Windows are made the way the+// compositor will make them: a clip spec in source units, resolved through a+// transform and snapped, so these also test that the pieces tile on the grid.+enum { GW = 640, GH = 480 };+static const float BG[4] = {224 / 255.f, 225 / 255.f, 232 / 255.f, 1};+static const float BODY[4] = {16 / 255.f, 18 / 255.f, 21 / 255.f, 1};+static const float SCALES[] = {1.f, 1.25f, 1.5f, 2.f};+static const float ZOOMS[] = {1.f, .85f, .70f, .55f, .25f};+#define COUNT(a) (sizeof(a) / sizeof(*(a)))++static void check(bool ok, const char *what, ...) {+ if (ok) return;+ char text[256];+ va_list args;+ va_start(args, what);+ vsnprintf(text, sizeof(text), what, args);+ va_end(args);+ fprintf(stderr, "FAIL: %s\n", text);+ exit(1);+}+struct image { int w, h; uint8_t *p; };+static uint8_t *at(const struct image *img, int x, int y) { return img->p + 4 * (y * img->w + x); }+static int diff(int a, int b) { return abs(a - b); }+static void read_image(struct rediwm_scene_renderer *r, struct wlr_buffer *b, struct image *img) {+ struct gl_state state;+ assert(enter(r, &state));+ img->w = b->width;+ img->h = b->height;+ img->p = realloc(img->p, 4 * (size_t)img->w * img->h);+ glBindFramebuffer(GL_FRAMEBUFFER, wlr_gles2_renderer_get_buffer_fbo(r->renderer, b));+ glPixelStorei(GL_PACK_ALIGNMENT, 1);+ glReadPixels(0, 0, img->w, img->h, GL_RGBA, GL_UNSIGNED_BYTE, img->p);+ assert(glGetError() == GL_NO_ERROR);+ leave(r, &state);+}+static struct wlr_buffer *target_sized(struct wlr_allocator *allocator, int w, int h) {+ uint64_t modifier = DRM_FORMAT_MOD_INVALID;+ struct wlr_drm_format format = {+ .format = DRM_FORMAT_ARGB8888, .len = 1, .capacity = 1, .modifiers = &modifier,+ };+ struct wlr_buffer *b = wlr_allocator_create_buffer(allocator, w, h, &format);+ assert(b);+ return b;+}+// A pass whose buffer starts as one opaque colour.+static struct wlr_render_pass *begin_fill(struct rediwm_scene_renderer *r, struct wlr_buffer *b,+ const float color[4], const pixman_region32_t *clip) {+ struct wlr_render_pass *pass = rediwm_scene_pass_begin(r, b, NULL);+ check(pass != NULL, "cannot begin an owned pass");+ wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){+ .box = {0, 0, b->width, b->height}, .color = {color[0], color[1], color[2], color[3]},+ .blend_mode = WLR_RENDER_BLEND_MODE_NONE, .clip = clip,+ });+ return pass;+}+static void finish(struct wlr_render_pass *pass) { check(wlr_render_pass_submit(pass), "an owned pass failed"); }+static void piece(struct wlr_render_pass *pass, const struct rediwm_pass_shape *shape,+ struct wlr_box box, const float color[4], const pixman_region32_t *clip) {+ rediwm_scene_pass_add_rect_shaped(pass, &(struct wlr_render_rect_options){+ .box = box, .color = {color[0], color[1], color[2], color[3]}, .clip = clip,+ }, shape);+}++// A window 200x140 (source units) at (30, 24), plus a titlebar of 36 and a footer+// of 10, placed in a layout offset by 8 device pixels.+struct window {+ struct rediwm_pass_shape shape;+ struct rediwm_clip clip;+ struct wlr_box title, client, footer;+};+static struct wlr_box box_of(struct rediwm_snap s) { return (struct wlr_box){s.x, s.y, s.width, s.height}; }+static struct window make_window(float scale, float zoom, float radius, double border) {+ const struct rediwm_xform xf = {(double)scale * zoom, 8, 8};+ const struct rediwm_clip_spec spec = {200, 140, {radius, radius, radius, radius}, border};+ struct window w = {.clip = rediwm_clip_resolve(xf, 30, 24, &spec)};+ w.shape.box = box_of(rediwm_edges_snap(w.clip.edges));+ for (int i = 0; i < 4; i++) w.shape.radii[i] = w.clip.radii[i];+ // A ring is whole device pixels, never thinner than one.+ w.shape.border = w.clip.border > 0 ? fmaxf(1.f, roundf(w.clip.border)) : 0;+ w.title = box_of(rediwm_edges_snap(rediwm_xform_rect(xf, 30, 24, 200, 36)));+ w.client = box_of(rediwm_edges_snap(rediwm_xform_rect(xf, 30, 24 + 36, 200, 140 - 36 - 10)));+ w.footer = box_of(rediwm_edges_snap(rediwm_xform_rect(xf, 30, 24 + 140 - 10, 200, 10)));+ return w;+}++// ---- Gate 1: the clip against a supersampled reference ---------------------------------+static bool inside_rrect(double px, double py, const struct rediwm_pass_shape *s) {+ double x = px - s->box.x, y = py - s->box.y;+ if (x < 0 || y < 0 || x > s->box.width || y > s->box.height) return false;+ double r = (x < s->box.width / 2.) ? (y < s->box.height / 2. ? s->radii[0] : s->radii[3])+ : (y < s->box.height / 2. ? s->radii[1] : s->radii[2]);+ double cx = x < s->box.width / 2. ? r : s->box.width - r, cy = y < s->box.height / 2. ? r : s->box.height - r;+ if (!((x < r || x > s->box.width - r) && (y < r || y > s->box.height - r))) return true;+ return hypot(x - cx, y - cy) <= r;+}+static double reference_coverage(const struct rediwm_pass_shape *s, int x, int y) {+ const int n = 8;+ int hits = 0;+ for (int j = 0; j < n; j++) for (int i = 0; i < n; i++) hits += inside_rrect(x + (i + .5) / n, y + (j + .5) / n, s);+ return (double)hits / (n * n);+}+static void gate_clip_accuracy(struct rediwm_scene_renderer *r, struct wlr_buffer *buf, struct image *img) {+ int worst = 0, fringe = 0;+ const uint8_t bg[3] = {224, 225, 232}, body[3] = {16, 18, 21};+ for (size_t si = 0; si < COUNT(SCALES); si++) for (size_t zi = 0; zi < COUNT(ZOOMS); zi++) {+ struct window w = make_window(SCALES[si], ZOOMS[zi], 10, 0);+ struct wlr_render_pass *pass = begin_fill(r, buf, BG, NULL);+ piece(pass, &w.shape, w.shape.box, BODY, NULL);+ finish(pass);+ read_image(r, buf, img);+ for (int y = w.shape.box.y - 2; y < w.shape.box.y + w.shape.box.height + 2; y++)+ for (int x = w.shape.box.x - 2; x < w.shape.box.x + w.shape.box.width + 2; x++) {+ double c = reference_coverage(&w.shape, x, y);+ for (int ch = 0; ch < 3; ch++) {+ int got = at(img, x, y)[ch];+ int want = (int)lround(bg[ch] * (1 - c) + body[ch] * c);+ if (diff(got, want) > worst) worst = diff(got, want);+ // No fringe: never outside the range of the two colours.+ int lo = body[ch] < bg[ch] ? body[ch] : bg[ch], hi = body[ch] < bg[ch] ? bg[ch] : body[ch];+ if (got < lo - 1 || got > hi + 1) fringe++;+ }+ }+ }+ check(fringe == 0, "clip: %d channel values fall outside the bg/body range (fringe)", fringe);+ // A one-pixel linear ramp on the distance is not area coverage: on a curved+ // 45-degree edge it is off by up to about .08 of the contrast, here 18 of 208+ // levels at scale 1.25, zoom .70 (an 8.75 px radius), pixel (2,2) of each+ // corner. The spike, without the 1.25 scale, measured 12. glass.c uses the+ // same ramp, and a straight edge or a pixel off the diagonal is exact.+ check(worst <= 20, "clip: differs from supersampled coverage by %d levels", worst);+ printf("PASS: rounded clip within %d levels of area coverage, no fringe, %zu scales x %zu zoom steps\n",+ worst, COUNT(SCALES), COUNT(ZOOMS));+}++// ---- Gate 2: pieces of one window share the clip with no seam --------------------------+static void gate_no_seams(struct rediwm_scene_renderer *r, struct wlr_buffer *buf, struct image *img) {+ const float red[4] = {1, 0, 0, 1}, green[4] = {0, 1, 0, 1}, blue[4] = {0, 0, 1, 1};+ for (size_t si = 0; si < COUNT(SCALES); si++) for (size_t zi = 0; zi < COUNT(ZOOMS); zi++) {+ struct window w = make_window(SCALES[si], ZOOMS[zi], 10, 0);+ struct wlr_render_pass *pass = begin_fill(r, buf, BG, NULL);+ piece(pass, &w.shape, w.title, red, NULL);+ piece(pass, &w.shape, w.client, green, NULL);+ piece(pass, &w.shape, w.footer, blue, NULL);+ finish(pass);+ read_image(r, buf, img);+ const struct wlr_box *b = &w.shape.box;+ const int margin = (int)ceilf(w.shape.radii[0]) + 2;+ for (int y = 0; y < b->height; y++) for (int x = margin; x < b->width - margin; x++) {+ const float *want = b->y + y < w.client.y ? red : b->y + y < w.footer.y ? green : blue;+ uint8_t *px = at(img, b->x + x, b->y + y);+ for (int ch = 0; ch < 3; ch++)+ check(diff(px[ch], (int)lround(want[ch] * 255)) <= 1,+ "seam at scale %.2f zoom %.2f, row %d col %d channel %d = %d",+ SCALES[si], ZOOMS[zi], y, x, ch, px[ch]);+ }+ // The pieces really tile the shape's box, with no gap and no overlap.+ check(w.title.y == b->y && w.title.y + w.title.height == w.client.y &&+ w.client.y + w.client.height == w.footer.y && w.footer.y + w.footer.height == b->y + b->height,+ "pieces do not tile the window at scale %.2f zoom %.2f", SCALES[si], ZOOMS[zi]);+ }+ puts("PASS: titlebar, client and footer meet without a seam at every scale and zoom");+}++// ---- Gate 3: border and body partition the shape's coverage ----------------------------+static void gate_border_partition(struct rediwm_scene_renderer *r, struct wlr_buffer *buf,+ struct image *img, struct image *ref) {+ int worst = 0;+ for (size_t si = 0; si < COUNT(SCALES); si++) for (size_t zi = 0; zi < COUNT(ZOOMS); zi++) {+ struct window w = make_window(SCALES[si], ZOOMS[zi], 10, 1);+ struct rediwm_pass_shape ringed = w.shape, plain = w.shape;+ plain.border = 0;+ memcpy(ringed.border_color, BODY, sizeof(BODY));+ // Ring and body in one colour, in three pieces, must equal one plain body,+ // whatever the split between them, or coverage leaks.+ struct wlr_render_pass *pass = begin_fill(r, buf, BG, NULL);+ piece(pass, &plain, plain.box, BODY, NULL);+ finish(pass);+ read_image(r, buf, ref);+ pass = begin_fill(r, buf, BG, NULL);+ piece(pass, &ringed, w.title, BODY, NULL);+ piece(pass, &ringed, w.client, BODY, NULL);+ piece(pass, &ringed, w.footer, BODY, NULL);+ finish(pass);+ read_image(r, buf, img);+ for (int i = 0; i < img->w * img->h * 4; i++) if (diff(img->p[i], ref->p[i]) > worst) worst = diff(img->p[i], ref->p[i]);+ // And the ring really is the ring: its own colour on the outer row of a+ // straight edge, the body colour just inside it.+ const float red[4] = {1, 0, 0, 1};+ memcpy(ringed.border_color, red, sizeof(red));+ pass = begin_fill(r, buf, BG, NULL);+ piece(pass, &ringed, ringed.box, BODY, NULL);+ finish(pass);+ read_image(r, buf, img);+ int cx = ringed.box.x + ringed.box.width / 2, bw = (int)ringed.border;+ check(at(img, cx, ringed.box.y)[0] >= 254 && at(img, cx, ringed.box.y)[1] <= 1,+ "outer row is not the border colour at scale %.2f zoom %.2f", SCALES[si], ZOOMS[zi]);+ check(diff(at(img, cx, ringed.box.y + bw)[0], 16) <= 1,+ "row inside the border is not the body at scale %.2f zoom %.2f", SCALES[si], ZOOMS[zi]);+ check(at(img, ringed.box.x, ringed.box.y + ringed.box.height / 2)[0] >= 254,+ "left edge is not the border colour at scale %.2f zoom %.2f", SCALES[si], ZOOMS[zi]);+ }+ check(worst <= 2, "border + body differ from one shape by %d levels", worst);+ printf("PASS: border ring and body, in one pass, add up to exactly the shape's coverage (max diff %d)\n", worst);+}++// ---- Gate 4: premultiplied texture edges and opaque (XRGB) textures ---------------------+static void gate_premultiplied(struct rediwm_scene_renderer *r, struct wlr_buffer *buf, struct image *img) {+ struct rediwm_pass_shape s = {.box = {40, 40, 80, 80}, .radii = {20, 20, 20, 20}};+ // 50% red, premultiplied: (128, 0, 0, 128) is a half-intensity red at half alpha.+ uint32_t half[16], junk = 0x000AC81E, white = 0xffffffff, solid_px[1] = {junk};+ for (int i = 0; i < 16; i++) half[i] = 0x80800000;+ struct wlr_texture *t_half = wlr_texture_from_pixels(r->renderer, DRM_FORMAT_ARGB8888, 16, 4, 4, half);+ struct wlr_texture *t_xrgb = wlr_texture_from_pixels(r->renderer, DRM_FORMAT_XRGB8888, 4, 1, 1, solid_px);+ struct wlr_texture *t_white = wlr_texture_from_pixels(r->renderer, DRM_FORMAT_XRGB8888, 4, 1, 1, &white);+ check(t_half && t_xrgb && t_white, "cannot create the test textures");+ struct { struct wlr_texture *t; float alpha; const char *name; } cases[] = {+ {t_half, 1, "half-transparent"}, {t_xrgb, 1, "xrgb"}, {t_white, .5f, "opacity"},+ };+ for (size_t i = 0; i < COUNT(cases); i++) {+ const bool on_black = cases[i].t == t_white;+ float base[4] = {BG[0], BG[1], BG[2], 1};+ if (on_black) memset(base, 0, sizeof(float) * 3);+ struct wlr_render_pass *pass = begin_fill(r, buf, base, NULL);+ float alpha = cases[i].alpha;+ rediwm_scene_pass_add_texture_shaped(pass, &(struct wlr_render_texture_options){+ .texture = cases[i].t, .dst_box = s.box, .alpha = &alpha}, &s);+ finish(pass);+ read_image(r, buf, img);+ uint8_t *in = at(img, 80, 80);+ const int want[3][3] = {{240, 112, 116}, {10, 200, 30}, {128, 128, 128}};+ for (int ch = 0; ch < 3; ch++)+ check(diff(in[ch], want[i][ch]) <= 2, "%s texel gave (%d,%d,%d) inside the shape",+ cases[i].name, in[0], in[1], in[2]);+ // At the edge the colour is the premultiplied source times coverage over the backdrop:+ // never darker or brighter than the two ends, and within the clip's accuracy of area coverage.+ double src_a = i == 0 ? .50196 : i == 2 ? .5 : 1;+ for (int y = s.box.y - 1; y < s.box.y + s.box.height + 1; y++)+ for (int x = s.box.x - 1; x < s.box.x + s.box.width + 1; x++) {+ double c = reference_coverage(&s, x, y);+ for (int ch = 0; ch < 3; ch++) {+ double bgv = base[ch] * 255;+ double want_v = want[i][ch] - (1 - src_a) * bgv; // the premultiplied source, 0..255+ double expect = want_v * c + bgv * (1 - src_a * c);+ check(diff(at(img, x, y)[ch], (int)lround(expect)) <= 14,+ "%s edge pixel (%d,%d) channel %d is %d, area coverage predicts %.0f",+ cases[i].name, x, y, ch, at(img, x, y)[ch], expect);+ }+ }+ }+ wlr_texture_destroy(t_half);+ wlr_texture_destroy(t_xrgb);+ wlr_texture_destroy(t_white);+ puts("PASS: premultiplied edges, opacity and XRGB textures blend correctly through the clip");+}++// ---- Gate 5: the opaque cover of the model is fully covered in real pixels -------------+// rediwm_clip_opaque promises its rectangles are inside full coverage; the output+// pass will draw them without blending and trust that. Painting them first with+// blending off, then the whole window over them, must change nothing.+static void gate_opaque_cover(struct rediwm_scene_renderer *r, struct wlr_buffer *buf,+ struct image *img, struct image *ref) {+ for (size_t si = 0; si < COUNT(SCALES); si++) for (size_t zi = 0; zi < COUNT(ZOOMS); zi++) {+ struct window w = make_window(SCALES[si], ZOOMS[zi], 10, 1);+ struct rediwm_pass_shape s = w.shape;+ memcpy(s.border_color, BODY, sizeof(BODY));+ struct wlr_render_pass *pass = begin_fill(r, buf, BG, NULL);+ piece(pass, &s, s.box, BODY, NULL);+ finish(pass);+ read_image(r, buf, ref);+ // Border colour equals the body's here, so the cover may include the ring.+ struct rediwm_snap cover[3];+ int n = rediwm_clip_opaque(&w.clip, cover);+ check(n > 0, "no opaque cover at scale %.2f zoom %.2f", SCALES[si], ZOOMS[zi]);+ pass = begin_fill(r, buf, BG, NULL);+ for (int i = 0; i < n; i++)+ wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){+ .box = box_of(cover[i]), .color = {BODY[0], BODY[1], BODY[2], 1},+ .blend_mode = WLR_RENDER_BLEND_MODE_NONE});+ piece(pass, &s, s.box, BODY, NULL);+ finish(pass);+ read_image(r, buf, img);+ for (int i = 0; i < img->w * img->h * 4; i++)+ check(diff(img->p[i], ref->p[i]) <= 1, "cover pixel differs at scale %.2f zoom %.2f (%d vs %d)",+ SCALES[si], ZOOMS[zi], img->p[i], ref->p[i]);+ }+ puts("PASS: the opaque cover draws without blending and changes nothing, at every scale and zoom");+}++// ---- Gate 6: analytic shadow ------------------------------------------------------------+static void gate_shadow(struct rediwm_scene_renderer *r, struct wlr_buffer *buf, struct image *img) {+ const float white[4] = {1, 1, 1, 1};+ const struct rediwm_pass_shape s = {.box = {80, 60, 200, 120}};+ const float sigma = 12, oy = 6;+ struct wlr_render_pass *pass = begin_fill(r, buf, white, NULL);+ rediwm_scene_pass_add_shadow(pass, &s, &(struct rediwm_pass_shadow){+ .offset_y = oy, .sigma = sigma, .color = {0, 0, 0, .5f}, .hide_inside = true});+ finish(pass);+ read_image(r, buf, img);+ int x = s.box.x + s.box.width / 2, bottom = s.box.y + s.box.height, worst = 0, last = 0;+ for (int dy = 0; dy < 60; dy++) {+ // Distance of the pixel centre below the offset shape's bottom edge.+ double d = (dy + .5) - oy;+ int want = (int)lround(255 * (1 - .5 * .5 * erfc(d / (sigma * 1.41421356237))));+ int got = at(img, x, bottom + dy)[0];+ if (diff(got, want) > worst) worst = diff(got, want);+ check(got >= last, "shadow is not monotonic at %d px (%d after %d)", dy, got, last);+ last = got;+ }+ check(worst <= 3, "shadow profile differs from the Gaussian edge by %d levels", worst);+ check(at(img, 10, 10)[0] == 255 && at(img, x, s.box.y + s.box.height / 2)[0] == 255,+ "shadow reaches outside its footprint or inside the window");+ // Not hidden inside: the same draw shows through the window.+ pass = begin_fill(r, buf, white, NULL);+ rediwm_scene_pass_add_shadow(pass, &s, &(struct rediwm_pass_shadow){+ .offset_y = oy, .sigma = sigma, .color = {0, 0, 0, .5f}});+ finish(pass);+ read_image(r, buf, img);+ check(at(img, x, s.box.y + s.box.height / 2)[0] < 200, "an unhidden shadow does not show under the shape");+ printf("PASS: analytic shadow follows the Gaussian edge (max diff %d) and stays out of the window\n", worst);+}++// ---- Gate 7: a partial repaint across a clip edge equals a full repaint -----------------+static void draw_scene(struct rediwm_scene_renderer *r, struct wlr_buffer *buf, const struct window *w,+ int variant, const pixman_region32_t *clip) {+ const float bodies[2][3][4] = {+ {{.2f, .3f, .5f, 1}, {.9f, .9f, .9f, 1}, {.3f, .3f, .3f, 1}},+ {{.8f, .2f, .2f, 1}, {.1f, .6f, .2f, 1}, {.6f, .6f, .1f, 1}},+ };+ struct rediwm_pass_shape s = w->shape;+ const float rings[2][4] = {{1, 1, 1, 1}, {1, 0, 0, 1}};+ memcpy(s.border_color, rings[variant], sizeof(rings[variant]));+ struct wlr_render_pass *pass = begin_fill(r, buf, BG, clip);+ rediwm_scene_pass_add_shadow(pass, &s, &(struct rediwm_pass_shadow){+ .offset_y = 12, .sigma = 12,+ .color = {0, 0, 0, .4f}, .hide_inside = true, .clip = clip});+ piece(pass, &s, w->title, bodies[variant][0], clip);+ piece(pass, &s, w->client, bodies[variant][1], clip);+ piece(pass, &s, w->footer, bodies[variant][2], clip);+ finish(pass);+}+static void gate_damage_at_clip_edge(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator,+ struct image *img) {+ struct wlr_buffer *a = target_sized(allocator, GW, GH), *b = target_sized(allocator, GW, GH),+ *c = target_sized(allocator, GW, GH);+ struct image one = {0}, two = {0};+ int differing_inside = 0, differing_outside = 0;+ const float zooms[] = {1.f, .55f};+ for (size_t si = 0; si < COUNT(SCALES); si++) for (size_t zi = 0; zi < COUNT(zooms); zi++) {+ struct window w = make_window(SCALES[si], zooms[zi], 10, 1);+ const struct wlr_box *bx = &w.shape.box;+ // Damage whose edges cut through the corner arcs, the ring and the shadow:+ // a square over the top-left arc, one over the bottom-right, and a band.+ pixman_region32_t damage;+ pixman_region32_init_rect(&damage, bx->x - 5, bx->y - 5, 13, 13);+ pixman_region32_union_rect(&damage, &damage, bx->x + bx->width - 9, bx->y + bx->height - 11, 30, 29);+ pixman_region32_union_rect(&damage, &damage, bx->x + bx->width / 3, bx->y + 3, 7, bx->height + 20);+ draw_scene(r, a, &w, 0, NULL);+ draw_scene(r, b, &w, 1, NULL);+ draw_scene(r, c, &w, 0, NULL);+ draw_scene(r, c, &w, 1, &damage);+ read_image(r, a, &one);+ read_image(r, b, &two);+ read_image(r, c, img);+ for (int y = 0; y < GH; y++) for (int x = 0; x < GW; x++) {+ bool in = pixman_region32_contains_point(&damage, x, y, NULL);+ const uint8_t *want = at(in ? &two : &one, x, y);+ for (int ch = 0; ch < 4; ch++)+ check(at(img, x, y)[ch] == want[ch],+ "partial repaint differs at (%d,%d) channel %d (%s damage): %d, wanted %d (scale %.2f)",+ x, y, ch, in ? "inside" : "outside", at(img, x, y)[ch], want[ch], SCALES[si]);+ if (memcmp(at(&one, x, y), at(&two, x, y), 4)) in ? differing_inside++ : differing_outside++;+ }+ pixman_region32_fini(&damage);+ }+ // The control: the two frames really differ both where the damage is and where it is not.+ check(differing_inside > 0 && differing_outside > 0, "the test frames do not differ across the damage edge");+ free(one.p);+ free(two.p);+ wlr_buffer_drop(a);+ wlr_buffer_drop(b);+ wlr_buffer_drop(c);+ puts("PASS: repainting only damage that cuts through arcs, ring and shadow equals a full repaint");+}++static void shaped_gates(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator) {+ struct wlr_buffer *buf = target_sized(allocator, GW, GH);+ struct image img = {0}, ref = {0};+ gate_clip_accuracy(r, buf, &img);+ gate_no_seams(r, buf, &img);+ gate_border_partition(r, buf, &img, &ref);+ gate_premultiplied(r, buf, &img);+ gate_opaque_cover(r, buf, &img, &ref);+ gate_shadow(r, buf, &img);+ gate_damage_at_clip_edge(r, allocator, &img);+ free(img.p);+ free(ref.p);+ wlr_buffer_drop(buf);+}int main(void) {wlr_log_init(WLR_ERROR, NULL);struct wl_display *display = wl_display_create();@@ -423,18 +881,24 @@ int main(void) {struct rediwm_scene_renderer *r = rediwm_scene_renderer_create(renderer);assert(r);struct wlr_buffer *actual = target(allocator), *expected = target(allocator);- parity(r, actual, expected);+ parity(r, actual, expected, false);+ shapes_fail_closed(r, actual);+ assert(rediwm_scene_renderer_enable_shapes(r));+ assert(rediwm_scene_renderer_enable_shapes(r)); // idempotent+ parity(r, actual, expected, true);damage(r, actual, expected);state_restoration(r, actual);external_texture(r, allocator, actual, expected);external_yuv(r, actual, expected);synchronization(r, actual);+ shaped_gates(r, allocator);// The ordinary wlroots pass must still work after owned GL has run.- parity(r, actual, expected);+ parity(r, actual, expected, false);if (r->has_vao) {// Exercise the GLES2 fallback even on hosts with vertex arrays.r->has_vao = false;- parity(r, actual, expected);+ parity(r, actual, expected, false);+ parity(r, actual, expected, true);state_restoration(r, actual);r->has_vao = true;}