Merge branch 'mesa_7_6_branch' into mesa_7_7_branch
[mesa.git] / src / egl / drivers / glx / egl_glx.c
index e2b3b4f67a31c59c2c2dd95ce2f58c393110a6ed..96292b0e9e867f8479980a32c01777d495ee9f8c 100644 (file)
  * Authors: Alan Hourihane <alanh@tungstengraphics.com>
  */
 
-/*
- * TODO: 
- *
- * test eglBind/ReleaseTexImage
- */
-
-
-#include <assert.h>
-#include <stdio.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include "dlfcn.h"
+#include <stdlib.h>
+#include <string.h>
 #include <X11/Xlib.h>
-#include <GL/gl.h>
-#include "glxclient.h"
-
-#define _EGL_PLATFORM_X
+#include <GL/glx.h>
 
+#include "eglconfigutil.h"
 #include "eglconfig.h"
 #include "eglcontext.h"
 #include "egldisplay.h"
 #include "egldriver.h"
 #include "eglglobals.h"
-#include "eglhash.h"
 #include "egllog.h"
 #include "eglsurface.h"
 
-#include <GL/gl.h>
-
 #define CALLOC_STRUCT(T)   (struct T *) calloc(1, sizeof(struct T))
+#define ARRAY_SIZE(a)      (sizeof(a) / sizeof(a[0]))
 
-static const EGLint all_apis = (EGL_OPENGL_ES_BIT
-                                | EGL_OPENGL_ES2_BIT
-                                | EGL_OPENVG_BIT
-                                /* | EGL_OPENGL_BIT */); /* can't do */
+#ifndef GLX_VERSION_1_4
+#error "GL/glx.h must be equal to or greater than GLX 1.4"
+#endif
+
+/*
+ * report OpenGL ES bits because apps usually forget to specify
+ * EGL_RENDERABLE_TYPE when choosing configs
+ */
+#define GLX_EGL_APIS (EGL_OPENGL_BIT | EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT)
 
-struct visual_attribs
-{
-   /* X visual attribs */
-   int id;
-   int klass;
-   int depth;
-   int redMask, greenMask, blueMask;
-   int colormapSize;
-   int bitsPerRGB;
-
-   /* GL visual attribs */
-   int supportsGL;
-   int transparentType;
-   int transparentRedValue;
-   int transparentGreenValue;
-   int transparentBlueValue;
-   int transparentAlphaValue;
-   int transparentIndexValue;
-   int bufferSize;
-   int level;
-   int render_type;
-   int doubleBuffer;
-   int stereo;
-   int auxBuffers;
-   int redSize, greenSize, blueSize, alphaSize;
-   int depthSize;
-   int stencilSize;
-   int accumRedSize, accumGreenSize, accumBlueSize, accumAlphaSize;
-   int numSamples, numMultisample;
-   int visualCaveat;
-};
 
 /** subclass of _EGLDriver */
 struct GLX_egl_driver
 {
    _EGLDriver Base;   /**< base class */
+};
+
 
+/** driver data of _EGLDisplay */
+struct GLX_egl_display
+{
+   Display *dpy;
    XVisualInfo *visuals;
    GLXFBConfig *fbconfigs;
 
    int glx_maj, glx_min;
+
+   const char *extensions;
+   EGLBoolean have_1_3;
+   EGLBoolean have_make_current_read;
+   EGLBoolean have_fbconfig;
+   EGLBoolean have_pbuffer;
+
+   /* GLX_SGIX_pbuffer */
+   PFNGLXCREATEGLXPBUFFERSGIXPROC glXCreateGLXPbufferSGIX;
+   PFNGLXDESTROYGLXPBUFFERSGIXPROC glXDestroyGLXPbufferSGIX;
+
+   /* workaround quirks of different GLX implementations */
+   EGLBoolean single_buffered_quirk;
+   EGLBoolean glx_window_quirk;
+
 };
 
 
@@ -128,7 +108,8 @@ struct GLX_egl_surface
 {
    _EGLSurface Base;   /**< base class */
 
-   GLXDrawable drawable;
+   Drawable drawable;
+   GLXDrawable glx_drawable;
 };
 
 
@@ -136,6 +117,8 @@ struct GLX_egl_surface
 struct GLX_egl_config
 {
    _EGLConfig Base;   /**< base class */
+   EGLBoolean double_buffered;
+   int index;
 };
 
 /** cast wrapper */
@@ -145,6 +128,12 @@ GLX_egl_driver(_EGLDriver *drv)
    return (struct GLX_egl_driver *) drv;
 }
 
+static struct GLX_egl_display *
+GLX_egl_display(_EGLDisplay *dpy)
+{
+   return (struct GLX_egl_display *) dpy->DriverData;
+}
+
 static struct GLX_egl_context *
 GLX_egl_context(_EGLContext *ctx)
 {
@@ -157,360 +146,449 @@ GLX_egl_surface(_EGLSurface *surf)
    return (struct GLX_egl_surface *) surf;
 }
 
-static GLboolean
-get_visual_attribs(Display *dpy, XVisualInfo *vInfo,
-                   struct visual_attribs *attribs)
+static int
+GLX_egl_config_index(_EGLConfig *conf)
 {
-   const char *ext = glXQueryExtensionsString(dpy, vInfo->screen);
-   int rgba;
+   return ((struct GLX_egl_config *) conf)->index;
+}
 
-   memset(attribs, 0, sizeof(struct visual_attribs));
 
