glx: Implement GLX_EXT_no_config_context
[mesa.git] / src / glx / glxcurrent.c
index 2e5111b8cf73738135cec72a1e7bf1ddaa6e18a1..7172009c34c0fc9451fc6439f04850ae5fb6121e 100644 (file)
  * Client-side GLX interface for current context management.
  */
 
-#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"
-#endif
-
 #include "glapi.h"
+#include "glx_error.h"
 
 /*
 ** We setup some dummy structures here so that the API can be used
@@ -73,11 +65,9 @@ struct glx_context dummyContext = {
  * Current context management and locking
  */
 
-#if defined( HAVE_PTHREAD )
-
 _X_HIDDEN pthread_mutex_t __glXmutex = PTHREAD_MUTEX_INITIALIZER;
 
-# if defined( GLX_USE_TLS )
+# if defined( USE_ELF_TLS )
 
 /**
  * Per-thread GLX context pointer.
@@ -142,18 +132,7 @@ __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
@@ -166,7 +145,7 @@ __glXSetCurrentContextNull(void)
 #endif
 }
 
-_X_EXPORT GLXContext
+_GLX_PUBLIC GLXContext
 glXGetCurrentContext(void)
 {
    struct glx_context *cx = __glXGetCurrentContext();
@@ -179,39 +158,107 @@ glXGetCurrentContext(void)
    }
 }
 
-_X_EXPORT GLXDrawable
+_GLX_PUBLIC GLXDrawable
 glXGetCurrentDrawable(void)
 {
    struct glx_context *gc = __glXGetCurrentContext();
    return gc->currentDrawable;
 }
 
+static Bool
+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);
+
+   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 ((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);
+
+
+   if (ret == 1)
+      *out_tag = reply.contextTag;
+
+   UnlockDisplay(dpy);
+   SyncHandle();
+
+   return ret;
+}
+
 static void
-__glXGenerateError(Display * dpy, 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 = __glXSetupForCommand(dpy);
-   error.minorCode = minorCode;
-   _XError(dpy, &error);
+   gc->currentDpy = dpy;
+   gc->currentDrawable = draw;
+   gc->currentReadable = read;
+}
+
+static Bool
+should_send(Display *dpy, struct glx_context *gc)
+{
+   /* always send for indirect contexts */
+   if (!gc->isDirect)
+      return 1;
+
+   /* don't send for broken servers. */
+   if (VendorRelease(dpy) < 12006000 || VendorRelease(dpy) >= 40000000)
+      return 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();
+   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
@@ -222,73 +269,112 @@ MakeContextCurrent(Display * dpy, GLXDrawable draw,
    }
 
    _glapi_check_multithread();
-
    __glXLock();
+
    if (oldGC == gc &&
-       gc->currentDrawable == draw && gc->currentReadable == read) {
-      __glXUnlock();
-      return True;
+       gc->currentDrawable == draw &&
+       gc->currentReadable == read) {
+      /* Same context and drawables: no op, just return */
+      ret = GL_TRUE;
    }
 
-   if (oldGC != &dummyContext) {
-      if (--oldGC->thread_refcount == 0) {
-        oldGC->vtable->unbind(oldGC, gc);
-        oldGC->currentDpy = 0;
+   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) {
-      /* 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) {
+      if (gc->vtable->bind(gc, gc, draw, read) != Success) {
          __glXSetCurrentContextNull();
-         __glXUnlock();
-         __glXGenerateError(dpy, None, GLXBadContext, X_GLXMakeContextCurrent);
-         return GL_FALSE;
+         goto out;
       }
+   }
 
-      if (gc->thread_refcount == 0) {
-         gc->currentDpy = dpy;
-         gc->currentDrawable = draw;
-         gc->currentReadable = read;
+   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;
+            }
+         }
       }
-      gc->thread_refcount++;
-      __glXSetCurrentContext(gc);
-   } else {
       __glXSetCurrentContextNull();
-   }
 
-   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);
+      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;
 
+out:
    __glXUnlock();
-
-   return GL_TRUE;
+   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)