Merge commit 'origin/gallium-0.1' into gallium-0.2
[mesa.git] / src / mesa / drivers / x11 / glxapi.c
index 8f7f5745da220cc26b3beb8be03ed483d242aad8..c2ccce6f520801c7f47e33c7d916a03830543e5b 100644 (file)
@@ -1,8 +1,8 @@
 /*
  * Mesa 3-D graphics library
- * Version:  6.3
+ * Version:  7.1
  * 
- * Copyright (C) 1999-2005  Brian Paul   All Rights Reserved.
+ * Copyright (C) 1999-2007  Brian Paul   All Rights Reserved.
  * 
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -34,8 +34,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include "glheader.h"
-#include "glapi.h"
+#include "main/glheader.h"
+#include "glapi/glapi.h"
 #include "glxapi.h"
 
 
@@ -80,31 +80,7 @@ get_dispatch(Display *dpy)
     * or Mesa's pseudo-GLX.
     */
    {
-      struct _glxapi_table *t = NULL;
-
-#ifdef GLX_BUILT_IN_XMESA
-      if (!getenv("LIBGL_FORCE_XMESA")) {
-         int ignore;
-         if (XQueryExtension( dpy, "GLX", &ignore, &ignore, &ignore )) {
-            /* the X server has the GLX extension */
-            t = _real_GetGLXDispatchTable();
-         }
-      }
-#endif
-
-      if (!t) {
-         /* Fallback to Mesa with Xlib driver */
-#ifdef GLX_BUILT_IN_XMESA
-         if (getenv("LIBGL_DEBUG")) {
-            fprintf(stderr,
-                    "libGL: server %s lacks the GLX extension.",
-                    dpy->display_name);
-            fprintf(stderr, " Using Mesa Xlib renderer.\n");
-         }
-#endif
-         t = _mesa_GetGLXDispatchTable();
-         assert(t);  /* this has to work */
-      }
+      struct _glxapi_table *t = _mesa_GetGLXDispatchTable();
 
       if (t) {
          struct display_dispatch *d;
@@ -131,6 +107,9 @@ get_dispatch(Display *dpy)
 }
 
 
+/* Don't use the GET_DISPATCH defined in glthread.h */
+#undef GET_DISPATCH
+
 #define GET_DISPATCH(DPY, TABLE)       \
    if (DPY == prevDisplay) {           \
       TABLE = prevTable;               \
@@ -145,13 +124,32 @@ get_dispatch(Display *dpy)
    
 
 
-/* Set by glXMakeCurrent() and glXMakeContextCurrent() only */
-#ifndef GLX_BUILT_IN_XMESA
+/**
+ * GLX API current context.
+ */
+#if defined(GLX_USE_TLS)
+PUBLIC __thread void * CurrentContext
+    __attribute__((tls_model("initial-exec")));
+#elif defined(THREADS)
+static _glthread_TSD ContextTSD;         /**< Per-thread context pointer */
+#else
 static GLXContext CurrentContext = 0;
-#define __glXGetCurrentContext() CurrentContext;
 #endif
 
 
+static void
+SetCurrentContext(GLXContext c)
+{
+#if defined(GLX_USE_TLS)
+   CurrentContext = c;
+#elif defined(THREADS)
+   _glthread_SetTSD(&ContextTSD, c);
+#else
+   CurrentContext = c;
+#endif
+}
+
+
 /*
  * GLX API entrypoints
  */
@@ -209,6 +207,8 @@ glXDestroyContext(Display *dpy, GLXContext ctx)
    GET_DISPATCH(dpy, t);
    if (!t)
       return;
+   if (glXGetCurrentContext() == ctx)
+      SetCurrentContext(NULL);
    (t->DestroyContext)(dpy, ctx);
 }
 
@@ -235,29 +235,25 @@ glXGetConfig(Display *dpy, XVisualInfo *visinfo, int attrib, int *value)
 }
 
 
-#ifdef GLX_BUILT_IN_XMESA
-/* Use real libGL's glXGetCurrentContext() function */
-#else
-/* stand-alone Mesa */
 GLXContext PUBLIC
 glXGetCurrentContext(void)
 {
+#if defined(GLX_USE_TLS)
+   return CurrentContext;
+#elif defined(THREADS)
+   return (GLXContext) _glthread_GetTSD(&ContextTSD);
+#else
    return CurrentContext;
-}
 #endif
+}
 
 
-#ifdef GLX_BUILT_IN_XMESA
-/* Use real libGL's glXGetCurrentContext() function */
-#else
-/* stand-alone Mesa */
 GLXDrawable PUBLIC
 glXGetCurrentDrawable(void)
 {
    __GLXcontext *gc = (__GLXcontext *) glXGetCurrentContext();
    return gc ? gc->currentDrawable : 0;
 }
-#endif
 
 
 Bool PUBLIC
@@ -281,11 +277,9 @@ glXMakeCurrent(Display *dpy, GLXDrawable drawable, GLXContext ctx)
       return False;
    }
    b = (*t->MakeCurrent)(dpy, drawable, ctx);
-#ifndef  GLX_BUILT_IN_XMESA
    if (b) {
-      CurrentContext = ctx;
+      SetCurrentContext(ctx);
    }
-#endif
    return b;
 }
 
@@ -397,7 +391,6 @@ glXQueryServerString(Display *dpy, int screen, int name)
 
 /*** GLX_VERSION_1_2 ***/
 
-#if !defined(GLX_BUILT_IN_XMESA)
 Display PUBLIC *
 glXGetCurrentDisplay(void)
 {
@@ -406,7 +399,6 @@ glXGetCurrentDisplay(void)
    if (NULL == gc) return NULL;
    return gc->currentDpy;
 }