-   attribs->id = vInfo->visualid;
-#if defined(__cplusplus) || defined(c_plusplus)
-   attribs->klass = vInfo->c_class;
-#else
-   attribs->klass = vInfo->class;
-#endif
-   attribs->depth = vInfo->depth;
-   attribs->redMask = vInfo->red_mask;
-   attribs->greenMask = vInfo->green_mask;
-   attribs->blueMask = vInfo->blue_mask;
-   attribs->colormapSize = vInfo->colormap_size;
-   attribs->bitsPerRGB = vInfo->bits_per_rgb;
-
-   if (glXGetConfig(dpy, vInfo, GLX_USE_GL, &attribs->supportsGL) != 0 ||
-       !attribs->supportsGL)
-      return GL_FALSE;
-   glXGetConfig(dpy, vInfo, GLX_BUFFER_SIZE, &attribs->bufferSize);
-   glXGetConfig(dpy, vInfo, GLX_LEVEL, &attribs->level);
-   glXGetConfig(dpy, vInfo, GLX_RGBA, &rgba);
-   if (!rgba)
-      return GL_FALSE;
-   attribs->render_type = GLX_RGBA_BIT;
-   
-   glXGetConfig(dpy, vInfo, GLX_DOUBLEBUFFER, &attribs->doubleBuffer);
-   if (!attribs->doubleBuffer)
-      return GL_FALSE;
-
-   glXGetConfig(dpy, vInfo, GLX_STEREO, &attribs->stereo);
-   glXGetConfig(dpy, vInfo, GLX_AUX_BUFFERS, &attribs->auxBuffers);
-   glXGetConfig(dpy, vInfo, GLX_RED_SIZE, &attribs->redSize);
-   glXGetConfig(dpy, vInfo, GLX_GREEN_SIZE, &attribs->greenSize);
-   glXGetConfig(dpy, vInfo, GLX_BLUE_SIZE, &attribs->blueSize);
-   glXGetConfig(dpy, vInfo, GLX_ALPHA_SIZE, &attribs->alphaSize);
-   glXGetConfig(dpy, vInfo, GLX_DEPTH_SIZE, &attribs->depthSize);
-   glXGetConfig(dpy, vInfo, GLX_STENCIL_SIZE, &attribs->stencilSize);
-   glXGetConfig(dpy, vInfo, GLX_ACCUM_RED_SIZE, &attribs->accumRedSize);
-   glXGetConfig(dpy, vInfo, GLX_ACCUM_GREEN_SIZE, &attribs->accumGreenSize);
-   glXGetConfig(dpy, vInfo, GLX_ACCUM_BLUE_SIZE, &attribs->accumBlueSize);
-   glXGetConfig(dpy, vInfo, GLX_ACCUM_ALPHA_SIZE, &attribs->accumAlphaSize);
-
-   /* get transparent pixel stuff */
-   glXGetConfig(dpy, vInfo,GLX_TRANSPARENT_TYPE, &attribs->transparentType);
-   if (attribs->transparentType == GLX_TRANSPARENT_RGB) {
-     glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_RED_VALUE, &attribs->transparentRedValue);
-     glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_GREEN_VALUE, &attribs->transparentGreenValue);
-     glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_BLUE_VALUE, &attribs->transparentBlueValue);
-     glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_ALPHA_VALUE, &attribs->transparentAlphaValue);
-   }
-   else if (attribs->transparentType == GLX_TRANSPARENT_INDEX) {
-     glXGetConfig(dpy, vInfo, GLX_TRANSPARENT_INDEX_VALUE, &attribs->transparentIndexValue);
-   }
-
-   /* multisample attribs */
-#ifdef GLX_ARB_multisample
-   if (ext && strstr(ext, "GLX_ARB_multisample")) {
-      glXGetConfig(dpy, vInfo, GLX_SAMPLE_BUFFERS_ARB, &attribs->numMultisample);
-      glXGetConfig(dpy, vInfo, GLX_SAMPLES_ARB, &attribs->numSamples);
-   }
-#endif
-   else {
-      attribs->numSamples = 0;
-      attribs->numMultisample = 0;
-   }
+#define MAP_ATTRIB(attr, memb) \
+   { attr, offsetof(__GLcontextModes, memb) }
+
+
+static const struct {
+   int attr;
+   int offset;
+} fbconfig_attributes[] = {
+   /* table 3.1 of GLX 1.4 */
+   MAP_ATTRIB(GLX_FBCONFIG_ID,                  fbconfigID),
+   MAP_ATTRIB(GLX_BUFFER_SIZE,                  rgbBits),
+   MAP_ATTRIB(GLX_LEVEL,                        level),
+   MAP_ATTRIB(GLX_DOUBLEBUFFER,                 doubleBufferMode),
+   MAP_ATTRIB(GLX_STEREO,                       stereoMode),
+   MAP_ATTRIB(GLX_AUX_BUFFERS,                  numAuxBuffers),
+   MAP_ATTRIB(GLX_RED_SIZE,                     redBits),
+   MAP_ATTRIB(GLX_GREEN_SIZE,                   greenBits),
+   MAP_ATTRIB(GLX_BLUE_SIZE,                    blueBits),
+   MAP_ATTRIB(GLX_ALPHA_SIZE,                   alphaBits),
+   MAP_ATTRIB(GLX_DEPTH_SIZE,                   depthBits),
+   MAP_ATTRIB(GLX_STENCIL_SIZE,                 stencilBits),
+   MAP_ATTRIB(GLX_ACCUM_RED_SIZE,               accumRedBits),
+   MAP_ATTRIB(GLX_ACCUM_GREEN_SIZE,             accumGreenBits),
+   MAP_ATTRIB(GLX_ACCUM_BLUE_SIZE,              accumBlueBits),
+   MAP_ATTRIB(GLX_ACCUM_ALPHA_SIZE,             accumAlphaBits),
+   MAP_ATTRIB(GLX_SAMPLE_BUFFERS,               sampleBuffers),
+   MAP_ATTRIB(GLX_SAMPLES,                      samples),
+   MAP_ATTRIB(GLX_RENDER_TYPE,                  renderType),
+   MAP_ATTRIB(GLX_DRAWABLE_TYPE,                drawableType),
+   MAP_ATTRIB(GLX_X_RENDERABLE,                 xRenderable),
+   MAP_ATTRIB(GLX_X_VISUAL_TYPE,                visualType),
+   MAP_ATTRIB(GLX_CONFIG_CAVEAT,                visualRating),
+   MAP_ATTRIB(GLX_TRANSPARENT_TYPE,             transparentPixel),
+   MAP_ATTRIB(GLX_TRANSPARENT_INDEX_VALUE,      transparentIndex),
+   MAP_ATTRIB(GLX_TRANSPARENT_RED_VALUE,        transparentRed),
+   MAP_ATTRIB(GLX_TRANSPARENT_GREEN_VALUE,      transparentGreen),
+   MAP_ATTRIB(GLX_TRANSPARENT_BLUE_VALUE,       transparentBlue),
+   MAP_ATTRIB(GLX_TRANSPARENT_ALPHA_VALUE,      transparentAlpha),
+   MAP_ATTRIB(GLX_MAX_PBUFFER_WIDTH,            maxPbufferWidth),
+   MAP_ATTRIB(GLX_MAX_PBUFFER_HEIGHT,           maxPbufferHeight),
+   MAP_ATTRIB(GLX_MAX_PBUFFER_PIXELS,           maxPbufferPixels),
+   MAP_ATTRIB(GLX_VISUAL_ID,                    visualID),
+};
 
-#if defined(GLX_EXT_visual_rating)
-   if (ext && strstr(ext, "GLX_EXT_visual_rating")) {
-      glXGetConfig(dpy, vInfo, GLX_VISUAL_CAVEAT_EXT, &attribs->visualCaveat);
-   }
-   else {
-      attribs->visualCaveat = GLX_NONE_EXT;
-   }
-#else
-   attribs->visualCaveat = 0;
-#endif
 
