Wayland: every new size makes a new swapchain, 87 ms on the phone in which the wallpaper shows where the keyboard was: let an application keep a larger swapchain and crop it with the viewport #203

Open
opened 2026-11-20 06:59:14 +00:00 by jorijnvdgraaf · 0 comments

Summary

Every new size makes a new swapchain. On a Fairphone 6 that takes a median 87 ms when the on-screen keyboard goes and the window grows back, most of it the driver allocating the three new buffers. Since the fix for #199 a window keeps its old size on screen until the first frame at the new one, so for those 87 ms KWin, which removes the keyboard at once, shows its background where the keyboard was. Before that fix the last frame was shown stretched over the keyboard's place for the same time.

A window whose swapchain is as large as the larger of the two sizes needs no new buffers to go between them. The viewport can show the part of the buffer that is drawn (wp_viewport.set_source), and the answer to the configure is then the next frame. It costs memory for as long as the window is the smaller size, so it has to be the application's choice and off by default. Line numbers refer to 89111e0.

Details

Where the 87 ms go, by the medians of the trace below: about 2 ms until the library starts on the rebuild, about 24 ms in the driver's present-mode queries (#202 is about those), about 60 ms from the end of those to the last of the three new buffers, which by the order of the calls is vkCreateSwapchainKHR, and about 1 ms to draw and present the frame. The 60 ms are the driver's, and no caching in the library takes them away: only a resize that needs no new swapchain does.

  • implementations/Crafter.Graphics-Window.cpp:853-885 (TakeDeferredSize) and :887-891 (RebuildIfDeferred): a size that changes width or height makes the swapchain again.
  • :1773-1787 and :1808 (CreateSwapchain): the swapchain's extent is width x height. A Wayland surface has no extent of its own.
  • :2101-2118 (SendFrameSurfaceState): the one place a request goes to the window's wp_viewport, set_destination at :2108. The library never sends set_source (lib/viewporter.h:363), so a surface always shows its whole buffer.

What already goes by the window's size and not by the image's, and so would draw into the top-left width x height of a larger image as it is:

  • the UI renderer's dispatches and the header it pushes (implementations/Crafter.Graphics-UI.cpp:107-206, -UI-Shared.cpp:32-44), and Rect::FromWindow (interfaces/Crafter.Graphics-UI.cppm:153);
  • RTPass (interfaces/Crafter.Graphics-RTPass.cppm:75, :128-129);
  • the frame capture, which copies width x height (-Window.cpp:1568);
  • the library's shaders: none of them asks the image for its size.

Not looked at: the examples' and the consumers' own passes and shaders. One that takes the image's size for the window's (imageSize() in a shader, a blit of the whole image) would draw outside the part that is shown.

Concrete case

A Fairphone 6 (KWin 6.8.2, Mesa 26.2.6 Turnip, scale 2.85) running a scratch program with one text field against the library at 89111e0, drawing on demand and asking for a frame from onFrameOwed, under WAYLAND_DEBUG=1. The on-screen keyboard was brought up and dismissed by hand 22 times. All 44 resizes were answered as #199 asks: every acknowledgement was committed with a buffer of the configured size and that size as the destination. What the owner saw: "its smoother but there is still the issue with dismissing the keyboard: the keyboard dissapears instantly, showing the phone background, and there is a delay until the app fills the void again".

In ms from the configure, medians (least, most):

the keyboard goes, to 392 x 796 (23) the keyboard comes, to 392 x 534 (22)
the first buffer of the new swapchain created 55.7 (40.8, 102.5) 47.9 (31.7, 60.5)
the last of its three buffers created, after the first 30.7 (7.2, 35.0) 22.0 (4.3, 31.3)
the frame's commit: buffer, destination, acknowledgement 86.7 (50.2, 135.3) 70.2 (36.6, 90.3)

One keyboard hide: the present-mode queries run from 1.5 to 24.5 ms, the three 1118 x 2269 buffers are created at 53.7, 68.7 and 79.1 ms, and the frame's destination, acknowledgement, attach and commit follow at 79.6 to 79.8 ms. When the keyboard comes the same wait is there and does not show, since the keyboard covers the part of the window that is to go.

The memory: the buffers are XBGR8888, so 10.1 MB each at 1118 x 2269 and 6.8 MB each at 1118 x 1522, three of them. Keeping the larger ones while the keyboard is up is 30 MB where the window now has 20.

