From 514eeebd3c406b9b86657ee46f015e9af6d329d3 Mon Sep 17 00:00:00 2001 From: Anatolii Smolianinov Date: Thu, 30 Oct 2025 11:13:32 +0100 Subject: [PATCH] backend/wayland: Fix HiDPI support for nested compositors When running a wlroots-based compositor nested under another Wayland compositor with HiDPI scaling, the nested window appears blurry and incorrectly sized. This is because the Wayland backend does not: 1. Detect and track the parent compositor's output scale 2. Set wl_surface_set_buffer_scale() to match the parent's scale 3. Multiply output dimensions by the scale factor when handling configure events This patch fixes all three issues by: - Adding wlr_wl_parent_output structure to track parent outputs - Listening to wl_output.scale events from the parent compositor - Setting buffer_scale on all backend surfaces (output and layers) - Scaling output dimensions to account for buffer scale - Setting wlr_output.scale so nested clients render at correct DPI Implements: https://gitlab.freedesktop.org/wlroots/wlroots/-/issues/854 Closes: https://gitlab.freedesktop.org/wlroots/wlroots/-/merge_requests/5186 Co-Authored-By: Hugo Osvaldo Barrera --- backend/wayland/backend.c | 114 ++++++++++++++++++++++++++++++++++++ backend/wayland/output.c | 41 ++++++++++--- backend/wayland/pointer.c | 7 ++- backend/wayland/seat.c | 5 +- backend/wayland/tablet_v2.c | 5 +- include/backend/wayland.h | 14 +++++ 6 files changed, 171 insertions(+), 15 deletions(-) diff --git a/backend/wayland/backend.c b/backend/wayland/backend.c index 0d294b543..d320bfd66 100644 --- a/backend/wayland/backend.c +++ b/backend/wayland/backend.c @@ -13,6 +13,7 @@ #include #include +#include #include #include "backend/wayland.h" @@ -87,6 +88,73 @@ static int dispatch_events(int fd, uint32_t mask, void *data) { return count; } +static void output_handle_geometry(void *data, struct wl_output *wl_output, + int32_t x, int32_t y, int32_t phys_width, int32_t phys_height, + int32_t subpixel, const char *make, const char *model, + int32_t transform) { + // We don't care about geometry for now +} + +static void output_handle_mode(void *data, struct wl_output *wl_output, + uint32_t flags, int32_t width, int32_t height, int32_t refresh) { + // We don't care about mode for now +} + +static void output_handle_done(void *data, struct wl_output *wl_output) { + // Nothing to do +} + +static void output_handle_scale(void *data, struct wl_output *wl_output, + int32_t factor) { + struct wlr_wl_parent_output *output = data; + output->scale = factor; + + wlr_log(WLR_DEBUG, "Parent output scale changed to %d", factor); + + // Update all backend outputs to use new maximum scale. + struct wlr_wl_backend *backend = output->backend; + int32_t max_scale = 1; + struct wlr_wl_parent_output *parent; + wl_list_for_each(parent, &backend->parent_outputs, link) { + if (parent->scale > max_scale) { + max_scale = parent->scale; + } + } + + // Propagate scale to outputs. + if (backend->max_parent_scale != max_scale) { + backend->max_parent_scale = max_scale; + + struct wlr_wl_output *wl_output; + wl_list_for_each(wl_output, &backend->outputs, link) { + wl_surface_set_buffer_scale(wl_output->surface, max_scale); + + if (wl_output->cursor.surface != NULL) { + wl_surface_set_buffer_scale(wl_output->cursor.surface, + max_scale); + } + + int32_t buffer_width = wl_output->logical_width * max_scale; + int32_t buffer_height = wl_output->logical_height * max_scale; + + // Request compositor to apply new mode and scale. + struct wlr_output_state state; + wlr_output_state_init(&state); + wlr_output_state_set_custom_mode(&state, buffer_width, buffer_height, 0); + wlr_output_state_set_scale(&state, (float)max_scale); + wlr_output_send_request_state(&wl_output->wlr_output, &state); + wlr_output_state_finish(&state); + } + } +} + +static const struct wl_output_listener output_listener = { + .geometry = output_handle_geometry, + .mode = output_handle_mode, + .done = output_handle_done, + .scale = output_handle_scale, +}; + static void xdg_wm_base_handle_ping(void *data, struct xdg_wm_base *base, uint32_t serial) { xdg_wm_base_pong(base, serial); @@ -419,9 +487,30 @@ static void registry_global(void *data, struct wl_registry *registry, } else if (strcmp(iface, wp_linux_drm_syncobj_manager_v1_interface.name) == 0) { wl->drm_syncobj_manager_v1 = wl_registry_bind(registry, name, &wp_linux_drm_syncobj_manager_v1_interface, 1); + } else if (strcmp(iface, wl_output_interface.name) == 0) { + struct wlr_wl_parent_output *output = calloc(1, sizeof(*output)); + if (output == NULL) { + wlr_log_errno(WLR_ERROR, "Failed to allocate parent output"); + return; + } + output->backend = wl; + output->wl_output = wl_registry_bind(registry, name, + &wl_output_interface, 2); // version 2 for scale support + output->scale = 1; + output->global_name = name; + wl_output_add_listener(output->wl_output, &output_listener, output); + wl_list_insert(&wl->parent_outputs, &output->link); + + wlr_log(WLR_DEBUG, "Bound to parent wl_output"); } } +static void parent_output_destroy(struct wlr_wl_parent_output *output) { + wl_list_remove(&output->link); + wl_output_destroy(output->wl_output); + free(output); +} + static void registry_global_remove(void *data, struct wl_registry *registry, uint32_t name) { struct wlr_wl_backend *wl = data; @@ -433,6 +522,24 @@ static void registry_global_remove(void *data, struct wl_registry *registry, break; } } + + struct wlr_wl_parent_output *output, *tmp; + wl_list_for_each_safe(output, tmp, &wl->parent_outputs, link) { + if (output->global_name == name) { + parent_output_destroy(output); + + // Recalculate max scale + int32_t max_scale = 1; + struct wlr_wl_parent_output *parent; + wl_list_for_each(parent, &wl->parent_outputs, link) { + if (parent->scale > max_scale) { + max_scale = parent->scale; + } + } + wl->max_parent_scale = max_scale; + break; + } + } } static const struct wl_registry_listener registry_listener = { @@ -485,6 +592,11 @@ static void backend_destroy(struct wlr_backend *backend) { wlr_output_destroy(&output->wlr_output); } + struct wlr_wl_parent_output *parent_output, *tmp_parent_output; + wl_list_for_each_safe(parent_output, tmp_parent_output, &wl->parent_outputs, link) { + parent_output_destroy(parent_output); + } + // Avoid using wl_list_for_each_safe() here: destroying a buffer may // have the side-effect of destroying the next one in the list while (!wl_list_empty(&wl->buffers)) { @@ -603,6 +715,8 @@ struct wlr_backend *wlr_wl_backend_create(struct wl_event_loop *loop, wl_list_init(&wl->seats); wl_list_init(&wl->buffers); wl_list_init(&wl->drm_syncobj_timelines); + wl_list_init(&wl->parent_outputs); + wl->max_parent_scale = 1; if (remote_display != NULL) { wl->remote_display = remote_display; diff --git a/backend/wayland/output.c b/backend/wayland/output.c index c3442f411..7c85b0489 100644 --- a/backend/wayland/output.c +++ b/backend/wayland/output.c @@ -566,6 +566,9 @@ static struct wlr_wl_output_layer *get_or_create_output_layer( &output_layer_addon_impl); layer->surface = wl_compositor_create_surface(output->backend->compositor); + + wl_surface_set_buffer_scale(layer->surface, output->backend->max_parent_scale); + layer->subsurface = wl_subcompositor_get_subsurface( output->backend->subcompositor, layer->surface, output->surface); @@ -914,6 +917,8 @@ static bool output_set_cursor(struct wlr_output *wlr_output, if (output->cursor.surface == NULL) { output->cursor.surface = wl_compositor_create_surface(backend->compositor); + wl_surface_set_buffer_scale(output->cursor.surface, + backend->max_parent_scale); } struct wl_surface *surface = output->cursor.surface; @@ -1005,10 +1010,14 @@ void update_wl_output_cursor(struct wlr_wl_output *output) { assert(pointer->output == output); assert(output->enter_serial); + // Hotspot must be in logical coordinates when buffer_scale is set. + int32_t scale = output->backend->max_parent_scale; + int32_t hotspot_x = output->cursor.hotspot_x / scale; + int32_t hotspot_y = output->cursor.hotspot_y / scale; + struct wlr_wl_seat *seat = pointer->seat; wl_pointer_set_cursor(seat->wl_pointer, output->enter_serial, - output->cursor.surface, output->cursor.hotspot_x, - output->cursor.hotspot_y); + output->cursor.surface, hotspot_x, hotspot_y); } } @@ -1036,17 +1045,20 @@ static void xdg_surface_handle_configure(void *data, struct wlr_wl_output *output = data; assert(output && output->xdg_surface == xdg_surface); - int32_t req_width = output->wlr_output.width; - int32_t req_height = output->wlr_output.height; + int32_t logical_width = output->logical_width; + int32_t logical_height = output->logical_height; if (output->requested_width > 0) { - req_width = output->requested_width; + logical_width = output->requested_width; output->requested_width = 0; } if (output->requested_height > 0) { - req_height = output->requested_height; + logical_height = output->requested_height; output->requested_height = 0; } + output->logical_width = logical_width; + output->logical_height = logical_height; + if (output->unmap_callback != NULL) { return; } @@ -1061,9 +1073,11 @@ static void xdg_surface_handle_configure(void *data, return; } + int32_t scale = output->backend->max_parent_scale; struct wlr_output_state state; wlr_output_state_init(&state); - wlr_output_state_set_custom_mode(&state, req_width, req_height, 0); + wlr_output_state_set_custom_mode(&state, logical_width * scale, logical_height * scale, 0); + wlr_output_state_set_scale(&state, (float)scale); wlr_output_send_request_state(&output->wlr_output, &state); wlr_output_state_finish(&state); } @@ -1108,9 +1122,16 @@ static struct wlr_wl_output *output_create(struct wlr_wl_backend *backend, } struct wlr_output *wlr_output = &output->wlr_output; + // Scale initial dimensions if parent has HiDPI scale + int32_t logical_width = 1280; + int32_t logical_height = 720; + output->logical_width = logical_width; + output->logical_height = logical_height; + + int32_t scale = backend->max_parent_scale; struct wlr_output_state state; wlr_output_state_init(&state); - wlr_output_state_set_custom_mode(&state, 1280, 720, 0); + wlr_output_state_set_custom_mode(&state, logical_width * scale, logical_height * scale, 0); wlr_output_init(wlr_output, &backend->backend, &output_impl, backend->event_loop, &state); @@ -1167,6 +1188,10 @@ struct wlr_output *wlr_wl_output_create(struct wlr_backend *wlr_backend) { return NULL; } + // Set buffer scale to match parent compositor's maximum scale + wl_surface_set_buffer_scale(surface, backend->max_parent_scale); + wlr_log(WLR_DEBUG, "Set output surface buffer scale to %d", backend->max_parent_scale); + struct wlr_wl_output *output = output_create(backend, surface); if (output == NULL) { wl_surface_destroy(surface); diff --git a/backend/wayland/pointer.c b/backend/wayland/pointer.c index b61e32cd0..ebd4d591d 100644 --- a/backend/wayland/pointer.c +++ b/backend/wayland/pointer.c @@ -88,12 +88,13 @@ static void pointer_handle_motion(void *data, struct wl_pointer *wl_pointer, return; } - struct wlr_output *wlr_output = &pointer->output->wlr_output; + // Parent compositor sends coordinates in logical (unscaled) space. + struct wlr_wl_output *output = pointer->output; struct wlr_pointer_motion_absolute_event event = { .pointer = &pointer->wlr_pointer, .time_msec = time, - .x = wl_fixed_to_double(sx) / wlr_output->width, - .y = wl_fixed_to_double(sy) / wlr_output->height, + .x = wl_fixed_to_double(sx) / output->logical_width, + .y = wl_fixed_to_double(sy) / output->logical_height, }; wl_signal_emit_mutable(&pointer->wlr_pointer.events.motion_absolute, &event); } diff --git a/backend/wayland/seat.c b/backend/wayland/seat.c index f178d9baf..bb79bd311 100644 --- a/backend/wayland/seat.c +++ b/backend/wayland/seat.c @@ -121,8 +121,9 @@ static void touch_coordinates_to_absolute(struct wlr_wl_seat *seat, */ struct wlr_wl_output *output, *tmp; wl_list_for_each_safe(output, tmp, &seat->backend->outputs, link) { - *sx = wl_fixed_to_double(x) / output->wlr_output.width; - *sy = wl_fixed_to_double(y) / output->wlr_output.height; + // Parent compositor sends coordinates in logical (unscaled) space. + *sx = wl_fixed_to_double(x) / output->logical_width; + *sy = wl_fixed_to_double(y) / output->logical_height; return; // Choose the first output in the list } diff --git a/backend/wayland/tablet_v2.c b/backend/wayland/tablet_v2.c index 3f6a16eed..41f2fd8dc 100644 --- a/backend/wayland/tablet_v2.c +++ b/backend/wayland/tablet_v2.c @@ -534,8 +534,9 @@ static void handle_tablet_tool_motion(void *data, struct zwp_tablet_tool_v2 *id, struct wlr_wl_output *output = tool->output; assert(output); - tool->x = wl_fixed_to_double(x) / output->wlr_output.width; - tool->y = wl_fixed_to_double(y) / output->wlr_output.height; + // Parent compositor sends coordinates in logical (unscaled) space. + tool->x = wl_fixed_to_double(x) / output->logical_width; + tool->y = wl_fixed_to_double(y) / output->logical_height; } static void handle_tablet_tool_pressure(void *data, diff --git a/include/backend/wayland.h b/include/backend/wayland.h index e24eb9fdf..9d20f3237 100644 --- a/include/backend/wayland.h +++ b/include/backend/wayland.h @@ -16,6 +16,14 @@ #include #include +struct wlr_wl_parent_output { + struct wlr_wl_backend *backend; + struct wl_output *wl_output; + uint32_t global_name; + int32_t scale; + struct wl_list link; +}; + struct wlr_wl_backend { struct wlr_backend backend; @@ -30,6 +38,9 @@ struct wlr_wl_backend { struct wl_listener event_loop_destroy; char *activation_token; + struct wl_list parent_outputs; // wlr_wl_parent_output.link + int32_t max_parent_scale; + /* remote state */ struct wl_display *remote_display; bool own_remote_display; @@ -99,6 +110,9 @@ struct wlr_wl_output { struct wlr_wl_backend *backend; struct wl_list link; + // From parent compositor, unscaled. + int32_t logical_width, logical_height; + struct wl_surface *surface; bool own_surface; struct wl_callback *frame_callback;