diff --git a/src/xrt/compositor/client/comp_gl_client.c b/src/xrt/compositor/client/comp_gl_client.c index 4752207e2..365e90141 100644 --- a/src/xrt/compositor/client/comp_gl_client.c +++ b/src/xrt/compositor/client/comp_gl_client.c @@ -249,11 +249,13 @@ vk_format_to_gl(int64_t format) } } -static struct xrt_swapchain * +static xrt_result_t client_gl_swapchain_create(struct xrt_compositor *xc, - struct xrt_swapchain_create_info *info) + struct xrt_swapchain_create_info *info, + struct xrt_swapchain **out_xsc) { struct client_gl_compositor *c = client_gl_compositor(xc); + xrt_result_t xret = XRT_SUCCESS; if (info->array_size > 1) { const char *version_str = (const char *)glGetString(GL_VERSION); @@ -262,25 +264,27 @@ client_gl_swapchain_create(struct xrt_compositor *xc, "%s - only one array layer is supported with " "OpenGL ES 2\n", __func__); - return NULL; + return XRT_ERROR_OPENGL; } } int64_t vk_format = gl_format_to_vk(info->format); if (vk_format == 0) { fprintf(stderr, "%s - Invalid format!\n", __func__); - return NULL; + return XRT_ERROR_VULKAN; } - struct xrt_swapchain_create_info vk_info = *info; - vk_info.format = vk_format; - struct xrt_swapchain_native *xscn = - xrt_comp_native_create_swapchain(c->xcn, &vk_info); + struct xrt_swapchain_create_info xinfo = *info; + xinfo.format = vk_format; + struct xrt_swapchain_native *xscn = NULL; + xret = xrt_comp_native_create_swapchain(c->xcn, &xinfo, &xscn); - if (xscn == NULL) { - return NULL; + if (xret != XRT_SUCCESS) { + return xret; } + assert(xscn != NULL); + struct xrt_swapchain *xsc = &xscn->base; struct client_gl_swapchain *sc = @@ -333,7 +337,8 @@ client_gl_swapchain_create(struct xrt_compositor *xc, : GL_TEXTURE_2D_ARRAY, prev_texture); - return &sc->base.base; + *out_xsc = &sc->base.base; + return XRT_SUCCESS; } static xrt_result_t diff --git a/src/xrt/compositor/client/comp_vk_client.c b/src/xrt/compositor/client/comp_vk_client.c index 1afcec37b..049862a3a 100644 --- a/src/xrt/compositor/client/comp_vk_client.c +++ b/src/xrt/compositor/client/comp_vk_client.c @@ -284,25 +284,29 @@ client_vk_compositor_layer_commit(struct xrt_compositor *xc, int64_t frame_id) return xrt_comp_layer_commit(&c->xcn->base, frame_id); } -static struct xrt_swapchain * +static xrt_result_t client_vk_swapchain_create(struct xrt_compositor *xc, - struct xrt_swapchain_create_info *info) + struct xrt_swapchain_create_info *info, + struct xrt_swapchain **out_xsc) { struct client_vk_compositor *c = client_vk_compositor(xc); VkCommandBuffer cmd_buffer; VkResult ret; + xrt_result_t xret; - struct xrt_swapchain_native *xscn = - xrt_comp_native_create_swapchain(c->xcn, info); + struct xrt_swapchain_native *xscn = NULL; + xret = xrt_comp_native_create_swapchain(c->xcn, info, &xscn); - if (xscn == NULL) { - return NULL; + if (xret != XRT_SUCCESS) { + return xret; } + assert(xscn != NULL); + struct xrt_swapchain *xsc = &xscn->base; ret = vk_init_cmd_buffer(&c->vk, &cmd_buffer); if (ret != VK_SUCCESS) { - return NULL; + return XRT_ERROR_VULKAN; } VkImageSubresourceRange subresource_range = { @@ -331,7 +335,7 @@ client_vk_swapchain_create(struct xrt_compositor *xc, if (ret != VK_SUCCESS) { - return NULL; + return XRT_ERROR_VULKAN; } /* @@ -348,7 +352,7 @@ client_vk_swapchain_create(struct xrt_compositor *xc, ret = vk_submit_cmd_buffer(&c->vk, cmd_buffer); if (ret != VK_SUCCESS) { - return NULL; + return XRT_ERROR_FAILED_TO_SUBMIT_VULKAN_COMMANDS; } // Prerecord command buffers for swapchain image ownership/layout @@ -356,11 +360,11 @@ client_vk_swapchain_create(struct xrt_compositor *xc, for (uint32_t i = 0; i < xsc->num_images; i++) { ret = vk_init_cmd_buffer(&c->vk, &sc->acquire[i]); if (ret != VK_SUCCESS) { - return NULL; + return XRT_ERROR_VULKAN; } ret = vk_init_cmd_buffer(&c->vk, &sc->release[i]); if (ret != VK_SUCCESS) { - return NULL; + return XRT_ERROR_VULKAN; } VkImageSubresourceRange subresource_range = { @@ -425,17 +429,19 @@ client_vk_swapchain_create(struct xrt_compositor *xc, if (ret != VK_SUCCESS) { VK_ERROR(vk, "vkEndCommandBuffer: %s", vk_result_string(ret)); - return NULL; + return XRT_ERROR_VULKAN; } ret = c->vk.vkEndCommandBuffer(sc->release[i]); if (ret != VK_SUCCESS) { VK_ERROR(vk, "vkEndCommandBuffer: %s", vk_result_string(ret)); - return NULL; + return XRT_ERROR_VULKAN; } } - return &sc->base.base; + *out_xsc = &sc->base.base; + + return XRT_SUCCESS; } struct client_vk_compositor * diff --git a/src/xrt/compositor/main/comp_compositor.h b/src/xrt/compositor/main/comp_compositor.h index 429031b2d..914438dd9 100644 --- a/src/xrt/compositor/main/comp_compositor.h +++ b/src/xrt/compositor/main/comp_compositor.h @@ -271,9 +271,10 @@ comp_compositor_garbage_collect(struct comp_compositor *c); * * @public @memberof comp_compositor */ -struct xrt_swapchain * +xrt_result_t comp_swapchain_create(struct xrt_compositor *xc, - struct xrt_swapchain_create_info *info); + struct xrt_swapchain_create_info *info, + struct xrt_swapchain **out_xsc); /*! * Swapchain destruct is delayed until it is safe to destroy them, this function diff --git a/src/xrt/compositor/main/comp_swapchain.c b/src/xrt/compositor/main/comp_swapchain.c index c97457b81..fae681c4c 100644 --- a/src/xrt/compositor/main/comp_swapchain.c +++ b/src/xrt/compositor/main/comp_swapchain.c @@ -221,9 +221,10 @@ err_image: * */ -struct xrt_swapchain * +xrt_result_t comp_swapchain_create(struct xrt_compositor *xc, - struct xrt_swapchain_create_info *info) + struct xrt_swapchain_create_info *info, + struct xrt_swapchain **out_xsc) { struct comp_compositor *c = comp_compositor(xc); VkCommandBuffer cmd_buffer; @@ -259,7 +260,7 @@ comp_swapchain_create(struct xrt_compositor *xc, //! @todo memory leak of image fds and swapchain // see // https://gitlab.freedesktop.org/monado/monado/issues/20 - return NULL; + return XRT_ERROR_VULKAN; } vk_create_sampler(&c->vk, &sc->images[i].sampler); @@ -332,7 +333,8 @@ comp_swapchain_create(struct xrt_compositor *xc, vk_submit_cmd_buffer(&c->vk, cmd_buffer); - return &sc->base.base; + *out_xsc = &sc->base.base; + return XRT_SUCCESS; } static void diff --git a/src/xrt/include/xrt/xrt_compositor.h b/src/xrt/include/xrt/xrt_compositor.h index 6734536ba..c1da13245 100644 --- a/src/xrt/include/xrt/xrt_compositor.h +++ b/src/xrt/include/xrt/xrt_compositor.h @@ -425,8 +425,9 @@ struct xrt_compositor /*! * Create a swapchain with a set of images. */ - struct xrt_swapchain *(*create_swapchain)( - struct xrt_compositor *xc, struct xrt_swapchain_create_info *info); + xrt_result_t (*create_swapchain)(struct xrt_compositor *xc, + struct xrt_swapchain_create_info *info, + struct xrt_swapchain **out_xsc); /*! * Poll events from this compositor. @@ -548,11 +549,12 @@ struct xrt_compositor * * @public @memberof xrt_compositor */ -static inline struct xrt_swapchain * +static inline xrt_result_t xrt_comp_create_swapchain(struct xrt_compositor *xc, - struct xrt_swapchain_create_info *info) + struct xrt_swapchain_create_info *info, + struct xrt_swapchain **out_xsc) { - return xc->create_swapchain(xc, info); + return xc->create_swapchain(xc, info, out_xsc); } /*! @@ -916,12 +918,17 @@ struct xrt_compositor_native * * @public @memberof xrt_compositor_native */ -static inline struct xrt_swapchain_native * +static inline xrt_result_t xrt_comp_native_create_swapchain(struct xrt_compositor_native *xcn, - struct xrt_swapchain_create_info *info) + struct xrt_swapchain_create_info *info, + struct xrt_swapchain_native **out_xscn) { - struct xrt_swapchain *xsc = xrt_comp_create_swapchain(&xcn->base, info); - return (struct xrt_swapchain_native *)xsc; + struct xrt_swapchain *xsc = NULL; + xrt_result_t ret = xrt_comp_create_swapchain(&xcn->base, info, &xsc); + if (ret == XRT_SUCCESS) { + *out_xscn = (struct xrt_swapchain_native *)xsc; + } + return ret; } /*! diff --git a/src/xrt/include/xrt/xrt_results.h b/src/xrt/include/xrt/xrt_results.h index 6818f935a..699636d1e 100644 --- a/src/xrt/include/xrt/xrt_results.h +++ b/src/xrt/include/xrt/xrt_results.h @@ -14,5 +14,7 @@ typedef enum xrt_result XRT_SUCCESS = 0, XRT_ERROR_IPC_FAILURE = -1, XRT_ERROR_NO_IMAGE_AVAILABLE = -2, - XRT_ERROR_FAILED_TO_SUBMIT_VULKAN_COMMANDS = -3, + XRT_ERROR_VULKAN = -3, + XRT_ERROR_OPENGL = -4, + XRT_ERROR_FAILED_TO_SUBMIT_VULKAN_COMMANDS = -5, } xrt_result_t; diff --git a/src/xrt/ipc/ipc_client_compositor.c b/src/xrt/ipc/ipc_client_compositor.c index 7c4272b1a..c985d0b5f 100644 --- a/src/xrt/ipc/ipc_client_compositor.c +++ b/src/xrt/ipc/ipc_client_compositor.c @@ -164,9 +164,10 @@ ipc_compositor_swapchain_release_image(struct xrt_swapchain *xsc, * */ -static struct xrt_swapchain * +static xrt_result_t ipc_compositor_swapchain_create(struct xrt_compositor *xc, - struct xrt_swapchain_create_info *info) + struct xrt_swapchain_create_info *info, + struct xrt_swapchain **out_xsc) { struct ipc_client_compositor *icc = ipc_client_compositor(xc); @@ -184,7 +185,7 @@ ipc_compositor_swapchain_create(struct xrt_compositor *xc, remote_fds, // fds IPC_MAX_SWAPCHAIN_FDS); // fds if (r != XRT_SUCCESS) { - return NULL; + return r; } struct ipc_client_swapchain *ics = @@ -202,7 +203,8 @@ ipc_compositor_swapchain_create(struct xrt_compositor *xc, ics->base.images[i].size = size; } - return &ics->base.base; + *out_xsc = &ics->base.base; + return XRT_SUCCESS; } static xrt_result_t diff --git a/src/xrt/ipc/ipc_server_client.c b/src/xrt/ipc/ipc_server_client.c index a862f0505..2df678b28 100644 --- a/src/xrt/ipc/ipc_server_client.c +++ b/src/xrt/ipc/ipc_server_client.c @@ -280,6 +280,8 @@ ipc_handle_swapchain_create(volatile struct ipc_client_state *ics, xrt_graphics_buffer_handle_t *out_handles, uint32_t *out_num_handles) { + xrt_result_t xret = XRT_SUCCESS; + // Our handle is just the index for now. uint32_t index = 0; for (; index < IPC_MAX_CLIENT_SWAPCHAINS; index++) { @@ -293,12 +295,16 @@ ipc_handle_swapchain_create(volatile struct ipc_client_state *ics, return XRT_ERROR_IPC_FAILURE; } + // create the swapchain + struct xrt_swapchain *xsc = NULL; + xret = xrt_comp_create_swapchain(ics->xc, info, &xsc); + if (xret != XRT_SUCCESS) { + return xret; + } + // It's now safe to increment the number of swapchains. ics->num_swapchains++; - // create the swapchain - struct xrt_swapchain *xsc = xrt_comp_create_swapchain(ics->xc, info); - uint32_t num_images = xsc->num_images; IPC_SPEW(ics->server, "IPC: Created swapchain %d\n", index); diff --git a/src/xrt/state_trackers/oxr/oxr_swapchain.c b/src/xrt/state_trackers/oxr/oxr_swapchain.c index 3aaa0bff6..0ea172d27 100644 --- a/src/xrt/state_trackers/oxr/oxr_swapchain.c +++ b/src/xrt/state_trackers/oxr/oxr_swapchain.c @@ -198,6 +198,8 @@ oxr_create_swapchain(struct oxr_logger *log, const XrSwapchainCreateInfo *createInfo, struct oxr_swapchain **out_swapchain) { + xrt_result_t xret = XRT_SUCCESS; + struct xrt_swapchain_create_info info; info.create = convert_create_flags(createInfo->createFlags); info.bits = convert_usage_bits(createInfo->usageFlags); @@ -209,13 +211,13 @@ oxr_create_swapchain(struct oxr_logger *log, info.array_size = createInfo->arraySize; info.mip_count = createInfo->mipCount; - struct xrt_swapchain *xsc = - xrt_comp_create_swapchain(sess->compositor, &info); - - if (xsc == NULL) { + struct xrt_swapchain *xsc = NULL; + xret = xrt_comp_create_swapchain(sess->compositor, &info, &xsc); + if (xret != XRT_SUCCESS) { return oxr_error(log, XR_ERROR_RUNTIME_FAILURE, "Failed to create swapchain"); } + assert(xsc != NULL); struct oxr_swapchain *sc = NULL; OXR_ALLOCATE_HANDLE_OR_RETURN(log, sc, OXR_XR_DEBUG_SWAPCHAIN,