From aecf54bc761de0c2dcdaf6819ed210a7bb882080 Mon Sep 17 00:00:00 2001 From: Samuel Onoja Date: Fri, 2 Oct 2026 08:07:24 +0100 Subject: [PATCH] Share visibility checks and test client presentation lifecycle --- compositor/src/display_presentation.rs | 15 +- compositor/src/presentation.rs | 10 +- scripts/tests/fixtures/presentation.c | 157 ++++++++++++++++++++ scripts/tests/test_presentation_isolated.py | 132 ++++++++++++++++ 4 files changed, 302 insertions(+), 12 deletions(-) create mode 100644 scripts/tests/fixtures/presentation.c create mode 100644 scripts/tests/test_presentation_isolated.py diff --git a/compositor/src/display_presentation.rs b/compositor/src/display_presentation.rs index 3e80d7b..fe56ce3 100644 --- a/compositor/src/display_presentation.rs +++ b/compositor/src/display_presentation.rs @@ -1,7 +1,9 @@ //! Visibility of client content on displayed outputs, independent of capture. use std::collections::{HashMap, HashSet}; -use smithay::backend::renderer::element::{Id, RenderElementPresentationState, RenderElementStates}; +use smithay::backend::renderer::element::{ + Id, RenderElementPresentationState, RenderElementState, RenderElementStates, +}; use smithay::output::Output; #[derive(Default)] @@ -15,14 +17,15 @@ pub(crate) struct DisplayPresentation { outputs: HashMap, } +pub(crate) fn is_visible(state: RenderElementState) -> bool { + state.visible_area > 0 && state.presentation_state != RenderElementPresentationState::Skipped +} + fn visible(states: &RenderElementStates) -> HashSet { states .states .iter() - .filter_map(|(id, state)| { - (state.visible_area > 0 && state.presentation_state != RenderElementPresentationState::Skipped) - .then(|| id.clone()) - }) + .filter_map(|(id, state)| is_visible(*state).then(|| id.clone())) .collect() } @@ -113,6 +116,8 @@ mod tests { assert!(!record.visible(&id)); record.presented(&output); assert!(record.visible(&id)); + record.presented(&output); // No new submission keeps the last displayed scene. + assert!(record.visible(&id)); record.queued(&output, &RenderElementStates::default()); assert!(record.visible(&id)); diff --git a/compositor/src/presentation.rs b/compositor/src/presentation.rs index 5e5be02..814828a 100644 --- a/compositor/src/presentation.rs +++ b/compositor/src/presentation.rs @@ -2,9 +2,7 @@ //! logical pixels. Content, clips and decorations share those exact edges. use std::time::{Duration, Instant}; -use smithay::backend::renderer::element::{ - Element, Id, Kind, RenderElement, RenderElementPresentationState, RenderElementStates, -}; +use smithay::backend::renderer::element::{Element, Id, Kind, RenderElement, RenderElementStates}; use smithay::backend::renderer::gles::{GlesError, GlesFrame, GlesRenderer, GlesTexProgram, GlesTexture, Uniform}; use smithay::backend::renderer::utils::{CommitCounter, DamageSet}; use smithay::desktop::utils::OutputPresentationFeedback; @@ -55,9 +53,7 @@ pub(crate) fn take_output_feedback( fn rendered_feedback_output(output: &Output, id: Id, rendered: &RenderElementStates) -> Option { rendered .element_render_state(id) - .is_some_and(|state| { - state.visible_area > 0 && state.presentation_state != RenderElementPresentationState::Skipped - }) + .is_some_and(crate::display_presentation::is_visible) .then(|| output.clone()) } @@ -330,7 +326,7 @@ mod tests { #[test] fn presentation_feedback_excludes_missing_and_occluded_surfaces() { - use smithay::backend::renderer::element::RenderElementState; + use smithay::backend::renderer::element::{RenderElementPresentationState, RenderElementState}; use smithay::output::{PhysicalProperties, Subpixel}; let output = Output::new( diff --git a/scripts/tests/fixtures/presentation.c b/scripts/tests/fixtures/presentation.c new file mode 100644 index 0000000..05df681 --- /dev/null +++ b/scripts/tests/fixtures/presentation.c @@ -0,0 +1,157 @@ +/* Presentation visibility, empty-commit callbacks and idle inhibition probe. */ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include "xdg-shell-client-protocol.h" +#include "idle-inhibit-client-protocol.h" +#include "idle-notify-client-protocol.h" + +static struct wl_compositor *compositor; +static struct wl_shm *shm; +static struct xdg_wm_base *wm; +static struct wl_surface *surface; +static struct wl_buffer *buffer; +static int width = 640, height = 480; +static int private_capture_seen; +static int covered; +static struct wl_seat *seat; +static struct zwp_idle_inhibit_manager_v1 *inhibit; +static struct ext_idle_notifier_v1 *notifier; +static int mapped = 1; +static void arm_frame(void); +static void frame_done(void *data, struct wl_callback *callback, uint32_t time) { + (void)data; (void)time; + wl_callback_destroy(callback); + puts("frame"); fflush(stdout); + if (mapped) { arm_frame(); wl_surface_commit(surface); } +} +static const struct wl_callback_listener frame_listener = {.done = frame_done}; +static void arm_frame(void) { + wl_callback_add_listener(wl_surface_frame(surface), &frame_listener, NULL); +} +static void idled(void *data, struct ext_idle_notification_v1 *notification) { + (void)data; (void)notification; puts("idle"); fflush(stdout); +} +static void resumed(void *data, struct ext_idle_notification_v1 *notification) { + (void)data; (void)notification; puts("resumed"); fflush(stdout); +} +static const struct ext_idle_notification_v1_listener idle_listener = {.idled = idled, .resumed = resumed}; + +static void ping(void *data, struct xdg_wm_base *base, uint32_t serial) { + (void)data; + xdg_wm_base_pong(base, serial); +} +static const struct xdg_wm_base_listener wm_listener = {.ping = ping}; + +static void global(void *data, struct wl_registry *registry, uint32_t name, + const char *interface, uint32_t version) { + (void)data; (void)version; + if (!strcmp(interface, "ferese_window_capture_manager_v1")) private_capture_seen = 1; + if (!strcmp(interface, "wl_seat")) seat = wl_registry_bind(registry, name, &wl_seat_interface, 1); + if (!strcmp(interface, "zwp_idle_inhibit_manager_v1")) inhibit = wl_registry_bind(registry, name, &zwp_idle_inhibit_manager_v1_interface, 1); + if (!strcmp(interface, "ext_idle_notifier_v1")) notifier = wl_registry_bind(registry, name, &ext_idle_notifier_v1_interface, 1); + if (!strcmp(interface, "wl_compositor")) + compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 4); + else if (!strcmp(interface, "wl_shm")) + shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); + else if (!strcmp(interface, "xdg_wm_base")) { + wm = wl_registry_bind(registry, name, &xdg_wm_base_interface, 1); + xdg_wm_base_add_listener(wm, &wm_listener, NULL); + } +} + +static void removed(void *data, struct wl_registry *registry, uint32_t name) { + (void)data; (void)registry; (void)name; +} +static const struct wl_registry_listener registry_listener = {global, removed}; + +static void configure(void *data, struct xdg_surface *xdg, uint32_t serial) { + (void)data; + xdg_surface_ack_configure(xdg, serial); + xdg_surface_set_window_geometry(xdg, 0, 0, width, height); + wl_surface_attach(surface, buffer, 0, 0); + wl_surface_damage(surface, 0, 0, width, height); + wl_surface_commit(surface); +} +static const struct xdg_surface_listener surface_listener = {.configure = configure}; + +static void size(void *data, struct xdg_toplevel *top, int32_t w, int32_t h, + struct wl_array *states) { + (void)data; (void)top; (void)w; (void)h; (void)states; +} + +static void close_window(void *data, struct xdg_toplevel *top) { + (void)data; (void)top; exit(0); +} +static const struct xdg_toplevel_listener top_listener = {.configure = size, .close = close_window}; + +static void make_buffer(void) { + int fd = memfd_create("presentation-test", MFD_CLOEXEC); + size_t bytes = width * height * 4; + if (fd < 0 || ftruncate(fd, bytes)) exit(3); + uint32_t *pixels = mmap(NULL, bytes, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (pixels == MAP_FAILED) exit(4); + for (size_t i = 0; i < (size_t)width * height; i++) { + pixels[i] = covered ? 0xff00ff00 : (i % width < 80 ? 0x80800000 : (i / width < (size_t)height / 2 ? 0xffff0000 : 0xff0000ff)); + } + struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, bytes); + buffer = wl_shm_pool_create_buffer(pool, 0, width, height, width * 4, WL_SHM_FORMAT_ARGB8888); + wl_shm_pool_destroy(pool); + close(fd); + munmap(pixels, bytes); +} + +int main(int argc, char **argv) { + struct wl_display *display = wl_display_connect(NULL); + if (!display) return 1; + struct wl_registry *registry = wl_display_get_registry(display); + wl_registry_add_listener(registry, ®istry_listener, NULL); + if (wl_display_roundtrip(display) < 0 || !compositor || !shm || !wm) return 2; + if (private_capture_seen || !seat || !inhibit || !notifier) return 9; + covered = argc > 1 && !strcmp(argv[1], "cover"); + make_buffer(); + surface = wl_compositor_create_surface(compositor); + struct xdg_surface *xdg = xdg_wm_base_get_xdg_surface(wm, surface); + xdg_surface_add_listener(xdg, &surface_listener, NULL); + struct xdg_toplevel *top = xdg_surface_get_toplevel(xdg); + xdg_toplevel_add_listener(top, &top_listener, NULL); + xdg_toplevel_set_app_id(top, "ferese.test.presentation"); + xdg_toplevel_set_title(top, covered ? "Occluding window" : "Inhibiting window"); + if (covered) { + struct wl_region *opaque = wl_compositor_create_region(compositor); + wl_region_add(opaque, 0, 0, width, height); + wl_surface_set_opaque_region(surface, opaque); + wl_region_destroy(opaque); + } else { + zwp_idle_inhibit_manager_v1_create_inhibitor(inhibit, surface); + ext_idle_notification_v1_add_listener(ext_idle_notifier_v1_get_idle_notification(notifier, 200, seat), &idle_listener, NULL); + arm_frame(); + } + wl_surface_commit(surface); + if (!covered) { + while (1) { + wl_display_flush(display); + struct pollfd fds[2] = {{wl_display_get_fd(display), POLLIN, 0}, {STDIN_FILENO, POLLIN, 0}}; + if (poll(fds, 2, -1) < 0) break; + if (fds[0].revents & POLLIN && wl_display_dispatch(display) < 0) break; + if (fds[1].revents & POLLIN) { + char command; + if (read(STDIN_FILENO, &command, 1) != 1) break; + if (command == 'u') { + mapped = 0; + wl_surface_attach(surface, NULL, 0, 0); + wl_surface_commit(surface); + } + } + } + } else { + while (wl_display_dispatch(display) != -1) {} + } + return 0; +} diff --git a/scripts/tests/test_presentation_isolated.py b/scripts/tests/test_presentation_isolated.py new file mode 100644 index 0000000..9f06e78 --- /dev/null +++ b/scripts/tests/test_presentation_isolated.py @@ -0,0 +1,132 @@ +"""Verify visibility-gated callbacks and idle inhibition on a private desktop. + +FERESE_TEST_PRESENTATION=1 python3 scripts/tests/test_presentation_isolated.py +Requires built debug compositor/control binaries, a Wayland host, cc and wayland-scanner. +""" +import json +import os +from pathlib import Path +import signal +import subprocess +import tempfile +import time +import unittest + +REPO = Path(__file__).resolve().parents[2] + + +@unittest.skipUnless(os.environ.get("FERESE_TEST_PRESENTATION") == "1", "requires a Wayland host") +class Presentation(unittest.TestCase): + def test_covered_and_unmapped_inhibitors_stop_inhibiting(self): + processes = [] + with tempfile.TemporaryDirectory(prefix="ferese-presentation-") as directory: + root = Path(directory) + protocols = subprocess.check_output(["pkg-config", "--variable=pkgdatadir", "wayland-protocols"], text=True).strip() + sources = [] + for name, relative in [("xdg-shell", "stable/xdg-shell/xdg-shell.xml"), + ("idle-inhibit", "unstable/idle-inhibit/idle-inhibit-unstable-v1.xml"), + ("idle-notify", "staging/ext-idle-notify/ext-idle-notify-v1.xml")]: + xml = str(Path(protocols) / relative) + for kind, suffix in [("client-header", "client-protocol.h"), ("private-code", "protocol.c")]: + subprocess.run(["wayland-scanner", kind, xml, str(root / f"{name}-{suffix}")], check=True) + sources.append(str(root / f"{name}-protocol.c")) + flags = subprocess.check_output(["pkg-config", "--cflags", "--libs", "wayland-client"], text=True).split() + subprocess.run(["cc", "-Wall", "-Wextra", "-I", str(root), str(REPO / "scripts/tests/fixtures/presentation.c"), + *sources, "-o", str(root / "client"), *flags], check=True) + runtime = root / "runtime" + runtime.mkdir(mode=0o700) + config = root / "config/ferese/config.kdl" + config.parent.mkdir(parents=True) + config.write_text('window-rule app-id="ferese.test.presentation" floating=#true\n' + 'animations { enabled #false; }\n' + 'theme { geometry { window-radius 0; border-width 0; focus-ring-width 0; }; }\n') + # Remembered geometry deliberately places both clients together; + # ordinary placement now avoids overlap. Keep host state isolated. + state = root / "state/ferese" + state.mkdir(parents=True) + (state / "floating.json").write_text(json.dumps({ + "ferese.test.presentation": { + "output": "ferese-winit", "fractions": [.1, .1, .4, .4], + }, + })) + display = Path(os.environ["WAYLAND_DISPLAY"]) + if not display.is_absolute(): + display = Path(os.environ["XDG_RUNTIME_DIR"]) / display + env = dict(os.environ, XDG_RUNTIME_DIR=str(runtime), XDG_CONFIG_HOME=str(root / "config"), XDG_STATE_HOME=str(root / "state"), WAYLAND_DISPLAY=str(display), FERESE_ENABLE_SCREENCOPY="1") + env.pop("WAYLAND_SOCKET", None) + env.pop("FERESE_SHELL_CONTROL_SOCKET", None) + log = (root / "compositor.log").open("w") + try: + compositor = subprocess.Popen([str(REPO / "target/debug/ferese"), "--backend=nested"], env=env, stdout=log, stderr=log, start_new_session=True) + processes.append(compositor) + deadline = time.monotonic() + 10 + while not (runtime / "ferese/control.sock").exists(): + if compositor.poll() is not None or time.monotonic() > deadline: + self.fail((root / "compositor.log").read_text()) + time.sleep(0.02) + sockets = list(runtime.glob("wayland-*")) + env["WAYLAND_DISPLAY"] = str(next(path for path in sockets if not path.name.endswith(".lock"))) + + def call(*args): + return subprocess.check_output([str(REPO / "target/debug/feresectl"), *args], env=env, timeout=10) + + def windows(count): + deadline = time.monotonic() + 10 + while time.monotonic() < deadline: + result = json.loads(call("get-windows")) + if len(result) == count and all(window["mapped"] and window["width"] == 640 for window in result): + return result + time.sleep(0.02) + self.fail((root / "compositor.log").read_text()) + + events = (root / "events").open("w") + target = subprocess.Popen([str(root / "client")], env=env, stdin=subprocess.PIPE, + stdout=events, stderr=log, start_new_session=True) + processes.append(target) + windows(1) + time.sleep(.4) + + def count(event): + return (root / "events").read_text().splitlines().count(event) + + before = count("frame") + idle = count("idle") + time.sleep(.5) + self.assertGreater(count("frame"), before, "visible empty commits must get callbacks") + self.assertEqual(count("idle"), idle, "visible inhibitor must keep the notifier active") + + cover = subprocess.Popen([str(root / "client"), "cover"], env=env, stdout=log, + stderr=log, start_new_session=True) + processes.append(cover) + current = windows(2) + self.assertEqual((current[0]["x"], current[0]["y"]), (current[1]["x"], current[1]["y"])) + time.sleep(.5) + before = count("frame") + time.sleep(.5) + self.assertEqual(count("frame"), before, "fully occluded client must stop receiving callbacks") + self.assertGreater(count("idle"), idle, "occluded inhibitor must stop inhibiting") + + cover.terminate() + cover.wait(timeout=5) + windows(1) + time.sleep(.5) + self.assertGreater(count("frame"), before, "revealed client must resume callbacks") + idle = count("idle") + time.sleep(.4) + self.assertEqual(count("idle"), idle) + target.stdin.write(b"u") + target.stdin.flush() + time.sleep(.6) + self.assertGreater(count("idle"), idle, "unmapped inhibitor must stop inhibiting") + target.stdin.close() + events.close() + finally: + for process in reversed(processes): + if process.poll() is None: + os.killpg(process.pid, signal.SIGTERM) + process.wait(timeout=5) + log.close() + + +if __name__ == "__main__": + unittest.main()