Merge branch '7.8'
[mesa.git] / src / glx / glxcmds.c
index 9b4a6da065ca01d6d64f96ce84c616a9f8fe08d0..be9d6f29c01e6770dcccfe08eca27d73d355b67e 100644 (file)
 #include "glcontextmodes.h"
 
 #ifdef GLX_DIRECT_RENDERING
+#ifdef GLX_USE_APPLEGL
+#include "apple_glx_context.h"
+#include "apple_glx.h"
+#include "glx_error.h"
+#define GC_IS_DIRECT(gc) ((gc)->isDirect)
+#else
 #include <sys/time.h>
 #include <X11/extensions/xf86vmode.h>
 #include "xf86dri.h"
 #define GC_IS_DIRECT(gc) ((gc)->driContext != NULL)
+#endif
 #else
 #define GC_IS_DIRECT(gc) (0)
 #endif
@@ -59,7 +66,7 @@ static const char __glXGLXClientVersion[] = "1.4";
 
 /****************************************************************************/
 
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
 
 static Bool windowExistsFlag;
 static int
@@ -110,10 +117,6 @@ 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
  *
@@ -348,6 +351,12 @@ AllocateGLXContext(Display * dpy)
       bufSize = __GLX_MAX_RENDER_CMD_SIZE;
    }
    gc->maxSmallRenderCommandSize = bufSize;
+   
+#ifdef GLX_USE_APPLEGL
+   gc->driContext = NULL;
+   gc->do_destroy = False;   
+#endif
+
    return gc;
 }
 
@@ -362,7 +371,7 @@ AllocateGLXContext(Display * dpy)
  */
 
 static GLXContext
