Introduce support for present's window flip mode. The support is not yet
complete, but works reasonable well for the most important use case, what
is fullscreen applications

We take a present flip and if the xwl_window->window has the same dimensions
as the presenting window, the flip is represented by a wl_buffer and
attached to the main wl_surface of the xwl_window.

After commit we are listening for the sync callback in order to tell present,
that the pixmap flip is not longer pending, for the frame callback in order
to update the msc counter and for the buffer release callback in order to tell
present that the pixmap is idle again.

In case the compositor is not sending any more frame callbacks, we use a slow
timer to continue the msc counter.

The following functionality is missing from this implementation:
* per window flips for child windows with smaller size than the xwl_window,
* queuing events to MSC times,
* reporting UST values to present.

To make use of this functionality Xwayland must run rootless and with
Glamor/GBM.

Signed-off-by: Roman Gilg <subd...@gmail.com>
---
 hw/xwayland/Makefile.am        |   1 +
 hw/xwayland/meson.build        |   1 +
 hw/xwayland/xwayland-present.c | 423 +++++++++++++++++++++++++++++++++++++++++
 hw/xwayland/xwayland.c         |  20 ++
 hw/xwayland/xwayland.h         |  31 +++
 5 files changed, 476 insertions(+)
 create mode 100644 hw/xwayland/xwayland-present.c

diff --git a/hw/xwayland/Makefile.am b/hw/xwayland/Makefile.am
index 7204591..9a2369b 100644
--- a/hw/xwayland/Makefile.am
+++ b/hw/xwayland/Makefile.am
@@ -11,6 +11,7 @@ Xwayland_CFLAGS =                             \
 
 Xwayland_SOURCES =                             \
        xwayland.c                              \
+       xwayland-present.c                      \
        xwayland-input.c                        \
        xwayland-cursor.c                       \
        xwayland-shm.c                          \
