Added few more stubs so that control reaches to DestroyDevice().
[mesa.git] / src / glx / glxcmds.c
index 290c86c6cd22d113e8b809e19d047079f08b6342..8766b007d739d2b63cb8b5cf8fb48cc911bc9b46 100644 (file)
 #ifdef GLX_USE_APPLEGL
 #include "apple/apple_glx_context.h"
 #include "apple/apple_glx.h"
+#include "util/debug.h"
 #else
 #include <sys/time.h>
-#ifdef XF86VIDMODE
+#ifndef GLX_USE_WINDOWSGL
 #include <X11/extensions/xf86vmode.h>
-#endif
+#endif /* GLX_USE_WINDOWSGL */
 #endif
 #endif
 
@@ -235,19 +236,23 @@ Bool
 validate_renderType_against_config(const struct glx_config *config,
                                    int renderType)
 {
-    switch (renderType) {
-    case GLX_RGBA_TYPE:
-        return (config->renderType & GLX_RGBA_BIT) != 0;
-    case GLX_COLOR_INDEX_TYPE:
-        return (config->renderType & GLX_COLOR_INDEX_BIT) != 0;
-    case GLX_RGBA_FLOAT_TYPE_ARB:
-        return (config->renderType & GLX_RGBA_FLOAT_BIT_ARB) != 0;
-    case GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT:
-        return (config->renderType & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) != 0;
-    default:
-        break;
-    }
-    return 0;
+   /* GLX_EXT_no_config_context supports any render type */
+   if (!config)
+      return True;
+
+   switch (renderType) {
+      case GLX_RGBA_TYPE:
+         return (config->renderType & GLX_RGBA_BIT) != 0;
+      case GLX_COLOR_INDEX_TYPE:
+         return (config->renderType & GLX_COLOR_INDEX_BIT) != 0;
+      case GLX_RGBA_FLOAT_TYPE_ARB:
+         return (config->renderType & GLX_RGBA_FLOAT_BIT_ARB) != 0;
+      case GLX_RGBA_UNSIGNED_FLOAT_TYPE_EXT:
+         return (config->renderType & GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT) != 0;
+      default:
+         break;
+   }
+   return 0;
 }
 
 _X_HIDDEN Bool
@@ -267,6 +272,44 @@ glx_context_init(struct glx_context *gc,
    return True;
 }
 
+/**
+ * Determine if a context uses direct rendering.
+ *
+ * \param dpy        Display where the context was created.
+ * \param contextID  ID of the context to be tested.
+ * \param error      Out parameter, set to True on error if not NULL
+ *
+ * \returns \c True if the context is direct rendering or not.
+ */
+static Bool
+__glXIsDirect(Display * dpy, GLXContextID contextID, Bool *error)
+{
+   CARD8 opcode;
+   xcb_connection_t *c;
+   xcb_generic_error_t *err;
+   xcb_glx_is_direct_reply_t *reply;
+   Bool is_direct;
+
+   opcode = __glXSetupForCommand(dpy);
+   if (!opcode) {
+      return False;
+   }
+
+   c = XGetXCBConnection(dpy);
+   reply = xcb_glx_is_direct_reply(c, xcb_glx_is_direct(c, contextID), &err);
+   is_direct = (reply != NULL && reply->is_direct) ? True : False;
+
+   if (err != NULL) {
+      if (error)
+         *error = True;
+      __glXSendErrorForXcb(dpy, err);
+      free(err);
+   }
+
+   free(reply);
+
+   return is_direct;
+}
 
 /**
  * Create a new context.
@@ -371,6 +414,21 @@ CreateContext(Display *dpy, int generic_id, struct glx_config *config,
    gc->share_xid = shareList ? shareList->xid : None;
    gc->imported = GL_FALSE;
 
+   /* Unlike most X resource creation requests, we're about to return a handle
+    * with client-side state, not just an XID. To simplify error handling
+    * elsewhere in libGL, force a round-trip here to ensure the CreateContext
+    * request above succeeded.
+    */
+   {
+      Bool error = False;
+      int isDirect = __glXIsDirect(dpy, gc->xid, &error);
+
+      if (error != False || isDirect != gc->isDirect) {
+         gc->vtable->destroy(gc);
+         gc = NULL;
+      }
+   }
+
    return (GLXContext) gc;
 }
 
