From c6031a26e8795477fc27103567e0b371f6b1a38e Mon Sep 17 00:00:00 2001 From: Jakob Bornecrantz Date: Wed, 3 Nov 2021 14:29:14 +0000 Subject: [PATCH] c/main: Refactor fence to be more independent --- src/xrt/compositor/main/comp_compositor.c | 12 +++++- src/xrt/compositor/main/comp_compositor.h | 9 +--- src/xrt/compositor/main/comp_sync.c | 52 +++++++++++------------ src/xrt/compositor/main/comp_sync.h | 35 +++++++++++++++ 4 files changed, 71 insertions(+), 37 deletions(-) create mode 100644 src/xrt/compositor/main/comp_sync.h diff --git a/src/xrt/compositor/main/comp_compositor.c b/src/xrt/compositor/main/comp_compositor.c index fa4e8ca07..3165b2fae 100644 --- a/src/xrt/compositor/main/comp_compositor.c +++ b/src/xrt/compositor/main/comp_compositor.c @@ -124,6 +124,16 @@ compositor_swapchain_import(struct xrt_compositor *xc, return comp_swapchain_import(&c->vk, &c->cscgc, info, native_images, num_images, out_xsc); } +static xrt_result_t +compositor_import_fence(struct xrt_compositor *xc, + xrt_graphics_sync_handle_t handle, + struct xrt_compositor_fence **out_xcf) +{ + struct comp_compositor *c = comp_compositor(xc); + + return comp_fence_import(&c->vk, handle, out_xcf); +} + static xrt_result_t compositor_begin_session(struct xrt_compositor *xc, enum xrt_view_type type) { @@ -1425,7 +1435,7 @@ xrt_gfx_provider_create_system(struct xrt_device *xdev, struct xrt_system_compos c->base.base.create_swapchain = compositor_swapchain_create; c->base.base.import_swapchain = compositor_swapchain_import; - c->base.base.import_fence = comp_compositor_import_fence; + c->base.base.import_fence = compositor_import_fence; c->base.base.begin_session = compositor_begin_session; c->base.base.end_session = compositor_end_session; c->base.base.predict_frame = compositor_predict_frame; diff --git a/src/xrt/compositor/main/comp_compositor.h b/src/xrt/compositor/main/comp_compositor.h index bc7b599e4..2fa9eb039 100644 --- a/src/xrt/compositor/main/comp_compositor.h +++ b/src/xrt/compositor/main/comp_compositor.h @@ -19,6 +19,7 @@ #include "vk/vk_image_allocator.h" +#include "main/comp_sync.h" #include "main/comp_settings.h" #include "main/comp_swapchain.h" #include "main/comp_window.h" @@ -210,14 +211,6 @@ comp_compositor(struct xrt_compositor *xc) return (struct comp_compositor *)xc; } -/*! - * For importing fences, defined in comp_sync.c . - */ -xrt_result_t -comp_compositor_import_fence(struct xrt_compositor *xc, - xrt_graphics_sync_handle_t handle, - struct xrt_compositor_fence **out_xcf); - /*! * Spew level logging. * diff --git a/src/xrt/compositor/main/comp_sync.c b/src/xrt/compositor/main/comp_sync.c index 2d852d5b4..1f40d82c5 100644 --- a/src/xrt/compositor/main/comp_sync.c +++ b/src/xrt/compositor/main/comp_sync.c @@ -2,18 +2,18 @@ // SPDX-License-Identifier: BSL-1.0 /*! * @file - * @brief Sync code for the main compositor. + * @brief Independent @ref xrt_compositor_fence implementation. * @author Jakob Bornecrantz * @ingroup comp_main */ +#include "xrt/xrt_config_os.h" + +#include "main/comp_sync.h" + #include "util/u_misc.h" #include "util/u_handles.h" - -#include "main/comp_compositor.h" - -#include -#include +#include "util/u_trace_marker.h" #include #include @@ -23,22 +23,23 @@ #endif - -struct fence -{ - struct xrt_compositor_fence base; - struct comp_compositor *c; - - VkFence fence; -}; - - /* * - * Helper functions. + * Structs. * */ +/*! + * A very simple implementation of a fence primitive. + */ +struct fence +{ + struct xrt_compositor_fence base; + + struct vk_bundle *vk; + + VkFence fence; +}; /* @@ -53,7 +54,7 @@ fence_wait(struct xrt_compositor_fence *xcf, uint64_t timeout) COMP_TRACE_MARKER(); struct fence *f = (struct fence *)xcf; - struct vk_bundle *vk = &f->c->vk; + struct vk_bundle *vk = f->vk; // Count no handle as signled fence. if (f->fence == VK_NULL_HANDLE) { @@ -65,7 +66,7 @@ fence_wait(struct xrt_compositor_fence *xcf, uint64_t timeout) return XRT_SUCCESS; } if (ret != VK_SUCCESS) { - COMP_ERROR(f->c, "vkWaitForFences: %s", vk_result_string(ret)); + VK_ERROR(vk, "vkWaitForFences: %s", vk_result_string(ret)); return XRT_ERROR_VULKAN; } @@ -78,7 +79,7 @@ fence_destroy(struct xrt_compositor_fence *xcf) COMP_TRACE_MARKER(); struct fence *f = (struct fence *)xcf; - struct vk_bundle *vk = &f->c->vk; + struct vk_bundle *vk = f->vk; if (f->fence != VK_NULL_HANDLE) { vk->vkDestroyFence(vk->device, f->fence, NULL); @@ -91,20 +92,15 @@ fence_destroy(struct xrt_compositor_fence *xcf) /* * - * Compositor function. + * 'Exported' function. * */ xrt_result_t -comp_compositor_import_fence(struct xrt_compositor *xc, - xrt_graphics_sync_handle_t handle, - struct xrt_compositor_fence **out_xcf) +comp_fence_import(struct vk_bundle *vk, xrt_graphics_sync_handle_t handle, struct xrt_compositor_fence **out_xcf) { COMP_TRACE_MARKER(); - struct comp_compositor *c = comp_compositor(xc); - struct vk_bundle *vk = &c->vk; - VkFence fence = VK_NULL_HANDLE; VkResult ret = vk_create_fence_sync_from_native(vk, handle, &fence); @@ -116,7 +112,7 @@ comp_compositor_import_fence(struct xrt_compositor *xc, f->base.wait = fence_wait; f->base.destroy = fence_destroy; f->fence = fence; - f->c = c; + f->vk = vk; *out_xcf = &f->base; diff --git a/src/xrt/compositor/main/comp_sync.h b/src/xrt/compositor/main/comp_sync.h new file mode 100644 index 000000000..50d0223f7 --- /dev/null +++ b/src/xrt/compositor/main/comp_sync.h @@ -0,0 +1,35 @@ +// Copyright 2019-2021, Collabora, Ltd. +// SPDX-License-Identifier: BSL-1.0 +/*! + * @file + * @brief Independent @ref xrt_compositor_fence implementation. + * @author Jakob Bornecrantz + * @ingroup comp_main + */ + +#pragma once + +#include "xrt/xrt_handles.h" +#include "xrt/xrt_compositor.h" +#include "vk/vk_helpers.h" + + +#ifdef __cplusplus +extern "C" { +#endif + + +/*! + * For importing @ref xrt_graphics_sync_handle_t and turn them into a @ref xrt_compositor_fence. + * + * The vk_bundle is owned by the compositor, its the state trackers job to make + * sure that compositor lives for as long as the fence does and that all fences + * are destroyed before the compositor is destroyed. + */ +xrt_result_t +comp_fence_import(struct vk_bundle *vk, xrt_graphics_sync_handle_t handle, struct xrt_compositor_fence **out_xcf); + + +#ifdef __cplusplus +} +#endif