Wayland: a resize sends the viewport's destination before a buffer of the new size exists, so the old frame is shown scaled to the new size until the next one #199

Closed
opened 2026-11-18 05:44:43 +00:00 by jorijnvdgraaf · 0 comments

Summary

At a resize RebuildSwapchain sends wp_viewport.set_destination for the new size from inside the configure listener, and the first buffer of that size reaches the compositor only with the consumer's next frame. The destination is double-buffered surface state: the next wl_surface.commit applies it, and that is the listener's own commit or a consumer's request for a frame callback, made while the surface still holds the old buffer. From that commit until the next frame's, the old picture is shown stretched or squashed to the new size. Line numbers refer to c006519.

Details

  • implementations/Crafter.Graphics-Window.cpp:784-788: RebuildSwapchain calls wp_viewport_set_destination with the new size, before RecreateSwapchainAndImages.
  • :792: onResize fires at the end of RebuildSwapchain. A consumer that draws on demand asks for a frame there (wl_surface.frame, wl_surface.commit), and that is the first commit after the new destination.
  • :1929: xdg_surface_handle_configure commits the surface itself once Resize has returned.
  • The buffer of the new size is attached by the present of the consumer's next Window::Update.

viewporter says of the destination: "it causes the surface size to become dst_width, dst_height. The source (rectangle) is scaled to exactly this size."

Concrete case

A Fairphone 6 with Plasma Mobile 6.8.2 (KWin 6.8.2, plasma-keyboard 6.8.2), scale 2.85, running imsd-messages 0.0.0's tree (ca5cd72, the library at c006519) under WAYLAND_DEBUG=1. The on-screen keyboard resizes the maximized window between 392 x 796 and 392 x 534 each time it comes or goes. One of those resizes, with the time since the configure (the lines between are left out):

   0.0 ms  xdg_toplevel#16.configure(392, 534, array[8])
   1.5 ms   -> wp_viewport#21.set_destination(393, 535)
   4.3 ms  wl_registry#42.global(...)                         the driver's WSI lists the globals
  25.1 ms  wl_registry#41.global(...)                         and again
  82.1 ms   -> wl_surface#3.frame(new id wl_callback#44)      the app's onResize listener asks for a frame
  82.1 ms   -> wl_surface#3.commit()                          the old buffer, 796 high, now at 535
  82.2 ms   -> wl_surface#3.commit()                          the configure listener's own
 114.2 ms   -> wl_surface#3.attach(wl_buffer#33, 0, 0)        the first buffer of the new size
 114.3 ms   -> wl_surface#3.commit()

Over the 22 keyboard resizes of that trace, 11 each way, in ms from the configure:

least median most
set_destination 0.8 1.8 79.8
the first commit after it, the old buffer still attached 59.6 85.2 154.9
the first buffer of the new size 88.1 111.9 190.5

Between the last two the old picture is at the new size: 15 to 36 ms, median 27 ms. That is by the trace and the protocol's rule. The screen was not filmed.

The table also shows what the rebuild costs. The first commit is the app's, made from its onResize listener, so RebuildSwapchain has taken a median 85 ms by then, and for that time the consumer's loop is inside the listener and handles nothing else. The second of the two listings of the globals comes a median 25 ms in. How the rest divides between vkCreateSwapchainKHR, the old swapchain's destruction and the onResize listeners that run before the app's was not measured.

Suggested fix

  • Send the destination with the frame it belongs to: set it just before the present of the first frame drawn into the new swapchain, so that the commit which attaches the new buffer applies both. Until then every commit shows the old buffer at its own size. If the configure's ack moves there too, the configure is answered by the frame that honours it.
  • Rebuild at the start of the next Render and not in the listener, as #200 asks for a lost device. The loop is then not held in the dispatch, and configures that arrive before a frame cost one rebuild between them.

Found tracing the on-screen keyboard's coming and going in imsd-messages on the phone, 2026-11-18. The app's side is Catcrafts/imsd-messages#1. #201 is about the destination's value in the same lines.

