From 21adc3b2856cf0c1fb318af1e28c110f44993ece Mon Sep 17 00:00:00 2001 From: George Wright Date: Wed, 12 Jan 2022 17:46:38 -0800 Subject: [PATCH 1/3] Don't retain the MTLTexture or MTLTextureDescriptor --- testing/test_metal_context.mm | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/testing/test_metal_context.mm b/testing/test_metal_context.mm index d2ea2cb3d1bc3..867359ba0e890 100644 --- a/testing/test_metal_context.mm +++ b/testing/test_metal_context.mm @@ -14,7 +14,7 @@ namespace flutter { TestMetalContext::TestMetalContext() { - auto device = fml::scoped_nsprotocol{[MTLCreateSystemDefaultDevice() retain]}; + auto device = fml::scoped_nsprotocol{MTLCreateSystemDefaultDevice()}; if (!device) { FML_LOG(ERROR) << "Could not acquire Metal device."; return; @@ -42,6 +42,9 @@ TestMetalContext::~TestMetalContext() { std::scoped_lock lock(textures_mutex); + for (auto& [_, texture] : textures_) { + [(id)texture.get() release]; + } textures_.clear(); if (device_) { [(__bridge id)device_ release]; @@ -65,11 +68,11 @@ TestMetalContext::TextureInfo TestMetalContext::CreateMetalTexture(const SkISize& size) { std::scoped_lock lock(textures_mutex); - auto texture_descriptor = fml::scoped_nsobject{ - [[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm - width:size.width() - height:size.height() - mipmapped:NO] retain]}; + auto texture_descriptor = fml::scoped_nsobject{[MTLTextureDescriptor + texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm + width:size.width() + height:size.height() + mipmapped:NO]}; // The most pessimistic option and disables all optimizations but allows tests // the most flexible access to the surface. They may read and write to the @@ -82,8 +85,7 @@ } id device = (__bridge id)GetMetalDevice(); - sk_cfp texture = - sk_cfp{[[device newTextureWithDescriptor:texture_descriptor.get()] retain]}; + sk_cfp texture = sk_cfp{[device newTextureWithDescriptor:texture_descriptor.get()]}; if (!texture) { FML_CHECK(false) << "Could not create texture from texture descriptor."; From 4f673c201e45135b77c324c07e8dd65e4e29284e Mon Sep 17 00:00:00 2001 From: George Wright Date: Thu, 13 Jan 2022 09:58:58 -0800 Subject: [PATCH 2/3] Don't double release --- testing/test_metal_context.mm | 3 --- 1 file changed, 3 deletions(-) diff --git a/testing/test_metal_context.mm b/testing/test_metal_context.mm index 867359ba0e890..1729c8f329f02 100644 --- a/testing/test_metal_context.mm +++ b/testing/test_metal_context.mm @@ -42,9 +42,6 @@ TestMetalContext::~TestMetalContext() { std::scoped_lock lock(textures_mutex); - for (auto& [_, texture] : textures_) { - [(id)texture.get() release]; - } textures_.clear(); if (device_) { [(__bridge id)device_ release]; From 7629cd7a0d97fb682e406b700701cbc20c9b3736 Mon Sep 17 00:00:00 2001 From: George Wright Date: Thu, 13 Jan 2022 13:47:05 -0800 Subject: [PATCH 3/3] MTLTextureDescriptor is already in the autorelease pool, so the retain is still necessary --- testing/test_metal_context.mm | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/testing/test_metal_context.mm b/testing/test_metal_context.mm index 1729c8f329f02..6ac70a2ae25f3 100644 --- a/testing/test_metal_context.mm +++ b/testing/test_metal_context.mm @@ -65,11 +65,11 @@ TestMetalContext::TextureInfo TestMetalContext::CreateMetalTexture(const SkISize& size) { std::scoped_lock lock(textures_mutex); - auto texture_descriptor = fml::scoped_nsobject{[MTLTextureDescriptor - texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm - width:size.width() - height:size.height() - mipmapped:NO]}; + auto texture_descriptor = fml::scoped_nsobject{ + [[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatBGRA8Unorm + width:size.width() + height:size.height() + mipmapped:NO] retain]}; // The most pessimistic option and disables all optimizations but allows tests // the most flexible access to the surface. They may read and write to the