* 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"
-#else
#include "glapi.h"
-#include "indirect_init.h"
-#endif
+#include "glx_error.h"
/*
** 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
** gl and glx entry points are designed to operate as nop's when using
** the dummy context structure.
*/
-static struct glx_context dummyContext = {
+struct glx_context dummyContext = {
&dummyBuffer[0],
&dummyBuffer[0],
&dummyBuffer[0],
&dummyBuffer[__GLX_BUFFER_LIMIT_SIZE],
sizeof(dummyBuffer),
+ &dummyVtable
};
-
-#ifndef GLX_USE_APPLEGL
-/*
-** All indirect rendering contexts will share the same indirect dispatch table.
-*/
-static struct _glapi_table *IndirectAPI = NULL;
-#endif
-
/*
* Current context management and locking
*/
-#if defined( PTHREADS )
-
_X_HIDDEN pthread_mutex_t __glXmutex = PTHREAD_MUTEX_INITIALIZER;
-# if defined( GLX_USE_TLS )
+# if defined( USE_ELF_TLS )
/**
* Per-thread GLX context pointer.
return (v == NULL) ? &dummyContext : (struct glx_context *) v;
}
-# 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
+# endif /* defined( USE_ELF_TLS ) */
_X_HIDDEN void
__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
+_GLX_PUBLIC GLXContext
glXGetCurrentContext(void)
{
struct glx_context *cx = __glXGetCurrentContext();
}
}
-_X_EXPORT GLXDrawable
+_GLX_PUBLIC GLXDrawable
glXGetCurrentDrawable(void)
{
struct glx_context *gc = __glXGetCurrentContext();
return gc->currentDrawable;
}
-
-#ifndef GLX_USE_APPLEGL
-/************************************************************************/
-
-/**
- * Sends a GLX protocol message to the specified display to make the context
- * and the drawables current.
- *
- * \param dpy Display to send the message to.
- * \param opcode Major opcode value for the display.
- * \param gc_id Context tag for the context to be made current.
- * \param draw Drawable ID for the "draw" drawable.
- * \param read Drawable ID for the "read" drawable.
- * \param reply Space to store the X-server's reply.
- *
- * \warning
- * This function assumes that \c dpy is locked with \c LockDisplay on entry.
- */
static Bool
-SendMakeCurrentRequest(Display * dpy, CARD8 opcode,
- GLXContextID gc_id, GLXContextTag gc_tag,
- GLXDrawable draw, GLXDrawable read,
- xGLXMakeCurrentReply * reply)
+SendMakeCurrentRequest(Display * dpy, GLXContextID gc_id,
+ GLXContextTag gc_tag, GLXDrawable draw,
+ GLXDrawable read, GLXContextTag *out_tag)
{
+ xGLXMakeCurrentReply reply;
Bool ret;
-
+ int opcode = __glXSetupForCommand(dpy);
LockDisplay(dpy);
else {
struct glx_display *priv = __glXInitialize(dpy);
- /* If the server can support the GLX 1.3 version, we should
- * perfer that. Not only that, some servers support GLX 1.3 but
- * not the SGI extension.
- */
-
if ((priv->majorVersion > 1) || (priv->minorVersion >= 3)) {
xGLXMakeContextCurrentReq *req;
}
}
- ret = _XReply(dpy, (xReply *) reply, 0, False);
+ ret = _XReply(dpy, (xReply *) &reply, 0, False);
+
+
+ if (ret == 1)
+ *out_tag = reply.contextTag;
UnlockDisplay(dpy);
SyncHandle();
}
static void
-__glXGenerateError(Display * dpy, struct glx_context *gc, XID resource,
- BYTE errorCode, CARD16 minorCode)
+SetGC(struct glx_context *gc, Display *dpy, GLXDrawable draw, GLXDrawable read)
{
- xError error;
-
- error.errorCode = errorCode;
- error.resourceID = resource;
- error.sequenceNumber = dpy->request;
- error.type = X_Error;
- error.majorCode = gc->majorOpcode;
- error.minorCode = minorCode;
- _XError(dpy, &error);
+ gc->currentDpy = dpy;
+ gc->currentDrawable = draw;
+ gc->currentReadable = read;
}
-#endif /* GLX_USE_APPLEGL */
-
-_X_HIDDEN int
-indirect_bind_context(struct glx_context *gc, struct glx_context *old,
- GLXDrawable draw, GLXDrawable read)
+static Bool
+should_send(Display *dpy, struct glx_context *gc)
{
- xGLXMakeCurrentReply reply;
- GLXContextTag tag;
- __GLXattribute *state;
- Display *dpy = gc->psc->dpy;
- int opcode = __glXSetupForCommand(dpy);
-
- if (old && !old->isDirect && old->psc->dpy == dpy)
- tag = old->currentContextTag;
- else
- tag = None;
-
- SendMakeCurrentRequest(dpy, opcode, gc->xid, tag, draw, read, &reply);
-
- if (!IndirectAPI)
- IndirectAPI = __glXNewIndirectAPI();
- _glapi_set_dispatch(IndirectAPI);
-
- gc->currentContextTag = reply.contextTag;
- state = gc->client_state_private;
- if (state->array_state == NULL) {
- glGetString(GL_EXTENSIONS);
- glGetString(GL_VERSION);
- __glXInitVertexArrayState(gc);
- }
-
- return Success;
-}
+ /* always send for indirect contexts */
+ if (!gc->isDirect)
+ return 1;
-_X_HIDDEN void
-indirect_unbind_context(struct glx_context *gc, struct glx_context *new)
-{
- Display *dpy = gc->psc->dpy;
- int opcode = __glXSetupForCommand(dpy);
- xGLXMakeCurrentReply reply;
+ /* don't send for broken servers. */
+ if (VendorRelease(dpy) < 12006000 || VendorRelease(dpy) >= 40000000)
+ return 0;
- /* We are either switching to no context, away from a indirect
- * context to a direct context or from one dpy to another and have
- * to send a request to the dpy to unbind the previous context.
- */
- if (!new || new->isDirect || new->psc->dpy != dpy)
- SendMakeCurrentRequest(dpy, opcode, None,
- gc->currentContextTag, None, None, &reply);
- gc->currentContextTag = 0;
+ return 1;
}
-/**
- * Make a particular context current.
- *
- * \note This is in this file so that it can access dummyContext.
- */
static Bool
MakeContextCurrent(Display * dpy, GLXDrawable draw,
GLXDrawable read, GLXContext gc_user)
{
struct glx_context *gc = (struct glx_context *) gc_user;
struct glx_context *oldGC = __glXGetCurrentContext();
- int ret = Success;
+ Bool ret = GL_FALSE;
/* 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();
+ __glXLock();
- if (gc != NULL && gc->thread_id != 0 && gc->thread_id != _glthread_GetID()) {
- __glXGenerateError(dpy, gc, gc->xid,
- BadAccess, X_GLXMakeContextCurrent);
- return False;
+ if (oldGC == gc &&
+ gc->currentDrawable == draw &&
+ gc->currentReadable == read) {
+ /* Same context and drawables: no op, just return */
+ ret = GL_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);
+ else if (oldGC == gc) {
+ /* Same context and new drawables: update drawable bindings */
+ if (should_send(dpy, gc)) {
+ if (!SendMakeCurrentRequest(dpy, gc->xid, gc->currentContextTag,
+ draw, read, &gc->currentContextTag)) {
+ goto out;
+ }
+ }
+
+ if (gc->vtable->bind(gc, gc, draw, read) != Success) {
+ __glXSetCurrentContextNull();
+ goto out;
+ }
}
- if (gc) {
- ret = gc->vtable->bind(gc, oldGC, draw, read);
- gc->currentDpy = dpy;
- gc->currentDrawable = draw;
- gc->currentReadable = read;
- gc->thread_id = _glthread_GetID();
- __glXSetCurrentContext(gc);
- } else {
+ else {
+ /* Different contexts: release the old, bind the new */
+ GLXContextTag oldTag = oldGC->currentContextTag;
+
+ if (oldGC != &dummyContext) {
+
+ if (--oldGC->thread_refcount == 0) {
+ if (oldGC->xid != None &&
+ should_send(dpy, oldGC) &&
+ !SendMakeCurrentRequest(dpy, None, oldTag, None, None,
+ &oldGC->currentContextTag)) {
+ goto out;
+ }
+
+ oldGC->vtable->unbind(oldGC, gc);
+
+ if (oldGC->xid == None) {
+ /* destroyed context, free it */
+ oldGC->vtable->destroy(oldGC);
+ oldTag = 0;
+ } else {
+ SetGC(oldGC, NULL, None, None);
+ oldTag = oldGC->currentContextTag;
+ }
+ }
+ }
__glXSetCurrentContextNull();
- }
- if (ret) {
- __glXGenerateError(dpy, gc, None, ret, X_GLXMakeContextCurrent);
- return GL_FALSE;
+ if (gc) {
+ /*
+ * MESA_multithread_makecurrent makes this complicated. We need to
+ * send the request if the new context is
+ *
+ * a) indirect (may be current to another client), or
+ * b) (direct and) newly being made current, or
+ * c) (direct and) being bound to new drawables
+ */
+ Bool new_drawables = gc->currentReadable != read ||
+ gc->currentDrawable != draw;
+
+ if (should_send(dpy, gc)) {
+ if (!gc->isDirect || !gc->thread_refcount || new_drawables) {
+ if (!SendMakeCurrentRequest(dpy, gc->xid, oldTag, draw, read,
+ &gc->currentContextTag)) {
+ goto out;
+ }
+ }
+ }
+
+ if (gc->vtable->bind(gc, oldGC, draw, read) != Success) {
+ __glXSendError(dpy, GLXBadContext, None, X_GLXMakeContextCurrent,
+ False);
+ goto out;
+ }
+
+ if (gc->thread_refcount == 0) {
+ SetGC(gc, dpy, draw, read);
+ }
+ gc->thread_refcount++;
+ __glXSetCurrentContext(gc);
+ }
}
+ ret = GL_TRUE;
- return GL_TRUE;
+out:
+ __glXUnlock();
+ return ret;
}
-_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)