replace _mesa_logbase2 with util_logbase2
[mesa.git] / src / mesa / state_tracker / st_manager.c
index caf8799a7b16a6a01fd8561c84ae305e25839dd6..79aa778a14ca428c6ed906de9780df9b0f2003df 100644 (file)
@@ -55,7 +55,7 @@
 
 #include "pipe/p_context.h"
 #include "pipe/p_screen.h"
-#include "util/u_format.h"
+#include "util/format/u_format.h"
 #include "util/u_helpers.h"
 #include "util/u_pointer.h"
 #include "util/u_inlines.h"
@@ -67,7 +67,7 @@ struct hash_table;
 struct st_manager_private
 {
    struct hash_table *stfbi_ht; /* framebuffer iface objects hash table */
-   mtx_t st_mutex;
+   simple_mtx_t st_mutex;
 };
 
 
@@ -208,7 +208,7 @@ st_framebuffer_validate(struct st_framebuffer *stfb,
    struct pipe_resource *textures[ST_ATTACHMENT_COUNT];
    uint width, height;
    unsigned i;
-   boolean changed = FALSE;
+   bool changed = false;
    int32_t new_stamp;
 
    new_stamp = p_atomic_read(&stfb->iface->stamp);
@@ -257,7 +257,7 @@ st_framebuffer_validate(struct st_framebuffer *stfb,
          st_set_ws_renderbuffer_surface(strb, ps);
          pipe_surface_reference(&ps, NULL);
 
-         changed = TRUE;
+         changed = true;
 
          width = strb->Base.Width;
          height = strb->Base.Height;
@@ -303,13 +303,13 @@ st_framebuffer_update_attachments(struct st_framebuffer *stfb)
  * Add a renderbuffer to the framebuffer.  The framebuffer is one that
  * corresponds to a window and is not a user-created FBO.
  */
-static boolean
+static bool
 st_framebuffer_add_renderbuffer(struct st_framebuffer *stfb,
                                 gl_buffer_index idx, bool prefer_srgb)
 {
    struct gl_renderbuffer *rb;
    enum pipe_format format;
-   boolean sw;
+   bool sw;
 
    assert(_mesa_is_winsys_fbo(&stfb->Base));
 
@@ -320,30 +320,30 @@ st_framebuffer_add_renderbuffer(struct st_framebuffer *stfb,
    switch (idx) {
    case BUFFER_DEPTH:
       format = stfb->iface->visual->depth_stencil_format;
-      sw = FALSE;
+      sw = false;
       break;
    case BUFFER_ACCUM:
       format = stfb->iface->visual->accum_format;
-      sw = TRUE;
+      sw = true;
       break;
    default:
       format = stfb->iface->visual->color_format;
       if (prefer_srgb)
          format = util_format_srgb(format);
-      sw = FALSE;
+      sw = false;
       break;
    }
 
    if (format == PIPE_FORMAT_NONE)
-      return FALSE;
+      return false;
 
    rb = st_new_renderbuffer_fb(format, stfb->iface->visual->samples, sw);
    if (!rb)
-      return FALSE;
+      return false;
 
    if (idx != BUFFER_DEPTH) {
       _mesa_attach_and_own_rb(&stfb->Base, idx, rb);
-      return TRUE;
+      return true;
    }
 
    bool rb_ownership_taken = false;
@@ -359,7 +359,7 @@ st_framebuffer_add_renderbuffer(struct st_framebuffer *stfb,
          _mesa_attach_and_own_rb(&stfb->Base, BUFFER_STENCIL, rb);
    }
 
-   return TRUE;
+   return true;
 }
 
 
@@ -380,8 +380,6 @@ st_visual_to_context_mode(const struct st_visual *visual,
       mode->stereoMode = GL_TRUE;
 
    if (visual->color_format != PIPE_FORMAT_NONE) {
-      mode->rgbMode = GL_TRUE;
-
       mode->redBits =
          util_format_get_component_bits(visual->color_format,
                UTIL_FORMAT_COLORSPACE_RGB, 0);
@@ -407,14 +405,9 @@ st_visual_to_context_mode(const struct st_visual *visual,
       mode->stencilBits =
          util_format_get_component_bits(visual->depth_stencil_format,
                UTIL_FORMAT_COLORSPACE_ZS, 1);
-
-      mode->haveDepthBuffer = mode->depthBits > 0;
-      mode->haveStencilBuffer = mode->stencilBits > 0;
    }
 
    if (visual->accum_format != PIPE_FORMAT_NONE) {
-      mode->haveAccumBuffer = GL_TRUE;
-
       mode->accumRedBits =
          util_format_get_component_bits(visual->accum_format,
                UTIL_FORMAT_COLORSPACE_RGB, 0);
@@ -476,7 +469,7 @@ st_framebuffer_create(struct st_context *st,
     * brings GLES on par with desktop GLs EXT_framebuffer_sRGB, in mesa this
     * is also expressed by using the same extension flag
     */
-   if (st->ctx->Extensions.EXT_framebuffer_sRGB) {
+   if (_mesa_has_EXT_framebuffer_sRGB(st->ctx)) {
       struct pipe_screen *screen = st->pipe->screen;
       const enum pipe_format srgb_format =
          util_format_srgb(stfbi->visual->color_format);
@@ -527,7 +520,7 @@ void
 st_framebuffer_reference(struct st_framebuffer **ptr,
                          struct st_framebuffer *stfb)
 {
-   struct gl_framebuffer *fb = &stfb->Base;
+   struct gl_framebuffer *fb = stfb ? &stfb->Base : NULL;
    _mesa_reference_framebuffer((struct gl_framebuffer **) ptr, fb);
 }
 
@@ -546,7 +539,7 @@ st_framebuffer_iface_equal(const void *a, const void *b)
 }
 
 
-static boolean
+static bool
 st_framebuffer_iface_lookup(struct st_manager *smapi,
                             const struct st_framebuffer_iface *stfbi)
 {
@@ -557,15 +550,15 @@ st_framebuffer_iface_lookup(struct st_manager *smapi,
    assert(smPriv);
    assert(smPriv->stfbi_ht);
 
-   mtx_lock(&smPriv->st_mutex);
+   simple_mtx_lock(&smPriv->st_mutex);
    entry = _mesa_hash_table_search(smPriv->stfbi_ht, stfbi);
-   mtx_unlock(&smPriv->st_mutex);
+   simple_mtx_unlock(&smPriv->st_mutex);
 
    return entry != NULL;
 }
 
 
-static boolean
+static bool
 st_framebuffer_iface_insert(struct st_manager *smapi,
                             struct st_framebuffer_iface *stfbi)
 {
@@ -576,9 +569,9 @@ st_framebuffer_iface_insert(struct st_manager *smapi,
    assert(smPriv);
    assert(smPriv->stfbi_ht);
 
-   mtx_lock(&smPriv->st_mutex);
+   simple_mtx_lock(&smPriv->st_mutex);
    entry = _mesa_hash_table_insert(smPriv->stfbi_ht, stfbi, stfbi);
-   mtx_unlock(&smPriv->st_mutex);
+   simple_mtx_unlock(&smPriv->st_mutex);
 
    return entry != NULL;
 }
@@ -595,7 +588,7 @@ st_framebuffer_iface_remove(struct st_manager *smapi,
    if (!smPriv || !smPriv->stfbi_ht)
       return;
 
-   mtx_lock(&smPriv->st_mutex);
+   simple_mtx_lock(&smPriv->st_mutex);
    entry = _mesa_hash_table_search(smPriv->stfbi_ht, stfbi);
    if (!entry)
       goto unlock;
@@ -603,7 +596,7 @@ st_framebuffer_iface_remove(struct st_manager *smapi,
    _mesa_hash_table_remove(smPriv->stfbi_ht, entry);
 
 unlock:
-   mtx_unlock(&smPriv->st_mutex);
+   simple_mtx_unlock(&smPriv->st_mutex);
 }
 
 
@@ -647,7 +640,7 @@ st_framebuffers_purge(struct st_context *st)
        * deleted.
        */
       if (!st_framebuffer_iface_lookup(smapi, stfbi)) {
-         LIST_DEL(&stfb->head);
+         list_del(&stfb->head);
          st_framebuffer_reference(&stfb, NULL);
       }
    }
@@ -656,7 +649,9 @@ st_framebuffers_purge(struct st_context *st)
 
 static void
 st_context_flush(struct st_context_iface *stctxi, unsigned flags,
-                 struct pipe_fence_handle **fence)
+                 struct pipe_fence_handle **fence,
+                 void (*before_flush_cb) (void*),
+                 void* args)
 {
    struct st_context *st = (struct st_context *) stctxi;
    unsigned pipe_flags = 0;
@@ -666,8 +661,15 @@ st_context_flush(struct st_context_iface *stctxi, unsigned flags,
    if (flags & ST_FLUSH_FENCE_FD)
       pipe_flags |= PIPE_FLUSH_FENCE_FD;
 
+   /* If both the bitmap cache is dirty and there are unflushed vertices,
+    * it means that glBitmap was called first and then glBegin.
+    */
+   st_flush_bitmap_cache(st);
    FLUSH_VERTICES(st->ctx, 0);
-   FLUSH_CURRENT(st->ctx, 0);
+
+   /* Notify the caller that we're ready to flush */
+   if (before_flush_cb)
+      before_flush_cb(args);
    st_flush(st, fence, pipe_flags);
 
    if ((flags & ST_FLUSH_WAIT) && fence && *fence) {
@@ -690,11 +692,11 @@ st_context_flush(struct st_context_iface *stctxi, unsigned flags,
       st->gfx_shaders_may_be_dirty = true;
 }
 
-static boolean
+static bool
 st_context_teximage(struct st_context_iface *stctxi,
                     enum st_texture_type tex_type,
                     int level, enum pipe_format pipe_format,
-                    struct pipe_resource *tex, boolean mipmap)
+                    struct pipe_resource *tex, bool mipmap)
 {
    struct st_context *st = (struct st_context *) stctxi;
    struct gl_context *ctx = st->ctx;
@@ -778,7 +780,7 @@ st_context_teximage(struct st_context_iface *stctxi,
    _mesa_dirty_texobj(ctx, texObj);
    _mesa_unlock_texture(ctx, texObj);
 
-   return TRUE;
+   return true;
 }
 
 
@@ -793,7 +795,7 @@ st_context_copy(struct st_context_iface *stctxi,
 }
 
 
-static boolean
+static bool
 st_context_share(struct st_context_iface *stctxi,
                  struct st_context_iface *stsrci)
 {
@@ -825,8 +827,8 @@ st_start_thread(struct st_context_iface *stctxi)
     * If glthread is disabled, st_draw.c re-pins driver threads regularly
     * based on the location of the app thread.
     */
-   struct glthread_state *glthread = st->ctx->GLThread;
-   if (glthread && st->pipe->set_context_param) {
+   struct glthread_state *glthread = &st->ctx->GLThread;
+   if (glthread->enabled && st->pipe->set_context_param) {
       util_pin_driver_threads_to_random_L3(st->pipe, &glthread->queue.threads[0]);
    }
 }
@@ -848,7 +850,7 @@ st_manager_destroy(struct st_manager *smapi)
 
    if (smPriv && smPriv->stfbi_ht) {
       _mesa_hash_table_destroy(smPriv->stfbi_ht, NULL);
-      mtx_destroy(&smPriv->st_mutex);
+      simple_mtx_destroy(&smPriv->st_mutex);
       free(smPriv);
       smapi->st_manager_private = NULL;
    }
@@ -898,7 +900,7 @@ st_api_create_context(struct st_api *stapi, struct st_manager *smapi,
       struct st_manager_private *smPriv;
 
       smPriv = CALLOC_STRUCT(st_manager_private);
-      mtx_init(&smPriv->st_mutex, mtx_plain);
+      simple_mtx_init(&smPriv->st_mutex, mtx_plain);
       smPriv->stfbi_ht = _mesa_hash_table_create(NULL,
                                                  st_framebuffer_iface_hash,
                                                  st_framebuffer_iface_equal);
@@ -1043,7 +1045,7 @@ st_framebuffer_reuse_or_create(struct st_context *st,
          }
 
          /* add to the context's winsys buffers list */
-         LIST_ADD(&cur->head, &st->winsys_buffers);
+         list_add(&cur->head, &st->winsys_buffers);
 
          st_framebuffer_reference(&stfb, cur);
       }
@@ -1053,14 +1055,14 @@ st_framebuffer_reuse_or_create(struct st_context *st,
 }
 
 
-static boolean
+static bool
 st_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
                     struct st_framebuffer_iface *stdrawi,
                     struct st_framebuffer_iface *streadi)
 {
    struct st_context *st = (struct st_context *) stctxi;
    struct st_framebuffer *stdraw, *stread;
-   boolean ret;
+   bool ret;
 
    if (st) {
       /* reuse or create the draw fb */
@@ -1105,10 +1107,17 @@ st_api_make_current(struct st_api *stapi, struct st_context_iface *stctxi,
    else {
       GET_CURRENT_CONTEXT(ctx);
 
-      ret = _mesa_make_current(NULL, NULL, NULL);
-
-      if (ctx)
+      if (ctx) {
+         /* Before releasing the context, release its associated
+          * winsys buffers first. Then purge the context's winsys buffers list
+          * to free the resources of any winsys buffers that no longer have
+          * an existing drawable.
+          */
+         ret = _mesa_make_current(ctx, NULL, NULL);
          st_framebuffers_purge(ctx->st);
+      }
+
+      ret = _mesa_make_current(NULL, NULL, NULL);
    }
 
    return ret;
@@ -1130,9 +1139,19 @@ st_manager_flush_frontbuffer(struct st_context *st)
    struct st_framebuffer *stfb = st_ws_framebuffer(st->ctx->DrawBuffer);
    struct st_renderbuffer *strb = NULL;
 
-   if (stfb)
-      strb = st_renderbuffer(stfb->Base.Attachment[BUFFER_FRONT_LEFT].
-                             Renderbuffer);
+   if (!stfb)
+      return;
+
+   /* If the context uses a doublebuffered visual, but the buffer is
+    * single-buffered, guess that it's a pbuffer, which doesn't need
+    * flushing.
+    */
+   if (st->ctx->Visual.doubleBufferMode &&
+       !stfb->Base.Visual.doubleBufferMode)
+      return;
+
+   strb = st_renderbuffer(stfb->Base.Attachment[BUFFER_FRONT_LEFT].
+                          Renderbuffer);
 
    /* Do we have a front color buffer and has it been drawn to since last
     * frontbuffer flush?
@@ -1191,7 +1210,7 @@ st_manager_flush_swapbuffers(void)
  * Add a color renderbuffer on demand.  The FBO must correspond to a window,
  * not a user-created FBO.
  */
-boolean
+bool
 st_manager_add_color_renderbuffer(struct st_context *st,
                                   struct gl_framebuffer *fb,
                                   gl_buffer_index idx)
@@ -1200,12 +1219,12 @@ st_manager_add_color_renderbuffer(struct st_context *st,
 
    /* FBO */
    if (!stfb)
-      return FALSE;
+      return false;
 
    assert(_mesa_is_winsys_fbo(fb));
 
    if (stfb->Base.Attachment[idx].Renderbuffer)
-      return TRUE;
+      return true;
 
    switch (idx) {
    case BUFFER_FRONT_LEFT:
@@ -1214,12 +1233,12 @@ st_manager_add_color_renderbuffer(struct st_context *st,
    case BUFFER_BACK_RIGHT:
       break;
    default:
-      return FALSE;
+      return false;
    }
 
    if (!st_framebuffer_add_renderbuffer(stfb, idx,
                                         stfb->Base.Visual.sRGBCapable))
-      return FALSE;
+      return false;
 
    st_framebuffer_update_attachments(stfb);
 
@@ -1233,7 +1252,7 @@ st_manager_add_color_renderbuffer(struct st_context *st,
 
    st_invalidate_buffers(st);
 
-   return TRUE;
+   return true;
 }
 
 
@@ -1252,10 +1271,11 @@ get_version(struct pipe_screen *screen,
    _mesa_init_constants(&consts, api);
    _mesa_init_extensions(&extensions);
 
-   st_init_limits(screen, &consts, &extensions, api);
+   st_init_limits(screen, &consts, &extensions);
    st_init_extensions(screen, &consts, &extensions, options, api);
-
-   return _mesa_get_version(&extensions, &consts, api);
+   version = _mesa_get_version(&extensions, &consts, api);
+   free(consts.SpirVExtensions);
+   return version;
 }