1 /**************************************************************************
2  *
3  * Copyright 2011 Marek Olšák <maraeo@gmail.com>
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28  /*
29   * Authors:
30   *   Marek Olšák <maraeo@gmail.com>
31   */
32 
33 #include "main/glheader.h"
34 #include "main/macros.h"
35 #include "pipe/p_context.h"
36 #include "pipe/p_screen.h"
37 #include "util/u_memory.h"
38 #include "st_context.h"
39 #include "st_cb_syncobj.h"
40 
41 struct st_sync_object {
42    struct gl_sync_object b;
43 
44    struct pipe_fence_handle *fence;
45    simple_mtx_t mutex; /**< protects "fence" */
46 };
47 
48 
st_new_sync_object(struct gl_context * ctx)49 static struct gl_sync_object *st_new_sync_object(struct gl_context *ctx)
50 {
51    struct st_sync_object *so = CALLOC_STRUCT(st_sync_object);
52 
53    simple_mtx_init(&so->mutex, mtx_plain);
54    return &so->b;
55 }
56 
st_delete_sync_object(struct gl_context * ctx,struct gl_sync_object * obj)57 static void st_delete_sync_object(struct gl_context *ctx,
58                                   struct gl_sync_object *obj)
59 {
60    struct pipe_screen *screen = st_context(ctx)->pipe->screen;
61    struct st_sync_object *so = (struct st_sync_object*)obj;
62 
63    screen->fence_reference(screen, &so->fence, NULL);
64    simple_mtx_destroy(&so->mutex);
65    free(so->b.Label);
66    free(so);
67 }
68 
st_fence_sync(struct gl_context * ctx,struct gl_sync_object * obj,GLenum condition,GLbitfield flags)69 static void st_fence_sync(struct gl_context *ctx, struct gl_sync_object *obj,
70                           GLenum condition, GLbitfield flags)
71 {
72    struct pipe_context *pipe = st_context(ctx)->pipe;
73    struct st_sync_object *so = (struct st_sync_object*)obj;
74 
75    assert(condition == GL_SYNC_GPU_COMMANDS_COMPLETE && flags == 0);
76    assert(so->fence == NULL);
77 
78    /* Deferred flush are only allowed when there's a single context. See issue 1430 */
79    pipe->flush(pipe, &so->fence, ctx->Shared->RefCount == 1 ? PIPE_FLUSH_DEFERRED : 0);
80 }
81 
st_client_wait_sync(struct gl_context * ctx,struct gl_sync_object * obj,GLbitfield flags,GLuint64 timeout)82 static void st_client_wait_sync(struct gl_context *ctx,
83                                 struct gl_sync_object *obj,
84                                 GLbitfield flags, GLuint64 timeout)
85 {
86    struct pipe_context *pipe = st_context(ctx)->pipe;
87    struct pipe_screen *screen = pipe->screen;
88    struct st_sync_object *so = (struct st_sync_object*)obj;
89    struct pipe_fence_handle *fence = NULL;
90 
91    /* If the fence doesn't exist, assume it's signalled. */
92    simple_mtx_lock(&so->mutex);
93    if (!so->fence) {
94       simple_mtx_unlock(&so->mutex);
95       so->b.StatusFlag = GL_TRUE;
96       return;
97    }
98 
99    /* We need a local copy of the fence pointer, so that we can call
100     * fence_finish unlocked.
101     */
102    screen->fence_reference(screen, &fence, so->fence);
103    simple_mtx_unlock(&so->mutex);
104 
105    /* Section 4.1.2 of OpenGL 4.5 (Compatibility Profile) says:
106     *    [...] if ClientWaitSync is called and all of the following are true:
107     *    - the SYNC_FLUSH_COMMANDS_BIT bit is set in flags,
108     *    - sync is unsignaled when ClientWaitSync is called,
109     *    - and the calls to ClientWaitSync and FenceSync were issued from
110     *      the same context,
111     *    then the GL will behave as if the equivalent of Flush were inserted
112     *    immediately after the creation of sync.
113     *
114     * Assume GL_SYNC_FLUSH_COMMANDS_BIT is always set, because applications
115     * forget to set it.
116     */
117    if (screen->fence_finish(screen, pipe, fence, timeout)) {
118       simple_mtx_lock(&so->mutex);
119       screen->fence_reference(screen, &so->fence, NULL);
120       simple_mtx_unlock(&so->mutex);
121       so->b.StatusFlag = GL_TRUE;
122    }
123    screen->fence_reference(screen, &fence, NULL);
124 }
125 
st_check_sync(struct gl_context * ctx,struct gl_sync_object * obj)126 static void st_check_sync(struct gl_context *ctx, struct gl_sync_object *obj)
127 {
128    st_client_wait_sync(ctx, obj, 0, 0);
129 }
130 
st_server_wait_sync(struct gl_context * ctx,struct gl_sync_object * obj,GLbitfield flags,GLuint64 timeout)131 static void st_server_wait_sync(struct gl_context *ctx,
132                                 struct gl_sync_object *obj,
133                                 GLbitfield flags, GLuint64 timeout)
134 {
135    struct pipe_context *pipe = st_context(ctx)->pipe;
136    struct pipe_screen *screen = pipe->screen;
137    struct st_sync_object *so = (struct st_sync_object*)obj;
138    struct pipe_fence_handle *fence = NULL;
139 
140    /* Nothing needs to be done here if the driver does not support async
141     * flushes. */
142    if (!pipe->fence_server_sync)
143       return;
144 
145    /* If the fence doesn't exist, assume it's signalled. */
146    simple_mtx_lock(&so->mutex);
147    if (!so->fence) {
148       simple_mtx_unlock(&so->mutex);
149       so->b.StatusFlag = GL_TRUE;
150       return;
151    }
152 
153    /* We need a local copy of the fence pointer. */
154    screen->fence_reference(screen, &fence, so->fence);
155    simple_mtx_unlock(&so->mutex);
156 
157    pipe->fence_server_sync(pipe, fence);
158    screen->fence_reference(screen, &fence, NULL);
159 }
160 
st_init_syncobj_functions(struct dd_function_table * functions)161 void st_init_syncobj_functions(struct dd_function_table *functions)
162 {
163    functions->NewSyncObject = st_new_sync_object;
164    functions->FenceSync = st_fence_sync;
165    functions->DeleteSyncObject = st_delete_sync_object;
166    functions->CheckSync = st_check_sync;
167    functions->ClientWaitSync = st_client_wait_sync;
168    functions->ServerWaitSync = st_server_wait_sync;
169 }
170