diff --git a/hw/xwayland/meson.build b/hw/xwayland/meson.build
index 24203c6..d58c5a1 100644
--- a/hw/xwayland/meson.build
+++ b/hw/xwayland/meson.build
@@ -1,5 +1,6 @@
 srcs = [
     'xwayland.c',
+    'xwayland-present.c',
     'xwayland-input.c',
     'xwayland-cursor.c',
     'xwayland-shm.c',
diff --git a/hw/xwayland/xwayland-present.c b/hw/xwayland/xwayland-present.c
new file mode 100644
index 0000000..d08c39e
--- /dev/null
+++ b/hw/xwayland/xwayland-present.c
@@ -0,0 +1,423 @@
+/*
+ * Copyright © 2018 Roman Gilg
+ *
+ * Permission to use, copy, modify, distribute, and sell this software
+ * and its documentation for any purpose is hereby granted without
+ * fee, provided that the above copyright notice appear in all copies
+ * and that both that copyright notice and this permission notice
+ * appear in supporting documentation, and that the name of the
+ * copyright holders not be used in advertising or publicity
+ * pertaining to distribution of the software without specific,
+ * written prior permission.  The copyright holders make no
+ * representations about the suitability of this software for any
+ * purpose.  It is provided "as is" without express or implied
+ * warranty.
+ *
+ * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
+ * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
+ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+ * SOFTWARE.
+ */
+
+#include "xwayland.h"
+
+#include <present.h>
+
+#define FRAME_TIMER_IVAL 67 // ~15fps
+
+static struct xorg_list xwl_present_windows;
+
+void
+xwl_present_cleanup(WindowPtr window)
+{
+    struct xwl_window           *xwl_window = xwl_window_of_top(window);
+    struct xwl_present_event    *event, *tmp;
+
+    if (!xwl_window)
+        return;
+
+    if (!xwl_window->present_window)
+        return;
+
+    if (xwl_window->present_window != window && xwl_window->window != window)
+        /* Unrealizing a non-presenting sibling */
+        return;
+
+    /*
+     * At this point we're either:
+     * - Unflipping.
+     * - Unrealizing the presenting window 'xwl_window->present_window'
+     *   or its ancestor 'xwl_window->window'.
+     * And therefore need to cleanup.
+     */
+
+    if (xwl_window->present_frame_callback) {
+        wl_callback_destroy(xwl_window->present_frame_callback);
+        xwl_window->present_frame_callback = NULL;
+    }
+
+    /* Reset base data */
+    xorg_list_del(&xwl_window->present_link);
+
+    xwl_window->present_surface = NULL;
+    xwl_window->present_window = NULL;
+
+    TimerFree(xwl_window->present_frame_timer);
+    xwl_window->present_frame_timer = NULL;
+
+    /* Clear remaining events */
+    xorg_list_for_each_entry_safe(event, tmp, &xwl_window->present_event_list, 
list) {
+        xorg_list_del(&event->list);
+        free(event);
+    }
+
+    /* Clear remaining buffer releases and inform Present about free 
ressources */
+    xorg_list_for_each_entry_safe(event, tmp, 
&xwl_window->present_release_queue, list) {
+        present_wnmd_event_notify(xwl_window->window, event->event_id, 0, 
xwl_window->present_msc);
+        xorg_list_del(&event->list);
+        if (event->pending)
+            event->abort = TRUE;
+        else
+            free(event);
+    }
+}
+
+static void
+buffer_release(void *data, struct wl_buffer *buffer)
+{
+    WindowPtr                   present_window = 
wl_buffer_get_user_data(buffer);
+    struct xwl_window           *xwl_window;
+    struct xwl_present_event    *event, *tmp;
+    Bool                        found_window = FALSE;
+
+    /* Find window */
+    xorg_list_for_each_entry(xwl_window, &xwl_present_windows, present_link) {
+        if (xwl_window->present_window == present_window) {
+            found_window = TRUE;
+            break;
+        }
+    }
+
+    if (!found_window)
+        return;
+
+    xorg_list_for_each_entry_safe(event, tmp, 
&xwl_window->present_release_queue, list) {
+        if (event->buffer == buffer) {
+            if (!event->abort)
+                present_wnmd_event_notify(present_window, event->event_id, 0, 
xwl_window->present_msc);
+            event->abort = TRUE;
+
+            if (!event->pending) {
+                xorg_list_del(&event->list);
+                free(event);
+            }
+            break;
+        }
+    }
+}
+
+static const struct wl_buffer_listener release_listener = {
+    buffer_release
+};
+
+static void
+xwl_present_events_notify(struct xwl_window *xwl_window)
+{
+    uint64_t                    msc = xwl_window->present_msc;
+    struct xwl_present_event    *event, *tmp;
+
+    xorg_list_for_each_entry_safe(event, tmp, &xwl_window->present_event_list, 
list) {
+        if (event->target_msc <= msc) {
+            present_wnmd_event_notify(xwl_window->present_window, 
event->event_id, 0, msc);
+            xorg_list_del(&event->list);
+            free(event);
+        }
+    }
+}
+
+static void
+present_frame_callback(void *data,
+               struct wl_callback *callback,
+               uint32_t time)
+{
+    struct xwl_window *xwl_window = data;
+
+    /* we do not need the timer anymore for this frame */
+    TimerCancel(xwl_window->present_frame_timer);
+
+    wl_callback_destroy(xwl_window->present_frame_callback);
+    xwl_window->present_frame_callback = NULL;
+
+    if (xwl_window->present_frame_timer_firing) {
+        /* If the timer was firing, this frame callback is too late */
+        xwl_window->present_frame_timer_firing = FALSE;
+        return;
+    }
+
+    xwl_window->present_msc++;
+    xwl_present_events_notify(xwl_window);
+}
+
+static const struct wl_callback_listener present_frame_listener = {
+    present_frame_callback
+};
+
+static CARD32
+present_frame_timer_callback(OsTimerPtr timer,
+                             CARD32 time,
+                             void *arg)
+{
+    struct xwl_window *xwl_window = arg;
+
+    xwl_window->present_frame_timer_firing = TRUE;
+    xwl_window->present_msc++;
+    xwl_present_events_notify(xwl_window);
+
+    return FRAME_TIMER_IVAL;
+}
+
+static void
+xwl_present_sync_callback(void *data,
+               struct wl_callback *callback,
+               uint32_t time)
+{
+    struct xwl_present_event *event = data;
+    struct xwl_window *xwl_window = event->xwl_window;
+
+    event->pending = FALSE;
+
+    // event might have been aborted
+    if (event->abort) {
+        xorg_list_del(&event->list);
+        free(event);
+    } else
+        present_wnmd_event_notify(xwl_window->present_window,
+                                  event->event_id,
+                                  0,
+                                  xwl_window->present_msc);
+}
+
+static const struct wl_callback_listener xwl_present_sync_listener = {
+    xwl_present_sync_callback
+};
+
+static RRCrtcPtr
+xwl_present_get_crtc(WindowPtr present_window)
+{
+    struct xwl_window *xwl_window = xwl_window_of_top(present_window);
+    if (xwl_window == NULL)
+        return NULL;
+
+    return xwl_window->present_crtc_fake;
+}
+
+static int
+xwl_present_get_ust_msc(WindowPtr present_window, uint64_t *ust, uint64_t *msc)
+{
+    struct xwl_window *xwl_window = xwl_window_of_top(present_window);
+    if (!xwl_window)
+        return BadAlloc;
+    *ust = 0;
+    *msc = xwl_window->present_msc;
+
+    return Success;
+}
+
+/*
+ * Queue an event to report back to the Present extension when the specified
+ * MSC has past
+ */
+static int
+xwl_present_queue_vblank(WindowPtr present_window,
+                         RRCrtcPtr crtc,
+                         uint64_t event_id,
+                         uint64_t msc)
+{
+    /*
+     * Queuing events is not yet implemented.
+     *
+     */
+    return BadRequest;
+    /* */
+}
+
+/*
+ * Remove a pending vblank event so that it is not reported
+ * to the extension
+ */
+static void
+xwl_present_abort_vblank(WindowPtr present_window, RRCrtcPtr crtc, uint64_t 
event_id, uint64_t msc)
+{
+    struct xwl_window *xwl_window = xwl_window_of_top(present_window);
+    struct xwl_present_event *event, *tmp;
+
+    xorg_list_for_each_entry_safe(event, tmp, &xwl_window->present_event_list, 
list) {
+        if (event->event_id == event_id) {
+            xorg_list_del(&event->list);
+            free(event);
+            return;
+        }
+    }
+
+    xorg_list_for_each_entry(event, &xwl_window->present_release_queue, list) {
+        if (event->event_id == event_id) {
+            event->abort = TRUE;
+            break;
+        }
+    }
+}
+
+static void
+xwl_present_flush(WindowPtr window)
+{
+    /* Only called when a Pixmap is copied instead of flipped,
+     * but in this case we wait on the next block_handler. */
+}
+
+static Bool
+xwl_present_check_flip(RRCrtcPtr crtc,
+                       WindowPtr present_window,
+                       PixmapPtr pixmap,
+                       Bool sync_flip)
+{
+    struct xwl_window *xwl_window = xwl_window_of_top(present_window);
+
+    if (!xwl_window)
+        return FALSE;
+
+    if (!xwl_window->present_crtc_fake)
+        return FALSE;
+    /*
+     * Make sure the client doesn't try to flip to another crtc
+     * than the one created for 'xwl_window'
+     */
+    if (xwl_window->present_crtc_fake != crtc)
+        return FALSE;
+
+    /*
+     * We currently only allow flips of windows, that have the same
+     * dimensions as their xwl_window parent window. For the case of
+     * different sizes subsurfaces are presumably the way forward.
+     */
+    if (!RegionEqual(&xwl_window->window->winSize, &present_window->winSize))
+        return FALSE;
+
+    return TRUE;
+}
+
+static void
+xwl_present_reset_present_window(struct xwl_window *xwl_window, WindowPtr 
present_window)
+{
+    /* Do not reset if it is the same present_window. But this also means, that
+     * we always switch to another child window, if it wants to present.
+     */
+    if (xwl_window->present_window == present_window)
+        return;
+
+    if (xwl_window->present_window)
+        xwl_present_cleanup(xwl_window->present_window);
+    xwl_window->present_window = present_window;
+    xorg_list_add(&xwl_window->present_link, &xwl_present_windows);
+}
+
+static Bool
+xwl_present_flip(WindowPtr present_window,
+                 RRCrtcPtr crtc,
+                 uint64_t event_id,
+                 uint64_t target_msc,
+                 PixmapPtr pixmap,
+                 Bool sync_flip,
+                 RegionPtr damage)
+{
+    struct xwl_window           *xwl_window = 
xwl_window_of_top(present_window);
+    BoxPtr                      present_box, damage_box;
+    Bool                        buffer_created;
+    struct wl_buffer            *buffer;
+    struct xwl_present_event    *event;
+
+    present_box = RegionExtents(&present_window->winSize);
+    damage_box = RegionExtents(damage);
+
+    /* Potentially reset the presenting window */
+    xwl_present_reset_present_window(xwl_window, present_window);
+    /* We can flip directly to the main surface (full screen window without 
clips) */
+    xwl_window->present_surface = xwl_window->surface;
+
+    event = malloc(sizeof *event);
+    if (!event) {
+        xwl_present_cleanup(present_window);
+        return FALSE;
+    }
+
+    buffer = xwl_glamor_pixmap_get_wl_buffer(pixmap,
+                                             present_box->x2 - present_box->x1,
+                                             present_box->y2 - present_box->y1,
+                                             &buffer_created);
+
+    event->event_id = event_id;
+    event->xwl_window = xwl_window;
+    event->buffer = buffer;
+    event->target_msc = xwl_window->present_msc;
+    event->pending = TRUE;
+    event->abort = FALSE;
+
+    xorg_list_add(&event->list, &xwl_window->present_release_queue);
+
+    if (buffer_created)
+        wl_buffer_add_listener(buffer, &release_listener, NULL);
+
+    wl_buffer_set_user_data(buffer, present_window);
+    wl_surface_attach(xwl_window->present_surface, buffer, 0, 0);
+
+    if (!xwl_window->present_frame_callback) {
+        xwl_window->present_frame_timer = 
TimerSet(xwl_window->present_frame_timer, 0, FRAME_TIMER_IVAL, 
&present_frame_timer_callback, xwl_window);
+
+        xwl_window->present_frame_callback = 
wl_surface_frame(xwl_window->present_surface);
+        wl_callback_add_listener(xwl_window->present_frame_callback, 
&present_frame_listener, xwl_window);
+    }
+
+    wl_surface_damage(xwl_window->present_surface, 0, 0,
+                      damage_box->x2 - damage_box->x1, damage_box->y2 - 
damage_box->y1);
+
+    wl_surface_commit(xwl_window->present_surface);
+
+    xwl_window->present_sync_callback = 
wl_display_sync(xwl_window->xwl_screen->display);
+    wl_callback_add_listener(xwl_window->present_sync_callback, 
&xwl_present_sync_listener, event);
+
+    wl_display_flush(xwl_window->xwl_screen->display);
+    return TRUE;
+}
+
+static void
+xwl_present_unflip(WindowPtr window, uint64_t event_id)
+{
+    xwl_present_cleanup(window);
+    present_wnmd_event_notify(window, event_id, 0, 0);
+}
+
+static present_wnmd_info_rec xwl_present_info = {
+    .version = PRESENT_SCREEN_INFO_VERSION,
+    .get_crtc = xwl_present_get_crtc,
+
+    .get_ust_msc = xwl_present_get_ust_msc,
+    .queue_vblank = xwl_present_queue_vblank,
+    .abort_vblank = xwl_present_abort_vblank,
+
+    .flush = xwl_present_flush,
+
+    .capabilities = PresentCapabilityAsync,
+    .check_flip = xwl_present_check_flip,
+    .flip = xwl_present_flip,
+    .unflip = xwl_present_unflip
+};
+
+Bool
+xwl_present_init(ScreenPtr screen)
+{
+    xorg_list_init(&xwl_present_windows);
+    return present_wnmd_screen_init(screen, &xwl_present_info);
+}
diff --git a/hw/xwayland/xwayland.c b/hw/xwayland/xwayland.c
index 2191956..386f11c 100644
--- a/hw/xwayland/xwayland.c
+++ b/hw/xwayland/xwayland.c
@@ -515,6 +515,13 @@ xwl_realize_window(WindowPtr window)
         wl_region_destroy(region);
     }
 
