replace _mesa_logbase2 with util_logbase2
[mesa.git] / src / mesa / state_tracker / st_cb_syncobj.c
index 94bf4861da92c1333b7e7ff0bbd9aa932f0107f2..2cbbc580c8cc26386156687d9e151e71e47dc7a5 100644 (file)
@@ -41,16 +41,16 @@ struct st_sync_object {
    struct gl_sync_object b;
 
    struct pipe_fence_handle *fence;
+   simple_mtx_t mutex; /**< protects "fence" */
 };
 
 
-static struct gl_sync_object * st_new_sync_object(struct gl_context *ctx,
-                                                  GLenum type)
+static struct gl_sync_object *st_new_sync_object(struct gl_context *ctx)
 {
-   if (type == GL_SYNC_FENCE)
-      return (struct gl_sync_object*)CALLOC_STRUCT(st_sync_object);
-   else
-      return NULL;
+   struct st_sync_object *so = CALLOC_STRUCT(st_sync_object);
+
+   simple_mtx_init(&so->mutex, mtx_plain);
+   return &so->b;
 }
 
 static void st_delete_sync_object(struct gl_context *ctx,
@@ -60,6 +60,8 @@ static void st_delete_sync_object(struct gl_context *ctx,
    struct st_sync_object *so = (struct st_sync_object*)obj;
 
    screen->fence_reference(screen, &so->fence, NULL);
+   simple_mtx_destroy(&so->mutex);
+   free(so->b.Label);
    free(so);
 }
 
@@ -72,43 +74,87 @@ static void st_fence_sync(struct gl_context *ctx, struct gl_sync_object *obj,
    assert(condition == GL_SYNC_GPU_COMMANDS_COMPLETE && flags == 0);
    assert(so->fence == NULL);
 
-   pipe->flush(pipe, &so->fence, 0);
+   /* Deferred flush are only allowed when there's a single context. See issue 1430 */
+   pipe->flush(pipe, &so->fence, ctx->Shared->RefCount == 1 ? PIPE_FLUSH_DEFERRED : 0);
 }
 
-static void st_check_sync(struct gl_context *ctx, struct gl_sync_object *obj)
+static void st_client_wait_sync(struct gl_context *ctx,
+                                struct gl_sync_object *obj,
+                                GLbitfield flags, GLuint64 timeout)
 {
-   struct pipe_screen *screen = st_context(ctx)->pipe->screen;
+   struct pipe_context *pipe = st_context(ctx)->pipe;
+   struct pipe_screen *screen = pipe->screen;
    struct st_sync_object *so = (struct st_sync_object*)obj;
+   struct pipe_fence_handle *fence = NULL;
 
-   if (so->fence && screen->fence_signalled(screen, so->fence)) {
+   /* If the fence doesn't exist, assume it's signalled. */
+   simple_mtx_lock(&so->mutex);
+   if (!so->fence) {
+      simple_mtx_unlock(&so->mutex);
+      so->b.StatusFlag = GL_TRUE;
+      return;
+   }
+
+   /* We need a local copy of the fence pointer, so that we can call
+    * fence_finish unlocked.
+    */
+   screen->fence_reference(screen, &fence, so->fence);
+   simple_mtx_unlock(&so->mutex);
+
+   /* Section 4.1.2 of OpenGL 4.5 (Compatibility Profile) says:
+    *    [...] if ClientWaitSync is called and all of the following are true:
+    *    - the SYNC_FLUSH_COMMANDS_BIT bit is set in flags,
+    *    - sync is unsignaled when ClientWaitSync is called,
+    *    - and the calls to ClientWaitSync and FenceSync were issued from
+    *      the same context,
+    *    then the GL will behave as if the equivalent of Flush were inserted
+    *    immediately after the creation of sync.
+    *
+    * Assume GL_SYNC_FLUSH_COMMANDS_BIT is always set, because applications
+    * forget to set it.
+    */
+   if (screen->fence_finish(screen, pipe, fence, timeout)) {
+      simple_mtx_lock(&so->mutex);
       screen->fence_reference(screen, &so->fence, NULL);
+      simple_mtx_unlock(&so->mutex);
       so->b.StatusFlag = GL_TRUE;
    }
+   screen->fence_reference(screen, &fence, NULL);
 }
 
-static void st_client_wait_sync(struct gl_context *ctx,
+static void st_check_sync(struct gl_context *ctx, struct gl_sync_object *obj)
+{
+   st_client_wait_sync(ctx, obj, 0, 0);
+}
+
+static void st_server_wait_sync(struct gl_context *ctx,
                                 struct gl_sync_object *obj,
                                 GLbitfield flags, GLuint64 timeout)
 {
-   struct pipe_screen *screen = st_context(ctx)->pipe->screen;
+   struct pipe_context *pipe = st_context(ctx)->pipe;
+   struct pipe_screen *screen = pipe->screen;
    struct st_sync_object *so = (struct st_sync_object*)obj;
+   struct pipe_fence_handle *fence = NULL;
 
-   /* We don't care about GL_SYNC_FLUSH_COMMANDS_BIT, because flush is
-    * already called when creating a fence. */
+   /* Nothing needs to be done here if the driver does not support async
+    * flushes. */
+   if (!pipe->fence_server_sync)
+      return;
 
-   if (so->fence &&
-       screen->fence_finish(screen, so->fence, timeout)) {
-      screen->fence_reference(screen, &so->fence, NULL);
+   /* If the fence doesn't exist, assume it's signalled. */
+   simple_mtx_lock(&so->mutex);
+   if (!so->fence) {
+      simple_mtx_unlock(&so->mutex);
       so->b.StatusFlag = GL_TRUE;
+      return;
    }
-}
 
-static void st_server_wait_sync(struct gl_context *ctx,
-                                struct gl_sync_object *obj,
-                                GLbitfield flags, GLuint64 timeout)
-{
-   /* NO-OP.
-    * Neither Gallium nor DRM interfaces support blocking on the GPU. */
+   /* We need a local copy of the fence pointer. */
+   screen->fence_reference(screen, &fence, so->fence);
+   simple_mtx_unlock(&so->mutex);
+
+   pipe->fence_server_sync(pipe, fence);
+   screen->fence_reference(screen, &fence, NULL);
 }
 
 void st_init_syncobj_functions(struct dd_function_table *functions)