Revert "dri2: Avoid round-tripping on DRI2GetBuffers for the same set of buffers."
[mesa.git] / src / glx / x11 / dri2_glx.c
index 2bee67780bde8defb78725b362af5f7e03be45c6..9c8f1101b9ef11d3617d3ed39dcde9e67004c3fe 100644 (file)
@@ -73,6 +73,9 @@ struct __GLXDRIdrawablePrivateRec {
     __DRIbuffer buffers[5];
     int bufferCount;
     int width, height;
+    int have_back;
+    int have_front;
+    int have_fake_front;
 };
 
 static void dri2DestroyContext(__GLXDRIcontext *context,
@@ -166,6 +169,7 @@ static __GLXDRIdrawable *dri2CreateDrawable(__GLXscreenConfigs *psc,
     pdraw->base.xDrawable = xDrawable;
     pdraw->base.drawable = drawable;
     pdraw->base.psc = psc;
+    pdraw->bufferCount = 0;
 
     DRI2CreateDrawable(psc->dpy, xDrawable);
 
@@ -190,12 +194,22 @@ static void dri2CopySubBuffer(__GLXDRIdrawable *pdraw,
     XRectangle xrect;
     XserverRegion region;
 
+    /* Check we have the right attachments */
+    if (!(priv->have_front && priv->have_back))
+       return;
+
     xrect.x = x;
     xrect.y = priv->height - y - height;
     xrect.width = width;
     xrect.height = height;
 
+#ifdef __DRI2_FLUSH
+    if (pdraw->psc->f)
+       (*pdraw->psc->f->flush)(pdraw->driDrawable);
+#endif
+
     region = XFixesCreateRegion(pdraw->psc->dpy, &xrect, 1);
+    /* should get a fence ID back from here at some point */
     DRI2CopyRegion(pdraw->psc->dpy, pdraw->drawable, region,
                   DRI2BufferFrontLeft, DRI2BufferBackLeft);
     XFixesDestroyRegion(pdraw->psc->dpy, region);
@@ -208,6 +222,57 @@ static void dri2SwapBuffers(__GLXDRIdrawable *pdraw)
     dri2CopySubBuffer(pdraw, 0, 0, priv->width, priv->height);
 }
 
+static void dri2WaitX(__GLXDRIdrawable *pdraw)
+{
+    __GLXDRIdrawablePrivate *priv = (__GLXDRIdrawablePrivate *) pdraw;
+    XRectangle xrect;
+    XserverRegion region;
+
+    /* Check we have the right attachments */
+    if (!(priv->have_fake_front && priv->have_front))
+       return;
+
+    xrect.x = 0;
+    xrect.y = 0;
+    xrect.width = priv->width;
+    xrect.height = priv->height;
+
+#ifdef __DRI2_FLUSH
+    if (pdraw->psc->f)
+       (*pdraw->psc->f->flush)(pdraw->driDrawable);
+#endif
+
+    region = XFixesCreateRegion(pdraw->psc->dpy, &xrect, 1);
+    DRI2CopyRegion(pdraw->psc->dpy, pdraw->drawable, region,
+                  DRI2BufferFakeFrontLeft, DRI2BufferFrontLeft);
+    XFixesDestroyRegion(pdraw->psc->dpy, region);
+}
+
+static void dri2WaitGL(__GLXDRIdrawable *pdraw)
+{
+    __GLXDRIdrawablePrivate *priv = (__GLXDRIdrawablePrivate *) pdraw;
+    XRectangle xrect;
+    XserverRegion region;
+
+    if (!(priv->have_fake_front && priv->have_front))
+       return;
+
+    xrect.x = 0;
+    xrect.y = 0;
+    xrect.width = priv->width;
+    xrect.height = priv->height;
+
+#ifdef __DRI2_FLUSH
+    if (pdraw->psc->f)
+       (*pdraw->psc->f->flush)(pdraw->driDrawable);
+#endif
+
+    region = XFixesCreateRegion(pdraw->psc->dpy, &xrect, 1);
+    DRI2CopyRegion(pdraw->psc->dpy, pdraw->drawable, region,
+                  DRI2BufferFrontLeft, DRI2BufferFakeFrontLeft);
+    XFixesDestroyRegion(pdraw->psc->dpy, region);
+}
+
 static void dri2DestroyScreen(__GLXscreenConfigs *psc)
 {
     /* Free the direct rendering per screen data */
@@ -233,6 +298,10 @@ dri2GetBuffers(__DRIdrawable *driDrawable,
 
     pdraw->width = *width;
     pdraw->height = *height;
+    pdraw->bufferCount = *out_count;
+    pdraw->have_front = 0;
+    pdraw->have_fake_front = 0;
+    pdraw->have_back = 0;
 
     /* This assumes the DRI2 buffer attachment tokens matches the
      * __DRIbuffer tokens. */
@@ -242,6 +311,12 @@ dri2GetBuffers(__DRIdrawable *driDrawable,
        pdraw->buffers[i].pitch = buffers[i].pitch;
        pdraw->buffers[i].cpp = buffers[i].cpp;
        pdraw->buffers[i].flags = buffers[i].flags;
+       if (pdraw->buffers[i].attachment == __DRI_BUFFER_FRONT_LEFT)
+           pdraw->have_front = 1;
+       if (pdraw->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
+           pdraw->have_fake_front = 1;
+       if (pdraw->buffers[i].attachment == __DRI_BUFFER_BACK_LEFT)
+           pdraw->have_back = 1;
     }
 
     Xfree(buffers);
@@ -282,8 +357,10 @@ static __GLXDRIscreen *dri2CreateScreen(__GLXscreenConfigs *psc, int screen,
        return NULL;
 
     psc->driver = driOpenDriver(driverName);
-    if (psc->driver == NULL)
+    if (psc->driver == NULL) {
+       ErrorMessageF("driver pointer missing\n");
        goto handle_error;
+    }
 
     extensions = dlsym(psc->driver, __DRI_DRIVER_EXTENSIONS);
     if (extensions == NULL) {
@@ -309,11 +386,15 @@ static __GLXDRIscreen *dri2CreateScreen(__GLXscreenConfigs *psc, int screen,
        return NULL;
     }
 
-    if (drmGetMagic(psc->fd, &magic))
+    if (drmGetMagic(psc->fd, &magic)) {
+       ErrorMessageF("failed to get magic\n");
        return NULL;
+    }
 
-    if (!DRI2Authenticate(psc->dpy, RootWindow(psc->dpy, screen), magic))
+    if (!DRI2Authenticate(psc->dpy, RootWindow(psc->dpy, screen), magic)) {
+       ErrorMessageF("failed to authenticate magic %d\n", magic);
        return NULL;
+    }
 
     psc->__driScreen = 
        psc->dri2->createNewScreen(screen, psc->fd,
@@ -332,6 +413,8 @@ static __GLXDRIscreen *dri2CreateScreen(__GLXscreenConfigs *psc, int screen,
     psp->createContext = dri2CreateContext;
     psp->createDrawable = dri2CreateDrawable;
     psp->swapBuffers = dri2SwapBuffers;
+    psp->waitGL = dri2WaitGL;
+    psp->waitX = dri2WaitX;
 
     /* DRI2 suports SubBuffer through DRI2CopyRegion, so it's always
      * available.*/