Skip to content

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

Plan scene graph stage 7: retire the wlr_scene data model

oxydizer8dc8343d7131
diff --git a/docs/plans/scene-graph-stage7.md b/docs/plans/scene-graph-stage7.md
new file mode 100644
index 0000000..9540890
--- /dev/null
+++ b/docs/plans/scene-graph-stage7.md
@@ -0,0 +1,135 @@
+# Scene graph, stage 7: retire the wlr_scene data model
+
+Status: draft 2026-10-15. Decisions D1-D5 below are recommendations, not taken.
+
+## Why, and what it is not for
+
+Stages 1-6 moved everything that *does* something off wlr_scene: the output pass
+(`scene_output.c`), damage, hit testing, frame callbacks and feedback
+(`projection.c`). What is left is wlr_scene as the *data model*: the nodes that
+panels, popups, the dock, the lock and client windows hang from, and the
+`wlr_scene_surface` glue that turns a `wl_surface` into a buffer node.
+
+**This is not a performance stage.** Measured 2026-10-15 (ReleaseSafe, headless
+GLES2, `perf record` 5 kHz, 12 s, 60 Hz test client, no damage): 2.18 % of a core;
+`wlr_scene_*` + `wlr_addon_find` + `wlr_surface_from_resource` are 11.5 % of those
+samples, about 0.25 % of a core, and an own tree still has to do the same
+position/enable/buffer updates, so the achievable saving is a fraction of that.
+Full damage: 7.57 % of a core, scene symbols 4.7 % of samples. The gate is
+therefore **no regression**, not a win.
+
+What it buys: one tree instead of source tree plus resolved items, no
+`wlr_scene_node` private-field reads (`out->scene->private.direct_scanout`,
+`private.color_manager_v1_destroy`), and node features (clip, shape, effect,
+per-node inertness) as fields instead of addons (`wlr_addon_find` is 1.4 % of idle
+samples on its own).
+
+## Inventory (2026-10-15, `src/`)
+
+* Zig: 45 `createSceneBuffer`, 36 `createSceneTree`, 1 `createSceneRect`, ~660
+ `.destroy(` (mixed), 93 `.node.setEnabled`, 82 `.node.destroy`, 61
+ `.node.setPosition`, 36 `setDestSize`, 32 `setFilterMode`, 30 `setBuffer`,
+ 12 `raiseToTop`, 6 `coords`, 5 `placeBelow`, 4 `setSourceBox`; `wlr.SceneBuffer`
+ in ~30 files, `SceneTree` in ~20, `SceneRect` in 9.
+* C: `projection.c` walks `struct wlr_scene_node` (56 struct references; fields
+ read: `type`, `enabled`, `x`, `y`, `children`, `parent`, `data`);
+ `scene_output.c` reads `wlr_scene_output`, `scene->outputs`,
+ `scene->private.direct_scanout`, `scene->gamma_control_manager_v1`,
+ `output->private.combined_color_transform`; `extra_protocols.c` reaches into
+ `scene->color_manager_v1` and its private destroy listener.
+* wlroots scene helpers still in use: `createSceneXdgSurface` (Toplevel, Popup),
+ `wlr_scene_subsurface_tree_set_clip` (Toplevel), `SceneSurface.tryFromBuffer`
+ (scene_data, text_input, capture/window_source), `forEachBuffer` (close
+ snapshot, capture, Toplevel fade), `wlr_scene_node_at` (layout trees),
+ `Scene.create` (also separate scenes in osd, caret_overlay, lock),
+ `SceneOutputLayout`/`createSceneOutput`/`getSceneOutput` (Server, Output,
+ reveal, capture, ipc), `wlr_scene_set_color_manager_v1`.
+
+## What wlr_scene still does that must be replaced
+
+1. **Node tree.** parent/children/stacking, position, enabled, `data`, destroy
+ signal, reparent, raise/place-below, `coords`.
+2. **Buffer node.** buffer, dest size, source box, opacity, filter, transform,
+ opaque region, `primary_output`, and the signals the protocol glue relies on
+ (stage 6 emits wlroots' own signals on source buffers: these become ours).
+3. **Surface binding** (`wlr_scene_surface`, `wlr_scene_xdg_surface`,
+ `wlr_scene_subsurface_tree`): wl_surface to buffer node, subsurface tracking
+ and stacking, client-buffer lock, damage, xdg geometry origin, popup parenting,
+ the subsurface clip. This is the only hard part, about 600 lines of wlroots.
+4. **Output scene** (`wlr_scene_output`): the `needs_frame` latch (the frame loop
+ hangs off it, see idle notes), position from the output layout, the bits of
+ state `scene_output.c` reads from the scene (direct scanout flag, gamma and
+ colour manager presence), `output_sample`/frame-done helpers.
+5. **Hit test and iteration** for the layout trees (`node_at`, `for_each_buffer`).
+
+## Design
+
+An own node module, C core with a thin Zig wrapper, living beside `scene_graph.h`:
+
+* `rediwm_node` (kind tree | buffer | rect; parent, intrusive child list, x, y,
+ enabled, `data`, destroy signal). `rediwm_buffer` and `rediwm_rect` embed it.
+ `projection.c` walks these instead of `wlr_scene_node`; its tree-walk is
+ already the resolve step, so the change is the type it reads.
+* The Zig wrapper keeps the method names (`node.setPosition`, `setEnabled`,
+ `createSceneBuffer` becomes `createBuffer`...) so the ~700 call sites change by
+ rename, not by rewrite, and the diff is reviewable.
+* `node_surface.c`: the surface binding, modelled on wlroots' `wlr_scene_surface`
+ (MIT-licensed; keep the credit), reusing `wlr_client_buffer`, the existing
+ feedback/frame-done code in `projection.c` and the long-held lock rules from
+ AGENTS.md.
+* `output_scene.c`: the per-output state `Output.zig` needs (needs-frame latch,
+ position, damage hand-off to the pass), replacing `wlr_scene_output` and
+ `SceneOutputLayout`; colour manager and gamma are plain pointers handed over by
+ `extra_protocols.c`, which also removes the private-listener workaround.
+
+## Sub-stages
+
+* **7a** Own node/buffer/rect types and wrapper; migrate the compositor-drawn UI
+ (panels, dock, taskbar, popups, lock, OSD, selector, toast, mini-map). Windows
+ still use wlr_scene_surface, so 7a keeps a bridge: a wlr scene buffer node that
+ is a *child of* an own tree is wrapped as an own buffer node with a listener
+ (removed in 7b). Gate: goldens identical at tolerance 0, `zig build test`,
+ the layout/hit-test suites.
+* **7b** Surface binding: xdg toplevels, popups, subsurfaces, layer surfaces,
+ Xwayland, IME popups, drag icons, session-lock surfaces, subsurface clip.
+ Gate: the protocol suites (`wlr_protocols`, `popups`, xwayland, text-input,
+ dnd) and a fresh damage audit build.
+* **7c** Output scene and layout; remove `wlr_scene_output`, the colour-manager
+ workaround and the `private.*` reads. Gate: output hot-plug suites (headless
+ multi-output), `commitIsRedundant`, night light/power-off tests; VT checklist
+ step 4 (hotplug) still unrun on hardware.
+* **7d** Delete the `wlroots` scene dependency (`wlr/types/wlr_scene.h`); the
+ separate `Scene.create` users go with it.
+* **7e** Cost gate once, as in stage 6 (benchmark at stage end): same session
+ A/B against stage 6, performance profile, idle floor, instructions:u, RSS.
+ Tolerance: no regression beyond noise; the expected delta is -0 to -0.1 % of a
+ core.
+
+## Decisions to take
+
+* **D1 API shape.** (A, recommended) keep wlroots' method names so call sites
+ change by rename; (B) redesign the node API now. B is cleaner and a week longer
+ with far more review surface.
+* **D2 Surface binding.** (A, recommended) port wlroots' logic onto the own
+ buffer node and keep `wlr_client_buffer`; (B) rewrite from the protocol spec.
+ B risks every subsurface edge case wlroots already learned.
+* **D3 Branch.** Work on `stage7`, one commit per sub-stage, merge to master only
+ after 7e (same discipline as `stage4e`).
+* **D4 Layout trees.** (A, recommended) migrate them in 7a too, so there is one
+ tree type at the end; (B) leave them on wlr_scene and keep the dependency.
+ B contradicts the point of the stage.
+* **D5 Order with the idle-CPU items.** The per-frame ticks in `Output.runFrame`
+ (`frame_metrics` timestamps, `anim`, Dock/Taskbar per-frame checks) and the
+ per-frame `memset`/`clock_gettime` are independent of this stage and carry the
+ measurable gain. (Recommended) do them first so the stage 7 gate measures
+ against the improved baseline.
+
+## Risks
+
+* The surface binding is where the regressions will be (subsurface order, popups
+ that outlive their parent, client-buffer lifetime). Nothing about the idle cost
+ justifies shipping a regression there; 7b is the stage most likely to be cut.
+* `wlr_scene_output_needs_frame` is the frame loop's latch. Replace it exactly
+ or the loop spins (13,000 fps) or stalls; see the idle-CPU notes.
+* The headless GLES2 suites have 34 known failures on HEAD: compare against a
+ `git worktree` of HEAD before calling anything a regression.