-   return GL_TRUE;
-}
+static EGLBoolean
+convert_fbconfig(Display *dpy, GLXFBConfig fbconfig,
+                 struct GLX_egl_config *GLX_conf)
+{
+   __GLcontextModes mode;
+   int err = 0, attr, val, i;
+
+   memset(&mode, 0, sizeof(mode));
+
+   for (i = 0; i < ARRAY_SIZE(fbconfig_attributes); i++) {
+      int offset = fbconfig_attributes[i].offset;
+      attr = fbconfig_attributes[i].attr;
+      err = glXGetFBConfigAttrib(dpy, fbconfig, attr, &val);
+      if (err) {
+         if (err == GLX_BAD_ATTRIBUTE) {
+            err = 0;
+            continue;
+         }
+         break;
+      }
+      *((int *) ((char *) &mode + offset)) = val;
+   }
+   if (err)
+      return EGL_FALSE;
 
-#ifdef GLX_VERSION_1_3
+   /* must have rgba bit */
+   if (!(mode.renderType & GLX_RGBA_BIT))
+      return EGL_FALSE;
 
-static int
-glx_token_to_visual_class(int visual_type)
-{
-   switch (visual_type) {
-   case GLX_TRUE_COLOR:
-      return TrueColor;
-   case GLX_DIRECT_COLOR:
-      return DirectColor;
-   case GLX_PSEUDO_COLOR:
-      return PseudoColor;
-   case GLX_STATIC_COLOR:
-      return StaticColor;
-   case GLX_GRAY_SCALE:
-      return GrayScale;
-   case GLX_STATIC_GRAY:
-      return StaticGray;
-   case GLX_NONE:
-   default:
-      return None;
+   /* pixmap and pbuffer surfaces must be single-buffered in EGL */
+   if (mode.doubleBufferMode) {
+      mode.drawableType &= ~(GLX_PIXMAP_BIT | GLX_PBUFFER_BIT);
+      if (!mode.drawableType)
+         return EGL_FALSE;
    }
+
+   mode.rgbMode = GL_TRUE;
+   mode.haveAccumBuffer = (mode.accumRedBits +
+                           mode.accumGreenBits +
+                           mode.accumBlueBits +
+                           mode.accumAlphaBits > 0);
+   mode.haveDepthBuffer = (mode.depthBits > 0);
+   mode.haveStencilBuffer = (mode.stencilBits > 0);
+
+   GLX_conf->double_buffered = (mode.doubleBufferMode != 0);
+   return _eglConfigFromContextModesRec(&GLX_conf->Base, &mode,
+                                        GLX_EGL_APIS, GLX_EGL_APIS);
 }
-static int
-get_fbconfig_attribs(Display *dpy, GLXFBConfig fbconfig,
-                    struct visual_attribs *attribs)
-{
-   int visual_type;
-   int fbconfig_id;
 
-   memset(attribs, 0, sizeof(struct visual_attribs));
 
-   glXGetFBConfigAttrib(dpy, fbconfig, GLX_FBCONFIG_ID, &fbconfig_id);
+static const struct {
+   int attr;
+   int offset;
+} visual_attributes[] = {
+   /* table 3.7 of GLX 1.4 */
+   /* no GLX_USE_GL */
+   MAP_ATTRIB(GLX_BUFFER_SIZE,         rgbBits),
+   MAP_ATTRIB(GLX_LEVEL,               level),
+   MAP_ATTRIB(GLX_RGBA,                rgbMode),
+   MAP_ATTRIB(GLX_DOUBLEBUFFER,        doubleBufferMode),
+   MAP_ATTRIB(GLX_STEREO,              stereoMode),
+   MAP_ATTRIB(GLX_AUX_BUFFERS,         numAuxBuffers),
+   MAP_ATTRIB(GLX_RED_SIZE,            redBits),
+   MAP_ATTRIB(GLX_GREEN_SIZE,          greenBits),
+   MAP_ATTRIB(GLX_BLUE_SIZE,           blueBits),
+   MAP_ATTRIB(GLX_ALPHA_SIZE,          alphaBits),
+   MAP_ATTRIB(GLX_DEPTH_SIZE,          depthBits),
+   MAP_ATTRIB(GLX_STENCIL_SIZE,        stencilBits),
+   MAP_ATTRIB(GLX_ACCUM_RED_SIZE,      accumRedBits),
+   MAP_ATTRIB(GLX_ACCUM_GREEN_SIZE,    accumGreenBits),
+   MAP_ATTRIB(GLX_ACCUM_BLUE_SIZE,     accumBlueBits),
+   MAP_ATTRIB(GLX_ACCUM_ALPHA_SIZE,    accumAlphaBits),
+   MAP_ATTRIB(GLX_SAMPLE_BUFFERS,      sampleBuffers),
+   MAP_ATTRIB(GLX_SAMPLES,             samples),
+   MAP_ATTRIB(GLX_FBCONFIG_ID,         fbconfigID),
+   /* GLX_EXT_visual_rating */
+   MAP_ATTRIB(GLX_VISUAL_CAVEAT_EXT,   visualRating),
+};
 
-   glXGetFBConfigAttrib(dpy, fbconfig, GLX_VISUAL_ID, &attribs->id);
 
-#if 0
-   attribs->depth = vInfo->depth;
-   attribs->redMask = vInfo->red_mask;
-   attribs->greenMask = vInfo->green_mask;
-   attribs->blueMask = vInfo->blue_mask;
-   attribs->colormapSize = vInfo->colormap_size;
-   attribs->bitsPerRGB = vInfo->bits_per_rgb;
+static int
+get_visual_type(const XVisualInfo *vis)
+{
+   int klass;
+
+#if defined(__cplusplus) || defined(c_plusplus)
+   klass = vis->c_class;
+#else
+   klass = vis->class;
 #endif
 
-   glXGetFBConfigAttrib(dpy, fbconfig, GLX_X_VISUAL_TYPE, &visual_type);
-   attribs->klass = glx_token_to_visual_class(visual_type);
+   switch (klass) {
+   case TrueColor:
+      return GLX_TRUE_COLOR;
+   case DirectColor:
+      return GLX_DIRECT_COLOR;
+   case PseudoColor:
+      return GLX_PSEUDO_COLOR;
+   case StaticColor:
+      return GLX_STATIC_COLOR;
+   case GrayScale:
+      return GLX_GRAY_SCALE;
+   case StaticGray:
+      return GLX_STATIC_GRAY;
+   default:
+      return GLX_NONE;
+   }
+}
 
-   glXGetFBConfigAttrib(dpy, fbconfig, GLX_BUFFER_SIZE, &attribs->bufferSize);
-   glXGetFBConfigAttrib(dpy, fbconfig, GLX_LEVEL, &attribs->level);
-   glXGetFBConfigAttrib(dpy, fbconfig, GLX_RENDER_TYPE, &attribs->render_type);
-   if (!(attribs->render_type & GLX_RGBA_BIT))
-      return 0;
 
-   glXGetFBConfigAttrib(dpy, fbconfig, GLX_DOUBLEBUFFER, &attribs->doubleBuffer);
-   if (!attribs->doubleBuffer)
-      return 0;
+static EGLBoolean
+convert_visual(Display *dpy, XVisualInfo *vinfo,
+               struct GLX_egl_config *GLX_conf)
+{
+   __GLcontextModes mode;
+   int err, attr, val, i;
 
-   glXGetFBConfigAttrib(dpy, fbconfig, GLX_STEREO, &attribs->stereo);
-   glXGetFBConfigAttrib(dpy, fbconfig, GLX_AUX_BUFFERS, &attribs->auxBuffers);
+   /* the visual must support OpenGL */
+   err = glXGetConfig(dpy, vinfo, GLX_USE_GL, &val);
+   if (err || !val)
+      return EGL_FALSE;
 
-   glXGetFBConfigAttrib(dpy, fbconfig, GLX_RED_SIZE, &attribs->redSize);
-   glXGetFBConfigAttrib(dpy, fbconfig, GLX_GREEN_SIZE, &attribs->greenSize);
-   glXGetFBConfigAttrib(dpy, fbconfig, GLX_BLUE_SIZE, &attribs->blueSize);
-   glXGetFBConfigAttrib(dpy, fbconfig, GLX_ALPHA_SIZE, &attribs->alphaSize);
-   glXGetFBConfigAttrib(dpy, fbconfig, GLX_DEPTH_SIZE, &attribs->depthSize);
-   glXGetFBConfigAttrib(dpy, fbconfig, GLX_STENCIL_SIZE, &attribs->stencilSize);
+   memset(&mode, 0, sizeof(mode));
+
+   for (i = 0; i < ARRAY_SIZE(visual_attributes); i++) {
+      int offset = visual_attributes[i].offset;
+      attr = visual_attributes[i].attr;
+      err = glXGetConfig(dpy, vinfo, attr, &val);
+      if (err) {
+         if (err == GLX_BAD_ATTRIBUTE) {
+            err = 0;
+            continue;
+         }
+         break;
+      }
+      *((int *) ((char *) &mode + offset)) = val;
+   }
+   if (err)
+      return EGL_FALSE;
 
-   glXGetFBConfigAttrib(dpy, fbconfig, GLX_ACCUM_RED_SIZE, &attribs->accumRedSize);
-   glXGetFBConfigAttrib(dpy, fbconfig, GLX_ACCUM_GREEN_SIZE, &attribs->accumGreenSize);
-   glXGetFBConfigAttrib(dpy, fbconfig, GLX_ACCUM_BLUE_SIZE, &attribs->accumBlueSize);
-   glXGetFBConfigAttrib(dpy, fbconfig, GLX_ACCUM_ALPHA_SIZE, &attribs->accumAlphaSize);
+   /* must be RGB mode */
+   if (!mode.rgbMode)
+      return EGL_FALSE;
 
-   /* get transparent pixel stuff */
-   glXGetFBConfigAttrib(dpy, fbconfig,GLX_TRANSPARENT_TYPE, &attribs->transparentType);
-   if (attribs->transparentType == GLX_TRANSPARENT_RGB) {
-     glXGetFBConfigAttrib(dpy, fbconfig, GLX_TRANSPARENT_RED_VALUE, &attribs->transparentRedValue);
-     glXGetFBConfigAttrib(dpy, fbconfig, GLX_TRANSPARENT_GREEN_VALUE, &attribs->transparentGreenValue);
-     glXGetFBConfigAttrib(dpy, fbconfig, GLX_TRANSPARENT_BLUE_VALUE, &attribs->transparentBlueValue);
-     glXGetFBConfigAttrib(dpy, fbconfig, GLX_TRANSPARENT_ALPHA_VALUE, &attribs->transparentAlphaValue);
-   }
-   else if (attribs->transparentType == GLX_TRANSPARENT_INDEX) {
-     glXGetFBConfigAttrib(dpy, fbconfig, GLX_TRANSPARENT_INDEX_VALUE, &attribs->transparentIndexValue);
-   }
+   mode.visualID = vinfo->visualid;
+   mode.visualType = get_visual_type(vinfo);
+   mode.redMask = vinfo->red_mask;
+   mode.greenMask = vinfo->green_mask;
+   mode.blueMask = vinfo->blue_mask;
+
+   mode.drawableType = GLX_WINDOW_BIT;
+   /* pixmap surfaces must be single-buffered in EGL */
+   if (!mode.doubleBufferMode)
+      mode.drawableType |= GLX_PIXMAP_BIT;
+
+   mode.renderType = GLX_RGBA_BIT;
+   mode.xRenderable = GL_TRUE;
+   mode.haveAccumBuffer = (mode.accumRedBits +
+                           mode.accumGreenBits +
+                           mode.accumBlueBits +
+                           mode.accumAlphaBits > 0);
+   mode.haveDepthBuffer = (mode.depthBits > 0);
+   mode.haveStencilBuffer = (mode.stencilBits > 0);
+
+   GLX_conf->double_buffered = (mode.doubleBufferMode != 0);
+   return _eglConfigFromContextModesRec(&GLX_conf->Base, &mode,
+                                        GLX_EGL_APIS, GLX_EGL_APIS);
+}
 
-   glXGetFBConfigAttrib(dpy, fbconfig, GLX_SAMPLE_BUFFERS, &attribs->numMultisample);
-   glXGetFBConfigAttrib(dpy, fbconfig, GLX_SAMPLES, &attribs->numSamples);
 
-   glXGetFBConfigAttrib(dpy, fbconfig, GLX_CONFIG_CAVEAT, &attribs->visualCaveat);
+static void
+fix_config(struct GLX_egl_display *GLX_dpy, struct GLX_egl_config *GLX_conf)
+{
+   _EGLConfig *conf = &GLX_conf->Base;
+   EGLint surface_type, r, g, b, a;
+
+   surface_type = GET_CONFIG_ATTRIB(conf, EGL_SURFACE_TYPE);
+   if (!GLX_conf->double_buffered && GLX_dpy->single_buffered_quirk) {
+      /* some GLX impls do not like single-buffered window surface */
+      surface_type &= ~EGL_WINDOW_BIT;
+      /* pbuffer bit is usually not set */
+      if (GLX_dpy->have_pbuffer)
+         surface_type |= EGL_PBUFFER_BIT;
+      SET_CONFIG_ATTRIB(conf, EGL_SURFACE_TYPE, surface_type);
+   }
 
-   if (attribs->id == 0) {
-      attribs->id = fbconfig_id;
-      return EGL_PBUFFER_BIT | EGL_PIXMAP_BIT;
+   /* no visual attribs unless window bit is set */
+   if (!(surface_type & EGL_WINDOW_BIT)) {
+      SET_CONFIG_ATTRIB(conf, EGL_NATIVE_VISUAL_ID, 0);
+      SET_CONFIG_ATTRIB(conf, EGL_NATIVE_VISUAL_TYPE, EGL_NONE);
    }
 
-   return EGL_WINDOW_BIT;
+   /* make sure buffer size is set correctly */
+   r = GET_CONFIG_ATTRIB(conf, EGL_RED_SIZE);
+   g = GET_CONFIG_ATTRIB(conf, EGL_GREEN_SIZE);
+   b = GET_CONFIG_ATTRIB(conf, EGL_BLUE_SIZE);
+   a = GET_CONFIG_ATTRIB(conf, EGL_ALPHA_SIZE);
+   SET_CONFIG_ATTRIB(conf, EGL_BUFFER_SIZE, r + g + b + a);
 }
 
-#endif
 
 static EGLBoolean
-create_configs(_EGLDisplay *disp, struct GLX_egl_driver *GLX_drv)
+create_configs(_EGLDisplay *dpy, struct GLX_egl_display *GLX_dpy,
+               EGLint screen)
 {
-   XVisualInfo theTemplate;
-   int numVisuals;
-   long mask;
-   int i;
-   int egl_configs = 1;
-   struct visual_attribs attribs;
+   EGLint num_configs = 0, i;
+   EGLint id = 1;
 
-   GLX_drv->fbconfigs = NULL;
-
-#ifdef GLX_VERSION_1_3
-   /* get list of all fbconfigs on this screen */
-   GLX_drv->fbconfigs = glXGetFBConfigs(disp->Xdpy, DefaultScreen(disp->Xdpy), &numVisuals);
+   if (GLX_dpy->have_fbconfig) {
+      GLX_dpy->fbconfigs = glXGetFBConfigs(GLX_dpy->dpy, screen, &num_configs);
+   }
+   else {
+      XVisualInfo vinfo_template;
+      long mask;
 
-   if (numVisuals == 0) {
-      GLX_drv->fbconfigs = NULL;
-      goto xvisual;
+      vinfo_template.screen = screen;
+      mask = VisualScreenMask;
+      GLX_dpy->visuals = XGetVisualInfo(GLX_dpy->dpy, mask, &vinfo_template,
+                                        &num_configs);
    }
 
-   for (i = 0; i < numVisuals; i++) {
-      struct GLX_egl_config *config;
-      int bit;
+   if (!num_configs)
+      return EGL_FALSE;
 
-      bit = get_fbconfig_attribs(disp->Xdpy, GLX_drv->fbconfigs[i], &attribs);
-      if (!bit)
+   for (i = 0; i < num_configs; i++) {
+      struct GLX_egl_config *GLX_conf, template;
+      EGLBoolean ok;
+
+      memset(&template, 0, sizeof(template));
+      _eglInitConfig(&template.Base, id);
+      if (GLX_dpy->have_fbconfig)
+         ok = convert_fbconfig(GLX_dpy->dpy, GLX_dpy->fbconfigs[i], &template);
+      else
+         ok = convert_visual(GLX_dpy->dpy, &GLX_dpy->visuals[i], &template);
+      if (!ok)
+        continue;
+
+      fix_config(GLX_dpy, &template);
+      if (!_eglValidateConfig(&template.Base, EGL_FALSE)) {
+         _eglLog(_EGL_DEBUG, "GLX: failed to validate config %d", i);
          continue;
+      }
 
-      config = CALLOC_STRUCT(GLX_egl_config);
-
-      _eglInitConfig(&config->Base, (i+1));
-      SET_CONFIG_ATTRIB(&config->Base, EGL_NATIVE_VISUAL_ID, attribs.id);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_BUFFER_SIZE, attribs.bufferSize);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_RED_SIZE, attribs.redSize);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_GREEN_SIZE, attribs.greenSize);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_BLUE_SIZE, attribs.blueSize);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_ALPHA_SIZE, attribs.alphaSize);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_DEPTH_SIZE, attribs.depthSize);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_STENCIL_SIZE, attribs.stencilSize);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_SAMPLES, attribs.numSamples);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_SAMPLE_BUFFERS, attribs.numMultisample);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_CONFORMANT, all_apis);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_RENDERABLE_TYPE, all_apis);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_SURFACE_TYPE, bit);
-
-      /* XXX possibly other things to init... */
+      GLX_conf = CALLOC_STRUCT(GLX_egl_config);
+      if (GLX_conf) {
+         memcpy(GLX_conf, &template, sizeof(template));
+         GLX_conf->index = i;
 
-      _eglAddConfig(disp, &config->Base);
+         _eglAddConfig(dpy, &GLX_conf->Base);
+         id++;
+      }
    }
 
