glx: Use _X_EXPORT instead of our own PUBLIC macro
[mesa.git] / src / glx / glxcmds.c
index ea267f3dd8ace2f2f024ee4eefff0ea4c7e99c5b..5de39f6a8d28dec05dc6ecc259f91e2a20d2f6a5 100644 (file)
 #include "glxclient.h"
 #include "glapi.h"
 #include "glxextensions.h"
-#include "glcontextmodes.h"
 
 #ifdef GLX_DIRECT_RENDERING
+#ifdef GLX_USE_APPLEGL
+#include "apple_glx_context.h"
+#include "apple_glx.h"
+#include "glx_error.h"
+#define GC_IS_DIRECT(gc) ((gc)->isDirect)
+#else
 #include <sys/time.h>
 #include <X11/extensions/xf86vmode.h>
 #include "xf86dri.h"
 #define GC_IS_DIRECT(gc) ((gc)->driContext != NULL)
+#endif
 #else
 #define GC_IS_DIRECT(gc) (0)
 #endif
 
 static const char __glXGLXClientVendorName[] = "Mesa Project and SGI";
 static const char __glXGLXClientVersion[] = "1.4";
+static const struct glx_context_vtable indirect_context_vtable;
 
-
-/****************************************************************************/
-
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
 
 static Bool windowExistsFlag;
 static int
@@ -80,33 +84,34 @@ windowExistsErrorHandler(Display * dpy, XErrorEvent * xerr)
  * \param dpy    Display to destroy drawables for
  * \param screen Screen number to destroy drawables for
  */
-static void
-GarbageCollectDRIDrawables(Display * dpy, __GLXscreenConfigs * sc)
+_X_HIDDEN void
+GarbageCollectDRIDrawables(__GLXscreenConfigs * sc)
 {
    XID draw;
    __GLXDRIdrawable *pdraw;
+   __GLXdisplayPrivate *priv = sc->display;
    XWindowAttributes xwa;
    int (*oldXErrorHandler) (Display *, XErrorEvent *);
 
    /* Set no-op error handler so Xlib doesn't bail out if the windows
     * has alreay been destroyed on the server. */
-   XSync(dpy, GL_FALSE);
+   XSync(priv->dpy, GL_FALSE);
    oldXErrorHandler = XSetErrorHandler(windowExistsErrorHandler);
 
-   if (__glxHashFirst(sc->drawHash, &draw, (void *) &pdraw) == 1) {
+   if (__glxHashFirst(priv->drawHash, &draw, (void *) &pdraw) == 1) {
       do {
          windowExistsFlag = GL_TRUE;
-         XGetWindowAttributes(dpy, draw, &xwa); /* dummy request */
+         XGetWindowAttributes(priv->dpy, draw, &xwa); /* dummy request */
          if (!windowExistsFlag) {
             /* Destroy the local drawable data, if the drawable no
                longer exists in the Xserver */
             (*pdraw->destroyDrawable) (pdraw);
-            __glxHashDelete(sc->drawHash, draw);
+            __glxHashDelete(priv->drawHash, draw);
          }
-      } while (__glxHashNext(sc->drawHash, &draw, (void *) &pdraw) == 1);
+      } while (__glxHashNext(priv->drawHash, &draw, (void *) &pdraw) == 1);
    }
 
-   XSync(dpy, GL_FALSE);
+   XSync(priv->dpy, GL_FALSE);
    XSetErrorHandler(oldXErrorHandler);
 }
 
@@ -120,28 +125,16 @@ GarbageCollectDRIDrawables(Display * dpy, __GLXscreenConfigs * sc)
  *           the drawable is not associated with a direct-rendering context.
  */
 _X_HIDDEN __GLXDRIdrawable *
-GetGLXDRIDrawable(Display * dpy, GLXDrawable drawable, int *const scrn_num)
+GetGLXDRIDrawable(Display * dpy, GLXDrawable drawable)
 {
    __GLXdisplayPrivate *priv = __glXInitialize(dpy);
    __GLXDRIdrawable *pdraw;
-   const unsigned screen_count = ScreenCount(dpy);
-   unsigned i;
-   __GLXscreenConfigs *psc;
 
    if (priv == NULL)
       return NULL;
 
-   for (i = 0; i < screen_count; i++) {
-      psc = &priv->screenConfigs[i];
-      if (psc->drawHash == NULL)
-         continue;
-
-      if (__glxHashLookup(psc->drawHash, drawable, (void *) &pdraw) == 0) {
-         if (scrn_num != NULL)
-            *scrn_num = i;
-         return pdraw;
-      }
-   }
+   if (__glxHashLookup(priv->drawHash, drawable, (void *) &pdraw) == 0)
+      return pdraw;
 
    return NULL;
 }
@@ -170,7 +163,7 @@ GetGLXScreenConfigs(Display * dpy, int scrn)
 
    return (priv
            && priv->screenConfigs !=
-           NULL) ? &priv->screenConfigs[scrn] : NULL;
+           NULL) ? priv->screenConfigs[scrn] : NULL;
 }
 
 
@@ -197,7 +190,7 @@ GetGLXPrivScreenConfig(Display * dpy, int scrn, __GLXdisplayPrivate ** ppriv,
    }
 
    /* Check to see if the GL is supported on this screen */
