The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

c/main: Refactor swapchain to be more independent

+240 -156
+28 -16
src/xrt/compositor/main/comp_compositor.c
··· 103 103 */ 104 104 105 105 static xrt_result_t 106 + compositor_swapchain_create(struct xrt_compositor *xc, 107 + const struct xrt_swapchain_create_info *info, 108 + struct xrt_swapchain **out_xsc) 109 + { 110 + struct comp_compositor *c = comp_compositor(xc); 111 + 112 + return comp_swapchain_create(&c->vk, &c->cscgc, info, out_xsc); 113 + } 114 + 115 + static xrt_result_t 116 + compositor_swapchain_import(struct xrt_compositor *xc, 117 + const struct xrt_swapchain_create_info *info, 118 + struct xrt_image_native *native_images, 119 + uint32_t num_images, 120 + struct xrt_swapchain **out_xsc) 121 + { 122 + struct comp_compositor *c = comp_compositor(xc); 123 + 124 + return comp_swapchain_import(&c->vk, &c->cscgc, info, native_images, num_images, out_xsc); 125 + } 126 + 127 + static xrt_result_t 106 128 compositor_begin_session(struct xrt_compositor *xc, enum xrt_view_type type) 107 129 { 108 130 struct comp_compositor *c = comp_compositor(xc); ··· 526 548 COMP_SPEW(c, "LAYER_COMMIT finished drawing at %8.3fms", ns_to_ms(c->last_frame_time_ns)); 527 549 528 550 // Now is a good point to garbage collect. 529 - comp_compositor_garbage_collect(c); 551 + comp_swapchain_garbage_collect(&c->cscgc); 530 552 531 553 return XRT_SUCCESS; 532 554 } ··· 576 598 COMP_DEBUG(c, "COMP_DESTROY"); 577 599 578 600 // Make sure we don't have anything to destroy. 579 - comp_compositor_garbage_collect(c); 601 + comp_swapchain_garbage_collect(&c->cscgc); 580 602 581 603 comp_renderer_destroy(&c->r); 582 604 ··· 611 633 612 634 os_precise_sleeper_deinit(&c->sleeper); 613 635 614 - u_threading_stack_fini(&c->threading.destroy_swapchains); 636 + u_threading_stack_fini(&c->cscgc.destroy_swapchains); 615 637 616 638 free(c); 617 639 } ··· 1401 1423 { 1402 1424 struct comp_compositor *c = U_TYPED_CALLOC(struct comp_compositor); 1403 1425 1404 - c->base.base.create_swapchain = comp_swapchain_create; 1405 - c->base.base.import_swapchain = comp_swapchain_import; 1426 + c->base.base.create_swapchain = compositor_swapchain_create; 1427 + c->base.base.import_swapchain = compositor_swapchain_import; 1406 1428 c->base.base.import_fence = comp_compositor_import_fence; 1407 1429 c->base.base.begin_session = compositor_begin_session; 1408 1430 c->base.base.end_session = compositor_end_session; ··· 1426 1448 c->frame.rendering.id = -1; 1427 1449 c->xdev = xdev; 1428 1450 1429 - u_threading_stack_init(&c->threading.destroy_swapchains); 1451 + u_threading_stack_init(&c->cscgc.destroy_swapchains); 1430 1452 1431 1453 COMP_DEBUG(c, "Doing init %p", (void *)c); 1432 1454 ··· 1583 1605 1584 1606 return comp_multi_create_system_compositor(&c->base, sys_info, out_xsysc); 1585 1607 } 1586 - 1587 - void 1588 - comp_compositor_garbage_collect(struct comp_compositor *c) 1589 - { 1590 - struct comp_swapchain *sc; 1591 - 1592 - while ((sc = u_threading_stack_pop(&c->threading.destroy_swapchains))) { 1593 - comp_swapchain_really_destroy(sc); 1594 - } 1595 - }
+3 -106
src/xrt/compositor/main/comp_compositor.h
··· 1 - // Copyright 2019-2020, Collabora, Ltd. 1 + // Copyright 2019-2021, Collabora, Ltd. 2 2 // SPDX-License-Identifier: BSL-1.0 3 3 /*! 4 4 * @file ··· 20 20 #include "vk/vk_image_allocator.h" 21 21 22 22 #include "main/comp_settings.h" 23 + #include "main/comp_swapchain.h" 23 24 #include "main/comp_window.h" 24 25 #include "main/comp_renderer.h" 25 26 #include "main/comp_target.h" ··· 39 40 * Structs 40 41 * 41 42 */ 42 - 43 - /*! 44 - * A single swapchain image, holds the needed state for tracking image usage. 45 - * 46 - * @ingroup comp_main 47 - * @see comp_swapchain 48 - */ 49 - struct comp_swapchain_image 50 - { 51 - //! Sampler used by the renderer and distortion code. 52 - VkSampler sampler; 53 - VkSampler repeat_sampler; 54 - //! Views used by the renderer and distortion code, for each array 55 - //! layer. 56 - struct 57 - { 58 - VkImageView *alpha; 59 - VkImageView *no_alpha; 60 - } views; 61 - //! The number of array slices in a texture, 1 == regular 2D texture. 62 - size_t array_size; 63 - }; 64 - 65 - /*! 66 - * A swapchain that is almost a one to one mapping to a OpenXR swapchain. 67 - * 68 - * Not used by the window backend that uses the vk_swapchain to render to. 69 - * 70 - * @ingroup comp_main 71 - * @implements xrt_swapchain_native 72 - * @see comp_compositor 73 - */ 74 - struct comp_swapchain 75 - { 76 - struct xrt_swapchain_native base; 77 - 78 - struct comp_compositor *c; 79 - 80 - struct vk_image_collection vkic; 81 - struct comp_swapchain_image images[XRT_MAX_SWAPCHAIN_IMAGES]; 82 - 83 - /*! 84 - * This fifo is used to always give out the oldest image to acquire 85 - * image, this should probably be made even smarter. 86 - */ 87 - struct u_index_fifo fifo; 88 - }; 89 43 90 44 /*! 91 45 * A single layer. ··· 221 175 struct comp_frame rendering; 222 176 } frame; 223 177 224 - struct 225 - { 226 - //! Thread object for safely destroying swapchain. 227 - struct u_threading_stack destroy_swapchains; 228 - } threading; 229 - 178 + struct comp_swapchain_gc cscgc; 230 179 231 180 struct 232 181 { ··· 251 200 comp_is_format_supported(struct comp_compositor *c, VkFormat format); 252 201 253 202 /*! 254 - * Convenience function to convert a xrt_swapchain to a comp_swapchain. 255 - * 256 - * @private @memberof comp_swapchain 257 - */ 258 - static inline struct comp_swapchain * 259 - comp_swapchain(struct xrt_swapchain *xsc) 260 - { 261 - return (struct comp_swapchain *)xsc; 262 - } 263 - 264 - /*! 265 203 * Convenience function to convert a xrt_compositor to a comp_compositor. 266 204 * 267 205 * @private @memberof comp_compositor ··· 271 209 { 272 210 return (struct comp_compositor *)xc; 273 211 } 274 - 275 - /*! 276 - * Do garbage collection, destroying any resources that has been scheduled for 277 - * destruction from other threads. 278 - * 279 - * @public @memberof comp_compositor 280 - */ 281 - void 282 - comp_compositor_garbage_collect(struct comp_compositor *c); 283 - 284 - /*! 285 - * A compositor function that is implemented in the swapchain code. 286 - * 287 - * @public @memberof comp_compositor 288 - */ 289 - xrt_result_t 290 - comp_swapchain_create(struct xrt_compositor *xc, 291 - const struct xrt_swapchain_create_info *info, 292 - struct xrt_swapchain **out_xsc); 293 - 294 - /*! 295 - * A compositor function that is implemented in the swapchain code. 296 - * 297 - * @public @memberof comp_compositor 298 - */ 299 - xrt_result_t 300 - comp_swapchain_import(struct xrt_compositor *xc, 301 - const struct xrt_swapchain_create_info *info, 302 - struct xrt_image_native *native_images, 303 - uint32_t num_images, 304 - struct xrt_swapchain **out_xsc); 305 - 306 - /*! 307 - * Swapchain destruct is delayed until it is safe to destroy them, this function 308 - * does the actual destruction and is called from @ref 309 - * comp_compositor_garbage_collect. 310 - * 311 - * @private @memberof comp_swapchain 312 - */ 313 - void 314 - comp_swapchain_really_destroy(struct comp_swapchain *sc); 315 212 316 213 /*! 317 214 * For importing fences, defined in comp_sync.c .
+54 -34
src/xrt/compositor/main/comp_swapchain.c
··· 2 2 // SPDX-License-Identifier: BSL-1.0 3 3 /*! 4 4 * @file 5 - * @brief Swapchain code for the main compositor. 5 + * @brief Independent swapchain implementation. 6 6 * @author Jakob Bornecrantz <jakob@collabora.com> 7 7 * @ingroup comp_main 8 8 */ 9 + 10 + #include "xrt/xrt_handles.h" 11 + #include "xrt/xrt_config_os.h" 9 12 10 13 #include "util/u_misc.h" 11 14 #include "util/u_handles.h" 12 15 13 - #include "main/comp_compositor.h" 14 - 15 - #include <xrt/xrt_handles.h> 16 - #include <xrt/xrt_config_os.h> 16 + #include "main/comp_swapchain.h" 17 17 18 18 #include <stdio.h> 19 19 #include <stdlib.h> ··· 30 30 { 31 31 struct comp_swapchain *sc = comp_swapchain(xsc); 32 32 33 - COMP_SPEW(sc->c, "DESTROY"); 33 + VK_TRACE(sc->vk, "DESTROY"); 34 34 35 - u_threading_stack_push(&sc->c->threading.destroy_swapchains, sc); 35 + u_threading_stack_push(&sc->gc->destroy_swapchains, sc); 36 36 } 37 37 38 38 static xrt_result_t ··· 40 40 { 41 41 struct comp_swapchain *sc = comp_swapchain(xsc); 42 42 43 - COMP_SPEW(sc->c, "ACQUIRE_IMAGE"); 43 + VK_TRACE(sc->vk, "ACQUIRE_IMAGE"); 44 44 45 45 // Returns negative on empty fifo. 46 46 int res = u_index_fifo_pop(&sc->fifo, out_index); ··· 55 55 { 56 56 struct comp_swapchain *sc = comp_swapchain(xsc); 57 57 58 - COMP_SPEW(sc->c, "WAIT_IMAGE"); 58 + VK_TRACE(sc->vk, "WAIT_IMAGE"); 59 59 return XRT_SUCCESS; 60 60 } 61 61 ··· 64 64 { 65 65 struct comp_swapchain *sc = comp_swapchain(xsc); 66 66 67 - COMP_SPEW(sc->c, "RELEASE_IMAGE"); 67 + VK_TRACE(sc->vk, "RELEASE_IMAGE"); 68 68 69 69 int res = u_index_fifo_push(&sc->fifo, index); 70 70 ··· 89 89 } 90 90 91 91 static struct comp_swapchain * 92 - alloc_and_set_funcs(struct comp_compositor *c, uint32_t num_images) 92 + alloc_and_set_funcs(struct vk_bundle *vk, struct comp_swapchain_gc *cscgc, uint32_t num_images) 93 93 { 94 94 struct comp_swapchain *sc = U_TYPED_CALLOC(struct comp_swapchain); 95 95 sc->base.base.destroy = swapchain_destroy; ··· 97 97 sc->base.base.wait_image = swapchain_wait_image; 98 98 sc->base.base.release_image = swapchain_release_image; 99 99 sc->base.base.num_images = num_images; 100 - sc->c = c; 100 + sc->vk = vk; 101 + sc->gc = cscgc; 101 102 102 103 // Make sure the handles are invalid. 103 104 for (uint32_t i = 0; i < ARRAY_SIZE(sc->base.images); i++) { ··· 128 129 } 129 130 130 131 static void 131 - do_post_create_vulkan_setup(struct comp_compositor *c, 132 + do_post_create_vulkan_setup(struct vk_bundle *vk, 132 133 const struct xrt_swapchain_create_info *info, 133 134 struct comp_swapchain *sc) 134 135 { 135 - struct vk_bundle *vk = &c->vk; 136 136 uint32_t num_images = sc->vkic.num_images; 137 137 VkCommandBuffer cmd_buffer; 138 138 ··· 271 271 D(Sampler, image->repeat_sampler); 272 272 } 273 273 274 + static bool 275 + is_format_supported(struct vk_bundle *vk, VkFormat format) 276 + { 277 + VkFormatProperties prop; 278 + 279 + vk->vkGetPhysicalDeviceFormatProperties(vk->physical_device, format, &prop); 280 + 281 + // This is a fairly crude way of checking support, 282 + // but works well enough. 283 + return prop.optimalTilingFeatures != 0; 284 + } 285 + 286 + 274 287 /* 275 288 * 276 - * Exported functions. 289 + * 'Exported' functions. 277 290 * 278 291 */ 279 292 280 293 xrt_result_t 281 - comp_swapchain_create(struct xrt_compositor *xc, 294 + comp_swapchain_create(struct vk_bundle *vk, 295 + struct comp_swapchain_gc *cscgc, 282 296 const struct xrt_swapchain_create_info *info, 283 297 struct xrt_swapchain **out_xsc) 284 298 { 285 - struct comp_compositor *c = comp_compositor(xc); 286 - struct vk_bundle *vk = &c->vk; 287 299 uint32_t num_images = 3; 288 300 VkResult ret; 289 301 290 - if (!comp_is_format_supported(c, info->format)) { 302 + if (!is_format_supported(vk, info->format)) { 291 303 return XRT_ERROR_SWAPCHAIN_FORMAT_UNSUPPORTED; 292 304 } 293 305 294 306 if ((info->create & XRT_SWAPCHAIN_CREATE_PROTECTED_CONTENT) != 0) { 295 - // This compositor doesn't support creating protected content 296 - // swapchains. 307 + // This compositor doesn't support creating protected content swapchains. 297 308 return XRT_ERROR_SWAPCHAIN_FLAG_VALID_BUT_UNSUPPORTED; 298 309 } 299 310 ··· 301 312 num_images = 1; 302 313 } 303 314 304 - struct comp_swapchain *sc = alloc_and_set_funcs(c, num_images); 315 + struct comp_swapchain *sc = alloc_and_set_funcs(vk, cscgc, num_images); 305 316 306 - COMP_DEBUG(c, "CREATE %p %dx%d %s (%ld)", (void *)sc, // 307 - info->width, info->height, // 308 - vk_color_format_string(info->format), info->format); 317 + VK_DEBUG(vk, "CREATE %p %dx%d %s (%ld)", (void *)sc, // 318 + info->width, info->height, // 319 + vk_color_format_string(info->format), info->format); 309 320 310 321 // Use the image helper to allocate the images. 311 322 ret = vk_ic_allocate(vk, info, num_images, &sc->vkic); ··· 330 341 sc->base.images[i].use_dedicated_allocation = sc->vkic.images[i].use_dedicated_allocation; 331 342 } 332 343 333 - do_post_create_vulkan_setup(c, info, sc); 344 + do_post_create_vulkan_setup(vk, info, sc); 334 345 335 346 // Correctly setup refcounts. 336 347 xrt_swapchain_reference(out_xsc, &sc->base.base); ··· 339 350 } 340 351 341 352 xrt_result_t 342 - comp_swapchain_import(struct xrt_compositor *xc, 353 + comp_swapchain_import(struct vk_bundle *vk, 354 + struct comp_swapchain_gc *cscgc, 343 355 const struct xrt_swapchain_create_info *info, 344 356 struct xrt_image_native *native_images, 345 357 uint32_t num_images, 346 358 struct xrt_swapchain **out_xsc) 347 359 { 348 - struct comp_compositor *c = comp_compositor(xc); 349 - struct vk_bundle *vk = &c->vk; 350 360 VkResult ret; 351 361 352 - struct comp_swapchain *sc = alloc_and_set_funcs(c, num_images); 362 + struct comp_swapchain *sc = alloc_and_set_funcs(vk, cscgc, num_images); 353 363 354 - COMP_DEBUG(c, "CREATE FROM NATIVE %p %dx%d", (void *)sc, info->width, info->height); 364 + VK_DEBUG(vk, "CREATE FROM NATIVE %p %dx%d", (void *)sc, info->width, info->height); 355 365 356 366 // Use the image helper to get the images. 357 367 ret = vk_ic_from_natives(vk, info, native_images, num_images, &sc->vkic); ··· 359 369 return XRT_ERROR_VULKAN; 360 370 } 361 371 362 - do_post_create_vulkan_setup(c, info, sc); 372 + do_post_create_vulkan_setup(vk, info, sc); 363 373 364 374 // Correctly setup refcounts. 365 375 xrt_swapchain_reference(out_xsc, &sc->base.base); ··· 370 380 void 371 381 comp_swapchain_really_destroy(struct comp_swapchain *sc) 372 382 { 373 - struct vk_bundle *vk = &sc->c->vk; 383 + struct vk_bundle *vk = sc->vk; 374 384 375 - COMP_SPEW(sc->c, "REALLY DESTROY"); 385 + VK_TRACE(vk, "REALLY DESTROY"); 376 386 377 387 for (uint32_t i = 0; i < sc->base.base.num_images; i++) { 378 388 image_cleanup(vk, &sc->images[i]); ··· 386 396 387 397 free(sc); 388 398 } 399 + 400 + void 401 + comp_swapchain_garbage_collect(struct comp_swapchain_gc *cscgc) 402 + { 403 + struct comp_swapchain *sc; 404 + 405 + while ((sc = u_threading_stack_pop(&cscgc->destroy_swapchains))) { 406 + comp_swapchain_really_destroy(sc); 407 + } 408 + }
+155
src/xrt/compositor/main/comp_swapchain.h
··· 1 + // Copyright 2019-2021, 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_main 9 + */ 10 + 11 + #pragma once 12 + 13 + #include "vk/vk_image_allocator.h" 14 + 15 + #include "util/u_threading.h" 16 + #include "util/u_index_fifo.h" 17 + 18 + 19 + #ifdef __cplusplus 20 + extern "C" { 21 + #endif 22 + 23 + 24 + /*! 25 + * A garbage collector that collects swapchains to be safely destroyed. 26 + */ 27 + struct comp_swapchain_gc 28 + { 29 + //! Thread object for safely destroying swapchain. 30 + struct u_threading_stack destroy_swapchains; 31 + }; 32 + 33 + /*! 34 + * A single swapchain image, holds the needed state for tracking image usage. 35 + * 36 + * @ingroup comp_main 37 + * @see comp_swapchain 38 + */ 39 + struct comp_swapchain_image 40 + { 41 + //! Sampler used by the renderer and distortion code. 42 + VkSampler sampler; 43 + VkSampler repeat_sampler; 44 + //! Views used by the renderer and distortion code, for each array layer. 45 + struct 46 + { 47 + VkImageView *alpha; 48 + VkImageView *no_alpha; 49 + } views; 50 + //! The number of array slices in a texture, 1 == regular 2D texture. 51 + size_t array_size; 52 + }; 53 + 54 + /*! 55 + * A swapchain that is almost a one to one mapping to a OpenXR swapchain. 56 + * 57 + * Not used by the window backend that uses the comp_target to render to. 58 + * 59 + * The vk_bundle is owned by the compositor, its the state trackers job to make 60 + * sure that compositor lives for as long as the swapchain does and that all 61 + * swapchains are destroyed before the compositor is destroyed. 62 + * 63 + * @ingroup comp_main 64 + * @implements xrt_swapchain_native 65 + * @see comp_compositor 66 + */ 67 + struct comp_swapchain 68 + { 69 + struct xrt_swapchain_native base; 70 + 71 + struct vk_bundle *vk; 72 + struct comp_swapchain_gc *gc; 73 + 74 + struct vk_image_collection vkic; 75 + struct comp_swapchain_image images[XRT_MAX_SWAPCHAIN_IMAGES]; 76 + 77 + /*! 78 + * This fifo is used to always give out the oldest image to acquire 79 + * image, this should probably be made even smarter. 80 + */ 81 + struct u_index_fifo fifo; 82 + }; 83 + 84 + 85 + /* 86 + * 87 + * Helper functions. 88 + * 89 + */ 90 + 91 + /*! 92 + * Convenience function to convert a xrt_swapchain to a comp_swapchain. 93 + * 94 + * @private @memberof comp_swapchain 95 + */ 96 + static inline struct comp_swapchain * 97 + comp_swapchain(struct xrt_swapchain *xsc) 98 + { 99 + return (struct comp_swapchain *)xsc; 100 + } 101 + 102 + 103 + /* 104 + * 105 + * 'Exported' functions. 106 + * 107 + */ 108 + 109 + /*! 110 + * Do garbage collection, destroying any resources that has been scheduled for 111 + * destruction from other threads. 112 + * 113 + * @public @memberof comp_compositor 114 + */ 115 + void 116 + comp_swapchain_garbage_collect(struct comp_swapchain_gc *cscgc); 117 + 118 + /*! 119 + * A compositor function that is implemented in the swapchain code. 120 + * 121 + * @public @memberof comp_compositor 122 + */ 123 + xrt_result_t 124 + comp_swapchain_create(struct vk_bundle *vk, 125 + struct comp_swapchain_gc *cscgc, 126 + const struct xrt_swapchain_create_info *info, 127 + struct xrt_swapchain **out_xsc); 128 + 129 + /*! 130 + * A compositor function that is implemented in the swapchain code. 131 + * 132 + * @public @memberof comp_compositor 133 + */ 134 + xrt_result_t 135 + comp_swapchain_import(struct vk_bundle *vk, 136 + struct comp_swapchain_gc *cscgc, 137 + const struct xrt_swapchain_create_info *info, 138 + struct xrt_image_native *native_images, 139 + uint32_t num_images, 140 + struct xrt_swapchain **out_xsc); 141 + 142 + /*! 143 + * Swapchain destruct is delayed until it is safe to destroy them, this function 144 + * does the actual destruction and is called from @ref 145 + * comp_swapchain_garbage_collect. 146 + * 147 + * @private @memberof comp_swapchain 148 + */ 149 + void 150 + comp_swapchain_really_destroy(struct comp_swapchain *sc); 151 + 152 + 153 + #ifdef __cplusplus 154 + } 155 + #endif