+_X_HIDDEN void
+driReleaseDrawables(struct glx_context *gc)
+{
+ 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);
+ }
+ }
+ }
+
+ 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);
+ }
+ }
+ }
+
+ 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;
+ }
+
+ 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;
+ }
+ }
+
+ 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;
+ }
+ }
+
+ /* 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;
+}
+