Added few more stubs so that control reaches to DestroyDevice().
[mesa.git] / src / glx / dri_common.c
index dbc6082313bef50b7333240a8ccc68206655514a..a5fae27fce9dbcfc385b5cf096e48f8fea97eda8 100644 (file)
  *   Kristian Høgsberg (krh@redhat.com)
  */
 
-#ifdef GLX_DIRECT_RENDERING
+#if defined(GLX_DIRECT_RENDERING) && !defined(GLX_USE_APPLEGL)
 
 #include <unistd.h>
 #include <dlfcn.h>
 #include <stdarg.h>
 #include "glxclient.h"
-#include "glcontextmodes.h"
 #include "dri_common.h"
+#include "loader.h"
 
 #ifndef RTLD_NOW
 #define RTLD_NOW 0
 #endif
 
 _X_HIDDEN void
-InfoMessageF(const char *f, ...)
+dri_message(int level, const char *f, ...)
 {
    va_list args;
-   const char *env;
-
-   if ((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) {
-      fprintf(stderr, "libGL: ");
-      va_start(args, f);
-      vfprintf(stderr, f, args);
-      va_end(args);
+   int threshold = _LOADER_WARNING;
+   const char *libgl_debug;
+
+   libgl_debug = getenv("LIBGL_DEBUG");
+   if (libgl_debug) {
+      if (strstr(libgl_debug, "quiet"))
+         threshold = _LOADER_FATAL;
+      else if (strstr(libgl_debug, "verbose"))
+         threshold = _LOADER_DEBUG;
    }
-}
 
-/**
- * Print error to stderr, unless LIBGL_DEBUG=="quiet".
- */
-_X_HIDDEN void
-ErrorMessageF(const char *f, ...)
-{
-   va_list args;
-   const char *env;
-
-   if ((env = getenv("LIBGL_DEBUG")) && !strstr(env, "quiet")) {
-      fprintf(stderr, "libGL error: ");
+   /* Note that the _LOADER_* levels are lower numbers for more severe. */
+   if (level <= threshold) {
+      fprintf(stderr, "libGL%s: ", level <= _LOADER_WARNING ? " error" : "");
       va_start(args, f);
       vfprintf(stderr, f, args);
       va_end(args);
    }
 }
 
-#ifndef DEFAULT_DRIVER_DIR
-/* this is normally defined in Mesa/configs/default with DRI_DRIVER_SEARCH_PATH */
-#define DEFAULT_DRIVER_DIR "/usr/local/lib/dri"
+#ifndef GL_LIB_NAME
+#define GL_LIB_NAME "libGL.so.1"
 #endif
 
 /**
@@ -92,81 +84,55 @@ ErrorMessageF(const char *f, ...)
  * directories specified by the \c LIBGL_DRIVERS_PATH environment variable in
  * order to find the driver.
  *
- * \param driverName - a name like "tdfx", "i810", "mga", etc.
+ * \param driverName - a name like "i965", "radeon", "nouveau", etc.
+ * \param out_driver_handle - Address to return the resulting dlopen() handle.
  *
  * \returns
- * A handle from \c dlopen, or \c NULL if driver file not found.
+ * The __DRIextension entrypoint table for the driver, or \c NULL if driver
+ * file not found.
  */
-_X_HIDDEN void *
-driOpenDriver(const char *driverName)
+_X_HIDDEN const __DRIextension **
+driOpenDriver(const char *driverName, void **out_driver_handle)
 {
-   void *glhandle, *handle;
-   const char *libPaths, *p, *next;
-   char realDriverName[200];
-   int len;
+   void *glhandle;
 
    /* Attempt to make sure libGL symbols will be visible to the driver */
-   glhandle = dlopen("libGL.so.1", RTLD_NOW | RTLD_GLOBAL);
-
-   libPaths = NULL;
-   if (geteuid() == getuid()) {
-      /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
-      libPaths = getenv("LIBGL_DRIVERS_PATH");
-      if (!libPaths)
-         libPaths = getenv("LIBGL_DRIVERS_DIR");        /* deprecated */
-   }
-   if (libPaths == NULL)
-      libPaths = DEFAULT_DRIVER_DIR;
-
-   handle = NULL;
-   for (p = libPaths; *p; p = next) {
-      next = strchr(p, ':');
-      if (next == NULL) {
-         len = strlen(p);
-         next = p + len;
-      }
-      else {
-         len = next - p;
-         next++;
-      }
+   glhandle = dlopen(GL_LIB_NAME, RTLD_NOW | RTLD_GLOBAL);
 
-#ifdef GLX_USE_TLS
-      snprintf(realDriverName, sizeof realDriverName,
-               "%.*s/tls/%s_dri.so", len, p, driverName);
-      InfoMessageF("OpenDriver: trying %s\n", realDriverName);
-      handle = dlopen(realDriverName, RTLD_NOW | RTLD_GLOBAL);
-#endif
+   static const char *search_path_vars[] = {
+      "LIBGL_DRIVERS_PATH",
+      "LIBGL_DRIVERS_DIR", /* deprecated */
+      NULL
+   };
 
-      if (handle == NULL) {
-         snprintf(realDriverName, sizeof realDriverName,
-                  "%.*s/%s_dri.so", len, p, driverName);
-         InfoMessageF("OpenDriver: trying %s\n", realDriverName);
-         handle = dlopen(realDriverName, RTLD_NOW | RTLD_GLOBAL);
-      }
-
-      if (handle != NULL)
-         break;
-      else
-         ErrorMessageF("dlopen %s failed (%s)\n", realDriverName, dlerror());
-   }
-
-   if (!handle)
-      ErrorMessageF("unable to load driver: %s_dri.so\n", driverName);
+   const __DRIextension **extensions =
+      loader_open_driver(driverName, out_driver_handle, search_path_vars);
 
    if (glhandle)
       dlclose(glhandle);
 
-   return handle;
+   return extensions;
+}
+
+static GLboolean
+__driGetMSCRate(__DRIdrawable *draw,
+               int32_t * numerator, int32_t * denominator,
+               void *loaderPrivate)
+{
+   __GLXDRIdrawable *glxDraw = loaderPrivate;
+
+   return __glxGetMscRate(glxDraw->psc, numerator, denominator);
 }
 
 _X_HIDDEN const __DRIsystemTimeExtension systemTimeExtension = {
-   {__DRI_SYSTEM_TIME, __DRI_SYSTEM_TIME_VERSION},
-   __glXGetUST,
-   __driGetMscRateOML
+   .base = {__DRI_SYSTEM_TIME, 1 },
+
+   .getUST              = __glXGetUST,
+   .getMSCRate          = __driGetMSCRate
 };
 
 #define __ATTRIB(attrib, field) \
-    { attrib, offsetof(__GLcontextModes, field) }
+    { attrib, offsetof(struct glx_config, field) }
 
 static const struct
 {
@@ -200,28 +166,29 @@ static const struct
       __ATTRIB(__DRI_ATTRIB_GREEN_MASK, greenMask),
       __ATTRIB(__DRI_ATTRIB_BLUE_MASK, blueMask),
       __ATTRIB(__DRI_ATTRIB_ALPHA_MASK, alphaMask),
+      __ATTRIB(__DRI_ATTRIB_RED_SHIFT, redShift),
+      __ATTRIB(__DRI_ATTRIB_GREEN_SHIFT, greenShift),
+      __ATTRIB(__DRI_ATTRIB_BLUE_SHIFT, blueShift),
+      __ATTRIB(__DRI_ATTRIB_ALPHA_SHIFT, alphaShift),
 #endif
       __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_WIDTH, maxPbufferWidth),
       __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_HEIGHT, maxPbufferHeight),
       __ATTRIB(__DRI_ATTRIB_MAX_PBUFFER_PIXELS, maxPbufferPixels),
       __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH, optimalPbufferWidth),
       __ATTRIB(__DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT, optimalPbufferHeight),
-#if 0
       __ATTRIB(__DRI_ATTRIB_SWAP_METHOD, swapMethod),
-#endif
-__ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGB, bindToTextureRgb),
+      __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGB, bindToTextureRgb),
       __ATTRIB(__DRI_ATTRIB_BIND_TO_TEXTURE_RGBA, bindToTextureRgba),
       __ATTRIB(__DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE,
                      bindToMipmapTexture),