@@ -388,15 +446,7 @@ glXCreateContext(Display * dpy, XVisualInfo * vis,
       config = glx_config_find_visual(psc->visuals, vis->visualid);
 
    if (config == 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);
+      __glXSendError(dpy, BadValue, vis->visualid, X_GLXCreateContext, True);
       return None;
    }
 
@@ -412,19 +462,6 @@ glXCreateContext(Display * dpy, XVisualInfo * vis,
        renderType = GLX_RGBA_TYPE;
    } else if (config->renderType & GLX_COLOR_INDEX_BIT) {
        renderType = GLX_COLOR_INDEX_TYPE;
-   } else if (config->rgbMode) {
-       /* If we're here, then renderType is not set correctly.  Let's use a
-        * safeguard - any TrueColor or DirectColor mode is RGB mode.  Such
-        * default value is needed by old DRI drivers, which didn't set
-        * renderType correctly as the value was just ignored.
-        */
-       renderType = GLX_RGBA_TYPE;
-   } else {
-       /* Safeguard - only one option left, all non-RGB modes are indexed
-        * modes.  Again, this allows drivers with invalid renderType to work
-        * properly.
-        */
-       renderType = GLX_COLOR_INDEX_TYPE;
    }
 #endif
 
@@ -524,7 +561,7 @@ glXWaitGL(void)
 {
    struct glx_context *gc = __glXGetCurrentContext();
 
-   if (gc != &dummyContext && gc->vtable->wait_gl)
+   if (gc->vtable->wait_gl)
       gc->vtable->wait_gl(gc);
 }
 
@@ -537,7 +574,7 @@ glXWaitX(void)
 {
    struct glx_context *gc = __glXGetCurrentContext();
 
-   if (gc != &dummyContext && gc->vtable->wait_x)
+   if (gc->vtable->wait_x)
       gc->vtable->wait_x(gc);
 }
 
@@ -546,7 +583,7 @@ glXUseXFont(Font font, int first, int count, int listBase)
 {
    struct glx_context *gc = __glXGetCurrentContext();
 
-   if (gc != &dummyContext && gc->vtable->use_x_font)
+   if (gc->vtable->use_x_font)
       gc->vtable->use_x_font(gc, font, first, count, listBase);
 }
 
@@ -615,42 +652,6 @@ glXCopyContext(Display * dpy, GLXContext source_user,
 }
 
 
-/**
- * Determine if a context uses direct rendering.
- *
- * \param dpy        Display where the context was created.
- * \param contextID  ID of the context to be tested.
- *
- * \returns \c True if the context is direct rendering or not.
- */
-static Bool
-__glXIsDirect(Display * dpy, GLXContextID contextID)
-{
-   CARD8 opcode;
-   xcb_connection_t *c;
-   xcb_generic_error_t *err;
-   xcb_glx_is_direct_reply_t *reply;
-   Bool is_direct;
-
-   opcode = __glXSetupForCommand(dpy);
-   if (!opcode) {
-      return False;
-   }
-
-   c = XGetXCBConnection(dpy);
-   reply = xcb_glx_is_direct_reply(c, xcb_glx_is_direct(c, contextID), &err);
-   is_direct = (reply != NULL && reply->is_direct) ? True : False;
-
-   if (err != NULL) {
-      __glXSendErrorForXcb(dpy, err);
-      free(err);
-   }
-
-   free(reply);
-
-   return is_direct;
-}
-
 /**
  * \todo
  * Shouldn't this function \b always return \c False when
@@ -671,7 +672,7 @@ glXIsDirect(Display * dpy, GLXContext gc_user)
 #ifdef GLX_USE_APPLEGL  /* TODO: indirect on darwin */
    return False;
 #else
-   return __glXIsDirect(dpy, gc->xid);
+   return __glXIsDirect(dpy, gc->xid, NULL);
 #endif
 }
 
