From 61c10440898040cbe3eb5eb9d7723f45ab0392a2 Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Tue, 6 Apr 2021 17:17:29 -0500 Subject: [PATCH] u/handles: Add graphics sync handle helpers. --- src/xrt/auxiliary/util/u_handles.c | 76 +++++++++++++++++++++++++++++- src/xrt/auxiliary/util/u_handles.h | 35 ++++++++++++-- 2 files changed, 106 insertions(+), 5 deletions(-) diff --git a/src/xrt/auxiliary/util/u_handles.c b/src/xrt/auxiliary/util/u_handles.c index c6ddcce5c..7c9d56e80 100644 --- a/src/xrt/auxiliary/util/u_handles.c +++ b/src/xrt/auxiliary/util/u_handles.c @@ -1,4 +1,4 @@ -// Copyright 2020, Collabora, Ltd. +// Copyright 2020-2021, Collabora, Ltd. // SPDX-License-Identifier: BSL-1.0 /*! * @file @@ -11,6 +11,12 @@ #include "u_handles.h" +/* + * + * Graphics Buffer Handles + * + */ + #if defined(XRT_GRAPHICS_BUFFER_HANDLE_IS_AHARDWAREBUFFER) #include @@ -89,3 +95,71 @@ u_graphics_buffer_unref(xrt_graphics_buffer_handle_t *handle_ptr) release_graphics_handle(handle); *handle_ptr = XRT_GRAPHICS_BUFFER_HANDLE_INVALID; } + +/* + * + * Graphics Sync Handles + * + */ + +#if defined(XRT_GRAPHICS_SYNC_HANDLE_IS_FD) +#include + +static inline void +release_sync_handle(xrt_graphics_sync_handle_t handle) +{ + close(handle); +} + +static inline xrt_graphics_sync_handle_t +ref_sync_handle(xrt_graphics_sync_handle_t handle) +{ + return dup(handle); +} + +#elif defined(XRT_GRAPHICS_SYNC_HANDLE_IS_WIN32_HANDLE) + +static inline void +release_sync_handle(xrt_graphics_sync_handle_t handle) +{ + CloseHandle(handle); +} + +static inline xrt_graphics_sync_handle_t +ref_sync_handle(xrt_graphics_sync_handle_t handle) +{ + HANDLE self = GetCurrentProcess(); + HANDLE result = NULL; + if (DuplicateHandle(self, handle, self, &result, 0, FALSE, DUPLICATE_SAME_ACCESS) != 0) { + return result; + } + return NULL; +} + +#else +#error "need port" +#endif + +xrt_graphics_sync_handle_t +u_graphics_sync_ref(xrt_graphics_sync_handle_t handle) +{ + if (xrt_graphics_sync_handle_is_valid(handle)) { + return ref_sync_handle(handle); + } + + return XRT_GRAPHICS_SYNC_HANDLE_INVALID; +} + +void +u_graphics_sync_unref(xrt_graphics_sync_handle_t *handle_ptr) +{ + if (handle_ptr == NULL) { + return; + } + xrt_graphics_sync_handle_t handle = *handle_ptr; + if (!xrt_graphics_sync_handle_is_valid(handle)) { + return; + } + release_sync_handle(handle); + *handle_ptr = XRT_GRAPHICS_SYNC_HANDLE_INVALID; +} diff --git a/src/xrt/auxiliary/util/u_handles.h b/src/xrt/auxiliary/util/u_handles.h index 34e7d9dad..a64d25cbf 100644 --- a/src/xrt/auxiliary/util/u_handles.h +++ b/src/xrt/auxiliary/util/u_handles.h @@ -1,4 +1,4 @@ -// Copyright 2020, Collabora, Ltd. +// Copyright 2020-20211, Collabora, Ltd. // SPDX-License-Identifier: BSL-1.0 /*! * @file @@ -17,13 +17,15 @@ #ifdef __cplusplus extern "C" { #endif + /*! * Increase the reference count on the buffer handle, returning the new * reference. * - * Depending on the underlying type, the value may be the same or different than - * what was passed in. It should be retained for use at release time, - * regardless. + * + * Depending on the underlying type, the value may be the same or different than what was passed in. It should be + * retained for use at release time, regardless. (For example, if the underlying native handle does not expose reference + * counting, it may be duplicated and the duplicate returned.) * * @public @memberof xrt_graphics_buffer_handle_t */ @@ -42,6 +44,31 @@ u_graphics_buffer_ref(xrt_graphics_buffer_handle_t handle); void u_graphics_buffer_unref(xrt_graphics_buffer_handle_t *handle); +/*! + * Increase the reference count on the sync handle, returning the new + * reference. + * + * Depending on the underlying type, the value may be the same or different than what was passed in. It should be + * retained for use at release time, regardless. (For example, if the underlying native handle does not expose reference + * counting, it may be duplicated and the duplicate returned.) + * + * @public @memberof xrt_graphics_sync_handle_t + */ +xrt_graphics_sync_handle_t +u_graphics_sync_ref(xrt_graphics_sync_handle_t handle); + +/*! + * Decrease the reference count/release the handle reference passed in. + * + * Be sure to only call this once per handle. + * + * Performs null-check and clears the value after unreferencing. + * + * @public @memberof xrt_graphics_sync_handle_t + */ +void +u_graphics_sync_unref(xrt_graphics_sync_handle_t *handle); + #ifdef __cplusplus } // extern "C" #endif