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.89111e0left 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_, where61f2698compared 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:
parent
61f2698d13
commit
e1b36b9257
6 changed files with 211 additions and 53 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue