fix(wayland): round a configured size's pixels half away from zero, and work both sizes out exactly from the scale's 120ths

A size the compositor configured became the buffers' size as
ceil(size * scale), in float. The protocol, as bundled in
lib/fractional-scale-v1.h, has the buffer's size as the surface's
times the scale, and says that "For toplevel surfaces, the size is
rounded halfway away from zero": wherever the product's fraction is
below a half, rounding up gave a pixel more than that. 89111e0 left
this as the rest of #201, and the comment on #165 asks for it. And the
sum was done in float, with the scale as a float, which a scale that
is no whole number of eighths is not exactly: a product that is a
whole number of pixels could come out just above it, and rounding up
then went a pixel past it, as 50 at 1.2 came to 61. The other way, the
surface's size for a window the application sized,
ceil(pixels / scale), had the same trouble: 171 pixels at 2.85 came to
61, where they are 60.

Window: ScaledPixels rounds half away from zero, and both it and
SurfaceLength work in 64-bit integers from the 120ths the compositor
sends: MatchSizesToScale from those of the scale the window draws at,
which a new member, drawnScale_, keeps beside scale, as they are not
preferredScale while a new scale waits, and the tests of whether a
scale gives the window new pixels from preferredScale. SurfaceLength
still rounds up, so that the surface of a window the application sized
is no smaller than its pixels at the scale. ScaledPixels never gives
less than one pixel: below a scale of a half a length of one rounds to
none, where rounding up gave one, and a swapchain cannot be made 0
wide or high. Nor more than the largest 32-bit value, which a
configured length can pass at a scale above 2 (2^30 at 4 comes to
2^32), where the cast to 32 bits would wrap it, to none in that case;
and SurfaceLength stops at the largest int32, which a length worked
out from 32-bit pixels can pass. The configure listener's test for a
configure at the size the window has uses the same sum, so a
configured size the window took is found to be the one it has, and so
do the tests, in TakeDeferredSize after a present and in
MatchSizesToScale, of whether a size or a scale comes to new pixels;
and the listener tells that no new scale waits from the 120ths,
preferredScale against drawnScale_, where 61f2698 compared the two
scales as floats.

So 392x796 at 2.85, the size in #201's phone trace, takes 1117x2269
pixels where it took 1118x2269. A window of 320x240 pixels at 1.75,
shown at 183x138, takes 320x242 for a configure of that size, where it
took 321x242.

Docs: the consumer notes say how a configured size's pixels are
rounded, and that the surface of a window the application sized is
rounded up; the guide's gotchas, that both are worked out in integers
from the 120ths; both, that the pixels are never fewer than one nor
more than the largest 32-bit value, and that the surface's length
never passes the largest int32. The gotcha and the interface's comment
on logicalWidth give, as a reason not to work the surface's size back
from the pixels, that a configured size worked back from them lands
above itself wherever its product with the scale is not whole. That
held while both roundings went up; now it lands above where that
product has a fraction of a half or more, short of the 32-bit limits
the two sums stop at, and both say so.

Tests: ConfigureResize takes the new numbers in its cases of #201's
trace and of the window at 1.75, and adds the protocol's own example
(100x50 at 1.5 is 150x75), halves that go up (853x101 at 1.5 is
1280x152), fractions below a half that go down (101x853 at 1.25 is
126x1066, which rounding up made 127x1067), and whole numbers the sum
in float came out above (50x25 at 1.2 is 60x30, which came to 61x31),
each acknowledged in the listener when it is configured again; a
configure of 1x300 at 0.4, whose width rounds to no pixel, taken as
1x120; a configure of 2^30x300 at 4, whose width is the largest 32-bit
value, and pixels of that value at a half, whose surface is the
largest int32 wide; and a window of 342x171 pixels at 2.85, whose
surface is 120x60, which came to 121x61. And it works configured sizes
into pixels and back with a window's own MatchSizesToScale: 853x101 at
1.5 comes back as 854x102, and 392x796 at 2.85 as 392x797.
ResizeViewport works the pixels and the surface's size out as the
window now does, and its comments say which way each is rounded.
This commit is contained in:
Jorijn van der Graaf 2026-11-27 10:07:37 +01:00
commit e1b36b9257
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
6 changed files with 211 additions and 53 deletions

