st/xa: Fix a memory leak
[mesa.git] / src / gallium / state_trackers / dri / dri_context.c
index 91d2d1fbbe87c5bc4a982f7393c205e1a2dea5b9..af9e3325f98ec7a6ce711382bda4c80bc33e7d38 100644 (file)
 #include "state_tracker/drm_driver.h"
 
 #include "pipe/p_context.h"
+#include "pipe-loader/pipe_loader.h"
 #include "state_tracker/st_context.h"
 
 GLboolean
 dri_create_context(gl_api api, const struct gl_config * visual,
-                  __DRIcontext * cPriv,
-                  unsigned major_version,
-                  unsigned minor_version,
-                  uint32_t flags,
-                   bool notify_reset,
-                  unsigned *error,
-                  void *sharedContextPrivate)
+                   __DRIcontext * cPriv,
+                   const struct __DriverContextConfig *ctx_config,
+                   unsigned *error,
+                   void *sharedContextPrivate)
 {
    __DRIscreen *sPriv = cPriv->driScreenPriv;
    struct dri_screen *screen = dri_screen(sPriv);
@@ -57,17 +55,26 @@ dri_create_context(gl_api api, const struct gl_config * visual,
    struct st_context_attribs attribs;
    enum st_context_error ctx_err = 0;
    unsigned allowed_flags = __DRI_CTX_FLAG_DEBUG |
-                            __DRI_CTX_FLAG_FORWARD_COMPATIBLE;
-
-   if (screen->has_reset_status_query)
+                            __DRI_CTX_FLAG_FORWARD_COMPATIBLE |
+                            __DRI_CTX_FLAG_NO_ERROR;
+   unsigned allowed_attribs =
+      __DRIVER_CONTEXT_ATTRIB_PRIORITY |
+      __DRIVER_CONTEXT_ATTRIB_RELEASE_BEHAVIOR;
+   const __DRIbackgroundCallableExtension *backgroundCallable =
+      screen->sPriv->dri2.backgroundCallable;
+   const struct driOptionCache *optionCache = &screen->dev->option_cache;
+
+   if (screen->has_reset_status_query) {
       allowed_flags |= __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS;
+      allowed_attribs |= __DRIVER_CONTEXT_ATTRIB_RESET_STRATEGY;
+   }
 
-   if (flags & ~allowed_flags) {
+   if (ctx_config->flags & ~allowed_flags) {
       *error = __DRI_CTX_ERROR_UNKNOWN_FLAG;
       goto fail;
    }
 
-   if (!screen->has_reset_status_query && notify_reset) {
+   if (ctx_config->attribute_mask & ~allowed_attribs) {
       *error = __DRI_CTX_ERROR_UNKNOWN_ATTRIBUTE;
       goto fail;
    }
@@ -82,12 +89,17 @@ dri_create_context(gl_api api, const struct gl_config * visual,
       break;
    case API_OPENGL_COMPAT:
    case API_OPENGL_CORE:
-      attribs.profile = api == API_OPENGL_COMPAT ? ST_PROFILE_DEFAULT
-                                                 : ST_PROFILE_OPENGL_CORE;
-      attribs.major = major_version;
-      attribs.minor = minor_version;
+      if (driQueryOptionb(optionCache, "force_compat_profile")) {
+         attribs.profile = ST_PROFILE_DEFAULT;
+      } else {
+         attribs.profile = api == API_OPENGL_COMPAT ? ST_PROFILE_DEFAULT
+                                                    : ST_PROFILE_OPENGL_CORE;
+      }
+
+      attribs.major = ctx_config->major_version;
+      attribs.minor = ctx_config->minor_version;
 
-      if ((flags & __DRI_CTX_FLAG_FORWARD_COMPATIBLE) != 0)
+      if ((ctx_config->flags & __DRI_CTX_FLAG_FORWARD_COMPATIBLE) != 0)
         attribs.flags |= ST_CONTEXT_FLAG_FORWARD_COMPATIBLE;
       break;
    default:
@@ -95,17 +107,40 @@ dri_create_context(gl_api api, const struct gl_config * visual,
       goto fail;
    }
 
-   if ((flags & __DRI_CTX_FLAG_DEBUG) != 0)
+   if ((ctx_config->flags & __DRI_CTX_FLAG_DEBUG) != 0)
       attribs.flags |= ST_CONTEXT_FLAG_DEBUG;
 
-   if (flags & __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS)
+   if (ctx_config->flags & __DRI_CTX_FLAG_ROBUST_BUFFER_ACCESS)
       attribs.flags |= ST_CONTEXT_FLAG_ROBUST_ACCESS;
 
-   if (notify_reset)
-      attribs.flags |= ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED;
+   if (ctx_config->attribute_mask & __DRIVER_CONTEXT_ATTRIB_RESET_STRATEGY)
+      if (ctx_config->reset_strategy != __DRI_CTX_RESET_NO_NOTIFICATION)
+         attribs.flags |= ST_CONTEXT_FLAG_RESET_NOTIFICATION_ENABLED;
+
+   if (ctx_config->flags & __DRI_CTX_FLAG_NO_ERROR)
+      attribs.flags |= ST_CONTEXT_FLAG_NO_ERROR;
+
+   if (ctx_config->attribute_mask & __DRIVER_CONTEXT_ATTRIB_PRIORITY) {
+      switch (ctx_config->priority) {
+      case __DRI_CTX_PRIORITY_LOW:
+         attribs.flags |= ST_CONTEXT_FLAG_LOW_PRIORITY;
+         break;
+      case __DRI_CTX_PRIORITY_HIGH:
+         attribs.flags |= ST_CONTEXT_FLAG_HIGH_PRIORITY;
+         break;
+      default:
+         break;
+      }
+   }
+
+   if ((ctx_config->attribute_mask & __DRIVER_CONTEXT_ATTRIB_RELEASE_BEHAVIOR)
+       && (ctx_config->release_behavior == __DRI_CTX_RELEASE_BEHAVIOR_NONE))
+      attribs.flags |= ST_CONTEXT_FLAG_RELEASE_NONE;
 
+   struct dri_context *share_ctx = NULL;
    if (sharedContextPrivate) {
-      st_share = ((struct dri_context *)sharedContextPrivate)->st;
+      share_ctx = (struct dri_context *)sharedContextPrivate;
+      st_share = share_ctx->st;
    }
 
    ctx = CALLOC_STRUCT(dri_context);
@@ -118,6 +153,9 @@ dri_create_context(gl_api api, const struct gl_config * visual,
    ctx->cPriv = cPriv;
    ctx->sPriv = sPriv;
 
+   if (driQueryOptionb(&screen->dev->option_cache, "mesa_no_error"))
+      attribs.flags |= ST_CONTEXT_FLAG_NO_ERROR;
+
    attribs.options = screen->options;
    dri_fill_st_visual(&attribs.visual, screen, visual);
    ctx->st = stapi->create_context(stapi, &screen->base, &attribs, &ctx_err,
@@ -153,15 +191,27 @@ dri_create_context(gl_api api, const struct gl_config * visual,
 
    if (ctx->st->cso_context) {
       ctx->pp = pp_init(ctx->st->pipe, screen->pp_enabled, ctx->st->cso_context);
-      ctx->hud = hud_create(ctx->st->pipe, ctx->st->cso_context);
+      ctx->hud = hud_create(ctx->st->cso_context,
+                            share_ctx ? share_ctx->hud : NULL);
    }
 
    /* Do this last. */
    if (ctx->st->start_thread &&
-       /* the driver loader must implement this */
-       screen->sPriv->dri2.backgroundCallable &&
-       driQueryOptionb(&screen->optionCache, "mesa_glthread"))
-      ctx->st->start_thread(ctx->st);
+         driQueryOptionb(&screen->dev->option_cache, "mesa_glthread")) {
+
+      if (backgroundCallable && backgroundCallable->base.version >= 2 &&
+            backgroundCallable->isThreadSafe) {
+
+         if (backgroundCallable->isThreadSafe(cPriv->loaderPrivate))
+            ctx->st->start_thread(ctx->st);
+         else
+            fprintf(stderr, "dri_create_context: glthread isn't thread safe "
+                  "- missing call XInitThreads\n");
+      } else {
+         fprintf(stderr, "dri_create_context: requested glthread but driver "
+               "is missing backgroundCallable V2 extension\n");
+      }
+   }
 
    *error = __DRI_CTX_ERROR_SUCCESS;
    return GL_TRUE;
@@ -180,7 +230,7 @@ dri_destroy_context(__DRIcontext * cPriv)
    struct dri_context *ctx = dri_context(cPriv);
 
    if (ctx->hud) {
-      hud_destroy(ctx->hud);
+      hud_destroy(ctx->hud, ctx->st->cso_context);
    }
 
    if (ctx->pp)
@@ -196,21 +246,25 @@ dri_destroy_context(__DRIcontext * cPriv)
    free(ctx);
 }
 
+/* This is called inside MakeCurrent to unbind the context. */
 GLboolean
 dri_unbind_context(__DRIcontext * cPriv)
 {
    /* dri_util.c ensures cPriv is not null */
    struct dri_screen *screen = dri_screen(cPriv->driScreenPriv);
    struct dri_context *ctx = dri_context(cPriv);
+   struct st_context_iface *st = ctx->st;
    struct st_api *stapi = screen->st_api;
 
    if (--ctx->bind_count == 0) {
-      if (ctx->st == ctx->stapi->get_current(ctx->stapi)) {
-         /* For conformance, unbind is supposed to flush the context.
-          * However, if we do it here we might end up flushing a partially
-          * destroyed context. Instead, we flush in dri_make_current and
-          * in dri_destroy_context which should cover all the cases.
-          */
+      if (st == stapi->get_current(stapi)) {
+         if (st->thread_finish)
+            st->thread_finish(st);
+
+         /* Record HUD queries for the duration the context was "current". */
+         if (ctx->hud)
+            hud_record_only(ctx->hud, st->pipe);
+
          stapi->make_current(stapi, NULL, NULL, NULL);
       }
    }
@@ -227,14 +281,6 @@ dri_make_current(__DRIcontext * cPriv,
    struct dri_context *ctx = dri_context(cPriv);
    struct dri_drawable *draw = dri_drawable(driDrawPriv);
    struct dri_drawable *read = dri_drawable(driReadPriv);
-   struct st_context_iface *old_st = ctx->stapi->get_current(ctx->stapi);
-
-   if (old_st && old_st->thread_finish)
-      old_st->thread_finish(old_st);
-
-   /* Flush the old context here so we don't have to flush on unbind() */
-   if (old_st && old_st != ctx->st)
-      old_st->flush(old_st, ST_FLUSH_FRONT, NULL);
 
    ++ctx->bind_count;