apple: Initial import of libGL for OSX from AppleSGLX svn repository.
[mesa.git] / src / glx / glxcmds.c
index c3be974ea913d9ef9aaee1a3eaa72dbd9a24f7db..4e5243c1fc3474dcbcd8602d7db74501b380fc12 100644 (file)
  */
 
 #include "glxclient.h"
+#ifdef GLX_USE_APPLEGL
+#include "apple_glx_context.h"
+#include "apple_glx.h"
+#include "glx_error.h"
+#else
 #include "glapi.h"
+#endif
 #include "glxextensions.h"
 #include "glcontextmodes.h"
 
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
 #include <sys/time.h>
 #include <X11/extensions/xf86vmode.h>
 #include "xf86dri.h"
+#define GC_IS_DIRECT(gc) ((gc)->driContext != NULL)
+#else
+#define GC_IS_DIRECT(gc) (0)
 #endif
 
 #if defined(USE_XCB)
@@ -62,6 +71,8 @@ static Bool windowExistsFlag;
 static int
 windowExistsErrorHandler(Display * dpy, XErrorEvent * xerr)
 {
+   (void) dpy;
+
    if (xerr->error_code == BadWindow) {
       windowExistsFlag = GL_FALSE;
    }
@@ -105,10 +116,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
  *
@@ -343,6 +350,12 @@ AllocateGLXContext(Display * dpy)
       bufSize = __GLX_MAX_RENDER_CMD_SIZE;
    }
    gc->maxSmallRenderCommandSize = bufSize;
+   
+#ifdef GLX_USE_APPLEGL
+   gc->apple = NULL;
+   gc->do_destroy = False;   
+#endif
+
    return gc;
 }
 
@@ -357,138 +370,137 @@ AllocateGLXContext(Display * dpy)
  */
 
 static GLXContext
-CreateContext(Display * dpy, XVisualInfo * vis,
+CreateContext(Display * dpy, int generic_id,
               const __GLcontextModes * const fbconfig,
               GLXContext shareList,
-              Bool allowDirect, GLXContextID contextID,
-              Bool use_glx_1_3, int renderType)
+              Bool allowDirect,
+             unsigned code, int renderType, int screen)
 {
    GLXContext gc;
-#ifdef GLX_DIRECT_RENDERING
-   int screen = (fbconfig == NULL) ? vis->screen : fbconfig->screen;
+#if defined(GLX_DIRECT_RENDERING) || defined(GLX_USE_APPLEGL)
    __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, screen);
 #endif
-
+#ifdef GLX_USE_APPLEGL
+    const __GLcontextModes *mode;
+    int errorcode;
+    bool x11error;
+#endif
+    
    if (dpy == NULL)
       return NULL;
 
+   if (generic_id == None)
+      return NULL;
+
    gc = AllocateGLXContext(dpy);
    if (!gc)
       return NULL;
 
-   if (None == contextID) {
-      if ((vis == NULL) && (fbconfig == NULL))
-         return NULL;
-
+#ifndef GLX_USE_APPLEGL
 #ifdef GLX_DIRECT_RENDERING
-      if (allowDirect && psc->driScreen) {
-         const __GLcontextModes *mode;
-
-         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;
-            }
-            if (renderType == 0) {
-               /* Initialize renderType now */
-               renderType = mode->rgbMode ? GLX_RGBA_TYPE : GLX_COLOR_INDEX_TYPE;
-            }
-         }
-         else {
-            mode = fbconfig;
-         }
-
-         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;
-         }
+   if (allowDirect && psc->driScreen) {
+      gc->driContext = psc->driScreen->createContext(psc, fbconfig, gc,
+                                                    shareList, renderType);
+      if (gc->driContext != NULL) {
+        gc->screen = screen;
+        gc->psc = psc;
+        gc->mode = fbconfig;
+        gc->isDirect = GL_TRUE;
       }
+   }
 #endif
 