-      __ATTRIB(__DRI_ATTRIB_YINVERTED, yInverted),};
-
-#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
+      __ATTRIB(__DRI_ATTRIB_YINVERTED, yInverted),
+      __ATTRIB(__DRI_ATTRIB_FRAMEBUFFER_SRGB_CAPABLE, sRGBCapable)
+};
 
 static int
-scalarEqual(__GLcontextModes * mode, unsigned int attrib, unsigned int value)
+scalarEqual(struct glx_config *mode, unsigned int attrib, unsigned int value)
 {
-   unsigned int glxValue;
-   int i;
+   unsigned glxValue, i;
 
    for (i = 0; i < ARRAY_SIZE(attribMap); i++)
       if (attribMap[i].attrib == attrib) {
@@ -233,8 +200,8 @@ scalarEqual(__GLcontextModes * mode, unsigned int attrib, unsigned int value)
 }
 
 static int
-driConfigEqual(const __DRIcoreExtension * core,
-               __GLcontextModes * modes, const __DRIconfig * driConfig)
+driConfigEqual(const __DRIcoreExtension *core,
+               struct glx_config *config, const __DRIconfig *driConfig)
 {
    unsigned int attrib, value, glxValue;
    int i;
@@ -247,10 +214,16 @@ driConfigEqual(const __DRIcoreExtension * core,
          if (value & __DRI_ATTRIB_RGBA_BIT) {
             glxValue |= GLX_RGBA_BIT;
          }
-         else if (value & __DRI_ATTRIB_COLOR_INDEX_BIT) {
+         if (value & __DRI_ATTRIB_COLOR_INDEX_BIT) {
             glxValue |= GLX_COLOR_INDEX_BIT;
          }
-         if (glxValue != modes->renderType)
+         if (value & __DRI_ATTRIB_FLOAT_BIT) {
+            glxValue |= GLX_RGBA_FLOAT_BIT_ARB;
+         }
+         if (value & __DRI_ATTRIB_UNSIGNED_FLOAT_BIT) {
+            glxValue |= GLX_RGBA_UNSIGNED_FLOAT_BIT_EXT;
+         }
+         if (glxValue != config->renderType)
             return GL_FALSE;
          break;
 
@@ -261,7 +234,7 @@ driConfigEqual(const __DRIcoreExtension * core,
             glxValue = GLX_SLOW_CONFIG;
          else
             glxValue = GLX_NONE;
-         if (glxValue != modes->visualRating)
+         if (glxValue != config->visualRating)
             return GL_FALSE;
          break;
 
@@ -273,13 +246,26 @@ driConfigEqual(const __DRIcoreExtension * core,
             glxValue |= GLX_TEXTURE_2D_BIT_EXT;
          if (value & __DRI_ATTRIB_TEXTURE_RECTANGLE_BIT)
             glxValue |= GLX_TEXTURE_RECTANGLE_BIT_EXT;
-         if (modes->bindToTextureTargets != GLX_DONT_CARE &&
-             glxValue != modes->bindToTextureTargets)
+         if (config->bindToTextureTargets != GLX_DONT_CARE &&
+             glxValue != config->bindToTextureTargets)
+            return GL_FALSE;
+         break;
+
+      case __DRI_ATTRIB_SWAP_METHOD:
+         if (value == __DRI_ATTRIB_SWAP_EXCHANGE)
+            glxValue = GLX_SWAP_EXCHANGE_OML;
+         else if (value == __DRI_ATTRIB_SWAP_COPY)
+            glxValue = GLX_SWAP_COPY_OML;
+         else
+            glxValue = GLX_SWAP_UNDEFINED_OML;
+
+         if (!scalarEqual(config, attrib, glxValue))
             return GL_FALSE;
+
          break;
 
       default:
-         if (!scalarEqual(modes, attrib, value))
+         if (!scalarEqual(config, attrib, value))
             return GL_FALSE;
       }
    }
@@ -287,41 +273,41 @@ driConfigEqual(const __DRIcoreExtension * core,
    return GL_TRUE;
 }
 
-static __GLcontextModes *
+static struct glx_config *
 createDriMode(const __DRIcoreExtension * core,
-              __GLcontextModes * modes, const __DRIconfig ** driConfigs)
+             struct glx_config *config, const __DRIconfig **driConfigs)
 {
-   __GLXDRIconfigPrivate *config;
+   __GLXDRIconfigPrivate *driConfig;
    int i;
 
    for (i = 0; driConfigs[i]; i++) {
-      if (driConfigEqual(core, modes, driConfigs[i]))
+      if (driConfigEqual(core, config, driConfigs[i]))
          break;
    }
 
    if (driConfigs[i] == NULL)
       return NULL;
 
-   config = Xmalloc(sizeof *config);
-   if (config == NULL)
+   driConfig = malloc(sizeof *driConfig);
+   if (driConfig == NULL)
       return NULL;
 
-   config->modes = *modes;
-   config->driConfig = driConfigs[i];
+   driConfig->base = *config;
+   driConfig->driConfig = driConfigs[i];
 
-   return &config->modes;
+   return &driConfig->base;
 }
 
-_X_HIDDEN __GLcontextModes *
+_X_HIDDEN struct glx_config *
 driConvertConfigs(const __DRIcoreExtension * core,
-                  __GLcontextModes * modes, const __DRIconfig ** configs)
+                  struct glx_config *configs, const __DRIconfig **driConfigs)
 {
-   __GLcontextModes head, *tail, *m;
+   struct glx_config head, *tail, *m;
 
    tail = &head;
    head.next = NULL;
-   for (m = modes; m; m = m->next) {
-      tail->next = createDriMode(core, m, configs);
+   for (m = configs; m; m = m->next) {
+      tail->next = createDriMode(core, m, driConfigs);
       if (tail->next == NULL) {
          /* no matching dri config for m */
          continue;
@@ -331,125 +317,306 @@ driConvertConfigs(const __DRIcoreExtension * core,
       tail = tail->next;
    }
 
-   _gl_context_modes_destroy(modes);
-
    return head.next;
 }
 
-/* Bind DRI1 specific extensions */
 _X_HIDDEN void
-driBindExtensions(__GLXscreenConfigs *psc)
+driDestroyConfigs(const __DRIconfig **configs)
 {
-   const __DRIextension **extensions;
    int i;
 
-   extensions = psc->core->getExtensions(psc->__driScreen);
-
-   for (i = 0; extensions[i]; i++) {
-#ifdef __DRI_SWAP_CONTROL
-      /* No DRI2 support for swap_control at the moment, since SwapBuffers
-       * is done by the X server */
-      if (strcmp(extensions[i]->name, __DRI_SWAP_CONTROL) == 0) {
-        psc->swapControl = (__DRIswapControlExtension *) extensions[i];
-        __glXEnableDirectExtension(psc, "GLX_SGI_swap_control");
-        __glXEnableDirectExtension(psc, "GLX_MESA_swap_control");
-      }
-#endif
-
-#ifdef __DRI_MEDIA_STREAM_COUNTER
-      if (strcmp(extensions[i]->name, __DRI_MEDIA_STREAM_COUNTER) == 0) {
-         psc->msc = (__DRImediaStreamCounterExtension *) extensions[i];
-         __glXEnableDirectExtension(psc, "GLX_SGI_video_sync");
-      }
-#endif
+   for (i = 0; configs[i]; i++)
+      free((__DRIconfig *) configs[i]);
+   free(configs);
+}
 
-#ifdef __DRI_SWAP_BUFFER_COUNTER
-      /* No driver supports this at this time and the extension is
-       * not defined in dri_interface.h.  Will enable
-       * GLX_OML_sync_control if implemented. */
-#endif
+static struct glx_config *
+driInferDrawableConfig(struct glx_screen *psc, GLXDrawable draw)
+{
+   unsigned int fbconfig = 0;
 
-      /* Ignore unknown extensions */
+   if (__glXGetDrawableAttribute(psc->dpy, draw, GLX_FBCONFIG_ID, &fbconfig)) {
+      return glx_config_find_fbconfig(psc->configs, fbconfig);
    }
+
+   return NULL;
 }
 
-/* Bind DRI2 specific extensions */
-_X_HIDDEN void
-dri2BindExtensions(__GLXscreenConfigs *psc)
+_X_HIDDEN __GLXDRIdrawable *
+driFetchDrawable(struct glx_context *gc, GLXDrawable glxDrawable)
 {
-   const __DRIextension **extensions;
-   int i;
+   struct glx_display *const priv = __glXInitialize(gc->psc->dpy);
+   __GLXDRIdrawable *pdraw;
+   struct glx_screen *psc;
+   struct glx_config *config = gc->config;
 
-   extensions = psc->core->getExtensions(psc->__driScreen);
+   if (priv == NULL)
+      return NULL;
 
-   for (i = 0; extensions[i]; i++) {
-#ifdef __DRI_TEX_BUFFER
-      if ((strcmp(extensions[i]->name, __DRI_TEX_BUFFER) == 0)) {
-        psc->texBuffer = (__DRItexBufferExtension *) extensions[i];
-        __glXEnableDirectExtension(psc, "GLX_EXT_texture_from_pixmap");
-      }
-#endif
+   if (glxDrawable == None)
+      return NULL;
 
-      __glXEnableDirectExtension(psc, "GLX_SGI_video_sync");
-      __glXEnableDirectExtension(psc, "GLX_SGI_swap_control");
-      __glXEnableDirectExtension(psc, "GLX_MESA_swap_control");
+   psc = priv->screens[gc->screen];
+   if (priv->drawHash == NULL)
+      return NULL;
 
-      /* FIXME: if DRI2 version supports it... */
-      __glXEnableDirectExtension(psc, "INTEL_swap_event");
+   if (__glxHashLookup(priv->drawHash, glxDrawable, (void *) &pdraw) == 0) {
+      pdraw->refcount ++;
+      return pdraw;
+   }
 
-#ifdef __DRI2_FLUSH
-      if ((strcmp(extensions[i]->name, __DRI2_FLUSH) == 0)) {
-        psc->f = (__DRI2flushExtension *) extensions[i];
-        /* internal driver extension, no GL extension exposed */
-      }
-#endif
+   if (config == NULL)
+      config = driInferDrawableConfig(gc->psc, glxDrawable);
+   if (config == NULL)
+      return NULL;
 
-#ifdef __DRI2_CONFIG_QUERY
-      if ((strcmp(extensions[i]->name, __DRI2_CONFIG_QUERY) == 0))
-        psc->config = (__DRI2configQueryExtension *) extensions[i];
-#endif
+   pdraw = psc->driScreen->createDrawable(psc, glxDrawable, glxDrawable,
+                                          config);
+
+   if (pdraw == NULL) {
+      ErrorMessageF("failed to create drawable\n");
+      return NULL;
    }
+
+   if (__glxHashInsert(priv->drawHash, glxDrawable, pdraw)) {
+      (*pdraw->destroyDrawable) (pdraw);
+      return NULL;
+   }
+   pdraw->refcount = 1;
+
+   return pdraw;
 }
 
-/* Bind extensions common to DRI1 and DRI2 */
 _X_HIDDEN void
-driBindCommonExtensions(__GLXscreenConfigs *psc)
+driReleaseDrawables(struct glx_context *gc)
 {
-   const __DRIextension **extensions;
-   int i;
-
-   extensions = psc->core->getExtensions(psc->__driScreen);
-
-   for (i = 0; extensions[i]; i++) {
-#ifdef __DRI_COPY_SUB_BUFFER
-      if (strcmp(extensions[i]->name, __DRI_COPY_SUB_BUFFER) == 0) {
-        psc->driCopySubBuffer = (__DRIcopySubBufferExtension *) extensions[i];
-        __glXEnableDirectExtension(psc, "GLX_MESA_copy_sub_buffer");
+   const struct glx_display *priv = gc->psc->display;
+   __GLXDRIdrawable *pdraw;
+
+   if (priv == NULL)
+      return;
+
+   if (__glxHashLookup(priv->drawHash,
+                      gc->currentDrawable, (void *) &pdraw) == 0) {
+      if (pdraw->drawable == pdraw->xDrawable) {
+        pdraw->refcount --;
+        if (pdraw->refcount == 0) {
+           pdraw->destroyDrawable(pdraw);
+           __glxHashDelete(priv->drawHash, gc->currentDrawable);
+        }
       }
-#endif
+   }
 
-#ifdef __DRI_ALLOCATE
-      if (strcmp(extensions[i]->name, __DRI_ALLOCATE) == 0) {
-        psc->allocate = (__DRIallocateExtension *) extensions[i];
-        __glXEnableDirectExtension(psc, "GLX_MESA_allocate_memory");
+   if (__glxHashLookup(priv->drawHash,
+                      gc->currentReadable, (void *) &pdraw) == 0) {
+      if (pdraw->drawable == pdraw->xDrawable) {
+        pdraw->refcount --;
+        if (pdraw->refcount == 0) {
+           pdraw->destroyDrawable(pdraw);
+           __glxHashDelete(priv->drawHash, gc->currentReadable);
+        }
       }
-#endif
+   }
+
+   gc->currentDrawable = None;
+   gc->currentReadable = None;
+
+}
+
+_X_HIDDEN bool
+dri2_convert_glx_attribs(unsigned num_attribs, const uint32_t *attribs,
+                         unsigned *major_ver, unsigned *minor_ver,
+                         uint32_t *render_type, uint32_t *flags, unsigned *api,
+                         int *reset, int *release, unsigned *error)
+{
+   unsigned i;
+   bool got_profile = false;
+   int no_error = 0;
+   uint32_t profile;
+
+   *major_ver = 1;
+   *minor_ver = 0;
+   *render_type = GLX_RGBA_TYPE;
+   *reset = __DRI_CTX_RESET_NO_NOTIFICATION;
+   *release = __DRI_CTX_RELEASE_BEHAVIOR_FLUSH;
+   *flags = 0;
+   *api = __DRI_API_OPENGL;
+
+   if (num_attribs == 0) {
+      return true;
+   }
+
+   /* This is actually an internal error, but what the heck.
+    */
+   if (attribs == NULL) {
+      *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
+      return false;
+   }
 
-#ifdef __DRI_FRAME_TRACKING
-      if (strcmp(extensions[i]->name, __DRI_FRAME_TRACKING) == 0) {
-        psc->frameTracking = (__DRIframeTrackingExtension *) extensions[i];
-        __glXEnableDirectExtension(psc, "GLX_MESA_swap_frame_usage");
+   for (i = 0; i < num_attribs; i++) {
+      switch (attribs[i * 2]) {
+      case GLX_CONTEXT_MAJOR_VERSION_ARB:
+        *major_ver = attribs[i * 2 + 1];
+        break;
+      case GLX_CONTEXT_MINOR_VERSION_ARB:
+        *minor_ver = attribs[i * 2 + 1];
+        break;
+      case GLX_CONTEXT_FLAGS_ARB:
+        *flags = attribs[i * 2 + 1];
+        break;
+      case GLX_CONTEXT_OPENGL_NO_ERROR_ARB:
+        no_error = attribs[i * 2 + 1];
+        break;
+      case GLX_CONTEXT_PROFILE_MASK_ARB:
+        profile = attribs[i * 2 + 1];
+        got_profile = true;
+        break;
+      case GLX_RENDER_TYPE:
+         *render_type = attribs[i * 2 + 1];
+        break;
+      case GLX_CONTEXT_RESET_NOTIFICATION_STRATEGY_ARB:
+         switch (attribs[i * 2 + 1]) {
+         case GLX_NO_RESET_NOTIFICATION_ARB:
+            *reset = __DRI_CTX_RESET_NO_NOTIFICATION;
+            break;
+         case GLX_LOSE_CONTEXT_ON_RESET_ARB:
+            *reset = __DRI_CTX_RESET_LOSE_CONTEXT;
+            break;
+         default:
+            *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
+            return false;
+         }
+         break;
+      case GLX_CONTEXT_RELEASE_BEHAVIOR_ARB:
+         switch (attribs[i * 2 + 1]) {
+         case GLX_CONTEXT_RELEASE_BEHAVIOR_NONE_ARB:
+            *release = __DRI_CTX_RELEASE_BEHAVIOR_NONE;
+            break;
+         case GLX_CONTEXT_RELEASE_BEHAVIOR_FLUSH_ARB:
+            *release = __DRI_CTX_RELEASE_BEHAVIOR_FLUSH;
+            break;
+         default:
+            *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
+            return false;
+         }
+         break;
+      default:
+        /* If an unknown attribute is received, fail.
+         */
+        *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
+        return false;
       }
-#endif
+   }
 
-#ifdef __DRI_READ_DRAWABLE
-      if (strcmp(extensions[i]->name, __DRI_READ_DRAWABLE) == 0) {
-        __glXEnableDirectExtension(psc, "GLX_SGI_make_current_read");
+   if (no_error) {
+      *flags |= __DRI_CTX_FLAG_NO_ERROR;
+   }
+
+   if (!got_profile) {
+      if (*major_ver > 3 || (*major_ver == 3 && *minor_ver >= 2))
+        *api = __DRI_API_OPENGL_CORE;
+   } else {
+      switch (profile) {
+      case GLX_CONTEXT_CORE_PROFILE_BIT_ARB:
+        /* There are no profiles before OpenGL 3.2.  The
+         * GLX_ARB_create_context_profile spec says:
+         *
+         *     "If the requested OpenGL version is less than 3.2,
+         *     GLX_CONTEXT_PROFILE_MASK_ARB is ignored and the functionality
+         *     of the context is determined solely by the requested version."
+         */
+        *api = (*major_ver > 3 || (*major_ver == 3 && *minor_ver >= 2))
+           ? __DRI_API_OPENGL_CORE : __DRI_API_OPENGL;
+        break;
+      case GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB:
+        *api = __DRI_API_OPENGL;
+        break;
+      case GLX_CONTEXT_ES_PROFILE_BIT_EXT:
+         if (*major_ver >= 3)
+            *api = __DRI_API_GLES3;
+         else if (*major_ver == 2 && *minor_ver == 0)
+            *api = __DRI_API_GLES2;
+         else if (*major_ver == 1 && *minor_ver < 2)
+            *api = __DRI_API_GLES;
+         else {
+            *error = __DRI_CTX_ERROR_BAD_API;
+            return false;
+         }
+         break;
+      default:
+        *error = __DRI_CTX_ERROR_BAD_API;
+        return false;
       }
-#endif
+   }
 
-      /* Ignore unknown extensions */
+   /* Unknown flag value.
+    */
+   if (*flags & ~(__DRI_CTX_FLAG_DEBUG | __DRI_CTX_FLAG_FORWARD_COMPATIBLE
+                  | __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS
+                  | __DRI_CTX_FLAG_NO_ERROR)) {
+      *error = __DRI_CTX_ERROR_UNKNOWN_FLAG;
+      return false;
    }
+
+   /* There are no forward-compatible contexts before OpenGL 3.0.  The
+    * GLX_ARB_create_context spec says:
+    *
+    *     "Forward-compatible contexts are defined only for OpenGL versions
+    *     3.0 and later."
+    */
+   if (*major_ver < 3 && (*flags & __DRI_CTX_FLAG_FORWARD_COMPATIBLE) != 0) {
+      *error = __DRI_CTX_ERROR_BAD_FLAG;
+      return false;
+   }
+
+   if (*major_ver >= 3 && *render_type == GLX_COLOR_INDEX_TYPE) {
+      *error = __DRI_CTX_ERROR_BAD_FLAG;
+      return false;
+   }
+
+   *error = __DRI_CTX_ERROR_SUCCESS;
+   return true;
+}
+
+_X_HIDDEN bool
+dri2_check_no_error(uint32_t flags, struct glx_context *share_context,
+                    int major, unsigned *error)
+{
+   Bool noError = flags & __DRI_CTX_FLAG_NO_ERROR;
+
+   /* The KHR_no_error specs say:
+    *
+    *    Requires OpenGL ES 2.0 or OpenGL 2.0.
+    */
+   if (noError && major < 2) {
+      *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
+      return false;
+   }
+
+   /* The GLX_ARB_create_context_no_error specs say:
+    *
+    *    BadMatch is generated if the value of GLX_CONTEXT_OPENGL_NO_ERROR_ARB
+    *    used to create <share_context> does not match the value of
+    *    GLX_CONTEXT_OPENGL_NO_ERROR_ARB for the context being created.
+    */
+   if (share_context && !!share_context->noError != !!noError) {
+      *error = __DRI_CTX_ERROR_BAD_FLAG;
+      return false;
+   }
+
+   /* The GLX_ARB_create_context_no_error specs say:
+    *
+    *    BadMatch is generated if the GLX_CONTEXT_OPENGL_NO_ERROR_ARB is TRUE at
+    *    the same time as a debug or robustness context is specified.
+    *
+    */
+   if (noError && ((flags & __DRI_CTX_FLAG_DEBUG) ||
+                   (flags & __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS))) {
+      *error = __DRI_CTX_ERROR_BAD_FLAG;
+      return false;
+   }
+
+   return true;
 }
 
 #endif /* GLX_DIRECT_RENDERING */