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

TestMetalContext: Use ARC-managed Metal types #56717

Merged
merged 1 commit into from
Nov 19, 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
5 changes: 3 additions & 2 deletions shell/platform/embedder/tests/embedder_test_context_metal.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
renderer_config_.type = FlutterRendererType::kMetal;
renderer_config_.metal = {
.struct_size = sizeof(FlutterMetalRendererConfig),
.device = metal_context_->GetMetalDevice(),
.present_command_queue = metal_context_->GetMetalCommandQueue(),
.device = (__bridge FlutterMetalDeviceHandle)metal_context_->GetMetalDevice(),
.present_command_queue =
(__bridge FlutterMetalCommandQueueHandle)metal_context_->GetMetalCommandQueue(),
.get_next_drawable_callback =
[](void* user_data, const FlutterFrameInfo* frame_info) {
return reinterpret_cast<EmbedderTestContextMetal*>(user_data)->GetNextDrawable(
Expand Down
9 changes: 6 additions & 3 deletions testing/test_metal_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <memory>
#include <mutex>

#include <Metal/Metal.h>

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

Expand All @@ -27,9 +29,9 @@ class TestMetalContext {

~TestMetalContext();

void* GetMetalDevice() const;
id<MTLDevice> GetMetalDevice() const;

void* GetMetalCommandQueue() const;
id<MTLCommandQueue> GetMetalCommandQueue() const;

sk_sp<GrDirectContext> GetSkiaContext() const;

Expand All @@ -41,7 +43,8 @@ class TestMetalContext {
TextureInfo GetTextureInfo(int64_t texture_id);

private:
std::unique_ptr<MetalObjCFields> metal_;
id<MTLDevice> device_;
id<MTLCommandQueue> command_queue_;
sk_sp<GrDirectContext> skia_context_;
std::mutex textures_mutex_;
int64_t texture_id_ctr_ = 1; // guarded by textures_mutex
Expand Down
23 changes: 8 additions & 15 deletions testing/test_metal_context.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@

namespace flutter::testing {

// 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 @@ -48,22 +42,21 @@
FML_LOG(ERROR) << "Could not create the GrDirectContext from the Metal Device "
"and command queue.";
}

metal_ = std::make_unique<MetalObjCFields>(MetalObjCFields{device, command_queue});
device_ = device;
command_queue_ = command_queue;
}

TestMetalContext::~TestMetalContext() {
std::scoped_lock lock(textures_mutex_);
textures_.clear();
metal_.reset();
}

void* TestMetalContext::GetMetalDevice() const {
return metal_ ? (__bridge void*)metal_->device : nil;
id<MTLDevice> TestMetalContext::GetMetalDevice() const {
return device_;
}

void* TestMetalContext::GetMetalCommandQueue() const {
return metal_ ? (__bridge void*)metal_->command_queue : nil;
id<MTLCommandQueue> TestMetalContext::GetMetalCommandQueue() const {
return command_queue_;
}

sk_sp<GrDirectContext> TestMetalContext::GetSkiaContext() const {
Expand All @@ -88,12 +81,12 @@
return {.texture_id = -1, .texture = nullptr};
}

if (!metal_) {
if (!device_) {
FML_CHECK(false) << "Invalid Metal device.";
return {.texture_id = -1, .texture = nullptr};
}

id<MTLTexture> texture = [metal_->device newTextureWithDescriptor:texture_descriptor];
id<MTLTexture> texture = [device_ newTextureWithDescriptor:texture_descriptor];
if (!texture) {
FML_CHECK(false) << "Could not create texture from texture descriptor.";
return {.texture_id = -1, .texture = nullptr};
Expand Down