-      LockDisplay(dpy);
-      if (fbconfig == NULL) {
-         xGLXCreateContextReq *req;
-
-         /* Send the glXCreateContext request */
-         GetReq(GLXCreateContext, req);
-         req->reqType = gc->majorOpcode;
-         req->glxCode = X_GLXCreateContext;
-         req->context = gc->xid = XAllocID(dpy);
-         req->visual = vis->visualid;
-         req->screen = vis->screen;
-         req->shareList = shareList ? shareList->xid : None;
-#ifdef GLX_DIRECT_RENDERING
-         req->isDirect = gc->driContext != NULL;
-#else
-         req->isDirect = 0;
-#endif
-      }
-      else if (use_glx_1_3) {
-         xGLXCreateNewContextReq *req;
-
-         /* Send the glXCreateNewContext request */
-         GetReq(GLXCreateNewContext, req);
-         req->reqType = gc->majorOpcode;
-         req->glxCode = X_GLXCreateNewContext;
-         req->context = gc->xid = XAllocID(dpy);
-         req->fbconfig = fbconfig->fbconfigID;
-         req->screen = fbconfig->screen;
-         req->renderType = renderType;
-         req->shareList = shareList ? shareList->xid : None;
-#ifdef GLX_DIRECT_RENDERING
-         req->isDirect = gc->driContext != NULL;
-#else
-         req->isDirect = 0;
-#endif
-      }
-      else {
-         xGLXVendorPrivateWithReplyReq *vpreq;
-         xGLXCreateContextWithConfigSGIXReq *req;
-
-         /* Send the glXCreateNewContext request */
-         GetReqExtra(GLXVendorPrivateWithReply,
-                     sz_xGLXCreateContextWithConfigSGIXReq -
-                     sz_xGLXVendorPrivateWithReplyReq, vpreq);
-         req = (xGLXCreateContextWithConfigSGIXReq *) vpreq;
-         req->reqType = gc->majorOpcode;
-         req->glxCode = X_GLXVendorPrivateWithReply;
-         req->vendorCode = X_GLXvop_CreateContextWithConfigSGIX;
-         req->context = gc->xid = XAllocID(dpy);
-         req->fbconfig = fbconfig->fbconfigID;
-         req->screen = fbconfig->screen;
-         req->renderType = renderType;
-         req->shareList = shareList ? shareList->xid : None;
-#ifdef GLX_DIRECT_RENDERING
-         req->isDirect = gc->driContext != NULL;
-#else
-         req->isDirect = 0;
-#endif
-      }
+   LockDisplay(dpy);
+   switch (code) {
+   case X_GLXCreateContext: {
+      xGLXCreateContextReq *req;
+
+      /* Send the glXCreateContext request */
+      GetReq(GLXCreateContext, req);
+      req->reqType = gc->majorOpcode;
+      req->glxCode = X_GLXCreateContext;
+      req->context = gc->xid = XAllocID(dpy);
+      req->visual = generic_id;
+      req->screen = screen;
+      req->shareList = shareList ? shareList->xid : None;
+      req->isDirect = GC_IS_DIRECT(gc);
+      break;
+   }
 
-      UnlockDisplay(dpy);
-      SyncHandle();
-      gc->imported = GL_FALSE;
+   case X_GLXCreateNewContext: {
+      xGLXCreateNewContextReq *req;
+
+      /* Send the glXCreateNewContext request */
+      GetReq(GLXCreateNewContext, req);
+      req->reqType = gc->majorOpcode;
+      req->glxCode = X_GLXCreateNewContext;
+      req->context = gc->xid = XAllocID(dpy);
+      req->fbconfig = generic_id;
+      req->screen = screen;
+      req->renderType = renderType;
+      req->shareList = shareList ? shareList->xid : None;
+      req->isDirect = GC_IS_DIRECT(gc);
+      break;
    }
-   else {
-      gc->xid = contextID;
-      gc->imported = GL_TRUE;
+
+   case X_GLXvop_CreateContextWithConfigSGIX: {
+      xGLXVendorPrivateWithReplyReq *vpreq;
+      xGLXCreateContextWithConfigSGIXReq *req;
+
+      /* Send the glXCreateNewContext request */
+      GetReqExtra(GLXVendorPrivateWithReply,
+                 sz_xGLXCreateContextWithConfigSGIXReq -
+                 sz_xGLXVendorPrivateWithReplyReq, vpreq);
+      req = (xGLXCreateContextWithConfigSGIXReq *) vpreq;
+      req->reqType = gc->majorOpcode;
+      req->glxCode = X_GLXVendorPrivateWithReply;
+      req->vendorCode = X_GLXvop_CreateContextWithConfigSGIX;
+      req->context = gc->xid = XAllocID(dpy);
+      req->fbconfig = generic_id;
+      req->screen = screen;
+      req->renderType = renderType;
+      req->shareList = shareList ? shareList->xid : None;
+      req->isDirect = GC_IS_DIRECT(gc);
+      break;
    }
 
+   default:
+      /* What to do here?  This case is the sign of an internal error.  It
+       * should never be reachable.
+       */
+      break;
+   }
+
+   UnlockDisplay(dpy);
+   SyncHandle();
+#endif
+
+   gc->imported = GL_FALSE;
+#ifdef GLX_USE_APPLEGL
+   mode = _gl_context_modes_find_fbconfig(psc->configs, 
+                                          fbconfig->fbconfigID);
+
+   if(NULL == mode) {
+      __glXSendError(dpy, BadValue, fbconfig->fbconfigID, X_GLXCreateContext,
+                     true);
+      __glXFreeContext(gc);
+      return NULL;
+   }
+   
+   if(apple_glx_create_context(&gc->apple, dpy, screen, mode, 
+                               shareList ? shareList->apple : NULL,
+                               &errorcode, &x11error)) {
+      __glXSendError(dpy, errorcode, 0, X_GLXCreateContext, x11error);
+      __glXFreeContext(gc);
+      return NULL;
+   }
+   
+   gc->currentContextTag = -1;
+   gc->mode = mode;
+   gc->isDirect = allowDirect;
+#else
    gc->renderType = renderType;
+#endif
 
    return gc;
 }
