* Client-side GLX interface for current context management.
*/
-#ifdef PTHREADS
+#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif
#include "glxclient.h"
-#ifdef GLX_USE_APPLEGL
-#include <stdlib.h>
-#include "apple_glx.h"
-#include "apple_glx_context.h"
-#else
#include "glapi.h"
-#endif
/*
** We setup some dummy structures here so that the API can be used
*/
static GLubyte dummyBuffer[__GLX_BUFFER_LIMIT_SIZE];
-
+static struct glx_context_vtable dummyVtable;
/*
** Dummy context used by small commands when there is no current context.
** All the
&dummyBuffer[0],
&dummyBuffer[__GLX_BUFFER_LIMIT_SIZE],
sizeof(dummyBuffer),
+ &dummyVtable
};
/*
* Current context management and locking
*/
-#if defined( PTHREADS )
+#if defined( HAVE_PTHREAD )
_X_HIDDEN pthread_mutex_t __glXmutex = PTHREAD_MUTEX_INITIALIZER;
# endif /* defined( GLX_USE_TLS ) */
-#elif defined( THREADS )
-
-#error Unknown threading method specified.
-
#else
/* not thread safe */
__glXSetCurrentContextNull(void)
{
__glXSetCurrentContext(&dummyContext);
-#ifndef GLX_USE_APPLEGL
-#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
+#if defined(GLX_DIRECT_RENDERING)
_glapi_set_dispatch(NULL); /* no-op functions */
_glapi_set_context(NULL);
#endif
-#endif
}
_X_EXPORT GLXContext
}
static void
-__glXGenerateError(Display * dpy, struct glx_context *gc, XID resource,
+__glXGenerateError(Display * dpy, XID resource,
BYTE errorCode, CARD16 minorCode)
{
xError error;
error.resourceID = resource;
error.sequenceNumber = dpy->request;
error.type = X_Error;
- error.majorCode = gc->majorOpcode;
+ error.majorCode = __glXSetupForCommand(dpy);
error.minorCode = minorCode;
_XError(dpy, &error);
}
{
struct glx_context *gc = (struct glx_context *) gc_user;
struct glx_context *oldGC = __glXGetCurrentContext();
- int ret = Success;
/* 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
return GL_FALSE;
}
- if (gc == NULL && (draw != None || read != None)) {
- __glXGenerateError(dpy, gc, (draw != None) ? draw : read,
- BadMatch, X_GLXMakeContextCurrent);
- return False;
- }
- if (gc != NULL && (draw == None || read == None)) {
- __glXGenerateError(dpy, gc, None, BadMatch, X_GLXMakeContextCurrent);
- return False;
- }
-
_glapi_check_multithread();
- if (gc != NULL && gc->thread_id != 0 && gc->thread_id != _glthread_GetID()) {
- __glXGenerateError(dpy, gc, gc->xid,
- BadAccess, X_GLXMakeContextCurrent);
- return False;
+ __glXLock();
+ if (oldGC == gc &&
+ gc->currentDrawable == draw && gc->currentReadable == read) {
+ __glXUnlock();
+ return True;
}
- if (oldGC != &dummyContext && oldGC != gc) {
- oldGC->vtable->unbind(oldGC, gc);
- oldGC->currentDpy = 0;
- oldGC->currentDrawable = None;
- oldGC->currentReadable = None;
- oldGC->thread_id = 0;
- if (oldGC->xid == None)
- /* We are switching away from a context that was
- * previously destroyed, so we need to free the memory
- * for the old handle.
- */
- oldGC->vtable->destroy(oldGC);
+ if (oldGC != &dummyContext) {
+ if (--oldGC->thread_refcount == 0) {
+ oldGC->vtable->unbind(oldGC, gc);
+ oldGC->currentDpy = 0;
+ }
}
if (gc) {
- ret = gc->vtable->bind(gc, oldGC, draw, read);
- gc->currentDpy = dpy;
- gc->currentDrawable = draw;
- gc->currentReadable = read;
- gc->thread_id = _glthread_GetID();
+ /* Attempt to bind the context. We do this before mucking with
+ * gc and __glXSetCurrentContext to properly handle our state in
+ * case of an error.
+ *
+ * If an error occurs, set the Null context since we've already
+ * blown away our old context. The caller is responsible for
+ * figuring out how to handle setting a valid context.
+ */
+ if (gc->vtable->bind(gc, oldGC, draw, read) != Success) {
+ __glXSetCurrentContextNull();
+ __glXUnlock();
+ __glXGenerateError(dpy, None, GLXBadContext, X_GLXMakeContextCurrent);
+ return GL_FALSE;
+ }
+
+ if (gc->thread_refcount == 0) {
+ gc->currentDpy = dpy;
+ gc->currentDrawable = draw;
+ gc->currentReadable = read;
+ }
+ gc->thread_refcount++;
__glXSetCurrentContext(gc);
} else {
__glXSetCurrentContextNull();
}
- if (ret) {
- __glXGenerateError(dpy, gc, None, ret, X_GLXMakeContextCurrent);
- return GL_FALSE;
+ if (oldGC->thread_refcount == 0 && oldGC != &dummyContext && oldGC->xid == None) {
+ /* We are switching away from a context that was
+ * previously destroyed, so we need to free the memory
+ * for the old handle. */
+ oldGC->vtable->destroy(oldGC);
}
+ __glXUnlock();
+
return GL_TRUE;
}