From 91116ec0f06236a66316e73506aa9f65f32429c9 Mon Sep 17 00:00:00 2001 From: Sunderland93 Date: Mon, 20 Jul 2026 13:53:07 +0400 Subject: [PATCH 1/2] [Wayland]Add support for tearing-control-v1 protocol --- .gitignore | 2 + Makefile.common | 3 +- .../staging/tearing-control/README | 4 + .../tearing-control/tearing-control-v1.xml | 123 ++++++++++++++++++ gfx/common/wayland/generate_wayland_protos.sh | 1 + gfx/common/wayland_common.c | 22 ++++ gfx/drivers_context/wayland_ctx.c | 8 ++ gfx/drivers_context/wayland_vk_ctx.c | 8 ++ input/common/wayland_common.c | 4 + input/common/wayland_common.h | 3 + 10 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 deps/wayland-protocols/staging/tearing-control/README create mode 100644 deps/wayland-protocols/staging/tearing-control/tearing-control-v1.xml diff --git a/.gitignore b/.gitignore index 465a70bee745..ae3b9622b365 100644 --- a/.gitignore +++ b/.gitignore @@ -255,6 +255,8 @@ gfx/common/wayland/webos-shell.c gfx/common/wayland/webos-shell.h gfx/common/wayland/webos-surface-group.c gfx/common/wayland/webos-surface-group.h +gfx/common/wayland/tearing-control-v1.h +gfx/common/wayland/tearing-control-v1.c # libretro-common samples libretro-common/samples/streams/rzip/rzip diff --git a/Makefile.common b/Makefile.common index 6578a25ed347..afb0244c783c 100644 --- a/Makefile.common +++ b/Makefile.common @@ -1321,7 +1321,8 @@ ifeq ($(HAVE_WAYLAND), 1) gfx/common/wayland/cursor-shape-v1.o \ gfx/common/wayland/tablet-unstable-v2.o \ gfx/common/wayland/content-type-v1.o \ - gfx/common/wayland/single-pixel-buffer-v1.o + gfx/common/wayland/single-pixel-buffer-v1.o \ + gfx/common/wayland/tearing-control-v1.o ifeq ($(HAVE_VULKAN), 1) OBJ += gfx/drivers_context/wayland_vk_ctx.o diff --git a/deps/wayland-protocols/staging/tearing-control/README b/deps/wayland-protocols/staging/tearing-control/README new file mode 100644 index 000000000000..b221e587d00e --- /dev/null +++ b/deps/wayland-protocols/staging/tearing-control/README @@ -0,0 +1,4 @@ +Tearing control protocol + +Maintainers: +Xaver Hugl (@Zamundaaa) diff --git a/deps/wayland-protocols/staging/tearing-control/tearing-control-v1.xml b/deps/wayland-protocols/staging/tearing-control/tearing-control-v1.xml new file mode 100644 index 000000000000..9c44fbfca5a0 --- /dev/null +++ b/deps/wayland-protocols/staging/tearing-control/tearing-control-v1.xml @@ -0,0 +1,123 @@ + + + + Copyright © 2021 Xaver Hugl + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice (including the next + paragraph) shall be included in all copies or substantial portions of the + Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. + + + + + For some use cases like games or drawing tablets it can make sense to + reduce latency by accepting tearing with the use of asynchronous page + flips. This global is a factory interface, allowing clients to inform + which type of presentation the content of their surfaces is suitable for. + + Graphics APIs like EGL or Vulkan, that manage the buffer queue and commits + of a wl_surface themselves, are likely to be using this extension + internally. If a client is using such an API for a wl_surface, it should + not directly use this extension on that surface, to avoid raising a + tearing_control_exists protocol error. + + Warning! The protocol described in this file is currently in the testing + phase. Backward compatible changes may be added together with the + corresponding interface version bump. Backward incompatible changes can + only be done by creating a new major version of the extension. + + + + + Destroy this tearing control factory object. Other objects, including + wp_tearing_control_v1 objects created by this factory, are not affected + by this request. + + + + + + + + + + Instantiate an interface extension for the given wl_surface to request + asynchronous page flips for presentation. + + If the given wl_surface already has a wp_tearing_control_v1 object + associated, the tearing_control_exists protocol error is raised. + + + + + + + + + An additional interface to a wl_surface object, which allows the client + to hint to the compositor if the content on the surface is suitable for + presentation with tearing. + The default presentation hint is vsync. See presentation_hint for more + details. + + If the associated wl_surface is destroyed, this object becomes inert and + should be destroyed. + + + + + This enum provides information for if submitted frames from the client + may be presented with tearing. + + + + The content of this surface is meant to be synchronized to the + vertical blanking period. This should not result in visible tearing + and may result in a delay before a surface commit is presented. + + + + + The content of this surface is meant to be presented with minimal + latency and tearing is acceptable. + + + + + + + Set the presentation hint for the associated wl_surface. This state is + double-buffered, see wl_surface.commit. + + The compositor is free to dynamically respect or ignore this hint based + on various conditions like hardware capabilities, surface state and + user preferences. + + + + + + + Destroy this surface tearing object and revert the presentation hint to + vsync. The change will be applied on the next wl_surface.commit. + + + + + diff --git a/gfx/common/wayland/generate_wayland_protos.sh b/gfx/common/wayland/generate_wayland_protos.sh index 4dbdd95b1044..edb4bb497649 100755 --- a/gfx/common/wayland/generate_wayland_protos.sh +++ b/gfx/common/wayland/generate_wayland_protos.sh @@ -81,6 +81,7 @@ generate_source 'staging/cursor-shape' 'cursor-shape-v1' generate_source 'unstable/tablet' 'tablet-unstable-v2' generate_source 'staging/content-type' 'content-type-v1' generate_source 'staging/single-pixel-buffer' 'single-pixel-buffer-v1' +generate_source 'staging/tearing-control' 'tearing-control-v1' generate_source 'staging/xdg-toplevel-icon' 'xdg-toplevel-icon-v1' generate_source 'staging/xdg-toplevel-tag' 'xdg-toplevel-tag-v1' diff --git a/gfx/common/wayland_common.c b/gfx/common/wayland_common.c index 6d973ae65646..5f9972374cbf 100644 --- a/gfx/common/wayland_common.c +++ b/gfx/common/wayland_common.c @@ -446,6 +446,8 @@ void gfx_ctx_wl_destroy_resources_common(gfx_ctx_wayland_data_t *wl) wp_viewport_destroy(wl->viewport); if (wl->fractional_scale) wp_fractional_scale_v1_destroy(wl->fractional_scale); + if (wl->tearing_control) + wp_tearing_control_v1_destroy(wl->tearing_control); if (wl->idle_inhibitor) zwp_idle_inhibitor_v1_destroy(wl->idle_inhibitor); if (wl->deco) @@ -467,6 +469,8 @@ void gfx_ctx_wl_destroy_resources_common(gfx_ctx_wayland_data_t *wl) xdg_toplevel_tag_manager_v1_destroy(wl->xdg_toplevel_tag_manager); if (wl->idle_inhibit_manager) zwp_idle_inhibit_manager_v1_destroy(wl->idle_inhibit_manager); + if (wl->tearing_control_manager) + wp_tearing_control_manager_v1_destroy(wl->tearing_control_manager); else { #ifdef HAVE_DBUS @@ -542,6 +546,8 @@ void gfx_ctx_wl_destroy_resources_common(gfx_ctx_wayland_data_t *wl) wl->idle_inhibit_manager = NULL; wl->deco_manager = NULL; wl->single_pixel_manager = NULL; + wl->tearing_control_manager = NULL; + wl->tearing_control = NULL; wl->surface = NULL; wl->xdg_surface = NULL; wl->xdg_toplevel = NULL; @@ -978,6 +984,11 @@ bool gfx_ctx_wl_init_common( RARCH_LOG("[Wayland] Compositor doesn't support the %s protocol.\n", xdg_toplevel_tag_manager_v1_interface.name); } + if (!wl->tearing_control_manager) + { + RARCH_LOG("[Wayland] Compositor doesn't support the %s protocol.\n", wp_tearing_control_manager_v1_interface.name); + } + wl->surface = wl_compositor_create_surface(wl->compositor); if (wl->viewporter) wl->viewport = wp_viewporter_get_viewport(wl->viewporter, wl->surface); @@ -995,6 +1006,17 @@ bool gfx_ctx_wl_init_common( wp_content_type_v1_set_content_type(wl->content_type, WP_CONTENT_TYPE_V1_TYPE_GAME); } + if (wl->tearing_control_manager) + { + bool video_vsync = settings->bools.video_vsync; + wl->tearing_control = wp_tearing_control_manager_v1_get_tearing_control( + wl->tearing_control_manager, wl->surface); + wp_tearing_control_v1_set_presentation_hint(wl->tearing_control, + video_vsync + ? WP_TEARING_CONTROL_V1_PRESENTATION_HINT_VSYNC + : WP_TEARING_CONTROL_V1_PRESENTATION_HINT_ASYNC); + } + wl_surface_add_listener(wl->surface, &wl_surface_listener, wl); #ifdef HAVE_LIBDECOR_H diff --git a/gfx/drivers_context/wayland_ctx.c b/gfx/drivers_context/wayland_ctx.c index cb1fd8386c5d..375a1949f043 100644 --- a/gfx/drivers_context/wayland_ctx.c +++ b/gfx/drivers_context/wayland_ctx.c @@ -345,6 +345,14 @@ static void gfx_ctx_wl_set_swap_interval(void *data, int swap_interval) #ifdef HAVE_EGL egl_set_swap_interval(&wl->egl, swap_interval); #endif + + if (wl->tearing_control) + { + wp_tearing_control_v1_set_presentation_hint(wl->tearing_control, + swap_interval == 0 + ? WP_TEARING_CONTROL_V1_PRESENTATION_HINT_ASYNC + : WP_TEARING_CONTROL_V1_PRESENTATION_HINT_VSYNC); + } } static bool gfx_ctx_wl_set_video_mode(void *data, diff --git a/gfx/drivers_context/wayland_vk_ctx.c b/gfx/drivers_context/wayland_vk_ctx.c index bef0da26b954..1be5b55223aa 100644 --- a/gfx/drivers_context/wayland_vk_ctx.c +++ b/gfx/drivers_context/wayland_vk_ctx.c @@ -141,6 +141,14 @@ static void gfx_ctx_wl_set_swap_interval(void *data, int swap_interval) if (wl->vk.swapchain) wl->vk.flags |= VK_DATA_FLAG_NEED_NEW_SWAPCHAIN; } + + if (wl->tearing_control) + { + wp_tearing_control_v1_set_presentation_hint(wl->tearing_control, + swap_interval == 0 + ? WP_TEARING_CONTROL_V1_PRESENTATION_HINT_ASYNC + : WP_TEARING_CONTROL_V1_PRESENTATION_HINT_VSYNC); + } } static bool gfx_ctx_wl_set_video_mode(void *data, diff --git a/input/common/wayland_common.c b/input/common/wayland_common.c index 5ef9c3fdcc70..28d282d60e5a 100644 --- a/input/common/wayland_common.c +++ b/input/common/wayland_common.c @@ -837,6 +837,10 @@ static void wl_registry_handle_global(void *data, struct wl_registry *reg, wl->xdg_toplevel_tag_manager = (struct xdg_toplevel_tag_manager_v1*) wl_registry_bind( reg, id, &xdg_toplevel_tag_manager_v1_interface, MIN(version, 1)); + else if (string_is_equal(interface, wp_tearing_control_manager_v1_interface.name) && found++) + wl->tearing_control_manager = (struct wp_tearing_control_manager_v1*) + wl_registry_bind( + reg, id, &wp_tearing_control_manager_v1_interface, MIN(version, 1)); if (found > 1) RARCH_LOG("[Wayland] Registered interface %s at version %u.\n", diff --git a/input/common/wayland_common.h b/input/common/wayland_common.h index 09d58b11a531..c55502be33c4 100644 --- a/input/common/wayland_common.h +++ b/input/common/wayland_common.h @@ -46,6 +46,7 @@ #include "../../gfx/common/wayland/pointer-constraints-unstable-v1.h" #include "../../gfx/common/wayland/relative-pointer-unstable-v1.h" #include "../../gfx/common/wayland/single-pixel-buffer-v1.h" +#include "../../gfx/common/wayland/tearing-control-v1.h" #include "../../gfx/common/wayland/viewporter.h" #include "../../gfx/common/wayland/xdg-decoration-unstable-v1.h" #include "../../gfx/common/wayland/xdg-shell.h" @@ -181,6 +182,8 @@ typedef struct gfx_ctx_wayland_data struct xdg_toplevel_icon_v1 *xdg_toplevel_icon; struct xdg_toplevel_icon_manager_v1 *xdg_toplevel_icon_manager; struct xdg_toplevel_tag_manager_v1 *xdg_toplevel_tag_manager; + struct wp_tearing_control_manager_v1 *tearing_control_manager; + struct wp_tearing_control_v1 *tearing_control; struct wl_keyboard *wl_keyboard; struct wl_pointer *wl_pointer; struct zwp_relative_pointer_v1 *wl_relative_pointer; From acb405ec344342ff7cfb8c32c8c22a3a9925eeda Mon Sep 17 00:00:00 2001 From: LibretroAdmin Date: Mon, 20 Jul 2026 17:40:47 +0000 Subject: [PATCH 2/2] Fix dangling else in tearing-control teardown The tearing-control-v1 support added the manager destroy call between idle_inhibit_manager's if and its else, so the DBus screensaver-inhibit fallback teardown (dbus_screensaver_uninhibit / dbus_close_connection) became gated on wp_tearing_control_manager_v1 being absent instead of zwp_idle_inhibit_manager_v1. Consequences: - Compositor with idle-inhibit but no tearing-control: DBus uninhibit and close_connection fire spuriously even though the DBus fallback was never used. - Compositor with tearing-control but no idle-inhibit (DBus fallback in use): the else is skipped, leaking the DBus connection and leaving the screensaver inhibited after exit. Move the tearing_control_manager destroy out from between the if/else so the DBus fallback binds to idle_inhibit_manager again. --- gfx/common/wayland_common.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gfx/common/wayland_common.c b/gfx/common/wayland_common.c index 5f9972374cbf..b6c34d4d78a1 100644 --- a/gfx/common/wayland_common.c +++ b/gfx/common/wayland_common.c @@ -469,8 +469,6 @@ void gfx_ctx_wl_destroy_resources_common(gfx_ctx_wayland_data_t *wl) xdg_toplevel_tag_manager_v1_destroy(wl->xdg_toplevel_tag_manager); if (wl->idle_inhibit_manager) zwp_idle_inhibit_manager_v1_destroy(wl->idle_inhibit_manager); - if (wl->tearing_control_manager) - wp_tearing_control_manager_v1_destroy(wl->tearing_control_manager); else { #ifdef HAVE_DBUS @@ -478,6 +476,8 @@ void gfx_ctx_wl_destroy_resources_common(gfx_ctx_wayland_data_t *wl) dbus_close_connection(); #endif } + if (wl->tearing_control_manager) + wp_tearing_control_manager_v1_destroy(wl->tearing_control_manager); if (wl->pointer_constraints) zwp_pointer_constraints_v1_destroy(wl->pointer_constraints); if (wl->relative_pointer_manager)