-   goto end;
-#endif
-
-xvisual:
-   /* get list of all visuals on this screen */
-   theTemplate.screen = DefaultScreen(disp->Xdpy);
-   mask = VisualScreenMask;
-   GLX_drv->visuals = XGetVisualInfo(disp->Xdpy, mask, &theTemplate, &numVisuals);
+   return EGL_TRUE;
+}
 
-   for (i = 0; i < numVisuals; i++) {
-      struct GLX_egl_config *config;
 
-      if (!get_visual_attribs(disp->Xdpy, &GLX_drv->visuals[i], &attribs))
-        continue;
+static void
+check_extensions(struct GLX_egl_display *GLX_dpy, EGLint screen)
+{
+   GLX_dpy->extensions =
+      glXQueryExtensionsString(GLX_dpy->dpy, screen);
+   if (GLX_dpy->extensions) {
+      /* glXGetProcAddress is assumed */
+
+      if (strstr(GLX_dpy->extensions, "GLX_SGI_make_current_read")) {
+         /* GLX 1.3 entry points are used */
+         GLX_dpy->have_make_current_read = EGL_TRUE;
+      }
 
-      config = CALLOC_STRUCT(GLX_egl_config);
+      if (strstr(GLX_dpy->extensions, "GLX_SGIX_fbconfig")) {
+         /* GLX 1.3 entry points are used */
+         GLX_dpy->have_fbconfig = EGL_TRUE;
+      }
 
-      _eglInitConfig(&config->Base, (i+1));
-      SET_CONFIG_ATTRIB(&config->Base, EGL_NATIVE_VISUAL_ID, attribs.id);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_BUFFER_SIZE, attribs.bufferSize);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_RED_SIZE, attribs.redSize);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_GREEN_SIZE, attribs.greenSize);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_BLUE_SIZE, attribs.blueSize);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_ALPHA_SIZE, attribs.alphaSize);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_DEPTH_SIZE, attribs.depthSize);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_STENCIL_SIZE, attribs.stencilSize);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_SAMPLES, attribs.numSamples);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_SAMPLE_BUFFERS, attribs.numMultisample);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_CONFORMANT, all_apis);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_RENDERABLE_TYPE, all_apis);
-      SET_CONFIG_ATTRIB(&config->Base, EGL_SURFACE_TYPE,
-                        (EGL_WINDOW_BIT /*| EGL_PBUFFER_BIT | EGL_PIXMAP_BIT*/));
+      if (strstr(GLX_dpy->extensions, "GLX_SGIX_pbuffer")) {
+         GLX_dpy->glXCreateGLXPbufferSGIX = (PFNGLXCREATEGLXPBUFFERSGIXPROC)
+            glXGetProcAddress((const GLubyte *) "glXCreateGLXPbufferSGIX");
+         GLX_dpy->glXDestroyGLXPbufferSGIX = (PFNGLXDESTROYGLXPBUFFERSGIXPROC)
+            glXGetProcAddress((const GLubyte *) "glXDestroyGLXPbufferSGIX");
 
-      /* XXX possibly other things to init... */
+         if (GLX_dpy->glXCreateGLXPbufferSGIX &&
+             GLX_dpy->glXDestroyGLXPbufferSGIX &&
+             GLX_dpy->have_fbconfig)
+            GLX_dpy->have_pbuffer = EGL_TRUE;
+      }
+   }
 
