RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
Split projection.c and scene_output.c into parts
Claudecedbfd7cd4bb
diff --git a/build/support.zig b/build/support.zigindex 90360ec..c3b894f 100644--- a/build/support.zig+++ b/build/support.zig@@ -40,16 +40,23 @@ pub fn addBassDsp(b: *std.Build, target: std.Build.ResolvedTarget) void {}).step);}+/// projection.c and its parts (projection_internal.h); projection_test.c+/// includes them all.+pub const projection_sources = [_][]const u8{ "src/projection.c", "src/projection_props.c", "src/projection_resolve.c", "src/projection_present.c", "src/projection_pass.c", "src/projection_pick.c" };++/// scene_output.c and its parts (scene_output_internal.h).+pub const scene_output_sources = [_][]const u8{ "src/scene_output.c", "src/scene_output_collect.c", "src/scene_output_effects.c", "src/scene_output_draw.c" };+pub fn addGlass(b: *std.Build, module: *std.Build.Module, damage_audit: bool) void {module.linkSystemLibrary("wlroots-0.20", .{});module.linkSystemLibrary("egl", .{});module.linkSystemLibrary("glesv2", .{});- module.addCSourceFile(.{ .file = b.path("src/projection.c"), .flags = if (damage_audit) &.{ "-std=c11", "-Wall", "-Wextra", "-DREDIWM_DAMAGE_AUDIT" } else &.{ "-std=c11", "-Wall", "-Wextra" } });+ module.addCSourceFiles(.{ .files = &projection_sources, .flags = if (damage_audit) &.{ "-std=c11", "-Wall", "-Wextra", "-DREDIWM_DAMAGE_AUDIT" } else &.{ "-std=c11", "-Wall", "-Wextra" } });module.addCSourceFile(.{ .file = b.path("src/node.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });module.addCSourceFile(.{ .file = b.path("src/node_surface.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });module.addCSourceFile(.{ .file = b.path("src/scene_outputs.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });module.addCSourceFile(.{ .file = b.path("src/scene_pass.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });- module.addCSourceFile(.{ .file = b.path("src/scene_output.c"), .flags = if (damage_audit) &.{ "-std=c11", "-Wall", "-Wextra", "-Werror", "-DREDIWM_DAMAGE_AUDIT" } else &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });+ module.addCSourceFiles(.{ .files = &scene_output_sources, .flags = if (damage_audit) &.{ "-std=c11", "-Wall", "-Wextra", "-Werror", "-DREDIWM_DAMAGE_AUDIT" } else &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });module.addCSourceFile(.{ .file = b.path("src/scene_damage.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });module.addCSourceFile(.{ .file = b.path("src/glass.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra" } });module.addCSourceFile(.{ .file = b.path("src/explicit_sync.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });diff --git a/build/tests.zig b/build/tests.zigindex 06c2b83..c0f04b6 100644--- a/build/tests.zig+++ b/build/tests.zig@@ -402,7 +402,7 @@ pub fn addC(ctx: Context, test_step: *std.Build.Step) void {inline for (.{ "wlroots-0.20", "wayland-server", "pixman-1", "egl", "glesv2" }) |lib| {glass_tests.root_module.linkSystemLibrary(lib, .{});}- glass_tests.root_module.addCSourceFile(.{ .file = b.path("src/projection.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra" } });+ glass_tests.root_module.addCSourceFiles(.{ .files = &support.projection_sources, .flags = &.{ "-std=c11", "-Wall", "-Wextra" } });glass_tests.root_module.addCSourceFile(.{ .file = b.path("src/scene_damage.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });glass_tests.root_module.addCSourceFile(.{ .file = b.path("src/scene_outputs.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });glass_tests.root_module.addCSourceFile(.{ .file = b.path("src/node.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });diff --git a/src/projection.c b/src/projection.cindex 679b779..f41a561 100644--- a/src/projection.c+++ b/src/projection.c@@ -1,348 +1,24 @@// Presentation-only geometry. Source xdg/subsurface nodes are never rewritten.// wlroots 0.20 ABI, pinned by build.zig.zon (as in glass.c).-#define WLR_USE_UNSTABLE-#define WLR_PRIVATE private-#include "projection.h"-#include "glass.h"-#include "node.h"-#include "scene_graph.h"-#include "scene_damage.h"-#include <math.h>-#include <stdlib.h>-#include <string.h>-#include <wlr/types/wlr_buffer.h>-#include <wlr/types/wlr_compositor.h>-#include <wlr/types/wlr_output.h>-#include "node_surface.h"-#include "scene_outputs.h"-#include <wlr/util/addon.h>-#include <wlr/util/log.h>+#include "projection_internal.h"// Two halves, joined by the typed graph of scene_graph.h. resolve() walks the// source tree and writes `item`s and group origins, in paint order, into the-// lists below. present() reads only that graph (and the content the source still-// owns: buffers and their damage), rounds, and turns what changed into damage-// events and screen damage. The graph is the one place geometry is computed;-// present() may not derive any of its own. The output pass, hit testing and the-// surface feedback all read the graph too: there is no second tree of the world.-// 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 {- // The compositor's scene. The children of its root are layers, bottom to top:- // `world` follows the camera, every other layer is laid out as is. The wlroots- // scene is kept for its outputs.- struct rediwm_scene *own;- struct rediwm_tree *world;- struct rediwm_scene_outputs *outputs;- // What changed since the last sync. A change to one top-level subtree (a group) marks- // that group; a change to the shape of the tree, a layer or anything not tied to- // one group marks them all (`all_dirty`). A sync resolves and presents only the- // marked groups, or none when nothing changed and the camera did not move.- bool any_dirty, all_dirty, have_camera;- double camera[5];- struct rediwm_tree *fixed;- // Every adapter, in creation order; `paint` holds the groups resolve() last- // reached, bottom to top.- struct wl_list leaves, groups, paint;- // The surface leaves whose outputs the output pass reports (stage 6), and what- // each output (by scene output index) looked like at the last report.- struct wl_list feedback;- struct {- float scale;- enum wl_output_transform transform;- enum wl_output_subpixel subpixel;- bool known;- } shown_output[64];- // The outputs whose scale, transform or subpixel order changed since: a surface- // on them is told again even where nothing else changed, as wlroots does, for- // its preferred scale.- uint64_t forced;- struct wl_listener new_surface;- struct wl_list surfaces;- uint64_t next_leaf_id;- // Layout-space damage the sync in progress collects, applied to every output- // when it ends (stage 6b).- pixman_region32_t pending;- struct rediwm_damage_event events[DAMAGE_EVENTS];- uint64_t next_event; // sequence number of the next event; 0 is never used-};+// projection's lists (projection_internal.h). present() reads only that graph+// (and the content the source still owns: buffers and their damage), rounds, and+// turns what changed into damage events and screen damage. The graph is the one+// place geometry is computed; present() may not derive any of its own. The output+// pass, hit testing and the surface feedback all read the graph too: there is no+// second tree of the world.+// The parts: this file holds the adapters' lifecycle, damage events and the+// sync; projection_props.c the tree properties, projection_resolve.c resolve(),+// projection_present.c present(), projection_pass.c what the output pass reads+// and reports, projection_pick.c hit testing. Shared types and helpers are in+// projection_internal.h.// The compositor has one projection; setters on nodes inside a client's subtree, which// cannot reach it through a scene, tell it directly.static struct rediwm_projection *current;-static void mark_all(struct rediwm_projection *p);-// Source top-level subtrees are contiguous in paint order: each is a group, and-// a window going away damages everything its leaves showed at once.-struct group {- struct wl_list link;- // In projection->paint, or alone, when the last resolve() did not reach it.- struct wl_list paint;- // The leaves resolve() last reached under this group, bottom to top.- struct wl_list items;- bool enabled;- // Changed since resolve() last walked it; walked at least once; walked by this sync.- bool dirty, resolved, touched;- // Holds a glass effect, whose box is its target's, which may be a node of another group:- // such a group is walked whenever anything is.- bool has_effect;- struct rediwm_projection *projection;- struct rediwm_node *source;- struct wl_listener destroy;- struct wlr_addon source_lookup;- bool seen;-};-// A list link that is always valid to remove: alone, or in a live list.-static void detach(struct wl_list *link) {- wl_list_remove(link);- wl_list_init(link);-}-struct leaf {- struct wl_list link;- // In its group's `items` when the last resolve() reached it, else alone.- struct wl_list paint;- struct rediwm_projection *projection;- // The node the pass reads the pixels from (`transport`): the buffer or rect itself.- struct rediwm_node *source;- // The own node it is.- struct rediwm_node *own;- // The group the last resolve() put it in.- struct group *group;- // A glass effect: decided when the leaf is made, as an effect is its own node.- bool effect;- struct wl_listener destroy, commit;- struct wlr_addon source_lookup;- struct rediwm_surface *surface;- struct feedback *feedback;- bool dirty, seen;- // Under an inert tree at the last resolve(): drawn, never picked.- bool inert;- // Commits can outpace presentation. Keep their union, including empty damage- // on a rotated buffer; buffer identity is not evidence of changed pixels.- pixman_region32_t damage;- bool full_damage;- // A compositor-owned buffer reported its own damage since the last sync;- // without a report a replaced buffer damages its whole box.- bool owned_damage;- int damage_width, damage_height;- // 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;- // `item.shadow` points at `shadow` when the leaf is under a shadow tree. A- // shadow has no buffer to damage: 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;- // What of the leaf nothing above hid when the output pass last built, over the- // whole layout: damage that changes only what the leaf shows is cut to it, as- // wlroots cut it to a node's visible region. Not known until the first build.- pixman_region32_t visible;- bool has_visible;- // Position in paint order at the last sync, to tell a restack.- uint32_t rank;- bool ranked;- struct rediwm_shadow shadow_drawn_as;- struct rediwm_snap shadow_drawn;- float shadow_drawn_opacity;- bool has_shadow_drawn;- struct wlr_buffer *last_buffer;-};-// 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 and dim 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 frame. The subtree's own-// clip or dim, if it sets one, still applies;-// * dim: every leaf under the tree loses this fraction of its brightness and of-// its colour (`rediwm_content.dim_*`), inherited like the clip. It is a field of-// the item, so a change of it damages what the leaves show with no rule of its-// own, and the pass applies it per draw. A subtree's own dim replaces the outer-// one, and `unclipped` ends it as it ends the clip;-// * shadow: every leaf under the tree is the analytic shadow of this shape-// (REDIWM_ITEM_SHADOW), resolved like the clip;-// * inert: the subtree is shown but takes no input: the pick passes through it-// to what is below (a window sliding off a workspace it has left).-struct dim {- float brightness, saturation;-};-struct tree_props {- struct wlr_addon addon;- // Where an own node keeps its properties (`rediwm_node.props`), cleared with them.- void **slot;- bool has_zoom, has_scale, has_clip, unclipped, has_shadow, inert, has_dim;- double zoom, scale;- struct dim dim;- struct rediwm_clip_spec clip;- struct rediwm_shadow_spec shadow;- // The clip as the last resolve() saw it in layout space, to tell a changed- // shape (to be damaged) from a moved window (whose leaves damage themselves).- struct rediwm_clip applied;- bool has_applied;-};-static void destroy_tree_props(struct wlr_addon *addon) {- struct tree_props *props = wl_container_of(addon, props, addon);- if (props->slot) *props->slot = NULL;- wlr_addon_finish(addon);- 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_addon_set *set, void **slot) {- if (slot) return *slot;- struct wlr_addon *addon = wlr_addon_find(set, 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_addon_set *set, void **slot) {- struct tree_props *props = tree_props_find(set, slot);- if (props) return props;- props = calloc(1, sizeof(*props));- if (!props) return NULL;- props->slot = slot;- if (slot) *slot = props;- wlr_addon_init(&props->addon, set, NULL, &tree_props_impl);- return props;-}-// Set by a setter that changed something, so the wrappers below ask for a frame only-// then: a window's zoom and clip are applied every frame and mostly unchanged.-static bool props_changed;-static bool set_tree_zoom(struct wlr_addon_set *set, void **slot, double scale) {- struct tree_props *props = tree_props_get(set, slot);- if (!props) return false;- if (!props->has_zoom || props->zoom != scale) props_changed = true;- props->zoom = scale;- props->has_zoom = true;- return true;-}-static bool set_tree_scale(struct wlr_addon_set *set, void **slot, double factor) {- if (factor <= 0) return false;- struct tree_props *props = tree_props_get(set, slot);- if (!props) return false;- if (!props->has_scale || props->scale != factor) props_changed = true;- props->scale = factor;- props->has_scale = true;- return true;-}-static bool finite_at_least_zero(double value) { return isfinite(value) && value >= 0; }-static bool set_tree_clip(struct wlr_addon_set *set, void **slot,- const struct rediwm_clip_spec *spec) {- if (!spec) {- struct tree_props *props = tree_props_find(set, slot);- if (props && props->has_clip) props_changed = true;- if (props) props->has_clip = false;- return true;- }- 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]) || !isfinite(spec->border_color[i]) ||- !isfinite(spec->glow_color[i]))- return false;- if (!isfinite(spec->glow_x) || !isfinite(spec->glow_y) || !finite_at_least_zero(spec->glow_radius) ||- !finite_at_least_zero(spec->glow_depth))- return false;- struct tree_props *props = tree_props_get(set, slot);- if (!props) return false;- if (!props->has_clip || memcmp(&props->clip, spec, sizeof(*spec))) props_changed = true;- props->clip = *spec;- props->has_clip = true;- return true;-}-static bool set_tree_shadow(struct wlr_addon_set *set, void **slot,- const struct rediwm_shadow_spec *spec) {- if (!spec) {- struct tree_props *props = tree_props_find(set, slot);- if (props && props->has_shadow) props_changed = true;- if (props) props->has_shadow = false;- return true;- }- if (spec->width <= 0 || spec->height <= 0 || !finite_at_least_zero(spec->softness) ||- !isfinite(spec->offset_x) || !isfinite(spec->offset_y))- return false;- for (int i = 0; i < 4; i++)- if (!finite_at_least_zero(spec->radii[i]) || !isfinite(spec->color[i])) return false;- struct tree_props *props = tree_props_get(set, slot);- if (!props) return false;- if (!props->has_shadow || memcmp(&props->shadow, spec, sizeof(*spec))) props_changed = true;- props->shadow = *spec;- props->has_shadow = true;- return true;-}-static bool set_tree_dim(struct wlr_addon_set *set, void **slot, float brightness, float saturation) {- if (!isfinite(brightness) || !isfinite(saturation) || brightness < 0 || brightness > 1 ||- saturation < 0 || saturation > 1)- return false;- if (brightness == 0 && saturation == 0) {- // Nothing taken away is no dim; a tree that has none gets no addon for it.- struct tree_props *props = tree_props_find(set, slot);- if (props && props->has_dim) props_changed = true;- if (props) props->has_dim = false;- return true;- }- struct tree_props *props = tree_props_get(set, slot);- if (!props) return false;- if (!props->has_dim || props->dim.brightness != brightness || props->dim.saturation != saturation) props_changed = true;- props->dim = (struct dim){brightness, saturation};- props->has_dim = true;- return true;-}-static bool set_tree_unclipped(struct wlr_addon_set *set, void **slot, bool unclipped) {- struct tree_props *props = unclipped ? tree_props_get(set, slot) : tree_props_find(set, slot);- if (unclipped && !props) return false;- if (props && props->unclipped != unclipped) props_changed = true;- if (props) props->unclipped = unclipped;- return true;-}-static bool set_tree_inert(struct wlr_addon_set *set, void **slot, bool inert) {- struct tree_props *props = inert ? tree_props_get(set, slot) : tree_props_find(set, slot);- if (inert && !props) return false;- if (props && props->inert != inert) props_changed = true;- if (props) props->inert = inert;- return true;-}--bool rediwm_projection_set_tree_zoom(struct rediwm_node *node, double scale) { props_changed = false; bool ok = set_tree_zoom(&node->addons, &node->props, scale); if (props_changed) rediwm_node_touch(node); return ok; }-bool rediwm_projection_set_tree_scale(struct rediwm_node *node, double factor) { props_changed = false; bool ok = set_tree_scale(&node->addons, &node->props, factor); if (props_changed) rediwm_node_touch(node); return ok; }-bool rediwm_projection_set_tree_clip(struct rediwm_node *node, const struct rediwm_clip_spec *spec) { props_changed = false; bool ok = set_tree_clip(&node->addons, &node->props, spec); if (props_changed) rediwm_node_touch(node); return ok; }-bool rediwm_projection_set_tree_shadow(struct rediwm_node *node, const struct rediwm_shadow_spec *spec) { props_changed = false; bool ok = set_tree_shadow(&node->addons, &node->props, spec); if (props_changed) rediwm_node_touch(node); return ok; }-bool rediwm_projection_set_tree_dim(struct rediwm_node *node, float brightness, float saturation) { props_changed = false; bool ok = set_tree_dim(&node->addons, &node->props, brightness, saturation); if (props_changed) rediwm_node_touch(node); return ok; }-bool rediwm_projection_set_tree_unclipped(struct rediwm_node *node, bool unclipped) { props_changed = false; bool ok = set_tree_unclipped(&node->addons, &node->props, unclipped); if (props_changed) rediwm_node_touch(node); return ok; }-bool rediwm_projection_set_tree_inert(struct rediwm_node *node, bool inert) { props_changed = false; bool ok = set_tree_inert(&node->addons, &node->props, inert); if (props_changed) rediwm_node_touch(node); return ok; }-static const struct wlr_addon_interface source_lookup_impl, group_source_impl;-struct watched_surface {- struct rediwm_projection *projection;- struct wlr_surface *surface;- struct wl_list link;- struct wl_listener commit, destroy;-};-static void mark_all(struct rediwm_projection *p) { p->any_dirty = p->all_dirty = true; }-static void mark_group(struct rediwm_projection *p, struct group *g) {- if (!g) {- mark_all(p);- return;- }- g->dirty = true;- p->any_dirty = true;-}-static void schedule(struct rediwm_projection *p) {- struct rediwm_scene_output *out;- wl_list_for_each(out, &p->outputs->outputs, link)- wlr_output_schedule_frame(out->output);-}-static void forget_shown(struct leaf *l);-static void damage_shadow(struct leaf *l, bool show, struct rediwm_snap at);+static const struct wlr_addon_interface source_lookup_impl;static void apply_damage(struct rediwm_projection *p);static void destroy_group(struct group *g) {// What the window's leaves showed is damaged at once, as a node going away was@@ -352,7 +28,7 @@ static void destroy_group(struct group *g) {struct leaf *l, *next;wl_list_for_each_safe(l, next, &g->items, paint) {forget_shown(l);- damage_shadow(l, false, (struct rediwm_snap){0});+ projection_damage_shadow(l, false, (struct rediwm_snap){0});detach(&l->paint);}apply_damage(g->projection);@@ -373,9 +49,9 @@ static void group_source_addon_destroy(struct wlr_addon *addon) {struct group *g = wl_container_of(addon, g, source_lookup);destroy_group(g);}-static const struct wlr_addon_interface group_source_impl = {+const struct wlr_addon_interface projection_group_source_impl = {.name = "rediwm projection group source", .destroy = group_source_addon_destroy};-static struct group *get_group(struct rediwm_projection *p,+struct group *projection_get_group(struct rediwm_projection *p,struct rediwm_node *source) {struct group *g = source->group;if (g)@@ -388,49 +64,12 @@ static struct group *get_group(struct rediwm_projection *p,wl_list_init(&g->paint);wl_list_init(&g->items);wl_list_insert(&p->groups, &g->link);- wlr_addon_init(&g->source_lookup, &source->addons, p, &group_source_impl);+ wlr_addon_init(&g->source_lookup, &source->addons, p, &projection_group_source_impl);source->group = g;g->destroy.notify = group_source_destroy;wl_signal_add(&source->events.destroy, &g->destroy);return g;}-// ---- Surface feedback (stage 6 of docs/plans/scene-graph.md) ----------------------// wlr_scene tells a surface where it is shown by emitting its scene buffer's own-// signals, from the node's visible region. The world's source tree is disabled, so-// wlroots sees nothing there; the output pass, which draws the graph, knows what-// is shown.-// For every client surface it reports, per output it builds, how many pixels are-// visible (not hidden by what is above), and this keeps the last report of each-// output. From the sums it applies wlroots' own rule (scene.c,-// update_node_update_outputs): an output is active when it shows more than a tenth-// of what is visible on all of them, and the primary one is the one showing most-// (the last, on a tie). It then emits the signals the surface binding listens to-// (node_surface.c) on the source buffer.-enum { FEEDBACK_OUTPUTS = 64 };-struct feedback {- struct wl_list link;- struct leaf *leaf;- // Visible pixels as the last build of each output (scene output index) saw them.- uint32_t area[FEEDBACK_OUTPUTS];- uint64_t active;- struct rediwm_scene_output *primary;-};-static void free_feedback(struct leaf *l) {- if (!l->feedback) return;- wl_list_remove(&l->feedback->link);- free(l->feedback);- l->feedback = NULL;-}-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;@@ -444,38 +83,13 @@ bool rediwm_projection_damage_events(struct rediwm_projection *p, uint64_t *cursuint64_t rediwm_projection_damage_cursor(struct rediwm_projection *p) {return p->next_event ? p->next_event : 1;}-// Layout-space damage for the screen, applied when the sync ends. `visible_only`-// cuts it to what the leaf showed at the last build: a change of what the leaf-// itself shows is invisible where something opaque lay over it, and anything that-// stops covering it damages that region itself.-static void damage_box(struct leaf *l, struct rediwm_snap s, bool visible_only) {- if (s.width <= 0 || s.height <= 0) return;- pixman_region32_t *pending = &l->projection->pending;- if (visible_only && l->has_visible) {- pixman_region32_t region;- pixman_region32_init_rect(®ion, s.x, s.y, s.width, s.height);- pixman_region32_intersect(®ion, ®ion, &l->visible);- pixman_region32_union(pending, pending, ®ion);- pixman_region32_fini(®ion);- } else {- pixman_region32_union_rect(pending, pending, s.x, s.y, s.width, s.height);- }-}-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);- damage_box(l, l->shown, true);- }- l->has_shown = false;-}static void destroy_leaf(struct leaf *l) {forget_shown(l);- damage_shadow(l, false, (struct rediwm_snap){0});+ projection_damage_shadow(l, false, (struct rediwm_snap){0});// A node going away damaged the screen at once, and a frame built before the// next sync (a capture) must see it.apply_damage(l->projection);- free_feedback(l);+ projection_free_feedback(l);wl_list_remove(&l->commit.link);wl_list_remove(&l->destroy.link);wl_list_remove(&l->link);@@ -533,7 +147,7 @@ static void committed(struct wl_listener *listener, void *data) {mark_group(l->projection, l->group);schedule(l->projection);}-static struct leaf *get_leaf(struct rediwm_projection *p, struct rediwm_node *source,+struct leaf *projection_get_leaf(struct rediwm_projection *p, struct rediwm_node *source,struct rediwm_node *own) {// A node's leaf is kept on the node.if (source->leaf)@@ -573,571 +187,6 @@ static struct leaf *get_leaf(struct rediwm_projection *p, struct rediwm_node *so}return l;}-// A glass effect covers its target exactly: the box is the target's (same-// parent), and it is shown only with it. The pass draws the blur (stage 5c).-static void effect_geometry(const struct rediwm_node *own, const struct rediwm_glass_info *info,- int *x, int *y, int *width, int *height, bool *enabled) {- const struct rediwm_buffer *t = info->target;- *x += t->node.x - own->x;- *y += t->node.y - own->y;- *enabled = *enabled && t->node.enabled;- *width = t->dst_width ? t->dst_width : t->buffer_width;- *height = t->dst_height ? t->dst_height : t->buffer_height;-}--// A clip's size, radii, border or colour changed: its views did not move, so-// nothing else tells wlroots that the corner arcs and the ring need repainting.-// The zone is the frame's rim, as wide as the largest radius plus the border and-// a margin; where the frame is thinner than twice that it is the whole box.-static bool clip_same_shape(const struct rediwm_clip *a, const struct rediwm_clip *b) {- if (a->edges.x2 - a->edges.x1 != b->edges.x2 - b->edges.x1 ||- a->edges.y2 - a->edges.y1 != b->edges.y2 - b->edges.y1 || a->border != b->border)- return false;- for (int i = 0; i < 4; i++)- if (a->radii[i] != b->radii[i] || a->border_color[i] != b->border_color[i]) return false;- return true;-}-// Where the highlight is, as far as it can be seen: a square around its point.-static bool clip_glow_box(const struct rediwm_clip *c, pixman_box32_t *out) {- if (c->glow_radius <= 0 || c->glow_color[3] <= 0) return false;- const double reach = c->glow_radius + 1;- *out = (pixman_box32_t){(int)floor(c->glow_x - reach), (int)floor(c->glow_y - reach),- (int)ceil(c->glow_x + reach), (int)ceil(c->glow_y + reach)};- return true;-}-static bool clip_same_glow(const struct rediwm_clip *a, const struct rediwm_clip *b) {- if (a->glow_radius != b->glow_radius || a->glow_depth != b->glow_depth) return false;- if (a->glow_radius <= 0) return true;- if (a->glow_x != b->glow_x || a->glow_y != b->glow_y) return false;- for (int i = 0; i < 4; i++)- if (a->glow_color[i] != b->glow_color[i]) return false;- return true;-}-// `only` limits the rim to a box (a moved highlight changes nothing else).-static void clip_rim(pixman_region32_t *out, const struct rediwm_clip *c, const pixman_box32_t *only) {- const struct rediwm_snap f = rediwm_edges_snap(c->edges);- if (f.width <= 0 || f.height <= 0) return;- double radius = 0;- for (int i = 0; i < 4; i++)- if (c->radii[i] > radius) radius = c->radii[i];- const int inset = (int)ceil(radius) + (int)ceil(c->border) + (int)ceil(c->glow_depth) + 2;- pixman_region32_t rim;- pixman_region32_init_rect(&rim, f.x, f.y, f.width, f.height);- if (f.width > 2 * inset && f.height > 2 * inset) {- pixman_region32_t inner;- pixman_region32_init_rect(&inner, f.x + inset, f.y + inset, f.width - 2 * inset,- f.height - 2 * inset);- pixman_region32_subtract(&rim, &rim, &inner);- pixman_region32_fini(&inner);- }- if (only) pixman_region32_intersect_rect(&rim, &rim, only->x1, only->y1, only->x2 - only->x1, only->y2 - only->y1);- pixman_region32_union(out, out, &rim);- pixman_region32_fini(&rim);-}-static void damage_clip_change(struct rediwm_projection *p, const struct rediwm_clip *before,- const struct rediwm_clip *after) {- pixman_region32_t region;- pixman_region32_init(®ion);- if (before && after && clip_same_shape(before, after)) {- // Only the highlight changed: its old and new places.- pixman_box32_t box;- if (clip_glow_box(before, &box)) clip_rim(®ion, before, &box);- if (clip_glow_box(after, &box)) clip_rim(®ion, after, &box);- } else {- if (before) clip_rim(®ion, before, NULL);- if (after) clip_rim(®ion, after, NULL);- }- pixman_region32_union(&p->pending, &p->pending, ®ion);- pixman_region32_fini(®ion);-}-// resolve(): source tree -> graph. Writes items, group origins, `seen` flags and-// the paint-order lists; creates adapters for nodes it meets the first time, but-// moves and shows nothing.-// What the walk carries down the tree: where the subtree is, whether it shows and-// takes input, and how it is presented. A tree's own properties are folded in as it-// is entered.-struct walk {- double x, y;- bool enabled, inert;- struct rediwm_xform t;- const struct rediwm_clip *clip;- const struct dim *dim;- const struct rediwm_shadow *shadow;-};-// `here` and `cast` hold the tree's clip and shadow for as long as the walk is below-// it.-static void apply_tree(struct rediwm_projection *p, struct tree_props *props, struct walk *w,- struct rediwm_clip *here, struct rediwm_shadow *cast) {- if (!props) return;- // Window zoom composes with the camera, around the frame origin.- if (props->has_zoom) w->t = rediwm_xform_scale_about(w->t, props->zoom, w->x, w->y);- if (props->has_scale) w->t = rediwm_xform_scale_about(w->t, props->scale, w->x, w->y);- if (props->unclipped) {- w->clip = NULL;- w->dim = NULL;- }- if (props->inert) w->inert = true;- if (props->has_dim) w->dim = &props->dim;- if (props->has_clip) {- *here = rediwm_clip_resolve(w->t, w->x, w->y, &props->clip);- w->clip = here;- // A hidden window damages nothing; showing it will.- if (w->enabled && (!props->has_applied || !clip_same_shape(&props->applied, here) ||- !clip_same_glow(&props->applied, here)))- damage_clip_change(p, props->has_applied ? &props->applied : NULL, here);- props->applied = *here;- props->has_applied = true;- } else if (props->has_applied) {- if (w->enabled) damage_clip_change(p, &props->applied, NULL);- props->has_applied = false;- }- if (props->has_shadow) {- *cast = rediwm_shadow_resolve(w->t, w->x, w->y, &props->shadow);- w->shadow = cast;- }-}-// One leaf into the graph. `node` is the node the pass reads pixels from;-// (x, y) and `enabled` are the leaf's own, from whichever tree placed it.-static void resolve_leaf(struct rediwm_projection *p, struct group *g, struct rediwm_node *node,- struct rediwm_node *own, const struct walk *w, double x, double y,- bool enabled) {- struct leaf *l = get_leaf(p, node, own);- if (!l) {- wlr_log(WLR_ERROR, "zoom: cannot allocate presentation node");- return;- }- l->seen = true;- if (l->inert != w->inert) l->inert = w->inert;- if (l->own != own) l->own = own;- l->group = g;- // Flattened leaves within each group retain the source's paint order.- wl_list_insert(g->items.prev, &l->paint);- struct rediwm_item *item = &l->item;- item->enabled = enabled;- item->scale = w->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 (w->clip) {- l->clip = *w->clip;- item->clip = &l->clip;- } else if (item->clip) {- item->clip = NULL;- }- if (node->type == REDIWM_NODE_RECT) {- struct rediwm_rect *r = rediwm_rect_from_node(node);- item->kind = REDIWM_ITEM_RECT;- item->width = r->width;- item->height = r->height;- memcpy(item->content.color, r->color, sizeof(item->content.color));- } else {- struct rediwm_buffer *b = rediwm_buffer_from_node(node);- if (l->effect) g->has_effect = true;- item->kind = l->effect ? REDIWM_ITEM_EFFECT- : l->surface ? REDIWM_ITEM_SURFACE- : REDIWM_ITEM_BUFFER;- item->width = b->dst_width;- item->height = b->dst_height;- if (item->kind == REDIWM_ITEM_EFFECT) {- struct rediwm_glass_info info;- int ex = 0, ey = 0;- if (own && rediwm_glass_effect_info(node, &info))- effect_geometry(own, &info, &ex, &ey, &item->width, &item->height, &item->enabled);- x += ex;- y += ey;- } else if (!item->width || !item->height) {- item->width = b->buffer_width;- item->height = b->buffer_height;- if (b->transform & 1) {- int tmp = item->width;- item->width = item->height;- item->height = tmp;- }- }- struct rediwm_content *content = &item->content;- content->src_box = b->src_box;- content->transform = b->transform;- content->opacity = b->opacity;- content->filter_mode = rediwm_filter_at_scale(w->t.scale, b->filter_mode);- content->transfer_function = b->transfer_function;- content->primaries = b->primaries;- content->color_encoding = b->color_encoding;- content->color_range = b->color_range;- }- if (w->dim) {- item->content.dim_brightness = w->dim->brightness;- item->content.dim_saturation = w->dim->saturation;- }- if (w->shadow && node->type == REDIWM_NODE_BUFFER) {- item->kind = REDIWM_ITEM_SHADOW;- l->shadow = *w->shadow;- item->shadow = &l->shadow;- } else if (item->shadow) {- item->shadow = NULL;- }- item->edges = rediwm_xform_rect(w->t, x, y, item->width, item->height);-}-static void resolve_own(struct rediwm_projection *p, struct group *g, struct rediwm_node *node,- const struct walk *parent) {- const double x = parent->x + node->x, y = parent->y + node->y;- const bool enabled = parent->enabled && node->enabled;- switch (node->type) {- case REDIWM_NODE_RECT:- resolve_leaf(p, g, node, node, parent, x, y, enabled);- return;- case REDIWM_NODE_BUFFER:- resolve_leaf(p, g, node, node, parent, x, y, enabled);- return;- case REDIWM_NODE_TREE: {- struct walk w = *parent;- w.x = x;- w.y = y;- w.enabled = enabled;- struct rediwm_clip here;- struct rediwm_shadow cast;- apply_tree(p, tree_props_find(&node->addons, &node->props), &w, &here, &cast);- struct rediwm_node *child;- wl_list_for_each(child, &((struct rediwm_tree *)node)->children, link)- resolve_own(p, g, child, &w);- return;- }- }-}--// What a shadow draws changed, or it appeared or went: damage the footprint it-// last drew and the one it draws now, in layout space.-static void damage_shadow(struct leaf *l, bool show, struct rediwm_snap at) {- const float opacity = l->item.content.opacity;- if (show == l->has_shadow_drawn &&- (!show || (at.x == l->shadow_drawn.x && at.y == l->shadow_drawn.y &&- at.width == l->shadow_drawn.width && at.height == l->shadow_drawn.height &&- opacity == l->shadow_drawn_opacity &&- rediwm_shadow_equal(&l->shadow, &l->shadow_drawn_as))))- return;- pixman_region32_t region;- pixman_region32_init(®ion);- if (l->has_shadow_drawn)- pixman_region32_union_rect(®ion, ®ion, l->shadow_drawn.x, l->shadow_drawn.y,- l->shadow_drawn.width, l->shadow_drawn.height);- if (show) pixman_region32_union_rect(®ion, ®ion, at.x, at.y, at.width, at.height);- if (pixman_region32_not_empty(®ion)) {- pixman_region32_union(&l->projection->pending, &l->projection->pending, ®ion);- const pixman_box32_t *b = pixman_region32_extents(®ion);- emit(l->projection, l->item.id, b->x1, b->y1, b->x2 - b->x1, b->y2 - b->y1);- }- pixman_region32_fini(®ion);- l->has_shadow_drawn = show;- l->shadow_drawn = at;- l->shadow_drawn_as = l->shadow;- l->shadow_drawn_opacity = opacity;-}--// The layout box a buffer-local box covers: through the source box onto the item's-// edges, one pixel wider for filtering, never beyond the leaf (damage outside its-// source box shows nowhere).-static struct rediwm_snap map_damage(const struct rediwm_item *item, const pixman_box32_t *b,- const struct wlr_fbox *src, struct rediwm_snap snap) {- 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;- 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;- return (struct rediwm_snap){cx1, cy1, cx2 - cx1, cy2 - cy1};-}-// What a commit's or a replaced buffer's damage changed: the event covers the-// bounding box of the buffer-local damage, the screen only the pieces of it (up to-// a few). 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);- damage_box(l, snap, true);- return;- }- if (!pixman_region32_not_empty((pixman_region32_t *)damage)) return;- 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_snap whole =- map_damage(item, pixman_region32_extents((pixman_region32_t *)damage), &src, snap);- emit(l->projection, item->id, whole.x, whole.y, whole.width, whole.height);- int count = 0;- const pixman_box32_t *rects = pixman_region32_rectangles((pixman_region32_t *)damage, &count);- if (count > 16) {- damage_box(l, whole, true);- return;- }- for (int i = 0; i < count; i++) {- const struct rediwm_snap piece = map_damage(item, &rects[i], &src, snap);- damage_box(l, piece, true);- }-}-// present(): graph -> damage. Reads items, never the source's geometry; rounds-// and tells the screen and the damage events what changed.-static void present_leaf(struct leaf *l) {- struct rediwm_item *item = &l->item;- // Round in layout coordinates first. Rounding a scaled local offset would- // change pixels at fractional zoom and when crossing negative origins.- const struct rediwm_snap snap = rediwm_edges_snap(item->edges);- const bool enabled = item->enabled && snap.width > 0 && snap.height > 0;- // 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.- // Only a leaf that puts pixels on screen has them: an input-only node (a- // resize grab) moving with its window must not read as a change below glass,- // and the pass draws effects itself (shadows report in damage_shadow).- bool paints = enabled;- if (item->kind == REDIWM_ITEM_RECT) {- paints = paints && item->content.color[3] > 0;- } else if (item->kind == REDIWM_ITEM_EFFECT || item->kind == REDIWM_ITEM_SHADOW) {- paints = false;- } else {- // What the output pass draws from: the buffer, or the texture left when GLES2- // uploaded it and let the buffer go, or a single-pixel colour.- const struct rediwm_buffer *sb = rediwm_buffer_from_node(l->source);- paints = paints && item->content.opacity > 0 &&- (sb->buffer || sb->texture || sb->is_single_pixel_buffer ||- (l->surface && l->surface->surface->buffer));- }- const bool restyled = !l->has_shown || !paints || 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 (paints) {- emit_snap(l, snap);- damage_box(l, snap, false);- l->shown = snap;- l->shown_content = item->content;- l->has_shown = true;- }- }- if (item->kind != REDIWM_ITEM_RECT && item->kind != REDIWM_ITEM_EFFECT) {- struct rediwm_buffer *b = rediwm_buffer_from_node(l->source);- struct wlr_buffer *backing = b->buffer;- // wlroots may cache a single-pixel buffer as a color and release the- // source node's buffer reference. The mapped surface still owns it.- if (!backing && l->surface && l->surface->surface->buffer)- backing = &l->surface->surface->buffer->base;- if (l->dirty || l->last_buffer != backing) {- const pixman_region32_t *damage =- backing && (l->surface || l->owned_damage) && l->dirty && !l->full_damage- ? &l->damage : NULL;- if (!restyled && l->has_shown)- damage_event_for_buffer(l, damage,- l->surface ? l->damage_width : backing ? backing->width : 0,- l->surface ? l->damage_height : backing ? backing->height : 0, snap);- l->last_buffer = backing;- }- }- if (item->kind == REDIWM_ITEM_SHADOW || l->has_shadow_drawn)- damage_shadow(l, item->kind == REDIWM_ITEM_SHADOW && enabled &&- item->content.opacity > 0 && item->shadow->color[3] > 0,- snap);- l->dirty = false;- l->full_damage = false;- l->owned_damage = false;- pixman_region32_clear(&l->damage);-}-// Not in 0.20's public headers, but exported; see-// rediwm_projection_present_owned.-struct wlr_client_buffer *wlr_client_buffer_create(struct wlr_buffer *buffer,- struct wlr_renderer *renderer);-bool wlr_client_buffer_apply_damage(struct wlr_client_buffer *client_buffer,- struct wlr_buffer *next,- const pixman_region32_t *damage);-// A plain buffer replaced on a scene node gets a new texture and a full-// upload. Wrapping it in a client buffer instead lets the next frame's-// damage update that texture in place, as for shm clients. The source node's-// lock is marked ignorable (like wlr_scene's own surface lock): the output pass-// reads the buffer from the node and is told the damage at the next sync.-bool rediwm_projection_present_owned(struct rediwm_projection *p,- struct rediwm_buffer *source,- struct wlr_renderer *renderer,- struct wlr_buffer *buffer,- const pixman_region32_t *damage) {- struct wlr_client_buffer *current =- source->buffer ? wlr_client_buffer_get(source->buffer) : NULL;- bool same_size = current && current->base.width == buffer->width &&- current->base.height == buffer->height;- if (same_size) {- // A full repaint still fits the existing texture.- pixman_region32_t whole;- pixman_region32_init_rect(&whole, 0, 0, buffer->width, buffer->height);- bool updated = wlr_client_buffer_apply_damage(current, buffer,- damage ? damage : &whole);- pixman_region32_fini(&whole);- if (updated) {- rediwm_projection_damage_buffer(p, &source->node, damage);- return true;- }- }- struct wlr_client_buffer *next = wlr_client_buffer_create(buffer, renderer);- if (!next) {- // Without a texture wrapper, fall back to the plain buffer.- if (current)- current->private.n_ignore_locks--;- rediwm_buffer_set_content(source, buffer);- rediwm_projection_damage_buffer(p, &source->node, NULL);- return false;- }- if (current)- current->private.n_ignore_locks--;- rediwm_buffer_set_content(source, &next->base);- next->private.n_ignore_locks++;- // The node's lock keeps it alive; drop the one creation handed us.- wlr_buffer_unlock(&next->base);- // Renderers without in-place updates (pixman) land here every frame. The- // replacement differs from its predecessor only where `damage` says, so a- // same-sized replacement keeps precise output damage.- rediwm_projection_damage_buffer(p, &source->node, same_size ? damage : NULL);- return false;-}-void rediwm_projection_damage_buffer(struct rediwm_projection *p,- struct rediwm_node *source,- const pixman_region32_t *damage) {- struct leaf *l = source->leaf;- // A leaf's first projection applies its whole buffer anyway.- if (!l || l->projection != p)- return;- if (l->surface)- return;- if (!damage ||- !pixman_region32_union(&l->damage, &l->damage, damage))- l->full_damage = true;- l->owned_damage = true;- l->dirty = true;- mark_group(p, l->group);- schedule(p);-}-static uint32_t region_area(const pixman_region32_t *region) {- int count = 0;- const pixman_box32_t *box = pixman_region32_rectangles((pixman_region32_t *)region, &count);- uint32_t area = 0;- for (int i = 0; i < count; i++)- area += (uint32_t)(box[i].x2 - box[i].x1) * (uint32_t)(box[i].y2 - box[i].y1);- return area;-}-static struct leaf *transport_leaf(struct rediwm_projection *p, struct rediwm_node *node) {- struct leaf *l = node->leaf;- return l && l->projection == p ? l : NULL;-}-static struct leaf *feedback_leaf(struct rediwm_projection *p, struct rediwm_node *node) {- struct leaf *l = transport_leaf(p, node);- return l && l->feedback ? l : NULL;-}-// What of an item nothing above hides, as the output pass last saw it (layout space).-void rediwm_projection_note_visible(struct rediwm_projection *p, struct rediwm_node *node,- const pixman_region32_t *visible) {- struct leaf *l = transport_leaf(p, node);- if (!l) return;- pixman_region32_copy(&l->visible, visible);- l->has_visible = true;-}-void rediwm_projection_feedback_begin(struct rediwm_projection *p) {- // Whatever this build does not reach (off every output, hidden, gone) shows- // nothing: damage that only changes what such a leaf shows is dropped.- struct leaf *leaf;- wl_list_for_each(leaf, &p->leaves, link) {- pixman_region32_clear(&leaf->visible);- leaf->has_visible = true;- }- struct feedback *f;- wl_list_for_each(f, &p->feedback, link) memset(f->area, 0, sizeof(f->area));- struct rediwm_scene_output *out;- wl_list_for_each(out, &p->outputs->outputs, link) {- const unsigned i = out->index % FEEDBACK_OUTPUTS;- if (p->shown_output[i].known &&- (p->shown_output[i].scale != out->output->scale ||- p->shown_output[i].transform != out->output->transform ||- p->shown_output[i].subpixel != out->output->subpixel))- p->forced |= 1ull << i;- p->shown_output[i].scale = out->output->scale;- p->shown_output[i].transform = out->output->transform;- p->shown_output[i].subpixel = out->output->subpixel;- p->shown_output[i].known = true;- }-}-// `visible` is what of the surface nothing above it hides, over the whole layout.-void rediwm_projection_feedback_note(struct rediwm_projection *p, struct rediwm_node *node,- const pixman_region32_t *visible) {- struct leaf *l = feedback_leaf(p, node);- if (!l || !pixman_region32_not_empty((pixman_region32_t *)visible)) return;- struct rediwm_scene_output *out;- wl_list_for_each(out, &p->outputs->outputs, link) {- if (!out->output->enabled) continue;- struct wlr_box box = {.x = out->x, .y = out->y};- wlr_output_effective_resolution(out->output, &box.width, &box.height);- pixman_region32_t shown;- pixman_region32_init(&shown);- pixman_region32_intersect_rect(&shown, (pixman_region32_t *)visible, box.x, box.y, box.width,- box.height);- l->feedback->area[out->index % FEEDBACK_OUTPUTS] = region_area(&shown);- pixman_region32_fini(&shown);- }-}-// The outputs and primary output of one surface from the last report of every-// output, and the signals of what changed.-static void update_feedback(struct leaf *l) {- struct feedback *f = l->feedback;- struct rediwm_scene_outputs *scene = l->projection->outputs;- struct rediwm_scene_output *out;- uint64_t total = 0;- wl_list_for_each(out, &scene->outputs, link)- if (out->output->enabled) total += f->area[out->index % FEEDBACK_OUTPUTS];- uint32_t largest = 0;- uint64_t active = 0;- size_t count = 0;- struct rediwm_scene_output *primary = NULL;- wl_list_for_each(out, &scene->outputs, link) {- if (!out->output->enabled) continue;- const uint32_t overlap = f->area[out->index % FEEDBACK_OUTPUTS];- // Ten percent of what is visible or less, and the output is ignored.- if (overlap > 0.1 * total) {- if (overlap >= largest) {- largest = overlap;- primary = out;- }- active |= 1ull << (out->index % FEEDBACK_OUTPUTS);- count++;- }- }- struct rediwm_buffer *b = rediwm_buffer_from_node(l->source);- const uint64_t before = f->active;- struct rediwm_scene_output *before_primary = f->primary;- f->active = active;- f->primary = primary;- if (before == active && before_primary == primary && !(active & l->projection->forced)) return;- struct rediwm_scene_output *outputs[FEEDBACK_OUTPUTS];- struct rediwm_outputs_update_event event = {.active = outputs, .size = count};- size_t i = 0;- wl_list_for_each(out, &scene->outputs, link)- if (active & (1ull << (out->index % FEEDBACK_OUTPUTS))) outputs[i++] = out;- wl_signal_emit_mutable(&b->events.outputs_update, &event);-}-void rediwm_projection_feedback_end(struct rediwm_projection *p) {- struct feedback *f, *next;- wl_list_for_each_safe(f, next, &p->feedback, link) update_feedback(f->leaf);- p->forced = 0;-}-// Frame callbacks for the world: the surfaces shown on any-// output (the surface itself picks the output that paces it).-void rediwm_projection_send_frame_done(struct rediwm_projection *p, struct rediwm_scene_output *out,- struct timespec *now) {- struct feedback *f, *next;- wl_list_for_each_safe(f, next, &p->feedback, link) {- if (!f->active || !f->leaf->seen || !f->leaf->item.enabled) continue;- struct rediwm_frame_done_event event = {.output = out, .when = *now};- wl_signal_emit_mutable(&rediwm_buffer_from_node(f->leaf->source)->events.frame_done, &event);- }-}// The group a node belongs to: the child of a layer it hangs under (root 0, layer 1,// group 2), or NULL for the root and the layers, and for a node not yet grouped.static struct group *group_of(struct rediwm_node *node) {@@ -1178,146 +227,6 @@ struct rediwm_projection *rediwm_projection_create(struct rediwm_scene *own, strown->changed_data = p;return p;}-// Source tree -> graph. Every adapter is detached from the paint lists first, so-// the lists hold exactly what this pass reaches, in source paint order.-static void resolve(struct rediwm_projection *p, double z, double cx, double cy,- double ox, double oy, struct rediwm_tree *fixed, bool full) {- struct leaf *l;- struct group *g;- // A full pass walks every group; otherwise only the marked ones are, and the rest- // keep the leaves and items they had.- if (full) {- wl_list_for_each(l, &p->leaves, link) {- l->seen = false;- detach(&l->paint);- }- }- wl_list_for_each(g, &p->groups, link) {- g->seen = false;- g->touched = false;- detach(&g->paint);- }- const struct rediwm_xform camera = rediwm_xform_camera(z, cx, cy, ox, oy);- const struct rediwm_xform identity = {1, 0, 0};- struct rediwm_node *layer;- wl_list_for_each(layer, &p->own->tree.children, link) {- if (layer->type != REDIWM_NODE_TREE) continue;- const bool world = layer == &p->world->node;- // The world is resolved from its children, ignoring its own place and state; any- // other layer is a tree like the rest and hides what it holds when disabled.- const struct walk base = {.x = world ? 0 : layer->x, .y = world ? 0 : layer->y,- .enabled = world || layer->enabled, .t = world ? camera : identity};- struct rediwm_node *node;- wl_list_for_each(node, &((struct rediwm_tree *)layer)->children, link) {- g = get_group(p, node);- if (!g) {- wlr_log(WLR_ERROR, "zoom: cannot allocate presentation group");- continue;- }- g->seen = true;- g->enabled = base.enabled && node->enabled;- wl_list_insert(p->paint.prev, &g->paint);- if (!full && g->resolved && !g->dirty && !g->has_effect) continue;- if (!full) {- struct leaf *next;- wl_list_for_each_safe(l, next, &g->items, paint) {- l->seen = false;- detach(&l->paint);- }- }- struct walk w = base;- // The fixed subtree is laid out as is; everything else follows the camera.- if (world && fixed && node == &fixed->node) w.t = identity;- g->resolved = true;- g->dirty = false;- g->touched = true;- g->has_effect = false;- resolve_own(p, g, node, &w);- }- }-}-static void damage_restacked(struct rediwm_projection *p);-// Graph -> damage, then forget what the graph no longer reaches.-static void present(struct rediwm_projection *p, bool full) {- struct group *g;- wl_list_for_each(g, &p->paint, paint) {- if (!full && !g->touched) continue;- struct leaf *l, *next;- wl_list_for_each_safe(l, next, &g->items, paint) present_leaf(l);- }- 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});- l->ranked = false;- }- // Nothing was stacked differently unless the shape of the tree changed.- if (full) damage_restacked(p);-}-// A leaf that is above what it was below, or the reverse, changes every pixel of-// the two where they overlap, and nothing else tells the screen: wlroots damaged a-// node it restacked. The leaves still there are checked in paint order against-// the order of the last sync; when it differs, the ones that must have moved (all-// but a longest run that kept its order) are damaged whole. Any pair that swapped-// has at least one of them outside that run, and the overlap lies in its box.-static void damage_restacked(struct rediwm_projection *p) {- size_t n = 0;- bool ordered = true, have = false;- uint32_t last = 0;- struct group *g;- struct leaf *l;- wl_list_for_each(g, &p->paint, paint) wl_list_for_each(l, &g->items, paint) {- if (!l->ranked) continue;- if (have && l->rank < last) ordered = false;- last = l->rank;- have = true;- n++;- }- if (!ordered) {- struct leaf **leaves = malloc(n * sizeof(*leaves));- size_t *tails = malloc(n * sizeof(*tails));- long *before = malloc(n * sizeof(*before));- bool *kept = calloc(n, sizeof(*kept));- if (leaves && tails && before && kept) {- size_t count = 0, length = 0;- wl_list_for_each(g, &p->paint, paint) wl_list_for_each(l, &g->items, paint)- if (l->ranked) leaves[count++] = l;- // Longest strictly increasing run of the old ranks.- for (size_t i = 0; i < count; i++) {- size_t lo = 0, hi = length;- while (lo < hi) {- const size_t mid = (lo + hi) / 2;- if (leaves[tails[mid]]->rank < leaves[i]->rank) lo = mid + 1;- else hi = mid;- }- before[i] = lo > 0 ? (long)tails[lo - 1] : -1;- tails[lo] = i;- if (lo == length) length++;- }- for (long i = length ? (long)tails[length - 1] : -1; i >= 0; i = before[i]) kept[i] = true;- for (size_t i = 0; i < count; i++) {- struct leaf *moved = leaves[i];- if (kept[i]) continue;- // A shadow shows no box of its own (`shown`), but it lies over or under- // what was restacked all the same.- if (moved->has_shown) damage_box(moved, moved->shown, false);- if (moved->has_shadow_drawn) damage_box(moved, moved->shadow_drawn, false);- }- } else {- // Out of memory: treat the whole layout as changed.- pixman_region32_union_rect(&p->pending, &p->pending, INT32_MIN / 2, INT32_MIN / 2, INT32_MAX, INT32_MAX);- }- free(leaves);- free(tails);- free(before);- free(kept);- }- uint32_t rank = 0;- wl_list_for_each(g, &p->paint, paint) wl_list_for_each(l, &g->items, paint) {- l->rank = rank++;- l->ranked = true;- }-}static void apply_damage(struct rediwm_projection *p) {if (pixman_region32_not_empty(&p->pending)) rediwm_scene_damage_layout(p->outputs, &p->pending);pixman_region32_clear(&p->pending);@@ -1340,7 +249,7 @@ static void audit_gate(struct rediwm_projection *p, double z, double cx, doublebefore[i] = l->item;who[i++] = l;}- resolve(p, z, cx, cy, ox, oy, fixed, true);+ projection_resolve(p, z, cx, cy, ox, oy, fixed, true);for (i = 0; i < n; i++) {const struct rediwm_item *a = &before[i], *b = &who[i]->item;if (a->enabled != b->enabled || a->kind != b->kind || memcmp(&a->edges, &b->edges, sizeof(a->edges)) ||@@ -1367,118 +276,19 @@ void rediwm_projection_sync_fixed(struct rediwm_projection *p, double z, doublep->have_camera = true;p->fixed = fixed;memcpy(p->camera, camera, sizeof(camera));- resolve(p, z, cx, cy, ox, oy, fixed, full);+ projection_resolve(p, z, cx, cy, ox, oy, fixed, full);#ifdef REDIWM_DAMAGE_AUDIT// Self-check of the gate: what a full walk would put in the graph must be what the// gated one did. Reports every leaf that differs, then goes on with the full result.if (!full) audit_gate(p, z, cx, cy, ox, oy, fixed);#endif- present(p, full);+ projection_present(p, full);apply_damage(p);}void rediwm_projection_sync(struct rediwm_projection *p, double z, double cx,double cy, double ox, double oy) {rediwm_projection_sync_fixed(p, z, cx, cy, ox, oy, NULL);}-bool rediwm_projection_for_each_item(struct rediwm_projection *p, bool reverse, rediwm_item_iterator iterator, void *data) {- for (struct wl_list *link = reverse ? p->paint.prev : p->paint.next;- link != &p->paint; link = reverse ? link->prev : link->next) {- struct group *g = wl_container_of(link, g, paint);- if (!g->enabled) continue;- for (struct wl_list *leaf_link = reverse ? g->items.prev : g->items.next;- leaf_link != &g->items; leaf_link = reverse ? leaf_link->prev : leaf_link->next) {- struct leaf *l = wl_container_of(leaf_link, l, paint);- if (l->item.enabled && !iterator(&l->item, l->source, data)) return false;- }- }- return true;-}-// A node's opaque region (in its own units) as it lands on screen, never larger-// than the real one.-//-// wlroots knows a client surface is opaque from its raw buffer's format (an XRGB-// buffer covers the whole surface) or from the client's own opaque region, and-// keeps that in the node's opaque region; the node's buffer is a wlr_client_buffer,-// which reveals no pixel format, so its own flag is false for every client. A node-// shown 1:1 uses the region as is. Otherwise each rectangle is scaled with the-// node and pulled one pixel inward, so rounding and the filter's reach beyond an-// opaque edge can never mark a translucent pixel opaque.-void rediwm_mirror_opaque(pixman_region32_t *out, const pixman_region32_t *source, int source_width,- int source_height, int width, int height) {- int count = 0;- const pixman_box32_t *box = pixman_region32_rectangles((pixman_region32_t *)source, &count);- const bool exact = width == source_width && height == source_height;- for (int i = 0; i < count; i++) {- if (exact) {- pixman_region32_union_rect(out, out, box[i].x1, box[i].y1, box[i].x2 - box[i].x1, box[i].y2 - box[i].y1);- } else if (source_width > 0 && source_height > 0) {- const double sx = (double)width / source_width;- const double sy = (double)height / source_height;- const int x1 = (int)ceil(box[i].x1 * sx) + 1;- const int y1 = (int)ceil(box[i].y1 * sy) + 1;- const int x2 = (int)floor(box[i].x2 * sx) - 1;- const int y2 = (int)floor(box[i].y2 * sy) - 1;- if (x2 > x1 && y2 > y1) pixman_region32_union_rect(out, out, x1, y1, x2 - x1, y2 - y1);- }- }- pixman_region32_intersect_rect(out, out, 0, 0, width, height);-}-// ---- Hit testing (stage 6 of docs/plans/scene-graph.md) ---------------------------// The topmost node at a layout point, from the graph: the items from the top of the-// paint order down, each tested at the pixels it covers once rounded, then against-// the window's shape and the node's own input rule.-// `only` restricts the pick to the leaves of one top-level subtree.-static bool pick_leaf(struct rediwm_projection *p, const struct group *only, double lx, double ly,- struct rediwm_hit *hit) {- const double px = floor(lx), py = floor(ly);- for (struct wl_list *gl = p->paint.prev; gl != &p->paint; gl = gl->prev) {- struct group *g = wl_container_of(gl, g, paint);- if (!g->enabled || (only && g != only)) continue;- for (struct wl_list *ll = g->items.prev; ll != &g->items; ll = ll->prev) {- struct leaf *l = wl_container_of(ll, l, paint);- const struct rediwm_item *item = &l->item;- // An effect takes no input, and a leaf with no pixels once rounded is not shown.- if (!item->enabled || item->kind == REDIWM_ITEM_EFFECT || l->inert) continue;- const struct rediwm_snap s = rediwm_edges_snap(item->edges);- if (s.width <= 0 || s.height <= 0 || px < s.x || px >= s.x + s.width || py < s.y ||- py >= s.y + s.height)- continue;- double x = lx - s.x, y = ly - s.y;- if (l->source->type == REDIWM_NODE_BUFFER) {- x *= (double)item->width / s.width;- y *= (double)item->height / s.height;- // A window's rounded corners are transparent: a point the shape cuts away- // goes to what is below. The point is in source units; the shape is in- // layout space.- if (item->clip && item->width > 0 && item->height > 0) {- const struct rediwm_edges *e = &item->edges;- const double cx = e->x1 + x * (e->x2 - e->x1) / item->width;- const double cy = e->y1 + y * (e->y2 - e->y1) / item->height;- if (rediwm_clip_distance(item->clip, cx, cy) > 0) continue;- }- struct rediwm_buffer *own = rediwm_buffer_from_node(l->own);- if (own->point_accepts_input && !own->point_accepts_input(own, &x, &y)) continue;- }- *hit = (struct rediwm_hit){.own = l->own, .x = x, .y = y};- return true;- }- }- return false;-}-bool rediwm_projection_node_at(struct rediwm_projection *p, double lx, double ly,- struct rediwm_hit *hit) {- return pick_leaf(p, NULL, lx, ly, hit);-}-// The topmost leaf of one top-level subtree of a layer (a window's frame tree) at a-// point, whatever lies above it.-bool rediwm_projection_node_at_in(struct rediwm_projection *p, struct rediwm_node *subtree,- double lx, double ly, struct rediwm_hit *hit) {- struct wlr_addon *addon = wlr_addon_find(&subtree->addons, p, &group_source_impl);- struct group *g;- if (!addon) return false;- g = wl_container_of(addon, g, source_lookup);- return pick_leaf(p, g, lx, ly, hit);-}static void watched_commit(struct wl_listener *listener, void *data) {(void)data;struct watched_surface *watch = wl_container_of(listener, watch, commit);diff --git a/src/projection_internal.h b/src/projection_internal.hnew file mode 100644index 0000000..b65e6ab--- /dev/null+++ b/src/projection_internal.h@@ -0,0 +1,283 @@+// Private to projection*.c and projection_test.c: the adapters the graph is+// built from and the small helpers its parts share. Everyone else uses+// projection.h. Helpers called per leaf are `static inline` here, as no file+// boundary may cost them their inlining.+#ifndef REDIWM_PROJECTION_INTERNAL_H+#define REDIWM_PROJECTION_INTERNAL_H++#define WLR_USE_UNSTABLE+#define WLR_PRIVATE private+#include "projection.h"+#include "glass.h"+#include "node.h"+#include "scene_graph.h"+#include "scene_damage.h"+#include <math.h>+#include <stdlib.h>+#include <string.h>+#include <wlr/types/wlr_buffer.h>+#include <wlr/types/wlr_compositor.h>+#include <wlr/types/wlr_output.h>+#include "node_surface.h"+#include "scene_outputs.h"+#include <wlr/util/addon.h>+#include <wlr/util/log.h>++// 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 {+ // The compositor's scene. The children of its root are layers, bottom to top:+ // `world` follows the camera, every other layer is laid out as is. The wlroots+ // scene is kept for its outputs.+ struct rediwm_scene *own;+ struct rediwm_tree *world;+ struct rediwm_scene_outputs *outputs;+ // What changed since the last sync. A change to one top-level subtree (a group) marks+ // that group; a change to the shape of the tree, a layer or anything not tied to+ // one group marks them all (`all_dirty`). A sync resolves and presents only the+ // marked groups, or none when nothing changed and the camera did not move.+ bool any_dirty, all_dirty, have_camera;+ double camera[5];+ struct rediwm_tree *fixed;+ // Every adapter, in creation order; `paint` holds the groups resolve() last+ // reached, bottom to top.+ struct wl_list leaves, groups, paint;+ // The surface leaves whose outputs the output pass reports (stage 6), and what+ // each output (by scene output index) looked like at the last report.+ struct wl_list feedback;+ struct {+ float scale;+ enum wl_output_transform transform;+ enum wl_output_subpixel subpixel;+ bool known;+ } shown_output[64];+ // The outputs whose scale, transform or subpixel order changed since: a surface+ // on them is told again even where nothing else changed, as wlroots does, for+ // its preferred scale.+ uint64_t forced;+ struct wl_listener new_surface;+ struct wl_list surfaces;+ uint64_t next_leaf_id;+ // Layout-space damage the sync in progress collects, applied to every output+ // when it ends (stage 6b).+ pixman_region32_t pending;+ 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: each is a group, and+// a window going away damages everything its leaves showed at once.+struct group {+ struct wl_list link;+ // In projection->paint, or alone, when the last resolve() did not reach it.+ struct wl_list paint;+ // The leaves resolve() last reached under this group, bottom to top.+ struct wl_list items;+ bool enabled;+ // Changed since resolve() last walked it; walked at least once; walked by this sync.+ bool dirty, resolved, touched;+ // Holds a glass effect, whose box is its target's, which may be a node of another group:+ // such a group is walked whenever anything is.+ bool has_effect;+ struct rediwm_projection *projection;+ struct rediwm_node *source;+ struct wl_listener destroy;+ struct wlr_addon source_lookup;+ bool seen;+};+// A list link that is always valid to remove: alone, or in a live list.+static inline void detach(struct wl_list *link) {+ wl_list_remove(link);+ wl_list_init(link);+}+struct leaf {+ struct wl_list link;+ // In its group's `items` when the last resolve() reached it, else alone.+ struct wl_list paint;+ struct rediwm_projection *projection;+ // The node the pass reads the pixels from (`transport`): the buffer or rect itself.+ struct rediwm_node *source;+ // The own node it is.+ struct rediwm_node *own;+ // The group the last resolve() put it in.+ struct group *group;+ // A glass effect: decided when the leaf is made, as an effect is its own node.+ bool effect;+ struct wl_listener destroy, commit;+ struct wlr_addon source_lookup;+ struct rediwm_surface *surface;+ struct feedback *feedback;+ bool dirty, seen;+ // Under an inert tree at the last resolve(): drawn, never picked.+ bool inert;+ // Commits can outpace presentation. Keep their union, including empty damage+ // on a rotated buffer; buffer identity is not evidence of changed pixels.+ pixman_region32_t damage;+ bool full_damage;+ // A compositor-owned buffer reported its own damage since the last sync;+ // without a report a replaced buffer damages its whole box.+ bool owned_damage;+ int damage_width, damage_height;+ // 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;+ // `item.shadow` points at `shadow` when the leaf is under a shadow tree. A+ // shadow has no buffer to damage: 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;+ // What of the leaf nothing above hid when the output pass last built, over the+ // whole layout: damage that changes only what the leaf shows is cut to it, as+ // wlroots cut it to a node's visible region. Not known until the first build.+ pixman_region32_t visible;+ bool has_visible;+ // Position in paint order at the last sync, to tell a restack.+ uint32_t rank;+ bool ranked;+ struct rediwm_shadow shadow_drawn_as;+ struct rediwm_snap shadow_drawn;+ float shadow_drawn_opacity;+ bool has_shadow_drawn;+ struct wlr_buffer *last_buffer;+};+// 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 and dim 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 frame. The subtree's own+// clip or dim, if it sets one, still applies;+// * dim: every leaf under the tree loses this fraction of its brightness and of+// its colour (`rediwm_content.dim_*`), inherited like the clip. It is a field of+// the item, so a change of it damages what the leaves show with no rule of its+// own, and the pass applies it per draw. A subtree's own dim replaces the outer+// one, and `unclipped` ends it as it ends the clip;+// * shadow: every leaf under the tree is the analytic shadow of this shape+// (REDIWM_ITEM_SHADOW), resolved like the clip;+// * inert: the subtree is shown but takes no input: the pick passes through it+// to what is below (a window sliding off a workspace it has left).+struct dim {+ float brightness, saturation;+};+struct tree_props {+ struct wlr_addon addon;+ // Where an own node keeps its properties (`rediwm_node.props`), cleared with them.+ void **slot;+ bool has_zoom, has_scale, has_clip, unclipped, has_shadow, inert, has_dim;+ double zoom, scale;+ struct dim dim;+ struct rediwm_clip_spec clip;+ struct rediwm_shadow_spec shadow;+ // The clip as the last resolve() saw it in layout space, to tell a changed+ // shape (to be damaged) from a moved window (whose leaves damage themselves).+ struct rediwm_clip applied;+ bool has_applied;+};+extern const struct wlr_addon_interface projection_tree_props_impl;+static inline struct tree_props *tree_props_find(struct wlr_addon_set *set, void **slot) {+ if (slot) return *slot;+ struct wlr_addon *addon = wlr_addon_find(set, NULL, &projection_tree_props_impl);+ struct tree_props *props;+ return addon ? (props = wl_container_of(addon, props, addon)) : NULL;+}+struct watched_surface {+ struct rediwm_projection *projection;+ struct wlr_surface *surface;+ struct wl_list link;+ struct wl_listener commit, destroy;+};+static inline void mark_all(struct rediwm_projection *p) { p->any_dirty = p->all_dirty = true; }+static inline void mark_group(struct rediwm_projection *p, struct group *g) {+ if (!g) {+ mark_all(p);+ return;+ }+ g->dirty = true;+ p->any_dirty = true;+}+static inline void schedule(struct rediwm_projection *p) {+ struct rediwm_scene_output *out;+ wl_list_for_each(out, &p->outputs->outputs, link)+ wlr_output_schedule_frame(out->output);+}+// ---- Surface feedback (stage 6 of docs/plans/scene-graph.md) ---------------------+// wlr_scene tells a surface where it is shown by emitting its scene buffer's own+// signals, from the node's visible region. The world's source tree is disabled, so+// wlroots sees nothing there; the output pass, which draws the graph, knows what+// is shown.+// For every client surface it reports, per output it builds, how many pixels are+// visible (not hidden by what is above), and this keeps the last report of each+// output. From the sums it applies wlroots' own rule (scene.c,+// update_node_update_outputs): an output is active when it shows more than a tenth+// of what is visible on all of them, and the primary one is the one showing most+// (the last, on a tie). It then emits the signals the surface binding listens to+// (node_surface.c) on the source buffer.+enum { FEEDBACK_OUTPUTS = 64 };+struct feedback {+ struct wl_list link;+ struct leaf *leaf;+ // Visible pixels as the last build of each output (scene output index) saw them.+ uint32_t area[FEEDBACK_OUTPUTS];+ uint64_t active;+ struct rediwm_scene_output *primary;+};+static inline 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 inline void emit_snap(struct leaf *l, struct rediwm_snap s) {+ emit(l->projection, l->item.id, s.x, s.y, s.width, s.height);+}+// Layout-space damage for the screen, applied when the sync ends. `visible_only`+// cuts it to what the leaf showed at the last build: a change of what the leaf+// itself shows is invisible where something opaque lay over it, and anything that+// stops covering it damages that region itself.+static inline void damage_box(struct leaf *l, struct rediwm_snap s, bool visible_only) {+ if (s.width <= 0 || s.height <= 0) return;+ pixman_region32_t *pending = &l->projection->pending;+ if (visible_only && l->has_visible) {+ pixman_region32_t region;+ pixman_region32_init_rect(®ion, s.x, s.y, s.width, s.height);+ pixman_region32_intersect(®ion, ®ion, &l->visible);+ pixman_region32_union(pending, pending, ®ion);+ pixman_region32_fini(®ion);+ } else {+ pixman_region32_union_rect(pending, pending, s.x, s.y, s.width, s.height);+ }+}+static inline void forget_shown(struct leaf *l) {+ if (l->has_shown) {+ emit_snap(l, l->shown);+ damage_box(l, l->shown, true);+ }+ l->has_shown = false;+}++// Shared between the parts (projection_*.c).+extern const struct wlr_addon_interface projection_group_source_impl;+struct group *projection_get_group(struct rediwm_projection *p, struct rediwm_node *source);+struct leaf *projection_get_leaf(struct rediwm_projection *p, struct rediwm_node *source,+ struct rediwm_node *own);+void projection_free_feedback(struct leaf *l);+void projection_resolve(struct rediwm_projection *p, double z, double cx, double cy,+ double ox, double oy, struct rediwm_tree *fixed, bool full);+void projection_present(struct rediwm_projection *p, bool full);+void projection_damage_shadow(struct leaf *l, bool show, struct rediwm_snap at);++#endifdiff --git a/src/projection_pass.c b/src/projection_pass.cnew file mode 100644index 0000000..605d5d3--- /dev/null+++ b/src/projection_pass.c@@ -0,0 +1,175 @@+// What the output pass reads from the graph and reports back: items in paint+// order, mirrored opaque regions, what of each leaf is visible, surface+// feedback and frame callbacks. Part of projection.c.+#include "projection_internal.h"++void projection_free_feedback(struct leaf *l) {+ if (!l->feedback) return;+ wl_list_remove(&l->feedback->link);+ free(l->feedback);+ l->feedback = NULL;+}+static uint32_t region_area(const pixman_region32_t *region) {+ int count = 0;+ const pixman_box32_t *box = pixman_region32_rectangles((pixman_region32_t *)region, &count);+ uint32_t area = 0;+ for (int i = 0; i < count; i++)+ area += (uint32_t)(box[i].x2 - box[i].x1) * (uint32_t)(box[i].y2 - box[i].y1);+ return area;+}+static struct leaf *transport_leaf(struct rediwm_projection *p, struct rediwm_node *node) {+ struct leaf *l = node->leaf;+ return l && l->projection == p ? l : NULL;+}+static struct leaf *feedback_leaf(struct rediwm_projection *p, struct rediwm_node *node) {+ struct leaf *l = transport_leaf(p, node);+ return l && l->feedback ? l : NULL;+}+// What of an item nothing above hides, as the output pass last saw it (layout space).+void rediwm_projection_note_visible(struct rediwm_projection *p, struct rediwm_node *node,+ const pixman_region32_t *visible) {+ struct leaf *l = transport_leaf(p, node);+ if (!l) return;+ pixman_region32_copy(&l->visible, visible);+ l->has_visible = true;+}+void rediwm_projection_feedback_begin(struct rediwm_projection *p) {+ // Whatever this build does not reach (off every output, hidden, gone) shows+ // nothing: damage that only changes what such a leaf shows is dropped.+ struct leaf *leaf;+ wl_list_for_each(leaf, &p->leaves, link) {+ pixman_region32_clear(&leaf->visible);+ leaf->has_visible = true;+ }+ struct feedback *f;+ wl_list_for_each(f, &p->feedback, link) memset(f->area, 0, sizeof(f->area));+ struct rediwm_scene_output *out;+ wl_list_for_each(out, &p->outputs->outputs, link) {+ const unsigned i = out->index % FEEDBACK_OUTPUTS;+ if (p->shown_output[i].known &&+ (p->shown_output[i].scale != out->output->scale ||+ p->shown_output[i].transform != out->output->transform ||+ p->shown_output[i].subpixel != out->output->subpixel))+ p->forced |= 1ull << i;+ p->shown_output[i].scale = out->output->scale;+ p->shown_output[i].transform = out->output->transform;+ p->shown_output[i].subpixel = out->output->subpixel;+ p->shown_output[i].known = true;+ }+}+// `visible` is what of the surface nothing above it hides, over the whole layout.+void rediwm_projection_feedback_note(struct rediwm_projection *p, struct rediwm_node *node,+ const pixman_region32_t *visible) {+ struct leaf *l = feedback_leaf(p, node);+ if (!l || !pixman_region32_not_empty((pixman_region32_t *)visible)) return;+ struct rediwm_scene_output *out;+ wl_list_for_each(out, &p->outputs->outputs, link) {+ if (!out->output->enabled) continue;+ struct wlr_box box = {.x = out->x, .y = out->y};+ wlr_output_effective_resolution(out->output, &box.width, &box.height);+ pixman_region32_t shown;+ pixman_region32_init(&shown);+ pixman_region32_intersect_rect(&shown, (pixman_region32_t *)visible, box.x, box.y, box.width,+ box.height);+ l->feedback->area[out->index % FEEDBACK_OUTPUTS] = region_area(&shown);+ pixman_region32_fini(&shown);+ }+}+// The outputs and primary output of one surface from the last report of every+// output, and the signals of what changed.+static void update_feedback(struct leaf *l) {+ struct feedback *f = l->feedback;+ struct rediwm_scene_outputs *scene = l->projection->outputs;+ struct rediwm_scene_output *out;+ uint64_t total = 0;+ wl_list_for_each(out, &scene->outputs, link)+ if (out->output->enabled) total += f->area[out->index % FEEDBACK_OUTPUTS];+ uint32_t largest = 0;+ uint64_t active = 0;+ size_t count = 0;+ struct rediwm_scene_output *primary = NULL;+ wl_list_for_each(out, &scene->outputs, link) {+ if (!out->output->enabled) continue;+ const uint32_t overlap = f->area[out->index % FEEDBACK_OUTPUTS];+ // Ten percent of what is visible or less, and the output is ignored.+ if (overlap > 0.1 * total) {+ if (overlap >= largest) {+ largest = overlap;+ primary = out;+ }+ active |= 1ull << (out->index % FEEDBACK_OUTPUTS);+ count++;+ }+ }+ struct rediwm_buffer *b = rediwm_buffer_from_node(l->source);+ const uint64_t before = f->active;+ struct rediwm_scene_output *before_primary = f->primary;+ f->active = active;+ f->primary = primary;+ if (before == active && before_primary == primary && !(active & l->projection->forced)) return;+ struct rediwm_scene_output *outputs[FEEDBACK_OUTPUTS];+ struct rediwm_outputs_update_event event = {.active = outputs, .size = count};+ size_t i = 0;+ wl_list_for_each(out, &scene->outputs, link)+ if (active & (1ull << (out->index % FEEDBACK_OUTPUTS))) outputs[i++] = out;+ wl_signal_emit_mutable(&b->events.outputs_update, &event);+}+void rediwm_projection_feedback_end(struct rediwm_projection *p) {+ struct feedback *f, *next;+ wl_list_for_each_safe(f, next, &p->feedback, link) update_feedback(f->leaf);+ p->forced = 0;+}+// Frame callbacks for the world: the surfaces shown on any+// output (the surface itself picks the output that paces it).+void rediwm_projection_send_frame_done(struct rediwm_projection *p, struct rediwm_scene_output *out,+ struct timespec *now) {+ struct feedback *f, *next;+ wl_list_for_each_safe(f, next, &p->feedback, link) {+ if (!f->active || !f->leaf->seen || !f->leaf->item.enabled) continue;+ struct rediwm_frame_done_event event = {.output = out, .when = *now};+ wl_signal_emit_mutable(&rediwm_buffer_from_node(f->leaf->source)->events.frame_done, &event);+ }+}+bool rediwm_projection_for_each_item(struct rediwm_projection *p, bool reverse, rediwm_item_iterator iterator, void *data) {+ for (struct wl_list *link = reverse ? p->paint.prev : p->paint.next;+ link != &p->paint; link = reverse ? link->prev : link->next) {+ struct group *g = wl_container_of(link, g, paint);+ if (!g->enabled) continue;+ for (struct wl_list *leaf_link = reverse ? g->items.prev : g->items.next;+ leaf_link != &g->items; leaf_link = reverse ? leaf_link->prev : leaf_link->next) {+ struct leaf *l = wl_container_of(leaf_link, l, paint);+ if (l->item.enabled && !iterator(&l->item, l->source, data)) return false;+ }+ }+ return true;+}+// A node's opaque region (in its own units) as it lands on screen, never larger+// than the real one.+//+// wlroots knows a client surface is opaque from its raw buffer's format (an XRGB+// buffer covers the whole surface) or from the client's own opaque region, and+// keeps that in the node's opaque region; the node's buffer is a wlr_client_buffer,+// which reveals no pixel format, so its own flag is false for every client. A node+// shown 1:1 uses the region as is. Otherwise each rectangle is scaled with the+// node and pulled one pixel inward, so rounding and the filter's reach beyond an+// opaque edge can never mark a translucent pixel opaque.+void rediwm_mirror_opaque(pixman_region32_t *out, const pixman_region32_t *source, int source_width,+ int source_height, int width, int height) {+ int count = 0;+ const pixman_box32_t *box = pixman_region32_rectangles((pixman_region32_t *)source, &count);+ const bool exact = width == source_width && height == source_height;+ for (int i = 0; i < count; i++) {+ if (exact) {+ pixman_region32_union_rect(out, out, box[i].x1, box[i].y1, box[i].x2 - box[i].x1, box[i].y2 - box[i].y1);+ } else if (source_width > 0 && source_height > 0) {+ const double sx = (double)width / source_width;+ const double sy = (double)height / source_height;+ const int x1 = (int)ceil(box[i].x1 * sx) + 1;+ const int y1 = (int)ceil(box[i].y1 * sy) + 1;+ const int x2 = (int)floor(box[i].x2 * sx) - 1;+ const int y2 = (int)floor(box[i].y2 * sy) - 1;+ if (x2 > x1 && y2 > y1) pixman_region32_union_rect(out, out, x1, y1, x2 - x1, y2 - y1);+ }+ }+ pixman_region32_intersect_rect(out, out, 0, 0, width, height);+}diff --git a/src/projection_pick.c b/src/projection_pick.cnew file mode 100644index 0000000..056b3a0--- /dev/null+++ b/src/projection_pick.c@@ -0,0 +1,58 @@+#include "projection_internal.h"++// ---- Hit testing (stage 6 of docs/plans/scene-graph.md) --------------------------+// The topmost node at a layout point, from the graph: the items from the top of the+// paint order down, each tested at the pixels it covers once rounded, then against+// the window's shape and the node's own input rule.+// `only` restricts the pick to the leaves of one top-level subtree.+static bool pick_leaf(struct rediwm_projection *p, const struct group *only, double lx, double ly,+ struct rediwm_hit *hit) {+ const double px = floor(lx), py = floor(ly);+ for (struct wl_list *gl = p->paint.prev; gl != &p->paint; gl = gl->prev) {+ struct group *g = wl_container_of(gl, g, paint);+ if (!g->enabled || (only && g != only)) continue;+ for (struct wl_list *ll = g->items.prev; ll != &g->items; ll = ll->prev) {+ struct leaf *l = wl_container_of(ll, l, paint);+ const struct rediwm_item *item = &l->item;+ // An effect takes no input, and a leaf with no pixels once rounded is not shown.+ if (!item->enabled || item->kind == REDIWM_ITEM_EFFECT || l->inert) continue;+ const struct rediwm_snap s = rediwm_edges_snap(item->edges);+ if (s.width <= 0 || s.height <= 0 || px < s.x || px >= s.x + s.width || py < s.y ||+ py >= s.y + s.height)+ continue;+ double x = lx - s.x, y = ly - s.y;+ if (l->source->type == REDIWM_NODE_BUFFER) {+ x *= (double)item->width / s.width;+ y *= (double)item->height / s.height;+ // A window's rounded corners are transparent: a point the shape cuts away+ // goes to what is below. The point is in source units; the shape is in+ // layout space.+ if (item->clip && item->width > 0 && item->height > 0) {+ const struct rediwm_edges *e = &item->edges;+ const double cx = e->x1 + x * (e->x2 - e->x1) / item->width;+ const double cy = e->y1 + y * (e->y2 - e->y1) / item->height;+ if (rediwm_clip_distance(item->clip, cx, cy) > 0) continue;+ }+ struct rediwm_buffer *own = rediwm_buffer_from_node(l->own);+ if (own->point_accepts_input && !own->point_accepts_input(own, &x, &y)) continue;+ }+ *hit = (struct rediwm_hit){.own = l->own, .x = x, .y = y};+ return true;+ }+ }+ return false;+}+bool rediwm_projection_node_at(struct rediwm_projection *p, double lx, double ly,+ struct rediwm_hit *hit) {+ return pick_leaf(p, NULL, lx, ly, hit);+}+// The topmost leaf of one top-level subtree of a layer (a window's frame tree) at a+// point, whatever lies above it.+bool rediwm_projection_node_at_in(struct rediwm_projection *p, struct rediwm_node *subtree,+ double lx, double ly, struct rediwm_hit *hit) {+ struct wlr_addon *addon = wlr_addon_find(&subtree->addons, p, &projection_group_source_impl);+ struct group *g;+ if (!addon) return false;+ g = wl_container_of(addon, g, source_lookup);+ return pick_leaf(p, g, lx, ly, hit);+}diff --git a/src/projection_present.c b/src/projection_present.cnew file mode 100644index 0000000..4adee41--- /dev/null+++ b/src/projection_present.c@@ -0,0 +1,299 @@+// present(): graph -> damage (projection.c has the overview), and the+// buffers the compositor itself replaces.+#include "projection_internal.h"++// What a shadow draws changed, or it appeared or went: damage the footprint it+// last drew and the one it draws now, in layout space.+void projection_damage_shadow(struct leaf *l, bool show, struct rediwm_snap at) {+ const float opacity = l->item.content.opacity;+ if (show == l->has_shadow_drawn &&+ (!show || (at.x == l->shadow_drawn.x && at.y == l->shadow_drawn.y &&+ at.width == l->shadow_drawn.width && at.height == l->shadow_drawn.height &&+ opacity == l->shadow_drawn_opacity &&+ rediwm_shadow_equal(&l->shadow, &l->shadow_drawn_as))))+ return;+ pixman_region32_t region;+ pixman_region32_init(®ion);+ if (l->has_shadow_drawn)+ pixman_region32_union_rect(®ion, ®ion, l->shadow_drawn.x, l->shadow_drawn.y,+ l->shadow_drawn.width, l->shadow_drawn.height);+ if (show) pixman_region32_union_rect(®ion, ®ion, at.x, at.y, at.width, at.height);+ if (pixman_region32_not_empty(®ion)) {+ pixman_region32_union(&l->projection->pending, &l->projection->pending, ®ion);+ const pixman_box32_t *b = pixman_region32_extents(®ion);+ emit(l->projection, l->item.id, b->x1, b->y1, b->x2 - b->x1, b->y2 - b->y1);+ }+ pixman_region32_fini(®ion);+ l->has_shadow_drawn = show;+ l->shadow_drawn = at;+ l->shadow_drawn_as = l->shadow;+ l->shadow_drawn_opacity = opacity;+}++// The layout box a buffer-local box covers: through the source box onto the item's+// edges, one pixel wider for filtering, never beyond the leaf (damage outside its+// source box shows nowhere).+static struct rediwm_snap map_damage(const struct rediwm_item *item, const pixman_box32_t *b,+ const struct wlr_fbox *src, struct rediwm_snap snap) {+ 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;+ 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;+ return (struct rediwm_snap){cx1, cy1, cx2 - cx1, cy2 - cy1};+}+// What a commit's or a replaced buffer's damage changed: the event covers the+// bounding box of the buffer-local damage, the screen only the pieces of it (up to+// a few). 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);+ damage_box(l, snap, true);+ return;+ }+ if (!pixman_region32_not_empty((pixman_region32_t *)damage)) return;+ 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_snap whole =+ map_damage(item, pixman_region32_extents((pixman_region32_t *)damage), &src, snap);+ emit(l->projection, item->id, whole.x, whole.y, whole.width, whole.height);+ int count = 0;+ const pixman_box32_t *rects = pixman_region32_rectangles((pixman_region32_t *)damage, &count);+ if (count > 16) {+ damage_box(l, whole, true);+ return;+ }+ for (int i = 0; i < count; i++) {+ const struct rediwm_snap piece = map_damage(item, &rects[i], &src, snap);+ damage_box(l, piece, true);+ }+}+// present(): graph -> damage. Reads items, never the source's geometry; rounds+// and tells the screen and the damage events what changed.+static void present_leaf(struct leaf *l) {+ struct rediwm_item *item = &l->item;+ // Round in layout coordinates first. Rounding a scaled local offset would+ // change pixels at fractional zoom and when crossing negative origins.+ const struct rediwm_snap snap = rediwm_edges_snap(item->edges);+ const bool enabled = item->enabled && snap.width > 0 && snap.height > 0;+ // 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.+ // Only a leaf that puts pixels on screen has them: an input-only node (a+ // resize grab) moving with its window must not read as a change below glass,+ // and the pass draws effects itself (shadows report in damage_shadow).+ bool paints = enabled;+ if (item->kind == REDIWM_ITEM_RECT) {+ paints = paints && item->content.color[3] > 0;+ } else if (item->kind == REDIWM_ITEM_EFFECT || item->kind == REDIWM_ITEM_SHADOW) {+ paints = false;+ } else {+ // What the output pass draws from: the buffer, or the texture left when GLES2+ // uploaded it and let the buffer go, or a single-pixel colour.+ const struct rediwm_buffer *sb = rediwm_buffer_from_node(l->source);+ paints = paints && item->content.opacity > 0 &&+ (sb->buffer || sb->texture || sb->is_single_pixel_buffer ||+ (l->surface && l->surface->surface->buffer));+ }+ const bool restyled = !l->has_shown || !paints || 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 (paints) {+ emit_snap(l, snap);+ damage_box(l, snap, false);+ l->shown = snap;+ l->shown_content = item->content;+ l->has_shown = true;+ }+ }+ if (item->kind != REDIWM_ITEM_RECT && item->kind != REDIWM_ITEM_EFFECT) {+ struct rediwm_buffer *b = rediwm_buffer_from_node(l->source);+ struct wlr_buffer *backing = b->buffer;+ // wlroots may cache a single-pixel buffer as a color and release the+ // source node's buffer reference. The mapped surface still owns it.+ if (!backing && l->surface && l->surface->surface->buffer)+ backing = &l->surface->surface->buffer->base;+ if (l->dirty || l->last_buffer != backing) {+ const pixman_region32_t *damage =+ backing && (l->surface || l->owned_damage) && l->dirty && !l->full_damage+ ? &l->damage : NULL;+ if (!restyled && l->has_shown)+ damage_event_for_buffer(l, damage,+ l->surface ? l->damage_width : backing ? backing->width : 0,+ l->surface ? l->damage_height : backing ? backing->height : 0, snap);+ l->last_buffer = backing;+ }+ }+ if (item->kind == REDIWM_ITEM_SHADOW || l->has_shadow_drawn)+ projection_damage_shadow(l, item->kind == REDIWM_ITEM_SHADOW && enabled &&+ item->content.opacity > 0 && item->shadow->color[3] > 0,+ snap);+ l->dirty = false;+ l->full_damage = false;+ l->owned_damage = false;+ pixman_region32_clear(&l->damage);+}+// Not in 0.20's public headers, but exported; see+// rediwm_projection_present_owned.+struct wlr_client_buffer *wlr_client_buffer_create(struct wlr_buffer *buffer,+ struct wlr_renderer *renderer);+bool wlr_client_buffer_apply_damage(struct wlr_client_buffer *client_buffer,+ struct wlr_buffer *next,+ const pixman_region32_t *damage);+// A plain buffer replaced on a scene node gets a new texture and a full+// upload. Wrapping it in a client buffer instead lets the next frame's+// damage update that texture in place, as for shm clients. The source node's+// lock is marked ignorable (like wlr_scene's own surface lock): the output pass+// reads the buffer from the node and is told the damage at the next sync.+bool rediwm_projection_present_owned(struct rediwm_projection *p,+ struct rediwm_buffer *source,+ struct wlr_renderer *renderer,+ struct wlr_buffer *buffer,+ const pixman_region32_t *damage) {+ struct wlr_client_buffer *current =+ source->buffer ? wlr_client_buffer_get(source->buffer) : NULL;+ bool same_size = current && current->base.width == buffer->width &&+ current->base.height == buffer->height;+ if (same_size) {+ // A full repaint still fits the existing texture.+ pixman_region32_t whole;+ pixman_region32_init_rect(&whole, 0, 0, buffer->width, buffer->height);+ bool updated = wlr_client_buffer_apply_damage(current, buffer,+ damage ? damage : &whole);+ pixman_region32_fini(&whole);+ if (updated) {+ rediwm_projection_damage_buffer(p, &source->node, damage);+ return true;+ }+ }+ struct wlr_client_buffer *next = wlr_client_buffer_create(buffer, renderer);+ if (!next) {+ // Without a texture wrapper, fall back to the plain buffer.+ if (current)+ current->private.n_ignore_locks--;+ rediwm_buffer_set_content(source, buffer);+ rediwm_projection_damage_buffer(p, &source->node, NULL);+ return false;+ }+ if (current)+ current->private.n_ignore_locks--;+ rediwm_buffer_set_content(source, &next->base);+ next->private.n_ignore_locks++;+ // The node's lock keeps it alive; drop the one creation handed us.+ wlr_buffer_unlock(&next->base);+ // Renderers without in-place updates (pixman) land here every frame. The+ // replacement differs from its predecessor only where `damage` says, so a+ // same-sized replacement keeps precise output damage.+ rediwm_projection_damage_buffer(p, &source->node, same_size ? damage : NULL);+ return false;+}+void rediwm_projection_damage_buffer(struct rediwm_projection *p,+ struct rediwm_node *source,+ const pixman_region32_t *damage) {+ struct leaf *l = source->leaf;+ // A leaf's first projection applies its whole buffer anyway.+ if (!l || l->projection != p)+ return;+ if (l->surface)+ return;+ if (!damage ||+ !pixman_region32_union(&l->damage, &l->damage, damage))+ l->full_damage = true;+ l->owned_damage = true;+ l->dirty = true;+ mark_group(p, l->group);+ schedule(p);+}+static void damage_restacked(struct rediwm_projection *p);+// Graph -> damage, then forget what the graph no longer reaches.+void projection_present(struct rediwm_projection *p, bool full) {+ struct group *g;+ wl_list_for_each(g, &p->paint, paint) {+ if (!full && !g->touched) continue;+ struct leaf *l, *next;+ wl_list_for_each_safe(l, next, &g->items, paint) present_leaf(l);+ }+ struct leaf *l;+ wl_list_for_each(l, &p->leaves, link) if (!l->seen) {+ forget_shown(l);+ projection_damage_shadow(l, false, (struct rediwm_snap){0});+ l->ranked = false;+ }+ // Nothing was stacked differently unless the shape of the tree changed.+ if (full) damage_restacked(p);+}+// A leaf that is above what it was below, or the reverse, changes every pixel of+// the two where they overlap, and nothing else tells the screen: wlroots damaged a+// node it restacked. The leaves still there are checked in paint order against+// the order of the last sync; when it differs, the ones that must have moved (all+// but a longest run that kept its order) are damaged whole. Any pair that swapped+// has at least one of them outside that run, and the overlap lies in its box.+static void damage_restacked(struct rediwm_projection *p) {+ size_t n = 0;+ bool ordered = true, have = false;+ uint32_t last = 0;+ struct group *g;+ struct leaf *l;+ wl_list_for_each(g, &p->paint, paint) wl_list_for_each(l, &g->items, paint) {+ if (!l->ranked) continue;+ if (have && l->rank < last) ordered = false;+ last = l->rank;+ have = true;+ n++;+ }+ if (!ordered) {+ struct leaf **leaves = malloc(n * sizeof(*leaves));+ size_t *tails = malloc(n * sizeof(*tails));+ long *before = malloc(n * sizeof(*before));+ bool *kept = calloc(n, sizeof(*kept));+ if (leaves && tails && before && kept) {+ size_t count = 0, length = 0;+ wl_list_for_each(g, &p->paint, paint) wl_list_for_each(l, &g->items, paint)+ if (l->ranked) leaves[count++] = l;+ // Longest strictly increasing run of the old ranks.+ for (size_t i = 0; i < count; i++) {+ size_t lo = 0, hi = length;+ while (lo < hi) {+ const size_t mid = (lo + hi) / 2;+ if (leaves[tails[mid]]->rank < leaves[i]->rank) lo = mid + 1;+ else hi = mid;+ }+ before[i] = lo > 0 ? (long)tails[lo - 1] : -1;+ tails[lo] = i;+ if (lo == length) length++;+ }+ for (long i = length ? (long)tails[length - 1] : -1; i >= 0; i = before[i]) kept[i] = true;+ for (size_t i = 0; i < count; i++) {+ struct leaf *moved = leaves[i];+ if (kept[i]) continue;+ // A shadow shows no box of its own (`shown`), but it lies over or under+ // what was restacked all the same.+ if (moved->has_shown) damage_box(moved, moved->shown, false);+ if (moved->has_shadow_drawn) damage_box(moved, moved->shadow_drawn, false);+ }+ } else {+ // Out of memory: treat the whole layout as changed.+ pixman_region32_union_rect(&p->pending, &p->pending, INT32_MIN / 2, INT32_MIN / 2, INT32_MAX, INT32_MAX);+ }+ free(leaves);+ free(tails);+ free(before);+ free(kept);+ }+ uint32_t rank = 0;+ wl_list_for_each(g, &p->paint, paint) wl_list_for_each(l, &g->items, paint) {+ l->rank = rank++;+ l->ranked = true;+ }+}diff --git a/src/projection_props.c b/src/projection_props.cnew file mode 100644index 0000000..e14b1df--- /dev/null+++ b/src/projection_props.c@@ -0,0 +1,126 @@+// Tree properties: zoom, scale, clip, shadow, dim, unclipped and inert, kept in+// one addon per source tree (see struct tree_props). Part of projection.c.+#include "projection_internal.h"++static void destroy_tree_props(struct wlr_addon *addon) {+ struct tree_props *props = wl_container_of(addon, props, addon);+ if (props->slot) *props->slot = NULL;+ wlr_addon_finish(addon);+ free(props);+}+const struct wlr_addon_interface projection_tree_props_impl = {+ .name = "rediwm tree presentation", .destroy = destroy_tree_props};+static struct tree_props *tree_props_get(struct wlr_addon_set *set, void **slot) {+ struct tree_props *props = tree_props_find(set, slot);+ if (props) return props;+ props = calloc(1, sizeof(*props));+ if (!props) return NULL;+ props->slot = slot;+ if (slot) *slot = props;+ wlr_addon_init(&props->addon, set, NULL, &projection_tree_props_impl);+ return props;+}+// Set by a setter that changed something, so the wrappers below ask for a frame only+// then: a window's zoom and clip are applied every frame and mostly unchanged.+static bool props_changed;+static bool set_tree_zoom(struct wlr_addon_set *set, void **slot, double scale) {+ struct tree_props *props = tree_props_get(set, slot);+ if (!props) return false;+ if (!props->has_zoom || props->zoom != scale) props_changed = true;+ props->zoom = scale;+ props->has_zoom = true;+ return true;+}+static bool set_tree_scale(struct wlr_addon_set *set, void **slot, double factor) {+ if (factor <= 0) return false;+ struct tree_props *props = tree_props_get(set, slot);+ if (!props) return false;+ if (!props->has_scale || props->scale != factor) props_changed = true;+ props->scale = factor;+ props->has_scale = true;+ return true;+}+static bool finite_at_least_zero(double value) { return isfinite(value) && value >= 0; }+static bool set_tree_clip(struct wlr_addon_set *set, void **slot,+ const struct rediwm_clip_spec *spec) {+ if (!spec) {+ struct tree_props *props = tree_props_find(set, slot);+ if (props && props->has_clip) props_changed = true;+ if (props) props->has_clip = false;+ return true;+ }+ 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]) || !isfinite(spec->border_color[i]) ||+ !isfinite(spec->glow_color[i]))+ return false;+ if (!isfinite(spec->glow_x) || !isfinite(spec->glow_y) || !finite_at_least_zero(spec->glow_radius) ||+ !finite_at_least_zero(spec->glow_depth))+ return false;+ struct tree_props *props = tree_props_get(set, slot);+ if (!props) return false;+ if (!props->has_clip || memcmp(&props->clip, spec, sizeof(*spec))) props_changed = true;+ props->clip = *spec;+ props->has_clip = true;+ return true;+}+static bool set_tree_shadow(struct wlr_addon_set *set, void **slot,+ const struct rediwm_shadow_spec *spec) {+ if (!spec) {+ struct tree_props *props = tree_props_find(set, slot);+ if (props && props->has_shadow) props_changed = true;+ if (props) props->has_shadow = false;+ return true;+ }+ if (spec->width <= 0 || spec->height <= 0 || !finite_at_least_zero(spec->softness) ||+ !isfinite(spec->offset_x) || !isfinite(spec->offset_y))+ return false;+ for (int i = 0; i < 4; i++)+ if (!finite_at_least_zero(spec->radii[i]) || !isfinite(spec->color[i])) return false;+ struct tree_props *props = tree_props_get(set, slot);+ if (!props) return false;+ if (!props->has_shadow || memcmp(&props->shadow, spec, sizeof(*spec))) props_changed = true;+ props->shadow = *spec;+ props->has_shadow = true;+ return true;+}+static bool set_tree_dim(struct wlr_addon_set *set, void **slot, float brightness, float saturation) {+ if (!isfinite(brightness) || !isfinite(saturation) || brightness < 0 || brightness > 1 ||+ saturation < 0 || saturation > 1)+ return false;+ if (brightness == 0 && saturation == 0) {+ // Nothing taken away is no dim; a tree that has none gets no addon for it.+ struct tree_props *props = tree_props_find(set, slot);+ if (props && props->has_dim) props_changed = true;+ if (props) props->has_dim = false;+ return true;+ }+ struct tree_props *props = tree_props_get(set, slot);+ if (!props) return false;+ if (!props->has_dim || props->dim.brightness != brightness || props->dim.saturation != saturation) props_changed = true;+ props->dim = (struct dim){brightness, saturation};+ props->has_dim = true;+ return true;+}+static bool set_tree_unclipped(struct wlr_addon_set *set, void **slot, bool unclipped) {+ struct tree_props *props = unclipped ? tree_props_get(set, slot) : tree_props_find(set, slot);+ if (unclipped && !props) return false;+ if (props && props->unclipped != unclipped) props_changed = true;+ if (props) props->unclipped = unclipped;+ return true;+}+static bool set_tree_inert(struct wlr_addon_set *set, void **slot, bool inert) {+ struct tree_props *props = inert ? tree_props_get(set, slot) : tree_props_find(set, slot);+ if (inert && !props) return false;+ if (props && props->inert != inert) props_changed = true;+ if (props) props->inert = inert;+ return true;+}++bool rediwm_projection_set_tree_zoom(struct rediwm_node *node, double scale) { props_changed = false; bool ok = set_tree_zoom(&node->addons, &node->props, scale); if (props_changed) rediwm_node_touch(node); return ok; }+bool rediwm_projection_set_tree_scale(struct rediwm_node *node, double factor) { props_changed = false; bool ok = set_tree_scale(&node->addons, &node->props, factor); if (props_changed) rediwm_node_touch(node); return ok; }+bool rediwm_projection_set_tree_clip(struct rediwm_node *node, const struct rediwm_clip_spec *spec) { props_changed = false; bool ok = set_tree_clip(&node->addons, &node->props, spec); if (props_changed) rediwm_node_touch(node); return ok; }+bool rediwm_projection_set_tree_shadow(struct rediwm_node *node, const struct rediwm_shadow_spec *spec) { props_changed = false; bool ok = set_tree_shadow(&node->addons, &node->props, spec); if (props_changed) rediwm_node_touch(node); return ok; }+bool rediwm_projection_set_tree_dim(struct rediwm_node *node, float brightness, float saturation) { props_changed = false; bool ok = set_tree_dim(&node->addons, &node->props, brightness, saturation); if (props_changed) rediwm_node_touch(node); return ok; }+bool rediwm_projection_set_tree_unclipped(struct rediwm_node *node, bool unclipped) { props_changed = false; bool ok = set_tree_unclipped(&node->addons, &node->props, unclipped); if (props_changed) rediwm_node_touch(node); return ok; }+bool rediwm_projection_set_tree_inert(struct rediwm_node *node, bool inert) { props_changed = false; bool ok = set_tree_inert(&node->addons, &node->props, inert); if (props_changed) rediwm_node_touch(node); return ok; }diff --git a/src/projection_resolve.c b/src/projection_resolve.cnew file mode 100644index 0000000..0d12347--- /dev/null+++ b/src/projection_resolve.c@@ -0,0 +1,292 @@+// resolve(): source tree -> graph (projection.c has the overview).+#include "projection_internal.h"++// A glass effect covers its target exactly: the box is the target's (same+// parent), and it is shown only with it. The pass draws the blur (stage 5c).+static void effect_geometry(const struct rediwm_node *own, const struct rediwm_glass_info *info,+ int *x, int *y, int *width, int *height, bool *enabled) {+ const struct rediwm_buffer *t = info->target;+ *x += t->node.x - own->x;+ *y += t->node.y - own->y;+ *enabled = *enabled && t->node.enabled;+ *width = t->dst_width ? t->dst_width : t->buffer_width;+ *height = t->dst_height ? t->dst_height : t->buffer_height;+}++// A clip's size, radii, border or colour changed: its views did not move, so+// nothing else tells wlroots that the corner arcs and the ring need repainting.+// The zone is the frame's rim, as wide as the largest radius plus the border and+// a margin; where the frame is thinner than twice that it is the whole box.+static bool clip_same_shape(const struct rediwm_clip *a, const struct rediwm_clip *b) {+ if (a->edges.x2 - a->edges.x1 != b->edges.x2 - b->edges.x1 ||+ a->edges.y2 - a->edges.y1 != b->edges.y2 - b->edges.y1 || a->border != b->border)+ return false;+ for (int i = 0; i < 4; i++)+ if (a->radii[i] != b->radii[i] || a->border_color[i] != b->border_color[i]) return false;+ return true;+}+// Where the highlight is, as far as it can be seen: a square around its point.+static bool clip_glow_box(const struct rediwm_clip *c, pixman_box32_t *out) {+ if (c->glow_radius <= 0 || c->glow_color[3] <= 0) return false;+ const double reach = c->glow_radius + 1;+ *out = (pixman_box32_t){(int)floor(c->glow_x - reach), (int)floor(c->glow_y - reach),+ (int)ceil(c->glow_x + reach), (int)ceil(c->glow_y + reach)};+ return true;+}+static bool clip_same_glow(const struct rediwm_clip *a, const struct rediwm_clip *b) {+ if (a->glow_radius != b->glow_radius || a->glow_depth != b->glow_depth) return false;+ if (a->glow_radius <= 0) return true;+ if (a->glow_x != b->glow_x || a->glow_y != b->glow_y) return false;+ for (int i = 0; i < 4; i++)+ if (a->glow_color[i] != b->glow_color[i]) return false;+ return true;+}+// `only` limits the rim to a box (a moved highlight changes nothing else).+static void clip_rim(pixman_region32_t *out, const struct rediwm_clip *c, const pixman_box32_t *only) {+ const struct rediwm_snap f = rediwm_edges_snap(c->edges);+ if (f.width <= 0 || f.height <= 0) return;+ double radius = 0;+ for (int i = 0; i < 4; i++)+ if (c->radii[i] > radius) radius = c->radii[i];+ const int inset = (int)ceil(radius) + (int)ceil(c->border) + (int)ceil(c->glow_depth) + 2;+ pixman_region32_t rim;+ pixman_region32_init_rect(&rim, f.x, f.y, f.width, f.height);+ if (f.width > 2 * inset && f.height > 2 * inset) {+ pixman_region32_t inner;+ pixman_region32_init_rect(&inner, f.x + inset, f.y + inset, f.width - 2 * inset,+ f.height - 2 * inset);+ pixman_region32_subtract(&rim, &rim, &inner);+ pixman_region32_fini(&inner);+ }+ if (only) pixman_region32_intersect_rect(&rim, &rim, only->x1, only->y1, only->x2 - only->x1, only->y2 - only->y1);+ pixman_region32_union(out, out, &rim);+ pixman_region32_fini(&rim);+}+static void damage_clip_change(struct rediwm_projection *p, const struct rediwm_clip *before,+ const struct rediwm_clip *after) {+ pixman_region32_t region;+ pixman_region32_init(®ion);+ if (before && after && clip_same_shape(before, after)) {+ // Only the highlight changed: its old and new places.+ pixman_box32_t box;+ if (clip_glow_box(before, &box)) clip_rim(®ion, before, &box);+ if (clip_glow_box(after, &box)) clip_rim(®ion, after, &box);+ } else {+ if (before) clip_rim(®ion, before, NULL);+ if (after) clip_rim(®ion, after, NULL);+ }+ pixman_region32_union(&p->pending, &p->pending, ®ion);+ pixman_region32_fini(®ion);+}+// resolve(): source tree -> graph. Writes items, group origins, `seen` flags and+// the paint-order lists; creates adapters for nodes it meets the first time, but+// moves and shows nothing.+// What the walk carries down the tree: where the subtree is, whether it shows and+// takes input, and how it is presented. A tree's own properties are folded in as it+// is entered.+struct walk {+ double x, y;+ bool enabled, inert;+ struct rediwm_xform t;+ const struct rediwm_clip *clip;+ const struct dim *dim;+ const struct rediwm_shadow *shadow;+};+// `here` and `cast` hold the tree's clip and shadow for as long as the walk is below+// it.+static void apply_tree(struct rediwm_projection *p, struct tree_props *props, struct walk *w,+ struct rediwm_clip *here, struct rediwm_shadow *cast) {+ if (!props) return;+ // Window zoom composes with the camera, around the frame origin.+ if (props->has_zoom) w->t = rediwm_xform_scale_about(w->t, props->zoom, w->x, w->y);+ if (props->has_scale) w->t = rediwm_xform_scale_about(w->t, props->scale, w->x, w->y);+ if (props->unclipped) {+ w->clip = NULL;+ w->dim = NULL;+ }+ if (props->inert) w->inert = true;+ if (props->has_dim) w->dim = &props->dim;+ if (props->has_clip) {+ *here = rediwm_clip_resolve(w->t, w->x, w->y, &props->clip);+ w->clip = here;+ // A hidden window damages nothing; showing it will.+ if (w->enabled && (!props->has_applied || !clip_same_shape(&props->applied, here) ||+ !clip_same_glow(&props->applied, here)))+ damage_clip_change(p, props->has_applied ? &props->applied : NULL, here);+ props->applied = *here;+ props->has_applied = true;+ } else if (props->has_applied) {+ if (w->enabled) damage_clip_change(p, &props->applied, NULL);+ props->has_applied = false;+ }+ if (props->has_shadow) {+ *cast = rediwm_shadow_resolve(w->t, w->x, w->y, &props->shadow);+ w->shadow = cast;+ }+}+// One leaf into the graph. `node` is the node the pass reads pixels from;+// (x, y) and `enabled` are the leaf's own, from whichever tree placed it.+static void resolve_leaf(struct rediwm_projection *p, struct group *g, struct rediwm_node *node,+ struct rediwm_node *own, const struct walk *w, double x, double y,+ bool enabled) {+ // A node keeps its leaf; only a new one leaves this file (projection.c).+ struct leaf *l = node->leaf ? node->leaf : projection_get_leaf(p, node, own);+ if (!l) {+ wlr_log(WLR_ERROR, "zoom: cannot allocate presentation node");+ return;+ }+ l->seen = true;+ if (l->inert != w->inert) l->inert = w->inert;+ if (l->own != own) l->own = own;+ l->group = g;+ // Flattened leaves within each group retain the source's paint order.+ wl_list_insert(g->items.prev, &l->paint);+ struct rediwm_item *item = &l->item;+ item->enabled = enabled;+ item->scale = w->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 (w->clip) {+ l->clip = *w->clip;+ item->clip = &l->clip;+ } else if (item->clip) {+ item->clip = NULL;+ }+ if (node->type == REDIWM_NODE_RECT) {+ struct rediwm_rect *r = rediwm_rect_from_node(node);+ item->kind = REDIWM_ITEM_RECT;+ item->width = r->width;+ item->height = r->height;+ memcpy(item->content.color, r->color, sizeof(item->content.color));+ } else {+ struct rediwm_buffer *b = rediwm_buffer_from_node(node);+ if (l->effect) g->has_effect = true;+ item->kind = l->effect ? REDIWM_ITEM_EFFECT+ : l->surface ? REDIWM_ITEM_SURFACE+ : REDIWM_ITEM_BUFFER;+ item->width = b->dst_width;+ item->height = b->dst_height;+ if (item->kind == REDIWM_ITEM_EFFECT) {+ struct rediwm_glass_info info;+ int ex = 0, ey = 0;+ if (own && rediwm_glass_effect_info(node, &info))+ effect_geometry(own, &info, &ex, &ey, &item->width, &item->height, &item->enabled);+ x += ex;+ y += ey;+ } else if (!item->width || !item->height) {+ item->width = b->buffer_width;+ item->height = b->buffer_height;+ if (b->transform & 1) {+ int tmp = item->width;+ item->width = item->height;+ item->height = tmp;+ }+ }+ struct rediwm_content *content = &item->content;+ content->src_box = b->src_box;+ content->transform = b->transform;+ content->opacity = b->opacity;+ content->filter_mode = rediwm_filter_at_scale(w->t.scale, b->filter_mode);+ content->transfer_function = b->transfer_function;+ content->primaries = b->primaries;+ content->color_encoding = b->color_encoding;+ content->color_range = b->color_range;+ }+ if (w->dim) {+ item->content.dim_brightness = w->dim->brightness;+ item->content.dim_saturation = w->dim->saturation;+ }+ if (w->shadow && node->type == REDIWM_NODE_BUFFER) {+ item->kind = REDIWM_ITEM_SHADOW;+ l->shadow = *w->shadow;+ item->shadow = &l->shadow;+ } else if (item->shadow) {+ item->shadow = NULL;+ }+ item->edges = rediwm_xform_rect(w->t, x, y, item->width, item->height);+}+static void resolve_own(struct rediwm_projection *p, struct group *g, struct rediwm_node *node,+ const struct walk *parent) {+ const double x = parent->x + node->x, y = parent->y + node->y;+ const bool enabled = parent->enabled && node->enabled;+ switch (node->type) {+ case REDIWM_NODE_RECT:+ resolve_leaf(p, g, node, node, parent, x, y, enabled);+ return;+ case REDIWM_NODE_BUFFER:+ resolve_leaf(p, g, node, node, parent, x, y, enabled);+ return;+ case REDIWM_NODE_TREE: {+ struct walk w = *parent;+ w.x = x;+ w.y = y;+ w.enabled = enabled;+ struct rediwm_clip here;+ struct rediwm_shadow cast;+ apply_tree(p, tree_props_find(&node->addons, &node->props), &w, &here, &cast);+ struct rediwm_node *child;+ wl_list_for_each(child, &((struct rediwm_tree *)node)->children, link)+ resolve_own(p, g, child, &w);+ return;+ }+ }+}+// Source tree -> graph. Every adapter is detached from the paint lists first, so+// the lists hold exactly what this pass reaches, in source paint order.+void projection_resolve(struct rediwm_projection *p, double z, double cx, double cy,+ double ox, double oy, struct rediwm_tree *fixed, bool full) {+ struct leaf *l;+ struct group *g;+ // A full pass walks every group; otherwise only the marked ones are, and the rest+ // keep the leaves and items they had.+ if (full) {+ wl_list_for_each(l, &p->leaves, link) {+ l->seen = false;+ detach(&l->paint);+ }+ }+ wl_list_for_each(g, &p->groups, link) {+ g->seen = false;+ g->touched = false;+ detach(&g->paint);+ }+ const struct rediwm_xform camera = rediwm_xform_camera(z, cx, cy, ox, oy);+ const struct rediwm_xform identity = {1, 0, 0};+ struct rediwm_node *layer;+ wl_list_for_each(layer, &p->own->tree.children, link) {+ if (layer->type != REDIWM_NODE_TREE) continue;+ const bool world = layer == &p->world->node;+ // The world is resolved from its children, ignoring its own place and state; any+ // other layer is a tree like the rest and hides what it holds when disabled.+ const struct walk base = {.x = world ? 0 : layer->x, .y = world ? 0 : layer->y,+ .enabled = world || layer->enabled, .t = world ? camera : identity};+ struct rediwm_node *node;+ wl_list_for_each(node, &((struct rediwm_tree *)layer)->children, link) {+ g = projection_get_group(p, node);+ if (!g) {+ wlr_log(WLR_ERROR, "zoom: cannot allocate presentation group");+ continue;+ }+ g->seen = true;+ g->enabled = base.enabled && node->enabled;+ wl_list_insert(p->paint.prev, &g->paint);+ if (!full && g->resolved && !g->dirty && !g->has_effect) continue;+ if (!full) {+ struct leaf *next;+ wl_list_for_each_safe(l, next, &g->items, paint) {+ l->seen = false;+ detach(&l->paint);+ }+ }+ struct walk w = base;+ // The fixed subtree is laid out as is; everything else follows the camera.+ if (world && fixed && node == &fixed->node) w.t = identity;+ g->resolved = true;+ g->dirty = false;+ g->touched = true;+ g->has_effect = false;+ resolve_own(p, g, node, &w);+ }+ }+}diff --git a/src/projection_test.c b/src/projection_test.cindex f465e04..b86e45a 100644--- a/src/projection_test.c+++ b/src/projection_test.c@@ -1,6 +1,12 @@// Synthetic surface commits into the real projection and wlroots scene damage// path. No renderer, GPU, socket or host desktop is needed.+// The whole projection in one unit, for its static helpers.#include "projection.c"+#include "projection_props.c"+#include "projection_resolve.c"+#include "projection_present.c"+#include "projection_pass.c"+#include "projection_pick.c"#include "node.h"#include <assert.h>#include <stdio.h>@@ -153,1220 +159,11 @@ static bool probe_item(const struct rediwm_item *item, struct rediwm_node *transassert(item->kind == probe->expected.kind && rediwm_content_equal(&item->content, &probe->expected.content));return true;}-// A sync with nothing changed resolves nothing; every kind of change brings it back.-static void test_clean_sync(struct rediwm_scene_outputs *scene) {- struct rig rig = rig_create(scene);- struct rediwm_projection *p = rediwm_projection_create(rig.own, rig.world, scene);- struct wlr_buffer *buffer = buffer_create();- struct rediwm_tree *frame = rediwm_tree_create(rig.world);- struct rediwm_buffer *node = rediwm_buffer_create(frame, buffer);- rediwm_buffer_set_dest_size(node, 40, 30);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- struct leaf *l = leaf_of(p, &node->node);- assert(l && l->item.edges.x2 - l->item.edges.x1 == 40);- // Poke the graph: a clean sync leaves it alone.- l->item.edges.x2 = 12345;- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(l->item.edges.x2 == 12345);- // The scene changed.- rediwm_node_set_position(&frame->node, 5, 5);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(l->item.edges.x2 == 45);- // The camera moved.- l->item.edges.x2 = 12345;- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(l->item.edges.x2 == 12345);- rediwm_projection_sync(p, 2, 0, 0, 0, 0);- assert(l->item.edges.x2 == 90);- // A tree property changed.- l->item.edges.x2 = 12345;- assert(rediwm_projection_set_tree_scale(&frame->node, .5));- rediwm_projection_sync(p, 2, 0, 0, 0, 0);- assert(l->item.edges.x2 == 50);- // A surface commit, or an owned buffer's damage.- l->item.edges.x2 = 12345;- rediwm_projection_damage_buffer(p, &node->node, NULL);- rediwm_projection_sync(p, 2, 0, 0, 0, 0);- assert(l->item.edges.x2 == 50);- // Only the group that changed is walked again; a change to the tree's shape walks all.- const float color[4] = {1, 0, 0, 1};- struct rediwm_tree *other = rediwm_tree_create(rig.world);- struct rediwm_rect *rect = rediwm_rect_create(other, 10, 10, color);- rediwm_projection_sync(p, 2, 0, 0, 0, 0);- struct leaf *rl = leaf_of(p, &rect->node);- assert(rl && rl->item.edges.x2 - rl->item.edges.x1 == 20);- rl->item.edges.x2 = 777;- l->item.edges.x2 = 12345;- rediwm_node_set_position(&frame->node, 7, 7);- rediwm_projection_sync(p, 2, 0, 0, 0, 0);- assert(l->item.edges.x2 != 12345 && rl->item.edges.x2 == 777);- rediwm_node_set_position(&rect->node, 1, 1);- rediwm_projection_sync(p, 2, 0, 0, 0, 0);- assert(rl->item.edges.x2 != 777);- rl->item.edges.x2 = 777;- rediwm_node_raise_to_top(&frame->node);- rediwm_projection_sync(p, 2, 0, 0, 0, 0);- assert(rl->item.edges.x2 != 777);- rediwm_projection_destroy(p);- rig_destroy(&rig);- wlr_buffer_drop(buffer);-}-static void test_graph(struct rediwm_scene_outputs *scene) {- struct rig rig = rig_create(scene);- struct rediwm_tree *source = rig.world;- struct rediwm_projection *p = rediwm_projection_create(rig.own, source, scene);- assert(p);- struct rediwm_tree *frame = rediwm_tree_create(source);- struct rediwm_tree *nested = rediwm_tree_create(frame);- struct rediwm_tree *other = rediwm_tree_create(source);- float color[4] = {.2, .3, .1, 1};- struct rediwm_rect *border = rediwm_rect_create(frame, 17, 9, color);- struct wlr_buffer *buffer = buffer_create(), *glass_buffer = buffer_create();- struct rediwm_buffer *client = rediwm_buffer_create(nested, buffer);- struct rediwm_buffer *glass = rediwm_buffer_create(frame, glass_buffer);- struct rediwm_rect *overlay = rediwm_rect_create(other, 20, 20, color);- assert(frame && nested && other && border && client && glass && overlay);- rediwm_node_set_position(&frame->node, 37, 29);- rediwm_node_set_position(&nested->node, -12, 5);- rediwm_node_set_position(&client->node, 4, 3);- rediwm_node_set_position(&border->node, -2, -3);- rediwm_node_set_position(&other->node, 90, 60);- effect_node = &glass->node;- rediwm_buffer_set_dest_size(client, 100, 60);- rediwm_buffer_set_dest_size(glass, 50, 20);- assert(rediwm_projection_set_tree_zoom(&frame->node, .85));-- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert_presented(p);- struct group *g = get_group(p, &frame->node), *h = get_group(p, &other->node);- struct leaf *border_leaf = leaf_of(p, &border->node), *client_leaf = leaf_of(p, &client->node),- *glass_leaf = leaf_of(p, &glass->node), *overlay_leaf = leaf_of(p, &overlay->node);- struct leaf *order[8];- assert(p->paint.next == &g->paint && g->paint.next == &h->paint && h->paint.next == &p->paint);-- // Kinds. A leaf is a surface only once wlroots has bound it to a wl_surface.- assert(border_leaf->item.kind == REDIWM_ITEM_RECT);- assert(client_leaf->item.kind == REDIWM_ITEM_BUFFER);- assert(glass_leaf->item.kind == REDIWM_ITEM_EFFECT);- struct wlr_surface surface = {0};- struct rediwm_surface scene_surface = {.surface = &surface};- client_leaf->surface = &scene_surface;- mark_all(p); // the test changed a leaf behind the scene's back- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(client_leaf->item.kind == REDIWM_ITEM_SURFACE);- client_leaf->surface = NULL; // The synthetic surface has no lifecycle listeners.- mark_all(p);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(client_leaf->item.kind == REDIWM_ITEM_BUFFER);-- // Paint order, bottom to top, flattened per window.- assert(items_of(g, order, 8) == 3 && order[0] == client_leaf && order[1] == border_leaf &&- order[2] == glass_leaf);- assert(items_of(h, order, 8) == 1 && order[0] == overlay_leaf);-- // Resolving alone shows nothing: what the last present showed is kept.- const struct rediwm_snap shown_before = client_leaf->shown;- const double z = .85, cx = 6.5, cy = -2.5, ox = -17, oy = 31;- resolve(p, z, cx, cy, ox, oy, NULL, true);- assert(client_leaf->has_shown && client_leaf->shown.x == shown_before.x &&- client_leaf->shown.y == shown_before.y && client_leaf->shown.width == shown_before.width);-- // The graph holds the exact, unrounded edges: the camera, then the window's- // zoom about its origin (37, 29), applied to the client at (29, 37) sized- // 100x60 in source units.- const double tx = ox * (1 - z) - z * cx, ty = oy * (1 - z) - z * cy, w = .85;- const double frame_x = z * 37 + tx, frame_y = z * 29 + ty;- const struct rediwm_edges e = client_leaf->item.edges;- assert(fabs(e.x1 - (frame_x + z * w * (29 - 37))) < 1e-9);- assert(fabs(e.y1 - (frame_y + z * w * (37 - 29))) < 1e-9);- assert(fabs(e.x2 - e.x1 - z * w * 100) < 1e-9 && fabs(e.y2 - e.y1 - z * w * 60) < 1e-9);- assert(e.x1 != floor(e.x1) && e.y1 != floor(e.y1)); // Fractional until presented.- // The output consumer sees the newly resolved graph even before present:- // what was last shown must never become a second source of truth.- struct graph_probe probe = {.transport = client_leaf->source, .expected = client_leaf->item};- assert(rediwm_projection_for_each_item(p, false, probe_item, &probe) && probe.count == 1);- probe.count = 0;- assert(rediwm_projection_for_each_item(p, true, probe_item, &probe) && probe.count == 1);- assert(fabs(client_leaf->item.scale - z * w) < 1e-12);- assert(client_leaf->item.width == 100 && client_leaf->item.height == 60);- // Edges, not origin and size: a size is the difference of two rounded edges.- const struct rediwm_snap s = rediwm_edges_snap(e);- assert(s.width == lround(e.x2) - lround(e.x1) && s.x == lround(e.x1));- // Scaled content is filtered bilinearly whatever the client asked for.- rediwm_buffer_set_filter_mode(client, WLR_SCALE_FILTER_NEAREST);- resolve(p, z, cx, cy, ox, oy, NULL, true);- assert(client_leaf->item.content.filter_mode == WLR_SCALE_FILTER_BILINEAR);- resolve(p, 1, 0, 0, 0, 0, NULL, true);- assert(rediwm_projection_set_tree_zoom(&frame->node, 1.0));- resolve(p, 1, 0, 0, 0, 0, NULL, true);- assert(client_leaf->item.content.filter_mode == WLR_SCALE_FILTER_NEAREST);- assert(rediwm_projection_set_tree_zoom(&frame->node, .85));- rediwm_buffer_set_filter_mode(client, WLR_SCALE_FILTER_BILINEAR);-- // Presenting shows exactly that graph, and only that graph.- present(p, true);- assert_presented(p);- {- const struct rediwm_snap now = rediwm_edges_snap(client_leaf->item.edges);- assert(client_leaf->shown.x == now.x && client_leaf->shown.y == now.y &&- client_leaf->shown.width == now.width && client_leaf->shown.height == now.height);- }- const double zooms[] = {.25, .5, .85, 1, 1.25};- for (size_t i = 0; i < sizeof(zooms) / sizeof(zooms[0]); ++i)- for (int x = -9; x <= 9; x += 3) {- rediwm_node_set_position(&frame->node, x * 7, -x * 5);- rediwm_projection_sync_fixed(p, zooms[i], x * .5, -x * .25, ox, oy, other);- assert_presented(p);- // Fixed: no camera.- assert(near(overlay_leaf->item.edges.x1, other->node.x) && near(overlay_leaf->item.edges.y1, other->node.y));- }- rediwm_node_set_position(&frame->node, 37, 29);-- // Order follows the source: raising a node reorders its list, a reparent- // moves the leaf between lists, and a disabled branch is still resolved.- rediwm_node_raise_to_top(&border->node);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(items_of(g, order, 8) == 3 && order[0] == client_leaf && order[1] == glass_leaf &&- order[2] == border_leaf);- rediwm_node_reparent(&client->node, other);- rediwm_node_set_enabled(&other->node, false);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(items_of(g, order, 8) == 2 && order[0] == glass_leaf && order[1] == border_leaf);- assert(items_of(h, order, 8) == 2 && order[0] == overlay_leaf && order[1] == client_leaf);- assert(!h->enabled && !client_leaf->item.enabled && !overlay_leaf->item.enabled);- assert(border_leaf->item.enabled);- assert_presented(p);- rediwm_node_set_enabled(&other->node, true);-- // A subtree the pass no longer reaches leaves the graph and is hidden.- rediwm_node_reparent(&other->node, &rig.parking->tree);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(wl_list_length(&p->paint) == 1 && wl_list_empty(&h->items));- assert(!h->seen && !client_leaf->seen && !client_leaf->has_shown);- assert(client_leaf->paint.next == &client_leaf->paint);- rediwm_node_reparent(&other->node, source);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(wl_list_length(&p->paint) == 2 && wl_list_length(&h->items) == 2);- assert_presented(p);-- // Destruction with the lists linked: a leaf leaves its list with its source,- // a group takes the leaves it still lists with it, and neither order of- // teardown touches freed list heads.- rediwm_node_destroy(&overlay->node);- assert(wl_list_length(&h->items) == 1 && wl_list_length(&p->leaves) == 3);- rediwm_node_destroy(&other->node);- assert(wl_list_length(&p->paint) == 1 && wl_list_length(&p->groups) == 1);- assert(wl_list_length(&g->items) == 2);- effect_node = NULL;- int checks = 0;- probe_group(g, &checks);- rediwm_node_destroy(&frame->node); // The group goes first, then its leaves.- assert(checks == 2); // Both leaves were checked after the group was freed.- assert(wl_list_empty(&p->groups) && wl_list_empty(&p->leaves) && wl_list_empty(&p->paint));- rediwm_projection_destroy(p);- rig_destroy(&rig);- wlr_buffer_drop(buffer);- wlr_buffer_drop(glass_buffer);-}-// Stage 4: the clip shape. Pure geometry first, with no scene at all.-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, {0}, 0, 0, 0, 0, {0}}, one) == 0);- assert(rediwm_clip_opaque(&(struct rediwm_clip){rediwm_xform_rect(identity, 5, 7, 120, 80), {40, 40, 40, 40}, 0, {0}, 0, 0, 0, 0, {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 rediwm_scene_outputs *scene) {- struct rig rig = rig_create(scene);- struct rediwm_tree *source = rig.world;- struct rediwm_projection *p = rediwm_projection_create(rig.own, source, scene);- assert(p);- struct rediwm_tree *frame = rediwm_tree_create(source);- struct rediwm_tree *nested = rediwm_tree_create(frame);- struct rediwm_tree *other = rediwm_tree_create(source);- float color[4] = {.2, .3, .1, 1};- struct rediwm_rect *border = rediwm_rect_create(frame, 17, 9, color);- struct wlr_buffer *buffer = buffer_create(), *glass_buffer = buffer_create(), *title_buffer = buffer_create();- struct rediwm_buffer *client = rediwm_buffer_create(nested, buffer);- struct rediwm_buffer *title = rediwm_buffer_create(frame, title_buffer);- struct rediwm_buffer *glass = rediwm_buffer_create(frame, glass_buffer);- struct rediwm_rect *overlay = rediwm_rect_create(other, 20, 20, color);- assert(frame && nested && other && border && client && title && glass && overlay);- rediwm_node_set_position(&frame->node, 37, 29);- rediwm_node_set_position(&nested->node, 2, 12);- rediwm_buffer_set_dest_size(client, 100, 60);- rediwm_buffer_set_dest_size(title, 100, 12);- rediwm_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 = leaf_of(p, &border->node), *client_leaf = leaf_of(p, &client->node),- *title_leaf = leaf_of(p, &title->node), *glass_leaf = leaf_of(p, &glass->node),- *overlay_leaf = leaf_of(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, true);- 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, true);-- // 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 rediwm_tree *subsurface = rediwm_tree_create(nested);- struct rediwm_tree *popup = rediwm_tree_create(nested);- struct wlr_buffer *popup_buffer = buffer_create(), *sub_buffer = buffer_create();- struct rediwm_buffer *popup_content = rediwm_buffer_create(popup, popup_buffer);- struct rediwm_buffer *sub_content = rediwm_buffer_create(subsurface, sub_buffer);- assert(popup_content && sub_content);- rediwm_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, true);- struct leaf *popup_leaf = leaf_of(p, &popup_content->node), *sub_leaf = leaf_of(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, true);- 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, true);- 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, true);- 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, true);- 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, true);- 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, true);- 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, true);- 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;- rediwm_node_destroy(&frame->node);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- rediwm_projection_destroy(p);- rig_destroy(&rig);- wlr_buffer_drop(buffer);- wlr_buffer_drop(glass_buffer);- wlr_buffer_drop(title_buffer);- wlr_buffer_drop(popup_buffer);- wlr_buffer_drop(sub_buffer);-}-// What a changed shape does to the output, and to the pointer. The views of a-// window do not move when its border colour changes, so the rim is damaged by the-// projection itself; and a point the shape cuts away goes to what is below.-// Damage is in output pixels and the points below are in layout coordinates, at-// whatever scale an earlier test left the output.-static bool layout_damaged(struct rediwm_scene_output *out, double x, double y) {- return damaged(out, (int)(x * out->output->scale), (int)(y * out->output->scale));-}-static void test_clip_output(struct rediwm_scene_outputs *scene, struct rediwm_scene_output *out) {- struct rig rig = rig_create(scene);- struct rediwm_tree *source = rig.world;- struct rediwm_projection *p = rediwm_projection_create(rig.own, source, scene);- assert(p);- struct rediwm_tree *under_tree = rediwm_tree_create(source);- struct rediwm_tree *frame = rediwm_tree_create(source);- struct wlr_buffer *under_buffer = buffer_create(), *client_buffer = buffer_create();- struct rediwm_buffer *under = rediwm_buffer_create(under_tree, under_buffer);- struct rediwm_buffer *client = rediwm_buffer_create(frame, client_buffer);- assert(under_tree && frame && under && client);- rediwm_node_set_position(&under_tree->node, 300, 200);- rediwm_node_set_position(&frame->node, 300, 200);- rediwm_buffer_set_dest_size(under, 120, 80);- rediwm_buffer_set_dest_size(client, 120, 80);- struct rediwm_clip_spec spec = {.width = 120, .height = 80, .radii = {10, 10, 10, 10}, .border = 1,- .border_color = {.1f, .1f, .1f, .1f}};- assert(rediwm_projection_set_tree_clip(&frame->node, &spec));- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- struct leaf *client_leaf = leaf_of(p, &client->node), *under_leaf = leaf_of(p, &under->node);-- // Points inside the window reach it; the corner pixel, which the arc cuts away,- // goes to the window below, and so does the one just outside the box.- double nx, ny;- struct rediwm_node *hit = pick(p, 360, 240, &nx, &ny);- assert(hit == client_leaf->own);- hit = pick(p, 300.5, 200.5, &nx, &ny);- assert(hit == under_leaf->own);- hit = pick(p, 419.5, 279.5, &nx, &ny); // bottom-right corner- assert(hit == under_leaf->own);- hit = pick(p, 310.5, 200.5, &nx, &ny); // on the straight edge- assert(hit == client_leaf->own);- // An inert window is still drawn, but the pointer reaches what is below it.- assert(rediwm_projection_set_tree_inert(&frame->node, true));- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(client_leaf->item.enabled && pick(p, 360, 240, &nx, &ny) == under_leaf->own);- assert(rediwm_projection_set_tree_inert(&frame->node, false));- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(pick(p, 360, 240, &nx, &ny) == client_leaf->own);- // Without a shape the corner is the window's again.- assert(rediwm_projection_set_tree_clip(&frame->node, NULL));- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(pick(p, 300.5, 200.5, &nx, &ny) == client_leaf->own);- clear_damage(out);-- // A shape appearing, a different border colour, and the shape going away each- // damage the rim and only the rim. Nothing else moved.- assert(rediwm_projection_set_tree_clip(&frame->node, &spec));- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(layout_damaged(out, 301, 201) && layout_damaged(out, 419, 279) && layout_damaged(out, 360, 202));- assert(!layout_damaged(out, 360, 240));- clear_damage(out);- rediwm_projection_sync(p, 1, 0, 0, 0, 0); // Nothing changed.- assert(!pixman_region32_not_empty(&out->damage_ring.current));- spec.border_color[0] = .3f;- assert(rediwm_projection_set_tree_clip(&frame->node, &spec));- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(layout_damaged(out, 301, 201) && layout_damaged(out, 419, 279) && layout_damaged(out, 300, 240));- assert(!layout_damaged(out, 360, 240) && !layout_damaged(out, 100, 100));- assert(pixman_region32_not_empty(&out->pending_commit_damage));- clear_damage(out);- // Setting the same shape again damages nothing.- assert(rediwm_projection_set_tree_clip(&frame->node, &spec));- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(!pixman_region32_not_empty(&out->damage_ring.current));- // The shape going away damages the rim, not the interior.- assert(rediwm_projection_set_tree_clip(&frame->node, NULL));- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(layout_damaged(out, 301, 201) && layout_damaged(out, 419, 279) && !layout_damaged(out, 360, 240));- // Moving a window moves its views, which damage themselves; the shape follows- // without a second, rim-only report of its own.- assert(rediwm_projection_set_tree_clip(&frame->node, &spec));- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- clear_damage(out);- rediwm_node_set_position(&frame->node, 310, 200);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(layout_damaged(out, 370, 240)); // the views of the window moved- rediwm_node_set_position(&frame->node, 300, 200);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(rediwm_projection_set_tree_clip(&frame->node, NULL));- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- // A hidden window damages nothing for a shape change.- rediwm_node_set_enabled(&frame->node, false);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- clear_damage(out);- assert(rediwm_projection_set_tree_clip(&frame->node, &spec));- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(!pixman_region32_not_empty(&out->damage_ring.current));- spec.border_color[1] = .7f;- assert(rediwm_projection_set_tree_clip(&frame->node, &spec));- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(!pixman_region32_not_empty(&out->damage_ring.current));-- rediwm_projection_destroy(p);- rig_destroy(&rig);- wlr_buffer_drop(under_buffer);- wlr_buffer_drop(client_buffer);-}-// Stage 4d: a shadow's view has no buffer, so wlroots damages nothing for it and the-// projection damages the footprint itself: when it appears, moves, changes, is-// hidden with its window, or goes away, and never when nothing changed.-static void test_shadow_output(struct rediwm_scene_outputs *scene, struct rediwm_scene_output *out) {- struct rig rig = rig_create(scene);- struct rediwm_tree *source = rig.world;- struct rediwm_projection *p = rediwm_projection_create(rig.own, source, scene);- assert(p);- struct rediwm_tree *frame = rediwm_tree_create(source);- struct rediwm_tree *shadow_tree = rediwm_tree_create(frame);- struct rediwm_buffer *footprint = rediwm_buffer_create(shadow_tree, NULL);- assert(frame && shadow_tree && footprint);- rediwm_node_set_position(&frame->node, 100, 100);- rediwm_node_set_position(&footprint->node, -16, -8);- rediwm_buffer_set_dest_size(footprint, 152, 112);- struct rediwm_shadow_spec spec = {.width = 120, .height = 80, .radii = {10, 10, 10, 10},- .offset_y = 8, .softness = 16, .color = {0, 0, 0, .4f}};- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- struct leaf *l = leaf_of(p, &footprint->node);- assert(l->item.kind == REDIWM_ITEM_BUFFER && !l->item.shadow);- clear_damage(out);-- assert(rediwm_projection_set_tree_shadow(&shadow_tree->node, &spec));- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(l->item.kind == REDIWM_ITEM_SHADOW && l->item.shadow);- assert(near(l->item.shadow->shape.edges.x1, 100) && near(l->item.shadow->shape.edges.y2, 180));- assert(near(l->item.shadow->offset_y, 8) && near(l->item.shadow->softness, 16));- assert(layout_damaged(out, 85, 93) && layout_damaged(out, 235, 203) && layout_damaged(out, 160, 140));- assert(!layout_damaged(out, 80, 140) && !layout_damaged(out, 160, 90));- clear_damage(out);- rediwm_projection_sync(p, 1, 0, 0, 0, 0); // Nothing changed.- assert(!pixman_region32_not_empty(&out->damage_ring.current));- // A moved window damages where its shadow was and where it is.- rediwm_node_set_position(&frame->node, 140, 100);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(layout_damaged(out, 85, 140) && layout_damaged(out, 270, 140));- clear_damage(out);- // A different colour damages it in place.- spec.color[3] = .5f;- assert(rediwm_projection_set_tree_shadow(&shadow_tree->node, &spec));- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(layout_damaged(out, 130, 140) && !layout_damaged(out, 100, 140));- clear_damage(out);- // Hidden with its window, shown again, and destroyed.- rediwm_node_set_enabled(&frame->node, false);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(layout_damaged(out, 130, 140));- clear_damage(out);- rediwm_node_set_enabled(&frame->node, true);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(layout_damaged(out, 130, 140));- clear_damage(out);- rediwm_node_destroy(&footprint->node);- assert(layout_damaged(out, 130, 140));- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- rediwm_projection_destroy(p);- rig_destroy(&rig);-}-// 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 rediwm_scene_outputs *scene) {- struct rig rig = rig_create(scene);- struct rediwm_tree *source = rig.world;- struct rediwm_projection *p = rediwm_projection_create(rig.own, source, scene);- assert(p);- uint64_t cursor = rediwm_projection_damage_cursor(p), other = cursor;- struct rediwm_tree *window = rediwm_tree_create(source);- struct wlr_buffer *buffer = buffer_create(); // 200x120- struct rediwm_buffer *a = rediwm_buffer_create(window, buffer);- struct rediwm_buffer *b = rediwm_buffer_create(window, buffer);- rediwm_node_set_position(&window->node, 40, 30);- rediwm_buffer_set_dest_size(a, 100, 60); // half size: one buffer px = 0.5- rediwm_node_set_position(&b->node, 0, 100);- rediwm_buffer_set_dest_size(b, 20, 20);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- struct leaf *la = leaf_of(p, &a->node), *lb = leaf_of(p, &b->node);- assert(la->item.id && lb->item.id && la->item.id != lb->item.id);+#include "projection_test_graph.c"+#include "projection_test_damage.c"+#include "projection_test_groups.c"- // 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.- rediwm_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));- rediwm_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));- rediwm_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.- rediwm_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;- rediwm_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 leaf that draws nothing (an input-only node such as a resize grab) has no- // events, however it moves: glass would otherwise take it for a change below.- struct rediwm_buffer *grab = rediwm_buffer_create(window, NULL);- rediwm_buffer_set_dest_size(grab, 300, 300);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- rediwm_node_set_position(&grab->node, 5, 5);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- rediwm_node_destroy(&grab->node);- assert(events_since(p, &cursor, NULL).count == 0);-- // 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++) {- rediwm_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);- rediwm_projection_destroy(p);- rig_destroy(&rig);- wlr_buffer_drop(buffer);-}-// A window's dim is a field of what its leaves show: inherited down the tree like-// its shape, ended where a popup hangs under it, and a change of it restyles (so-// damages) every leaf under the tree and no other.-static void test_dim(struct rediwm_scene_outputs *scene) {- struct rig rig = rig_create(scene);- struct rediwm_tree *source = rig.world;- struct rediwm_projection *p = rediwm_projection_create(rig.own, source, scene);- assert(p);- uint64_t cursor = rediwm_projection_damage_cursor(p);- struct rediwm_tree *window = rediwm_tree_create(source);- struct rediwm_tree *content = rediwm_tree_create(window);- struct rediwm_tree *popup = rediwm_tree_create(content);- struct rediwm_tree *sibling = rediwm_tree_create(source);- float color[4] = {.2, .3, .1, 1};- struct wlr_buffer *buffer = buffer_create();- struct rediwm_rect *ring = rediwm_rect_create(window, 10, 10, color);- struct rediwm_buffer *client = rediwm_buffer_create(content, buffer);- struct rediwm_buffer *menu = rediwm_buffer_create(popup, buffer);- struct rediwm_buffer *other = rediwm_buffer_create(sibling, buffer);- assert(ring && client && menu && other);- rediwm_node_set_position(&window->node, 40, 30);- rediwm_node_set_position(&client->node, 0, 10);- rediwm_buffer_set_dest_size(client, 100, 60);- rediwm_node_set_position(&popup->node, 50, 20);- rediwm_buffer_set_dest_size(menu, 30, 30);- rediwm_node_set_position(&sibling->node, 300, 200);- rediwm_buffer_set_dest_size(other, 20, 20);- assert(rediwm_projection_set_tree_unclipped(&popup->node, true));- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- struct leaf *lring = leaf_of(p, &ring->node), *lclient = leaf_of(p, &client->node),- *lmenu = leaf_of(p, &menu->node), *lother = leaf_of(p, &other->node);- assert(events_since(p, &cursor, NULL).count == 4); // Appearing.-- // Nothing is dimmed until a tree asks, and taking nothing away makes no addon.- assert(rediwm_projection_set_tree_dim(&sibling->node, 0, 0));- assert(!lring->item.content.dim_brightness && !lclient->item.content.dim_saturation);-- // Every leaf of the window, not the popup under it and not another window.- assert(rediwm_projection_set_tree_dim(&window->node, .08f, .1f));- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(lring->item.content.dim_brightness == .08f && lring->item.content.dim_saturation == .1f);- assert(lclient->item.content.dim_brightness == .08f && lclient->item.content.dim_saturation == .1f);- assert(!lmenu->item.content.dim_brightness && !lmenu->item.content.dim_saturation);- assert(!lother->item.content.dim_brightness && !lother->item.content.dim_saturation);- // The change restyles those two leaves (each reports its box as it was and as it- // is) and no other.- struct events_seen seen = events_since(p, &cursor, NULL);- assert(seen.count == 4 && event_is(&seen.last[1], lclient->item.id, 40, 40, 100, 60) &&- event_is(&seen.last[3], lring->item.id, 40, 30, 10, 10));- for (int i = 0; i < 4; i++) assert(seen.last[i].leaf == lclient->item.id || seen.last[i].leaf == lring->item.id);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(events_since(p, &cursor, NULL).count == 0); // Unchanged is no damage.- assert(rediwm_projection_set_tree_dim(&window->node, .1f, .1f));- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(events_since(p, &cursor, NULL).count == 4);-- // A fraction outside 0 to 1 is refused and the old dim stays.- assert(!rediwm_projection_set_tree_dim(&window->node, -.1f, 0));- assert(!rediwm_projection_set_tree_dim(&window->node, 0, 1.5f));- assert(!rediwm_projection_set_tree_dim(&window->node, NAN, 0));- assert(!rediwm_projection_set_tree_dim(&window->node, 0, INFINITY));- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(lclient->item.content.dim_brightness == .1f && events_since(p, &cursor, NULL).count == 0);-- // The barrier does not hide a dim the subtree sets for itself, and lifting it- // gives the popup the window's.- assert(rediwm_projection_set_tree_dim(&popup->node, .5f, 0));- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(lmenu->item.content.dim_brightness == .5f && lmenu->item.content.dim_saturation == 0);- assert(rediwm_projection_set_tree_dim(&popup->node, 0, 0));- assert(rediwm_projection_set_tree_unclipped(&popup->node, false));- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(lmenu->item.content.dim_brightness == .1f);- assert(rediwm_projection_set_tree_unclipped(&popup->node, true));- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(!lmenu->item.content.dim_brightness);-- // Taking nothing away undoes it, and restyles the leaves back.- events_since(p, &cursor, NULL);- assert(rediwm_projection_set_tree_dim(&window->node, 0, 0));- assert(rediwm_projection_set_tree_dim(&window->node, 0, 0)); // Again is fine.- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(!lring->item.content.dim_brightness && !lclient->item.content.dim_saturation);- assert(events_since(p, &cursor, NULL).count == 4);-- rediwm_projection_destroy(p);- rig_destroy(&rig);- wlr_buffer_drop(buffer);-}-// The output pass hides what a client's opaque region covers, from the region as-// the surface node keeps it. The buffers of a client reveal no format, like a-// wlr_client_buffer, so only the node's own region can say they are opaque: it-// has to land on screen exactly, or one pixel inside where it is scaled.-static void test_opaque(void) {- pixman_region32_t source, out;- pixman_region32_init_rect(&source, 50, 30, 100, 60);- pixman_region32_init(&out);- // Shown 1:1, a region is used as is, clipped to the box.- rediwm_mirror_opaque(&out, &source, 200, 120, 200, 120);- assert(pixman_region32_n_rects(&out) == 1);- assert(pixman_region32_contains_point(&out, 50, 30, NULL) && pixman_region32_contains_point(&out, 149, 89, NULL));- assert(!pixman_region32_contains_point(&out, 49, 30, NULL) && !pixman_region32_contains_point(&out, 150, 89, NULL));- pixman_region32_clear(&out);- pixman_region32_clear(&source);- pixman_region32_union_rect(&source, &source, 0, 0, 300, 300);- rediwm_mirror_opaque(&out, &source, 200, 120, 200, 120);- assert(pixman_region32_contains_point(&out, 199, 119, NULL) && !pixman_region32_contains_point(&out, 200, 0, NULL));- // Nothing says nothing.- pixman_region32_clear(&out);- pixman_region32_clear(&source);- rediwm_mirror_opaque(&out, &source, 200, 120, 200, 120);- assert(!pixman_region32_not_empty(&out));-- // Scaled with the node and pulled one pixel in: the middle is opaque, the edge- // pixels, which the filter may blend with what is outside, are not.- pixman_region32_union_rect(&source, &source, 0, 0, 200, 120);- rediwm_mirror_opaque(&out, &source, 200, 120, 100, 60);- assert(pixman_region32_contains_point(&out, 40, 25, NULL));- assert(!pixman_region32_contains_point(&out, 0, 0, NULL) && !pixman_region32_contains_point(&out, 99, 59, NULL));- assert(pixman_region32_contains_point(&out, 1, 1, NULL) && pixman_region32_contains_point(&out, 98, 58, NULL));- // A rectangle thinner than the inset vanishes.- pixman_region32_clear(&out);- pixman_region32_clear(&source);- pixman_region32_union_rect(&source, &source, 10, 10, 2, 2);- rediwm_mirror_opaque(&out, &source, 200, 120, 100, 60);- assert(!pixman_region32_not_empty(&out));- // A node with no size shows no region.- pixman_region32_union_rect(&source, &source, 0, 0, 50, 50);- rediwm_mirror_opaque(&out, &source, 0, 0, 100, 60);- assert(!pixman_region32_not_empty(&out));- pixman_region32_fini(&source);- pixman_region32_fini(&out);-}-static bool in_group(struct group *g, struct leaf *l) {- struct leaf *listed;- wl_list_for_each(listed, &g->items, paint) if (listed == l) return true;- return false;-}-static int snap_width(struct leaf *l) { return rediwm_edges_snap(l->item.edges).width; }-static void test_groups(struct rediwm_scene_outputs *scene, struct rediwm_scene_output *out) {- struct rig rig = rig_create(scene);- struct rediwm_tree *source = rig.world;- struct rediwm_projection *p = rediwm_projection_create(rig.own, source, scene);- assert(p);- struct rediwm_tree *frame = rediwm_tree_create(source);- struct rediwm_tree *nested = rediwm_tree_create(frame);- struct rediwm_tree *other = rediwm_tree_create(source);- float color[4] = {.2, .3, .1, 1};- struct rediwm_rect *border = rediwm_rect_create(frame, 17, 9, color);- struct wlr_buffer *buffer = buffer_create();- struct rediwm_buffer *client = rediwm_buffer_create(nested, buffer);- struct rediwm_rect *overlay = rediwm_rect_create(other, 20, 20, color);- assert(frame && nested && other && border && client && overlay);- rediwm_node_set_position(&frame->node, 37, 29);- rediwm_node_set_position(&nested->node, -12, 5);- rediwm_node_set_position(&client->node, 4, 3);- rediwm_node_set_position(&border->node, -2, -3);- rediwm_node_set_position(&other->node, 90, 60);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- struct group *g = get_group(p, &frame->node);- struct group *h = get_group(p, &other->node);- struct leaf *l = leaf_of(p, &client->node);- struct leaf *border_leaf = leaf_of(p, &border->node);- assert(in_group(g, l) && in_group(g, border_leaf) && in_group(h, leaf_of(p, &overlay->node)));- assert(wl_list_length(&p->paint) == 2);- assert_position(p, &client->node, 29, 37);- assert_position(p, &border->node, 35, 26);- clear_damage(out);- rediwm_node_set_position(&frame->node, 83, 71);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- // Ordinary movement moves every leaf of the group.- assert_position(p, &client->node, 75, 79);- assert_position(p, &border->node, 81, 68);- assert(damaged(out, 50, 60) && damaged(out, 200, 150));- clear_damage(out);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(!pixman_region32_not_empty(&out->damage_ring.current));-- // A fixed desktop ignores the camera while neighboring windows still pan.- rediwm_projection_sync_fixed(p, .5, 10, 20, 0, 0, frame);- assert_position(p, &client->node, 75, 79);- assert_position(p, &overlay->node, 40, 20);- assert(snap_width(l) == 200);- rediwm_projection_sync(p, .5, 10, 20, 0, 0);- assert_position(p, &client->node, 33, 30);-- // Endpoint rounding must remain global, including half-pixels crossing- // zero. Integer rounding of a local offset is not equivalent here.- const double zooms[] = {.5, .85, 1, 1.25};- for (size_t i = 0; i < sizeof(zooms) / sizeof(zooms[0]); ++i) {- double z = zooms[i], cx = 6.5, cy = -2.5, ox = -17, oy = 31;- double tx = ox * (1 - z) - z * cx, ty = oy * (1 - z) - z * cy;- for (int x = -13; x <= 13; ++x) {- rediwm_node_set_position(&frame->node, x, -x);- rediwm_projection_sync(p, z, cx, cy, ox, oy);- assert_position(p, &client->node, lround(z * (x - 8) + tx),- lround(z * (-x + 8) + ty));- assert_position(p, &border->node, lround(z * (x - 2) + tx),- lround(z * (-x - 3) + ty));- assert(snap_width(l) == lround(z * (x - 8 + 200) + tx) -- lround(z * (x - 8) + tx));- }- }- // Window zoom multiplies the camera scale while preserving its world origin.- // Nested geometry keeps the same per-leaf projected scale.- assert(rediwm_projection_set_tree_zoom(&frame->node, .85));- rediwm_node_set_position(&frame->node, -17, 23);- rediwm_projection_sync(p, .5, 3, -7, 0, 0);- assert_position(p, &client->node, lround(.5 * -17 - 1.5 + (.85 * .5) * -8),- lround(.5 * 23 + 3.5 + (.85 * .5) * 8));- assert(l->item.scale == (.85 * .5));-- // A relative tree scale composes with camera and window zoom, preserving origin.- assert(rediwm_projection_set_tree_scale(&nested->node, .5));- rediwm_projection_sync(p, .5, 3, -7, 0, 0);- assert(fabs(l->item.scale - ((.85 * .5) * .5)) < .0001);- assert_position(p, &client->node, lround(.5 * -17 - 1.5 + (.85 * .5) * -12 + (.85 * .5) * .5 * 4),- lround(.5 * 23 + 3.5 + (.85 * .5) * 5 + (.85 * .5) * .5 * 3));- assert(rediwm_projection_set_tree_scale(&nested->node, 1.0));- rediwm_projection_sync(p, .5, 3, -7, 0, 0);- assert(fabs(l->item.scale - (.85 * .5)) < .0001);- assert(rediwm_projection_set_tree_zoom(&frame->node, 1.0));-- // Reorder a whole window and a leaf within it, retaining source paint order.- rediwm_node_raise_to_top(&frame->node);- rediwm_node_raise_to_top(&nested->node);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- struct leaf *order[8];- assert(p->paint.next == &h->paint && h->paint.next == &g->paint && g->paint.next == &p->paint);- assert(items_of(g, order, 8) == 2 && order[0] == border_leaf && order[1] == l);- // The source node and its input stay attached to the leaf when a subsurface- // changes source root, rather than retaining a stale group.- rediwm_node_reparent(&client->node, other);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(in_group(h, l) && !in_group(g, l));- assert_position(p, &client->node, 94, 63);- double input_x, input_y;- assert(pick(p, 94 + 40, 63 + 24, &input_x, &input_y) == &client->node);- assert(input_x == 40 && input_y == 24);- rediwm_node_set_enabled(&other->node, false);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(!l->item.enabled && !pick(p, 94 + 40, 63 + 24, &input_x, &input_y));- rediwm_node_set_enabled(&other->node, true);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert_position(p, &client->node, 94, 63);-- // A former source root can become nested, leave the projected tree, and- // return. Its unused group is not reached while its leaves change group, and- // neither its former position nor window zoom may leak into other leaves.- rediwm_node_reparent(&frame->node, other);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(!g->seen);- assert(in_group(h, border_leaf));- assert_position(p, &border->node, 71, 80);- assert_position(p, &client->node, 94, 63);- rediwm_node_reparent(&frame->node, &rig.parking->tree);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(!border_leaf->seen);- rediwm_node_reparent(&frame->node, source);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(g->seen);- assert(in_group(g, border_leaf));- assert_position(p, &border->node, -19, 20);-- // A leaf moved out of a window's tree before the tree is destroyed keeps its- // leaf: the surviving source node needs nothing recreated.- struct rediwm_tree *temporary = rediwm_tree_create(source);- struct rediwm_rect *survivor = rediwm_rect_create(temporary, 12, 12, color);- assert(temporary && survivor);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- rediwm_node_reparent(&survivor->node, other);- rediwm_node_destroy(&temporary->node);- assert(leaf_of(p, &survivor->node));- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert_position(p, &survivor->node, 90, 60);- rediwm_node_destroy(&survivor->node);-- // A direct leaf can itself be a former group root. After it becomes nested,- // destroying the source runs both leaf and group cleanup on the same node.- struct rediwm_rect *direct = rediwm_rect_create(source, 12, 12, color);- assert(direct);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- rediwm_node_reparent(&direct->node, other);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(in_group(h, leaf_of(p, &direct->node)));- rediwm_node_destroy(&direct->node);- assert(wl_list_length(&p->groups) == 2 && wl_list_length(&p->leaves) == 3);-- // Source-first teardown destroys its group without dangling leaf listeners.- rediwm_node_destroy(&frame->node);- assert(wl_list_length(&p->groups) == 1 && wl_list_length(&p->leaves) == 2);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- rediwm_node_destroy(&other->node);- assert(wl_list_empty(&p->groups) && wl_list_empty(&p->leaves));- rediwm_projection_destroy(p);- rig_destroy(&rig);- wlr_buffer_drop(buffer);-}-// Stage 6b: the screen's damage comes from the graph. A restack damages what-// changed order, a change of what a leaf itself shows is cut to what was visible-// of it at the last build, and a window going away is damaged at once.-static void test_graph_damage(struct rediwm_scene_outputs *scene, struct rediwm_scene_output *out) {- struct rig rig = rig_create(scene);- struct rediwm_tree *source = rig.world;- struct rediwm_projection *p = rediwm_projection_create(rig.own, source, scene);- assert(p);- struct wlr_buffer *buffer = buffer_create(); // 200x120- struct rediwm_tree *frame_a = rediwm_tree_create(source);- struct rediwm_tree *frame_b = rediwm_tree_create(source);- struct rediwm_buffer *a = rediwm_buffer_create(frame_a, buffer);- struct rediwm_buffer *b = rediwm_buffer_create(frame_b, buffer);- rediwm_node_set_position(&frame_a->node, 20, 20);- rediwm_node_set_position(&frame_b->node, 60, 40);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- clear_damage(out);-- // Raising the window below changes the pixels where the two overlap, and says- // nothing about the rest of the screen.- rediwm_node_raise_to_top(&frame_a->node);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(layout_damaged(out, 100, 60) && layout_damaged(out, 210, 130));- assert(!layout_damaged(out, 350, 300) && !layout_damaged(out, 5, 5));- clear_damage(out);- rediwm_projection_sync(p, 1, 0, 0, 0, 0); // Same order: nothing.- assert(!pixman_region32_not_empty(&out->damage_ring.current));-- // What changes only what a leaf shows is cut to what of it was visible at the- // last build: the right part of `a` was hidden, so its change is not damaged.- struct leaf *la = leaf_of(p, &a->node), *lb = leaf_of(p, &b->node);- pixman_region32_t visible;- pixman_region32_init_rect(&visible, 20, 20, 50, 120);- rediwm_projection_note_visible(p, &a->node, &visible);- rediwm_projection_damage_buffer(p, &a->node, NULL);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(layout_damaged(out, 30, 60) && !layout_damaged(out, 150, 60));- clear_damage(out);- // Nothing visible, nothing to damage; a leaf the pass never saw is damaged whole.- pixman_region32_clear(&visible);- rediwm_projection_note_visible(p, &a->node, &visible);- rediwm_projection_damage_buffer(p, &a->node, NULL);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(!pixman_region32_not_empty(&out->damage_ring.current));- assert(!lb->has_visible);- rediwm_projection_damage_buffer(p, &b->node, NULL);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(layout_damaged(out, 70, 50) && layout_damaged(out, 250, 150));- clear_damage(out);- // A leaf that is moved damages where it was only where it was visible, and- // where it is whatever the last build saw.- pixman_region32_init_rect(&visible, 60, 40, 40, 120);- rediwm_projection_note_visible(p, &b->node, &visible);- rediwm_node_set_position(&frame_b->node, 200, 150);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- assert(layout_damaged(out, 70, 60) && !layout_damaged(out, 150, 60)); // old box, cut- assert(layout_damaged(out, 250, 200)); // new box, whole- clear_damage(out);- (void)la;-- // A window going away damages what it showed without waiting for a sync: a- // capture can build a frame before the next one.- rediwm_projection_note_visible(p, &a->node, &(pixman_region32_t){.extents = {20, 20, 220, 140}, .data = NULL});- rediwm_node_destroy(&frame_a->node);- assert(layout_damaged(out, 30, 30) && layout_damaged(out, 210, 130));- pixman_region32_fini(&visible);- rediwm_projection_destroy(p);- rig_destroy(&rig);- wlr_buffer_drop(buffer);-}-// Stage 6a: what the output pass reports of a surface becomes the signals wlroots-// would have sent. Two outputs side by side, a node over the seam.-static int outputs_updates, frame_dones, update_size;-static struct rediwm_scene_output *update_active[4];-static void on_outputs_update(struct wl_listener *listener, void *data) {- (void)listener;- const struct rediwm_outputs_update_event *event = data;- outputs_updates++;- update_size = (int)event->size;- for (size_t i = 0; i < event->size && i < 4; i++) update_active[i] = event->active[i];-}-static void on_frame_done(struct wl_listener *listener, void *data) {- (void)listener;- (void)data;- frame_dones++;-}-static void report(struct rediwm_projection *p, struct rediwm_node *node, int x, int y, int w, int h) {- pixman_region32_t visible;- pixman_region32_init(&visible);- if (w > 0) pixman_region32_union_rect(&visible, &visible, x, y, w, h);- rediwm_projection_feedback_begin(p);- rediwm_projection_feedback_note(p, node, &visible);- rediwm_projection_feedback_end(p);- pixman_region32_fini(&visible);-}-static void test_feedback(struct rediwm_scene_outputs *scene, struct wlr_backend *backend, struct rediwm_scene_output *left,- struct wlr_output *left_output) {- struct wlr_output *right_output = wlr_headless_add_output(backend, 320, 240);- assert(right_output);- struct wlr_output_state state;- wlr_output_state_init(&state);- wlr_output_state_set_enabled(&state, true);- assert(wlr_output_commit_state(right_output, &state));- wlr_output_state_finish(&state);- struct rediwm_scene_output *right = rediwm_scene_output_create(scene, right_output, NULL);- assert(right);- int seam = 0, unused = 0;- wlr_output_effective_resolution(left_output, &seam, &unused);- right->x = seam;-- struct rig rig = rig_create(scene);- struct rediwm_tree *source = rig.world;- struct rediwm_projection *p = rediwm_projection_create(rig.own, source, scene);- assert(p);- struct wlr_buffer *buffer = buffer_create();- struct rediwm_buffer *node = rediwm_buffer_create(source, buffer);- rediwm_node_set_position(&node->node, seam - 26, 50);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- struct leaf *l = leaf_of(p, &node->node);- l->feedback = calloc(1, sizeof(*l->feedback));- assert(l->feedback);- l->feedback->leaf = l;- wl_list_insert(&p->feedback, &l->feedback->link);- struct wl_listener updated = {.notify = on_outputs_update}, done = {.notify = on_frame_done};- wl_signal_add(&node->events.outputs_update, &updated);- wl_signal_add(&node->events.frame_done, &done);- struct timespec now = {0};-- // Over the seam: both outputs show more than a tenth, the one with most is primary.- report(p, &node->node, seam - 26, 50, 200, 100); // 26 columns left, 174 right- assert(outputs_updates == 1 && update_size == 2 && l->feedback->primary == right);- assert(l->feedback->active == (1ull << left->index | 1ull << right->index));- report(p, &node->node, seam - 26, 50, 200, 100);- assert(outputs_updates == 1); // Unchanged: no signal.- // A sliver on the other side is ignored (10 % rule): 2 of 28 columns.- report(p, &node->node, seam - 26, 50, 28, 100);- assert(outputs_updates == 2 && update_size == 1 && update_active[0] == left && l->feedback->primary == left);- // Frames are sent for what is shown, and for no output it is not.- rediwm_projection_send_frame_done(p, left, &now);- assert(frame_dones == 1);- // A hidden leaf hears none, whatever it was told last.- rediwm_node_set_enabled(&node->node, false);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- rediwm_projection_send_frame_done(p, left, &now);- assert(frame_dones == 1);- rediwm_node_set_enabled(&node->node, true);- rediwm_projection_sync(p, 1, 0, 0, 0, 0);- report(p, &node->node, 0, 0, 0, 0); // hidden- assert(outputs_updates == 3 && update_size == 0 && !l->feedback->active && !l->feedback->primary);- rediwm_projection_send_frame_done(p, left, &now);- assert(frame_dones == 1); // nothing shown, nothing sent- // An active output whose scale changed hears again, whatever else is the same.- report(p, &node->node, seam - 26, 50, 26, 100);- assert(outputs_updates == 4 && update_size == 1);- report(p, &node->node, seam - 26, 50, 26, 100);- assert(outputs_updates == 4);- wlr_output_state_init(&state);- wlr_output_state_set_scale(&state, 1.25f);- assert(wlr_output_commit_state(left_output, &state));- wlr_output_state_finish(&state);- report(p, &node->node, seam - 26, 50, 26, 100);- assert(outputs_updates == 5);- report(p, &node->node, seam - 26, 50, 26, 100);- assert(outputs_updates == 5);- wlr_output_state_init(&state);- wlr_output_state_set_scale(&state, 1.f);- assert(wlr_output_commit_state(left_output, &state));- wlr_output_state_finish(&state);-- wl_list_remove(&updated.link);- wl_list_remove(&done.link);- rediwm_projection_destroy(p);- rig_destroy(&rig);- wlr_buffer_drop(buffer);- rediwm_scene_output_destroy(right);- wlr_output_destroy(right_output);-}int main(void) {struct wl_display *display = wl_display_create();assert(display);diff --git a/src/projection_test_damage.c b/src/projection_test_damage.cnew file mode 100644index 0000000..9c9aa5d--- /dev/null+++ b/src/projection_test_damage.c@@ -0,0 +1,414 @@+// Output damage tests: clips and shadows on screen, damage events, dim and opaque+// regions.+// Included by projection_test.c, in its one unit with the projection.++// What a changed shape does to the output, and to the pointer. The views of a+// window do not move when its border colour changes, so the rim is damaged by the+// projection itself; and a point the shape cuts away goes to what is below.+// Damage is in output pixels and the points below are in layout coordinates, at+// whatever scale an earlier test left the output.+static bool layout_damaged(struct rediwm_scene_output *out, double x, double y) {+ return damaged(out, (int)(x * out->output->scale), (int)(y * out->output->scale));+}+static void test_clip_output(struct rediwm_scene_outputs *scene, struct rediwm_scene_output *out) {+ struct rig rig = rig_create(scene);+ struct rediwm_tree *source = rig.world;+ struct rediwm_projection *p = rediwm_projection_create(rig.own, source, scene);+ assert(p);+ struct rediwm_tree *under_tree = rediwm_tree_create(source);+ struct rediwm_tree *frame = rediwm_tree_create(source);+ struct wlr_buffer *under_buffer = buffer_create(), *client_buffer = buffer_create();+ struct rediwm_buffer *under = rediwm_buffer_create(under_tree, under_buffer);+ struct rediwm_buffer *client = rediwm_buffer_create(frame, client_buffer);+ assert(under_tree && frame && under && client);+ rediwm_node_set_position(&under_tree->node, 300, 200);+ rediwm_node_set_position(&frame->node, 300, 200);+ rediwm_buffer_set_dest_size(under, 120, 80);+ rediwm_buffer_set_dest_size(client, 120, 80);+ struct rediwm_clip_spec spec = {.width = 120, .height = 80, .radii = {10, 10, 10, 10}, .border = 1,+ .border_color = {.1f, .1f, .1f, .1f}};+ assert(rediwm_projection_set_tree_clip(&frame->node, &spec));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ struct leaf *client_leaf = leaf_of(p, &client->node), *under_leaf = leaf_of(p, &under->node);++ // Points inside the window reach it; the corner pixel, which the arc cuts away,+ // goes to the window below, and so does the one just outside the box.+ double nx, ny;+ struct rediwm_node *hit = pick(p, 360, 240, &nx, &ny);+ assert(hit == client_leaf->own);+ hit = pick(p, 300.5, 200.5, &nx, &ny);+ assert(hit == under_leaf->own);+ hit = pick(p, 419.5, 279.5, &nx, &ny); // bottom-right corner+ assert(hit == under_leaf->own);+ hit = pick(p, 310.5, 200.5, &nx, &ny); // on the straight edge+ assert(hit == client_leaf->own);+ // An inert window is still drawn, but the pointer reaches what is below it.+ assert(rediwm_projection_set_tree_inert(&frame->node, true));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(client_leaf->item.enabled && pick(p, 360, 240, &nx, &ny) == under_leaf->own);+ assert(rediwm_projection_set_tree_inert(&frame->node, false));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(pick(p, 360, 240, &nx, &ny) == client_leaf->own);+ // Without a shape the corner is the window's again.+ assert(rediwm_projection_set_tree_clip(&frame->node, NULL));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(pick(p, 300.5, 200.5, &nx, &ny) == client_leaf->own);+ clear_damage(out);++ // A shape appearing, a different border colour, and the shape going away each+ // damage the rim and only the rim. Nothing else moved.+ assert(rediwm_projection_set_tree_clip(&frame->node, &spec));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(layout_damaged(out, 301, 201) && layout_damaged(out, 419, 279) && layout_damaged(out, 360, 202));+ assert(!layout_damaged(out, 360, 240));+ clear_damage(out);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0); // Nothing changed.+ assert(!pixman_region32_not_empty(&out->damage_ring.current));+ spec.border_color[0] = .3f;+ assert(rediwm_projection_set_tree_clip(&frame->node, &spec));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(layout_damaged(out, 301, 201) && layout_damaged(out, 419, 279) && layout_damaged(out, 300, 240));+ assert(!layout_damaged(out, 360, 240) && !layout_damaged(out, 100, 100));+ assert(pixman_region32_not_empty(&out->pending_commit_damage));+ clear_damage(out);+ // Setting the same shape again damages nothing.+ assert(rediwm_projection_set_tree_clip(&frame->node, &spec));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(!pixman_region32_not_empty(&out->damage_ring.current));+ // The shape going away damages the rim, not the interior.+ assert(rediwm_projection_set_tree_clip(&frame->node, NULL));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(layout_damaged(out, 301, 201) && layout_damaged(out, 419, 279) && !layout_damaged(out, 360, 240));+ // Moving a window moves its views, which damage themselves; the shape follows+ // without a second, rim-only report of its own.+ assert(rediwm_projection_set_tree_clip(&frame->node, &spec));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ clear_damage(out);+ rediwm_node_set_position(&frame->node, 310, 200);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(layout_damaged(out, 370, 240)); // the views of the window moved+ rediwm_node_set_position(&frame->node, 300, 200);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(rediwm_projection_set_tree_clip(&frame->node, NULL));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ // A hidden window damages nothing for a shape change.+ rediwm_node_set_enabled(&frame->node, false);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ clear_damage(out);+ assert(rediwm_projection_set_tree_clip(&frame->node, &spec));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(!pixman_region32_not_empty(&out->damage_ring.current));+ spec.border_color[1] = .7f;+ assert(rediwm_projection_set_tree_clip(&frame->node, &spec));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(!pixman_region32_not_empty(&out->damage_ring.current));++ rediwm_projection_destroy(p);+ rig_destroy(&rig);+ wlr_buffer_drop(under_buffer);+ wlr_buffer_drop(client_buffer);+}+// Stage 4d: a shadow's view has no buffer, so wlroots damages nothing for it and the+// projection damages the footprint itself: when it appears, moves, changes, is+// hidden with its window, or goes away, and never when nothing changed.+static void test_shadow_output(struct rediwm_scene_outputs *scene, struct rediwm_scene_output *out) {+ struct rig rig = rig_create(scene);+ struct rediwm_tree *source = rig.world;+ struct rediwm_projection *p = rediwm_projection_create(rig.own, source, scene);+ assert(p);+ struct rediwm_tree *frame = rediwm_tree_create(source);+ struct rediwm_tree *shadow_tree = rediwm_tree_create(frame);+ struct rediwm_buffer *footprint = rediwm_buffer_create(shadow_tree, NULL);+ assert(frame && shadow_tree && footprint);+ rediwm_node_set_position(&frame->node, 100, 100);+ rediwm_node_set_position(&footprint->node, -16, -8);+ rediwm_buffer_set_dest_size(footprint, 152, 112);+ struct rediwm_shadow_spec spec = {.width = 120, .height = 80, .radii = {10, 10, 10, 10},+ .offset_y = 8, .softness = 16, .color = {0, 0, 0, .4f}};+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ struct leaf *l = leaf_of(p, &footprint->node);+ assert(l->item.kind == REDIWM_ITEM_BUFFER && !l->item.shadow);+ clear_damage(out);++ assert(rediwm_projection_set_tree_shadow(&shadow_tree->node, &spec));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(l->item.kind == REDIWM_ITEM_SHADOW && l->item.shadow);+ assert(near(l->item.shadow->shape.edges.x1, 100) && near(l->item.shadow->shape.edges.y2, 180));+ assert(near(l->item.shadow->offset_y, 8) && near(l->item.shadow->softness, 16));+ assert(layout_damaged(out, 85, 93) && layout_damaged(out, 235, 203) && layout_damaged(out, 160, 140));+ assert(!layout_damaged(out, 80, 140) && !layout_damaged(out, 160, 90));+ clear_damage(out);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0); // Nothing changed.+ assert(!pixman_region32_not_empty(&out->damage_ring.current));+ // A moved window damages where its shadow was and where it is.+ rediwm_node_set_position(&frame->node, 140, 100);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(layout_damaged(out, 85, 140) && layout_damaged(out, 270, 140));+ clear_damage(out);+ // A different colour damages it in place.+ spec.color[3] = .5f;+ assert(rediwm_projection_set_tree_shadow(&shadow_tree->node, &spec));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(layout_damaged(out, 130, 140) && !layout_damaged(out, 100, 140));+ clear_damage(out);+ // Hidden with its window, shown again, and destroyed.+ rediwm_node_set_enabled(&frame->node, false);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(layout_damaged(out, 130, 140));+ clear_damage(out);+ rediwm_node_set_enabled(&frame->node, true);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(layout_damaged(out, 130, 140));+ clear_damage(out);+ rediwm_node_destroy(&footprint->node);+ assert(layout_damaged(out, 130, 140));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);++ rediwm_projection_destroy(p);+ rig_destroy(&rig);+}+// 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 rediwm_scene_outputs *scene) {+ struct rig rig = rig_create(scene);+ struct rediwm_tree *source = rig.world;+ struct rediwm_projection *p = rediwm_projection_create(rig.own, source, scene);+ assert(p);+ uint64_t cursor = rediwm_projection_damage_cursor(p), other = cursor;+ struct rediwm_tree *window = rediwm_tree_create(source);+ struct wlr_buffer *buffer = buffer_create(); // 200x120+ struct rediwm_buffer *a = rediwm_buffer_create(window, buffer);+ struct rediwm_buffer *b = rediwm_buffer_create(window, buffer);+ rediwm_node_set_position(&window->node, 40, 30);+ rediwm_buffer_set_dest_size(a, 100, 60); // half size: one buffer px = 0.5+ rediwm_node_set_position(&b->node, 0, 100);+ rediwm_buffer_set_dest_size(b, 20, 20);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ struct leaf *la = leaf_of(p, &a->node), *lb = leaf_of(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.+ rediwm_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));+ rediwm_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));+ rediwm_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.+ rediwm_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;+ rediwm_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 leaf that draws nothing (an input-only node such as a resize grab) has no+ // events, however it moves: glass would otherwise take it for a change below.+ struct rediwm_buffer *grab = rediwm_buffer_create(window, NULL);+ rediwm_buffer_set_dest_size(grab, 300, 300);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ rediwm_node_set_position(&grab->node, 5, 5);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ rediwm_node_destroy(&grab->node);+ assert(events_since(p, &cursor, NULL).count == 0);++ // 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++) {+ rediwm_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);+ rediwm_projection_destroy(p);+ rig_destroy(&rig);+ wlr_buffer_drop(buffer);+}+// A window's dim is a field of what its leaves show: inherited down the tree like+// its shape, ended where a popup hangs under it, and a change of it restyles (so+// damages) every leaf under the tree and no other.+static void test_dim(struct rediwm_scene_outputs *scene) {+ struct rig rig = rig_create(scene);+ struct rediwm_tree *source = rig.world;+ struct rediwm_projection *p = rediwm_projection_create(rig.own, source, scene);+ assert(p);+ uint64_t cursor = rediwm_projection_damage_cursor(p);+ struct rediwm_tree *window = rediwm_tree_create(source);+ struct rediwm_tree *content = rediwm_tree_create(window);+ struct rediwm_tree *popup = rediwm_tree_create(content);+ struct rediwm_tree *sibling = rediwm_tree_create(source);+ float color[4] = {.2, .3, .1, 1};+ struct wlr_buffer *buffer = buffer_create();+ struct rediwm_rect *ring = rediwm_rect_create(window, 10, 10, color);+ struct rediwm_buffer *client = rediwm_buffer_create(content, buffer);+ struct rediwm_buffer *menu = rediwm_buffer_create(popup, buffer);+ struct rediwm_buffer *other = rediwm_buffer_create(sibling, buffer);+ assert(ring && client && menu && other);+ rediwm_node_set_position(&window->node, 40, 30);+ rediwm_node_set_position(&client->node, 0, 10);+ rediwm_buffer_set_dest_size(client, 100, 60);+ rediwm_node_set_position(&popup->node, 50, 20);+ rediwm_buffer_set_dest_size(menu, 30, 30);+ rediwm_node_set_position(&sibling->node, 300, 200);+ rediwm_buffer_set_dest_size(other, 20, 20);+ assert(rediwm_projection_set_tree_unclipped(&popup->node, true));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ struct leaf *lring = leaf_of(p, &ring->node), *lclient = leaf_of(p, &client->node),+ *lmenu = leaf_of(p, &menu->node), *lother = leaf_of(p, &other->node);+ assert(events_since(p, &cursor, NULL).count == 4); // Appearing.++ // Nothing is dimmed until a tree asks, and taking nothing away makes no addon.+ assert(rediwm_projection_set_tree_dim(&sibling->node, 0, 0));+ assert(!lring->item.content.dim_brightness && !lclient->item.content.dim_saturation);++ // Every leaf of the window, not the popup under it and not another window.+ assert(rediwm_projection_set_tree_dim(&window->node, .08f, .1f));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(lring->item.content.dim_brightness == .08f && lring->item.content.dim_saturation == .1f);+ assert(lclient->item.content.dim_brightness == .08f && lclient->item.content.dim_saturation == .1f);+ assert(!lmenu->item.content.dim_brightness && !lmenu->item.content.dim_saturation);+ assert(!lother->item.content.dim_brightness && !lother->item.content.dim_saturation);+ // The change restyles those two leaves (each reports its box as it was and as it+ // is) and no other.+ struct events_seen seen = events_since(p, &cursor, NULL);+ assert(seen.count == 4 && event_is(&seen.last[1], lclient->item.id, 40, 40, 100, 60) &&+ event_is(&seen.last[3], lring->item.id, 40, 30, 10, 10));+ for (int i = 0; i < 4; i++) assert(seen.last[i].leaf == lclient->item.id || seen.last[i].leaf == lring->item.id);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(events_since(p, &cursor, NULL).count == 0); // Unchanged is no damage.+ assert(rediwm_projection_set_tree_dim(&window->node, .1f, .1f));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(events_since(p, &cursor, NULL).count == 4);++ // A fraction outside 0 to 1 is refused and the old dim stays.+ assert(!rediwm_projection_set_tree_dim(&window->node, -.1f, 0));+ assert(!rediwm_projection_set_tree_dim(&window->node, 0, 1.5f));+ assert(!rediwm_projection_set_tree_dim(&window->node, NAN, 0));+ assert(!rediwm_projection_set_tree_dim(&window->node, 0, INFINITY));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(lclient->item.content.dim_brightness == .1f && events_since(p, &cursor, NULL).count == 0);++ // The barrier does not hide a dim the subtree sets for itself, and lifting it+ // gives the popup the window's.+ assert(rediwm_projection_set_tree_dim(&popup->node, .5f, 0));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(lmenu->item.content.dim_brightness == .5f && lmenu->item.content.dim_saturation == 0);+ assert(rediwm_projection_set_tree_dim(&popup->node, 0, 0));+ assert(rediwm_projection_set_tree_unclipped(&popup->node, false));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(lmenu->item.content.dim_brightness == .1f);+ assert(rediwm_projection_set_tree_unclipped(&popup->node, true));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(!lmenu->item.content.dim_brightness);++ // Taking nothing away undoes it, and restyles the leaves back.+ events_since(p, &cursor, NULL);+ assert(rediwm_projection_set_tree_dim(&window->node, 0, 0));+ assert(rediwm_projection_set_tree_dim(&window->node, 0, 0)); // Again is fine.+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(!lring->item.content.dim_brightness && !lclient->item.content.dim_saturation);+ assert(events_since(p, &cursor, NULL).count == 4);++ rediwm_projection_destroy(p);+ rig_destroy(&rig);+ wlr_buffer_drop(buffer);+}+// The output pass hides what a client's opaque region covers, from the region as+// the surface node keeps it. The buffers of a client reveal no format, like a+// wlr_client_buffer, so only the node's own region can say they are opaque: it+// has to land on screen exactly, or one pixel inside where it is scaled.+static void test_opaque(void) {+ pixman_region32_t source, out;+ pixman_region32_init_rect(&source, 50, 30, 100, 60);+ pixman_region32_init(&out);+ // Shown 1:1, a region is used as is, clipped to the box.+ rediwm_mirror_opaque(&out, &source, 200, 120, 200, 120);+ assert(pixman_region32_n_rects(&out) == 1);+ assert(pixman_region32_contains_point(&out, 50, 30, NULL) && pixman_region32_contains_point(&out, 149, 89, NULL));+ assert(!pixman_region32_contains_point(&out, 49, 30, NULL) && !pixman_region32_contains_point(&out, 150, 89, NULL));+ pixman_region32_clear(&out);+ pixman_region32_clear(&source);+ pixman_region32_union_rect(&source, &source, 0, 0, 300, 300);+ rediwm_mirror_opaque(&out, &source, 200, 120, 200, 120);+ assert(pixman_region32_contains_point(&out, 199, 119, NULL) && !pixman_region32_contains_point(&out, 200, 0, NULL));+ // Nothing says nothing.+ pixman_region32_clear(&out);+ pixman_region32_clear(&source);+ rediwm_mirror_opaque(&out, &source, 200, 120, 200, 120);+ assert(!pixman_region32_not_empty(&out));++ // Scaled with the node and pulled one pixel in: the middle is opaque, the edge+ // pixels, which the filter may blend with what is outside, are not.+ pixman_region32_union_rect(&source, &source, 0, 0, 200, 120);+ rediwm_mirror_opaque(&out, &source, 200, 120, 100, 60);+ assert(pixman_region32_contains_point(&out, 40, 25, NULL));+ assert(!pixman_region32_contains_point(&out, 0, 0, NULL) && !pixman_region32_contains_point(&out, 99, 59, NULL));+ assert(pixman_region32_contains_point(&out, 1, 1, NULL) && pixman_region32_contains_point(&out, 98, 58, NULL));+ // A rectangle thinner than the inset vanishes.+ pixman_region32_clear(&out);+ pixman_region32_clear(&source);+ pixman_region32_union_rect(&source, &source, 10, 10, 2, 2);+ rediwm_mirror_opaque(&out, &source, 200, 120, 100, 60);+ assert(!pixman_region32_not_empty(&out));+ // A node with no size shows no region.+ pixman_region32_union_rect(&source, &source, 0, 0, 50, 50);+ rediwm_mirror_opaque(&out, &source, 0, 0, 100, 60);+ assert(!pixman_region32_not_empty(&out));+ pixman_region32_fini(&source);+ pixman_region32_fini(&out);+}diff --git a/src/projection_test_graph.c b/src/projection_test_graph.cnew file mode 100644index 0000000..d58b29d--- /dev/null+++ b/src/projection_test_graph.c@@ -0,0 +1,465 @@+// Graph tests: clean syncs, geometry, clips and their projection.+// Included by projection_test.c, in its one unit with the projection.++// A sync with nothing changed resolves nothing; every kind of change brings it back.+static void test_clean_sync(struct rediwm_scene_outputs *scene) {+ struct rig rig = rig_create(scene);+ struct rediwm_projection *p = rediwm_projection_create(rig.own, rig.world, scene);+ struct wlr_buffer *buffer = buffer_create();+ struct rediwm_tree *frame = rediwm_tree_create(rig.world);+ struct rediwm_buffer *node = rediwm_buffer_create(frame, buffer);+ rediwm_buffer_set_dest_size(node, 40, 30);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ struct leaf *l = leaf_of(p, &node->node);+ assert(l && l->item.edges.x2 - l->item.edges.x1 == 40);+ // Poke the graph: a clean sync leaves it alone.+ l->item.edges.x2 = 12345;+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(l->item.edges.x2 == 12345);+ // The scene changed.+ rediwm_node_set_position(&frame->node, 5, 5);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(l->item.edges.x2 == 45);+ // The camera moved.+ l->item.edges.x2 = 12345;+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(l->item.edges.x2 == 12345);+ rediwm_projection_sync(p, 2, 0, 0, 0, 0);+ assert(l->item.edges.x2 == 90);+ // A tree property changed.+ l->item.edges.x2 = 12345;+ assert(rediwm_projection_set_tree_scale(&frame->node, .5));+ rediwm_projection_sync(p, 2, 0, 0, 0, 0);+ assert(l->item.edges.x2 == 50);+ // A surface commit, or an owned buffer's damage.+ l->item.edges.x2 = 12345;+ rediwm_projection_damage_buffer(p, &node->node, NULL);+ rediwm_projection_sync(p, 2, 0, 0, 0, 0);+ assert(l->item.edges.x2 == 50);+ // Only the group that changed is walked again; a change to the tree's shape walks all.+ const float color[4] = {1, 0, 0, 1};+ struct rediwm_tree *other = rediwm_tree_create(rig.world);+ struct rediwm_rect *rect = rediwm_rect_create(other, 10, 10, color);+ rediwm_projection_sync(p, 2, 0, 0, 0, 0);+ struct leaf *rl = leaf_of(p, &rect->node);+ assert(rl && rl->item.edges.x2 - rl->item.edges.x1 == 20);+ rl->item.edges.x2 = 777;+ l->item.edges.x2 = 12345;+ rediwm_node_set_position(&frame->node, 7, 7);+ rediwm_projection_sync(p, 2, 0, 0, 0, 0);+ assert(l->item.edges.x2 != 12345 && rl->item.edges.x2 == 777);+ rediwm_node_set_position(&rect->node, 1, 1);+ rediwm_projection_sync(p, 2, 0, 0, 0, 0);+ assert(rl->item.edges.x2 != 777);+ rl->item.edges.x2 = 777;+ rediwm_node_raise_to_top(&frame->node);+ rediwm_projection_sync(p, 2, 0, 0, 0, 0);+ assert(rl->item.edges.x2 != 777);+ rediwm_projection_destroy(p);+ rig_destroy(&rig);+ wlr_buffer_drop(buffer);+}+static void test_graph(struct rediwm_scene_outputs *scene) {+ struct rig rig = rig_create(scene);+ struct rediwm_tree *source = rig.world;+ struct rediwm_projection *p = rediwm_projection_create(rig.own, source, scene);+ assert(p);+ struct rediwm_tree *frame = rediwm_tree_create(source);+ struct rediwm_tree *nested = rediwm_tree_create(frame);+ struct rediwm_tree *other = rediwm_tree_create(source);+ float color[4] = {.2, .3, .1, 1};+ struct rediwm_rect *border = rediwm_rect_create(frame, 17, 9, color);+ struct wlr_buffer *buffer = buffer_create(), *glass_buffer = buffer_create();+ struct rediwm_buffer *client = rediwm_buffer_create(nested, buffer);+ struct rediwm_buffer *glass = rediwm_buffer_create(frame, glass_buffer);+ struct rediwm_rect *overlay = rediwm_rect_create(other, 20, 20, color);+ assert(frame && nested && other && border && client && glass && overlay);+ rediwm_node_set_position(&frame->node, 37, 29);+ rediwm_node_set_position(&nested->node, -12, 5);+ rediwm_node_set_position(&client->node, 4, 3);+ rediwm_node_set_position(&border->node, -2, -3);+ rediwm_node_set_position(&other->node, 90, 60);+ effect_node = &glass->node;+ rediwm_buffer_set_dest_size(client, 100, 60);+ rediwm_buffer_set_dest_size(glass, 50, 20);+ assert(rediwm_projection_set_tree_zoom(&frame->node, .85));++ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert_presented(p);+ struct group *g = projection_get_group(p, &frame->node), *h = projection_get_group(p, &other->node);+ struct leaf *border_leaf = leaf_of(p, &border->node), *client_leaf = leaf_of(p, &client->node),+ *glass_leaf = leaf_of(p, &glass->node), *overlay_leaf = leaf_of(p, &overlay->node);+ struct leaf *order[8];+ assert(p->paint.next == &g->paint && g->paint.next == &h->paint && h->paint.next == &p->paint);++ // Kinds. A leaf is a surface only once wlroots has bound it to a wl_surface.+ assert(border_leaf->item.kind == REDIWM_ITEM_RECT);+ assert(client_leaf->item.kind == REDIWM_ITEM_BUFFER);+ assert(glass_leaf->item.kind == REDIWM_ITEM_EFFECT);+ struct wlr_surface surface = {0};+ struct rediwm_surface scene_surface = {.surface = &surface};+ client_leaf->surface = &scene_surface;+ mark_all(p); // the test changed a leaf behind the scene's back+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(client_leaf->item.kind == REDIWM_ITEM_SURFACE);+ client_leaf->surface = NULL; // The synthetic surface has no lifecycle listeners.+ mark_all(p);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(client_leaf->item.kind == REDIWM_ITEM_BUFFER);++ // Paint order, bottom to top, flattened per window.+ assert(items_of(g, order, 8) == 3 && order[0] == client_leaf && order[1] == border_leaf &&+ order[2] == glass_leaf);+ assert(items_of(h, order, 8) == 1 && order[0] == overlay_leaf);++ // Resolving alone shows nothing: what the last present showed is kept.+ const struct rediwm_snap shown_before = client_leaf->shown;+ const double z = .85, cx = 6.5, cy = -2.5, ox = -17, oy = 31;+ projection_resolve(p, z, cx, cy, ox, oy, NULL, true);+ assert(client_leaf->has_shown && client_leaf->shown.x == shown_before.x &&+ client_leaf->shown.y == shown_before.y && client_leaf->shown.width == shown_before.width);++ // The graph holds the exact, unrounded edges: the camera, then the window's+ // zoom about its origin (37, 29), applied to the client at (29, 37) sized+ // 100x60 in source units.+ const double tx = ox * (1 - z) - z * cx, ty = oy * (1 - z) - z * cy, w = .85;+ const double frame_x = z * 37 + tx, frame_y = z * 29 + ty;+ const struct rediwm_edges e = client_leaf->item.edges;+ assert(fabs(e.x1 - (frame_x + z * w * (29 - 37))) < 1e-9);+ assert(fabs(e.y1 - (frame_y + z * w * (37 - 29))) < 1e-9);+ assert(fabs(e.x2 - e.x1 - z * w * 100) < 1e-9 && fabs(e.y2 - e.y1 - z * w * 60) < 1e-9);+ assert(e.x1 != floor(e.x1) && e.y1 != floor(e.y1)); // Fractional until presented.+ // The output consumer sees the newly resolved graph even before present:+ // what was last shown must never become a second source of truth.+ struct graph_probe probe = {.transport = client_leaf->source, .expected = client_leaf->item};+ assert(rediwm_projection_for_each_item(p, false, probe_item, &probe) && probe.count == 1);+ probe.count = 0;+ assert(rediwm_projection_for_each_item(p, true, probe_item, &probe) && probe.count == 1);+ assert(fabs(client_leaf->item.scale - z * w) < 1e-12);+ assert(client_leaf->item.width == 100 && client_leaf->item.height == 60);+ // Edges, not origin and size: a size is the difference of two rounded edges.+ const struct rediwm_snap s = rediwm_edges_snap(e);+ assert(s.width == lround(e.x2) - lround(e.x1) && s.x == lround(e.x1));+ // Scaled content is filtered bilinearly whatever the client asked for.+ rediwm_buffer_set_filter_mode(client, WLR_SCALE_FILTER_NEAREST);+ projection_resolve(p, z, cx, cy, ox, oy, NULL, true);+ assert(client_leaf->item.content.filter_mode == WLR_SCALE_FILTER_BILINEAR);+ projection_resolve(p, 1, 0, 0, 0, 0, NULL, true);+ assert(rediwm_projection_set_tree_zoom(&frame->node, 1.0));+ projection_resolve(p, 1, 0, 0, 0, 0, NULL, true);+ assert(client_leaf->item.content.filter_mode == WLR_SCALE_FILTER_NEAREST);+ assert(rediwm_projection_set_tree_zoom(&frame->node, .85));+ rediwm_buffer_set_filter_mode(client, WLR_SCALE_FILTER_BILINEAR);++ // Presenting shows exactly that graph, and only that graph.+ projection_present(p, true);+ assert_presented(p);+ {+ const struct rediwm_snap now = rediwm_edges_snap(client_leaf->item.edges);+ assert(client_leaf->shown.x == now.x && client_leaf->shown.y == now.y &&+ client_leaf->shown.width == now.width && client_leaf->shown.height == now.height);+ }+ const double zooms[] = {.25, .5, .85, 1, 1.25};+ for (size_t i = 0; i < sizeof(zooms) / sizeof(zooms[0]); ++i)+ for (int x = -9; x <= 9; x += 3) {+ rediwm_node_set_position(&frame->node, x * 7, -x * 5);+ rediwm_projection_sync_fixed(p, zooms[i], x * .5, -x * .25, ox, oy, other);+ assert_presented(p);+ // Fixed: no camera.+ assert(near(overlay_leaf->item.edges.x1, other->node.x) && near(overlay_leaf->item.edges.y1, other->node.y));+ }+ rediwm_node_set_position(&frame->node, 37, 29);++ // Order follows the source: raising a node reorders its list, a reparent+ // moves the leaf between lists, and a disabled branch is still resolved.+ rediwm_node_raise_to_top(&border->node);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(items_of(g, order, 8) == 3 && order[0] == client_leaf && order[1] == glass_leaf &&+ order[2] == border_leaf);+ rediwm_node_reparent(&client->node, other);+ rediwm_node_set_enabled(&other->node, false);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(items_of(g, order, 8) == 2 && order[0] == glass_leaf && order[1] == border_leaf);+ assert(items_of(h, order, 8) == 2 && order[0] == overlay_leaf && order[1] == client_leaf);+ assert(!h->enabled && !client_leaf->item.enabled && !overlay_leaf->item.enabled);+ assert(border_leaf->item.enabled);+ assert_presented(p);+ rediwm_node_set_enabled(&other->node, true);++ // A subtree the pass no longer reaches leaves the graph and is hidden.+ rediwm_node_reparent(&other->node, &rig.parking->tree);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(wl_list_length(&p->paint) == 1 && wl_list_empty(&h->items));+ assert(!h->seen && !client_leaf->seen && !client_leaf->has_shown);+ assert(client_leaf->paint.next == &client_leaf->paint);+ rediwm_node_reparent(&other->node, source);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(wl_list_length(&p->paint) == 2 && wl_list_length(&h->items) == 2);+ assert_presented(p);++ // Destruction with the lists linked: a leaf leaves its list with its source,+ // a group takes the leaves it still lists with it, and neither order of+ // teardown touches freed list heads.+ rediwm_node_destroy(&overlay->node);+ assert(wl_list_length(&h->items) == 1 && wl_list_length(&p->leaves) == 3);+ rediwm_node_destroy(&other->node);+ assert(wl_list_length(&p->paint) == 1 && wl_list_length(&p->groups) == 1);+ assert(wl_list_length(&g->items) == 2);+ effect_node = NULL;+ int checks = 0;+ probe_group(g, &checks);+ rediwm_node_destroy(&frame->node); // The group goes first, then its leaves.+ assert(checks == 2); // Both leaves were checked after the group was freed.+ assert(wl_list_empty(&p->groups) && wl_list_empty(&p->leaves) && wl_list_empty(&p->paint));+ rediwm_projection_destroy(p);+ rig_destroy(&rig);+ wlr_buffer_drop(buffer);+ wlr_buffer_drop(glass_buffer);+}+// Stage 4: the clip shape. Pure geometry first, with no scene at all.+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, {0}, 0, 0, 0, 0, {0}}, one) == 0);+ assert(rediwm_clip_opaque(&(struct rediwm_clip){rediwm_xform_rect(identity, 5, 7, 120, 80), {40, 40, 40, 40}, 0, {0}, 0, 0, 0, 0, {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 rediwm_scene_outputs *scene) {+ struct rig rig = rig_create(scene);+ struct rediwm_tree *source = rig.world;+ struct rediwm_projection *p = rediwm_projection_create(rig.own, source, scene);+ assert(p);+ struct rediwm_tree *frame = rediwm_tree_create(source);+ struct rediwm_tree *nested = rediwm_tree_create(frame);+ struct rediwm_tree *other = rediwm_tree_create(source);+ float color[4] = {.2, .3, .1, 1};+ struct rediwm_rect *border = rediwm_rect_create(frame, 17, 9, color);+ struct wlr_buffer *buffer = buffer_create(), *glass_buffer = buffer_create(), *title_buffer = buffer_create();+ struct rediwm_buffer *client = rediwm_buffer_create(nested, buffer);+ struct rediwm_buffer *title = rediwm_buffer_create(frame, title_buffer);+ struct rediwm_buffer *glass = rediwm_buffer_create(frame, glass_buffer);+ struct rediwm_rect *overlay = rediwm_rect_create(other, 20, 20, color);+ assert(frame && nested && other && border && client && title && glass && overlay);+ rediwm_node_set_position(&frame->node, 37, 29);+ rediwm_node_set_position(&nested->node, 2, 12);+ rediwm_buffer_set_dest_size(client, 100, 60);+ rediwm_buffer_set_dest_size(title, 100, 12);+ rediwm_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 = leaf_of(p, &border->node), *client_leaf = leaf_of(p, &client->node),+ *title_leaf = leaf_of(p, &title->node), *glass_leaf = leaf_of(p, &glass->node),+ *overlay_leaf = leaf_of(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.+ projection_resolve(p, .5, 12, -8, 3, 4, NULL, true);+ 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));+ projection_resolve(p, 1, 0, 0, 0, 0, NULL, true);++ // 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 rediwm_tree *subsurface = rediwm_tree_create(nested);+ struct rediwm_tree *popup = rediwm_tree_create(nested);+ struct wlr_buffer *popup_buffer = buffer_create(), *sub_buffer = buffer_create();+ struct rediwm_buffer *popup_content = rediwm_buffer_create(popup, popup_buffer);+ struct rediwm_buffer *sub_content = rediwm_buffer_create(subsurface, sub_buffer);+ assert(popup_content && sub_content);+ rediwm_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.+ projection_resolve(p, 1, 0, 0, 0, 0, NULL, true);+ struct leaf *popup_leaf = leaf_of(p, &popup_content->node), *sub_leaf = leaf_of(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.+ projection_resolve(p, 1, 0, 0, 0, 0, NULL, true);+ 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));+ projection_resolve(p, 1, 0, 0, 0, 0, NULL, true);+ 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));+ projection_resolve(p, 1, 0, 0, 0, 0, NULL, true);+ 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));+ projection_resolve(p, 1, 0, 0, 0, 0, NULL, true);+ 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]));+ projection_resolve(p, 1, 0, 0, 0, 0, NULL, true);+ assert(near(client_leaf->item.clip->edges.x2, want.edges.x2));+ spec.width = 200;+ assert(rediwm_projection_set_tree_clip(&frame->node, &spec));+ projection_resolve(p, 1, 0, 0, 0, 0, NULL, true);+ 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.+ projection_resolve(p, 1, 0, 0, 0, 0, NULL, true);+ 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;+ rediwm_node_destroy(&frame->node);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ rediwm_projection_destroy(p);+ rig_destroy(&rig);+ wlr_buffer_drop(buffer);+ wlr_buffer_drop(glass_buffer);+ wlr_buffer_drop(title_buffer);+ wlr_buffer_drop(popup_buffer);+ wlr_buffer_drop(sub_buffer);+}diff --git a/src/projection_test_groups.c b/src/projection_test_groups.cnew file mode 100644index 0000000..6208bbb--- /dev/null+++ b/src/projection_test_groups.c@@ -0,0 +1,346 @@+// Group and feedback tests: grouping, damage per group, surface feedback and+// frame callbacks.+// Included by projection_test.c, in its one unit with the projection.++static bool in_group(struct group *g, struct leaf *l) {+ struct leaf *listed;+ wl_list_for_each(listed, &g->items, paint) if (listed == l) return true;+ return false;+}+static int snap_width(struct leaf *l) { return rediwm_edges_snap(l->item.edges).width; }+static void test_groups(struct rediwm_scene_outputs *scene, struct rediwm_scene_output *out) {+ struct rig rig = rig_create(scene);+ struct rediwm_tree *source = rig.world;+ struct rediwm_projection *p = rediwm_projection_create(rig.own, source, scene);+ assert(p);+ struct rediwm_tree *frame = rediwm_tree_create(source);+ struct rediwm_tree *nested = rediwm_tree_create(frame);+ struct rediwm_tree *other = rediwm_tree_create(source);+ float color[4] = {.2, .3, .1, 1};+ struct rediwm_rect *border = rediwm_rect_create(frame, 17, 9, color);+ struct wlr_buffer *buffer = buffer_create();+ struct rediwm_buffer *client = rediwm_buffer_create(nested, buffer);+ struct rediwm_rect *overlay = rediwm_rect_create(other, 20, 20, color);+ assert(frame && nested && other && border && client && overlay);+ rediwm_node_set_position(&frame->node, 37, 29);+ rediwm_node_set_position(&nested->node, -12, 5);+ rediwm_node_set_position(&client->node, 4, 3);+ rediwm_node_set_position(&border->node, -2, -3);+ rediwm_node_set_position(&other->node, 90, 60);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ struct group *g = projection_get_group(p, &frame->node);+ struct group *h = projection_get_group(p, &other->node);+ struct leaf *l = leaf_of(p, &client->node);+ struct leaf *border_leaf = leaf_of(p, &border->node);+ assert(in_group(g, l) && in_group(g, border_leaf) && in_group(h, leaf_of(p, &overlay->node)));+ assert(wl_list_length(&p->paint) == 2);+ assert_position(p, &client->node, 29, 37);+ assert_position(p, &border->node, 35, 26);+ clear_damage(out);+ rediwm_node_set_position(&frame->node, 83, 71);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ // Ordinary movement moves every leaf of the group.+ assert_position(p, &client->node, 75, 79);+ assert_position(p, &border->node, 81, 68);+ assert(damaged(out, 50, 60) && damaged(out, 200, 150));+ clear_damage(out);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(!pixman_region32_not_empty(&out->damage_ring.current));++ // A fixed desktop ignores the camera while neighboring windows still pan.+ rediwm_projection_sync_fixed(p, .5, 10, 20, 0, 0, frame);+ assert_position(p, &client->node, 75, 79);+ assert_position(p, &overlay->node, 40, 20);+ assert(snap_width(l) == 200);+ rediwm_projection_sync(p, .5, 10, 20, 0, 0);+ assert_position(p, &client->node, 33, 30);++ // Endpoint rounding must remain global, including half-pixels crossing+ // zero. Integer rounding of a local offset is not equivalent here.+ const double zooms[] = {.5, .85, 1, 1.25};+ for (size_t i = 0; i < sizeof(zooms) / sizeof(zooms[0]); ++i) {+ double z = zooms[i], cx = 6.5, cy = -2.5, ox = -17, oy = 31;+ double tx = ox * (1 - z) - z * cx, ty = oy * (1 - z) - z * cy;+ for (int x = -13; x <= 13; ++x) {+ rediwm_node_set_position(&frame->node, x, -x);+ rediwm_projection_sync(p, z, cx, cy, ox, oy);+ assert_position(p, &client->node, lround(z * (x - 8) + tx),+ lround(z * (-x + 8) + ty));+ assert_position(p, &border->node, lround(z * (x - 2) + tx),+ lround(z * (-x - 3) + ty));+ assert(snap_width(l) == lround(z * (x - 8 + 200) + tx) -+ lround(z * (x - 8) + tx));+ }+ }+ // Window zoom multiplies the camera scale while preserving its world origin.+ // Nested geometry keeps the same per-leaf projected scale.+ assert(rediwm_projection_set_tree_zoom(&frame->node, .85));+ rediwm_node_set_position(&frame->node, -17, 23);+ rediwm_projection_sync(p, .5, 3, -7, 0, 0);+ assert_position(p, &client->node, lround(.5 * -17 - 1.5 + (.85 * .5) * -8),+ lround(.5 * 23 + 3.5 + (.85 * .5) * 8));+ assert(l->item.scale == (.85 * .5));++ // A relative tree scale composes with camera and window zoom, preserving origin.+ assert(rediwm_projection_set_tree_scale(&nested->node, .5));+ rediwm_projection_sync(p, .5, 3, -7, 0, 0);+ assert(fabs(l->item.scale - ((.85 * .5) * .5)) < .0001);+ assert_position(p, &client->node, lround(.5 * -17 - 1.5 + (.85 * .5) * -12 + (.85 * .5) * .5 * 4),+ lround(.5 * 23 + 3.5 + (.85 * .5) * 5 + (.85 * .5) * .5 * 3));+ assert(rediwm_projection_set_tree_scale(&nested->node, 1.0));+ rediwm_projection_sync(p, .5, 3, -7, 0, 0);+ assert(fabs(l->item.scale - (.85 * .5)) < .0001);+ assert(rediwm_projection_set_tree_zoom(&frame->node, 1.0));++ // Reorder a whole window and a leaf within it, retaining source paint order.+ rediwm_node_raise_to_top(&frame->node);+ rediwm_node_raise_to_top(&nested->node);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ struct leaf *order[8];+ assert(p->paint.next == &h->paint && h->paint.next == &g->paint && g->paint.next == &p->paint);+ assert(items_of(g, order, 8) == 2 && order[0] == border_leaf && order[1] == l);+ // The source node and its input stay attached to the leaf when a subsurface+ // changes source root, rather than retaining a stale group.+ rediwm_node_reparent(&client->node, other);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(in_group(h, l) && !in_group(g, l));+ assert_position(p, &client->node, 94, 63);+ double input_x, input_y;+ assert(pick(p, 94 + 40, 63 + 24, &input_x, &input_y) == &client->node);+ assert(input_x == 40 && input_y == 24);+ rediwm_node_set_enabled(&other->node, false);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(!l->item.enabled && !pick(p, 94 + 40, 63 + 24, &input_x, &input_y));+ rediwm_node_set_enabled(&other->node, true);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert_position(p, &client->node, 94, 63);++ // A former source root can become nested, leave the projected tree, and+ // return. Its unused group is not reached while its leaves change group, and+ // neither its former position nor window zoom may leak into other leaves.+ rediwm_node_reparent(&frame->node, other);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(!g->seen);+ assert(in_group(h, border_leaf));+ assert_position(p, &border->node, 71, 80);+ assert_position(p, &client->node, 94, 63);+ rediwm_node_reparent(&frame->node, &rig.parking->tree);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(!border_leaf->seen);+ rediwm_node_reparent(&frame->node, source);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(g->seen);+ assert(in_group(g, border_leaf));+ assert_position(p, &border->node, -19, 20);++ // A leaf moved out of a window's tree before the tree is destroyed keeps its+ // leaf: the surviving source node needs nothing recreated.+ struct rediwm_tree *temporary = rediwm_tree_create(source);+ struct rediwm_rect *survivor = rediwm_rect_create(temporary, 12, 12, color);+ assert(temporary && survivor);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ rediwm_node_reparent(&survivor->node, other);+ rediwm_node_destroy(&temporary->node);+ assert(leaf_of(p, &survivor->node));+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert_position(p, &survivor->node, 90, 60);+ rediwm_node_destroy(&survivor->node);++ // A direct leaf can itself be a former group root. After it becomes nested,+ // destroying the source runs both leaf and group cleanup on the same node.+ struct rediwm_rect *direct = rediwm_rect_create(source, 12, 12, color);+ assert(direct);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ rediwm_node_reparent(&direct->node, other);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(in_group(h, leaf_of(p, &direct->node)));+ rediwm_node_destroy(&direct->node);+ assert(wl_list_length(&p->groups) == 2 && wl_list_length(&p->leaves) == 3);++ // Source-first teardown destroys its group without dangling leaf listeners.+ rediwm_node_destroy(&frame->node);+ assert(wl_list_length(&p->groups) == 1 && wl_list_length(&p->leaves) == 2);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ rediwm_node_destroy(&other->node);+ assert(wl_list_empty(&p->groups) && wl_list_empty(&p->leaves));+ rediwm_projection_destroy(p);+ rig_destroy(&rig);+ wlr_buffer_drop(buffer);+}+// Stage 6b: the screen's damage comes from the graph. A restack damages what+// changed order, a change of what a leaf itself shows is cut to what was visible+// of it at the last build, and a window going away is damaged at once.+static void test_graph_damage(struct rediwm_scene_outputs *scene, struct rediwm_scene_output *out) {+ struct rig rig = rig_create(scene);+ struct rediwm_tree *source = rig.world;+ struct rediwm_projection *p = rediwm_projection_create(rig.own, source, scene);+ assert(p);+ struct wlr_buffer *buffer = buffer_create(); // 200x120+ struct rediwm_tree *frame_a = rediwm_tree_create(source);+ struct rediwm_tree *frame_b = rediwm_tree_create(source);+ struct rediwm_buffer *a = rediwm_buffer_create(frame_a, buffer);+ struct rediwm_buffer *b = rediwm_buffer_create(frame_b, buffer);+ rediwm_node_set_position(&frame_a->node, 20, 20);+ rediwm_node_set_position(&frame_b->node, 60, 40);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ clear_damage(out);++ // Raising the window below changes the pixels where the two overlap, and says+ // nothing about the rest of the screen.+ rediwm_node_raise_to_top(&frame_a->node);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(layout_damaged(out, 100, 60) && layout_damaged(out, 210, 130));+ assert(!layout_damaged(out, 350, 300) && !layout_damaged(out, 5, 5));+ clear_damage(out);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0); // Same order: nothing.+ assert(!pixman_region32_not_empty(&out->damage_ring.current));++ // What changes only what a leaf shows is cut to what of it was visible at the+ // last build: the right part of `a` was hidden, so its change is not damaged.+ struct leaf *la = leaf_of(p, &a->node), *lb = leaf_of(p, &b->node);+ pixman_region32_t visible;+ pixman_region32_init_rect(&visible, 20, 20, 50, 120);+ rediwm_projection_note_visible(p, &a->node, &visible);+ rediwm_projection_damage_buffer(p, &a->node, NULL);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(layout_damaged(out, 30, 60) && !layout_damaged(out, 150, 60));+ clear_damage(out);+ // Nothing visible, nothing to damage; a leaf the pass never saw is damaged whole.+ pixman_region32_clear(&visible);+ rediwm_projection_note_visible(p, &a->node, &visible);+ rediwm_projection_damage_buffer(p, &a->node, NULL);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(!pixman_region32_not_empty(&out->damage_ring.current));+ assert(!lb->has_visible);+ rediwm_projection_damage_buffer(p, &b->node, NULL);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(layout_damaged(out, 70, 50) && layout_damaged(out, 250, 150));+ clear_damage(out);+ // A leaf that is moved damages where it was only where it was visible, and+ // where it is whatever the last build saw.+ pixman_region32_init_rect(&visible, 60, 40, 40, 120);+ rediwm_projection_note_visible(p, &b->node, &visible);+ rediwm_node_set_position(&frame_b->node, 200, 150);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ assert(layout_damaged(out, 70, 60) && !layout_damaged(out, 150, 60)); // old box, cut+ assert(layout_damaged(out, 250, 200)); // new box, whole+ clear_damage(out);+ (void)la;++ // A window going away damages what it showed without waiting for a sync: a+ // capture can build a frame before the next one.+ rediwm_projection_note_visible(p, &a->node, &(pixman_region32_t){.extents = {20, 20, 220, 140}, .data = NULL});+ rediwm_node_destroy(&frame_a->node);+ assert(layout_damaged(out, 30, 30) && layout_damaged(out, 210, 130));+ pixman_region32_fini(&visible);+ rediwm_projection_destroy(p);+ rig_destroy(&rig);+ wlr_buffer_drop(buffer);+}+// Stage 6a: what the output pass reports of a surface becomes the signals wlroots+// would have sent. Two outputs side by side, a node over the seam.+static int outputs_updates, frame_dones, update_size;+static struct rediwm_scene_output *update_active[4];+static void on_outputs_update(struct wl_listener *listener, void *data) {+ (void)listener;+ const struct rediwm_outputs_update_event *event = data;+ outputs_updates++;+ update_size = (int)event->size;+ for (size_t i = 0; i < event->size && i < 4; i++) update_active[i] = event->active[i];+}+static void on_frame_done(struct wl_listener *listener, void *data) {+ (void)listener;+ (void)data;+ frame_dones++;+}+static void report(struct rediwm_projection *p, struct rediwm_node *node, int x, int y, int w, int h) {+ pixman_region32_t visible;+ pixman_region32_init(&visible);+ if (w > 0) pixman_region32_union_rect(&visible, &visible, x, y, w, h);+ rediwm_projection_feedback_begin(p);+ rediwm_projection_feedback_note(p, node, &visible);+ rediwm_projection_feedback_end(p);+ pixman_region32_fini(&visible);+}+static void test_feedback(struct rediwm_scene_outputs *scene, struct wlr_backend *backend, struct rediwm_scene_output *left,+ struct wlr_output *left_output) {+ struct wlr_output *right_output = wlr_headless_add_output(backend, 320, 240);+ assert(right_output);+ struct wlr_output_state state;+ wlr_output_state_init(&state);+ wlr_output_state_set_enabled(&state, true);+ assert(wlr_output_commit_state(right_output, &state));+ wlr_output_state_finish(&state);+ struct rediwm_scene_output *right = rediwm_scene_output_create(scene, right_output, NULL);+ assert(right);+ int seam = 0, unused = 0;+ wlr_output_effective_resolution(left_output, &seam, &unused);+ right->x = seam;++ struct rig rig = rig_create(scene);+ struct rediwm_tree *source = rig.world;+ struct rediwm_projection *p = rediwm_projection_create(rig.own, source, scene);+ assert(p);+ struct wlr_buffer *buffer = buffer_create();+ struct rediwm_buffer *node = rediwm_buffer_create(source, buffer);+ rediwm_node_set_position(&node->node, seam - 26, 50);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ struct leaf *l = leaf_of(p, &node->node);+ l->feedback = calloc(1, sizeof(*l->feedback));+ assert(l->feedback);+ l->feedback->leaf = l;+ wl_list_insert(&p->feedback, &l->feedback->link);+ struct wl_listener updated = {.notify = on_outputs_update}, done = {.notify = on_frame_done};+ wl_signal_add(&node->events.outputs_update, &updated);+ wl_signal_add(&node->events.frame_done, &done);+ struct timespec now = {0};++ // Over the seam: both outputs show more than a tenth, the one with most is primary.+ report(p, &node->node, seam - 26, 50, 200, 100); // 26 columns left, 174 right+ assert(outputs_updates == 1 && update_size == 2 && l->feedback->primary == right);+ assert(l->feedback->active == (1ull << left->index | 1ull << right->index));+ report(p, &node->node, seam - 26, 50, 200, 100);+ assert(outputs_updates == 1); // Unchanged: no signal.+ // A sliver on the other side is ignored (10 % rule): 2 of 28 columns.+ report(p, &node->node, seam - 26, 50, 28, 100);+ assert(outputs_updates == 2 && update_size == 1 && update_active[0] == left && l->feedback->primary == left);+ // Frames are sent for what is shown, and for no output it is not.+ rediwm_projection_send_frame_done(p, left, &now);+ assert(frame_dones == 1);+ // A hidden leaf hears none, whatever it was told last.+ rediwm_node_set_enabled(&node->node, false);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ rediwm_projection_send_frame_done(p, left, &now);+ assert(frame_dones == 1);+ rediwm_node_set_enabled(&node->node, true);+ rediwm_projection_sync(p, 1, 0, 0, 0, 0);+ report(p, &node->node, 0, 0, 0, 0); // hidden+ assert(outputs_updates == 3 && update_size == 0 && !l->feedback->active && !l->feedback->primary);+ rediwm_projection_send_frame_done(p, left, &now);+ assert(frame_dones == 1); // nothing shown, nothing sent+ // An active output whose scale changed hears again, whatever else is the same.+ report(p, &node->node, seam - 26, 50, 26, 100);+ assert(outputs_updates == 4 && update_size == 1);+ report(p, &node->node, seam - 26, 50, 26, 100);+ assert(outputs_updates == 4);+ wlr_output_state_init(&state);+ wlr_output_state_set_scale(&state, 1.25f);+ assert(wlr_output_commit_state(left_output, &state));+ wlr_output_state_finish(&state);+ report(p, &node->node, seam - 26, 50, 26, 100);+ assert(outputs_updates == 5);+ report(p, &node->node, seam - 26, 50, 26, 100);+ assert(outputs_updates == 5);+ wlr_output_state_init(&state);+ wlr_output_state_set_scale(&state, 1.f);+ assert(wlr_output_commit_state(left_output, &state));+ wlr_output_state_finish(&state);++ wl_list_remove(&updated.link);+ wl_list_remove(&done.link);+ rediwm_projection_destroy(p);+ rig_destroy(&rig);+ wlr_buffer_drop(buffer);+ rediwm_scene_output_destroy(right);+ wlr_output_destroy(right_output);+}diff --git a/src/scene_output.c b/src/scene_output.cindex 0393ca8..20a0616 100644--- a/src/scene_output.c+++ b/src/scene_output.c@@ -1,89 +1,12 @@// Owned output assembly, wlroots 0.20 ABI. Geometry is resolved by projection;// scene nodes below are resource/feedback transports, never world geometry.-#define WLR_USE_UNSTABLE-#define WLR_PRIVATE private-#include "scene_output.h"-#include "scene_outputs.h"-#include "scene_view.h"-#include "scene_pass.h"-#include "scene_graph.h"-#include "projection.h"-#include "glass.h"-#include "node.h"-#include <math.h>-#include <stdlib.h>-#include <string.h>-#ifdef REDIWM_DAMAGE_AUDIT-#include <drm_fourcc.h>-#include <wlr/render/allocator.h>-#include <wlr/render/wlr_renderer.h>-#endif-#include <wlr/render/drm_syncobj.h>-#include <wlr/render/swapchain.h>-#include <wlr/render/wlr_texture.h>-#include <wlr/types/wlr_buffer.h>-#include <wlr/types/wlr_compositor.h>-#include <wlr/types/wlr_damage_ring.h>-#include <wlr/types/wlr_output.h>-#include "node.h"-#include <wlr/util/log.h>-#include <wlr/util/region.h>-#include <wlr/util/transform.h>+#include "scene_output_internal.h"++// The parts: scene_output_collect.c turns the graph into this frame's entries,+// scene_output_effects.c decides which glass blurs are re-taken and+// scene_output_draw.c draws entries and effects. This file keeps the+// per-output state, direct scanout, the damage audit and the frame build.-// Stage 5c: a glass effect's blur, kept per effect and output between frames-// (D1, D3 of docs/plans/scene-graph-stage5.md) and re-taken only when something-// below the effect changed in the region it reads.-struct effect_cache {- struct wl_list link;- uint64_t serial; // rediwm_glass_info.serial- struct rediwm_backdrop *backdrop;- struct wlr_box region; // device pixels the blur covers- float sigma; // device pixels- bool valid, seen;- // A hold moved the blur without re-taking it: re-take it once released.- bool carried;-};-struct output_state {- struct wl_list link;- struct rediwm_output_renderer *renderer;- struct wlr_output *output;- struct wl_listener destroy;- uint64_t cursor; // into the projection's damage events- bool started;- // Software cursors drawn last frame (device pixels), whose damage now is old.- struct wlr_box cursors[4];- int ncursors;- struct wl_list caches;- // rediwm_scene_output_set_veil: black over the frame (0 to 1), and whether it- // changed since the last frame was built, so that change re-blurs no glass.- float veil;- bool veil_changed;-};-struct entry {- struct rediwm_item item;- struct rediwm_node *transport;- struct wlr_box box; // snap of item edges in layout space- pixman_region32_t visible, opaque;- bool sampled;- // Effects only: what glass registered, the box the blur reads (layout, the- // target's grown by three sigma), the surface drawn over it, and whether- // this frame takes a new blur.- struct rediwm_glass_info glass;- struct wlr_box padded;- float sigma; // layout pixels- struct rediwm_node *front;- struct effect_cache *cache;- bool reblur;-};-static bool render_entry(struct rediwm_output_renderer *r, struct entry *e, struct rediwm_scene_output *out,- struct wlr_render_pass *pass, const struct output_view *v, pixman_region32_t *damage, bool backdrop);-struct rediwm_output_renderer {- struct rediwm_scene_renderer *pass;- struct wl_array entries; // reusable scratch; no retained resource references- struct wl_list outputs; // output_state- struct rediwm_projection *projection; // of the frame being built- bool has_backdrops;-};struct rediwm_output_renderer *rediwm_output_renderer_create(struct wlr_renderer *renderer) {struct rediwm_output_renderer *r = calloc(1, sizeof(*r));if (!r) return NULL;@@ -157,235 +80,6 @@ bool rediwm_scene_output_set_veil(struct rediwm_output_renderer *r, struct rediwrediwm_scene_output_damage_whole(out);return true;}-// Stage 3 parity deliberately retains the legacy layout snap, then snaps-// output-scaled edges. Removing that first snap is a geometry change, not a-// renderer switch. No view node position/size is consulted here.-static struct wlr_box device_box(struct wlr_box box, const struct output_view *v) {- box.x -= v->logical.x;- box.y -= v->logical.y;- int x2 = round((box.x + box.width) * v->scale);- int y2 = round((box.y + box.height) * v->scale);- box.x = round(box.x * v->scale);- box.y = round(box.y * v->scale);- box.width = x2 - box.x;- box.height = y2 - box.y;- wlr_box_transform(&box, &box, wlr_output_transform_invert(v->transform),- v->trans_width, v->trans_height);- return box;-}-static void device_region(pixman_region32_t *region, const struct output_view *v, bool expand) {- output_view_region(region, v, expand);-}-// Which device corner each logical corner (tl, tr, br, bl) lands on, found by-// moving a one-pixel box in each corner of a small frame through the same-// transform device_box applies, so the two cannot disagree.-static void device_radii(float out[4], const float in[4], enum wl_output_transform transform) {- enum { W = 3, H = 5 };- const struct wlr_box corners[4] = {{0, 0, 1, 1}, {W - 1, 0, 1, 1}, {W - 1, H - 1, 1, 1}, {0, H - 1, 1, 1}};- const int w = (transform & 1) ? H : W, h = (transform & 1) ? W : H;- for (int i = 0; i < 4; i++) {- struct wlr_box b;- wlr_box_transform(&b, &corners[i], wlr_output_transform_invert(transform), W, H);- out[b.x < w / 2 ? (b.y < h / 2 ? 0 : 3) : (b.y < h / 2 ? 1 : 2)] = in[i];- }-}-// The pass's shape for a clip: the clip's box snapped in layout space like any-// item's, then taken to device pixels by the same function as the pieces that-// tile it, so the shape and the quads agree on every pixel column. A ring is-// whole device pixels, never thinner than one.-static void shape_for(struct rediwm_pass_shape *s, const struct rediwm_clip *c, const struct output_view *v) {- const struct rediwm_snap f = rediwm_edges_snap(c->edges);- s->box = device_box((struct wlr_box){f.x, f.y, f.width, f.height}, v);- float radii[4];- for (int i = 0; i < 4; i++) radii[i] = (float)(c->radii[i] * v->scale);- device_radii(s->radii, radii, v->transform);- s->border = c->border > 0 ? fmaxf(1.f, roundf((float)(c->border * v->scale))) : 0.f;- for (int i = 0; i < 4; i++) s->border_color[i] = c->border_color[i];- s->glow_radius = s->glow_depth = 0;- memset(s->glow_center, 0, sizeof(s->glow_center));- memset(s->glow_color, 0, sizeof(s->glow_color));- if (c->glow_radius > 0 && c->glow_color[3] > 0) {- // The highlight's point goes through the same transform as the box, as a- // one-pixel box (the loss is under a layout pixel, far inside its radius).- const struct wlr_box at = device_box((struct wlr_box){(int)floor(c->glow_x), (int)floor(c->glow_y), 1, 1}, v);- s->glow_center[0] = at.x + at.width * .5f - s->box.x;- s->glow_center[1] = at.y + at.height * .5f - s->box.y;- s->glow_radius = (float)(c->glow_radius * v->scale);- s->glow_depth = (float)(c->glow_depth * v->scale);- for (int i = 0; i < 4; i++) s->glow_color[i] = c->glow_color[i];- }-}-// What of a clipped item may be drawn without the shape and without blending: the-// model's opaque cover, minus a margin around the edge where the ring is drawn.-// Layout space, like the item's own opaque region.-static void clip_cover(pixman_region32_t *out, const struct rediwm_clip *c, double scale) {- pixman_region32_init(out);- struct rediwm_snap parts[3];- const int n = rediwm_clip_opaque(c, parts);- for (int i = 0; i < n; i++)- pixman_region32_union_rect(out, out, parts[i].x, parts[i].y, parts[i].width, parts[i].height);- if (c->border > 0) {- const struct rediwm_snap f = rediwm_edges_snap(c->edges);- const int ring = (int)ceil(fmax(c->border, 1.0 / scale)) + 1;- if (f.width > 2 * ring && f.height > 2 * ring)- pixman_region32_intersect_rect(out, out, f.x + ring, f.y + ring, f.width - 2 * ring, f.height - 2 * ring);- else- pixman_region32_clear(out);- }-}-struct collector {- struct rediwm_output_renderer *renderer;- const struct output_view *view;- // The logical boxes of the other enabled outputs.- struct wlr_box other[15];- int others;- pixman_region32_t covered;- // A window's items are contiguous and share one clip, so its cover is built- // once for the run rather than for each of its six or so items.- struct rediwm_clip cover_of;- pixman_region32_t cover;- bool has_cover;- // What the effects visited so far read (layout): items there are kept even- // where surfaces above hide them, for the blurs only (stage 5f).- pixman_region32_t reads;-};-static bool same_clip(const struct rediwm_clip *a, const struct rediwm_clip *b) {- if (a->edges.x1 != b->edges.x1 || a->edges.y1 != b->edges.y1 || a->edges.x2 != b->edges.x2 ||- a->edges.y2 != b->edges.y2 || a->border != b->border) return false;- for (int i = 0; i < 4; i++) if (a->radii[i] != b->radii[i]) return false;- return true;-}-static bool collect(const struct rediwm_item *item, struct rediwm_node *transport, void *data) {- struct collector *collector = data;- struct rediwm_output_renderer *r = collector->renderer;- if (!item->enabled) { return true; }- struct rediwm_snap s = rediwm_edges_snap(item->edges);- if (s.width <= 0 || s.height <= 0) { return true; }- struct wlr_box box = {s.x, s.y, s.width, s.height}, intersection;- const bool here = wlr_box_intersection(&intersection, &box, &collector->view->logical);- // An item on another output only is not drawn for this one, but it still hides- // what is below it there, which the surface feedback below counts (stage 6).- bool elsewhere = false;- for (int i = 0; i < collector->others && !elsewhere; i++) {- struct wlr_box other;- elsewhere = wlr_box_intersection(&other, &box, &collector->other[i]);- }- if (!here && !elsewhere) { return true; }- // Visit top-to-bottom so fully occluded items never enter the render list- // and never allocate/copy content. The graph, not the view's visible region,- // supplies the bounds and paint order.- pixman_region32_t visible;- pixman_region32_init_rect(&visible, box.x, box.y, box.width, box.height);- pixman_region32_subtract(&visible, &visible, &collector->covered);- // What a client surface shows of itself on every output is what it is told:- // this counts for an item that draws nothing as well (opacity 0), as it does- // for wlroots.- if (item->kind == REDIWM_ITEM_SURFACE)- rediwm_projection_feedback_note(r->projection, transport, &visible);- if (item->kind != REDIWM_ITEM_EFFECT)- rediwm_projection_note_visible(r->projection, transport, &visible);- if (here)- pixman_region32_intersect_rect(&visible, &visible, intersection.x, intersection.y,- intersection.width, intersection.height);- struct rediwm_glass_info glass = {0};- bool skip = false;- if (item->kind == REDIWM_ITEM_RECT) {- skip = item->content.color[3] == 0;- } else if (item->kind == REDIWM_ITEM_EFFECT) {- // Glass is drawn here (stage 5c); without the blur program there is none,- // and the surface's own tint shows, as glass.c's fallback did.- skip = !here || !r->has_backdrops || item->content.opacity == 0 ||- !rediwm_glass_effect_info(transport, &glass) || glass.opacity <= 0;- } else if (item->kind == REDIWM_ITEM_SHADOW) {- // No buffer: the pixels are the shadow's, and never opaque.- skip = item->content.opacity == 0 || item->shadow->color[3] == 0;- } else {- struct rediwm_buffer *b = rediwm_buffer_from_node(transport);- skip = item->content.opacity == 0 ||- (!b->buffer && !b->texture && !b->is_single_pixel_buffer);- }- if (skip) { pixman_region32_fini(&visible); return true; }- if (here && !pixman_region32_not_empty(&visible) &&- pixman_region32_contains_rectangle(&collector->reads, &(pixman_box32_t){intersection.x, intersection.y,- intersection.x + intersection.width, intersection.y + intersection.height}) == PIXMAN_REGION_OUT) {- pixman_region32_fini(&visible);- return true;- }- struct entry *e = wl_array_add(&r->entries, sizeof(*e));- if (!e) { pixman_region32_fini(&visible); return false; }- *e = (struct entry){.item = *item, .transport = transport,- .box = box, .visible = visible};- pixman_region32_init(&e->opaque);- if (item->kind == REDIWM_ITEM_EFFECT) {- // The blur reads three sigma around the surface, below the effect in- // paint order, including what opaque surfaces above hide on screen: items- // there stay in the list for it (`reads`), not for the screen.- e->glass = glass;- e->sigma = glass.sigma * item->scale;- const int pad = (int)ceil(3 * e->sigma);- e->padded = (struct wlr_box){box.x - pad, box.y - pad, box.width + 2 * pad, box.height + 2 * pad};- pixman_region32_union_rect(&collector->reads, &collector->reads,- e->padded.x, e->padded.y, e->padded.width, e->padded.height);- e->front = &glass.target->node;- return true;- }- bool full = item->kind == REDIWM_ITEM_RECT && item->content.color[3] == 1;- if (item->kind != REDIWM_ITEM_RECT && item->kind != REDIWM_ITEM_SHADOW) {- struct rediwm_buffer *b = rediwm_buffer_from_node(transport);- // Match the surface adapter's opacity policy. A cached raster whose- // buffer was released must not suddenly hide previously visible peers.- if (b->buffer && item->content.opacity == 1) {- full = b->buffer_is_opaque;- if (!full) {- rediwm_mirror_opaque(&e->opaque, &b->opaque_region, item->width, item->height, s.width, s.height);- pixman_region32_translate(&e->opaque, s.x, s.y);- }- }- }- if (full) pixman_region32_union_rect(&e->opaque, &e->opaque, s.x, s.y, s.width, s.height);- if (item->clip) {- // The corners are transparent and the ring is drawn by the shape: only- // the interior may hide what is below or skip blending.- if (!collector->has_cover || !same_clip(&collector->cover_of, item->clip)) {- if (collector->has_cover) pixman_region32_fini(&collector->cover);- clip_cover(&collector->cover, item->clip, collector->view->scale);- collector->cover_of = *item->clip;- collector->has_cover = true;- }- pixman_region32_intersect(&e->opaque, &e->opaque, &collector->cover);- }- pixman_region32_union(&collector->covered, &collector->covered, &e->opaque);- if (!here) {- // Only what it hides, for the feedback of what is below.- pixman_region32_fini(&e->visible);- pixman_region32_fini(&e->opaque);- r->entries.size -= sizeof(*e);- }- return true;-}-static void clear_entries(struct rediwm_output_renderer *r) {- struct entry *e;- wl_array_for_each(e, &r->entries) {- pixman_region32_fini(&e->visible);- pixman_region32_fini(&e->opaque);- }- r->entries.size = 0;-}-// The node's own texture cache. Do not keep an extra buffer lock: shm clients must-// still update in place.-static struct wlr_texture *buffer_texture(struct rediwm_buffer *b, struct wlr_renderer *renderer) {- if (!b->buffer || b->texture) return b->texture;- struct wlr_client_buffer *client = wlr_client_buffer_get(b->buffer);- if (client) return client->texture;- struct wlr_texture *texture = wlr_texture_from_buffer(renderer, b->buffer);- if (!texture) return NULL;- if (b->own_buffer) {- b->own_buffer = false;- wlr_buffer_unlock(b->buffer);- }- rediwm_buffer_set_texture(b, texture);- return texture;-}static void sample(struct entry *e, struct rediwm_scene_output *out, bool scanout) {if (e->item.kind == REDIWM_ITEM_RECT) return;struct rediwm_buffer *b = rediwm_buffer_from_node(e->transport);@@ -440,371 +134,6 @@ static bool try_scanout(struct entry *e, struct rediwm_scene_output *out,if (ok) sample(e, out, true);return ok;}-// Most entries lie outside a small repaint, and the region arithmetic below-// (scale, expand, transform) is what costs at fractional scale. Reject from the-// device box first. A clip is a subregion of the entry's box rounded outward by-// wlr_region_scale and then expanded by one pixel, so two pixels of margin-// around the rounded box can never reject an entry that would have drawn.-static bool misses_damage(const struct wlr_box *dst, pixman_region32_t *damage) {- const pixman_box32_t *extents = pixman_region32_extents(damage);- return dst->x - 2 >= extents->x2 || dst->x + dst->width + 2 <= extents->x1 ||- dst->y - 2 >= extents->y2 || dst->y + dst->height + 2 <= extents->y1;-}-// ---- Effects: which blurs this frame re-takes (D1) ----------------------------------uint64_t rediwm_scene_output_reblurs; // read and reset by the performance stats-struct attribution {- const struct output_view *view;- struct entry *entries;- size_t len;- struct wl_array events; // struct seen_event- pixman_region32_t explained; // device pixels events account for-};-struct seen_event { uint64_t leaf; struct wlr_box box; }; // device pixels-static void note_event(const struct rediwm_damage_event *ev, void *data) {- struct attribution *a = data;- struct wlr_box box = device_box((struct wlr_box){ev->box.x, ev->box.y, ev->box.width, ev->box.height}, a->view);- // wlroots widens a scaled buffer's damage for filtering; so does this.- box = (struct wlr_box){box.x - 2, box.y - 2, box.width + 4, box.height + 4};- struct seen_event *se = wl_array_add(&a->events, sizeof(*se));- if (se) *se = (struct seen_event){ev->leaf, box};- pixman_region32_union_rect(&a->explained, &a->explained, box.x, box.y, box.width, box.height);-}-// Paint position of a leaf in this frame (0 = top), or -1 when it is not drawn.-static long position_of(const struct attribution *a, uint64_t leaf) {- if (!leaf) return -1;- for (size_t i = 0; i < a->len; i++) if (a->entries[i].item.id == leaf) return (long)i;- return -1;-}-static bool boxes_meet(const struct wlr_box *a, const struct wlr_box *b) {- struct wlr_box out;- return wlr_box_intersection(&out, a, b);-}-static struct effect_cache *cache_for(struct output_state *os, uint64_t serial) {- struct effect_cache *c;- wl_list_for_each(c, &os->caches, link) if (c->serial == serial) return c;- c = calloc(1, sizeof(*c));- if (!c) return NULL;- c->serial = serial;- wl_list_insert(&os->caches, &c->link);- return c;-}-// Decides, bottom to top, which effects re-blur: a blur is kept unless something-// painted below the effect, or something no event explains, changed in the region-// it reads, or the region or sigma changed (a held effect keeps it while moving).-// A re-blur changes the effect's own pixels (`changed`). Returns false on-// allocation failure.-static bool decide_effects(struct rediwm_output_renderer *r, struct output_state *os,- struct rediwm_scene_output *out, const struct output_view *v, pixman_region32_t *changed) {- struct entry *entries = r->entries.data;- const size_t len = r->entries.size / sizeof(*entries);- struct attribution a = {.view = v, .entries = entries, .len = len};- wl_array_init(&a.events);- pixman_region32_init(&a.explained);- bool complete = os->started && rediwm_projection_damage_events(r->projection, &os->cursor, note_event, &a);- if (!os->started) {- os->cursor = rediwm_projection_damage_cursor(r->projection);- os->started = true;- }- // A software cursor is drawn over everything, so its damage (where it was and- // where it is) never changes what a blur reads.- struct wlr_output_cursor *cursor;- int ncursors = 0;- struct wlr_box cursors[4];- wl_list_for_each(cursor, &out->output->cursors, link) {- if (ncursors == 4 || !cursor->enabled || !cursor->visible || cursor == out->output->hardware_cursor) continue;- cursors[ncursors++] = (struct wlr_box){(int)floor(cursor->x) - cursor->hotspot_x - 4,- (int)floor(cursor->y) - cursor->hotspot_y - 4, (int)cursor->width + 8, (int)cursor->height + 8};- }- for (int i = 0; i < os->ncursors; i++)- pixman_region32_union_rect(&a.explained, &a.explained, os->cursors[i].x, os->cursors[i].y,- os->cursors[i].width, os->cursors[i].height);- for (int i = 0; i < ncursors; i++) {- pixman_region32_union_rect(&a.explained, &a.explained, cursors[i].x, cursors[i].y,- cursors[i].width, cursors[i].height);- os->cursors[i] = cursors[i];- }- os->ncursors = ncursors;- // The veil is drawn over every effect, so repainting the output for it changes- // nothing a blur reads. While it changes, damage no event explains goes- // unnoticed too; a resize or failed frame still re-blurs on its own.- if (os->veil_changed)- pixman_region32_union_rect(&a.explained, &a.explained, 0, 0, v->width, v->height);- pixman_region32_t unexplained;- pixman_region32_init(&unexplained);- pixman_region32_subtract(&unexplained, &out->pending_commit_damage, &a.explained);- struct effect_cache *c;- wl_list_for_each(c, &os->caches, link) c->seen = false;- bool ok = true;- for (size_t i = len; i > 0; i--) {- struct entry *e = &entries[i - 1];- if (e->item.kind != REDIWM_ITEM_EFFECT) continue;- e->cache = cache_for(os, e->glass.serial);- if (!e->cache) { ok = false; continue; }- c = e->cache;- c->seen = true;- const struct wlr_box region = device_box(e->padded, v);- const float sigma = e->sigma * v->scale;- const bool same_size = c->valid && c->sigma == sigma && c->region.width == region.width &&- c->region.height == region.height;- bool reblur = !same_size || !complete || (c->carried && !e->glass.held);- if (!reblur && (c->region.x != region.x || c->region.y != region.y)) {- // A held effect carries its last blur along; otherwise a move re-blurs.- if (e->glass.held) {- c->region = region;- c->carried = true;- } else {- reblur = true;- }- }- if (!reblur && !e->glass.held) {- const long here = (long)(i - 1);- struct seen_event *se;- wl_array_for_each(se, &a.events) {- if (!boxes_meet(&se->box, ®ion)) continue;- const long at = position_of(&a, se->leaf);- if (at < 0 || at > here) { reblur = true; break; }- }- if (!reblur && pixman_region32_contains_rectangle(&unexplained,- &(pixman_box32_t){region.x, region.y, region.x + region.width, region.y + region.height}) != PIXMAN_REGION_OUT)- reblur = true;- // An effect below that re-blurs changes what this one reads.- for (size_t j = len; !reblur && j > i; j--) {- const struct entry *below = &entries[j - 1];- if (below->item.kind == REDIWM_ITEM_EFFECT && below->reblur) {- const struct wlr_box bb = device_box(below->box, v);- if (boxes_meet(&bb, ®ion)) reblur = true;- }- }- }- e->reblur = reblur;- if (reblur) {- const struct wlr_box own = device_box(e->box, v);- pixman_region32_union_rect(changed, changed, own.x, own.y, own.width, own.height);- }- }- pixman_region32_fini(&unexplained);- pixman_region32_fini(&a.explained);- wl_array_release(&a.events);- return ok;-}-static void render_effect(struct entry *e, struct wlr_render_pass *pass, const struct output_view *v,- struct rediwm_output_renderer *r, struct rediwm_scene_output *out, const pixman_region32_t *clip,- bool backdrop) {- struct effect_cache *c = e->cache;- if (!c) return;- struct wlr_renderer *renderer = out->output->renderer;- if (e->reblur && !backdrop && !pixman_region32_not_empty((pixman_region32_t *)clip)) {- // Hidden here (kept only for another effect's blur): re-take it when shown.- c->valid = false;- return;- }- if (e->reblur && !backdrop) {- // Draw what lies below the effect, bottom up, into a quarter-size image of- // the region the blur reads, and blur that (stage 5f): no full-size copy.- c->region = device_box(e->padded, v);- c->sigma = e->sigma * v->scale;- // A quarter of the logical size, as glass.c blurred: the same pixels at any- // scale (a quarter of the device size cost 2.25 times more at 1.5).- rediwm_scene_pass_begin_backdrop(pass, c->region, 4 * (float)v->scale);- struct entry *entries = r->entries.data;- const size_t len = r->entries.size / sizeof(*entries);- const size_t here = (size_t)(e - entries);- // Cull within the blur's region as the screen is culled: walking down from- // the effect, an item that opaque items below the effect hide entirely is- // not drawn (a fullscreen client under the taskbar hides everything else).- pixman_region32_t region, hidden;- pixman_region32_init_rect(®ion, c->region.x, c->region.y, c->region.width, c->region.height);- pixman_region32_init(&hidden);- bool draw[len];- for (size_t j = here + 1; j < len; j++) {- struct entry *below = &entries[j];- const struct wlr_box b = device_box(below->box, v);- draw[j] = pixman_region32_contains_rectangle(®ion, &(pixman_box32_t){b.x, b.y, b.x + b.width,- b.y + b.height}) != PIXMAN_REGION_OUT &&- pixman_region32_contains_rectangle(&hidden, &(pixman_box32_t){b.x, b.y, b.x + b.width,- b.y + b.height}) != PIXMAN_REGION_IN;- if (draw[j] && pixman_region32_not_empty(&below->opaque)) {- pixman_region32_t opaque;- pixman_region32_init(&opaque);- pixman_region32_copy(&opaque, &below->opaque);- device_region(&opaque, v, false);- pixman_region32_union(&hidden, &hidden, &opaque);- pixman_region32_fini(&opaque);- }- }- for (size_t j = len; j > here + 1; j--)- if (draw[j - 1]) render_entry(r, &entries[j - 1], out, pass, v, ®ion, true);- pixman_region32_fini(&hidden);- pixman_region32_fini(®ion);- rediwm_scene_pass_end_backdrop(pass, &c->backdrop, c->sigma);- c->valid = true;- c->carried = false;- rediwm_scene_output_reblurs++;- }- if (!c->valid || !c->backdrop || !pixman_region32_not_empty((pixman_region32_t *)clip)) return;- const struct wlr_box dst = device_box(e->box, v);- struct rediwm_pass_shape shape = {.box = dst};- if (e->item.clip) {- shape_for(&shape, e->item.clip, v);- } else {- const float radius = e->glass.radius * e->item.scale * v->scale;- for (int i = 0; i < 4; i++) shape.radii[i] = radius;- }- shape.border = 0;- struct rediwm_pass_backdrop o = {.backdrop = c->backdrop, .saturation = e->glass.saturation,- .opacity = e->glass.opacity * e->item.content.opacity, .clip = clip};- // The surface over the effect: its alpha is made up to the coverage.- struct entry *entries = r->entries.data;- const size_t len = r->entries.size / sizeof(*entries);- for (size_t i = 0; i < len; i++) {- struct entry *f = &entries[i];- if (f->transport != e->front || f->item.kind == REDIWM_ITEM_RECT) continue;- struct rediwm_buffer *b = rediwm_buffer_from_node(f->transport);- if (b->is_single_pixel_buffer || b->transform != WL_OUTPUT_TRANSFORM_NORMAL) break;- o.front = buffer_texture(b, renderer);- o.front_box = device_box(f->box, v);- o.front_opacity = f->item.content.opacity;- break;- }- // The effect shows what is behind its window: it is dimmed with the window.- const struct rediwm_content *content = &e->item.content;- const bool graded = content->dim_brightness > 0 || content->dim_saturation > 0;- if (graded) rediwm_scene_pass_set_dim(pass, content->dim_brightness, content->dim_saturation);- rediwm_scene_pass_add_backdrop(pass, &shape, dst, &o);- if (graded) rediwm_scene_pass_set_dim(pass, 0, 0);-}-// `backdrop`: drawing into an effect's quarter-size image (stage 5f), where an-// item counts with its whole box, not what is visible of it on screen, and-// `damage` is the region the blur reads.-static bool render_entry(struct rediwm_output_renderer *r, struct entry *e, struct rediwm_scene_output *out,- struct wlr_render_pass *pass, const struct output_view *v, pixman_region32_t *damage, bool backdrop) {- struct wlr_box dst = device_box(e->box, v);- if (misses_damage(&dst, damage)) return true;- pixman_region32_t clip, blend, core;- pixman_region32_init(&clip);- pixman_region32_init(&blend);- pixman_region32_init(&core);- bool graded = false;- if (backdrop) pixman_region32_init_rect(&clip, dst.x, dst.y, dst.width, dst.height);- else {- pixman_region32_copy(&clip, &e->visible);- device_region(&clip, v, true);- }- pixman_region32_intersect(&clip, &clip, damage);- bool ok = true;- if (e->item.kind == REDIWM_ITEM_EFFECT) {- // A re-blur takes its blur even where the effect itself is hidden.- render_effect(e, pass, v, r, out, &clip, backdrop);- goto done;- }- if (!pixman_region32_not_empty(&clip)) goto done;- // A clipped item is drawn in two parts: `core` (its interior, which the model- // proves fully covered and opaque) as before, without the shape, and the rest- // (corners, ring, anything translucent) through the shape.- struct rediwm_pass_shape shape;- const bool shaped = e->item.clip != NULL;- if (shaped) {- shape_for(&shape, e->item.clip, v);- pixman_region32_copy(&core, &e->opaque);- device_region(&core, v, false);- // One rectangle, deeper than the corner arcs and the ring. The model's cover- // is a cross of up to three, which leaves about twelve scissored draws per- // window for what a rim of a few shaded pixels costs far less than: draws- // dominated the output build in a drag.- float depth = fmaxf(fmaxf(shape.radii[0], shape.radii[1]), fmaxf(shape.radii[2], shape.radii[3]));- const int inset = (int)ceilf(fmaxf(depth, shape.border + shape.glow_depth)) + REDIWM_CLIP_OPAQUE_MARGIN;- if (shape.box.width > 2 * inset && shape.box.height > 2 * inset)- pixman_region32_intersect_rect(&core, &core, shape.box.x + inset, shape.box.y + inset,- shape.box.width - 2 * inset, shape.box.height - 2 * inset);- else- pixman_region32_clear(&core);- pixman_region32_intersect(&core, &core, &clip);- pixman_region32_subtract(&clip, &clip, &core);- }- const struct rediwm_content *c = &e->item.content;- // A window that is not the focus is drawn dimmed, whole: the pass grades each- // draw (the interior, the rim and the ring alike) until it is reset below.- graded = c->dim_brightness > 0 || c->dim_saturation > 0;- if (graded) rediwm_scene_pass_set_dim(pass, c->dim_brightness, c->dim_saturation);- if (e->item.kind == REDIWM_ITEM_SHADOW) {- // The offset is taken to device pixels as the difference of two snapped- // boxes, so an output transform turns it with them.- const struct rediwm_shadow *sh = e->item.shadow;- struct rediwm_clip moved = sh->shape;- moved.edges.x1 += sh->offset_x;- moved.edges.x2 += sh->offset_x;- moved.edges.y1 += sh->offset_y;- moved.edges.y2 += sh->offset_y;- struct rediwm_pass_shape at;- shape_for(&shape, &sh->shape, v);- shape_for(&at, &moved, v);- struct rediwm_pass_shadow shadow = {- .offset_x = (float)(at.box.x - shape.box.x), .offset_y = (float)(at.box.y - shape.box.y),- .softness = (float)(sh->softness * v->scale), .hide_inside = true, .clip = &clip};- for (int i = 0; i < 4; i++) shadow.color[i] = sh->color[i] * c->opacity;- rediwm_scene_pass_add_shadow(pass, &shape, &shadow);- } else if (e->item.kind == REDIWM_ITEM_RECT) {- struct wlr_render_rect_options rect = {.box = dst,- .color = {c->color[0], c->color[1], c->color[2], c->color[3]}};- if (pixman_region32_not_empty(&core)) {- rect.clip = &core;- wlr_render_pass_add_rect(pass, &rect);- }- rect.clip = &clip;- if (!shaped) wlr_render_pass_add_rect(pass, &rect);- else if (pixman_region32_not_empty(&clip)) rediwm_scene_pass_add_rect_shaped(pass, &rect, &shape);- } else {- struct rediwm_buffer *b = rediwm_buffer_from_node(e->transport);- if (b->is_single_pixel_buffer) {- const uint32_t *color = b->single_pixel_buffer_color;- // Match wlroots' single-pixel path during parity, including its- // treatment of opacity (only alpha is multiplied there).- struct wlr_render_rect_options rect = {.box = dst,- .color = {(float)color[0] / (float)UINT32_MAX, (float)color[1] / (float)UINT32_MAX,- (float)color[2] / (float)UINT32_MAX, (float)color[3] / (float)UINT32_MAX * c->opacity}};- if (pixman_region32_not_empty(&core)) {- rect.clip = &core;- wlr_render_pass_add_rect(pass, &rect);- }- rect.clip = &clip;- if (!shaped) wlr_render_pass_add_rect(pass, &rect);- else if (pixman_region32_not_empty(&clip)) rediwm_scene_pass_add_rect_shaped(pass, &rect, &shape);- } else {- struct wlr_texture *texture = buffer_texture(b, out->output->renderer);- if (!texture) { ok = false; goto done; }- struct wlr_render_texture_options t = {- .texture = texture, .dst_box = dst, .src_box = c->src_box,- .transform = wlr_output_transform_compose(wlr_output_transform_invert(c->transform), v->transform),- .alpha = &c->opacity, .filter_mode = c->filter_mode,- .wait_timeline = b->wait_timeline, .wait_point = b->wait_point,- };- if (pixman_region32_not_empty(&core)) {- t.clip = &core;- t.blend_mode = WLR_RENDER_BLEND_MODE_NONE;- wlr_render_pass_add_texture(pass, &t);- }- t.clip = &clip;- if (shaped) {- t.blend_mode = WLR_RENDER_BLEND_MODE_PREMULTIPLIED;- if (pixman_region32_not_empty(&clip)) rediwm_scene_pass_add_texture_shaped(pass, &t, &shape);- } else {- pixman_region32_copy(&blend, &e->opaque);- device_region(&blend, v, false);- pixman_region32_subtract(&blend, &clip, &blend);- t.blend_mode = pixman_region32_not_empty(&blend) ? WLR_RENDER_BLEND_MODE_PREMULTIPLIED : WLR_RENDER_BLEND_MODE_NONE;- wlr_render_pass_add_texture(pass, &t);- }- }- e->sampled = true;- }-done:- // What is drawn next (the next item, the software cursor) is not this item's.- if (graded) rediwm_scene_pass_set_dim(pass, 0, 0);- pixman_region32_fini(&clip);- pixman_region32_fini(&blend);- pixman_region32_fini(&core);- return ok;-}#ifdef REDIWM_DAMAGE_AUDIT// Debug audit of the damage (`zig build -Ddamage-audit=true`, TESTING.md): after a// frame is drawn into the swapchain buffer with only the damage it was given, the@@ -849,7 +178,7 @@ static void audit_damage(struct rediwm_output_renderer *r, struct rediwm_scene_o.box = {0, 0, real->width, real->height}, .color = {0, 0, 0, 1}, .clip = &whole});// Blurs are kept as the real pass left them: this audits damage, not glass.for (size_t i = 0; i < len; i++) entries[i].reblur = false;- for (size_t i = len; i > 0; i--) render_entry(r, &entries[i - 1], out, pass, v, &whole, false);+ for (size_t i = len; i > 0; i--) scene_output_render_entry(r, &entries[i - 1], out, pass, v, &whole, false);if (veil > 0) wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){.box = {0, 0, real->width, real->height}, .color = {0, 0, 0, veil}, .clip = &whole});wlr_output_add_software_cursors_to_render_pass(output, pass, &whole);@@ -931,12 +260,12 @@ bool rediwm_scene_output_build_state(struct rediwm_output_renderer *r,}pixman_region32_init(&collector.covered);pixman_region32_init(&collector.reads);- bool collected = rediwm_projection_for_each_item(projection, true, collect, &collector);+ bool collected = rediwm_projection_for_each_item(projection, true, scene_output_collect, &collector);if (collector.has_cover) pixman_region32_fini(&collector.cover);pixman_region32_fini(&collector.reads);if (!collected) {pixman_region32_fini(&collector.covered);- clear_entries(r);+ scene_output_clear_entries(r);return false;}rediwm_projection_feedback_end(projection);@@ -950,10 +279,10 @@ bool rediwm_scene_output_build_state(struct rediwm_output_renderer *r,struct output_state *os = r->has_backdrops ? output_state_get(r, output) : NULL;struct output_state *veiled = output_state_find(r, output);const float veil = veiled ? veiled->veil : 0;- if (os && !decide_effects(r, os, out, &v, &changed)) {+ if (os && !scene_output_decide_effects(r, os, out, &v, &changed)) {pixman_region32_fini(&changed);pixman_region32_fini(&collector.covered);- clear_entries(r);+ scene_output_clear_entries(r);return false;}if (pixman_region32_not_empty(&changed)) {@@ -994,7 +323,7 @@ bool rediwm_scene_output_build_state(struct rediwm_output_renderer *r,.box = {0, 0, buffer->width, buffer->height}, .color = {0, 0, 0, 1}, .clip = &background});ok = true;for (size_t i = len; i > 0; i--)- if (!render_entry(r, &entries[i - 1], out, pass, &v, &damage, false)) ok = false;+ if (!scene_output_render_entry(r, &entries[i - 1], out, pass, &v, &damage, false)) ok = false;if (veil > 0) wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){.box = {0, 0, buffer->width, buffer->height}, .color = {0, 0, 0, veil}, .clip = &damage});wlr_output_add_software_cursors_to_render_pass(output, pass, &damage);@@ -1029,6 +358,6 @@ done:if (!c->seen) cache_destroy(r, c);}}- clear_entries(r);+ scene_output_clear_entries(r);return ok;}diff --git a/src/scene_output_collect.c b/src/scene_output_collect.cnew file mode 100644index 0000000..a1dbd87--- /dev/null+++ b/src/scene_output_collect.c@@ -0,0 +1,144 @@+// What a frame draws: the graph's items, top to bottom, culled against what is+// above them, with their visible and opaque regions. Part of scene_output.c.+#include "scene_output_internal.h"++// What of a clipped item may be drawn without the shape and without blending: the+// model's opaque cover, minus a margin around the edge where the ring is drawn.+// Layout space, like the item's own opaque region.+static void clip_cover(pixman_region32_t *out, const struct rediwm_clip *c, double scale) {+ pixman_region32_init(out);+ struct rediwm_snap parts[3];+ const int n = rediwm_clip_opaque(c, parts);+ for (int i = 0; i < n; i++)+ pixman_region32_union_rect(out, out, parts[i].x, parts[i].y, parts[i].width, parts[i].height);+ if (c->border > 0) {+ const struct rediwm_snap f = rediwm_edges_snap(c->edges);+ const int ring = (int)ceil(fmax(c->border, 1.0 / scale)) + 1;+ if (f.width > 2 * ring && f.height > 2 * ring)+ pixman_region32_intersect_rect(out, out, f.x + ring, f.y + ring, f.width - 2 * ring, f.height - 2 * ring);+ else+ pixman_region32_clear(out);+ }+}+static bool same_clip(const struct rediwm_clip *a, const struct rediwm_clip *b) {+ if (a->edges.x1 != b->edges.x1 || a->edges.y1 != b->edges.y1 || a->edges.x2 != b->edges.x2 ||+ a->edges.y2 != b->edges.y2 || a->border != b->border) return false;+ for (int i = 0; i < 4; i++) if (a->radii[i] != b->radii[i]) return false;+ return true;+}+bool scene_output_collect(const struct rediwm_item *item, struct rediwm_node *transport, void *data) {+ struct collector *collector = data;+ struct rediwm_output_renderer *r = collector->renderer;+ if (!item->enabled) { return true; }+ struct rediwm_snap s = rediwm_edges_snap(item->edges);+ if (s.width <= 0 || s.height <= 0) { return true; }+ struct wlr_box box = {s.x, s.y, s.width, s.height}, intersection;+ const bool here = wlr_box_intersection(&intersection, &box, &collector->view->logical);+ // An item on another output only is not drawn for this one, but it still hides+ // what is below it there, which the surface feedback below counts (stage 6).+ bool elsewhere = false;+ for (int i = 0; i < collector->others && !elsewhere; i++) {+ struct wlr_box other;+ elsewhere = wlr_box_intersection(&other, &box, &collector->other[i]);+ }+ if (!here && !elsewhere) { return true; }+ // Visit top-to-bottom so fully occluded items never enter the render list+ // and never allocate/copy content. The graph, not the view's visible region,+ // supplies the bounds and paint order.+ pixman_region32_t visible;+ pixman_region32_init_rect(&visible, box.x, box.y, box.width, box.height);+ pixman_region32_subtract(&visible, &visible, &collector->covered);+ // What a client surface shows of itself on every output is what it is told:+ // this counts for an item that draws nothing as well (opacity 0), as it does+ // for wlroots.+ if (item->kind == REDIWM_ITEM_SURFACE)+ rediwm_projection_feedback_note(r->projection, transport, &visible);+ if (item->kind != REDIWM_ITEM_EFFECT)+ rediwm_projection_note_visible(r->projection, transport, &visible);+ if (here)+ pixman_region32_intersect_rect(&visible, &visible, intersection.x, intersection.y,+ intersection.width, intersection.height);+ struct rediwm_glass_info glass = {0};+ bool skip = false;+ if (item->kind == REDIWM_ITEM_RECT) {+ skip = item->content.color[3] == 0;+ } else if (item->kind == REDIWM_ITEM_EFFECT) {+ // Glass is drawn here (stage 5c); without the blur program there is none,+ // and the surface's own tint shows, as glass.c's fallback did.+ skip = !here || !r->has_backdrops || item->content.opacity == 0 ||+ !rediwm_glass_effect_info(transport, &glass) || glass.opacity <= 0;+ } else if (item->kind == REDIWM_ITEM_SHADOW) {+ // No buffer: the pixels are the shadow's, and never opaque.+ skip = item->content.opacity == 0 || item->shadow->color[3] == 0;+ } else {+ struct rediwm_buffer *b = rediwm_buffer_from_node(transport);+ skip = item->content.opacity == 0 ||+ (!b->buffer && !b->texture && !b->is_single_pixel_buffer);+ }+ if (skip) { pixman_region32_fini(&visible); return true; }+ if (here && !pixman_region32_not_empty(&visible) &&+ pixman_region32_contains_rectangle(&collector->reads, &(pixman_box32_t){intersection.x, intersection.y,+ intersection.x + intersection.width, intersection.y + intersection.height}) == PIXMAN_REGION_OUT) {+ pixman_region32_fini(&visible);+ return true;+ }+ struct entry *e = wl_array_add(&r->entries, sizeof(*e));+ if (!e) { pixman_region32_fini(&visible); return false; }+ *e = (struct entry){.item = *item, .transport = transport,+ .box = box, .visible = visible};+ pixman_region32_init(&e->opaque);+ if (item->kind == REDIWM_ITEM_EFFECT) {+ // The blur reads three sigma around the surface, below the effect in+ // paint order, including what opaque surfaces above hide on screen: items+ // there stay in the list for it (`reads`), not for the screen.+ e->glass = glass;+ e->sigma = glass.sigma * item->scale;+ const int pad = (int)ceil(3 * e->sigma);+ e->padded = (struct wlr_box){box.x - pad, box.y - pad, box.width + 2 * pad, box.height + 2 * pad};+ pixman_region32_union_rect(&collector->reads, &collector->reads,+ e->padded.x, e->padded.y, e->padded.width, e->padded.height);+ e->front = &glass.target->node;+ return true;+ }+ bool full = item->kind == REDIWM_ITEM_RECT && item->content.color[3] == 1;+ if (item->kind != REDIWM_ITEM_RECT && item->kind != REDIWM_ITEM_SHADOW) {+ struct rediwm_buffer *b = rediwm_buffer_from_node(transport);+ // Match the surface adapter's opacity policy. A cached raster whose+ // buffer was released must not suddenly hide previously visible peers.+ if (b->buffer && item->content.opacity == 1) {+ full = b->buffer_is_opaque;+ if (!full) {+ rediwm_mirror_opaque(&e->opaque, &b->opaque_region, item->width, item->height, s.width, s.height);+ pixman_region32_translate(&e->opaque, s.x, s.y);+ }+ }+ }+ if (full) pixman_region32_union_rect(&e->opaque, &e->opaque, s.x, s.y, s.width, s.height);+ if (item->clip) {+ // The corners are transparent and the ring is drawn by the shape: only+ // the interior may hide what is below or skip blending.+ if (!collector->has_cover || !same_clip(&collector->cover_of, item->clip)) {+ if (collector->has_cover) pixman_region32_fini(&collector->cover);+ clip_cover(&collector->cover, item->clip, collector->view->scale);+ collector->cover_of = *item->clip;+ collector->has_cover = true;+ }+ pixman_region32_intersect(&e->opaque, &e->opaque, &collector->cover);+ }+ pixman_region32_union(&collector->covered, &collector->covered, &e->opaque);+ if (!here) {+ // Only what it hides, for the feedback of what is below.+ pixman_region32_fini(&e->visible);+ pixman_region32_fini(&e->opaque);+ r->entries.size -= sizeof(*e);+ }+ return true;+}+void scene_output_clear_entries(struct rediwm_output_renderer *r) {+ struct entry *e;+ wl_array_for_each(e, &r->entries) {+ pixman_region32_fini(&e->visible);+ pixman_region32_fini(&e->opaque);+ }+ r->entries.size = 0;+}diff --git a/src/scene_output_draw.c b/src/scene_output_draw.cnew file mode 100644index 0000000..33869cf--- /dev/null+++ b/src/scene_output_draw.c@@ -0,0 +1,286 @@+// Drawing entries through the owned pass (scene_pass.c): clipped shapes, rects,+// textures, shadows and glass. Part of scene_output.c.+#include "scene_output_internal.h"++// Which device corner each logical corner (tl, tr, br, bl) lands on, found by+// moving a one-pixel box in each corner of a small frame through the same+// transform device_box applies, so the two cannot disagree.+static void device_radii(float out[4], const float in[4], enum wl_output_transform transform) {+ enum { W = 3, H = 5 };+ const struct wlr_box corners[4] = {{0, 0, 1, 1}, {W - 1, 0, 1, 1}, {W - 1, H - 1, 1, 1}, {0, H - 1, 1, 1}};+ const int w = (transform & 1) ? H : W, h = (transform & 1) ? W : H;+ for (int i = 0; i < 4; i++) {+ struct wlr_box b;+ wlr_box_transform(&b, &corners[i], wlr_output_transform_invert(transform), W, H);+ out[b.x < w / 2 ? (b.y < h / 2 ? 0 : 3) : (b.y < h / 2 ? 1 : 2)] = in[i];+ }+}+// The pass's shape for a clip: the clip's box snapped in layout space like any+// item's, then taken to device pixels by the same function as the pieces that+// tile it, so the shape and the quads agree on every pixel column. A ring is+// whole device pixels, never thinner than one.+static void shape_for(struct rediwm_pass_shape *s, const struct rediwm_clip *c, const struct output_view *v) {+ const struct rediwm_snap f = rediwm_edges_snap(c->edges);+ s->box = device_box((struct wlr_box){f.x, f.y, f.width, f.height}, v);+ float radii[4];+ for (int i = 0; i < 4; i++) radii[i] = (float)(c->radii[i] * v->scale);+ device_radii(s->radii, radii, v->transform);+ s->border = c->border > 0 ? fmaxf(1.f, roundf((float)(c->border * v->scale))) : 0.f;+ for (int i = 0; i < 4; i++) s->border_color[i] = c->border_color[i];+ s->glow_radius = s->glow_depth = 0;+ memset(s->glow_center, 0, sizeof(s->glow_center));+ memset(s->glow_color, 0, sizeof(s->glow_color));+ if (c->glow_radius > 0 && c->glow_color[3] > 0) {+ // The highlight's point goes through the same transform as the box, as a+ // one-pixel box (the loss is under a layout pixel, far inside its radius).+ const struct wlr_box at = device_box((struct wlr_box){(int)floor(c->glow_x), (int)floor(c->glow_y), 1, 1}, v);+ s->glow_center[0] = at.x + at.width * .5f - s->box.x;+ s->glow_center[1] = at.y + at.height * .5f - s->box.y;+ s->glow_radius = (float)(c->glow_radius * v->scale);+ s->glow_depth = (float)(c->glow_depth * v->scale);+ for (int i = 0; i < 4; i++) s->glow_color[i] = c->glow_color[i];+ }+}+// The node's own texture cache. Do not keep an extra buffer lock: shm clients must+// still update in place.+static struct wlr_texture *buffer_texture(struct rediwm_buffer *b, struct wlr_renderer *renderer) {+ if (!b->buffer || b->texture) return b->texture;+ struct wlr_client_buffer *client = wlr_client_buffer_get(b->buffer);+ if (client) return client->texture;+ struct wlr_texture *texture = wlr_texture_from_buffer(renderer, b->buffer);+ if (!texture) return NULL;+ if (b->own_buffer) {+ b->own_buffer = false;+ wlr_buffer_unlock(b->buffer);+ }+ rediwm_buffer_set_texture(b, texture);+ return texture;+}+// Most entries lie outside a small repaint, and the region arithmetic below+// (scale, expand, transform) is what costs at fractional scale. Reject from the+// device box first. A clip is a subregion of the entry's box rounded outward by+// wlr_region_scale and then expanded by one pixel, so two pixels of margin+// around the rounded box can never reject an entry that would have drawn.+static bool misses_damage(const struct wlr_box *dst, pixman_region32_t *damage) {+ const pixman_box32_t *extents = pixman_region32_extents(damage);+ return dst->x - 2 >= extents->x2 || dst->x + dst->width + 2 <= extents->x1 ||+ dst->y - 2 >= extents->y2 || dst->y + dst->height + 2 <= extents->y1;+}+static void render_effect(struct entry *e, struct wlr_render_pass *pass, const struct output_view *v,+ struct rediwm_output_renderer *r, struct rediwm_scene_output *out, const pixman_region32_t *clip,+ bool backdrop) {+ struct effect_cache *c = e->cache;+ if (!c) return;+ struct wlr_renderer *renderer = out->output->renderer;+ if (e->reblur && !backdrop && !pixman_region32_not_empty((pixman_region32_t *)clip)) {+ // Hidden here (kept only for another effect's blur): re-take it when shown.+ c->valid = false;+ return;+ }+ if (e->reblur && !backdrop) {+ // Draw what lies below the effect, bottom up, into a quarter-size image of+ // the region the blur reads, and blur that (stage 5f): no full-size copy.+ c->region = device_box(e->padded, v);+ c->sigma = e->sigma * v->scale;+ // A quarter of the logical size, as glass.c blurred: the same pixels at any+ // scale (a quarter of the device size cost 2.25 times more at 1.5).+ rediwm_scene_pass_begin_backdrop(pass, c->region, 4 * (float)v->scale);+ struct entry *entries = r->entries.data;+ const size_t len = r->entries.size / sizeof(*entries);+ const size_t here = (size_t)(e - entries);+ // Cull within the blur's region as the screen is culled: walking down from+ // the effect, an item that opaque items below the effect hide entirely is+ // not drawn (a fullscreen client under the taskbar hides everything else).+ pixman_region32_t region, hidden;+ pixman_region32_init_rect(®ion, c->region.x, c->region.y, c->region.width, c->region.height);+ pixman_region32_init(&hidden);+ bool draw[len];+ for (size_t j = here + 1; j < len; j++) {+ struct entry *below = &entries[j];+ const struct wlr_box b = device_box(below->box, v);+ draw[j] = pixman_region32_contains_rectangle(®ion, &(pixman_box32_t){b.x, b.y, b.x + b.width,+ b.y + b.height}) != PIXMAN_REGION_OUT &&+ pixman_region32_contains_rectangle(&hidden, &(pixman_box32_t){b.x, b.y, b.x + b.width,+ b.y + b.height}) != PIXMAN_REGION_IN;+ if (draw[j] && pixman_region32_not_empty(&below->opaque)) {+ pixman_region32_t opaque;+ pixman_region32_init(&opaque);+ pixman_region32_copy(&opaque, &below->opaque);+ device_region(&opaque, v, false);+ pixman_region32_union(&hidden, &hidden, &opaque);+ pixman_region32_fini(&opaque);+ }+ }+ for (size_t j = len; j > here + 1; j--)+ if (draw[j - 1]) scene_output_render_entry(r, &entries[j - 1], out, pass, v, ®ion, true);+ pixman_region32_fini(&hidden);+ pixman_region32_fini(®ion);+ rediwm_scene_pass_end_backdrop(pass, &c->backdrop, c->sigma);+ c->valid = true;+ c->carried = false;+ rediwm_scene_output_reblurs++;+ }+ if (!c->valid || !c->backdrop || !pixman_region32_not_empty((pixman_region32_t *)clip)) return;+ const struct wlr_box dst = device_box(e->box, v);+ struct rediwm_pass_shape shape = {.box = dst};+ if (e->item.clip) {+ shape_for(&shape, e->item.clip, v);+ } else {+ const float radius = e->glass.radius * e->item.scale * v->scale;+ for (int i = 0; i < 4; i++) shape.radii[i] = radius;+ }+ shape.border = 0;+ struct rediwm_pass_backdrop o = {.backdrop = c->backdrop, .saturation = e->glass.saturation,+ .opacity = e->glass.opacity * e->item.content.opacity, .clip = clip};+ // The surface over the effect: its alpha is made up to the coverage.+ struct entry *entries = r->entries.data;+ const size_t len = r->entries.size / sizeof(*entries);+ for (size_t i = 0; i < len; i++) {+ struct entry *f = &entries[i];+ if (f->transport != e->front || f->item.kind == REDIWM_ITEM_RECT) continue;+ struct rediwm_buffer *b = rediwm_buffer_from_node(f->transport);+ if (b->is_single_pixel_buffer || b->transform != WL_OUTPUT_TRANSFORM_NORMAL) break;+ o.front = buffer_texture(b, renderer);+ o.front_box = device_box(f->box, v);+ o.front_opacity = f->item.content.opacity;+ break;+ }+ // The effect shows what is behind its window: it is dimmed with the window.+ const struct rediwm_content *content = &e->item.content;+ const bool graded = content->dim_brightness > 0 || content->dim_saturation > 0;+ if (graded) rediwm_scene_pass_set_dim(pass, content->dim_brightness, content->dim_saturation);+ rediwm_scene_pass_add_backdrop(pass, &shape, dst, &o);+ if (graded) rediwm_scene_pass_set_dim(pass, 0, 0);+}+// `backdrop`: drawing into an effect's quarter-size image (stage 5f), where an+// item counts with its whole box, not what is visible of it on screen, and+// `damage` is the region the blur reads.+bool scene_output_render_entry(struct rediwm_output_renderer *r, struct entry *e, struct rediwm_scene_output *out,+ struct wlr_render_pass *pass, const struct output_view *v, pixman_region32_t *damage, bool backdrop) {+ struct wlr_box dst = device_box(e->box, v);+ if (misses_damage(&dst, damage)) return true;+ pixman_region32_t clip, blend, core;+ pixman_region32_init(&clip);+ pixman_region32_init(&blend);+ pixman_region32_init(&core);+ bool graded = false;+ if (backdrop) pixman_region32_init_rect(&clip, dst.x, dst.y, dst.width, dst.height);+ else {+ pixman_region32_copy(&clip, &e->visible);+ device_region(&clip, v, true);+ }+ pixman_region32_intersect(&clip, &clip, damage);+ bool ok = true;+ if (e->item.kind == REDIWM_ITEM_EFFECT) {+ // A re-blur takes its blur even where the effect itself is hidden.+ render_effect(e, pass, v, r, out, &clip, backdrop);+ goto done;+ }+ if (!pixman_region32_not_empty(&clip)) goto done;+ // A clipped item is drawn in two parts: `core` (its interior, which the model+ // proves fully covered and opaque) as before, without the shape, and the rest+ // (corners, ring, anything translucent) through the shape.+ struct rediwm_pass_shape shape;+ const bool shaped = e->item.clip != NULL;+ if (shaped) {+ shape_for(&shape, e->item.clip, v);+ pixman_region32_copy(&core, &e->opaque);+ device_region(&core, v, false);+ // One rectangle, deeper than the corner arcs and the ring. The model's cover+ // is a cross of up to three, which leaves about twelve scissored draws per+ // window for what a rim of a few shaded pixels costs far less than: draws+ // dominated the output build in a drag.+ float depth = fmaxf(fmaxf(shape.radii[0], shape.radii[1]), fmaxf(shape.radii[2], shape.radii[3]));+ const int inset = (int)ceilf(fmaxf(depth, shape.border + shape.glow_depth)) + REDIWM_CLIP_OPAQUE_MARGIN;+ if (shape.box.width > 2 * inset && shape.box.height > 2 * inset)+ pixman_region32_intersect_rect(&core, &core, shape.box.x + inset, shape.box.y + inset,+ shape.box.width - 2 * inset, shape.box.height - 2 * inset);+ else+ pixman_region32_clear(&core);+ pixman_region32_intersect(&core, &core, &clip);+ pixman_region32_subtract(&clip, &clip, &core);+ }+ const struct rediwm_content *c = &e->item.content;+ // A window that is not the focus is drawn dimmed, whole: the pass grades each+ // draw (the interior, the rim and the ring alike) until it is reset below.+ graded = c->dim_brightness > 0 || c->dim_saturation > 0;+ if (graded) rediwm_scene_pass_set_dim(pass, c->dim_brightness, c->dim_saturation);+ if (e->item.kind == REDIWM_ITEM_SHADOW) {+ // The offset is taken to device pixels as the difference of two snapped+ // boxes, so an output transform turns it with them.+ const struct rediwm_shadow *sh = e->item.shadow;+ struct rediwm_clip moved = sh->shape;+ moved.edges.x1 += sh->offset_x;+ moved.edges.x2 += sh->offset_x;+ moved.edges.y1 += sh->offset_y;+ moved.edges.y2 += sh->offset_y;+ struct rediwm_pass_shape at;+ shape_for(&shape, &sh->shape, v);+ shape_for(&at, &moved, v);+ struct rediwm_pass_shadow shadow = {+ .offset_x = (float)(at.box.x - shape.box.x), .offset_y = (float)(at.box.y - shape.box.y),+ .softness = (float)(sh->softness * v->scale), .hide_inside = true, .clip = &clip};+ for (int i = 0; i < 4; i++) shadow.color[i] = sh->color[i] * c->opacity;+ rediwm_scene_pass_add_shadow(pass, &shape, &shadow);+ } else if (e->item.kind == REDIWM_ITEM_RECT) {+ struct wlr_render_rect_options rect = {.box = dst,+ .color = {c->color[0], c->color[1], c->color[2], c->color[3]}};+ if (pixman_region32_not_empty(&core)) {+ rect.clip = &core;+ wlr_render_pass_add_rect(pass, &rect);+ }+ rect.clip = &clip;+ if (!shaped) wlr_render_pass_add_rect(pass, &rect);+ else if (pixman_region32_not_empty(&clip)) rediwm_scene_pass_add_rect_shaped(pass, &rect, &shape);+ } else {+ struct rediwm_buffer *b = rediwm_buffer_from_node(e->transport);+ if (b->is_single_pixel_buffer) {+ const uint32_t *color = b->single_pixel_buffer_color;+ // Match wlroots' single-pixel path during parity, including its+ // treatment of opacity (only alpha is multiplied there).+ struct wlr_render_rect_options rect = {.box = dst,+ .color = {(float)color[0] / (float)UINT32_MAX, (float)color[1] / (float)UINT32_MAX,+ (float)color[2] / (float)UINT32_MAX, (float)color[3] / (float)UINT32_MAX * c->opacity}};+ if (pixman_region32_not_empty(&core)) {+ rect.clip = &core;+ wlr_render_pass_add_rect(pass, &rect);+ }+ rect.clip = &clip;+ if (!shaped) wlr_render_pass_add_rect(pass, &rect);+ else if (pixman_region32_not_empty(&clip)) rediwm_scene_pass_add_rect_shaped(pass, &rect, &shape);+ } else {+ struct wlr_texture *texture = buffer_texture(b, out->output->renderer);+ if (!texture) { ok = false; goto done; }+ struct wlr_render_texture_options t = {+ .texture = texture, .dst_box = dst, .src_box = c->src_box,+ .transform = wlr_output_transform_compose(wlr_output_transform_invert(c->transform), v->transform),+ .alpha = &c->opacity, .filter_mode = c->filter_mode,+ .wait_timeline = b->wait_timeline, .wait_point = b->wait_point,+ };+ if (pixman_region32_not_empty(&core)) {+ t.clip = &core;+ t.blend_mode = WLR_RENDER_BLEND_MODE_NONE;+ wlr_render_pass_add_texture(pass, &t);+ }+ t.clip = &clip;+ if (shaped) {+ t.blend_mode = WLR_RENDER_BLEND_MODE_PREMULTIPLIED;+ if (pixman_region32_not_empty(&clip)) rediwm_scene_pass_add_texture_shaped(pass, &t, &shape);+ } else {+ pixman_region32_copy(&blend, &e->opaque);+ device_region(&blend, v, false);+ pixman_region32_subtract(&blend, &clip, &blend);+ t.blend_mode = pixman_region32_not_empty(&blend) ? WLR_RENDER_BLEND_MODE_PREMULTIPLIED : WLR_RENDER_BLEND_MODE_NONE;+ wlr_render_pass_add_texture(pass, &t);+ }+ }+ e->sampled = true;+ }+done:+ // What is drawn next (the next item, the software cursor) is not this item's.+ if (graded) rediwm_scene_pass_set_dim(pass, 0, 0);+ pixman_region32_fini(&clip);+ pixman_region32_fini(&blend);+ pixman_region32_fini(&core);+ return ok;+}diff --git a/src/scene_output_effects.c b/src/scene_output_effects.cnew file mode 100644index 0000000..cef0c4f--- /dev/null+++ b/src/scene_output_effects.c@@ -0,0 +1,140 @@+// ---- Effects: which blurs this frame re-takes (D1) ---------------------------------+// Part of scene_output.c.+#include "scene_output_internal.h"++uint64_t rediwm_scene_output_reblurs; // read and reset by the performance stats+struct attribution {+ const struct output_view *view;+ struct entry *entries;+ size_t len;+ struct wl_array events; // struct seen_event+ pixman_region32_t explained; // device pixels events account for+};+struct seen_event { uint64_t leaf; struct wlr_box box; }; // device pixels+static void note_event(const struct rediwm_damage_event *ev, void *data) {+ struct attribution *a = data;+ struct wlr_box box = device_box((struct wlr_box){ev->box.x, ev->box.y, ev->box.width, ev->box.height}, a->view);+ // wlroots widens a scaled buffer's damage for filtering; so does this.+ box = (struct wlr_box){box.x - 2, box.y - 2, box.width + 4, box.height + 4};+ struct seen_event *se = wl_array_add(&a->events, sizeof(*se));+ if (se) *se = (struct seen_event){ev->leaf, box};+ pixman_region32_union_rect(&a->explained, &a->explained, box.x, box.y, box.width, box.height);+}+// Paint position of a leaf in this frame (0 = top), or -1 when it is not drawn.+static long position_of(const struct attribution *a, uint64_t leaf) {+ if (!leaf) return -1;+ for (size_t i = 0; i < a->len; i++) if (a->entries[i].item.id == leaf) return (long)i;+ return -1;+}+static bool boxes_meet(const struct wlr_box *a, const struct wlr_box *b) {+ struct wlr_box out;+ return wlr_box_intersection(&out, a, b);+}+static struct effect_cache *cache_for(struct output_state *os, uint64_t serial) {+ struct effect_cache *c;+ wl_list_for_each(c, &os->caches, link) if (c->serial == serial) return c;+ c = calloc(1, sizeof(*c));+ if (!c) return NULL;+ c->serial = serial;+ wl_list_insert(&os->caches, &c->link);+ return c;+}+// Decides, bottom to top, which effects re-blur: a blur is kept unless something+// painted below the effect, or something no event explains, changed in the region+// it reads, or the region or sigma changed (a held effect keeps it while moving).+// A re-blur changes the effect's own pixels (`changed`). Returns false on+// allocation failure.+bool scene_output_decide_effects(struct rediwm_output_renderer *r, struct output_state *os,+ struct rediwm_scene_output *out, const struct output_view *v, pixman_region32_t *changed) {+ struct entry *entries = r->entries.data;+ const size_t len = r->entries.size / sizeof(*entries);+ struct attribution a = {.view = v, .entries = entries, .len = len};+ wl_array_init(&a.events);+ pixman_region32_init(&a.explained);+ bool complete = os->started && rediwm_projection_damage_events(r->projection, &os->cursor, note_event, &a);+ if (!os->started) {+ os->cursor = rediwm_projection_damage_cursor(r->projection);+ os->started = true;+ }+ // A software cursor is drawn over everything, so its damage (where it was and+ // where it is) never changes what a blur reads.+ struct wlr_output_cursor *cursor;+ int ncursors = 0;+ struct wlr_box cursors[4];+ wl_list_for_each(cursor, &out->output->cursors, link) {+ if (ncursors == 4 || !cursor->enabled || !cursor->visible || cursor == out->output->hardware_cursor) continue;+ cursors[ncursors++] = (struct wlr_box){(int)floor(cursor->x) - cursor->hotspot_x - 4,+ (int)floor(cursor->y) - cursor->hotspot_y - 4, (int)cursor->width + 8, (int)cursor->height + 8};+ }+ for (int i = 0; i < os->ncursors; i++)+ pixman_region32_union_rect(&a.explained, &a.explained, os->cursors[i].x, os->cursors[i].y,+ os->cursors[i].width, os->cursors[i].height);+ for (int i = 0; i < ncursors; i++) {+ pixman_region32_union_rect(&a.explained, &a.explained, cursors[i].x, cursors[i].y,+ cursors[i].width, cursors[i].height);+ os->cursors[i] = cursors[i];+ }+ os->ncursors = ncursors;+ // The veil is drawn over every effect, so repainting the output for it changes+ // nothing a blur reads. While it changes, damage no event explains goes+ // unnoticed too; a resize or failed frame still re-blurs on its own.+ if (os->veil_changed)+ pixman_region32_union_rect(&a.explained, &a.explained, 0, 0, v->width, v->height);+ pixman_region32_t unexplained;+ pixman_region32_init(&unexplained);+ pixman_region32_subtract(&unexplained, &out->pending_commit_damage, &a.explained);+ struct effect_cache *c;+ wl_list_for_each(c, &os->caches, link) c->seen = false;+ bool ok = true;+ for (size_t i = len; i > 0; i--) {+ struct entry *e = &entries[i - 1];+ if (e->item.kind != REDIWM_ITEM_EFFECT) continue;+ e->cache = cache_for(os, e->glass.serial);+ if (!e->cache) { ok = false; continue; }+ c = e->cache;+ c->seen = true;+ const struct wlr_box region = device_box(e->padded, v);+ const float sigma = e->sigma * v->scale;+ const bool same_size = c->valid && c->sigma == sigma && c->region.width == region.width &&+ c->region.height == region.height;+ bool reblur = !same_size || !complete || (c->carried && !e->glass.held);+ if (!reblur && (c->region.x != region.x || c->region.y != region.y)) {+ // A held effect carries its last blur along; otherwise a move re-blurs.+ if (e->glass.held) {+ c->region = region;+ c->carried = true;+ } else {+ reblur = true;+ }+ }+ if (!reblur && !e->glass.held) {+ const long here = (long)(i - 1);+ struct seen_event *se;+ wl_array_for_each(se, &a.events) {+ if (!boxes_meet(&se->box, ®ion)) continue;+ const long at = position_of(&a, se->leaf);+ if (at < 0 || at > here) { reblur = true; break; }+ }+ if (!reblur && pixman_region32_contains_rectangle(&unexplained,+ &(pixman_box32_t){region.x, region.y, region.x + region.width, region.y + region.height}) != PIXMAN_REGION_OUT)+ reblur = true;+ // An effect below that re-blurs changes what this one reads.+ for (size_t j = len; !reblur && j > i; j--) {+ const struct entry *below = &entries[j - 1];+ if (below->item.kind == REDIWM_ITEM_EFFECT && below->reblur) {+ const struct wlr_box bb = device_box(below->box, v);+ if (boxes_meet(&bb, ®ion)) reblur = true;+ }+ }+ }+ e->reblur = reblur;+ if (reblur) {+ const struct wlr_box own = device_box(e->box, v);+ pixman_region32_union_rect(changed, changed, own.x, own.y, own.width, own.height);+ }+ }+ pixman_region32_fini(&unexplained);+ pixman_region32_fini(&a.explained);+ wl_array_release(&a.events);+ return ok;+}diff --git a/src/scene_output_internal.h b/src/scene_output_internal.hnew file mode 100644index 0000000..7f57c46--- /dev/null+++ b/src/scene_output_internal.h@@ -0,0 +1,134 @@+// Private to scene_output*.c: what the output pass keeps per output and builds+// per frame, and the device-space helpers every part uses (`static inline`, so a+// file boundary costs them no inlining). Everyone else uses scene_output.h.+#ifndef REDIWM_SCENE_OUTPUT_INTERNAL_H+#define REDIWM_SCENE_OUTPUT_INTERNAL_H++#define WLR_USE_UNSTABLE+#define WLR_PRIVATE private+#include "scene_output.h"+#include "scene_outputs.h"+#include "scene_view.h"+#include "scene_pass.h"+#include "scene_graph.h"+#include "projection.h"+#include "glass.h"+#include "node.h"+#include <math.h>+#include <stdlib.h>+#include <string.h>+#ifdef REDIWM_DAMAGE_AUDIT+#include <drm_fourcc.h>+#include <wlr/render/allocator.h>+#include <wlr/render/wlr_renderer.h>+#endif+#include <wlr/render/drm_syncobj.h>+#include <wlr/render/swapchain.h>+#include <wlr/render/wlr_texture.h>+#include <wlr/types/wlr_buffer.h>+#include <wlr/types/wlr_compositor.h>+#include <wlr/types/wlr_damage_ring.h>+#include <wlr/types/wlr_output.h>+#include "node.h"+#include <wlr/util/log.h>+#include <wlr/util/region.h>+#include <wlr/util/transform.h>++// Stage 5c: a glass effect's blur, kept per effect and output between frames+// (D1, D3 of docs/plans/scene-graph-stage5.md) and re-taken only when something+// below the effect changed in the region it reads.+struct effect_cache {+ struct wl_list link;+ uint64_t serial; // rediwm_glass_info.serial+ struct rediwm_backdrop *backdrop;+ struct wlr_box region; // device pixels the blur covers+ float sigma; // device pixels+ bool valid, seen;+ // A hold moved the blur without re-taking it: re-take it once released.+ bool carried;+};+struct output_state {+ struct wl_list link;+ struct rediwm_output_renderer *renderer;+ struct wlr_output *output;+ struct wl_listener destroy;+ uint64_t cursor; // into the projection's damage events+ bool started;+ // Software cursors drawn last frame (device pixels), whose damage now is old.+ struct wlr_box cursors[4];+ int ncursors;+ struct wl_list caches;+ // rediwm_scene_output_set_veil: black over the frame (0 to 1), and whether it+ // changed since the last frame was built, so that change re-blurs no glass.+ float veil;+ bool veil_changed;+};+struct entry {+ struct rediwm_item item;+ struct rediwm_node *transport;+ struct wlr_box box; // snap of item edges in layout space+ pixman_region32_t visible, opaque;+ bool sampled;+ // Effects only: what glass registered, the box the blur reads (layout, the+ // target's grown by three sigma), the surface drawn over it, and whether+ // this frame takes a new blur.+ struct rediwm_glass_info glass;+ struct wlr_box padded;+ float sigma; // layout pixels+ struct rediwm_node *front;+ struct effect_cache *cache;+ bool reblur;+};+struct rediwm_output_renderer {+ struct rediwm_scene_renderer *pass;+ struct wl_array entries; // reusable scratch; no retained resource references+ struct wl_list outputs; // output_state+ struct rediwm_projection *projection; // of the frame being built+ bool has_backdrops;+};+// Stage 3 parity deliberately retains the legacy layout snap, then snaps+// output-scaled edges. Removing that first snap is a geometry change, not a+// renderer switch. No view node position/size is consulted here.+static inline struct wlr_box device_box(struct wlr_box box, const struct output_view *v) {+ box.x -= v->logical.x;+ box.y -= v->logical.y;+ int x2 = round((box.x + box.width) * v->scale);+ int y2 = round((box.y + box.height) * v->scale);+ box.x = round(box.x * v->scale);+ box.y = round(box.y * v->scale);+ box.width = x2 - box.x;+ box.height = y2 - box.y;+ wlr_box_transform(&box, &box, wlr_output_transform_invert(v->transform),+ v->trans_width, v->trans_height);+ return box;+}+static inline void device_region(pixman_region32_t *region, const struct output_view *v, bool expand) {+ output_view_region(region, v, expand);+}+struct collector {+ struct rediwm_output_renderer *renderer;+ const struct output_view *view;+ // The logical boxes of the other enabled outputs.+ struct wlr_box other[15];+ int others;+ pixman_region32_t covered;+ // A window's items are contiguous and share one clip, so its cover is built+ // once for the run rather than for each of its six or so items.+ struct rediwm_clip cover_of;+ pixman_region32_t cover;+ bool has_cover;+ // What the effects visited so far read (layout): items there are kept even+ // where surfaces above hide them, for the blurs only (stage 5f).+ pixman_region32_t reads;+};++// Shared between the parts (scene_output_*.c).+extern uint64_t rediwm_scene_output_reblurs;+bool scene_output_collect(const struct rediwm_item *item, struct rediwm_node *transport, void *data);+void scene_output_clear_entries(struct rediwm_output_renderer *r);+bool scene_output_decide_effects(struct rediwm_output_renderer *r, struct output_state *os,+ struct rediwm_scene_output *out, const struct output_view *v, pixman_region32_t *changed);+bool scene_output_render_entry(struct rediwm_output_renderer *r, struct entry *e, struct rediwm_scene_output *out,+ struct wlr_render_pass *pass, const struct output_view *v, pixman_region32_t *damage, bool backdrop);++#endifdiff --git a/src/scene_pass_test.c b/src/scene_pass_test.cindex 705561c..f05c00a 100644--- a/src/scene_pass_test.c+++ b/src/scene_pass_test.c@@ -88,1193 +88,12 @@ static struct wlr_texture *texture_create(struct wlr_renderer *renderer, bool op}// `shaped` runs every case through the shaped programs, with a shape that covers// the whole buffer and has no corners: it must still equal wlroots exactly.-static void parity(struct rediwm_scene_renderer *r, struct wlr_buffer *actual,- struct wlr_buffer *expected, bool shaped) {- const struct rediwm_pass_shape cover = {.box = {-50, -50, WIDTH + 100, HEIGHT + 100}};- const struct rediwm_pass_shape *shape = shaped ? &cover : NULL;- pixman_region32_t clip;- pixman_region32_init_rect(&clip, -10, 8, 65, 31);- pixman_region32_union_rect(&clip, &clip, 71, 43, 81, 80);- int worst = 0, cases = 0;- for (int opaque = 0; opaque < 2; opaque++) {- struct wlr_texture *t = texture_create(r->renderer, opaque);- for (int tr = 0; tr < 8; tr++) for (int filter = 0; filter < 2; filter++)- for (int crop = 0; crop < 2; crop++) for (int partial = 0; partial < 2; partial++) {- float alpha = partial ? .61 : 1;- struct wlr_render_pass *pass = wlr_renderer_begin_buffer_pass(r->renderer, expected, NULL);- assert(pass);- clear(pass);- paint(pass, t, tr, filter, crop, alpha, partial ? &clip : NULL, NULL);- assert(wlr_render_pass_submit(pass));- pass = rediwm_scene_pass_begin(r, actual, NULL);- assert(pass);- clear(pass);- paint(pass, t, tr, filter, crop, alpha, partial ? &clip : NULL, shape);- assert(wlr_render_pass_submit(pass));- int d = compare(r, actual, expected);- if (d > 1) {- fprintf(stderr, "parity: shaped=%d opaque=%d transform=%d filter=%d crop=%d partial=%d diff=%d\n",- shaped, opaque, tr, filter, crop, partial, d);- abort();- }- if (d > worst) worst = d;- cases++;- }- wlr_texture_destroy(t);- }- pixman_region32_fini(&clip);- printf("PASS: %d texture/transform/crop/filter/alpha/damage cases%s (max difference %d)\n", cases,- shaped ? " through the shaped programs" : "", worst);-}-// The shaped programs are built with the renderer (stage 5, D4). A bad shape is-// the caller's bug: it must fail the pass at submit, not draw something else,-// and the next pass must work.-static void shapes_fail_closed(struct rediwm_scene_renderer *r, struct wlr_buffer *buffer) {- assert(rediwm_scene_renderer_enable_shapes(r));- const struct rediwm_pass_shape shape = {.box = {10, 10, 50, 50}, .radii = {5, -1, 5, 5}};- struct wlr_render_pass *pass = rediwm_scene_pass_begin(r, buffer, NULL);- assert(pass);- rediwm_scene_pass_add_rect_shaped(pass, &(struct wlr_render_rect_options){- .box = {10, 10, 50, 50}, .color = {1, 0, 0, 1}}, &shape);- assert(!wlr_render_pass_submit(pass));- pass = rediwm_scene_pass_begin(r, buffer, NULL);- assert(pass && wlr_render_pass_submit(pass));- puts("PASS: shapes are built with the renderer; a bad shape fails the pass and the next pass works");-}-static void damage(struct rediwm_scene_renderer *r, struct wlr_buffer *actual,- struct wlr_buffer *expected) {- struct wlr_render_pass *pass = rediwm_scene_pass_begin(r, actual, NULL);- assert(pass);- clear(pass);- assert(wlr_render_pass_submit(pass));- uint8_t before[WIDTH * HEIGHT * 4], after[sizeof(before)];- read_pixels(r, actual, before);- pixman_region32_t clip;- pixman_region32_init_rect(&clip, 11, 13, 23, 17);- pixman_region32_union_rect(&clip, &clip, 71, 60, 13, 19);- struct wlr_texture *t = texture_create(r->renderer, false);- pass = rediwm_scene_pass_begin(r, actual, NULL);- assert(pass);- paint(pass, t, 3, 0, 1, .73, &clip, NULL);- assert(wlr_render_pass_submit(pass));- read_pixels(r, actual, after);- int changed = 0;- for (int y = 0; y < HEIGHT; y++) for (int x = 0; x < WIDTH; x++) {- bool same = memcmp(&before[4 * (y * WIDTH + x)], &after[4 * (y * WIDTH + x)], 4) == 0;- if (!pixman_region32_contains_point(&clip, x, y, NULL)) assert(same);- else changed += !same;- }- assert(changed > 0);- // An empty damage region must not paint anything, even after a prior clip.- pixman_region32_clear(&clip);- pass = rediwm_scene_pass_begin(r, actual, NULL);- assert(pass);- paint(pass, t, 1, 1, 0, 1, &clip, NULL);- assert(wlr_render_pass_submit(pass));- read_pixels(r, actual, before);- assert(memcmp(before, after, sizeof(before)) == 0);- // NONE must replace rather than alpha blend, on both texture and rect paths.- for (int owned = 0; owned < 2; owned++) {- pass = owned ? rediwm_scene_pass_begin(r, actual, NULL) :- wlr_renderer_begin_buffer_pass(r->renderer, expected, NULL);- assert(pass);- clear(pass);- wlr_render_pass_add_texture(pass, &(struct wlr_render_texture_options){- .texture = t, .dst_box = {17, 19, TW, TH}, .blend_mode = WLR_RENDER_BLEND_MODE_NONE,- });- wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){- .box = {71, 8, 11, 13}, .color = {.1, .2, .1, .3}, .blend_mode = WLR_RENDER_BLEND_MODE_NONE,- });- assert(wlr_render_pass_submit(pass));- }- assert(compare(r, actual, expected) <= 1);- wlr_texture_destroy(t);- pixman_region32_fini(&clip);- puts("PASS: disjoint and empty damage preserve untouched pixels; disabled blending matches wlroots");-}-static void state_restoration(struct rediwm_scene_renderer *r, struct wlr_buffer *buffer) {- struct gl_state original;- assert(enter(r, &original));- GLuint vbo, textures[2], caller_vao = 0;- if (r->has_vao) {- r->gen_vertex_arrays(1, &caller_vao);- r->bind_vertex_array(caller_vao);- }- glGenBuffers(1, &vbo);- glBindBuffer(GL_ARRAY_BUFFER, vbo);- glBufferData(GL_ARRAY_BUFFER, 128, NULL, GL_STATIC_DRAW);- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 16, (void *)4);- glEnableVertexAttribArray(0);- glVertexAttribPointer(1, 2, GL_SHORT, GL_TRUE, 8, (void *)16);- glDisableVertexAttribArray(1);- glGenTextures(2, textures);- glActiveTexture(GL_TEXTURE0);- glBindTexture(GL_TEXTURE_2D, textures[0]);- glActiveTexture(GL_TEXTURE3);- glBindTexture(GL_TEXTURE_2D, textures[1]);- glViewport(7, 9, 53, 41);- glScissor(2, 3, 19, 23);- glEnable(GL_SCISSOR_TEST);- glDisable(GL_BLEND);- glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA);- glBlendEquationSeparate(GL_FUNC_REVERSE_SUBTRACT, GL_FUNC_SUBTRACT);- struct gl_state before = {0}, after = {0};- assert(enter(r, &before));- leave(r, &before);- struct wlr_texture *t = texture_create(r->renderer, false);- struct wlr_render_pass *pass = rediwm_scene_pass_begin(r, buffer, NULL);- assert(pass);- assert(!rediwm_scene_pass_begin(r, buffer, NULL)); // no nested owned passes- clear(pass);- paint(pass, t, 1, 0, 1, .5, NULL, NULL);- assert(wlr_render_pass_submit(pass));- assert(enter(r, &after));- leave(r, &after);- assert(memcmp(&before, &after, sizeof(before)) == 0);- GLint value;- glGetVertexAttribiv(0, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &value);- assert(value == (GLint)vbo);- glGetVertexAttribiv(0, GL_VERTEX_ATTRIB_ARRAY_SIZE, &value);- assert(value == 3);- glGetVertexAttribiv(0, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &value);- assert(value == 16);- glGetVertexAttribiv(1, GL_VERTEX_ATTRIB_ARRAY_SIZE, &value);- assert(value == 2);- glGetVertexAttribiv(1, GL_VERTEX_ATTRIB_ARRAY_TYPE, &value);- assert(value == GL_SHORT);- glGetVertexAttribiv(1, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &value);- assert(!value);- assert(glGetError() == GL_NO_ERROR);- wlr_texture_destroy(t);- glDeleteBuffers(1, &vbo);- glDeleteTextures(2, textures);- if (caller_vao) r->delete_vertex_arrays(1, &caller_vao);- leave(r, &original);- puts("PASS: GL bindings, attributes, blend/scissor/viewport and EGL context restored");-}-static void external_texture(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator,- struct wlr_buffer *actual, struct wlr_buffer *expected) {- if (!r->has_external) { puts("SKIP: external-OES unavailable"); return; }- struct wlr_buffer *source = target(allocator);- struct wlr_texture *pattern = texture_create(r->renderer, false);- struct wlr_render_pass *base = wlr_renderer_begin_buffer_pass(r->renderer, source, NULL);- assert(base);- clear(base);- paint(base, pattern, 0, 1, 0, 1, NULL, NULL);- assert(wlr_render_pass_submit(base));- wlr_texture_destroy(pattern);- struct wlr_texture *reference = wlr_texture_from_buffer(r->renderer, source);- assert(reference);- struct wlr_dmabuf_attributes dma;- assert(wlr_buffer_get_dmabuf(source, &dma) && dma.n_planes == 1);- EGLint attrs[32] = {EGL_WIDTH, dma.width, EGL_HEIGHT, dma.height,- EGL_LINUX_DRM_FOURCC_EXT, dma.format, EGL_DMA_BUF_PLANE0_FD_EXT, dma.fd[0],- EGL_DMA_BUF_PLANE0_OFFSET_EXT, dma.offset[0], EGL_DMA_BUF_PLANE0_PITCH_EXT, dma.stride[0]};- int n = 12;- if (dma.modifier != DRM_FORMAT_MOD_INVALID) {- attrs[n++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT;- attrs[n++] = dma.modifier & UINT32_MAX;- attrs[n++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT;- attrs[n++] = dma.modifier >> 32;- }- attrs[n] = EGL_NONE;- PFNEGLCREATEIMAGEKHRPROC create_image = (PFNEGLCREATEIMAGEKHRPROC)eglGetProcAddress("eglCreateImageKHR");- PFNEGLDESTROYIMAGEKHRPROC destroy_image = (PFNEGLDESTROYIMAGEKHRPROC)eglGetProcAddress("eglDestroyImageKHR");- PFNGLEGLIMAGETARGETTEXTURE2DOESPROC bind_image =- (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)eglGetProcAddress("glEGLImageTargetTexture2DOES");- assert(create_image && destroy_image && bind_image);- struct gl_state state;- assert(enter(r, &state));- EGLImageKHR image = create_image(state.display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, attrs);- assert(image != EGL_NO_IMAGE_KHR);- struct wlr_gles2_texture_attribs a = {.target = GL_TEXTURE_EXTERNAL_OES, .has_alpha = true};- glGenTextures(1, &a.tex);- glBindTexture(a.target, a.tex);- bind_image(a.target, image);- glTexParameteri(a.target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);- glTexParameteri(a.target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);- leave(r, &state);- for (int tr = 0; tr < 8; tr++) for (int filter = 0; filter < 2; filter++) {- float alpha = .73;- struct wlr_render_texture_options o = {- .texture = reference, .src_box = {4.25, 8.5, 107.5, 71.25},- .dst_box = {7, 9, 111, 79}, .transform = tr, .filter_mode = filter, .alpha = &alpha,- };- base = wlr_renderer_begin_buffer_pass(r->renderer, expected, NULL);- assert(base);- clear(base);- wlr_render_pass_add_texture(base, &o);- assert(wlr_render_pass_submit(base));- base = rediwm_scene_pass_begin(r, actual, NULL);- assert(base);- clear(base);- struct pass *p = wl_container_of(base, p, base);- // Force a real external EGLImage through the same draw routine; this- // driver's normal import of an RGB dmabuf chooses a 2D target.- texture_draw(p, &o, a, NULL);- assert(wlr_render_pass_submit(base));- assert(compare(r, actual, expected) <= 1);- // The shaped external program with a cover-everything shape is the same draw.- const struct rediwm_pass_shape cover = {.box = {-50, -50, WIDTH + 100, HEIGHT + 100}};- base = rediwm_scene_pass_begin(r, actual, NULL);- assert(base);- clear(base);- p = wl_container_of(base, p, base);- texture_draw(p, &o, a, &cover);- assert(wlr_render_pass_submit(base));- assert(compare(r, actual, expected) <= 1);- // And through a rounded, bordered shape the external texture equals the 2D- // one of the same pixels: the clip does not depend on the sampler.- const struct rediwm_pass_shape round = {.box = {12, 14, 100, 70},- .radii = {18, 9, 25.5f, 3}, .border = 2, .border_color = {.5f, .1f, .1f, .5f}};- base = rediwm_scene_pass_begin(r, expected, NULL);- assert(base);- clear(base);- rediwm_scene_pass_add_texture_shaped(base, &o, &round);- assert(wlr_render_pass_submit(base));- base = rediwm_scene_pass_begin(r, actual, NULL);- assert(base);- clear(base);- p = wl_container_of(base, p, base);- texture_draw(p, &o, a, &round);- assert(wlr_render_pass_submit(base));- assert(compare(r, actual, expected) <= 1);- // Two blank images would also match: the corner must be cut, the middle drawn.- uint8_t shown[WIDTH * HEIGHT * 4];- read_pixels(r, actual, shown);- assert(memcmp(&shown[4 * (15 * WIDTH + 13)], &shown[0], 4) == 0);- assert(memcmp(&shown[4 * (50 * WIDTH + 60)], &shown[0], 4) != 0);- }- assert(enter(r, &state));- glDeleteTextures(1, &a.tex);- destroy_image(state.display, image);- leave(r, &state);- wlr_texture_destroy(reference);- wlr_buffer_drop(source);- puts("PASS: external-OES EGLImage matches wlroots across all transforms and filters, plain and shaped");-}-// Driver-converted YUV. A linear NV12 dmabuf is imported by wlroots exactly as a-// video client's would be. Mesa reports it external-only, so both passes must-// sample it through samplerExternalOES and leave the YUV->RGB conversion to the-// driver. Mesa's gbm cannot allocate NV12, so a linear R8 buffer 1.5x as tall is-// described as two planes of one dmabuf (chroma on a page boundary), as a video-// client would send it. This covers the external program with real YUV; it does-// not cover other drivers' conversion or any tiled/compressed modifier.-static void external_yuv(struct rediwm_scene_renderer *r, struct wlr_buffer *actual,- struct wlr_buffer *expected) {- enum { YW = 64, YH = 64 };- if (!r->has_external) { puts("SKIP: external-OES unavailable"); return; }- int drm_fd = wlr_renderer_get_drm_fd(r->renderer);- struct gbm_device *gbm = drm_fd >= 0 ? gbm_create_device(drm_fd) : NULL;- struct gbm_bo *bo = gbm ? gbm_bo_create(gbm, YW, YH * 3 / 2, GBM_FORMAT_R8, GBM_BO_USE_LINEAR) : NULL;- if (!bo || gbm_bo_get_modifier(bo) != DRM_FORMAT_MOD_LINEAR) {- puts("SKIP: gbm cannot allocate a linear buffer for NV12");- if (bo) gbm_bo_destroy(bo);- if (gbm) gbm_device_destroy(gbm);- return;- }- uint32_t stride = gbm_bo_get_stride(bo);- struct wlr_dmabuf_attributes dma = {.width = YW, .height = YH, .format = DRM_FORMAT_NV12,- .modifier = DRM_FORMAT_MOD_LINEAR, .n_planes = 2,- .offset = {0, stride * YH}, .stride = {stride, stride}};- assert(dma.offset[1] % 4096 == 0);- for (int i = 0; i < 2; i++) {- dma.fd[i] = gbm_bo_get_fd(bo);- assert(dma.fd[i] >= 0);- }- size_t length = dma.offset[1] + (size_t)dma.stride[1] * (YH / 2);- uint8_t *map = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, dma.fd[0], 0);- assert(map != MAP_FAILED);- struct dma_buf_sync sync = {.flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_WRITE};- ioctl(dma.fd[0], DMA_BUF_IOCTL_SYNC, &sync);- for (int y = 0; y < YH; y++) for (int x = 0; x < YW; x++)- map[dma.offset[0] + y * dma.stride[0] + x] = 16 + (x * 3 + y * 5) % 220;- for (int y = 0; y < YH / 2; y++) for (int x = 0; x < YW / 2; x++) {- uint8_t *uv = &map[dma.offset[1] + y * dma.stride[1] + 2 * x];- uv[0] = 32 + (x * 11 + y * 3) % 192;- uv[1] = 32 + (x * 5 + y * 13) % 192;- }- sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_WRITE;- ioctl(dma.fd[0], DMA_BUF_IOCTL_SYNC, &sync);- munmap(map, length);- struct wlr_texture *texture = wlr_texture_from_dmabuf(r->renderer, &dma);- struct wlr_gles2_texture_attribs a = {0};- if (texture) wlr_gles2_texture_get_attribs(texture, &a);- if (!texture || a.target != GL_TEXTURE_EXTERNAL_OES) {- puts(texture ? "SKIP: driver imports NV12 as GL_TEXTURE_2D" : "SKIP: renderer cannot import linear NV12");- } else {- assert(!a.has_alpha);- float alpha = .81f;- for (int tr = 0; tr < 8; tr++) for (int filter = 0; filter < 2; filter++) {- struct wlr_render_texture_options o = {- .texture = texture, .src_box = {2.25, 3.5, 40.5, 30.25},- .dst_box = {7, 9, 111, 79}, .transform = tr, .filter_mode = filter, .alpha = &alpha,- };- struct wlr_render_pass *pass = wlr_renderer_begin_buffer_pass(r->renderer, expected, NULL);- assert(pass);- clear(pass);- wlr_render_pass_add_texture(pass, &o);- assert(wlr_render_pass_submit(pass));- pass = rediwm_scene_pass_begin(r, actual, NULL);- assert(pass);- clear(pass);- wlr_render_pass_add_texture(pass, &o);- assert(wlr_render_pass_submit(pass));- assert(compare(r, actual, expected) <= 1);- }- // Parity with a blank image would prove nothing: the video must have drawn.- uint8_t pixels[WIDTH * HEIGHT * 4];- read_pixels(r, actual, pixels);- int drawn = 0;- for (size_t i = 0; i < sizeof(pixels); i += 4)- if (abs(pixels[i] - 28) > 8 || abs(pixels[i + 1] - 59) > 8 || abs(pixels[i + 2] - 94) > 8) drawn++;- assert(drawn > 4000);- puts("PASS: external-only NV12 dmabuf matches wlroots across all transforms and filters");- }- if (texture) wlr_texture_destroy(texture);- wlr_dmabuf_attributes_finish(&dma);- gbm_bo_destroy(bo);- gbm_device_destroy(gbm);-}-static void synchronization(struct rediwm_scene_renderer *r, struct wlr_buffer *buffer) {- if (!r->has_sync) { puts("SKIP: renderer timelines unavailable"); return; }- struct wlr_drm_syncobj_timeline *timeline =- wlr_drm_syncobj_timeline_create(wlr_renderer_get_drm_fd(r->renderer));- assert(timeline);- assert(wlr_drm_syncobj_timeline_signal(timeline, 1));- struct wlr_texture *t = texture_create(r->renderer, false);- struct wlr_render_pass *base = rediwm_scene_pass_begin(r, buffer,- &(struct wlr_buffer_pass_options){.signal_timeline = timeline, .signal_point = 2});- assert(base);- clear(base);- wlr_render_pass_add_texture(base, &(struct wlr_render_texture_options){- .texture = t, .dst_box = {7, 9, TW, TH}, .wait_timeline = timeline, .wait_point = 1,- });- assert(wlr_render_pass_submit(base));- uint8_t pixels[WIDTH * HEIGHT * 4];- read_pixels(r, buffer, pixels); // finish the submitted GPU work before querying- bool signaled = false;- assert(wlr_drm_syncobj_timeline_check(timeline, 2, DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT, &signaled));- assert(signaled);- base = rediwm_scene_pass_begin(r, buffer, NULL);- assert(base);- // A point that has never materialized must fail closed, without sampling.- wlr_render_pass_add_texture(base, &(struct wlr_render_texture_options){- .texture = t, .wait_timeline = timeline, .wait_point = 99,- });- assert(!wlr_render_pass_submit(base));- // Failure released the destination lock and the renderer's active pass.- base = rediwm_scene_pass_begin(r, buffer, NULL);- assert(base && wlr_render_pass_submit(base));- wlr_texture_destroy(t);- wlr_drm_syncobj_timeline_unref(timeline);- puts("PASS: acquire/release timeline points and recovery from unavailable acquire");-}--// ---- Stage 4b: rounded shapes (docs/plans/scene-graph-stage4.md) -----------------------// The stage 1 spike's gates, on the production pass. Windows are made the way the-// compositor will make them: a clip spec in source units, resolved through a-// transform and snapped, so these also test that the pieces tile on the grid.-enum { GW = 640, GH = 480 };-static const float BG[4] = {224 / 255.f, 225 / 255.f, 232 / 255.f, 1};-static const float BODY[4] = {16 / 255.f, 18 / 255.f, 21 / 255.f, 1};-static const float SCALES[] = {1.f, 1.25f, 1.5f, 2.f};-static const float ZOOMS[] = {1.f, .85f, .70f, .55f, .25f};-#define COUNT(a) (sizeof(a) / sizeof(*(a)))--static void check(bool ok, const char *what, ...) {- if (ok) return;- char text[256];- va_list args;- va_start(args, what);- vsnprintf(text, sizeof(text), what, args);- va_end(args);- fprintf(stderr, "FAIL: %s\n", text);- exit(1);-}-struct image { int w, h; uint8_t *p; };-static uint8_t *at(const struct image *img, int x, int y) { return img->p + 4 * (y * img->w + x); }-static int diff(int a, int b) { return abs(a - b); }-static void read_image(struct rediwm_scene_renderer *r, struct wlr_buffer *b, struct image *img) {- struct gl_state state;- assert(enter(r, &state));- img->w = b->width;- img->h = b->height;- img->p = realloc(img->p, 4 * (size_t)img->w * img->h);- glBindFramebuffer(GL_FRAMEBUFFER, wlr_gles2_renderer_get_buffer_fbo(r->renderer, b));- glPixelStorei(GL_PACK_ALIGNMENT, 1);- glReadPixels(0, 0, img->w, img->h, GL_RGBA, GL_UNSIGNED_BYTE, img->p);- assert(glGetError() == GL_NO_ERROR);- leave(r, &state);-}-static struct wlr_buffer *target_sized(struct wlr_allocator *allocator, int w, int h) {- uint64_t modifier = DRM_FORMAT_MOD_INVALID;- struct wlr_drm_format format = {- .format = DRM_FORMAT_ARGB8888, .len = 1, .capacity = 1, .modifiers = &modifier,- };- struct wlr_buffer *b = wlr_allocator_create_buffer(allocator, w, h, &format);- assert(b);- return b;-}-// A pass whose buffer starts as one opaque colour.-static struct wlr_render_pass *begin_fill(struct rediwm_scene_renderer *r, struct wlr_buffer *b,- const float color[4], const pixman_region32_t *clip) {- struct wlr_render_pass *pass = rediwm_scene_pass_begin(r, b, NULL);- check(pass != NULL, "cannot begin an owned pass");- wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){- .box = {0, 0, b->width, b->height}, .color = {color[0], color[1], color[2], color[3]},- .blend_mode = WLR_RENDER_BLEND_MODE_NONE, .clip = clip,- });- return pass;-}-static void finish(struct wlr_render_pass *pass) { check(wlr_render_pass_submit(pass), "an owned pass failed"); }-static void piece(struct wlr_render_pass *pass, const struct rediwm_pass_shape *shape,- struct wlr_box box, const float color[4], const pixman_region32_t *clip) {- rediwm_scene_pass_add_rect_shaped(pass, &(struct wlr_render_rect_options){- .box = box, .color = {color[0], color[1], color[2], color[3]}, .clip = clip,- }, shape);-}--// A window 200x140 (source units) at (30, 24), plus a titlebar of 36 and a footer-// of 10, placed in a layout offset by 8 device pixels.-struct window {- struct rediwm_pass_shape shape;- struct rediwm_clip clip;- struct wlr_box title, client, footer;-};-static struct wlr_box box_of(struct rediwm_snap s) { return (struct wlr_box){s.x, s.y, s.width, s.height}; }-static struct window make_window(float scale, float zoom, float radius, double border) {- const struct rediwm_xform xf = {(double)scale * zoom, 8, 8};- const struct rediwm_clip_spec spec = {.width = 200, .height = 140, .radii = {radius, radius, radius, radius}, .border = border};- struct window w = {.clip = rediwm_clip_resolve(xf, 30, 24, &spec)};- w.shape.box = box_of(rediwm_edges_snap(w.clip.edges));- for (int i = 0; i < 4; i++) w.shape.radii[i] = w.clip.radii[i];- // A ring is whole device pixels, never thinner than one.- w.shape.border = w.clip.border > 0 ? fmaxf(1.f, roundf(w.clip.border)) : 0;- w.title = box_of(rediwm_edges_snap(rediwm_xform_rect(xf, 30, 24, 200, 36)));- w.client = box_of(rediwm_edges_snap(rediwm_xform_rect(xf, 30, 24 + 36, 200, 140 - 36 - 10)));- w.footer = box_of(rediwm_edges_snap(rediwm_xform_rect(xf, 30, 24 + 140 - 10, 200, 10)));- return w;-}--// ---- Gate 1: the clip against a supersampled reference ----------------------------------static bool inside_rrect(double px, double py, const struct rediwm_pass_shape *s) {- double x = px - s->box.x, y = py - s->box.y;- if (x < 0 || y < 0 || x > s->box.width || y > s->box.height) return false;- double r = (x < s->box.width / 2.) ? (y < s->box.height / 2. ? s->radii[0] : s->radii[3])- : (y < s->box.height / 2. ? s->radii[1] : s->radii[2]);- double cx = x < s->box.width / 2. ? r : s->box.width - r, cy = y < s->box.height / 2. ? r : s->box.height - r;- if (!((x < r || x > s->box.width - r) && (y < r || y > s->box.height - r))) return true;- return hypot(x - cx, y - cy) <= r;-}-static double reference_coverage(const struct rediwm_pass_shape *s, int x, int y) {- const int n = 8;- int hits = 0;- for (int j = 0; j < n; j++) for (int i = 0; i < n; i++) hits += inside_rrect(x + (i + .5) / n, y + (j + .5) / n, s);- return (double)hits / (n * n);-}-static void gate_clip_accuracy(struct rediwm_scene_renderer *r, struct wlr_buffer *buf, struct image *img) {- int worst = 0, fringe = 0;- const uint8_t bg[3] = {224, 225, 232}, body[3] = {16, 18, 21};- for (size_t si = 0; si < COUNT(SCALES); si++) for (size_t zi = 0; zi < COUNT(ZOOMS); zi++) {- struct window w = make_window(SCALES[si], ZOOMS[zi], 10, 0);- struct wlr_render_pass *pass = begin_fill(r, buf, BG, NULL);- piece(pass, &w.shape, w.shape.box, BODY, NULL);- finish(pass);- read_image(r, buf, img);- for (int y = w.shape.box.y - 2; y < w.shape.box.y + w.shape.box.height + 2; y++)- for (int x = w.shape.box.x - 2; x < w.shape.box.x + w.shape.box.width + 2; x++) {- double c = reference_coverage(&w.shape, x, y);- for (int ch = 0; ch < 3; ch++) {- int got = at(img, x, y)[ch];- int want = (int)lround(bg[ch] * (1 - c) + body[ch] * c);- if (diff(got, want) > worst) worst = diff(got, want);- // No fringe: never outside the range of the two colours.- int lo = body[ch] < bg[ch] ? body[ch] : bg[ch], hi = body[ch] < bg[ch] ? bg[ch] : body[ch];- if (got < lo - 1 || got > hi + 1) fringe++;- }- }- }- check(fringe == 0, "clip: %d channel values fall outside the bg/body range (fringe)", fringe);- // A one-pixel linear ramp on the distance is not area coverage: on a curved- // 45-degree edge it is off by up to about .08 of the contrast, here 18 of 208- // levels at scale 1.25, zoom .70 (an 8.75 px radius), pixel (2,2) of each- // corner. The spike, without the 1.25 scale, measured 12. glass.c uses the- // same ramp, and a straight edge or a pixel off the diagonal is exact.- check(worst <= 20, "clip: differs from supersampled coverage by %d levels", worst);- printf("PASS: rounded clip within %d levels of area coverage, no fringe, %zu scales x %zu zoom steps\n",- worst, COUNT(SCALES), COUNT(ZOOMS));-}--// ---- Gate 2: pieces of one window share the clip with no seam ---------------------------static void gate_no_seams(struct rediwm_scene_renderer *r, struct wlr_buffer *buf, struct image *img) {- const float red[4] = {1, 0, 0, 1}, green[4] = {0, 1, 0, 1}, blue[4] = {0, 0, 1, 1};- for (size_t si = 0; si < COUNT(SCALES); si++) for (size_t zi = 0; zi < COUNT(ZOOMS); zi++) {- struct window w = make_window(SCALES[si], ZOOMS[zi], 10, 0);- struct wlr_render_pass *pass = begin_fill(r, buf, BG, NULL);- piece(pass, &w.shape, w.title, red, NULL);- piece(pass, &w.shape, w.client, green, NULL);- piece(pass, &w.shape, w.footer, blue, NULL);- finish(pass);- read_image(r, buf, img);- const struct wlr_box *b = &w.shape.box;- const int margin = (int)ceilf(w.shape.radii[0]) + 2;- for (int y = 0; y < b->height; y++) for (int x = margin; x < b->width - margin; x++) {- const float *want = b->y + y < w.client.y ? red : b->y + y < w.footer.y ? green : blue;- uint8_t *px = at(img, b->x + x, b->y + y);- for (int ch = 0; ch < 3; ch++)- check(diff(px[ch], (int)lround(want[ch] * 255)) <= 1,- "seam at scale %.2f zoom %.2f, row %d col %d channel %d = %d",- SCALES[si], ZOOMS[zi], y, x, ch, px[ch]);- }- // The pieces really tile the shape's box, with no gap and no overlap.- check(w.title.y == b->y && w.title.y + w.title.height == w.client.y &&- w.client.y + w.client.height == w.footer.y && w.footer.y + w.footer.height == b->y + b->height,- "pieces do not tile the window at scale %.2f zoom %.2f", SCALES[si], ZOOMS[zi]);- }- puts("PASS: titlebar, client and footer meet without a seam at every scale and zoom");-}--// ---- Gate 3: border and body partition the shape's coverage -----------------------------static void gate_border_partition(struct rediwm_scene_renderer *r, struct wlr_buffer *buf,- struct image *img, struct image *ref) {- int worst = 0;- for (size_t si = 0; si < COUNT(SCALES); si++) for (size_t zi = 0; zi < COUNT(ZOOMS); zi++) {- struct window w = make_window(SCALES[si], ZOOMS[zi], 10, 1);- struct rediwm_pass_shape ringed = w.shape, plain = w.shape;- plain.border = 0;- memcpy(ringed.border_color, BODY, sizeof(BODY));- // Ring and body in one colour, in three pieces, must equal one plain body,- // whatever the split between them, or coverage leaks.- struct wlr_render_pass *pass = begin_fill(r, buf, BG, NULL);- piece(pass, &plain, plain.box, BODY, NULL);- finish(pass);- read_image(r, buf, ref);- pass = begin_fill(r, buf, BG, NULL);- piece(pass, &ringed, w.title, BODY, NULL);- piece(pass, &ringed, w.client, BODY, NULL);- piece(pass, &ringed, w.footer, BODY, NULL);- finish(pass);- read_image(r, buf, img);- for (int i = 0; i < img->w * img->h * 4; i++) if (diff(img->p[i], ref->p[i]) > worst) worst = diff(img->p[i], ref->p[i]);- // And the ring really is the ring: its own colour on the outer row of a- // straight edge, the body colour just inside it.- const float red[4] = {1, 0, 0, 1};- memcpy(ringed.border_color, red, sizeof(red));- pass = begin_fill(r, buf, BG, NULL);- piece(pass, &ringed, ringed.box, BODY, NULL);- finish(pass);- read_image(r, buf, img);- int cx = ringed.box.x + ringed.box.width / 2, bw = (int)ringed.border;- check(at(img, cx, ringed.box.y)[0] >= 254 && at(img, cx, ringed.box.y)[1] <= 1,- "outer row is not the border colour at scale %.2f zoom %.2f", SCALES[si], ZOOMS[zi]);- check(diff(at(img, cx, ringed.box.y + bw)[0], 16) <= 1,- "row inside the border is not the body at scale %.2f zoom %.2f", SCALES[si], ZOOMS[zi]);- check(at(img, ringed.box.x, ringed.box.y + ringed.box.height / 2)[0] >= 254,- "left edge is not the border colour at scale %.2f zoom %.2f", SCALES[si], ZOOMS[zi]);- // A translucent ring (the default theme's is 9% white) is composited over the- // body, not in place of it: ring + body * (1 - alpha), per channel.- const float faint[4] = {.09f, .09f, .09f, .09f};- memcpy(ringed.border_color, faint, sizeof(faint));- pass = begin_fill(r, buf, BG, NULL);- piece(pass, &ringed, ringed.box, BODY, NULL);- finish(pass);- read_image(r, buf, img);- for (int ch = 0; ch < 3; ch++)- check(diff(at(img, cx, ringed.box.y)[ch], (int)lround(255 * (.09 + BODY[ch] * .91))) <= 1,- "translucent ring is not over the body at scale %.2f zoom %.2f: channel %d is %d",- SCALES[si], ZOOMS[zi], ch, at(img, cx, ringed.box.y)[ch]);- }- check(worst <= 2, "border + body differ from one shape by %d levels", worst);- printf("PASS: border ring and body, in one pass, add up to exactly the shape's coverage (max diff %d)\n", worst);-}--// ---- Gate 4: premultiplied texture edges and opaque (XRGB) textures ----------------------static void gate_premultiplied(struct rediwm_scene_renderer *r, struct wlr_buffer *buf, struct image *img) {- struct rediwm_pass_shape s = {.box = {40, 40, 80, 80}, .radii = {20, 20, 20, 20}};- // 50% red, premultiplied: (128, 0, 0, 128) is a half-intensity red at half alpha.- uint32_t half[16], junk = 0x000AC81E, white = 0xffffffff, solid_px[1] = {junk};- for (int i = 0; i < 16; i++) half[i] = 0x80800000;- struct wlr_texture *t_half = wlr_texture_from_pixels(r->renderer, DRM_FORMAT_ARGB8888, 16, 4, 4, half);- struct wlr_texture *t_xrgb = wlr_texture_from_pixels(r->renderer, DRM_FORMAT_XRGB8888, 4, 1, 1, solid_px);- struct wlr_texture *t_white = wlr_texture_from_pixels(r->renderer, DRM_FORMAT_XRGB8888, 4, 1, 1, &white);- check(t_half && t_xrgb && t_white, "cannot create the test textures");- struct { struct wlr_texture *t; float alpha; const char *name; } cases[] = {- {t_half, 1, "half-transparent"}, {t_xrgb, 1, "xrgb"}, {t_white, .5f, "opacity"},- };- for (size_t i = 0; i < COUNT(cases); i++) {- const bool on_black = cases[i].t == t_white;- float base[4] = {BG[0], BG[1], BG[2], 1};- if (on_black) memset(base, 0, sizeof(float) * 3);- struct wlr_render_pass *pass = begin_fill(r, buf, base, NULL);- float alpha = cases[i].alpha;- rediwm_scene_pass_add_texture_shaped(pass, &(struct wlr_render_texture_options){- .texture = cases[i].t, .dst_box = s.box, .alpha = &alpha}, &s);- finish(pass);- read_image(r, buf, img);- uint8_t *in = at(img, 80, 80);- const int want[3][3] = {{240, 112, 116}, {10, 200, 30}, {128, 128, 128}};- for (int ch = 0; ch < 3; ch++)- check(diff(in[ch], want[i][ch]) <= 2, "%s texel gave (%d,%d,%d) inside the shape",- cases[i].name, in[0], in[1], in[2]);- // At the edge the colour is the premultiplied source times coverage over the backdrop:- // never darker or brighter than the two ends, and within the clip's accuracy of area coverage.- double src_a = i == 0 ? .50196 : i == 2 ? .5 : 1;- for (int y = s.box.y - 1; y < s.box.y + s.box.height + 1; y++)- for (int x = s.box.x - 1; x < s.box.x + s.box.width + 1; x++) {- double c = reference_coverage(&s, x, y);- for (int ch = 0; ch < 3; ch++) {- double bgv = base[ch] * 255;- double want_v = want[i][ch] - (1 - src_a) * bgv; // the premultiplied source, 0..255- double expect = want_v * c + bgv * (1 - src_a * c);- check(diff(at(img, x, y)[ch], (int)lround(expect)) <= 14,- "%s edge pixel (%d,%d) channel %d is %d, area coverage predicts %.0f",- cases[i].name, x, y, ch, at(img, x, y)[ch], expect);- }- }- }- wlr_texture_destroy(t_half);- wlr_texture_destroy(t_xrgb);- wlr_texture_destroy(t_white);- puts("PASS: premultiplied edges, opacity and XRGB textures blend correctly through the clip");-}--// ---- Dimming: a window that is not the focus is graded in the pass -----------------// What rediwm_scene_pass_set_dim documents, worked out here on its own: the colour is-// pulled toward its luminance by the saturation taken away, then everything is scaled-// by the brightness left. On premultiplied values, as the pass has them.-static void graded(const double in[3], double brightness, double saturation, double out[3]) {- const double l = .213 * in[0] + .715 * in[1] + .072 * in[2];- for (int i = 0; i < 3; i++) out[i] = (l + (in[i] - l) * (1 - saturation)) * (1 - brightness);-}-static void check_pixel(const struct image *img, int x, int y, const double want[3], const char *what) {- for (int ch = 0; ch < 3; ch++)- check(diff(at(img, x, y)[ch], (int)lround(255 * want[ch])) <= 2, "%s: (%d,%d) channel %d is %d, not %ld",- what, x, y, ch, at(img, x, y)[ch], lround(255 * want[ch]));-}-static void gate_dim(struct rediwm_scene_renderer *r, struct wlr_buffer *buf, struct image *img) {- const float db = .10f, ds = .20f;- const float c[4] = {200 / 255.f, 100 / 255.f, 50 / 255.f, 1};- const double cd[3] = {c[0], c[1], c[2]}, bg[3] = {BG[0], BG[1], BG[2]};- double want[3];- // A plain rect, then one after the dim is taken off: the first is graded, the- // second and what was drawn before are not.- struct wlr_render_pass *pass = begin_fill(r, buf, BG, NULL);- rediwm_scene_pass_set_dim(pass, db, ds);- wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){.box = {20, 20, 40, 40}, .color = {c[0], c[1], c[2], 1}});- rediwm_scene_pass_set_dim(pass, 0, 0);- wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){.box = {80, 20, 40, 40}, .color = {c[0], c[1], c[2], 1}});- finish(pass);- read_image(r, buf, img);- graded(cd, db, ds, want);- check_pixel(img, 40, 40, want, "a dimmed plain rect");- check_pixel(img, 100, 40, cd, "a rect after the dim is taken off");- check_pixel(img, 300, 300, bg, "the background before the dim");- // A dimmed rect through a shape: the interior and the ring are graded, the- // corner the shape cuts away is the backdrop, whole.- const struct rediwm_pass_shape ring = {.box = {140, 100, 80, 80}, .radii = {20, 20, 20, 20}, .border = 4,- .border_color = {.5f, 0, 0, .5f}};- pass = begin_fill(r, buf, BG, NULL);- rediwm_scene_pass_set_dim(pass, db, ds);- piece(pass, &ring, ring.box, c, NULL);- rediwm_scene_pass_set_dim(pass, 0, 0);- finish(pass);- read_image(r, buf, img);- graded(cd, db, ds, want);- check_pixel(img, 180, 140, want, "a dimmed shaped interior");- const double under_ring[3] = {cd[0] * .5 + .5, cd[1] * .5, cd[2] * .5};- graded(under_ring, db, ds, want);- check_pixel(img, 141, 140, want, "a dimmed ring");- check_pixel(img, 141, 101, bg, "the corner a dimmed shape cuts away");- // A half-transparent texel: graded as premultiplied, then over the backdrop,- // so it keeps its alpha.- uint32_t half[16];- for (int i = 0; i < 16; i++) half[i] = 0x80800000;- struct wlr_texture *t_half = wlr_texture_from_pixels(r->renderer, DRM_FORMAT_ARGB8888, 16, 4, 4, half);- check(t_half != NULL, "cannot create the test texture");- const struct rediwm_pass_shape card = {.box = {240, 100, 80, 80}, .radii = {20, 20, 20, 20}};- pass = begin_fill(r, buf, BG, NULL);- rediwm_scene_pass_set_dim(pass, db, ds);- rediwm_scene_pass_add_texture_shaped(pass, &(struct wlr_render_texture_options){- .texture = t_half, .dst_box = card.box}, &card);- rediwm_scene_pass_set_dim(pass, 0, 0);- finish(pass);- read_image(r, buf, img);- const double alpha = 128 / 255., src[3] = {128 / 255., 0, 0};- graded(src, db, ds, want);- for (int i = 0; i < 3; i++) want[i] += bg[i] * (1 - alpha);- check_pixel(img, 280, 140, want, "a dimmed half-transparent texel");- wlr_texture_destroy(t_half);- // A shadow is not graded, whatever the pass holds.- const struct rediwm_pass_shape cast = {.box = {340, 100, 80, 80}, .radii = {20, 20, 20, 20}};- const struct rediwm_pass_shadow shadow = {.softness = 12, .color = {.6f, 0, 0, .6f}};- uint8_t plain[3];- for (int dimmed = 0; dimmed < 2; dimmed++) {- pass = begin_fill(r, buf, BG, NULL);- if (dimmed) rediwm_scene_pass_set_dim(pass, db, ds);- rediwm_scene_pass_add_shadow(pass, &cast, &shadow);- finish(pass);- read_image(r, buf, img);- if (!dimmed) memcpy(plain, at(img, 380, 140), 3);- else for (int ch = 0; ch < 3; ch++)- check(diff(at(img, 380, 140)[ch], plain[ch]) <= 1, "a dimmed pass graded a shadow (channel %d)", ch);- }- // A pass that ends with the dim still set leaves none for the next one.- pass = begin_fill(r, buf, BG, NULL);- rediwm_scene_pass_set_dim(pass, .5f, .5f);- wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){.box = {20, 20, 40, 40}, .color = {c[0], c[1], c[2], 1}});- finish(pass);- pass = begin_fill(r, buf, BG, NULL);- wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){.box = {20, 20, 40, 40}, .color = {c[0], c[1], c[2], 1}});- finish(pass);- read_image(r, buf, img);- check_pixel(img, 40, 40, cd, "the first rect of a pass after one that kept its dim");- // A fraction outside 0 to 1 fails the pass, and the next one works.- const float bad[][2] = {{-.1f, 0}, {0, 1.5f}, {NAN, 0}, {0, INFINITY}};- for (size_t i = 0; i < COUNT(bad); i++) {- pass = begin_fill(r, buf, BG, NULL);- rediwm_scene_pass_set_dim(pass, bad[i][0], bad[i][1]);- check(!wlr_render_pass_submit(pass), "a dim of (%g, %g) did not fail the pass", bad[i][0], bad[i][1]);- }- pass = begin_fill(r, buf, BG, NULL);- finish(pass);- puts("PASS: a dimmed pass grades rects, shapes, rings and translucent texels, not shadows, and fails closed");-}--// ---- Gate 5: the opaque cover of the model is fully covered in real pixels --------------// rediwm_clip_opaque promises its rectangles are inside full coverage; the output-// pass will draw them without blending and trust that. Painting them first with-// blending off, then the whole window over them, must change nothing.-static void gate_opaque_cover(struct rediwm_scene_renderer *r, struct wlr_buffer *buf,- struct image *img, struct image *ref) {- for (size_t si = 0; si < COUNT(SCALES); si++) for (size_t zi = 0; zi < COUNT(ZOOMS); zi++) {- struct window w = make_window(SCALES[si], ZOOMS[zi], 10, 1);- struct rediwm_pass_shape s = w.shape;- memcpy(s.border_color, BODY, sizeof(BODY));- struct wlr_render_pass *pass = begin_fill(r, buf, BG, NULL);- piece(pass, &s, s.box, BODY, NULL);- finish(pass);- read_image(r, buf, ref);- // Border colour equals the body's here, so the cover may include the ring.- struct rediwm_snap cover[3];- int n = rediwm_clip_opaque(&w.clip, cover);- check(n > 0, "no opaque cover at scale %.2f zoom %.2f", SCALES[si], ZOOMS[zi]);- pass = begin_fill(r, buf, BG, NULL);- for (int i = 0; i < n; i++)- wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){- .box = box_of(cover[i]), .color = {BODY[0], BODY[1], BODY[2], 1},- .blend_mode = WLR_RENDER_BLEND_MODE_NONE});- piece(pass, &s, s.box, BODY, NULL);- finish(pass);- read_image(r, buf, img);- for (int i = 0; i < img->w * img->h * 4; i++)- check(diff(img->p[i], ref->p[i]) <= 1, "cover pixel differs at scale %.2f zoom %.2f (%d vs %d)",- SCALES[si], ZOOMS[zi], img->p[i], ref->p[i]);- }- puts("PASS: the opaque cover draws without blending and changes nothing, at every scale and zoom");-}--// ---- Gate 6: analytic shadow -------------------------------------------------------------// The CPU chrome's shadow (chrome.zig: ShadowBuffer.rasterize, shadowCoverage,-// edgeCoverage) at one pixel: smoothstep falloff from the moved shape, cut out of-// the unmoved one with a one-pixel ramp. Every pixel is compared, corners too.-static double cpu_shadow(double px, double py, const struct rediwm_pass_shape *s, double oy, double softness) {- const double radii[4] = {s->radii[0], s->radii[1], s->radii[2], s->radii[3]};- const double d = rediwm_sd_rounded_box(px, py, s->box.x, s->box.y + oy, s->box.width, s->box.height, radii);- const double frame_d = rediwm_sd_rounded_box(px, py, s->box.x, s->box.y, s->box.width, s->box.height, radii);- double t = 1 - d / softness;- t = t < 0 ? 0 : t > 1 ? 1 : t;- double inside = .5 - frame_d;- inside = inside < 0 ? 0 : inside > 1 ? 1 : inside;- return t * t * (3 - 2 * t) * (1 - inside);-}-static void gate_shadow(struct rediwm_scene_renderer *r, struct wlr_buffer *buf, struct image *img) {- const float white[4] = {1, 1, 1, 1};- // The default theme: radius 10, softness 24, offset 8, at scale 1 and 1.5.- const float scales[] = {1, 1.5f};- int worst = 0;- for (int si = 0; si < 2; si++) {- const float k = scales[si];- const float softness = 24 * k, oy = roundf(8 * k), radius = 10 * k;- const struct rediwm_pass_shape s = {.box = {80, 60, 200, 120}, .radii = {radius, radius, radius, radius}};- struct wlr_render_pass *pass = begin_fill(r, buf, white, NULL);- rediwm_scene_pass_add_shadow(pass, &s, &(struct rediwm_pass_shadow){- .offset_y = oy, .softness = softness, .color = {0, 0, 0, .5f}, .hide_inside = true});- finish(pass);- read_image(r, buf, img);- for (int y = 0; y < img->h; y++)- for (int x = 0; x < img->w; x++) {- const int want = (int)lround(255 * (1 - .5 * cpu_shadow(x + .5, y + .5, &s, oy, softness)));- const int got = at(img, x, y)[0];- if (diff(got, want) > worst) worst = diff(got, want);- }- const int cx = s.box.x + s.box.width / 2, bottom = s.box.y + s.box.height;- int last = 255;- for (int dy = (int)oy; dy < 60; dy++) {- const int got = at(img, cx, bottom + dy)[0];- check(got >= last || dy == (int)oy, "shadow is not monotonic at %d px (%d after %d)", dy, got, last);- last = got;- }- check(at(img, 10, 10)[0] == 255 && at(img, cx, s.box.y + s.box.height / 2)[0] == 255,- "shadow reaches outside its footprint or inside the window");- }- check(worst <= 1, "shadow differs from the CPU chrome's by %d levels", worst);- // Not hidden inside: the same draw shows through the window.- const struct rediwm_pass_shape s = {.box = {80, 60, 200, 120}};- struct wlr_render_pass *pass = begin_fill(r, buf, white, NULL);- rediwm_scene_pass_add_shadow(pass, &s, &(struct rediwm_pass_shadow){- .offset_y = 6, .softness = 24, .color = {0, 0, 0, .5f}});- finish(pass);- read_image(r, buf, img);- check(at(img, s.box.x + s.box.width / 2, s.box.y + s.box.height / 2)[0] < 200,- "an unhidden shadow does not show under the shape");- printf("PASS: analytic shadow equals the CPU chrome's at every pixel, corners included (max diff %d), "- "and stays out of the window\n", worst);-}-// ---- Gate 7: a partial repaint across a clip edge equals a full repaint ------------------static void draw_scene(struct rediwm_scene_renderer *r, struct wlr_buffer *buf, const struct window *w,- int variant, const pixman_region32_t *clip) {- const float bodies[2][3][4] = {- {{.2f, .3f, .5f, 1}, {.9f, .9f, .9f, 1}, {.3f, .3f, .3f, 1}},- {{.8f, .2f, .2f, 1}, {.1f, .6f, .2f, 1}, {.6f, .6f, .1f, 1}},- };- struct rediwm_pass_shape s = w->shape;- const float rings[2][4] = {{1, 1, 1, 1}, {1, 0, 0, 1}};- memcpy(s.border_color, rings[variant], sizeof(rings[variant]));- struct wlr_render_pass *pass = begin_fill(r, buf, BG, clip);- rediwm_scene_pass_add_shadow(pass, &s, &(struct rediwm_pass_shadow){- .offset_y = 12, .softness = 24,- .color = {0, 0, 0, .4f}, .hide_inside = true, .clip = clip});- piece(pass, &s, w->title, bodies[variant][0], clip);- piece(pass, &s, w->client, bodies[variant][1], clip);- piece(pass, &s, w->footer, bodies[variant][2], clip);- finish(pass);-}-static void gate_damage_at_clip_edge(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator,- struct image *img) {- struct wlr_buffer *a = target_sized(allocator, GW, GH), *b = target_sized(allocator, GW, GH),- *c = target_sized(allocator, GW, GH);- struct image one = {0}, two = {0};- int differing_inside = 0, differing_outside = 0;- const float zooms[] = {1.f, .55f};- for (size_t si = 0; si < COUNT(SCALES); si++) for (size_t zi = 0; zi < COUNT(zooms); zi++) {- struct window w = make_window(SCALES[si], zooms[zi], 10, 1);- const struct wlr_box *bx = &w.shape.box;- // Damage whose edges cut through the corner arcs, the ring and the shadow:- // a square over the top-left arc, one over the bottom-right, and a band.- pixman_region32_t damage;- pixman_region32_init_rect(&damage, bx->x - 5, bx->y - 5, 13, 13);- pixman_region32_union_rect(&damage, &damage, bx->x + bx->width - 9, bx->y + bx->height - 11, 30, 29);- pixman_region32_union_rect(&damage, &damage, bx->x + bx->width / 3, bx->y + 3, 7, bx->height + 20);- draw_scene(r, a, &w, 0, NULL);- draw_scene(r, b, &w, 1, NULL);- draw_scene(r, c, &w, 0, NULL);- draw_scene(r, c, &w, 1, &damage);- read_image(r, a, &one);- read_image(r, b, &two);- read_image(r, c, img);- for (int y = 0; y < GH; y++) for (int x = 0; x < GW; x++) {- bool in = pixman_region32_contains_point(&damage, x, y, NULL);- const uint8_t *want = at(in ? &two : &one, x, y);- for (int ch = 0; ch < 4; ch++)- check(at(img, x, y)[ch] == want[ch],- "partial repaint differs at (%d,%d) channel %d (%s damage): %d, wanted %d (scale %.2f)",- x, y, ch, in ? "inside" : "outside", at(img, x, y)[ch], want[ch], SCALES[si]);- if (memcmp(at(&one, x, y), at(&two, x, y), 4)) in ? differing_inside++ : differing_outside++;- }- pixman_region32_fini(&damage);- }- // The control: the two frames really differ both where the damage is and where it is not.- check(differing_inside > 0 && differing_outside > 0, "the test frames do not differ across the damage edge");- free(one.p);- free(two.p);- wlr_buffer_drop(a);- wlr_buffer_drop(b);- wlr_buffer_drop(c);- puts("PASS: repainting only damage that cuts through arcs, ring and shadow equals a full repaint");-}+#include "scene_pass_test_parity.c"+#include "scene_pass_test_shapes.c"+#include "scene_pass_test_shading.c"+#include "scene_pass_test_backdrop.c"-static void shaped_gates(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator) {- struct wlr_buffer *buf = target_sized(allocator, GW, GH);- struct image img = {0}, ref = {0};- gate_clip_accuracy(r, buf, &img);- gate_no_seams(r, buf, &img);- gate_border_partition(r, buf, &img, &ref);- gate_premultiplied(r, buf, &img);- gate_dim(r, buf, &img);- gate_opaque_cover(r, buf, &img, &ref);- gate_shadow(r, buf, &img);- gate_damage_at_clip_edge(r, allocator, &img);- free(img.p);- free(ref.p);- wlr_buffer_drop(buf);-}-// ---- Stage 5b: backdrops -------------------------------------------------------------static struct wlr_buffer *target_format(struct wlr_allocator *allocator, int w, int h, uint32_t fmt) {- uint64_t modifier = DRM_FORMAT_MOD_INVALID;- struct wlr_drm_format format = {.format = fmt, .len = 1, .capacity = 1, .modifiers = &modifier};- struct wlr_buffer *b = wlr_allocator_create_buffer(allocator, w, h, &format);- assert(b);- return b;-}-static const float BANDS[5][4] = {{1, 0, 0, 1}, {0, .8f, 0, 1}, {0, 0, 1, 1}, {1, 1, 1, 1}, {.1f, .1f, .1f, 1}};-static void bands(struct wlr_render_pass *pass, int width, int height) {- for (int x = 0, i = 0; x < width; x += 40, i++)- wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){- .box = {x, 0, 40, height}, .color = {BANDS[i % 5][0], BANDS[i % 5][1], BANDS[i % 5][2], 1}});-}-static void fill(struct wlr_render_pass *pass, const float color[4]) {- wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){- .box = {0, 0, GW, GH}, .color = {color[0], color[1], color[2], color[3]}});-}-// Vertical bands 40 px wide, then a backdrop of `region` composited over `box`.-static float backdrop_factor = 4;-static struct rediwm_backdrop *draw_backdrop(struct rediwm_scene_renderer *r, struct wlr_buffer *buf,- struct rediwm_backdrop *b, struct wlr_box region, struct wlr_box box, float sigma,- const struct rediwm_pass_backdrop *opts, const pixman_region32_t *clip) {- struct wlr_render_pass *pass = begin_fill(r, buf, BG, NULL);- bands(pass, buf->width, buf->height);- // What lies below the effect, drawn again into the quarter-size backdrop.- rediwm_scene_pass_begin_backdrop(pass, region, backdrop_factor);- bands(pass, buf->width, buf->height);- rediwm_scene_pass_end_backdrop(pass, &b, sigma);- struct rediwm_pass_backdrop o = opts ? *opts : (struct rediwm_pass_backdrop){.saturation = 1, .opacity = 1};- o.backdrop = b;- o.clip = clip;- rediwm_scene_pass_add_backdrop(pass, &(struct rediwm_pass_shape){.box = box}, box, &o);- finish(pass);- return b;-}-// The exact full-resolution Gaussian of the bands, clamped at the buffer edge, as-// the reference the quarter-size blur approximates.-static double band_channel(int x, int width, int c) {- x = x < 0 ? 0 : x >= width ? width - 1 : x;- return BANDS[(x / 40) % 5][c];-}-static void gate_backdrop_blur(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator, struct image *img) {- const struct wlr_box region = {100, 100, 400, 200}, box = {148, 148, 304, 104};- int worst[4] = {0};- for (int f = 0; f < 4; f++) {- // ARGB and XRGB buffers, reduced by 4 (scale 1) and by 6 (glass at 1.5).- backdrop_factor = f < 2 ? 4 : 6;- // Sigma in reduced pixels as glass has it (22 logical px over a quarter of- // the logical size): the same at every scale.- const float sigma = 4 * backdrop_factor;- struct wlr_buffer *buf = target_format(allocator, GW, GH, f % 2 ? DRM_FORMAT_XRGB8888 : DRM_FORMAT_ARGB8888);- struct rediwm_backdrop *b = draw_backdrop(r, buf, NULL, region, box, sigma, NULL, NULL);- read_image(r, buf, img);- for (int x = box.x; x < box.x + box.width; x++) {- double want[3] = {0};- double weight = 0;- for (int k = -96; k <= 96; k++) {- const double w = exp(-.5 * k * k / (sigma * sigma));- // Clamped to the copied region, as the blur's limits are.- int sx = x + k;- sx = sx < region.x ? region.x : sx >= region.x + region.width ? region.x + region.width - 1 : sx;- for (int c = 0; c < 3; c++) want[c] += w * band_channel(sx, GW, c);- weight += w;- }- for (int y = box.y; y < box.y + box.height; y += 13)- for (int c = 0; c < 3; c++) {- const int d = diff(at(img, x, y)[c], (int)lround(255 * want[c] / weight));- if (d > worst[f]) worst[f] = d;- }- }- rediwm_scene_renderer_destroy_backdrop(r, b);- wlr_buffer_drop(buf);- }- // Reduced by 4 the bands' edges fall on whole pixels of the image; by 6 they do- // not, and an edge drawn at reduced size moves by up to half a pixel (3 device- // pixels), which on a blurred step of sigma 24 is 255 / (sqrt(2 pi) 24) levels- // per pixel: 13. glass.c drew its reduced capture the same way.- for (int f = 0; f < 4; f++)- check(worst[f] <= (f < 2 ? 2 : 15), "the reduced blur differs from the exact Gaussian by %d levels (case %d)", worst[f], f);- backdrop_factor = 4;- printf("PASS: backdrop blur against the exact Gaussian: %d levels reduced by 4, %d by 6 (edge quantisation), ARGB and XRGB\n",- worst[0] > worst[1] ? worst[0] : worst[1], worst[2] > worst[3] ? worst[2] : worst[3]);-}-static void gate_backdrop_properties(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator, struct image *img) {- struct wlr_buffer *buf = target_sized(allocator, GW, GH);- struct rediwm_backdrop *b = NULL;- // A red/blue step at x = 320 blurs to an even mix there, monotonic across,- // unchanged far from it.- struct wlr_render_pass *pass = begin_fill(r, buf, (float[4]){1, 0, 0, 1}, NULL);- wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){.box = {320, 0, 320, GH}, .color = {0, 0, 1, 1}});- const struct wlr_box region = {120, 100, 400, 200}, box = {168, 148, 304, 104};- rediwm_scene_pass_begin_backdrop(pass, region, 4);- fill(pass, (float[4]){1, 0, 0, 1});- wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){.box = {320, 0, 320, GH}, .color = {0, 0, 1, 1}});- rediwm_scene_pass_end_backdrop(pass, &b, 16);- rediwm_scene_pass_add_backdrop(pass, &(struct rediwm_pass_shape){.box = box}, box,- &(struct rediwm_pass_backdrop){.backdrop = b, .saturation = 1, .opacity = 1});- finish(pass);- read_image(r, buf, img);- const int y = 200;- check(diff(at(img, 320, y)[0], 128) <= 12 && diff(at(img, 320, y)[2], 128) <= 12,- "the step does not blur to an even mix (%d, %d)", at(img, 320, y)[0], at(img, 320, y)[2]);- for (int x = box.x + 1; x < box.x + box.width; x++)- check(at(img, x, y)[0] <= at(img, x - 1, y)[0] + 1, "the blurred step is not monotonic at %d", x);- check(at(img, box.x, y)[0] >= 250 && at(img, box.x + box.width - 1, y)[2] >= 250,- "the blur reaches too far (%d, %d)", at(img, box.x, y)[0], at(img, box.x + box.width - 1, y)[2]);- // At the buffer edge the blur replicates it: no black comes in.- pass = begin_fill(r, buf, (float[4]){0, 1, 0, 1}, NULL);- const struct wlr_box edge_region = {-60, 100, 200, 200}, edge_box = {0, 148, 92, 104};- rediwm_scene_pass_begin_backdrop(pass, edge_region, 4);- fill(pass, (float[4]){0, 1, 0, 1});- rediwm_scene_pass_end_backdrop(pass, &b, 16);- rediwm_scene_pass_add_backdrop(pass, &(struct rediwm_pass_shape){.box = edge_box}, edge_box,- &(struct rediwm_pass_backdrop){.backdrop = b, .saturation = 1, .opacity = 1});- finish(pass);- read_image(r, buf, img);- for (int x = 0; x < 92; x++)- check(at(img, x, 200)[1] >= 253 && at(img, x, 200)[0] <= 2, "the buffer edge darkens the blur at %d", x);- // Saturation as glass.c applies it, on a uniform colour the blur keeps.- const float c[3] = {.2f, .4f, .6f};- pass = begin_fill(r, buf, (float[4]){c[0], c[1], c[2], 1}, NULL);- rediwm_scene_pass_begin_backdrop(pass, region, 4);- fill(pass, (float[4]){c[0], c[1], c[2], 1});- rediwm_scene_pass_end_backdrop(pass, &b, 16);- rediwm_scene_pass_add_backdrop(pass, &(struct rediwm_pass_shape){.box = box}, box,- &(struct rediwm_pass_backdrop){.backdrop = b, .saturation = 1.5f, .opacity = 1});- finish(pass);- read_image(r, buf, img);- const double l = .213 * c[0] + .715 * c[1] + .072 * c[2];- for (int i = 0; i < 3; i++) {- const int want = (int)lround(255 * (l + (c[i] - l) * 1.5));- check(diff(at(img, 300, 200)[i], want) <= 2, "saturation: channel %d is %d, not %d", i, at(img, 300, 200)[i], want);- }- rediwm_scene_renderer_destroy_backdrop(r, b);- wlr_buffer_drop(buf);- puts("PASS: a blurred step mixes evenly and monotonically, the buffer edge replicates, saturation matches glass");-}-static void gate_backdrop_front(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator, struct image *img) {- struct wlr_buffer *buf = target_sized(allocator, GW, GH);- uint32_t half[16 * 16];- for (int i = 0; i < 16 * 16; i++) half[i] = 0x80000000; // alpha 128, premultiplied black- struct wlr_texture *front = wlr_texture_from_pixels(r->renderer, DRM_FORMAT_ARGB8888, 16 * 4, 16, 16, half);- assert(front);- struct rediwm_backdrop *b = NULL;- const struct wlr_box region = {100, 100, 200, 200}, box = {150, 150, 100, 100};- // On a transparent buffer the composite's alpha is what it adds to the front.- struct wlr_render_pass *pass = begin_fill(r, buf, (float[4]){1, 1, 1, 1}, NULL);- rediwm_scene_pass_begin_backdrop(pass, region, 4);- fill(pass, (float[4]){1, 1, 1, 1});- rediwm_scene_pass_end_backdrop(pass, &b, 8);- wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){.box = {0, 0, GW, GH},- .color = {0, 0, 0, 0}, .blend_mode = WLR_RENDER_BLEND_MODE_NONE});- rediwm_scene_pass_add_backdrop(pass, &(struct rediwm_pass_shape){.box = box}, box,- &(struct rediwm_pass_backdrop){.backdrop = b, .saturation = 1, .opacity = .5f,- .front = front, .front_box = box, .front_opacity = .5f});- finish(pass);- read_image(r, buf, img);- const double f = 128 / 255.0 * .5, want = (.5 - f) / (1 - f);- check(diff(at(img, 200, 200)[3], (int)lround(255 * want)) <= 1, "front alpha: %d, not %d",- at(img, 200, 200)[3], (int)lround(255 * want));- // An opaque front leaves nothing for the effect to add.- pass = begin_fill(r, buf, (float[4]){1, 1, 1, 1}, NULL);- rediwm_scene_pass_begin_backdrop(pass, region, 4);- fill(pass, (float[4]){1, 1, 1, 1});- rediwm_scene_pass_end_backdrop(pass, &b, 8);- wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){.box = {0, 0, GW, GH},- .color = {0, 0, 0, 0}, .blend_mode = WLR_RENDER_BLEND_MODE_NONE});- rediwm_scene_pass_add_backdrop(pass, &(struct rediwm_pass_shape){.box = box}, box,- &(struct rediwm_pass_backdrop){.backdrop = b, .saturation = 1, .opacity = 1,- .front = front, .front_box = box, .front_opacity = 2.f});- finish(pass);- read_image(r, buf, img);- check(at(img, 200, 200)[3] == 0, "an opaque front still gets a backdrop (%d)", at(img, 200, 200)[3]);- // A clipped composite equals that part of a full one: the copy and the blur- // ignore the clip.- struct image full = {0};- b = draw_backdrop(r, buf, b, (struct wlr_box){100, 100, 400, 200}, (struct wlr_box){148, 148, 304, 104}, 16, NULL, NULL);- read_image(r, buf, &full);- pixman_region32_t clip;- pixman_region32_init_rect(&clip, 148, 148, 150, 104);- b = draw_backdrop(r, buf, b, (struct wlr_box){100, 100, 400, 200}, (struct wlr_box){148, 148, 304, 104}, 16, NULL, &clip);- read_image(r, buf, img);- for (int y = 148; y < 252; y++) for (int x = 148; x < 298; x++) for (int c = 0; c < 4; c++)- check(at(img, x, y)[c] == at(&full, x, y)[c], "a clipped composite differs at %d,%d", x, y);- pixman_region32_fini(&clip);- free(full.p);- wlr_texture_destroy(front);- rediwm_scene_renderer_destroy_backdrop(r, b);- wlr_buffer_drop(buf);- puts("PASS: the backdrop makes up the front's alpha to the coverage; a clipped composite equals a full one");-}-// The redirect moves boxes, shape origins and scissors into the quarter-size-// image: a rounded piece and a clipped rect land where they belong in it.-static void gate_backdrop_redirect(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator, struct image *img) {- struct wlr_buffer *buf = target_sized(allocator, GW, GH);- struct rediwm_backdrop *b = NULL;- const struct wlr_box region = {100, 80, 400, 240};- const float gray[4] = {.5f, .5f, .5f, 1};- struct wlr_render_pass *pass = begin_fill(r, buf, BG, NULL);- rediwm_scene_pass_begin_backdrop(pass, region, 4);- fill(pass, gray);- const struct rediwm_pass_shape shape = {.box = {148, 120, 200, 120}, .radii = {40, 40, 40, 40}};- rediwm_scene_pass_add_rect_shaped(pass, &(struct wlr_render_rect_options){- .box = shape.box, .color = {1, 0, 0, 1}}, &shape);- pixman_region32_t left;- pixman_region32_init_rect(&left, 380, 200, 32, 60);- wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){- .box = {380, 200, 64, 60}, .color = {0, 0, 1, 1}, .clip = &left});- rediwm_scene_pass_end_backdrop(pass, &b, .5f);- rediwm_scene_pass_add_backdrop(pass, &(struct rediwm_pass_shape){.box = region}, region,- &(struct rediwm_pass_backdrop){.backdrop = b, .saturation = 1, .opacity = 1});- finish(pass);- read_image(r, buf, img);- struct { int x, y; float c[3]; const char *what; } probes[] = {- {248, 180, {1, 0, 0}, "inside the rounded piece"},- {152, 124, {.5f, .5f, .5f}, "a corner the arc cuts away"},- {392, 230, {0, 0, 1}, "the clipped rect"},- {432, 230, {.5f, .5f, .5f}, "outside the clip"},- {110, 90, {.5f, .5f, .5f}, "the region's corner"},- };- for (size_t i = 0; i < sizeof(probes) / sizeof(probes[0]); i++)- for (int c = 0; c < 3; c++)- check(diff(at(img, probes[i].x, probes[i].y)[c], (int)lround(255 * probes[i].c[c])) <= 10,- "redirected drawing: %s is %d,%d,%d", probes[i].what, at(img, probes[i].x, probes[i].y)[0],- at(img, probes[i].x, probes[i].y)[1], at(img, probes[i].x, probes[i].y)[2]);- pixman_region32_fini(&left);- rediwm_scene_renderer_destroy_backdrop(r, b);- wlr_buffer_drop(buf);- puts("PASS: redirected draws land in the quarter-size image with their shape, offset and clip");-}-// The composite shows what is behind its window, so a dimmed window dims it too.-static void gate_backdrop_dim(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator, struct image *img) {- struct wlr_buffer *buf = target_sized(allocator, GW, GH);- struct rediwm_backdrop *b = NULL;- const struct wlr_box region = {120, 100, 400, 200}, box = {168, 148, 304, 104};- const float c[3] = {.2f, .4f, .6f};- const double in[3] = {c[0], c[1], c[2]};- double want[3];- struct wlr_render_pass *pass = begin_fill(r, buf, (float[4]){c[0], c[1], c[2], 1}, NULL);- rediwm_scene_pass_begin_backdrop(pass, region, 4);- fill(pass, (float[4]){c[0], c[1], c[2], 1});- rediwm_scene_pass_end_backdrop(pass, &b, 16);- rediwm_scene_pass_set_dim(pass, .1f, .2f);- rediwm_scene_pass_add_backdrop(pass, &(struct rediwm_pass_shape){.box = box}, box,- &(struct rediwm_pass_backdrop){.backdrop = b, .saturation = 1, .opacity = 1});- rediwm_scene_pass_set_dim(pass, 0, 0);- finish(pass);- read_image(r, buf, img);- graded(in, .1, .2, want);- check_pixel(img, 300, 200, want, "a dimmed backdrop");- check_pixel(img, 20, 20, in, "outside it");- rediwm_scene_renderer_destroy_backdrop(r, b);- wlr_buffer_drop(buf);- puts("PASS: a dimmed backdrop composite is graded like the window it backs");-}-static void backdrop_gates(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator) {- struct image img = {0};- gate_backdrop_blur(r, allocator, &img);- gate_backdrop_properties(r, allocator, &img);- gate_backdrop_front(r, allocator, &img);- gate_backdrop_redirect(r, allocator, &img);- gate_backdrop_dim(r, allocator, &img);- free(img.p);-}int main(void) {wlr_log_init(WLR_ERROR, NULL);struct wl_display *display = wl_display_create();diff --git a/src/scene_pass_test_backdrop.c b/src/scene_pass_test_backdrop.cnew file mode 100644index 0000000..ed7fc96--- /dev/null+++ b/src/scene_pass_test_backdrop.c@@ -0,0 +1,269 @@+// Stage 5b backdrop gates: blur, properties, the front surface, redirection+// and dimming.+// Included by scene_pass_test.c, in its one unit with scene_pass.c.++// ---- Stage 5b: backdrops ------------------------------------------------------------+static struct wlr_buffer *target_format(struct wlr_allocator *allocator, int w, int h, uint32_t fmt) {+ uint64_t modifier = DRM_FORMAT_MOD_INVALID;+ struct wlr_drm_format format = {.format = fmt, .len = 1, .capacity = 1, .modifiers = &modifier};+ struct wlr_buffer *b = wlr_allocator_create_buffer(allocator, w, h, &format);+ assert(b);+ return b;+}+static const float BANDS[5][4] = {{1, 0, 0, 1}, {0, .8f, 0, 1}, {0, 0, 1, 1}, {1, 1, 1, 1}, {.1f, .1f, .1f, 1}};+static void bands(struct wlr_render_pass *pass, int width, int height) {+ for (int x = 0, i = 0; x < width; x += 40, i++)+ wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){+ .box = {x, 0, 40, height}, .color = {BANDS[i % 5][0], BANDS[i % 5][1], BANDS[i % 5][2], 1}});+}+static void fill(struct wlr_render_pass *pass, const float color[4]) {+ wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){+ .box = {0, 0, GW, GH}, .color = {color[0], color[1], color[2], color[3]}});+}+// Vertical bands 40 px wide, then a backdrop of `region` composited over `box`.+static float backdrop_factor = 4;+static struct rediwm_backdrop *draw_backdrop(struct rediwm_scene_renderer *r, struct wlr_buffer *buf,+ struct rediwm_backdrop *b, struct wlr_box region, struct wlr_box box, float sigma,+ const struct rediwm_pass_backdrop *opts, const pixman_region32_t *clip) {+ struct wlr_render_pass *pass = begin_fill(r, buf, BG, NULL);+ bands(pass, buf->width, buf->height);+ // What lies below the effect, drawn again into the quarter-size backdrop.+ rediwm_scene_pass_begin_backdrop(pass, region, backdrop_factor);+ bands(pass, buf->width, buf->height);+ rediwm_scene_pass_end_backdrop(pass, &b, sigma);+ struct rediwm_pass_backdrop o = opts ? *opts : (struct rediwm_pass_backdrop){.saturation = 1, .opacity = 1};+ o.backdrop = b;+ o.clip = clip;+ rediwm_scene_pass_add_backdrop(pass, &(struct rediwm_pass_shape){.box = box}, box, &o);+ finish(pass);+ return b;+}+// The exact full-resolution Gaussian of the bands, clamped at the buffer edge, as+// the reference the quarter-size blur approximates.+static double band_channel(int x, int width, int c) {+ x = x < 0 ? 0 : x >= width ? width - 1 : x;+ return BANDS[(x / 40) % 5][c];+}+static void gate_backdrop_blur(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator, struct image *img) {+ const struct wlr_box region = {100, 100, 400, 200}, box = {148, 148, 304, 104};+ int worst[4] = {0};+ for (int f = 0; f < 4; f++) {+ // ARGB and XRGB buffers, reduced by 4 (scale 1) and by 6 (glass at 1.5).+ backdrop_factor = f < 2 ? 4 : 6;+ // Sigma in reduced pixels as glass has it (22 logical px over a quarter of+ // the logical size): the same at every scale.+ const float sigma = 4 * backdrop_factor;+ struct wlr_buffer *buf = target_format(allocator, GW, GH, f % 2 ? DRM_FORMAT_XRGB8888 : DRM_FORMAT_ARGB8888);+ struct rediwm_backdrop *b = draw_backdrop(r, buf, NULL, region, box, sigma, NULL, NULL);+ read_image(r, buf, img);+ for (int x = box.x; x < box.x + box.width; x++) {+ double want[3] = {0};+ double weight = 0;+ for (int k = -96; k <= 96; k++) {+ const double w = exp(-.5 * k * k / (sigma * sigma));+ // Clamped to the copied region, as the blur's limits are.+ int sx = x + k;+ sx = sx < region.x ? region.x : sx >= region.x + region.width ? region.x + region.width - 1 : sx;+ for (int c = 0; c < 3; c++) want[c] += w * band_channel(sx, GW, c);+ weight += w;+ }+ for (int y = box.y; y < box.y + box.height; y += 13)+ for (int c = 0; c < 3; c++) {+ const int d = diff(at(img, x, y)[c], (int)lround(255 * want[c] / weight));+ if (d > worst[f]) worst[f] = d;+ }+ }+ rediwm_scene_renderer_destroy_backdrop(r, b);+ wlr_buffer_drop(buf);+ }+ // Reduced by 4 the bands' edges fall on whole pixels of the image; by 6 they do+ // not, and an edge drawn at reduced size moves by up to half a pixel (3 device+ // pixels), which on a blurred step of sigma 24 is 255 / (sqrt(2 pi) 24) levels+ // per pixel: 13. glass.c drew its reduced capture the same way.+ for (int f = 0; f < 4; f++)+ check(worst[f] <= (f < 2 ? 2 : 15), "the reduced blur differs from the exact Gaussian by %d levels (case %d)", worst[f], f);+ backdrop_factor = 4;+ printf("PASS: backdrop blur against the exact Gaussian: %d levels reduced by 4, %d by 6 (edge quantisation), ARGB and XRGB\n",+ worst[0] > worst[1] ? worst[0] : worst[1], worst[2] > worst[3] ? worst[2] : worst[3]);+}+static void gate_backdrop_properties(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator, struct image *img) {+ struct wlr_buffer *buf = target_sized(allocator, GW, GH);+ struct rediwm_backdrop *b = NULL;+ // A red/blue step at x = 320 blurs to an even mix there, monotonic across,+ // unchanged far from it.+ struct wlr_render_pass *pass = begin_fill(r, buf, (float[4]){1, 0, 0, 1}, NULL);+ wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){.box = {320, 0, 320, GH}, .color = {0, 0, 1, 1}});+ const struct wlr_box region = {120, 100, 400, 200}, box = {168, 148, 304, 104};+ rediwm_scene_pass_begin_backdrop(pass, region, 4);+ fill(pass, (float[4]){1, 0, 0, 1});+ wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){.box = {320, 0, 320, GH}, .color = {0, 0, 1, 1}});+ rediwm_scene_pass_end_backdrop(pass, &b, 16);+ rediwm_scene_pass_add_backdrop(pass, &(struct rediwm_pass_shape){.box = box}, box,+ &(struct rediwm_pass_backdrop){.backdrop = b, .saturation = 1, .opacity = 1});+ finish(pass);+ read_image(r, buf, img);+ const int y = 200;+ check(diff(at(img, 320, y)[0], 128) <= 12 && diff(at(img, 320, y)[2], 128) <= 12,+ "the step does not blur to an even mix (%d, %d)", at(img, 320, y)[0], at(img, 320, y)[2]);+ for (int x = box.x + 1; x < box.x + box.width; x++)+ check(at(img, x, y)[0] <= at(img, x - 1, y)[0] + 1, "the blurred step is not monotonic at %d", x);+ check(at(img, box.x, y)[0] >= 250 && at(img, box.x + box.width - 1, y)[2] >= 250,+ "the blur reaches too far (%d, %d)", at(img, box.x, y)[0], at(img, box.x + box.width - 1, y)[2]);+ // At the buffer edge the blur replicates it: no black comes in.+ pass = begin_fill(r, buf, (float[4]){0, 1, 0, 1}, NULL);+ const struct wlr_box edge_region = {-60, 100, 200, 200}, edge_box = {0, 148, 92, 104};+ rediwm_scene_pass_begin_backdrop(pass, edge_region, 4);+ fill(pass, (float[4]){0, 1, 0, 1});+ rediwm_scene_pass_end_backdrop(pass, &b, 16);+ rediwm_scene_pass_add_backdrop(pass, &(struct rediwm_pass_shape){.box = edge_box}, edge_box,+ &(struct rediwm_pass_backdrop){.backdrop = b, .saturation = 1, .opacity = 1});+ finish(pass);+ read_image(r, buf, img);+ for (int x = 0; x < 92; x++)+ check(at(img, x, 200)[1] >= 253 && at(img, x, 200)[0] <= 2, "the buffer edge darkens the blur at %d", x);+ // Saturation as glass.c applies it, on a uniform colour the blur keeps.+ const float c[3] = {.2f, .4f, .6f};+ pass = begin_fill(r, buf, (float[4]){c[0], c[1], c[2], 1}, NULL);+ rediwm_scene_pass_begin_backdrop(pass, region, 4);+ fill(pass, (float[4]){c[0], c[1], c[2], 1});+ rediwm_scene_pass_end_backdrop(pass, &b, 16);+ rediwm_scene_pass_add_backdrop(pass, &(struct rediwm_pass_shape){.box = box}, box,+ &(struct rediwm_pass_backdrop){.backdrop = b, .saturation = 1.5f, .opacity = 1});+ finish(pass);+ read_image(r, buf, img);+ const double l = .213 * c[0] + .715 * c[1] + .072 * c[2];+ for (int i = 0; i < 3; i++) {+ const int want = (int)lround(255 * (l + (c[i] - l) * 1.5));+ check(diff(at(img, 300, 200)[i], want) <= 2, "saturation: channel %d is %d, not %d", i, at(img, 300, 200)[i], want);+ }+ rediwm_scene_renderer_destroy_backdrop(r, b);+ wlr_buffer_drop(buf);+ puts("PASS: a blurred step mixes evenly and monotonically, the buffer edge replicates, saturation matches glass");+}+static void gate_backdrop_front(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator, struct image *img) {+ struct wlr_buffer *buf = target_sized(allocator, GW, GH);+ uint32_t half[16 * 16];+ for (int i = 0; i < 16 * 16; i++) half[i] = 0x80000000; // alpha 128, premultiplied black+ struct wlr_texture *front = wlr_texture_from_pixels(r->renderer, DRM_FORMAT_ARGB8888, 16 * 4, 16, 16, half);+ assert(front);+ struct rediwm_backdrop *b = NULL;+ const struct wlr_box region = {100, 100, 200, 200}, box = {150, 150, 100, 100};+ // On a transparent buffer the composite's alpha is what it adds to the front.+ struct wlr_render_pass *pass = begin_fill(r, buf, (float[4]){1, 1, 1, 1}, NULL);+ rediwm_scene_pass_begin_backdrop(pass, region, 4);+ fill(pass, (float[4]){1, 1, 1, 1});+ rediwm_scene_pass_end_backdrop(pass, &b, 8);+ wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){.box = {0, 0, GW, GH},+ .color = {0, 0, 0, 0}, .blend_mode = WLR_RENDER_BLEND_MODE_NONE});+ rediwm_scene_pass_add_backdrop(pass, &(struct rediwm_pass_shape){.box = box}, box,+ &(struct rediwm_pass_backdrop){.backdrop = b, .saturation = 1, .opacity = .5f,+ .front = front, .front_box = box, .front_opacity = .5f});+ finish(pass);+ read_image(r, buf, img);+ const double f = 128 / 255.0 * .5, want = (.5 - f) / (1 - f);+ check(diff(at(img, 200, 200)[3], (int)lround(255 * want)) <= 1, "front alpha: %d, not %d",+ at(img, 200, 200)[3], (int)lround(255 * want));+ // An opaque front leaves nothing for the effect to add.+ pass = begin_fill(r, buf, (float[4]){1, 1, 1, 1}, NULL);+ rediwm_scene_pass_begin_backdrop(pass, region, 4);+ fill(pass, (float[4]){1, 1, 1, 1});+ rediwm_scene_pass_end_backdrop(pass, &b, 8);+ wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){.box = {0, 0, GW, GH},+ .color = {0, 0, 0, 0}, .blend_mode = WLR_RENDER_BLEND_MODE_NONE});+ rediwm_scene_pass_add_backdrop(pass, &(struct rediwm_pass_shape){.box = box}, box,+ &(struct rediwm_pass_backdrop){.backdrop = b, .saturation = 1, .opacity = 1,+ .front = front, .front_box = box, .front_opacity = 2.f});+ finish(pass);+ read_image(r, buf, img);+ check(at(img, 200, 200)[3] == 0, "an opaque front still gets a backdrop (%d)", at(img, 200, 200)[3]);+ // A clipped composite equals that part of a full one: the copy and the blur+ // ignore the clip.+ struct image full = {0};+ b = draw_backdrop(r, buf, b, (struct wlr_box){100, 100, 400, 200}, (struct wlr_box){148, 148, 304, 104}, 16, NULL, NULL);+ read_image(r, buf, &full);+ pixman_region32_t clip;+ pixman_region32_init_rect(&clip, 148, 148, 150, 104);+ b = draw_backdrop(r, buf, b, (struct wlr_box){100, 100, 400, 200}, (struct wlr_box){148, 148, 304, 104}, 16, NULL, &clip);+ read_image(r, buf, img);+ for (int y = 148; y < 252; y++) for (int x = 148; x < 298; x++) for (int c = 0; c < 4; c++)+ check(at(img, x, y)[c] == at(&full, x, y)[c], "a clipped composite differs at %d,%d", x, y);+ pixman_region32_fini(&clip);+ free(full.p);+ wlr_texture_destroy(front);+ rediwm_scene_renderer_destroy_backdrop(r, b);+ wlr_buffer_drop(buf);+ puts("PASS: the backdrop makes up the front's alpha to the coverage; a clipped composite equals a full one");+}+// The redirect moves boxes, shape origins and scissors into the quarter-size+// image: a rounded piece and a clipped rect land where they belong in it.+static void gate_backdrop_redirect(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator, struct image *img) {+ struct wlr_buffer *buf = target_sized(allocator, GW, GH);+ struct rediwm_backdrop *b = NULL;+ const struct wlr_box region = {100, 80, 400, 240};+ const float gray[4] = {.5f, .5f, .5f, 1};+ struct wlr_render_pass *pass = begin_fill(r, buf, BG, NULL);+ rediwm_scene_pass_begin_backdrop(pass, region, 4);+ fill(pass, gray);+ const struct rediwm_pass_shape shape = {.box = {148, 120, 200, 120}, .radii = {40, 40, 40, 40}};+ rediwm_scene_pass_add_rect_shaped(pass, &(struct wlr_render_rect_options){+ .box = shape.box, .color = {1, 0, 0, 1}}, &shape);+ pixman_region32_t left;+ pixman_region32_init_rect(&left, 380, 200, 32, 60);+ wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){+ .box = {380, 200, 64, 60}, .color = {0, 0, 1, 1}, .clip = &left});+ rediwm_scene_pass_end_backdrop(pass, &b, .5f);+ rediwm_scene_pass_add_backdrop(pass, &(struct rediwm_pass_shape){.box = region}, region,+ &(struct rediwm_pass_backdrop){.backdrop = b, .saturation = 1, .opacity = 1});+ finish(pass);+ read_image(r, buf, img);+ struct { int x, y; float c[3]; const char *what; } probes[] = {+ {248, 180, {1, 0, 0}, "inside the rounded piece"},+ {152, 124, {.5f, .5f, .5f}, "a corner the arc cuts away"},+ {392, 230, {0, 0, 1}, "the clipped rect"},+ {432, 230, {.5f, .5f, .5f}, "outside the clip"},+ {110, 90, {.5f, .5f, .5f}, "the region's corner"},+ };+ for (size_t i = 0; i < sizeof(probes) / sizeof(probes[0]); i++)+ for (int c = 0; c < 3; c++)+ check(diff(at(img, probes[i].x, probes[i].y)[c], (int)lround(255 * probes[i].c[c])) <= 10,+ "redirected drawing: %s is %d,%d,%d", probes[i].what, at(img, probes[i].x, probes[i].y)[0],+ at(img, probes[i].x, probes[i].y)[1], at(img, probes[i].x, probes[i].y)[2]);+ pixman_region32_fini(&left);+ rediwm_scene_renderer_destroy_backdrop(r, b);+ wlr_buffer_drop(buf);+ puts("PASS: redirected draws land in the quarter-size image with their shape, offset and clip");+}+// The composite shows what is behind its window, so a dimmed window dims it too.+static void gate_backdrop_dim(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator, struct image *img) {+ struct wlr_buffer *buf = target_sized(allocator, GW, GH);+ struct rediwm_backdrop *b = NULL;+ const struct wlr_box region = {120, 100, 400, 200}, box = {168, 148, 304, 104};+ const float c[3] = {.2f, .4f, .6f};+ const double in[3] = {c[0], c[1], c[2]};+ double want[3];+ struct wlr_render_pass *pass = begin_fill(r, buf, (float[4]){c[0], c[1], c[2], 1}, NULL);+ rediwm_scene_pass_begin_backdrop(pass, region, 4);+ fill(pass, (float[4]){c[0], c[1], c[2], 1});+ rediwm_scene_pass_end_backdrop(pass, &b, 16);+ rediwm_scene_pass_set_dim(pass, .1f, .2f);+ rediwm_scene_pass_add_backdrop(pass, &(struct rediwm_pass_shape){.box = box}, box,+ &(struct rediwm_pass_backdrop){.backdrop = b, .saturation = 1, .opacity = 1});+ rediwm_scene_pass_set_dim(pass, 0, 0);+ finish(pass);+ read_image(r, buf, img);+ graded(in, .1, .2, want);+ check_pixel(img, 300, 200, want, "a dimmed backdrop");+ check_pixel(img, 20, 20, in, "outside it");+ rediwm_scene_renderer_destroy_backdrop(r, b);+ wlr_buffer_drop(buf);+ puts("PASS: a dimmed backdrop composite is graded like the window it backs");+}+static void backdrop_gates(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator) {+ struct image img = {0};+ gate_backdrop_blur(r, allocator, &img);+ gate_backdrop_properties(r, allocator, &img);+ gate_backdrop_front(r, allocator, &img);+ gate_backdrop_redirect(r, allocator, &img);+ gate_backdrop_dim(r, allocator, &img);+ free(img.p);+}diff --git a/src/scene_pass_test_parity.c b/src/scene_pass_test_parity.cnew file mode 100644index 0000000..2b11ae6--- /dev/null+++ b/src/scene_pass_test_parity.c@@ -0,0 +1,386 @@+// Stage 3 gates: parity with wlroots, failing closed, damage, GL state,+// external and YUV textures, and synchronization.+// Included by scene_pass_test.c, in its one unit with scene_pass.c.++static void parity(struct rediwm_scene_renderer *r, struct wlr_buffer *actual,+ struct wlr_buffer *expected, bool shaped) {+ const struct rediwm_pass_shape cover = {.box = {-50, -50, WIDTH + 100, HEIGHT + 100}};+ const struct rediwm_pass_shape *shape = shaped ? &cover : NULL;+ pixman_region32_t clip;+ pixman_region32_init_rect(&clip, -10, 8, 65, 31);+ pixman_region32_union_rect(&clip, &clip, 71, 43, 81, 80);+ int worst = 0, cases = 0;+ for (int opaque = 0; opaque < 2; opaque++) {+ struct wlr_texture *t = texture_create(r->renderer, opaque);+ for (int tr = 0; tr < 8; tr++) for (int filter = 0; filter < 2; filter++)+ for (int crop = 0; crop < 2; crop++) for (int partial = 0; partial < 2; partial++) {+ float alpha = partial ? .61 : 1;+ struct wlr_render_pass *pass = wlr_renderer_begin_buffer_pass(r->renderer, expected, NULL);+ assert(pass);+ clear(pass);+ paint(pass, t, tr, filter, crop, alpha, partial ? &clip : NULL, NULL);+ assert(wlr_render_pass_submit(pass));+ pass = rediwm_scene_pass_begin(r, actual, NULL);+ assert(pass);+ clear(pass);+ paint(pass, t, tr, filter, crop, alpha, partial ? &clip : NULL, shape);+ assert(wlr_render_pass_submit(pass));+ int d = compare(r, actual, expected);+ if (d > 1) {+ fprintf(stderr, "parity: shaped=%d opaque=%d transform=%d filter=%d crop=%d partial=%d diff=%d\n",+ shaped, opaque, tr, filter, crop, partial, d);+ abort();+ }+ if (d > worst) worst = d;+ cases++;+ }+ wlr_texture_destroy(t);+ }+ pixman_region32_fini(&clip);+ printf("PASS: %d texture/transform/crop/filter/alpha/damage cases%s (max difference %d)\n", cases,+ shaped ? " through the shaped programs" : "", worst);+}+// The shaped programs are built with the renderer (stage 5, D4). A bad shape is+// the caller's bug: it must fail the pass at submit, not draw something else,+// and the next pass must work.+static void shapes_fail_closed(struct rediwm_scene_renderer *r, struct wlr_buffer *buffer) {+ assert(rediwm_scene_renderer_enable_shapes(r));+ const struct rediwm_pass_shape shape = {.box = {10, 10, 50, 50}, .radii = {5, -1, 5, 5}};+ struct wlr_render_pass *pass = rediwm_scene_pass_begin(r, buffer, NULL);+ assert(pass);+ rediwm_scene_pass_add_rect_shaped(pass, &(struct wlr_render_rect_options){+ .box = {10, 10, 50, 50}, .color = {1, 0, 0, 1}}, &shape);+ assert(!wlr_render_pass_submit(pass));+ pass = rediwm_scene_pass_begin(r, buffer, NULL);+ assert(pass && wlr_render_pass_submit(pass));+ puts("PASS: shapes are built with the renderer; a bad shape fails the pass and the next pass works");+}+static void damage(struct rediwm_scene_renderer *r, struct wlr_buffer *actual,+ struct wlr_buffer *expected) {+ struct wlr_render_pass *pass = rediwm_scene_pass_begin(r, actual, NULL);+ assert(pass);+ clear(pass);+ assert(wlr_render_pass_submit(pass));+ uint8_t before[WIDTH * HEIGHT * 4], after[sizeof(before)];+ read_pixels(r, actual, before);+ pixman_region32_t clip;+ pixman_region32_init_rect(&clip, 11, 13, 23, 17);+ pixman_region32_union_rect(&clip, &clip, 71, 60, 13, 19);+ struct wlr_texture *t = texture_create(r->renderer, false);+ pass = rediwm_scene_pass_begin(r, actual, NULL);+ assert(pass);+ paint(pass, t, 3, 0, 1, .73, &clip, NULL);+ assert(wlr_render_pass_submit(pass));+ read_pixels(r, actual, after);+ int changed = 0;+ for (int y = 0; y < HEIGHT; y++) for (int x = 0; x < WIDTH; x++) {+ bool same = memcmp(&before[4 * (y * WIDTH + x)], &after[4 * (y * WIDTH + x)], 4) == 0;+ if (!pixman_region32_contains_point(&clip, x, y, NULL)) assert(same);+ else changed += !same;+ }+ assert(changed > 0);+ // An empty damage region must not paint anything, even after a prior clip.+ pixman_region32_clear(&clip);+ pass = rediwm_scene_pass_begin(r, actual, NULL);+ assert(pass);+ paint(pass, t, 1, 1, 0, 1, &clip, NULL);+ assert(wlr_render_pass_submit(pass));+ read_pixels(r, actual, before);+ assert(memcmp(before, after, sizeof(before)) == 0);+ // NONE must replace rather than alpha blend, on both texture and rect paths.+ for (int owned = 0; owned < 2; owned++) {+ pass = owned ? rediwm_scene_pass_begin(r, actual, NULL) :+ wlr_renderer_begin_buffer_pass(r->renderer, expected, NULL);+ assert(pass);+ clear(pass);+ wlr_render_pass_add_texture(pass, &(struct wlr_render_texture_options){+ .texture = t, .dst_box = {17, 19, TW, TH}, .blend_mode = WLR_RENDER_BLEND_MODE_NONE,+ });+ wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){+ .box = {71, 8, 11, 13}, .color = {.1, .2, .1, .3}, .blend_mode = WLR_RENDER_BLEND_MODE_NONE,+ });+ assert(wlr_render_pass_submit(pass));+ }+ assert(compare(r, actual, expected) <= 1);+ wlr_texture_destroy(t);+ pixman_region32_fini(&clip);+ puts("PASS: disjoint and empty damage preserve untouched pixels; disabled blending matches wlroots");+}+static void state_restoration(struct rediwm_scene_renderer *r, struct wlr_buffer *buffer) {+ struct gl_state original;+ assert(enter(r, &original));+ GLuint vbo, textures[2], caller_vao = 0;+ if (r->has_vao) {+ r->gen_vertex_arrays(1, &caller_vao);+ r->bind_vertex_array(caller_vao);+ }+ glGenBuffers(1, &vbo);+ glBindBuffer(GL_ARRAY_BUFFER, vbo);+ glBufferData(GL_ARRAY_BUFFER, 128, NULL, GL_STATIC_DRAW);+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 16, (void *)4);+ glEnableVertexAttribArray(0);+ glVertexAttribPointer(1, 2, GL_SHORT, GL_TRUE, 8, (void *)16);+ glDisableVertexAttribArray(1);+ glGenTextures(2, textures);+ glActiveTexture(GL_TEXTURE0);+ glBindTexture(GL_TEXTURE_2D, textures[0]);+ glActiveTexture(GL_TEXTURE3);+ glBindTexture(GL_TEXTURE_2D, textures[1]);+ glViewport(7, 9, 53, 41);+ glScissor(2, 3, 19, 23);+ glEnable(GL_SCISSOR_TEST);+ glDisable(GL_BLEND);+ glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA);+ glBlendEquationSeparate(GL_FUNC_REVERSE_SUBTRACT, GL_FUNC_SUBTRACT);+ struct gl_state before = {0}, after = {0};+ assert(enter(r, &before));+ leave(r, &before);+ struct wlr_texture *t = texture_create(r->renderer, false);+ struct wlr_render_pass *pass = rediwm_scene_pass_begin(r, buffer, NULL);+ assert(pass);+ assert(!rediwm_scene_pass_begin(r, buffer, NULL)); // no nested owned passes+ clear(pass);+ paint(pass, t, 1, 0, 1, .5, NULL, NULL);+ assert(wlr_render_pass_submit(pass));+ assert(enter(r, &after));+ leave(r, &after);+ assert(memcmp(&before, &after, sizeof(before)) == 0);+ GLint value;+ glGetVertexAttribiv(0, GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING, &value);+ assert(value == (GLint)vbo);+ glGetVertexAttribiv(0, GL_VERTEX_ATTRIB_ARRAY_SIZE, &value);+ assert(value == 3);+ glGetVertexAttribiv(0, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &value);+ assert(value == 16);+ glGetVertexAttribiv(1, GL_VERTEX_ATTRIB_ARRAY_SIZE, &value);+ assert(value == 2);+ glGetVertexAttribiv(1, GL_VERTEX_ATTRIB_ARRAY_TYPE, &value);+ assert(value == GL_SHORT);+ glGetVertexAttribiv(1, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &value);+ assert(!value);+ assert(glGetError() == GL_NO_ERROR);+ wlr_texture_destroy(t);+ glDeleteBuffers(1, &vbo);+ glDeleteTextures(2, textures);+ if (caller_vao) r->delete_vertex_arrays(1, &caller_vao);+ leave(r, &original);+ puts("PASS: GL bindings, attributes, blend/scissor/viewport and EGL context restored");+}+static void external_texture(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator,+ struct wlr_buffer *actual, struct wlr_buffer *expected) {+ if (!r->has_external) { puts("SKIP: external-OES unavailable"); return; }+ struct wlr_buffer *source = target(allocator);+ struct wlr_texture *pattern = texture_create(r->renderer, false);+ struct wlr_render_pass *base = wlr_renderer_begin_buffer_pass(r->renderer, source, NULL);+ assert(base);+ clear(base);+ paint(base, pattern, 0, 1, 0, 1, NULL, NULL);+ assert(wlr_render_pass_submit(base));+ wlr_texture_destroy(pattern);+ struct wlr_texture *reference = wlr_texture_from_buffer(r->renderer, source);+ assert(reference);+ struct wlr_dmabuf_attributes dma;+ assert(wlr_buffer_get_dmabuf(source, &dma) && dma.n_planes == 1);+ EGLint attrs[32] = {EGL_WIDTH, dma.width, EGL_HEIGHT, dma.height,+ EGL_LINUX_DRM_FOURCC_EXT, dma.format, EGL_DMA_BUF_PLANE0_FD_EXT, dma.fd[0],+ EGL_DMA_BUF_PLANE0_OFFSET_EXT, dma.offset[0], EGL_DMA_BUF_PLANE0_PITCH_EXT, dma.stride[0]};+ int n = 12;+ if (dma.modifier != DRM_FORMAT_MOD_INVALID) {+ attrs[n++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT;+ attrs[n++] = dma.modifier & UINT32_MAX;+ attrs[n++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT;+ attrs[n++] = dma.modifier >> 32;+ }+ attrs[n] = EGL_NONE;+ PFNEGLCREATEIMAGEKHRPROC create_image = (PFNEGLCREATEIMAGEKHRPROC)eglGetProcAddress("eglCreateImageKHR");+ PFNEGLDESTROYIMAGEKHRPROC destroy_image = (PFNEGLDESTROYIMAGEKHRPROC)eglGetProcAddress("eglDestroyImageKHR");+ PFNGLEGLIMAGETARGETTEXTURE2DOESPROC bind_image =+ (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)eglGetProcAddress("glEGLImageTargetTexture2DOES");+ assert(create_image && destroy_image && bind_image);+ struct gl_state state;+ assert(enter(r, &state));+ EGLImageKHR image = create_image(state.display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, attrs);+ assert(image != EGL_NO_IMAGE_KHR);+ struct wlr_gles2_texture_attribs a = {.target = GL_TEXTURE_EXTERNAL_OES, .has_alpha = true};+ glGenTextures(1, &a.tex);+ glBindTexture(a.target, a.tex);+ bind_image(a.target, image);+ glTexParameteri(a.target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);+ glTexParameteri(a.target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);+ leave(r, &state);+ for (int tr = 0; tr < 8; tr++) for (int filter = 0; filter < 2; filter++) {+ float alpha = .73;+ struct wlr_render_texture_options o = {+ .texture = reference, .src_box = {4.25, 8.5, 107.5, 71.25},+ .dst_box = {7, 9, 111, 79}, .transform = tr, .filter_mode = filter, .alpha = &alpha,+ };+ base = wlr_renderer_begin_buffer_pass(r->renderer, expected, NULL);+ assert(base);+ clear(base);+ wlr_render_pass_add_texture(base, &o);+ assert(wlr_render_pass_submit(base));+ base = rediwm_scene_pass_begin(r, actual, NULL);+ assert(base);+ clear(base);+ struct pass *p = wl_container_of(base, p, base);+ // Force a real external EGLImage through the same draw routine; this+ // driver's normal import of an RGB dmabuf chooses a 2D target.+ texture_draw(p, &o, a, NULL);+ assert(wlr_render_pass_submit(base));+ assert(compare(r, actual, expected) <= 1);+ // The shaped external program with a cover-everything shape is the same draw.+ const struct rediwm_pass_shape cover = {.box = {-50, -50, WIDTH + 100, HEIGHT + 100}};+ base = rediwm_scene_pass_begin(r, actual, NULL);+ assert(base);+ clear(base);+ p = wl_container_of(base, p, base);+ texture_draw(p, &o, a, &cover);+ assert(wlr_render_pass_submit(base));+ assert(compare(r, actual, expected) <= 1);+ // And through a rounded, bordered shape the external texture equals the 2D+ // one of the same pixels: the clip does not depend on the sampler.+ const struct rediwm_pass_shape round = {.box = {12, 14, 100, 70},+ .radii = {18, 9, 25.5f, 3}, .border = 2, .border_color = {.5f, .1f, .1f, .5f}};+ base = rediwm_scene_pass_begin(r, expected, NULL);+ assert(base);+ clear(base);+ rediwm_scene_pass_add_texture_shaped(base, &o, &round);+ assert(wlr_render_pass_submit(base));+ base = rediwm_scene_pass_begin(r, actual, NULL);+ assert(base);+ clear(base);+ p = wl_container_of(base, p, base);+ texture_draw(p, &o, a, &round);+ assert(wlr_render_pass_submit(base));+ assert(compare(r, actual, expected) <= 1);+ // Two blank images would also match: the corner must be cut, the middle drawn.+ uint8_t shown[WIDTH * HEIGHT * 4];+ read_pixels(r, actual, shown);+ assert(memcmp(&shown[4 * (15 * WIDTH + 13)], &shown[0], 4) == 0);+ assert(memcmp(&shown[4 * (50 * WIDTH + 60)], &shown[0], 4) != 0);+ }+ assert(enter(r, &state));+ glDeleteTextures(1, &a.tex);+ destroy_image(state.display, image);+ leave(r, &state);+ wlr_texture_destroy(reference);+ wlr_buffer_drop(source);+ puts("PASS: external-OES EGLImage matches wlroots across all transforms and filters, plain and shaped");+}+// Driver-converted YUV. A linear NV12 dmabuf is imported by wlroots exactly as a+// video client's would be. Mesa reports it external-only, so both passes must+// sample it through samplerExternalOES and leave the YUV->RGB conversion to the+// driver. Mesa's gbm cannot allocate NV12, so a linear R8 buffer 1.5x as tall is+// described as two planes of one dmabuf (chroma on a page boundary), as a video+// client would send it. This covers the external program with real YUV; it does+// not cover other drivers' conversion or any tiled/compressed modifier.+static void external_yuv(struct rediwm_scene_renderer *r, struct wlr_buffer *actual,+ struct wlr_buffer *expected) {+ enum { YW = 64, YH = 64 };+ if (!r->has_external) { puts("SKIP: external-OES unavailable"); return; }+ int drm_fd = wlr_renderer_get_drm_fd(r->renderer);+ struct gbm_device *gbm = drm_fd >= 0 ? gbm_create_device(drm_fd) : NULL;+ struct gbm_bo *bo = gbm ? gbm_bo_create(gbm, YW, YH * 3 / 2, GBM_FORMAT_R8, GBM_BO_USE_LINEAR) : NULL;+ if (!bo || gbm_bo_get_modifier(bo) != DRM_FORMAT_MOD_LINEAR) {+ puts("SKIP: gbm cannot allocate a linear buffer for NV12");+ if (bo) gbm_bo_destroy(bo);+ if (gbm) gbm_device_destroy(gbm);+ return;+ }+ uint32_t stride = gbm_bo_get_stride(bo);+ struct wlr_dmabuf_attributes dma = {.width = YW, .height = YH, .format = DRM_FORMAT_NV12,+ .modifier = DRM_FORMAT_MOD_LINEAR, .n_planes = 2,+ .offset = {0, stride * YH}, .stride = {stride, stride}};+ assert(dma.offset[1] % 4096 == 0);+ for (int i = 0; i < 2; i++) {+ dma.fd[i] = gbm_bo_get_fd(bo);+ assert(dma.fd[i] >= 0);+ }+ size_t length = dma.offset[1] + (size_t)dma.stride[1] * (YH / 2);+ uint8_t *map = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, dma.fd[0], 0);+ assert(map != MAP_FAILED);+ struct dma_buf_sync sync = {.flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_WRITE};+ ioctl(dma.fd[0], DMA_BUF_IOCTL_SYNC, &sync);+ for (int y = 0; y < YH; y++) for (int x = 0; x < YW; x++)+ map[dma.offset[0] + y * dma.stride[0] + x] = 16 + (x * 3 + y * 5) % 220;+ for (int y = 0; y < YH / 2; y++) for (int x = 0; x < YW / 2; x++) {+ uint8_t *uv = &map[dma.offset[1] + y * dma.stride[1] + 2 * x];+ uv[0] = 32 + (x * 11 + y * 3) % 192;+ uv[1] = 32 + (x * 5 + y * 13) % 192;+ }+ sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_WRITE;+ ioctl(dma.fd[0], DMA_BUF_IOCTL_SYNC, &sync);+ munmap(map, length);+ struct wlr_texture *texture = wlr_texture_from_dmabuf(r->renderer, &dma);+ struct wlr_gles2_texture_attribs a = {0};+ if (texture) wlr_gles2_texture_get_attribs(texture, &a);+ if (!texture || a.target != GL_TEXTURE_EXTERNAL_OES) {+ puts(texture ? "SKIP: driver imports NV12 as GL_TEXTURE_2D" : "SKIP: renderer cannot import linear NV12");+ } else {+ assert(!a.has_alpha);+ float alpha = .81f;+ for (int tr = 0; tr < 8; tr++) for (int filter = 0; filter < 2; filter++) {+ struct wlr_render_texture_options o = {+ .texture = texture, .src_box = {2.25, 3.5, 40.5, 30.25},+ .dst_box = {7, 9, 111, 79}, .transform = tr, .filter_mode = filter, .alpha = &alpha,+ };+ struct wlr_render_pass *pass = wlr_renderer_begin_buffer_pass(r->renderer, expected, NULL);+ assert(pass);+ clear(pass);+ wlr_render_pass_add_texture(pass, &o);+ assert(wlr_render_pass_submit(pass));+ pass = rediwm_scene_pass_begin(r, actual, NULL);+ assert(pass);+ clear(pass);+ wlr_render_pass_add_texture(pass, &o);+ assert(wlr_render_pass_submit(pass));+ assert(compare(r, actual, expected) <= 1);+ }+ // Parity with a blank image would prove nothing: the video must have drawn.+ uint8_t pixels[WIDTH * HEIGHT * 4];+ read_pixels(r, actual, pixels);+ int drawn = 0;+ for (size_t i = 0; i < sizeof(pixels); i += 4)+ if (abs(pixels[i] - 28) > 8 || abs(pixels[i + 1] - 59) > 8 || abs(pixels[i + 2] - 94) > 8) drawn++;+ assert(drawn > 4000);+ puts("PASS: external-only NV12 dmabuf matches wlroots across all transforms and filters");+ }+ if (texture) wlr_texture_destroy(texture);+ wlr_dmabuf_attributes_finish(&dma);+ gbm_bo_destroy(bo);+ gbm_device_destroy(gbm);+}+static void synchronization(struct rediwm_scene_renderer *r, struct wlr_buffer *buffer) {+ if (!r->has_sync) { puts("SKIP: renderer timelines unavailable"); return; }+ struct wlr_drm_syncobj_timeline *timeline =+ wlr_drm_syncobj_timeline_create(wlr_renderer_get_drm_fd(r->renderer));+ assert(timeline);+ assert(wlr_drm_syncobj_timeline_signal(timeline, 1));+ struct wlr_texture *t = texture_create(r->renderer, false);+ struct wlr_render_pass *base = rediwm_scene_pass_begin(r, buffer,+ &(struct wlr_buffer_pass_options){.signal_timeline = timeline, .signal_point = 2});+ assert(base);+ clear(base);+ wlr_render_pass_add_texture(base, &(struct wlr_render_texture_options){+ .texture = t, .dst_box = {7, 9, TW, TH}, .wait_timeline = timeline, .wait_point = 1,+ });+ assert(wlr_render_pass_submit(base));+ uint8_t pixels[WIDTH * HEIGHT * 4];+ read_pixels(r, buffer, pixels); // finish the submitted GPU work before querying+ bool signaled = false;+ assert(wlr_drm_syncobj_timeline_check(timeline, 2, DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT, &signaled));+ assert(signaled);+ base = rediwm_scene_pass_begin(r, buffer, NULL);+ assert(base);+ // A point that has never materialized must fail closed, without sampling.+ wlr_render_pass_add_texture(base, &(struct wlr_render_texture_options){+ .texture = t, .wait_timeline = timeline, .wait_point = 99,+ });+ assert(!wlr_render_pass_submit(base));+ // Failure released the destination lock and the renderer's active pass.+ base = rediwm_scene_pass_begin(r, buffer, NULL);+ assert(base && wlr_render_pass_submit(base));+ wlr_texture_destroy(t);+ wlr_drm_syncobj_timeline_unref(timeline);+ puts("PASS: acquire/release timeline points and recovery from unavailable acquire");+}diff --git a/src/scene_pass_test_shading.c b/src/scene_pass_test_shading.cnew file mode 100644index 0000000..3c9adaa--- /dev/null+++ b/src/scene_pass_test_shading.c@@ -0,0 +1,275 @@+// Dimming, the opaque cover, the analytic shadow and partial repaints across a+// clip edge; shaped_gates() runs every shape gate.+// Included by scene_pass_test.c, in its one unit with scene_pass.c.++// ---- Dimming: a window that is not the focus is graded in the pass ----------------+// What rediwm_scene_pass_set_dim documents, worked out here on its own: the colour is+// pulled toward its luminance by the saturation taken away, then everything is scaled+// by the brightness left. On premultiplied values, as the pass has them.+static void graded(const double in[3], double brightness, double saturation, double out[3]) {+ const double l = .213 * in[0] + .715 * in[1] + .072 * in[2];+ for (int i = 0; i < 3; i++) out[i] = (l + (in[i] - l) * (1 - saturation)) * (1 - brightness);+}+static void check_pixel(const struct image *img, int x, int y, const double want[3], const char *what) {+ for (int ch = 0; ch < 3; ch++)+ check(diff(at(img, x, y)[ch], (int)lround(255 * want[ch])) <= 2, "%s: (%d,%d) channel %d is %d, not %ld",+ what, x, y, ch, at(img, x, y)[ch], lround(255 * want[ch]));+}+static void gate_dim(struct rediwm_scene_renderer *r, struct wlr_buffer *buf, struct image *img) {+ const float db = .10f, ds = .20f;+ const float c[4] = {200 / 255.f, 100 / 255.f, 50 / 255.f, 1};+ const double cd[3] = {c[0], c[1], c[2]}, bg[3] = {BG[0], BG[1], BG[2]};+ double want[3];+ // A plain rect, then one after the dim is taken off: the first is graded, the+ // second and what was drawn before are not.+ struct wlr_render_pass *pass = begin_fill(r, buf, BG, NULL);+ rediwm_scene_pass_set_dim(pass, db, ds);+ wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){.box = {20, 20, 40, 40}, .color = {c[0], c[1], c[2], 1}});+ rediwm_scene_pass_set_dim(pass, 0, 0);+ wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){.box = {80, 20, 40, 40}, .color = {c[0], c[1], c[2], 1}});+ finish(pass);+ read_image(r, buf, img);+ graded(cd, db, ds, want);+ check_pixel(img, 40, 40, want, "a dimmed plain rect");+ check_pixel(img, 100, 40, cd, "a rect after the dim is taken off");+ check_pixel(img, 300, 300, bg, "the background before the dim");+ // A dimmed rect through a shape: the interior and the ring are graded, the+ // corner the shape cuts away is the backdrop, whole.+ const struct rediwm_pass_shape ring = {.box = {140, 100, 80, 80}, .radii = {20, 20, 20, 20}, .border = 4,+ .border_color = {.5f, 0, 0, .5f}};+ pass = begin_fill(r, buf, BG, NULL);+ rediwm_scene_pass_set_dim(pass, db, ds);+ piece(pass, &ring, ring.box, c, NULL);+ rediwm_scene_pass_set_dim(pass, 0, 0);+ finish(pass);+ read_image(r, buf, img);+ graded(cd, db, ds, want);+ check_pixel(img, 180, 140, want, "a dimmed shaped interior");+ const double under_ring[3] = {cd[0] * .5 + .5, cd[1] * .5, cd[2] * .5};+ graded(under_ring, db, ds, want);+ check_pixel(img, 141, 140, want, "a dimmed ring");+ check_pixel(img, 141, 101, bg, "the corner a dimmed shape cuts away");+ // A half-transparent texel: graded as premultiplied, then over the backdrop,+ // so it keeps its alpha.+ uint32_t half[16];+ for (int i = 0; i < 16; i++) half[i] = 0x80800000;+ struct wlr_texture *t_half = wlr_texture_from_pixels(r->renderer, DRM_FORMAT_ARGB8888, 16, 4, 4, half);+ check(t_half != NULL, "cannot create the test texture");+ const struct rediwm_pass_shape card = {.box = {240, 100, 80, 80}, .radii = {20, 20, 20, 20}};+ pass = begin_fill(r, buf, BG, NULL);+ rediwm_scene_pass_set_dim(pass, db, ds);+ rediwm_scene_pass_add_texture_shaped(pass, &(struct wlr_render_texture_options){+ .texture = t_half, .dst_box = card.box}, &card);+ rediwm_scene_pass_set_dim(pass, 0, 0);+ finish(pass);+ read_image(r, buf, img);+ const double alpha = 128 / 255., src[3] = {128 / 255., 0, 0};+ graded(src, db, ds, want);+ for (int i = 0; i < 3; i++) want[i] += bg[i] * (1 - alpha);+ check_pixel(img, 280, 140, want, "a dimmed half-transparent texel");+ wlr_texture_destroy(t_half);+ // A shadow is not graded, whatever the pass holds.+ const struct rediwm_pass_shape cast = {.box = {340, 100, 80, 80}, .radii = {20, 20, 20, 20}};+ const struct rediwm_pass_shadow shadow = {.softness = 12, .color = {.6f, 0, 0, .6f}};+ uint8_t plain[3];+ for (int dimmed = 0; dimmed < 2; dimmed++) {+ pass = begin_fill(r, buf, BG, NULL);+ if (dimmed) rediwm_scene_pass_set_dim(pass, db, ds);+ rediwm_scene_pass_add_shadow(pass, &cast, &shadow);+ finish(pass);+ read_image(r, buf, img);+ if (!dimmed) memcpy(plain, at(img, 380, 140), 3);+ else for (int ch = 0; ch < 3; ch++)+ check(diff(at(img, 380, 140)[ch], plain[ch]) <= 1, "a dimmed pass graded a shadow (channel %d)", ch);+ }+ // A pass that ends with the dim still set leaves none for the next one.+ pass = begin_fill(r, buf, BG, NULL);+ rediwm_scene_pass_set_dim(pass, .5f, .5f);+ wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){.box = {20, 20, 40, 40}, .color = {c[0], c[1], c[2], 1}});+ finish(pass);+ pass = begin_fill(r, buf, BG, NULL);+ wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){.box = {20, 20, 40, 40}, .color = {c[0], c[1], c[2], 1}});+ finish(pass);+ read_image(r, buf, img);+ check_pixel(img, 40, 40, cd, "the first rect of a pass after one that kept its dim");+ // A fraction outside 0 to 1 fails the pass, and the next one works.+ const float bad[][2] = {{-.1f, 0}, {0, 1.5f}, {NAN, 0}, {0, INFINITY}};+ for (size_t i = 0; i < COUNT(bad); i++) {+ pass = begin_fill(r, buf, BG, NULL);+ rediwm_scene_pass_set_dim(pass, bad[i][0], bad[i][1]);+ check(!wlr_render_pass_submit(pass), "a dim of (%g, %g) did not fail the pass", bad[i][0], bad[i][1]);+ }+ pass = begin_fill(r, buf, BG, NULL);+ finish(pass);+ puts("PASS: a dimmed pass grades rects, shapes, rings and translucent texels, not shadows, and fails closed");+}++// ---- Gate 5: the opaque cover of the model is fully covered in real pixels -------------+// rediwm_clip_opaque promises its rectangles are inside full coverage; the output+// pass will draw them without blending and trust that. Painting them first with+// blending off, then the whole window over them, must change nothing.+static void gate_opaque_cover(struct rediwm_scene_renderer *r, struct wlr_buffer *buf,+ struct image *img, struct image *ref) {+ for (size_t si = 0; si < COUNT(SCALES); si++) for (size_t zi = 0; zi < COUNT(ZOOMS); zi++) {+ struct window w = make_window(SCALES[si], ZOOMS[zi], 10, 1);+ struct rediwm_pass_shape s = w.shape;+ memcpy(s.border_color, BODY, sizeof(BODY));+ struct wlr_render_pass *pass = begin_fill(r, buf, BG, NULL);+ piece(pass, &s, s.box, BODY, NULL);+ finish(pass);+ read_image(r, buf, ref);+ // Border colour equals the body's here, so the cover may include the ring.+ struct rediwm_snap cover[3];+ int n = rediwm_clip_opaque(&w.clip, cover);+ check(n > 0, "no opaque cover at scale %.2f zoom %.2f", SCALES[si], ZOOMS[zi]);+ pass = begin_fill(r, buf, BG, NULL);+ for (int i = 0; i < n; i++)+ wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){+ .box = box_of(cover[i]), .color = {BODY[0], BODY[1], BODY[2], 1},+ .blend_mode = WLR_RENDER_BLEND_MODE_NONE});+ piece(pass, &s, s.box, BODY, NULL);+ finish(pass);+ read_image(r, buf, img);+ for (int i = 0; i < img->w * img->h * 4; i++)+ check(diff(img->p[i], ref->p[i]) <= 1, "cover pixel differs at scale %.2f zoom %.2f (%d vs %d)",+ SCALES[si], ZOOMS[zi], img->p[i], ref->p[i]);+ }+ puts("PASS: the opaque cover draws without blending and changes nothing, at every scale and zoom");+}++// ---- Gate 6: analytic shadow ------------------------------------------------------------+// The CPU chrome's shadow (chrome.zig: ShadowBuffer.rasterize, shadowCoverage,+// edgeCoverage) at one pixel: smoothstep falloff from the moved shape, cut out of+// the unmoved one with a one-pixel ramp. Every pixel is compared, corners too.+static double cpu_shadow(double px, double py, const struct rediwm_pass_shape *s, double oy, double softness) {+ const double radii[4] = {s->radii[0], s->radii[1], s->radii[2], s->radii[3]};+ const double d = rediwm_sd_rounded_box(px, py, s->box.x, s->box.y + oy, s->box.width, s->box.height, radii);+ const double frame_d = rediwm_sd_rounded_box(px, py, s->box.x, s->box.y, s->box.width, s->box.height, radii);+ double t = 1 - d / softness;+ t = t < 0 ? 0 : t > 1 ? 1 : t;+ double inside = .5 - frame_d;+ inside = inside < 0 ? 0 : inside > 1 ? 1 : inside;+ return t * t * (3 - 2 * t) * (1 - inside);+}+static void gate_shadow(struct rediwm_scene_renderer *r, struct wlr_buffer *buf, struct image *img) {+ const float white[4] = {1, 1, 1, 1};+ // The default theme: radius 10, softness 24, offset 8, at scale 1 and 1.5.+ const float scales[] = {1, 1.5f};+ int worst = 0;+ for (int si = 0; si < 2; si++) {+ const float k = scales[si];+ const float softness = 24 * k, oy = roundf(8 * k), radius = 10 * k;+ const struct rediwm_pass_shape s = {.box = {80, 60, 200, 120}, .radii = {radius, radius, radius, radius}};+ struct wlr_render_pass *pass = begin_fill(r, buf, white, NULL);+ rediwm_scene_pass_add_shadow(pass, &s, &(struct rediwm_pass_shadow){+ .offset_y = oy, .softness = softness, .color = {0, 0, 0, .5f}, .hide_inside = true});+ finish(pass);+ read_image(r, buf, img);+ for (int y = 0; y < img->h; y++)+ for (int x = 0; x < img->w; x++) {+ const int want = (int)lround(255 * (1 - .5 * cpu_shadow(x + .5, y + .5, &s, oy, softness)));+ const int got = at(img, x, y)[0];+ if (diff(got, want) > worst) worst = diff(got, want);+ }+ const int cx = s.box.x + s.box.width / 2, bottom = s.box.y + s.box.height;+ int last = 255;+ for (int dy = (int)oy; dy < 60; dy++) {+ const int got = at(img, cx, bottom + dy)[0];+ check(got >= last || dy == (int)oy, "shadow is not monotonic at %d px (%d after %d)", dy, got, last);+ last = got;+ }+ check(at(img, 10, 10)[0] == 255 && at(img, cx, s.box.y + s.box.height / 2)[0] == 255,+ "shadow reaches outside its footprint or inside the window");+ }+ check(worst <= 1, "shadow differs from the CPU chrome's by %d levels", worst);+ // Not hidden inside: the same draw shows through the window.+ const struct rediwm_pass_shape s = {.box = {80, 60, 200, 120}};+ struct wlr_render_pass *pass = begin_fill(r, buf, white, NULL);+ rediwm_scene_pass_add_shadow(pass, &s, &(struct rediwm_pass_shadow){+ .offset_y = 6, .softness = 24, .color = {0, 0, 0, .5f}});+ finish(pass);+ read_image(r, buf, img);+ check(at(img, s.box.x + s.box.width / 2, s.box.y + s.box.height / 2)[0] < 200,+ "an unhidden shadow does not show under the shape");+ printf("PASS: analytic shadow equals the CPU chrome's at every pixel, corners included (max diff %d), "+ "and stays out of the window\n", worst);+}++// ---- Gate 7: a partial repaint across a clip edge equals a full repaint -----------------+static void draw_scene(struct rediwm_scene_renderer *r, struct wlr_buffer *buf, const struct window *w,+ int variant, const pixman_region32_t *clip) {+ const float bodies[2][3][4] = {+ {{.2f, .3f, .5f, 1}, {.9f, .9f, .9f, 1}, {.3f, .3f, .3f, 1}},+ {{.8f, .2f, .2f, 1}, {.1f, .6f, .2f, 1}, {.6f, .6f, .1f, 1}},+ };+ struct rediwm_pass_shape s = w->shape;+ const float rings[2][4] = {{1, 1, 1, 1}, {1, 0, 0, 1}};+ memcpy(s.border_color, rings[variant], sizeof(rings[variant]));+ struct wlr_render_pass *pass = begin_fill(r, buf, BG, clip);+ rediwm_scene_pass_add_shadow(pass, &s, &(struct rediwm_pass_shadow){+ .offset_y = 12, .softness = 24,+ .color = {0, 0, 0, .4f}, .hide_inside = true, .clip = clip});+ piece(pass, &s, w->title, bodies[variant][0], clip);+ piece(pass, &s, w->client, bodies[variant][1], clip);+ piece(pass, &s, w->footer, bodies[variant][2], clip);+ finish(pass);+}+static void gate_damage_at_clip_edge(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator,+ struct image *img) {+ struct wlr_buffer *a = target_sized(allocator, GW, GH), *b = target_sized(allocator, GW, GH),+ *c = target_sized(allocator, GW, GH);+ struct image one = {0}, two = {0};+ int differing_inside = 0, differing_outside = 0;+ const float zooms[] = {1.f, .55f};+ for (size_t si = 0; si < COUNT(SCALES); si++) for (size_t zi = 0; zi < COUNT(zooms); zi++) {+ struct window w = make_window(SCALES[si], zooms[zi], 10, 1);+ const struct wlr_box *bx = &w.shape.box;+ // Damage whose edges cut through the corner arcs, the ring and the shadow:+ // a square over the top-left arc, one over the bottom-right, and a band.+ pixman_region32_t damage;+ pixman_region32_init_rect(&damage, bx->x - 5, bx->y - 5, 13, 13);+ pixman_region32_union_rect(&damage, &damage, bx->x + bx->width - 9, bx->y + bx->height - 11, 30, 29);+ pixman_region32_union_rect(&damage, &damage, bx->x + bx->width / 3, bx->y + 3, 7, bx->height + 20);+ draw_scene(r, a, &w, 0, NULL);+ draw_scene(r, b, &w, 1, NULL);+ draw_scene(r, c, &w, 0, NULL);+ draw_scene(r, c, &w, 1, &damage);+ read_image(r, a, &one);+ read_image(r, b, &two);+ read_image(r, c, img);+ for (int y = 0; y < GH; y++) for (int x = 0; x < GW; x++) {+ bool in = pixman_region32_contains_point(&damage, x, y, NULL);+ const uint8_t *want = at(in ? &two : &one, x, y);+ for (int ch = 0; ch < 4; ch++)+ check(at(img, x, y)[ch] == want[ch],+ "partial repaint differs at (%d,%d) channel %d (%s damage): %d, wanted %d (scale %.2f)",+ x, y, ch, in ? "inside" : "outside", at(img, x, y)[ch], want[ch], SCALES[si]);+ if (memcmp(at(&one, x, y), at(&two, x, y), 4)) in ? differing_inside++ : differing_outside++;+ }+ pixman_region32_fini(&damage);+ }+ // The control: the two frames really differ both where the damage is and where it is not.+ check(differing_inside > 0 && differing_outside > 0, "the test frames do not differ across the damage edge");+ free(one.p);+ free(two.p);+ wlr_buffer_drop(a);+ wlr_buffer_drop(b);+ wlr_buffer_drop(c);+ puts("PASS: repainting only damage that cuts through arcs, ring and shadow equals a full repaint");+}++static void shaped_gates(struct rediwm_scene_renderer *r, struct wlr_allocator *allocator) {+ struct wlr_buffer *buf = target_sized(allocator, GW, GH);+ struct image img = {0}, ref = {0};+ gate_clip_accuracy(r, buf, &img);+ gate_no_seams(r, buf, &img);+ gate_border_partition(r, buf, &img, &ref);+ gate_premultiplied(r, buf, &img);+ gate_dim(r, buf, &img);+ gate_opaque_cover(r, buf, &img, &ref);+ gate_shadow(r, buf, &img);+ gate_damage_at_clip_edge(r, allocator, &img);+ free(img.p);+ free(ref.p);+ wlr_buffer_drop(buf);+}diff --git a/src/scene_pass_test_shapes.c b/src/scene_pass_test_shapes.cnew file mode 100644index 0000000..48afd77--- /dev/null+++ b/src/scene_pass_test_shapes.c@@ -0,0 +1,271 @@+// Stage 4b shape gates 1-4: clip accuracy, seams, border partition and+// premultiplied edges, with the shared shape helpers.+// Included by scene_pass_test.c, in its one unit with scene_pass.c.++// ---- Stage 4b: rounded shapes (docs/plans/scene-graph-stage4.md) ----------------------+// The stage 1 spike's gates, on the production pass. Windows are made the way the+// compositor will make them: a clip spec in source units, resolved through a+// transform and snapped, so these also test that the pieces tile on the grid.+enum { GW = 640, GH = 480 };+static const float BG[4] = {224 / 255.f, 225 / 255.f, 232 / 255.f, 1};+static const float BODY[4] = {16 / 255.f, 18 / 255.f, 21 / 255.f, 1};+static const float SCALES[] = {1.f, 1.25f, 1.5f, 2.f};+static const float ZOOMS[] = {1.f, .85f, .70f, .55f, .25f};+#define COUNT(a) (sizeof(a) / sizeof(*(a)))++static void check(bool ok, const char *what, ...) {+ if (ok) return;+ char text[256];+ va_list args;+ va_start(args, what);+ vsnprintf(text, sizeof(text), what, args);+ va_end(args);+ fprintf(stderr, "FAIL: %s\n", text);+ exit(1);+}+struct image { int w, h; uint8_t *p; };+static uint8_t *at(const struct image *img, int x, int y) { return img->p + 4 * (y * img->w + x); }+static int diff(int a, int b) { return abs(a - b); }+static void read_image(struct rediwm_scene_renderer *r, struct wlr_buffer *b, struct image *img) {+ struct gl_state state;+ assert(enter(r, &state));+ img->w = b->width;+ img->h = b->height;+ img->p = realloc(img->p, 4 * (size_t)img->w * img->h);+ glBindFramebuffer(GL_FRAMEBUFFER, wlr_gles2_renderer_get_buffer_fbo(r->renderer, b));+ glPixelStorei(GL_PACK_ALIGNMENT, 1);+ glReadPixels(0, 0, img->w, img->h, GL_RGBA, GL_UNSIGNED_BYTE, img->p);+ assert(glGetError() == GL_NO_ERROR);+ leave(r, &state);+}+static struct wlr_buffer *target_sized(struct wlr_allocator *allocator, int w, int h) {+ uint64_t modifier = DRM_FORMAT_MOD_INVALID;+ struct wlr_drm_format format = {+ .format = DRM_FORMAT_ARGB8888, .len = 1, .capacity = 1, .modifiers = &modifier,+ };+ struct wlr_buffer *b = wlr_allocator_create_buffer(allocator, w, h, &format);+ assert(b);+ return b;+}+// A pass whose buffer starts as one opaque colour.+static struct wlr_render_pass *begin_fill(struct rediwm_scene_renderer *r, struct wlr_buffer *b,+ const float color[4], const pixman_region32_t *clip) {+ struct wlr_render_pass *pass = rediwm_scene_pass_begin(r, b, NULL);+ check(pass != NULL, "cannot begin an owned pass");+ wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){+ .box = {0, 0, b->width, b->height}, .color = {color[0], color[1], color[2], color[3]},+ .blend_mode = WLR_RENDER_BLEND_MODE_NONE, .clip = clip,+ });+ return pass;+}+static void finish(struct wlr_render_pass *pass) { check(wlr_render_pass_submit(pass), "an owned pass failed"); }+static void piece(struct wlr_render_pass *pass, const struct rediwm_pass_shape *shape,+ struct wlr_box box, const float color[4], const pixman_region32_t *clip) {+ rediwm_scene_pass_add_rect_shaped(pass, &(struct wlr_render_rect_options){+ .box = box, .color = {color[0], color[1], color[2], color[3]}, .clip = clip,+ }, shape);+}++// A window 200x140 (source units) at (30, 24), plus a titlebar of 36 and a footer+// of 10, placed in a layout offset by 8 device pixels.+struct window {+ struct rediwm_pass_shape shape;+ struct rediwm_clip clip;+ struct wlr_box title, client, footer;+};+static struct wlr_box box_of(struct rediwm_snap s) { return (struct wlr_box){s.x, s.y, s.width, s.height}; }+static struct window make_window(float scale, float zoom, float radius, double border) {+ const struct rediwm_xform xf = {(double)scale * zoom, 8, 8};+ const struct rediwm_clip_spec spec = {.width = 200, .height = 140, .radii = {radius, radius, radius, radius}, .border = border};+ struct window w = {.clip = rediwm_clip_resolve(xf, 30, 24, &spec)};+ w.shape.box = box_of(rediwm_edges_snap(w.clip.edges));+ for (int i = 0; i < 4; i++) w.shape.radii[i] = w.clip.radii[i];+ // A ring is whole device pixels, never thinner than one.+ w.shape.border = w.clip.border > 0 ? fmaxf(1.f, roundf(w.clip.border)) : 0;+ w.title = box_of(rediwm_edges_snap(rediwm_xform_rect(xf, 30, 24, 200, 36)));+ w.client = box_of(rediwm_edges_snap(rediwm_xform_rect(xf, 30, 24 + 36, 200, 140 - 36 - 10)));+ w.footer = box_of(rediwm_edges_snap(rediwm_xform_rect(xf, 30, 24 + 140 - 10, 200, 10)));+ return w;+}++// ---- Gate 1: the clip against a supersampled reference ---------------------------------+static bool inside_rrect(double px, double py, const struct rediwm_pass_shape *s) {+ double x = px - s->box.x, y = py - s->box.y;+ if (x < 0 || y < 0 || x > s->box.width || y > s->box.height) return false;+ double r = (x < s->box.width / 2.) ? (y < s->box.height / 2. ? s->radii[0] : s->radii[3])+ : (y < s->box.height / 2. ? s->radii[1] : s->radii[2]);+ double cx = x < s->box.width / 2. ? r : s->box.width - r, cy = y < s->box.height / 2. ? r : s->box.height - r;+ if (!((x < r || x > s->box.width - r) && (y < r || y > s->box.height - r))) return true;+ return hypot(x - cx, y - cy) <= r;+}+static double reference_coverage(const struct rediwm_pass_shape *s, int x, int y) {+ const int n = 8;+ int hits = 0;+ for (int j = 0; j < n; j++) for (int i = 0; i < n; i++) hits += inside_rrect(x + (i + .5) / n, y + (j + .5) / n, s);+ return (double)hits / (n * n);+}+static void gate_clip_accuracy(struct rediwm_scene_renderer *r, struct wlr_buffer *buf, struct image *img) {+ int worst = 0, fringe = 0;+ const uint8_t bg[3] = {224, 225, 232}, body[3] = {16, 18, 21};+ for (size_t si = 0; si < COUNT(SCALES); si++) for (size_t zi = 0; zi < COUNT(ZOOMS); zi++) {+ struct window w = make_window(SCALES[si], ZOOMS[zi], 10, 0);+ struct wlr_render_pass *pass = begin_fill(r, buf, BG, NULL);+ piece(pass, &w.shape, w.shape.box, BODY, NULL);+ finish(pass);+ read_image(r, buf, img);+ for (int y = w.shape.box.y - 2; y < w.shape.box.y + w.shape.box.height + 2; y++)+ for (int x = w.shape.box.x - 2; x < w.shape.box.x + w.shape.box.width + 2; x++) {+ double c = reference_coverage(&w.shape, x, y);+ for (int ch = 0; ch < 3; ch++) {+ int got = at(img, x, y)[ch];+ int want = (int)lround(bg[ch] * (1 - c) + body[ch] * c);+ if (diff(got, want) > worst) worst = diff(got, want);+ // No fringe: never outside the range of the two colours.+ int lo = body[ch] < bg[ch] ? body[ch] : bg[ch], hi = body[ch] < bg[ch] ? bg[ch] : body[ch];+ if (got < lo - 1 || got > hi + 1) fringe++;+ }+ }+ }+ check(fringe == 0, "clip: %d channel values fall outside the bg/body range (fringe)", fringe);+ // A one-pixel linear ramp on the distance is not area coverage: on a curved+ // 45-degree edge it is off by up to about .08 of the contrast, here 18 of 208+ // levels at scale 1.25, zoom .70 (an 8.75 px radius), pixel (2,2) of each+ // corner. The spike, without the 1.25 scale, measured 12. glass.c uses the+ // same ramp, and a straight edge or a pixel off the diagonal is exact.+ check(worst <= 20, "clip: differs from supersampled coverage by %d levels", worst);+ printf("PASS: rounded clip within %d levels of area coverage, no fringe, %zu scales x %zu zoom steps\n",+ worst, COUNT(SCALES), COUNT(ZOOMS));+}++// ---- Gate 2: pieces of one window share the clip with no seam --------------------------+static void gate_no_seams(struct rediwm_scene_renderer *r, struct wlr_buffer *buf, struct image *img) {+ const float red[4] = {1, 0, 0, 1}, green[4] = {0, 1, 0, 1}, blue[4] = {0, 0, 1, 1};+ for (size_t si = 0; si < COUNT(SCALES); si++) for (size_t zi = 0; zi < COUNT(ZOOMS); zi++) {+ struct window w = make_window(SCALES[si], ZOOMS[zi], 10, 0);+ struct wlr_render_pass *pass = begin_fill(r, buf, BG, NULL);+ piece(pass, &w.shape, w.title, red, NULL);+ piece(pass, &w.shape, w.client, green, NULL);+ piece(pass, &w.shape, w.footer, blue, NULL);+ finish(pass);+ read_image(r, buf, img);+ const struct wlr_box *b = &w.shape.box;+ const int margin = (int)ceilf(w.shape.radii[0]) + 2;+ for (int y = 0; y < b->height; y++) for (int x = margin; x < b->width - margin; x++) {+ const float *want = b->y + y < w.client.y ? red : b->y + y < w.footer.y ? green : blue;+ uint8_t *px = at(img, b->x + x, b->y + y);+ for (int ch = 0; ch < 3; ch++)+ check(diff(px[ch], (int)lround(want[ch] * 255)) <= 1,+ "seam at scale %.2f zoom %.2f, row %d col %d channel %d = %d",+ SCALES[si], ZOOMS[zi], y, x, ch, px[ch]);+ }+ // The pieces really tile the shape's box, with no gap and no overlap.+ check(w.title.y == b->y && w.title.y + w.title.height == w.client.y &&+ w.client.y + w.client.height == w.footer.y && w.footer.y + w.footer.height == b->y + b->height,+ "pieces do not tile the window at scale %.2f zoom %.2f", SCALES[si], ZOOMS[zi]);+ }+ puts("PASS: titlebar, client and footer meet without a seam at every scale and zoom");+}++// ---- Gate 3: border and body partition the shape's coverage ----------------------------+static void gate_border_partition(struct rediwm_scene_renderer *r, struct wlr_buffer *buf,+ struct image *img, struct image *ref) {+ int worst = 0;+ for (size_t si = 0; si < COUNT(SCALES); si++) for (size_t zi = 0; zi < COUNT(ZOOMS); zi++) {+ struct window w = make_window(SCALES[si], ZOOMS[zi], 10, 1);+ struct rediwm_pass_shape ringed = w.shape, plain = w.shape;+ plain.border = 0;+ memcpy(ringed.border_color, BODY, sizeof(BODY));+ // Ring and body in one colour, in three pieces, must equal one plain body,+ // whatever the split between them, or coverage leaks.+ struct wlr_render_pass *pass = begin_fill(r, buf, BG, NULL);+ piece(pass, &plain, plain.box, BODY, NULL);+ finish(pass);+ read_image(r, buf, ref);+ pass = begin_fill(r, buf, BG, NULL);+ piece(pass, &ringed, w.title, BODY, NULL);+ piece(pass, &ringed, w.client, BODY, NULL);+ piece(pass, &ringed, w.footer, BODY, NULL);+ finish(pass);+ read_image(r, buf, img);+ for (int i = 0; i < img->w * img->h * 4; i++) if (diff(img->p[i], ref->p[i]) > worst) worst = diff(img->p[i], ref->p[i]);+ // And the ring really is the ring: its own colour on the outer row of a+ // straight edge, the body colour just inside it.+ const float red[4] = {1, 0, 0, 1};+ memcpy(ringed.border_color, red, sizeof(red));+ pass = begin_fill(r, buf, BG, NULL);+ piece(pass, &ringed, ringed.box, BODY, NULL);+ finish(pass);+ read_image(r, buf, img);+ int cx = ringed.box.x + ringed.box.width / 2, bw = (int)ringed.border;+ check(at(img, cx, ringed.box.y)[0] >= 254 && at(img, cx, ringed.box.y)[1] <= 1,+ "outer row is not the border colour at scale %.2f zoom %.2f", SCALES[si], ZOOMS[zi]);+ check(diff(at(img, cx, ringed.box.y + bw)[0], 16) <= 1,+ "row inside the border is not the body at scale %.2f zoom %.2f", SCALES[si], ZOOMS[zi]);+ check(at(img, ringed.box.x, ringed.box.y + ringed.box.height / 2)[0] >= 254,+ "left edge is not the border colour at scale %.2f zoom %.2f", SCALES[si], ZOOMS[zi]);+ // A translucent ring (the default theme's is 9% white) is composited over the+ // body, not in place of it: ring + body * (1 - alpha), per channel.+ const float faint[4] = {.09f, .09f, .09f, .09f};+ memcpy(ringed.border_color, faint, sizeof(faint));+ pass = begin_fill(r, buf, BG, NULL);+ piece(pass, &ringed, ringed.box, BODY, NULL);+ finish(pass);+ read_image(r, buf, img);+ for (int ch = 0; ch < 3; ch++)+ check(diff(at(img, cx, ringed.box.y)[ch], (int)lround(255 * (.09 + BODY[ch] * .91))) <= 1,+ "translucent ring is not over the body at scale %.2f zoom %.2f: channel %d is %d",+ SCALES[si], ZOOMS[zi], ch, at(img, cx, ringed.box.y)[ch]);+ }+ check(worst <= 2, "border + body differ from one shape by %d levels", worst);+ printf("PASS: border ring and body, in one pass, add up to exactly the shape's coverage (max diff %d)\n", worst);+}++// ---- Gate 4: premultiplied texture edges and opaque (XRGB) textures ---------------------+static void gate_premultiplied(struct rediwm_scene_renderer *r, struct wlr_buffer *buf, struct image *img) {+ struct rediwm_pass_shape s = {.box = {40, 40, 80, 80}, .radii = {20, 20, 20, 20}};+ // 50% red, premultiplied: (128, 0, 0, 128) is a half-intensity red at half alpha.+ uint32_t half[16], junk = 0x000AC81E, white = 0xffffffff, solid_px[1] = {junk};+ for (int i = 0; i < 16; i++) half[i] = 0x80800000;+ struct wlr_texture *t_half = wlr_texture_from_pixels(r->renderer, DRM_FORMAT_ARGB8888, 16, 4, 4, half);+ struct wlr_texture *t_xrgb = wlr_texture_from_pixels(r->renderer, DRM_FORMAT_XRGB8888, 4, 1, 1, solid_px);+ struct wlr_texture *t_white = wlr_texture_from_pixels(r->renderer, DRM_FORMAT_XRGB8888, 4, 1, 1, &white);+ check(t_half && t_xrgb && t_white, "cannot create the test textures");+ struct { struct wlr_texture *t; float alpha; const char *name; } cases[] = {+ {t_half, 1, "half-transparent"}, {t_xrgb, 1, "xrgb"}, {t_white, .5f, "opacity"},+ };+ for (size_t i = 0; i < COUNT(cases); i++) {+ const bool on_black = cases[i].t == t_white;+ float base[4] = {BG[0], BG[1], BG[2], 1};+ if (on_black) memset(base, 0, sizeof(float) * 3);+ struct wlr_render_pass *pass = begin_fill(r, buf, base, NULL);+ float alpha = cases[i].alpha;+ rediwm_scene_pass_add_texture_shaped(pass, &(struct wlr_render_texture_options){+ .texture = cases[i].t, .dst_box = s.box, .alpha = &alpha}, &s);+ finish(pass);+ read_image(r, buf, img);+ uint8_t *in = at(img, 80, 80);+ const int want[3][3] = {{240, 112, 116}, {10, 200, 30}, {128, 128, 128}};+ for (int ch = 0; ch < 3; ch++)+ check(diff(in[ch], want[i][ch]) <= 2, "%s texel gave (%d,%d,%d) inside the shape",+ cases[i].name, in[0], in[1], in[2]);+ // At the edge the colour is the premultiplied source times coverage over the backdrop:+ // never darker or brighter than the two ends, and within the clip's accuracy of area coverage.+ double src_a = i == 0 ? .50196 : i == 2 ? .5 : 1;+ for (int y = s.box.y - 1; y < s.box.y + s.box.height + 1; y++)+ for (int x = s.box.x - 1; x < s.box.x + s.box.width + 1; x++) {+ double c = reference_coverage(&s, x, y);+ for (int ch = 0; ch < 3; ch++) {+ double bgv = base[ch] * 255;+ double want_v = want[i][ch] - (1 - src_a) * bgv; // the premultiplied source, 0..255+ double expect = want_v * c + bgv * (1 - src_a * c);+ check(diff(at(img, x, y)[ch], (int)lround(expect)) <= 14,+ "%s edge pixel (%d,%d) channel %d is %d, area coverage predicts %.0f",+ cases[i].name, x, y, ch, at(img, x, y)[ch], expect);+ }+ }+ }+ wlr_texture_destroy(t_half);+ wlr_texture_destroy(t_xrgb);+ wlr_texture_destroy(t_white);+ puts("PASS: premultiplied edges, opacity and XRGB textures blend correctly through the clip");+}