Crafter.Graphics/tests/ConfigureResize/main.cpp

859 lines
41 KiB
C++
Raw Normal View History

fix(wayland): the surface takes a new size with the first frame drawn at it, and a configure's rebuild waits for Render At a resize RebuildSwapchain sent wp_viewport.set_destination for the new size at once, and the configure listener, which had called it, acknowledged the configure and committed the surface. The destination is the surface's size, and it is state the next wl_surface.commit applies whatever buffer is attached then. That commit was the listener's, or the one a consumer makes to ask for a frame callback, and it put the old frame at the new size, where it stayed until the consumer's next frame brought a buffer of that size (#199). The acknowledgement named that commit as the answer to the configure. And the rebuild ran inside libwayland's dispatch: the consumer's loop stood still in it, each new size before a frame cost a swapchain, and a Vulkan error there was thrown from inside the dispatch, where the `try` a consumer has around Update() does not cover it (#200). Window: the configure listener keeps a new size (deferredLogicalWidth and Height, surface-local) and, where a frame is owed, the serial (unackedConfigure), and makes no Vulkan call. Render() takes what waits at its two boundaries (RebuildIfDeferred) and makes the swapchain again where that calls for one: first thing, for what came since the last frame, and after its present, as before, for what its listeners and the present asked for, and now for a size configured while they ran. Right before its present a frame sends what the surface is to take with its buffer (SendFrameSurfaceState): the destination, when the surface's size is not the one last sent (sentLogicalWidth and Height), and the acknowledgement of the configure it answers, the last one when several came. The present's commit then applies them together with the buffer. The constructor sends no destination: the window's first frame does. The surface's size is state of the window for that, beside the buffers' (logicalWidth and Height; logicalConfigured says which of the two sizes the window was given). TakeDeferredSize makes a configured size the surface's, unchanged, and works the pixels out of it with the scale of that moment. MatchSizesToScale does the same before every swapchain is made, and for a window the application sized (the constructor's size, Resize) works the surface's size out of the pixels, which is what RebuildSwapchain and the constructor did when they sent the destination. The destination is that state as it is and not ceil(width / scale) at the present, because the scale is the compositor's to change in any dispatch: between a configure and the frame that takes it, and in the middle of a frame, when a listener reads the clipboard or makes a window. And a size and a scale that change together can come to the pixels the swapchain has: then no swapchain is made, and the frame still brings the surface its size with the acknowledgement. With that the destination of a configured size is the size itself, where it was worked back from the pixels and could come out larger (392x796 at 2.85 came back as 393x797), and a configure at the size the surface has leaves a window the compositor sized alone while the scale stays what it was, where it could make the window grow again (#201). A window the application sized, configured at the size its surface has, takes the pixels for that size once where its own do not come to it (320x240 at 1.75, shown at 183x138, becomes 321x242), and is then left alone in the same way. That much of #201 comes with this change, since the state it asks for is the one this needs. The buffers' size is still rounded up, where the protocol rounds half away from zero: the rest of #201. A swapchain RebuildSwapchain made for another reason after the scale changed (a present mode, a present that found the swapchain out of date) applied the new scale by working the destination out again from the pixels. It still does for a window the application sized. A window the compositor sized keeps the surface's size and takes the pixels for it. The acquire's OUT_OF_DATE path repeated four of RebuildSwapchain's lines and sent no destination: it calls RebuildSwapchain now, and so settles the two sizes as the other rebuilds do. Of a Resize and a configured size the one asked for last is taken, as when the listener resized on the spot: a configure with a size clears a Resize that waits for its frame's present, also when its size is the one the window has, and Resize clears a configured size that waits. A configure that asks for nothing new while nothing is owed is acknowledged in the listener, as before. One that comes while a size waits, or while the surface does not show the window as it is (a swapchain still to be made, one that has presented no frame yet, a size not sent yet), waits for that frame. One for the size the window has, when nothing else is owed, is acknowledged at once and takes the wait away with it, as an acknowledgement of a serial older than the last is a protocol error. A frame being recorded when a size comes does not acknowledge it while that size waits; when a Resize later in the frame takes the size's place, that frame does. The listener still commits, which applies whatever else is pending on the surface, such as a request for a frame callback that StartUpdate made after a frame. For consumers: onResize for a compositor's resize fires from inside Render(), before the onUpdate of the frame that brings the size, with the queue idle and no frame being recorded, as Resize fires it; until that frame the window keeps its old size on screen. A window that draws on demand took onResize as its cue to ask for a frame, and gets onFrameOwed for it: the Wayland backend's alone, fired from the configure after onToplevelState, and only for a window that is still there after that event's listeners, whenever the configure leaves a frame owed. A size the surface does not have yet that comes to the pixels the window has (the scale changed with it) fires onFrameOwed and no onResize. Resize and SetPresentMode outside a frame rebuild at once as they did, and there too the surface takes the new size with the next frame. A rebuild that throws stays pending, since RebuildSwapchain clears swapchainRebuildPending only once the swapchain is made, and the next Render() tries it again. The README, the consumer notes and the guide's gotchas say so, and the bug-prover lens section lists the pending surface state and the scale among the re-entry contexts. Examples: InputSystem and the native mains of VulkanTriangle, RTVolume and Sponza drew one frame and then only ran the event loop, so nothing would have answered a compositor's resize of their windows. They call StartUpdate after that frame, as the examples that draw every frame do. Not changed: a scale that changes by itself still reaches the buffers and the surface only with the next swapchain or the next configured size (#165); the first configure's size (#166); and Win32's WM_SIZE, which still rebuilds from the window procedure. A window under StartUpdate on the compositor's clock still draws from its frame callback, so for it the rebuild, like every frame's Vulkan call, stays inside libwayland's dispatch; one on its own clock is updated by StartSync's loop after the dispatch, as before. #200 is answered for a consumer that calls Update() itself. A present that fails after SendFrameSurfaceState leaves what that sent pending for the surface's next commit, whoever makes it. Tests: ConfigureResize feeds the configure callbacks of a window whose surfaces are proxies on a wl_display over a socketpair, as ToplevelState does, with a made-up swapchain handle and no device, steps the two ends of a frame, and reads the requests off the wire: a configure for a new size sends one bare commit; the frame sends the destination and the acknowledgement before its present, once; several configures before a frame cost one of each, of the last serial; a configure for nothing new, or back to the window's size, is acknowledged in the listener, and the serial that waited never is; a frame being recorded when a size comes does not acknowledge it while the size waits, and does once a Resize has taken its place; the window's first frame and the first at any new size of the surface bring the destination, and a configure before it waits, and so does one for the size a rebuild that threw is still pending for, and one that comes after a present that found the swapchain out of date, whose surface state is not sent a second time; without a viewport the acknowledgement still waits. The destination is the configured size when the scale changes before the frame, when it changes between the frame's start and its present, and when the size and the scale together come to the swapchain's pixels, in either order of the two events, which makes no swapchain; with the numbers of #201's trace, 392x796 at 2.85, it is that size, and three more configures at it change nothing. A swapchain made after the scale changed keeps a configured window's surface size and an application-sized window's pixels. Of a Resize and a configured size the later one is taken, also when it is the size the window has. onFrameOwed fires after onToplevelState and not for a window its listener destroyed; every acknowledgement names a later serial than the one before. ResizeViewport runs a window against the compositor with WAYLAND_DEBUG set and stderr in a file, and replays the log, which has the driver's attach and commit too: at every commit the destination in effect belongs to the buffer in effect, to within a surface-local pixel, and every acknowledgement of a configure that asked for a size, once the surface has a buffer, is committed with a buffer of that size in effect and with that size itself as the destination, through a Resize with a commit before the next frame, a size the compositor configures (the window asks to be maximized), and one it configures while the frame loop of StartUpdate runs (the window asks to be restored), which that loop takes and answers by itself. Where the compositor configures no other size at that request, the step is a SKIP, and the test holds the log to it: no configure for another size came. `ResizeViewport --keyboard` is the manual probe for a phone: it asks for text input and takes it back three times (SetTextInput), which is what brings a phone's on-screen keyboard up and down, and holds the resizes that makes to the same checks.
2026-11-19 04:47:00 +01:00
//SPDX-License-Identifier: LGPL-3.0-only
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// Issue #199: a size the compositor configures is answered by the frame drawn
// at it. The configure listener used to make the swapchain again on the spot,
// acknowledge the configure, send the viewport's destination for the new size
// and commit the surface. The destination is the surface's size, and that
// commit applied it to the buffer still attached: the old frame was shown
// scaled to the new size until the consumer's next frame brought a buffer of
// that size. The acknowledgement had named the old frame as the answer to
// the configure, and the rebuild's Vulkan calls ran, and threw, inside
// libwayland's dispatch (#200).
//
// Now the listener keeps the size and the serial, and Render() does the rest
// at its boundaries: RebuildIfDeferred takes the size (TakeDeferredSize) and
// makes the swapchain again, and SendFrameSurfaceState sends the destination
// and the acknowledgement right before the present, whose commit applies
// them with the frame's buffer. The surface's size is state of the window
// for that (logicalWidth, logicalHeight): a size the compositor configured
// is sent as the destination unchanged, and is not worked back from the
// pixels and a scale that may have changed since. This test drives those
// pieces without a swapchain: the window is default-constructed with a
// made-up swapchain handle, its surfaces are proxies on a wl_display
// connected to one end of a socketpair, as in ToplevelState, and the test
// calls the listener callbacks itself and reads the requests off the other
// end. `Begin` and `Present` below stand for the two ends of Render();
// ResizeViewport runs the real one against a compositor. Asserts:
//
// - a configure for a new size sends one bare commit: no destination, no
// acknowledgement, and no Vulkan call (there is no device here, and the
// old listener's rebuild would have crashed on it). The size and the
// serial wait, the window's size stays the swapchain's, and onFrameOwed
// fires
// - the frame takes the size and sends the destination and the
// acknowledgement before its present, once
// - several configures before a frame cost one rebuild and one
// acknowledgement, of the last serial, and a configure without a size
// among them waits with the others
// - a configure that asks for nothing new while nothing is owed is
// acknowledged in the listener, before its commit, and fires nothing
// - a configure back to the size the window has takes the wait away: it
// is acknowledged at once, and the serial that waited never is, which
// would be a protocol error after a later one
// - a frame being recorded when a size comes does not acknowledge it
// - a window's first frame brings the destination (the constructor sends
// none), and so does the first frame at any new size; a configure that
// comes before that frame waits for it
// - a configure for the size a rebuild that threw is still pending for
// waits for the frame as well, and so does one that comes after a
// present that found the swapchain out of date, whose surface state is
// not sent a second time
// - without a viewport the acknowledgement still waits for the frame
// - the size is turned into pixels with the scale of the moment the frame
// takes it, and the destination is the configured size whatever the
// scale does: when it changes before the frame, between the frame's
// start and its present (a listener that dispatches events), and when
// the new size at the new scale comes to the pixels the swapchain has,
// in either order of the two events. That last frame makes no swapchain
// and still brings the surface its size
// - on a fractional scale the destination is the configured size and not a
// pixel more, and a window configured at the size it has is left alone
// (the numbers of #201's phone trace)
// - a swapchain made for another reason after the scale changed: a window
// the compositor sized keeps the surface's size and takes the pixels for
// it, one the application sized keeps its pixels and the surface takes
// the size they come to
// - of a Resize from a listener of the frame and a configured size, the
// one asked for last is taken, also when it is the size the window has
// - onFrameOwed fires after onToplevelState, with the states set, and not
// at all for a window an onToplevelState listener destroyed
// - every acknowledgement names a later serial than the one before
//
// No compositor and no GPU. Needs the Wayland backend, hence Linux only.
#include <wayland-client.h>
#include <sys/socket.h>
#include <unistd.h>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include "../../lib/xdg-shell-client-protocol.h"
#include "../../lib/viewporter.h"
import Crafter.Graphics;
import Crafter.Event;
import std;
using namespace Crafter;
namespace {
int failures = 0;
void Check(bool ok, std::string_view what) {
std::println("{} {}", ok ? "PASS" : "FAIL", what);
if (!ok) ++failures;
}
using Requests = std::vector<std::string>;
// `got` against `expected`, printing what came when they differ.
void Expect(const Requests& got, const Requests& expected, std::string_view what) {
Check(got == expected, what);
if (got != expected) {
std::println(" sent: {}", got);
std::println(" expected: {}", expected);
}
}
Requests Sorted(Requests requests) {
std::ranges::sort(requests);
return requests;
}
// Every acknowledgement the test read off the wire, in order.
std::vector<std::uint32_t> acknowledged;
// A compositor's serials: one counter, never reused.
std::uint32_t lastSerial = 100;
// A window as its constructor leaves it: the first configure taken, a
// swapchain made, no frame presented yet. The swapchain is a handle nothing
// dereferences and a generation; the surface, the xdg_surface, the toplevel
// and the viewport are proxies nothing answers.
struct Fixture {
wl_display* display;
int peer;
std::optional<Window> holder;
std::uint32_t surfaceId = 0;
std::uint32_t xdgSurfaceId = 0;
std::uint32_t viewportId = 0;
int owed = 0;
std::optional<EventListener<void>> owedSub;
Fixture(wl_display* display, wl_registry* registry, int peer, float scale, std::uint32_t width, std::uint32_t height, bool viewport = true)
: display(display), peer(peer) {
Window& window = holder.emplace();
window.surface = static_cast<wl_surface*>(wl_registry_bind(registry, 1, &wl_surface_interface, 1));
window.xdgSurface = static_cast<xdg_surface*>(wl_registry_bind(registry, 2, &xdg_surface_interface, 1));
window.xdgToplevel = static_cast<xdg_toplevel*>(wl_registry_bind(registry, 3, &xdg_toplevel_interface, 1));
if (viewport) {
window.wpViewport = static_cast<wp_viewport*>(wl_registry_bind(registry, 4, &wp_viewport_interface, 1));
viewportId = wl_proxy_get_id(reinterpret_cast<wl_proxy*>(window.wpViewport));
}
surfaceId = wl_proxy_get_id(reinterpret_cast<wl_proxy*>(window.surface));
xdgSurfaceId = wl_proxy_get_id(reinterpret_cast<wl_proxy*>(window.xdgSurface));
window.scale = scale;
window.width = width;
window.height = height;
// The configure the constructor waits for, before there is a swapchain.
Configure(0, 0);
// The surface's size, from the constructor's pixels.
window.MatchSizesToScale();
window.swapChain = reinterpret_cast<decltype(window.swapChain)>(static_cast<std::uintptr_t>(0x1));
window.swapchainGeneration = 1;
// Where the constructor lists it, and where the handler looks for a
// window that outlived its listeners.
Device::windows.push_back(&window);
owedSub.emplace(&window.onFrameOwed, [this] { ++owed; });
Sent();
}
~Fixture() {
// ~Window destroys the proxies; without a device it leaves the
// swapchain handle alone.
holder.reset();
Sent();
}
Window& window() { return *holder; }
// The compositor's two events of one batch. Returns the serial.
std::uint32_t Configure(std::int32_t width, std::int32_t height, std::initializer_list<std::uint32_t> states = {}) {
Window& window = *holder;
wl_array array;
wl_array_init(&array);
for (std::uint32_t state : states) {
*static_cast<std::uint32_t*>(wl_array_add(&array, sizeof(state))) = state;
}
// The handles before the calls: the second may destroy the window.
xdg_surface* const surface = window.xdgSurface;
Window::xdg_toplevel_configure(&window, window.xdgToplevel, width, height, &array);
wl_array_release(&array);
const std::uint32_t serial = ++lastSerial;
Window::xdg_surface_handle_configure(&window, surface, serial);
return serial;
}
// What RebuildSwapchain does besides its Vulkan calls: the window's two
// sizes from the scale of the moment, and a swapchain, which
// CreateSwapchain numbers, after which none waits to be made.
void Rebuild() {
Window& window = *holder;
window.MatchSizesToScale();
++window.swapchainGeneration;
window.swapchainRebuildPending = false;
}
// The start of Render(), which is RebuildIfDeferred: the size that waits
// becomes the window's, and new pixels, or a rebuild that waited, make a
// new swapchain. True when one was made.
bool Begin() {
Window& window = *holder;
if (!window.TakeDeferredSize() && !window.swapchainRebuildPending) return false;
Rebuild();
return true;
}
// The end of Render(): the surface state for the frame, then the
// present, after which the surface shows this swapchain.
void Present() {
Window& window = *holder;
window.SendFrameSurfaceState();
window.presentedGeneration = window.swapchainGeneration;
}
// A whole frame.
void Frame() {
Begin();
Present();
}
// What the window has sent to its surface, its xdg_surface and its
// viewport since the last call, in order: "commit", "ack <serial>",
// "destination <w>x<h>". A request is its object id, then its size and
// opcode in one word, then its arguments.
Requests Sent() {
wl_display_flush(display);
std::vector<std::uint8_t> wire;
std::uint8_t chunk[4096];
for (ssize_t n; (n = recv(peer, chunk, sizeof(chunk), MSG_DONTWAIT)) > 0;) {
wire.insert(wire.end(), chunk, chunk + n);
}
Requests requests;
for (std::size_t at = 0; at + 8 <= wire.size();) {
std::uint32_t header[2];
std::memcpy(header, wire.data() + at, sizeof(header));
const std::uint32_t size = header[1] >> 16;
const std::uint32_t opcode = header[1] & 0xFFFF;
if (size < 8 || at + size > wire.size()) break;
const auto argument = [&](std::size_t index) {
std::int32_t value = 0;
if (8 + 4 * (index + 1) <= size) std::memcpy(&value, wire.data() + at + 8 + 4 * index, sizeof(value));
return value;
};
if (header[0] == surfaceId && opcode == WL_SURFACE_COMMIT) {
requests.push_back("commit");
} else if (header[0] == xdgSurfaceId && opcode == XDG_SURFACE_ACK_CONFIGURE) {
requests.push_back(std::format("ack {}", static_cast<std::uint32_t>(argument(0))));
acknowledged.push_back(static_cast<std::uint32_t>(argument(0)));
} else if (viewportId != 0 && header[0] == viewportId && opcode == WP_VIEWPORT_SET_DESTINATION) {
requests.push_back(std::format("destination {}x{}", argument(0), argument(1)));
}
at += size;
}
return requests;
}
};
bool NothingWaits(const Window& window) {
return window.deferredLogicalWidth == 0 && window.deferredLogicalHeight == 0 && !window.unackedConfigure
&& window.presentedGeneration == window.swapchainGeneration
&& window.sentLogicalWidth == window.logicalWidth && window.sentLogicalHeight == window.logicalHeight;
}
// The scale as the compositor names it, in 120ths.
void Scale(Window& window, std::uint32_t scale120) {
Window::xdg_surface_handle_preferred_scale(&window, nullptr, scale120);
}
} // namespace
int main() {
int fds[2];
if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, fds) != 0) {
std::println("FAIL socketpair: {}", std::strerror(errno));
return EXIT_FAILURE;
}
// The display owns fds[0] from here.
wl_display* display = wl_display_connect_to_fd(fds[0]);
if (display == nullptr) {
std::println("FAIL wl_display_connect_to_fd: {}", std::strerror(errno));
return EXIT_FAILURE;
}
// libwayland makes whatever proxy it is asked to bind; nothing answers.
wl_registry* registry = wl_display_get_registry(display);
const auto fixture = [&](float scale, std::uint32_t width, std::uint32_t height, bool viewport = true) {
return Fixture(display, registry, fds[1], scale, width, height, viewport);
};
// The window's first frame: the constructor sends no destination, the
// frame does. Every fixture after this one has it behind it.
{
Fixture f = fixture(2.0f, 640, 480);
f.Present();
Expect(f.Sent(), { "destination 320x240" }, "a window's first frame brings the viewport's destination");
Check(NothingWaits(f.window()), "after it nothing is owed");
f.Frame();
Expect(f.Sent(), {}, "the frames after it send nothing");
}
// A new size, and the frame that answers it.
{
Fixture f = fixture(2.0f, 640, 480);
f.Frame();
f.Sent();
Window& window = f.window();
const std::uint32_t serial = f.Configure(400, 300);
Expect(f.Sent(), { "commit" },
"a configure for a new size: one bare commit, with neither the surface's new size nor an acknowledgement");
Check(window.width == 640 && window.height == 480,
std::format("the window's size is still the swapchain's ({}x{})", window.width, window.height));
Check(window.deferredLogicalWidth == 400 && window.deferredLogicalHeight == 300 && window.unackedConfigure == serial,
"the size and the serial wait for a frame");
Check(f.owed == 1, std::format("onFrameOwed fired once ({})", f.owed));
Check(f.Begin(), "the next frame takes the size");
Check(window.width == 800 && window.height == 600 && window.deferredLogicalWidth == 0 && window.deferredLogicalHeight == 0,
std::format("the window has it in pixels ({}x{} at scale 2), and no size waits", window.width, window.height));
Expect(f.Sent(), {}, "taking it sends nothing");
f.Present();
Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 400x300" }),
"before its present the frame sends the destination and the acknowledgement, and does not commit");
Check(NothingWaits(window), "after the frame nothing is owed");
f.Frame();
Expect(f.Sent(), {}, "the frames after it send neither again");
Check(f.owed == 1, "onFrameOwed fired for the configure only");
}
// Several configures before a frame.
{
Fixture f = fixture(1.0f, 320, 240);
f.Frame();
f.Sent();
Window& window = f.window();
f.Configure(400, 300);
f.Configure(500, 350);
const std::uint32_t last = f.Configure(0, 0, { XDG_TOPLEVEL_STATE_ACTIVATED });
Expect(f.Sent(), { "commit", "commit", "commit" }, "three configures before a frame: three bare commits");
Check(window.deferredLogicalWidth == 500 && window.deferredLogicalHeight == 350,
"the last size asked for waits, through a configure without a size");
Check(window.unackedConfigure == last, "the last serial waits, the one without a size");
Check(f.owed == 3, std::format("onFrameOwed fired for each ({})", f.owed));
Check(f.Begin() && window.width == 500 && window.height == 350, "one frame takes the last size");
f.Present();
Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", last), "destination 500x350" }),
"and sends one destination and one acknowledgement, of the last serial");
Check(NothingWaits(window), "after the frame nothing is owed");
}
// Nothing new while nothing is owed: answered in the listener, as before.
{
Fixture f = fixture(2.0f, 640, 480);
f.Frame();
f.Sent();
Window& window = f.window();
const std::uint32_t none = f.Configure(0, 0, { XDG_TOPLEVEL_STATE_ACTIVATED });
Expect(f.Sent(), { std::format("ack {}", none), "commit" },
"a configure without a size while nothing is owed: acknowledged in the listener, before its commit");
const std::uint32_t same = f.Configure(320, 240);
Expect(f.Sent(), { std::format("ack {}", same), "commit" },
"a configure for the size the window has: the same");
Check(NothingWaits(window) && f.owed == 0, "neither leaves a frame owed or fires onFrameOwed");
Check(!f.Begin(), "and the next frame makes no swapchain");
f.Present();
Expect(f.Sent(), {}, "nor sends anything");
}
// The compositor goes back to the size the window has before a frame.
{
Fixture f = fixture(1.0f, 320, 240);
f.Frame();
f.Sent();
Window& window = f.window();
const std::uint32_t first = f.Configure(400, 300);
f.Sent();
const std::uint32_t back = f.Configure(320, 240);
Expect(f.Sent(), { std::format("ack {}", back), "commit" },
"a configure back to the window's size: acknowledged at once, as the surface shows that size");
Check(NothingWaits(window), "the size and the serial that waited are gone");
Check(!f.Begin(), "the next frame makes no swapchain");
f.Present();
Expect(f.Sent(), {}, std::format("and never acknowledges {}, the serial before the one acknowledged", first));
}
// A size that comes while a frame is being recorded.
{
Fixture f = fixture(1.0f, 320, 240);
f.Frame();
f.Sent();
Window& window = f.window();
f.Begin();
const std::uint32_t serial = f.Configure(400, 300);
f.Present();
Expect(f.Sent(), { "commit" },
"a frame being recorded when a size comes: its present sends neither a destination nor an acknowledgement");
Check(window.unackedConfigure == serial && window.deferredLogicalWidth == 400, "the size and the serial wait on");
Check(f.Begin() && window.width == 400 && window.height == 300, "the frame after it takes the size");
f.Present();
Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 400x300" }), "and answers the configure");
}
// A configure before the window's first frame.
{
Fixture f = fixture(2.0f, 640, 480);
const std::uint32_t serial = f.Configure(0, 0, { XDG_TOPLEVEL_STATE_ACTIVATED });
Expect(f.Sent(), { "commit" }, "a configure before the window's first frame: not acknowledged in the listener");
Check(f.owed == 1, "onFrameOwed fired for it");
f.Present();
Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 320x240" }),
"the first frame brings the destination and the acknowledgement");
}
// A swapchain made between frames, as Resize makes one outside a frame:
// its first frame brings the destination, and a configure before that
// frame waits for it.
{
Fixture f = fixture(2.0f, 640, 480);
f.Frame();
f.Sent();
Window& window = f.window();
window.width = 800;
window.height = 500;
window.logicalConfigured = false;
f.Rebuild();
Expect(f.Sent(), {}, "a swapchain made at a new size sends the surface nothing");
const std::uint32_t serial = f.Configure(0, 0);
Expect(f.Sent(), { "commit" }, "a configure while a new swapchain waits for its first frame: not acknowledged in the listener");
Check(!f.Begin(), "no size waits for the frame");
f.Present();
Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 400x250" }),
"the swapchain's first frame brings its destination and the acknowledgement");
// The same size again, as SetPresentMode makes a swapchain: the
// surface has its size, and a configure still waits for a buffer of
// the swapchain there is.
f.Rebuild();
const std::uint32_t second = f.Configure(400, 250);
Expect(f.Sent(), { "commit" }, "a configure for the window's size while a swapchain waits for its first frame: not acknowledged in the listener either");
Check(!f.Begin(), "nor is there a size for the frame to take");
f.Present();
Expect(f.Sent(), { std::format("ack {}", second) }, "that frame acknowledges it and sends no destination: the surface's size is the one it has");
}
// A rebuild that threw leaves the window's sizes ahead of the swapchain
// and stays pending for the next Render(). A configure for that size is
// not answered by what the surface shows: it waits for the frame too.
{
Fixture f = fixture(2.0f, 640, 480);
f.Frame();
f.Sent();
Window& window = f.window();
// A size is taken and its rebuild throws: RebuildSwapchain has set
// its flag and made nothing.
f.Configure(400, 300);
Check(window.TakeDeferredSize() && window.width == 800 && window.height == 600, "a size is taken, and the rebuild for it throws");
window.swapchainRebuildPending = true;
f.Sent();
const std::uint32_t serial = f.Configure(400, 300);
Expect(f.Sent(), { "commit" },
"a configure for the size a pending rebuild is for: not acknowledged in the listener, though the window's size says so already");
Check(window.deferredLogicalWidth == 0 && window.unackedConfigure == serial && f.owed == 2,
"no size waits, the serial does, and onFrameOwed fired");
Check(f.Begin() && window.width == 800 && window.height == 600 && !window.swapchainRebuildPending,
"the next frame makes the swapchain that waited");
f.Present();
Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 400x300" }),
"the frame of the swapchain made at last brings the destination and the acknowledgement");
}
// A present that finds the swapchain out of date queues nothing, and
// Render() makes the swapchain again after it. What went out before
// that present is not sent a second time, and a configure that comes in
// between waits for the first frame of the swapchain made after it.
{
Fixture f = fixture(2.0f, 640, 480);
f.Frame();
f.Sent();
Window& window = f.window();
const std::uint32_t serial = f.Configure(400, 300);
f.Sent();
Check(f.Begin(), "a frame takes a size");
// The present that follows returns VK_ERROR_OUT_OF_DATE_KHR:
// presentedGeneration stays, and a rebuild waits.
window.SendFrameSurfaceState();
window.swapchainRebuildPending = true;
Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 400x300" }),
"its destination and acknowledgement go out before its present, which finds the swapchain out of date");
const std::uint32_t next = f.Configure(400, 300);
Expect(f.Sent(), { "commit" }, "a configure for that size before the swapchain is made again: not acknowledged in the listener");
Check(f.Begin(), "the next frame makes the swapchain");
f.Present();
Expect(f.Sent(), { std::format("ack {}", next) }, "and acknowledges, without sending the destination a second time");
Check(NothingWaits(window), "after it nothing is owed");
}
// Without wp_viewporter there is no destination, and the scale is 1.
{
Fixture f = fixture(1.0f, 320, 240, false);
f.Frame();
Expect(f.Sent(), {}, "without a viewport the first frame sends nothing");
Window& window = f.window();
const std::uint32_t serial = f.Configure(400, 300);
Expect(f.Sent(), { "commit" }, "without a viewport: a configure for a new size still waits for its frame");
Check(f.Begin() && window.width == 400 && window.height == 300, "the frame takes the size");
f.Present();
Expect(f.Sent(), { std::format("ack {}", serial) }, "and acknowledges, with no destination");
}
// The scale that counts for the pixels is the one of the moment the frame
// takes the size. The destination is the configured size whatever the
// scale does.
{
Fixture f = fixture(2.0f, 640, 480);
f.Frame();
f.Sent();
Window& window = f.window();
const std::uint32_t serial = f.Configure(400, 300);
Scale(window, 180);
Check(f.Begin() && window.width == 600 && window.height == 450,
std::format("a scale that changes before the frame: the size is taken at the new one ({}x{} at 1.5)", window.width, window.height));
f.Sent();
f.Present();
Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 400x300" }),
"and the destination is the size the compositor configured");
}
// The scale changes between the frame's start and its present: a
// listener of the frame dispatched the event (it made a window, or read
// the clipboard). The swapchain was made for the old scale, and the
// destination is still the configured size, not that swapchain's pixels
// over the new scale (534x400).
{
Fixture f = fixture(2.0f, 640, 480);
f.Frame();
f.Sent();
Window& window = f.window();
const std::uint32_t serial = f.Configure(400, 300);
Check(f.Begin() && window.width == 800 && window.height == 600, "the frame takes a size at scale 2");
Scale(window, 180);
f.Sent();
f.Present();
Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 400x300" }),
"a scale that changes while the frame is recorded: its present still sends the configured size");
Check(NothingWaits(window), "and nothing is owed after it");
}
// A new size and a new scale that together come to the pixels the
// swapchain has: an output whose scale changes under a window that fills
// it. No swapchain is made, and the surface still has to be told its new
// size, in the commit that acknowledges the configure. First the
// configure, then the scale.
{
Fixture f = fixture(2.0f, 640, 480);
f.Frame();
Expect(f.Sent(), { "destination 320x240" }, "a window of 640x480 pixels at scale 2 is 320x240");
Window& window = f.window();
const std::uint32_t serial = f.Configure(640, 480);
Expect(f.Sent(), { "commit" }, "a configure for 640x480 at scale 2: a new size, which waits");
Check(f.owed == 1, "onFrameOwed fired");
Scale(window, 120);
Check(!f.Begin() && window.width == 640 && window.height == 480,
"the scale becomes 1 before the frame: the size comes to the pixels the swapchain has, and none is made");
f.Present();
Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 640x480" }),
"the frame brings the surface its new size with the acknowledgement");
Check(NothingWaits(window), "after it nothing is owed");
const std::uint32_t again = f.Configure(640, 480);
Expect(f.Sent(), { std::format("ack {}", again), "commit" }, "and the same configure again is answered by what the surface shows");
}
// The other order: the scale first, then the configure, whose size is in
// pixels the one the swapchain has.
{
Fixture f = fixture(1.5f, 600, 450);
f.Frame();
Expect(f.Sent(), { "destination 400x300" }, "a window of 600x450 pixels at scale 1.5 is 400x300");
Window& window = f.window();
Scale(window, 240);
const std::uint32_t serial = f.Configure(300, 225);
Expect(f.Sent(), { "commit" },
"the scale becomes 2, then a configure for 300x225, the pixels the swapchain has: not acknowledged in the listener, as the surface is 400x300");
Check(window.deferredLogicalWidth == 300 && window.deferredLogicalHeight == 225 && f.owed == 1, "the size waits, and onFrameOwed fired");
Check(!f.Begin() && window.width == 600 && window.height == 450, "the frame makes no swapchain");
f.Present();
Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 300x225" }),
"and brings the surface its new size with the acknowledgement");
}
// The size the surface has, after the scale changed: the buffers are
// made again for the new scale, and the surface is sent nothing.
{
Fixture f = fixture(2.0f, 640, 480);
f.Frame();
f.Sent();
Window& window = f.window();
Scale(window, 180);
const std::uint32_t serial = f.Configure(320, 240);
Expect(f.Sent(), { "commit" }, "a configure for the surface's size after the scale changed: it waits for buffers at the new scale");
Check(f.Begin() && window.width == 480 && window.height == 360,
std::format("the frame makes the swapchain again for the scale ({}x{} at 1.5)", window.width, window.height));
f.Present();
Expect(f.Sent(), { std::format("ack {}", serial) }, "and acknowledges, with no destination: the surface's size is the one it has");
}
// A fractional scale, with the numbers of #201's trace: a phone at 2.85
// whose maximized window is configured 392x796. The destination is that
// size, where it used to be worked back from the 1118x2269 pixels to
// 393x797, and the window is left alone when the compositor configures
// it at the size it has, where it used to grow with each such configure
// until it was 400x800.
{
Fixture f = fixture(2.85f, 1140, 2280);
f.Frame();
Expect(f.Sent(), { "destination 400x800" }, "a window of 1140x2280 pixels at scale 2.85 is 400x800");
Window& window = f.window();
const std::uint32_t serial = f.Configure(392, 796);
f.Sent();
Check(f.Begin() && window.width == 1118 && window.height == 2269,
std::format("392x796 at scale 2.85 is taken as {}x{} pixels", window.width, window.height));
f.Present();
Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 392x796" }),
"and the destination is 392x796, the configured size itself");
for (int i = 0; i < 3; ++i) {
const std::uint32_t again = f.Configure(392, 796);
Expect(f.Sent(), { std::format("ack {}", again), "commit" }, "the compositor configures the window at the size it has: acknowledged at once");
Check(!f.Begin() && window.width == 1118 && window.height == 2269, "and the window does not grow");
f.Present();
Expect(f.Sent(), {}, "nor is the surface sent anything");
}
}
// A window the application sized, whose pixels do not come to a whole
// surface-local size: 320x240 at 1.75 is shown at 183x138. When the
// compositor configures that size, the window takes the pixels for it
// once, and is then left alone.
{
Fixture f = fixture(1.75f, 320, 240);
f.Frame();
Expect(f.Sent(), { "destination 183x138" }, "a window of 320x240 pixels at scale 1.75 is shown at 183x138");
Window& window = f.window();
const std::uint32_t serial = f.Configure(183, 138);
f.Sent();
Check(f.Begin() && window.width == 321 && window.height == 242,
std::format("configured at 183x138, it takes the pixels for that size ({}x{})", window.width, window.height));
f.Present();
Expect(f.Sent(), { std::format("ack {}", serial) }, "and the surface, which has that size, is sent no destination");
const std::uint32_t again = f.Configure(183, 138);
Expect(f.Sent(), { std::format("ack {}", again), "commit" }, "the same configure again is acknowledged at once");
Check(!f.Begin() && window.width == 321 && window.height == 242, "and changes nothing");
}
// A swapchain made for another reason (a present mode, a swapchain out
// of date) after the scale changed, with no configure in between. A
// window the compositor sized keeps the surface's size and takes the
// pixels for it at the new scale; one the application sized keeps its
// pixels, and the surface takes the size they come to, as it always did.
{
Fixture f = fixture(2.0f, 640, 480);
f.Frame();
f.Configure(400, 300);
f.Frame();
f.Sent();
Window& window = f.window();
Check(window.width == 800 && window.height == 600 && window.logicalConfigured, "a window the compositor sized, 400x300 at scale 2");
Scale(window, 180);
f.Rebuild();
Check(window.width == 600 && window.height == 450 && window.logicalWidth == 400 && window.logicalHeight == 300,
std::format("a swapchain made after the scale became 1.5: the pixels for 400x300 at it ({}x{})", window.width, window.height));
f.Present();
Expect(f.Sent(), {}, "and the surface, still 400x300, is sent nothing");
}
{
Fixture f = fixture(2.0f, 640, 480);
f.Frame();
f.Sent();
Window& window = f.window();
Check(!window.logicalConfigured, "a window the application sized, 640x480 pixels at scale 2");
Scale(window, 120);
f.Rebuild();
Check(window.width == 640 && window.height == 480, "a swapchain made after the scale became 1: the pixels stay");
f.Present();
Expect(f.Sent(), { "destination 640x480" }, "and the surface takes the size they come to at it");
}
// A Resize from a listener of the frame waits in pixels, for that
// frame's present.
{
Fixture f = fixture(2.0f, 640, 480);
f.Frame();
f.Sent();
Window& window = f.window();
window.recording = true;
window.Resize(1000, 700);
window.recording = false;
Check(window.width == 640 && window.deferredWidth == 1000, "a Resize during a frame waits");
Check(f.Begin() && window.width == 1000 && window.height == 700 && window.deferredWidth == 0 && window.deferredHeight == 0,
"and is taken at the frame boundary");
f.Present();
Expect(f.Sent(), { "destination 500x350" }, "the first frame at it brings the surface the size the pixels come to");
}
// Of a Resize and a configured size, the one asked for last is taken.
// The Resize first: the compositor's later size takes its place.
{
Fixture f = fixture(2.0f, 640, 480);
f.Frame();
f.Sent();
Window& window = f.window();
window.recording = true;
window.Resize(1200, 800);
window.recording = false;
f.Configure(400, 300);
Check(window.deferredWidth == 0 && window.deferredHeight == 0 && window.deferredLogicalWidth == 400,
"a configure after a Resize that waits: the compositor's size waits in its place");
Check(f.Begin() && window.width == 800 && window.height == 600,
std::format("and is the one taken ({}x{})", window.width, window.height));
Check(!f.Begin(), "a second take finds nothing");
}
// The same when the compositor's later size is the one the window has:
// the Resize is dropped then too, and the window stays as it is. The
// configure comes while the frame is being recorded, as the Resize did.
{
Fixture f = fixture(1.0f, 800, 600);
f.Frame();
f.Sent();
Window& window = f.window();
window.recording = true;
window.Resize(1000, 700);
const std::uint32_t serial = f.Configure(800, 600);
Expect(f.Sent(), { std::format("ack {}", serial), "commit" },
"a configure for the size the window has, after a Resize that waits: acknowledged at once, as the surface shows that size");
Check(window.deferredWidth == 0 && window.deferredHeight == 0, "the Resize waits no more");
f.Present();
window.recording = false;
Check(!f.Begin() && window.width == 800 && window.height == 600,
std::format("after the frame the window is still the size the compositor configured ({}x{})", window.width, window.height));
Expect(f.Sent(), {}, "and nothing more is sent");
}
// The configure first, then a Resize in the same frame: the
// application's later size takes the compositor's place. The serial is
// acknowledged by the frame being recorded, which no size waits behind
// any more.
{
Fixture f = fixture(2.0f, 640, 480);
f.Frame();
f.Sent();
Window& window = f.window();
window.recording = true;
const std::uint32_t serial = f.Configure(400, 300);
window.Resize(1000, 700);
Check(window.deferredLogicalWidth == 0 && window.deferredLogicalHeight == 0 && window.deferredWidth == 1000,
"a Resize after a configure that waits: the application's size waits in its place");
f.Sent();
f.Present();
window.recording = false;
Expect(f.Sent(), { std::format("ack {}", serial) }, "the frame acknowledges the configure");
Check(f.Begin() && window.width == 1000 && window.height == 700 && !window.logicalConfigured,
std::format("and the size taken after it is the application's ({}x{})", window.width, window.height));
f.Present();
Expect(f.Sent(), { "destination 500x350" }, "which the surface takes with its first frame");
}
// And a Resize outside a frame to the size the window has: the
// compositor's size that waited is dropped, and nothing is rebuilt.
{
Fixture f = fixture(2.0f, 640, 480);
f.Frame();
f.Sent();
Window& window = f.window();
const std::uint32_t serial = f.Configure(400, 300);
f.Sent();
window.Resize(640, 480);
Check(window.deferredLogicalWidth == 0 && window.deferredLogicalHeight == 0 && window.unackedConfigure == serial,
"a Resize to the size the window has, after a configure that waits: the size waits no more, the serial does");
Check(!f.Begin() && window.width == 640 && window.height == 480, "the next frame makes no swapchain");
f.Present();
Expect(f.Sent(), { std::format("ack {}", serial) }, "and acknowledges the configure");
}
// The events: onToplevelState first, then onFrameOwed with the states set.
{
Fixture f = fixture(1.0f, 320, 240);
f.Frame();
f.Sent();
Window& window = f.window();
std::vector<std::string> order;
EventListener<void> stateSub(&window.onToplevelState, [&] { order.push_back("state"); });
EventListener<void> owedSub(&window.onFrameOwed, [&] { order.push_back(window.activated ? "owed, activated" : "owed"); });
f.Configure(400, 300, { XDG_TOPLEVEL_STATE_ACTIVATED });
Check(order == std::vector<std::string>{ "state", "owed, activated" },
"onFrameOwed fires after onToplevelState, with the configure's states set");
if (order != std::vector<std::string>{ "state", "owed, activated" }) std::println(" fired: {}", order);
}
// A listener of onToplevelState destroys the window: the handler fires
// nothing more on it. Another window is made in its place, at the same
// address, so an event fired through the old pointer would show.
{
Fixture f = fixture(1.0f, 320, 240);
f.Frame();
f.Sent();
int firedOnReplacement = 0;
std::optional<EventListener<void>> replacementSub;
EventListener<void> stateSub(&f.window().onToplevelState, [&] {
f.holder.reset();
Window& replacement = f.holder.emplace();
replacementSub.emplace(&replacement.onFrameOwed, [&] { ++firedOnReplacement; });
});
f.Configure(400, 300, { XDG_TOPLEVEL_STATE_ACTIVATED });
Check(firedOnReplacement == 0 && f.owed == 0,
"a window destroyed from onToplevelState: onFrameOwed does not fire through the handler's pointer");
replacementSub.reset();
}
Check(std::ranges::adjacent_find(acknowledged, std::greater_equal{}) == acknowledged.end() && !acknowledged.empty(),
std::format("each of the {} acknowledgements names a later serial than the one before", acknowledged.size()));
const int error = wl_display_get_error(display);
Check(error == 0, "the display took every request");
if (error != 0) std::println(" the display failed: {}", std::strerror(error));
wl_registry_destroy(registry);
wl_display_disconnect(display);
close(fds[1]);
if (failures != 0) {
std::println("{} check(s) failed", failures);
return EXIT_FAILURE;
}
std::println("all checks passed");
return EXIT_SUCCESS;
}