@@ -820,7 +821,7 @@ glXSwapBuffers(Display * dpy, GLXDrawable drawable)
 {
 #ifdef GLX_USE_APPLEGL
    struct glx_context * gc = __glXGetCurrentContext();
-   if(gc != &DummyContext && apple_glx_is_current_drawable(dpy, gc->driContext, drawable)) {
+   if(gc != &dummyContext && apple_glx_is_current_drawable(dpy, gc->driContext, drawable)) {
       apple_glx_swap_buffers(gc->driContext);
    } else {
       __glXSendError(dpy, GLXBadCurrentWindow, 0, X_GLXSwapBuffers, false);
@@ -840,7 +841,7 @@ glXSwapBuffers(Display * dpy, GLXDrawable drawable)
       if (pdraw != NULL) {
          Bool flush = gc != &dummyContext && drawable == gc->currentDrawable;
 
-         (*pdraw->psc->driScreen->swapBuffers)(pdraw, 0, 0, 0, flush);
+         pdraw->psc->driScreen->swapBuffers(pdraw, 0, 0, 0, flush);
          return;
       }
    }
@@ -922,7 +923,6 @@ init_fbconfig_for_chooser(struct glx_config * config,
     * glXChooseVisual.
     */
    if (fbconfig_style_tags) {
-      config->rgbMode = GL_TRUE;
       config->doubleBufferMode = GLX_DONT_CARE;
       config->renderType = GLX_RGBA_BIT;
    }
@@ -940,6 +940,7 @@ init_fbconfig_for_chooser(struct glx_config * config,
    config->fbconfigID = (GLXFBConfigID) (GLX_DONT_CARE);
 
    config->swapMethod = GLX_DONT_CARE;
+   config->sRGBCapable = GLX_DONT_CARE;
 }
 
 #define MATCH_DONT_CARE( param )        \
@@ -1288,7 +1289,7 @@ glXChooseVisual(Display * dpy, int screen, int *attribList)
    }
 
 #ifdef GLX_USE_APPLEGL
-   if(visualList && getenv("LIBGL_DUMP_VISUALID")) {
+   if(visualList && env_var_as_boolean("LIBGL_DUMP_VISUALID", false)) {
       printf("visualid 0x%lx\n", visualList[0].visualid);
    }
 #endif
@@ -1403,15 +1404,9 @@ glXImportContextEXT(Display *dpy, GLXContextID contextID)
    xGLXQueryContextReply reply;
    CARD8 opcode;
    struct glx_context *ctx;
-
-   /* This GLX implementation knows about 5 different properties, so
-    * allow the server to send us one of each.
-    */
-   int propList[5 * 2], *pProp, nPropListBytes;
-   int numProps;
-   int i, renderType;
-   XID share;
-   struct glx_config *mode;
+   int i, renderType = GLX_RGBA_TYPE; /* By default, assume RGBA context */
+   XID share = None;
+   struct glx_config *mode = NULL;
    uint32_t fbconfigID = 0;
    uint32_t visualID = 0;
    uint32_t screen = 0;
@@ -1436,7 +1431,7 @@ glXImportContextEXT(Display *dpy, GLXContextID contextID)
       return NULL;
    }
 
-   if (__glXIsDirect(dpy, contextID))
+   if (__glXIsDirect(dpy, contextID, NULL))
       return NULL;
 
    opcode = __glXSetupForCommand(dpy);
@@ -1469,42 +1464,36 @@ glXImportContextEXT(Display *dpy, GLXContextID contextID)
       req->context = contextID;
    }
 
-   _XReply(dpy, (xReply *) & reply, 0, False);
-
-   if (reply.n <= __GLX_MAX_CONTEXT_PROPS)
-      nPropListBytes = reply.n * 2 * sizeof propList[0];
-   else
-      nPropListBytes = 0;
-   _XRead(dpy, (char *) propList, nPropListBytes);
+   if (_XReply(dpy, (xReply *) & reply, 0, False) &&
+       reply.n < (INT32_MAX / 2)) {
+
+      for (i = 0; i < reply.n; i++) {
+         int prop[2];
+
+         _XRead(dpy, (char *)prop, sizeof(prop));
+         switch (prop[0]) {
+         case GLX_SCREEN:
+            screen = prop[1];
+            got_screen = True;
+            break;
+         case GLX_SHARE_CONTEXT_EXT:
+            share = prop[1];
+            break;
+         case GLX_VISUAL_ID_EXT:
+            visualID = prop[1];
+            break;
+         case GLX_FBCONFIG_ID:
+            fbconfigID = prop[1];
+            break;
+         case GLX_RENDER_TYPE:
+            renderType = prop[1];
+            break;
+         }
+      }
+   }
    UnlockDisplay(dpy);
    SyncHandle();
 
-   numProps = nPropListBytes / (2 * sizeof(propList[0]));
-   share = None;
-   mode = NULL;
-   renderType = GLX_RGBA_TYPE; /* By default, assume RGBA context */
-   pProp = propList;
-
-   for (i = 0, pProp = propList; i < numProps; i++, pProp += 2)
-      switch (pProp[0]) {
-      case GLX_SCREEN:
-        screen = pProp[1];
-        got_screen = True;
-        break;
-      case GLX_SHARE_CONTEXT_EXT:
-        share = pProp[1];
-        break;
-      case GLX_VISUAL_ID_EXT:
-        visualID = pProp[1];
-        break;
-      case GLX_FBCONFIG_ID:
-        fbconfigID = pProp[1];
-        break;
-      case GLX_RENDER_TYPE:
-        renderType = pProp[1];
-        break;
-      }
-
    if (!got_screen)
       return NULL;
 
@@ -1742,7 +1731,9 @@ __glXSwapIntervalSGI(int interval)
 {
    xGLXVendorPrivateReq *req;
    struct glx_context *gc = __glXGetCurrentContext();
+#ifdef GLX_DIRECT_RENDERING
    struct glx_screen *psc;
+#endif
    Display *dpy;
    CARD32 *interval_ptr;
    CARD8 opcode;
@@ -1755,9 +1746,9 @@ __glXSwapIntervalSGI(int interval)
       return GLX_BAD_VALUE;
    }
 
+#ifdef GLX_DIRECT_RENDERING
    psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
 
-#ifdef GLX_DIRECT_RENDERING
    if (gc->isDirect && psc && psc->driScreen &&
           psc->driScreen->setSwapInterval) {
       __GLXDRIdrawable *pdraw =
@@ -1857,32 +1848,26 @@ __glXGetSwapIntervalMESA(void)
 static int
 __glXGetVideoSyncSGI(unsigned int *count)
 {
+#ifdef GLX_DIRECT_RENDERING
    int64_t ust, msc, sbc;
    int ret;
    struct glx_context *gc = __glXGetCurrentContext();
    struct glx_screen *psc;
-#ifdef GLX_DIRECT_RENDERING
    __GLXDRIdrawable *pdraw;
-#endif
 
    if (gc == &dummyContext)
       return GLX_BAD_CONTEXT;
 
-#ifdef GLX_DIRECT_RENDERING
    if (!gc->isDirect)
       return GLX_BAD_CONTEXT;
-#endif
 
    psc = GetGLXScreenConfigs(gc->currentDpy, gc->screen);
-#ifdef GLX_DIRECT_RENDERING
    pdraw = GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
-#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
     * FIXME: documentation for the GLX encoding.
     */
-#ifdef GLX_DIRECT_RENDERING
    if (psc && psc->driScreen && psc->driScreen->getDrawableMSC) {
       ret = psc->driScreen->getDrawableMSC(psc, pdraw, &ust, &msc, &sbc);
       *count = (unsigned) msc;
@@ -1897,12 +1882,12 @@ static int
 __glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
 {
    struct glx_context *gc = __glXGetCurrentContext();
-   struct glx_screen *psc;
 #ifdef GLX_DIRECT_RENDERING
+   struct glx_screen *psc;
    __GLXDRIdrawable *pdraw;
-#endif
    int64_t ust, msc, sbc;
    int ret;
+#endif
 
    if (divisor <= 0 || remainder < 0)
       return GLX_BAD_VALUE;
@@ -1913,14 +1898,10 @@ __glXWaitVideoSyncSGI(int divisor, int remainder, unsigned int *count)
 #ifdef GLX_DIRECT_RENDERING
    if (!gc->isDirect)
       return GLX_BAD_CONTEXT;
-#endif
 
    psc = GetGLXScreenConfigs( gc->currentDpy, gc->screen);
-#ifdef GLX_DIRECT_RENDERING
    pdraw = GetGLXDRIDrawable(gc->currentDpy, gc->currentDrawable);
-#endif
 
-#ifdef GLX_DIRECT_RENDERING
    if (psc && psc->driScreen && psc->driScreen->waitForMSC) {
       ret = psc->driScreen->waitForMSC(pdraw, 0, divisor, remainder, &ust, &msc,
                                       &sbc);
@@ -2051,40 +2032,6 @@ glXGetFBConfigFromVisualSGIX(Display * dpy, XVisualInfo * vis)
 }
 
 #ifndef GLX_USE_APPLEGL
-/*
-** GLX_SGIX_swap_group
-*/
-static void
-__glXJoinSwapGroupSGIX(Display * dpy, GLXDrawable drawable,
-                       GLXDrawable member)
-{
-   (void) dpy;
-   (void) drawable;
-   (void) member;
-}
-
-
-/*
-** GLX_SGIX_swap_barrier
-*/
-static void
-__glXBindSwapBarrierSGIX(Display * dpy, GLXDrawable drawable, int barrier)
-{
-   (void) dpy;
-   (void) drawable;
-   (void) barrier;
-}
-
-static Bool
-__glXQueryMaxSwapBarriersSGIX(Display * dpy, int screen, int *max)
-{
-   (void) dpy;
-   (void) screen;
-   (void) max;
-   return False;
-}
-
-
 /*
 ** GLX_OML_sync_control
 */
@@ -2093,11 +2040,11 @@ __glXGetSyncValuesOML(Display * dpy, GLXDrawable drawable,
                       int64_t * ust, int64_t * msc, int64_t * sbc)
 {
    struct glx_display * const priv = __glXInitialize(dpy);
-   int ret;
 #ifdef GLX_DIRECT_RENDERING
+   int ret;
    __GLXDRIdrawable *pdraw;
-#endif
    struct glx_screen *psc;
+#endif
 
    if (!priv)
       return False;
@@ -2119,7 +2066,7 @@ _X_HIDDEN GLboolean
 __glxGetMscRate(struct glx_screen *psc,
                int32_t * numerator, int32_t * denominator)
 {
-#ifdef XF86VIDMODE
+#if !defined(GLX_USE_WINDOWSGL)
    XF86VidModeModeLine mode_line;
    int dot_clock;
    int i;
@@ -2166,7 +2113,6 @@ __glxGetMscRate(struct glx_screen *psc,
 
       return True;
    }
-   else
 #endif
 
    return False;
@@ -2193,7 +2139,7 @@ _X_HIDDEN GLboolean
 __glXGetMscRateOML(Display * dpy, GLXDrawable drawable,
                    int32_t * numerator, int32_t * denominator)
 {
-#if defined( GLX_DIRECT_RENDERING ) && defined( XF86VIDMODE )
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL) && !defined(GLX_USE_WINDOWSGL)
    __GLXDRIdrawable *draw = GetGLXDRIDrawable(dpy, drawable);
 
    if (draw == NULL)
@@ -2439,7 +2385,7 @@ __glXBindTexImageEXT(Display * dpy,
 {
    struct glx_context *gc = __glXGetCurrentContext();
 
-   if (gc == &dummyContext || gc->vtable->bind_tex_image == NULL)
+   if (gc->vtable->bind_tex_image == NULL)
       return;
 
    gc->vtable->bind_tex_image(dpy, drawable, buffer, attrib_list);
@@ -2450,7 +2396,7 @@ __glXReleaseTexImageEXT(Display * dpy, GLXDrawable drawable, int buffer)
 {
    struct glx_context *gc = __glXGetCurrentContext();
 
-   if (gc == &dummyContext || gc->vtable->release_tex_image == NULL)
+   if (gc->vtable->release_tex_image == NULL)
       return;
 
    gc->vtable->release_tex_image(dpy, drawable, buffer);
@@ -2556,13 +2502,6 @@ static const struct name_address_pair GLX_functions[] = {
    GLX_FUNCTION(glXSelectEventSGIX),
    GLX_FUNCTION(glXGetSelectedEventSGIX),
 
-   /*** GLX_SGIX_swap_group ***/
-   GLX_FUNCTION2(glXJoinSwapGroupSGIX, __glXJoinSwapGroupSGIX),
-
-   /*** GLX_SGIX_swap_barrier ***/
-   GLX_FUNCTION2(glXBindSwapBarrierSGIX, __glXBindSwapBarrierSGIX),
-   GLX_FUNCTION2(glXQueryMaxSwapBarriersSGIX, __glXQueryMaxSwapBarriersSGIX),
-
    /*** GLX_MESA_copy_sub_buffer ***/
    GLX_FUNCTION2(glXCopySubBufferMESA, __glXCopySubBufferMESA),