RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process
compositor: callback-only commits no longer build a frame
oxydizer6b60400f945c
diff --git a/docs/agent-notes/rendering.md b/docs/agent-notes/rendering.mdindex 9123165..298e669 100644--- a/docs/agent-notes/rendering.md+++ b/docs/agent-notes/rendering.md@@ -59,6 +59,11 @@ chrome, capture, or retained panel painting. General rules remain in`wlr_output_commit_state`. `commitIsRedundant` skips commits with no damage,colour change, attach-render lock or cursor change; register anything newthere. Skipped frames get callbacks from `vblank_timer`, never immediately.+ A client commit with nothing new (no attach, no damage, no remap) changes no+ node and schedules no frame: its callbacks go through the scene's+ `frame_callbacks` hook (`Output.frameCallbacksWanted`), which arms the timer+ at idle unless a frame is coming anyway. Keep it that way: a setter or listener+ that reports a change on every commit brings back a whole frame per commit.Build output state through `Server.buildOutputState`, including capture: theowned GLES2 pass is the only presenter (the session requires GLES2) and keepsone swapchain damage history. It reads graph geometry and the buffers,diff --git a/src/Output.zig b/src/Output.zigindex 6c92452..1126e68 100644--- a/src/Output.zig+++ b/src/Output.zig@@ -105,6 +105,10 @@ frame_wanted: bool = false,/// Last known vblank: a page-flip frame event or a fired `vblank_timer`.last_vblank_ns: u64 = 0,flip_pending: bool = false,+/// Decides, once this dispatch's commits are handled, whether client frame+/// callbacks need a frame (`frameCallbacksWanted`).+callback_idle: ?*wl.EventSource = null,+callback_frame_seq: u64 = 0,tearing_idle: ?*wl.EventSource = null,tearing_rejected: bool = false,/// An ext-session-lock client waits for this output to commit a locked frame.@@ -421,6 +425,7 @@ pub const commitIsRedundant = output_frame.commitIsRedundant;pub const refreshPeriodNs = output_frame.refreshPeriodNs;pub const deferFrameDone = output_frame.deferFrameDone;pub const handleVblank = output_frame.handleVblank;+pub const frameCallbacksWanted = output_frame.frameCallbacksWanted;pub const sendFrameDone = output_frame.sendFrameDone;pub const tearingAllowed = output_frame.tearingAllowed;pub const queueTearing = output_frame.queueTearing;@@ -532,6 +537,7 @@ pub fn handleDestroy(listener: *wl.Listener(*wlr.Output), _: *wlr.Output) void {if (output.power_menu) |pm| pm.destroy();output.color.deinit();if (output.vblank_timer) |timer| timer.remove();+ if (output.callback_idle) |idle| idle.remove();output.link.remove();output.frame.link.remove();output.present.link.remove();diff --git a/src/Server.zig b/src/Server.zigindex 842b38c..bd96cb7 100644--- a/src/Server.zig+++ b/src/Server.zig@@ -378,6 +378,7 @@ pub fn init(server: *Server, scale: ?f32, icon_theme_cfg: icon_theme.Config, io:const nodes = sn.Scene.create() catch return error.SceneCreateFailed;errdefer nodes.destroy();nodes.outputs = scene_outputs;+ nodes.frame_callbacks = Output.frameCallbacksWanted;const background_tree = nodes.tree.createSceneTree() catch return error.SceneCreateFailed;const desktop_tree = nodes.tree.createSceneTree() catch return error.SceneCreateFailed;const world_tree = nodes.tree.createSceneTree() catch return error.SceneCreateFailed;diff --git a/src/glass.c b/src/glass.cindex f5fb415..3a2a148 100644--- a/src/glass.c+++ b/src/glass.c@@ -135,10 +135,11 @@ void rediwm_glass_hold_all(struct glass_engine *engine, bool hold)}void rediwm_glass_configure(struct glass_effect *effect, float radius, float opacity){- if (!effect)+ if (!effect || (effect->radius == radius && effect->opacity == opacity))return;effect->radius = radius;effect->opacity = opacity;+ rediwm_node_touch(&effect->node->node);}void rediwm_glass_detach(struct glass_effect *effect){diff --git a/src/ipc/wait.zig b/src/ipc/wait.zigindex 1e85adf..0b5ba85 100644--- a/src/ipc/wait.zig+++ b/src/ipc/wait.zig@@ -367,6 +367,11 @@ pub const WaitManager = struct {}}+ pub fn waitsForFrame(mgr: *const WaitManager) bool {+ for (mgr.entries.items) |entry| if (entry.kind == .frame) return true;+ return false;+ }+pub fn checkAll(mgr: *WaitManager) void {const now = anim.nowMs();var i: usize = 0;diff --git a/src/node.h b/src/node.hindex e38f536..7fd9585 100644--- a/src/node.h+++ b/src/node.h@@ -26,6 +26,7 @@#include <wlr/util/box.h>struct wlr_buffer;+struct wlr_output;struct wlr_texture;struct wlr_drm_syncobj_timeline;struct rediwm_scene;@@ -125,6 +126,9 @@ struct rediwm_scene {void *changed_data;// The outputs this scene is shown on (what surfaces are told about).struct rediwm_scene_outputs *outputs;+ // A client surface shown on `output` committed with frame callbacks waiting. NULL+ // schedules a frame, which delivers them; the owner may deliver them without one.+ void (*frame_callbacks)(struct wlr_output *output);};static inline struct rediwm_tree *rediwm_tree_from_node(struct rediwm_node *node) {diff --git a/src/node.zig b/src/node.zigindex f98898f..f977253 100644--- a/src/node.zig+++ b/src/node.zig@@ -237,6 +237,8 @@ pub const Scene = extern struct {changed_data: ?*anyopaque,/// The outputs this scene is shown on (scene_outputs.h).outputs: ?*anyopaque,+ /// A client surface committed with frame callbacks waiting (node.h).+ frame_callbacks: ?*const fn (output: *wlr.Output) callconv(.c) void,extern fn rediwm_scene_create(changed: ?*const fn (?*anyopaque, ?*Node, bool) callconv(.c) void, data: ?*anyopaque) ?*Scene;pub fn create() !*Scene {diff --git a/src/node_surface.c b/src/node_surface.cindex 445a7f2..a7c0f4f 100644--- a/src/node_surface.c+++ b/src/node_surface.c@@ -287,6 +287,16 @@ static void surface_reconfigure(struct rediwm_surface *scene_surface) {scene_buffer->color_encoding = color_encoding;scene_buffer->color_range = color_range;+ // A commit that attached nothing and damaged nothing shows the same content. Setting+ // the buffer again would report the node changed and build a whole frame for a+ // client that only asked for a frame callback (the lock mark below nets to zero).+ if (surface->buffer && scene_buffer->buffer == &surface->buffer->base &&+ !(state->committed & WLR_SURFACE_STATE_BUFFER) &&+ !pixman_region32_not_empty(&surface->buffer_damage)) {+ pixman_region32_fini(&opaque);+ return;+ }+unmark_client_buffer(scene_buffer);if (surface->buffer) {@@ -332,8 +342,13 @@ static void handle_scene_surface_surface_commit(struct wl_listener *listener, vo// never reach the surface anyway.bool enabled = rediwm_node_coords(&surface->buffer->node, NULL, NULL);struct wlr_output *output = get_surface_frame_pacing_output(surface->surface);- if (!wl_list_empty(&surface->surface->current.frame_callback_list) && output && enabled)- wlr_output_schedule_frame(output);+ if (!wl_list_empty(&surface->surface->current.frame_callback_list) && output && enabled) {+ struct rediwm_scene *scene = surface->buffer->node.scene;+ if (scene && scene->frame_callbacks)+ scene->frame_callbacks(output);+ else+ wlr_output_schedule_frame(output);+ }}static bool scene_buffer_point_accepts_input(struct rediwm_buffer *buffer, double *sx, double *sy) {diff --git a/src/output/frame.zig b/src/output/frame.zigindex 6ac4e9b..8d314a7 100644--- a/src/output/frame.zig+++ b/src/output/frame.zig@@ -113,6 +113,36 @@ pub fn deferFrameDone(output: *Output) void {output.vblank_target_ns = target;}+/// A client surface committed with frame callbacks waiting (node_surface.c).+/// A commit that changed nothing must not build a frame just to find it+/// redundant: once every commit handler of this dispatch has run, deliver the+/// callbacks at the next vblank unless a frame is coming anyway.+pub fn frameCallbacksWanted(wlr_output: *wlr.Output) callconv(.c) void {+ const output: *Output = @ptrCast(@alignCast(wlr_output.data orelse {+ wlr_output.scheduleFrame();+ return;+ }));+ if (output.vblank_armed or output.callback_idle != null) return;+ output.callback_frame_seq = output.frame_seq;+ output.callback_idle = output.server.wl_server.getEventLoop().addIdle(*Output, callbacksIdle, output) catch {+ wlr_output.scheduleFrame();+ return;+ };+}++fn callbacksIdle(output: *Output) void {+ output.callback_idle = null;+ const wlr_output = output.wlr_output;+ // A frame since the request sent them; a scheduled one or the timer will.+ if (output.frame_seq != output.callback_frame_seq or output.vblank_armed or wlr_output.needs_frame) return;+ const scene_output = output.server.scene_outputs.get(wlr_output) orelse return wlr_output.scheduleFrame();+ // A frame waiter (IPC) counts on client frames reaching the output.+ const waiting = if (output.server.ipc) |ipc| ipc.wait_mgr.waitsForFrame() else false;+ if (wlr_output.frame_pending or waiting or !commitIsRedundant(output, scene_output)) return wlr_output.scheduleFrame();+ stats.global_stats.output_skipped_commits +%= 1;+ deferFrameDone(output);+}+pub fn handleVblank(output: *Output) c_int {output.vblank_armed = false;output.last_vblank_ns = output.vblank_target_ns;diff --git a/src/projection.c b/src/projection.cindex 6cd4563..679b779 100644--- a/src/projection.c+++ b/src/projection.c@@ -513,6 +513,11 @@ static void committed(struct wl_listener *listener, void *data) {struct leaf *l = wl_container_of(listener, l, commit);struct wlr_surface *surface = l->surface->surface;const struct wlr_surface_state *state = &surface->current;+ // A commit with no new content (a client only asking for a frame callback) leaves+ // the leaf as it is; geometry and regions reach it through the node setters.+ if (!(state->committed & WLR_SURFACE_STATE_BUFFER) &&+ !pixman_region32_not_empty(&surface->buffer_damage) && !rediwm_surface_state_remapped(state))+ return;// Regions from different buffer mappings cannot safely be combined. A full// repaint also covers a mapping changed and restored between presentations.if (state->buffer_width != l->damage_width ||@@ -1485,7 +1490,10 @@ static void watched_commit(struct wl_listener *listener, void *data) {bool known = false;wl_list_for_each(f, &p->feedback, link)if (f->leaf->surface && f->leaf->surface->surface == watch->surface) known = true;- if (!known) mark_all(p);+ // A known surface's leaf reports its own changes; frame callbacks alone are the+ // surface node's to deliver.+ if (known) return;+ mark_all(p);schedule(p);}static void watched_destroy(struct wl_listener *listener, void *data) {diff --git a/src/projection_test.c b/src/projection_test.cindex e747bcb..f465e04 100644--- a/src/projection_test.c+++ b/src/projection_test.c@@ -65,6 +65,7 @@ static void commit_damage(struct leaf *l, int x, int y, int width, int height) {pixman_region32_clear(&surface->buffer_damage);assert(pixman_region32_union_rect(&surface->buffer_damage,&surface->buffer_damage, x, y, width, height));+ surface->current.committed |= WLR_SURFACE_STATE_BUFFER; // an attachcommitted(&l->commit, NULL);}static bool damaged(struct rediwm_scene_output *out, int x, int y) {@@ -1413,6 +1414,13 @@ int main(void) {assert(!damaged(out, 100, 60));clear_damage(out);+ // A commit that attaches and damages nothing (a client only asking for a frame+ // callback) leaves the leaf and its group clean.+ surface.current.committed = 0;+ pixman_region32_clear(&surface.buffer_damage);+ committed(&l->commit, NULL);+ assert(!l->dirty && !p->any_dirty);+// Rotation with empty damage is not a repaint; rotation with partial damage// must retain only that region. The source tree itself is disabled.rediwm_buffer_set_buffer(node, b);diff --git a/src/toplevel/chrome_sync.zig b/src/toplevel/chrome_sync.zigindex 1a75cdc..dad816e 100644--- a/src/toplevel/chrome_sync.zig+++ b/src/toplevel/chrome_sync.zig@@ -14,6 +14,7 @@ const theme = @import("ui").theme;const stats = @import("../ipc/stats.zig");const xdg = @import("../xdg.zig");const border_glow = @import("../border_glow.zig");+const gpa = @import("../main.zig").gpa;const log = std.log.scoped(.toplevel);const titlebar_stretch = Toplevel.titlebar_stretch;const applySizeClip = Toplevel.applySizeClip;@@ -96,8 +97,9 @@ pub fn clearChromeCache(toplevel: *Toplevel) void {/// `rebuild_shadow` is false on a pure hover repaint, which cannot change the/// shadow; geometry and theme changes pass true.+/// Every change goes through node and projection setters, which ask for a+/// frame themselves: a client commit that changes nothing schedules none.pub fn syncChrome(toplevel: *Toplevel, rebuild_shadow: bool, now_ms: i64) !void {- defer toplevel.server.scheduleFrames();defer if (toplevel.server.input.grabbed_toplevel == toplevel) toplevel.server.input.updateGrabBorderGlow();const geometry_box = toplevel.clientGeometry();if (geometry_box.width <= 0 or geometry_box.height <= 0) return;@@ -146,9 +148,12 @@ pub fn syncChrome(toplevel: *Toplevel, rebuild_shadow: bool, now_ms: i64) !voidtoplevel.updateHeldIcon(icon);const hover = toplevel.hover_anim.value(now_ms);const chip_alpha = toplevel.chip_alpha.value(now_ms);- var tab_storage: [@import("../window_tabs.zig").max_tabs]@import("../chrome_tabs.zig").Tab = undefined;+ // Only a grouped window has tabs. Others skip the 4 KiB scratch, which+ // ReleaseSafe would fill on every client commit.+ const tab_storage = if (toplevel.tab_group != 0) try gpa.create([@import("../window_tabs.zig").max_tabs]@import("../chrome_tabs.zig").Tab) else null;+ defer if (tab_storage) |storage| gpa.destroy(storage);const state: chrome.ChromeState = .{- .tabs = toplevel.tabStrip(&tab_storage),+ .tabs = toplevel.tabStrip(tab_storage),.active = toplevel.activated,.workspace_focus = toplevel.hasWorkspaceFocusBorder(),.maximized = toplevel.isMaximized(),diff --git a/src/toplevel/tabs.zig b/src/toplevel/tabs.zigindex 234739f..fc1a424 100644--- a/src/toplevel/tabs.zig+++ b/src/toplevel/tabs.zig@@ -5,7 +5,19 @@ const Toplevel = @import("../Toplevel.zig");const geometry = @import("../geometry.zig");const nowMs = Toplevel.nowMs;-pub fn tabStrip(toplevel: *Toplevel, storage: *[@import("../window_tabs.zig").max_tabs]@import("../chrome_tabs.zig").Tab) @import("../chrome_tabs.zig").Strip {+/// `storage` may be null for a window outside any tab group, which has no tabs.+pub fn tabStrip(toplevel: *Toplevel, storage: ?*[@import("../window_tabs.zig").max_tabs]@import("../chrome_tabs.zig").Tab) @import("../chrome_tabs.zig").Strip {+ var strip: @import("../chrome_tabs.zig").Strip = .{ .first = toplevel.tab_first, .hover = toplevel.tab_hover, .notice = toplevel.tab_notice };+ const selected = if (storage) |tabs| fillTabs(toplevel, tabs, &strip) else 0;+ const geom = @import("../chrome_tabs.zig").geometry(toplevel.chrome_width, toplevel.chromeDensity(), strip);+ if (selected < strip.first) strip.first = selected;+ if (selected >= strip.first + geom.count) strip.first = selected + 1 -| geom.count;+ toplevel.tab_first = strip.first;+ return strip;+}++/// The group's tabs into `storage`; returns the index of `toplevel`'s own tab.+fn fillTabs(toplevel: *Toplevel, storage: *[@import("../window_tabs.zig").max_tabs]@import("../chrome_tabs.zig").Tab, strip: *@import("../chrome_tabs.zig").Strip) usize {var members_storage: [@import("../window_tabs.zig").max_tabs]*Toplevel = undefined;const members = @import("../window_tabs.zig").members(toplevel, &members_storage);var selected: usize = 0;@@ -13,12 +25,8 @@ pub fn tabStrip(toplevel: *Toplevel, storage: *[@import("../window_tabs.zig").mastorage[i] = .{ .id = member.id, .title = member.title(), .active = member == toplevel, .attention = member.needs_attention };if (member == toplevel) selected = i;}- var strip: @import("../chrome_tabs.zig").Strip = .{ .tabs = storage[0..members.len], .first = toplevel.tab_first, .hover = toplevel.tab_hover, .notice = toplevel.tab_notice };- const geom = @import("../chrome_tabs.zig").geometry(toplevel.chrome_width, toplevel.chromeDensity(), strip);- if (selected < strip.first) strip.first = selected;- if (selected >= strip.first + geom.count) strip.first = selected + 1 -| geom.count;- toplevel.tab_first = strip.first;- return strip;+ strip.tabs = storage[0..members.len];+ return selected;}pub fn tabAt(toplevel: *Toplevel, x: f64, y: f64) @import("../chrome_tabs.zig").Hit {