Skip to content

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

Stage 7a: own scene node core with unit tests

oxydizer3f8e10eb9d90
diff --git a/build.zig b/build.zig
index a7fc220..7601b2b 100644
--- a/build.zig
+++ b/build.zig
@@ -693,6 +693,19 @@ pub fn build(b: *std.Build) void {
test_step.dependOn(&run_gpu_timing_tests.step);
b.step("test-gpu-timing", "Test GPU timing query lifetime with a fake driver").dependOn(&run_gpu_timing_tests.step);
+ const node_tests = b.addExecutable(.{
+ .name = "node-tests",
+ .root_module = b.createModule(.{ .target = target, .optimize = .Debug, .link_libc = true }),
+ });
+ node_tests.root_module.addCSourceFile(.{ .file = b.path("src/node_test.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });
+ node_tests.root_module.addCSourceFile(.{ .file = b.path("src/node.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });
+ inline for (.{ "wlroots-0.20", "wayland-server", "pixman-1" }) |lib| {
+ node_tests.root_module.linkSystemLibrary(lib, .{});
+ }
+ const run_node_tests = b.addRunArtifact(node_tests);
+ test_step.dependOn(&run_node_tests.step);
+ b.step("test-node", "Test the compositor's own scene nodes").dependOn(&run_node_tests.step);
+
const projection_tests = b.addExecutable(.{
.name = "projection-tests",
.root_module = b.createModule(.{ .target = target, .optimize = .Debug, .link_libc = true }),
diff --git a/docs/plans/scene-graph-stage7.md b/docs/plans/scene-graph-stage7.md
index 9540890..518b4fb 100644
--- a/docs/plans/scene-graph-stage7.md
+++ b/docs/plans/scene-graph-stage7.md
@@ -1,6 +1,7 @@
# Scene graph, stage 7: retire the wlr_scene data model
-Status: draft 2026-10-15. Decisions D1-D5 below are recommendations, not taken.
+Status: D1-D4 taken as recommended and D5 (idle-CPU items first, done: 6179c18) on
+2026-10-15; 7a started on branch `stage7` (see "Findings from starting 7a").
## Why, and what it is not for
@@ -62,6 +63,51 @@ samples on its own).
colour manager presence), `output_sample`/frame-done helpers.
5. **Hit test and iteration** for the layout trees (`node_at`, `for_each_buffer`).
+## Findings from starting 7a (2026-10-15)
+
+Reading `projection.c`, `scene_output.c` and `scene_damage.c` before writing code
+showed that the sub-stages below understate 7a:
+
+* **Layout trees are not resolved by the projection.** Only the world's source
+ tree becomes leaves; the six layout trees (background, desktop, taskbar,
+ overlay, IME, lock) are still wlroots nodes that `visit_items` walks and whose
+ damage `wlr_scene_output` computes itself. The projection reports its own
+ damage into wlroots' ring through `scene_damage.c`
+ (`rediwm_scene_damage_layout`), and `scene_output.c` reads
+ `out->private.pending_commit_damage` and `damage_ring`. So own nodes have to
+ become projection sources too, as extra groups with an identity transform (the
+ `sync_fixed` path already does this for one subtree), or layout damage is lost.
+* **A mutation must ask for a frame.** The world's leaves hear their changes from
+ surface commits and listeners; a plain `setPosition` on a layout node was heard by
+ wlroots. The own mutators call the scene's `changed` hook for this
+ (`node.c`), and the projection finds what moved by comparing items at the next
+ sync, as it does for the world.
+* **`scene_output.c` depends on `wlr_scene_buffer` internals** (`private.texture`,
+ `renderer_destroy`, `own_buffer`, `wait_timeline`/`wait_point`,
+ `is_single_pixel_buffer`, `buffer_is_opaque`, `buffer_width/height`), so the own
+ buffer node has to carry the texture cache and wait points that
+ `wlr_scene_buffer_set_buffer_with_options` keeps, not just the public fields.
+* **More wlroots scene helpers in use** than the inventory listed:
+ `wlr.SceneLayerSurfaceV1` (`LayerSurface.zig`), `createSceneSubsurfaceTree` (4
+ sites: Toplevel, xwayland_unmanaged, text_input, client_lock).
+* **Mixed trees** are needed while windows still use wlr_scene_xdg_surface: the
+ `REDIWM_NODE_FOREIGN` kind holds a wlr subtree inside an own tree, hosted under
+ one hidden disabled wlr_scene root. The projection resolves it with its existing
+ wlr walk at the foreign node's accumulated position.
+
+Revised 7a order, each step gated by identical goldens and the suites that pass on HEAD:
+
+1. `node.h`/`node.c` and `node_test.c` (`zig build test-node`): the data model, the
+ change hook, stacking, picking, teardown. **Done.**
+2. The projection reads `rediwm_node` sources (leaf/group/addons on the own node),
+ resolves foreign subtrees, and takes the layout trees as extra groups with an
+ identity transform; damage and picking for them move out of wlr_scene.
+3. `scene_output.c` draws own buffers: texture cache, wait points, single-pixel
+ colour, `output_sample`; `glass.c` effect nodes become own buffer nodes.
+4. The Zig wrapper (`node.zig`, wlr_scene's method names) and the mechanical swap
+ of call sites, layout trees first, then the compositor-drawn UI; windows keep
+ `wlr.SceneTree` under a foreign node until 7b.
+
## Design
An own node module, C core with a thin Zig wrapper, living beside `scene_graph.h`:
diff --git a/src/node.c b/src/node.c
new file mode 100644
index 0000000..ab96a16
--- /dev/null
+++ b/src/node.c
@@ -0,0 +1,345 @@
+// Own scene nodes: see node.h.
+#define WLR_USE_UNSTABLE
+#include "node.h"
+#include <stdlib.h>
+#include <string.h>
+#include <wlr/types/wlr_buffer.h>
+
+static void changed(struct rediwm_node *node) {
+ struct rediwm_scene *scene = node->scene;
+ if (scene && scene->changed) scene->changed(scene->changed_data);
+}
+
+static void node_init(struct rediwm_node *node, enum rediwm_node_type type, struct rediwm_tree *parent) {
+ node->type = type;
+ node->parent = parent;
+ node->enabled = true;
+ node->x = node->y = 0;
+ node->data = NULL;
+ wl_signal_init(&node->events.destroy);
+ wlr_addon_set_init(&node->addons);
+ wl_list_init(&node->link);
+ if (parent) {
+ node->scene = parent->node.scene;
+ wl_list_insert(parent->children.prev, &node->link);
+ } else {
+ node->scene = NULL;
+ }
+}
+
+struct rediwm_scene *rediwm_scene_create(void (*notify)(void *), void *data) {
+ struct rediwm_scene *scene = calloc(1, sizeof(*scene));
+ if (!scene) return NULL;
+ node_init(&scene->tree.node, REDIWM_NODE_TREE, NULL);
+ wl_list_init(&scene->tree.children);
+ scene->tree.node.scene = scene;
+ scene->changed = notify;
+ scene->changed_data = data;
+ return scene;
+}
+
+void rediwm_scene_destroy(struct rediwm_scene *scene) {
+ if (!scene) return;
+ // Nothing may call back into an owner that is already going away.
+ scene->changed = NULL;
+ rediwm_node_destroy(&scene->tree.node);
+}
+
+struct rediwm_tree *rediwm_tree_create(struct rediwm_tree *parent) {
+ struct rediwm_tree *tree = calloc(1, sizeof(*tree));
+ if (!tree) return NULL;
+ node_init(&tree->node, REDIWM_NODE_TREE, parent);
+ wl_list_init(&tree->children);
+ changed(&tree->node);
+ return tree;
+}
+
+struct rediwm_rect *rediwm_rect_create(struct rediwm_tree *parent, int width, int height,
+ const float color[4]) {
+ struct rediwm_rect *rect = calloc(1, sizeof(*rect));
+ if (!rect) return NULL;
+ node_init(&rect->node, REDIWM_NODE_RECT, parent);
+ rect->width = width;
+ rect->height = height;
+ memcpy(rect->color, color, sizeof(rect->color));
+ changed(&rect->node);
+ return rect;
+}
+
+struct rediwm_buffer *rediwm_buffer_create(struct rediwm_tree *parent, struct wlr_buffer *buffer) {
+ struct rediwm_buffer *b = calloc(1, sizeof(*b));
+ if (!b) return NULL;
+ node_init(&b->node, REDIWM_NODE_BUFFER, parent);
+ wl_signal_init(&b->events.outputs_update);
+ wl_signal_init(&b->events.output_enter);
+ wl_signal_init(&b->events.output_leave);
+ wl_signal_init(&b->events.output_sample);
+ wl_signal_init(&b->events.frame_done);
+ b->opacity = 1;
+ b->filter_mode = WLR_SCALE_FILTER_BILINEAR;
+ b->transform = WL_OUTPUT_TRANSFORM_NORMAL;
+ pixman_region32_init(&b->opaque_region);
+ if (buffer) rediwm_buffer_set_buffer(b, buffer);
+ changed(&b->node);
+ return b;
+}
+
+struct rediwm_foreign *rediwm_foreign_create(struct rediwm_tree *parent, struct wlr_scene_node *wlr) {
+ struct rediwm_foreign *f = calloc(1, sizeof(*f));
+ if (!f) return NULL;
+ node_init(&f->node, REDIWM_NODE_FOREIGN, parent);
+ f->wlr = wlr;
+ changed(&f->node);
+ return f;
+}
+
+void rediwm_node_destroy(struct rediwm_node *node) {
+ if (!node) return;
+ wl_signal_emit_mutable(&node->events.destroy, NULL);
+ if (node->type == REDIWM_NODE_TREE) {
+ struct rediwm_tree *tree = (struct rediwm_tree *)node;
+ struct rediwm_node *child, *tmp;
+ wl_list_for_each_safe(child, tmp, &tree->children, link) rediwm_node_destroy(child);
+ } else if (node->type == REDIWM_NODE_BUFFER) {
+ struct rediwm_buffer *b = (struct rediwm_buffer *)node;
+ rediwm_buffer_set_buffer(b, NULL);
+ pixman_region32_fini(&b->opaque_region);
+ }
+ // Report before the node and its scene are gone; a root has no parent to tell.
+ const bool shown = node->parent != NULL;
+ struct rediwm_scene *scene = node->scene;
+ wlr_addon_set_finish(&node->addons);
+ wl_list_remove(&node->link);
+ free(node);
+ if (shown && scene && scene->changed) scene->changed(scene->changed_data);
+}
+
+void rediwm_node_set_enabled(struct rediwm_node *node, bool enabled) {
+ if (node->enabled == enabled) return;
+ node->enabled = enabled;
+ changed(node);
+}
+
+void rediwm_node_set_position(struct rediwm_node *node, int x, int y) {
+ if (node->x == x && node->y == y) return;
+ node->x = x;
+ node->y = y;
+ changed(node);
+}
+
+// Moves the node to follow `after` (a link of its parent's list, or the list head).
+static void move_after(struct rediwm_node *node, struct wl_list *after) {
+ wl_list_remove(&node->link);
+ wl_list_insert(after, &node->link);
+ changed(node);
+}
+
+void rediwm_node_raise_to_top(struct rediwm_node *node) {
+ if (!node->parent || node->link.next == &node->parent->children) return;
+ move_after(node, node->parent->children.prev);
+}
+
+void rediwm_node_lower_to_bottom(struct rediwm_node *node) {
+ if (!node->parent || node->link.prev == &node->parent->children) return;
+ move_after(node, &node->parent->children);
+}
+
+void rediwm_node_place_above(struct rediwm_node *node, struct rediwm_node *sibling) {
+ if (node == sibling || !node->parent || node->parent != sibling->parent ||
+ node->link.prev == &sibling->link)
+ return;
+ move_after(node, &sibling->link);
+}
+
+void rediwm_node_place_below(struct rediwm_node *node, struct rediwm_node *sibling) {
+ if (node == sibling || !node->parent || node->parent != sibling->parent ||
+ node->link.next == &sibling->link)
+ return;
+ move_after(node, sibling->link.prev);
+}
+
+static bool is_ancestor(const struct rediwm_node *ancestor, const struct rediwm_node *node) {
+ for (const struct rediwm_tree *t = node->parent; t; t = t->node.parent)
+ if (&t->node == ancestor) return true;
+ return false;
+}
+
+void rediwm_node_reparent(struct rediwm_node *node, struct rediwm_tree *parent) {
+ if (!node->parent || node->parent == parent) return;
+ if (&parent->node == node || is_ancestor(node, &parent->node)) return;
+ wl_list_remove(&node->link);
+ node->parent = parent;
+ wl_list_insert(parent->children.prev, &node->link);
+ // A different scene keeps its own owner informed from now on.
+ struct rediwm_scene *from = node->scene;
+ node->scene = parent->node.scene;
+ if (from && from != node->scene && from->changed) from->changed(from->changed_data);
+ changed(node);
+}
+
+bool rediwm_node_coords(struct rediwm_node *node, int *lx, int *ly) {
+ int x = 0, y = 0;
+ bool enabled = true;
+ for (struct rediwm_node *n = node; n; n = n->parent ? &n->parent->node : NULL) {
+ x += n->x;
+ y += n->y;
+ enabled = enabled && n->enabled;
+ }
+ if (!enabled) x = y = 0;
+ if (lx) *lx = x;
+ if (ly) *ly = y;
+ return enabled;
+}
+
+bool rediwm_node_visible(const struct rediwm_node *node) {
+ for (; node; node = node->parent ? &node->parent->node : NULL)
+ if (!node->enabled) return false;
+ return true;
+}
+
+void rediwm_rect_set_size(struct rediwm_rect *rect, int width, int height) {
+ if (rect->width == width && rect->height == height) return;
+ rect->width = width;
+ rect->height = height;
+ changed(&rect->node);
+}
+
+void rediwm_rect_set_color(struct rediwm_rect *rect, const float color[4]) {
+ if (!memcmp(rect->color, color, sizeof(rect->color))) return;
+ memcpy(rect->color, color, sizeof(rect->color));
+ changed(&rect->node);
+}
+
+void rediwm_buffer_set_buffer(struct rediwm_buffer *b, struct wlr_buffer *buffer) {
+ // A buffer set again is new content even if the pointer matches: a compositor-owned
+ // buffer is redrawn in place, and the projection is told its damage separately.
+ struct wlr_buffer *old = b->buffer;
+ b->buffer = buffer ? wlr_buffer_lock(buffer) : NULL;
+ if (buffer) {
+ b->buffer_width = buffer->width;
+ b->buffer_height = buffer->height;
+ } else {
+ b->buffer_width = b->buffer_height = 0;
+ }
+ if (old) wlr_buffer_unlock(old);
+ changed(&b->node);
+}
+
+void rediwm_buffer_set_dest_size(struct rediwm_buffer *b, int width, int height) {
+ if (b->dst_width == width && b->dst_height == height) return;
+ b->dst_width = width;
+ b->dst_height = height;
+ changed(&b->node);
+}
+
+void rediwm_buffer_set_source_box(struct rediwm_buffer *b, const struct wlr_fbox *box) {
+ const struct wlr_fbox none = {0};
+ const struct wlr_fbox *next = box ? box : &none;
+ if (!memcmp(&b->src_box, next, sizeof(*next))) return;
+ b->src_box = *next;
+ changed(&b->node);
+}
+
+void rediwm_buffer_set_opacity(struct rediwm_buffer *b, float opacity) {
+ if (b->opacity == opacity) return;
+ b->opacity = opacity;
+ changed(&b->node);
+}
+
+void rediwm_buffer_set_filter_mode(struct rediwm_buffer *b, enum wlr_scale_filter_mode mode) {
+ if (b->filter_mode == mode) return;
+ b->filter_mode = mode;
+ changed(&b->node);
+}
+
+void rediwm_buffer_set_transform(struct rediwm_buffer *b, enum wl_output_transform transform) {
+ if (b->transform == transform) return;
+ b->transform = transform;
+ changed(&b->node);
+}
+
+void rediwm_buffer_set_opaque_region(struct rediwm_buffer *b, const pixman_region32_t *region) {
+ if (!region) {
+ if (!pixman_region32_not_empty(&b->opaque_region)) return;
+ pixman_region32_clear(&b->opaque_region);
+ } else {
+ if (pixman_region32_equal((pixman_region32_t *)region, &b->opaque_region)) return;
+ pixman_region32_copy(&b->opaque_region, region);
+ }
+ changed(&b->node);
+}
+
+void rediwm_buffer_size(const struct rediwm_buffer *b, int *width, int *height) {
+ int w = b->dst_width, h = b->dst_height;
+ if (!w || !h) {
+ w = b->buffer_width;
+ h = b->buffer_height;
+ if (b->transform & 1) {
+ const int tmp = w;
+ w = h;
+ h = tmp;
+ }
+ }
+ if (width) *width = w;
+ if (height) *height = h;
+}
+
+struct rediwm_node *rediwm_node_at(struct rediwm_node *node, double x, double y, double *nx, double *ny) {
+ if (!node->enabled) return NULL;
+ x -= node->x;
+ y -= node->y;
+ switch (node->type) {
+ case REDIWM_NODE_TREE: {
+ struct rediwm_tree *tree = (struct rediwm_tree *)node;
+ for (struct wl_list *link = tree->children.prev; link != &tree->children; link = link->prev) {
+ struct rediwm_node *child = wl_container_of(link, child, link);
+ struct rediwm_node *hit = rediwm_node_at(child, x, y, nx, ny);
+ if (hit) return hit;
+ }
+ return NULL;
+ }
+ case REDIWM_NODE_RECT: {
+ struct rediwm_rect *rect = (struct rediwm_rect *)node;
+ if (x < 0 || y < 0 || x >= rect->width || y >= rect->height) return NULL;
+ break;
+ }
+ case REDIWM_NODE_BUFFER: {
+ struct rediwm_buffer *b = (struct rediwm_buffer *)node;
+ int width, height;
+ rediwm_buffer_size(b, &width, &height);
+ if (x < 0 || y < 0 || x >= width || y >= height) return NULL;
+ if (b->point_accepts_input && !b->point_accepts_input(b, &x, &y)) return NULL;
+ break;
+ }
+ case REDIWM_NODE_FOREIGN:
+ return NULL;
+ }
+ if (nx) *nx = x;
+ if (ny) *ny = y;
+ return node;
+}
+
+static void for_each_buffer(struct rediwm_node *node, int x, int y, rediwm_buffer_iterator fn, void *data) {
+ if (!node->enabled) return;
+ x += node->x;
+ y += node->y;
+ if (node->type == REDIWM_NODE_TREE) {
+ struct rediwm_node *child;
+ wl_list_for_each(child, &((struct rediwm_tree *)node)->children, link)
+ for_each_buffer(child, x, y, fn, data);
+ } else if (node->type == REDIWM_NODE_BUFFER) {
+ fn((struct rediwm_buffer *)node, x, y, data);
+ }
+}
+
+void rediwm_node_for_each_buffer(struct rediwm_node *node, rediwm_buffer_iterator fn, void *data) {
+ // Relative to the node passed in, as wlr_scene_node_for_each_buffer.
+ if (!node->enabled) return;
+ if (node->type == REDIWM_NODE_TREE) {
+ struct rediwm_node *child;
+ wl_list_for_each(child, &((struct rediwm_tree *)node)->children, link)
+ for_each_buffer(child, 0, 0, fn, data);
+ } else if (node->type == REDIWM_NODE_BUFFER) {
+ fn((struct rediwm_buffer *)node, 0, 0, data);
+ }
+}
diff --git a/src/node.h b/src/node.h
new file mode 100644
index 0000000..ebd19c7
--- /dev/null
+++ b/src/node.h
@@ -0,0 +1,165 @@
+// The compositor's own scene nodes (stage 7 of docs/plans/scene-graph-stage7.md).
+//
+// What wlr_scene gave the compositor as a data model: a tree of nodes with a
+// position, an enabled flag and an owner pointer, whose leaves are buffers or
+// rectangles. It does no drawing, damage or picking here; the projection resolves
+// the tree into items and the output pass draws them. Every mutator that can change
+// what the tree shows reports it through the scene's `changed` hook, which asks for
+// a frame; the projection then finds what moved by comparing items, as it does for
+// the world today.
+//
+// Field names follow wlr_scene on purpose: the projection and the pass read the
+// same members under the same names, and the Zig wrapper keeps wlr_scene's method
+// names, so a call site changes by type, not by shape.
+//
+// Single-threaded, like the rest of the compositor.
+#pragma once
+#include <pixman.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <wayland-server-core.h>
+#include <wayland-server-protocol.h>
+#include <wlr/render/pass.h>
+#include <wlr/util/addon.h>
+#include <wlr/util/box.h>
+
+struct wlr_buffer;
+struct rediwm_scene;
+struct rediwm_tree;
+
+enum rediwm_node_type {
+ REDIWM_NODE_TREE,
+ REDIWM_NODE_RECT,
+ REDIWM_NODE_BUFFER,
+ // A wlr_scene subtree hosted inside an own tree: the client surface trees
+ // wlr_scene_xdg_surface and friends build. Stage 7b removes it.
+ REDIWM_NODE_FOREIGN,
+};
+
+struct rediwm_node {
+ enum rediwm_node_type type;
+ struct rediwm_tree *parent; // NULL for a scene's root
+ struct wl_list link; // in parent->children, bottom to top
+ bool enabled;
+ int x, y; // relative to the parent
+ void *data;
+ struct {
+ struct wl_signal destroy;
+ } events;
+ struct wlr_addon_set addons;
+ struct rediwm_scene *scene;
+};
+
+struct rediwm_tree {
+ struct rediwm_node node;
+ struct wl_list children; // rediwm_node.link, bottom to top
+};
+
+struct rediwm_rect {
+ struct rediwm_node node;
+ int width, height;
+ float color[4]; // premultiplied, as wlr_scene_rect
+};
+
+struct rediwm_buffer;
+typedef bool (*rediwm_buffer_point_accepts_input)(struct rediwm_buffer *, double *x, double *y);
+
+struct rediwm_buffer {
+ struct rediwm_node node;
+ struct wlr_buffer *buffer; // locked while set; may be NULL
+ struct {
+ struct wl_signal outputs_update, output_enter, output_leave, output_sample, frame_done;
+ } events;
+ rediwm_buffer_point_accepts_input point_accepts_input; // may be NULL
+ float opacity;
+ enum wlr_scale_filter_mode filter_mode;
+ struct wlr_fbox src_box;
+ int dst_width, dst_height;
+ enum wl_output_transform transform;
+ pixman_region32_t opaque_region;
+ // The size of `buffer` as last set, kept for when the renderer releases it.
+ int buffer_width, buffer_height;
+};
+
+// A wlr_scene subtree held by an own tree. `wlr` is the subtree's root node; its
+// own position stays (0, 0) and the foreign node carries the position.
+struct rediwm_foreign {
+ struct rediwm_node node;
+ struct wlr_scene_node *wlr;
+};
+
+struct rediwm_scene {
+ struct rediwm_tree tree;
+ // Something the tree shows may have changed: the owner schedules a frame.
+ void (*changed)(void *data);
+ void *changed_data;
+};
+
+static inline struct rediwm_tree *rediwm_tree_from_node(struct rediwm_node *node) {
+ return node->type == REDIWM_NODE_TREE ? (struct rediwm_tree *)node : NULL;
+}
+static inline struct rediwm_rect *rediwm_rect_from_node(struct rediwm_node *node) {
+ return node->type == REDIWM_NODE_RECT ? (struct rediwm_rect *)node : NULL;
+}
+static inline struct rediwm_buffer *rediwm_buffer_from_node(struct rediwm_node *node) {
+ return node->type == REDIWM_NODE_BUFFER ? (struct rediwm_buffer *)node : NULL;
+}
+static inline struct rediwm_foreign *rediwm_foreign_from_node(struct rediwm_node *node) {
+ return node->type == REDIWM_NODE_FOREIGN ? (struct rediwm_foreign *)node : NULL;
+}
+
+// A scene is its root tree. `changed` may be NULL.
+struct rediwm_scene *rediwm_scene_create(void (*changed)(void *), void *data);
+void rediwm_scene_destroy(struct rediwm_scene *);
+
+// Children are created enabled, at (0, 0), on top of their siblings. NULL on
+// allocation failure.
+struct rediwm_tree *rediwm_tree_create(struct rediwm_tree *parent);
+struct rediwm_rect *rediwm_rect_create(struct rediwm_tree *parent, int width, int height,
+ const float color[4]);
+struct rediwm_buffer *rediwm_buffer_create(struct rediwm_tree *parent, struct wlr_buffer *);
+struct rediwm_foreign *rediwm_foreign_create(struct rediwm_tree *parent, struct wlr_scene_node *wlr);
+
+// Destroys the node and everything under it; each node emits events.destroy first.
+void rediwm_node_destroy(struct rediwm_node *);
+void rediwm_node_set_enabled(struct rediwm_node *, bool enabled);
+void rediwm_node_set_position(struct rediwm_node *, int x, int y);
+// Restacking: all of these are no-ops where the node already sits.
+void rediwm_node_raise_to_top(struct rediwm_node *);
+void rediwm_node_lower_to_bottom(struct rediwm_node *);
+void rediwm_node_place_above(struct rediwm_node *, struct rediwm_node *sibling);
+void rediwm_node_place_below(struct rediwm_node *, struct rediwm_node *sibling);
+// Moves the node to the top of `parent`, which must not be inside the node.
+void rediwm_node_reparent(struct rediwm_node *, struct rediwm_tree *parent);
+// The node's origin in the scene's root; false (and 0, 0) if some ancestor is
+// disabled, as wlr_scene_node_coords.
+bool rediwm_node_coords(struct rediwm_node *, int *lx, int *ly);
+// Whether the node and every ancestor are enabled.
+bool rediwm_node_visible(const struct rediwm_node *);
+
+void rediwm_rect_set_size(struct rediwm_rect *, int width, int height);
+void rediwm_rect_set_color(struct rediwm_rect *, const float color[4]);
+
+// Takes a lock on `buffer` (NULL clears). Nothing is damaged here: the caller of a
+// compositor-owned buffer reports its damage to the projection.
+void rediwm_buffer_set_buffer(struct rediwm_buffer *, struct wlr_buffer *);
+void rediwm_buffer_set_dest_size(struct rediwm_buffer *, int width, int height);
+void rediwm_buffer_set_source_box(struct rediwm_buffer *, const struct wlr_fbox *);
+void rediwm_buffer_set_opacity(struct rediwm_buffer *, float opacity);
+void rediwm_buffer_set_filter_mode(struct rediwm_buffer *, enum wlr_scale_filter_mode);
+void rediwm_buffer_set_transform(struct rediwm_buffer *, enum wl_output_transform);
+// A copy of the region, in buffer-local units; NULL clears it.
+void rediwm_buffer_set_opaque_region(struct rediwm_buffer *, const pixman_region32_t *);
+// The size the buffer node is shown at when nothing sets one: the destination, else
+// the buffer's own size with the transform applied.
+void rediwm_buffer_size(const struct rediwm_buffer *, int *width, int *height);
+
+// The topmost enabled node of the subtree at (x, y) in the subtree's own
+// coordinates, with the point in the node's, as wlr_scene_node_at. Buffers answer
+// through `point_accepts_input`. Foreign nodes are not looked into here: the
+// caller's pick of them is the projection's.
+struct rediwm_node *rediwm_node_at(struct rediwm_node *, double x, double y, double *nx, double *ny);
+typedef void (*rediwm_buffer_iterator)(struct rediwm_buffer *, int sx, int sy, void *data);
+// Every buffer of the subtree, bottom to top, with its position relative to the
+// node passed in (as wlr_scene_node_for_each_buffer; disabled nodes are skipped).
+void rediwm_node_for_each_buffer(struct rediwm_node *, rediwm_buffer_iterator, void *data);
diff --git a/src/node_test.c b/src/node_test.c
new file mode 100644
index 0000000..94bf08e
--- /dev/null
+++ b/src/node_test.c
@@ -0,0 +1,178 @@
+// Own scene nodes: tree structure, the change hook, stacking, picking and teardown.
+// No wlroots backend or renderer is needed.
+#define WLR_USE_UNSTABLE
+#include "node.h"
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <wlr/interfaces/wlr_buffer.h>
+#include <wlr/types/wlr_buffer.h>
+
+static int changes;
+static void on_change(void *data) {
+ (void)data;
+ changes++;
+}
+
+static void buffer_destroy(struct wlr_buffer *buffer) { free(buffer); }
+static const struct wlr_buffer_impl buffer_impl = {.destroy = buffer_destroy};
+static struct wlr_buffer *buffer_create(int width, int height) {
+ struct wlr_buffer *buffer = calloc(1, sizeof(*buffer));
+ assert(buffer);
+ wlr_buffer_init(buffer, &buffer_impl, width, height);
+ return buffer;
+}
+
+static struct rediwm_node *nth(struct rediwm_tree *tree, int n) {
+ struct rediwm_node *node;
+ wl_list_for_each(node, &tree->children, link) if (n-- == 0) return node;
+ return NULL;
+}
+
+static int destroyed;
+static void on_destroy(struct wl_listener *l, void *data) {
+ (void)l;
+ (void)data;
+ destroyed++;
+}
+
+static bool accepts_left_half(struct rediwm_buffer *b, double *x, double *y) {
+ (void)y;
+ int width, height;
+ rediwm_buffer_size(b, &width, &height);
+ return *x < width / 2.0;
+}
+
+static void collect(struct rediwm_buffer *b, int sx, int sy, void *data) {
+ int *out = data;
+ out[0]++;
+ out[1] = sx;
+ out[2] = sy;
+ (void)b;
+}
+
+int main(void) {
+ struct rediwm_scene *scene = rediwm_scene_create(on_change, NULL);
+ assert(scene && scene->tree.node.enabled && !scene->tree.node.parent);
+
+ // Creation reports, a no-op mutation does not.
+ struct rediwm_tree *a = rediwm_tree_create(&scene->tree);
+ struct rediwm_tree *b = rediwm_tree_create(&scene->tree);
+ assert(a && b && changes == 2);
+ assert(nth(&scene->tree, 0) == &a->node && nth(&scene->tree, 1) == &b->node);
+ changes = 0;
+ rediwm_node_set_position(&a->node, 0, 0);
+ rediwm_node_set_enabled(&a->node, true);
+ assert(changes == 0);
+ rediwm_node_set_position(&a->node, 10, 20);
+ rediwm_node_set_enabled(&a->node, false);
+ rediwm_node_set_enabled(&a->node, true);
+ assert(changes == 3);
+
+ // Coordinates sum the ancestors, and a disabled one hides the subtree.
+ struct rediwm_tree *inner = rediwm_tree_create(a);
+ rediwm_node_set_position(&inner->node, 5, 7);
+ int lx, ly;
+ assert(rediwm_node_coords(&inner->node, &lx, &ly) && lx == 15 && ly == 27);
+ rediwm_node_set_enabled(&a->node, false);
+ assert(!rediwm_node_coords(&inner->node, &lx, &ly) && lx == 0 && ly == 0);
+ assert(!rediwm_node_visible(&inner->node));
+ rediwm_node_set_enabled(&a->node, true);
+ assert(rediwm_node_visible(&inner->node));
+
+ // Stacking.
+ changes = 0;
+ rediwm_node_raise_to_top(&b->node);
+ assert(changes == 0); // already on top
+ rediwm_node_raise_to_top(&a->node);
+ assert(nth(&scene->tree, 1) == &a->node && changes == 1);
+ rediwm_node_place_below(&a->node, &b->node);
+ assert(nth(&scene->tree, 0) == &a->node);
+ rediwm_node_place_above(&a->node, &b->node);
+ assert(nth(&scene->tree, 1) == &a->node);
+ rediwm_node_lower_to_bottom(&a->node);
+ assert(nth(&scene->tree, 0) == &a->node);
+ rediwm_node_place_above(&a->node, &inner->node); // not a sibling: ignored
+ assert(nth(&scene->tree, 0) == &a->node);
+
+ // Reparenting goes to the top of the new parent and refuses a cycle.
+ rediwm_node_reparent(&inner->node, b);
+ assert(inner->node.parent == b && nth(b, 0) == &inner->node);
+ rediwm_node_reparent(&b->node, inner);
+ assert(b->node.parent == &scene->tree);
+ rediwm_node_reparent(&b->node, b);
+ assert(b->node.parent == &scene->tree);
+
+ // Rect and buffer state.
+ const float red[4] = {1, 0, 0, 1};
+ struct rediwm_rect *rect = rediwm_rect_create(&scene->tree, 30, 20, red);
+ changes = 0;
+ rediwm_rect_set_size(rect, 30, 20);
+ rediwm_rect_set_color(rect, red);
+ assert(changes == 0);
+ rediwm_rect_set_size(rect, 31, 20);
+ assert(changes == 1 && rect->width == 31);
+
+ struct wlr_buffer *raw = buffer_create(200, 100);
+ struct rediwm_buffer *buf = rediwm_buffer_create(&scene->tree, raw);
+ wlr_buffer_drop(raw); // the node keeps its own lock
+ assert(buf->buffer == raw && buf->buffer_width == 200 && buf->buffer_height == 100);
+ int w, h;
+ rediwm_buffer_size(buf, &w, &h);
+ assert(w == 200 && h == 100);
+ rediwm_buffer_set_transform(buf, WL_OUTPUT_TRANSFORM_90);
+ rediwm_buffer_size(buf, &w, &h);
+ assert(w == 100 && h == 200);
+ rediwm_buffer_set_transform(buf, WL_OUTPUT_TRANSFORM_NORMAL);
+ rediwm_buffer_set_dest_size(buf, 40, 40);
+ rediwm_buffer_size(buf, &w, &h);
+ assert(w == 40 && h == 40);
+ changes = 0;
+ rediwm_buffer_set_dest_size(buf, 40, 40);
+ rediwm_buffer_set_opacity(buf, 1);
+ rediwm_buffer_set_filter_mode(buf, WLR_SCALE_FILTER_BILINEAR);
+ rediwm_buffer_set_source_box(buf, NULL);
+ rediwm_buffer_set_opaque_region(buf, NULL);
+ assert(changes == 0);
+ rediwm_buffer_set_opacity(buf, 0.5f);
+ rediwm_buffer_set_filter_mode(buf, WLR_SCALE_FILTER_NEAREST);
+ assert(changes == 2);
+ pixman_region32_t opaque;
+ pixman_region32_init_rect(&opaque, 0, 0, 10, 10);
+ rediwm_buffer_set_opaque_region(buf, &opaque);
+ rediwm_buffer_set_opaque_region(buf, &opaque);
+ assert(changes == 3 && pixman_region32_not_empty(&buf->opaque_region));
+ pixman_region32_fini(&opaque);
+
+ // Picking: topmost first, in the node's own coordinates, honouring input rules.
+ rediwm_node_set_position(&rect->node, 100, 100);
+ rediwm_node_set_position(&buf->node, 100, 100);
+ buf->point_accepts_input = accepts_left_half;
+ double nx = -1, ny = -1;
+ assert(rediwm_node_at(&scene->tree.node, 110, 105, &nx, &ny) == &buf->node && nx == 10 && ny == 5);
+ assert(rediwm_node_at(&scene->tree.node, 130, 105, &nx, &ny) == &rect->node && nx == 30 && ny == 5);
+ assert(!rediwm_node_at(&scene->tree.node, 99, 100, &nx, &ny));
+ rediwm_node_set_enabled(&buf->node, false);
+ assert(rediwm_node_at(&scene->tree.node, 110, 105, &nx, &ny) == &rect->node);
+ rediwm_node_set_enabled(&buf->node, true);
+
+ int seen[3] = {0};
+ rediwm_node_for_each_buffer(&scene->tree.node, collect, seen);
+ assert(seen[0] == 1 && seen[1] == 100 && seen[2] == 100);
+
+ // Teardown: children go with the tree, each reports, addons are finished, and the
+ // buffer lock is released (the buffer is freed with its last lock).
+ struct wl_listener listener = {.notify = on_destroy};
+ wl_signal_add(&b->node.events.destroy, &listener);
+ struct wl_listener inner_listener = {.notify = on_destroy};
+ wl_signal_add(&inner->node.events.destroy, &inner_listener);
+ changes = 0;
+ rediwm_node_destroy(&b->node);
+ assert(destroyed == 2 && changes >= 1);
+ assert(nth(&scene->tree, 0) == &a->node);
+ rediwm_node_destroy(&buf->node);
+
+ rediwm_scene_destroy(scene);
+ puts("node tests passed");
+ return 0;
+}