glx: fix shared memory leak in X11
[mesa.git] / src / glx / glxcurrent.c
index c92a2fd3cc28b26d6d5f5d07ec8555d2c9fe1f0a..2b9c708c3ee6fc97fd11fa503bbdbbce54a21eb6 100644 (file)
  * Client-side GLX interface for current context management.
  */
 
-#ifdef PTHREADS
 #include <pthread.h>
-#endif
 
 #include "glxclient.h"
-#ifdef GLX_USE_APPLEGL
-#include <stdlib.h>
-
-#include "apple_glx.h"
-#include "apple_glx_context.h"
-#endif
-
 #include "glapi.h"
+#include "glx_error.h"
 
 /*
 ** We setup some dummy structures here so that the API can be used
@@ -73,8 +65,6 @@ struct glx_context dummyContext = {
  * Current context management and locking
  */
 
-#if defined( PTHREADS )
-
 _X_HIDDEN pthread_mutex_t __glXmutex = PTHREAD_MUTEX_INITIALIZER;
 
 # if defined( GLX_USE_TLS )
@@ -144,17 +134,6 @@ __glXGetCurrentContext(void)
 
 # endif /* defined( GLX_USE_TLS ) */
 
-#elif defined( THREADS )
-
-#error Unknown threading method specified.
-
-#else
-
-/* not thread safe */
-_X_HIDDEN struct glx_context *__glXcurrentContext = &dummyContext;
-
-#endif
-
 
 _X_HIDDEN void
 __glXSetCurrentContextNull(void)
@@ -166,7 +145,7 @@ __glXSetCurrentContextNull(void)
 #endif
 }
 
-_X_EXPORT GLXContext
+_GLX_PUBLIC GLXContext
 glXGetCurrentContext(void)
 {
    struct glx_context *cx = __glXGetCurrentContext();
@@ -179,28 +158,13 @@ glXGetCurrentContext(void)
    }
 }
 
-_X_EXPORT GLXDrawable
+_GLX_PUBLIC GLXDrawable
 glXGetCurrentDrawable(void)
 {
    struct glx_context *gc = __glXGetCurrentContext();
    return gc->currentDrawable;
 }
 
-static void
-__glXGenerateError(Display * dpy, XID resource,
-                   BYTE errorCode, CARD16 minorCode)
-{
-   xError error;
-
-   error.errorCode = errorCode;
-   error.resourceID = resource;
-   error.sequenceNumber = dpy->request;
-   error.type = X_Error;
-   error.majorCode = __glXSetupForCommand(dpy);
-   error.minorCode = minorCode;
-   _XError(dpy, &error);
-}
-
 /**
  * Make a particular context current.
  *
@@ -213,16 +177,6 @@ MakeContextCurrent(Display * dpy, GLXDrawable draw,
    struct glx_context *gc = (struct glx_context *) gc_user;
    struct glx_context *oldGC = __glXGetCurrentContext();
 
-   /* XXX: If this is left out, then libGL ends up not having this
-    * symbol, and drivers using it fail to load.  Compare the
-    * implementation of this symbol to _glapi_noop_enable_warnings(),
-    * though, which gets into the library despite no callers, the same
-    * prototypes, and the same compile flags to the files containing
-    * them.  Moving the definition to glapi_nop.c gets it into the
-    * library, though.
-    */
-   (void)_glthread_GetID();
-
    /* Make sure that the new context has a nonzero ID.  In the request,
     * a zero context ID is used only to mean that we bind to no current
     * context.
@@ -231,16 +185,6 @@ MakeContextCurrent(Display * dpy, GLXDrawable draw,
       return GL_FALSE;
    }
 
-   if (gc == NULL && (draw != None || read != None)) {
-      __glXGenerateError(dpy, (draw != None) ? draw : read,
-                         BadMatch, X_GLXMakeContextCurrent);
-      return False;
-   }
-   if (gc != NULL && (draw == None || read == None)) {
-      __glXGenerateError(dpy, None, BadMatch, X_GLXMakeContextCurrent);
-      return False;
-   }
-
    _glapi_check_multithread();
 
    __glXLock();
@@ -250,6 +194,13 @@ MakeContextCurrent(Display * dpy, GLXDrawable draw,
       return True;
    }
 
+   /* can't have only one be 0 */
+   if (!!draw != !!read) {
+      __glXUnlock();
+      __glXSendError(dpy, BadMatch, None, X_GLXMakeContextCurrent, True);
+      return False;
+   }
+
    if (oldGC != &dummyContext) {
       if (--oldGC->thread_refcount == 0) {
         oldGC->vtable->unbind(oldGC, gc);
@@ -269,7 +220,8 @@ MakeContextCurrent(Display * dpy, GLXDrawable draw,
       if (gc->vtable->bind(gc, oldGC, draw, read) != Success) {
          __glXSetCurrentContextNull();
          __glXUnlock();
-         __glXGenerateError(dpy, None, GLXBadContext, X_GLXMakeContextCurrent);
+         __glXSendError(dpy, GLXBadContext, None, X_GLXMakeContextCurrent,
+                        False);
          return GL_FALSE;
       }
 
@@ -293,22 +245,34 @@ MakeContextCurrent(Display * dpy, GLXDrawable draw,
 
    __glXUnlock();
 
+   /* The indirect vertex array state must to be initialised after we
+    * have setup the context, as it needs to query server attributes.
+    */
+   if (gc && !gc->isDirect) {
+      __GLXattribute *state = gc->client_state_private;
+      if (state && state->array_state == NULL) {
+         glGetString(GL_EXTENSIONS);
+         glGetString(GL_VERSION);
+         __glXInitVertexArrayState(gc);
+      }
+   }
+
    return GL_TRUE;
 }
 
 
-_X_EXPORT Bool
+_GLX_PUBLIC Bool
 glXMakeCurrent(Display * dpy, GLXDrawable draw, GLXContext gc)
 {
    return MakeContextCurrent(dpy, draw, draw, gc);
 }
 
-_X_EXPORT
+_GLX_PUBLIC
 GLX_ALIAS(Bool, glXMakeCurrentReadSGI,
           (Display * dpy, GLXDrawable d, GLXDrawable r, GLXContext ctx),
           (dpy, d, r, ctx), MakeContextCurrent)
 
-_X_EXPORT
+_GLX_PUBLIC
 GLX_ALIAS(Bool, glXMakeContextCurrent,
           (Display * dpy, GLXDrawable d, GLXDrawable r,
            GLXContext ctx), (dpy, d, r, ctx), MakeContextCurrent)