-      _eglAddConfig(disp, &config->Base);
+   if (GLX_dpy->glx_maj == 1 && GLX_dpy->glx_min >= 3) {
+      GLX_dpy->have_1_3 = EGL_TRUE;
+      GLX_dpy->have_make_current_read = EGL_TRUE;
+      GLX_dpy->have_fbconfig = EGL_TRUE;
+      GLX_dpy->have_pbuffer = EGL_TRUE;
    }
+}
 
-end:
-   return EGL_TRUE;
+
+static void
+check_quirks(struct GLX_egl_display *GLX_dpy, EGLint screen)
+{
+   const char *vendor;
+
+   GLX_dpy->single_buffered_quirk = EGL_TRUE;
+   GLX_dpy->glx_window_quirk = EGL_TRUE;
+
+   vendor = glXGetClientString(GLX_dpy->dpy, GLX_VENDOR);
+   if (vendor && strstr(vendor, "NVIDIA")) {
+      vendor = glXQueryServerString(GLX_dpy->dpy, screen, GLX_VENDOR);
+      if (vendor && strstr(vendor, "NVIDIA")) {
+         _eglLog(_EGL_DEBUG, "disable quirks");
+         GLX_dpy->single_buffered_quirk = EGL_FALSE;
+         GLX_dpy->glx_window_quirk = EGL_FALSE;
+      }
+   }
 }
 
+
 /**
  * Called via eglInitialize(), GLX_drv->API.Initialize().
  */
 static EGLBoolean
