From fc69b80234d4b3a2882a7b5ca1c8e0a49d472dc6 Mon Sep 17 00:00:00 2001 From: Ph42oN Date: Thu, 4 Jan 2024 01:24:29 +0200 Subject: [PATCH] tearing protocol support and documentation --- docs/labwc-config.5.scd | 12 +++++++ include/config/rcxml.h | 1 + include/labwc.h | 6 ++++ include/view.h | 1 + protocols/meson.build | 1 + src/config/rcxml.c | 2 ++ src/meson.build | 1 + src/output.c | 6 ++-- src/server.c | 4 +++ src/tearing.c | 80 +++++++++++++++++++++++++++++++++++++++++ src/view.c | 17 ++------- 11 files changed, 112 insertions(+), 19 deletions(-) create mode 100644 src/tearing.c diff --git a/docs/labwc-config.5.scd b/docs/labwc-config.5.scd index db01c469..81c2784f 100644 --- a/docs/labwc-config.5.scd +++ b/docs/labwc-config.5.scd @@ -109,6 +109,7 @@ this is for compatibility with Openbox. server 0 no + no no ``` @@ -128,6 +129,17 @@ this is for compatibility with Openbox. *fullscreen* enables adaptive sync whenever a window is in fullscreen mode. +** [yes|no|fullscreen|always] + Allow tearing to reduce input lag. Default is no. + This open requires setting the environment variable WLR_DRM_NO_ATOMIC=1. + + *yes* allow tearing if an open window requests it. + + *fullscreen* allow tearing if an open window requests it, or if any + window is in fullscreen mode. + + *always* allow tearing regardless of window status. + ** [yes|no] Try to re-use the existing output mode (resolution / refresh rate). This may prevent unnecessary screenblank delays when starting labwc diff --git a/include/config/rcxml.h b/include/config/rcxml.h index 9396ebae..bb82609e 100644 --- a/include/config/rcxml.h +++ b/include/config/rcxml.h @@ -38,6 +38,7 @@ enum tearing_mode { LAB_TEARING_DISABLED, LAB_TEARING_ENABLED, LAB_TEARING_FULLSCREEN, + LAB_TEARING_ALWAYS, }; struct usable_area_override { diff --git a/include/labwc.h b/include/labwc.h index 74bd3fe9..1e387905 100644 --- a/include/labwc.h +++ b/include/labwc.h @@ -39,6 +39,7 @@ #include #include #include +#include #include #include "config/keybind.h" #include "config/rcxml.h" @@ -318,6 +319,9 @@ struct server { struct wlr_pointer_constraints_v1 *constraints; struct wl_listener new_constraint; + struct wlr_tearing_control_manager_v1 *tearing_control; + struct wl_listener tearing_new_object; + /* Set when in cycle (alt-tab) mode */ struct osd_state { struct view *cycle_view; @@ -479,6 +483,8 @@ void handle_output_power_manager_set_mode(struct wl_listener *listener, void output_add_virtual(struct server *server, const char *output_name); void output_remove_virtual(struct server *server, const char *output_name); void output_enable_adaptive_sync(struct wlr_output *output, bool enabled); +void new_tearing_hint(struct wl_listener *listener, void *data); +void set_tearing(struct output *output); void server_init(struct server *server); void server_start(struct server *server); diff --git a/include/view.h b/include/view.h index bef7afea..6d1c12dd 100644 --- a/include/view.h +++ b/include/view.h @@ -151,6 +151,7 @@ struct view { bool minimized; enum view_axis maximized; bool fullscreen; + bool tearing_hint; bool visible_on_all_workspaces; enum view_edge tiled; bool inhibits_keybinds; diff --git a/protocols/meson.build b/protocols/meson.build index 527f9d4c..741da7bc 100644 --- a/protocols/meson.build +++ b/protocols/meson.build @@ -18,6 +18,7 @@ server_protocols = [ wl_protocol_dir / 'unstable/pointer-constraints/pointer-constraints-unstable-v1.xml', wl_protocol_dir / 'staging/cursor-shape/cursor-shape-v1.xml', wl_protocol_dir / 'staging/drm-lease/drm-lease-v1.xml', + wl_protocol_dir / 'staging/tearing-control/tearing-control-v1.xml', 'wlr-layer-shell-unstable-v1.xml', 'wlr-input-inhibitor-unstable-v1.xml', 'wlr-output-power-management-unstable-v1.xml', diff --git a/src/config/rcxml.c b/src/config/rcxml.c index 5aa2b693..11bd7f77 100644 --- a/src/config/rcxml.c +++ b/src/config/rcxml.c @@ -630,6 +630,8 @@ set_tearing_mode(const char *str, enum tearing_mode *variable) { if (!strcasecmp(str, "fullscreen")) { *variable = LAB_TEARING_FULLSCREEN; + } else if (!strcasecmp(str, "always")) { + *variable = LAB_TEARING_ALWAYS; } else { int ret = parse_bool(str, -1); if (ret == 1) { diff --git a/src/meson.build b/src/meson.build index a14dc83e..baf46cb8 100644 --- a/src/meson.build +++ b/src/meson.build @@ -19,6 +19,7 @@ labwc_sources = files( 'server.c', 'session-lock.c', 'snap.c', + 'tearing.c', 'theme.c', 'view.c', 'view-impl-common.c', diff --git a/src/output.c b/src/output.c index 8103eea6..4319c847 100644 --- a/src/output.c +++ b/src/output.c @@ -68,9 +68,7 @@ output_frame_notify(struct wl_listener *listener, void *data) return; } - if (output->tearing) { - output->wlr_output->pending.tearing_page_flip = true; - } + output->wlr_output->pending.tearing_page_flip = output->tearing; lab_wlr_scene_output_commit(output->scene_output); @@ -277,7 +275,7 @@ new_output_notify(struct wl_listener *listener, void *data) wl_list_init(&output->regions); - if (rc.allow_tearing == LAB_TEARING_ENABLED) { + if (rc.allow_tearing == LAB_TEARING_ALWAYS) { output->tearing = true; } else { output->tearing = false; diff --git a/src/server.c b/src/server.c index e7a58be9..df4aface 100644 --- a/src/server.c +++ b/src/server.c @@ -449,6 +449,10 @@ server_init(struct server *server) wl_signal_add(&server->output_power_manager_v1->events.set_mode, &server->output_power_manager_set_mode); + server->tearing_control = wlr_tearing_control_manager_v1_create(server->wl_display, 1); + server->tearing_new_object.notify = new_tearing_hint; + wl_signal_add(&server->tearing_control->events.new_object, &server->tearing_new_object); + layers_init(server); #if HAVE_XWAYLAND diff --git a/src/tearing.c b/src/tearing.c new file mode 100644 index 00000000..351994d6 --- /dev/null +++ b/src/tearing.c @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include "labwc.h" +#include "view.h" + +struct tearing_controller { + struct wlr_tearing_control_v1 *tearing_control; + struct wl_listener set_hint; + struct wl_listener destroy; +}; + +static void +set_tearing_hint(struct wl_listener *listener, void *data) +{ + struct tearing_controller *controller = wl_container_of(listener, controller, set_hint); + struct view *view = view_from_wlr_surface(controller->tearing_control->surface); + if (view) { + view->tearing_hint = controller->tearing_control->hint; + } +} + +static void +tearing_controller_destroy(struct wl_listener *listener, void *data) +{ + struct tearing_controller *controller = wl_container_of(listener, controller, destroy); + free(controller); +} + +void +new_tearing_hint(struct wl_listener *listener, void *data) +{ + struct server *server = wl_container_of(listener, server, tearing_new_object); + struct wlr_tearing_control_v1 *tearing_control = data; + + enum wp_tearing_control_v1_presentation_hint hint = + wlr_tearing_control_manager_v1_surface_hint_from_surface + (server->tearing_control, tearing_control->surface); + wlr_log(WLR_DEBUG, "New presentation hint %d received for surface %p", + hint, tearing_control->surface); + + struct tearing_controller *controller = calloc(1, sizeof(struct tearing_controller)); + if (!controller) { + return; + } + controller->tearing_control = tearing_control; + controller->set_hint.notify = set_tearing_hint; + wl_signal_add(&tearing_control->events.set_hint, &controller->set_hint); + controller->destroy.notify = tearing_controller_destroy; + wl_signal_add(&tearing_control->events.destroy, &controller->destroy); +} + +void +set_tearing(struct output *output) +{ + if (rc.allow_tearing == LAB_TEARING_DISABLED) { + output->tearing = false; + return; + } + + if (rc.allow_tearing == LAB_TEARING_ALWAYS) { + output->tearing = true; + return; + } + + struct server *server = output->server; + struct view *view; + bool on_fullscreen = rc.allow_tearing == LAB_TEARING_FULLSCREEN; + + wl_list_for_each(view, &server->views, link) { + if (view->output != output) { + continue; + } + + if (view->tearing_hint || (on_fullscreen && view->fullscreen)) { + output->tearing = true; + return; + } + } + output->tearing = false; +} diff --git a/src/view.c b/src/view.c index 15c6256e..09499faa 100644 --- a/src/view.c +++ b/src/view.c @@ -273,16 +273,6 @@ set_adaptive_sync_fullscreen(struct view *view) wlr_output_commit(view->output->wlr_output); } -static void -set_tearing_fullscreen(struct view *view) -{ - if (rc.allow_tearing != LAB_TEARING_FULLSCREEN) { - return; - } - /* Enable tearing if view is fullscreen */ - view->output->tearing = view->fullscreen; -} - void view_set_activated(struct view *view, bool activated) { @@ -307,7 +297,7 @@ view_set_activated(struct view *view, bool activated) } } set_adaptive_sync_fullscreen(view); - set_tearing_fullscreen(view); + set_tearing(view->output); } void @@ -1210,7 +1200,7 @@ view_set_fullscreen(struct view *view, bool fullscreen) view_apply_special_geometry(view); } set_adaptive_sync_fullscreen(view); - set_tearing_fullscreen(view); + set_tearing(view->output); } void @@ -1881,9 +1871,6 @@ view_destroy(struct view *view) if (rc.adaptive_sync == LAB_ADAPTIVE_SYNC_FULLSCREEN) { wlr_output_enable_adaptive_sync(view->output->wlr_output, false); } - if (rc.allow_tearing == LAB_TEARING_FULLSCREEN) { - view->output->tearing = false; - } } /* If we spawned a window menu, close it */