]> git.mdlowis.com Git - proto/labwc.git/commitdiff
Add tearing support (#1390)
authorPh42oN <59069994+Ph42oN@users.noreply.github.com>
Mon, 8 Jan 2024 20:58:58 +0000 (22:58 +0200)
committerGitHub <noreply@github.com>
Mon, 8 Jan 2024 20:58:58 +0000 (20:58 +0000)
Co-authored-by: Andrew J. Hesford <ajh@sideband.org>
13 files changed:
docs/labwc-actions.5.scd
docs/labwc-config.5.scd
docs/rc.xml.all
include/config/rcxml.h
include/labwc.h
include/view.h
protocols/meson.build
src/action.c
src/config/rcxml.c
src/meson.build
src/output.c
src/server.c
src/tearing.c [new file with mode: 0644]

index 20f057642e373d09c75982d63ac8f8be5f017239..cad047bffe621652beed0256377fe1a80f702e32 100644 (file)
@@ -154,6 +154,9 @@ Actions are used in menus and keyboard/mouse bindings.
        the usual keybinds will function again until switching back to the
        original window. There can be multiple windows with this mode set.
 
+*<action name="ToggleTearing" />*
+       Toggles tearing for the focused window.
+
 *<action name="FocusOutput" output="HDMI-A-1" />*
        Give focus to topmost window on given output and warp the cursor
        to the center of the window. If the given output does not contain
index fcf028fe6a0a691452a3dbc5b015f561c4dc3a4e..592958abd0428662e6056cdcbc217e637b9de40c 100644 (file)
@@ -109,6 +109,7 @@ this is for compatibility with Openbox.
   <decoration>server</decoration>
   <gap>0</gap>
   <adaptiveSync>no</adaptiveSync>
+  <allowTearing>no</allowTearing>
   <reuseOutputMode>no</reuseOutputMode>
 </core>
 ```
@@ -128,6 +129,12 @@ this is for compatibility with Openbox.
        *fullscreen* enables adaptive sync whenever a window is in fullscreen
        mode.
 
+*<core><allowTearing>* [yes|no]
+       Allow tearing to reduce input lag. Default is no.
+       This option requires setting the environment variable WLR_DRM_NO_ATOMIC=1.
+
+       *yes* allow tearing if requested by the active window.
+
 *<core><reuseOutputMode>* [yes|no]
        Try to re-use the existing output mode (resolution / refresh rate).
        This may prevent unnecessary screenblank delays when starting labwc
index 42429990c096e38aa1a2a0c730fec303de54a5d1..a8937adbae86d42f3dbea4d7771d6ce91ed8a050 100644 (file)
@@ -11,6 +11,7 @@
     <decoration>server</decoration>
     <gap>0</gap>
     <adaptiveSync>no</adaptiveSync>
+    <allowTearing>no</allowTearing>
     <reuseOutputMode>no</reuseOutputMode>
   </core>
 
index d03e4a74c9aa34e908341b745723bfbeb77f6731..27a342daf6f38460abb3c5e9651f6fa0f59be416 100644 (file)
@@ -53,6 +53,7 @@ struct rcxml {
        bool xdg_shell_server_side_deco;
        int gap;
        enum adaptive_sync_mode adaptive_sync;
+       bool allow_tearing;
        bool reuse_output_mode;
        enum view_placement_policy placement_policy;
 
index 7c409f6089c0771c5ed16c99fbd3cc0a5fe62e41..01f86b7368a27189f2f7a1f0b845ccfa229888c9 100644 (file)
@@ -39,6 +39,7 @@
 #include <wlr/types/wlr_drm_lease_v1.h>
 #include <wlr/types/wlr_virtual_pointer_v1.h>
 #include <wlr/types/wlr_virtual_keyboard_v1.h>
+#include <wlr/types/wlr_tearing_control_v1.h>
 #include <wlr/util/log.h>
 #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;
@@ -478,6 +482,7 @@ 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 server_init(struct server *server);
 void server_start(struct server *server);
index bfb84bd1a13d81143f67999a705a7de64f3a7b62..8c1c54912c63231c908d62017fdad0111df1bd9f 100644 (file)
@@ -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;
index 1cec5594dd71f4c83468310182510c73648caa57..0ec9154b27f580ff85da435931dd18481dbe29ce 100644 (file)
@@ -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',
index dabb31c92d1c4a577b2e9cfab38acc99130fc607..46bc8c9ee284395bc7ddae1f42327dac1bf36259 100644 (file)
@@ -102,6 +102,7 @@ enum action_type {
        ACTION_TYPE_VIRTUAL_OUTPUT_ADD,
        ACTION_TYPE_VIRTUAL_OUTPUT_REMOVE,
        ACTION_TYPE_AUTO_PLACE,
+       ACTION_TYPE_TOGGLE_TEARING,
 };
 
 const char *action_names[] = {
@@ -149,6 +150,7 @@ const char *action_names[] = {
        "VirtualOutputAdd",
        "VirtualOutputRemove",
        "AutoPlace",
+       "ToggleTearing",
        NULL
 };
 
@@ -951,6 +953,13 @@ actions_run(struct view *activator, struct server *server,
                                }
                        }
                        break;
+               case ACTION_TYPE_TOGGLE_TEARING:
+                       if (view) {
+                               view->tearing_hint = !view->tearing_hint;
+                               wlr_log(WLR_DEBUG, "tearing %sabled",
+                                       view->tearing_hint ? "en" : "dis");
+                       }
+                       break;
                case ACTION_TYPE_INVALID:
                        wlr_log(WLR_ERROR, "Not executing unknown action");
                        break;
index 1ff3766fdfce6935b9f1da0b1a54fec145beaef2..fd6fd0433704c56808afe95d735f31db414f1cf9 100644 (file)
@@ -740,6 +740,12 @@ entry(xmlNode *node, char *nodename, char *content)
                rc.gap = atoi(content);
        } else if (!strcasecmp(nodename, "adaptiveSync.core")) {
                set_adaptive_sync_mode(content, &rc.adaptive_sync);
+       } else if (!strcasecmp(nodename, "allowTearing.core")) {
+               set_bool(content, &rc.allow_tearing);
+               if (rc.allow_tearing && strcmp(getenv("WLR_DRM_NO_ATOMIC"), "1")) {
+                       rc.allow_tearing = false;
+                       wlr_log(WLR_INFO, "WLR_DRM_NO_ATOMIC is not 1, tearing disabled");
+               }
        } else if (!strcasecmp(nodename, "reuseOutputMode.core")) {
                set_bool(content, &rc.reuse_output_mode);
        } else if (!strcmp(nodename, "policy.placement")) {
index a14dc83e8e1c2d607b8071ad8e4b844dc6779fa5..baf46cb8c33973e0594cdc4e034d11d45bc337d1 100644 (file)
@@ -19,6 +19,7 @@ labwc_sources = files(
   'server.c',
   'session-lock.c',
   'snap.c',
+  'tearing.c',
   'theme.c',
   'view.c',
   'view-impl-common.c',
index 833cb09e905fbddf8d5460f4e3b633f1d8a011e1..968e89ca5cf3ab3b9e9c0f6c33b697ad93048910 100644 (file)
 #include "view.h"
 #include "xwayland.h"
 
+static bool
+get_tearing_preference(struct output *output)
+{
+       struct server *server = output->server;
+
+       /* Never allow tearing when disabled */
+       if (!rc.allow_tearing) {
+               return false;
+       }
+
+       /* Tearing is only allowed for the output with the active view */
+       if (!server->active_view || server->active_view->output != output) {
+               return false;
+       }
+
+       /* If the active view requests tearing, or it is toggled on with action, allow it */
+       return server->active_view->tearing_hint;
+}
+
 static void
 output_frame_notify(struct wl_listener *listener, void *data)
 {
@@ -68,7 +87,9 @@ output_frame_notify(struct wl_listener *listener, void *data)
                return;
        }
 
-       wlr_scene_output_commit(output->scene_output, NULL);
+       output->wlr_output->pending.tearing_page_flip =
+               get_tearing_preference(output);
+       lab_wlr_scene_output_commit(output->scene_output);
 
        struct timespec now = { 0 };
        clock_gettime(CLOCK_MONOTONIC, &now);
index e7a58be981ea6189f590ec4e6f897c793ea754bc..df4aface70711d81b278377fd165a905effa6ca7 100644 (file)
@@ -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 (file)
index 0000000..dc9f743
--- /dev/null
@@ -0,0 +1,50 @@
+// 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 && controller->tearing_control->hint) {
+               view->tearing_hint = true;
+       }
+}
+
+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);
+}