st/mesa: use DXT SRGB formats for COMPRESSED_SRGB
[mesa.git] / src / mesa / state_tracker / st_cb_flush.c
index c7efa40e38a80a8866fd8bc721a78639df49b7a2..5a2343d3aec30991a542c783dff0aebce9200144 100644 (file)
 #include "st_context.h"
 #include "st_cb_bitmap.h"
 #include "st_cb_flush.h"
+#include "st_cb_clear.h"
 #include "st_cb_fbo.h"
-#include "st_public.h"
+#include "st_manager.h"
 #include "pipe/p_context.h"
 #include "pipe/p_defines.h"
-#include "pipe/p_winsys.h"
+#include "pipe/p_screen.h"
+#include "util/u_gen_mipmap.h"
+#include "util/u_blit.h"
 
 
-void st_flush( struct st_context *st, uint pipeFlushFlags,
-               struct pipe_fence_handle **fence )
+/** Check if we have a front color buffer and if it's been drawn to. */
+static INLINE GLboolean
+is_front_buffer_dirty(struct st_context *st)
 {
-   FLUSH_VERTICES(st->ctx, 0);
-
-   st_flush_bitmap_cache(st);
-
-   st->pipe->flush( st->pipe, pipeFlushFlags, fence );
+   struct gl_framebuffer *fb = st->ctx->DrawBuffer;
+   struct st_renderbuffer *strb
+      = st_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
+   return strb && strb->defined;
 }
 
 
-static void st_gl_flush( struct st_context *st, uint pipeFlushFlags,
-                         struct pipe_fence_handle **fence )
+/**
+ * Tell the screen to display the front color buffer on-screen.
+ */
+static void
+display_front_buffer(struct st_context *st)
 {
-   GLframebuffer *fb = st->ctx->DrawBuffer;
+   struct gl_framebuffer *fb = st->ctx->DrawBuffer;
+   struct st_renderbuffer *strb
+      = st_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
 
-   st_flush_bitmap_cache(st);
+   if (strb) {
+      /* Hook for copying "fake" frontbuffer if necessary:
+       */
+      st_manager_flush_frontbuffer(st);
+   }
+}
 
-   FLUSH_VERTICES(st->ctx, 0);
 
-   if (!fb)
-      return;
+void st_flush( struct st_context *st, uint pipeFlushFlags,
+               struct pipe_fence_handle **fence )
+{
+   FLUSH_CURRENT(st->ctx, 0);
 
-   /* XXX: temporary hack.  This flag should only be set if we do any
-    * rendering to the front buffer.
-    *
-    * Further more, the scissor rectangle could be tracked to
-    * construct a dirty region of the front buffer, to avoid
-    * situations where it must be copied repeatedly.
-    *
-    * In the extreme case, some kind of timer could be set up to allow
-    * coalescing of multiple flushes to the frontbuffer, which can be
-    * quite a performance drain if there are a sufficient number of
-    * them.
+   /* Release any vertex buffers that might potentially be accessed in
+    * successive frames:
     */
-   st->flags.frontbuffer_dirty
-      = (fb->_ColorDrawBufferMask[0] & BUFFER_BIT_FRONT_LEFT);
-
-   if (st->flags.frontbuffer_dirty) {
-      struct st_renderbuffer *strb
-         = st_renderbuffer(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer);
-      struct pipe_surface *front_surf = strb->surface;
-
-      /* If we aren't rendering to the frontbuffer, this is a noop.
-       * This should be uncontroversial for glFlush, though people may
-       * feel more strongly about glFinish.
-       *
-       * Additionally, need to make sure that the frontbuffer_dirty
-       * flag really gets set on frontbuffer rendering.
-       */
-      st->pipe->flush( st->pipe, pipeFlushFlags, fence );
+   st_flush_bitmap(st);
+   st_flush_clear(st);
+   util_blit_flush(st->blit);
+   util_gen_mipmap_flush(st->gen_mipmap);
 
-      /* Hook for copying "fake" frontbuffer if necessary:
-       */
-      st->pipe->winsys->flush_frontbuffer( st->pipe->winsys, front_surf,
-                                           st->pipe->priv );
-      st->flags.frontbuffer_dirty = 0;
-   }
+   st->pipe->flush( st->pipe, pipeFlushFlags, fence );
 }
 
 
 /**
- * Called via ctx->Driver.Flush()
+ * Flush, and wait for completion.
  */
-static void st_glFlush(GLcontext *ctx)
+void st_finish( struct st_context *st )
 {
-   st_gl_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
+   struct pipe_fence_handle *fence = NULL;
+
+   st_flush(st, PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, &fence);
+
+   if(fence) {
+      st->pipe->screen->fence_finish(st->pipe->screen, fence, 0);
+      st->pipe->screen->fence_reference(st->pipe->screen, &fence, NULL);
+   }
 }
 
 
-void st_finish( struct st_context *st )
+
+/**
+ * Called via ctx->Driver.Flush()
+ */
+static void st_glFlush(struct gl_context *ctx)
 {
-   struct pipe_fence_handle *fence = NULL;
+   struct st_context *st = st_context(ctx);
 
-   st_flush(st, PIPE_FLUSH_RENDER_CACHE, &fence);
+   /* Don't call st_finish() here.  It is not the state tracker's
+    * responsibilty to inject sleeps in the hope of avoiding buffer
+    * synchronization issues.  Calling finish() here will just hide
+    * problems that need to be fixed elsewhere.
+    */
+   st_flush(st, PIPE_FLUSH_RENDER_CACHE | PIPE_FLUSH_FRAME, NULL);
 
-   st->pipe->winsys->fence_finish(st->pipe->winsys, fence, 0);
-   st->pipe->winsys->fence_reference(st->pipe->winsys, &fence, NULL);
+   if (is_front_buffer_dirty(st)) {
+      display_front_buffer(st);
+   }
 }
 
 
 /**
  * Called via ctx->Driver.Finish()
  */
-static void st_glFinish(GLcontext *ctx)
+static void st_glFinish(struct gl_context *ctx)
 {
-   st_finish( ctx->st );
+   struct st_context *st = st_context(ctx);
+
+   st_finish(st);
+
+   if (is_front_buffer_dirty(st)) {
+      display_front_buffer(st);
+   }
 }
 
 
@@ -138,4 +149,16 @@ void st_init_flush_functions(struct dd_function_table *functions)
 {
    functions->Flush = st_glFlush;
    functions->Finish = st_glFinish;
+
+   /* Windows opengl32.dll calls glFinish prior to every swapbuffers.
+    * This is unnecessary and degrades performance.  Luckily we have some
+    * scope to work around this, as the externally-visible behaviour of
+    * Finish() is identical to Flush() in all cases - no differences in
+    * rendering or ReadPixels are visible if we opt not to wait here.
+    *
+    * Only set this up on windows to avoid suprise elsewhere.
+    */
+#ifdef PIPE_OS_WINDOWS
+   functions->Finish = st_glFlush;
+#endif
 }