From f758cd1115255ed301751feaee3bc36b481fb409 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 Closes: https://gitlab.freedesktop.org/wlroots/wlroots/-/issues/854 --- backend/wayland/backend.c | 88 +++++++++++++++++++++++++++++++++++++++ backend/wayland/output.c | 36 +++++++++++++++- include/backend/wayland.h | 11 +++++ 3 files changed, 134 insertions(+), 1 deletion(-) diff --git a/backend/wayland/backend.c b/backend/wayland/backend.c index 14a783b67..c4c0a0885 100644 --- a/backend/wayland/backend.c +++ b/backend/wayland/backend.c @@ -87,6 +87,48 @@ 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 the 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; + } + } + backend->max_parent_scale = max_scale; +} + +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 +461,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 +496,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 +566,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 +689,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 fb4d1f914..e85c13c43 100644 --- a/backend/wayland/output.c +++ b/backend/wayland/output.c @@ -566,6 +566,12 @@ static struct wlr_wl_output_layer *get_or_create_output_layer( &output_layer_addon_impl); layer->surface = wl_compositor_create_surface(output->backend->compositor); + + // Set buffer scale for layer surfaces too + if (output->backend->max_parent_scale > 1) { + 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); @@ -1047,6 +1053,14 @@ static void xdg_surface_handle_configure(void *data, output->requested_height = 0; } + // If buffer scale is set, multiply dimensions to account for it + // The parent compositor sends logical dimensions, but we need physical buffer dimensions + int32_t scale = output->backend->max_parent_scale; + if (scale > 1) { + req_width *= scale; + req_height *= scale; + } + if (output->unmap_callback != NULL) { return; } @@ -1110,7 +1124,15 @@ static struct wlr_wl_output *output_create(struct wlr_wl_backend *backend, struct wlr_output_state state; wlr_output_state_init(&state); - wlr_output_state_set_custom_mode(&state, 1280, 720, 0); + + // Scale initial dimensions if parent has HiDPI scale + int32_t width = 1280; + int32_t height = 720; + if (backend->max_parent_scale > 1) { + width *= backend->max_parent_scale; + height *= backend->max_parent_scale; + } + wlr_output_state_set_custom_mode(&state, width, height, 0); wlr_output_init(wlr_output, &backend->backend, &output_impl, backend->event_loop, &state); @@ -1118,6 +1140,12 @@ static struct wlr_wl_output *output_create(struct wlr_wl_backend *backend, wlr_output->adaptive_sync_status = WLR_OUTPUT_ADAPTIVE_SYNC_ENABLED; + // Set output scale to match parent compositor so clients render at correct DPI + if (backend->max_parent_scale > 1) { + wlr_output->scale = (float)backend->max_parent_scale; + wlr_log(WLR_DEBUG, "Set output scale to %.1f to match parent", wlr_output->scale); + } + size_t output_num = ++last_output_num; char name[64]; @@ -1167,6 +1195,12 @@ struct wlr_output *wlr_wl_output_create(struct wlr_backend *wlr_backend) { return NULL; } + // Set buffer scale to match parent compositor's maximum scale + if (backend->max_parent_scale > 1) { + 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/include/backend/wayland.h b/include/backend/wayland.h index e24eb9fdf..11a4e023a 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;