File diff suppressed because one or more lines are too long

View file

@ -146,7 +146,7 @@ These were decided deliberately. Do not undo them without the maintainer's say-s
- Buffer slot indices: `AllocateBufferSlots` returns an index relative to the heap's buffer region, while GLSL `descriptor_heap` SSBO arrays index in buffer-descriptor units from heap byte 0. `BufferSlot` converts to `bufferStartElement + raw`; raw code (the RT examples) passes `heap.bufferStartElement` as a `uint16_t` spec constant, which needs `shaderInt16`. In descriptor-set mode `bufferStartElement` is 0. Image and sampler slots are used as they are. A wrong index reads zeros, which looks like "every item field is zero".
- `UIRenderer::RegisterBuffer` refuses a buffer created without `VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT`. Freed slots are handed out again at once, so a slot may only be freed when no frame in flight reads it (after `vkQueueWaitIdle`, or at a scene switch). In descriptor-set mode a slot write destroys the previous view immediately.
- The swapchain image stays in `VK_IMAGE_LAYOUT_GENERAL` across the pass list and is never cleared; `Window::clearColor` is unused. `swapchainUsage` says whether `TRANSFER_DST` was available. `onResize` fires after a swapchain rebuild; slots listed with `AddSwapchainSlot` follow by themselves, and anything else holding the old images is made again there. A rebuild asked for during a frame (`Resize` or `SetPresentMode` from a listener) waits for that frame's present, since the rest of the frame indexes the swapchain it acquired from. `RenderPass::Record`'s `frameIdx` is the frame slot, not the image.
- On Wayland the configure listener only keeps a size (`deferredLogicalWidth`) and a serial (`unackedConfigure`): `Render()` rebuilds at its boundaries (`RebuildIfDeferred`), so a compositor's resize, where it changes the pixels, fires `onResize` from inside it, and a rebuild that throws stays pending for the next one. The window has two sizes, `width` x `height` (the buffers' pixels) and `logicalWidth` x `logicalHeight` (the surface's size: a configured size unchanged, `logicalConfigured`, or what the application's pixels come to), and `scale`, the scale the window draws at, ties them only where one is worked out from the other: when a configured size or a new scale is taken at a frame boundary and before every swapchain (`MatchSizesToScale`, which is also where `preferredScale`, the compositor's last word, becomes `scale`). The pointer, the touches and the caret are converted with `scale`, so it changes there and nowhere else (#165): in the constructor and at the start of a frame, and elsewhere only where it gives the window new pixels (`MatchSizesToScale`'s `takeScale`, `TakeDeferredSize`'s `FrameBoundary`). Once the window has its first swapchain, a new scale that leaves the pixels as they are, alone, with a configured size or with a swapchain made for another reason, waits for the next frame's start, as the compositor maps the surface, and the input on it, by the destination it has until that frame's commit. Nothing works the surface's size back from `width / scale` at another moment: the compositor's scale changes in any dispatch, in the middle of a frame too (a listener that dispatches events), and for a configured size the two roundings land above it wherever the size times the scale is not a whole number (#201: 392x796 at 2.85 came back as 393x797). Of a `Resize` and a configured size the one asked for last waits, each clearing the other's. What the surface takes with a buffer goes out in `SendFrameSurfaceState`, right before the present: the viewport's destination when the surface's size is not the one last sent (`sentLogicalWidth`), which is sent nowhere else, and the acknowledgement of a configure that waits for a frame (one that asks for nothing new while nothing is owed is acknowledged in the listener). Both are state the next `wl_surface.commit` applies, and commits that are no frame's happen all the time: the listener's own, a consumer's request for a frame callback, and the second commit the NVIDIA driver makes in a FIFO present where the compositor has `wp_fifo_v1` (seen on `615.78.08` under KWin 6.8.0). So a destination sent at the rebuild is applied to the old buffer, which is then shown scaled (#199), and an acknowledgement sent in the listener for a new size names that frame as the answer. A change that touches the surface's size (the buffers' rounding of #201, say) keeps to that, as the scale of #165 does: its listener only keeps it, and the frame boundary that takes it settles the sizes and leaves the destination to the frame. The cue for a window that draws on demand is `onFrameOwed`.
- On Wayland the configure listener only keeps a size (`deferredLogicalWidth`) and a serial (`unackedConfigure`): `Render()` rebuilds at its boundaries (`RebuildIfDeferred`), so a compositor's resize, where it changes the pixels, fires `onResize` from inside it, and a rebuild that throws stays pending for the next one. The window has two sizes, `width` x `height` (the buffers' pixels) and `logicalWidth` x `logicalHeight` (the surface's size: a configured size unchanged, `logicalConfigured`, or what the application's pixels come to), and `scale`, the scale the window draws at, ties them only where one is worked out from the other: when a configured size or a new scale is taken at a frame boundary and before every swapchain (`MatchSizesToScale`, which is also where `preferredScale`, the compositor's last word, becomes `scale`). The pointer, the touches and the caret are converted with `scale`, so it changes there and nowhere else (#165): in the constructor and at the start of a frame, and elsewhere only where it gives the window new pixels (`MatchSizesToScale`'s `takeScale`, `TakeDeferredSize`'s `FrameBoundary`). Once the window has its first swapchain, a new scale that leaves the pixels as they are, alone, with a configured size or with a swapchain made for another reason, waits for the next frame's start, as the compositor maps the surface, and the input on it, by the destination it has until that frame's commit. Nothing works the surface's size back from `width / scale` at another moment: the compositor's scale changes in any dispatch, in the middle of a frame too (a listener that dispatches events), and for a configured size the two roundings land above it wherever the size times the scale has a fraction of a half or more, short of the 32-bit limits the two sums stop at (853 at 1.5 comes back as 854, and #201's 392x796 at 2.85, which came back as 393x797 while both roundings went up, as 392x797). Of a `Resize` and a configured size the one asked for last waits, each clearing the other's. What the surface takes with a buffer goes out in `SendFrameSurfaceState`, right before the present: the viewport's destination when the surface's size is not the one last sent (`sentLogicalWidth`), which is sent nowhere else, and the acknowledgement of a configure that waits for a frame (one that asks for nothing new while nothing is owed is acknowledged in the listener). Both are state the next `wl_surface.commit` applies, and commits that are no frame's happen all the time: the listener's own, a consumer's request for a frame callback, and the second commit the NVIDIA driver makes in a FIFO present where the compositor has `wp_fifo_v1` (seen on `615.78.08` under KWin 6.8.0). So a destination sent at the rebuild is applied to the old buffer, which is then shown scaled (#199), and an acknowledgement sent in the listener for a new size names that frame as the answer. A change that touches the surface's size keeps to that, as the scale of #165 does: its listener only keeps it, and the frame boundary that takes it settles the sizes and leaves the destination to the frame. The sizes are worked out from the compositor's 120ths in integers (`ScaledPixels`, half away from zero as fractional-scale-v1 rounds a toplevel's buffer, never below one pixel and never past the largest 32-bit value; `SurfaceLength`, up, and never past the largest int32): in float a whole number of pixels can come out a little above itself and be rounded past (50 at 1.2 came to 61). The cue for a window that draws on demand is `onFrameOwed`.
- `Device::frameCounter` is one clock for every window, so the deletion queue and the staging rings assume one rendering window. The font atlas evicts on it too, on both backends: without a `Window` advancing it nothing is ever evicted, and a full atlas leaves new glyphs blank, so a headless test steps it itself (`FontAtlasEviction`).
- `ImageVulkan::Create`'s default `consumerStage` is the RT stage, corrected to compute on a device without ray tracing; the font atlas passes compute explicitly. `VulkanTransition`'s `GENERAL` mapping does the same.
- `UIRenderer::ShapeText` and `FontAtlas` key their caches on the `Font*`: call `UIRenderer::InvalidateFont`, which clears both, before destroying a `Font`, or a new one at the same address aliases stale runs and glyphs. The atlas also keeps the `Font*` of a glyph waiting for a cell until the next frame's first call, where it rasterises from it.

View file

@ -856,18 +856,34 @@ namespace {
return preferred / 120.0f;
}
// The pixels a surface-local length takes at `scale`, as a size the
// compositor configured becomes the swapchain's. Rounded up, in float,
// where fractional-scale-v1 rounds half away from zero: that much of
// #201 is as it was.
std::uint32_t ScaledPixels(std::int32_t logical, float scale) {
return static_cast<std::uint32_t>(std::ceil(logical * scale));
// The pixels a surface-local length takes at a scale of `preferred`
// 120ths, as a size the compositor configured becomes the swapchain's:
// rounded half away from zero, as fractional-scale-v1 says a toplevel's
// buffer is (#201). Worked out in integers from the 120ths the
// compositor sends: a scale that is no whole number of eighths, such as
// 1.2 or 2.85, is not exact in float, where a product that is a whole
// number of pixels can come out just above it and be rounded a pixel
// past it (50 at 1.2 came to 61). And never below one pixel: below a
// scale of a half a length of one rounds to none, where rounding up gave
// one, and a swapchain cannot be made 0 wide or high. Nor past the
// largest 32-bit value, which a configured length can pass at a scale
// above 2 (2^30 at 4 comes to 2^32): the cast would wrap it, to none in
// that case.
std::uint32_t ScaledPixels(std::int32_t logical, std::uint32_t preferred) {
const std::uint64_t pixels = (static_cast<std::uint64_t>(logical) * preferred + 60) / 120;
return static_cast<std::uint32_t>(std::clamp<std::uint64_t>(pixels, 1, std::numeric_limits<std::uint32_t>::max()));
}
// The other way: the surface-local length `pixels` come to at `scale`,
// for a window the application sized.
std::int32_t SurfaceLength(std::uint32_t pixels, float scale) {
return static_cast<std::int32_t>(std::ceil(pixels / scale));
// The other way: the surface-local length `pixels` come to at a scale
// of `preferred` 120ths, for a window the application sized. Rounded up,
// so that the surface is no smaller than its pixels at the scale, and in
// integers for the same reason (171 at 2.85 came to 61, not 60). Never
// past the largest int32, the surface's length being sent as one, which
// a length worked out from 32-bit pixels can pass: the cast would wrap
// it.
std::int32_t SurfaceLength(std::uint32_t pixels, std::uint32_t preferred) {
const std::uint64_t length = (static_cast<std::uint64_t>(pixels) * 120 + preferred - 1) / preferred;
return static_cast<std::int32_t>(std::min<std::uint64_t>(length, std::numeric_limits<std::int32_t>::max()));
}
}
@ -888,16 +904,18 @@ void Window::MatchSizesToScale(bool takeScale) {
// the application sized, whose pixels are its own, leaves the scale for
// the start of that frame.
const float before = scale;
const float newest = ScaleOf(preferredScale);
const bool newPixels = logicalConfigured && deferredLogicalWidth == 0
&& (ScaledPixels(logicalWidth, newest) != width || ScaledPixels(logicalHeight, newest) != height);
if (takeScale || newPixels) scale = newest;
&& (ScaledPixels(logicalWidth, preferredScale) != width || ScaledPixels(logicalHeight, preferredScale) != height);
if (takeScale || newPixels) {
drawnScale_ = preferredScale;
scale = ScaleOf(drawnScale_);
}
if (logicalConfigured) {
width = ScaledPixels(logicalWidth, scale);
height = ScaledPixels(logicalHeight, scale);
width = ScaledPixels(logicalWidth, drawnScale_);
height = ScaledPixels(logicalHeight, drawnScale_);
} else {
logicalWidth = SurfaceLength(width, scale);
logicalHeight = SurfaceLength(height, scale);
logicalWidth = SurfaceLength(width, drawnScale_);
logicalHeight = SurfaceLength(height, drawnScale_);
}
// What the window holds in pixels of the scale before goes to the new
// one. The compositor gives the pointer and the fingers in surface-local
@ -972,7 +990,7 @@ bool Window::TakeDeferredSize(FrameBoundary boundary) {
const bool configured = sized || logicalConfigured;
const std::int32_t surfaceWidth = sized ? deferredLogicalWidth : logicalWidth;
const std::int32_t surfaceHeight = sized ? deferredLogicalHeight : logicalHeight;
const bool newPixels = configured && (ScaledPixels(surfaceWidth, newest) != width || ScaledPixels(surfaceHeight, newest) != height);
const bool newPixels = configured && (ScaledPixels(surfaceWidth, preferredScale) != width || ScaledPixels(surfaceHeight, preferredScale) != height);
if (boundary == FrameBoundary::Start || !rescaled || newPixels) {
if (sized) {
logicalWidth = deferredLogicalWidth;
@ -2176,12 +2194,11 @@ void Window::xdg_surface_handle_configure(void* data, xdg_surface* xdg_surface,
// and the size together, so that a window the compositor sized
// always has the pixels its surface's size comes to at the scale it
// draws at.
const float newest = ScaleOf(window->preferredScale);
const bool current = window->pendingLogicalWidth == window->logicalWidth
&& window->pendingLogicalHeight == window->logicalHeight
&& newest == window->scale
&& ScaledPixels(window->pendingLogicalWidth, newest) == window->width
&& ScaledPixels(window->pendingLogicalHeight, newest) == window->height;
&& window->preferredScale == window->drawnScale_
&& ScaledPixels(window->pendingLogicalWidth, window->preferredScale) == window->width
&& ScaledPixels(window->pendingLogicalHeight, window->preferredScale) == window->height;
window->deferredLogicalWidth = current ? 0 : window->pendingLogicalWidth;
window->deferredLogicalHeight = current ? 0 : window->pendingLogicalHeight;
// And the size is the compositor's from here, also where the

View file

@ -427,6 +427,10 @@ export namespace Crafter {
// frame's commit the compositor maps the surface, and the pointer
// and the touches on it, as before (MatchSizesToScale).
std::uint32_t preferredScale = 120;
// The scale the window draws at in 120ths, as `scale` holds it in
// float: what MatchSizesToScale works the two sizes out with,
// exactly, also while a new scale waits in preferredScale.
std::uint32_t drawnScale_ = 120;
bool configured = false;
// Pending size from the most recent xdg_toplevel.configure, in
// surface-local (logical DP) units, until the xdg_surface.configure
@ -448,14 +452,15 @@ export namespace Crafter {
// when it is sent: the scale may have changed since they were
// worked out, and for a size the compositor configured the two
// roundings, there and back, land above it wherever the size times
// the scale is not a whole number (#201). logicalConfigured says
// which of the two sizes the window was given. True: a configure
// gave this one, and it is that size unchanged, with width and
// height the pixels it came to when they were last worked out
// (MatchSizesToScale). False: the application gave width and height
// (the constructor's size, a Resize), and this is what they came
// to. Without a viewport the scale stays 1 and the two sizes are
// the same numbers.
// the scale has a fraction of a half or more, short of the 32-bit
// limits the two sums stop at (#201).
// logicalConfigured says which of the two sizes the window was
// given. True: a configure gave this one, and it is that size
// unchanged, with width and height the pixels it came to when they
// were last worked out (MatchSizesToScale). False: the application
// gave width and height (the constructor's size, a Resize), and
// this is what they came to. Without a viewport the scale stays 1
// and the two sizes are the same numbers.
std::int32_t logicalWidth = 0;
std::int32_t logicalHeight = 0;
bool logicalConfigured = false;

View file

@ -63,6 +63,21 @@
// - 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 configured size's pixels are the size times the scale rounded half
// away from zero, as fractional-scale-v1 has it for a toplevel, worked
// out exactly from the scale's 120ths: the protocol's own 100x50 at 1.5,
// halves that go up, less than halves that go down where rounding up
// gave a pixel more, and whole numbers the sum in float came out above.
// The pixels are never fewer than one, where the protocol's rounding
// gives none: a length of one below a scale of a half; nor more than
// the largest 32-bit value, which 2^30 at 4 comes past, where the cast
// would wrap them to none. A surface's length worked out from pixels
// stops at the largest int32 in the same way. A window the
// application sized has its surface's size rounded up from its pixels,
// exactly too. Worked back from its pixels, a configured size short
// of the 32-bit limits the two sums stop at lands above itself where
// its product with the scale has a fraction of a half or more, and,
// from a scale of a half up, on itself below that
// - 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 at the new scale, which it draws at from that swapchain on, the
@ -724,7 +739,8 @@ int main() {
// 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.
// until it was 400x800. The pixels are 392x796 at 2.85 rounded half away
// from zero, 1117.2 and 2268.6, where they were rounded up to 1118x2269.
{
Fixture f = fixture(342, 1140, 2280);
f.Frame();
@ -733,7 +749,7 @@ int main() {
const std::uint32_t serial = f.Configure(392, 796);
f.Sent();
Check(f.Begin() && window.width == 1118 && window.height == 2269,
Check(f.Begin() && window.width == 1117 && 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" }),
@ -741,7 +757,7 @@ int main() {
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");
Check(!f.Begin() && window.width == 1117 && window.height == 2269, "and the window does not grow");
f.Present();
Expect(f.Sent(), {}, "nor is the surface sent anything");
}
@ -750,7 +766,8 @@ int main() {
// 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.
// once, and is then left alone. 183x138 at 1.75 is 320.25x241.5, so
// the width stays and the height goes to 242.
{
Fixture f = fixture(210, 320, 240);
f.Frame();
@ -759,13 +776,129 @@ int main() {
const std::uint32_t serial = f.Configure(183, 138);
f.Sent();
Check(f.Begin() && window.width == 321 && window.height == 242,
Check(f.Begin() && window.width == 320 && 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");
Check(!f.Begin() && window.width == 320 && window.height == 242, "and changes nothing");
}
// The rounding of the pixels, as fractional-scale-v1 has it for a
// toplevel: a configured size times the scale, rounded half away from
// zero, worked out exactly. Its own example is 100x50 at 1.5, which is
// 150x75. A half goes up: 853x101 at 1.5 is 1279.5x151.5. Less than a
// half goes down, where rounding up gave a pixel more: 101x853 at 1.25
// is 126.25x1066.25. And a whole number stays one, where the sum in
// float came out above it and was rounded up past it: 50x25 at 1.2 is
// 60x30, and came to 61x31.
{
struct Case { std::uint32_t scale; std::int32_t width, height; std::uint32_t pixelsWidth, pixelsHeight; };
for (const Case& c : { Case{ 180, 100, 50, 150, 75 }, Case{ 180, 853, 101, 1280, 152 }, Case{ 150, 101, 853, 126, 1066 },
Case{ 144, 50, 25, 60, 30 } }) {
Fixture f = fixture(c.scale, 640, 480);
f.Frame();
f.Sent();
Window& window = f.window();
const std::uint32_t serial = f.Configure(c.width, c.height);
f.Sent();
Check(f.Begin() && window.width == c.pixelsWidth && window.height == c.pixelsHeight,
std::format("{}x{} at {} is taken as {}x{} pixels ({}x{})", c.width, c.height, c.scale / 120.0f, c.pixelsWidth, c.pixelsHeight,
window.width, window.height));
f.Present();
Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), std::format("destination {}x{}", c.width, c.height) }),
"and the frame brings the surface that size");
const std::uint32_t again = f.Configure(c.width, c.height);
Expect(f.Sent(), { std::format("ack {}", again), "commit" }, "the same configure again is acknowledged at once, as the pixels are those");
}
}
// The other way, for a window the application sized: the surface is its
// pixels over the scale, rounded up, and worked out exactly too. 342x171
// at 2.85 is 120x60, which the sum in float came out above and was
// rounded up past to 121x61.
{
Fixture f = fixture(342, 342, 171);
f.Frame();
Expect(f.Sent(), { "destination 120x60" }, "a window of 342x171 pixels at scale 2.85 is 120x60");
}
// Never below one pixel: at 0.4, 48 120ths, a length of one is 0.4 of a
// pixel, which rounds to none, and a swapchain cannot be 0 wide. 1x300
// at 0.4 is taken as 1x120.
{
Fixture f = fixture(48, 640, 480);
f.Frame();
f.Sent();
Window& window = f.window();
const std::uint32_t serial = f.Configure(1, 300);
f.Sent();
Check(f.Begin() && window.width == 1 && window.height == 120,
std::format("1x300 at 0.4 is taken as 1x120 pixels, one pixel and not none for the width ({}x{})", window.width, window.height));
f.Present();
Expect(Sorted(f.Sent()), Sorted({ std::format("ack {}", serial), "destination 1x300" }), "and the frame brings the surface 1x300");
const std::uint32_t again = f.Configure(1, 300);
Expect(f.Sent(), { std::format("ack {}", again), "commit" }, "the same configure again is acknowledged at once, as the pixels are those");
}
// Nor past the largest 32-bit value: a configured length of 2^30 at a
// scale of 4 comes to 2^32 pixels, which the cast to 32 bits would wrap
// to none. And the surface's length worked out from pixels of the
// largest 32-bit value at a half stops at the largest int32, where the
// cast would wrap it.
{
Fixture f = fixture(480, 640, 480);
f.Frame();
f.Sent();
Window& window = f.window();
f.Configure(1073741824, 300);
Check(f.Begin() && window.width == std::numeric_limits<std::uint32_t>::max() && window.height == 1200,
std::format("a configure of 2^30x300 at 4 takes {}x{} pixels, the largest 32-bit value and not none", window.width, window.height));
f.Present();
f.Sent();
}
{
// Value-initialized, as below: the scale it takes converts the
// positions it holds.
Window window{};
window.preferredScale = 60;
window.width = std::numeric_limits<std::uint32_t>::max();
window.height = 100;
window.MatchSizesToScale(true);
Check(window.logicalWidth == std::numeric_limits<std::int32_t>::max() && window.logicalHeight == 200,
std::format("pixels of the largest 32-bit value at a half come to a surface {} wide, the largest int32", window.logicalWidth));
}
// A configured size worked back from its pixels, as nothing in the
// window does any more, the surface's size being state of its own:
// short of the 32-bit limits the two sums stop at, it lands above itself
// where its product with the scale has a fraction of a half or more,
// and, from a scale of a half up, on itself below that.
// 853x101 at 1.5 is 1279.5x151.5, so 1280x152 pixels, which come to
// 854x102; 392x796 at 2.85 is 1117.2x2268.6, so 1117x2269 pixels, which
// come to 392x797. The window's own sums: the pixels as for a configured
// size, then the surface's size as for a window the application sized at
// those pixels.
{
struct Case { std::uint32_t scale; std::int32_t width, height, backWidth, backHeight; };
for (const Case& c : { Case{ 180, 853, 101, 854, 102 }, Case{ 342, 392, 796, 392, 797 } }) {
// Value-initialized: the scale it takes converts the positions
// it holds, which a default-initialized window has none of.
Window window{};
window.preferredScale = c.scale;
window.logicalWidth = c.width;
window.logicalHeight = c.height;
window.logicalConfigured = true;
window.MatchSizesToScale(true);
const std::uint32_t pixelsWidth = window.width;
const std::uint32_t pixelsHeight = window.height;
window.logicalConfigured = false;
window.MatchSizesToScale(true);
Check(window.logicalWidth == c.backWidth && window.logicalHeight == c.backHeight,
std::format("{}x{} at {} is {}x{} pixels, which come to {}x{} ({}x{})", c.width, c.height, c.scale / 120.0f,
pixelsWidth, pixelsHeight, c.backWidth, c.backHeight, window.logicalWidth, window.logicalHeight));
}
}
// A swapchain made for another reason (a present mode, a swapchain out

View file

@ -324,23 +324,26 @@ Replay ReplayLog(const fs::path& log, const Objects& objects) {
}
// Whether `destination` is the size of `buffer` at `scale`, to within one
// surface-local pixel: the library rounds one up from the other, the
// destination from the pixels of a window the application sized, the pixels
// from the destination of one the compositor sized.
// surface-local pixel: the library rounds one from the other, the
// destination up from the pixels of a window the application sized, the
// pixels half away from zero from the destination of one the compositor
// sized.
bool Belongs(Extent destination, Extent buffer, float scale) {
return std::abs(destination.width * scale - buffer.width) <= scale + 0.01f
&& std::abs(destination.height * scale - buffer.height) <= scale + 0.01f;
}
// As the configure listener turns a surface-local length into pixels.
std::uint32_t Pixels(std::int32_t logical, float scale) {
return static_cast<std::uint32_t>(std::ceil(logical * scale));
// As the window turns a configured surface-local length into pixels at a
// scale of `scale` 120ths: rounded half away from zero, in integers, and
// never below one.
std::uint32_t Pixels(std::int32_t logical, std::uint32_t scale) {
return static_cast<std::uint32_t>(std::max<std::uint64_t>(1, (static_cast<std::uint64_t>(logical) * scale + 60) / 120));
}
// As the window works out the surface-local length its pixels come to,
// for a window the application sized.
std::int32_t SurfaceLength(std::uint32_t pixels, float scale) {
return static_cast<std::int32_t>(std::ceil(pixels / scale));
// for a window the application sized: rounded up, in integers.
std::int32_t SurfaceLength(std::uint32_t pixels, std::uint32_t scale) {
return static_cast<std::int32_t>((static_cast<std::uint64_t>(pixels) * 120 + scale - 1) / scale);
}
// Whether a pointer motion to the middle of the window's surface, as the
@ -467,7 +470,7 @@ Scenario Drive(bool keyboard) {
window.width, window.height, window.currentMousePos.x, window.currentMousePos.y));
if (window.logicalConfigured) {
Check(window.logicalWidth == logicalWidthBefore && window.logicalHeight == logicalHeightBefore
&& window.width == Pixels(logicalWidthBefore, scale) && window.height == Pixels(logicalHeightBefore, scale),
&& window.width == Pixels(logicalWidthBefore, to) && window.height == Pixels(logicalHeightBefore, to),
std::format("a window the compositor sized keeps its surface's {}x{} and takes the pixels for it ({}x{})",
window.logicalWidth, window.logicalHeight, window.width, window.height));
Check(window.swapchainGeneration != generation && resizesInRender == inRenderBefore + 1
@ -477,7 +480,7 @@ Scenario Drive(bool keyboard) {
Check(window.width == widthBefore && window.height == heightBefore && window.swapchainGeneration == generation
&& resizesInRender + resizesElsewhere == resizesBefore,
std::format("a window the application sized keeps its {}x{} pixels: no swapchain is made, and onResize does not fire", window.width, window.height));
Check(window.logicalWidth == SurfaceLength(widthBefore, scale) && window.logicalHeight == SurfaceLength(heightBefore, scale)
Check(window.logicalWidth == SurfaceLength(widthBefore, to) && window.logicalHeight == SurfaceLength(heightBefore, to)
&& window.sentLogicalWidth == window.logicalWidth && window.sentLogicalHeight == window.logicalHeight,
std::format("and its surface takes the {}x{} they come to, sent with that frame", window.logicalWidth, window.logicalHeight));
}
@ -577,7 +580,7 @@ Scenario Drive(bool keyboard) {
const int inRenderBefore = resizesInRender;
render(1);
Check(window.width == Pixels(logicalWidth, window.scale) && window.height == Pixels(logicalHeight, window.scale)
Check(window.width == Pixels(logicalWidth, window.preferredScale) && window.height == Pixels(logicalHeight, window.preferredScale)
&& window.swapchainGeneration != generation,
std::format("the next Render() makes the swapchain at the configured size ({}x{})", window.width, window.height));
Check(window.logicalWidth == logicalWidth && window.logicalHeight == logicalHeight
@ -755,9 +758,9 @@ int main(int argc, char** argv) {
// Every acknowledgement: the commit that carries it says "this answers
// the configure", so the buffer in effect with it has to be of the size
// that configure asked for, to within the pixel the rounding of the
// scale may add, and the destination in effect has to be that size
// itself: the surface's size is the compositor's to give, and the
// destination used to be worked back from the buffer's pixels, to
// scale may add or take away, and the destination in effect has to be
// that size itself: the surface's size is the compositor's to give, and
// the destination used to be worked back from the buffer's pixels, to
// more than was configured where the scale does not divide them
// (#201). For a new size that is the frame's commit; where
// the surface has the size already it is the listener's own. A