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