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