66
77#include < Foundation/Foundation.h>
88
9+ #include " flutter/fml/concurrent_message_loop.h"
910#include " flutter/fml/file.h"
1011#include " flutter/fml/logging.h"
1112#include " flutter/fml/paths.h"
13+ #include " flutter/fml/synchronization/sync_switch.h"
1214#include " impeller/core/sampler_descriptor.h"
1315#include " impeller/renderer/backend/metal/sampler_library_mtl.h"
1416#include " impeller/renderer/capabilities.h"
@@ -67,10 +69,12 @@ static bool DeviceSupportsComputeSubgroups(id<MTLDevice> device) {
6769
6870ContextMTL::ContextMTL (
6971 id <MTLDevice > device,
72+ id <MTLCommandQueue > command_queue,
7073 NSArray <id <MTLLibrary >>* shader_libraries,
7174 std::shared_ptr<fml::ConcurrentTaskRunner> worker_task_runner,
7275 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch)
7376 : device_(device),
77+ command_queue_ (command_queue),
7478 worker_task_runner_(std::move(worker_task_runner)),
7579 is_gpu_disabled_sync_switch_(std::move(is_gpu_disabled_sync_switch)) {
7680 // Validate device.
@@ -96,16 +100,6 @@ static bool DeviceSupportsComputeSubgroups(id<MTLDevice> device) {
96100 shader_library_ = std::move (library);
97101 }
98102
99- // Setup command queue.
100- {
101- command_queue_ = device_.newCommandQueue ;
102- if (!command_queue_) {
103- VALIDATION_LOG << " Could not setup the command queue." ;
104- return ;
105- }
106- command_queue_.label = @" Impeller Command Queue" ;
107- }
108-
109103 // Setup the pipeline library.
110104 {
111105 pipeline_library_ =
@@ -204,13 +198,28 @@ static bool DeviceSupportsComputeSubgroups(id<MTLDevice> device) {
204198 return ::MTLCreateSystemDefaultDevice ();
205199}
206200
201+ static id <MTLCommandQueue > CreateMetalCommandQueue (id <MTLDevice > device) {
202+ auto command_queue = device.newCommandQueue ;
203+ if (!command_queue) {
204+ VALIDATION_LOG << " Could not setup the command queue." ;
205+ return nullptr ;
206+ }
207+ command_queue.label = @" Impeller Command Queue" ;
208+ return command_queue;
209+ }
210+
207211std::shared_ptr<ContextMTL> ContextMTL::Create (
208212 const std::vector<std::string>& shader_library_paths,
209213 std::shared_ptr<fml::ConcurrentTaskRunner> worker_task_runner,
210214 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch) {
211215 auto device = CreateMetalDevice ();
216+ auto command_queue = CreateMetalCommandQueue (device);
217+ if (!command_queue) {
218+ return nullptr ;
219+ }
212220 auto context = std::shared_ptr<ContextMTL>(new ContextMTL (
213- device, MTLShaderLibraryFromFilePaths (device, shader_library_paths),
221+ device, command_queue,
222+ MTLShaderLibraryFromFilePaths (device, shader_library_paths),
214223 std::move (worker_task_runner), std::move (is_gpu_disabled_sync_switch)));
215224 if (!context->IsValid ()) {
216225 FML_LOG (ERROR) << " Could not create Metal context." ;
@@ -223,11 +232,35 @@ static bool DeviceSupportsComputeSubgroups(id<MTLDevice> device) {
223232 const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data,
224233 std::shared_ptr<fml::ConcurrentTaskRunner> worker_task_runner,
225234 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
226- const std::string& label ) {
235+ const std::string& library_label ) {
227236 auto device = CreateMetalDevice ();
237+ auto command_queue = CreateMetalCommandQueue (device);
238+ if (!command_queue) {
239+ return nullptr ;
240+ }
241+ auto context = std::shared_ptr<ContextMTL>(new ContextMTL (
242+ device, command_queue,
243+ MTLShaderLibraryFromFileData (device, shader_libraries_data,
244+ library_label),
245+ std::move (worker_task_runner), std::move (is_gpu_disabled_sync_switch)));
246+ if (!context->IsValid ()) {
247+ FML_LOG (ERROR) << " Could not create Metal context." ;
248+ return nullptr ;
249+ }
250+ return context;
251+ }
252+
253+ std::shared_ptr<ContextMTL> ContextMTL::Create (
254+ id <MTLDevice > device,
255+ id <MTLCommandQueue > command_queue,
256+ const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data,
257+ std::shared_ptr<fml::ConcurrentTaskRunner> worker_task_runner,
258+ std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
259+ const std::string& library_label) {
228260 auto context = std::shared_ptr<ContextMTL>(new ContextMTL (
229- device,
230- MTLShaderLibraryFromFileData (device, shader_libraries_data, label),
261+ device, command_queue,
262+ MTLShaderLibraryFromFileData (device, shader_libraries_data,
263+ library_label),
231264 std::move (worker_task_runner), std::move (is_gpu_disabled_sync_switch)));
232265 if (!context->IsValid ()) {
233266 FML_LOG (ERROR) << " Could not create Metal context." ;
0 commit comments