DRI2: Drop sarea, implement swap buffers in the X server.
[mesa.git] / src / glx / x11 / glxcmds.c
index 1497c6b30ebd7e5703d5b07d4010ff661fb03475..9197130dcaa13beba93845f121e5d06345aefc03 100644 (file)
  * Client-side GLX interface.
  */
 
-#include <inttypes.h>
 #include "glxclient.h"
-#include <X11/extensions/extutil.h>
-#include <X11/extensions/Xext.h>
-#include <assert.h>
-#include <string.h>
 #include "glapi.h"
-#ifdef GLX_DIRECT_RENDERING
-#include "indirect_init.h"
-#include <X11/extensions/xf86vmode.h>
-#include "xf86dri.h"
-#endif
 #include "glxextensions.h"
 #include "glcontextmodes.h"
 #include "glheader.h"
+
+#ifdef GLX_DIRECT_RENDERING
 #include <sys/time.h>
+#include <X11/extensions/xf86vmode.h>
+#include "xf86dri.h"
+#endif
 
 static const char __glXGLXClientVendorName[] = "SGI";
 static const char __glXGLXClientVersion[] = "1.4";
@@ -83,7 +78,7 @@ static int windowExistsErrorHandler(Display *dpy, XErrorEvent *xerr)
 static void GarbageCollectDRIDrawables(Display *dpy, __GLXscreenConfigs *sc)
 {
     XID draw;
-    __GLXdrawable *pdraw;
+    __GLXDRIdrawable *pdraw;
     XWindowAttributes xwa;
     int (*oldXErrorHandler)(Display *, XErrorEvent *);
 
@@ -99,9 +94,8 @@ static void GarbageCollectDRIDrawables(Display *dpy, __GLXscreenConfigs *sc)
            if (!windowExistsFlag) {
                /* Destroy the local drawable data, if the drawable no
                   longer exists in the Xserver */
-               (*pdraw->driDrawable.destroyDrawable)(&pdraw->driDrawable);
-               XF86DRIDestroyDrawable(dpy, sc->scr, draw);
-               Xfree(pdraw);
+               (*pdraw->destroyDrawable)(pdraw);
+                __glxHashDelete(sc->drawHash, draw);
            }
        } while (__glxHashNext(sc->drawHash, &draw, (void *)&pdraw) == 1);
     }
@@ -110,6 +104,9 @@ static void GarbageCollectDRIDrawables(Display *dpy, __GLXscreenConfigs *sc)
     XSetErrorHandler(oldXErrorHandler);
 }
 
+extern __GLXDRIdrawable *
+GetGLXDRIDrawable(Display *dpy, GLXDrawable drawable, int * const scrn_num);
+
 /**
  * Get the __DRIdrawable for the drawable associated with a GLXContext
  * 
@@ -119,24 +116,27 @@ static void GarbageCollectDRIDrawables(Display *dpy, __GLXscreenConfigs *sc)
  * \returns  A pointer to the context's __DRIdrawable on success, or NULL if
  *           the drawable is not associated with a direct-rendering context.
  */
