Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

iOS,macOS: Refactor TestMetalContext for ARC #56510

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions testing/test_metal_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
#define FLUTTER_TESTING_TEST_METAL_CONTEXT_H_

#include <map>
#include <memory>
#include <mutex>

#include "third_party/skia/include/gpu/ganesh/GrDirectContext.h"
#include "third_party/skia/include/ports/SkCFObject.h"

namespace flutter {

struct MetalObjCFields;

class TestMetalContext {
public:
struct TextureInfo {
Expand All @@ -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
Expand Down
38 changes: 16 additions & 22 deletions testing/test_metal_context.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

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) {
Expand All @@ -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>(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 {
Expand All @@ -98,8 +88,12 @@
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};
Expand Down