The open source OpenXR runtime
at main 252 lines 6.5 kB view raw
1// Copyright 2019-2023, Collabora, Ltd. 2// SPDX-License-Identifier: BSL-1.0 3/*! 4 * @file 5 * @brief Independent swapchain implementation. 6 * @author Jakob Bornecrantz <jakob@collabora.com> 7 * @author Lubosz Sarnecki <lubosz.sarnecki@collabora.com> 8 * @ingroup comp_util 9 */ 10 11#pragma once 12 13#include "vk/vk_image_allocator.h" 14#include "vk/vk_cmd_pool.h" 15 16#include "util/u_threading.h" 17#include "util/u_index_fifo.h" 18 19 20#ifdef __cplusplus 21extern "C" { 22#endif 23 24 25struct comp_swapchain; 26 27/*! 28 * Callback for implementing own destroy function, should call 29 * @ref comp_swapchain_teardown and is responsible for memory. 30 * 31 * @ingroup comp_util 32 */ 33typedef void (*comp_swapchain_destroy_func_t)(struct comp_swapchain *sc); 34 35/*! 36 * Shared resource(s) and garbage collector for swapchains. The garbage 37 * collector allows to delay the destruction until it's safe to destroy them. 38 * The lifetime of @p pool is handled by the compositor that implements this 39 * struct. 40 * 41 * @ingroup comp_util 42 */ 43struct comp_swapchain_shared 44{ 45 //! Thread object for safely destroying swapchain. 46 struct u_threading_stack destroy_swapchains; 47 48 struct vk_cmd_pool pool; 49}; 50 51/*! 52 * A single swapchain image, holds the needed state for tracking image usage. 53 * 54 * @ingroup comp_util 55 * @see comp_swapchain 56 */ 57struct comp_swapchain_image 58{ 59 //! Views used by the renderer and distortion code, for each array layer. 60 struct 61 { 62 VkImageView *alpha; 63 VkImageView *no_alpha; 64 } views; 65 //! The number of array slices in a texture, 1 == regular 2D texture. 66 size_t array_size; 67 68 //! A usage counter, similar to a reference counter. 69 uint32_t use_count; 70 71 //! A condition variable per swapchain image that is notified when @ref use_count count reaches 0. 72 pthread_cond_t use_cond; 73 74 //! A mutex per swapchain image that is used with @ref use_cond. 75 struct os_mutex use_mutex; 76}; 77 78/*! 79 * A swapchain that is almost a one to one mapping to a OpenXR swapchain. 80 * 81 * Not used by the window backend that uses the comp_target to render to. 82 * 83 * The vk_bundle is owned by the compositor, its the state trackers job to make 84 * sure that compositor lives for as long as the swapchain does and that all 85 * swapchains are destroyed before the compositor is destroyed. 86 * 87 * @ingroup comp_util 88 * @implements xrt_swapchain_native 89 * @see comp_compositor 90 */ 91struct comp_swapchain 92{ 93 struct xrt_swapchain_native base; 94 95 struct vk_bundle *vk; 96 struct comp_swapchain_shared *cscs; 97 98 struct vk_image_collection vkic; 99 struct comp_swapchain_image images[XRT_MAX_SWAPCHAIN_IMAGES]; 100 101 /*! 102 * This fifo is used to always give out the oldest image to acquire 103 * image, this should probably be made even smarter. 104 */ 105 struct u_index_fifo fifo; 106 107 //! Virtual real destroy function. 108 comp_swapchain_destroy_func_t real_destroy; 109}; 110 111 112/* 113 * 114 * Helper functions. 115 * 116 */ 117 118/*! 119 * Convenience function to convert an xrt_swapchain to a comp_swapchain. 120 * 121 * @ingroup comp_util 122 * @private @memberof comp_swapchain 123 */ 124static inline struct comp_swapchain * 125comp_swapchain(struct xrt_swapchain *xsc) 126{ 127 return (struct comp_swapchain *)xsc; 128} 129 130 131/* 132 * 133 * 'Exported' parent-class functions. 134 * 135 */ 136 137/*! 138 * Helper to init a comp_swachain struct as if it was a create operation, 139 * useful for wrapping comp_swapchain within another struct. Ref-count is 140 * set to zero so the caller need to init it correctly. 141 * 142 * @ingroup comp_util 143 */ 144xrt_result_t 145comp_swapchain_create_init(struct comp_swapchain *sc, 146 comp_swapchain_destroy_func_t destroy_func, 147 struct vk_bundle *vk, 148 struct comp_swapchain_shared *cscs, 149 const struct xrt_swapchain_create_info *info, 150 const struct xrt_swapchain_create_properties *xsccp); 151 152/*! 153 * Helper to init a comp_swachain struct as if it was a import operation, 154 * useful for wrapping comp_swapchain within another struct. Ref-count is 155 * set to zero so the caller need to init it correctly. 156 * 157 * @ingroup comp_util 158 */ 159xrt_result_t 160comp_swapchain_import_init(struct comp_swapchain *sc, 161 comp_swapchain_destroy_func_t destroy_func, 162 struct vk_bundle *vk, 163 struct comp_swapchain_shared *cscs, 164 const struct xrt_swapchain_create_info *info, 165 struct xrt_image_native *native_images, 166 uint32_t native_image_count); 167 168/*! 169 * De-inits a comp_swapchain, usable for classes sub-classing comp_swapchain. 170 * 171 * @ingroup comp_util 172 */ 173void 174comp_swapchain_teardown(struct comp_swapchain *sc); 175 176 177/* 178 * 179 * 'Exported' shared struct functions. 180 * 181 */ 182 183/*! 184 * Create the shared struct. 185 * 186 * @ingroup comp_util 187 */ 188XRT_CHECK_RESULT xrt_result_t 189comp_swapchain_shared_init(struct comp_swapchain_shared *cscs, struct vk_bundle *vk); 190 191/*! 192 * Destroy the shared struct. 193 * 194 * @ingroup comp_util 195 */ 196void 197comp_swapchain_shared_destroy(struct comp_swapchain_shared *cscs, struct vk_bundle *vk); 198 199/*! 200 * Do garbage collection, destroying any resources that has been scheduled for 201 * destruction from other threads. 202 * 203 * @ingroup comp_util 204 */ 205void 206comp_swapchain_shared_garbage_collect(struct comp_swapchain_shared *cscs); 207 208 209/* 210 * 211 * 'Exported' default implementation. 212 * 213 */ 214 215/*! 216 * A compositor function that is implemented in the swapchain code. 217 * 218 * @ingroup comp_util 219 */ 220xrt_result_t 221comp_swapchain_get_create_properties(const struct xrt_swapchain_create_info *info, 222 struct xrt_swapchain_create_properties *xsccp); 223 224/*! 225 * A compositor function that is implemented in the swapchain code. 226 * 227 * @ingroup comp_util 228 */ 229xrt_result_t 230comp_swapchain_create(struct vk_bundle *vk, 231 struct comp_swapchain_shared *cscs, 232 const struct xrt_swapchain_create_info *info, 233 const struct xrt_swapchain_create_properties *xsccp, 234 struct xrt_swapchain **out_xsc); 235 236/*! 237 * A compositor function that is implemented in the swapchain code. 238 * 239 * @ingroup comp_util 240 */ 241xrt_result_t 242comp_swapchain_import(struct vk_bundle *vk, 243 struct comp_swapchain_shared *cscs, 244 const struct xrt_swapchain_create_info *info, 245 struct xrt_image_native *native_images, 246 uint32_t image_count, 247 struct xrt_swapchain **out_xsc); 248 249 250#ifdef __cplusplus 251} 252#endif