DRI2: Drop sarea, implement swap buffers in the X server.
[mesa.git] / src / glx / x11 / glxcmds.c
index 0951493726947c2de04e71da7c4c7ffa19daa8ab..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";
@@ -80,24 +75,18 @@ static int 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, int screen)
+static void GarbageCollectDRIDrawables(Display *dpy, __GLXscreenConfigs *sc)
 {
-    __GLXdisplayPrivate * const priv = __glXInitialize(dpy);
-    __GLXscreenConfigs *sc;
-    __DRIid draw;
-    __DRIdrawable *pdraw;
+    XID draw;
+    __GLXDRIdrawable *pdraw;
     XWindowAttributes xwa;
     int (*oldXErrorHandler)(Display *, XErrorEvent *);
 
-    if (priv == NULL || priv->driDisplay.private == NULL)
-       return;
-
     /* 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);
     oldXErrorHandler = XSetErrorHandler(windowExistsErrorHandler);
 
-    sc = &priv->screenConfigs[screen];
     if (__glxHashFirst(sc->drawHash, &draw, (void *)&pdraw) == 1) {
        do {
            windowExistsFlag = GL_TRUE;
@@ -105,15 +94,19 @@ static void GarbageCollectDRIDrawables(Display *dpy, int screen)
            if (!windowExistsFlag) {
                /* Destroy the local drawable data, if the drawable no
                   longer exists in the Xserver */
-               (*pdraw->destroyDrawable)(dpy, pdraw->private);
-               Xfree(pdraw);
+               (*pdraw->destroyDrawable)(pdraw);
+                __glxHashDelete(sc->drawHash, draw);
            }
        } while (__glxHashNext(sc->drawHash, &draw, (void *)&pdraw) == 1);
     }
 
+    XSync(dpy, GL_FALSE);
     XSetErrorHandler(oldXErrorHandler);
 }
 
+extern __GLXDRIdrawable *
+GetGLXDRIDrawable(Display *dpy, GLXDrawable drawable, int * const scrn_num);
+
 /**
  * Get the __DRIdrawable for the drawable associated with a GLXContext
  * 
@@ -123,21 +116,24 @@ static void GarbageCollectDRIDrawables(Display *dpy, int screen)
  * \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);
-    __DRIdrawable * 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;
@@ -316,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
@@ -364,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;
@@ -377,41 +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;
 
-           /* 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) {
-               void * const shared = (shareList != NULL)
-                   ? shareList->driContext.private : NULL;
-               gc->driContext.private = 
-                 (*psc->driScreen.createNewContext)( dpy, mode, renderType,
-                                                     shared,
-                                                     &gc->driContext );
-               if (gc->driContext.private) {
-                   gc->isDirect = GL_TRUE;
-                   gc->screen = mode->screen;
-                   gc->vid = mode->visualID;
-                   gc->fbconfigID = mode->fbconfigID;
-                   gc->mode = mode;
-               }
+           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
@@ -428,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;
@@ -442,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;
@@ -460,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);
@@ -482,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);
@@ -518,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)(dpy, gc->screen,
-                                            gc->driContext.private);
-           gc->driContext.private = NULL;
-       }
-       GarbageCollectDRIDrawables(dpy, gc->screen);
+    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();
@@ -605,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
@@ -641,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;
     }
@@ -671,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;
     }
@@ -711,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
@@ -783,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
     }
@@ -846,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)(dpy, pdraw->private);
+    if (pdraw != NULL) {
+       glFlush();          
+       (*pdraw->psc->driScreen->swapBuffers)(pdraw);
        return;
     }
 #endif
@@ -893,12 +900,12 @@ PUBLIC int glXGetConfig(Display *dpy, XVisualInfo *vis, int attribute,
 {
     __GLXdisplayPrivate *priv;
     __GLXscreenConfigs *psc;
+    __GLcontextModes *modes;
     int   status;
 
     status = GetGLXPrivScreenConfig( dpy, vis->screen, & priv, & psc );
     if ( status == Success ) {
-       const __GLcontextModes * const modes = _gl_context_modes_find_visual(
-                                            psc->configs, vis->visualid );
+       modes = _gl_context_modes_find_visual(psc->visuals, vis->visualid);
 
        /* Lookup attribute after first finding a match on the visual */
        if ( modes != NULL ) {
@@ -1276,7 +1283,7 @@ PUBLIC XVisualInfo *glXChooseVisual(Display *dpy, int screen, int *attribList)
     ** Compute a score for those that do
     ** Remember which visual, if any, got the highest score
     */
-    for ( modes = psc->configs ; modes != NULL ; modes = modes->next ) {
+    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)) ) {
@@ -1321,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
@@ -1500,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++;
@@ -1531,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;
     }
