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