mesa: return INVALID_VALUE from WaitSync if flags != 0
[mesa.git] / src / mesa / main / syncobj.c
1 /*
2 * Copyright © 2009 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file syncobj.c
26 * Sync object management.
27 *
28 * Unlike textures and other objects that are shared between contexts, sync
29 * objects are not bound to the context. As a result, the reference counting
30 * and delete behavior of sync objects is slightly different. References to
31 * sync objects are added:
32 *
33 * - By \c glFencSynce. This sets the initial reference count to 1.
34 * - At the start of \c glClientWaitSync. The reference is held for the
35 * duration of the wait call.
36 *
37 * References are removed:
38 *
39 * - By \c glDeleteSync.
40 * - At the end of \c glClientWaitSync.
41 *
42 * Additionally, drivers may call \c _mesa_ref_sync_object and
43 * \c _mesa_unref_sync_object as needed to implement \c ServerWaitSync.
44 *
45 * As with shader objects, sync object names become invalid as soon as
46 * \c glDeleteSync is called. For this reason \c glDeleteSync sets the
47 * \c DeletePending flag. All functions validate object handles by testing
48 * this flag.
49 *
50 * \note
51 * Only \c GL_ARB_sync objects are shared between contexts. If support is ever
52 * added for either \c GL_NV_fence or \c GL_APPLE_fence different semantics
53 * will need to be implemented.
54 *
55 * \author Ian Romanick <ian.d.romanick@intel.com>
56 */
57
58 #include "glheader.h"
59 #include "imports.h"
60 #include "context.h"
61 #include "macros.h"
62 #include "mfeatures.h"
63 #include "get.h"
64 #include "dispatch.h"
65 #include "mtypes.h"
66
67 #include "syncobj.h"
68
69 static struct gl_sync_object *
70 _mesa_new_sync_object(struct gl_context *ctx, GLenum type)
71 {
72 struct gl_sync_object *s = MALLOC_STRUCT(gl_sync_object);
73 (void) ctx;
74 (void) type;
75
76 return s;
77 }
78
79
80 static void
81 _mesa_delete_sync_object(struct gl_context *ctx, struct gl_sync_object *syncObj)
82 {
83 (void) ctx;
84 free(syncObj);
85 }
86
87
88 static void
89 _mesa_fence_sync(struct gl_context *ctx, struct gl_sync_object *syncObj,
90 GLenum condition, GLbitfield flags)
91 {
92 (void) ctx;
93 (void) condition;
94 (void) flags;
95
96 syncObj->StatusFlag = 1;
97 }
98
99
100 static void
101 _mesa_check_sync(struct gl_context *ctx, struct gl_sync_object *syncObj)
102 {
103 (void) ctx;
104 (void) syncObj;
105
106 /* No-op for software rendering. Hardware drivers will need to determine
107 * whether the state of the sync object has changed.
108 */
109 }
110
111
112 static void
113 _mesa_wait_sync(struct gl_context *ctx, struct gl_sync_object *syncObj,
114 GLbitfield flags, GLuint64 timeout)
115 {
116 (void) ctx;
117 (void) syncObj;
118 (void) flags;
119 (void) timeout;
120
121 /* No-op for software rendering. Hardware drivers will need to wait until
122 * the state of the sync object changes or the timeout expires.
123 */
124 }
125
126
127 void
128 _mesa_init_sync_object_functions(struct dd_function_table *driver)
129 {
130 driver->NewSyncObject = _mesa_new_sync_object;
131 driver->FenceSync = _mesa_fence_sync;
132 driver->DeleteSyncObject = _mesa_delete_sync_object;
133 driver->CheckSync = _mesa_check_sync;
134
135 /* Use the same no-op wait function for both.
136 */
137 driver->ClientWaitSync = _mesa_wait_sync;
138 driver->ServerWaitSync = _mesa_wait_sync;
139 }
140
141
142 void
143 _mesa_init_sync_dispatch(struct _glapi_table *disp)
144 {
145 SET_IsSync(disp, _mesa_IsSync);
146 SET_DeleteSync(disp, _mesa_DeleteSync);
147 SET_FenceSync(disp, _mesa_FenceSync);
148 SET_ClientWaitSync(disp, _mesa_ClientWaitSync);
149 SET_WaitSync(disp, _mesa_WaitSync);
150 SET_GetInteger64v(disp, _mesa_GetInteger64v);
151 SET_GetSynciv(disp, _mesa_GetSynciv);
152 }
153
154
155 /**
156 * Allocate/init the context state related to sync objects.
157 */
158 void
159 _mesa_init_sync(struct gl_context *ctx)
160 {
161 (void) ctx;
162 }
163
164
165 /**
166 * Free the context state related to sync objects.
167 */
168 void
169 _mesa_free_sync_data(struct gl_context *ctx)
170 {
171 (void) ctx;
172 }
173
174
175 static int
176 _mesa_validate_sync(struct gl_sync_object *syncObj)
177 {
178 return (syncObj != NULL)
179 && (syncObj->Type == GL_SYNC_FENCE)
180 && !syncObj->DeletePending;
181 }
182
183
184 void
185 _mesa_ref_sync_object(struct gl_context *ctx, struct gl_sync_object *syncObj)
186 {
187 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
188 syncObj->RefCount++;
189 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
190 }
191
192
193 void
194 _mesa_unref_sync_object(struct gl_context *ctx, struct gl_sync_object *syncObj)
195 {
196 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
197 syncObj->RefCount--;
198 if (syncObj->RefCount == 0) {
199 remove_from_list(& syncObj->link);
200 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
201
202 ctx->Driver.DeleteSyncObject(ctx, syncObj);
203 } else {
204 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
205 }
206 }
207
208
209 GLboolean GLAPIENTRY
210 _mesa_IsSync(GLsync sync)
211 {
212 GET_CURRENT_CONTEXT(ctx);
213 struct gl_sync_object *const syncObj = (struct gl_sync_object *) sync;
214 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
215
216 return _mesa_validate_sync(syncObj) ? GL_TRUE : GL_FALSE;
217 }
218
219
220 void GLAPIENTRY
221 _mesa_DeleteSync(GLsync sync)
222 {
223 GET_CURRENT_CONTEXT(ctx);
224 struct gl_sync_object *const syncObj = (struct gl_sync_object *) sync;
225 ASSERT_OUTSIDE_BEGIN_END(ctx);
226
227 /* From the GL_ARB_sync spec:
228 *
229 * DeleteSync will silently ignore a <sync> value of zero. An
230 * INVALID_VALUE error is generated if <sync> is neither zero nor the
231 * name of a sync object.
232 */
233 if (sync == 0) {
234 return;
235 }
236
237 if (!_mesa_validate_sync(syncObj)) {
238 _mesa_error(ctx, GL_INVALID_OPERATION, "glDeleteSync");
239 return;
240 }
241
242 /* If there are no client-waits or server-waits pending on this sync, delete
243 * the underlying object.
244 */
245 syncObj->DeletePending = GL_TRUE;
246 _mesa_unref_sync_object(ctx, syncObj);
247 }
248
249
250 GLsync GLAPIENTRY
251 _mesa_FenceSync(GLenum condition, GLbitfield flags)
252 {
253 GET_CURRENT_CONTEXT(ctx);
254 struct gl_sync_object *syncObj;
255 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
256
257 if (condition != GL_SYNC_GPU_COMMANDS_COMPLETE) {
258 _mesa_error(ctx, GL_INVALID_ENUM, "glFenceSync(condition=0x%x)",
259 condition);
260 return 0;
261 }
262
263 if (flags != 0) {
264 _mesa_error(ctx, GL_INVALID_VALUE, "glFenceSync(flags=0x%x)",
265 condition);
266 return 0;
267 }
268
269 syncObj = ctx->Driver.NewSyncObject(ctx, GL_SYNC_FENCE);
270 if (syncObj != NULL) {
271 syncObj->Type = GL_SYNC_FENCE;
272 /* The name is not currently used, and it is never visible to
273 * applications. If sync support is extended to provide support for
274 * NV_fence, this field will be used. We'll also need to add an object
275 * ID hashtable.
276 */
277 syncObj->Name = 1;
278 syncObj->RefCount = 1;
279 syncObj->DeletePending = GL_FALSE;
280 syncObj->SyncCondition = condition;
281 syncObj->Flags = flags;
282 syncObj->StatusFlag = 0;
283
284 ctx->Driver.FenceSync(ctx, syncObj, condition, flags);
285
286 _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
287 insert_at_tail(& ctx->Shared->SyncObjects, & syncObj->link);
288 _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
289
290 return (GLsync) syncObj;
291 }
292
293 return NULL;
294 }
295
296
297 GLenum GLAPIENTRY
298 _mesa_ClientWaitSync(GLsync sync, GLbitfield flags, GLuint64 timeout)
299 {
300 GET_CURRENT_CONTEXT(ctx);
301 struct gl_sync_object *const syncObj = (struct gl_sync_object *) sync;
302 GLenum ret;
303 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_WAIT_FAILED);
304
305 if (!_mesa_validate_sync(syncObj)) {
306 _mesa_error(ctx, GL_INVALID_OPERATION, "glClientWaitSync");
307 return GL_WAIT_FAILED;
308 }
309
310 if ((flags & ~GL_SYNC_FLUSH_COMMANDS_BIT) != 0) {
311 _mesa_error(ctx, GL_INVALID_VALUE, "glClientWaitSync(flags=0x%x)", flags);
312 return GL_WAIT_FAILED;
313 }
314
315 _mesa_ref_sync_object(ctx, syncObj);
316
317 /* From the GL_ARB_sync spec:
318 *
319 * ClientWaitSync returns one of four status values. A return value of
320 * ALREADY_SIGNALED indicates that <sync> was signaled at the time
321 * ClientWaitSync was called. ALREADY_SIGNALED will always be returned
322 * if <sync> was signaled, even if the value of <timeout> is zero.
323 */
324 ctx->Driver.CheckSync(ctx, syncObj);
325 if (syncObj->StatusFlag) {
326 ret = GL_ALREADY_SIGNALED;
327 } else {
328 if (timeout == 0) {
329 ret = GL_TIMEOUT_EXPIRED;
330 } else {
331 ctx->Driver.ClientWaitSync(ctx, syncObj, flags, timeout);
332
333 ret = syncObj->StatusFlag ? GL_CONDITION_SATISFIED : GL_TIMEOUT_EXPIRED;
334 }
335 }
336
337 _mesa_unref_sync_object(ctx, syncObj);
338 return ret;
339 }
340
341
342 void GLAPIENTRY
343 _mesa_WaitSync(GLsync sync, GLbitfield flags, GLuint64 timeout)
344 {
345 GET_CURRENT_CONTEXT(ctx);
346 struct gl_sync_object *const syncObj = (struct gl_sync_object *) sync;
347 ASSERT_OUTSIDE_BEGIN_END(ctx);
348
349 if (!_mesa_validate_sync(syncObj)) {
350 _mesa_error(ctx, GL_INVALID_OPERATION, "glWaitSync");
351 return;
352 }
353
354 if (flags != 0) {
355 _mesa_error(ctx, GL_INVALID_VALUE, "glWaitSync(flags=0x%x)", flags);
356 return;
357 }
358
359 /* From the GL_ARB_sync spec:
360 *
361 * If the value of <timeout> is zero, then WaitSync does nothing.
362 */
363 if (timeout == 0) {
364 return;
365 }
366
367 ctx->Driver.ServerWaitSync(ctx, syncObj, flags, timeout);
368 }
369
370
371 void GLAPIENTRY
372 _mesa_GetSynciv(GLsync sync, GLenum pname, GLsizei bufSize, GLsizei *length,
373 GLint *values)
374 {
375 GET_CURRENT_CONTEXT(ctx);
376 struct gl_sync_object *const syncObj = (struct gl_sync_object *) sync;
377 GLsizei size = 0;
378 GLint v[1];
379 ASSERT_OUTSIDE_BEGIN_END(ctx);
380
381 if (!_mesa_validate_sync(syncObj)) {
382 _mesa_error(ctx, GL_INVALID_OPERATION, "glGetSynciv");
383 return;
384 }
385
386 switch (pname) {
387 case GL_OBJECT_TYPE:
388 v[0] = syncObj->Type;
389 size = 1;
390 break;
391
392 case GL_SYNC_CONDITION:
393 v[0] = syncObj->SyncCondition;
394 size = 1;
395 break;
396
397 case GL_SYNC_STATUS:
398 /* Update the state of the sync by dipping into the driver. Note that
399 * this call won't block. It just updates state in the common object
400 * data from the current driver state.
401 */
402 ctx->Driver.CheckSync(ctx, syncObj);
403
404 v[0] = (syncObj->StatusFlag) ? GL_SIGNALED : GL_UNSIGNALED;
405 size = 1;
406 break;
407
408 case GL_SYNC_FLAGS:
409 v[0] = syncObj->Flags;
410 size = 1;
411 break;
412
413 default:
414 _mesa_error(ctx, GL_INVALID_ENUM, "glGetSynciv(pname=0x%x)\n", pname);
415 return;
416 }
417
418 if (size > 0) {
419 const GLsizei copy_count = MIN2(size, bufSize);
420
421 memcpy(values, v, sizeof(GLint) * copy_count);
422 }
423
424 if (length != NULL) {
425 *length = size;
426 }
427 }