mesa: drop fence type parameter from NewSyncObject()
[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 <inttypes.h>
59 #include "glheader.h"
60 #include "imports.h"
61 #include "context.h"
62 #include "macros.h"
63 #include "get.h"
64 #include "dispatch.h"
65 #include "mtypes.h"
66 #include "util/hash_table.h"
67 #include "util/set.h"
68
69 #include "syncobj.h"
70
71 static struct gl_sync_object *
72 _mesa_new_sync_object(struct gl_context *ctx)
73 {
74 struct gl_sync_object *s = CALLOC_STRUCT(gl_sync_object);
75 (void) ctx;
76
77 return s;
78 }
79
80
81 static void
82 _mesa_delete_sync_object(struct gl_context *ctx, struct gl_sync_object *syncObj)
83 {
84 (void) ctx;
85 free(syncObj->Label);
86 free(syncObj);
87 }
88
89
90 static void
91 _mesa_fence_sync(struct gl_context *ctx, struct gl_sync_object *syncObj,
92 GLenum condition, GLbitfield flags)
93 {
94 (void) ctx;
95 (void) condition;
96 (void) flags;
97
98 syncObj->StatusFlag = 1;
99 }
100
101
102 static void
103 _mesa_check_sync(struct gl_context *ctx, struct gl_sync_object *syncObj)
104 {
105 (void) ctx;
106 (void) syncObj;
107
108 /* No-op for software rendering. Hardware drivers will need to determine
109 * whether the state of the sync object has changed.
110 */
111 }
112
113
114 static void
115 _mesa_wait_sync(struct gl_context *ctx, struct gl_sync_object *syncObj,
116 GLbitfield flags, GLuint64 timeout)
117 {
118 (void) ctx;
119 (void) syncObj;
120 (void) flags;
121 (void) timeout;
122
123 /* No-op for software rendering. Hardware drivers will need to wait until
124 * the state of the sync object changes or the timeout expires.
125 */
126 }
127
128
129 void
130 _mesa_init_sync_object_functions(struct dd_function_table *driver)
131 {
132 driver->NewSyncObject = _mesa_new_sync_object;
133 driver->FenceSync = _mesa_fence_sync;
134 driver->DeleteSyncObject = _mesa_delete_sync_object;
135 driver->CheckSync = _mesa_check_sync;
136
137 /* Use the same no-op wait function for both.
138 */
139 driver->ClientWaitSync = _mesa_wait_sync;
140 driver->ServerWaitSync = _mesa_wait_sync;
141 }
142
143 /**
144 * Allocate/init the context state related to sync objects.
145 */
146 void
147 _mesa_init_sync(struct gl_context *ctx)
148 {
149 (void) ctx;
150 }
151
152
153 /**
154 * Free the context state related to sync objects.
155 */
156 void
157 _mesa_free_sync_data(struct gl_context *ctx)
158 {
159 (void) ctx;
160 }
161
162
163 /**
164 * Check if the given sync object is:
165 * - non-null
166 * - not in sync objects hash table
167 * - type is GL_SYNC_FENCE
168 * - not marked as deleted
169 *
170 * Returns the internal gl_sync_object pointer if the sync object is valid
171 * or NULL if it isn't.
172 *
173 * If "incRefCount" is true, the reference count is incremented, which is
174 * normally what you want; otherwise, a glDeleteSync from another thread
175 * could delete the sync object while you are still working on it.
176 */
177 struct gl_sync_object *
178 _mesa_get_and_ref_sync(struct gl_context *ctx, GLsync sync, bool incRefCount)
179 {
180 struct gl_sync_object *syncObj = (struct gl_sync_object *) sync;
181 mtx_lock(&ctx->Shared->Mutex);
182 if (syncObj != NULL
183 && _mesa_set_search(ctx->Shared->SyncObjects, syncObj) != NULL
184 && (syncObj->Type == GL_SYNC_FENCE)
185 && !syncObj->DeletePending) {
186 if (incRefCount) {
187 syncObj->RefCount++;
188 }
189 } else {
190 syncObj = NULL;
191 }
192 mtx_unlock(&ctx->Shared->Mutex);
193 return syncObj;
194 }
195
196
197 void
198 _mesa_unref_sync_object(struct gl_context *ctx, struct gl_sync_object *syncObj,
199 int amount)
200 {
201 struct set_entry *entry;
202
203 mtx_lock(&ctx->Shared->Mutex);
204 syncObj->RefCount -= amount;
205 if (syncObj->RefCount == 0) {
206 entry = _mesa_set_search(ctx->Shared->SyncObjects, syncObj);
207 assert (entry != NULL);
208 _mesa_set_remove(ctx->Shared->SyncObjects, entry);
209 mtx_unlock(&ctx->Shared->Mutex);
210
211 ctx->Driver.DeleteSyncObject(ctx, syncObj);
212 } else {
213 mtx_unlock(&ctx->Shared->Mutex);
214 }
215 }
216
217
218 GLboolean GLAPIENTRY
219 _mesa_IsSync(GLsync sync)
220 {
221 GET_CURRENT_CONTEXT(ctx);
222 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
223
224 return _mesa_get_and_ref_sync(ctx, sync, false) ? GL_TRUE : GL_FALSE;
225 }
226
227
228 void GLAPIENTRY
229 _mesa_DeleteSync(GLsync sync)
230 {
231 GET_CURRENT_CONTEXT(ctx);
232 struct gl_sync_object *syncObj;
233
234 /* From the GL_ARB_sync spec:
235 *
236 * DeleteSync will silently ignore a <sync> value of zero. An
237 * INVALID_VALUE error is generated if <sync> is neither zero nor the
238 * name of a sync object.
239 */
240 if (sync == 0) {
241 return;
242 }
243
244 syncObj = _mesa_get_and_ref_sync(ctx, sync, true);
245 if (!syncObj) {
246 _mesa_error(ctx, GL_INVALID_VALUE, "glDeleteSync (not a valid sync object)");
247 return;
248 }
249
250 /* If there are no client-waits or server-waits pending on this sync, delete
251 * the underlying object. Note that we double-unref the object, as
252 * _mesa_get_and_ref_sync above took an extra refcount to make sure the pointer
253 * is valid for us to manipulate.
254 */
255 syncObj->DeletePending = GL_TRUE;
256 _mesa_unref_sync_object(ctx, syncObj, 2);
257 }
258
259
260 static GLsync
261 fence_sync(struct gl_context *ctx, GLenum condition, GLbitfield flags)
262 {
263 struct gl_sync_object *syncObj;
264
265 syncObj = ctx->Driver.NewSyncObject(ctx);
266 if (syncObj != NULL) {
267 syncObj->Type = GL_SYNC_FENCE;
268 /* The name is not currently used, and it is never visible to
269 * applications. If sync support is extended to provide support for
270 * NV_fence, this field will be used. We'll also need to add an object
271 * ID hashtable.
272 */
273 syncObj->Name = 1;
274 syncObj->RefCount = 1;
275 syncObj->DeletePending = GL_FALSE;
276 syncObj->SyncCondition = condition;
277 syncObj->Flags = flags;
278 syncObj->StatusFlag = 0;
279
280 ctx->Driver.FenceSync(ctx, syncObj, condition, flags);
281
282 mtx_lock(&ctx->Shared->Mutex);
283 _mesa_set_add(ctx->Shared->SyncObjects, syncObj);
284 mtx_unlock(&ctx->Shared->Mutex);
285
286 return (GLsync)syncObj;
287 }
288
289 return NULL;
290 }
291
292
293 GLsync GLAPIENTRY
294 _mesa_FenceSync_no_error(GLenum condition, GLbitfield flags)
295 {
296 GET_CURRENT_CONTEXT(ctx);
297 return fence_sync(ctx, condition, flags);
298 }
299
300
301 GLsync GLAPIENTRY
302 _mesa_FenceSync(GLenum condition, GLbitfield flags)
303 {
304 GET_CURRENT_CONTEXT(ctx);
305 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
306
307 if (condition != GL_SYNC_GPU_COMMANDS_COMPLETE) {
308 _mesa_error(ctx, GL_INVALID_ENUM, "glFenceSync(condition=0x%x)",
309 condition);
310 return 0;
311 }
312
313 if (flags != 0) {
314 _mesa_error(ctx, GL_INVALID_VALUE, "glFenceSync(flags=0x%x)",
315 condition);
316 return 0;
317 }
318
319 return fence_sync(ctx, condition, flags);
320 }
321
322
323 static GLenum
324 client_wait_sync(struct gl_context *ctx, struct gl_sync_object *syncObj,
325 GLbitfield flags, GLuint64 timeout)
326 {
327 GLenum ret;
328
329 /* From the GL_ARB_sync spec:
330 *
331 * ClientWaitSync returns one of four status values. A return value of
332 * ALREADY_SIGNALED indicates that <sync> was signaled at the time
333 * ClientWaitSync was called. ALREADY_SIGNALED will always be returned
334 * if <sync> was signaled, even if the value of <timeout> is zero.
335 */
336 ctx->Driver.CheckSync(ctx, syncObj);
337 if (syncObj->StatusFlag) {
338 ret = GL_ALREADY_SIGNALED;
339 } else {
340 if (timeout == 0) {
341 ret = GL_TIMEOUT_EXPIRED;
342 } else {
343 ctx->Driver.ClientWaitSync(ctx, syncObj, flags, timeout);
344
345 ret = syncObj->StatusFlag ? GL_CONDITION_SATISFIED : GL_TIMEOUT_EXPIRED;
346 }
347 }
348
349 _mesa_unref_sync_object(ctx, syncObj, 1);
350 return ret;
351 }
352
353
354 GLenum GLAPIENTRY
355 _mesa_ClientWaitSync_no_error(GLsync sync, GLbitfield flags, GLuint64 timeout)
356 {
357 GET_CURRENT_CONTEXT(ctx);
358
359 struct gl_sync_object *syncObj = _mesa_get_and_ref_sync(ctx, sync, true);
360 return client_wait_sync(ctx, syncObj, flags, timeout);
361 }
362
363
364 GLenum GLAPIENTRY
365 _mesa_ClientWaitSync(GLsync sync, GLbitfield flags, GLuint64 timeout)
366 {
367 GET_CURRENT_CONTEXT(ctx);
368 struct gl_sync_object *syncObj;
369
370 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_WAIT_FAILED);
371
372 if ((flags & ~GL_SYNC_FLUSH_COMMANDS_BIT) != 0) {
373 _mesa_error(ctx, GL_INVALID_VALUE, "glClientWaitSync(flags=0x%x)", flags);
374 return GL_WAIT_FAILED;
375 }
376
377 syncObj = _mesa_get_and_ref_sync(ctx, sync, true);
378 if (!syncObj) {
379 _mesa_error(ctx, GL_INVALID_VALUE, "glClientWaitSync (not a valid sync object)");
380 return GL_WAIT_FAILED;
381 }
382
383 return client_wait_sync(ctx, syncObj, flags, timeout);
384 }
385
386
387 void GLAPIENTRY
388 _mesa_WaitSync(GLsync sync, GLbitfield flags, GLuint64 timeout)
389 {
390 GET_CURRENT_CONTEXT(ctx);
391 struct gl_sync_object *syncObj;
392
393 if (flags != 0) {
394 _mesa_error(ctx, GL_INVALID_VALUE, "glWaitSync(flags=0x%x)", flags);
395 return;
396 }
397
398 if (timeout != GL_TIMEOUT_IGNORED) {
399 _mesa_error(ctx, GL_INVALID_VALUE, "glWaitSync(timeout=0x%" PRIx64 ")",
400 (uint64_t) timeout);
401 return;
402 }
403
404 syncObj = _mesa_get_and_ref_sync(ctx, sync, true);
405 if (!syncObj) {
406 _mesa_error(ctx, GL_INVALID_VALUE, "glWaitSync (not a valid sync object)");
407 return;
408 }
409
410 ctx->Driver.ServerWaitSync(ctx, syncObj, flags, timeout);
411 _mesa_unref_sync_object(ctx, syncObj, 1);
412 }
413
414
415 void GLAPIENTRY
416 _mesa_GetSynciv(GLsync sync, GLenum pname, GLsizei bufSize, GLsizei *length,
417 GLint *values)
418 {
419 GET_CURRENT_CONTEXT(ctx);
420 struct gl_sync_object *syncObj;
421 GLsizei size = 0;
422 GLint v[1];
423
424 syncObj = _mesa_get_and_ref_sync(ctx, sync, true);
425 if (!syncObj) {
426 _mesa_error(ctx, GL_INVALID_VALUE, "glGetSynciv (not a valid sync object)");
427 return;
428 }
429
430 switch (pname) {
431 case GL_OBJECT_TYPE:
432 v[0] = syncObj->Type;
433 size = 1;
434 break;
435
436 case GL_SYNC_CONDITION:
437 v[0] = syncObj->SyncCondition;
438 size = 1;
439 break;
440
441 case GL_SYNC_STATUS:
442 /* Update the state of the sync by dipping into the driver. Note that
443 * this call won't block. It just updates state in the common object
444 * data from the current driver state.
445 */
446 ctx->Driver.CheckSync(ctx, syncObj);
447
448 v[0] = (syncObj->StatusFlag) ? GL_SIGNALED : GL_UNSIGNALED;
449 size = 1;
450 break;
451
452 case GL_SYNC_FLAGS:
453 v[0] = syncObj->Flags;
454 size = 1;
455 break;
456
457 default:
458 _mesa_error(ctx, GL_INVALID_ENUM, "glGetSynciv(pname=0x%x)\n", pname);
459 _mesa_unref_sync_object(ctx, syncObj, 1);
460 return;
461 }
462
463 /* Section 4.1.3 (Sync Object Queries) of the OpenGL ES 3.10 spec says:
464 *
465 * "An INVALID_VALUE error is generated if bufSize is negative."
466 */
467 if (bufSize < 0) {
468 _mesa_error(ctx, GL_INVALID_VALUE, "glGetSynciv(pname=0x%x)\n", pname);
469 }
470
471 if (size > 0 && bufSize > 0) {
472 const GLsizei copy_count = MIN2(size, bufSize);
473
474 memcpy(values, v, sizeof(GLint) * copy_count);
475 }
476
477 if (length != NULL) {
478 *length = size;
479 }
480
481 _mesa_unref_sync_object(ctx, syncObj, 1);
482 }