Wayland: a window cannot be sized in surface-local units: the constructor and Resize take pixels, so on a fractional scale the surface comes out one larger than meant, and every start makes two swapchains #204

Open
opened 2026-11-21 05:13:36 +00:00 by jorijnvdgraaf · 0 comments

Summary

An application that lays its window out in surface-local units has no way to say "this window is 421 x 761". The constructor and Resize take the buffers' pixels, and on Wayland the surface's size is then worked back from them. So the application multiplies by the scale itself, rounds up, and gets a surface one unit larger than it asked for wherever the product is not a whole number: the ceil of a ceil of #201, which 89111e0 ended for a size the compositor configures and which is still there for a size the application gives. Two more things follow from the same gap: the pixels have to be chosen before the scale is known, which costs a second swapchain at every start, and when the scale changes the window keeps its pixels where it was meant to keep its size on screen. Line numbers refer to a9ce1a0.

Details

  • interfaces/Crafter.Graphics-Window.cppm:176-177 and :253: both constructors and Resize take std::uint32_t width, height, pixels. The surface's size is state of the window (logicalWidth, logicalHeight, :406-407), but only a configure can give it one: logicalConfigured (:408) is set by the configure's path alone.
  • implementations/Crafter.Graphics-Window.cpp:786-792 (Resize): sets width and height, clears logicalConfigured, rebuilds.
  • :843-851 (MatchSizesToScale): for a window that is not logicalConfigured, logicalWidth = SurfaceLength(width, scale), which is ceil(pixels / scale) (:838-840). An application that wants n surface-local units passes ceil(n * scale) pixels, the sum ScaledPixels does (:832-834), and its surface becomes ceil(ceil(n * scale) / scale).
  • :393, :413, :554 (the constructor): the preferred scale arrives while the constructor waits for the first configure (:393), after its arguments were given, so an application cannot know the scale before it has a window. It passes pixels worked out at a guess, the constructor settles the two sizes from them (:413) and makes the swapchain (:554), and the application's first act with its window is a Resize to the pixels it meant, which makes the swapchain a second time. #203 has what one costs on a phone.
  • :2054-2062 (xdg_surface_handle_configure): when the compositor later configures the window at the size its surface has, current is false, since ceil(logical * scale) is not the pixels the application passed, and the frame that answers it makes the swapchain once more. 89111e0's message has this case (320 x 240 at 1.75 becoming 321 x 242).
  • :843-851 again, at the first swapchain made after the scale changed: an application-sized window keeps width x height and takes the surface size they come to at the new scale. For a size that was meant in pixels that is the right one of the two to keep. For one that was meant in surface-local units it is the wrong one: a window of 1280 x 720 pixels moved from an output at 1.75 to one at 1.0 goes from 732 x 412 on screen to 1280 x 720. The application has to notice the new scale itself and call Resize again, and #165's fix would not spare it that, since for this window the pixels are what the library keeps.

Concrete case

KWin 6.8.0 on the development desktop, at a scale of 1.85. nostro's app (Catcrafts/Nostro d35c486, the library at a9ce1a0) started with --size 421x761 under WAYLAND_DEBUG=1, 2026-11-21:

[05:11:59.176842] wp_fractional_scale_v1#17.preferred_scale(222)
[05:11:59.176856] xdg_toplevel#16.configure(0, 0, array[0])
[05:11:59.176876]  -> xdg_surface#15.ack_configure(7586)
[05:11:59.202895]  -> wp_viewport#21.set_destination(422, 762)
[05:11:59.202951]  -> wl_surface#3.attach(wl_buffer#48, 0, 0)

The app makes its window with 421 x 761, the scale not being known yet, and then calls Resize(779, 1408): 421 x 1.85 = 778.85 and 761 x 1.85 = 1407.85, rounded up. 779 / 1.85 = 421.08 and 1408 / 1.85 = 761.08, so the surface is 422 x 762 where 421 x 761 was meant, and a buffer 779 pixels wide is laid over 780.7. The frame the app saved was 779 x 1408 pixels.

In the same run on 2026-11-20 (the trace was not kept) KWin configured the window at 422 x 762 a second after it appeared, and the frame that answered made a third swapchain, 781 x 1410. On 2026-11-21 that configure did not come within the three seconds the run lasted.

