glx/dri: Add utility function dri2_convert_glx_attribs
authorIan Romanick <ian.d.romanick@intel.com>
Thu, 1 Dec 2011 21:52:53 +0000 (13:52 -0800)
committerIan Romanick <ian.d.romanick@intel.com>
Mon, 2 Jan 2012 20:41:45 +0000 (12:41 -0800)
This converts all of the GLX data from glXCreateContextAttribsARB to
the values expected by the DRI driver interfaces.

Signed-off-by: Ian Romanick <ian.d.romanick@intel.com>
src/glx/dri_common.c
src/glx/dri_common.h

index 1482b88bd93d3f68acdf0bc02abc7696a510edc0..5d50aa92e530c50f319a8da616a109f5452c5d09 100644 (file)
@@ -419,4 +419,106 @@ driReleaseDrawables(struct glx_context *gc)
 
 }
 
+_X_HIDDEN bool
+dri2_convert_glx_attribs(unsigned num_attribs, const uint32_t *attribs,
+                        unsigned *major_ver, unsigned *minor_ver,
+                        uint32_t *flags, unsigned *api, unsigned *error)
+{
+   unsigned i;
+   bool got_profile = false;
+   uint32_t profile;
+   int render_type = GLX_RGBA_TYPE;
+
+   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;
+   }
+
+   *major_ver = 1;
+   *minor_ver = 0;
+
+   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_PROFILE_MASK_ARB:
+        profile = attribs[i * 2 + 1];
+        got_profile = true;
+        break;
+      case GLX_RENDER_TYPE:
+        render_type = attribs[i * 2 + 1];
+        break;
+      default:
+        /* If an unknown attribute is received, fail.
+         */
+        *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
+        return false;
+      }
+   }
+
+   *api = __DRI_API_OPENGL;
+   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;
+      default:
+        *error = __DRI_CTX_ERROR_BAD_API;
+        return false;
+      }
+   }
+
+   /* Unknown flag value.
+    */
+   if (*flags & ~(__DRI_CTX_FLAG_DEBUG | __DRI_CTX_FLAG_FORWARD_COMPATIBLE)) {
+      *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;
+}
+
 #endif /* GLX_DIRECT_RENDERING */
index 13b5ae471d13b84da9110cfde2e222c25e1666be..3bcdebcc5296d89399f743cc0d969763146f9f1d 100644 (file)
@@ -37,6 +37,7 @@
 #define _DRI_COMMON_H
 
 #include <GL/internal/dri_interface.h>
+#include <stdbool.h>
 
 typedef struct __GLXDRIconfigPrivateRec __GLXDRIconfigPrivate;
 
@@ -66,4 +67,9 @@ extern void ErrorMessageF(const char *f, ...);
 
 extern void *driOpenDriver(const char *driverName);
 
+extern bool
+dri2_convert_glx_attribs(unsigned num_attribs, const uint32_t *attribs,
+                        unsigned *major_ver, unsigned *minor_ver,
+                        uint32_t *flags, unsigned *api, unsigned *error);
+
 #endif /* _DRI_COMMON_H */