All of lore.kernel.org
 help / color / mirror / Atom feed
From: Qiang Yu <Qiang.Yu-5C7GfCeVMHo@public.gmane.org>
To: xorg-devel-go0+a7rfsptAfugRpC6u6w@public.gmane.org
Cc: Qiang Yu <Qiang.Yu-5C7GfCeVMHo@public.gmane.org>,
	amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org
Subject: [PATCH xserver 3/6] modesetting: add DRI2 page flip support
Date: Wed, 17 Aug 2016 18:29:08 +0800	[thread overview]
Message-ID: <1471429751-17269-4-git-send-email-Qiang.Yu@amd.com> (raw)
In-Reply-To: <1471429751-17269-1-git-send-email-Qiang.Yu-5C7GfCeVMHo@public.gmane.org>

Signed-off-by: Qiang Yu <Qiang.Yu@amd.com>
---
 hw/xfree86/drivers/modesetting/dri2.c | 260 ++++++++++++++++++++++++++++++++--
 1 file changed, 251 insertions(+), 9 deletions(-)

diff --git a/hw/xfree86/drivers/modesetting/dri2.c b/hw/xfree86/drivers/modesetting/dri2.c
index 83cb3e0..11e0e6f 100644
--- a/hw/xfree86/drivers/modesetting/dri2.c
+++ b/hw/xfree86/drivers/modesetting/dri2.c
@@ -46,6 +46,7 @@
 
 enum ms_dri2_frame_event_type {
     MS_DRI2_QUEUE_SWAP,
+    MS_DRI2_QUEUE_FLIP,
     MS_DRI2_WAIT_MSC,
 };
 
@@ -399,6 +400,220 @@ ms_dri2_blit_swap(DrawablePtr drawable,
     ms_dri2_copy_region(drawable, &region, dst, src);
 }
 