-GLX_eglInitialize(_EGLDriver *drv, EGLDisplay dpy,
-                   EGLint *minor, EGLint *major)
+GLX_eglInitialize(_EGLDriver *drv, _EGLDisplay *disp,
+                   EGLint *major, EGLint *minor)
 {
-   struct GLX_egl_driver *GLX_drv = GLX_egl_driver(drv);
-   _EGLDisplay *disp = _eglLookupDisplay(dpy);
+   struct GLX_egl_display *GLX_dpy;
 
-   _eglLog(_EGL_DEBUG, "GLX: eglInitialize");
+   GLX_dpy = CALLOC_STRUCT(GLX_egl_display);
+   if (!GLX_dpy)
+      return _eglError(EGL_BAD_ALLOC, "eglInitialize");
 
-   if (!disp->Xdpy) {
-      disp->Xdpy = XOpenDisplay(NULL);
-      if (!disp->Xdpy) {
+   GLX_dpy->dpy = (Display *) disp->NativeDisplay;
+   if (!GLX_dpy->dpy) {
+      GLX_dpy->dpy = XOpenDisplay(NULL);
+      if (!GLX_dpy->dpy) {
          _eglLog(_EGL_WARNING, "GLX: XOpenDisplay failed");
+         free(GLX_dpy);
          return EGL_FALSE;
       }
-   } 
-
-   glXQueryVersion(disp->Xdpy, &GLX_drv->glx_maj, &GLX_drv->glx_min);
-   
-   GLX_drv->Base.Initialized = EGL_TRUE;
-
-   GLX_drv->Base.Name = "GLX";
+   }
 
-   /* we're supporting EGL 1.4 */
-   *minor = 1;
-   *major = 4;
+   if (!glXQueryVersion(GLX_dpy->dpy, &GLX_dpy->glx_maj, &GLX_dpy->glx_min)) {
+      _eglLog(_EGL_WARNING, "GLX: glXQueryVersion failed");
+      if (!disp->NativeDisplay)
+         XCloseDisplay(GLX_dpy->dpy);
+      free(GLX_dpy);
+      return EGL_FALSE;
+   }
 
-   create_configs(disp, GLX_drv);
+   check_extensions(GLX_dpy, DefaultScreen(GLX_dpy->dpy));
+   check_quirks(GLX_dpy, DefaultScreen(GLX_dpy->dpy));
 
-   return EGL_TRUE;
-}
+   create_configs(disp, GLX_dpy, DefaultScreen(GLX_dpy->dpy));
+   if (!disp->NumConfigs) {
+      _eglLog(_EGL_WARNING, "GLX: failed to create any config");
+      if (!disp->NativeDisplay)
+         XCloseDisplay(GLX_dpy->dpy);
+      free(GLX_dpy);
+      return EGL_FALSE;
+   }
 
-/*
- * Do some clean-up that normally occurs in XCloseDisplay().
- * We do this here because we're about to unload a dynamic library
- * that has added some per-display extension data and callbacks.
- * If we don't do this here we'll crash in XCloseDisplay() because it'll
- * try to call functions that went away when the driver library was unloaded.
- */
-static void
-FreeDisplayExt(Display *dpy)
-{
-   _XExtension *ext, *next;
+   disp->DriverData = (void *) GLX_dpy;
+   disp->ClientAPIsMask = GLX_EGL_APIS;
 
-   for (ext = dpy->ext_procs; ext; ext = next) {
-      next = ext->next;
-      if (ext->close_display) {
-         ext->close_display(dpy, &ext->codes);
-         ext->close_display = NULL;
-      }
-      if (ext->name)
-         Xfree(ext->name);
-      Xfree(ext);
-   }
-   dpy->ext_procs = NULL;
+   /* we're supporting EGL 1.4 */
+   *major = 1;
+   *minor = 4;
 
-   _XFreeExtData (dpy->ext_data);
-   dpy->ext_data = NULL;
+   return EGL_TRUE;
 }
 
 /**
  * Called via eglTerminate(), drv->API.Terminate().
  */
 static EGLBoolean
-GLX_eglTerminate(_EGLDriver *drv, EGLDisplay dpy)
+GLX_eglTerminate(_EGLDriver *drv, _EGLDisplay *disp)
 {
-   _EGLDisplay *disp = _eglLookupDisplay(dpy);
+   struct GLX_egl_display *GLX_dpy = GLX_egl_display(disp);
 
-   _eglLog(_EGL_DEBUG, "GLX: eglTerminate");
+   _eglReleaseDisplayResources(drv, disp);
+   _eglCleanupDisplay(disp);
 
-   FreeDisplayExt(disp->Xdpy);
+   if (GLX_dpy->visuals)
+      XFree(GLX_dpy->visuals);
+   if (GLX_dpy->fbconfigs)
+      XFree(GLX_dpy->fbconfigs);
+
+   if (!disp->NativeDisplay)
+      XCloseDisplay(GLX_dpy->dpy);
+   free(GLX_dpy);
+
+   disp->DriverData = NULL;
 
    return EGL_TRUE;
 }
@@ -519,52 +597,43 @@ GLX_eglTerminate(_EGLDriver *drv, EGLDisplay dpy)
 /**
  * Called via eglCreateContext(), drv->API.CreateContext().
  */
-static EGLContext
-GLX_eglCreateContext(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
-                      EGLContext share_list, const EGLint *attrib_list)
+static _EGLContext *
+GLX_eglCreateContext(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
+                      _EGLContext *share_list, const EGLint *attrib_list)
 {
-   _EGLDisplay *disp = _eglLookupDisplay(dpy);
    struct GLX_egl_context *GLX_ctx = CALLOC_STRUCT(GLX_egl_context);
-   struct GLX_egl_driver *GLX_drv = GLX_egl_driver(drv);
-   struct GLX_egl_context *GLX_ctx_shared = NULL;
-   _EGLConfig *conf;
+   struct GLX_egl_display *GLX_dpy = GLX_egl_display(disp);
+   struct GLX_egl_context *GLX_ctx_shared = GLX_egl_context(share_list);
 
-   if (!GLX_ctx)
-      return EGL_NO_CONTEXT;
-
-   if (!_eglInitContext(drv, dpy, &GLX_ctx->Base, config, attrib_list)) {
-      free(GLX_ctx);
-      return EGL_NO_CONTEXT;
+   if (!GLX_ctx) {
+      _eglError(EGL_BAD_ALLOC, "eglCreateContext");
+      return NULL;
    }
 
-   if (share_list != EGL_NO_CONTEXT) {
-      _EGLContext *shareCtx = _eglLookupContext(share_list);
-      if (!shareCtx) {
-         _eglError(EGL_BAD_CONTEXT, "eglCreateContext(share_list)");
-         return EGL_FALSE;
-      }
-      GLX_ctx_shared = GLX_egl_context(shareCtx);
+   if (!_eglInitContext(drv, &GLX_ctx->Base, conf, attrib_list)) {
+      free(GLX_ctx);
+      return NULL;
    }
 
-   conf = _eglLookupConfig(drv, dpy, config);
-   assert(conf);
-
-#ifdef GLX_VERSION_1_3
-   if (GLX_drv->fbconfigs)
-      GLX_ctx->context = glXCreateNewContext(disp->Xdpy, GLX_drv->fbconfigs[(int)config-1], GLX_RGBA_TYPE, GLX_ctx_shared ? GLX_ctx_shared->context : NULL, GL_TRUE);
+   if (GLX_dpy->have_fbconfig)
+      GLX_ctx->context =
+         glXCreateNewContext(GLX_dpy->dpy,
+                             GLX_dpy->fbconfigs[GLX_egl_config_index(conf)],
+                             GLX_RGBA_TYPE,
+                             GLX_ctx_shared ? GLX_ctx_shared->context : NULL,
+                             GL_TRUE);
    else
-#endif
-      GLX_ctx->context = glXCreateContext(disp->Xdpy, &GLX_drv->visuals[(int)config-1], GLX_ctx_shared ? GLX_ctx_shared->context : NULL, GL_TRUE);
-   if (!GLX_ctx->context)
-      return EGL_FALSE;
-
-#if 1
-   /* (maybe?) need to have a direct rendering context */
-   if (!glXIsDirect(disp->Xdpy, GLX_ctx->context))
-      return EGL_FALSE;
-#endif
+      GLX_ctx->context =
+         glXCreateContext(GLX_dpy->dpy,
+                          &GLX_dpy->visuals[GLX_egl_config_index(conf)],
+                          GLX_ctx_shared ? GLX_ctx_shared->context : NULL,
+                          GL_TRUE);
+   if (!GLX_ctx->context) {
+      free(GLX_ctx);
+      return NULL;
+   }
 
-   return _eglGetContextHandle(&GLX_ctx->Base);
+   return &GLX_ctx->Base;
 }
 
 
@@ -572,27 +641,29 @@ GLX_eglCreateContext(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
  * Called via eglMakeCurrent(), drv->API.MakeCurrent().
  */
 static EGLBoolean
-GLX_eglMakeCurrent(_EGLDriver *drv, EGLDisplay dpy, EGLSurface d,
-                    EGLSurface r, EGLContext context)
+GLX_eglMakeCurrent(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *dsurf,
+                   _EGLSurface *rsurf, _EGLContext *ctx)
 {
-   _EGLDisplay *disp = _eglLookupDisplay(dpy);
-   _EGLContext *ctx = _eglLookupContext(context);
-   _EGLSurface *dsurf = _eglLookupSurface(d);
-   _EGLSurface *rsurf = _eglLookupSurface(r);
+   struct GLX_egl_display *GLX_dpy = GLX_egl_display(disp);
    struct GLX_egl_surface *GLX_dsurf = GLX_egl_surface(dsurf);
    struct GLX_egl_surface *GLX_rsurf = GLX_egl_surface(rsurf);
    struct GLX_egl_context *GLX_ctx = GLX_egl_context(ctx);
+   GLXDrawable ddraw, rdraw;
+   GLXContext cctx;
 
-   if (!_eglMakeCurrent(drv, dpy, d, r, context))
+   if (!_eglMakeCurrent(drv, disp, dsurf, rsurf, ctx))
       return EGL_FALSE;
 
-#ifdef GLX_VERSION_1_3
-   if (!glXMakeContextCurrent(disp->Xdpy, GLX_dsurf ? GLX_dsurf->drawable : 0, GLX_rsurf ? GLX_rsurf->drawable : 0, GLX_ctx ? GLX_ctx->context : NULL))
-#endif
-      if (!glXMakeCurrent(disp->Xdpy, GLX_dsurf ? GLX_dsurf->drawable : 0, GLX_ctx ? GLX_ctx->context : NULL))
-         return EGL_FALSE;
+   ddraw = (GLX_dsurf) ? GLX_dsurf->glx_drawable : None;
+   rdraw = (GLX_rsurf) ? GLX_rsurf->glx_drawable : None;
+   cctx = (GLX_ctx) ? GLX_ctx->context : NULL;
 
-   return EGL_TRUE;
+   if (GLX_dpy->have_make_current_read)
+      return glXMakeContextCurrent(GLX_dpy->dpy, ddraw, rdraw, cctx);
+   else if (ddraw == rdraw)
+      return glXMakeCurrent(GLX_dpy->dpy, ddraw, cctx);
+
+   return EGL_FALSE;
 }
 
 /** Get size of given window */
@@ -612,205 +683,248 @@ get_drawable_size(Display *dpy, Drawable d, uint *width, uint *height)
 /**
  * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
  */
-static EGLSurface
-GLX_eglCreateWindowSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
+static _EGLSurface *
+GLX_eglCreateWindowSurface(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
                             NativeWindowType window, const EGLint *attrib_list)
 {
-   _EGLDisplay *disp = _eglLookupDisplay(dpy);
+   struct GLX_egl_display *GLX_dpy = GLX_egl_display(disp);
    struct GLX_egl_surface *GLX_surf;
    uint width, height;
 
    GLX_surf = CALLOC_STRUCT(GLX_egl_surface);
-   if (!GLX_surf)
-      return EGL_NO_SURFACE;
+   if (!GLX_surf) {
+      _eglError(EGL_BAD_ALLOC, "eglCreateWindowSurface");
+      return NULL;
+   }
 
-   if (!_eglInitSurface(drv, dpy, &GLX_surf->Base, EGL_WINDOW_BIT,
-                        config, attrib_list)) {
+   if (!_eglInitSurface(drv, &GLX_surf->Base, EGL_WINDOW_BIT,
+                        conf, attrib_list)) {
       free(GLX_surf);
-      return EGL_FALSE;
+      return NULL;
    }
 
-   _eglSaveSurface(&GLX_surf->Base);
-
    GLX_surf->drawable = window;
-   get_drawable_size(disp->Xdpy, window, &width, &height);
+
+   if (GLX_dpy->have_1_3 && !GLX_dpy->glx_window_quirk)
+      GLX_surf->glx_drawable =
+         glXCreateWindow(GLX_dpy->dpy,
+                         GLX_dpy->fbconfigs[GLX_egl_config_index(conf)],
+                         GLX_surf->drawable, NULL);
+   else
+      GLX_surf->glx_drawable = GLX_surf->drawable;
+
+   if (!GLX_surf->glx_drawable) {
+      free(GLX_surf);
+      return NULL;
+   }
+
+   get_drawable_size(GLX_dpy->dpy, window, &width, &height);
    GLX_surf->Base.Width = width;
    GLX_surf->Base.Height = height;
 
-   return _eglGetSurfaceHandle(&GLX_surf->Base);
+   return &GLX_surf->Base;
 }
 
-#ifdef GLX_VERSION_1_3
-static EGLSurface
-GLX_eglCreatePixmapSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config, 
+static _EGLSurface *
+GLX_eglCreatePixmapSurface(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
                           NativePixmapType pixmap, const EGLint *attrib_list)
 {
-   struct GLX_egl_driver *GLX_drv = GLX_egl_driver(drv);
-   _EGLDisplay *disp = _eglLookupDisplay(dpy);
+   struct GLX_egl_display *GLX_dpy = GLX_egl_display(disp);
    struct GLX_egl_surface *GLX_surf;
-   int i;
+   uint width, height;
 
    GLX_surf = CALLOC_STRUCT(GLX_egl_surface);
-   if (!GLX_surf)
-      return EGL_NO_SURFACE;
+   if (!GLX_surf) {
+      _eglError(EGL_BAD_ALLOC, "eglCreatePixmapSurface");
+      return NULL;
+   }
 
-   if (!_eglInitSurface(drv, dpy, &GLX_surf->Base, EGL_PIXMAP_BIT,
-                        config, attrib_list)) {
+   if (!_eglInitSurface(drv, &GLX_surf->Base, EGL_PIXMAP_BIT,
+                        conf, attrib_list)) {
       free(GLX_surf);
-      return EGL_FALSE;
+      return NULL;
    }
 
-   _eglSaveSurface(&GLX_surf->Base);
+   GLX_surf->drawable = pixmap;
 
-   for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
-      switch (attrib_list[i]) {
-         /* no attribs at this time */
-      default:
-         _eglError(EGL_BAD_ATTRIBUTE, "eglCreatePixmapSurface");
-         return EGL_NO_SURFACE;
+   if (GLX_dpy->have_1_3) {
+      GLX_surf->glx_drawable =
+         glXCreatePixmap(GLX_dpy->dpy,
+                         GLX_dpy->fbconfigs[GLX_egl_config_index(conf)],
+                         GLX_surf->drawable, NULL);
+   }
+   else if (GLX_dpy->have_fbconfig) {
+      GLXFBConfig fbconfig = GLX_dpy->fbconfigs[GLX_egl_config_index(conf)];
+      XVisualInfo *vinfo = glXGetVisualFromFBConfig(GLX_dpy->dpy, fbconfig);
+      if (vinfo) {
+         GLX_surf->glx_drawable =
+            glXCreateGLXPixmap(GLX_dpy->dpy, vinfo, GLX_surf->drawable);
+         XFree(vinfo);
       }
    }
+   else {
+      GLX_surf->glx_drawable =
+         glXCreateGLXPixmap(GLX_dpy->dpy,
+                            &GLX_dpy->visuals[GLX_egl_config_index(conf)],
+                            GLX_surf->drawable);
+   }
 
-   GLX_surf->drawable = glXCreatePixmap(disp->Xdpy, GLX_drv->fbconfigs[(int)config-1], pixmap, NULL);
+   if (!GLX_surf->glx_drawable) {
+      free(GLX_surf);
+      return NULL;
+   }
 
-   return _eglGetSurfaceHandle(&GLX_surf->Base);
+   get_drawable_size(GLX_dpy->dpy, pixmap, &width, &height);
+   GLX_surf->Base.Width = width;
+   GLX_surf->Base.Height = height;
+
+   return &GLX_surf->Base;
 }
 
-static EGLSurface
-GLX_eglCreatePbufferSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
-                            const EGLint *attrib_list)
+static _EGLSurface *
+GLX_eglCreatePbufferSurface(_EGLDriver *drv, _EGLDisplay *disp,
+                            _EGLConfig *conf, const EGLint *attrib_list)
 {
-   struct GLX_egl_driver *GLX_drv = GLX_egl_driver(drv);
-   _EGLDisplay *disp = _eglLookupDisplay(dpy);
+   struct GLX_egl_display *GLX_dpy = GLX_egl_display(disp);
    struct GLX_egl_surface *GLX_surf;
    int attribs[5];
-   int i = 0, j = 0;
+   int i;
 
    GLX_surf = CALLOC_STRUCT(GLX_egl_surface);
-   if (!GLX_surf)
-      return EGL_NO_SURFACE;
+   if (!GLX_surf) {
+      _eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");
+      return NULL;
+   }
 
-   if (!_eglInitSurface(drv, dpy, &GLX_surf->Base, EGL_PBUFFER_BIT,
-                        config, attrib_list)) {
+   if (!_eglInitSurface(drv, &GLX_surf->Base, EGL_PBUFFER_BIT,
+                        conf, attrib_list)) {
       free(GLX_surf);
-      return EGL_NO_SURFACE;
+      return NULL;
    }
 
-   _eglSaveSurface(&GLX_surf->Base);
+   i = 0;
+   attribs[i] = None;
 
-   while(attrib_list[i] != EGL_NONE) {
-      switch (attrib_list[i]) {
-         case EGL_WIDTH:
-           attribs[j++] = GLX_PBUFFER_WIDTH;
-           attribs[j++] = attrib_list[i+1];
-           break;
-        case EGL_HEIGHT:
-           attribs[j++] = GLX_PBUFFER_HEIGHT;
-           attribs[j++] = attrib_list[i+1];
-           break;
+   GLX_surf->drawable = None;
+
+   if (GLX_dpy->have_1_3) {
+      /* put geometry in attribs */
+      if (GLX_surf->Base.Width) {
+         attribs[i++] = GLX_PBUFFER_WIDTH;
+         attribs[i++] = GLX_surf->Base.Width;
+      }
+      if (GLX_surf->Base.Height) {
+         attribs[i++] = GLX_PBUFFER_HEIGHT;
+         attribs[i++] = GLX_surf->Base.Height;
       }
-      i++;
+      attribs[i] = None;
+
+      GLX_surf->glx_drawable =
+         glXCreatePbuffer(GLX_dpy->dpy,
+                          GLX_dpy->fbconfigs[GLX_egl_config_index(conf)],
+                          attribs);
+   }
+   else if (GLX_dpy->have_pbuffer) {
+      GLX_surf->glx_drawable = GLX_dpy->glXCreateGLXPbufferSGIX(
+            GLX_dpy->dpy,
+            GLX_dpy->fbconfigs[GLX_egl_config_index(conf)],
+            GLX_surf->Base.Width,
+            GLX_surf->Base.Height,
+            attribs);
    }
-   attribs[j++] = 0;
 
-   GLX_surf->drawable = glXCreatePbuffer(disp->Xdpy, GLX_drv->fbconfigs[(int)config-1], attribs);
+   if (!GLX_surf->glx_drawable) {
+      free(GLX_surf);
+      return NULL;
+   }
 
-   return _eglGetSurfaceHandle(&GLX_surf->Base);
+   return &GLX_surf->Base;
 }
-#endif
 
 static EGLBoolean
-GLX_eglDestroySurface(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface)
+GLX_eglDestroySurface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
 {
-   _EGLDisplay *disp = _eglLookupDisplay(dpy);
-   _EGLSurface *surf = _eglLookupSurface(surface);
-   return EGL_TRUE;
-   if (surf) {
-      _eglHashRemove(_eglGlobal.Surfaces, (EGLuint) surface);
-      if (surf->IsBound) {
-         surf->DeletePending = EGL_TRUE;
+   struct GLX_egl_display *GLX_dpy = GLX_egl_display(disp);
+   if (!_eglIsSurfaceBound(surf)) {
+      struct GLX_egl_surface *GLX_surf = GLX_egl_surface(surf);
+
+      if (GLX_dpy->have_1_3) {
+         switch (surf->Type) {
+         case EGL_WINDOW_BIT:
+            if (!GLX_dpy->glx_window_quirk)
+               glXDestroyWindow(GLX_dpy->dpy, GLX_surf->glx_drawable);
+            break;
+         case EGL_PBUFFER_BIT:
+            glXDestroyPbuffer(GLX_dpy->dpy, GLX_surf->glx_drawable);
+            break;
+         case EGL_PIXMAP_BIT:
+            glXDestroyPixmap(GLX_dpy->dpy, GLX_surf->glx_drawable);
+            break;
+         default:
+            break;
+         }
       }
       else {
-         free(surf);
+         switch (surf->Type) {
+         case EGL_PBUFFER_BIT:
+            GLX_dpy->glXDestroyGLXPbufferSGIX(GLX_dpy->dpy,
+                                              GLX_surf->glx_drawable);
+            break;
+         case EGL_PIXMAP_BIT:
+            glXDestroyGLXPixmap(GLX_dpy->dpy, GLX_surf->glx_drawable);
+            break;
+         default:
+            break;
+         }
       }
-
-      return EGL_TRUE;
-   }
-   else {
-      _eglError(EGL_BAD_SURFACE, "eglDestroySurface");
-      return EGL_FALSE;
+      free(surf);
    }
+
+   return EGL_TRUE;
 }
 
 
 static EGLBoolean
-GLX_eglBindTexImage(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface,
-                     EGLint buffer)
+GLX_eglSwapBuffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
 {
-   _EGLDisplay *disp = _eglLookupDisplay(dpy);
-   _EGLSurface *surf = _eglLookupSurface(surface);
-   struct GLX_egl_surface *GLX_surf = GLX_egl_surface(surf);
+   struct GLX_egl_display *GLX_dpy = GLX_egl_display(disp);
+   struct GLX_egl_surface *GLX_surf = GLX_egl_surface(draw);
 
-   /* buffer ?? */
-   glXBindTexImageEXT(disp->Xdpy, GLX_surf->drawable, GLX_FRONT_LEFT_EXT, NULL);
+   glXSwapBuffers(GLX_dpy->dpy, GLX_surf->glx_drawable);
 
    return EGL_TRUE;
 }
 
+/*
+ * Called from eglGetProcAddress() via drv->API.GetProcAddress().
+ */
+static _EGLProc
+GLX_eglGetProcAddress(const char *procname)
+{
+   return (_EGLProc) glXGetProcAddress((const GLubyte *) procname);
+}
 
 static EGLBoolean
-GLX_eglReleaseTexImage(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface,
-                        EGLint buffer)
+GLX_eglWaitClient(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx)
 {
-   _EGLDisplay *disp = _eglLookupDisplay(dpy);
-   _EGLSurface *surf = _eglLookupSurface(surface);
-   struct GLX_egl_surface *GLX_surf = GLX_egl_surface(surf);
-
-   /* buffer ?? */
-   glXReleaseTexImageEXT(disp->Xdpy, GLX_surf->drawable, GLX_FRONT_LEFT_EXT);
-
+   glXWaitGL();
    return EGL_TRUE;
 }
 
-
 static EGLBoolean
-GLX_eglSwapBuffers(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw)
+GLX_eglWaitNative(_EGLDriver *drv, _EGLDisplay *dpy, EGLint engine)
 {
-   _EGLDisplay *disp = _eglLookupDisplay(dpy);
-   _EGLSurface *surf = _eglLookupSurface(draw);
-   struct GLX_egl_surface *GLX_surf = GLX_egl_surface(surf);
-
-   _eglLog(_EGL_DEBUG, "GLX: EGL SwapBuffers 0x%x",draw);
-
-   /* error checking step: */
-   if (!_eglSwapBuffers(drv, dpy, draw))
-      return EGL_FALSE;
-
-   glXSwapBuffers(disp->Xdpy, GLX_surf->drawable);
-
+   if (engine != EGL_CORE_NATIVE_ENGINE)
+      return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
+   glXWaitX();
    return EGL_TRUE;
 }
 
-/*
- * Called from eglGetProcAddress() via drv->API.GetProcAddress().
- */
-static _EGLProc
-GLX_eglGetProcAddress(const char *procname)
+static void
+GLX_Unload(_EGLDriver *drv)
 {
-   /* This is a bit of a hack to get at the gallium/Mesa state tracker
-    *     * function st_get_proc_address().  This will probably change at
-    *         * some point.
-    *             */
-   _EGLProc (*get_proc_addr)(const char *procname);
-   get_proc_addr = dlsym(NULL, "st_get_proc_address");
-   if (get_proc_addr)
-      return get_proc_addr(procname);
-
-   get_proc_addr = glXGetProcAddress((const GLubyte *)procname);
-   if (get_proc_addr)
-      return get_proc_addr(procname);
-
-   return (_EGLProc)dlsym(NULL, procname);
+   struct GLX_egl_driver *GLX_drv = GLX_egl_driver(drv);
+   free(GLX_drv);
 }
 
 
@@ -819,11 +933,9 @@ GLX_eglGetProcAddress(const char *procname)
  * Create a new _EGLDriver object and init its dispatch table.
  */
 _EGLDriver *
-_eglMain(_EGLDisplay *disp, const char *args)
+_eglMain(const char *args)
 {
    struct GLX_egl_driver *GLX_drv = CALLOC_STRUCT(GLX_egl_driver);
-   char *env;
-   int maj = 0, min = 0;
 
    if (!GLX_drv)
       return NULL;
@@ -834,37 +946,16 @@ _eglMain(_EGLDisplay *disp, const char *args)
    GLX_drv->Base.API.CreateContext = GLX_eglCreateContext;
    GLX_drv->Base.API.MakeCurrent = GLX_eglMakeCurrent;
    GLX_drv->Base.API.CreateWindowSurface = GLX_eglCreateWindowSurface;
-#ifdef GLX_VERSION_1_3
-   if (GLX_drv->glx_maj == 1 && GLX_drv->glx_min >= 3) {
-      GLX_drv->Base.API.CreatePixmapSurface = GLX_eglCreatePixmapSurface;
-      GLX_drv->Base.API.CreatePbufferSurface = GLX_eglCreatePbufferSurface;
-      printf("GLX: Pbuffer and Pixmap support\n");
-   } else {
-      printf("GLX: No pbuffer or pixmap support\n");
-   }
-#endif
+   GLX_drv->Base.API.CreatePixmapSurface = GLX_eglCreatePixmapSurface;
+   GLX_drv->Base.API.CreatePbufferSurface = GLX_eglCreatePbufferSurface;
    GLX_drv->Base.API.DestroySurface = GLX_eglDestroySurface;
-   GLX_drv->Base.API.BindTexImage = GLX_eglBindTexImage;
-   GLX_drv->Base.API.ReleaseTexImage = GLX_eglReleaseTexImage;
    GLX_drv->Base.API.SwapBuffers = GLX_eglSwapBuffers;
    GLX_drv->Base.API.GetProcAddress = GLX_eglGetProcAddress;
+   GLX_drv->Base.API.WaitClient = GLX_eglWaitClient;
+   GLX_drv->Base.API.WaitNative = GLX_eglWaitNative;
 
-   GLX_drv->Base.ClientAPIsMask = all_apis;
    GLX_drv->Base.Name = "GLX";
-
-   _eglLog(_EGL_DEBUG, "GLX: main(%s)", args);
-
-   /* set new DRI path to pick up EGL version (which doesn't contain any mesa 
-    * code), but don't override if one is already set.
-    */
-   env = getenv("LIBGL_DRIVERS_PATH");
-   if (env) {
-      if (!strstr(env, "egl")) {
-         sprintf(env, "%s/egl", env);
-         setenv("LIBGL_DRIVERS_PATH", env, 1);
-      }
-   } else
-      setenv("LIBGL_DRIVERS_PATH", DEFAULT_DRIVER_DIR"/egl", 0);
+   GLX_drv->Base.Unload = GLX_Unload;
 
    return &GLX_drv->Base;
 }