Monado OpenXR Runtime
xrt_handles.h
Go to the documentation of this file.
1// Copyright 2020, Collabora, Ltd.
2// Copyright 2025, NVIDIA CORPORATION.
3// SPDX-License-Identifier: BSL-1.0
4/*!
5 * @file
6 * @brief Native handle types.
7 * @author Rylie Pavlik <rylie.pavlik@collabora.com>
8 * @ingroup xrt_iface
9 */
10
11#pragma once
12
13#include "xrt_config_os.h"
14#include <stdbool.h>
15#include <stddef.h>
16
17#ifdef XRT_OS_WINDOWS
18#include "xrt_windows.h"
19#else // !XRT_OS_WINDOWS
20#include "unistd.h"
21#endif // !XRT_OS_WINDOWS
22
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
28#if defined(XRT_OS_WINDOWS)
29/*!
30 * The type for an IPC handle.
31 *
32 * On Windows, this is HANDLE.
33 */
34typedef HANDLE xrt_ipc_handle_t;
35
36/*!
37 * An invalid value for an IPC handle.
38 *
39 * Note that there may be more than one value that's invalid - use
40 * @ref xrt_ipc_handle_is_valid instead of comparing against this!
41 *
42 * @relates xrt_ipc_handle_t
43 */
44#define XRT_IPC_HANDLE_INVALID INVALID_HANDLE_VALUE
45
46/*!
47 * Check whether an IPC handle is valid.
48 *
49 * @public @memberof xrt_ipc_handle_t
50 */
51static inline bool
52xrt_ipc_handle_is_valid(xrt_ipc_handle_t handle)
53{
54 return handle != INVALID_HANDLE_VALUE;
55}
56
57/*!
58 * Close an IPC handle.
59 *
60 * @public @memberof xrt_ipc_handle_t
61 */
62static inline void
63xrt_ipc_handle_close(xrt_ipc_handle_t handle)
64{
65 CloseHandle(handle);
66}
67
68#else // !XRT_OS_WINDOWS
69
70/*!
71 * The type for an IPC handle.
72 *
73 * On non-Windows, this is a file descriptor.
74 */
75typedef int xrt_ipc_handle_t;
76
77/*!
78 * An invalid value for an IPC handle.
79 *
80 * Note that there may be more than one value that's invalid - use
81 * @ref xrt_ipc_handle_is_valid instead of comparing against this!
82 *
83 * @relates xrt_ipc_handle_t
84 */
85#define XRT_IPC_HANDLE_INVALID (-1)
86
87/*!
88 * Check whether an IPC handle is valid.
89 *
90 * @public @memberof xrt_ipc_handle_t
91 */
92static inline bool
93xrt_ipc_handle_is_valid(xrt_ipc_handle_t handle)
94{
95 return handle >= 0;
96}
97
98/*!
99 * Close an IPC handle.
100 *
101 * @public @memberof xrt_ipc_handle_t
102 */
103static inline void
104xrt_ipc_handle_close(xrt_ipc_handle_t handle)
105{
106 close(handle);
107}
108
109#endif // !XRT_OS_WINDOWS
110
111/*
112 *
113 * xrt_shmem_handle_t
114 *
115 */
116
117#if defined(XRT_OS_WINDOWS)
118/*!
119 * The type for shared memory blocks shared over IPC.
120 *
121 * On Windows, this is a HANDLE.
122 */
123typedef HANDLE xrt_shmem_handle_t;
124
125/*!
126 * Check whether a shared memory handle is valid.
127 *
128 * @public @memberof xrt_shmem_handle_t
129 */
130static inline bool
132{
133 return handle != NULL;
134}
135
136/*!
137 * An invalid value for a shared memory block.
138 *
139 * Note that there may be more than one value that's invalid - use
140 * @ref xrt_shmem_is_valid instead of comparing against this!
141 *
142 * @relates xrt_shmem_handle_t
143 */
144#define XRT_SHMEM_HANDLE_INVALID (NULL)
145
146#else // !XRT_OS_WINDOWS
147
148/*!
149 * The type for shared memory blocks shared over IPC.
150 *
151 * On Linux, this is a file descriptor.
152 */
154
155/*!
156 * Defined to allow detection of the underlying type.
157 *
158 * @relates xrt_shmem_handle_t
159 */
160#define XRT_SHMEM_HANDLE_IS_FD 1
161
162/*!
163 * Check whether a shared memory handle is valid.
164 *
165 * @public @memberof xrt_shmem_handle_t
166 */
167static inline bool
169{
170 return handle >= 0;
171}
172
173/*!
174 * An invalid value for a shared memory block.
175 *
176 * Note that there may be more than one value that's invalid - use
177 * @ref xrt_shmem_is_valid instead of comparing against this!
178 *
179 * @relates xrt_shmem_handle_t
180 */
181#define XRT_SHMEM_HANDLE_INVALID (-1)
182
183#endif // !XRT_OS_WINDOWS
184
185/*
186 *
187 * xrt_graphics_buffer_handle_t
188 *
189 */
190
191#if defined(XRT_OS_ANDROID) && defined(XRT_OS_ANDROID_USE_AHB) && (__ANDROID_API__ >= 26)
192typedef struct AHardwareBuffer AHardwareBuffer;
193
194/*!
195 * The type underlying buffers shared between compositor clients and the main
196 * compositor.
197 *
198 * On Android platform 26+, this is an @p AHardwareBuffer pointer.
199 */
200typedef AHardwareBuffer *xrt_graphics_buffer_handle_t;
201
202/*!
203 * Defined to allow detection of the underlying type.
204 *
205 * @relates xrt_graphics_buffer_handle_t
206 */
207#define XRT_GRAPHICS_BUFFER_HANDLE_IS_AHARDWAREBUFFER 1
208
209/*!
210 * Defined to indicate that the graphics buffer has a reference added by the import into Vulkan,
211 * and is not consumed nor does it need to be kept alive until destruction of the imported image.
212 *
213 * @relates xrt_graphics_buffer_handle_t
214 * @see XRT_GRAPHICS_BUFFER_HANDLE_CONSUMED_BY_VULKAN_IMPORT
215 */
216#define XRT_GRAPHICS_BUFFER_HANDLE_REFERENCE_ADDED_BY_VULKAN_IMPORT 1
217
218/*!
219 * Check whether a graphics buffer handle is valid.
220 *
221 * @public @memberof xrt_graphics_buffer_handle_t
222 */
223static inline bool
224xrt_graphics_buffer_is_valid(xrt_graphics_buffer_handle_t handle)
225{
226 return handle != NULL;
227}
228
229/*!
230 * An invalid value for a graphics buffer.
231 *
232 * Note that there may be more than one value that's invalid - use
233 * xrt_graphics_buffer_is_valid() instead of comparing against this!
234 *
235 * @relates xrt_graphics_buffer_handle_t
236 */
237#define XRT_GRAPHICS_BUFFER_HANDLE_INVALID NULL
238
239#elif defined(XRT_OS_ANDROID) && !defined(XRT_OS_ANDROID_USE_AHB) || defined(XRT_OS_LINUX) || defined(XRT_OS_OSX)
240
241/*!
242 * The type underlying buffers shared between compositor clients and the main
243 * compositor.
244 *
245 * On Linux, this is a file descriptor.
246 *
247 * We selected the graphics handle to be FDs on OSX because that allowed the
248 * code to compile, but it's probably not what we want to actually use on OSX.
249 * So we will need to change that down the line, as it looks like OSX uses byte
250 * only arrays to share MTLTextures.
251 */
253
254/*!
255 * Defined to allow detection of the underlying type.
256 *
257 * @relates xrt_graphics_buffer_handle_t
258 */
259#define XRT_GRAPHICS_BUFFER_HANDLE_IS_FD 1
260
261/*!
262 * Defined to indicate that the graphics buffer is consumed by the import into Vulkan
263 *
264 * @relates xrt_graphics_buffer_handle_t
265 * @see XRT_GRAPHICS_BUFFER_HANDLE_REFERENCE_ADDED_BY_VULKAN_IMPORT
266 */
267#define XRT_GRAPHICS_BUFFER_HANDLE_CONSUMED_BY_VULKAN_IMPORT 1
268
269/*!
270 * Check whether a graphics buffer handle is valid.
271 *
272 * @public @memberof xrt_graphics_buffer_handle_t
273 */
274static inline bool
275xrt_graphics_buffer_is_valid(xrt_graphics_buffer_handle_t handle)
276{
277 return handle >= 0;
278}
279
280/*!
281 * An invalid value for a graphics buffer.
282 *
283 * Note that there may be more than one value that's invalid - use
284 * xrt_graphics_buffer_is_valid() instead of comparing against this!
285 *
286 * @relates xrt_graphics_buffer_handle_t
287 */
288#define XRT_GRAPHICS_BUFFER_HANDLE_INVALID (-1)
289
290#elif defined(XRT_OS_WINDOWS)
291
292/*!
293 * The type underlying buffers shared between compositor clients and the main
294 * compositor.
295 *
296 * On Windows, this is a HANDLE.
297 */
298typedef HANDLE xrt_graphics_buffer_handle_t;
299
300/*!
301 * Defined to allow detection of the underlying type.
302 *
303 * @relates xrt_graphics_buffer_handle_t
304 */
305#define XRT_GRAPHICS_BUFFER_HANDLE_IS_WIN32_HANDLE 1
306
307/*!
308 * Defined to indicate that the graphics buffer is consumed by the import into Vulkan
309 *
310 * @relates xrt_graphics_buffer_handle_t
311 * @see XRT_GRAPHICS_BUFFER_HANDLE_REFERENCE_ADDED_BY_VULKAN_IMPORT
312 */
313#define XRT_GRAPHICS_BUFFER_HANDLE_CONSUMED_BY_VULKAN_IMPORT 1
314
315/*!
316 * Check whether a graphics buffer handle is valid.
317 *
318 * @public @memberof xrt_graphics_buffer_handle_t
319 */
320static inline bool
321xrt_graphics_buffer_is_valid(xrt_graphics_buffer_handle_t handle)
322{
323 return handle != NULL;
324}
325
326/*!
327 * An invalid value for a graphics buffer.
328 *
329 * Note that there may be more than one value that's invalid - use
330 * xrt_graphics_buffer_is_valid() instead of comparing against this!
331 *
332 * @relates xrt_graphics_buffer_handle_t
333 */
334#define XRT_GRAPHICS_BUFFER_HANDLE_INVALID (NULL)
335#else
336#error "Not yet implemented for this platform"
337#endif
338
339#ifdef XRT_OS_UNIX
340
341
342/*
343 *
344 * xrt_graphics_sync_handle_t
345 *
346 */
347
348/*!
349 * The type underlying synchronization primitives (semaphores, etc) shared
350 * between compositor clients and the main compositor.
351 *
352 * On Linux, this is a file descriptor.
353 */
355
356/*!
357 * Defined to allow detection of the underlying type.
358 *
359 * @relates xrt_graphics_sync_handle_t
360 */
361#define XRT_GRAPHICS_SYNC_HANDLE_IS_FD 1
362
363/*!
364 * Check whether a graphics sync handle is valid.
365 *
366 * @public @memberof xrt_graphics_sync_handle_t
367 */
368static inline bool
369xrt_graphics_sync_handle_is_valid(xrt_graphics_sync_handle_t handle)
370{
371 return handle >= 0;
372}
373
374/*!
375 * An invalid value for a graphics sync primitive.
376 *
377 * Note that there may be more than one value that's invalid - use
378 * xrt_graphics_sync_handle_is_valid() instead of comparing against this!
379 *
380 * @relates xrt_graphics_sync_handle_t
381 */
382#define XRT_GRAPHICS_SYNC_HANDLE_INVALID (-1)
383
384#elif defined(XRT_OS_WINDOWS)
385
386/*!
387 * The type underlying synchronization primitives (semaphores, etc) shared
388 * between compositor clients and the main compositor.
389 *
390 * On Windows, this is a HANDLE.
391 */
392typedef HANDLE xrt_graphics_sync_handle_t;
393
394/*!
395 * Defined to allow detection of the underlying type.
396 *
397 * @relates xrt_graphics_sync_handle_t
398 */
399#define XRT_GRAPHICS_SYNC_HANDLE_IS_WIN32_HANDLE 1
400
401/*!
402 * Check whether a graphics sync handle is valid.
403 *
404 * @public @memberof xrt_graphics_sync_handle_t
405 */
406static inline bool
407xrt_graphics_sync_handle_is_valid(xrt_graphics_sync_handle_t handle)
408{
409 return handle != NULL;
410}
411
412/*!
413 * An invalid value for a graphics sync primitive.
414 *
415 * Note that there may be more than one value that's invalid - use
416 * xrt_graphics_sync_handle_is_valid() instead of comparing against this!
417 *
418 * @relates xrt_graphics_sync_handle_t
419 */
420#define XRT_GRAPHICS_SYNC_HANDLE_INVALID (NULL)
421
422#endif
423
424#if (!defined(XRT_GRAPHICS_BUFFER_HANDLE_REFERENCE_ADDED_BY_VULKAN_IMPORT)) && \
425 (!defined(XRT_GRAPHICS_BUFFER_HANDLE_CONSUMED_BY_VULKAN_IMPORT))
426// Exactly one of these must be defined in each platform.
427#error "Needs port: Must define a macro indicating the Vulkan image import buffer behavior"
428#endif
429
430#ifdef __cplusplus
431}
432#endif
Generic typedef for platform-specific shared memory handle.
static bool xrt_shmem_is_valid(xrt_shmem_handle_t handle)
Check whether a shared memory handle is valid.
Definition: xrt_handles.h:168
Auto detect OS and certain features.
int xrt_graphics_buffer_handle_t
The type underlying buffers shared between compositor clients and the main compositor.
Definition: xrt_handles.h:252
int xrt_shmem_handle_t
The type for shared memory blocks shared over IPC.
Definition: xrt_handles.h:153
int xrt_graphics_sync_handle_t
The type underlying synchronization primitives (semaphores, etc) shared between compositor clients an...
Definition: xrt_handles.h:354
int xrt_ipc_handle_t
The type for an IPC handle.
Definition: xrt_handles.h:75
A minimal way to include Windows.h.