glx: Fix use after free in drisw error path
[mesa.git] / src / glx / glxcmds.c
index f418ef4a04cb668a4d2fe73cbb0fc969988e2baa..d4aa8b5866893690710f515af70a9927ae22a585 100644 (file)
 #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
 
 #if defined(USE_XCB)
@@ -61,7 +58,6 @@
 
 static const char __glXGLXClientVendorName[] = "Mesa Project and SGI";
 static const char __glXGLXClientVersion[] = "1.4";
-static const struct glx_context_vtable indirect_context_vtable;
 
 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
 
@@ -89,7 +85,7 @@ GarbageCollectDRIDrawables(struct glx_screen * sc)
 {
    XID draw;
    __GLXDRIdrawable *pdraw;
-   __GLXdisplayPrivate *priv = sc->display;
+   struct glx_display *priv = sc->display;
    XWindowAttributes xwa;
    int (*oldXErrorHandler) (Display *, XErrorEvent *);
 
@@ -127,7 +123,7 @@ GarbageCollectDRIDrawables(struct glx_screen * sc)
 _X_HIDDEN __GLXDRIdrawable *
 GetGLXDRIDrawable(Display * dpy, GLXDrawable drawable)
 {
-   __GLXdisplayPrivate *priv = __glXInitialize(dpy);
+   struct glx_display *priv = __glXInitialize(dpy);
    __GLXDRIdrawable *pdraw;
 
    if (priv == NULL)
@@ -159,7 +155,7 @@ GetGLXDRIDrawable(Display * dpy, GLXDrawable drawable)
 static struct glx_screen *
 GetGLXScreenConfigs(Display * dpy, int scrn)
 {
-   __GLXdisplayPrivate *const priv = __glXInitialize(dpy);
+   struct glx_display *const priv = __glXInitialize(dpy);
 
    return (priv
            && priv->screens !=
@@ -168,7 +164,7 @@ GetGLXScreenConfigs(Display * dpy, int scrn)
 
 
 static int
-GetGLXPrivScreenConfig(Display * dpy, int scrn, __GLXdisplayPrivate ** ppriv,
+GetGLXPrivScreenConfig(Display * dpy, int scrn, struct glx_display ** ppriv,
                        struct glx_screen ** ppsc)
 {
    /* Initialize the extension, if needed .  This has the added value
@@ -213,7 +209,7 @@ GetGLXPrivScreenConfig(Display * dpy, int scrn, __GLXdisplayPrivate ** ppriv,
 static struct glx_config *
 ValidateGLXFBConfig(Display * dpy, GLXFBConfig fbconfig)
 {
-   __GLXdisplayPrivate *const priv = __glXInitialize(dpy);
+   struct glx_display *const priv = __glXInitialize(dpy);
    int num_screens = ScreenCount(dpy);
    unsigned i;
    struct glx_config *config;
@@ -232,188 +228,8 @@ ValidateGLXFBConfig(Display * dpy, GLXFBConfig fbconfig)
    return NULL;
 }
 
-#ifdef GLX_USE_APPLEGL
-
-static const struct glx_context_vtable applegl_context_vtable;
-
-static __GLcontext *
-applegl_create_context(struct glx_screen *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
- * later in the function for direct-rendering contexts.  Direct-rendering
- * contexts don't need to track client state, so they don't need that memory
- * at all.
- *
- * \todo Eliminate \c __glXInitVertexArrayState.  Replace it with a new
- * function called \c __glXAllocateClientState that allocates the memory and
- * does all the initialization (including the pixel pack / unpack).
- */
-static GLXContext
-indirect_create_context(struct glx_screen *psc,
-                       struct glx_config *mode,
-                       GLXContext shareList, int renderType)
-{
-   GLXContext gc;
-   int bufSize;
-   CARD8 opcode;
-   __GLXattribute *state;
-
-   opcode = __glXSetupForCommand(psc->dpy);
-   if (!opcode) {
-      return NULL;
-   }
-
-   /* Allocate our context record */
-   gc = (GLXContext) Xmalloc(sizeof(struct __GLXcontextRec));
-   if (!gc) {
-      /* Out of memory */
-      return NULL;
-   }
-   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 */
-      Xfree(gc);
-      return NULL;
-   }
-   gc->client_state_private = state;
-   memset(gc->client_state_private, 0, sizeof(struct __GLXattributeRec));
-   state->NoDrawArraysProtocol = (getenv("LIBGL_NO_DRAWARRAYS") != NULL);
-
-   /*
-    ** Create a temporary buffer to hold GLX rendering commands.  The size
-    ** of the buffer is selected so that the maximum number of GLX rendering
-    ** commands can fit in a single X packet and still have room in the X
-    ** packet for the GLXRenderReq header.
-    */
-
-   bufSize = (XMaxRequestSize(psc->dpy) * 4) - sz_xGLXRenderReq;
-   gc->buf = (GLubyte *) Xmalloc(bufSize);
-   if (!gc->buf) {
-      Xfree(gc->client_state_private);
-      Xfree(gc);
-      return NULL;
-   }
-   gc->bufSize = bufSize;
-
-   /* Fill in the new context */
-   gc->renderMode = GL_RENDER;
-
-   state->storePack.alignment = 4;
-   state->storeUnpack.alignment = 4;
-
-   gc->attributes.stackPointer = &gc->attributes.stack[0];
-
-   /*
-    ** PERFORMANCE NOTE: A mode dependent fill image can speed things up.
-    ** Other code uses the fastImageUnpack bit, but it is never set
-    ** to GL_TRUE.
-    */
-   gc->fastImageUnpack = GL_FALSE;
-   gc->fillImage = __glFillImage;
-   gc->pc = gc->buf;
-   gc->bufEnd = gc->buf + bufSize;
-   gc->isDirect = GL_FALSE;
-   if (__glXDebug) {
-      /*
-       ** Set limit register so that there will be one command per packet
-       */
-      gc->limit = gc->buf;
-   }
-   else {
-      gc->limit = gc->buf + bufSize - __GLX_BUFFER_LIMIT_SIZE;
-   }
-   gc->majorOpcode = opcode;
-
-   /*
-    ** Constrain the maximum drawing command size allowed to be
-    ** transfered using the X_GLXRender protocol request.  First
-    ** constrain by a software limit, then constrain by the protocl
-    ** limit.
-    */
-   if (bufSize > __GLX_RENDER_CMD_SIZE_LIMIT) {
-      bufSize = __GLX_RENDER_CMD_SIZE_LIMIT;
-   }
-   if (bufSize > __GLX_MAX_RENDER_CMD_SIZE) {
-      bufSize = __GLX_MAX_RENDER_CMD_SIZE;
-   }
-   gc->maxSmallRenderCommandSize = bufSize;
-   
-
-   return gc;
-}
-
-struct glx_screen_vtable indirect_screen_vtable = {
-   indirect_create_context
-};
-
-_X_HIDDEN struct glx_screen *
-indirect_create_screen(int screen, __GLXdisplayPrivate * priv)
-{
-   struct glx_screen *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,
+glx_context_init(struct glx_context *gc,
                 struct glx_screen *psc, struct glx_config *config)
 {
    gc->majorOpcode = __glXSetupForCommand(psc->display->dpy);
@@ -424,6 +240,7 @@ glx_context_init(__GLXcontext *gc,
    gc->psc = psc;
    gc->config = config;
    gc->isDirect = GL_TRUE;
+   gc->currentContextTag = -1;
 
    return GL_TRUE;
 }
@@ -441,13 +258,13 @@ glx_context_init(__GLXcontext *gc,
 static GLXContext
 CreateContext(Display * dpy, int generic_id,
               struct glx_config *config,
-              GLXContext shareList,
+              GLXContext shareList_user,
               Bool allowDirect,
              unsigned code, int renderType, int screen)
 {
-   GLXContext gc = NULL;
+   struct glx_context *gc = NULL;
    struct glx_screen *const psc = GetGLXScreenConfigs(dpy, screen);
-
+   struct glx_context *shareList = (struct glx_context *) shareList_user;
    if (dpy == NULL)
       return NULL;
 
@@ -475,7 +292,7 @@ CreateContext(Display * dpy, int generic_id,
       req->visual = generic_id;
       req->screen = screen;
       req->shareList = shareList ? shareList->xid : None;
-      req->isDirect = GC_IS_DIRECT(gc);
+      req->isDirect = gc->isDirect;
       break;
    }
 
@@ -491,7 +308,7 @@ CreateContext(Display * dpy, int generic_id,
       req->screen = screen;
       req->renderType = renderType;
       req->shareList = shareList ? shareList->xid : None;
-      req->isDirect = GC_IS_DIRECT(gc);
+      req->isDirect = gc->isDirect;
       break;
    }
 
@@ -512,7 +329,7 @@ CreateContext(Display * dpy, int generic_id,
       req->screen = screen;
       req->renderType = renderType;
       req->shareList = shareList ? shareList->xid : None;
-      req->isDirect = GC_IS_DIRECT(gc);
+      req->isDirect = gc->isDirect;
       break;
    }
 
@@ -529,7 +346,7 @@ CreateContext(Display * dpy, int generic_id,
    gc->imported = GL_FALSE;
    gc->renderType = renderType;
 
-   return gc;
+   return (GLXContext) gc;
 }
 
 _X_EXPORT GLXContext
@@ -578,34 +395,14 @@ glx_send_destroy_context(Display *dpy, XID xid)
    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)
-      XFree((char *) gc->renderer);
-   if (gc->version)
-      XFree((char *) gc->version);
-   if (gc->extensions)
-      XFree((char *) gc->extensions);
-   __glFreeAttributeState(gc);
-   XFree((char *) gc->buf);
-   Xfree((char *) gc->client_state_private);
-   XFree((char *) gc);
-}
-
 /*
 ** Destroy the named context
 */
 static void
-DestroyContext(Display * dpy, GLXContext gc)
+DestroyContext(Display * dpy, GLXContext ctx)
 {
+   struct glx_context *gc = (struct glx_context *) ctx;
+
    if (!gc)
       return;
 
@@ -640,7 +437,7 @@ glXDestroyContext(Display * dpy, GLXContext gc)
 _X_EXPORT Bool
 glXQueryVersion(Display * dpy, int *major, int *minor)
 {
-   __GLXdisplayPrivate *priv;
+   struct glx_display *priv;
 
    /* Init the extension.  This fetches the major and minor version. */
    priv = __glXInitialize(dpy);
@@ -673,25 +470,6 @@ glXQueryExtension(Display * dpy, int *errorBase, int *eventBase)
    return rv;
 }
 
-static void
-indirect_wait_gl(__GLXcontext *gc)
-{
-   xGLXWaitGLReq *req;
-   Display *dpy = gc->currentDpy;
-
-   /* Flush any pending commands out */
-   __glXFlushRenderBuffer(gc, gc->pc);
-
-   /* Send the glXWaitGL request */
-   LockDisplay(dpy);
-   GetReq(GLXWaitGL, req);
-   req->reqType = gc->majorOpcode;
-   req->glxCode = X_GLXWaitGL;
-   req->contextTag = gc->currentContextTag;
-   UnlockDisplay(dpy);
-   SyncHandle();
-}
-
 /*
 ** Put a barrier in the token stream that forces the GL to finish its
 ** work before X can proceed.
@@ -699,30 +477,12 @@ indirect_wait_gl(__GLXcontext *gc)
 _X_EXPORT void
 glXWaitGL(void)
 {
-   GLXContext gc = __glXGetCurrentContext();
+   struct glx_context *gc = __glXGetCurrentContext();
 
-   if (gc && gc->vtable->use_x_font)
+   if (gc && gc->vtable->wait_gl)
       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);
-
-   LockDisplay(dpy);
-   GetReq(GLXWaitX, req);
-   req->reqType = gc->majorOpcode;
-   req->glxCode = X_GLXWaitX;
-   req->contextTag = gc->currentContextTag;
-   UnlockDisplay(dpy);
-   SyncHandle();
-}
-
 /*
 ** Put a barrier in the token stream that forces X to finish its
 ** work before GL can proceed.
@@ -730,71 +490,16 @@ indirect_wait_x(__GLXcontext *gc)
 _X_EXPORT void
 glXWaitX(void)
 {
-   GLXContext gc = __glXGetCurrentContext();
+   struct glx_context *gc = __glXGetCurrentContext();
 
-   if (gc && gc->vtable->use_x_font)
+   if (gc && gc->vtable->wait_x)
       gc->vtable->wait_x(gc);
 }
 
-static void
-indirect_use_x_font(__GLXcontext *gc,
-                   Font font, int first, int count, int listBase)
-{
-   xGLXUseXFontReq *req;
-   Display *dpy = gc->currentDpy;
-
-   /* Flush any pending commands out */
-   __glXFlushRenderBuffer(gc, gc->pc);
-
-   /* Send the glXUseFont request */
-   LockDisplay(dpy);
-   GetReq(GLXUseXFont, req);
-   req->reqType = gc->majorOpcode;
-   req->glxCode = X_GLXUseXFont;
-   req->contextTag = gc->currentContextTag;
-   req->font = font;
-   req->first = first;
-   req->count = count;
-   req->listBase = listBase;
-   UnlockDisplay(dpy);
-   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();
+   struct glx_context *gc = __glXGetCurrentContext();
 
    if (gc && gc->vtable->use_x_font)
       gc->vtable->use_x_font(gc, font, first, count, listBase);
@@ -807,11 +512,13 @@ glXUseXFont(Font font, int first, int count, int listBase)
 ** attribute "mask".
 */
 _X_EXPORT void
-glXCopyContext(Display * dpy, GLXContext source,
-               GLXContext dest, unsigned long mask)
+glXCopyContext(Display * dpy, GLXContext source_user,
+              GLXContext dest_user, unsigned long mask)
 {
+   struct glx_context *source = (struct glx_context *) source_user;
+   struct glx_context *dest = (struct glx_context *) dest_user;
 #ifdef GLX_USE_APPLEGL
-   GLXContext gc = __glXGetCurrentContext();
+   struct glx_context *gc = __glXGetCurrentContext();
    int errorcode;
    bool x11error;
 
@@ -822,7 +529,7 @@ glXCopyContext(Display * dpy, GLXContext source,
    
 #else
    xGLXCopyContextReq *req;
-   GLXContext gc = __glXGetCurrentContext();
+   struct glx_context *gc = __glXGetCurrentContext();
    GLXContextTag tag;
    CARD8 opcode;
 
@@ -832,7 +539,7 @@ glXCopyContext(Display * dpy, GLXContext source,
    }
 
 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
-   if (gc->driContext) {
+   if (gc->isDirect) {
       /* NOT_DONE: This does not work yet */
    }
 #endif
@@ -918,12 +625,14 @@ __glXIsDirect(Display * dpy, GLXContextID contextID)
  * the GLX protocol here at all?
  */
 _X_EXPORT Bool
-glXIsDirect(Display * dpy, GLXContext gc)
+glXIsDirect(Display * dpy, GLXContext gc_user)
 {
+   struct glx_context *gc = (struct glx_context *) gc_user;
+
    if (!gc) {
       return GL_FALSE;
    }
-   else if (GC_IS_DIRECT(gc)) {
+   else if (gc->isDirect) {
       return GL_TRUE;
    }
 #ifdef GLX_USE_APPLEGL  /* TODO: indirect on darwin */
@@ -974,7 +683,7 @@ glXCreateGLXPixmap(Display * dpy, XVisualInfo * vis, Pixmap pixmap)
       /* FIXME: Maybe delay __DRIdrawable creation until the drawable
        * is actually bound to a context... */
 
-      __GLXdisplayPrivate *const priv = __glXInitialize(dpy);
+      struct glx_display *const priv = __glXInitialize(dpy);
       __GLXDRIdrawable *pdraw;
       struct glx_screen *psc;
       struct glx_config *config;
@@ -1029,7 +738,7 @@ glXDestroyGLXPixmap(Display * dpy, GLXPixmap glxpixmap)
 
 #if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
    {
-      __GLXdisplayPrivate *const priv = __glXInitialize(dpy);
+      struct glx_display *const priv = __glXInitialize(dpy);
       __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, glxpixmap);
 
       if (pdraw != NULL) {
@@ -1052,7 +761,7 @@ glXSwapBuffers(Display * dpy, GLXDrawable drawable)
       __glXSendError(dpy, GLXBadCurrentWindow, 0, X_GLXSwapBuffers, false);
    }
 #else
-   GLXContext gc;
+   struct glx_context *gc;
    GLXContextTag tag;
    CARD8 opcode;
 #ifdef USE_XCB
@@ -1118,7 +827,7 @@ _X_EXPORT int
 glXGetConfig(Display * dpy, XVisualInfo * vis, int attribute,
              int *value_return)
 {
-   __GLXdisplayPrivate *priv;
+   struct glx_display *priv;
    struct glx_screen *psc;
    struct glx_config *config;
    int status;
@@ -1479,7 +1188,7 @@ _X_EXPORT XVisualInfo *
 glXChooseVisual(Display * dpy, int screen, int *attribList)
 {
    XVisualInfo *visualList = NULL;
-   __GLXdisplayPrivate *priv;
+   struct glx_display *priv;
    struct glx_screen *psc;
    struct glx_config test_config;
    struct glx_config *config;
@@ -1545,7 +1254,7 @@ _X_EXPORT const char *
 glXQueryExtensionsString(Display * dpy, int screen)
 {
    struct glx_screen *psc;
-   __GLXdisplayPrivate *priv;
+   struct glx_display *priv;
 
    if (GetGLXPrivScreenConfig(dpy, screen, &priv, &psc) != Success) {
       return NULL;
@@ -1591,7 +1300,7 @@ _X_EXPORT const char *
 glXQueryServerString(Display * dpy, int screen, int name)
 {
    struct glx_screen *psc;
-   __GLXdisplayPrivate *priv;
+   struct glx_display *priv;
    const char **str;
 
 
@@ -1660,7 +1369,7 @@ __glXClientInfo(Display * dpy, int opcode)
 _X_EXPORT Display *
 glXGetCurrentDisplay(void)
 {
-   GLXContext gc = __glXGetCurrentContext();
+   struct glx_context *gc = __glXGetCurrentContext();
    if (NULL == gc)
       return NULL;
    return gc->currentDpy;
@@ -1674,11 +1383,11 @@ GLX_ALIAS(Display *, glXGetCurrentDisplayEXT, (void), (),
 _X_EXPORT GLXContext
 glXImportContextEXT(Display *dpy, GLXContextID contextID)
 {
-   __GLXdisplayPrivate *priv = __glXInitialize(dpy);
+   struct glx_display *priv = __glXInitialize(dpy);
    struct glx_screen *psc;
    xGLXQueryContextReply reply;
    CARD8 opcode;
-   GLXContext ctx;
+   struct glx_context *ctx;
    int propList[__GLX_MAX_CONTEXT_PROPS * 2], *pProp, nPropListBytes;
    int i, renderType;
    XID share;
@@ -1767,14 +1476,16 @@ glXImportContextEXT(Display *dpy, GLXContextID contextID)
    ctx->imported = GL_TRUE;
    ctx->share_xid = share;
 
-   return ctx;
+   return (GLXContext) ctx;
 }
 
 #endif
 
 _X_EXPORT int
-glXQueryContext(Display * dpy, GLXContext ctx, int attribute, int *value)
+glXQueryContext(Display * dpy, GLXContext ctx_user, int attribute, int *value)
 {
+   struct glx_context *ctx = (struct glx_context *) ctx_user;
+
    switch (attribute) {
       case GLX_SHARE_CONTEXT_EXT:
       *value = ctx->share_xid;
@@ -1802,8 +1513,10 @@ GLX_ALIAS(int, glXQueryContextInfoEXT,
           (Display * dpy, GLXContext ctx, int attribute, int *value),
           (dpy, ctx, attribute, value), glXQueryContext)
 
-_X_EXPORT GLXContextID glXGetContextIDEXT(const GLXContext ctx)
+_X_EXPORT GLXContextID glXGetContextIDEXT(const GLXContext ctx_user)
 {
+   struct glx_context *ctx = (struct glx_context *) ctx_user;
+
    return ctx->xid;
 }
 
@@ -1853,7 +1566,8 @@ glXCreateNewContext(Display * dpy, GLXFBConfig fbconfig,
 _X_EXPORT GLXDrawable
 glXGetCurrentReadDrawable(void)
 {
-   GLXContext gc = __glXGetCurrentContext();
+   struct glx_context *gc = __glXGetCurrentContext();
+
    return gc->currentReadable;
 }
 
@@ -1861,7 +1575,7 @@ glXGetCurrentReadDrawable(void)
 _X_EXPORT GLXFBConfig *
 glXGetFBConfigs(Display * dpy, int screen, int *nelements)
 {
-   __GLXdisplayPrivate *priv = __glXInitialize(dpy);
+   struct glx_display *priv = __glXInitialize(dpy);
    struct glx_config **config_list = NULL;
    struct glx_config *config;
    unsigned num_configs = 0;
@@ -1934,7 +1648,7 @@ static int
 __glXSwapIntervalSGI(int interval)
 {
    xGLXVendorPrivateReq *req;
-   GLXContext gc = __glXGetCurrentContext();
+   struct glx_context *gc = __glXGetCurrentContext();
    struct glx_screen *psc;
    Display *dpy;
    CARD32 *interval_ptr;
@@ -1951,7 +1665,7 @@ __glXSwapIntervalSGI(int interval)
    psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
 
 #ifdef GLX_DIRECT_RENDERING
-   if (gc->driContext && psc->driScreen && psc->driScreen->setSwapInterval) {
+   if (gc->isDirect && psc->driScreen && psc->driScreen->setSwapInterval) {
       __GLXDRIdrawable *pdraw =
         GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
       psc->driScreen->setSwapInterval(pdraw, interval);
@@ -1991,9 +1705,9 @@ static int
 __glXSwapIntervalMESA(unsigned int interval)
 {
 #ifdef GLX_DIRECT_RENDERING
-   GLXContext gc = __glXGetCurrentContext();
+   struct glx_context *gc = __glXGetCurrentContext();
 
-   if (gc != NULL && gc->driContext) {
+   if (gc != NULL && gc->isDirect) {
       struct glx_screen *psc;
 
       psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
@@ -2013,9 +1727,9 @@ static int
 __glXGetSwapIntervalMESA(void)
 {
 #ifdef GLX_DIRECT_RENDERING
-   GLXContext gc = __glXGetCurrentContext();
+   struct glx_context *gc = __glXGetCurrentContext();
 
-   if (gc != NULL && gc->driContext) {
+   if (gc != NULL && gc->isDirect) {
       struct glx_screen *psc;
 
       psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
@@ -2039,7 +1753,7 @@ __glXGetVideoSyncSGI(unsigned int *count)
 {
    int64_t ust, msc, sbc;
    int ret;
-   GLXContext gc = __glXGetCurrentContext();
+   struct glx_context *gc = __glXGetCurrentContext();
    struct glx_screen *psc;
 #ifdef GLX_DIRECT_RENDERING
    __GLXDRIdrawable *pdraw;
@@ -2049,7 +1763,7 @@ __glXGetVideoSyncSGI(unsigned int *count)
       return GLX_BAD_CONTEXT;
 
 #ifdef GLX_DIRECT_RENDERING
-   if (!gc->driContext)
+   if (!gc->isDirect)
       return GLX_BAD_CONTEXT;
 #endif
 
@@ -2076,7 +1790,7 @@ __glXGetVideoSyncSGI(unsigned int *count)
 static int
 __glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
 {
-   GLXContext gc = __glXGetCurrentContext();
+   struct glx_context *gc = __glXGetCurrentContext();
    struct glx_screen *psc;
 #ifdef GLX_DIRECT_RENDERING
    __GLXDRIdrawable *pdraw;
@@ -2091,7 +1805,7 @@ __glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
       return GLX_BAD_CONTEXT;
 
 #ifdef GLX_DIRECT_RENDERING
-   if (!gc->driContext)
+   if (!gc->isDirect)
       return GLX_BAD_CONTEXT;
 #endif
 
@@ -2217,7 +1931,7 @@ glXCreateContextWithConfigSGIX(Display * dpy,
 _X_EXPORT GLXFBConfigSGIX
 glXGetFBConfigFromVisualSGIX(Display * dpy, XVisualInfo * vis)
 {
-   __GLXdisplayPrivate *priv;
+   struct glx_display *priv;
    struct glx_screen *psc = NULL;
 
    if ((GetGLXPrivScreenConfig(dpy, vis->screen, &priv, &psc) != Success)
@@ -2272,7 +1986,7 @@ static Bool
 __glXGetSyncValuesOML(Display * dpy, GLXDrawable drawable,
                       int64_t * ust, int64_t * msc, int64_t * sbc)
 {
-   __GLXdisplayPrivate * const priv = __glXInitialize(dpy);
+   struct glx_display * const priv = __glXInitialize(dpy);
    int ret;
 #ifdef GLX_DIRECT_RENDERING
    __GLXDRIdrawable *pdraw;
@@ -2402,7 +2116,7 @@ static int64_t
 __glXSwapBuffersMscOML(Display * dpy, GLXDrawable drawable,
                        int64_t target_msc, int64_t divisor, int64_t remainder)
 {
-   GLXContext gc = __glXGetCurrentContext();
+   struct glx_context *gc = __glXGetCurrentContext();
 #ifdef GLX_DIRECT_RENDERING
    __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable);
    struct glx_screen *psc = pdraw ? pdraw->psc : NULL;
@@ -2412,7 +2126,7 @@ __glXSwapBuffersMscOML(Display * dpy, GLXDrawable drawable,
       return -1;
 
 #ifdef GLX_DIRECT_RENDERING
-   if (!pdraw || !gc->driContext)
+   if (!pdraw || !gc->isDirect)
       return -1;
 #endif
 
@@ -2561,7 +2275,7 @@ __glXCopySubBufferMESA(Display * dpy, GLXDrawable drawable,
                        int x, int y, int width, int height)
 {
    xGLXVendorPrivateReq *req;
-   GLXContext gc;
+   struct glx_context *gc;
    GLXContextTag tag;
    CARD32 *drawable_ptr;
    INT32 *x_ptr, *y_ptr, *w_ptr, *h_ptr;
@@ -2621,108 +2335,12 @@ __glXCopySubBufferMESA(Display * dpy, GLXDrawable drawable,
    SyncHandle();
 }
 
-
-/**
- * GLX_EXT_texture_from_pixmap
- */
-static void
-indirect_bind_tex_image(Display * dpy,
-                       GLXDrawable drawable,
-                       int buffer, const int *attrib_list)
-{
-   xGLXVendorPrivateReq *req;
-   GLXContext gc = __glXGetCurrentContext();
-   CARD32 *drawable_ptr;
-   INT32 *buffer_ptr;
-   CARD32 *num_attrib_ptr;
-   CARD32 *attrib_ptr;
-   CARD8 opcode;
-   unsigned int i;
-
-   i = 0;
-   if (attrib_list) {
-      while (attrib_list[i * 2] != None)
-         i++;
-   }
-
-   opcode = __glXSetupForCommand(dpy);
-   if (!opcode)
-      return;
-
-   LockDisplay(dpy);
-   GetReqExtra(GLXVendorPrivate, 12 + 8 * i, req);
-   req->reqType = opcode;
-   req->glxCode = X_GLXVendorPrivate;
-   req->vendorCode = X_GLXvop_BindTexImageEXT;
-   req->contextTag = gc->currentContextTag;
-
-   drawable_ptr = (CARD32 *) (req + 1);
-   buffer_ptr = (INT32 *) (drawable_ptr + 1);
-   num_attrib_ptr = (CARD32 *) (buffer_ptr + 1);
-   attrib_ptr = (CARD32 *) (num_attrib_ptr + 1);
-
-   *drawable_ptr = drawable;
-   *buffer_ptr = buffer;
-   *num_attrib_ptr = (CARD32) i;
-
-   i = 0;
-   if (attrib_list) {
-      while (attrib_list[i * 2] != None) {
-         *attrib_ptr++ = (CARD32) attrib_list[i * 2 + 0];
-         *attrib_ptr++ = (CARD32) attrib_list[i * 2 + 1];
-         i++;
-      }
-   }
-
-   UnlockDisplay(dpy);
-   SyncHandle();
-}
-
-static void
-indirect_release_tex_image(Display * dpy, GLXDrawable drawable, int buffer)
-{
-   xGLXVendorPrivateReq *req;
-   GLXContext gc = __glXGetCurrentContext();
-   CARD32 *drawable_ptr;
-   INT32 *buffer_ptr;
-   CARD8 opcode;
-
-   opcode = __glXSetupForCommand(dpy);
-   if (!opcode)
-      return;
-
-   LockDisplay(dpy);
-   GetReqExtra(GLXVendorPrivate, sizeof(CARD32) + sizeof(INT32), req);
-   req->reqType = opcode;
-   req->glxCode = X_GLXVendorPrivate;
-   req->vendorCode = X_GLXvop_ReleaseTexImageEXT;
-   req->contextTag = gc->currentContextTag;
-
-   drawable_ptr = (CARD32 *) (req + 1);
-   buffer_ptr = (INT32 *) (drawable_ptr + 1);
-
-   *drawable_ptr = drawable;
-   *buffer_ptr = buffer;
-
-   UnlockDisplay(dpy);
-   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();
+   struct glx_context *gc = __glXGetCurrentContext();
 
    if (gc == NULL || gc->vtable->bind_tex_image == NULL)
       return;
@@ -2733,7 +2351,7 @@ __glXBindTexImageEXT(Display * dpy,
 static void
 __glXReleaseTexImageEXT(Display * dpy, GLXDrawable drawable, int buffer)
 {
-   GLXContext gc = __glXGetCurrentContext();
+   struct glx_context *gc = __glXGetCurrentContext();
 
    if (gc == NULL || gc->vtable->release_tex_image == NULL)
       return;