The two roundings done in 32-bit float, as the code does them, for the sizes 1 to 3000: at 1.5 every odd size comes out one larger, at 1.25 and 1.75 every size that is not a multiple of 4, at 1.85 all but the multiples of 20, at 2.85 all but 127. They are #201's numbers, the sum being the same.

Not tried: the change of scale, which is read from the code.

Suggested fix

Let the application give the surface's size, and treat that as a configured size is treated.

  • A resize in surface-local units beside Resize, which stays in pixels. It makes the size given logicalWidth x logicalHeight, unchanged, marks the window as one that was given its surface's size (logicalConfigured, which then says that of whoever gave it) and works the pixels out with the scale of that moment, as TakeDeferredSize does for a configure's size (:876-883). Of this call, Resize and a configure the one asked for last is taken, as now.
  • The same for the constructor: a form whose size is surface-local, applied after the wait at :393, when the scale the compositor has named so far is known, so that the first swapchain is made at the pixels meant. With #166 a size in the first configure takes its place.
  • Such a window keeps its surface's size when the scale changes by what MatchSizesToScale already does for a configured one, and is covered by #165's fix as a configured one is.

As far as a consumer can try it: nostro hands the window a surface-local size by setting deferredLogicalWidth and deferredLogicalHeight and calling RebuildIfDeferred(), its stand-in for #166 (Canvas's constructor in implementations/Nostro.Ui.Shell-Canvas.cpp). With 501 x 701 put there by hand on the same desktop (2026-11-20, the trace not kept) the first frame sent set_destination(501, 701) with a buffer of 927 x 1297, and KWin's configure(501, 701) a second later was acknowledged in the listener, with no new swapchain.

Related: #201 (the same two roundings, for a configured size; its rest, the rounding of the buffers, applies here unchanged), #165, #166, #203. For #165, from the same consumer: a window that draws on demand needs onFrameOwed from a change of scale as it gets it from a configure, or nothing draws the frame at the new scale.

