5 #ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_CONTEXT_MTL_H_
6 #define FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_CONTEXT_MTL_H_
8 #include <Metal/Metal.h>
13 #include "flutter/fml/concurrent_message_loop.h"
14 #include "flutter/fml/synchronization/sync_switch.h"
15 #include "fml/closure.h"
27 #if TARGET_OS_SIMULATOR
28 #define IMPELLER_CA_METAL_LAYER_AVAILABLE API_AVAILABLE(macos(10.11), ios(13.0))
29 #else // TARGET_OS_SIMULATOR
30 #define IMPELLER_CA_METAL_LAYER_AVAILABLE API_AVAILABLE(macos(10.11), ios(8.0))
31 #endif // TARGET_OS_SIMULATOR
58 id<MTLCaptureScope> current_capture_scope_;
59 bool scope_active_ =
false;
67 public std::enable_shared_from_this<ContextMTL> {
69 static std::shared_ptr<ContextMTL>
Create(
70 const std::vector<std::string>& shader_library_paths,
71 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch);
73 static std::shared_ptr<ContextMTL>
Create(
74 const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data,
75 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
76 const std::string& label,
77 std::optional<PixelFormat> pixel_format_override = std::nullopt);
79 static std::shared_ptr<ContextMTL>
Create(
81 id<MTLCommandQueue> command_queue,
82 const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data,
83 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
84 const std::string& label);
119 const std::shared_ptr<const Capabilities>&
GetCapabilities()
const override;
121 void SetCapabilities(
const std::shared_ptr<const Capabilities>& capabilities);
133 #ifdef IMPELLER_DEBUG
134 std::shared_ptr<GPUTracerMTL> GetGPUTracer()
const;
136 const std::shared_ptr<ImpellerMetalCaptureManager> GetCaptureManager()
const;
137 #endif // IMPELLER_DEBUG
141 const fml::closure& failure)
override;
144 class SyncSwitchObserver :
public fml::SyncSwitch::Observer {
146 explicit SyncSwitchObserver(
ContextMTL& parent);
147 virtual ~SyncSwitchObserver() =
default;
148 void OnSyncSwitchUpdate(
bool new_value)
override;
154 struct PendingTasks {
156 fml::closure failure;
159 id<MTLDevice> device_ =
nullptr;
160 id<MTLCommandQueue> command_queue_ =
nullptr;
161 std::shared_ptr<ShaderLibraryMTL> shader_library_;
162 std::shared_ptr<PipelineLibraryMTL> pipeline_library_;
163 std::shared_ptr<SamplerLibrary> sampler_library_;
164 std::shared_ptr<AllocatorMTL> resource_allocator_;
165 std::shared_ptr<const Capabilities> device_capabilities_;
166 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch_;
167 Mutex tasks_awaiting_gpu_mutex_;
168 std::deque<PendingTasks> tasks_awaiting_gpu_ IPLR_GUARDED_BY(
169 tasks_awaiting_gpu_mutex_);
170 std::unique_ptr<SyncSwitchObserver> sync_switch_observer_;
171 std::shared_ptr<CommandQueue> command_queue_ip_;
172 #ifdef IMPELLER_DEBUG
173 std::shared_ptr<GPUTracerMTL> gpu_tracer_;
174 std::shared_ptr<ImpellerMetalCaptureManager> capture_manager_;
175 #endif // IMPELLER_DEBUG
176 bool is_valid_ =
false;
179 id<MTLCommandQueue> command_queue,
180 NSArray<id<MTLLibrary>>* shader_libraries,
181 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
182 std::optional<PixelFormat> pixel_format_override = std::nullopt);
184 std::shared_ptr<CommandBuffer> CreateCommandBufferInQueue(
185 id<MTLCommandQueue> queue)
const;
187 void FlushTasksAwaitingGPU();
196 #endif // FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_CONTEXT_MTL_H_