+    if (xwl_screen->present) {
+        xwl_window->present_crtc_fake = RRCrtcCreate(xwl_screen->screen, 
xwl_window);
+        xwl_window->present_msc = 1;
+        xorg_list_init(&xwl_window->present_event_list);
+        xorg_list_init(&xwl_window->present_release_queue);
+    }
+
     wl_display_flush(xwl_screen->display);
 
     send_surface_id_event(xwl_window);
@@ -580,6 +587,10 @@ xwl_unrealize_window(WindowPtr window)
 
     compUnredirectWindow(serverClient, window, CompositeRedirectManual);
 
+    if (xwl_screen->present)
+        /* Always cleanup Present (Present might have been active on child 
window) */
+        xwl_present_cleanup(window);
+
     screen->UnrealizeWindow = xwl_screen->UnrealizeWindow;
     ret = (*screen->UnrealizeWindow) (window);
     xwl_screen->UnrealizeWindow = screen->UnrealizeWindow;
@@ -598,6 +609,9 @@ xwl_unrealize_window(WindowPtr window)
     if (xwl_window->frame_callback)
         wl_callback_destroy(xwl_window->frame_callback);
 
+    if (xwl_window->present_crtc_fake)
+        RRCrtcDestroy(xwl_window->present_crtc_fake);
+
     free(xwl_window);
     dixSetPrivate(&window->devPrivates, &xwl_window_private_key, NULL);
 
