9 #include "flutter/fml/concurrent_message_loop.h"
10 #include "flutter/fml/file.h"
11 #include "flutter/fml/logging.h"
12 #include "flutter/fml/paths.h"
13 #include "flutter/fml/synchronization/sync_switch.h"
24 #if FML_OS_IOS_SIMULATOR
26 #else // FML_OS_IOS_SIMULATOR
28 if (@available(macOS 10.15, iOS 13, tvOS 13, *)) {
29 return [device supportsFamily:MTLGPUFamilyApple2];
35 return [device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily2_v1];
39 #endif // FML_OS_IOS_SIMULATOR
43 bool supports_subgroups =
false;
46 if (@available(ios 13.0, tvos 13.0, macos 10.15, *)) {
47 supports_subgroups = [device supportsFamily:MTLGPUFamilyApple7] ||
48 [device supportsFamily:MTLGPUFamilyMac2];
50 return supports_subgroups;
74 ContextMTL::ContextMTL(
76 id<MTLCommandQueue> command_queue,
77 NSArray<id<MTLLibrary>>* shader_libraries,
78 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch)
80 command_queue_(command_queue),
81 is_gpu_disabled_sync_switch_(
std::move(is_gpu_disabled_sync_switch)) {
88 sync_switch_observer_.reset(
new SyncSwitchObserver(*
this));
89 is_gpu_disabled_sync_switch_->AddObserver(sync_switch_observer_.get());
93 if (shader_libraries == nil) {
99 auto library = std::shared_ptr<ShaderLibraryMTL>(
100 new ShaderLibraryMTL(shader_libraries));
101 if (!library->IsValid()) {
105 shader_library_ = std::move(library);
111 std::shared_ptr<PipelineLibraryMTL>(
new PipelineLibraryMTL(device_));
117 std::shared_ptr<SamplerLibraryMTL>(
new SamplerLibraryMTL(device_));
122 resource_allocator_ = std::shared_ptr<AllocatorMTL>(
123 new AllocatorMTL(device_,
"Impeller Permanents Allocator"));
124 if (!resource_allocator_) {
130 device_capabilities_ =
132 command_queue_ip_ = std::make_shared<CommandQueue>();
133 #ifdef IMPELLER_DEBUG
134 gpu_tracer_ = std::make_shared<GPUTracerMTL>();
135 #endif // IMPELLER_DEBUG
140 id<MTLDevice> device,
141 const std::vector<std::string>& libraries_paths) {
142 NSMutableArray<id<MTLLibrary>>* found_libraries = [NSMutableArray array];
143 for (
const auto& library_path : libraries_paths) {
144 if (!fml::IsFile(library_path)) {
146 << library_path <<
"'";
149 NSError* shader_library_error = nil;
150 auto library = [device newLibraryWithFile:@(library_path.c_str())
151 error:&shader_library_error];
153 FML_LOG(ERROR) <<
"Could not create shader library: "
154 << shader_library_error.localizedDescription.UTF8String;
157 [found_libraries addObject:library];
159 return found_libraries;
163 id<MTLDevice> device,
164 const std::vector<std::shared_ptr<fml::Mapping>>& libraries_data,
165 const std::string& label) {
166 NSMutableArray<id<MTLLibrary>>* found_libraries = [NSMutableArray array];
167 for (
const auto& library_data : libraries_data) {
168 if (library_data ==
nullptr) {
169 FML_LOG(ERROR) <<
"Shader library data was null.";
173 __block
auto data = library_data;
176 ::dispatch_data_create(library_data->GetMapping(),
177 library_data->GetSize(),
178 dispatch_get_main_queue(),
184 if (!dispatch_data) {
185 FML_LOG(ERROR) <<
"Could not wrap shader data in dispatch data.";
189 NSError* shader_library_error = nil;
190 auto library = [device newLibraryWithData:dispatch_data
191 error:&shader_library_error];
193 FML_LOG(ERROR) <<
"Could not create shader library: "
194 << shader_library_error.localizedDescription.UTF8String;
197 if (!label.empty()) {
198 library.label = @(label.c_str());
200 [found_libraries addObject:library];
202 return found_libraries;
206 return ::MTLCreateSystemDefaultDevice();
210 auto command_queue = device.newCommandQueue;
211 if (!command_queue) {
215 command_queue.label =
@"Impeller Command Queue";
216 return command_queue;
219 std::shared_ptr<ContextMTL> ContextMTL::Create(
220 const std::vector<std::string>& shader_library_paths,
221 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch) {
224 if (!command_queue) {
227 auto context = std::shared_ptr<ContextMTL>(
new ContextMTL(
228 device, command_queue,
230 std::move(is_gpu_disabled_sync_switch)));
231 if (!context->IsValid()) {
232 FML_LOG(ERROR) <<
"Could not create Metal context.";
238 std::shared_ptr<ContextMTL> ContextMTL::Create(
239 const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data,
240 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
241 const std::string& library_label) {
244 if (!command_queue) {
247 auto context = std::shared_ptr<ContextMTL>(
251 std::move(is_gpu_disabled_sync_switch)));
252 if (!context->IsValid()) {
253 FML_LOG(ERROR) <<
"Could not create Metal context.";
259 std::shared_ptr<ContextMTL> ContextMTL::Create(
260 id<MTLDevice> device,
261 id<MTLCommandQueue> command_queue,
262 const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data,
263 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
264 const std::string& library_label) {
265 auto context = std::shared_ptr<ContextMTL>(
269 std::move(is_gpu_disabled_sync_switch)));
270 if (!context->IsValid()) {
271 FML_LOG(ERROR) <<
"Could not create Metal context.";
277 ContextMTL::~ContextMTL() {
278 is_gpu_disabled_sync_switch_->RemoveObserver(sync_switch_observer_.get());
282 return Context::BackendType::kMetal;
286 std::string ContextMTL::DescribeGpuModel()
const {
287 return std::string([[device_ name] UTF8String]);
291 bool ContextMTL::IsValid()
const {
296 std::shared_ptr<ShaderLibrary> ContextMTL::GetShaderLibrary()
const {
297 return shader_library_;
301 std::shared_ptr<PipelineLibrary> ContextMTL::GetPipelineLibrary()
const {
302 return pipeline_library_;
306 std::shared_ptr<SamplerLibrary> ContextMTL::GetSamplerLibrary()
const {
307 return sampler_library_;
312 return CreateCommandBufferInQueue(command_queue_);
316 void ContextMTL::Shutdown() {}
318 #ifdef IMPELLER_DEBUG
319 std::shared_ptr<GPUTracerMTL> ContextMTL::GetGPUTracer()
const {
322 #endif // IMPELLER_DEBUG
324 std::shared_ptr<const fml::SyncSwitch> ContextMTL::GetIsGpuDisabledSyncSwitch()
326 return is_gpu_disabled_sync_switch_;
329 std::shared_ptr<CommandBuffer> ContextMTL::CreateCommandBufferInQueue(
330 id<MTLCommandQueue> queue)
const {
335 auto buffer = std::shared_ptr<CommandBufferMTL>(
336 new CommandBufferMTL(weak_from_this(), queue));
337 if (!buffer->IsValid()) {
343 std::shared_ptr<Allocator> ContextMTL::GetResourceAllocator()
const {
344 return resource_allocator_;
347 id<MTLDevice> ContextMTL::GetMTLDevice()
const {
351 const std::shared_ptr<const Capabilities>& ContextMTL::GetCapabilities()
const {
352 return device_capabilities_;
355 void ContextMTL::SetCapabilities(
356 const std::shared_ptr<const Capabilities>& capabilities) {
357 device_capabilities_ = capabilities;
361 bool ContextMTL::UpdateOffscreenLayerPixelFormat(
PixelFormat format) {
366 id<MTLCommandBuffer> ContextMTL::CreateMTLCommandBuffer(
367 const std::string& label)
const {
368 auto buffer = [command_queue_ commandBuffer];
369 if (!label.empty()) {
370 [buffer setLabel:@(label.data())];
375 void ContextMTL::StoreTaskForGPU(
const std::function<
void()>& task) {
376 tasks_awaiting_gpu_.emplace_back(task);
377 while (tasks_awaiting_gpu_.size() > kMaxTasksAwaitingGPU) {
378 tasks_awaiting_gpu_.front()();
379 tasks_awaiting_gpu_.pop_front();
383 void ContextMTL::FlushTasksAwaitingGPU() {
384 for (
const auto& task : tasks_awaiting_gpu_) {
387 tasks_awaiting_gpu_.clear();
390 ContextMTL::SyncSwitchObserver::SyncSwitchObserver(ContextMTL& parent)
393 void ContextMTL::SyncSwitchObserver::OnSyncSwitchUpdate(
bool new_is_disabled) {
394 if (!new_is_disabled) {
395 parent_.FlushTasksAwaitingGPU();
401 return command_queue_ip_;