-   *ppsc = &((*ppriv)->screenConfigs[scrn]);
+   *ppsc = (*ppriv)->screenConfigs[scrn];
    if ((*ppsc)->configs == NULL) {
       /* No support for GL on this screen regardless of visual */
       return GLX_BAD_VISUAL;
@@ -214,32 +207,80 @@ GetGLXPrivScreenConfig(Display * dpy, int scrn, __GLXdisplayPrivate ** ppriv,
  * \param config  Application supplied \c GLXFBConfig.
  *
  * \returns If the \c GLXFBConfig is valid, the a pointer to the matching
- *          \c __GLcontextModes structure is returned.  Otherwise, \c NULL
+ *          \c struct glx_config structure is returned.  Otherwise, \c NULL
  *          is returned.
  */
-static __GLcontextModes *
-ValidateGLXFBConfig(Display * dpy, GLXFBConfig config)
+static struct glx_config *
+ValidateGLXFBConfig(Display * dpy, GLXFBConfig fbconfig)
 {
    __GLXdisplayPrivate *const priv = __glXInitialize(dpy);
-   const unsigned num_screens = ScreenCount(dpy);
+   int num_screens = ScreenCount(dpy);
    unsigned i;
-   const __GLcontextModes *modes;
-
+   struct glx_config *config;
 
    if (priv != NULL) {
       for (i = 0; i < num_screens; i++) {
-         for (modes = priv->screenConfigs[i].configs; modes != NULL;
-              modes = modes->next) {
-            if (modes == (__GLcontextModes *) config) {
-               return (__GLcontextModes *) config;
-            }
-         }
+        for (config = priv->screenConfigs[i]->configs; config != NULL;
+             config = config->next) {
+           if (config == (struct glx_config *) fbconfig) {
+              return config;
+           }
+        }
       }
    }
 
    return NULL;
 }
 
+#ifdef GLX_USE_APPLEGL
+
+static const struct glx_context_vtable applegl_context_vtable;
+
+static __GLcontext *
+applegl_create_context(__GLXscreenConfigs *psc,
+                      struct glx_config *mode,
+                      GLXContext shareList, int renderType)
+{
+   __GLXcontext *gc;
+   int errorcode;
+   bool x11error;
+
+   /* TODO: Integrate this with apple_glx_create_context and make
+    * struct apple_glx_context inherit from __GLXcontext. */
+
+   gc = Xmalloc(sizeof *gc);
+   if (pcp == NULL)
+      return NULL;
+
+   memset(gc, 0, sizeof *gc);
+   if (!glx_context_init(&gc->base, &psc->base, mode)) {
+      Xfree(gc);
+      return NULL;
+   }
+
+   gc->vtable = &applegl_context_vtable;
+   gc->driContext = NULL;
+   gc->do_destroy = False;
+
+   /* TODO: darwin: Integrate with above to do indirect */
+   if(apple_glx_create_context(&gc->driContext, dpy, screen, fbconfig, 
+                              shareList ? shareList->driContext : NULL,
+                              &errorcode, &x11error)) {
+      __glXSendError(dpy, errorcode, 0, X_GLXCreateContext, x11error);
+      gc->vtable->destroy(gc);
+      return NULL;
+   }
+
+   gc->currentContextTag = -1;
+   gc->mode = fbconfig;
+   gc->isDirect = allowDirect;
+   gc->xid = 1; /* Just something not None, so we know when to destroy
+                * it in MakeContextCurrent. */
+
+   return gc;
+}
+#endif
+
 
 /**
  * \todo It should be possible to move the allocate of \c client_state_private
@@ -252,17 +293,16 @@ ValidateGLXFBConfig(Display * dpy, GLXFBConfig config)
  * does all the initialization (including the pixel pack / unpack).
  */
 static GLXContext
-AllocateGLXContext(Display * dpy)
+indirect_create_context(__GLXscreenConfigs *psc,
+                       struct glx_config *mode,
+                       GLXContext shareList, int renderType)
 {
    GLXContext gc;
    int bufSize;
    CARD8 opcode;
    __GLXattribute *state;
 
-   if (!dpy)
-      return NULL;
-
-   opcode = __glXSetupForCommand(dpy);
+   opcode = __glXSetupForCommand(psc->dpy);
    if (!opcode) {
       return NULL;
    }
@@ -275,6 +315,9 @@ AllocateGLXContext(Display * dpy)
    }
    memset(gc, 0, sizeof(struct __GLXcontextRec));
 
+   glx_context_init(gc, psc, mode);
+   gc->isDirect = GL_FALSE;
+   gc->vtable = &indirect_context_vtable;
    state = Xmalloc(sizeof(struct __GLXattributeRec));
    if (state == NULL) {
       /* Out of memory */
@@ -292,7 +335,7 @@ AllocateGLXContext(Display * dpy)
     ** packet for the GLXRenderReq header.
     */
 
-   bufSize = (XMaxRequestSize(dpy) * 4) - sz_xGLXRenderReq;
+   bufSize = (XMaxRequestSize(psc->dpy) * 4) - sz_xGLXRenderReq;
    gc->buf = (GLubyte *) Xmalloc(bufSize);
    if (!gc->buf) {
       Xfree(gc->client_state_private);
@@ -328,7 +371,6 @@ AllocateGLXContext(Display * dpy)
    else {
       gc->limit = gc->buf + bufSize - __GLX_BUFFER_LIMIT_SIZE;
    }
-   gc->createDpy = dpy;
    gc->majorOpcode = opcode;
 
    /*
@@ -344,9 +386,48 @@ AllocateGLXContext(Display * dpy)
       bufSize = __GLX_MAX_RENDER_CMD_SIZE;
    }
    gc->maxSmallRenderCommandSize = bufSize;
+   
+
    return gc;
 }
 
+struct glx_screen_vtable indirect_screen_vtable = {
+   indirect_create_context
+};
+
+_X_HIDDEN __GLXscreenConfigs *
+indirect_create_screen(int screen, __GLXdisplayPrivate * priv)
+{
+   __GLXscreenConfigs *psc;
+
+   psc = Xmalloc(sizeof *psc);
+   if (psc == NULL)
+      return NULL;
+
+   memset(psc, 0, sizeof *psc);
+   glx_screen_init(psc, screen, priv);
+   psc->vtable = &indirect_screen_vtable;
+
+   return psc;
+}
+
+
+_X_HIDDEN Bool
+glx_context_init(__GLXcontext *gc,
+                __GLXscreenConfigs *psc, struct glx_config *config)
+{
+   gc->majorOpcode = __glXSetupForCommand(psc->display->dpy);
+   if (!gc->majorOpcode)
+      return GL_FALSE;
+
+   gc->screen = psc->scr;
+   gc->psc = psc;
+   gc->config = config;
+   gc->isDirect = GL_TRUE;
+
+   return GL_TRUE;
+}
+
 
 /**
  * Create a new context.  Exactly one of \c vis and \c fbconfig should be
@@ -359,15 +440,13 @@ AllocateGLXContext(Display * dpy)
 
 static GLXContext
 CreateContext(Display * dpy, int generic_id,
-              const __GLcontextModes * const fbconfig,
+              struct glx_config *config,
               GLXContext shareList,
               Bool allowDirect,
              unsigned code, int renderType, int screen)
 {
-   GLXContext gc;
-#ifdef GLX_DIRECT_RENDERING
+   GLXContext gc = NULL;
    __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, screen);
-#endif
 
    if (dpy == NULL)
       return NULL;
@@ -375,23 +454,14 @@ CreateContext(Display * dpy, int generic_id,
    if (generic_id == None)
       return NULL;
 
-   gc = AllocateGLXContext(dpy);
+   gc = NULL;
+   if (allowDirect && psc->vtable->create_context)
+      gc = psc->vtable->create_context(psc, config, shareList, renderType);
+   if (!gc)
+      gc = indirect_create_context(psc, config, shareList, renderType);
    if (!gc)
       return NULL;
 
-#ifdef GLX_DIRECT_RENDERING
-   if (allowDirect && psc->driScreen) {
-      gc->driContext = psc->driScreen->createContext(psc, fbconfig, gc,
-                                                    shareList, renderType);
-      if (gc->driContext != NULL) {
-        gc->screen = screen;
-        gc->psc = psc;
-        gc->mode = fbconfig;
-        gc->isDirect = GL_TRUE;
-      }
-   }
-#endif
-
    LockDisplay(dpy);
    switch (code) {
    case X_GLXCreateContext: {
@@ -462,18 +532,18 @@ CreateContext(Display * dpy, int generic_id,
    return gc;
 }
 
-PUBLIC GLXContext
+_X_EXPORT GLXContext
 glXCreateContext(Display * dpy, XVisualInfo * vis,
                  GLXContext shareList, Bool allowDirect)
 {
-   const __GLcontextModes *mode = NULL;
+   struct glx_config *config = NULL;
    int renderType = 0;
 
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) || defined(GLX_USE_APPLEGL)
    __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, vis->screen);
 
-   mode = _gl_context_modes_find_visual(psc->visuals, vis->visualid);
-   if (mode == NULL) {
+   config = glx_config_find_visual(psc->visuals, vis->visualid);
+   if (config == NULL) {
       xError error;
 
       error.errorCode = BadValue;
@@ -486,16 +556,36 @@ glXCreateContext(Display * dpy, XVisualInfo * vis,
       return None;
    }
 
-   renderType = mode->rgbMode ? GLX_RGBA_TYPE : GLX_COLOR_INDEX_TYPE;
+   renderType = config->rgbMode ? GLX_RGBA_TYPE : GLX_COLOR_INDEX_TYPE;
 #endif
 
-   return CreateContext(dpy, vis->visualid, mode, shareList, allowDirect,
+   return CreateContext(dpy, vis->visualid, config, shareList, allowDirect,
                         X_GLXCreateContext, renderType, vis->screen);
 }
 
 _X_HIDDEN void
-__glXFreeContext(__GLXcontext * gc)
+glx_send_destroy_context(Display *dpy, XID xid)
+{
+   CARD8 opcode = __glXSetupForCommand(dpy);
+   xGLXDestroyContextReq *req;
+
+   LockDisplay(dpy);
+   GetReq(GLXDestroyContext, req);
+   req->reqType = opcode;
+   req->glxCode = X_GLXDestroyContext;
+   req->context = xid;
+   UnlockDisplay(dpy);
+   SyncHandle();
+}
+
+static void
+indirect_destroy_context(__GLXcontext *gc)
 {
+   if (!gc->imported && gc->xid)
+      glx_send_destroy_context(gc->psc->dpy, gc->xid);
+
+   __glXFreeVertexArrayState(gc);
+
    if (gc->vendor)
       XFree((char *) gc->vendor);
    if (gc->renderer)
@@ -508,7 +598,6 @@ __glXFreeContext(__GLXcontext * gc)
    XFree((char *) gc->buf);
    Xfree((char *) gc->client_state_private);
    XFree((char *) gc);
-
 }
 
 /*
@@ -517,68 +606,29 @@ __glXFreeContext(__GLXcontext * gc)
 static void
 DestroyContext(Display * dpy, GLXContext gc)
 {
-   xGLXDestroyContextReq *req;
-   GLXContextID xid;
-   CARD8 opcode;
-   GLboolean imported;
-
-   opcode = __glXSetupForCommand(dpy);
-   if (!opcode || !gc) {
+   if (!gc)
       return;
-   }
 
    __glXLock();
-   xid = gc->xid;
-   imported = gc->imported;
-   gc->xid = None;
-
    if (gc->currentDpy) {
       /* This context is bound to some thread.  According to the man page,
        * we should not actually delete the context until it's unbound.
        * Note that we set gc->xid = None above.  In MakeContextCurrent()
        * we check for that and delete the context there.
        */
+      if (!gc->imported)
+        glx_send_destroy_context(dpy, gc->xid);
+      gc->xid = None;
       __glXUnlock();
       return;
    }
+   __glXUnlock();
 
-#ifdef GLX_DIRECT_RENDERING
-   /* Destroy the direct rendering context */
-   if (gc->driContext) {
-      (*gc->driContext->destroyContext) (gc->driContext, gc->psc, dpy);
-      gc->driContext = NULL;
-      GarbageCollectDRIDrawables(dpy, gc->psc);
-   }
-#endif
-
-   __glXFreeVertexArrayState(gc);
-
-   if (gc->currentDpy) {
-      /* Have to free later cuz it's in use now */
-      __glXUnlock();
-   }
-   else {
-      /* Destroy the handle if not current to anybody */
-      __glXUnlock();
-      __glXFreeContext(gc);
-   }
-
-   if (!imported) {
-      /*
-       ** This dpy also created the server side part of the context.
-       ** Send the glXDestroyContext request.
-       */
-      LockDisplay(dpy);
-      GetReq(GLXDestroyContext, req);
-      req->reqType = opcode;
-      req->glxCode = X_GLXDestroyContext;
-      req->context = xid;
-      UnlockDisplay(dpy);
-      SyncHandle();
-   }
+   if (gc->vtable->destroy)
+      gc->vtable->destroy(gc);
 }
 
-PUBLIC void
+_X_EXPORT void
 glXDestroyContext(Display * dpy, GLXContext gc)
 {
    DestroyContext(dpy, gc);
@@ -587,7 +637,7 @@ glXDestroyContext(Display * dpy, GLXContext gc)
 /*
 ** Return the major and minor version #s for the GLX extension
 */
-PUBLIC Bool
+_X_EXPORT Bool
 glXQueryVersion(Display * dpy, int *major, int *minor)
 {
    __GLXdisplayPrivate *priv;
@@ -607,7 +657,7 @@ glXQueryVersion(Display * dpy, int *major, int *minor)
 /*
 ** Query the existance of the GLX extension
 */
-PUBLIC Bool
+_X_EXPORT Bool
 glXQueryExtension(Display * dpy, int *errorBase, int *eventBase)
 {
    int major_op, erb, evb;
@@ -623,39 +673,15 @@ glXQueryExtension(Display * dpy, int *errorBase, int *eventBase)
    return rv;
 }
 
-/*
-** Put a barrier in the token stream that forces the GL to finish its
-** work before X can proceed.
-*/
-PUBLIC void
-glXWaitGL(void)
+static void
+indirect_wait_gl(__GLXcontext *gc)
 {
    xGLXWaitGLReq *req;
-   GLXContext gc = __glXGetCurrentContext();
    Display *dpy = gc->currentDpy;
 
-   if (!dpy)
-      return;
-
    /* Flush any pending commands out */
    __glXFlushRenderBuffer(gc, gc->pc);
 
-#ifdef GLX_DIRECT_RENDERING
-   if (gc->driContext) {
-      int screen;
-      __GLXDRIdrawable *pdraw =
-         GetGLXDRIDrawable(dpy, gc->currentDrawable, &screen);
-
-      if (pdraw != NULL) {
-         __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, screen);
-         glFlush();
-         if (psc->driScreen->waitGL != NULL)
-            (*psc->driScreen->waitGL) (pdraw);
-      }
-      return;
-   }
-#endif
-
    /* Send the glXWaitGL request */
    LockDisplay(dpy);
    GetReq(GLXWaitGL, req);
@@ -667,42 +693,27 @@ glXWaitGL(void)
 }
 
 /*
-** Put a barrier in the token stream that forces X to finish its
-** work before GL can proceed.
+** Put a barrier in the token stream that forces the GL to finish its
+** work before X can proceed.
 */
-PUBLIC void
-glXWaitX(void)
+_X_EXPORT void
+glXWaitGL(void)
 {
-   xGLXWaitXReq *req;
    GLXContext gc = __glXGetCurrentContext();
-   Display *dpy = gc->currentDpy;
 
-   if (!dpy)
-      return;
+   if (gc && gc->vtable->use_x_font)
+      gc->vtable->wait_gl(gc);
+}
+
+static void
+indirect_wait_x(__GLXcontext *gc)
+{
+   xGLXWaitXReq *req;
+   Display *dpy = gc->currentDpy;
 
    /* Flush any pending commands out */
    __glXFlushRenderBuffer(gc, gc->pc);
 
-#ifdef GLX_DIRECT_RENDERING
-   if (gc->driContext) {
-      int screen;
-      __GLXDRIdrawable *pdraw =
-         GetGLXDRIDrawable(dpy, gc->currentDrawable, &screen);
-
-      if (pdraw != NULL) {
-         __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, screen);
-         if (psc->driScreen->waitX != NULL)
-            (*psc->driScreen->waitX) (pdraw);
-      }
-      else
-         XSync(dpy, False);
-      return;
-   }
-#endif
-
-   /*
-    ** Send the glXWaitX request.
-    */
    LockDisplay(dpy);
    GetReq(GLXWaitX, req);
    req->reqType = gc->majorOpcode;
@@ -712,25 +723,28 @@ glXWaitX(void)
    SyncHandle();
 }
 
-PUBLIC void
-glXUseXFont(Font font, int first, int count, int listBase)
+/*
+** Put a barrier in the token stream that forces X to finish its
+** work before GL can proceed.
+*/
+_X_EXPORT void
+glXWaitX(void)
 {
-   xGLXUseXFontReq *req;
    GLXContext gc = __glXGetCurrentContext();
-   Display *dpy = gc->currentDpy;
 
-   if (!dpy)
-      return;
+   if (gc && gc->vtable->use_x_font)
+      gc->vtable->wait_x(gc);
+}
 
-   /* Flush any pending commands out */
-   (void) __glXFlushRenderBuffer(gc, gc->pc);
+static void
+indirect_use_x_font(__GLXcontext *gc,
+                   Font font, int first, int count, int listBase)
+{
+   xGLXUseXFontReq *req;
+   Display *dpy = gc->currentDpy;
 
-#ifdef GLX_DIRECT_RENDERING
-   if (gc->driContext) {
-      DRI_glXUseXFont(font, first, count, listBase);
-      return;
-   }
-#endif
+   /* Flush any pending commands out */
+   __glXFlushRenderBuffer(gc, gc->pc);
 
    /* Send the glXUseFont request */
    LockDisplay(dpy);
@@ -746,16 +760,67 @@ glXUseXFont(Font font, int first, int count, int listBase)
    SyncHandle();
 }
 
+#ifdef GLX_USE_APPLEGL
+
+static void
+applegl_destroy_context(__GLXcontext *gc)
+{
+   apple_glx_destroy_context(&gc->driContext, gc->currentDpy);
+}
+
+static void
+applegl_wait_gl(__GLXcontext *gc)
+{
+   glFinish();
+}
+
+static void
+applegl_wait_x(__GLXcontext *gc)
+{
+   apple_glx_waitx(gc->dpy, gc->driContext);
+}
+
+static const struct glx_context_vtable applegl_context_vtable = {
+   applegl_destroy_context,
+   applegl_wait_gl,
+   applegl_wait_x,
+   DRI_glXUseXFont,
+   NULL, /* bind_tex_image, */
+   NULL, /* release_tex_image, */
+};
+
+#endif
+
+_X_EXPORT void
+glXUseXFont(Font font, int first, int count, int listBase)
+{
+   GLXContext gc = __glXGetCurrentContext();
+
+   if (gc && gc->vtable->use_x_font)
+      gc->vtable->use_x_font(gc, font, first, count, listBase);
+}
+
 /************************************************************************/
 
 /*
 ** Copy the source context to the destination context using the
 ** attribute "mask".
 */
-PUBLIC void
+_X_EXPORT void
 glXCopyContext(Display * dpy, GLXContext source,
                GLXContext dest, unsigned long mask)
 {
+#ifdef GLX_USE_APPLEGL
+   GLXContext gc = __glXGetCurrentContext();
+   int errorcode;
+   bool x11error;
+
+   if(apple_glx_copy_context(gc->driContext, source->driContext, dest->driContext,
+                             mask, &errorcode, &x11error)) {
+      __glXSendError(dpy, errorcode, 0, X_GLXCopyContext, x11error);
+   }
+   
+#else
    xGLXCopyContextReq *req;
    GLXContext gc = __glXGetCurrentContext();
    GLXContextTag tag;
@@ -766,7 +831,7 @@ glXCopyContext(Display * dpy, GLXContext source,
       return;
    }
 
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
    if (gc->driContext) {
       /* NOT_DONE: This does not work yet */
    }
@@ -794,6 +859,7 @@ glXCopyContext(Display * dpy, GLXContext source,
    req->contextTag = tag;
    UnlockDisplay(dpy);
    SyncHandle();
+#endif /* GLX_USE_APPLEGL */
 }
 
 
@@ -851,7 +917,7 @@ __glXIsDirect(Display * dpy, GLXContextID contextID)
  * \c GLX_DIRECT_RENDERING is not defined?  Do we really need to bother with
  * the GLX protocol here at all?
  */
-PUBLIC Bool
+_X_EXPORT Bool
 glXIsDirect(Display * dpy, GLXContext gc)
 {
    if (!gc) {
@@ -860,12 +926,28 @@ glXIsDirect(Display * dpy, GLXContext gc)
    else if (GC_IS_DIRECT(gc)) {
       return GL_TRUE;
    }
+#ifdef GLX_USE_APPLEGL  /* TODO: indirect on darwin */
+      return GL_FALSE;
+#else
    return __glXIsDirect(dpy, gc->xid);
+#endif
 }
 
-PUBLIC GLXPixmap
+_X_EXPORT GLXPixmap
 glXCreateGLXPixmap(Display * dpy, XVisualInfo * vis, Pixmap pixmap)
 {
+#ifdef GLX_USE_APPLEGL
+   int screen = vis->screen;
+   __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, screen);
+   const struct glx_config *config;
+
+   config = _gl_context_modes_find_visual(psc->visuals, vis->visualid);
+   
+   if(apple_glx_pixmap_create(dpy, vis->screen, pixmap, config))
+      return None;
+   
+   return pixmap;
+#else
    xGLXCreateGLXPixmapReq *req;
    GLXPixmap xid;
    CARD8 opcode;
@@ -887,7 +969,7 @@ glXCreateGLXPixmap(Display * dpy, XVisualInfo * vis, Pixmap pixmap)
    UnlockDisplay(dpy);
    SyncHandle();
 
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
    do {
       /* FIXME: Maybe delay __DRIdrawable creation until the drawable
        * is actually bound to a context... */
@@ -895,19 +977,19 @@ glXCreateGLXPixmap(Display * dpy, XVisualInfo * vis, Pixmap pixmap)
       __GLXdisplayPrivate *const priv = __glXInitialize(dpy);
       __GLXDRIdrawable *pdraw;
       __GLXscreenConfigs *psc;
-      __GLcontextModes *modes;
+      struct glx_config *config;
 
-      psc = &priv->screenConfigs[vis->screen];
+      psc = priv->screenConfigs[vis->screen];
       if (psc->driScreen == NULL)
          break;
-      modes = _gl_context_modes_find_visual(psc->visuals, vis->visualid);
-      pdraw = psc->driScreen->createDrawable(psc, pixmap, req->glxpixmap, modes);
+      config = glx_config_find_visual(psc->visuals, vis->visualid);
+      pdraw = psc->driScreen->createDrawable(psc, pixmap, req->glxpixmap, config);
       if (pdraw == NULL) {
          fprintf(stderr, "failed to create pixmap\n");
          break;
       }
 
-      if (__glxHashInsert(psc->drawHash, req->glxpixmap, pdraw)) {
+      if (__glxHashInsert(priv->drawHash, req->glxpixmap, pdraw)) {
          (*pdraw->destroyDrawable) (pdraw);
          return None;           /* FIXME: Check what we're supposed to do here... */
       }
@@ -915,14 +997,19 @@ glXCreateGLXPixmap(Display * dpy, XVisualInfo * vis, Pixmap pixmap)
 #endif
 
    return xid;
+#endif
 }
 
 /*
 ** Destroy the named pixmap
 */
-PUBLIC void
+_X_EXPORT void
 glXDestroyGLXPixmap(Display * dpy, GLXPixmap glxpixmap)
 {
+#ifdef GLX_USE_APPLEGL
+   if(apple_glx_pixmap_destroy(dpy, glxpixmap))
+      __glXSendError(dpy, GLXBadPixmap, glxpixmap, X_GLXDestroyPixmap, false);
+#else
    xGLXDestroyGLXPixmapReq *req;
    CARD8 opcode;
 
@@ -940,24 +1027,31 @@ glXDestroyGLXPixmap(Display * dpy, GLXPixmap glxpixmap)
    UnlockDisplay(dpy);
    SyncHandle();
 
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
    {
-      int screen;
       __GLXdisplayPrivate *const priv = __glXInitialize(dpy);
-      __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, glxpixmap, &screen);
-      __GLXscreenConfigs *psc = &priv->screenConfigs[screen];
+      __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, glxpixmap);
 
       if (pdraw != NULL) {
          (*pdraw->destroyDrawable) (pdraw);
-         __glxHashDelete(psc->drawHash, glxpixmap);
+         __glxHashDelete(priv->drawHash, glxpixmap);
       }
    }
 #endif
+#endif /* GLX_USE_APPLEGL */
 }
 
-PUBLIC void
+_X_EXPORT void
 glXSwapBuffers(Display * dpy, GLXDrawable drawable)
 {
+#ifdef GLX_USE_APPLEGL
+   GLXContext gc = glXGetCurrentContext();
+   if(gc && apple_glx_is_current_drawable(dpy, gc->driContext, drawable)) {
+      apple_glx_swap_buffers(gc->driContext);
+   } else {
+      __glXSendError(dpy, GLXBadCurrentWindow, 0, X_GLXSwapBuffers, false);
+   }
+#else
    GLXContext gc;
    GLXContextTag tag;
    CARD8 opcode;
@@ -967,8 +1061,8 @@ glXSwapBuffers(Display * dpy, GLXDrawable drawable)
    xGLXSwapBuffersReq *req;
 #endif
 
-#ifdef GLX_DIRECT_RENDERING
-   __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, NULL);
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
+   __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
 
    if (pdraw != NULL) {
       glFlush();
@@ -1012,6 +1106,7 @@ glXSwapBuffers(Display * dpy, GLXDrawable drawable)
    SyncHandle();
    XFlush(dpy);
 #endif /* USE_XCB */
+#endif /* GLX_USE_APPLEGL */
 }
 
 
@@ -1019,22 +1114,22 @@ glXSwapBuffers(Display * dpy, GLXDrawable drawable)
 ** Return configuration information for the given display, screen and
 ** visual combination.
 */
-PUBLIC int
+_X_EXPORT int
 glXGetConfig(Display * dpy, XVisualInfo * vis, int attribute,
              int *value_return)
 {
    __GLXdisplayPrivate *priv;
    __GLXscreenConfigs *psc;
-   __GLcontextModes *modes;
+   struct glx_config *config;
    int status;
 
    status = GetGLXPrivScreenConfig(dpy, vis->screen, &priv, &psc);
    if (status == Success) {
-      modes = _gl_context_modes_find_visual(psc->visuals, vis->visualid);
+      config = glx_config_find_visual(psc->visuals, vis->visualid);
 
       /* Lookup attribute after first finding a match on the visual */
-      if (modes != NULL) {
-         return _gl_get_context_mode_data(modes, attribute, value_return);
+      if (config != NULL) {
+        return glx_config_get(config, attribute, value_return);
       }
 
       status = GLX_BAD_VISUAL;
@@ -1055,10 +1150,10 @@ glXGetConfig(Display * dpy, XVisualInfo * vis, int attribute,
 /************************************************************************/
 
 static void
-init_fbconfig_for_chooser(__GLcontextModes * config,
+init_fbconfig_for_chooser(struct glx_config * config,
                           GLboolean fbconfig_style_tags)
 {
-   memset(config, 0, sizeof(__GLcontextModes));
+   memset(config, 0, sizeof(struct glx_config));
    config->visualID = (XID) GLX_DONT_CARE;
    config->visualType = GLX_DONT_CARE;
 
@@ -1110,6 +1205,13 @@ init_fbconfig_for_chooser(__GLcontextModes * config,
     }                                           \
   } while ( 0 )
 
+/* Test that all bits from a are contained in b */
+#define MATCH_MASK(param)                      \
+  do {                                         \
+    if ((a->param & ~b->param) != 0)           \
+      return False;                            \
+  } while (0);
+
 /**
  * Determine if two GLXFBConfigs are compatible.
  *
@@ -1117,8 +1219,8 @@ init_fbconfig_for_chooser(__GLcontextModes * config,
  * \param b  Server specified config to test against \c a.
  */
 static Bool
-fbconfigs_compatible(const __GLcontextModes * const a,
-                     const __GLcontextModes * const b)
+fbconfigs_compatible(const struct glx_config * const a,
+                     const struct glx_config * const b)
 {
    MATCH_DONT_CARE(doubleBufferMode);
    MATCH_DONT_CARE(visualType);
@@ -1148,11 +1250,8 @@ fbconfigs_compatible(const __GLcontextModes * const a,
    MATCH_DONT_CARE(stereoMode);
    MATCH_EXACT(level);
 
-   if (((a->drawableType & b->drawableType) == 0)
-       || ((a->renderType & b->renderType) == 0)) {
-      return False;
-   }
-
+   MATCH_MASK(drawableType);
+   MATCH_MASK(renderType);
 
    /* There is a bug in a few of the XFree86 DDX drivers.  They contain
     * visuals with a "transparent type" of 0 when they really mean GLX_NONE.
@@ -1240,8 +1339,7 @@ fbconfigs_compatible(const __GLcontextModes * const a,
  * \sa qsort, glXChooseVisual, glXChooseFBConfig, glXChooseFBConfigSGIX
  */
 static int
-fbconfig_compare(const __GLcontextModes * const *const a,
-                 const __GLcontextModes * const *const b)
+fbconfig_compare(struct glx_config **a, struct glx_config **b)
 {
    /* The order of these comparisons must NOT change.  It is defined by
     * the GLX 1.3 spec and ARB_multisample.
@@ -1326,10 +1424,10 @@ fbconfig_compare(const __GLcontextModes * const *const a,
  * \sa glXChooseVisual, glXChooseFBConfig, glXChooseFBConfigSGIX
  */
 static int
-choose_visual(__GLcontextModes ** configs, int num_configs,
+choose_visual(struct glx_config ** configs, int num_configs,
               const int *attribList, GLboolean fbconfig_style_tags)
 {
-   __GLcontextModes test_config;
+   struct glx_config test_config;
    int base;
    int i;
 
@@ -1365,7 +1463,7 @@ choose_visual(__GLcontextModes ** configs, int num_configs,
     * specifications.
     */
 
-   qsort(configs, base, sizeof(__GLcontextModes *),
+   qsort(configs, base, sizeof(struct glx_config *),
          (int (*)(const void *, const void *)) fbconfig_compare);
    return base;
 }
@@ -1377,15 +1475,15 @@ choose_visual(__GLcontextModes ** configs, int num_configs,
 ** Return the visual that best matches the template.  Return None if no
 ** visual matches the template.
 */
-PUBLIC XVisualInfo *
+_X_EXPORT XVisualInfo *
 glXChooseVisual(Display * dpy, int screen, int *attribList)
 {
    XVisualInfo *visualList = NULL;
    __GLXdisplayPrivate *priv;
    __GLXscreenConfigs *psc;
-   __GLcontextModes test_config;
-   __GLcontextModes *modes;
-   const __GLcontextModes *best_config = NULL;
+   struct glx_config test_config;
+   struct glx_config *config;
+   struct glx_config *best_config = NULL;
 
    /*
     ** Get a list of all visuals, return if list is empty
@@ -1412,35 +1510,38 @@ glXChooseVisual(Display * dpy, int screen, int *attribList)
     ** Otherwise, create an XVisualInfo list with just the selected X visual
     ** and return this.
     */
-   for (modes = psc->visuals; modes != NULL; modes = modes->next) {
-      if (fbconfigs_compatible(&test_config, modes)
-          && ((best_config == NULL)
-              ||
-              (fbconfig_compare
-               ((const __GLcontextModes * const *const) &modes,
-                &best_config) < 0))) {
+   for (config = psc->visuals; config != NULL; config = config->next) {
+      if (fbconfigs_compatible(&test_config, config)
+          && ((best_config == NULL) ||
+              (fbconfig_compare (&config, &best_config) < 0))) {
          XVisualInfo visualTemplate;
          XVisualInfo *newList;
          int i;
 
          visualTemplate.screen = screen;
-         visualTemplate.visualid = modes->visualID;
+         visualTemplate.visualid = config->visualID;
          newList = XGetVisualInfo(dpy, VisualScreenMask | VisualIDMask,
                                   &visualTemplate, &i);
 
          if (newList) {
             Xfree(visualList);
             visualList = newList;
-            best_config = modes;
+            best_config = config;
          }
       }
    }
 
+#ifdef GLX_USE_APPLEGL
+   if(visualList && getenv("LIBGL_DUMP_VISUALID")) {
+      printf("visualid 0x%lx\n", visualList[0].visualid);
+   }
+#endif
+
    return visualList;
 }
 
 
-PUBLIC const char *
+_X_EXPORT const char *
 glXQueryExtensionsString(Display * dpy, int screen)
 {
    __GLXscreenConfigs *psc;
@@ -1458,7 +1559,7 @@ glXQueryExtensionsString(Display * dpy, int screen)
       }
 
       __glXCalculateUsableExtensions(psc,
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
                                      (psc->driScreen != NULL),
 #else
                                      GL_FALSE,
@@ -1469,7 +1570,7 @@ glXQueryExtensionsString(Display * dpy, int screen)
    return psc->effectiveGLXexts;
 }
 
-PUBLIC const char *
+_X_EXPORT const char *
 glXGetClientString(Display * dpy, int name)
 {
    (void) dpy;
@@ -1486,7 +1587,7 @@ glXGetClientString(Display * dpy, int name)
    }
 }
 
-PUBLIC const char *
+_X_EXPORT const char *
 glXQueryServerString(Display * dpy, int screen, int name)
 {
    __GLXscreenConfigs *psc;
@@ -1556,7 +1657,7 @@ __glXClientInfo(Display * dpy, int opcode)
 ** EXT_import_context
 */
 
-PUBLIC Display *
+_X_EXPORT Display *
 glXGetCurrentDisplay(void)
 {
    GLXContext gc = __glXGetCurrentContext();
@@ -1565,151 +1666,130 @@ glXGetCurrentDisplay(void)
    return gc->currentDpy;
 }
 
-PUBLIC
+_X_EXPORT
 GLX_ALIAS(Display *, glXGetCurrentDisplayEXT, (void), (),
           glXGetCurrentDisplay)
 
-/**
- * Used internally by libGL to send \c xGLXQueryContextinfoExtReq requests
- * to the X-server.
- *
- * \param dpy  Display where \c ctx was created.
- * \param ctx  Context to query.
- * \returns  \c Success on success.  \c GLX_BAD_CONTEXT if \c ctx is invalid,
- *           or zero if the request failed due to internal problems (i.e.,
- *           unable to allocate temporary memory, etc.)
- *
- * \note
- * This function dynamically determines whether to use the EXT_import_context
- * version of the protocol or the GLX 1.3 version of the protocol.
- */
-static int __glXQueryContextInfo(Display * dpy, GLXContext ctx)
+#ifndef GLX_USE_APPLEGL
+_X_EXPORT GLXContext
+glXImportContextEXT(Display *dpy, GLXContextID contextID)
 {
    __GLXdisplayPrivate *priv = __glXInitialize(dpy);
+   __GLXscreenConfigs *psc;
    xGLXQueryContextReply reply;
    CARD8 opcode;
-   GLuint numValues;
-   int retval;
+   GLXContext ctx;
+   int propList[__GLX_MAX_CONTEXT_PROPS * 2], *pProp, nPropListBytes;
+   int i, renderType;
+   XID share;
+   struct glx_config *mode;
+
+   if (contextID == None || __glXIsDirect(dpy, contextID))
+      return NULL;
 
-   if (ctx == NULL) {
-      return GLX_BAD_CONTEXT;
-   }
    opcode = __glXSetupForCommand(dpy);
-   if (!opcode) {
+   if (!opcode)
       return 0;
-   }
 
    /* Send the glXQueryContextInfoEXT request */
    LockDisplay(dpy);
 
-   if ((priv->majorVersion > 1) || (priv->minorVersion >= 3)) {
+   if (priv->majorVersion > 1 || priv->minorVersion >= 3) {
       xGLXQueryContextReq *req;
 
       GetReq(GLXQueryContext, req);
 
       req->reqType = opcode;
       req->glxCode = X_GLXQueryContext;
-      req->context = (unsigned int) (ctx->xid);
+      req->context = contextID;
    }
    else {
       xGLXVendorPrivateReq *vpreq;
       xGLXQueryContextInfoEXTReq *req;
 
       GetReqExtra(GLXVendorPrivate,
-                  sz_xGLXQueryContextInfoEXTReq - sz_xGLXVendorPrivateReq,
-                  vpreq);
+                 sz_xGLXQueryContextInfoEXTReq - sz_xGLXVendorPrivateReq,
+                 vpreq);
       req = (xGLXQueryContextInfoEXTReq *) vpreq;
       req->reqType = opcode;
       req->glxCode = X_GLXVendorPrivateWithReply;
       req->vendorCode = X_GLXvop_QueryContextInfoEXT;
-      req->context = (unsigned int) (ctx->xid);
+      req->context = contextID;
    }
 
    _XReply(dpy, (xReply *) & reply, 0, False);
 
-   numValues = reply.n;
-   if (numValues == 0)
-      retval = Success;
-   else if (numValues > __GLX_MAX_CONTEXT_PROPS)
-      retval = 0;
-   else {
-      int *propList, *pProp;
-      int nPropListBytes;
-
-      nPropListBytes = numValues << 3;
-      propList = (int *) Xmalloc(nPropListBytes);
-      if (NULL == propList) {
-         retval = 0;
-      }
-      else {
-        unsigned i;
-
-         _XRead(dpy, (char *) propList, nPropListBytes);
-         pProp = propList;
-         for (i = 0; i < numValues; i++) {
-            switch (*pProp++) {
-            case GLX_SHARE_CONTEXT_EXT:
-               ctx->share_xid = *pProp++;
-               break;
-            case GLX_VISUAL_ID_EXT:
-               ctx->mode =
-                  _gl_context_modes_find_visual(ctx->psc->visuals, *pProp++);
-               break;
-            case GLX_SCREEN:
-               ctx->screen = *pProp++;
-               break;
-            case GLX_FBCONFIG_ID:
-               ctx->mode =
-                  _gl_context_modes_find_fbconfig(ctx->psc->configs,
-                                                  *pProp++);
-               break;
-            case GLX_RENDER_TYPE:
-               ctx->renderType = *pProp++;
-               break;
-            default:
-               pProp++;
-               continue;
-            }
-         }
-         Xfree((char *) propList);
-         retval = Success;
-      }
-   }
+   if (reply.n <= __GLX_MAX_CONTEXT_PROPS)
+      nPropListBytes = reply.n * 2 * sizeof propList[0];
+   else
+      nPropListBytes = 0;
+   _XRead(dpy, (char *) propList, nPropListBytes);
    UnlockDisplay(dpy);
    SyncHandle();
-   return retval;
-}
 
-PUBLIC int
-glXQueryContext(Display * dpy, GLXContext ctx, int attribute, int *value)
-{
-   int retVal;
+   /* Look up screen first so we can look up visuals/fbconfigs later */
+   psc = NULL;
+   for (i = 0, pProp = propList; i < reply.n; i++, pProp += 2)
+      if (pProp[0] == GLX_SCREEN)
+        psc = GetGLXScreenConfigs(dpy, pProp[1]);
+   if (psc == NULL)
+      return NULL;
+
+   share = None;
+   mode = NULL;
+   renderType = 0;
+   pProp = propList;
+
+   for (i = 0, pProp = propList; i < reply.n; i++, pProp += 2)
+      switch (pProp[0]) {
+      case GLX_SHARE_CONTEXT_EXT:
+        share = pProp[1];
+        break;
+      case GLX_VISUAL_ID_EXT:
+        mode = glx_config_find_visual(psc->visuals, pProp[1]);
+        break;
+      case GLX_FBCONFIG_ID:
+        mode = glx_config_find_fbconfig(psc->configs, pProp[1]);
+        break;
+      case GLX_RENDER_TYPE:
+        renderType = pProp[1];
+        break;
+      }
+
+   if (mode == NULL)
+      return NULL;
+
+   ctx = indirect_create_context(psc, mode, NULL, renderType);
+   if (ctx == NULL)
+      return NULL;
+
+   ctx->xid = contextID;
+   ctx->imported = GL_TRUE;
+   ctx->share_xid = share;
+
+   return ctx;
+}
 
-   /* get the information from the server if we don't have it already */
-#ifdef GLX_DIRECT_RENDERING
-   if (!ctx->driContext && (ctx->mode == NULL)) {
-#else
-   if (ctx->mode == NULL) {
 #endif
-      retVal = __glXQueryContextInfo(dpy, ctx);
-      if (Success != retVal)
-         return retVal;
-   }
+
+_X_EXPORT int
+glXQueryContext(Display * dpy, GLXContext ctx, int attribute, int *value)
+{
    switch (attribute) {
-   case GLX_SHARE_CONTEXT_EXT:
-      *value = (int) (ctx->share_xid);
+      case GLX_SHARE_CONTEXT_EXT:
+      *value = ctx->share_xid;
       break;
    case GLX_VISUAL_ID_EXT:
-      *value = ctx->mode ? ctx->mode->visualID : None;
+      *value = ctx->config ? ctx->config->visualID : None;
       break;
    case GLX_SCREEN:
-      *value = (int) (ctx->screen);
+      *value = ctx->screen;
       break;
    case GLX_FBCONFIG_ID:
-      *value = ctx->mode ? ctx->mode->fbconfigID : None;
+      *value = ctx->config ? ctx->config->fbconfigID : None;
       break;
    case GLX_RENDER_TYPE:
-      *value = (int) (ctx->renderType);
+      *value = ctx->renderType;
       break;
    default:
       return GLX_BAD_ATTRIBUTE;
@@ -1717,62 +1797,32 @@ glXQueryContext(Display * dpy, GLXContext ctx, int attribute, int *value)
    return Success;
 }
 
-PUBLIC
+_X_EXPORT
 GLX_ALIAS(int, glXQueryContextInfoEXT,
           (Display * dpy, GLXContext ctx, int attribute, int *value),
           (dpy, ctx, attribute, value), glXQueryContext)
 
-PUBLIC GLXContextID glXGetContextIDEXT(const GLXContext ctx)
+_X_EXPORT GLXContextID glXGetContextIDEXT(const GLXContext ctx)
 {
    return ctx->xid;
 }
 
-PUBLIC GLXContext
-glXImportContextEXT(Display * dpy, GLXContextID contextID)
-{
-   GLXContext ctx;
-
-   if (contextID == None) {
-      return NULL;
-   }
-   if (__glXIsDirect(dpy, contextID)) {
-      return NULL;
-   }
-
-   ctx = AllocateGLXContext(dpy);
-   if (NULL != ctx) {
-      ctx->xid = contextID;
-      ctx->imported = GL_TRUE;
-
-      if (Success != __glXQueryContextInfo(dpy, ctx)) {
-        __glXFreeContext(ctx);
-        ctx = NULL;
-      }
-   }
-   return ctx;
-}
-
-PUBLIC void
+_X_EXPORT void
 glXFreeContextEXT(Display * dpy, GLXContext ctx)
 {
    DestroyContext(dpy, ctx);
 }
 
 
-
-/*
- * GLX 1.3 functions - these are just stubs for now!
- */
-
-PUBLIC GLXFBConfig *
+_X_EXPORT GLXFBConfig *
 glXChooseFBConfig(Display * dpy, int screen,
                   const int *attribList, int *nitems)
 {
-   __GLcontextModes **config_list;
+   struct glx_config **config_list;
    int list_size;
 
 
-   config_list = (__GLcontextModes **)
+   config_list = (struct glx_config **)
       glXGetFBConfigs(dpy, screen, &list_size);
 
    if ((config_list != NULL) && (list_size > 0) && (attribList != NULL)) {
@@ -1788,20 +1838,19 @@ glXChooseFBConfig(Display * dpy, int screen,
 }
 
 
-PUBLIC GLXContext
-glXCreateNewContext(Display * dpy, GLXFBConfig config,
+_X_EXPORT GLXContext
+glXCreateNewContext(Display * dpy, GLXFBConfig fbconfig,
                     int renderType, GLXContext shareList, Bool allowDirect)
 {
-   const __GLcontextModes *const fbconfig =
-      (const __GLcontextModes *const) config;
+   struct glx_config *config = (struct glx_config *) fbconfig;
 
-   return CreateContext(dpy, fbconfig->fbconfigID, fbconfig, shareList,
-                        allowDirect, X_GLXCreateNewContext, renderType,
-                       fbconfig->screen);
+   return CreateContext(dpy, config->fbconfigID, config, shareList,
+                       allowDirect, X_GLXCreateNewContext, renderType,
+                       config->screen);
 }
 
 
-PUBLIC GLXDrawable
+_X_EXPORT GLXDrawable
 glXGetCurrentReadDrawable(void)
 {
    GLXContext gc = __glXGetCurrentContext();
@@ -1809,75 +1858,75 @@ glXGetCurrentReadDrawable(void)
 }
 
 
-PUBLIC GLXFBConfig *
+_X_EXPORT GLXFBConfig *
 glXGetFBConfigs(Display * dpy, int screen, int *nelements)
 {
    __GLXdisplayPrivate *priv = __glXInitialize(dpy);
-   __GLcontextModes **config = NULL;
+   struct glx_config **config_list = NULL;
+   struct glx_config *config;
+   unsigned num_configs = 0;
    int i;
 
    *nelements = 0;
    if (priv && (priv->screenConfigs != NULL)
        && (screen >= 0) && (screen <= ScreenCount(dpy))
-       && (priv->screenConfigs[screen].configs != NULL)
-       && (priv->screenConfigs[screen].configs->fbconfigID
+       && (priv->screenConfigs[screen]->configs != NULL)
+       && (priv->screenConfigs[screen]->configs->fbconfigID
           != (int) GLX_DONT_CARE)) {
-      unsigned num_configs = 0;
-      __GLcontextModes *modes;
 
-
-      for (modes = priv->screenConfigs[screen].configs; modes != NULL;
-           modes = modes->next) {
-         if (modes->fbconfigID != (int) GLX_DONT_CARE) {
+      for (config = priv->screenConfigs[screen]->configs; config != NULL;
+           config = config->next) {
+         if (config->fbconfigID != (int) GLX_DONT_CARE) {
             num_configs++;
          }
       }
 
-      config = (__GLcontextModes **) Xmalloc(sizeof(__GLcontextModes *)
-                                             * num_configs);
-      if (config != NULL) {
+      config_list = Xmalloc(num_configs * sizeof *config_list);
+      if (config_list != NULL) {
          *nelements = num_configs;
          i = 0;
-         for (modes = priv->screenConfigs[screen].configs; modes != NULL;
-              modes = modes->next) {
-            if (modes->fbconfigID != (int) GLX_DONT_CARE) {
-               config[i] = modes;
+         for (config = priv->screenConfigs[screen]->configs; config != NULL;
+              config = config->next) {
+            if (config->fbconfigID != (int) GLX_DONT_CARE) {
+               config_list[i] = config;
                i++;
             }
          }
       }
    }
-   return (GLXFBConfig *) config;
+
+   return (GLXFBConfig *) config_list;
 }
 
 
-PUBLIC int
-glXGetFBConfigAttrib(Display * dpy, GLXFBConfig config,
+_X_EXPORT int
+glXGetFBConfigAttrib(Display * dpy, GLXFBConfig fbconfig,
                      int attribute, int *value)
 {
-   __GLcontextModes *const modes = ValidateGLXFBConfig(dpy, config);
+   struct glx_config *config = ValidateGLXFBConfig(dpy, fbconfig);
+
+   if (config == NULL)
+      return GLXBadFBConfig;
 
-   return (modes != NULL)
-      ? _gl_get_context_mode_data(modes, attribute, value)
-      : GLXBadFBConfig;
+   return glx_config_get(config, attribute, value);
 }
 
 
-PUBLIC XVisualInfo *
-glXGetVisualFromFBConfig(Display * dpy, GLXFBConfig config)
+_X_EXPORT XVisualInfo *
+glXGetVisualFromFBConfig(Display * dpy, GLXFBConfig fbconfig)
 {
    XVisualInfo visualTemplate;
-   __GLcontextModes *fbconfig = (__GLcontextModes *) config;
+   struct glx_config *config = (struct glx_config *) fbconfig;
    int count;
 
    /*
     ** Get a list of all visuals, return if list is empty
     */
-   visualTemplate.visualid = fbconfig->visualID;
+   visualTemplate.visualid = config->visualID;
    return XGetVisualInfo(dpy, VisualIDMask, &visualTemplate, &count);
 }
 
-
+#ifndef GLX_USE_APPLEGL
 /*
 ** GLX_SGI_swap_control
 */
@@ -1899,31 +1948,16 @@ __glXSwapIntervalSGI(int interval)
       return GLX_BAD_VALUE;
    }
 
-#ifdef __DRI_SWAP_CONTROL
-   if (gc->driContext) {
-      __GLXscreenConfigs * const psc = GetGLXScreenConfigs( gc->currentDpy,
-                                                           gc->screen );
-      __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(gc->currentDpy,
-                                                 gc->currentDrawable,
-                                                 NULL);
-      if (psc->swapControl != NULL && pdraw != NULL) {
-        psc->swapControl->setSwapInterval(pdraw->driDrawable, interval);
-        return 0;
-      }
-      else if (pdraw == NULL) {
-        return GLX_BAD_CONTEXT;
-      }
-   }
-#endif
    psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
 
+#ifdef GLX_DIRECT_RENDERING
    if (gc->driContext && psc->driScreen && psc->driScreen->setSwapInterval) {
-      __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(gc->currentDpy,
-                                                 gc->currentDrawable,
-                                                 NULL);
+      __GLXDRIdrawable *pdraw =
+        GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
       psc->driScreen->setSwapInterval(pdraw, interval);
       return 0;
    }
+#endif
 
    dpy = gc->currentDpy;
    opcode = __glXSetupForCommand(dpy);
@@ -1956,35 +1990,20 @@ __glXSwapIntervalSGI(int interval)
 static int
 __glXSwapIntervalMESA(unsigned int interval)
 {
+#ifdef GLX_DIRECT_RENDERING
    GLXContext gc = __glXGetCurrentContext();
 
-#ifdef __DRI_SWAP_CONTROL
-   if (gc != NULL && gc->driContext) {
-      __GLXscreenConfigs *const psc = GetGLXScreenConfigs(gc->currentDpy,
-                                                          gc->screen);
-
-      if ((psc != NULL) && (psc->driScreen != NULL)) {
-         __GLXDRIdrawable *pdraw =
-            GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
-         if (psc->swapControl != NULL && pdraw != NULL) {
-            psc->swapControl->setSwapInterval(pdraw->driDrawable, interval);
-            return 0;
-         }
-      }
-   }
-#endif
-
    if (gc != NULL && gc->driContext) {
       __GLXscreenConfigs *psc;
 
       psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
       if (psc->driScreen && psc->driScreen->setSwapInterval) {
-         __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(gc->currentDpy,
-                                                    gc->currentDrawable, NULL);
-        psc->driScreen->setSwapInterval(pdraw, interval);
-        return 0;
+         __GLXDRIdrawable *pdraw =
+           GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
+        return psc->driScreen->setSwapInterval(pdraw, interval);
       }
    }
+#endif
 
    return GLX_BAD_CONTEXT;
 }
@@ -1993,135 +2012,22 @@ __glXSwapIntervalMESA(unsigned int interval)
 static int
 __glXGetSwapIntervalMESA(void)
 {
-#ifdef __DRI_SWAP_CONTROL
+#ifdef GLX_DIRECT_RENDERING
    GLXContext gc = __glXGetCurrentContext();
 
-   if (gc != NULL && gc->driContext) {
-      __GLXscreenConfigs *const psc = GetGLXScreenConfigs(gc->currentDpy,
-                                                          gc->screen);
-
-      if ((psc != NULL) && (psc->driScreen != NULL)) {
-         __GLXDRIdrawable *pdraw =
-            GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
-         if (psc->swapControl != NULL && pdraw != NULL) {
-            return psc->swapControl->getSwapInterval(pdraw->driDrawable);
-         }
-      }
-   }
-#endif
    if (gc != NULL && gc->driContext) {
       __GLXscreenConfigs *psc;
 
       psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
       if (psc->driScreen && psc->driScreen->getSwapInterval) {
-         __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(gc->currentDpy,
-                                                    gc->currentDrawable, NULL);
+         __GLXDRIdrawable *pdraw =
+           GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
         return psc->driScreen->getSwapInterval(pdraw);
       }
    }
-
-   return 0;
-}
-
-
-/*
-** GLX_MESA_swap_frame_usage
-*/
-
-static GLint
-__glXBeginFrameTrackingMESA(Display * dpy, GLXDrawable drawable)
-{
-   int status = GLX_BAD_CONTEXT;
-#ifdef __DRI_FRAME_TRACKING
-   int screen = 0;
-   __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
-   __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, screen);
-
-   if (pdraw != NULL && psc->frameTracking != NULL)
-      status = psc->frameTracking->frameTracking(pdraw->driDrawable, GL_TRUE);
-#else
-   (void) dpy;
-   (void) drawable;
 #endif
-   return status;
-}
-
-
-static GLint
-__glXEndFrameTrackingMESA(Display * dpy, GLXDrawable drawable)
-{
-   int status = GLX_BAD_CONTEXT;
-#ifdef __DRI_FRAME_TRACKING
-   int screen = 0;
-   __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
-   __GLXscreenConfigs *psc = GetGLXScreenConfigs(dpy, screen);
 
-   if (pdraw != NULL && psc->frameTracking != NULL)
-      status = psc->frameTracking->frameTracking(pdraw->driDrawable,
-                                                 GL_FALSE);
-#else
-   (void) dpy;
-   (void) drawable;
-#endif
-   return status;
-}
-
-
-static GLint
-__glXGetFrameUsageMESA(Display * dpy, GLXDrawable drawable, GLfloat * usage)
-{
-   int status = GLX_BAD_CONTEXT;
-#ifdef __DRI_FRAME_TRACKING
-   int screen = 0;
-   __GLXDRIdrawable *const pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
-   __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, screen);
-
-   if (pdraw != NULL && psc->frameTracking != NULL) {
-      int64_t sbc, missedFrames;
-      float lastMissedUsage;
-
-      status = psc->frameTracking->queryFrameTracking(pdraw->driDrawable,
-                                                      &sbc,
-                                                      &missedFrames,
-                                                      &lastMissedUsage,
-                                                      usage);
-   }
-#else
-   (void) dpy;
-   (void) drawable;
-   (void) usage;
-#endif
-   return status;
-}
-
-
-static GLint
-__glXQueryFrameTrackingMESA(Display * dpy, GLXDrawable drawable,
-                            int64_t * sbc, int64_t * missedFrames,
-                            GLfloat * lastMissedUsage)
-{
-   int status = GLX_BAD_CONTEXT;
-#ifdef __DRI_FRAME_TRACKING
-   int screen = 0;
-   __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
-   __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, screen);
-
-   if (pdraw != NULL && psc->frameTracking != NULL) {
-      float usage;
-
-      status = psc->frameTracking->queryFrameTracking(pdraw->driDrawable,
-                                                      sbc, missedFrames,
-                                                      lastMissedUsage,
-                                                      &usage);
-   }
-#else
-   (void) dpy;
-   (void) drawable;
-   (void) sbc;
-   (void) missedFrames;
-   (void) lastMissedUsage;
-#endif
-   return status;
+   return 0;
 }
 
 
@@ -2135,32 +2041,34 @@ __glXGetVideoSyncSGI(unsigned int *count)
    int ret;
    GLXContext gc = __glXGetCurrentContext();
    __GLXscreenConfigs *psc;
+#ifdef GLX_DIRECT_RENDERING
    __GLXDRIdrawable *pdraw;
+#endif
 
-   if (!gc || !gc->driContext)
+   if (!gc)
       return GLX_BAD_CONTEXT;
 
+#ifdef GLX_DIRECT_RENDERING
+   if (!gc->driContext)
+      return GLX_BAD_CONTEXT;
+#endif
+
    psc = GetGLXScreenConfigs(gc->currentDpy, gc->screen);
-   pdraw = GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
+#ifdef GLX_DIRECT_RENDERING
+   pdraw = GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
+#endif
 
    /* FIXME: Looking at the GLX_SGI_video_sync spec in the extension registry,
     * FIXME: there should be a GLX encoding for this call.  I can find no
     * FIXME: documentation for the GLX encoding.
     */
-#ifdef __DRI_MEDIA_STREAM_COUNTER
-   if ( psc->msc && psc->driScreen ) {
-      ret = (*psc->msc->getDrawableMSC)(psc->__driScreen,
-                                       pdraw->driDrawable, &msc);
-      *count = (unsigned) msc;
-
-      return (ret == 0) ? 0 : GLX_BAD_CONTEXT;
-   }
-#endif
+#ifdef GLX_DIRECT_RENDERING
    if (psc->driScreen && psc->driScreen->getDrawableMSC) {
       ret = psc->driScreen->getDrawableMSC(psc, pdraw, &ust, &msc, &sbc);
       *count = (unsigned) msc;
       return (ret == True) ? 0 : GLX_BAD_CONTEXT;
    }
+#endif
 
    return GLX_BAD_CONTEXT;
 }
@@ -2170,37 +2078,41 @@ __glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
 {
    GLXContext gc = __glXGetCurrentContext();
    __GLXscreenConfigs *psc;
+#ifdef GLX_DIRECT_RENDERING
    __GLXDRIdrawable *pdraw;
+#endif
    int64_t ust, msc, sbc;
    int ret;
 
    if (divisor <= 0 || remainder < 0)
       return GLX_BAD_VALUE;
 
-   if (!gc || !gc->driContext)
+   if (!gc)
       return GLX_BAD_CONTEXT;
 
-   psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
-   pdraw = GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
+#ifdef GLX_DIRECT_RENDERING
+   if (!gc->driContext)
+      return GLX_BAD_CONTEXT;
+#endif
 
-#ifdef __DRI_MEDIA_STREAM_COUNTER
-   if (psc->msc != NULL && psc->driScreen ) {
-      ret = (*psc->msc->waitForMSC)(pdraw->driDrawable, 0,
-                                   divisor, remainder, &msc, &sbc);
-      *count = (unsigned) msc;
-      return (ret == 0) ? 0 : GLX_BAD_CONTEXT;
-   }
+   psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
+#ifdef GLX_DIRECT_RENDERING
+   pdraw = GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
 #endif
+
+#ifdef GLX_DIRECT_RENDERING
    if (psc->driScreen && psc->driScreen->waitForMSC) {
       ret = psc->driScreen->waitForMSC(pdraw, 0, divisor, remainder, &ust, &msc,
                                       &sbc);
       *count = (unsigned) msc;
       return (ret == True) ? 0 : GLX_BAD_CONTEXT;
    }
+#endif
 
    return GLX_BAD_CONTEXT;
 }
 
+#endif /* GLX_USE_APPLEGL */
 
 /*
 ** GLX_SGIX_fbconfig
@@ -2208,38 +2120,45 @@ __glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
 ** GLX_functions table.
 */
 
-PUBLIC
+_X_EXPORT
 GLX_ALIAS(int, glXGetFBConfigAttribSGIX,
           (Display * dpy, GLXFBConfigSGIX config, int attribute, int *value),
           (dpy, config, attribute, value), glXGetFBConfigAttrib)
 
-PUBLIC GLX_ALIAS(GLXFBConfigSGIX *, glXChooseFBConfigSGIX,
+_X_EXPORT GLX_ALIAS(GLXFBConfigSGIX *, glXChooseFBConfigSGIX,
                  (Display * dpy, int screen, int *attrib_list,
                   int *nelements), (dpy, screen, attrib_list, nelements),
                  glXChooseFBConfig)
 
-PUBLIC GLX_ALIAS(XVisualInfo *, glXGetVisualFromFBConfigSGIX,
+_X_EXPORT GLX_ALIAS(XVisualInfo *, glXGetVisualFromFBConfigSGIX,
                  (Display * dpy, GLXFBConfigSGIX config),
                  (dpy, config), glXGetVisualFromFBConfig)
 
-PUBLIC GLXPixmap
+_X_EXPORT GLXPixmap
 glXCreateGLXPixmapWithConfigSGIX(Display * dpy,
-                                 GLXFBConfigSGIX config,
+                                 GLXFBConfigSGIX fbconfig,
                                  Pixmap pixmap)
 {
+#ifndef GLX_USE_APPLEGL
    xGLXVendorPrivateWithReplyReq *vpreq;
    xGLXCreateGLXPixmapWithConfigSGIXReq *req;
    GLXPixmap xid = None;
    CARD8 opcode;
-   const __GLcontextModes *const fbconfig = (__GLcontextModes *) config;
    __GLXscreenConfigs *psc;
+#endif
+   struct glx_config *config = (struct glx_config *) fbconfig;
 
 
    if ((dpy == NULL) || (config == NULL)) {
       return None;
    }
+#ifdef GLX_USE_APPLEGL
+   if(apple_glx_pixmap_create(dpy, config->screen, pixmap, config))
+      return None;
+   return pixmap;
+#else
 
-   psc = GetGLXScreenConfigs(dpy, fbconfig->screen);
+   psc = GetGLXScreenConfigs(dpy, config->screen);
    if ((psc != NULL)
        && __glXExtensionBitIsEnabled(psc, SGIX_fbconfig_bit)) {
       opcode = __glXSetupForCommand(dpy);
@@ -2256,8 +2175,8 @@ glXCreateGLXPixmapWithConfigSGIX(Display * dpy,
       req->reqType = opcode;
       req->glxCode = X_GLXVendorPrivateWithReply;
       req->vendorCode = X_GLXvop_CreateGLXPixmapWithConfigSGIX;
-      req->screen = fbconfig->screen;
-      req->fbconfig = fbconfig->fbconfigID;
+      req->screen = config->screen;
+      req->fbconfig = config->fbconfigID;
       req->pixmap = pixmap;
       req->glxpixmap = xid = XAllocID(dpy);
       UnlockDisplay(dpy);
@@ -2265,15 +2184,16 @@ glXCreateGLXPixmapWithConfigSGIX(Display * dpy,
    }
 
    return xid;
+#endif
 }
 
-PUBLIC GLXContext
+_X_EXPORT GLXContext
 glXCreateContextWithConfigSGIX(Display * dpy,
-                               GLXFBConfigSGIX config, int renderType,
+                               GLXFBConfigSGIX fbconfig, int renderType,
                                GLXContext shareList, Bool allowDirect)
 {
    GLXContext gc = NULL;
-   const __GLcontextModes *const fbconfig = (__GLcontextModes *) config;
+   struct glx_config *config = (struct glx_config *) fbconfig;
    __GLXscreenConfigs *psc;
 
 
@@ -2281,20 +2201,20 @@ glXCreateContextWithConfigSGIX(Display * dpy,
       return None;
    }
 
-   psc = GetGLXScreenConfigs(dpy, fbconfig->screen);
+   psc = GetGLXScreenConfigs(dpy, config->screen);
    if ((psc != NULL)
        && __glXExtensionBitIsEnabled(psc, SGIX_fbconfig_bit)) {
-      gc = CreateContext(dpy, fbconfig->fbconfigID, fbconfig, shareList,
+      gc = CreateContext(dpy, config->fbconfigID, config, shareList,
                          allowDirect,
                         X_GLXvop_CreateContextWithConfigSGIX, renderType,
-                        fbconfig->screen);
+                        config->screen);
    }
 
    return gc;
 }
 
 
-PUBLIC GLXFBConfigSGIX
+_X_EXPORT GLXFBConfigSGIX
 glXGetFBConfigFromVisualSGIX(Display * dpy, XVisualInfo * vis)
 {
    __GLXdisplayPrivate *priv;
@@ -2303,14 +2223,14 @@ glXGetFBConfigFromVisualSGIX(Display * dpy, XVisualInfo * vis)
    if ((GetGLXPrivScreenConfig(dpy, vis->screen, &priv, &psc) != Success)
        && __glXExtensionBitIsEnabled(psc, SGIX_fbconfig_bit)
        && (psc->configs->fbconfigID != (int) GLX_DONT_CARE)) {
-      return (GLXFBConfigSGIX) _gl_context_modes_find_visual(psc->configs,
-                                                             vis->visualid);
+      return (GLXFBConfigSGIX) glx_config_find_visual(psc->configs,
+                                                     vis->visualid);
    }
 
    return NULL;
 }
 
-
+#ifndef GLX_USE_APPLEGL
 /*
 ** GLX_SGIX_swap_group
 */
@@ -2353,44 +2273,37 @@ __glXGetSyncValuesOML(Display * dpy, GLXDrawable drawable,
                       int64_t * ust, int64_t * msc, int64_t * sbc)
 {
    __GLXdisplayPrivate * const priv = __glXInitialize(dpy);
-   int i, ret;
+   int ret;
+#ifdef GLX_DIRECT_RENDERING
    __GLXDRIdrawable *pdraw;
+#endif
    __GLXscreenConfigs *psc;
 
    if (!priv)
       return False;
 
-   pdraw = GetGLXDRIDrawable(dpy, drawable, &i);
-   psc = &priv->screenConfigs[i];
-
-#if defined(__DRI_SWAP_BUFFER_COUNTER) && defined(__DRI_MEDIA_STREAM_COUNTER)
-   if (pdraw && psc->sbc && psc->msc)
-      return ( (pdraw && psc->sbc && psc->msc)
-              && ((*psc->msc->getMSC)(psc->driScreen, msc) == 0)
-              && ((*psc->sbc->getSBC)(pdraw->driDrawable, sbc) == 0)
-              && (__glXGetUST(ust) == 0) );
-#endif
-   if (pdraw && psc && psc->driScreen && psc->driScreen->getDrawableMSC) {
+#ifdef GLX_DIRECT_RENDERING
+   pdraw = GetGLXDRIDrawable(dpy, drawable);
+   psc = pdraw ? pdraw->psc : NULL;
+   if (pdraw && psc->driScreen->getDrawableMSC) {
       ret = psc->driScreen->getDrawableMSC(psc, pdraw, ust, msc, sbc);
       return ret;
    }
+#endif
 
    return False;
 }
 
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
 _X_HIDDEN GLboolean
-__driGetMscRateOML(__DRIdrawable * draw,
-                   int32_t * numerator, int32_t * denominator, void *private)
+__glxGetMscRate(__GLXDRIdrawable *glxDraw,
+               int32_t * numerator, int32_t * denominator)
 {
 #ifdef XF86VIDMODE
    __GLXscreenConfigs *psc;
    XF86VidModeModeLine mode_line;
    int dot_clock;
    int i;
-   __GLXDRIdrawable *glxDraw = private;
-
-   (void) draw;
 
    psc = glxDraw->psc;
    if (XF86VidModeQueryVersion(psc->dpy, &i, &i) &&
@@ -2469,12 +2382,12 @@ __glXGetMscRateOML(Display * dpy, GLXDrawable drawable,
                    int32_t * numerator, int32_t * denominator)
 {
 #if defined( GLX_DIRECT_RENDERING ) && defined( XF86VIDMODE )
-   __GLXDRIdrawable *draw = GetGLXDRIDrawable(dpy, drawable, NULL);
+   __GLXDRIdrawable *draw = GetGLXDRIDrawable(dpy, drawable);
 
    if (draw == NULL)
       return False;
 
-   return __driGetMscRateOML(draw->driDrawable, numerator, denominator, draw);
+   return __glxGetMscRate(draw, numerator, denominator);
 #else
    (void) dpy;
    (void) drawable;
@@ -2490,12 +2403,18 @@ __glXSwapBuffersMscOML(Display * dpy, GLXDrawable drawable,
                        int64_t target_msc, int64_t divisor, int64_t remainder)
 {
    GLXContext gc = __glXGetCurrentContext();
-   int screen;
-   __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
-   __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, screen);
+#ifdef GLX_DIRECT_RENDERING
+   __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
+   __GLXscreenConfigs *psc = pdraw ? pdraw->psc : NULL;
+#endif
+
+   if (!gc) /* no GLX for this */
+      return -1;
 
-   if (!pdraw || !gc || !gc->driContext) /* no GLX for this */
+#ifdef GLX_DIRECT_RENDERING
+   if (!pdraw || !gc->driContext)
       return -1;
+#endif
 
    /* The OML_sync_control spec says these should "generate a GLX_BAD_VALUE
     * error", but it also says "It [glXSwapBuffersMscOML] will return a value
@@ -2510,12 +2429,6 @@ __glXSwapBuffersMscOML(Display * dpy, GLXDrawable drawable,
    if (target_msc == 0 && divisor == 0 && remainder == 0)
       remainder = 1;
 
-#ifdef __DRI_SWAP_BUFFER_COUNTER
-   if (psc->counters != NULL)
-      return (*psc->sbc->swapBuffersMSC)(pdraw->driDrawable, target_msc,
-                                        divisor, remainder);
-#endif
-
 #ifdef GLX_DIRECT_RENDERING
    if (psc->driScreen && psc->driScreen->swapBuffers)
       return (*psc->driScreen->swapBuffers)(pdraw, target_msc, divisor,
@@ -2532,9 +2445,10 @@ __glXWaitForMscOML(Display * dpy, GLXDrawable drawable,
                    int64_t remainder, int64_t * ust,
                    int64_t * msc, int64_t * sbc)
 {
-   int screen;
-   __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
-   __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, screen );
+#ifdef GLX_DIRECT_RENDERING
+   __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
+#endif
+   __GLXscreenConfigs *psc = pdraw ? pdraw->psc : NULL;
    int ret;
 
 
@@ -2546,22 +2460,13 @@ __glXWaitForMscOML(Display * dpy, GLXDrawable drawable,
    if (divisor > 0 && remainder >= divisor)
       return False;
 
-#ifdef __DRI_MEDIA_STREAM_COUNTER
-   if (pdraw != NULL && psc->msc != NULL) {
-      ret = (*psc->msc->waitForMSC) (pdraw->driDrawable, target_msc,
-                                     divisor, remainder, msc, sbc);
-
-      /* __glXGetUST returns zero on success and non-zero on failure.
-       * This function returns True on success and False on failure.
-       */
-      return ((ret == 0) && (__glXGetUST(ust) == 0));
-   }
-#endif
+#ifdef GLX_DIRECT_RENDERING
    if (pdraw && psc->driScreen && psc->driScreen->waitForMSC) {
       ret = psc->driScreen->waitForMSC(pdraw, target_msc, divisor, remainder,
                                       ust, msc, sbc);
       return ret;
    }
+#endif
 
    return False;
 }
@@ -2572,9 +2477,10 @@ __glXWaitForSbcOML(Display * dpy, GLXDrawable drawable,
                    int64_t target_sbc, int64_t * ust,
                    int64_t * msc, int64_t * sbc)
 {
-   int screen;
-   __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
-   __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, screen);
+#ifdef GLX_DIRECT_RENDERING
+   __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
+#endif
+   __GLXscreenConfigs *psc = pdraw ? pdraw->psc : NULL;
    int ret;
 
    /* The OML_sync_control spec says this should "generate a GLX_BAD_VALUE
@@ -2582,88 +2488,15 @@ __glXWaitForSbcOML(Display * dpy, GLXDrawable drawable,
     */
    if (target_sbc < 0)
       return False;
-#ifdef __DRI_SWAP_BUFFER_COUNTER
-   if (pdraw != NULL && psc->sbc != NULL) {
-      ret =
-         (*psc->sbc->waitForSBC) (pdraw->driDrawable, target_sbc, msc, sbc);
 
-      /* __glXGetUST returns zero on success and non-zero on failure.
-       * This function returns True on success and False on failure.
-       */
-      return ((ret == 0) && (__glXGetUST(ust) == 0));
-   }
-#endif
+#ifdef GLX_DIRECT_RENDERING
    if (pdraw && psc->driScreen && psc->driScreen->waitForSBC) {
       ret = psc->driScreen->waitForSBC(pdraw, target_sbc, ust, msc, sbc);
       return ret;
    }
-   return False;
-}
-
-
-/**
- * GLX_MESA_allocate_memory
- */
-/*@{*/
-
-PUBLIC void *
-glXAllocateMemoryMESA(Display * dpy, int scrn,
-                      size_t size, float readFreq,
-                      float writeFreq, float priority)
-{
-#ifdef __DRI_ALLOCATE
-   __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, scrn);
-
-   if (psc && psc->allocate)
-      return (*psc->allocate->allocateMemory) (psc->__driScreen, size,
-                                               readFreq, writeFreq, priority);
-
-#else
-   (void) dpy;
-   (void) scrn;
-   (void) size;
-   (void) readFreq;
-   (void) writeFreq;
-   (void) priority;
-#endif /* __DRI_ALLOCATE */
-
-   return NULL;
-}
-
-
-PUBLIC void
-glXFreeMemoryMESA(Display * dpy, int scrn, void *pointer)
-{
-#ifdef __DRI_ALLOCATE
-   __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, scrn);
-
-   if (psc && psc->allocate)
-      (*psc->allocate->freeMemory) (psc->__driScreen, pointer);
-
-#else
-   (void) dpy;
-   (void) scrn;
-   (void) pointer;
-#endif /* __DRI_ALLOCATE */
-}
-
-
-PUBLIC GLuint
-glXGetMemoryOffsetMESA(Display * dpy, int scrn, const void *pointer)
-{
-#ifdef __DRI_ALLOCATE
-   __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, scrn);
-
-   if (psc && psc->allocate)
-      return (*psc->allocate->memoryOffset) (psc->__driScreen, pointer);
-
-#else
-   (void) dpy;
-   (void) scrn;
-   (void) pointer;
-#endif /* GLX_DIRECT_RENDERING */
+#endif
 
-   return ~0L;
+   return False;
 }
 
 /*@}*/
@@ -2705,7 +2538,7 @@ __glXReleaseBuffersMESA(Display * dpy, GLXDrawable d)
 }
 
 
-PUBLIC GLXPixmap
+_X_EXPORT GLXPixmap
 glXCreateGLXPixmapMESA(Display * dpy, XVisualInfo * visual,
                        Pixmap pixmap, Colormap cmap)
 {
@@ -2734,11 +2567,10 @@ __glXCopySubBufferMESA(Display * dpy, GLXDrawable drawable,
    INT32 *x_ptr, *y_ptr, *w_ptr, *h_ptr;
    CARD8 opcode;
 
-#ifdef __DRI_COPY_SUB_BUFFER
-   int screen;
-   __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
+   __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
    if (pdraw != NULL) {
-      __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, screen);
+      __GLXscreenConfigs *psc = pdraw->psc;
       if (psc->driScreen->copySubBuffer != NULL) {
          glFlush();
          (*psc->driScreen->copySubBuffer) (pdraw, x, y, width, height);
@@ -2793,10 +2625,10 @@ __glXCopySubBufferMESA(Display * dpy, GLXDrawable drawable,
 /**
  * GLX_EXT_texture_from_pixmap
  */
-/*@{*/
 static void
-__glXBindTexImageEXT(Display * dpy,
-                     GLXDrawable drawable, int buffer, const int *attrib_list)
+indirect_bind_tex_image(Display * dpy,
+                       GLXDrawable drawable,
+                       int buffer, const int *attrib_list)
 {
    xGLXVendorPrivateReq *req;
    GLXContext gc = __glXGetCurrentContext();
@@ -2807,37 +2639,12 @@ __glXBindTexImageEXT(Display * dpy,
    CARD8 opcode;
    unsigned int i;
 
-   if (gc == NULL)
-      return;
-
    i = 0;
    if (attrib_list) {
       while (attrib_list[i * 2] != None)
          i++;
    }
 
-#ifdef GLX_DIRECT_RENDERING
-   if (gc->driContext) {
-      __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, NULL);
-
-      if (pdraw != NULL) {
-         if (pdraw->psc->texBuffer->base.version >= 2 &&
-             pdraw->psc->texBuffer->setTexBuffer2 != NULL) {
-            (*pdraw->psc->texBuffer->setTexBuffer2) (gc->__driContext,
-                                                     pdraw->textureTarget,
-                                                     pdraw->textureFormat,
-                                                     pdraw->driDrawable);
-         }
-         else {
-            (*pdraw->psc->texBuffer->setTexBuffer) (gc->__driContext,
-                                                    pdraw->textureTarget,
-                                                    pdraw->driDrawable);
-         }
-      }
-      return;
-   }
-#endif
-
    opcode = __glXSetupForCommand(dpy);
    if (!opcode)
       return;
@@ -2872,7 +2679,7 @@ __glXBindTexImageEXT(Display * dpy,
 }
 
 static void
-__glXReleaseTexImageEXT(Display * dpy, GLXDrawable drawable, int buffer)
+indirect_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
 {
    xGLXVendorPrivateReq *req;
    GLXContext gc = __glXGetCurrentContext();
@@ -2880,9 +2687,6 @@ __glXReleaseTexImageEXT(Display * dpy, GLXDrawable drawable, int buffer)
    INT32 *buffer_ptr;
    CARD8 opcode;
 
-   if ((gc == NULL) || GC_IS_DIRECT(gc))
-      return;
-
    opcode = __glXSetupForCommand(dpy);
    if (!opcode)
       return;
@@ -2904,8 +2708,43 @@ __glXReleaseTexImageEXT(Display * dpy, GLXDrawable drawable, int buffer)
    SyncHandle();
 }
 
+static const struct glx_context_vtable indirect_context_vtable = {
+   indirect_destroy_context,
+   indirect_wait_gl,
+   indirect_wait_x,
+   indirect_use_x_font,
+   indirect_bind_tex_image,
+   indirect_release_tex_image,
+};
+
+/*@{*/
+static void
+__glXBindTexImageEXT(Display * dpy,
+                     GLXDrawable drawable, int buffer, const int *attrib_list)
+{
+   GLXContext gc = __glXGetCurrentContext();
+
+   if (gc == NULL || gc->vtable->bind_tex_image == NULL)
+      return;
+
+   gc->vtable->bind_tex_image(dpy, drawable, buffer, attrib_list);
+}
+
+static void
+__glXReleaseTexImageEXT(Display * dpy, GLXDrawable drawable, int buffer)
+{
+   GLXContext gc = __glXGetCurrentContext();
+
+   if (gc == NULL || gc->vtable->release_tex_image == NULL)
+      return;
+
+   gc->vtable->release_tex_image(dpy, drawable, buffer);
+}
+
 /*@}*/
 
+#endif /* GLX_USE_APPLEGL */
+
 /**
  * \c strdup is actually not a standard ANSI C or POSIX routine.
  * Irix will not define it if ANSI mode is in effect.
@@ -2983,6 +2822,7 @@ static const struct name_address_pair GLX_functions[] = {
    GLX_FUNCTION(glXQueryDrawable),
    GLX_FUNCTION(glXSelectEvent),
 
+#ifndef GLX_USE_APPLEGL
    /*** GLX_SGI_swap_control ***/
    GLX_FUNCTION2(glXSwapIntervalSGI, __glXSwapIntervalSGI),
 
@@ -3000,6 +2840,7 @@ static const struct name_address_pair GLX_functions[] = {
    GLX_FUNCTION2(glXGetCurrentDisplayEXT, glXGetCurrentDisplay),
    GLX_FUNCTION(glXImportContextEXT),
    GLX_FUNCTION2(glXQueryContextInfoEXT, glXQueryContext),
+#endif
 
    /*** GLX_SGIX_fbconfig ***/
    GLX_FUNCTION2(glXGetFBConfigAttribSGIX, glXGetFBConfigAttrib),
@@ -3009,6 +2850,7 @@ static const struct name_address_pair GLX_functions[] = {
    GLX_FUNCTION2(glXGetVisualFromFBConfigSGIX, glXGetVisualFromFBConfig),
    GLX_FUNCTION(glXGetFBConfigFromVisualSGIX),
 
+#ifndef GLX_USE_APPLEGL
    /*** GLX_SGIX_pbuffer ***/
    GLX_FUNCTION(glXCreateGLXPbufferSGIX),
    GLX_FUNCTION(glXDestroyGLXPbufferSGIX),
@@ -3023,11 +2865,6 @@ static const struct name_address_pair GLX_functions[] = {
    GLX_FUNCTION2(glXBindSwapBarrierSGIX, __glXBindSwapBarrierSGIX),
    GLX_FUNCTION2(glXQueryMaxSwapBarriersSGIX, __glXQueryMaxSwapBarriersSGIX),
 
-   /*** GLX_MESA_allocate_memory ***/
-   GLX_FUNCTION(glXAllocateMemoryMESA),
-   GLX_FUNCTION(glXFreeMemoryMESA),
-   GLX_FUNCTION(glXGetMemoryOffsetMESA),
-
    /*** GLX_MESA_copy_sub_buffer ***/
    GLX_FUNCTION2(glXCopySubBufferMESA, __glXCopySubBufferMESA),
 
@@ -3040,12 +2877,7 @@ static const struct name_address_pair GLX_functions[] = {
    /*** GLX_MESA_swap_control ***/
    GLX_FUNCTION2(glXSwapIntervalMESA, __glXSwapIntervalMESA),
    GLX_FUNCTION2(glXGetSwapIntervalMESA, __glXGetSwapIntervalMESA),
-
-   /*** GLX_MESA_swap_frame_usage ***/
-   GLX_FUNCTION2(glXBeginFrameTrackingMESA, __glXBeginFrameTrackingMESA),
-   GLX_FUNCTION2(glXEndFrameTrackingMESA, __glXEndFrameTrackingMESA),
-   GLX_FUNCTION2(glXGetFrameUsageMESA, __glXGetFrameUsageMESA),
-   GLX_FUNCTION2(glXQueryFrameTrackingMESA, __glXQueryFrameTrackingMESA),
+#endif
 
    /*** GLX_ARB_get_proc_address ***/
    GLX_FUNCTION(glXGetProcAddressARB),
@@ -3053,6 +2885,7 @@ static const struct name_address_pair GLX_functions[] = {
    /*** GLX 1.4 ***/
    GLX_FUNCTION2(glXGetProcAddress, glXGetProcAddressARB),
 
+#ifndef GLX_USE_APPLEGL
    /*** GLX_OML_sync_control ***/
    GLX_FUNCTION2(glXWaitForSbcOML, __glXWaitForSbcOML),
    GLX_FUNCTION2(glXWaitForMscOML, __glXWaitForMscOML),
@@ -3063,8 +2896,9 @@ static const struct name_address_pair GLX_functions[] = {
    /*** GLX_EXT_texture_from_pixmap ***/
    GLX_FUNCTION2(glXBindTexImageEXT, __glXBindTexImageEXT),
    GLX_FUNCTION2(glXReleaseTexImageEXT, __glXReleaseTexImageEXT),
+#endif
 
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
    /*** DRI configuration ***/
    GLX_FUNCTION(glXGetScreenDriver),
    GLX_FUNCTION(glXGetDriverConfig),
@@ -3073,7 +2907,7 @@ static const struct name_address_pair GLX_functions[] = {
    {NULL, NULL}                 /* end of list */
 };
 
-
+#ifndef GLX_USE_APPLEGL
 static const GLvoid *
 get_glx_proc_address(const char *funcName)
 {
@@ -3087,7 +2921,7 @@ get_glx_proc_address(const char *funcName)
 
    return NULL;
 }
-
+#endif
 
 /**
  * Get the address of a named GL function.  This is the pre-GLX 1.4 name for
@@ -3098,7 +2932,7 @@ get_glx_proc_address(const char *funcName)
  *
  * \sa glXGetProcAddress
  */
-PUBLIC void (*glXGetProcAddressARB(const GLubyte * procName)) (void)
+_X_EXPORT void (*glXGetProcAddressARB(const GLubyte * procName)) (void)
 {
    typedef void (*gl_function) (void);
    gl_function f;
@@ -3110,13 +2944,15 @@ PUBLIC void (*glXGetProcAddressARB(const GLubyte * procName)) (void)
     * DRI based drivers from searching the core GL function table for
     * internal API functions.
     */
-
+#ifdef GLX_USE_APPLEGL
+   f = (gl_function) apple_glx_get_proc_address(procName);
+#else
    f = (gl_function) get_glx_proc_address((const char *) procName);
    if ((f == NULL) && (procName[0] == 'g') && (procName[1] == 'l')
        && (procName[2] != 'X')) {
       f = (gl_function) _glapi_get_proc_address((const char *) procName);
    }
-
+#endif
    return f;
 }
 
@@ -3129,7 +2965,7 @@ PUBLIC void (*glXGetProcAddressARB(const GLubyte * procName)) (void)
  *
  * \sa glXGetProcAddressARB
  */
-PUBLIC void (*glXGetProcAddress(const GLubyte * procName)) (void)
+_X_EXPORT void (*glXGetProcAddress(const GLubyte * procName)) (void)
 #if defined(__GNUC__) && !defined(GLX_ALIAS_UNSUPPORTED)
    __attribute__ ((alias("glXGetProcAddressARB")));
 #else
@@ -3139,7 +2975,7 @@ PUBLIC void (*glXGetProcAddress(const GLubyte * procName)) (void)
 #endif /* __GNUC__ */
 
 
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
 /**
  * Get the unadjusted system time (UST).  Currently, the UST is measured in
  * microseconds since Epoc.  The actual resolution of the UST may vary from