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
This commit is contained in:
Chris Bracken
2024-11-11 13:57:04 -08:00
committed by GitHub
parent 6bef907a09
commit b773005a98
2 changed files with 20 additions and 25 deletions

View File

@@ -6,6 +6,7 @@
#define FLUTTER_TESTING_TEST_METAL_CONTEXT_H_
#include <map>
#include <memory>
#include <mutex>
#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<MTLDevice>
void* command_queue_; // id<MTLCommandQueue>
std::unique_ptr<MetalObjCFields> metal_;
sk_sp<GrDirectContext> skia_context_;
std::mutex textures_mutex_;
int64_t texture_id_ctr_ = 1; // guarded by textures_mutex

View File

@@ -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<MTLDevice> device;
id<MTLCommandQueue> command_queue;
};
TestMetalContext::TestMetalContext() {
id<MTLDevice> 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>(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<MTLDevice> device = // NOLINT(clang-analyzer-deadcode.DeadStores)
(__bridge_transfer id<MTLDevice>)device_;
device = nil;
}
if (command_queue_) {
// Release and transfer to ARC-managed pointer.
// TODO(cbracken): https://github.com/flutter/flutter/issues/157942
id<MTLCommandQueue> command_queue = // NOLINT(clang-analyzer-deadcode.DeadStores)
(__bridge_transfer id<MTLCommandQueue>)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<GrDirectContext> TestMetalContext::GetSkiaContext() const {
@@ -98,8 +88,12 @@ TestMetalContext::TextureInfo TestMetalContext::CreateMetalTexture(const SkISize
return {.texture_id = -1, .texture = nullptr};
}
id<MTLDevice> device = (__bridge id<MTLDevice>)GetMetalDevice();
id<MTLTexture> texture = [device newTextureWithDescriptor:texture_descriptor];
if (!metal_) {
FML_CHECK(false) << "Invalid Metal device.";
return {.texture_id = -1, .texture = nullptr};
}
id<MTLTexture> texture = [metal_->device newTextureWithDescriptor:texture_descriptor];
if (!texture) {
FML_CHECK(false) << "Could not create texture from texture descriptor.";
return {.texture_id = -1, .texture = nullptr};