@@ -683,6 +697,9 @@ xwl_screen_post_damage(struct xwl_screen *xwl_screen)
 
     xorg_list_for_each_entry_safe(xwl_window, next_xwl_window,
                                   &xwl_screen->damage_window_list, 
link_damage) {
+        /* Present on the main surface. So don't commit here as well. */
+        if (xwl_window->present_surface)
+            continue;
         /* If we're waiting on a frame callback from the server,
          * don't attach a new buffer. */
         if (xwl_window->frame_callback)
@@ -1022,6 +1039,9 @@ xwl_screen_init(ScreenPtr pScreen, int argc, char **argv)
     }
 #endif
 
+    if (xwl_screen->glamor && xwl_screen->rootless)
+        xwl_screen->present = xwl_present_init(pScreen);
+
     if (!xwl_screen->glamor) {
         xwl_screen->CreateScreenResources = pScreen->CreateScreenResources;
         pScreen->CreateScreenResources = xwl_shm_create_screen_resources;
diff --git a/hw/xwayland/xwayland.h b/hw/xwayland/xwayland.h
index 783ab59..c294a92 100644
--- a/hw/xwayland/xwayland.h
+++ b/hw/xwayland/xwayland.h
@@ -36,6 +36,7 @@
 
 #include <X11/X.h>
 
+#include <os.h>
 #include <fb.h>
 #include <input.h>
 #include <dix.h>
@@ -61,6 +62,7 @@ struct xwl_screen {
     int listen_fd_count;
     int rootless;
     int glamor;
+    int present;
 
     CreateScreenResourcesProcPtr CreateScreenResources;
     CloseScreenProcPtr CloseScreen;
@@ -106,6 +108,18 @@ struct xwl_screen {
     Atom allow_commits_prop;
 };
 
+struct xwl_present_event {
+    uint64_t event_id;
+    uint64_t target_msc;
+    Bool abort;
+    Bool pending;
+
+    struct xwl_window *xwl_window;
+    struct wl_buffer *buffer;
+
+    struct xorg_list list;
+};
+
 struct xwl_window {
     struct xwl_screen *xwl_screen;
     struct wl_surface *surface;
@@ -115,6 +129,20 @@ struct xwl_window {
     struct xorg_list link_damage;
     struct wl_callback *frame_callback;
     Bool allow_commits;
+
+    /* present */
+    RRCrtcPtr present_crtc_fake;
+    struct xorg_list present_link;
+    WindowPtr present_window;
+    struct wl_surface *present_surface;
+    uint64_t present_msc;
+
+    Bool present_frame_timer_firing;
+    OsTimerPtr present_frame_timer;
+    struct wl_callback *present_frame_callback;
+    struct wl_callback *present_sync_callback;
+    struct xorg_list present_event_list;
+    struct xorg_list present_release_queue;
 };
 
 #define MODIFIER_META 0x01
@@ -333,6 +361,9 @@ struct wl_buffer *xwl_glamor_pixmap_get_wl_buffer(PixmapPtr 
pixmap,
                                                   unsigned short height,
                                                   Bool *created);
 
+Bool xwl_present_init(ScreenPtr screen);
+void xwl_present_cleanup(WindowPtr window);
+
 void xwl_screen_release_tablet_manager(struct xwl_screen *xwl_screen);
 
 void xwl_output_get_xdg_output(struct xwl_output *xwl_output);
-- 
2.7.4

_______________________________________________
xorg-devel@lists.x.org: X.Org development
Archives: http://lists.x.org/archives/xorg-devel
Info: https://lists.x.org/mailman/listinfo/xorg-devel

Reply via email to