Monado OpenXR Runtime
Loading...
Searching...
No Matches
comp_swapchain.h
Go to the documentation of this file.
1// Copyright 2019-2023, Collabora, Ltd.
2// Copyright 2025-2026, NVIDIA CORPORATION.
3// SPDX-License-Identifier: BSL-1.0
4/*!
5 * @file
6 * @brief Independent swapchain implementation.
7 * @author Jakob Bornecrantz <jakob@collabora.com>
8 * @author Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
9 * @ingroup comp_util
10 */
11
12#pragma once
13
16
17#include "vk/vk_cmd_pool.h"
18
19#include "util/u_threading.h"
20#include "util/u_index_fifo.h"
21
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27
28struct comp_swapchain;
29
30/*!
31 * Callback for implementing own destroy function, should call
32 * @ref comp_swapchain_teardown and is responsible for memory.
33 *
34 * @ingroup comp_util
35 */
37
38/*!
39 * Shared resource(s) and garbage collector for swapchains. The garbage
40 * collector allows to delay the destruction until it's safe to destroy them.
41 * The lifetime of @p pool is handled by the compositor that implements this
42 * struct.
43 *
44 * @ingroup comp_util
45 */
47{
48 //! Thread object for safely destroying swapchain.
50
51 struct vk_cmd_pool pool;
52};
53
54/*!
55 * A single swapchain image, holds the needed state for tracking image usage.
56 *
57 * @ingroup comp_util
58 * @see comp_swapchain
59 */
61{
62 //! Vulkan image handle.
63 VkImage image;
64
65 //! Views used by the renderer and distortion code, for each array layer.
66 struct
67 {
68 VkImageView *alpha;
69 VkImageView *no_alpha;
71 //! The number of array slices in a texture, 1 == regular 2D texture.
72 size_t array_size;
73
74 //! A usage counter, similar to a reference counter.
75 uint32_t use_count;
76
77 //! A condition variable per swapchain image that is notified when @ref use_count count reaches 0.
78 pthread_cond_t use_cond;
79
80 //! A mutex per swapchain image that is used with @ref use_cond.
82};
83
84/*!
85 * A swapchain that is almost a one to one mapping to a OpenXR swapchain.
86 *
87 * Not used by the window backend that uses the comp_target to render to.
88 *
89 * The vk_bundle is owned by the compositor, its the state trackers job to make
90 * sure that compositor lives for as long as the swapchain does and that all
91 * swapchains are destroyed before the compositor is destroyed.
92 *
93 * @ingroup comp_util
94 * @implements xrt_swapchain_native
95 * @see comp_compositor
96 */
98{
99 struct xrt_swapchain_native base;
100
101 struct vk_bundle *vk;
102 struct comp_swapchain_shared *cscs;
103
105
106 /*!
107 * This fifo is used to always give out the oldest image to acquire
108 * image, this should probably be made even smarter.
109 */
111
112 //! Virtual real destroy function.
114};
115
116
117/*
118 *
119 * Helper functions.
120 *
121 */
122
123/*!
124 * Convenience function to convert an xrt_swapchain to a comp_swapchain.
125 *
126 * @ingroup comp_util
127 * @private @memberof comp_swapchain
128 */
129static inline struct comp_swapchain *
130comp_swapchain(struct xrt_swapchain *xsc)
131{
132 return (struct comp_swapchain *)xsc;
133}
134
135
136/*
137 *
138 * 'Exported' parent-class functions.
139 *
140 */
141
142/*!
143 * Helper to init a comp_swachain struct as if it was a create operation,
144 * useful for wrapping comp_swapchain within another struct. Ref-count is
145 * set to zero so the caller need to init it correctly.
146 *
147 * @ingroup comp_util
148 */
152 struct vk_bundle *vk,
153 struct comp_swapchain_shared *cscs,
154 const struct xrt_swapchain_create_info *info,
155 const struct xrt_swapchain_create_properties *xsccp);
156
157/*!
158 * Helper to init a comp_swachain struct as if it was a import operation,
159 * useful for wrapping comp_swapchain within another struct. Ref-count is
160 * set to zero so the caller need to init it correctly.
161 *
162 * @ingroup comp_util
163 */
167 struct vk_bundle *vk,
168 struct comp_swapchain_shared *cscs,
169 const struct xrt_swapchain_create_info *info,
170 struct xrt_image_native *native_images,
171 uint32_t native_image_count);
172
173/*!
174 * De-inits a comp_swapchain, usable for classes sub-classing comp_swapchain.
175 *
176 * @ingroup comp_util
177 */
178void
180
181
182/*
183 *
184 * 'Exported' shared struct functions.
185 *
186 */
187
188/*!
189 * Create the shared struct.
190 *
191 * @ingroup comp_util
192 */
193XRT_CHECK_RESULT xrt_result_t
195
196/*!
197 * Destroy the shared struct.
198 *
199 * @ingroup comp_util
200 */
201void
203
204/*!
205 * Do garbage collection, destroying any resources that has been scheduled for
206 * destruction from other threads.
207 *
208 * @ingroup comp_util
209 */
210void
212
213
214/*
215 *
216 * 'Exported' default implementation.
217 *
218 */
219
220/*!
221 * A compositor function that is implemented in the swapchain code.
222 *
223 * @ingroup comp_util
224 */
227 struct xrt_swapchain_create_properties *xsccp);
228
229/*!
230 * A compositor function that is implemented in the swapchain code.
231 *
232 * @ingroup comp_util
233 */
236 struct comp_swapchain_shared *cscs,
237 const struct xrt_swapchain_create_info *info,
238 const struct xrt_swapchain_create_properties *xsccp,
239 struct xrt_swapchain **out_xsc);
240
241/*!
242 * A compositor function that is implemented in the swapchain code.
243 *
244 * @ingroup comp_util
245 */
248 struct comp_swapchain_shared *cscs,
249 const struct xrt_swapchain_create_info *info,
250 struct xrt_image_native *native_images,
251 uint32_t image_count,
252 struct xrt_swapchain **out_xsc);
253
254
255#ifdef __cplusplus
256}
257#endif
void(* comp_swapchain_destroy_func_t)(struct comp_swapchain *sc)
Callback for implementing own destroy function, should call comp_swapchain_teardown and is responsibl...
Definition comp_swapchain.h:36
void comp_swapchain_shared_garbage_collect(struct comp_swapchain_shared *cscs)
Do garbage collection, destroying any resources that has been scheduled for destruction from other th...
Definition comp_swapchain.c:708
void comp_swapchain_teardown(struct comp_swapchain *sc)
De-inits a comp_swapchain, usable for classes sub-classing comp_swapchain.
Definition comp_swapchain.c:658
xrt_result_t comp_swapchain_create_init(struct comp_swapchain *sc, comp_swapchain_destroy_func_t destroy_func, struct vk_bundle *vk, struct comp_swapchain_shared *cscs, const struct xrt_swapchain_create_info *info, const struct xrt_swapchain_create_properties *xsccp)
Helper to init a comp_swachain struct as if it was a create operation, useful for wrapping comp_swapc...
Definition comp_swapchain.c:568
xrt_result_t comp_swapchain_import(struct vk_bundle *vk, struct comp_swapchain_shared *cscs, const struct xrt_swapchain_create_info *info, struct xrt_image_native *native_images, uint32_t image_count, struct xrt_swapchain **out_xsc)
A compositor function that is implemented in the swapchain code.
Definition comp_swapchain.c:770
XRT_CHECK_RESULT xrt_result_t comp_swapchain_shared_init(struct comp_swapchain_shared *cscs, struct vk_bundle *vk)
Create the shared struct.
Definition comp_swapchain.c:690
void comp_swapchain_shared_destroy(struct comp_swapchain_shared *cscs, struct vk_bundle *vk)
Destroy the shared struct.
Definition comp_swapchain.c:702
xrt_result_t comp_swapchain_create(struct vk_bundle *vk, struct comp_swapchain_shared *cscs, const struct xrt_swapchain_create_info *info, const struct xrt_swapchain_create_properties *xsccp, struct xrt_swapchain **out_xsc)
A compositor function that is implemented in the swapchain code.
Definition comp_swapchain.c:742
xrt_result_t comp_swapchain_import_init(struct comp_swapchain *sc, comp_swapchain_destroy_func_t destroy_func, struct vk_bundle *vk, struct comp_swapchain_shared *cscs, const struct xrt_swapchain_create_info *info, struct xrt_image_native *native_images, uint32_t native_image_count)
Helper to init a comp_swachain struct as if it was a import operation, useful for wrapping comp_swapc...
Definition comp_swapchain.c:613
xrt_result_t comp_swapchain_get_create_properties(const struct xrt_swapchain_create_info *info, struct xrt_swapchain_create_properties *xsccp)
A compositor function that is implemented in the swapchain code.
Definition comp_swapchain.c:725
#define XRT_MAX_SWAPCHAIN_IMAGES
Max swapchain images, artificial limit.
Definition xrt_limits.h:53
enum xrt_result xrt_result_t
Result type used across Monado.
A single swapchain image, holds the needed state for tracking image usage.
Definition comp_swapchain.h:61
struct os_mutex use_mutex
A mutex per swapchain image that is used with use_cond.
Definition comp_swapchain.h:81
pthread_cond_t use_cond
A condition variable per swapchain image that is notified when use_count count reaches 0.
Definition comp_swapchain.h:78
size_t array_size
The number of array slices in a texture, 1 == regular 2D texture.
Definition comp_swapchain.h:72
struct comp_swapchain_image::@115 views
Views used by the renderer and distortion code, for each array layer.
VkImage image
Vulkan image handle.
Definition comp_swapchain.h:63
uint32_t use_count
A usage counter, similar to a reference counter.
Definition comp_swapchain.h:75
Shared resource(s) and garbage collector for swapchains.
Definition comp_swapchain.h:47
struct u_threading_stack destroy_swapchains
Thread object for safely destroying swapchain.
Definition comp_swapchain.h:49
A swapchain that is almost a one to one mapping to a OpenXR swapchain.
Definition comp_swapchain.h:98
struct u_index_fifo fifo
This fifo is used to always give out the oldest image to acquire image, this should probably be made ...
Definition comp_swapchain.h:110
comp_swapchain_destroy_func_t real_destroy
Virtual real destroy function.
Definition comp_swapchain.h:113
A wrapper around a native mutex.
Definition os_threading.h:69
Definition u_worker.c:50
Definition u_index_fifo.h:21
Definition u_threading.h:17
A bundle of Vulkan functions and objects, used by both Compositor and Compositor client code.
Definition vk_helpers.h:82
Small helper to manage lock around a command pool.
Definition vk_cmd_pool.h:33
A single image of a swapchain based on native buffer handles.
Definition xrt_compositor.h:2103
Swapchain creation info.
Definition xrt_compositor.h:883
Struct used to negotiate properties of a swapchain that is created outside of the compositor.
Definition xrt_compositor.h:925
Native compositor swapchain: extends xrt_swapchain with a process-local id and (usually) an xrt_alloc...
Definition xrt_compositor.h:2150
Common swapchain interface/base.
Definition xrt_compositor.h:552
uint32_t image_count
Number of images.
Definition xrt_compositor.h:563
A FIFO for indices.
Slightly higher level thread safe helpers.
Command pool helpers.
Allocation collection.
Include all of the Vulkan headers in one place, and cope with any "messy" includes implied by it.