@@ -1540,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);
@@ -1644,6 +1657,7 @@ PUBLIC GLXFBConfig *glXGetFBConfigs(Display *dpy, int screen, int *nelements)
     __GLcontextModes ** config = NULL;
     int   i;
 
+    *nelements = 0;
     if ( (priv->screenConfigs != NULL)
         && (screen >= 0) && (screen <= ScreenCount(dpy))
         && (priv->screenConfigs[screen].configs != NULL)
@@ -1668,8 +1682,10 @@ PUBLIC GLXFBConfig *glXGetFBConfigs(Display *dpy, int screen, int *nelements)
            for ( modes = priv->screenConfigs[screen].configs
                  ; modes != NULL
                  ; modes = modes->next ) {
-               config[i] = modes;
-               i++;
+               if ( modes->fbconfigID != GLX_DONT_CARE ) {
+                   config[i] = modes;
+                   i++;
+               }
            }
        }
     }
@@ -1721,16 +1737,15 @@ static int __glXSwapIntervalSGI(int interval)
       return GLX_BAD_VALUE;
    }
 
-#ifdef GLX_DIRECT_RENDERING
-   if ( gc->isDirect ) {
+#ifdef __DRI_SWAP_CONTROL
+   if (gc->driContext) {
        __GLXscreenConfigs * const psc = GetGLXScreenConfigs( gc->currentDpy,
                                                             gc->screen );
-       __DRIdrawable * const pdraw = GetDRIDrawable( gc->currentDpy,
-                                                    gc->currentDrawable,
-                                                    NULL );
-       if ( __glXExtensionBitIsEnabled( psc, SGI_swap_control_bit )
-           && (pdraw != NULL) ) {
-          pdraw->swap_interval = interval;
+       __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(gc->currentDpy,
+                                                  gc->currentDrawable,
+                                                  NULL);
+       if (psc->swapControl != NULL && pdraw != NULL) {
+          psc->swapControl->setSwapInterval(pdraw->driDrawable, interval);
           return 0;
        }
        else {
@@ -1768,23 +1783,22 @@ static int __glXSwapIntervalSGI(int interval)
 */
 static int __glXSwapIntervalMESA(unsigned int interval)
 {
-#ifdef GLX_DIRECT_RENDERING
+#ifdef __DRI_SWAP_CONTROL
    GLXContext gc = __glXGetCurrentContext();
 
    if ( interval < 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 != NULL) && (psc->driScreen.private != NULL)
-          && __glXExtensionBitIsEnabled( psc, MESA_swap_control_bit ) ) {
-        __DRIdrawable * const pdraw = 
-            GetDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
-        if ( pdraw != NULL ) {
-           pdraw->swap_interval = interval;
+      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;
         }
       }
@@ -1799,19 +1813,18 @@ static int __glXSwapIntervalMESA(unsigned int interval)
 
 static int __glXGetSwapIntervalMESA(void)
 {
-#ifdef GLX_DIRECT_RENDERING
+#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)
-          && __glXExtensionBitIsEnabled( psc, MESA_swap_control_bit ) ) {
-        __DRIdrawable * const pdraw = 
-            GetDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
-        if ( pdraw != NULL ) {
-           return pdraw->swap_interval;
+      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);
         }
       }
    }
@@ -1828,15 +1841,13 @@ static int __glXGetSwapIntervalMESA(void)
 static GLint __glXBeginFrameTrackingMESA(Display *dpy, GLXDrawable drawable)
 {
    int   status = GLX_BAD_CONTEXT;
-#ifdef GLX_DIRECT_RENDERING
+#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) && (pdraw->frameTracking != NULL)
-       && __glXExtensionBitIsEnabled( psc, MESA_swap_frame_usage_bit ) ) {
-      status = pdraw->frameTracking( dpy, pdraw->private, GL_TRUE );
-   }
+   if (pdraw != NULL && psc->frameTracking != NULL)
+       status = psc->frameTracking->frameTracking(pdraw->driDrawable, GL_TRUE);
 #else
    (void) dpy;
    (void) drawable;
@@ -1848,15 +1859,14 @@ static GLint __glXBeginFrameTrackingMESA(Display *dpy, GLXDrawable drawable)
 static GLint __glXEndFrameTrackingMESA(Display *dpy, GLXDrawable drawable)
 {
    int   status = GLX_BAD_CONTEXT;
-#ifdef GLX_DIRECT_RENDERING
+#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) && (pdraw->frameTracking != NULL)
-       && __glXExtensionBitIsEnabled( psc, MESA_swap_frame_usage_bit ) ) {
-      status = pdraw->frameTracking( dpy, pdraw->private, GL_FALSE );
-   }
+   if (pdraw != NULL && psc->frameTracking != NULL)
+       status = psc->frameTracking->frameTracking(pdraw->driDrawable,
+                                                 GL_FALSE);
 #else
    (void) dpy;
    (void) drawable;
@@ -1869,19 +1879,20 @@ static GLint __glXGetFrameUsageMESA(Display *dpy, GLXDrawable drawable,
                                    GLfloat *usage)
 {
    int   status = GLX_BAD_CONTEXT;
-#ifdef GLX_DIRECT_RENDERING
+#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 ) && (pdraw->queryFrameTracking != NULL)
-       && __glXExtensionBitIsEnabled( psc, MESA_swap_frame_usage_bit ) ) {
-      int64_t sbc, missedFrames;
-      float   lastMissedUsage;
+   if (pdraw != NULL && psc->frameTracking != NULL) {
+       int64_t sbc, missedFrames;
+       float   lastMissedUsage;
 
-      status = pdraw->queryFrameTracking( dpy, pdraw->private, &sbc,
-                                         &missedFrames, &lastMissedUsage,
-                                         usage );
+       status = psc->frameTracking->queryFrameTracking(pdraw->driDrawable,
+                                                      &sbc,
+                                                      &missedFrames,
+                                                      &lastMissedUsage,
+                                                      usage);
    }
 #else
    (void) dpy;
@@ -1897,18 +1908,17 @@ static GLint __glXQueryFrameTrackingMESA(Display *dpy, GLXDrawable drawable,
                                         GLfloat *lastMissedUsage)
 {
    int   status = GLX_BAD_CONTEXT;
-#ifdef GLX_DIRECT_RENDERING
+#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 ) && (pdraw->queryFrameTracking != NULL)
-       && __glXExtensionBitIsEnabled( psc, MESA_swap_frame_usage_bit ) ) {
+   if (pdraw != NULL && psc->frameTracking != NULL) {
       float   usage;
 
-      status = pdraw->queryFrameTracking( dpy, pdraw->private, sbc,
-                                         missedFrames, lastMissedUsage,
-                                         & usage );
+      status = psc->frameTracking->queryFrameTracking(pdraw->driDrawable,
+                                                     sbc, missedFrames,
+                                                     lastMissedUsage, &usage);
    }
 #else
    (void) dpy;
@@ -1930,21 +1940,24 @@ static int __glXGetVideoSyncSGI(unsigned int *count)
     * FIXME: there should be a GLX encoding for this call.  I can find no
     * FIXME: documentation for the GLX encoding.
     */
-#ifdef GLX_DIRECT_RENDERING
+#ifdef __DRI_MEDIA_STREAM_COUNTER
    GLXContext gc = __glXGetCurrentContext();
 
 
-   if ( (gc != NULL) && gc->isDirect ) {
+   if (gc != NULL && gc->driContext) {
       __GLXscreenConfigs * const psc = GetGLXScreenConfigs( gc->currentDpy,
                                                            gc->screen );
-      if ( __glXExtensionBitIsEnabled( psc, SGI_video_sync_bit )
-          && psc->driScreen.private && psc->driScreen.getMSC) {
-        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->driScreen.getMSC( psc->driScreen.private, & temp );
-        *count = (unsigned) temp;
-        return (ret == 0) ? 0 : GLX_BAD_CONTEXT;
+         return (ret == 0) ? 0 : GLX_BAD_CONTEXT;
       }
    }
 #else
@@ -1955,30 +1968,26 @@ static int __glXGetVideoSyncSGI(unsigned int *count)
 
 static int __glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
 {
-#ifdef GLX_DIRECT_RENDERING
+#ifdef __DRI_MEDIA_STREAM_COUNTER
    GLXContext gc = __glXGetCurrentContext();
 
    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 ( __glXExtensionBitIsEnabled( psc, SGI_video_sync_bit )
-          && psc->driScreen.private ) {
-        __DRIdrawable * const pdraw = 
-            GetDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
-        if ( (pdraw != NULL) && (pdraw->waitForMSC != NULL) ) {
-           int       ret;
-           int64_t   msc;
-           int64_t   sbc;
-
-           ret = (*pdraw->waitForMSC)( gc->currentDpy, pdraw->private,
-                                       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
@@ -2130,20 +2139,19 @@ static Bool __glXQueryMaxSwapBarriersSGIX(Display *dpy, int screen, int *max)
 static Bool __glXGetSyncValuesOML(Display *dpy, GLXDrawable drawable,
                                  int64_t *ust, int64_t *msc, int64_t *sbc)
 {
-#ifdef GLX_DIRECT_RENDERING
+#if defined(__DRI_SWAP_BUFFER_COUNTER) && defined(__DRI_MEDIA_STREAM_COUNTER)
     __GLXdisplayPrivate * const priv = __glXInitialize(dpy);
 
     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 && pdraw->getSBC && psc->driScreen.getMSC)
-                && __glXExtensionBitIsEnabled( psc, OML_sync_control_bit )
-                && ((*psc->driScreen.getMSC)( psc->driScreen.private, msc ) == 0)
-                && ((*pdraw->getSBC)( dpy, psc->driScreen.private, sbc ) == 0)
-                && (__glXGetUST( ust ) == 0) );
+       return ( (pdraw && psc->sbc && psc->msc)
+                && ((*psc->msc->getMSC)(psc->driScreen, msc) == 0)
+                && ((*psc->sbc->getSBC)(pdraw->driDrawable, sbc) == 0)
+                && (__glXGetUST(ust) == 0) );
     }
 #else
    (void) dpy;
@@ -2155,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.
@@ -2172,69 +2242,17 @@ static Bool __glXGetSyncValuesOML(Display *dpy, GLXDrawable drawable,
  *       when GLX_OML_sync_control appears in the client extension string.
  */
 
-Bool __glXGetMscRateOML(Display * dpy, GLXDrawable drawable,
-                       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 )
-   __GLXdisplayPrivate * const priv = __glXInitialize(dpy);
-
-
-   if ( priv != NULL ) {
-      XF86VidModeModeLine   mode_line;
-      int   dot_clock;
-      int   screen_num;
-      int   i;
+    __GLXDRIdrawable *draw = GetGLXDRIDrawable(dpy, drawable, NULL);
 
+    if (draw == NULL)
+       return False;
 
-      if (GetDRIDrawable( dpy, drawable, & screen_num) != NULL
-         && XF86VidModeQueryVersion( dpy, & i, & i )
-         && XF86VidModeGetModeLine( dpy, screen_num, & 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;
-
-        (void) drawable;
-        return True;
-      }
-   }
+    return __driGetMscRateOML(draw->driDrawable, numerator, denominator, draw);
 #else
    (void) dpy;
    (void) drawable;
@@ -2249,9 +2267,9 @@ static int64_t __glXSwapBuffersMscOML(Display *dpy, GLXDrawable drawable,
                                      int64_t target_msc, int64_t divisor,
                                      int64_t remainder)
 {
-#ifdef GLX_DIRECT_RENDERING
+#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
@@ -2264,11 +2282,10 @@ static int64_t __glXSwapBuffersMscOML(Display *dpy, GLXDrawable drawable,
    if ( divisor > 0 && remainder >= divisor )
       return -1;
 
-   if ( (pdraw != NULL) && (pdraw->swapBuffersMSC != NULL)
-       && __glXExtensionBitIsEnabled( psc, OML_sync_control_bit ) ) {
-      return (*pdraw->swapBuffersMSC)(dpy, pdraw->private, target_msc,
-                                     divisor, remainder);
-   }
+   if (pdraw != NULL && psc->counters != NULL)
+      return (*psc->sbc->swapBuffersMSC)(pdraw->driDrawable, target_msc,
+                                        divisor, remainder);
+
 #else
    (void) dpy;
    (void) drawable;
@@ -2285,9 +2302,9 @@ static Bool __glXWaitForMscOML(Display * dpy, GLXDrawable drawable,
                               int64_t remainder, int64_t *ust,
                               int64_t *msc, int64_t *sbc)
 {
-#ifdef GLX_DIRECT_RENDERING
+#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,10 +2316,9 @@ static Bool __glXWaitForMscOML(Display * dpy, GLXDrawable drawable,
    if ( divisor > 0 && remainder >= divisor )
       return False;
 
-   if ( (pdraw != NULL) && (pdraw->waitForMSC != NULL)
-       && __glXExtensionBitIsEnabled( psc, OML_sync_control_bit ) ) {
-      ret = (*pdraw->waitForMSC)( dpy, pdraw->private, target_msc,
-                                 divisor, remainder, msc, sbc );
+   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.
@@ -2327,9 +2343,9 @@ static Bool __glXWaitForSbcOML(Display * dpy, GLXDrawable drawable,
                               int64_t target_sbc, int64_t *ust,
                               int64_t *msc, int64_t *sbc )
 {
-#ifdef GLX_DIRECT_RENDERING
+#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;
 
@@ -2339,9 +2355,8 @@ static Bool __glXWaitForSbcOML(Display * dpy, GLXDrawable drawable,
    if ( target_sbc < 0 )
       return False;
 
-   if ( (pdraw != NULL) && (pdraw->waitForSBC != NULL)
-       && __glXExtensionBitIsEnabled( psc, OML_sync_control_bit )) {
-      ret = (*pdraw->waitForSBC)( dpy, pdraw->private, target_sbc, msc, sbc );
+   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.
@@ -2369,16 +2384,13 @@ PUBLIC void *glXAllocateMemoryMESA(Display *dpy, int scrn,
                                   size_t size, float readFreq,
                                   float writeFreq, float priority)
 {
-#ifdef GLX_DIRECT_RENDERING
+#ifdef __DRI_ALLOCATE
    __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, scrn );
 
-   if ( __glXExtensionBitIsEnabled( psc, MESA_allocate_memory_bit ) ) {
-      if (psc && psc->driScreen.private && psc->driScreen.allocateMemory) {
-        return (*psc->driScreen.allocateMemory)( dpy, scrn, size,
-                                                 readFreq, writeFreq,
-                                                 priority );
-      }
-   }
+   if (psc && psc->allocate)
+       return (*psc->allocate->allocateMemory)(psc->__driScreen, size,
+                                              readFreq, writeFreq, priority);
+
 #else
    (void) dpy;
    (void) scrn;
@@ -2394,14 +2406,12 @@ PUBLIC void *glXAllocateMemoryMESA(Display *dpy, int scrn,
 
 PUBLIC void glXFreeMemoryMESA(Display *dpy, int scrn, void *pointer)
 {
-#ifdef GLX_DIRECT_RENDERING
+#ifdef __DRI_ALLOCATE
    __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, scrn );
 
-   if ( __glXExtensionBitIsEnabled( psc, MESA_allocate_memory_bit ) ) {
-      if (psc && psc->driScreen.private && psc->driScreen.freeMemory) {
-        (*psc->driScreen.freeMemory)( dpy, scrn, pointer );
-      }
-   }
+   if (psc && psc->allocate)
+        (*psc->allocate->freeMemory)(psc->__driScreen, pointer);
+
 #else
    (void) dpy;
    (void) scrn;
@@ -2413,14 +2423,12 @@ PUBLIC void glXFreeMemoryMESA(Display *dpy, int scrn, void *pointer)
 PUBLIC GLuint glXGetMemoryOffsetMESA( Display *dpy, int scrn,
                                      const void *pointer )
 {
-#ifdef GLX_DIRECT_RENDERING
+#ifdef __DRI_ALLOCATE
    __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, scrn );
 
-   if ( __glXExtensionBitIsEnabled( psc, MESA_allocate_memory_bit ) ) {
-      if (psc && psc->driScreen.private && psc->driScreen.memoryOffset) {
-        return (*psc->driScreen.memoryOffset)( dpy, scrn, pointer );
-      }
-   }
+   if (psc && psc->allocate)
+       return (*psc->allocate->memoryOffset)(psc->__driScreen, pointer);
+
 #else
    (void) dpy;
    (void) scrn;
@@ -2493,13 +2501,14 @@ static void __glXCopySubBufferMESA(Display *dpy, GLXDrawable drawable,
     INT32 *x_ptr, *y_ptr, *w_ptr, *h_ptr;
     CARD8 opcode;
 
-#ifdef GLX_DIRECT_RENDERING
+#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 ( __glXExtensionBitIsEnabled( psc, MESA_copy_sub_buffer_bit ) ) {
-           (*pdraw->copySubBuffer)(dpy, pdraw->private, x, y, width, height);
+       if (psc->copySubBuffer != NULL) {
+           (*psc->copySubBuffer->copySubBuffer)(pdraw->driDrawable,
+                                                x, y, width, height);
        }
 
        return;
@@ -2575,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);
@@ -2627,7 +2644,7 @@ static void __glXReleaseTexImageEXT(Display *dpy,
        return;
 
 #ifdef GLX_DIRECT_RENDERING
-    if (gc->isDirect)
+    if (gc->driContext)
        return;
 #endif
 
@@ -2659,7 +2676,7 @@ static void __glXReleaseTexImageEXT(Display *dpy,
  * 
  * \sa strdup
  */
-char *
+_X_HIDDEN char *
 __glXstrdup(const char *str)
 {
    char *copy;
@@ -2886,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
@@ -2948,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;