-CreateContext(Display * dpy, XVisualInfo * vis,
+CreateContext(Display * dpy, int generic_id,
               const __GLcontextModes * const fbconfig,
               GLXContext shareList,
               Bool allowDirect,
@@ -370,19 +379,25 @@ CreateContext(Display * dpy, XVisualInfo * vis,
 {
    GLXContext gc;
 #ifdef GLX_DIRECT_RENDERING
+#ifdef GLX_USE_APPLEGL
+   int errorcode;
+   bool x11error;
+#else
    __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, screen);
 #endif
-
+#endif
+    
    if (dpy == NULL)
       return NULL;
 
-   gc = AllocateGLXContext(dpy);
-   if (!gc)
+   if (generic_id == None)
       return NULL;
 
-   if ((vis == NULL) && (fbconfig == NULL))
+   gc = AllocateGLXContext(dpy);
+   if (!gc)
       return NULL;
 
+#ifndef GLX_USE_APPLEGL  /* TODO: darwin indirect */
 #ifdef GLX_DIRECT_RENDERING
    if (allowDirect && psc->driScreen) {
       gc->driContext = psc->driScreen->createContext(psc, fbconfig, gc,
@@ -406,7 +421,7 @@ CreateContext(Display * dpy, XVisualInfo * vis,
       req->reqType = gc->majorOpcode;
       req->glxCode = X_GLXCreateContext;
       req->context = gc->xid = XAllocID(dpy);
-      req->visual = vis->visualid;
+      req->visual = generic_id;
       req->screen = screen;
       req->shareList = shareList ? shareList->xid : None;
       req->isDirect = GC_IS_DIRECT(gc);
@@ -421,7 +436,7 @@ CreateContext(Display * dpy, XVisualInfo * vis,
       req->reqType = gc->majorOpcode;
       req->glxCode = X_GLXCreateNewContext;
       req->context = gc->xid = XAllocID(dpy);
-      req->fbconfig = fbconfig->fbconfigID;
+      req->fbconfig = generic_id;
       req->screen = screen;
       req->renderType = renderType;
       req->shareList = shareList ? shareList->xid : None;
@@ -442,7 +457,7 @@ CreateContext(Display * dpy, XVisualInfo * vis,
       req->glxCode = X_GLXVendorPrivateWithReply;
       req->vendorCode = X_GLXvop_CreateContextWithConfigSGIX;
       req->context = gc->xid = XAllocID(dpy);
-      req->fbconfig = fbconfig->fbconfigID;
+      req->fbconfig = generic_id;
       req->screen = screen;
       req->renderType = renderType;
       req->shareList = shareList ? shareList->xid : None;
@@ -459,10 +474,26 @@ CreateContext(Display * dpy, XVisualInfo * vis,
 
    UnlockDisplay(dpy);
    SyncHandle();
+#endif
 
    gc->imported = GL_FALSE;
    gc->renderType = renderType;
 
+   /* TODO: darwin: Integrate with above to do indirect */
+#ifdef GLX_USE_APPLEGL
+   if(apple_glx_create_context(&gc->driContext, dpy, screen, fbconfig, 
+                               shareList ? shareList->driContext : NULL,
+                               &errorcode, &x11error)) {
+      __glXSendError(dpy, errorcode, 0, X_GLXCreateContext, x11error);
+      __glXFreeContext(gc);
+      return NULL;
+   }
+   
+   gc->currentContextTag = -1;
+   gc->mode = fbconfig;
+   gc->isDirect = allowDirect;
+#endif
+
    return gc;
 }
 
@@ -473,7 +504,7 @@ glXCreateContext(Display * dpy, XVisualInfo * vis,
    const __GLcontextModes *mode = NULL;
    int renderType = 0;
 
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) || defined(GLX_USE_APPLEGL)
    __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, vis->screen);
 
    mode = _gl_context_modes_find_visual(psc->visuals, vis->visualid);
@@ -493,7 +524,7 @@ glXCreateContext(Display * dpy, XVisualInfo * vis,
    renderType = mode->rgbMode ? GLX_RGBA_TYPE : GLX_COLOR_INDEX_TYPE;
 #endif
 
-   return CreateContext(dpy, vis, mode, shareList, allowDirect,
+   return CreateContext(dpy, vis->visualid, mode, shareList, allowDirect,
                         X_GLXCreateContext, renderType, vis->screen);
 }
 
@@ -521,6 +552,7 @@ __glXFreeContext(__GLXcontext * gc)
 static void
 DestroyContext(Display * dpy, GLXContext gc)
 {
+#ifndef GLX_USE_APPLEGL /* TODO: darwin: indirect */
    xGLXDestroyContextReq *req;
    GLXContextID xid;
    CARD8 opcode;
@@ -546,7 +578,7 @@ DestroyContext(Display * dpy, GLXContext gc)
       return;
    }
 
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
    /* Destroy the direct rendering context */
    if (gc->driContext) {
       (*gc->driContext->destroyContext) (gc->driContext, gc->psc, dpy);
@@ -556,17 +588,31 @@ DestroyContext(Display * dpy, GLXContext gc)
 #endif
 
    __glXFreeVertexArrayState(gc);
+#else
+   __glXLock();
+#endif /* GLX_USE_APPLEGL */   
 
    if (gc->currentDpy) {
+#ifdef GLX_USE_APPLEGL
+      /* 
+       * Set the Bool that indicates that we should destroy this GLX context
+       * when the context is no longer current.
+       */
+      gc->do_destroy = True;
+#endif
       /* Have to free later cuz it's in use now */
       __glXUnlock();
    }
    else {
       /* Destroy the handle if not current to anybody */
       __glXUnlock();
+#ifdef GLX_USE_APPLEGL
+      if(gc->driContext)
+         apple_glx_destroy_context(&gc->driContext, dpy);
+#endif
       __glXFreeContext(gc);
    }
-
+#ifndef GLX_USE_APPLEGL
    if (!imported) {
       /*
        ** This dpy also created the server side part of the context.
@@ -580,6 +626,7 @@ DestroyContext(Display * dpy, GLXContext gc)
       UnlockDisplay(dpy);
       SyncHandle();
    }
+#endif
 }
 
 PUBLIC void
@@ -634,7 +681,9 @@ glXQueryExtension(Display * dpy, int *errorBase, int *eventBase)
 PUBLIC void
 glXWaitGL(void)
 {
+#ifndef GLX_USE_APPLEGL
    xGLXWaitGLReq *req;
+#endif
    GLXContext gc = __glXGetCurrentContext();
    Display *dpy = gc->currentDpy;
 
@@ -643,8 +692,10 @@ glXWaitGL(void)
 
    /* Flush any pending commands out */
    __glXFlushRenderBuffer(gc, gc->pc);
-
-#ifdef GLX_DIRECT_RENDERING
+#ifdef GLX_USE_APPLEGL
+   glFinish();
+#else
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
    if (gc->driContext) {
       int screen;
       __GLXDRIdrawable *pdraw =
@@ -668,6 +719,7 @@ glXWaitGL(void)
    req->contextTag = gc->currentContextTag;
    UnlockDisplay(dpy);
    SyncHandle();
+#endif /* GLX_USE_APPLEGL */
 }
 
 /*
@@ -677,7 +729,9 @@ glXWaitGL(void)
 PUBLIC void
 glXWaitX(void)
 {
+#ifndef GLX_USE_APPLEGL
    xGLXWaitXReq *req;
+#endif
    GLXContext gc = __glXGetCurrentContext();
    Display *dpy = gc->currentDpy;
 
@@ -687,7 +741,10 @@ glXWaitX(void)
    /* Flush any pending commands out */
    __glXFlushRenderBuffer(gc, gc->pc);
 
-#ifdef GLX_DIRECT_RENDERING
+#ifdef GLX_USE_APPLEGL
+   apple_glx_waitx(dpy, gc->driContext);
+#else
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
    if (gc->driContext) {
       int screen;
       __GLXDRIdrawable *pdraw =
@@ -714,12 +771,15 @@ glXWaitX(void)
    req->contextTag = gc->currentContextTag;
    UnlockDisplay(dpy);
    SyncHandle();
+#endif /* GLX_USE_APPLEGL */
 }
 
 PUBLIC void
 glXUseXFont(Font font, int first, int count, int listBase)
 {
+#ifndef GLX_USE_APPLEGL
    xGLXUseXFontReq *req;
+#endif
    GLXContext gc = __glXGetCurrentContext();
    Display *dpy = gc->currentDpy;
 
@@ -728,8 +788,10 @@ glXUseXFont(Font font, int first, int count, int listBase)
 
    /* Flush any pending commands out */
    (void) __glXFlushRenderBuffer(gc, gc->pc);
-
-#ifdef GLX_DIRECT_RENDERING
+#ifdef GLX_USE_APPLEGL
+   DRI_glXUseXFont(font, first, count, listBase); 
+#else
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
    if (gc->driContext) {
       DRI_glXUseXFont(font, first, count, listBase);
       return;
@@ -748,6 +810,7 @@ glXUseXFont(Font font, int first, int count, int listBase)
    req->listBase = listBase;
    UnlockDisplay(dpy);
    SyncHandle();
+#endif /* GLX_USE_APPLEGL */
 }
 
 /************************************************************************/
@@ -760,6 +823,17 @@ PUBLIC void
 glXCopyContext(Display * dpy, GLXContext source,
                GLXContext dest, unsigned long mask)
 {
+#ifdef GLX_USE_APPLEGL
+   GLXContext gc = __glXGetCurrentContext();
+   int errorcode;
+   bool x11error;
+
+   if(apple_glx_copy_context(gc->driContext, source->driContext, dest->driContext,
+                             mask, &errorcode, &x11error)) {
+      __glXSendError(dpy, errorcode, 0, X_GLXCopyContext, x11error);
+   }
+   
+#else
    xGLXCopyContextReq *req;
    GLXContext gc = __glXGetCurrentContext();
    GLXContextTag tag;
@@ -770,7 +844,7 @@ glXCopyContext(Display * dpy, GLXContext source,
       return;
    }
 
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
    if (gc->driContext) {
       /* NOT_DONE: This does not work yet */
    }
@@ -798,6 +872,7 @@ glXCopyContext(Display * dpy, GLXContext source,
    req->contextTag = tag;
    UnlockDisplay(dpy);
    SyncHandle();
+#endif /* GLX_USE_APPLEGL */
 }
 
 
@@ -812,6 +887,9 @@ glXCopyContext(Display * dpy, GLXContext source,
 static Bool
 __glXIsDirect(Display * dpy, GLXContextID contextID)
 {
+#ifndef GLX_USE_APPLEGL /* TODO: apple indirect */
+   return GC_IS_DIRECT(gc);
+#else
 #if !defined(USE_XCB)
    xGLXIsDirectReq *req;
    xGLXIsDirectReply reply;
@@ -847,6 +925,7 @@ __glXIsDirect(Display * dpy, GLXContextID contextID)
 
    return reply.isDirect;
 #endif /* USE_XCB */
+#endif /* GLX_USE_APPLEGL */
 }
 
 /**
@@ -870,6 +949,18 @@ glXIsDirect(Display * dpy, GLXContext gc)
 PUBLIC GLXPixmap
 glXCreateGLXPixmap(Display * dpy, XVisualInfo * vis, Pixmap pixmap)
 {
+#ifdef GLX_USE_APPLEGL
+   int screen = vis->screen;
+   __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, screen);
+   const __GLcontextModes *modes;
+
+   modes = _gl_context_modes_find_visual(psc->visuals, vis->visualid);
+   
+   if(apple_glx_pixmap_create(dpy, vis->screen, pixmap, modes))
+      return None;
+   
+   return pixmap;
+#else
    xGLXCreateGLXPixmapReq *req;
    GLXPixmap xid;
    CARD8 opcode;
@@ -891,7 +982,7 @@ glXCreateGLXPixmap(Display * dpy, XVisualInfo * vis, Pixmap pixmap)
    UnlockDisplay(dpy);
    SyncHandle();
 
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
    do {
       /* FIXME: Maybe delay __DRIdrawable creation until the drawable
        * is actually bound to a context... */
@@ -919,6 +1010,7 @@ glXCreateGLXPixmap(Display * dpy, XVisualInfo * vis, Pixmap pixmap)
 #endif
 
    return xid;
+#endif
 }
 
 /*
@@ -927,6 +1019,10 @@ glXCreateGLXPixmap(Display * dpy, XVisualInfo * vis, Pixmap pixmap)
 PUBLIC void
 glXDestroyGLXPixmap(Display * dpy, GLXPixmap glxpixmap)
 {
+#ifdef GLX_USE_APPLEGL
+   if(apple_glx_pixmap_destroy(dpy, glxpixmap))
+      __glXSendError(dpy, GLXBadPixmap, glxpixmap, X_GLXDestroyPixmap, false);
+#else
    xGLXDestroyGLXPixmapReq *req;
    CARD8 opcode;
 
@@ -944,7 +1040,7 @@ glXDestroyGLXPixmap(Display * dpy, GLXPixmap glxpixmap)
    UnlockDisplay(dpy);
    SyncHandle();
 
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
    {
       int screen;
       __GLXdisplayPrivate *const priv = __glXInitialize(dpy);
@@ -957,11 +1053,20 @@ glXDestroyGLXPixmap(Display * dpy, GLXPixmap glxpixmap)
       }
    }
 #endif
+#endif /* GLX_USE_APPLEGL */
 }
 
 PUBLIC void
 glXSwapBuffers(Display * dpy, GLXDrawable drawable)
 {
+#ifdef GLX_USE_APPLEGL
+   GLXContext gc = glXGetCurrentContext();
+   if(gc && apple_glx_is_current_drawable(dpy, gc->driContext, drawable)) {
+      apple_glx_swap_buffers(gc->driContext);
+   } else {
+      __glXSendError(dpy, GLXBadCurrentWindow, 0, X_GLXSwapBuffers, false);
+   }
+#else
    GLXContext gc;
    GLXContextTag tag;
    CARD8 opcode;
@@ -971,7 +1076,7 @@ glXSwapBuffers(Display * dpy, GLXDrawable drawable)
    xGLXSwapBuffersReq *req;
 #endif
 
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
    __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, NULL);
 
    if (pdraw != NULL) {
@@ -1016,6 +1121,7 @@ glXSwapBuffers(Display * dpy, GLXDrawable drawable)
    SyncHandle();
    XFlush(dpy);
 #endif /* USE_XCB */
+#endif /* GLX_USE_APPLEGL */
 }
 
 
@@ -1114,6 +1220,13 @@ init_fbconfig_for_chooser(__GLcontextModes * config,
     }                                           \
   } while ( 0 )
 
+/* Test that all bits from a are contained in b */
+#define MATCH_MASK(param)                      \
+  do {                                         \
+    if ((a->param & ~b->param) != 0)           \
+      return False;                            \
+  } while (0);
+
 /**
  * Determine if two GLXFBConfigs are compatible.
  *
@@ -1152,11 +1265,8 @@ fbconfigs_compatible(const __GLcontextModes * const a,
    MATCH_DONT_CARE(stereoMode);
    MATCH_EXACT(level);
 
-   if (((a->drawableType & b->drawableType) == 0)
-       || ((a->renderType & b->renderType) == 0)) {
-      return False;
-   }
-
+   MATCH_MASK(drawableType);
+   MATCH_MASK(renderType);
 
    /* There is a bug in a few of the XFree86 DDX drivers.  They contain
     * visuals with a "transparent type" of 0 when they really mean GLX_NONE.
@@ -1440,6 +1550,12 @@ glXChooseVisual(Display * dpy, int screen, int *attribList)
       }
    }
 
+#ifdef GLX_USE_APPLEGL
+   if(visualList && getenv("LIBGL_DUMP_VISUALID")) {
+      printf("visualid 0x%lx\n", visualList[0].visualid);
+   }
+#endif
+
    return visualList;
 }
 
@@ -1462,7 +1578,7 @@ glXQueryExtensionsString(Display * dpy, int screen)
       }
 
       __glXCalculateUsableExtensions(psc,
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
                                      (psc->driScreen != NULL),
 #else
                                      GL_FALSE,
@@ -1573,6 +1689,7 @@ PUBLIC
 GLX_ALIAS(Display *, glXGetCurrentDisplayEXT, (void), (),
           glXGetCurrentDisplay)
 
+#ifndef GLX_USE_APPLEGL
 /**
  * Used internally by libGL to send \c xGLXQueryContextinfoExtReq requests
  * to the X-server.
@@ -1684,13 +1801,16 @@ static int __glXQueryContextInfo(Display * dpy, GLXContext ctx)
    return retval;
 }
 
+#endif
+
 PUBLIC int
 glXQueryContext(Display * dpy, GLXContext ctx, int attribute, int *value)
 {
+#ifndef GLX_USE_APPLEGL
    int retVal;
 
    /* get the information from the server if we don't have it already */
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
    if (!ctx->driContext && (ctx->mode == NULL)) {
 #else
    if (ctx->mode == NULL) {
@@ -1699,13 +1819,17 @@ glXQueryContext(Display * dpy, GLXContext ctx, int attribute, int *value)
       if (Success != retVal)
          return retVal;
    }
+#endif
+
    switch (attribute) {
-   case GLX_SHARE_CONTEXT_EXT:
+#ifndef GLX_USE_APPLEGL
+      case GLX_SHARE_CONTEXT_EXT:
       *value = (int) (ctx->share_xid);
       break;
    case GLX_VISUAL_ID_EXT:
       *value = ctx->mode ? ctx->mode->visualID : None;
       break;
+#endif
    case GLX_SCREEN:
       *value = (int) (ctx->screen);
       break;
@@ -1734,6 +1858,9 @@ PUBLIC GLXContextID glXGetContextIDEXT(const GLXContext ctx)
 PUBLIC GLXContext
 glXImportContextEXT(Display * dpy, GLXContextID contextID)
 {
+#ifdef GLX_USE_APPLEGL
+   return NULL;
+#else
    GLXContext ctx;
 
    if (contextID == None) {
@@ -1754,6 +1881,7 @@ glXImportContextEXT(Display * dpy, GLXContextID contextID)
       }
    }
    return ctx;
+#endif
 }
 
 PUBLIC void
@@ -1799,7 +1927,7 @@ glXCreateNewContext(Display * dpy, GLXFBConfig config,
    const __GLcontextModes *const fbconfig =
       (const __GLcontextModes *const) config;
 
-   return CreateContext(dpy, NULL, fbconfig, shareList,
+   return CreateContext(dpy, fbconfig->fbconfigID, fbconfig, shareList,
                         allowDirect, X_GLXCreateNewContext, renderType,
                        fbconfig->screen);
 }
@@ -1881,7 +2009,7 @@ glXGetVisualFromFBConfig(Display * dpy, GLXFBConfig config)
    return XGetVisualInfo(dpy, VisualIDMask, &visualTemplate, &count);
 }
 
-
+#ifndef GLX_USE_APPLEGL
 /*
 ** GLX_SGI_swap_control
 */
@@ -1921,6 +2049,7 @@ __glXSwapIntervalSGI(int interval)
 #endif
    psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
 
+#ifdef GLX_DIRECT_RENDERING
    if (gc->driContext && psc->driScreen && psc->driScreen->setSwapInterval) {
       __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(gc->currentDpy,
                                                  gc->currentDrawable,
@@ -1928,6 +2057,7 @@ __glXSwapIntervalSGI(int interval)
       psc->driScreen->setSwapInterval(pdraw, interval);
       return 0;
    }
+#endif
 
    dpy = gc->currentDpy;
    opcode = __glXSetupForCommand(dpy);
@@ -1978,6 +2108,7 @@ __glXSwapIntervalMESA(unsigned int interval)
    }
 #endif
 
+#ifdef GLX_DIRECT_RENDERING
    if (gc != NULL && gc->driContext) {
       __GLXscreenConfigs *psc;
 
@@ -1989,6 +2120,7 @@ __glXSwapIntervalMESA(unsigned int interval)
         return 0;
       }
    }
+#endif
 
    return GLX_BAD_CONTEXT;
 }
@@ -2013,6 +2145,8 @@ __glXGetSwapIntervalMESA(void)
       }
    }
 #endif
+
+#ifdef GLX_DIRECT_RENDERING
    if (gc != NULL && gc->driContext) {
       __GLXscreenConfigs *psc;
 
@@ -2023,6 +2157,7 @@ __glXGetSwapIntervalMESA(void)
         return psc->driScreen->getSwapInterval(pdraw);
       }
    }
+#endif
 
    return 0;
 }
@@ -2139,13 +2274,22 @@ __glXGetVideoSyncSGI(unsigned int *count)
    int ret;
    GLXContext gc = __glXGetCurrentContext();
    __GLXscreenConfigs *psc;
+#ifdef GLX_DIRECT_RENDERING
    __GLXDRIdrawable *pdraw;
+#endif
+
+   if (!gc)
+      return GLX_BAD_CONTEXT;
 
-   if (!gc || !gc->driContext)
+#ifdef GLX_DIRECT_RENDERING
+   if (!gc->driContext)
       return GLX_BAD_CONTEXT;
+#endif
 
    psc = GetGLXScreenConfigs(gc->currentDpy, gc->screen);
+#ifdef GLX_DIRECT_RENDERING
    pdraw = GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
+#endif
 
    /* FIXME: Looking at the GLX_SGI_video_sync spec in the extension registry,
     * FIXME: there should be a GLX encoding for this call.  I can find no
@@ -2160,11 +2304,14 @@ __glXGetVideoSyncSGI(unsigned int *count)
       return (ret == 0) ? 0 : GLX_BAD_CONTEXT;
    }
 #endif
+
+#ifdef GLX_DIRECT_RENDERING
    if (psc->driScreen && psc->driScreen->getDrawableMSC) {
       ret = psc->driScreen->getDrawableMSC(psc, pdraw, &ust, &msc, &sbc);
       *count = (unsigned) msc;
       return (ret == True) ? 0 : GLX_BAD_CONTEXT;
    }
+#endif
 
    return GLX_BAD_CONTEXT;
 }
@@ -2174,18 +2321,27 @@ __glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
 {
    GLXContext gc = __glXGetCurrentContext();
    __GLXscreenConfigs *psc;
+#ifdef GLX_DIRECT_RENDERING
    __GLXDRIdrawable *pdraw;
+#endif
    int64_t ust, msc, sbc;
    int ret;
 
    if (divisor <= 0 || remainder < 0)
       return GLX_BAD_VALUE;
 
-   if (!gc || !gc->driContext)
+   if (!gc)
       return GLX_BAD_CONTEXT;
 
+#ifdef GLX_DIRECT_RENDERING
+   if (!gc->driContext)
+      return GLX_BAD_CONTEXT;
+#endif
+
    psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
+#ifdef GLX_DIRECT_RENDERING
    pdraw = GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
+#endif
 
 #ifdef __DRI_MEDIA_STREAM_COUNTER
    if (psc->msc != NULL && psc->driScreen ) {
@@ -2195,16 +2351,20 @@ __glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
       return (ret == 0) ? 0 : GLX_BAD_CONTEXT;
    }
 #endif
+
+#ifdef GLX_DIRECT_RENDERING
    if (psc->driScreen && psc->driScreen->waitForMSC) {
       ret = psc->driScreen->waitForMSC(pdraw, 0, divisor, remainder, &ust, &msc,
                                       &sbc);
       *count = (unsigned) msc;
       return (ret == True) ? 0 : GLX_BAD_CONTEXT;
    }
+#endif
 
    return GLX_BAD_CONTEXT;
 }
 
+#endif /* GLX_USE_APPLEGL */
 
 /*
 ** GLX_SGIX_fbconfig
@@ -2231,17 +2391,24 @@ glXCreateGLXPixmapWithConfigSGIX(Display * dpy,
                                  GLXFBConfigSGIX config,
                                  Pixmap pixmap)
 {
+#ifndef GLX_USE_APPLEGL
    xGLXVendorPrivateWithReplyReq *vpreq;
    xGLXCreateGLXPixmapWithConfigSGIXReq *req;
    GLXPixmap xid = None;
    CARD8 opcode;
-   const __GLcontextModes *const fbconfig = (__GLcontextModes *) config;
    __GLXscreenConfigs *psc;
+#endif
+   const __GLcontextModes *const fbconfig = (__GLcontextModes *) config;
 
 
    if ((dpy == NULL) || (config == NULL)) {
       return None;
    }
+#ifdef GLX_USE_APPLEGL
+   if(apple_glx_pixmap_create(dpy, fbconfig->screen, pixmap, fbconfig))
+      return None;
+   return pixmap;
+#else
 
    psc = GetGLXScreenConfigs(dpy, fbconfig->screen);
    if ((psc != NULL)
@@ -2269,6 +2436,7 @@ glXCreateGLXPixmapWithConfigSGIX(Display * dpy,
    }
 
    return xid;
+#endif
 }
 
 PUBLIC GLXContext
@@ -2288,7 +2456,7 @@ glXCreateContextWithConfigSGIX(Display * dpy,
    psc = GetGLXScreenConfigs(dpy, fbconfig->screen);
    if ((psc != NULL)
        && __glXExtensionBitIsEnabled(psc, SGIX_fbconfig_bit)) {
-      gc = CreateContext(dpy, NULL, (__GLcontextModes *) config, shareList,
+      gc = CreateContext(dpy, fbconfig->fbconfigID, fbconfig, shareList,
                          allowDirect,
                         X_GLXvop_CreateContextWithConfigSGIX, renderType,
                         fbconfig->screen);
@@ -2314,7 +2482,7 @@ glXGetFBConfigFromVisualSGIX(Display * dpy, XVisualInfo * vis)
    return NULL;
 }
 
-
+#ifndef GLX_USE_APPLEGL
 /*
 ** GLX_SGIX_swap_group
 */
@@ -2358,31 +2526,38 @@ __glXGetSyncValuesOML(Display * dpy, GLXDrawable drawable,
 {
    __GLXdisplayPrivate * const priv = __glXInitialize(dpy);
    int i, ret;
+#ifdef GLX_DIRECT_RENDERING
    __GLXDRIdrawable *pdraw;
+#endif
    __GLXscreenConfigs *psc;
 
    if (!priv)
       return False;
 
+#ifdef GLX_DIRECT_RENDERING
    pdraw = GetGLXDRIDrawable(dpy, drawable, &i);
+#endif
    psc = &priv->screenConfigs[i];
 
 #if defined(__DRI_SWAP_BUFFER_COUNTER) && defined(__DRI_MEDIA_STREAM_COUNTER)
-   if (pdraw && psc->sbc && psc->sbc)
+   if (pdraw && psc->sbc && psc->msc)
       return ( (pdraw && psc->sbc && psc->msc)
               && ((*psc->msc->getMSC)(psc->driScreen, msc) == 0)
               && ((*psc->sbc->getSBC)(pdraw->driDrawable, sbc) == 0)
               && (__glXGetUST(ust) == 0) );
 #endif
+
+#ifdef GLX_DIRECT_RENDERING
    if (pdraw && psc && psc->driScreen && psc->driScreen->getDrawableMSC) {
       ret = psc->driScreen->getDrawableMSC(psc, pdraw, ust, msc, sbc);
       return ret;
    }
+#endif
 
    return False;
 }
 
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
 _X_HIDDEN GLboolean
 __driGetMscRateOML(__DRIdrawable * draw,
                    int32_t * numerator, int32_t * denominator, void *private)
@@ -2495,11 +2670,18 @@ __glXSwapBuffersMscOML(Display * dpy, GLXDrawable drawable,
 {
    GLXContext gc = __glXGetCurrentContext();
    int screen;
+#ifdef GLX_DIRECT_RENDERING
    __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
+#endif
    __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, screen);
 
-   if (!pdraw || !gc->driContext) /* no GLX for this */
+   if (!gc) /* no GLX for this */
+      return -1;
+
+#ifdef GLX_DIRECT_RENDERING
+   if (!pdraw || !gc->driContext)
       return -1;
+#endif
 
    /* The OML_sync_control spec says these should "generate a GLX_BAD_VALUE
     * error", but it also says "It [glXSwapBuffersMscOML] will return a value
@@ -2511,6 +2693,9 @@ __glXSwapBuffersMscOML(Display * dpy, GLXDrawable drawable,
    if (divisor > 0 && remainder >= divisor)
       return -1;
 
+   if (target_msc == 0 && divisor == 0 && remainder == 0)
+      remainder = 1;
+
 #ifdef __DRI_SWAP_BUFFER_COUNTER
    if (psc->counters != NULL)
       return (*psc->sbc->swapBuffersMSC)(pdraw->driDrawable, target_msc,
@@ -2534,12 +2719,12 @@ __glXWaitForMscOML(Display * dpy, GLXDrawable drawable,
                    int64_t * msc, int64_t * sbc)
 {
    int screen;
+#ifdef GLX_DIRECT_RENDERING
    __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
+#endif
    __GLXscreenConfigs * const psc = GetGLXScreenConfigs( dpy, screen );
    int ret;
 
-   fprintf(stderr, "waitmsc: %lld, %lld, %lld\n", target_msc, divisor,
-          remainder);
 
    /* The OML_sync_control spec says these should "generate a GLX_BAD_VALUE
     * error", but the return type in the spec is Bool.
@@ -2551,7 +2736,6 @@ __glXWaitForMscOML(Display * dpy, GLXDrawable drawable,
 
 #ifdef __DRI_MEDIA_STREAM_COUNTER
    if (pdraw != NULL && psc->msc != NULL) {
-      fprintf(stderr, "dri1 msc\n");
       ret = (*psc->msc->waitForMSC) (pdraw->driDrawable, target_msc,
                                      divisor, remainder, msc, sbc);
 
@@ -2561,13 +2745,15 @@ __glXWaitForMscOML(Display * dpy, GLXDrawable drawable,
       return ((ret == 0) && (__glXGetUST(ust) == 0));
    }
 #endif
+
+#ifdef GLX_DIRECT_RENDERING
    if (pdraw && psc->driScreen && psc->driScreen->waitForMSC) {
       ret = psc->driScreen->waitForMSC(pdraw, target_msc, divisor, remainder,
                                       ust, msc, sbc);
       return ret;
    }
+#endif
 
-   fprintf(stderr, "no drawable??\n");
    return False;
 }
 
@@ -2578,7 +2764,9 @@ __glXWaitForSbcOML(Display * dpy, GLXDrawable drawable,
                    int64_t * msc, int64_t * sbc)
 {
    int screen;
+#ifdef GLX_DIRECT_RENDERING
    __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
+#endif
    __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, screen);
    int ret;
 
@@ -2598,10 +2786,14 @@ __glXWaitForSbcOML(Display * dpy, GLXDrawable drawable,
       return ((ret == 0) && (__glXGetUST(ust) == 0));
    }
 #endif
-   if (pdraw && psc->driScreen && psc->driScreen->waitForMSC) {
+
+#ifdef GLX_DIRECT_RENDERING
+   if (pdraw && psc->driScreen && psc->driScreen->waitForSBC) {
       ret = psc->driScreen->waitForSBC(pdraw, target_sbc, ust, msc, sbc);
       return ret;
    }
+#endif
+
    return False;
 }
 
@@ -2821,7 +3013,7 @@ __glXBindTexImageEXT(Display * dpy,
          i++;
    }
 
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
    if (gc->driContext) {
       __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, NULL);
 
@@ -2911,6 +3103,8 @@ __glXReleaseTexImageEXT(Display * dpy, GLXDrawable drawable, int buffer)
 
 /*@}*/
 
+#endif /* GLX_USE_APPLEGL */
+
 /**
  * \c strdup is actually not a standard ANSI C or POSIX routine.
  * Irix will not define it if ANSI mode is in effect.
@@ -2988,6 +3182,7 @@ static const struct name_address_pair GLX_functions[] = {
    GLX_FUNCTION(glXQueryDrawable),
    GLX_FUNCTION(glXSelectEvent),
 
+#ifndef GLX_USE_APPLEGL
    /*** GLX_SGI_swap_control ***/
    GLX_FUNCTION2(glXSwapIntervalSGI, __glXSwapIntervalSGI),
 
@@ -3005,6 +3200,7 @@ static const struct name_address_pair GLX_functions[] = {
    GLX_FUNCTION2(glXGetCurrentDisplayEXT, glXGetCurrentDisplay),
    GLX_FUNCTION(glXImportContextEXT),
    GLX_FUNCTION2(glXQueryContextInfoEXT, glXQueryContext),
+#endif
 
    /*** GLX_SGIX_fbconfig ***/
    GLX_FUNCTION2(glXGetFBConfigAttribSGIX, glXGetFBConfigAttrib),
@@ -3014,6 +3210,7 @@ static const struct name_address_pair GLX_functions[] = {
    GLX_FUNCTION2(glXGetVisualFromFBConfigSGIX, glXGetVisualFromFBConfig),
    GLX_FUNCTION(glXGetFBConfigFromVisualSGIX),
 
+#ifndef GLX_USE_APPLEGL
    /*** GLX_SGIX_pbuffer ***/
    GLX_FUNCTION(glXCreateGLXPbufferSGIX),
    GLX_FUNCTION(glXDestroyGLXPbufferSGIX),
@@ -3051,6 +3248,7 @@ static const struct name_address_pair GLX_functions[] = {
    GLX_FUNCTION2(glXEndFrameTrackingMESA, __glXEndFrameTrackingMESA),
    GLX_FUNCTION2(glXGetFrameUsageMESA, __glXGetFrameUsageMESA),
    GLX_FUNCTION2(glXQueryFrameTrackingMESA, __glXQueryFrameTrackingMESA),
+#endif
 
    /*** GLX_ARB_get_proc_address ***/
    GLX_FUNCTION(glXGetProcAddressARB),
@@ -3058,6 +3256,7 @@ static const struct name_address_pair GLX_functions[] = {
    /*** GLX 1.4 ***/
    GLX_FUNCTION2(glXGetProcAddress, glXGetProcAddressARB),
 
+#ifndef GLX_USE_APPLEGL
    /*** GLX_OML_sync_control ***/
    GLX_FUNCTION2(glXWaitForSbcOML, __glXWaitForSbcOML),
    GLX_FUNCTION2(glXWaitForMscOML, __glXWaitForMscOML),
@@ -3068,8 +3267,9 @@ static const struct name_address_pair GLX_functions[] = {
    /*** GLX_EXT_texture_from_pixmap ***/
    GLX_FUNCTION2(glXBindTexImageEXT, __glXBindTexImageEXT),
    GLX_FUNCTION2(glXReleaseTexImageEXT, __glXReleaseTexImageEXT),
+#endif
 
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
    /*** DRI configuration ***/
    GLX_FUNCTION(glXGetScreenDriver),
    GLX_FUNCTION(glXGetDriverConfig),
@@ -3078,7 +3278,7 @@ static const struct name_address_pair GLX_functions[] = {
    {NULL, NULL}                 /* end of list */
 };
 
-
+#ifndef GLX_USE_APPLEGL
 static const GLvoid *
 get_glx_proc_address(const char *funcName)
 {
@@ -3092,7 +3292,7 @@ get_glx_proc_address(const char *funcName)
 
    return NULL;
 }
-
+#endif
 
 /**
  * Get the address of a named GL function.  This is the pre-GLX 1.4 name for
@@ -3115,13 +3315,15 @@ PUBLIC void (*glXGetProcAddressARB(const GLubyte * procName)) (void)
     * DRI based drivers from searching the core GL function table for
     * internal API functions.
     */
-
+#ifdef GLX_USE_APPLEGL
+   f = (gl_function) apple_glx_get_proc_address(procName);
+#else
    f = (gl_function) get_glx_proc_address((const char *) procName);
    if ((f == NULL) && (procName[0] == 'g') && (procName[1] == 'l')
        && (procName[2] != 'X')) {
       f = (gl_function) _glapi_get_proc_address((const char *) procName);
    }
-
+#endif
    return f;
 }
 
@@ -3144,7 +3346,7 @@ PUBLIC void (*glXGetProcAddress(const GLubyte * procName)) (void)
 #endif /* __GNUC__ */
 
 
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
 /**
  * Get the unadjusted system time (UST).  Currently, the UST is measured in
  * microseconds since Epoc.  The actual resolution of the UST may vary from