replace _mesa_logbase2 with util_logbase2
[mesa.git] / src / mesa / state_tracker / st_cb_syncobj.c
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 "st_context.h"
38 #include "st_cb_syncobj.h"
39
40 struct st_sync_object {
41 struct gl_sync_object b;
42
43 struct pipe_fence_handle *fence;
44 simple_mtx_t mutex; /**< protects "fence" */
45 };
46
47
48 static struct gl_sync_object *st_new_sync_object(struct gl_context *ctx)
49 {
50 struct st_sync_object *so = CALLOC_STRUCT(st_sync_object);
51
52 simple_mtx_init(&so->mutex, mtx_plain);
53 return &so->b;
54 }
55
56 static void st_delete_sync_object(struct gl_context *ctx,
57 struct gl_sync_object *obj)
58 {
59 struct pipe_screen *screen = st_context(ctx)->pipe->screen;
60 struct st_sync_object *so = (struct st_sync_object*)obj;
61
62 screen->fence_reference(screen, &so->fence, NULL);
63 simple_mtx_destroy(&so->mutex);
64 free(so->b.Label);
65 free(so);
66 }
67
68 static void st_fence_sync(struct gl_context *ctx, struct gl_sync_object *obj,
69 GLenum condition, GLbitfield flags)
70 {
71 struct pipe_context *pipe = st_context(ctx)->pipe;
72 struct st_sync_object *so = (struct st_sync_object*)obj;
73
74 assert(condition == GL_SYNC_GPU_COMMANDS_COMPLETE && flags == 0);
75 assert(so->fence == NULL);
76
77 /* Deferred flush are only allowed when there's a single context. See issue 1430 */
78 pipe->flush(pipe, &so->fence, ctx->Shared->RefCount == 1 ? PIPE_FLUSH_DEFERRED : 0);
79 }
80
81 static void st_client_wait_sync(struct gl_context *ctx,
82 struct gl_sync_object *obj,
83 GLbitfield flags, GLuint64 timeout)
84 {
85 struct pipe_context *pipe = st_context(ctx)->pipe;
86 struct pipe_screen *screen = pipe->screen;
87 struct st_sync_object *so = (struct st_sync_object*)obj;
88 struct pipe_fence_handle *fence = NULL;
89
90 /* If the fence doesn't exist, assume it's signalled. */
91 simple_mtx_lock(&so->mutex);
92 if (!so->fence) {
93 simple_mtx_unlock(&so->mutex);
94 so->b.StatusFlag = GL_TRUE;
95 return;
96 }
97
98 /* We need a local copy of the fence pointer, so that we can call
99 * fence_finish unlocked.
100 */
101 screen->fence_reference(screen, &fence, so->fence);
102 simple_mtx_unlock(&so->mutex);
103
104 /* Section 4.1.2 of OpenGL 4.5 (Compatibility Profile) says:
105 * [...] if ClientWaitSync is called and all of the following are true:
106 * - the SYNC_FLUSH_COMMANDS_BIT bit is set in flags,
107 * - sync is unsignaled when ClientWaitSync is called,
108 * - and the calls to ClientWaitSync and FenceSync were issued from
109 * the same context,
110 * then the GL will behave as if the equivalent of Flush were inserted
111 * immediately after the creation of sync.
112 *
113 * Assume GL_SYNC_FLUSH_COMMANDS_BIT is always set, because applications
114 * forget to set it.
115 */
116 if (screen->fence_finish(screen, pipe, fence, timeout)) {
117 simple_mtx_lock(&so->mutex);
118 screen->fence_reference(screen, &so->fence, NULL);
119 simple_mtx_unlock(&so->mutex);
120 so->b.StatusFlag = GL_TRUE;
121 }
122 screen->fence_reference(screen, &fence, NULL);
123 }
124
125 static void st_check_sync(struct gl_context *ctx, struct gl_sync_object *obj)
126 {
127 st_client_wait_sync(ctx, obj, 0, 0);
128 }
129
130 static void st_server_wait_sync(struct gl_context *ctx,
131 struct gl_sync_object *obj,
132 GLbitfield flags, GLuint64 timeout)
133 {
134 struct pipe_context *pipe = st_context(ctx)->pipe;
135 struct pipe_screen *screen = pipe->screen;
136 struct st_sync_object *so = (struct st_sync_object*)obj;
137 struct pipe_fence_handle *fence = NULL;
138
139 /* Nothing needs to be done here if the driver does not support async
140 * flushes. */
141 if (!pipe->fence_server_sync)
142 return;
143
144 /* If the fence doesn't exist, assume it's signalled. */
145 simple_mtx_lock(&so->mutex);
146 if (!so->fence) {
147 simple_mtx_unlock(&so->mutex);
148 so->b.StatusFlag = GL_TRUE;
149 return;
150 }
151
152 /* We need a local copy of the fence pointer. */
153 screen->fence_reference(screen, &fence, so->fence);
154 simple_mtx_unlock(&so->mutex);
155
156 pipe->fence_server_sync(pipe, fence);
157 screen->fence_reference(screen, &fence, NULL);
158 }
159
160 void st_init_syncobj_functions(struct dd_function_table *functions)
161 {
162 functions->NewSyncObject = st_new_sync_object;
163 functions->FenceSync = st_fence_sync;
164 functions->DeleteSyncObject = st_delete_sync_object;
165 functions->CheckSync = st_check_sync;
166 functions->ClientWaitSync = st_client_wait_sync;
167 functions->ServerWaitSync = st_server_wait_sync;
168 }