## Summary At a resize `RebuildSwapchain` sends `wp_viewport.set_destination` for the new size from inside the configure listener, and the first buffer of that size reaches the compositor only with the consumer's next frame. The destination is double-buffered surface state: the next `wl_surface.commit` applies it, and that is the listener's own commit or a consumer's request for a frame callback, made while the surface still holds the old buffer. From that commit until the next frame's, the old picture is shown stretched or squashed to the new size. Line numbers refer to `c006519`. ## Details - `implementations/Crafter.Graphics-Window.cpp:784-788`: `RebuildSwapchain` calls `wp_viewport_set_destination` with the new size, before `RecreateSwapchainAndImages`. - `:792`: `onResize` fires at the end of `RebuildSwapchain`. A consumer that draws on demand asks for a frame there (`wl_surface.frame`, `wl_surface.commit`), and that is the first commit after the new destination. - `:1929`: `xdg_surface_handle_configure` commits the surface itself once `Resize` has returned. - The buffer of the new size is attached by the present of the consumer's next `Window::Update`. viewporter says of the destination: "it causes the surface size to become dst_width, dst_height. The source (rectangle) is scaled to exactly this size." ## Concrete case A Fairphone 6 with Plasma Mobile 6.8.2 (KWin 6.8.2, plasma-keyboard 6.8.2), scale 2.85, running imsd-messages 0.0.0's tree (ca5cd72, the library at c006519) under `WAYLAND_DEBUG=1`. The on-screen keyboard resizes the maximized window between 392 x 796 and 392 x 534 each time it comes or goes. One of those resizes, with the time since the configure (the lines between are left out): ``` 0.0 ms xdg_toplevel#16.configure(392, 534, array[8]) 1.5 ms -> wp_viewport#21.set_destination(393, 535) 4.3 ms wl_registry#42.global(...) the driver's WSI lists the globals 25.1 ms wl_registry#41.global(...) and again 82.1 ms -> wl_surface#3.frame(new id wl_callback#44) the app's onResize listener asks for a frame 82.1 ms -> wl_surface#3.commit() the old buffer, 796 high, now at 535 82.2 ms -> wl_surface#3.commit() the configure listener's own 114.2 ms -> wl_surface#3.attach(wl_buffer#33, 0, 0) the first buffer of the new size 114.3 ms -> wl_surface#3.commit() ``` Over the 22 keyboard resizes of that trace, 11 each way, in ms from the configure: | | least | median | most | |---|---|---|---| | `set_destination` | 0.8 | 1.8 | 79.8 | | the first commit after it, the old buffer still attached | 59.6 | 85.2 | 154.9 | | the first buffer of the new size | 88.1 | 111.9 | 190.5 | Between the last two the old picture is at the new size: 15 to 36 ms, median 27 ms. That is by the trace and the protocol's rule. The screen was not filmed. The table also shows what the rebuild costs. The first commit is the app's, made from its `onResize` listener, so `RebuildSwapchain` has taken a median 85 ms by then, and for that time the consumer's loop is inside the listener and handles nothing else. The second of the two listings of the globals comes a median 25 ms in. How the rest divides between `vkCreateSwapchainKHR`, the old swapchain's destruction and the `onResize` listeners that run before the app's was not measured. ## Suggested fix - Send the destination with the frame it belongs to: set it just before the present of the first frame drawn into the new swapchain, so that the commit which attaches the new buffer applies both. Until then every commit shows the old buffer at its own size. If the configure's ack moves there too, the configure is answered by the frame that honours it. - Rebuild at the start of the next `Render` and not in the listener, as #200 asks for a lost device. The loop is then not held in the dispatch, and configures that arrive before a frame cost one rebuild between them. Found tracing the on-screen keyboard's coming and going in imsd-messages on the phone, 2026-11-18. The app's side is Catcrafts/imsd-messages#1. #201 is about the destination's value in the same lines.
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#199
No description provided.