wsi: Fix Metal WSI CAMetalDrawable leak

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/31238>
This commit is contained in:
Aleksi Sapon 2024-09-18 14:53:10 -04:00 committed by Marge Bot
parent 55260d7729
commit 6967f59906
3 changed files with 19 additions and 15 deletions

View file

@ -252,7 +252,7 @@ wsi_metal_surface_get_present_rectangles(VkIcdSurfaceBase *surface,
struct wsi_metal_image {
struct wsi_image base;
CAMetalDrawable *drawable;
CAMetalDrawableBridged *drawable;
};
struct wsi_metal_swapchain {
@ -302,7 +302,7 @@ wsi_metal_swapchain_acquire_next_image(struct wsi_swapchain *wsi_chain,
while (1) {
/* Try to acquire an drawable. Unfortunately we might block for up to 1 second. */
CAMetalDrawable *drawable = wsi_metal_layer_acquire_drawable(chain->surface->pLayer);
CAMetalDrawableBridged *drawable = wsi_metal_layer_acquire_drawable(chain->surface->pLayer);
if (drawable) {
uint32_t i = (chain->current_image_index++) % chain->base.image_count;
*image_index = i;

View file

@ -12,12 +12,10 @@
#ifdef __OBJC__
@class CAMetalLayer;
@class CAMetalDrawable;
typedef unsigned long NSUInteger;
typedef enum MTLPixelFormat : NSUInteger MTLPixelFormat;
#else
typedef void CAMetalLayer;
typedef void CAMetalDrawable;
typedef enum MTLPixelFormat : unsigned long
{
MTLPixelFormatBGRA8Unorm = 80,
@ -28,6 +26,8 @@ typedef enum MTLPixelFormat : unsigned long
} MTLPixelFormat;
#endif
typedef void CAMetalDrawableBridged;
void
wsi_metal_layer_size(const CAMetalLayer *metal_layer,
uint32_t *width, uint32_t *height);
@ -37,7 +37,7 @@ wsi_metal_layer_configure(const CAMetalLayer *metal_layer,
uint32_t width, uint32_t height, uint32_t image_count,
MTLPixelFormat format, bool enable_opaque, bool enable_immediate);
CAMetalDrawable *
CAMetalDrawableBridged *
wsi_metal_layer_acquire_drawable(const CAMetalLayer *metal_layer);
struct wsi_metal_layer_blit_context;
@ -49,10 +49,12 @@ void
wsi_destroy_metal_layer_blit_context(struct wsi_metal_layer_blit_context *context);
void
wsi_metal_layer_blit_and_present(struct wsi_metal_layer_blit_context *context, CAMetalDrawable **drawable_ptr,
void *buffer, uint32_t width, uint32_t height, uint32_t row_pitch);
wsi_metal_layer_blit_and_present(struct wsi_metal_layer_blit_context *context,
CAMetalDrawableBridged **drawable_ptr, void *buffer,
uint32_t width, uint32_t height, uint32_t row_pitch);
void
wsi_metal_layer_cancel_present(struct wsi_metal_layer_blit_context *context, CAMetalDrawable **drawable_ptr);
wsi_metal_layer_cancel_present(struct wsi_metal_layer_blit_context *context,
CAMetalDrawableBridged **drawable_ptr);
#endif // WSI_COMMON_METAL_LAYER_H

View file

@ -45,12 +45,12 @@ wsi_metal_layer_configure(const CAMetalLayer *metal_layer,
}
}
CAMetalDrawable *
CAMetalDrawableBridged *
wsi_metal_layer_acquire_drawable(const CAMetalLayer *metal_layer)
{
@autoreleasepool {
id<CAMetalDrawable> drawable = [metal_layer nextDrawable];
return (CAMetalDrawable *)drawable;
return (__bridge_retained CAMetalDrawableBridged *)drawable;
}
}
@ -84,11 +84,12 @@ wsi_destroy_metal_layer_blit_context(struct wsi_metal_layer_blit_context *contex
}
void
wsi_metal_layer_blit_and_present(struct wsi_metal_layer_blit_context *context, CAMetalDrawable **drawable_ptr,
void *buffer, uint32_t width, uint32_t height, uint32_t row_pitch)
wsi_metal_layer_blit_and_present(struct wsi_metal_layer_blit_context *context,
CAMetalDrawableBridged **drawable_ptr, void *buffer,
uint32_t width, uint32_t height, uint32_t row_pitch)
{
@autoreleasepool {
id<CAMetalDrawable> drawable = (id<CAMetalDrawable>)*drawable_ptr;
id<CAMetalDrawable> drawable = (__bridge_transfer id<CAMetalDrawable>)*drawable_ptr;
id<MTLCommandBuffer> commandBuffer = [context->commandQueue commandBuffer];
id<MTLBlitCommandEncoder> commandEncoder = [commandBuffer blitCommandEncoder];
@ -117,10 +118,11 @@ wsi_metal_layer_blit_and_present(struct wsi_metal_layer_blit_context *context, C
}
void
wsi_metal_layer_cancel_present(struct wsi_metal_layer_blit_context *context, CAMetalDrawable **drawable_ptr)
wsi_metal_layer_cancel_present(struct wsi_metal_layer_blit_context *context,
CAMetalDrawableBridged **drawable_ptr)
{
@autoreleasepool {
id<CAMetalDrawable> drawable = (id<CAMetalDrawable>)*drawable_ptr;
id<CAMetalDrawable> drawable = (__bridge_transfer id<CAMetalDrawable>)*drawable_ptr;
if (drawable == nil)
return;