Skip to content

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

scene graph plan, stage 5a: damage attribution in the graph

oxydizer8c97371aa61b
diff --git a/docs/plans/scene-graph-stage5.md b/docs/plans/scene-graph-stage5.md
index 4a7abd4..1f76d39 100644
--- a/docs/plans/scene-graph-stage5.md
+++ b/docs/plans/scene-graph-stage5.md
@@ -1,6 +1,6 @@
# Scene graph plan, stage 5: backdrop effects in the pass
-Status: **planned, decisions taken (all as recommended), nothing implemented**, 2026-10-11. Stage 4 of
+Status: **5a done, 5b next**, 2026-10-11. Decisions taken (all as recommended). Stage 4 of
[scene-graph.md](scene-graph.md) is done (window shapes, analytic shadows, the
owned pass as the only presenter). This stage moves glass, the blurred backdrop
behind titlebars, the taskbar and the start and power menus, from its side
@@ -121,11 +121,26 @@ a shaped-program mode, one blur program, plain programs dropped if free.
Each leaves a working compositor and its own correctness gates; cost is judged
once, after 5e (see [scene-graph-stage4.md](scene-graph-stage4.md), Measurement).
-* **5a. Effect items and damage attribution in the graph.** An effect item carries
- its kind, sigma, saturation, opacity, target link and padded extent; resolve
- records per-item damage in layout space (the input to D1 (B)). Pure tests in
- `projection_test.c`: below/above attribution, geometry changes, hidden and
- destroyed items.
+* **5a. Damage attribution in the graph.** Every leaf has a stable id
+ (`item.id`). `present` appends a damage event (leaf id, layout-space box) to a
+ ring the projection owns whenever a leaf's pixels change: commit or owned-buffer
+ damage (mapped from buffer coordinates; the whole box under a non-normal
+ transform or without damage), a changed box, content or visibility (old and new
+ box), a shadow footprint, an unreached or destroyed leaf (its last box). Each
+ consumer, one per output, reads the events since its own cursor
+ (`rediwm_projection_damage_events`); a cursor older than the ring reports the
+ loss, and the consumer treats everything as changed. In 5c an output attributes
+ an event to "below an effect" when its leaf is painted below the effect or is
+ no longer in the frame, and treats damage no event explains (the taskbar,
+ wallpaper, overlays, clip rims) as below too, so attribution can only err
+ towards re-blurring. Pure tests in `projection_test.c`.
+ Effect items gain their parameters (kind, sigma, saturation, target) in 5c,
+ where they stop being glass-rendered buffers.
+ *Done.* `item.id`, the event ring (4096 events) and
+ `rediwm_projection_damage_events`/`_cursor`; `test_damage_events` covers
+ appearance, mapped and unreported buffer damage, moves, hiding, restyles,
+ destruction, two consumers and a consumer that falls behind. Content is
+ compared per leaf per sync to tell a restyle (about fifteen field compares).
* **5b. Backdrop primitives in `scene_pass`.** Region copy from the pass's
framebuffer, the two quarter-size blur passes (unscissored), the composite
through the shape with the target's alpha. Gates in `test-scene-pass`: the
diff --git a/src/projection.c b/src/projection.c
index fbf7e82..56a0f38 100644
--- a/src/projection.c
+++ b/src/projection.c
@@ -21,6 +21,12 @@
// content the source still owns: buffers, wait points, opaque regions), rounds,
// and applies the result to the view tree. The graph is the one place geometry
// is computed; present() may not derive any of its own.
+// Damage events: which leaf changed which layout-space box, for consumers that
+// must tell what lies below a point in paint order from what lies above it
+// (backdrop effects, stage 5 of docs/plans/scene-graph.md). A ring: each consumer
+// keeps its own cursor, and one that falls behind by more than the ring holds is
+// told so and must treat everything as changed.
+enum { DAMAGE_EVENTS = 4096 };
struct rediwm_projection {
struct wlr_scene_tree *source, *view;
struct wlr_scene *scene;
@@ -29,6 +35,9 @@ struct rediwm_projection {
struct wl_list leaves, groups, paint;
struct wl_listener new_surface;
struct wl_list surfaces;
+ uint64_t next_leaf_id;
+ struct rediwm_damage_event events[DAMAGE_EVENTS];
+ uint64_t next_event; // sequence number of the next event; 0 is never used
};
// Source top-level subtrees are contiguous in paint order. Moving their
// presentation tree updates wlroots visibility once for the whole window,
@@ -102,6 +111,10 @@ struct leaf {
// shadow's view has no buffer, so wlroots damages nothing for it: present()
// damages the footprint it last drew (`shadow_drawn`) and the new one.
struct rediwm_shadow shadow;
+ // The box present() last showed the leaf in, for its damage events.
+ struct rediwm_snap shown;
+ struct rediwm_content shown_content;
+ bool has_shown;
struct rediwm_shadow shadow_drawn_as;
struct rediwm_snap shadow_drawn;
float shadow_drawn_opacity;
@@ -357,8 +370,36 @@ static void mark_view_buffer(struct leaf *l, struct wlr_buffer *backing) {
client->private.n_ignore_locks++;
l->marked = client;
}
+static void emit(struct rediwm_projection *p, uint64_t leaf, int x, int y, int width, int height) {
+ if (width <= 0 || height <= 0) return;
+ if (!p->next_event) p->next_event = 1;
+ p->events[p->next_event % DAMAGE_EVENTS] =
+ (struct rediwm_damage_event){.leaf = leaf, .box = {x, y, width, height}};
+ p->next_event++;
+}
+static void emit_snap(struct leaf *l, struct rediwm_snap s) {
+ emit(l->projection, l->item.id, s.x, s.y, s.width, s.height);
+}
+bool rediwm_projection_damage_events(struct rediwm_projection *p, uint64_t *cursor,
+ rediwm_damage_event_iterator fn, void *data) {
+ const uint64_t end = p->next_event ? p->next_event : 1;
+ const uint64_t oldest = end > DAMAGE_EVENTS ? end - DAMAGE_EVENTS : 1;
+ bool complete = *cursor >= oldest;
+ for (uint64_t seq = complete ? *cursor : oldest; seq < end; seq++)
+ fn(&p->events[seq % DAMAGE_EVENTS], data);
+ *cursor = end;
+ return complete;
+}
+uint64_t rediwm_projection_damage_cursor(struct rediwm_projection *p) {
+ return p->next_event ? p->next_event : 1;
+}
static void damage_shadow(struct leaf *l, bool show, struct rediwm_snap at);
+static void forget_shown(struct leaf *l) {
+ if (l->has_shown) emit_snap(l, l->shown);
+ l->has_shown = false;
+}
static void destroy_leaf(struct leaf *l) {
+ forget_shown(l);
damage_shadow(l, false, (struct rediwm_snap){0});
unmark_view_buffer(l);
route_surface(l, l->surface ? l->surface->buffer : NULL);
@@ -462,6 +503,7 @@ static struct leaf *get_leaf(struct rediwm_projection *p,
return NULL;
l->source = source;
l->projection = p;
+ l->item.id = ++p->next_leaf_id;
l->dirty = true;
l->full_damage = true;
pixman_region32_init(&l->damage);
@@ -709,8 +751,11 @@ static void damage_shadow(struct leaf *l, bool show, struct rediwm_snap at) {
pixman_region32_union_rect(&region, &region, l->shadow_drawn.x, l->shadow_drawn.y,
l->shadow_drawn.width, l->shadow_drawn.height);
if (show) pixman_region32_union_rect(&region, &region, at.x, at.y, at.width, at.height);
- if (pixman_region32_not_empty(&region))
+ if (pixman_region32_not_empty(&region)) {
rediwm_scene_damage_layout(l->projection->scene, &region);
+ const pixman_box32_t *b = pixman_region32_extents(&region);
+ emit(l->projection, l->item.id, b->x1, b->y1, b->x2 - b->x1, b->y2 - b->y1);
+ }
pixman_region32_fini(&region);
l->has_shadow_drawn = show;
l->shadow_drawn = at;
@@ -718,6 +763,34 @@ static void damage_shadow(struct leaf *l, bool show, struct rediwm_snap at) {
l->shadow_drawn_opacity = opacity;
}
+// The layout box a buffer-local damage region covers: its bounding box through
+// the source box onto the item's edges, one pixel wider for filtering. The whole
+// box without damage, or under a transform (conservative, like glass.c was).
+static void damage_event_for_buffer(struct leaf *l, const pixman_region32_t *damage,
+ int buffer_width, int buffer_height,
+ struct rediwm_snap snap) {
+ const struct rediwm_item *item = &l->item;
+ if (!damage || item->content.transform != WL_OUTPUT_TRANSFORM_NORMAL ||
+ buffer_width <= 0 || buffer_height <= 0) {
+ emit_snap(l, snap);
+ return;
+ }
+ if (!pixman_region32_not_empty(damage)) return;
+ const pixman_box32_t *b = pixman_region32_extents(damage);
+ struct wlr_fbox src = item->content.src_box;
+ if (wlr_fbox_empty(&src)) src = (struct wlr_fbox){0, 0, buffer_width, buffer_height};
+ const struct rediwm_edges *e = &item->edges;
+ const double sx = (e->x2 - e->x1) / src.width, sy = (e->y2 - e->y1) / src.height;
+ const int x1 = (int)floor(e->x1 + (b->x1 - src.x) * sx) - 1;
+ const int y1 = (int)floor(e->y1 + (b->y1 - src.y) * sy) - 1;
+ const int x2 = (int)ceil(e->x1 + (b->x2 - src.x) * sx) + 1;
+ const int y2 = (int)ceil(e->y1 + (b->y2 - src.y) * sy) + 1;
+ // Never beyond the leaf: damage outside its source box shows nowhere.
+ const int cx1 = x1 > snap.x ? x1 : snap.x, cy1 = y1 > snap.y ? y1 : snap.y;
+ const int cx2 = x2 < snap.x + snap.width ? x2 : snap.x + snap.width;
+ const int cy2 = y2 < snap.y + snap.height ? y2 : snap.y + snap.height;
+ emit(l->projection, item->id, cx1, cy1, cx2 - cx1, cy2 - cy1);
+}
// present(): graph -> view tree. Reads items and group origins, never the
// source's geometry; rounds, orders and applies, and tells wlroots about the
// buffers and damage that changed.
@@ -744,6 +817,21 @@ static void present_leaf(struct group *g, struct leaf *l,
next.height = snap.height;
next.enabled = item->enabled && snap.width > 0 && snap.height > 0;
next.content = item->content;
+ // Damage events: a moved, resized, shown, hidden or restyled leaf changes its
+ // old and its new box; otherwise only what a new buffer or commit damaged.
+ const bool restyled = !l->has_shown || !next.enabled || snap.x != l->shown.x ||
+ snap.y != l->shown.y || snap.width != l->shown.width ||
+ snap.height != l->shown.height ||
+ !rediwm_content_equal(&item->content, &l->shown_content);
+ if (restyled) {
+ forget_shown(l);
+ if (next.enabled) {
+ emit_snap(l, snap);
+ l->shown = snap;
+ l->shown_content = item->content;
+ l->has_shown = true;
+ }
+ }
if (item->kind == REDIWM_ITEM_RECT) {
if (!l->has_applied || !view_state_equal(&next, &l->applied)) {
struct wlr_scene_rect *v = wlr_scene_rect_from_node(l->view);
@@ -771,6 +859,10 @@ static void present_leaf(struct group *g, struct leaf *l,
.wait_timeline = b->private.wait_timeline,
.wait_point = b->private.wait_point,
};
+ if (!restyled && l->has_shown)
+ damage_event_for_buffer(l, options.damage,
+ l->surface ? l->damage_width : backing ? backing->width : 0,
+ l->surface ? l->damage_height : backing ? backing->height : 0, snap);
unmark_view_buffer(l);
wlr_scene_buffer_set_buffer_with_options(v, backing, &options);
mark_view_buffer(l, backing);
@@ -962,6 +1054,7 @@ static void present(struct rediwm_projection *p) {
}
struct leaf *l;
wl_list_for_each(l, &p->leaves, link) if (!l->seen) {
+ forget_shown(l);
damage_shadow(l, false, (struct rediwm_snap){0});
wlr_scene_node_set_enabled(l->view, false);
// The cached state no longer describes the view; reapply on return.
diff --git a/src/projection.h b/src/projection.h
index 5efa1c0..a6995cf 100644
--- a/src/projection.h
+++ b/src/projection.h
@@ -1,5 +1,6 @@
#pragma once
#include <stdbool.h>
+#include <stdint.h>
struct wlr_scene_node;
struct wlr_scene_tree;
struct wlr_scene;
@@ -27,6 +28,19 @@ struct rediwm_projection *rediwm_projection_create(struct wlr_scene_tree *,
void rediwm_projection_destroy(struct rediwm_projection *);
void rediwm_projection_sync(struct rediwm_projection *, double, double, double,
double, double);
+// A leaf's pixels changed in `box` (layout space) during a sync. `leaf` is the
+// item's `id`; a leaf that is gone or hidden reports its last box.
+struct rediwm_damage_event {
+ uint64_t leaf;
+ struct { int x, y, width, height; } box;
+};
+typedef void (*rediwm_damage_event_iterator)(const struct rediwm_damage_event *, void *);
+// Calls `fn` for every event since `*cursor` and moves the cursor past them. A
+// consumer starts from rediwm_projection_damage_cursor(). Returns false when
+// events were lost (the cursor fell behind the ring): treat everything as changed.
+bool rediwm_projection_damage_events(struct rediwm_projection *, uint64_t *cursor,
+ rediwm_damage_event_iterator, void *);
+uint64_t rediwm_projection_damage_cursor(struct rediwm_projection *);
// Optional root subtree presented in layout coordinates, independent of the camera.
void rediwm_projection_sync_fixed(struct rediwm_projection *, double, double, double,
double, double, struct wlr_scene_tree *);
diff --git a/src/projection_test.c b/src/projection_test.c
index e73ceac..f5b450d 100644
--- a/src/projection_test.c
+++ b/src/projection_test.c
@@ -699,6 +699,109 @@ static void test_shadow_output(struct wlr_scene *scene, struct wlr_scene_output
rediwm_projection_destroy(p);
wlr_scene_node_destroy(&source->node);
}
+// Stage 5a: damage events name the leaf and the layout box its pixels changed in,
+// for consumers that must tell what lies below a backdrop effect from what lies
+// above it. Each consumer reads through its own cursor.
+struct events_seen { int count; struct rediwm_damage_event last[16]; };
+static void collect_event(const struct rediwm_damage_event *e, void *data) {
+ struct events_seen *seen = data;
+ if (seen->count < 16) seen->last[seen->count] = *e;
+ seen->count++;
+}
+static struct events_seen events_since(struct rediwm_projection *p, uint64_t *cursor, bool *complete) {
+ struct events_seen seen = {0};
+ bool ok = rediwm_projection_damage_events(p, cursor, collect_event, &seen);
+ if (complete) *complete = ok;
+ return seen;
+}
+static bool event_is(const struct rediwm_damage_event *e, uint64_t leaf, int x, int y, int w, int h) {
+ return e->leaf == leaf && e->box.x == x && e->box.y == y && e->box.width == w && e->box.height == h;
+}
+static void test_damage_events(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);
+ uint64_t cursor = rediwm_projection_damage_cursor(p), other = cursor;
+ struct wlr_scene_tree *window = wlr_scene_tree_create(source);
+ struct wlr_buffer *buffer = buffer_create(); // 200x120
+ struct wlr_scene_buffer *a = wlr_scene_buffer_create(window, buffer);
+ struct wlr_scene_buffer *b = wlr_scene_buffer_create(window, buffer);
+ wlr_scene_node_set_position(&window->node, 40, 30);
+ wlr_scene_buffer_set_dest_size(a, 100, 60); // half size: one buffer px = 0.5
+ wlr_scene_node_set_position(&b->node, 0, 100);
+ wlr_scene_buffer_set_dest_size(b, 20, 20);
+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);
+ struct leaf *la = get_leaf(p, &a->node), *lb = get_leaf(p, &b->node);
+ assert(la->item.id && lb->item.id && la->item.id != lb->item.id);
+
+ // Appearing: each leaf's whole box.
+ struct events_seen seen = events_since(p, &cursor, NULL);
+ assert(seen.count == 2);
+ assert(event_is(&seen.last[0], la->item.id, 40, 30, 100, 60));
+ assert(event_is(&seen.last[1], lb->item.id, 40, 130, 20, 20));
+ rediwm_projection_sync(p, 1, 0, 0, 0, 0); // Nothing changed.
+ assert(events_since(p, &cursor, NULL).count == 0);
+
+ // Reported buffer damage maps through the scale, a pixel wider for filtering.
+ pixman_region32_t damage;
+ pixman_region32_init_rect(&damage, 20, 40, 10, 10);
+ rediwm_projection_damage_buffer(p, &a->node, &damage);
+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);
+ seen = events_since(p, &cursor, NULL);
+ assert(seen.count == 1 && event_is(&seen.last[0], la->item.id, 49, 49, 7, 7));
+ // Unreported damage is the whole box.
+ rediwm_projection_damage_buffer(p, &a->node, NULL);
+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);
+ seen = events_since(p, &cursor, NULL);
+ assert(seen.count == 1 && event_is(&seen.last[0], la->item.id, 40, 30, 100, 60));
+
+ // A move changes the old and the new box; a hidden leaf its last one.
+ wlr_scene_node_set_position(&b->node, 10, 100);
+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);
+ seen = events_since(p, &cursor, NULL);
+ assert(seen.count == 2);
+ assert(event_is(&seen.last[0], lb->item.id, 40, 130, 20, 20));
+ assert(event_is(&seen.last[1], lb->item.id, 50, 130, 20, 20));
+ wlr_scene_node_set_enabled(&b->node, false);
+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);
+ seen = events_since(p, &cursor, NULL);
+ assert(seen.count == 1 && event_is(&seen.last[0], lb->item.id, 50, 130, 20, 20));
+ wlr_scene_node_set_enabled(&b->node, true);
+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);
+ seen = events_since(p, &cursor, NULL);
+ assert(seen.count == 1 && event_is(&seen.last[0], lb->item.id, 50, 130, 20, 20));
+ // A changed opacity is a restyle of the whole box.
+ wlr_scene_buffer_set_opacity(a, .5f);
+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);
+ seen = events_since(p, &cursor, NULL);
+ assert(seen.count == 2 && event_is(&seen.last[1], la->item.id, 40, 30, 100, 60));
+ // A destroyed leaf reports its last box at once.
+ const uint64_t gone = lb->item.id;
+ wlr_scene_node_destroy(&b->node);
+ seen = events_since(p, &cursor, NULL);
+ assert(seen.count == 1 && event_is(&seen.last[0], gone, 50, 130, 20, 20));
+
+ // A second consumer read nothing yet and sees everything since its cursor.
+ bool complete = false;
+ seen = events_since(p, &other, &complete);
+ assert(complete && seen.count == 11);
+ // One that falls behind the ring is told so.
+ uint64_t stale = rediwm_projection_damage_cursor(p);
+ for (int i = 0; i < 2100; i++) {
+ wlr_scene_node_set_position(&window->node, 40 + i % 2, 30);
+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);
+ }
+ events_since(p, &stale, &complete);
+ assert(!complete);
+ assert(events_since(p, &stale, &complete).count == 0 && complete);
+
+ pixman_region32_fini(&damage);
+ wlr_scene_node_destroy(&view->node);
+ rediwm_projection_destroy(p);
+ wlr_scene_node_destroy(&source->node);
+ wlr_buffer_drop(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.
@@ -1063,6 +1166,7 @@ int main(void) {
test_clip_projection(scene);
test_clip_output(scene, out);
test_shadow_output(scene, out);
+ test_damage_events(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 6ba588c..d461304 100644
--- a/src/scene_graph.h
+++ b/src/scene_graph.h
@@ -247,6 +247,9 @@ rediwm_filter_at_scale(double scale, enum wlr_scale_filter_mode requested) {
// One leaf of the resolved graph.
struct rediwm_item {
enum rediwm_item_kind kind;
+ // Stable for the leaf's life, never 0 for a projected leaf; 0 for the items of
+ // nodes outside the projection. Damage events name leaves by it.
+ uint64_t id;
// Every node from the leaf to the root of its group is enabled. Whether the
// item has any pixels once rounded is the presenter's to add.
bool enabled;