-#endif
 
 
 
@@ -500,16 +492,12 @@ glXDestroyWindow(Display *dpy, GLXWindow window)
 }
 
 
-#ifdef GLX_BUILT_IN_XMESA
-/* Use the glXGetCurrentReadDrawable() function from libGL */
-#else
 GLXDrawable PUBLIC
 glXGetCurrentReadDrawable(void)
 {
    __GLXcontext *gc = (__GLXcontext *) glXGetCurrentContext();
    return gc ? gc->currentReadable : 0;
 }
-#endif
 
 
 int PUBLIC
@@ -564,11 +552,9 @@ glXMakeContextCurrent(Display *dpy, GLXDrawable draw, GLXDrawable read, GLXConte
    if (!t)
       return False;
    b = (t->MakeContextCurrent)(dpy, draw, read, ctx);
-#ifndef GLX_BUILT_IN_XMESA
    if (b) {
-      CurrentContext = ctx;
+      SetCurrentContext(ctx);
    }
-#endif
    return b;
 }
 
@@ -631,8 +617,8 @@ glXGetVideoSyncSGI(unsigned int *count)
    struct _glxapi_table *t;
    Display *dpy = glXGetCurrentDisplay();
    GET_DISPATCH(dpy, t);
-   if (!t)
-      return 0;
+   if (!t || !glXGetCurrentContext())
+      return GLX_BAD_CONTEXT;
    return (t->GetVideoSyncSGI)(count);
 }
 
@@ -642,8 +628,8 @@ glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
    struct _glxapi_table *t;
    Display *dpy = glXGetCurrentDisplay();
    GET_DISPATCH(dpy, t);
-   if (!t)
-      return 0;
+   if (!t || !glXGetCurrentContext())
+      return GLX_BAD_CONTEXT;
    return (t->WaitVideoSyncSGI)(divisor, remainder, count);
 }
 
@@ -657,20 +643,15 @@ glXMakeCurrentReadSGI(Display *dpy, GLXDrawable draw, GLXDrawable read, GLXConte
    struct _glxapi_table *t;
    GET_DISPATCH(dpy, t);
    if (!t)
-      return 0;
+      return False;
    return (t->MakeCurrentReadSGI)(dpy, draw, read, ctx);
 }
 
-#ifdef GLX_BUILT_IN_XMESA
-/* Use glXGetCurrentReadDrawableSGI() from libGL */
-#else
-/* stand-alone Mesa */
 GLXDrawable PUBLIC
 glXGetCurrentReadDrawableSGI(void)
 {
    return glXGetCurrentReadDrawable();
 }
-#endif
 
 
 #if defined(_VL_H)
@@ -710,27 +691,17 @@ glXFreeContextEXT(Display *dpy, GLXContext context)
    (t->FreeContextEXT)(dpy, context);
 }
 
-#ifdef GLX_BUILT_IN_XMESA
-/* Use real libGL's glXGetContextIDEXT() function */
-#else
-/* stand-alone Mesa */
 GLXContextID PUBLIC
 glXGetContextIDEXT(const GLXContext context)
 {
    return ((__GLXcontext *) context)->xid;
 }
-#endif
 
-#ifdef GLX_BUILT_IN_XMESA
-/* Use real libGL's glXGetCurrentDisplayEXT() function */
-#else
-/* stand-alone Mesa */
 Display PUBLIC *
 glXGetCurrentDisplayEXT(void)
 {
    return glXGetCurrentDisplay();
 }
-#endif
 
 GLXContext PUBLIC
 glXImportContextEXT(Display *dpy, GLXContextID contextID)
@@ -1133,6 +1104,27 @@ glXGetMemoryOffsetMESA(Display *dpy, int scrn, const void *pointer)
 }
 
 
+/*** GLX_EXT_texture_from_pixmap */
+
+void
+glXBindTexImageEXT(Display *dpy, GLXDrawable drawable, int buffer,
+                   const int *attrib_list)
+{
+   struct _glxapi_table *t;
+   GET_DISPATCH(dpy, t);
+   if (t)
+      t->BindTexImageEXT(dpy, drawable, buffer, attrib_list);
+}
+
+void
+glXReleaseTexImageEXT(Display *dpy, GLXDrawable drawable, int buffer)
+{
+   struct _glxapi_table *t;
+   GET_DISPATCH(dpy, t);
+   if (t)
+      t->ReleaseTexImageEXT(dpy, drawable, buffer);
+}
+
 
 /**********************************************************************/
 /* GLX API management functions                                       */
@@ -1176,6 +1168,9 @@ _glxapi_get_extensions(void)
 #endif
 #ifdef GLX_SGIX_pbuffer
       "GLX_SGIX_pbuffer",
+#endif
+#ifdef GLX_EXT_texture_from_pixmap
+      "GLX_EXT_texture_from_pixmap",
 #endif
       NULL
    };
@@ -1362,6 +1357,10 @@ static struct name_address_pair GLX_functions[] = {
    { "glXFreeMemoryMESA", (__GLXextFuncPtr) glXFreeMemoryMESA },
    { "glXGetMemoryOffsetMESA", (__GLXextFuncPtr) glXGetMemoryOffsetMESA },
 
+   /*** GLX_EXT_texture_from_pixmap ***/
+   { "glXBindTexImageEXT", (__GLXextFuncPtr) glXBindTexImageEXT },
+   { "glXReleaseTexImageEXT", (__GLXextFuncPtr) glXReleaseTexImageEXT },
+
    { NULL, NULL }   /* end of list */
 };