From b773005a98eee71a18d4c206a9e9d118cd14b2ab Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Mon, 11 Nov 2024 13:57:04 -0800 Subject: [PATCH] iOS,macOS: Refactor TestMetalContext for ARC (flutter/engine#56510) `TestMetalContext` cannot include any Objective-C types in its header file since that header is included in pure C++ translation units. Previously, we declared these fields as `void*`, which then requires manual `__bridge_retained` and `__bridge_transfer` casts to perform the necessary retain/relase manipulation while opting the object in and out of ARC management. Instead, we extract these to a struct in the implementation file and allow the fields to be managed by ARC. This is an interim measure consistent with the approach we've taken [elsewhere][1] until we implement the proper long-term solution. The proper long-term solution is to refactor `EmbedderTest` (see: https://github.com/flutter/flutter/issues/157942) and other related code to split out backends into separate translation units, with the Metal backend code in Objective-C++ implementation files, which would then mean the headers could include Objective-C ivars. No changes to tests, since this change introduces no behaviour changes. Issue: https://github.com/flutter/flutter/issues/157942 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../src/flutter/testing/test_metal_context.h | 7 ++-- .../src/flutter/testing/test_metal_context.mm | 38 ++++++++----------- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/engine/src/flutter/testing/test_metal_context.h b/engine/src/flutter/testing/test_metal_context.h index 5a12dcd23c..c0c8f8c71b 100644 --- a/engine/src/flutter/testing/test_metal_context.h +++ b/engine/src/flutter/testing/test_metal_context.h @@ -6,6 +6,7 @@ #define FLUTTER_TESTING_TEST_METAL_CONTEXT_H_ #include +#include #include #include "third_party/skia/include/gpu/ganesh/GrDirectContext.h" @@ -13,6 +14,8 @@ namespace flutter { +struct MetalObjCFields; + class TestMetalContext { public: struct TextureInfo { @@ -38,9 +41,7 @@ class TestMetalContext { TextureInfo GetTextureInfo(int64_t texture_id); private: - // TODO(cbracken): https://github.com/flutter/flutter/issues/157942 - void* device_; // id - void* command_queue_; // id + std::unique_ptr metal_; 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 370af6fb26..38f51569fb 100644 --- a/engine/src/flutter/testing/test_metal_context.mm +++ b/engine/src/flutter/testing/test_metal_context.mm @@ -17,6 +17,12 @@ static_assert(__has_feature(objc_arc), "ARC must be enabled."); namespace flutter { +// TOOD(cbracken): https://github.com/flutter/flutter/issues/157942 +struct MetalObjCFields { + id device; + id command_queue; +}; + TestMetalContext::TestMetalContext() { id device = MTLCreateSystemDefaultDevice(); if (!device) { @@ -43,37 +49,21 @@ TestMetalContext::TestMetalContext() { "and command queue."; } - // 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; + metal_ = std::make_unique(MetalObjCFields{device, command_queue}); } TestMetalContext::~TestMetalContext() { std::scoped_lock lock(textures_mutex_); textures_.clear(); - if (device_) { - // 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_) { - // 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; - } + metal_.reset(); } void* TestMetalContext::GetMetalDevice() const { - return device_; + return metal_ ? (__bridge void*)metal_->device : nil; } void* TestMetalContext::GetMetalCommandQueue() const { - return command_queue_; + return metal_ ? (__bridge void*)metal_->command_queue : nil; } sk_sp TestMetalContext::GetSkiaContext() const { @@ -98,8 +88,12 @@ TestMetalContext::TextureInfo TestMetalContext::CreateMetalTexture(const SkISize return {.texture_id = -1, .texture = nullptr}; } - id device = (__bridge id)GetMetalDevice(); - id texture = [device newTextureWithDescriptor:texture_descriptor]; + if (!metal_) { + FML_CHECK(false) << "Invalid Metal device."; + return {.texture_id = -1, .texture = nullptr}; + } + + id texture = [metal_->device newTextureWithDescriptor:texture_descriptor]; if (!texture) { FML_CHECK(false) << "Could not create texture from texture descriptor."; return {.texture_id = -1, .texture = nullptr};