@@ -497,8 +509,31 @@ PUBLIC GLXContext
 glXCreateContext(Display * dpy, XVisualInfo * vis,
                  GLXContext shareList, Bool allowDirect)
 {
-   return CreateContext(dpy, vis, NULL, shareList, allowDirect, None,
-                        False, 0);
+   const __GLcontextModes *mode = NULL;
+   int renderType = 0;
+
+#ifdef GLX_DIRECT_RENDERING
+   __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, vis->screen);
+
+   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 = __glXSetupForCommand(dpy);
+      error.minorCode = X_GLXCreateContext;
+      _XError(dpy, &error);
+      return None;
+   }
+
+   renderType = mode->rgbMode ? GLX_RGBA_TYPE : GLX_COLOR_INDEX_TYPE;
+#endif
+
+   return CreateContext(dpy, vis->visualid, mode, shareList, allowDirect,
+                        X_GLXCreateContext, renderType, vis->screen);
 }
 
 _X_HIDDEN void
@@ -512,7 +547,9 @@ __glXFreeContext(__GLXcontext * gc)
       XFree((char *) gc->version);
    if (gc->extensions)
       XFree((char *) gc->extensions);
+#ifndef GLX_USE_APPLEGL
    __glFreeAttributeState(gc);
+#endif
    XFree((char *) gc->buf);
    Xfree((char *) gc->client_state_private);
    XFree((char *) gc);
@@ -525,6 +562,7 @@ __glXFreeContext(__GLXcontext * gc)
 static void
 DestroyContext(Display * dpy, GLXContext gc)
 {
+#ifndef GLX_USE_APPLEGL
    xGLXDestroyContextReq *req;
    GLXContextID xid;
    CARD8 opcode;
@@ -560,17 +598,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->apple)
+         apple_glx_destroy_context(&gc->apple, dpy);
+#endif
       __glXFreeContext(gc);
    }
