Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

scene graph plan, stage 4a: rounded clip in the projection model

oxydizerd368a23a6f82
diff --git a/AGENTS.md b/AGENTS.md
index 6632ac9..64a28ea 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -81,7 +81,11 @@ the one for the directory you work in: `src/files`, `src/editor`,
(clip, opacity, fractional placement) is a field on the item, not a
computation in `present_leaf`. A leaf or group leaves its `paint` list before
the head is freed (`detach`, `finish_group`). Buffer identity, wait points and
- opaque regions are still read from the source node by `present`.
+ opaque regions are still read from the source node by `present`. A tree's
+ rounded clip (`rediwm_projection_set_tree_clip`) is a field on each item
+ (`item.clip`), resolved after the tree's own zoom and scale; an xdg popup
+ lives under its parent's tree and must carry
+ `rediwm_projection_set_tree_unclipped`.
* **Client buffers:** `wlr_client_buffer_apply_damage` works in place only
without extra locks, so mark long-held locks ignorable. v4
`attach(buffer, 0, 0)` still sets OFFSET; only nonzero dx/dy remaps. Don't
diff --git a/docs/plans/scene-graph-stage4.md b/docs/plans/scene-graph-stage4.md
index 4647e4b..8a31bf2 100644
--- a/docs/plans/scene-graph-stage4.md
+++ b/docs/plans/scene-graph-stage4.md
@@ -1,10 +1,10 @@
# Scene graph plan, stage 4: clipping and primitives
-Status: **draft**, 2026-10-09, nothing implemented. Stage 3 of
+Status: **4a done, 4b to 4e not started**, 2026-10-09. Stage 3 of
[scene-graph.md](scene-graph.md) (the owned output pass, radius zero) is done
apart from hardware qualification; this stage is why the plan exists: the user
-requires clipped client corners, and wlroots 0.20 cannot mask a client. Open
-decisions are listed at the end; D1 changes the shape of the work.
+requires clipped client corners, and wlroots 0.20 cannot mask a client. The five
+decisions at the end were taken the same day, all as recommended.
## Goal and exit
@@ -93,7 +93,12 @@ presenters. Two ways to live with that, chosen in D1.
(`Toplevel.frame_tree`), set through a new projection call beside
`rediwm_projection_set_tree_scale`, and resolve copies it onto every leaf of the
group, so chrome, client and subsurfaces share one shape. Popups are separate
-trees and are never clipped by their parent. Radius 0 means no clip: maximized
+trees in the sense that they belong to no window's shape, but the source graph
+makes them children of their parent surface's tree (`Popup.create` uses
+`parent_tree.createSceneXdgSurface`), under `content_tree` and `frame_tree`. A
+tree clip would clip menus and tooltips that hang outside the window, so a popup's
+tree carries a barrier (`rediwm_projection_set_tree_unclipped`, set by
+`Popup.create` in 4c) that ends the inherited clip. Radius 0 means no clip: maximized
and fullscreen windows, and client-side-decorated windows (decided in stage 0:
unclipped unless a rule asks). `.shell` windows (Settings) are clipped like
native ones.
@@ -147,10 +152,47 @@ rest are untouched.
Each leaves a working compositor and its own gates.
-**4a. Clip in the model (no visible change).** Clip on groups and items,
-`resolve` copies it, opaque-region helper; pure tests in `projection_test.c`
-(edges, per-corner radii, opaque cross, composition with scale, camera and `1/N`).
-Gate: 16/16 goldens unchanged, no cost change.
+**4a. Clip in the model (no visible change). Done.** `scene_graph.h` has
+`rediwm_clip_spec` (what a tree sets: size, four radii, border, in source units),
+`rediwm_clip` (the same in layout space), `rediwm_clip_resolve`, the per-corner
+signed distance `rediwm_sd_rounded_box` (the stage 1 shader's formula),
+`rediwm_coverage` and `rediwm_clip_opaque`. `projection.c` has two tree addons
+beside zoom and scale: `rediwm_projection_set_tree_clip` (NULL removes it; bad
+sizes, negative or non-finite radii and borders are refused) and
+`rediwm_projection_set_tree_unclipped` (the popup barrier). Resolve evaluates a
+tree's clip after that tree's own zoom and scale, so it follows its window and an
+inner scale never changes it; an inner clipped tree replaces the outer shape for
+its own leaves, and nothing intersects them. Each leaf stores its copy and
+`item.clip` points at it (NULL when unclipped); the glass effect item takes its
+target's clip into storage of its own. No compositor code sets a clip yet.
+
+The opaque cover is the snapped frame minus a square at each corner two pixels
+deeper than the radius (the margin keeps the anti-aliased arc, and the outward
+rounding of a region converted to device pixels at any scale of 1 or more, out of
+the claim). It returns up to three rectangles, or one full-height column when the
+corners leave no room for a middle band: the first version returned nothing for a
+pill-shaped window, which the property test caught. Tests (`test-projection`):
+the geometry above against hand-computed distances including per-corner radii and
+mirroring; a property test over six sizes, seven radius sets and scales 1 to 2
+with fractional origins, asserting every claimed pixel has full coverage under the
+shape, claims never overlap, square corners lose nothing and what is left out is
+bounded; and the projection path (zoom, camera, nested scale, sibling window,
+effect inheritance, replacement, removal, refusal of bad shapes, popup barrier,
+destruction). Zoom, scale, clip and the barrier share one per-tree addon
+(`tree_props`), so resolve does one lookup per tree, where zoom and scale had two.
+
+Gate met. The existing projection tests are unchanged and pass; Debug and
+ReleaseSafe build; `zig build test` passes; and screenshots from both presenters
+at scale 1, 1.5 and mixed are **bit-identical to ones captured before 4a**
+(16 of 16 each, tolerance 0). Cost was measured by timing `resolve()` on a
+30-window scene of 120 trees and 600 leaves, built against the committed
+`projection.c` and against this one, alternating runs: 10.1-10.2 us before and
+10.9 us after, about **+0.7 us per resolve, 1 ns per leaf**. Ablation found where
+it comes from: the unified lookup and the clip math cost nothing measurable; the
+cost is writing the new `item.clip` field on every leaf, and writing only when it
+changes (`else if (item->clip)`) halved it from 1.5 us. A typical scene has a
+fraction of those leaves, so this is far below what any frame-level measure can
+resolve; 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
@@ -223,31 +265,36 @@ before 4c and judge the stage against those.
at the clip edge** (stale edge pixels): both have explicit rules and gates.
* **Hit-test post-filter** must keep pointer constraints, tablets, touch and
drag-and-drop on one path (they all use `hitTest`).
-* **Consumers that bypass the group:** window capture (`WindowCaptureSource`,
- screencopy of a window), close-animation snapshots, the taskbar peek, switcher
- and mini-map previews. Each either renders through the pass (and inherits the
- clip) or needs the shape applied; audit in 4a. Decide whether a captured window
- keeps transparent corners (recommended: yes).
+* **Consumers that bypass the group** (audited in 4a): the close-animation
+ snapshot is a child of `content_tree`, hence inside the clipped tree, and
+ inherits the shape. The taskbar peek only changes the real window's opacity.
+ The switcher shows a scaled thumbnail of the client buffer on a card and the
+ mini-map draws rectangles; neither shows a framed window, so neither changes.
+ **Window capture does bypass it:** `WindowCaptureSource` walks the content tree
+ with `wlr_scene_node_for_each_buffer` and draws each buffer in its own loop
+ (through the selected pass). It captures the client without the chrome, so D3
+ reduces to the client's two bottom corners; making them transparent means giving
+ that loop the frame's clip and the clip shader (4c), not a graph change.
* **Fractional zoom and 1/N:** the arc is analytic, but the quad edges still snap;
gates cover the full grid.
* **Pre-existing failures** in suites this stage rewrites (`window_corners` flake,
the Xwayland titlebar assertion) are fixed here rather than carried.
-## Decisions needed
-
-* **D1. What happens to the legacy presenter at 4e.** (a) Remove the `legacy`
- choice there, folding the chrome-related part of stage 6 into stage 4;
- simplest code, and the flag in `footerHeight` disappears, but there is no
- fallback if the owned path misbehaves on some GPU. (b) Keep it selectable until
- stage 6, with the old frame geometry behind the flag; safer, but two geometries
- and the old skirt code survive another stage. Recommendation: (a), after the
- hardware checklist and a period of use as the default.
-* **D2. Shadows:** analytic in 4d with a fallback to CPU patches, or leave them
- CPU permanently. Recommendation: try 4d, keep the fallback.
-* **D3. Window capture corners:** transparent (recommended) or square.
-* **D4. CSD windows:** stay unclipped with the rect outline (current decision), or
- gain an opt-in rule now. Recommendation: later; it is independent.
-* **D5. Hit tests:** post-filter now (recommended), graph hit test with stage 6.
+## Decisions (taken 2026-10-09, all as recommended)
+
+* **D1. The legacy presenter goes at 4e.** The `legacy` choice is removed there,
+ folding the chrome-related part of stage 6 into this stage, and the footer flag
+ disappears with it. It follows the stage 3 hardware checklist and a period of
+ use with `owned` as the default; until then 4c keeps the old geometry behind the
+ flag. Cost: no fallback if the owned path misbehaves on some GPU, which is why
+ the checklist comes first.
+* **D2. Shadows:** try analytic in 4d and keep the CPU patches as the fallback if
+ the corners do not pass.
+* **D3. Window capture:** the client's bottom corners are transparent in a
+ captured window (see the audit above for how).
+* **D4. CSD windows:** stay unclipped, with the rect outline.
+* **D5. Hit tests:** a shape post-filter on the existing pick now; the graph hit
+ test arrives with stage 6.
## Not in this stage
diff --git a/src/projection.c b/src/projection.c
index 1d416f3..91f415f 100644
--- a/src/projection.c
+++ b/src/projection.c
@@ -89,64 +89,95 @@ struct leaf {
// without a report a replaced buffer damages its whole view.
bool owned_damage;
int damage_width, damage_height;
- // The graph's view of this leaf, rewritten by every resolve().
+ // The graph's view of this leaf, rewritten by every resolve(). `item.clip`
+ // points at `clip` when the leaf is under a clipped tree.
struct rediwm_item item;
+ struct rediwm_clip clip;
// Glass's raster is published after resolve; its target still supplies the
// geometry. Kept separately so an unchanged/held blur survives the next sync.
struct rediwm_item effect_item;
+ struct rediwm_clip effect_clip;
struct wlr_buffer *last_buffer;
// The client buffer whose view lock we told wlroots to ignore, if any.
struct wlr_client_buffer *marked;
struct view_state applied;
bool has_applied;
};
-struct tree_zoom {
+// Everything the compositor says about how a source tree is presented, in one
+// addon. Resolve meets every tree of every window on every frame, and each
+// property that had an addon of its own cost a lookup there. The properties:
+// * zoom: a window's scale about its origin, composing with the camera;
+// * scale: a relative presentation scale (Xwayland's 1/N), composing with both;
+// * clip: the rounded shape that clips everything under the tree. Resolve
+// evaluates it after this tree's own zoom and scale, so it follows its window
+// and an inner scale never changes it. A clipped tree inside another replaces
+// the outer shape for its own leaves; nothing intersects them;
+// * unclipped: ends the inherited clip for the subtree. A popup's scene tree is a
+// child of its parent surface's, so it sits under the window's clipped tree in
+// the source graph while belonging to no window shape. The subtree's own clip,
+// if it sets one, still applies.
+struct tree_props {
struct wlr_addon addon;
- double scale;
+ bool has_zoom, has_scale, has_clip, unclipped;
+ double zoom, scale;
+ struct rediwm_clip_spec clip;
};
-static void destroy_tree_zoom(struct wlr_addon *addon) {
- struct tree_zoom *zoom = wl_container_of(addon, zoom, addon);
+static void destroy_tree_props(struct wlr_addon *addon) {
+ struct tree_props *props = wl_container_of(addon, props, addon);
wlr_addon_finish(addon);
- free(zoom);
+ free(props);
+}
+static const struct wlr_addon_interface tree_props_impl = {
+ .name = "rediwm tree presentation", .destroy = destroy_tree_props};
+static struct tree_props *tree_props_find(struct wlr_scene_node *node) {
+ struct wlr_addon *addon = wlr_addon_find(&node->addons, NULL, &tree_props_impl);
+ struct tree_props *props;
+ return addon ? (props = wl_container_of(addon, props, addon)) : NULL;
+}
+static struct tree_props *tree_props_get(struct wlr_scene_node *node) {
+ struct tree_props *props = tree_props_find(node);
+ if (props) return props;
+ props = calloc(1, sizeof(*props));
+ if (!props) return NULL;
+ wlr_addon_init(&props->addon, &node->addons, NULL, &tree_props_impl);
+ return props;
}
-static const struct wlr_addon_interface tree_zoom_impl = {
- .name = "rediwm window zoom", .destroy = destroy_tree_zoom};
bool rediwm_projection_set_tree_zoom(struct wlr_scene_node *node, double scale) {
- struct wlr_addon *addon = wlr_addon_find(&node->addons, NULL, &tree_zoom_impl);
- struct tree_zoom *zoom;
- if (addon) {
- zoom = wl_container_of(addon, zoom, addon);
- } else {
- zoom = calloc(1, sizeof(*zoom));
- if (!zoom) return false;
- wlr_addon_init(&zoom->addon, &node->addons, NULL, &tree_zoom_impl);
- }
- zoom->scale = scale;
+ struct tree_props *props = tree_props_get(node);
+ if (!props) return false;
+ props->zoom = scale;
+ props->has_zoom = true;
return true;
}
-struct tree_scale {
- struct wlr_addon addon;
- double factor;
-};
-static void destroy_tree_scale(struct wlr_addon *addon) {
- struct tree_scale *scale = wl_container_of(addon, scale, addon);
- wlr_addon_finish(addon);
- free(scale);
-}
-static const struct wlr_addon_interface tree_scale_impl = {
- .name = "rediwm tree scale", .destroy = destroy_tree_scale};
bool rediwm_projection_set_tree_scale(struct wlr_scene_node *node, double factor) {
if (factor <= 0) return false;
- struct wlr_addon *addon = wlr_addon_find(&node->addons, NULL, &tree_scale_impl);
- struct tree_scale *scale;
- if (addon) {
- scale = wl_container_of(addon, scale, addon);
- } else {
- scale = calloc(1, sizeof(*scale));
- if (!scale) return false;
- wlr_addon_init(&scale->addon, &node->addons, NULL, &tree_scale_impl);
+ struct tree_props *props = tree_props_get(node);
+ if (!props) return false;
+ props->scale = factor;
+ props->has_scale = true;
+ return true;
+}
+static bool finite_at_least_zero(double value) { return isfinite(value) && value >= 0; }
+bool rediwm_projection_set_tree_clip(struct wlr_scene_node *node,
+ const struct rediwm_clip_spec *spec) {
+ if (!spec) {
+ struct tree_props *props = tree_props_find(node);
+ if (props) props->has_clip = false;
+ return true;
}
- scale->factor = factor;
+ if (spec->width <= 0 || spec->height <= 0 || !finite_at_least_zero(spec->border)) return false;
+ for (int i = 0; i < 4; i++)
+ if (!finite_at_least_zero(spec->radii[i])) return false;
+ struct tree_props *props = tree_props_get(node);
+ if (!props) return false;
+ props->clip = *spec;
+ props->has_clip = true;
+ return true;
+}
+bool rediwm_projection_set_tree_unclipped(struct wlr_scene_node *node, bool unclipped) {
+ struct tree_props *props = unclipped ? tree_props_get(node) : tree_props_find(node);
+ if (unclipped && !props) return false;
+ if (props) props->unclipped = unclipped;
return true;
}
static const struct wlr_addon_interface lookup_impl, source_lookup_impl;
@@ -477,25 +508,27 @@ extern bool rediwm_glass_is_effect_node(struct wlr_scene_node *node);
// moves, sizes and shows nothing in the view tree.
static void resolve_node(struct rediwm_projection *p, struct group *g,
struct wlr_scene_node *node, double x, double y,
- bool enabled, struct rediwm_xform t) {
+ bool enabled, struct rediwm_xform t,
+ const struct rediwm_clip *clip) {
x += node->x;
y += node->y;
enabled = enabled && node->enabled;
if (node->type == WLR_SCENE_NODE_TREE) {
- struct wlr_addon *addon = wlr_addon_find(&node->addons, NULL, &tree_zoom_impl);
- if (addon) {
- struct tree_zoom *zoom = wl_container_of(addon, zoom, addon);
+ struct rediwm_clip here;
+ const struct tree_props *props = tree_props_find(node);
+ if (props) {
// Window zoom composes with the camera, around the frame origin.
- t = rediwm_xform_scale_about(t, zoom->scale, x, y);
- }
- struct wlr_addon *scale_addon = wlr_addon_find(&node->addons, NULL, &tree_scale_impl);
- if (scale_addon) {
- struct tree_scale *scale = wl_container_of(scale_addon, scale, addon);
- t = rediwm_xform_scale_about(t, scale->factor, x, y);
+ if (props->has_zoom) t = rediwm_xform_scale_about(t, props->zoom, x, y);
+ if (props->has_scale) t = rediwm_xform_scale_about(t, props->scale, x, y);
+ if (props->unclipped) clip = NULL;
+ if (props->has_clip) {
+ here = rediwm_clip_resolve(t, x, y, &props->clip);
+ clip = &here;
+ }
}
struct wlr_scene_node *child;
wl_list_for_each(child, &wlr_scene_tree_from_node(node)->children, link)
- resolve_node(p, g, child, x, y, enabled, t);
+ resolve_node(p, g, child, x, y, enabled, t, clip);
return;
}
struct leaf *l = get_leaf(p, node);
@@ -510,6 +543,14 @@ static void resolve_node(struct rediwm_projection *p, struct group *g,
item->enabled = enabled;
item->scale = t.scale;
item->content = (struct rediwm_content){0};
+ // Only write what changes: rewriting a NULL over a NULL on every leaf of every
+ // window dirties the line for nothing (measured at about 1 ns per leaf).
+ if (clip) {
+ l->clip = *clip;
+ item->clip = &l->clip;
+ } else if (item->clip) {
+ item->clip = NULL;
+ }
if (node->type == WLR_SCENE_NODE_RECT) {
struct wlr_scene_rect *r = wlr_scene_rect_from_node(node);
item->kind = REDIWM_ITEM_RECT;
@@ -762,7 +803,7 @@ static void resolve(struct rediwm_projection *p, double z, double cx, double cy,
rediwm_xform_point(t, node->x, node->y, &g->x, &g->y);
g->enabled = node->enabled;
wl_list_insert(p->paint.prev, &g->paint);
- resolve_node(p, g, node, 0, 0, true, t);
+ resolve_node(p, g, node, 0, 0, true, t, NULL);
}
}
// Graph -> view tree, then hide what the graph no longer reaches.
@@ -839,6 +880,11 @@ void rediwm_projection_effect_updated(struct wlr_scene_buffer *effect, struct wl
if (!l || !t) return;
const struct rediwm_snap snap = rediwm_edges_snap(t->item.edges);
l->effect_item = layout_item(&effect->node, snap.x, snap.y);
+ // The blur takes the shape of the titlebar it sits under.
+ if (t->item.clip) {
+ l->effect_clip = *t->item.clip;
+ l->effect_item.clip = &l->effect_clip;
+ }
}
static bool visit_items(struct rediwm_projection *p, struct wlr_scene_node *node,
int x, int y, bool reverse, rediwm_item_iterator iterator, void *data) {
diff --git a/src/projection.h b/src/projection.h
index 96574f2..1e65858 100644
--- a/src/projection.h
+++ b/src/projection.h
@@ -7,6 +7,7 @@ struct wlr_scene_buffer;
struct wlr_surface_state;
struct rediwm_projection;
struct rediwm_item;
+struct rediwm_clip_spec;
// Synchronous traversal of the whole displayed graph (bottom-to-top unless
// reverse is true). World
// geometry comes from resolved items, never the view tree. `transport` owns
@@ -35,6 +36,13 @@ bool rediwm_projection_set_tree_zoom(struct wlr_scene_node *, double);
// Relative presentation scale. Composes with parent camera and window zoom.
bool rediwm_projection_set_tree_scale(struct wlr_scene_node *, double);
double rediwm_projection_node_zoom(struct wlr_scene_node *);
+// One rounded-rectangle shape for every leaf under the tree, in the tree's own
+// units and anchored at its origin; NULL removes it. Returns false for a size
+// that is not positive or a radius or border that is negative or not finite.
+bool rediwm_projection_set_tree_clip(struct wlr_scene_node *, const struct rediwm_clip_spec *);
+// Ends the clip inherited from an ancestor for this subtree (an xdg popup lives
+// under its parent's tree but is no part of the window's shape).
+bool rediwm_projection_set_tree_unclipped(struct wlr_scene_node *, bool);
// Buffer-local damage for the next buffer set on a compositor-owned source
// buffer node; NULL damages all of it. Surface leaves track their own.
struct pixman_region32;
diff --git a/src/projection_test.c b/src/projection_test.c
index 79a5b46..1f7acc2 100644
--- a/src/projection_test.c
+++ b/src/projection_test.c
@@ -280,6 +280,265 @@ static void test_graph(struct wlr_scene *scene) {
wlr_buffer_drop(buffer);
wlr_buffer_drop(glass_buffer);
}
+// Stage 4: the clip shape. Pure geometry first, with no scene at all.
+static bool near(double a, double b) { return fabs(a - b) < 1e-9; }
+static void test_clip_geometry(void) {
+ // Resolve composes the camera and the window's zoom into the transform, takes
+ // the rectangle's edges from it, scales radius and border, and caps a radius
+ // at half the short side first.
+ const struct rediwm_xform camera = rediwm_xform_camera(.85, 6.5, -2.5, -17, 31);
+ const struct rediwm_xform t = rediwm_xform_scale_about(camera, .75, 37, 29);
+ const struct rediwm_clip_spec spec = {.width = 120, .height = 80, .radii = {10, 10, 6, 60}, .border = 1};
+ const struct rediwm_clip clip = rediwm_clip_resolve(t, 37, 29, &spec);
+ const struct rediwm_edges expected = rediwm_xform_rect(t, 37, 29, 120, 80);
+ assert(clip.edges.x1 == expected.x1 && clip.edges.y1 == expected.y1 &&
+ clip.edges.x2 == expected.x2 && clip.edges.y2 == expected.y2);
+ assert(near(clip.radii[0], 10 * t.scale) && near(clip.radii[2], 6 * t.scale));
+ assert(near(clip.radii[3], 40 * t.scale)); // 60 capped to half of the 80 side.
+ assert(near(clip.border, t.scale));
+
+ // The distance, in a 100x60 box at (20, 10) with radius 10.
+ const double all[4] = {10, 10, 10, 10};
+ const double arc = 10 * (1 - sqrt(.5));
+ assert(near(rediwm_sd_rounded_box(70, 40, 20, 10, 100, 60, all), -20 - 10));
+ assert(near(rediwm_sd_rounded_box(70, 10, 20, 10, 100, 60, all), 0)); // On the top edge.
+ assert(near(rediwm_sd_rounded_box(20, 40, 20, 10, 100, 60, all), 0)); // On the left edge.
+ assert(near(rediwm_sd_rounded_box(20 + arc, 10 + arc, 20, 10, 100, 60, all), 0)); // On the arc.
+ assert(near(rediwm_sd_rounded_box(20, 10, 20, 10, 100, 60, all), 10 * (sqrt(2) - 1)));
+ assert(rediwm_sd_rounded_box(20 + arc + 1, 10 + arc + 1, 20, 10, 100, 60, all) < 0);
+ // Each corner has its own radius: the sharp one reaches the bounding corner,
+ // the large one stays away from it, and the others are untouched.
+ const double mixed[4] = {0, 20, 5, 10};
+ assert(near(rediwm_sd_rounded_box(20, 10, 20, 10, 100, 60, mixed), 0));
+ assert(near(rediwm_sd_rounded_box(120, 10, 20, 10, 100, 60, mixed), 20 * (sqrt(2) - 1)));
+ assert(near(rediwm_sd_rounded_box(120, 70, 20, 10, 100, 60, mixed), 5 * (sqrt(2) - 1)));
+ assert(near(rediwm_sd_rounded_box(20, 70, 20, 10, 100, 60, mixed), 10 * (sqrt(2) - 1)));
+ // Symmetric: the same radii mirrored give the mirrored distance.
+ const double mirrored[4] = {20, 0, 10, 5};
+ for (double px = 18; px <= 122; px += 6.5)
+ for (double py = 8; py <= 72; py += 5.5)
+ assert(near(rediwm_sd_rounded_box(px, py, 20, 10, 100, 60, mixed),
+ rediwm_sd_rounded_box(140 - px, py, 20, 10, 100, 60, mirrored)));
+ // Coverage is the one-pixel ramp the CPU chrome and the shader both use.
+ assert(rediwm_coverage(-.5) == 1 && rediwm_coverage(-3) == 1 && rediwm_coverage(.5) == 0 &&
+ rediwm_coverage(9) == 0 && near(rediwm_coverage(0), .5) && near(rediwm_coverage(.25), .25));
+}
+// Every pixel the opaque cover claims must be fully covered by the shape, and it
+// may leave out only the corner squares. Checked in the clip's own pixel grid,
+// for fractional origins and for clips authored at fractional scales.
+static void test_clip_opaque(void) {
+ const int sizes[][2] = {{40, 40}, {120, 80}, {121, 79}, {300, 200}, {17, 60}, {8, 8}};
+ const double radii[][4] = {{10, 10, 10, 10}, {0, 0, 0, 0}, {10, 4, 0, 20}, {30, 30, 6, 6},
+ {50, 50, 50, 50}, {0, 0, 12, 12}, {3, 3, 3, 3}};
+ const double scales[] = {1, 1.25, 1.5, 2};
+ for (size_t si = 0; si < sizeof(sizes) / sizeof(sizes[0]); ++si)
+ for (size_t ri = 0; ri < sizeof(radii) / sizeof(radii[0]); ++ri)
+ for (size_t k = 0; k < sizeof(scales) / sizeof(scales[0]); ++k) {
+ const struct rediwm_clip_spec spec = {
+ .width = sizes[si][0], .height = sizes[si][1],
+ .radii = {radii[ri][0], radii[ri][1], radii[ri][2], radii[ri][3]}};
+ // Authored at the output's scale, at a fractional origin: the space the
+ // pass evaluates the shape in.
+ const struct rediwm_xform t = {scales[k], 13.3, 7.8};
+ const struct rediwm_clip clip = rediwm_clip_resolve(t, 5.1, 3.7, &spec);
+ const struct rediwm_snap f = rediwm_edges_snap(clip.edges);
+ struct rediwm_snap rects[3];
+ const int n = rediwm_clip_opaque(&clip, rects);
+ assert(n >= 0 && n <= 3);
+ long claimed = 0;
+ for (int i = 0; i < n; i++) {
+ assert(rects[i].width > 0 && rects[i].height > 0);
+ assert(rects[i].x >= f.x && rects[i].y >= f.y &&
+ rects[i].x + rects[i].width <= f.x + f.width &&
+ rects[i].y + rects[i].height <= f.y + f.height);
+ for (int y = rects[i].y; y < rects[i].y + rects[i].height; y++)
+ for (int x = rects[i].x; x < rects[i].x + rects[i].width; x++) {
+ assert(rediwm_coverage(rediwm_sd_rounded_box(x + .5, y + .5, f.x, f.y, f.width,
+ f.height, clip.radii)) == 1);
+ claimed++;
+ }
+ }
+ // Rectangles never overlap: a pixel claimed twice would be counted twice
+ // by a consumer that sums areas.
+ for (int i = 0; i < n; i++)
+ for (int j = i + 1; j < n; j++)
+ assert(rects[i].x + rects[i].width <= rects[j].x || rects[j].x + rects[j].width <= rects[i].x ||
+ rects[i].y + rects[i].height <= rects[j].y || rects[j].y + rects[j].height <= rects[i].y);
+ long covered = 0;
+ for (int y = f.y; y < f.y + f.height; y++)
+ for (int x = f.x; x < f.x + f.width; x++)
+ covered += rediwm_coverage(rediwm_sd_rounded_box(x + .5, y + .5, f.x, f.y, f.width,
+ f.height, clip.radii)) == 1;
+ int inset[4];
+ for (int i = 0; i < 4; i++)
+ inset[i] = clip.radii[i] > 0 ? (int)ceil(clip.radii[i]) + REDIWM_CLIP_OPAQUE_MARGIN : 0;
+ const int top = inset[0] > inset[1] ? inset[0] : inset[1];
+ const int bottom = inset[2] > inset[3] ? inset[2] : inset[3];
+ // What it leaves out of the fully covered pixels is bounded by the corner
+ // squares and the strips beside them, or by the two corner columns when
+ // there is no room for a middle band; a window too small for its corners
+ // may claim nothing at all.
+ const int left = inset[0] > inset[3] ? inset[0] : inset[3];
+ const int right = inset[1] > inset[2] ? inset[1] : inset[2];
+ const long bound = top + bottom > f.height
+ ? (long)(left + right) * f.height
+ : (long)(inset[0] + inset[1]) * top + (long)(inset[2] + inset[3]) * bottom;
+ assert(covered - claimed <= bound);
+ if (n == 0) assert(top + bottom > f.height ? f.width - left - right <= 0
+ : f.width - inset[0] - inset[1] <= 0 && f.height == top + bottom);
+ if (radii[ri][0] + radii[ri][1] + radii[ri][2] + radii[ri][3] == 0) {
+ assert(n == 1 && rects[0].x == f.x && rects[0].y == f.y && rects[0].width == f.width &&
+ rects[0].height == f.height); // Square corners lose nothing.
+ }
+ }
+ // A window too small for its corners claims nothing rather than guessing, and a
+ // pill keeps the full-height column between its two semicircles.
+ struct rediwm_snap one[3];
+ const struct rediwm_xform identity = {1, 0, 0};
+ assert(rediwm_clip_opaque(&(struct rediwm_clip){rediwm_xform_rect(identity, 0, 0, 8, 8), {4, 4, 4, 4}, 0}, one) == 0);
+ assert(rediwm_clip_opaque(&(struct rediwm_clip){rediwm_xform_rect(identity, 5, 7, 120, 80), {40, 40, 40, 40}, 0}, one) == 1);
+ assert(one[0].x == 5 + 42 && one[0].y == 7 && one[0].width == 120 - 84 && one[0].height == 80);
+}
+static void test_clip_projection(struct wlr_scene *scene) {
+ struct wlr_scene_tree *source = wlr_scene_tree_create(&scene->tree);
+ struct wlr_scene_tree *view = wlr_scene_tree_create(&scene->tree);
+ struct rediwm_projection *p = rediwm_projection_create(source, view, scene);
+ assert(p);
+ struct wlr_scene_tree *frame = wlr_scene_tree_create(source);
+ struct wlr_scene_tree *nested = wlr_scene_tree_create(frame);
+ struct wlr_scene_tree *other = wlr_scene_tree_create(source);
+ float color[4] = {.2, .3, .1, 1};
+ struct wlr_scene_rect *border = wlr_scene_rect_create(frame, 17, 9, color);
+ struct wlr_buffer *buffer = buffer_create(), *glass_buffer = buffer_create(), *title_buffer = buffer_create();
+ struct wlr_scene_buffer *client = wlr_scene_buffer_create(nested, buffer);
+ struct wlr_scene_buffer *title = wlr_scene_buffer_create(frame, title_buffer);
+ struct wlr_scene_buffer *glass = wlr_scene_buffer_create(frame, glass_buffer);
+ struct wlr_scene_rect *overlay = wlr_scene_rect_create(other, 20, 20, color);
+ assert(frame && nested && other && border && client && title && glass && overlay);
+ wlr_scene_node_set_position(&frame->node, 37, 29);
+ wlr_scene_node_set_position(&nested->node, 2, 12);
+ wlr_scene_buffer_set_dest_size(client, 100, 60);
+ wlr_scene_buffer_set_dest_size(title, 100, 12);
+ wlr_scene_buffer_set_dest_size(glass, 100, 12);
+ effect_node = &glass->node;
+ assert(rediwm_projection_set_tree_zoom(&frame->node, .85));
+ assert(rediwm_projection_set_tree_scale(&nested->node, .5));
+
+ // Nothing is clipped until a tree asks.
+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);
+ struct leaf *border_leaf = get_leaf(p, &border->node), *client_leaf = get_leaf(p, &client->node),
+ *title_leaf = get_leaf(p, &title->node), *glass_leaf = get_leaf(p, &glass->node),
+ *overlay_leaf = get_leaf(p, &overlay->node);
+ assert(!border_leaf->item.clip && !client_leaf->item.clip && !overlay_leaf->item.clip);
+
+ // Every leaf under the tree carries the one shape: the frame's rectangle after
+ // the window zoom about its origin, not changed by the inner tree's own scale.
+ struct rediwm_clip_spec spec = {.width = 120, .height = 80, .radii = {10, 10, 6, 6}, .border = 1};
+ assert(rediwm_projection_set_tree_clip(&frame->node, &spec));
+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);
+ const struct rediwm_xform zoomed = rediwm_xform_scale_about((struct rediwm_xform){1, 0, 0}, .85, 37, 29);
+ const struct rediwm_clip want = rediwm_clip_resolve(zoomed, 37, 29, &spec);
+ struct leaf *clipped[] = {border_leaf, client_leaf, title_leaf, glass_leaf};
+ for (size_t i = 0; i < sizeof(clipped) / sizeof(clipped[0]); ++i) {
+ const struct rediwm_clip *c = clipped[i]->item.clip;
+ assert(c == &clipped[i]->clip);
+ assert(near(c->edges.x1, want.edges.x1) && near(c->edges.y1, want.edges.y1) &&
+ near(c->edges.x2, want.edges.x2) && near(c->edges.y2, want.edges.y2));
+ assert(near(c->radii[0], 10 * .85) && near(c->radii[3], 6 * .85) && near(c->border, .85));
+ }
+ assert(near(want.edges.x1, 37) && near(want.edges.x2, 37 + .85 * 120)); // The origin stays put.
+ assert(!overlay_leaf->item.clip); // A sibling window is not clipped.
+ // It follows the camera with the window.
+ resolve(p, .5, 12, -8, 3, 4, NULL);
+ const struct rediwm_xform moved = rediwm_xform_scale_about(rediwm_xform_camera(.5, 12, -8, 3, 4), .85, 37, 29);
+ const struct rediwm_clip want_moved = rediwm_clip_resolve(moved, 37, 29, &spec);
+ assert(near(client_leaf->item.clip->edges.x1, want_moved.edges.x1) &&
+ near(client_leaf->item.clip->edges.y2, want_moved.edges.y2) &&
+ near(client_leaf->item.clip->radii[0], 10 * .85 * .5));
+ resolve(p, 1, 0, 0, 0, 0, NULL);
+
+ // The graph consumer sees the pointer too, and a glass effect takes the shape
+ // of the titlebar it sits under, in storage of its own.
+ // Glass works on the projected nodes, as the compositor calls it.
+ rediwm_projection_effect_updated(wlr_scene_buffer_from_node(glass_leaf->view),
+ wlr_scene_buffer_from_node(title_leaf->view));
+ assert(glass_leaf->effect_item.clip == &glass_leaf->effect_clip);
+ assert(glass_leaf->effect_item.clip != title_leaf->item.clip);
+ assert(near(glass_leaf->effect_item.clip->edges.x1, want.edges.x1) &&
+ near(glass_leaf->effect_item.clip->radii[1], 10 * .85));
+
+ // A popup lives under its parent's tree but is no part of the window's shape;
+ // its siblings, such as subsurfaces, still are. The barrier can be lifted.
+ struct wlr_scene_tree *subsurface = wlr_scene_tree_create(nested);
+ struct wlr_scene_tree *popup = wlr_scene_tree_create(nested);
+ struct wlr_buffer *popup_buffer = buffer_create(), *sub_buffer = buffer_create();
+ struct wlr_scene_buffer *popup_content = wlr_scene_buffer_create(popup, popup_buffer);
+ struct wlr_scene_buffer *sub_content = wlr_scene_buffer_create(subsurface, sub_buffer);
+ assert(popup_content && sub_content);
+ wlr_scene_node_set_position(&popup->node, 90, 70); // Hanging outside the 120x80 frame.
+ assert(rediwm_projection_set_tree_unclipped(&popup->node, true));
+ assert(rediwm_projection_set_tree_unclipped(&popup->node, true)); // Idempotent.
+ resolve(p, 1, 0, 0, 0, 0, NULL);
+ struct leaf *popup_leaf = get_leaf(p, &popup_content->node), *sub_leaf = get_leaf(p, &sub_content->node);
+ assert(!popup_leaf->item.clip && sub_leaf->item.clip && client_leaf->item.clip);
+ assert(rediwm_projection_set_tree_unclipped(&popup->node, false));
+ assert(rediwm_projection_set_tree_unclipped(&popup->node, false)); // Nothing to lift is fine.
+ resolve(p, 1, 0, 0, 0, 0, NULL);
+ assert(popup_leaf->item.clip);
+ assert(rediwm_projection_set_tree_unclipped(&popup->node, true));
+ // A barrier does not hide a clip the subtree sets for itself.
+ struct rediwm_clip_spec own = {.width = 50, .height = 40, .radii = {3, 3, 3, 3}};
+ assert(rediwm_projection_set_tree_clip(&popup->node, &own));
+ resolve(p, 1, 0, 0, 0, 0, NULL);
+ assert(popup_leaf->item.clip && near(popup_leaf->item.clip->edges.x2 - popup_leaf->item.clip->edges.x1, .85 * .5 * 50));
+ assert(rediwm_projection_set_tree_clip(&popup->node, NULL));
+ resolve(p, 1, 0, 0, 0, 0, NULL);
+ assert(!popup_leaf->item.clip);
+ assert(rediwm_projection_set_tree_unclipped(&popup->node, false));
+
+ // An inner clipped tree shapes its own leaves only.
+ struct rediwm_clip_spec inner_spec = {.width = 30, .height = 20, .radii = {4, 4, 4, 4}};
+ assert(rediwm_projection_set_tree_clip(&nested->node, &inner_spec));
+ resolve(p, 1, 0, 0, 0, 0, NULL);
+ assert(near(client_leaf->item.clip->edges.x2 - client_leaf->item.clip->edges.x1, .85 * .5 * 30));
+ assert(near(title_leaf->item.clip->edges.x2 - title_leaf->item.clip->edges.x1, .85 * 120));
+ assert(rediwm_projection_set_tree_clip(&nested->node, NULL));
+
+ // A bad shape is refused and leaves the old one in force; NULL removes it.
+ const struct rediwm_clip_spec bad[] = {
+ {.width = 0, .height = 80}, {.width = 120, .height = -1}, {.width = 120, .height = 80, .border = -1},
+ {.width = 120, .height = 80, .radii = {1, -1, 1, 1}}, {.width = 120, .height = 80, .radii = {NAN, 1, 1, 1}},
+ {.width = 120, .height = 80, .border = INFINITY}};
+ for (size_t i = 0; i < sizeof(bad) / sizeof(bad[0]); ++i) assert(!rediwm_projection_set_tree_clip(&frame->node, &bad[i]));
+ resolve(p, 1, 0, 0, 0, 0, NULL);
+ assert(near(client_leaf->item.clip->edges.x2, want.edges.x2));
+ spec.width = 200;
+ assert(rediwm_projection_set_tree_clip(&frame->node, &spec));
+ resolve(p, 1, 0, 0, 0, 0, NULL);
+ assert(near(client_leaf->item.clip->edges.x2, 37 + .85 * 200));
+ assert(rediwm_projection_set_tree_clip(&frame->node, NULL));
+ assert(rediwm_projection_set_tree_clip(&frame->node, NULL)); // Removing nothing is fine.
+ resolve(p, 1, 0, 0, 0, 0, NULL);
+ assert(!client_leaf->item.clip && !border_leaf->item.clip);
+
+ // Presenting a clipped graph changes nothing about the view tree yet: the
+ // shape reaches no pixel until the output pass draws it.
+ assert(rediwm_projection_set_tree_clip(&frame->node, &spec));
+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);
+ assert_presented(p);
+ // Destroying the clipped tree frees its addon with it.
+ effect_node = NULL;
+ wlr_scene_node_destroy(&frame->node);
+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);
+ wlr_scene_node_destroy(&view->node);
+ rediwm_projection_destroy(p);
+ wlr_scene_node_destroy(&source->node);
+ wlr_buffer_drop(buffer);
+ wlr_buffer_drop(glass_buffer);
+ wlr_buffer_drop(title_buffer);
+ wlr_buffer_drop(popup_buffer);
+ wlr_buffer_drop(sub_buffer);
+}
// A client surface's opaque region has to reach its view, or nothing below the
// window is ever culled. The buffers here reveal no format, like a
// wlr_client_buffer, so only the node's own region can say they are opaque.
@@ -639,6 +898,9 @@ int main(void) {
rediwm_projection_destroy(p);
test_groups(scene, out);
test_graph(scene);
+ test_clip_geometry();
+ test_clip_opaque();
+ test_clip_projection(scene);
test_opaque(scene);
wlr_scene_node_destroy(&scene->tree.node);
wlr_buffer_drop(a);
diff --git a/src/scene_graph.h b/src/scene_graph.h
index c1318c6..2778f71 100644
--- a/src/scene_graph.h
+++ b/src/scene_graph.h
@@ -67,6 +67,96 @@ static inline struct rediwm_snap rediwm_edges_snap(struct rediwm_edges e) {
return (struct rediwm_snap){x, y, (int)lround(e.x2) - x, (int)lround(e.y2) - y};
}
+// Stage 4 of docs/plans/scene-graph.md: one rounded-rectangle shape clips every
+// leaf of a tree (titlebar, client, subsurfaces). `rediwm_clip_spec` is what the
+// compositor sets on the tree, in source units and anchored at the tree's origin;
+// `rediwm_clip` is the same shape in layout space as resolve hands it on. A
+// presenter snaps `edges` like any item's, so the shape and the quads that tile
+// it agree on pixel columns, and evaluates the distance in device pixels.
+struct rediwm_clip_spec {
+ int width, height;
+ // Top-left, top-right, bottom-right, bottom-left.
+ double radii[4];
+ double border;
+};
+struct rediwm_clip {
+ struct rediwm_edges edges;
+ double radii[4];
+ double border;
+};
+// Radii are capped at half the short side before scaling, as the CPU chrome
+// does, so a small window never asks for an impossible arc.
+static inline struct rediwm_clip rediwm_clip_resolve(struct rediwm_xform t, double x,
+ double y,
+ const struct rediwm_clip_spec *spec) {
+ const double cap = 0.5 * (spec->width < spec->height ? spec->width : spec->height);
+ struct rediwm_clip clip = {
+ .edges = rediwm_xform_rect(t, x, y, spec->width, spec->height),
+ .border = spec->border * t.scale};
+ for (int i = 0; i < 4; i++)
+ clip.radii[i] = (spec->radii[i] < cap ? spec->radii[i] : cap) * t.scale;
+ return clip;
+}
+// Signed distance, negative inside, from (px, py) to the rounded rectangle
+// (x, y, w, h). The radius is picked by quadrant: the stage 1 shader's formula,
+// which `ui/sdf.zig` computes for one radius.
+static inline double rediwm_sd_rounded_box(double px, double py, double x, double y,
+ double w, double h, const double radii[4]) {
+ const double qx = px - (x + w / 2), qy = py - (y + h / 2);
+ const double r = qx < 0 ? (qy < 0 ? radii[0] : radii[3]) : (qy < 0 ? radii[1] : radii[2]);
+ const double dx = fabs(qx) - (w / 2 - r), dy = fabs(qy) - (h / 2 - r);
+ const double ox = dx > 0 ? dx : 0, oy = dy > 0 ? dy : 0;
+ const double inner = dx > dy ? dx : dy;
+ return sqrt(ox * ox + oy * oy) + (inner < 0 ? inner : 0) - r;
+}
+// Pixel coverage at signed distance `d` from an edge: a one-pixel ramp centred on it.
+static inline double rediwm_coverage(double d) {
+ return d >= .5 ? 0 : d <= -.5 ? 1 : .5 - d;
+}
+static inline double rediwm_clip_distance(const struct rediwm_clip *clip, double px,
+ double py) {
+ return rediwm_sd_rounded_box(px, py, clip->edges.x1, clip->edges.y1,
+ clip->edges.x2 - clip->edges.x1,
+ clip->edges.y2 - clip->edges.y1, clip->radii);
+}
+// A conservative cover of the pixels the shape fully covers, on the grid of the
+// space the clip is in: its snapped rectangle minus a square at each corner two
+// pixels deeper than the radius. The anti-aliased arc lies inside the radius
+// square; the margin keeps it, and the outward rounding of a region converted to
+// device pixels at any output scale of 1 or more, out of the claim. Culling and
+// blend decisions may only trust what is returned. Up to three rectangles: a
+// strip between the top corners, the full-width middle, a strip between the
+// bottom corners. When the corners leave no room for a middle (a pill, or a
+// small window with large radii) it is the one full-height column between the
+// left and right corners instead. Returns how many, 0 when nothing is safe.
+enum { REDIWM_CLIP_OPAQUE_MARGIN = 2 };
+static inline int rediwm_clip_opaque(const struct rediwm_clip *clip,
+ struct rediwm_snap out[3]) {
+ const struct rediwm_snap f = rediwm_edges_snap(clip->edges);
+ if (f.width <= 0 || f.height <= 0) return 0;
+ int inset[4];
+ for (int i = 0; i < 4; i++)
+ inset[i] = clip->radii[i] > 0 ? (int)ceil(clip->radii[i]) + REDIWM_CLIP_OPAQUE_MARGIN : 0;
+ const int top = inset[0] > inset[1] ? inset[0] : inset[1];
+ const int bottom = inset[2] > inset[3] ? inset[2] : inset[3];
+ if (top + bottom > f.height) {
+ const int left = inset[0] > inset[3] ? inset[0] : inset[3];
+ const int right = inset[1] > inset[2] ? inset[1] : inset[2];
+ if (f.width - left - right <= 0) return 0;
+ out[0] = (struct rediwm_snap){f.x + left, f.y, f.width - left - right, f.height};
+ return 1;
+ }
+ int n = 0;
+ if (top > 0 && f.width - inset[0] - inset[1] > 0)
+ out[n++] = (struct rediwm_snap){f.x + inset[0], f.y, f.width - inset[0] - inset[1], top};
+ if (f.height - top - bottom > 0)
+ out[n++] = (struct rediwm_snap){f.x, f.y + top, f.width, f.height - top - bottom};
+ if (bottom > 0 && f.width - inset[3] - inset[2] > 0)
+ out[n++] = (struct rediwm_snap){f.x + inset[3], f.y + f.height - bottom,
+ f.width - inset[3] - inset[2], bottom};
+ return n;
+}
+
enum rediwm_item_kind {
REDIWM_ITEM_RECT, // a solid colour
REDIWM_ITEM_SURFACE, // the buffer of a client wl_surface
@@ -118,4 +208,7 @@ struct rediwm_item {
double scale;
struct rediwm_edges edges;
struct rediwm_content content;
+ // The shape clipping this item with the rest of its tree, or NULL. Owned by the
+ // leaf the item belongs to: valid for the callback, until the next resolve.
+ const struct rediwm_clip *clip;
};