The application decides

The owner, when this was filed: "we need to make sure that its controlled by the application, for this specific case applications can enable it, otherwise they use memory nothing".

  • Off by default. The swapchain is the window's size, as now, and nothing is kept.
  • An application turns it on for a window whose sizes it knows, as a phone application knows that its window goes between the screen with the keyboard and without. Saying which extent to keep serves that better than "the largest this window has had": a desktop window that was once maximized at 3840 x 2034 has three buffers of 31 MB, and would keep 94 MB for a window since restored to 400 x 300. And there has to be a way to give the memory back.
  • Wayland with wp_viewporter only. Without a viewport there is no crop, and the window stays at its exact size, as it stays at scale 1. By design rule 1 the members are the Wayland block's.

Suggested fix

  • The window's size (width, height: what the passes draw at) and the swapchain's extent become two things. CreateSwapchain makes the larger of the window's size and what the application asked to keep.
  • A new size within the swapchain's extent makes no swapchain: TakeDeferredSize and RebuildIfDeferred tell the two cases apart. onResize still has to fire, since what is sized after the window changes, though the images stay; its comment ("the old images have been destroyed", interfaces/Crafter.Graphics-Window.cppm:82-90) then holds for a rebuild only. To decide.
  • SendFrameSurfaceState sends wp_viewport.set_source(0, 0, width, height) beside the destination, with the first frame drawn at a size that is not the buffer's, and unsets it (all four -1) with the first frame at the buffer's own size. Like the destination it is state the next commit applies, so it goes out there and nowhere else (#199).
  • Tests. ConfigureResize reads the source off the wire as it reads the destination, and gets the cases: a size within the extent, one beyond it, the application turning the reserve on and off. ResizeViewport's replay learns the source rectangle, so that "the destination in effect is the size of the buffer in effect" becomes "of the part of the buffer shown". And a check on pixels that a frame drawn at the smaller size into the larger image comes out right, CaptureNextFrame included.
  • Then the measurement again on the phone: the wait should be one frame.

Found by the owner trying the on-screen keyboard by hand on the phone at 89111e0, 2026-11-20.

## Summary Every new size makes a new swapchain. On a Fairphone 6 that takes a median 87 ms when the on-screen keyboard goes and the window grows back, most of it the driver allocating the three new buffers. Since the fix for #199 a window keeps its old size on screen until the first frame at the new one, so for those 87 ms KWin, which removes the keyboard at once, shows its background where the keyboard was. Before that fix the last frame was shown stretched over the keyboard's place for the same time. A window whose swapchain is as large as the larger of the two sizes needs no new buffers to go between them. The viewport can show the part of the buffer that is drawn (`wp_viewport.set_source`), and the answer to the configure is then the next frame. It costs memory for as long as the window is the smaller size, so it has to be the application's choice and off by default. Line numbers refer to `89111e0`. ## Details Where the 87 ms go, by the medians of the trace below: about 2 ms until the library starts on the rebuild, about 24 ms in the driver's present-mode queries (#202 is about those), about 60 ms from the end of those to the last of the three new buffers, which by the order of the calls is `vkCreateSwapchainKHR`, and about 1 ms to draw and present the frame. The 60 ms are the driver's, and no caching in the library takes them away: only a resize that needs no new swapchain does. - `implementations/Crafter.Graphics-Window.cpp:853-885` (`TakeDeferredSize`) and `:887-891` (`RebuildIfDeferred`): a size that changes `width` or `height` makes the swapchain again. - `:1773-1787` and `:1808` (`CreateSwapchain`): the swapchain's extent is `width` x `height`. A Wayland surface has no extent of its own. - `:2101-2118` (`SendFrameSurfaceState`): the one place a request goes to the window's `wp_viewport`, `set_destination` at `:2108`. The library never sends `set_source` (`lib/viewporter.h:363`), so a surface always shows its whole buffer. What already goes by the window's size and not by the image's, and so would draw into the top-left `width` x `height` of a larger image as it is: - the UI renderer's dispatches and the header it pushes (`implementations/Crafter.Graphics-UI.cpp:107-206`, `-UI-Shared.cpp:32-44`), and `Rect::FromWindow` (`interfaces/Crafter.Graphics-UI.cppm:153`); - `RTPass` (`interfaces/Crafter.Graphics-RTPass.cppm:75`, `:128-129`); - the frame capture, which copies `width` x `height` (`-Window.cpp:1568`); - the library's shaders: none of them asks the image for its size. Not looked at: the examples' and the consumers' own passes and shaders. One that takes the image's size for the window's (`imageSize()` in a shader, a blit of the whole image) would draw outside the part that is shown. ## Concrete case A Fairphone 6 (KWin 6.8.2, Mesa 26.2.6 Turnip, scale 2.85) running a scratch program with one text field against the library at `89111e0`, drawing on demand and asking for a frame from `onFrameOwed`, under `WAYLAND_DEBUG=1`. The on-screen keyboard was brought up and dismissed by hand 22 times. All 44 resizes were answered as #199 asks: every acknowledgement was committed with a buffer of the configured size and that size as the destination. What the owner saw: "its smoother but there is still the issue with dismissing the keyboard: the keyboard dissapears instantly, showing the phone background, and there is a delay until the app fills the void again". In ms from the configure, medians (least, most): | | the keyboard goes, to 392 x 796 (23) | the keyboard comes, to 392 x 534 (22) | |---|---|---| | the first buffer of the new swapchain created | 55.7 (40.8, 102.5) | 47.9 (31.7, 60.5) | | the last of its three buffers created, after the first | 30.7 (7.2, 35.0) | 22.0 (4.3, 31.3) | | the frame's commit: buffer, destination, acknowledgement | 86.7 (50.2, 135.3) | 70.2 (36.6, 90.3) | One keyboard hide: the present-mode queries run from 1.5 to 24.5 ms, the three 1118 x 2269 buffers are created at 53.7, 68.7 and 79.1 ms, and the frame's destination, acknowledgement, attach and commit follow at 79.6 to 79.8 ms. When the keyboard comes the same wait is there and does not show, since the keyboard covers the part of the window that is to go. The memory: the buffers are XBGR8888, so 10.1 MB each at 1118 x 2269 and 6.8 MB each at 1118 x 1522, three of them. Keeping the larger ones while the keyboard is up is 30 MB where the window now has 20. ## The application decides The owner, when this was filed: "we need to make sure that its controlled by the application, for this specific case applications can enable it, otherwise they use memory nothing". - Off by default. The swapchain is the window's size, as now, and nothing is kept. - An application turns it on for a window whose sizes it knows, as a phone application knows that its window goes between the screen with the keyboard and without. Saying which extent to keep serves that better than "the largest this window has had": a desktop window that was once maximized at 3840 x 2034 has three buffers of 31 MB, and would keep 94 MB for a window since restored to 400 x 300. And there has to be a way to give the memory back. - Wayland with `wp_viewporter` only. Without a viewport there is no crop, and the window stays at its exact size, as it stays at scale 1. By design rule 1 the members are the Wayland block's. ## Suggested fix - The window's size (`width`, `height`: what the passes draw at) and the swapchain's extent become two things. `CreateSwapchain` makes the larger of the window's size and what the application asked to keep. - A new size within the swapchain's extent makes no swapchain: `TakeDeferredSize` and `RebuildIfDeferred` tell the two cases apart. `onResize` still has to fire, since what is sized after the window changes, though the images stay; its comment ("the old images have been destroyed", `interfaces/Crafter.Graphics-Window.cppm:82-90`) then holds for a rebuild only. To decide. - `SendFrameSurfaceState` sends `wp_viewport.set_source(0, 0, width, height)` beside the destination, with the first frame drawn at a size that is not the buffer's, and unsets it (all four -1) with the first frame at the buffer's own size. Like the destination it is state the next commit applies, so it goes out there and nowhere else (#199). - Tests. `ConfigureResize` reads the source off the wire as it reads the destination, and gets the cases: a size within the extent, one beyond it, the application turning the reserve on and off. `ResizeViewport`'s replay learns the source rectangle, so that "the destination in effect is the size of the buffer in effect" becomes "of the part of the buffer shown". And a check on pixels that a frame drawn at the smaller size into the larger image comes out right, `CaptureNextFrame` included. - Then the measurement again on the phone: the wait should be one frame. Found by the owner trying the on-screen keyboard by hand on the phone at `89111e0`, 2026-11-20.
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#203
No description provided.