-
+#ifndef GLX_USE_APPLEGL
    if (!imported) {
       /*
        ** This dpy also created the server side part of the context.
@@ -584,6 +636,7 @@ DestroyContext(Display * dpy, GLXContext gc)
       UnlockDisplay(dpy);
       SyncHandle();
    }
+#endif
 }
 
 PUBLIC void
@@ -638,7 +691,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;
 
@@ -647,7 +702,9 @@ glXWaitGL(void)
 
    /* Flush any pending commands out */
    __glXFlushRenderBuffer(gc, gc->pc);
-
+#ifdef GLX_USE_APPLEGL
+   glFinish();
+#else
 #ifdef GLX_DIRECT_RENDERING
    if (gc->driContext) {
       int screen;
@@ -672,6 +729,7 @@ glXWaitGL(void)
    req->contextTag = gc->currentContextTag;
    UnlockDisplay(dpy);
    SyncHandle();
+#endif /* GLX_USE_APPLEGL */
 }
 
 /*
@@ -681,7 +739,9 @@ glXWaitGL(void)
 PUBLIC void
 glXWaitX(void)
 {
+#ifndef GLX_USE_APPLEGL
    xGLXWaitXReq *req;
+#endif
    GLXContext gc = __glXGetCurrentContext();
    Display *dpy = gc->currentDpy;
 
@@ -691,6 +751,9 @@ glXWaitX(void)
    /* Flush any pending commands out */
    __glXFlushRenderBuffer(gc, gc->pc);
 
+#ifdef GLX_USE_APPLEGL
+   apple_glx_waitx(dpy, gc->apple);
+#else
 #ifdef GLX_DIRECT_RENDERING
    if (gc->driContext) {
       int screen;
@@ -718,12 +781,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;
 
@@ -732,7 +798,9 @@ glXUseXFont(Font font, int first, int count, int listBase)
 
    /* Flush any pending commands out */
    (void) __glXFlushRenderBuffer(gc, gc->pc);
-
+#ifdef GLX_USE_APPLEGL
+   DRI_glXUseXFont(font, first, count, listBase); 
+#else
 #ifdef GLX_DIRECT_RENDERING
    if (gc->driContext) {
       DRI_glXUseXFont(font, first, count, listBase);
@@ -752,6 +820,7 @@ glXUseXFont(Font font, int first, int count, int listBase)
    req->listBase = listBase;
    UnlockDisplay(dpy);
    SyncHandle();
+#endif /* GLX_USE_APPLEGL */
 }
 
 /************************************************************************/
@@ -764,6 +833,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->apple, source->apple, dest->apple,
+                             mask, &errorcode, &x11error)) {
+      __glXSendError(dpy, errorcode, 0, X_GLXCopyContext, x11error);
+   }
+   
+#else
    xGLXCopyContextReq *req;
    GLXContext gc = __glXGetCurrentContext();
    GLXContextTag tag;
@@ -802,9 +882,11 @@ glXCopyContext(Display * dpy, GLXContext source,
    req->contextTag = tag;
    UnlockDisplay(dpy);
    SyncHandle();
+#endif /* GLX_USE_APPLEGL */
 }
 
 
+#ifndef GLX_USE_APPLEGL
 /**
  * Determine if a context uses direct rendering.
  *
@@ -852,6 +934,7 @@ __glXIsDirect(Display * dpy, GLXContextID contextID)
    return reply.isDirect;
 #endif /* USE_XCB */
 }
+#endif /* GLX_USE_APPLEGL */
 
 /**
  * \todo
@@ -862,20 +945,42 @@ __glXIsDirect(Display * dpy, GLXContextID contextID)
 PUBLIC Bool
 glXIsDirect(Display * dpy, GLXContext gc)
 {
+#ifdef GLX_USE_APPLEGL
+   /*
+    * This isn't an ideal test.  
+    * glXIsDirect should probably search a list of contexts.
+    */
+   if(NULL == gc) {
+      __glXSendError(dpy, GLXBadContext, 0, X_GLXIsDirect, false);
+      return False;
+   }
+   return gc->isDirect;
+#else
    if (!gc) {
       return GL_FALSE;
-#ifdef GLX_DIRECT_RENDERING
    }
-   else if (gc->driContext) {
+   else if (GC_IS_DIRECT(gc)) {
       return GL_TRUE;
-#endif
    }
    return __glXIsDirect(dpy, gc->xid);
+#endif /* GLX_USE_APPLEGL */
 }
 
 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;
@@ -925,6 +1030,7 @@ glXCreateGLXPixmap(Display * dpy, XVisualInfo * vis, Pixmap pixmap)
 #endif
 
    return xid;
+#endif
 }
 
 /*
@@ -933,6 +1039,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;
 
@@ -963,11 +1073,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->apple, drawable)) {
+      apple_glx_swap_buffers(gc->apple);
+   } else {
+      __glXSendError(dpy, GLXBadCurrentWindow, 0, X_GLXSwapBuffers, false);
+   }
+#else
    GLXContext gc;
    GLXContextTag tag;
    CARD8 opcode;
@@ -1022,6 +1141,7 @@ glXSwapBuffers(Display * dpy, GLXDrawable drawable)
    SyncHandle();
    XFlush(dpy);
 #endif /* USE_XCB */
+#endif /* GLX_USE_APPLEGL */
 }
 
 
