2 * Copyright © 2009 Intel Corporation
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:
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
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.
26 * Sync object management.
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:
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.
37 * References are removed:
39 * - By \c glDeleteSync.
40 * - At the end of \c glClientWaitSync.
42 * Additionally, drivers may call \c _mesa_ref_sync_object and
43 * \c _mesa_unref_sync_object as needed to implement \c ServerWaitSync.
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
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.
55 * \author Ian Romanick <ian.d.romanick@intel.com>
66 #include "util/hash_table.h"
71 static struct gl_sync_object
*
72 _mesa_new_sync_object(struct gl_context
*ctx
, GLenum type
)
74 struct gl_sync_object
*s
= CALLOC_STRUCT(gl_sync_object
);
83 _mesa_delete_sync_object(struct gl_context
*ctx
, struct gl_sync_object
*syncObj
)
92 _mesa_fence_sync(struct gl_context
*ctx
, struct gl_sync_object
*syncObj
,
93 GLenum condition
, GLbitfield flags
)
99 syncObj
->StatusFlag
= 1;
104 _mesa_check_sync(struct gl_context
*ctx
, struct gl_sync_object
*syncObj
)
109 /* No-op for software rendering. Hardware drivers will need to determine
110 * whether the state of the sync object has changed.
116 _mesa_wait_sync(struct gl_context
*ctx
, struct gl_sync_object
*syncObj
,
117 GLbitfield flags
, GLuint64 timeout
)
124 /* No-op for software rendering. Hardware drivers will need to wait until
125 * the state of the sync object changes or the timeout expires.
131 _mesa_init_sync_object_functions(struct dd_function_table
*driver
)
133 driver
->NewSyncObject
= _mesa_new_sync_object
;
134 driver
->FenceSync
= _mesa_fence_sync
;
135 driver
->DeleteSyncObject
= _mesa_delete_sync_object
;
136 driver
->CheckSync
= _mesa_check_sync
;
138 /* Use the same no-op wait function for both.
140 driver
->ClientWaitSync
= _mesa_wait_sync
;
141 driver
->ServerWaitSync
= _mesa_wait_sync
;
145 * Allocate/init the context state related to sync objects.
148 _mesa_init_sync(struct gl_context
*ctx
)
155 * Free the context state related to sync objects.
158 _mesa_free_sync_data(struct gl_context
*ctx
)
165 * Check if the given sync object is:
167 * - not in sync objects hash table
168 * - type is GL_SYNC_FENCE
169 * - not marked as deleted
171 * Returns the internal gl_sync_object pointer if the sync object is valid
172 * or NULL if it isn't.
174 * If "incRefCount" is true, the reference count is incremented, which is
175 * normally what you want; otherwise, a glDeleteSync from another thread
176 * could delete the sync object while you are still working on it.
178 struct gl_sync_object
*
179 _mesa_get_and_ref_sync(struct gl_context
*ctx
, GLsync sync
, bool incRefCount
)
181 struct gl_sync_object
*syncObj
= (struct gl_sync_object
*) sync
;
182 mtx_lock(&ctx
->Shared
->Mutex
);
184 && _mesa_set_search(ctx
->Shared
->SyncObjects
, syncObj
) != NULL
185 && (syncObj
->Type
== GL_SYNC_FENCE
)
186 && !syncObj
->DeletePending
) {
193 mtx_unlock(&ctx
->Shared
->Mutex
);
199 _mesa_unref_sync_object(struct gl_context
*ctx
, struct gl_sync_object
*syncObj
,
202 struct set_entry
*entry
;
204 mtx_lock(&ctx
->Shared
->Mutex
);
205 syncObj
->RefCount
-= amount
;
206 if (syncObj
->RefCount
== 0) {
207 entry
= _mesa_set_search(ctx
->Shared
->SyncObjects
, syncObj
);
208 assert (entry
!= NULL
);
209 _mesa_set_remove(ctx
->Shared
->SyncObjects
, entry
);
210 mtx_unlock(&ctx
->Shared
->Mutex
);
212 ctx
->Driver
.DeleteSyncObject(ctx
, syncObj
);
214 mtx_unlock(&ctx
->Shared
->Mutex
);
220 _mesa_IsSync(GLsync sync
)
222 GET_CURRENT_CONTEXT(ctx
);
223 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_FALSE
);
225 return _mesa_get_and_ref_sync(ctx
, sync
, false) ? GL_TRUE
: GL_FALSE
;
230 _mesa_DeleteSync(GLsync sync
)
232 GET_CURRENT_CONTEXT(ctx
);
233 struct gl_sync_object
*syncObj
;
235 /* From the GL_ARB_sync spec:
237 * DeleteSync will silently ignore a <sync> value of zero. An
238 * INVALID_VALUE error is generated if <sync> is neither zero nor the
239 * name of a sync object.
245 syncObj
= _mesa_get_and_ref_sync(ctx
, sync
, true);
247 _mesa_error(ctx
, GL_INVALID_VALUE
, "glDeleteSync (not a valid sync object)");
251 /* If there are no client-waits or server-waits pending on this sync, delete
252 * the underlying object. Note that we double-unref the object, as
253 * _mesa_get_and_ref_sync above took an extra refcount to make sure the pointer
254 * is valid for us to manipulate.
256 syncObj
->DeletePending
= GL_TRUE
;
257 _mesa_unref_sync_object(ctx
, syncObj
, 2);
262 _mesa_FenceSync(GLenum condition
, GLbitfield flags
)
264 GET_CURRENT_CONTEXT(ctx
);
265 struct gl_sync_object
*syncObj
;
266 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, 0);
268 if (condition
!= GL_SYNC_GPU_COMMANDS_COMPLETE
) {
269 _mesa_error(ctx
, GL_INVALID_ENUM
, "glFenceSync(condition=0x%x)",
275 _mesa_error(ctx
, GL_INVALID_VALUE
, "glFenceSync(flags=0x%x)",
280 syncObj
= ctx
->Driver
.NewSyncObject(ctx
, GL_SYNC_FENCE
);
281 if (syncObj
!= NULL
) {
282 syncObj
->Type
= GL_SYNC_FENCE
;
283 /* The name is not currently used, and it is never visible to
284 * applications. If sync support is extended to provide support for
285 * NV_fence, this field will be used. We'll also need to add an object
289 syncObj
->RefCount
= 1;
290 syncObj
->DeletePending
= GL_FALSE
;
291 syncObj
->SyncCondition
= condition
;
292 syncObj
->Flags
= flags
;
293 syncObj
->StatusFlag
= 0;
295 ctx
->Driver
.FenceSync(ctx
, syncObj
, condition
, flags
);
297 mtx_lock(&ctx
->Shared
->Mutex
);
298 _mesa_set_add(ctx
->Shared
->SyncObjects
, syncObj
);
299 mtx_unlock(&ctx
->Shared
->Mutex
);
301 return (GLsync
) syncObj
;
309 client_wait_sync(struct gl_context
*ctx
, struct gl_sync_object
*syncObj
,
310 GLbitfield flags
, GLuint64 timeout
)
314 /* From the GL_ARB_sync spec:
316 * ClientWaitSync returns one of four status values. A return value of
317 * ALREADY_SIGNALED indicates that <sync> was signaled at the time
318 * ClientWaitSync was called. ALREADY_SIGNALED will always be returned
319 * if <sync> was signaled, even if the value of <timeout> is zero.
321 ctx
->Driver
.CheckSync(ctx
, syncObj
);
322 if (syncObj
->StatusFlag
) {
323 ret
= GL_ALREADY_SIGNALED
;
326 ret
= GL_TIMEOUT_EXPIRED
;
328 ctx
->Driver
.ClientWaitSync(ctx
, syncObj
, flags
, timeout
);
330 ret
= syncObj
->StatusFlag
? GL_CONDITION_SATISFIED
: GL_TIMEOUT_EXPIRED
;
334 _mesa_unref_sync_object(ctx
, syncObj
, 1);
340 _mesa_ClientWaitSync_no_error(GLsync sync
, GLbitfield flags
, GLuint64 timeout
)
342 GET_CURRENT_CONTEXT(ctx
);
344 struct gl_sync_object
*syncObj
= _mesa_get_and_ref_sync(ctx
, sync
, true);
345 return client_wait_sync(ctx
, syncObj
, flags
, timeout
);
350 _mesa_ClientWaitSync(GLsync sync
, GLbitfield flags
, GLuint64 timeout
)
352 GET_CURRENT_CONTEXT(ctx
);
353 struct gl_sync_object
*syncObj
;
355 ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx
, GL_WAIT_FAILED
);
357 if ((flags
& ~GL_SYNC_FLUSH_COMMANDS_BIT
) != 0) {
358 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClientWaitSync(flags=0x%x)", flags
);
359 return GL_WAIT_FAILED
;
362 syncObj
= _mesa_get_and_ref_sync(ctx
, sync
, true);
364 _mesa_error(ctx
, GL_INVALID_VALUE
, "glClientWaitSync (not a valid sync object)");
365 return GL_WAIT_FAILED
;
368 return client_wait_sync(ctx
, syncObj
, flags
, timeout
);
373 _mesa_WaitSync(GLsync sync
, GLbitfield flags
, GLuint64 timeout
)
375 GET_CURRENT_CONTEXT(ctx
);
376 struct gl_sync_object
*syncObj
;
379 _mesa_error(ctx
, GL_INVALID_VALUE
, "glWaitSync(flags=0x%x)", flags
);
383 if (timeout
!= GL_TIMEOUT_IGNORED
) {
384 _mesa_error(ctx
, GL_INVALID_VALUE
, "glWaitSync(timeout=0x%" PRIx64
")",
389 syncObj
= _mesa_get_and_ref_sync(ctx
, sync
, true);
391 _mesa_error(ctx
, GL_INVALID_VALUE
, "glWaitSync (not a valid sync object)");
395 ctx
->Driver
.ServerWaitSync(ctx
, syncObj
, flags
, timeout
);
396 _mesa_unref_sync_object(ctx
, syncObj
, 1);
401 _mesa_GetSynciv(GLsync sync
, GLenum pname
, GLsizei bufSize
, GLsizei
*length
,
404 GET_CURRENT_CONTEXT(ctx
);
405 struct gl_sync_object
*syncObj
;
409 syncObj
= _mesa_get_and_ref_sync(ctx
, sync
, true);
411 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetSynciv (not a valid sync object)");
417 v
[0] = syncObj
->Type
;
421 case GL_SYNC_CONDITION
:
422 v
[0] = syncObj
->SyncCondition
;
427 /* Update the state of the sync by dipping into the driver. Note that
428 * this call won't block. It just updates state in the common object
429 * data from the current driver state.
431 ctx
->Driver
.CheckSync(ctx
, syncObj
);
433 v
[0] = (syncObj
->StatusFlag
) ? GL_SIGNALED
: GL_UNSIGNALED
;
438 v
[0] = syncObj
->Flags
;
443 _mesa_error(ctx
, GL_INVALID_ENUM
, "glGetSynciv(pname=0x%x)\n", pname
);
444 _mesa_unref_sync_object(ctx
, syncObj
, 1);
448 /* Section 4.1.3 (Sync Object Queries) of the OpenGL ES 3.10 spec says:
450 * "An INVALID_VALUE error is generated if bufSize is negative."
453 _mesa_error(ctx
, GL_INVALID_VALUE
, "glGetSynciv(pname=0x%x)\n", pname
);
456 if (size
> 0 && bufSize
> 0) {
457 const GLsizei copy_count
= MIN2(size
, bufSize
);
459 memcpy(values
, v
, sizeof(GLint
) * copy_count
);
462 if (length
!= NULL
) {
466 _mesa_unref_sync_object(ctx
, syncObj
, 1);