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

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