Monado OpenXR Runtime
Loading...
Searching...
No Matches
xrt_allocation_collection.h
Go to the documentation of this file.
1// Copyright 2025-2026, NVIDIA CORPORATION.
2// SPDX-License-Identifier: BSL-1.0
3/*!
4 * @file
5 * @brief Allocation collection.
6 * @author Jakob Bornecrantz <tbornecrantz@nvidia.com>
7 * @ingroup xrt
8 */
9
10#pragma once
11
12#include "xrt/xrt_compiler.h"
13#include "xrt/xrt_defines.h"
14#include "xrt/xrt_limits.h"
15
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
22
23
24/*!
25 * @addtogroup xrt_iface
26 * @{
27 */
28
29/*!
30 * Allocation collection property, the properties that can be queried with the
31 * @ref xrt_allocation_collection::get_property_raw function.
32 *
33 * Users do not call @ref xrt_allocation_collection::get_property_raw directly,
34 * instead each property has a strongly typed helper function that is noted on
35 * the property. Helpers for properties whose type needs big headers are placed
36 * in their own `xrt_allocation_collection_<suffix>.h` header so users opt-in to
37 * including them.
38 */
40{
41 /*!
42 * Get with @ref xrt_allocation_collection_get_swapchain_info.
43 *
44 * Copies the @ref xrt_swapchain_create_info struct used to create this
45 * allocation collection into the provided pointer. Like this:
46 * ```c
47 * *(struct xrt_swapchain_create_info *)ptr = <the swapchain info>;
48 * ```
49 *
50 * The @p ptr parameter is cast to a pointer to
51 * @ref xrt_swapchain_create_info and the data is copied into it.
52 * The caller owns the copied data and is free to modify it as needed.
53 *
54 * @param ptr Pointer to a @ref xrt_swapchain_create_info to
55 * receive the copy (will be cast from void*).
56 */
58
59 /*!
60 * Get with @ref xrt_allocation_collection_get_vk_device.
61 *
62 * Returns the VkDevice the Vulkan allocations were created on.
63 *
64 * The @p ptr parameter is cast to a pointer to VkDevice and the handle
65 * is written to it. Like this:
66 * ```c
67 * *(VkDevice *)ptr = device;
68 * ```
69 *
70 * Lifetime note:
71 *
72 * The allocation collection does not own the VkDevice, the device
73 * outlives the collection.
74 *
75 * Callers:
76 * - MUST NOT call vkDestroyDevice on the handle.
77 *
78 * @param ptr Pointer to a VkDevice to receive the handle.
79 */
81
82 /*!
83 * Returns the MTLDevice pointer.
84 *
85 * The @p ptr parameter is cast to a pointer to void pointer
86 * (void**) and the MTLDevice pointer is written to it. Like this:
87 * ```c
88 * *(void **)ptr = (__bridge void *)<the MTLDevice pointer>;
89 * ```
90 *
91 * Reference counting note:
92 *
93 * The allocation collection retains ownership of the MTLDevice object.
94 * The returned pointer is only valid as long as the allocation
95 * collection exists.
96 *
97 * Callers:
98 * - MUST NOT call [device release] on this pointer.
99 * - MUST ensure the allocation collection outlives their use of
100 * the device.
101 * - If longer lifetime is needed, caller SHOULD cast back
102 * to id<MTLDevice> and call [device retain], then
103 * [device release] when done.
104 *
105 * @param ptr Pointer to a void* to receive the MTLDevice pointer
106 * (will be cast from void* to void**).
107 */
109};
110
111/*!
112 * Allocation type, the types that the allocation collection can return from the
113 * @ref xrt_allocation_collection::get_all_raw function. Each type has its own
114 * element type and lifetime semantics, which the caller needs to know about.
115 *
116 * Like with @ref xrt_allocation_property each type has a strongly typed helper
117 * function that does the type checking, users do not call
118 * @ref xrt_allocation_collection::get_all_raw directly.
119 */
121{
122 /*!
123 * Get with @ref xrt_allocation_collection_get_vk_images.
124 *
125 * VkImage handle.
126 *
127 * Lifetime note:
128 *
129 * VkImage handles do not have reference counting, the allocation collection
130 * owns the VkImage objects. The returned handles are only valid as long
131 * as the allocation collection exists.
132 *
133 * Callers:
134 * - MUST NOT call vkDestroyImage on these handles.
135 * - MUST ensure the allocation collection outlives their use of
136 * the VkImage handles.
137 */
139
140 /*!
141 * MTLTexture handle (as void pointer).
142 *
143 * Reference counting note:
144 *
145 * The allocation collection retains ownership of the MTLTexture objects.
146 * The returned pointers are only valid as long as the allocation
147 * collection exists.
148 *
149 * Callers:
150 * - MUST NOT call [texture release] on these pointers.
151 * - MUST ensure the allocation collection outlives their use of
152 * the textures.
153 * - If longer lifetime is needed, caller SHOULD cast back
154 * to id<MTLTexture> and call [texture retain], then
155 * [texture release] when done.
156 */
158
159 /*!
160 * IOSurfaceRef handle.
161 *
162 * Reference counting note:
163 *
164 * The allocation collection retains ownership of the IOSurfaceRef objects.
165 * The returned pointers are only valid as long as the allocation
166 * collection exists.
167 *
168 * Callers:
169 * - MUST NOT call CFRelease on these pointers.
170 * - MUST ensure the allocation collection outlives their use of
171 * the surfaces.
172 * - If longer lifetime is needed, caller SHOULD call
173 * CFRetain, then CFRelease when done.
174 */
176
177 /*!
178 * Get with @ref xrt_allocation_collection_get_native_images.
179 *
180 * Native image handle (@ref xrt_image_native).
181 *
182 * Ownership note:
183 *
184 * Unlike other allocation types where the collection retains ownership,
185 * calling get_all with this type transfers ownership of the native
186 * handles to the caller. This is an all-or-nothing operation: if
187 * the call succeeds, the caller is responsible for closing/freeing
188 * all returned handles.
189 *
190 * The exact semantics for freeing depends on the underlying handle
191 * type (e.g., file descriptors on Linux, HANDLE on Windows, AHB on
192 * Android). In general, callers should use the appropriate platform
193 * API or @ref u_graphics_buffer_unref to release the handles.
194 *
195 * Callers:
196 * - MUST close/unref all handles if get_all succeeds.
197 * - SHOULD NOT call get_all with this type multiple times, as each
198 * call may create new handles (implementation dependent).
199 */
201};
202
203/*!
204 * A collection of allocations, these can be images, buffers or general memory.
205 *
206 * This object is meant to be simple, just a container that more complex
207 * objects like @ref xrt_swapchain are built on top of. Its main purpose is to
208 * share common code between those objects, such as sending handles over IPC,
209 * importing into Vulkan and OpenGL, and converting between allocation types.
210 *
211 * The properties of the collection are immutable after creation, unless
212 * otherwise noted. The collection may be "virtual", like the IPC client one
213 * which refers to allocations living in the service.
214 *
215 * Implementations can be fairly high level and may change the layout or even
216 * the backing storage of the memory, what matters is that the objects returned
217 * by @ref xrt_allocation_collection::get_all_raw keep referring to the same
218 * allocations.
219 *
220 * This interface does not synchronize the contents of the allocations, that is
221 * done by wrapper objects such as @ref xrt_swapchain.
222 */
224{
225 /*!
226 * Reference helper.
227 */
229
230 /*!
231 * Number of images in the collection, this is the total number of
232 * images in the collection, not the number of images of a given type.
233 * All images are guaranteed to be of the types supported by the
234 * @p supported_types array.
235 */
236 uint32_t image_count;
237
238 /*!
239 * Supported properties that can be queried from the @p get_property
240 * function. The @p supported_properties_count field is the number of
241 * supported properties.
242 */
243 struct
244 {
245 enum xrt_allocation_property property;
247
248 /*!
249 * Number of supported properties.
250 */
252
253 /*!
254 * Supported allocation types that can be queried from the @p get_all
255 * function. Each types have different element size and semantics,
256 * which the caller needs to know about. The @p supported_types_count
257 * field is the number of supported types.
258 */
259 struct
260 {
261 enum xrt_allocation_type type;
263
264 /*!
265 * Number of supported allocation types.
266 */
268
269 /*!
270 * Get a property from the allocation collection.
271 *
272 * This function is type-erased and internal, users MUST use the typed
273 * helper function noted on each @ref xrt_allocation_property.
274 *
275 * @param xac The allocation collection.
276 * @param prop The property to get.
277 * @param ptr Pointer to write the property value to (cast as needed per property).
278 */
280 enum xrt_allocation_property prop,
281 void *ptr);
282
283 /*!
284 * Get all images of a given type.
285 *
286 * This function is type-erased and internal, users MUST use the typed
287 * helper function noted on each @ref xrt_allocation_type.
288 *
289 * @param xac The allocation collection.
290 * @param type The type of the images to get.
291 * @param ptr The pointer to the images.
292 */
294
295 /*!
296 * Destroy the allocation collection,
297 * called when the reference count reaches zero.
298 */
299 void (*destroy)(struct xrt_allocation_collection *xac);
300};
301
302/*!
303 * Typed helper to get the @ref XRT_ALLOCATION_PROPERTY_SWAPCHAIN_INFO property.
304 *
305 * @param xac The allocation collection.
306 * @param[out] out_info Receives a copy of the swapchain create info.
307 *
308 * @public @memberof xrt_allocation_collection
309 */
310static inline xrt_result_t
316
317/*!
318 * Update the reference counts on allocation collection(s).
319 *
320 * @param[in,out] dst Pointer to a object reference: if the object reference is
321 * non-null will decrement its counter. The reference that
322 * @p dst points to will be set to @p src.
323 * @param[in] src New object for @p dst to refer to (may be null).
324 * If non-null, will have its refcount increased.
325 * @ingroup xrt_iface
326 * @relates xrt_allocation_collection
327 */
328static inline void
330{
331 struct xrt_allocation_collection *old_dst = *dst;
332
333 if (old_dst == src) {
334 return;
335 }
336
337 if (src) {
338 xrt_reference_inc(&src->reference);
339 }
340
341 *dst = src;
342
343 if (old_dst) {
344 if (xrt_reference_dec_and_is_zero(&old_dst->reference)) {
345 old_dst->destroy(old_dst);
346 }
347 }
348}
349
350/*!
351 * @}
352 */
353
354#ifdef __cplusplus
355}
356#endif
#define XRT_MAX_ALLOCATION_TYPE_COUNT
Max number of allocation types that an allocation collection can support.
Definition xrt_limits.h:84
xrt_allocation_property
Allocation collection property, the properties that can be queried with the xrt_allocation_collection...
Definition xrt_allocation_collection.h:40
static void xrt_allocation_collection_reference(struct xrt_allocation_collection **dst, struct xrt_allocation_collection *src)
Update the reference counts on allocation collection(s).
Definition xrt_allocation_collection.h:329
enum xrt_result xrt_result_t
Result type used across Monado.
static xrt_result_t xrt_allocation_collection_get_swapchain_info(struct xrt_allocation_collection *xac, struct xrt_swapchain_create_info *out_info)
Typed helper to get the XRT_ALLOCATION_PROPERTY_SWAPCHAIN_INFO property.
Definition xrt_allocation_collection.h:311
#define XRT_MAX_ALLOCATION_PROPERTY_COUNT
Max number of allocation properties that can be reported by the allocation collection in xrt_allocati...
Definition xrt_limits.h:79
xrt_allocation_type
Allocation type, the types that the allocation collection can return from the xrt_allocation_collecti...
Definition xrt_allocation_collection.h:121
@ XRT_ALLOCATION_PROPERTY_VK_DEVICE
Get with xrt_allocation_collection_get_vk_device.
Definition xrt_allocation_collection.h:80
@ XRT_ALLOCATION_PROPERTY_METAL_DEVICE
Returns the MTLDevice pointer.
Definition xrt_allocation_collection.h:108
@ XRT_ALLOCATION_PROPERTY_SWAPCHAIN_INFO
Get with xrt_allocation_collection_get_swapchain_info.
Definition xrt_allocation_collection.h:57
@ XRT_ALLOCATION_TYPE_NATIVE_IMAGE
Get with xrt_allocation_collection_get_native_images.
Definition xrt_allocation_collection.h:200
@ XRT_ALLOCATION_TYPE_METAL_TEXTURE
MTLTexture handle (as void pointer).
Definition xrt_allocation_collection.h:157
@ XRT_ALLOCATION_TYPE_IOSURFACE
IOSurfaceRef handle.
Definition xrt_allocation_collection.h:175
@ XRT_ALLOCATION_TYPE_VULKAN_IMAGE
Get with xrt_allocation_collection_get_vk_images.
Definition xrt_allocation_collection.h:138
A collection of allocations, these can be images, buffers or general memory.
Definition xrt_allocation_collection.h:224
void(* destroy)(struct xrt_allocation_collection *xac)
Destroy the allocation collection, called when the reference count reaches zero.
Definition xrt_allocation_collection.h:299
uint32_t image_count
Number of images in the collection, this is the total number of images in the collection,...
Definition xrt_allocation_collection.h:236
xrt_result_t(* get_property_raw)(struct xrt_allocation_collection *xac, enum xrt_allocation_property prop, void *ptr)
Get a property from the allocation collection.
Definition xrt_allocation_collection.h:279
uint32_t supported_types_count
Number of supported allocation types.
Definition xrt_allocation_collection.h:267
struct xrt_allocation_collection::@241 supported_types[XRT_MAX_ALLOCATION_TYPE_COUNT]
Supported allocation types that can be queried from the get_all function.
xrt_result_t(* get_all_raw)(struct xrt_allocation_collection *xac, enum xrt_allocation_type type, void *ptr)
Get all images of a given type.
Definition xrt_allocation_collection.h:293
uint32_t supported_properties_count
Number of supported properties.
Definition xrt_allocation_collection.h:251
struct xrt_reference reference
Reference helper.
Definition xrt_allocation_collection.h:228
struct xrt_allocation_collection::@240 supported_properties[XRT_MAX_ALLOCATION_PROPERTY_COUNT]
Supported properties that can be queried from the get_property function.
A base class for reference counted objects.
Definition xrt_defines.h:99
Swapchain creation info.
Definition xrt_compositor.h:883
Header holding common defines.
Common defines and enums for XRT.
Header for limits of the XRT interfaces.