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
Labels
No labels
claude:blocked
claude:done
claude:failed
claude:in-progress
claude:ready
bug
duplicate
enhancement
help wanted
invalid
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
Catcrafts/Crafter.Graphics#204
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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
Resizetake 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, which89111e0ended 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 toa9ce1a0.Details
interfaces/Crafter.Graphics-Window.cppm:176-177and:253: both constructors andResizetakestd::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): setswidthandheight, clearslogicalConfigured, rebuilds.:843-851(MatchSizesToScale): for a window that is notlogicalConfigured,logicalWidth = SurfaceLength(width, scale), which isceil(pixels / scale)(:838-840). An application that wantsnsurface-local units passesceil(n * scale)pixels, the sumScaledPixelsdoes (:832-834), and its surface becomesceil(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 aResizeto 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,currentis false, sinceceil(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-851again, at the first swapchain made after the scale changed: an application-sized window keepswidth x heightand 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 newscaleitself and callResizeagain, 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 421x761underWAYLAND_DEBUG=1, 2026-11-21: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.
Resize, which stays in pixels. It makes the size givenlogicalWidth 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, asTakeDeferredSizedoes for a configure's size (:876-883). Of this call,Resizeand a configure the one asked for last is taken, as now.: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.MatchSizesToScalealready 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
deferredLogicalWidthanddeferredLogicalHeightand callingRebuildIfDeferred(), its stand-in for #166 (Canvas's constructor inimplementations/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 sentset_destination(501, 701)with a buffer of 927 x 1297, and KWin'sconfigure(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
onFrameOwedfrom a change of scale as it gets it from a configure, or nothing draws the frame at the new scale.