@@ -1099,7 +1219,7 @@ init_fbconfig_for_chooser(__GLcontextModes * config,
 
 #define MATCH_DONT_CARE( param )        \
   do {                                  \
-    if ( (a-> param != GLX_DONT_CARE)   \
+    if ( ((int) a-> param != (int) GLX_DONT_CARE)   \
          && (a-> param != b-> param) ) {        \
       return False;                             \
     }                                           \
@@ -1107,7 +1227,7 @@ init_fbconfig_for_chooser(__GLcontextModes * config,
 
 #define MATCH_MINIMUM( param )                  \
   do {                                          \
-    if ( (a-> param != GLX_DONT_CARE)           \
+    if ( ((int) a-> param != (int) GLX_DONT_CARE)      \
          && (a-> param > b-> param) ) {         \
       return False;                             \
     }                                           \
@@ -1174,7 +1294,7 @@ fbconfigs_compatible(const __GLcontextModes * const a,
     * the (broken) drivers.
     */
 
-   if (a->transparentPixel != GLX_DONT_CARE && a->transparentPixel != 0) {
+   if (a->transparentPixel != (int) GLX_DONT_CARE && a->transparentPixel != 0) {
       if (a->transparentPixel == GLX_NONE) {
          if (b->transparentPixel != GLX_NONE && b->transparentPixel != 0)
             return False;
@@ -1446,6 +1566,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;
 }
 
@@ -1482,6 +1608,8 @@ glXQueryExtensionsString(Display * dpy, int screen)
 PUBLIC const char *
 glXGetClientString(Display * dpy, int name)
 {
+   (void) dpy;
+
    switch (name) {
    case GLX_VENDOR:
       return (__glXGLXClientVendorName);
@@ -1577,6 +1705,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.
@@ -1643,7 +1772,6 @@ static int __glXQueryContextInfo(Display * dpy, GLXContext ctx)
    else {
       int *propList, *pProp;
       int nPropListBytes;
-      int i;
 
       nPropListBytes = numValues << 3;
       propList = (int *) Xmalloc(nPropListBytes);
@@ -1651,6 +1779,8 @@ static int __glXQueryContextInfo(Display * dpy, GLXContext ctx)
          retval = 0;
       }
       else {
+        unsigned i;
+
          _XRead(dpy, (char *) propList, nPropListBytes);
          pProp = propList;
          for (i = 0; i < numValues; i++) {
@@ -1687,9 +1817,12 @@ 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 */
@@ -1702,13 +1835,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;
@@ -1737,6 +1874,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) {
@@ -1746,13 +1886,18 @@ glXImportContextEXT(Display * dpy, GLXContextID contextID)
       return NULL;
    }
 
-   ctx = CreateContext(dpy, NULL, NULL, NULL, False, contextID, False, 0);
+   ctx = AllocateGLXContext(dpy);
    if (NULL != ctx) {
+      ctx->xid = contextID;
+      ctx->imported = GL_TRUE;
+
       if (Success != __glXQueryContextInfo(dpy, ctx)) {
-         return NULL;
+        __glXFreeContext(ctx);
+        ctx = NULL;
       }
    }
    return ctx;
+#endif
 }
 
 PUBLIC void
@@ -1795,8 +1940,12 @@ PUBLIC GLXContext
 glXCreateNewContext(Display * dpy, GLXFBConfig config,
                     int renderType, GLXContext shareList, Bool allowDirect)
 {
-   return CreateContext(dpy, NULL, (__GLcontextModes *) config, shareList,
-                        allowDirect, None, True, renderType);
+   const __GLcontextModes *const fbconfig =
+      (const __GLcontextModes *const) config;
+
+   return CreateContext(dpy, fbconfig->fbconfigID, fbconfig, shareList,
+                        allowDirect, X_GLXCreateNewContext, renderType,
+                       fbconfig->screen);
 }
 
 
@@ -1819,14 +1968,15 @@ glXGetFBConfigs(Display * dpy, int screen, int *nelements)
    if (priv && (priv->screenConfigs != NULL)
        && (screen >= 0) && (screen <= ScreenCount(dpy))
        && (priv->screenConfigs[screen].configs != NULL)
-       && (priv->screenConfigs[screen].configs->fbconfigID != GLX_DONT_CARE)) {
+       && (priv->screenConfigs[screen].configs->fbconfigID
+          != (int) GLX_DONT_CARE)) {
       unsigned num_configs = 0;
       __GLcontextModes *modes;
 
 
       for (modes = priv->screenConfigs[screen].configs; modes != NULL;
            modes = modes->next) {
-         if (modes->fbconfigID != GLX_DONT_CARE) {
+         if (modes->fbconfigID != (int) GLX_DONT_CARE) {
             num_configs++;
          }
       }
@@ -1838,7 +1988,7 @@ glXGetFBConfigs(Display * dpy, int screen, int *nelements)
          i = 0;
          for (modes = priv->screenConfigs[screen].configs; modes != NULL;
               modes = modes->next) {
-            if (modes->fbconfigID != GLX_DONT_CARE) {
+            if (modes->fbconfigID != (int) GLX_DONT_CARE) {
                config[i] = modes;
                i++;
             }
@@ -1875,7 +2025,7 @@ glXGetVisualFromFBConfig(Display * dpy, GLXFBConfig config)
    return XGetVisualInfo(dpy, VisualIDMask, &visualTemplate, &count);
 }
 
-
+#ifndef GLX_USE_APPLEGL
 /*
 ** GLX_SGI_swap_control
 */
@@ -1915,6 +2065,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,
@@ -1922,6 +2073,7 @@ __glXSwapIntervalSGI(int interval)
       psc->driScreen->setSwapInterval(pdraw, interval);
       return 0;
    }
+#endif
 
    dpy = gc->currentDpy;
    opcode = __glXSetupForCommand(dpy);
@@ -1956,10 +2108,6 @@ __glXSwapIntervalMESA(unsigned int interval)
 {
    GLXContext gc = __glXGetCurrentContext();
 
-   if (interval < 0) {
-      return GLX_BAD_VALUE;
-   }
-
 #ifdef __DRI_SWAP_CONTROL
    if (gc != NULL && gc->driContext) {
       __GLXscreenConfigs *const psc = GetGLXScreenConfigs(gc->currentDpy,
@@ -1976,6 +2124,7 @@ __glXSwapIntervalMESA(unsigned int interval)
    }
 #endif
 
+#ifdef GLX_DIRECT_RENDERING
    if (gc != NULL && gc->driContext) {
       __GLXscreenConfigs *psc;
 
@@ -1987,6 +2136,7 @@ __glXSwapIntervalMESA(unsigned int interval)
         return 0;
       }
    }
+#endif
 
    return GLX_BAD_CONTEXT;
 }
@@ -2139,9 +2289,14 @@ __glXGetVideoSyncSGI(unsigned int *count)
    __GLXscreenConfigs *psc;
    __GLXDRIdrawable *pdraw;
 
-   if (!gc || !gc->driContext)
+   if (!gc)
       return GLX_BAD_CONTEXT;
 
+#ifdef GLX_DIRECT_RENDERING
+   if (!gc->driContext)
+      return GLX_BAD_CONTEXT;
+#endif
+
    psc = GetGLXScreenConfigs(gc->currentDpy, gc->screen);
    pdraw = GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
 
@@ -2179,8 +2334,13 @@ __glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
    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);
    pdraw = GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable, NULL);
@@ -2203,6 +2363,7 @@ __glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
    return GLX_BAD_CONTEXT;
 }
 
+#endif /* GLX_USE_APPLEGL */
 
 /*
 ** GLX_SGIX_fbconfig
@@ -2229,17 +2390,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)
@@ -2267,6 +2435,7 @@ glXCreateGLXPixmapWithConfigSGIX(Display * dpy,
    }
 
    return xid;
+#endif
 }
 
 PUBLIC GLXContext
@@ -2286,8 +2455,10 @@ glXCreateContextWithConfigSGIX(Display * dpy,
    psc = GetGLXScreenConfigs(dpy, fbconfig->screen);
    if ((psc != NULL)
        && __glXExtensionBitIsEnabled(psc, SGIX_fbconfig_bit)) {
-      gc = CreateContext(dpy, NULL, (__GLcontextModes *) config, shareList,
-                         allowDirect, None, False, renderType);
+      gc = CreateContext(dpy, fbconfig->fbconfigID, fbconfig, shareList,
+                         allowDirect,
+                        X_GLXvop_CreateContextWithConfigSGIX, renderType,
+                        fbconfig->screen);
    }
 
    return gc;
@@ -2302,7 +2473,7 @@ glXGetFBConfigFromVisualSGIX(Display * dpy, XVisualInfo * vis)
 
    if ((GetGLXPrivScreenConfig(dpy, vis->screen, &priv, &psc) != Success)
        && __glXExtensionBitIsEnabled(psc, SGIX_fbconfig_bit)
-       && (psc->configs->fbconfigID != GLX_DONT_CARE)) {
+       && (psc->configs->fbconfigID != (int) GLX_DONT_CARE)) {
       return (GLXFBConfigSGIX) _gl_context_modes_find_visual(psc->configs,
                                                              vis->visualid);
    }
@@ -2310,7 +2481,7 @@ glXGetFBConfigFromVisualSGIX(Display * dpy, XVisualInfo * vis)
    return NULL;
 }
 
-
+#ifndef GLX_USE_APPLEGL
 /*
 ** GLX_SGIX_swap_group
 */
@@ -2364,7 +2535,7 @@ __glXGetSyncValuesOML(Display * dpy, GLXDrawable drawable,
    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)
@@ -2390,6 +2561,8 @@ __driGetMscRateOML(__DRIdrawable * draw,
    int i;
    __GLXDRIdrawable *glxDraw = private;
 
+   (void) draw;
+
    psc = glxDraw->psc;
    if (XF86VidModeQueryVersion(psc->dpy, &i, &i) &&
        XF86VidModeGetModeLine(psc->dpy, psc->scr, &dot_clock, &mode_line)) {
@@ -2436,6 +2609,11 @@ __driGetMscRateOML(__DRIdrawable * draw,
    else
       return False;
 #else
+   (void) draw;
+   (void) numerator;
+   (void) denominator;
+   (void) private;
+
    return False;
 #endif
 }
@@ -2487,8 +2665,13 @@ __glXSwapBuffersMscOML(Display * dpy, GLXDrawable drawable,
    __GLXDRIdrawable *pdraw = GetGLXDRIDrawable(dpy, drawable, &screen);
    __GLXscreenConfigs *const psc = GetGLXScreenConfigs(dpy, screen);
 
-   if (!pdraw || !gc->driContext) /* no GLX for this */
+   if (!pdraw || !gc) /* no GLX for this */
+      return -1;
+
+#ifdef GLX_DIRECT_RENDERING
+   if (!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
@@ -2527,8 +2710,6 @@ __glXWaitForMscOML(Display * dpy, GLXDrawable drawable,
    __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.
@@ -2540,7 +2721,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);
 
@@ -2556,7 +2736,6 @@ __glXWaitForMscOML(Display * dpy, GLXDrawable drawable,
       return ret;
    }
 
-   fprintf(stderr, "no drawable??\n");
    return False;
 }
 
@@ -2587,7 +2766,7 @@ __glXWaitForSbcOML(Display * dpy, GLXDrawable drawable,
       return ((ret == 0) && (__glXGetUST(ust) == 0));
    }
 #endif
-   if (pdraw && psc->driScreen && psc->driScreen->waitForMSC) {
+   if (pdraw && psc->driScreen && psc->driScreen->waitForSBC) {
       ret = psc->driScreen->waitForSBC(pdraw, target_sbc, ust, msc, sbc);
       return ret;
    }
@@ -2874,14 +3053,9 @@ __glXReleaseTexImageEXT(Display * dpy, GLXDrawable drawable, int buffer)
    INT32 *buffer_ptr;
    CARD8 opcode;
 
-   if (gc == NULL)
+   if ((gc == NULL) || GC_IS_DIRECT(gc))
       return;
 
-#ifdef GLX_DIRECT_RENDERING
-   if (gc->driContext)
-      return;
-#endif
-
    opcode = __glXSetupForCommand(dpy);
    if (!opcode)
       return;
@@ -2905,6 +3079,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.
@@ -2982,6 +3158,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),
 
@@ -2999,6 +3176,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),
@@ -3008,6 +3186,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),
@@ -3045,6 +3224,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),
@@ -3052,6 +3232,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),
@@ -3062,6 +3243,7 @@ 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
    /*** DRI configuration ***/
@@ -3072,7 +3254,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)
 {
@@ -3086,7 +3268,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
@@ -3109,13 +3291,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;
 }