diff --git a/testing/test_metal_context.h b/testing/test_metal_context.h index 5a12dcd23c229..c0c8f8c71b301 100644 --- a/testing/test_metal_context.h +++ b/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/testing/test_metal_context.mm b/testing/test_metal_context.mm index 370af6fb263e0..38f51569fb208 100644 --- a/testing/test_metal_context.mm +++ b/testing/test_metal_context.mm @@ -17,6 +17,12 @@ 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 @@ "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 @@ 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};