[PATCH 3/5] present: Add mode for pixmap flips per window

Roman Gilg subdiff at gmail.com
Tue Aug 29 15:24:30 UTC 2017


This patch adds a new mode to the internal flip mode API, that
enables per window flips on capable hardware platforms.

Pixmap flips are possible for every window with a parent window and
without redirection.

Besides the per window flips the new mode diverges from the old
screen flipping by holding back the PresentIdleNotify event for a
flipped pixmaps of a preceding frame until present_winmode_event_notify
is called once more.

The mode is initialized by calling present_winmode_screen_init instead
of present_screen_init for a particular ScreenPtr.

Signed-off-by: Roman Gilg <subdiff at gmail.com>
---
 present/Makefile.am       |   3 +-
 present/meson.build       |   1 +
 present/present.h         |  55 +++-
 present/present_priv.h    |  32 ++-
 present/present_screen.c  |  63 ++++-
 present/present_winmode.c | 640 ++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 786 insertions(+), 8 deletions(-)
 create mode 100644 present/present_winmode.c

diff --git a/present/Makefile.am b/present/Makefile.am
index 543d7ff..cbb2810 100644
--- a/present/Makefile.am
+++ b/present/Makefile.am
@@ -15,6 +15,7 @@ libpresent_la_SOURCES = \
 	present_request.c \
 	present_screen.c \
 	present_scrmode.c \
-	present_vblank.c
+	present_vblank.c \
+	present_winmode.c
 
 sdk_HEADERS = present.h presentext.h
