diff --git a/engine/src/flutter/testing/BUILD.gn b/engine/src/flutter/testing/BUILD.gn index 2272389273..54bdcf4000 100644 --- a/engine/src/flutter/testing/BUILD.gn +++ b/engine/src/flutter/testing/BUILD.gn @@ -211,6 +211,9 @@ if (is_mac || is_ios) { } } + cflags_objc = flutter_cflags_objc_arc + cflags_objcc = flutter_cflags_objcc_arc + testonly = true } } diff --git a/engine/src/flutter/testing/test_metal_context.h b/engine/src/flutter/testing/test_metal_context.h index 19e6196332..5a12dcd23c 100644 --- a/engine/src/flutter/testing/test_metal_context.h +++ b/engine/src/flutter/testing/test_metal_context.h @@ -38,8 +38,9 @@ class TestMetalContext { TextureInfo GetTextureInfo(int64_t texture_id); private: - void* device_; - void* command_queue_; + // TODO(cbracken): https://github.com/flutter/flutter/issues/157942 + void* device_; // id + void* command_queue_; // id sk_sp skia_context_; std::mutex textures_mutex_; int64_t texture_id_ctr_ = 1; // guarded by textures_mutex diff --git a/engine/src/flutter/testing/test_metal_context.mm b/engine/src/flutter/testing/test_metal_context.mm index 9b5d59c070..156a3f82b1 100644 --- a/engine/src/flutter/testing/test_metal_context.mm +++ b/engine/src/flutter/testing/test_metal_context.mm @@ -14,46 +14,58 @@ #include "third_party/skia/include/gpu/ganesh/mtl/GrMtlBackendContext.h" #include "third_party/skia/include/gpu/ganesh/mtl/GrMtlDirectContext.h" +static_assert(__has_feature(objc_arc), "ARC must be enabled."); + namespace flutter { TestMetalContext::TestMetalContext() { - auto device = fml::scoped_nsprotocol{MTLCreateSystemDefaultDevice()}; + id device = MTLCreateSystemDefaultDevice(); if (!device) { FML_LOG(ERROR) << "Could not acquire Metal device."; return; } - auto command_queue = fml::scoped_nsobject{[device.get() newCommandQueue]}; + id command_queue = [device newCommandQueue]; if (!command_queue) { FML_LOG(ERROR) << "Could not create the default command queue."; return; } - [command_queue.get() setLabel:@"Flutter Test Queue"]; + [command_queue setLabel:@"Flutter Test Queue"]; GrMtlBackendContext backendContext = {}; // Skia expect arguments to `MakeMetal` transfer ownership of the reference in for release later // when the GrDirectContext is collected. - backendContext.fDevice.reset([device.get() retain]); - backendContext.fQueue.reset([command_queue.get() retain]); + backendContext.fDevice.retain((__bridge GrMTLHandle)device); + backendContext.fQueue.retain((__bridge GrMTLHandle)command_queue); skia_context_ = GrDirectContexts::MakeMetal(backendContext); if (!skia_context_) { FML_LOG(ERROR) << "Could not create the GrDirectContext from the Metal Device " "and command queue."; } - device_ = [device.get() retain]; - command_queue_ = [command_queue.get() retain]; + // Retain and transfer to non-ARC-managed pointers. + // TODO(cbracken): https://github.com/flutter/flutter/issues/157942 + device_ = (__bridge_retained void*)device; + command_queue_ = (__bridge_retained void*)command_queue; } TestMetalContext::~TestMetalContext() { std::scoped_lock lock(textures_mutex_); textures_.clear(); if (device_) { - [(__bridge id)device_ release]; + // Release and transfer to ARC-managed pointer. + // TODO(cbracken): https://github.com/flutter/flutter/issues/157942 + id device = // NOLINT(clang-analyzer-deadcode.DeadStores) + (__bridge_transfer id)device_; + device = nil; } if (command_queue_) { - [(__bridge id)command_queue_ release]; + // Release and transfer to ARC-managed pointer. + // TODO(cbracken): https://github.com/flutter/flutter/issues/157942 + id command_queue = // NOLINT(clang-analyzer-deadcode.DeadStores) + (__bridge_transfer id)command_queue_; + command_queue = nil; } } @@ -71,16 +83,16 @@ sk_sp TestMetalContext::GetSkiaContext() const { 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]}; + MTLTextureDescriptor* texture_descriptor = + [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 // surface from shaders or use as a pixel view. - texture_descriptor.get().usage = MTLTextureUsageUnknown; + texture_descriptor.usage = MTLTextureUsageUnknown; if (!texture_descriptor) { FML_CHECK(false) << "Invalid texture descriptor."; @@ -88,19 +100,20 @@ TestMetalContext::TextureInfo TestMetalContext::CreateMetalTexture(const SkISize } id device = (__bridge id)GetMetalDevice(); - sk_cfp texture = sk_cfp{[device newTextureWithDescriptor:texture_descriptor.get()]}; - + id texture = [device newTextureWithDescriptor:texture_descriptor]; if (!texture) { FML_CHECK(false) << "Could not create texture from texture descriptor."; return {.texture_id = -1, .texture = nullptr}; } const int64_t texture_id = texture_id_ctr_++; - textures_[texture_id] = texture; + sk_cfp texture_ptr; + texture_ptr.retain((__bridge void*)texture); + textures_[texture_id] = texture_ptr; return { .texture_id = texture_id, - .texture = texture.get(), + .texture = (__bridge void*)texture, }; } diff --git a/engine/src/flutter/testing/test_metal_surface_impl.mm b/engine/src/flutter/testing/test_metal_surface_impl.mm index 23c870bd0e..550001a84c 100644 --- a/engine/src/flutter/testing/test_metal_surface_impl.mm +++ b/engine/src/flutter/testing/test_metal_surface_impl.mm @@ -21,6 +21,8 @@ #include "third_party/skia/include/gpu/ganesh/mtl/GrMtlBackendSurface.h" #include "third_party/skia/include/gpu/ganesh/mtl/GrMtlTypes.h" +static_assert(__has_feature(objc_arc), "ARC must be enabled."); + namespace flutter { void TestMetalSurfaceImpl::Init(const TestMetalContext::TextureInfo& texture_info, @@ -28,7 +30,7 @@ void TestMetalSurfaceImpl::Init(const TestMetalContext::TextureInfo& texture_inf id texture = (__bridge id)texture_info.texture; GrMtlTextureInfo skia_texture_info; - skia_texture_info.fTexture.reset([texture retain]); + skia_texture_info.fTexture.retain((__bridge GrMTLHandle)texture); GrBackendTexture backend_texture = GrBackendTextures::MakeMtl( surface_size.width(), surface_size.height(), skgpu::Mipmapped::kNo, skia_texture_info);