The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

c/client: Refactor context mutex locking to helper function

+101 -52
+15 -15
src/xrt/compositor/client/comp_gl_client.c
··· 382 382 383 383 sync_handle = XRT_GRAPHICS_SYNC_HANDLE_INVALID; 384 384 385 - xrt_result_t xret = c->context_begin(xc); 385 + xrt_result_t xret = client_gl_compositor_context_begin(xc); 386 386 if (xret == XRT_SUCCESS) { 387 387 sync_handle = handle_fencing_or_finish(c); 388 - c->context_end(xc); 388 + client_gl_compositor_context_end(xc); 389 389 } 390 390 391 391 COMP_TRACE_IDENT(layer_commit); ··· 411 411 struct client_gl_compositor *c = client_gl_compositor(xc); 412 412 xrt_result_t xret = XRT_SUCCESS; 413 413 414 - xret = c->context_begin(xc); 414 + xret = client_gl_compositor_context_begin(xc); 415 415 if (xret != XRT_SUCCESS) { 416 416 return xret; 417 417 } ··· 420 420 const char *version_str = (const char *)glGetString(GL_VERSION); 421 421 if (strstr(version_str, "OpenGL ES 2.") == version_str) { 422 422 U_LOG_E("Only one array layer is supported with OpenGL ES 2"); 423 - c->context_end(xc); 423 + client_gl_compositor_context_end(xc); 424 424 return XRT_ERROR_SWAPCHAIN_FLAG_VALID_BUT_UNSUPPORTED; 425 425 } 426 426 } ··· 428 428 int64_t vk_format = gl_format_to_vk(info->format); 429 429 if (vk_format == 0) { 430 430 U_LOG_E("Invalid format!"); 431 - c->context_end(xc); 431 + client_gl_compositor_context_end(xc); 432 432 return XRT_ERROR_SWAPCHAIN_FORMAT_UNSUPPORTED; 433 433 } 434 434 ··· 439 439 440 440 441 441 if (xret != XRT_SUCCESS) { 442 - c->context_end(xc); 442 + client_gl_compositor_context_end(xc); 443 443 return xret; 444 444 } 445 445 assert(xscn != NULL); ··· 458 458 if (NULL == c->create_swapchain(xc, info, xscn, &sc)) { 459 459 // Drop our reference, does NULL checking. 460 460 xrt_swapchain_reference(&xsc, NULL); 461 - c->context_end(xc); 461 + client_gl_compositor_context_end(xc); 462 462 return XRT_ERROR_OPENGL; 463 463 } 464 464 465 465 if (sc == NULL) { 466 466 U_LOG_E("Could not create OpenGL swapchain."); 467 - c->context_end(xc); 467 + client_gl_compositor_context_end(xc); 468 468 return XRT_ERROR_OPENGL; 469 469 } 470 470 ··· 483 483 484 484 glBindTexture(tex_target, prev_texture); 485 485 486 - c->context_end(xc); 486 + client_gl_compositor_context_end(xc); 487 487 488 488 *out_xsc = &sc->base.base; 489 489 return XRT_SUCCESS; ··· 520 520 bool 521 521 client_gl_compositor_init(struct client_gl_compositor *c, 522 522 struct xrt_compositor_native *xcn, 523 - client_gl_context_begin_func_t context_begin, 524 - client_gl_context_end_func_t context_end, 523 + client_gl_context_begin_locked_func_t context_begin_locked, 524 + client_gl_context_end_locked_func_t context_end_locked, 525 525 client_gl_swapchain_create_func_t create_swapchain, 526 526 client_gl_insert_fence_func_t insert_fence) 527 527 { 528 - assert(context_begin != NULL); 529 - assert(context_end != NULL); 528 + assert(context_begin_locked != NULL); 529 + assert(context_end_locked != NULL); 530 530 531 531 c->base.base.get_swapchain_create_properties = client_gl_compositor_get_swapchain_create_properties; 532 532 c->base.base.create_swapchain = client_gl_swapchain_create; ··· 546 546 c->base.base.layer_commit = client_gl_compositor_layer_commit; 547 547 c->base.base.destroy = client_gl_compositor_destroy; 548 548 c->base.base.poll_events = client_gl_compositor_poll_events; 549 - c->context_begin = context_begin; 550 - c->context_end = context_end; 549 + c->context_begin_locked = context_begin_locked; 550 + c->context_end_locked = context_end_locked; 551 551 c->create_swapchain = create_swapchain; 552 552 c->insert_fence = insert_fence; 553 553 c->xcn = xcn;
+65 -14
src/xrt/compositor/client/comp_gl_client.h
··· 1 - // Copyright 2019, Collabora, Ltd. 1 + // Copyright 2019-2022, Collabora, Ltd. 2 2 // SPDX-License-Identifier: BSL-1.0 3 3 /*! 4 4 * @file ··· 53 53 }; 54 54 55 55 /*! 56 - * Fetches the OpenGL context that is current on this thread and makes the OpenGL context given in the graphics binding 57 - * current instead. Only one thread at a time can operate on the sections between @ref client_gl_context_begin_func_t 58 - * and 59 - * @ref client_gl_context_end_func_t, therefore client_gl_context_end_func_t MUST be called to avoid blocking the next 60 - * thread calling @ref client_gl_context_begin_func_t. 56 + * Fetches the OpenGL context that is current on this thread and makes the 57 + * OpenGL context given in the graphics binding current instead. Only one thread 58 + * at a time can operate on the sections between 59 + * @ref client_gl_context_begin_locked_func_t and 60 + * @ref client_gl_context_end_locked_func_t, 61 + * therefore @ref client_gl_context_end_locked_func_t MUST be called to avoid 62 + * blocking the next thread calling @ref client_gl_context_begin_locked_func_t. 61 63 * 62 - * If the return value is not XRT_SUCCESS, @ref client_gl_context_end_func_t should not be called. 64 + * This function must be called with the context_mutex locked held, that is 65 + * handled by the helper function @ref client_gl_compositor_context_begin. 66 + * 67 + * If the return value is not XRT_SUCCESS, 68 + * @ref client_gl_context_end_locked_func_t should not be called. 63 69 */ 64 - typedef xrt_result_t (*client_gl_context_begin_func_t)(struct xrt_compositor *xc); 70 + typedef xrt_result_t (*client_gl_context_begin_locked_func_t)(struct xrt_compositor *xc); 65 71 66 72 /*! 67 - * Makes the OpenGL context current that was current before @ref client_gl_context_begin_func_t was called. 73 + * Makes the OpenGL context current that was current before 74 + * @ref client_gl_context_begin_locked_func_t was called. 75 + * 76 + * This function must be called with the context_mutex locked held, successful 77 + * call to @ref client_gl_compositor_context_begin will ensure that. The lock is 78 + * not released by this function, but @ref client_gl_compositor_context_end does 79 + * release it. 68 80 */ 69 - typedef void (*client_gl_context_end_func_t)(struct xrt_compositor *xc); 81 + typedef void (*client_gl_context_end_locked_func_t)(struct xrt_compositor *xc); 70 82 71 83 /*! 72 84 * The type of a swapchain create constructor. ··· 113 125 /*! 114 126 * Function pointer for making the OpenGL context current. 115 127 */ 116 - client_gl_context_begin_func_t context_begin; 128 + client_gl_context_begin_locked_func_t context_begin_locked; 117 129 118 130 /*! 119 131 * Function pointer for restoring prior OpenGL context. 120 132 */ 121 - client_gl_context_end_func_t context_end; 133 + client_gl_context_end_locked_func_t context_end_locked; 122 134 123 135 /*! 124 136 * Function pointer for creating the client swapchain. ··· 172 184 bool 173 185 client_gl_compositor_init(struct client_gl_compositor *c, 174 186 struct xrt_compositor_native *xcn, 175 - client_gl_context_begin_func_t context_begin, 176 - client_gl_context_end_func_t context_end, 187 + client_gl_context_begin_locked_func_t context_begin, 188 + client_gl_context_end_locked_func_t context_end, 177 189 client_gl_swapchain_create_func_t create_swapchain, 178 190 client_gl_insert_fence_func_t insert_fence); 179 191 ··· 186 198 */ 187 199 void 188 200 client_gl_compositor_close(struct client_gl_compositor *c); 201 + 202 + /*! 203 + * @copydoc client_gl_context_begin_locked_func_t 204 + * 205 + * Helper for calling through the function pointer. 206 + * 207 + * @public @memberof client_gl_compositor 208 + */ 209 + static inline xrt_result_t 210 + client_gl_compositor_context_begin(struct xrt_compositor *xc) 211 + { 212 + struct client_gl_compositor *cgc = client_gl_compositor(xc); 213 + 214 + os_mutex_lock(&cgc->context_mutex); 215 + 216 + xrt_result_t xret = cgc->context_begin_locked(xc); 217 + if (xret != XRT_SUCCESS) { 218 + os_mutex_unlock(&cgc->context_mutex); 219 + } 220 + 221 + return xret; 222 + } 223 + 224 + /*! 225 + * @copydoc client_gl_context_end_locked_func_t 226 + * 227 + * Helper for calling through the function pointer. 228 + * 229 + * @public @memberof client_gl_compositor 230 + */ 231 + static inline void 232 + client_gl_compositor_context_end(struct xrt_compositor *xc) 233 + { 234 + struct client_gl_compositor *cgc = client_gl_compositor(xc); 235 + 236 + cgc->context_end_locked(xc); 237 + 238 + os_mutex_unlock(&cgc->context_mutex); 239 + } 189 240 190 241 191 242 #ifdef __cplusplus
+2 -2
src/xrt/compositor/client/comp_gl_memobj_swapchain.c
··· 50 50 uint32_t image_count = sc->base.base.base.image_count; 51 51 52 52 struct client_gl_compositor *c = sc->base.gl_compositor; 53 - enum xrt_result xret = c->context_begin(&c->base.base); 53 + enum xrt_result xret = client_gl_compositor_context_begin(&c->base.base); 54 54 55 55 if (image_count > 0) { 56 56 if (xret == XRT_SUCCESS) { ··· 64 64 } 65 65 66 66 if (xret == XRT_SUCCESS) { 67 - c->context_end(&c->base.base); 67 + client_gl_compositor_context_end(&c->base.base); 68 68 } 69 69 70 70 // Drop our reference, does NULL checking.
+9 -10
src/xrt/compositor/client/comp_gl_win32_client.c
··· 76 76 } 77 77 78 78 static xrt_result_t 79 - client_gl_context_begin(struct xrt_compositor *xc) 79 + client_gl_context_begin_locked(struct xrt_compositor *xc) 80 80 { 81 81 struct client_gl_win32_compositor *c = client_gl_win32_compositor(xc); 82 82 83 83 struct client_gl_context *app_ctx = &c->app_context; 84 - 85 - os_mutex_lock(&c->base.context_mutex); 86 84 87 85 context_save_current(&c->temp_context); 88 86 ··· 92 90 (void *)c->temp_context.hGLRC, (void *)app_ctx->hGLRC); 93 91 94 92 if (need_make_current && !context_make_current(app_ctx)) { 95 - os_mutex_unlock(&c->base.context_mutex); 96 - 97 93 U_LOG_E("Failed to make WGL context current"); 98 94 // No need to restore on failure. 99 95 return XRT_ERROR_OPENGL; ··· 103 99 } 104 100 105 101 static void 106 - client_gl_context_end(struct xrt_compositor *xc) 102 + client_gl_context_end_locked(struct xrt_compositor *xc) 107 103 { 108 104 struct client_gl_win32_compositor *c = client_gl_win32_compositor(xc); 109 105 ··· 120 116 U_LOG_E("Failed to make old WGL context current!"); 121 117 // fall through to os_mutex_unlock even if we didn't succeed in restoring the context 122 118 } 123 - 124 - os_mutex_unlock(&c->base.context_mutex); 125 119 } 126 120 127 121 static GLADapiproc ··· 228 222 // Same for the opengl library handle 229 223 c->opengl = opengl; 230 224 231 - if (!client_gl_compositor_init(&c->base, xcn, client_gl_context_begin, client_gl_context_end, 232 - client_gl_memobj_swapchain_create, NULL)) { 225 + if (!client_gl_compositor_init( // 226 + &c->base, // 227 + xcn, // 228 + client_gl_context_begin_locked, // 229 + client_gl_context_end_locked, // 230 + client_gl_memobj_swapchain_create, // 231 + NULL)) { // 233 232 U_LOG_E("Failed to init parent GL client compositor!"); 234 233 FreeLibrary(opengl); 235 234 free(c);
+10 -11
src/xrt/compositor/client/comp_gl_xlib_client.c
··· 1 - // Copyright 2019, Collabora, Ltd. 1 + // Copyright 2019-2022, Collabora, Ltd. 2 2 // SPDX-License-Identifier: BSL-1.0 3 3 /*! 4 4 * @file ··· 74 74 } 75 75 76 76 static xrt_result_t 77 - client_gl_context_begin(struct xrt_compositor *xc) 77 + client_gl_context_begin_locked(struct xrt_compositor *xc) 78 78 { 79 79 struct client_gl_xlib_compositor *c = client_gl_xlib_compositor(xc); 80 80 81 81 struct client_gl_context *app_ctx = &c->app_context; 82 - 83 - os_mutex_lock(&c->base.context_mutex); 84 82 85 83 context_save_current(&c->temp_context); 86 84 ··· 90 88 (void *)c->temp_context.ctx, (void *)app_ctx->ctx); 91 89 92 90 if (need_make_current && !context_make_current(app_ctx)) { 93 - os_mutex_unlock(&c->base.context_mutex); 94 - 95 91 U_LOG_E("Failed to make GLX context current"); 96 92 // No need to restore on failure. 97 93 return XRT_ERROR_OPENGL; ··· 101 97 } 102 98 103 99 static void 104 - client_gl_context_end(struct xrt_compositor *xc) 100 + client_gl_context_end_locked(struct xrt_compositor *xc) 105 101 { 106 102 struct client_gl_xlib_compositor *c = client_gl_xlib_compositor(xc); 107 103 ··· 120 116 (unsigned long)current_glx_context->read, (void *)current_glx_context->ctx); 121 117 // fall through to os_mutex_unlock even if we didn't succeed in restoring the context 122 118 } 123 - 124 - os_mutex_unlock(&c->base.context_mutex); 125 119 } 126 120 127 121 typedef void (*void_ptr_func)(); ··· 198 192 // Move the app context to the struct. 199 193 c->app_context = app_ctx; 200 194 201 - if (!client_gl_compositor_init(&c->base, xcn, client_gl_context_begin, client_gl_context_end, 202 - client_gl_memobj_swapchain_create, NULL)) { 195 + if (!client_gl_compositor_init( // 196 + &c->base, // 197 + xcn, // 198 + client_gl_context_begin_locked, // 199 + client_gl_context_end_locked, // 200 + client_gl_memobj_swapchain_create, // 201 + NULL)) { // 203 202 free(c); 204 203 return NULL; 205 204 }