diff --git a/present/meson.build b/present/meson.build
index 2cf1bf8..ed288ea 100644
--- a/present/meson.build
+++ b/present/meson.build
@@ -9,6 +9,7 @@ srcs_present = [
     'present_screen.c',
     'present_scrmode.c',
     'present_vblank.c',
+    'present_winmode.c',
 ]
 
 libxserver_present = static_library('libxserver_present',
diff --git a/present/present.h b/present/present.h
index aab2e16..7a0afbd 100644
--- a/present/present.h
+++ b/present/present.h
@@ -37,6 +37,8 @@ typedef RRCrtcPtr (*present_get_crtc_ptr) (WindowPtr window);
  */
 typedef int (*present_get_ust_msc_ptr) (RRCrtcPtr crtc, uint64_t *ust, uint64_t *msc);
 
+typedef int (*present_winmode_get_ust_msc_ptr) (WindowPtr window, uint64_t *ust, uint64_t *msc);
+
 /* Queue callback on 'crtc' for time 'msc'. Call present_event_notify with 'event_id'
  * at or after 'msc'. Return false if it didn't happen (which might occur if 'crtc'
  * is not currently generating vblanks).
@@ -44,6 +46,10 @@ typedef int (*present_get_ust_msc_ptr) (RRCrtcPtr crtc, uint64_t *ust, uint64_t
 typedef Bool (*present_queue_vblank_ptr) (RRCrtcPtr crtc,
                                           uint64_t event_id,
                                           uint64_t msc);
+typedef Bool (*present_winmode_queue_vblank_ptr) (WindowPtr window,
+                                                  RRCrtcPtr crtc,
+                                                  uint64_t event_id,
+                                                  uint64_t msc);
 
 /* Abort pending vblank. The extension is no longer interested in
  * 'event_id' which was to be notified at 'msc'. If possible, the
@@ -51,6 +57,8 @@ typedef Bool (*present_queue_vblank_ptr) (RRCrtcPtr crtc,
  */
 typedef void (*present_abort_vblank_ptr) (RRCrtcPtr crtc, uint64_t event_id, uint64_t msc);
 
+typedef void (*present_winmode_abort_vblank_ptr) (WindowPtr window, RRCrtcPtr crtc, uint64_t event_id, uint64_t msc);
+
 /* Flush pending drawing on 'window' to the hardware.
  */
 typedef void (*present_flush_ptr) (WindowPtr window);
@@ -76,6 +84,20 @@ typedef Bool (*present_flip_ptr) (RRCrtcPtr crtc,
                                   PixmapPtr pixmap,
                                   Bool sync_flip);
 
+/* Flip pixmap for window, return false if it didn't happen.
+ *
+ * Like present_flip_ptr, additionaly with:
+ *
+ * 'window' is to be used for any necessary synchronization.
+ *
+ */
+typedef Bool (*present_winmode_flip_ptr) (WindowPtr window,
+                                          RRCrtcPtr crtc,
+                                          uint64_t event_id,
+                                          uint64_t target_msc,
+                                          PixmapPtr pixmap,
+                                          Bool sync_flip);
+
 /* "unflip" back to the regular screen scanout buffer
  *
  * present_event_notify should be called with 'event_id' when the unflip occurs.
@@ -83,6 +105,13 @@ typedef Bool (*present_flip_ptr) (RRCrtcPtr crtc,
 typedef void (*present_unflip_ptr) (ScreenPtr screen,
                                     uint64_t event_id);
 
+/* "unflip" back to the regular window scanout buffer
+ *
+ * present_event_notify should be called with 'event_id' when the unflip occurs.
+ */
+typedef void (*present_winmode_unflip_ptr) (WindowPtr window,
+                                            uint64_t event_id);
+
 #define PRESENT_SCREEN_INFO_VERSION        0
 
 typedef struct present_screen_info {
@@ -97,9 +126,23 @@ typedef struct present_screen_info {
     present_check_flip_ptr              check_flip;
     present_flip_ptr                    flip;
     present_unflip_ptr                  unflip;
-
 } present_screen_info_rec, *present_screen_info_ptr;
 
+typedef struct present_winmode_screen_info {
+    uint32_t                            version;
+
+    present_get_crtc_ptr                get_crtc;
+    present_winmode_get_ust_msc_ptr     get_ust_msc;
+    present_winmode_queue_vblank_ptr    queue_vblank;
+    present_winmode_abort_vblank_ptr    abort_vblank;
+    present_flush_ptr                   flush;
+    uint32_t                            capabilities;
+    present_check_flip_ptr              check_flip;
+    present_winmode_flip_ptr            flip;
+    present_winmode_unflip_ptr          unflip;
+
+} present_winmode_screen_info_rec, *present_winmode_screen_info_ptr;
+
 /*
  * Called when 'event_id' occurs. 'ust' and 'msc' indicate when the
  * event actually happened
@@ -107,6 +150,13 @@ typedef struct present_screen_info {
 extern _X_EXPORT void
 present_event_notify(uint64_t event_id, uint64_t ust, uint64_t msc);
 
+/*
+ * Called when 'event_id' occurs for 'window'.
+ * 'ust' and 'msc' indicate when the event actually happened
+ */
+extern _X_EXPORT void
+present_winmode_event_notify(WindowPtr window, uint64_t event_id, uint64_t ust, uint64_t msc);
+
 /* 'crtc' has been turned off, so any pending events will never occur.
  */
 extern _X_EXPORT void
@@ -115,6 +165,9 @@ present_event_abandon(RRCrtcPtr crtc);
 extern _X_EXPORT Bool
 present_screen_init(ScreenPtr screen, present_screen_info_ptr info);
 
+extern _X_EXPORT Bool
+present_winmode_screen_init(ScreenPtr screen, present_winmode_screen_info_ptr info);
+
 typedef void (*present_complete_notify_proc)(WindowPtr window,
                                              CARD8 kind,
                                              CARD8 mode,
diff --git a/present/present_priv.h b/present/present_priv.h
index 80a5b4d..59d547f 100644
--- a/present/present_priv.h
+++ b/present/present_priv.h
@@ -153,7 +153,8 @@ struct present_screen_priv {
     present_fence_ptr           flip_idle_fence;
     Bool                        flip_sync;
 
-    present_screen_info_ptr     info;
+    present_screen_info_ptr         info;
+    present_winmode_screen_info_ptr winmode_info;
 
     /* Mode hooks */
     present_priv_query_capabilities_ptr query_capabilities;
@@ -203,11 +204,22 @@ typedef struct present_event {
 struct present_window_priv {
     WindowPtr              window;
     present_event_ptr      events;
-    RRCrtcPtr              crtc;        /* Last reported CRTC from get_ust_msc */
+    RRCrtcPtr              crtc;            /* Last reported CRTC from get_ust_msc */
     uint64_t               msc_offset;
-    uint64_t               msc;         /* Last reported MSC from the current crtc */
-    struct xorg_list       vblank_queue;
+    uint64_t               msc;             /* Last reported MSC from the current crtc */
+    struct xorg_list       vblank_queue;    /* Vblanks of 'window' waiting on execute or flip */
     struct xorg_list       notifies;
+
+    /* For window mode */
+    uint64_t               event_id;
+    struct xorg_list       exec_queue;
+    struct xorg_list       flip_queue;
+    struct xorg_list       idle_queue;
+
+    present_vblank_ptr     flip_pending;
+    present_vblank_ptr     flip_active;
+    uint64_t               unflip_event_id;
+    Bool                   restore_pixmap;
 };
 
 #define PresentCrtcNeverSet     ((RRCrtcPtr) 1)
@@ -341,6 +353,18 @@ void
 present_scrmode_init_scrmode(present_screen_priv_ptr screen_priv);
 
 /*
+ * present_winmode.c
+ */
+void
+present_winmode_set_abort_flip(WindowPtr window);
+
+void
+present_winmode_restore_window_pixmap(WindowPtr window);
+
+void
+present_winmode_init_winmode(present_screen_priv_ptr screen_priv);
+
+/*
  * present_event.c
  */
 
diff --git a/present/present_screen.c b/present/present_screen.c
index 8072488..30a7233 100644
--- a/present/present_screen.c
+++ b/present/present_screen.c
@@ -46,6 +46,10 @@ present_get_window_priv(WindowPtr window, Bool create)
     xorg_list_init(&window_priv->vblank_queue);
     xorg_list_init(&window_priv->notifies);
 
+    xorg_list_init(&window_priv->exec_queue);
+    xorg_list_init(&window_priv->flip_queue);
+    xorg_list_init(&window_priv->idle_queue);
+
     window_priv->window = window;
     window_priv->crtc = PresentCrtcNeverSet;
     dixSetPrivate(&window->devPrivates, &present_window_private_key, window_priv);
@@ -86,7 +90,7 @@ present_free_window_vblank(WindowPtr window)
 }
 
 static void
-present_clear_window_flip(WindowPtr window)
+present_scrmode_clear_window_flip(WindowPtr window)
 {
     ScreenPtr                   screen = window->drawable.pScreen;
     present_screen_priv_ptr     screen_priv = present_screen_priv(screen);
@@ -102,6 +106,35 @@ present_clear_window_flip(WindowPtr window)
     }
 }
 
+static void
+present_winmode_clear_window_flip(WindowPtr window)
+{
+    present_window_priv_ptr     window_priv = present_window_priv(window);
+    present_vblank_ptr          vblank, tmp;
+
+    if (window_priv->restore_pixmap)
+        present_winmode_restore_window_pixmap(window);
+
+    if (window_priv->flip_pending) {
+        present_winmode_set_abort_flip(window);
+        window_priv->flip_pending->window = NULL;
+    }
+
+    xorg_list_for_each_entry_safe(vblank, tmp, &window_priv->idle_queue, event_queue) {
+        present_pixmap_idle(vblank->pixmap, vblank->window, vblank->serial, vblank->idle_fence);
+        /* The pixmap will be destroyed by freeing the window resources. */
+        vblank->pixmap = NULL;
+        present_vblank_destroy(vblank);
+    }
+
+    vblank = window_priv->flip_active;
+    if (vblank) {
+        present_pixmap_idle(vblank->pixmap, vblank->window, vblank->serial, vblank->idle_fence);
+        present_vblank_destroy(vblank);
+    }
+    window_priv->flip_active = NULL;
+}
+
 /*
  * Hook the close window function to clean up our window private
  */
@@ -117,7 +150,12 @@ present_destroy_window(WindowPtr window)
         present_clear_window_notifies(window);
         present_free_events(window);
         present_free_window_vblank(window);
-        present_clear_window_flip(window);
+
+        if (screen_priv->winmode_info)
+            present_winmode_clear_window_flip(window);
+        else
+            present_scrmode_clear_window_flip(window);
+
         free(window_priv);
     }
     unwrap(screen_priv, screen, DestroyWindow);
@@ -224,6 +262,27 @@ present_screen_init(ScreenPtr screen, present_screen_info_ptr info)
 }
 
 /*
+ * Initialize a screen for use with present in window mode
+ */
+int
+present_winmode_screen_init(ScreenPtr screen, present_winmode_screen_info_ptr info)
+{
+    if (!present_screen_register_priv_keys())
+        return FALSE;
+
+    if (!present_screen_priv(screen)) {
+        present_screen_priv_ptr screen_priv = present_screen_priv_init(screen);
+        if (!screen_priv)
+            return FALSE;
+
+        screen_priv->winmode_info = info;
+        present_winmode_init_winmode(screen_priv);
+    }
+
+    return TRUE;
+}
+
+/*
  * Initialize the present extension
  */
 void
diff --git a/present/present_winmode.c b/present/present_winmode.c
new file mode 100644
index 0000000..d05f86e
--- /dev/null
+++ b/present/present_winmode.c
@@ -0,0 +1,640 @@
+/*
+ * Copyright © 2017 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.
+ */
+
+#ifdef HAVE_XORG_CONFIG_H
+#include <xorg-config.h>
+#endif
+
+#include "present_priv.h"
+
+static void
+present_winmode_execute(present_vblank_ptr vblank, uint64_t ust, uint64_t crtc_msc);
+
+static void
+present_winmode_create_event_id(present_window_priv_ptr window_priv, present_vblank_ptr vblank)
+{
+    vblank->event_id = ++window_priv->event_id;
+}
+
+static uint32_t
+present_winmode_query_capabilities(present_screen_priv_ptr screen_priv)
+{
+    return screen_priv->winmode_info->capabilities;
+}
+
+static RRCrtcPtr
+present_winmode_get_crtc(present_screen_priv_ptr screen_priv, WindowPtr window)
+{
+    return (*screen_priv->winmode_info->get_crtc)(window);
+}
+
+static int
+present_winmode_get_ust_msc(ScreenPtr screen, WindowPtr window, uint64_t *ust, uint64_t *msc)
+{
+    present_screen_priv_ptr screen_priv = present_screen_priv(screen);
+    return (*screen_priv->winmode_info->get_ust_msc)(window, ust, msc);
+}
+
+/*
+ * When the wait fence or previous flip is completed, it's time
+ * to re-try the request
+ */
+static void
+present_winmode_re_execute(present_vblank_ptr vblank)
+{
+    uint64_t ust = 0, crtc_msc = 0;
+
+    (void) present_winmode_get_ust_msc(vblank->screen, vblank->window, &ust, &crtc_msc);
+    present_winmode_execute(vblank, ust, crtc_msc);
+}
+
+static void
+present_winmode_flip_try_ready(WindowPtr window)
+{
+    present_window_priv_ptr window_priv = present_window_priv(window);
+    present_vblank_ptr      vblank;
+
+    xorg_list_for_each_entry(vblank, &window_priv->flip_queue, event_queue) {
+        if (vblank->queued) {
+            present_winmode_re_execute(vblank);
+            return;
+        }
+    }
+}
+
+static void
+present_winmode_free_idle_vblank(present_vblank_ptr vblank)
+{
+    present_pixmap_idle(vblank->pixmap, vblank->window, vblank->serial, vblank->idle_fence);
+    present_vblank_destroy(vblank);
+}
+
+/*
+ * Free any left over idle vblanks
+ */
+static void
+present_winmode_free_idle_vblanks(WindowPtr window)
+{
+    present_window_priv_ptr         window_priv = present_window_priv(window);
+    present_vblank_ptr              vblank, tmp;
+
+    xorg_list_for_each_entry_safe(vblank, tmp, &window_priv->idle_queue, event_queue) {
+        present_winmode_free_idle_vblank(vblank);
+    }
+
+    if (window_priv->flip_active) {
+        present_winmode_free_idle_vblank(window_priv->flip_active);
+        window_priv->flip_active = NULL;
+    }
+}
+
+void
+present_winmode_restore_window_pixmap(WindowPtr window)
+{
+    ScreenPtr                   screen = window->drawable.pScreen;
+    present_window_priv_ptr     window_priv = present_window_priv(window);
+    PixmapPtr                   flip_pixmap, restore_pixmap;
+
+    if (!window_priv->restore_pixmap)
+        return;
+    window_priv->restore_pixmap = FALSE;
+
+    flip_pixmap = window_priv->flip_pending ? window_priv->flip_pending->pixmap :
+                                              window_priv->flip_active->pixmap;
+
+    assert (flip_pixmap);
+
+    restore_pixmap = screen->GetWindowPixmap(window->parent);
+    if (!restore_pixmap)
+        return;
+
+    /* Update the window pixmap with the current flip pixmap contents */
+    present_copy_region(&restore_pixmap->drawable, flip_pixmap, NULL, 0, 0);
+
+    /* Switch back to using the original window pixmap now to avoid
+     * 2D applications drawing to the wrong pixmap.
+     */
+    present_set_tree_pixmap(window, flip_pixmap, restore_pixmap);
+}
+
+void
+present_winmode_set_abort_flip(WindowPtr window)
+{
+    present_window_priv_ptr window_priv = present_window_priv(window);
+
+    if (!window_priv->flip_pending->abort_flip) {
+        present_winmode_restore_window_pixmap(window);
+        window_priv->flip_pending->abort_flip = TRUE;
+    }
+}
+
+static void
+present_winmode_unflip(WindowPtr window)
+{
+    present_window_priv_ptr window_priv = present_window_priv(window);
+    present_screen_priv_ptr screen_priv = present_screen_priv(window->drawable.pScreen);
+
+    assert (!window_priv->unflip_event_id);
+    assert (!window_priv->flip_pending);
+
+    present_winmode_restore_window_pixmap(window);
+
+    window_priv->unflip_event_id = ++window_priv->event_id;
+    DebugPresent(("u %lld\n", window_priv->unflip_event_id));
+    (*screen_priv->winmode_info->unflip) (window, window_priv->unflip_event_id);
+}
+
+static void
+present_winmode_flip_notify(present_vblank_ptr vblank, uint64_t ust, uint64_t crtc_msc)
+{
+    WindowPtr                   window = vblank->window;
+    present_window_priv_ptr     window_priv = present_window_priv(window);
+
+    DebugPresent(("\tn %lld %p %8lld: %08lx -> %08lx\n",
+                  vblank->event_id, vblank, vblank->target_msc,
+                  vblank->pixmap ? vblank->pixmap->drawable.id : 0,
+                  vblank->window ? vblank->window->drawable.id : 0));
+
+    assert (vblank == window_priv->flip_pending);
+
+    xorg_list_del(&vblank->event_queue);
+
+    if (window_priv->flip_active)
+        /* Put the flip in the idle_queue and wait for further notice from DDX */
+        xorg_list_append(&window_priv->flip_active->event_queue, &window_priv->idle_queue);
+
+    window_priv->flip_active = vblank;
+    window_priv->flip_pending = NULL;
+
+    if (vblank->abort_flip)
+        present_winmode_unflip(window);
+
+    present_vblank_notify(vblank, PresentCompleteKindPixmap, PresentCompleteModeFlip, ust, crtc_msc);
+    present_winmode_flip_try_ready(window);
+}
+
+void
+present_winmode_event_notify(WindowPtr window, uint64_t event_id, uint64_t ust, uint64_t msc)
+{
+    present_window_priv_ptr     window_priv = present_window_priv(window);
+    present_vblank_ptr          vblank;
+
+    if (!window_priv)
+        return;
+    if (!event_id)
+        return;
+
+    DebugPresent(("\te %lld ust %lld msc %lld\n", event_id, ust, msc));
+    xorg_list_for_each_entry(vblank, &window_priv->exec_queue, event_queue) {
+        if (event_id == vblank->event_id) {
+            present_winmode_execute(vblank, ust, msc);
+            return;
+        }
+    }
+    xorg_list_for_each_entry(vblank, &window_priv->flip_queue, event_queue) {
+        if (vblank->event_id == event_id) {
+            if (vblank->queued) {
+                present_winmode_execute(vblank, ust, msc);
+            } else {
+                assert(vblank->window);
+                present_winmode_flip_notify(vblank, ust, msc);
+            }
+            return;
+        }
+    }
+
+    xorg_list_for_each_entry(vblank, &window_priv->idle_queue, event_queue) {
+        if (vblank->event_id == event_id) {
+            present_winmode_free_idle_vblank(vblank);
+            return;
+        }
+    }
+
+    if (event_id == window_priv->unflip_event_id) {
+        DebugPresent(("\tun %lld\n", event_id));
+        window_priv->unflip_event_id = 0;
+        present_winmode_free_idle_vblanks(window_priv->window);
+        present_winmode_flip_try_ready(window_priv->window);
+    }
+}
+
+static Bool
+present_winmode_check_flip(RRCrtcPtr    crtc,
+                           WindowPtr    window,
+                           PixmapPtr    pixmap,
+                           Bool         sync_flip,
+                           RegionPtr    valid,
+                           int16_t      x_off,
+                           int16_t      y_off)
+{
+    ScreenPtr               screen = window->drawable.pScreen;
+    present_screen_priv_ptr screen_priv = present_screen_priv(screen);
+
+    if (!screen_priv)
+        return FALSE;
+
+    if (!screen_priv->winmode_info)
+        return FALSE;
+
+    if (!crtc)
+        return FALSE;
+
+    /* Check to see if the driver supports flips at all */
+    if (!screen_priv->winmode_info->flip)
+        return FALSE;
+
+    /* Only flip windows with parent */
+    if (!window->parent)
+        return FALSE;
+
+    /* Don't flip redirected windows */
+    if (window->redirectDraw != RedirectDrawNone)
+        return FALSE;
+
+    /* Source pixmap must align with window exactly */
+    if (x_off || y_off)
+        return FALSE;
+
+    if (window->drawable.width != pixmap->drawable.width ||
+            window->drawable.height != pixmap->drawable.height)
+        return FALSE;
+
+    /* Ask the driver for permission */
+    if (screen_priv->winmode_info->check_flip) {
+        if (!(*screen_priv->winmode_info->check_flip) (crtc, window, pixmap, sync_flip)) {
+            DebugPresent(("\td %08lx -> %08lx\n", window->drawable.id, pixmap ? pixmap->drawable.id : 0));
+            return FALSE;
+        }
+    }
+
+    return TRUE;
+}
+
+/*
+ * 'window' is being reconfigured. Check to see if it is involved
+ * in flipping and clean up as necessary.
+ */
+static void
+present_winmode_check_flip_window (WindowPtr window)
+{
+    present_window_priv_ptr window_priv = present_window_priv(window);
+    present_vblank_ptr      flip_pending;
+    present_vblank_ptr      flip_active;
+    present_vblank_ptr      vblank;
+
+    /* If this window hasn't ever been used with Present, it can't be
+     * flipping
+     */
+    if (!window_priv)
+        return;
+
+    if (window_priv->unflip_event_id)
+        return;
+
+    flip_pending = window_priv->flip_pending;
+    flip_active = window_priv->flip_active;
+
+    if (flip_pending) {
+        if (!present_winmode_check_flip(flip_pending->crtc, flip_pending->window, flip_pending->pixmap,
+                                flip_pending->sync_flip, NULL, 0, 0))
+            present_winmode_set_abort_flip(window);
+    } else if (flip_active) {
+        if (!present_winmode_check_flip(flip_active->crtc, flip_active->window, flip_active->pixmap, flip_active->sync_flip, NULL, 0, 0))
+            present_winmode_unflip(window);
+    }
+
+    /* Now check any queued vblanks */
+    xorg_list_for_each_entry(vblank, &window_priv->vblank_queue, window_list) {
+        if (vblank->queued && vblank->flip && !present_winmode_check_flip(vblank->crtc, window, vblank->pixmap, vblank->sync_flip, NULL, 0, 0)) {
+            vblank->flip = FALSE;
+            if (vblank->sync_flip)
+                vblank->requeue = TRUE;
+        }
+    }
+}
+
+static Bool
+present_winmode_flip(WindowPtr window,
+                     RRCrtcPtr crtc,
+                     uint64_t event_id,
+                     uint64_t target_msc,
+                     PixmapPtr pixmap,
+                     Bool sync_flip)
+{
+    ScreenPtr                   screen = crtc->pScreen;
+    present_screen_priv_ptr     screen_priv = present_screen_priv(screen);
+
+    return (*screen_priv->winmode_info->flip) (window, crtc, event_id, target_msc, pixmap, sync_flip);
+}
+
+/*
+ * Once the required MSC has been reached, execute the pending request.
+ *
+ * For requests to actually present something, either blt contents to
+ * the window pixmap or queue a frame buffer swap.
+ *
+ * For requests to just get the current MSC/UST combo, skip that part and
+ * go straight to event delivery.
+ */
+
+static void
+present_winmode_execute(present_vblank_ptr vblank, uint64_t ust, uint64_t crtc_msc)
+{
+    WindowPtr               window = vblank->window;
+    present_window_priv_ptr window_priv = present_window_priv(window);
+
+    if (present_execute_wait(vblank, crtc_msc))
+        return;
+
+    if (vblank->flip && vblank->pixmap && vblank->window) {
+        if (window_priv->flip_pending || window_priv->unflip_event_id) {
+            DebugPresent(("\tr %lld %p (pending %p unflip %lld)\n",
+                          vblank->event_id, vblank,
+                          window_priv->flip_pending, window_priv->unflip_event_id));
+            xorg_list_del(&vblank->event_queue);
+            xorg_list_append(&vblank->event_queue, &window_priv->flip_queue);
+            vblank->flip_ready = TRUE;
+            return;
+        }
+    }
+
+    xorg_list_del(&vblank->event_queue);
+    xorg_list_del(&vblank->window_list);
+    vblank->queued = FALSE;
+
+    if (vblank->pixmap && vblank->window) {
+
+        if (vblank->flip) {
+            RegionPtr damage;
+
+            DebugPresent(("\tf %lld %p %8lld: %08lx -> %08lx\n",
+                          vblank->event_id, vblank, crtc_msc,
+                          vblank->pixmap->drawable.id, vblank->window->drawable.id));
+
+            /* Prepare to flip by placing it in the flip queue
+             */
+            xorg_list_add(&vblank->event_queue, &window_priv->flip_queue);
+
+            /* Try to flip
+             */
+            window_priv->flip_pending = vblank;
+
+            if (present_winmode_flip(vblank->window, vblank->crtc, vblank->event_id,
+                                     vblank->target_msc, vblank->pixmap, vblank->sync_flip)) {
+                /* Fix window pixmaps:
+                 *  1) Needs to restore pixmap from now on
+                 *  2) Set current flip window pixmap to the new pixmap
+                 */
+                window_priv->restore_pixmap = TRUE;
+                present_set_tree_pixmap(vblank->window, NULL, vblank->pixmap);
+
+                /* Report update region as damaged
+                 */
+                if (vblank->update) {
+                    damage = vblank->update;
+                    RegionIntersect(damage, damage, &window->clipList);
+                } else
+                    damage = &window->clipList;
+
+                DamageDamageRegion(&vblank->window->drawable, damage);
+
+                return;
+            }
+
+            xorg_list_del(&vblank->event_queue);
+            /* Oops, flip failed. Clear the flip_pending field
+              */
+            window_priv->flip_pending = NULL;
+            vblank->flip = FALSE;
+        }
+        DebugPresent(("\tc %p %8lld: %08lx -> %08lx\n", vblank, crtc_msc, vblank->pixmap->drawable.id, vblank->window->drawable.id));
+
+        if (window_priv->flip_pending)
+            present_winmode_set_abort_flip(window);
+        else if (!window_priv->unflip_event_id && window_priv->flip_active)
+            present_winmode_unflip(window);
+
+        present_execute_copy(vblank, crtc_msc);
+
+        if (vblank->queued) {
+            xorg_list_add(&vblank->event_queue, &window_priv->exec_queue);
+            xorg_list_append(&vblank->window_list, &window_priv->vblank_queue);
+
+            return;
+        }
+    }
+
+    present_execute_post(vblank, ust, crtc_msc);
+}
+
+static int
+present_winmode_queue_vblank(ScreenPtr screen,
+                             WindowPtr window,
+                             RRCrtcPtr crtc,
+                             uint64_t event_id,
+                             uint64_t msc)
+{
+    present_screen_priv_ptr screen_priv = present_screen_priv(screen);
+    return (*screen_priv->winmode_info->queue_vblank) (window, crtc, event_id, msc);
+}
+
+static uint64_t
+present_winmode_window_to_crtc_msc(WindowPtr window, RRCrtcPtr crtc, uint64_t window_msc, uint64_t new_msc)
+{
+    present_window_priv_ptr window_priv = present_get_window_priv(window, TRUE);
+
+    if (crtc != window_priv->crtc) {
+        uint64_t        old_ust, old_msc;
+
+        if (window_priv->crtc == PresentCrtcNeverSet) {
+            window_priv->msc_offset = 0;
+        } else {
+            /* The old CRTC may have been turned off, in which case
+             * we'll just use whatever previous MSC we'd seen from this CRTC
+             */
+
+            if (present_winmode_get_ust_msc(window->drawable.pScreen, window, &old_ust, &old_msc) != Success)
+                old_msc = window_priv->msc;
+
+            window_priv->msc_offset += new_msc - old_msc;
+        }
+        window_priv->crtc = crtc;
+    }
+
+    return window_msc + window_priv->msc_offset;
+}
+
+static int
+present_winmode_present_pixmap(present_window_priv_ptr window_priv,
+                               PixmapPtr pixmap,
+                               CARD32 serial,
+                               RegionPtr valid,
+                               RegionPtr update,
+                               int16_t x_off,
+                               int16_t y_off,
+                               RRCrtcPtr target_crtc,
+                               SyncFence *wait_fence,
+                               SyncFence *idle_fence,
+                               uint32_t options,
+                               uint64_t window_msc,
+                               uint64_t divisor,
+                               uint64_t remainder,
+                               present_notify_ptr notifies,
+                               int num_notifies)
+{
+    WindowPtr               window = window_priv->window;
+    ScreenPtr               screen = window->drawable.pScreen;
+    present_screen_priv_ptr screen_priv = present_screen_priv(screen);
+    uint64_t                ust = 0;
+    uint64_t                target_msc;
+    uint64_t                crtc_msc = 0;
+    present_vblank_ptr      vblank, tmp;
+
+    target_crtc = present_winmode_get_crtc(screen_priv, window);
+
+    if (present_winmode_get_ust_msc(screen, window, &ust, &crtc_msc) == Success)
+        window_priv->msc = crtc_msc;
+
+    target_msc = present_winmode_window_to_crtc_msc(window, target_crtc, window_msc, crtc_msc);
+
+    present_adjust_timings(options,
+                           &crtc_msc,
+                           &target_msc,
+                           divisor,
+                           remainder);
+
+    if (!update && pixmap) {
+        xorg_list_for_each_entry_safe(vblank, tmp, &window_priv->vblank_queue, window_list) {
+
+            if (!vblank->pixmap)
+                continue;
+
+            if (!vblank->queued)
+                continue;
+
+            if (vblank->target_msc != target_msc)
+                continue;
+
+            present_vblank_scrap(vblank);
+            if (vblank->flip_ready)
+                present_winmode_re_execute(vblank);
+        }
+    }
+
+    vblank = present_vblank_create(window_priv,
+                                   pixmap,
+                                   serial,
+                                   valid,
+                                   update,
+                                   x_off,
+                                   y_off,
+                                   target_crtc,
+                                   wait_fence,
+                                   idle_fence,
+                                   options,
+                                   &screen_priv->winmode_info->capabilities,
+                                   notifies,
+                                   num_notifies,
+                                   &target_msc,
+                                   crtc_msc);
+    if (!vblank)
+        return BadAlloc;
+
+    xorg_list_append(&vblank->event_queue, &window_priv->exec_queue);
+    vblank->queued = TRUE;
+    if (crtc_msc < target_msc) {
+        if (present_winmode_queue_vblank(screen, window, target_crtc, vblank->event_id, target_msc) == Success) {
+            return Success;
+        }
+        DebugPresent(("present_queue_vblank failed\n"));
+    }
+
+    present_winmode_execute(vblank, ust, crtc_msc);
+    return Success;
+}
+
+static void
+present_winmode_abort_vblank(ScreenPtr screen, WindowPtr window, RRCrtcPtr crtc, uint64_t event_id, uint64_t msc)
+{
+    present_screen_priv_ptr screen_priv = present_screen_priv(screen);
+    present_window_priv_ptr window_priv = present_window_priv(window);
+    present_vblank_ptr      vblank;
+
+    assert(crtc);
+
+    (*screen_priv->winmode_info->abort_vblank) (window, crtc, event_id, msc);
+
+    xorg_list_for_each_entry(vblank, &window_priv->exec_queue, event_queue) {
+        int64_t match = event_id - vblank->event_id;
+        if (match == 0) {
+            xorg_list_del(&vblank->event_queue);
+            vblank->queued = FALSE;
+            return;
+        }
+        if (match < 0)
+            break;
+    }
+    xorg_list_for_each_entry(vblank, &window_priv->flip_queue, event_queue) {
+        if (vblank->event_id == event_id) {
+            xorg_list_del(&vblank->event_queue);
+            vblank->queued = FALSE;
+            return;
+        }
+    }
+}
+
+static void
+present_winmode_flips_destroy(ScreenPtr screen)
+{
+    /* Cleanup done on window destruction */
+}
+
+static void
+present_winmode_flush(WindowPtr window)
+{
+    ScreenPtr               screen = window->drawable.pScreen;
+    present_screen_priv_ptr screen_priv = present_screen_priv(screen);
+
+    (*screen_priv->winmode_info->flush) (window);
+}
+
+void
+present_winmode_init_winmode(present_screen_priv_ptr screen_priv)
+{
+    screen_priv->query_capabilities =   &present_winmode_query_capabilities;
+    screen_priv->get_crtc           =   &present_winmode_get_crtc;
+
+    screen_priv->check_flip         =   &present_winmode_check_flip;
+    screen_priv->check_flip_window  =   &present_winmode_check_flip_window;
+
+    screen_priv->present_pixmap     =   &present_winmode_present_pixmap;
+    screen_priv->create_event_id    =   &present_winmode_create_event_id;
+    screen_priv->queue_vblank       =   &present_winmode_queue_vblank;
+    screen_priv->flush              =   &present_winmode_flush;
+    screen_priv->re_execute         =   &present_winmode_re_execute;
+
+    screen_priv->abort_vblank       =   &present_winmode_abort_vblank;
+    screen_priv->flips_destroy      =   &present_winmode_flips_destroy;
+}
-- 
2.7.4



More information about the xorg-devel mailing list