## Summary An application that lays its window out in surface-local units has no way to say "this window is 421 x 761". The constructor and `Resize` take the buffers' pixels, and on Wayland the surface's size is then worked back from them. So the application multiplies by the scale itself, rounds up, and gets a surface one unit larger than it asked for wherever the product is not a whole number: the ceil of a ceil of #201, which 89111e0 ended for a size the compositor configures and which is still there for a size the application gives. Two more things follow from the same gap: the pixels have to be chosen before the scale is known, which costs a second swapchain at every start, and when the scale changes the window keeps its pixels where it was meant to keep its size on screen. Line numbers refer to `a9ce1a0`. ## Details - `interfaces/Crafter.Graphics-Window.cppm:176-177` and `:253`: both constructors and `Resize` take `std::uint32_t width, height`, pixels. The surface's size is state of the window (`logicalWidth`, `logicalHeight`, `:406-407`), but only a configure can give it one: `logicalConfigured` (`:408`) is set by the configure's path alone. - `implementations/Crafter.Graphics-Window.cpp:786-792` (`Resize`): sets `width` and `height`, clears `logicalConfigured`, rebuilds. - `:843-851` (`MatchSizesToScale`): for a window that is not `logicalConfigured`, `logicalWidth = SurfaceLength(width, scale)`, which is `ceil(pixels / scale)` (`:838-840`). An application that wants `n` surface-local units passes `ceil(n * scale)` pixels, the sum `ScaledPixels` does (`:832-834`), and its surface becomes `ceil(ceil(n * scale) / scale)`. - `:393`, `:413`, `:554` (the constructor): the preferred scale arrives while the constructor waits for the first configure (`:393`), after its arguments were given, so an application cannot know the scale before it has a window. It passes pixels worked out at a guess, the constructor settles the two sizes from them (`:413`) and makes the swapchain (`:554`), and the application's first act with its window is a `Resize` to the pixels it meant, which makes the swapchain a second time. #203 has what one costs on a phone. - `:2054-2062` (`xdg_surface_handle_configure`): when the compositor later configures the window at the size its surface has, `current` is false, since `ceil(logical * scale)` is not the pixels the application passed, and the frame that answers it makes the swapchain once more. 89111e0's message has this case (320 x 240 at 1.75 becoming 321 x 242). - `:843-851` again, at the first swapchain made after the scale changed: an application-sized window keeps `width x height` and takes the surface size they come to at the new scale. For a size that was meant in pixels that is the right one of the two to keep. For one that was meant in surface-local units it is the wrong one: a window of 1280 x 720 pixels moved from an output at 1.75 to one at 1.0 goes from 732 x 412 on screen to 1280 x 720. The application has to notice the new `scale` itself and call `Resize` again, and #165's fix would not spare it that, since for this window the pixels are what the library keeps. ## Concrete case KWin 6.8.0 on the development desktop, at a scale of 1.85. nostro's app (Catcrafts/Nostro d35c486, the library at a9ce1a0) started with `--size 421x761` under `WAYLAND_DEBUG=1`, 2026-11-21: ``` [05:11:59.176842] wp_fractional_scale_v1#17.preferred_scale(222) [05:11:59.176856] xdg_toplevel#16.configure(0, 0, array[0]) [05:11:59.176876] -> xdg_surface#15.ack_configure(7586) [05:11:59.202895] -> wp_viewport#21.set_destination(422, 762) [05:11:59.202951] -> wl_surface#3.attach(wl_buffer#48, 0, 0) ``` The app makes its window with 421 x 761, the scale not being known yet, and then calls `Resize(779, 1408)`: 421 x 1.85 = 778.85 and 761 x 1.85 = 1407.85, rounded up. 779 / 1.85 = 421.08 and 1408 / 1.85 = 761.08, so the surface is 422 x 762 where 421 x 761 was meant, and a buffer 779 pixels wide is laid over 780.7. The frame the app saved was 779 x 1408 pixels. In the same run on 2026-11-20 (the trace was not kept) KWin configured the window at 422 x 762 a second after it appeared, and the frame that answered made a third swapchain, 781 x 1410. On 2026-11-21 that configure did not come within the three seconds the run lasted. The two roundings done in 32-bit float, as the code does them, for the sizes 1 to 3000: at 1.5 every odd size comes out one larger, at 1.25 and 1.75 every size that is not a multiple of 4, at 1.85 all but the multiples of 20, at 2.85 all but 127. They are #201's numbers, the sum being the same. Not tried: the change of scale, which is read from the code. ## Suggested fix Let the application give the surface's size, and treat that as a configured size is treated. - A resize in surface-local units beside `Resize`, which stays in pixels. It makes the size given `logicalWidth x logicalHeight`, unchanged, marks the window as one that was given its surface's size (`logicalConfigured`, which then says that of whoever gave it) and works the pixels out with the scale of that moment, as `TakeDeferredSize` does for a configure's size (`:876-883`). Of this call, `Resize` and a configure the one asked for last is taken, as now. - The same for the constructor: a form whose size is surface-local, applied after the wait at `:393`, when the scale the compositor has named so far is known, so that the first swapchain is made at the pixels meant. With #166 a size in the first configure takes its place. - Such a window keeps its surface's size when the scale changes by what `MatchSizesToScale` already does for a configured one, and is covered by #165's fix as a configured one is. As far as a consumer can try it: nostro hands the window a surface-local size by setting `deferredLogicalWidth` and `deferredLogicalHeight` and calling `RebuildIfDeferred()`, its stand-in for #166 (`Canvas`'s constructor in `implementations/Nostro.Ui.Shell-Canvas.cpp`). With 501 x 701 put there by hand on the same desktop (2026-11-20, the trace not kept) the first frame sent `set_destination(501, 701)` with a buffer of 927 x 1297, and KWin's `configure(501, 701)` a second later was acknowledged in the listener, with no new swapchain. Related: #201 (the same two roundings, for a configured size; its rest, the rounding of the buffers, applies here unchanged), #165, #166, #203. For #165, from the same consumer: a window that draws on demand needs `onFrameOwed` from a change of scale as it gets it from a configure, or nothing draws the frame at the new scale.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Catcrafts/Crafter.Graphics#204
No description provided.