+struct ms_dri2_vblank_event {
+    XID drawable_id;
+    ClientPtr client;
+    DRI2SwapEventPtr event_complete;
+    void *event_data;
+};
+
+static void
+ms_dri2_flip_free(struct ms_crtc_pageflip *flip)
+{
+    struct ms_flipdata *flipdata = flip->flipdata;
+
+    free(flip);
+    if (--flipdata->flip_count > 0)
+        return;
+    free(flipdata);
+}
+
+static void
+ms_dri2_flip_abort(void *data)
+{
+    struct ms_crtc_pageflip *flip = data;
+    struct ms_flipdata *flipdata = flip->flipdata;
+
+    if (flipdata->flip_count == 1)
+        free(flipdata->event);
+
+    ms_dri2_flip_free(flip);
+}
+
+static void
+ms_dri2_flip_handler(uint64_t msc, uint64_t ust, void *data)
+{
+    struct ms_crtc_pageflip *flip = data;
+    ScreenPtr screen = flip->flipdata->screen;
+    ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
+    modesettingPtr ms = modesettingPTR(scrn);
+    struct ms_flipdata *flipdata = flip->flipdata;
+    struct ms_dri2_vblank_event *event = flipdata->event;
+
+    if (flip->on_reference_crtc) {
+        flipdata->fe_msc = msc;
+        flipdata->fe_usec = ust;
+    }
+
+    if (flipdata->flip_count == 1) {
+        uint32_t frame = flipdata->fe_msc;
+        uint32_t tv_sec = flipdata->fe_usec / 1000000;
+        uint32_t tv_usec = flipdata->fe_usec % 1000000;
+        DrawablePtr drawable;
+        int status;
+
+        status = dixLookupDrawable(&drawable, event->drawable_id, serverClient,
+                                   M_ANY, DixWriteAccess);
+        if (status == Success)
+            DRI2SwapComplete(event->client, drawable, frame, tv_sec, tv_usec,
+                             DRI2_FLIP_COMPLETE, event->event_complete,
+                             event->event_data);
+        drmModeRmFB(ms->fd, flipdata->old_fb_id);
+    }
+
+    ms_dri2_flip_free(flip);
+}
+
+static Bool
+ms_dri2_schedule_flip(ms_dri2_frame_event_ptr info)
+{
+    DrawablePtr draw = info->drawable;
+    ScreenPtr screen = draw->pScreen;
+    ms_dri2_buffer_private_ptr back_priv = info->back->driverPrivate;
+    struct ms_dri2_vblank_event *event;
+    drmmode_crtc_private_ptr drmmode_crtc = info->crtc->driver_private;
+
+    event = calloc(1, sizeof(struct ms_dri2_vblank_event));
+    if (!event)
+        return FALSE;
+
+    event->drawable_id = draw->id;
+    event->client = info->client;
+    event->event_complete = info->event_complete;
+    event->event_data = info->event_data;
+
+    if (ms_do_pageflip(screen, back_priv->pixmap, event,
+                       drmmode_crtc->vblank_pipe, FALSE,
+                       ms_dri2_flip_handler,
+                       ms_dri2_flip_abort)) {
+        return TRUE;
+    }
+    return FALSE;
+}
+
+static Bool
+update_front(DrawablePtr draw, DRI2BufferPtr front)
+{
+    ScreenPtr screen = draw->pScreen;
+    PixmapPtr pixmap = get_drawable_pixmap(draw);
+    ms_dri2_buffer_private_ptr priv = front->driverPrivate;
+    CARD32 size;
+    CARD16 pitch;
+
+    front->name = glamor_name_from_pixmap(pixmap, &pitch, &size);
+    if (front->name < 0)
+        return FALSE;
+
+    (*screen->DestroyPixmap) (priv->pixmap);
+    front->pitch = pixmap->devKind;
+    front->cpp = pixmap->drawable.bitsPerPixel / 8;
+    priv->pixmap = pixmap;
+    pixmap->refcnt++;
+
+    return TRUE;
+}
+
+static Bool
+can_exchange(ScrnInfoPtr scrn, DrawablePtr draw,
+	     DRI2BufferPtr front, DRI2BufferPtr back)
+{
+    ms_dri2_buffer_private_ptr front_priv = front->driverPrivate;
+    ms_dri2_buffer_private_ptr back_priv = back->driverPrivate;
+    PixmapPtr front_pixmap;
+    PixmapPtr back_pixmap = back_priv->pixmap;
+    xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(scrn);
+    int num_crtcs_on = 0;
+    int i;
+
+    for (i = 0; i < config->num_crtc; i++) {
+        drmmode_crtc_private_ptr drmmode_crtc = config->crtc[i]->driver_private;
+
+        /* Don't do pageflipping if CRTCs are rotated. */
+#ifdef GLAMOR_HAS_GBM
+        if (drmmode_crtc->rotate_bo.gbm)
+            return FALSE;
+#endif
+
+        if (ms_crtc_on(config->crtc[i]))
+            num_crtcs_on++;
+    }
+
+    /* We can't do pageflipping if all the CRTCs are off. */
+    if (num_crtcs_on == 0)
+        return FALSE;
+
+    if (!update_front(draw, front))
+        return FALSE;
+
+    front_pixmap = front_priv->pixmap;
+
+    if (front_pixmap->drawable.width != back_pixmap->drawable.width)
+        return FALSE;
+
+    if (front_pixmap->drawable.height != back_pixmap->drawable.height)
+        return FALSE;
+
+    if (front_pixmap->drawable.bitsPerPixel !=
+        back_pixmap->drawable.bitsPerPixel)
+        return FALSE;
+
+    if (front_pixmap->devKind != back_pixmap->devKind)
+        return FALSE;
+
+    return TRUE;
+}
+
+static Bool
+can_flip(ScrnInfoPtr scrn, DrawablePtr draw,
+	 DRI2BufferPtr front, DRI2BufferPtr back)
+{
+    modesettingPtr ms = modesettingPTR(scrn);
+
+    return draw->type == DRAWABLE_WINDOW &&
+        ms->drmmode.pageflip &&
+        scrn->vtSema &&
+        DRI2CanFlip(draw) && can_exchange(scrn, draw, front, back);
+}
+
+static void
+ms_dri2_exchange_buffers(DrawablePtr draw, DRI2BufferPtr front,
+                         DRI2BufferPtr back)
+{
+    ms_dri2_buffer_private_ptr front_priv = front->driverPrivate;
+    ms_dri2_buffer_private_ptr back_priv = back->driverPrivate;
+    ScreenPtr screen = draw->pScreen;
+    ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
+    modesettingPtr ms = modesettingPTR(scrn);
+    msPixmapPrivPtr front_pix = msGetPixmapPriv(&ms->drmmode, front_priv->pixmap);
+    msPixmapPrivPtr back_pix = msGetPixmapPriv(&ms->drmmode, back_priv->pixmap);
+    msPixmapPrivRec tmp_pix;
+    RegionRec region;
+    int tmp;
+
+    /* Swap BO names so DRI works */
+    tmp = front->name;
+    front->name = back->name;
+    back->name = tmp;
+
+    /* Swap pixmap privates */
+    tmp_pix = *front_pix;
+    *front_pix = *back_pix;
+    *back_pix = tmp_pix;
+
+    glamor_egl_exchange_buffers(front_priv->pixmap, back_priv->pixmap);
+
+    /* Post damage on the front buffer so that listeners, such
+     * as DisplayLink know take a copy and shove it over the USB.
+     * also for sw cursors.
+     */
+    region.extents.x1 = region.extents.y1 = 0;
+    region.extents.x2 = front_priv->pixmap->drawable.width;
+    region.extents.y2 = front_priv->pixmap->drawable.width;
+    region.data = NULL;
+    DamageRegionAppend(&front_priv->pixmap->drawable, &region);
+    DamageRegionProcessPending(&front_priv->pixmap->drawable);
+}
+
 static void
 ms_dri2_frame_event_handler(uint64_t msc,
                             uint64_t usec,
@@ -417,6 +632,13 @@ ms_dri2_frame_event_handler(uint64_t msc,
     }
 
     switch (frame_info->type) {
+    case MS_DRI2_QUEUE_FLIP:
+        if (can_flip(scrn, drawable, frame_info->front, frame_info->back) &&
+            ms_dri2_schedule_flip(frame_info)) {
+            ms_dri2_exchange_buffers(drawable, frame_info->front, frame_info->back);
+            break;
+        }
+        /* else fall through to blit */
     case MS_DRI2_QUEUE_SWAP:
         ms_dri2_blit_swap(drawable, frame_info->front, frame_info->back);
         DRI2SwapComplete(frame_info->client, drawable, msc, tv_sec, tv_usec,
@@ -607,7 +829,7 @@ ms_dri2_schedule_swap(ClientPtr client, DrawablePtr draw,
     ScrnInfoPtr scrn = xf86ScreenToScrn(screen);
     modesettingPtr ms = modesettingPTR(scrn);
     drmVBlank vbl;
-    int ret;
+    int ret, flip = 0;
     xf86CrtcPtr crtc = ms_dri2_crtc_covering_drawable(draw);
     drmmode_crtc_private_ptr drmmode_crtc;
     ms_dri2_frame_event_ptr frame_info = NULL;
@@ -645,20 +867,36 @@ ms_dri2_schedule_swap(ClientPtr client, DrawablePtr draw,
 
     ret = ms_get_crtc_ust_msc(crtc, &current_ust, &current_msc);
 
+    /* Flips need to be submitted one frame before */
+    if (can_flip(scrn, draw, front, back)) {
+        frame_info->type = MS_DRI2_QUEUE_FLIP;
+        flip = 1;
+    }
+
+    /* Correct target_msc by 'flip' if frame_info->type == MS_DRI2_QUEUE_FLIP.
+     * Do it early, so handling of different timing constraints
+     * for divisor, remainder and msc vs. target_msc works.
+     */
+    if (*target_msc > 0)
+        *target_msc -= flip;
+
     /*
      * If divisor is zero, or current_msc is smaller than target_msc
      * we just need to make sure target_msc passes before initiating
      * the swap.
      */
     if (divisor == 0 || current_msc < *target_msc) {
-        /* We need to use DRM_VBLANK_NEXTONMISS to avoid unreliable
-         * timestamping later on.
-         */
         vbl.request.type = (DRM_VBLANK_ABSOLUTE |
-                            DRM_VBLANK_NEXTONMISS |
                             DRM_VBLANK_EVENT |
                             drmmode_crtc->vblank_pipe);
 
+        /* If non-pageflipping, but blitting/exchanging, we need to use
+         * DRM_VBLANK_NEXTONMISS to avoid unreliable timestamping later
+         * on.
+         */
+        if (flip == 0)
+            vbl.request.type |= DRM_VBLANK_NEXTONMISS;
+
         /* If target_msc already reached or passed, set it to
          * current_msc to ensure we return a reasonable value back
          * to the caller. This makes swap_interval logic more robust.
@@ -683,7 +921,8 @@ ms_dri2_schedule_swap(ClientPtr client, DrawablePtr draw,
             goto blit_fallback;
         }
 
-        *target_msc = ms_kernel_msc_to_crtc_msc(crtc, vbl.reply.sequence);
+        *target_msc = ms_kernel_msc_to_crtc_msc(crtc,
+                                                vbl.reply.sequence + flip);
         frame_info->frame = *target_msc;
 
         return TRUE;
@@ -695,9 +934,10 @@ ms_dri2_schedule_swap(ClientPtr client, DrawablePtr draw,
      * equation.
      */
     vbl.request.type = (DRM_VBLANK_ABSOLUTE |
-                        DRM_VBLANK_NEXTONMISS |
                         DRM_VBLANK_EVENT |
                         drmmode_crtc->vblank_pipe);
+    if (flip == 0)
+        vbl.request.type |= DRM_VBLANK_NEXTONMISS;
 
     request_msc = current_msc - (current_msc % divisor) +
         remainder;
@@ -721,7 +961,8 @@ ms_dri2_schedule_swap(ClientPtr client, DrawablePtr draw,
     if (!seq)
         goto blit_fallback;
 
-    vbl.request.sequence = ms_crtc_msc_to_kernel_msc(crtc, request_msc);
+    /* Account for 1 frame extra pageflip delay if flip > 0 */
+    vbl.request.sequence = ms_crtc_msc_to_kernel_msc(crtc, request_msc) - flip;
     vbl.request.signal = (unsigned long)seq;
 
     ret = drmWaitVBlank(ms->fd, &vbl);
@@ -732,7 +973,8 @@ ms_dri2_schedule_swap(ClientPtr client, DrawablePtr draw,
         goto blit_fallback;
     }
 
-    *target_msc = ms_kernel_msc_to_crtc_msc(crtc, vbl.reply.sequence);
+    /* Adjust returned value for 1 fame pageflip offset of flip > 0 */
+    *target_msc = ms_kernel_msc_to_crtc_msc(crtc, vbl.reply.sequence + flip);
     frame_info->frame = *target_msc;
 
     return TRUE;
-- 
2.7.4

_______________________________________________
amd-gfx mailing list
amd-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

  parent reply	other threads:[~2016-08-17 10:29 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-17 10:29 [PATCH xserver 0/6] modesetting: add DRI2 page flip support Qiang Yu
     [not found] ` <1471429751-17269-1-git-send-email-Qiang.Yu-5C7GfCeVMHo@public.gmane.org>
2016-08-17 10:29   ` [PATCH xserver 1/6] modesetting: make ms_do_pageflip generic for share with DRI2 Qiang Yu
     [not found]     ` <1471429751-17269-2-git-send-email-Qiang.Yu-5C7GfCeVMHo@public.gmane.org>
2016-08-18  7:48       ` Michel Dänzer
2016-08-17 10:29   ` [PATCH xserver 2/6] modesetting: move ms_do_pageflip to pageflip.c Qiang Yu
2016-08-17 10:29   ` Qiang Yu [this message]
     [not found]     ` <1471429751-17269-4-git-send-email-Qiang.Yu-5C7GfCeVMHo@public.gmane.org>
2016-08-18  8:12       ` [PATCH xserver 3/6] modesetting: add DRI2 page flip support Michel Dänzer
     [not found]         ` <7ae3a1a6-0e00-856b-885f-a8c39e7d5842-otUistvHUpPR7s880joybQ@public.gmane.org>
2016-08-19  7:06           ` Yu, Qiang
     [not found]             ` <DM5PR12MB120959145EBDFF1847B5FF1A8F160-2J9CzHegvk/Xl62ebUdyqgdYzm3356FpvxpqHgZTriW3zl9H0oFU5g@public.gmane.org>
2016-08-19  7:25               ` Michel Dänzer
     [not found]                 ` <1349a122-864e-2abd-9599-c7fa41e900c2-otUistvHUpPR7s880joybQ@public.gmane.org>
2016-08-19  7:48                   ` Yu, Qiang
     [not found]                     ` <MWHPR12MB12152F00C2A18FC6149473F18F160-Gy0DoCVfaSXDoeKT4XqzagdYzm3356FpvxpqHgZTriW3zl9H0oFU5g@public.gmane.org>
2016-08-19  9:06                       ` Michel Dänzer
2016-08-17 10:29   ` [PATCH xserver 4/6] modesetting: exclude DRI2 and Present page flip Qiang Yu
2016-08-17 10:29   ` [PATCH xserver 5/6] modesetting: merge common page flip code for present and dri2 Qiang Yu
2016-08-17 10:29   ` [PATCH xserver 6/6] modesetting: remove redundent pixmap destroy Qiang Yu
2016-08-17 15:54   ` [PATCH xserver 0/6] modesetting: add DRI2 page flip support Martin Peres
2016-08-18  8:18   ` Michel Dänzer
     [not found]     ` <1b121a8c-a201-396e-426c-838c4abed8f3-otUistvHUpPR7s880joybQ@public.gmane.org>
2016-08-19  1:52       ` Yu, Qiang
2016-08-18 11:03   ` Emil Velikov
     [not found]     ` <CACvgo52vhzcyAS384uQdYy_XrA0LmKRdsRnLF0UB0OhV49A7uw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-08-19  2:02       ` Yu, Qiang
     [not found]         ` <MWHPR12MB121593BB7CA6C60D65B21E6B8F160-Gy0DoCVfaSXDoeKT4XqzagdYzm3356FpvxpqHgZTriW3zl9H0oFU5g@public.gmane.org>
2016-08-19  2:09           ` Michel Dänzer
     [not found]             ` <09c2176a-6a7e-b28d-d7e1-6a4bb76dab58-otUistvHUpPR7s880joybQ@public.gmane.org>
2016-08-19  5:11               ` Keith Packard
2016-08-19  7:57               ` Emil Velikov
     [not found]                 ` <CACvgo51CgrEqe+pE-0-FOrhk3NhLSygLA54RU2U3aoFfHEvXGw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2016-08-19  9:09                   ` Michel Dänzer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1471429751-17269-4-git-send-email-Qiang.Yu@amd.com \
    --to=qiang.yu-5c7gfcevmho@public.gmane.org \
    --cc=amd-gfx-PD4FTy7X32lNgt0PjOBp9y5qC8QIuHrW@public.gmane.org \
    --cc=xorg-devel-go0+a7rfsptAfugRpC6u6w@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.