-static __DRIdrawable *
-GetDRIDrawable( Display *dpy, GLXDrawable drawable, int * const scrn_num )
+_X_HIDDEN __GLXDRIdrawable *
+GetGLXDRIDrawable(Display *dpy, GLXDrawable drawable, int * const scrn_num)
 {
-    __GLXdisplayPrivate * const priv = __glXInitialize(dpy);
-    __GLXdrawable * const pdraw;
+    __GLXdisplayPrivate *priv = __glXInitialize(dpy);
+    __GLXDRIdrawable *pdraw;
     const unsigned  screen_count = ScreenCount(dpy);
     unsigned   i;
-    __GLXscreenConfigs *sc;
+    __GLXscreenConfigs *psc;
 
-    if (priv == NULL || priv->driDisplay.private == NULL)
+    if (priv == NULL)
        return NULL;
     
     for (i = 0; i < screen_count; i++) {
-       sc = &priv->screenConfigs[i];
-       if (__glxHashLookup(sc->drawHash, drawable, (void *) &pdraw) == 0) {
+       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->driDrawable;
+           return pdraw;
        }
     }
 
@@ -312,9 +312,9 @@ GLXContext AllocateGLXContext( Display *dpy )
     */
     gc->fastImageUnpack = GL_FALSE;
     gc->fillImage = __glFillImage;
-    gc->isDirect = GL_FALSE;
     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
@@ -360,6 +360,10 @@ CreateContext(Display *dpy, XVisualInfo *vis,
              Bool use_glx_1_3, int renderType)
 {
     GLXContext gc;
+#ifdef GLX_DIRECT_RENDERING
+    int screen = (fbconfig == NULL) ? vis->screen : fbconfig->screen;
+    __GLXscreenConfigs * const psc = GetGLXScreenConfigs(dpy, screen);
+#endif
 
     if ( dpy == NULL )
        return NULL;
@@ -373,56 +377,36 @@ CreateContext(Display *dpy, XVisualInfo *vis,
            return NULL;
 
 #ifdef GLX_DIRECT_RENDERING
-       if (allowDirect) {
-           int screen = (fbconfig == NULL) ? vis->screen : fbconfig->screen;
-           __GLXscreenConfigs * const psc = GetGLXScreenConfigs(dpy, screen);
+       if (allowDirect && psc->driScreen) {
            const __GLcontextModes * mode;
-           drm_context_t hwContext;
 
-           /* The value of fbconfig cannot change because it is tested
-            * later in the function.
-            */
-           if ( fbconfig == NULL ) {
-               /* FIXME: Is it possible for the __GLcontextModes structure
-                * FIXME: to not be found?
-                */
-               mode = _gl_context_modes_find_visual( psc->configs,
-                                                     vis->visualid );
-               assert( mode != NULL );
-               assert( mode->screen == screen );
+           if (fbconfig == NULL) {
+               mode = _gl_context_modes_find_visual(psc->visuals, vis->visualid);
+               if (mode == NULL) {
+                  xError error;
+
+                  error.errorCode = BadValue;
+                  error.resourceID = vis->visualid;
+                  error.sequenceNumber = dpy->request;
+                  error.type = X_Error;
+                  error.majorCode = gc->majorOpcode;
+                  error.minorCode = X_GLXCreateContext;
+                  _XError(dpy, &error);
+                  return None;
+               }
            }
            else {
                mode = fbconfig;
            }
 
-           if (psc && psc->driScreen.private) {
-               __DRIcontext *shared = (shareList != NULL)
-                   ? &shareList->driContext : NULL;
-
-
-               if (!XF86DRICreateContextWithConfig(dpy, psc->scr,
-                                                   mode->fbconfigID,
-                                                   &gc->hwContextID, &hwContext))
-                   /* gah, handle this better */
-                   return NULL;
-
-               gc->driContext.private = 
-                 (*psc->driScreen.createNewContext)( &psc->driScreen,
-                                                     mode, renderType,
-                                                     shared,
-                                                     hwContext,
-                                                     &gc->driContext );
-               if (gc->driContext.private) {
-                   gc->isDirect = GL_TRUE;
-                   gc->screen = mode->screen;
-                   gc->psc = psc;
-                   gc->vid = mode->visualID;
-                   gc->fbconfigID = mode->fbconfigID;
-                   gc->mode = mode;
-               }
-               else {
-                   XF86DRIDestroyContext(dpy, psc->scr, gc->hwContextID);
-               }
+           gc->driContext = psc->driScreen->createContext(psc, mode, gc,
+                                                          shareList,
+                                                          renderType);
+           if (gc->driContext != NULL) {
+               gc->screen = mode->screen;
+               gc->psc = psc;
+               gc->mode = mode;
+               gc->isDirect = GL_TRUE;
            }
        }
 #endif
@@ -439,7 +423,11 @@ CreateContext(Display *dpy, XVisualInfo *vis,
            req->visual = vis->visualid;
            req->screen = vis->screen;
            req->shareList = shareList ? shareList->xid : None;
-           req->isDirect = gc->isDirect;
+#ifdef GLX_DIRECT_RENDERING
+           req->isDirect = gc->driContext != NULL;
+#else
+           req->isDirect = 0;
+#endif
        }
        else if ( use_glx_1_3 ) {
            xGLXCreateNewContextReq *req;
@@ -453,7 +441,11 @@ CreateContext(Display *dpy, XVisualInfo *vis,
            req->screen = fbconfig->screen;
            req->renderType = renderType;
            req->shareList = shareList ? shareList->xid : None;
-           req->isDirect = gc->isDirect;
+#ifdef GLX_DIRECT_RENDERING
+           req->isDirect = gc->driContext != NULL;
+#else
+           req->isDirect = 0;
+#endif
        }
        else {
            xGLXVendorPrivateWithReplyReq *vpreq;
@@ -471,7 +463,11 @@ CreateContext(Display *dpy, XVisualInfo *vis,
            req->screen = fbconfig->screen;
            req->renderType = renderType;
            req->shareList = shareList ? shareList->xid : None;
-           req->isDirect = gc->isDirect;
+#ifdef GLX_DIRECT_RENDERING
+           req->isDirect = gc->driContext != NULL;
+#else
+           req->isDirect = 0;
+#endif
        }
 
        UnlockDisplay(dpy);
@@ -493,7 +489,7 @@ PUBLIC GLXContext glXCreateContext(Display *dpy, XVisualInfo *vis,
                        False, 0);
 }
 
-void __glXFreeContext(__GLXcontext *gc)
+_X_HIDDEN void __glXFreeContext(__GLXcontext *gc)
 {
     if (gc->vendor) XFree((char *) gc->vendor);
     if (gc->renderer) XFree((char *) gc->renderer);
@@ -529,16 +525,15 @@ DestroyContext(Display *dpy, GLXContext gc)
 
 #ifdef GLX_DIRECT_RENDERING
     /* Destroy the direct rendering context */
-    if (gc->isDirect) {
-       if (gc->driContext.private) {
-           (*gc->driContext.destroyContext)(&gc->driContext);
-           XF86DRIDestroyContext(dpy, gc->psc->scr, gc->hwContextID);
-           gc->driContext.private = NULL;
-       }
+    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();
@@ -616,7 +611,7 @@ PUBLIC void glXWaitGL(void)
     __glXFlushRenderBuffer(gc, gc->pc);
 
 #ifdef GLX_DIRECT_RENDERING
-    if (gc->isDirect) {
+    if (gc->driContext) {
 /* This bit of ugliness unwraps the glFinish function */
 #ifdef glFinish
 #undef glFinish
@@ -652,7 +647,7 @@ PUBLIC void glXWaitX(void)
     __glXFlushRenderBuffer(gc, gc->pc);
 
 #ifdef GLX_DIRECT_RENDERING
-    if (gc->isDirect) {
+    if (gc->driContext) {
        XSync(dpy, False);
        return;
     }
@@ -682,7 +677,7 @@ PUBLIC void glXUseXFont(Font font, int first, int count, int listBase)
     (void) __glXFlushRenderBuffer(gc, gc->pc);
 
 #ifdef GLX_DIRECT_RENDERING
-    if (gc->isDirect) {
+    if (gc->driContext) {
       DRI_glXUseXFont(font, first, count, listBase);
       return;
     }
@@ -722,7 +717,7 @@ PUBLIC void glXCopyContext(Display *dpy, GLXContext source,
     }
 
 #ifdef GLX_DIRECT_RENDERING
-    if (gc->isDirect) {
+    if (gc->driContext) {
        /* NOT_DONE: This does not work yet */
     }
 #endif
@@ -794,7 +789,7 @@ PUBLIC Bool glXIsDirect(Display *dpy, GLXContext gc)
     if (!gc) {
        return GL_FALSE;
 #ifdef GLX_DIRECT_RENDERING
-    } else if (gc->isDirect) {
+    } else if (gc->driContext) {
        return GL_TRUE;
 #endif
     }
@@ -857,10 +852,11 @@ PUBLIC void glXSwapBuffers(Display *dpy, GLXDrawable drawable)
     GLXContextTag tag;
     CARD8 opcode;
 #ifdef GLX_DIRECT_RENDERING
-    __DRIdrawable *pdraw = GetDRIDrawable( dpy, drawable, NULL );
+    __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, NULL);
 
-    if ( pdraw != NULL ) {
-       (*pdraw->swapBuffers)(pdraw);
+    if (pdraw != NULL) {
+       glFlush();          
+       (*pdraw->psc->driScreen->swapBuffers)(pdraw);
        return;
     }
 #endif
@@ -1332,7 +1328,7 @@ PUBLIC const char *glXQueryExtensionsString( Display *dpy, int screen )
 
        __glXCalculateUsableExtensions(psc,
 #ifdef GLX_DIRECT_RENDERING
-                                      (psc->driScreen.private != NULL),
+                                      (psc->driScreen != NULL),
 #else
                                       GL_FALSE,
 #endif
@@ -1511,13 +1507,15 @@ static int __glXQueryContextInfo(Display *dpy, GLXContext ctx)
                    ctx->share_xid = *pProp++;
                    break;
                case GLX_VISUAL_ID_EXT:
-                   ctx->vid = *pProp++;
+                   ctx->mode =
+                       _gl_context_modes_find_visual(ctx->psc->visuals, *pProp++);
                    break;
                case GLX_SCREEN:
                    ctx->screen = *pProp++;
                    break;
                case GLX_FBCONFIG_ID:
-                   ctx->fbconfigID = *pProp++;
+                   ctx->mode =
+                       _gl_context_modes_find_fbconfig(ctx->psc->configs, *pProp++);
                    break;
                case GLX_RENDER_TYPE:
                    ctx->renderType = *pProp++;
@@ -1542,7 +1540,11 @@ glXQueryContext(Display *dpy, GLXContext ctx, int attribute, int *value)
     int retVal;
 
     /* get the information from the server if we don't have it already */
-    if (!ctx->isDirect && (ctx->vid == None)) {
+#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;
     }
@@ -1551,13 +1553,13 @@ glXQueryContext(Display *dpy, GLXContext ctx, int attribute, int *value)
        *value = (int)(ctx->share_xid);
        break;
     case GLX_VISUAL_ID_EXT:
-       *value = (int)(ctx->vid);
+       *value = ctx->mode ? ctx->mode->visualID : None;
        break;
     case GLX_SCREEN:
        *value = (int)(ctx->screen);
        break;
     case GLX_FBCONFIG_ID:
-       *value = (int)(ctx->fbconfigID);
+       *value = ctx->mode ? ctx->mode->fbconfigID : None;
        break;
     case GLX_RENDER_TYPE:
        *value = (int)(ctx->renderType);
@@ -1736,14 +1738,14 @@ static int __glXSwapIntervalSGI(int interval)
    }
 
 #ifdef __DRI_SWAP_CONTROL
-   if ( gc->isDirect ) {
+   if (gc->driContext) {
        __GLXscreenConfigs * const psc = GetGLXScreenConfigs( gc->currentDpy,
                                                             gc->screen );
-       __DRIdrawable * const pdraw = GetDRIDrawable( gc->currentDpy,
-                                                    gc->currentDrawable,
-                                                    NULL );
+       __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(gc->currentDpy,
+                                                  gc->currentDrawable,
+                                                  NULL);
        if (psc->swapControl != NULL && pdraw != NULL) {
-          psc->swapControl->setSwapInterval(pdraw, interval);
+          psc->swapControl->setSwapInterval(pdraw->driDrawable, interval);
           return 0;
        }
        else {
@@ -1788,15 +1790,15 @@ static int __glXSwapIntervalMESA(unsigned int interval)
       return GLX_BAD_VALUE;
    }
 
-   if ( (gc != NULL) && gc->isDirect ) {
+   if (gc != NULL && gc->driContext) {
       __GLXscreenConfigs * const psc = GetGLXScreenConfigs( gc->currentDpy,
                                                            gc->screen );
       
-      if ( (psc != NULL) && (psc->driScreen.private != NULL) ) {
-        __DRIdrawable * const pdraw = 
-            GetDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
+      if ( (psc != NULL) && (psc->driScreen != NULL) ) {
+        __GLXDRIdrawable *pdraw = 
+            GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
         if (psc->swapControl != NULL && pdraw != NULL) {
-           psc->swapControl->setSwapInterval(pdraw, interval);
+           psc->swapControl->setSwapInterval(pdraw->driDrawable, interval);
            return 0;
         }
       }
@@ -1814,15 +1816,15 @@ static int __glXGetSwapIntervalMESA(void)
 #ifdef __DRI_SWAP_CONTROL
    GLXContext gc = __glXGetCurrentContext();
 
-   if ( (gc != NULL) && gc->isDirect ) {
+   if (gc != NULL && gc->driContext) {
       __GLXscreenConfigs * const psc = GetGLXScreenConfigs( gc->currentDpy,
                                                            gc->screen );
       
-      if ( (psc != NULL) && (psc->driScreen.private != NULL) ) {
-        __DRIdrawable * const pdraw = 
-            GetDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
+      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);
+           return psc->swapControl->getSwapInterval(pdraw->driDrawable);
         }
       }
    }
@@ -1841,11 +1843,11 @@ static GLint __glXBeginFrameTrackingMESA(Display *dpy, GLXDrawable drawable)
    int   status = GLX_BAD_CONTEXT;
 #ifdef __DRI_FRAME_TRACKING
    int screen;
-   __DRIdrawable * const pdraw = GetDRIDrawable(dpy, drawable, & screen);
+   __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
    __GLXscreenConfigs * const psc = GetGLXScreenConfigs(dpy, screen);
 
    if (pdraw != NULL && psc->frameTracking != NULL)
-       status = psc->frameTracking->frameTracking(pdraw, GL_TRUE);
+       status = psc->frameTracking->frameTracking(pdraw->driDrawable, GL_TRUE);
 #else
    (void) dpy;
    (void) drawable;
@@ -1859,11 +1861,12 @@ static GLint __glXEndFrameTrackingMESA(Display *dpy, GLXDrawable drawable)
    int   status = GLX_BAD_CONTEXT;
 #ifdef __DRI_FRAME_TRACKING
    int screen;
-   __DRIdrawable * const pdraw = GetDRIDrawable(dpy, drawable, & screen);
-   __GLXscreenConfigs * const psc = GetGLXScreenConfigs(dpy, screen);
+   __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, & screen);
+   __GLXscreenConfigs *psc = GetGLXScreenConfigs(dpy, screen);
 
    if (pdraw != NULL && psc->frameTracking != NULL)
-       status = psc->frameTracking->frameTracking(pdraw, GL_FALSE);
+       status = psc->frameTracking->frameTracking(pdraw->driDrawable,
+                                                 GL_FALSE);
 #else
    (void) dpy;
    (void) drawable;
@@ -1878,14 +1881,15 @@ static GLint __glXGetFrameUsageMESA(Display *dpy, GLXDrawable drawable,
    int   status = GLX_BAD_CONTEXT;
 #ifdef __DRI_FRAME_TRACKING
    int screen;
-   __DRIdrawable * const pdraw = GetDRIDrawable(dpy, drawable, & screen);
+   __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, &sbc,
+       status = psc->frameTracking->queryFrameTracking(pdraw->driDrawable,
+                                                      &sbc,
                                                       &missedFrames,
                                                       &lastMissedUsage,
                                                       usage);
@@ -1906,13 +1910,14 @@ static GLint __glXQueryFrameTrackingMESA(Display *dpy, GLXDrawable drawable,
    int   status = GLX_BAD_CONTEXT;
 #ifdef __DRI_FRAME_TRACKING
    int screen;
-   __DRIdrawable * const pdraw = GetDRIDrawable(dpy, drawable, & screen);
+   __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, sbc, missedFrames,
+      status = psc->frameTracking->queryFrameTracking(pdraw->driDrawable,
+                                                     sbc, missedFrames,
                                                      lastMissedUsage, &usage);
    }
 #else
@@ -1939,16 +1944,20 @@ static int __glXGetVideoSyncSGI(unsigned int *count)
    GLXContext gc = __glXGetCurrentContext();
 
 
-   if ( (gc != NULL) && gc->isDirect ) {
+   if (gc != NULL && gc->driContext) {
       __GLXscreenConfigs * const psc = GetGLXScreenConfigs( gc->currentDpy,
                                                            gc->screen );
-      if (psc->msc != NULL && psc->driScreen.private != NULL) {
-        int       ret;
-        int64_t   temp;
+      if ( psc->msc && psc->driScreen ) {
+          __GLXDRIdrawable *pdraw = 
+              GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
+         int64_t temp; 
+         int ret;
+         ret = (*psc->msc->getDrawableMSC)(psc->__driScreen,
+                                           pdraw->driDrawable, &temp);
+         *count = (unsigned) temp;
 
-        ret = psc->msc->getMSC(&psc->driScreen, &temp);
-        *count = (unsigned) temp;
-        return (ret == 0) ? 0 : GLX_BAD_CONTEXT;
+         return (ret == 0) ? 0 : GLX_BAD_CONTEXT;
       }
    }
 #else
@@ -1965,22 +1974,20 @@ static int __glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count
    if ( divisor <= 0 || remainder < 0 )
      return GLX_BAD_VALUE;
 
-   if ( (gc != NULL) && gc->isDirect ) {
+   if (gc != NULL && gc->driContext) {
       __GLXscreenConfigs * const psc = GetGLXScreenConfigs( gc->currentDpy,
                                                            gc->screen );
-      if (psc->msc != NULL && psc->driScreen.private ) {
-        __DRIdrawable * const pdraw = 
-            GetDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
-        if (pdraw != NULL) {
-           int       ret;
-           int64_t   msc;
-           int64_t   sbc;
-
-           ret = (*psc->msc->waitForMSC)(pdraw, 0,
-                                         divisor, remainder, &msc, &sbc);
-           *count = (unsigned) msc;
-           return (ret == 0) ? 0 : GLX_BAD_CONTEXT;
-        }
+      if (psc->msc != NULL && psc->driScreen ) {
+        __GLXDRIdrawable *pdraw = 
+            GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
+        int       ret;
+        int64_t   msc;
+        int64_t   sbc;
+
+        ret = (*psc->msc->waitForMSC)(pdraw->driDrawable, 0,
+                                      divisor, remainder, &msc, &sbc);
+        *count = (unsigned) msc;
+        return (ret == 0) ? 0 : GLX_BAD_CONTEXT;
       }
    }
 #else
@@ -2137,13 +2144,13 @@ static Bool __glXGetSyncValuesOML(Display *dpy, GLXDrawable drawable,
 
     if ( priv != NULL ) {
        int   i;
-       __DRIdrawable * const pdraw = GetDRIDrawable( dpy, drawable, & i );
+       __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &i);
        __GLXscreenConfigs * const psc = &priv->screenConfigs[i];
 
        assert( (pdraw == NULL) || (i != -1) );
        return ( (pdraw && psc->sbc && psc->msc)
-                && ((*psc->msc->getMSC)(&psc->driScreen, msc) == 0)
-                && ((*psc->sbc->getSBC)(pdraw, sbc) == 0)
+                && ((*psc->msc->getMSC)(psc->driScreen, msc) == 0)
+                && ((*psc->sbc->getSBC)(pdraw->driDrawable, sbc) == 0)
                 && (__glXGetUST(ust) == 0) );
     }
 #else
@@ -2156,6 +2163,68 @@ static Bool __glXGetSyncValuesOML(Display *dpy, GLXDrawable drawable,
    return False;
 }
 
+#ifdef GLX_DIRECT_RENDERING
+_X_HIDDEN GLboolean
+__driGetMscRateOML(__DRIdrawable *draw,
+                  int32_t *numerator, int32_t *denominator, void *private)
+{
+#ifdef XF86VIDMODE
+    __GLXscreenConfigs *psc;
+    XF86VidModeModeLine   mode_line;
+    int   dot_clock;
+    int   i;
+    __GLXDRIdrawable *glxDraw = private;
+
+    psc = glxDraw->psc;
+    if (XF86VidModeQueryVersion(psc->dpy, &i, &i) &&
+       XF86VidModeGetModeLine(psc->dpy, psc->scr, &dot_clock, &mode_line) ) {
+       unsigned   n = dot_clock * 1000;
+       unsigned   d = mode_line.vtotal * mode_line.htotal;
+       
+# define V_INTERLACE 0x010
+# define V_DBLSCAN   0x020
+
+       if (mode_line.flags & V_INTERLACE)
+           n *= 2;
+       else if (mode_line.flags & V_DBLSCAN)
+           d *= 2;
+
+       /* The OML_sync_control spec requires that if the refresh rate is a
+        * whole number, that the returned numerator be equal to the refresh
+        * rate and the denominator be 1.
+        */
+
+       if (n % d == 0) {
+           n /= d;
+           d = 1;
+       }
+       else {
+           static const unsigned f[] = { 13, 11, 7, 5, 3, 2, 0 };
+
+           /* This is a poor man's way to reduce a fraction.  It's far from
+            * perfect, but it will work well enough for this situation.
+            */
+
+           for (i = 0; f[i] != 0; i++) {
+               while (n % f[i] == 0 && d % f[i] == 0) {
+                   d /= f[i];
+                   n /= f[i];
+               }
+           }
+       }
+
+       *numerator = n;
+       *denominator = d;
+
+       return True;
+    }
+    else
+       return False;
+#else
+    return False;
+#endif
+}
+#endif
 
 /**
  * Determine the refresh rate of the specified drawable and display.
@@ -2173,71 +2242,20 @@ static Bool __glXGetSyncValuesOML(Display *dpy, GLXDrawable drawable,
  *       when GLX_OML_sync_control appears in the client extension string.
  */
 
-GLboolean __glXGetMscRateOML(__DRIdrawable *draw,
-                            int32_t * numerator, int32_t * denominator)
+_X_HIDDEN GLboolean __glXGetMscRateOML(Display * dpy, GLXDrawable drawable,
+                                      int32_t * numerator,
+                                      int32_t * denominator)
 {
 #if defined( GLX_DIRECT_RENDERING ) && defined( XF86VIDMODE )
-    __GLXdrawable *glxDraw =
-       containerOf(draw, __GLXdrawable, driDrawable);
-    __GLXscreenConfigs *psc = glxDraw->psc;
-    Display *dpy = psc->dpy;
-   __GLXdisplayPrivate * const priv = __glXInitialize(dpy);
-
-
-   if ( priv != NULL ) {
-      XF86VidModeModeLine   mode_line;
-      int   dot_clock;
-      int   i;
-
-
-      if (XF86VidModeQueryVersion( dpy, & i, & i ) &&
-         XF86VidModeGetModeLine(dpy, psc->scr, &dot_clock, &mode_line) ) {
-        unsigned   n = dot_clock * 1000;
-        unsigned   d = mode_line.vtotal * mode_line.htotal;
-
-# define V_INTERLACE 0x010
-# define V_DBLSCAN   0x020
-
-        if ( (mode_line.flags & V_INTERLACE) ) {
-           n *= 2;
-        }
-        else if ( (mode_line.flags & V_DBLSCAN) ) {
-           d *= 2;
-        }
+    __GLXDRIdrawable *draw = GetGLXDRIDrawable(dpy, drawable, NULL);
 
-        /* The OML_sync_control spec requires that if the refresh rate is a
-         * whole number, that the returned numerator be equal to the refresh
-         * rate and the denominator be 1.
-         */
-
-        if ( (n % d) == 0 ) {
-           n /= d;
-           d = 1;
-        }
-        else {
-           static const unsigned f[] = { 13, 11, 7, 5, 3, 2, 0 };
-
-
-           /* This is a poor man's way to reduce a fraction.  It's far from
-            * perfect, but it will work well enough for this situation.
-            */
-
-           for ( i = 0 ; f[i] != 0 ; i++ ) {
-              while ( ((n % f[i]) == 0) && ((d % f[i]) == 0) ) {
-                 d /= f[i];
-                 n /= f[i];
-              }
-           }
-        }
-
-        *numerator = n;
-        *denominator = d;
+    if (draw == NULL)
+       return False;
 
-        return True;
-      }
-   }
+    return __driGetMscRateOML(draw->driDrawable, numerator, denominator, draw);
 #else
-   (void) draw;
+   (void) dpy;
+   (void) drawable;
    (void) numerator;
    (void) denominator;
 #endif
@@ -2251,7 +2269,7 @@ static int64_t __glXSwapBuffersMscOML(Display *dpy, GLXDrawable drawable,
 {
 #ifdef __DRI_SWAP_BUFFER_COUNTER
    int screen;
-   __DRIdrawable *pdraw = GetDRIDrawable( dpy, drawable, & screen );
+   __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
    __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, screen );
 
    /* The OML_sync_control spec says these should "generate a GLX_BAD_VALUE
@@ -2265,7 +2283,7 @@ static int64_t __glXSwapBuffersMscOML(Display *dpy, GLXDrawable drawable,
       return -1;
 
    if (pdraw != NULL && psc->counters != NULL)
-      return (*psc->sbc->swapBuffersMSC)(pdraw, target_msc,
+      return (*psc->sbc->swapBuffersMSC)(pdraw->driDrawable, target_msc,
                                         divisor, remainder);
 
 #else
@@ -2286,7 +2304,7 @@ static Bool __glXWaitForMscOML(Display * dpy, GLXDrawable drawable,
 {
 #ifdef __DRI_MEDIA_STREAM_COUNTER
    int screen;
-   __DRIdrawable *pdraw = GetDRIDrawable( dpy, drawable, & screen );
+   __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
    __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, screen );
    int  ret;
 
@@ -2299,7 +2317,7 @@ static Bool __glXWaitForMscOML(Display * dpy, GLXDrawable drawable,
       return False;
 
    if (pdraw != NULL && psc->msc != NULL) {
-      ret = (*psc->msc->waitForMSC)(pdraw, target_msc,
+      ret = (*psc->msc->waitForMSC)(pdraw->driDrawable, target_msc,
                                    divisor, remainder, msc, sbc);
 
       /* __glXGetUST returns zero on success and non-zero on failure.
@@ -2327,7 +2345,7 @@ static Bool __glXWaitForSbcOML(Display * dpy, GLXDrawable drawable,
 {
 #ifdef __DRI_SWAP_BUFFER_COUNTER
    int screen;
-   __DRIdrawable *pdraw = GetDRIDrawable( dpy, drawable, & screen );
+   __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
    __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, screen );
    int  ret;
 
@@ -2338,7 +2356,7 @@ static Bool __glXWaitForSbcOML(Display * dpy, GLXDrawable drawable,
       return False;
 
    if (pdraw != NULL && psc->sbc != NULL) {
-      ret = (*psc->sbc->waitForSBC)(pdraw, target_sbc, msc, sbc);
+      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.
@@ -2370,9 +2388,8 @@ PUBLIC void *glXAllocateMemoryMESA(Display *dpy, int scrn,
    __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, scrn );
 
    if (psc && psc->allocate)
-       return (*psc->allocate->allocateMemory)( &psc->driScreen, size,
-                                               readFreq, writeFreq,
-                                               priority );
+       return (*psc->allocate->allocateMemory)(psc->__driScreen, size,
+                                              readFreq, writeFreq, priority);
 
 #else
    (void) dpy;
@@ -2393,7 +2410,7 @@ PUBLIC void glXFreeMemoryMESA(Display *dpy, int scrn, void *pointer)
    __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, scrn );
 
    if (psc && psc->allocate)
-        (*psc->allocate->freeMemory)( &psc->driScreen, pointer );
+        (*psc->allocate->freeMemory)(psc->__driScreen, pointer);
 
 #else
    (void) dpy;
@@ -2410,7 +2427,7 @@ PUBLIC GLuint glXGetMemoryOffsetMESA( Display *dpy, int scrn,
    __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, scrn );
 
    if (psc && psc->allocate)
-       return (*psc->allocate->memoryOffset)( &psc->driScreen, pointer );
+       return (*psc->allocate->memoryOffset)(psc->__driScreen, pointer);
 
 #else
    (void) dpy;
@@ -2486,11 +2503,12 @@ static void __glXCopySubBufferMESA(Display *dpy, GLXDrawable drawable,
 
 #ifdef __DRI_COPY_SUB_BUFFER
     int screen;
-    __DRIdrawable *pdraw = GetDRIDrawable( dpy, drawable, & screen );
+    __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
     if ( pdraw != NULL ) {
        __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, screen );
        if (psc->copySubBuffer != NULL) {
-           (*psc->copySubBuffer->copySubBuffer)(pdraw, x, y, width, height);
+           (*psc->copySubBuffer->copySubBuffer)(pdraw->driDrawable,
+                                                x, y, width, height);
        }
 
        return;
@@ -2566,8 +2584,16 @@ static void __glXBindTexImageEXT(Display *dpy,
     }
  
 #ifdef GLX_DIRECT_RENDERING
-    if (gc->isDirect)
+    if (gc->driContext) {
+       __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, NULL);
+
+       if (pdraw != NULL)
+           (*pdraw->psc->texBuffer->setTexBuffer)(gc->__driContext,
+                                                  pdraw->textureTarget,
+                                                  pdraw->driDrawable);
+
        return;
+    }
 #endif
 
     opcode = __glXSetupForCommand(dpy);
@@ -2618,7 +2644,7 @@ static void __glXReleaseTexImageEXT(Display *dpy,
        return;
 
 #ifdef GLX_DIRECT_RENDERING
-    if (gc->isDirect)
+    if (gc->driContext)
        return;
 #endif
 
@@ -2650,7 +2676,7 @@ static void __glXReleaseTexImageEXT(Display *dpy,
  * 
  * \sa strdup
  */
-char *
+_X_HIDDEN char *
 __glXstrdup(const char *str)
 {
    char *copy;
@@ -2877,54 +2903,6 @@ PUBLIC void (*glXGetProcAddress(const GLubyte *procName))( void )
 
 
 #ifdef GLX_DIRECT_RENDERING
-/**
- * Retrieves the verion of the internal libGL API in YYYYMMDD format.  This
- * might be used by the DRI drivers to determine how new libGL is at runtime.
- * Drivers should not call this function directly.  They should instead use
- * \c glXGetProcAddress to obtain a pointer to the function.
- * 
- * \returns An 8-digit decimal number representing the internal libGL API in
- *          YYYYMMDD format.
- * 
- * \sa glXGetProcAddress, PFNGLXGETINTERNALVERSIONPROC
- *
- * \since Internal API version 20021121.
- */
-int __glXGetInternalVersion(void)
-{
-    /* History:
-     * 20021121 - Initial version
-     * 20021128 - Added __glXWindowExists() function
-     * 20021207 - Added support for dynamic GLX extensions,
-     *            GLX_SGI_swap_control, GLX_SGI_video_sync,
-     *            GLX_OML_sync_control, and GLX_MESA_swap_control.
-     *            Never officially released.  Do NOT test against
-     *            this version.  Use 20030317 instead.
-     * 20030317 - Added support GLX_SGIX_fbconfig,
-     *            GLX_MESA_swap_frame_usage, GLX_OML_swap_method,
-     *            GLX_{ARB,SGIS}_multisample, and
-     *            GLX_SGIX_visual_select_group.
-     * 20030606 - Added support for GLX_SGI_make_current_read.
-     * 20030813 - Made support for dynamic extensions multi-head aware.
-     * 20030818 - Added support for GLX_MESA_allocate_memory in place of the
-     *            deprecated GLX_NV_vertex_array_range & GLX_MESA_agp_offset
-     *            interfaces.
-     * 20031201 - Added support for the first round of DRI interface changes.
-     *            Do NOT test against this version!  It has binary
-     *            compatibility bugs, use 20040317 instead.
-     * 20040317 - Added the 'mode' field to __DRIcontextRec.
-     * 20040415 - Added support for bindContext3 and unbindContext3.
-     * 20040602 - Add __glXGetDrawableInfo.  I though that was there
-     *            months ago. :(
-     * 20050727 - Gut all the old interfaces.  This breaks compatability with
-     *            any DRI driver built to any previous version.
-     * 20060314 - Added support for GLX_MESA_copy_sub_buffer.
-     * 20070105 - Added support for damage reporting.
-     */
-    return 20070105;
-}
-
-
 /**
  * Get the unadjusted system time (UST).  Currently, the UST is measured in
  * microseconds since Epoc.  The actual resolution of the UST may vary from
@@ -2939,7 +2917,7 @@ int __glXGetInternalVersion(void)
  *
  * \since Internal API version 20030317.
  */
-int __glXGetUST( int64_t * ust )
+_X_HIDDEN int __glXGetUST( int64_t * ust )
 {
     struct timeval  tv;