From f078ed0c5b76855ea6c0e7eb37fd0e1364f2298f Mon Sep 17 00:00:00 2001
From: Dave Airlie <airlied@redhat.com>
Date: Thu, 11 Jun 2009 10:50:32 +1000
Subject: [PATCH] GLX: attempt to fix glean makeCurrent test cases.

Two parts to this:

One we don't keep pointers to possibly freed memory anymore once we unbind the
drawables from the context. Brian I need to figure out what the comment
you made there, can we get a glean/piglit test so we can fix it properly?

If the new gc is the same as the oldGC, we call the unbind even though
we just bound it in that function. doh.
---
 src/glx/x11/glxcurrent.c               |    2 +-
 src/mesa/drivers/dri/common/dri_util.c |    6 +++++-
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/glx/x11/glxcurrent.c b/src/glx/x11/glxcurrent.c
index 01f4233..d44e0dd 100644
--- a/src/glx/x11/glxcurrent.c
+++ b/src/glx/x11/glxcurrent.c
@@ -457,7 +457,7 @@ MakeContextCurrent(Display * dpy, GLXDrawable draw,
                                     &dummy_reply);
    }
 #ifdef GLX_DIRECT_RENDERING
-   else if (oldGC->driContext) {
+   else if (oldGC->driContext && oldGC != gc) {
       oldGC->driContext->unbindContext(oldGC->driContext);
    }
 #endif
diff --git a/src/mesa/drivers/dri/common/dri_util.c b/src/mesa/drivers/dri/common/dri_util.c
index ae0e61e..2b1493a 100644
--- a/src/mesa/drivers/dri/common/dri_util.c
+++ b/src/mesa/drivers/dri/common/dri_util.c
@@ -122,6 +122,9 @@ static int driUnbindContext(__DRIcontext *pcp)
     pdp = pcp->driDrawablePriv;
     prp = pcp->driReadablePriv;
 
+    /* already unbound */
+    if (!pdp && !prp)
+      return GL_TRUE;
     /* Let driver unbind drawable from context */
     (*psp->DriverAPI.UnbindContext)(pcp);
 
@@ -146,9 +149,10 @@ static int driUnbindContext(__DRIcontext *pcp)
      * window we can determine the last context bound to the window and
      * use that context's lock. (BrianP, 2-Dec-2000)
      */
+    pcp->driDrawablePriv = pcp->driReadablePriv = NULL;
+
 #if 0
     /* Unbind the drawable */
-    pcp->driDrawablePriv = NULL;
     pdp->driContextPriv = &psp->dummyContextPriv;
 #endif
 
-- 
1.6.2.2

