etnaviv: split dump_shader
[mesa.git] / src / glx / glxcurrent.c
index 595586049471a15d0647b5a123d571aa815710f8..9c5d2af895639a380b536379352c1bb0a3a2fe5b 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"
-#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.
@@ -150,36 +132,20 @@ __glXGetCurrentContext(void)
    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();
@@ -192,163 +158,13 @@ glXGetCurrentContext(void)
    }
 }
 
-_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)
-{
-   Bool ret;
-
-
-   LockDisplay(dpy);
-
-   if (draw == read) {
-      xGLXMakeCurrentReq *req;
-
-      GetReq(GLXMakeCurrent, req);
-      req->reqType = opcode;
-      req->glxCode = X_GLXMakeCurrent;
-      req->drawable = draw;
-      req->context = gc_id;
-      req->oldContextTag = gc_tag;
-   }
-   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;
-
-         GetReq(GLXMakeContextCurrent, req);
-         req->reqType = opcode;
-         req->glxCode = X_GLXMakeContextCurrent;
-         req->drawable = draw;
-         req->readdrawable = read;
-         req->context = gc_id;
-         req->oldContextTag = gc_tag;
-      }
-      else {
-         xGLXVendorPrivateWithReplyReq *vpreq;
-         xGLXMakeCurrentReadSGIReq *req;
-
-         GetReqExtra(GLXVendorPrivateWithReply,
-                     sz_xGLXMakeCurrentReadSGIReq -
-                     sz_xGLXVendorPrivateWithReplyReq, vpreq);
-         req = (xGLXMakeCurrentReadSGIReq *) vpreq;
-         req->reqType = opcode;
-         req->glxCode = X_GLXVendorPrivateWithReply;
-         req->vendorCode = X_GLXvop_MakeCurrentReadSGI;
-         req->drawable = draw;
-         req->readable = read;
-         req->context = gc_id;
-         req->oldContextTag = gc_tag;
-      }
-   }
-
-   ret = _XReply(dpy, (xReply *) reply, 0, False);
-
-   UnlockDisplay(dpy);
-   SyncHandle();
-
-   return ret;
-}
-
-static void
-__glXGenerateError(Display * dpy, struct glx_context *gc, XID resource,
-                   BYTE errorCode, CARD16 minorCode)
-{
-   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);
-}
-
-#endif /* GLX_USE_APPLEGL */
-
-_X_HIDDEN int
-indirect_bind_context(struct glx_context *gc, struct glx_context *old,
-                     GLXDrawable draw, GLXDrawable read)
-{
-   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;
-}
-
-_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;
-
-   /* 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;
-}
-
 /**
  * Make a particular context current.
  *
@@ -360,7 +176,6 @@ MakeContextCurrent(Display * dpy, GLXDrawable draw,
 {
    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
@@ -370,70 +185,94 @@ MakeContextCurrent(Display * dpy, GLXDrawable draw,
       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);
+   __glXLock();
+   if (oldGC == gc &&
+       gc->currentDrawable == draw && gc->currentReadable == read) {
+      __glXUnlock();
+      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 && 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();
+         __glXSendError(dpy, GLXBadContext, None, X_GLXMakeContextCurrent,
+                        False);
+         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();
+
+   /* 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)