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))
30 #define IMPELLER_CA_METAL_LAYER_AVAILABLE API_AVAILABLE(macos(10.11), ios(8.0))
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;
124 void SetCapabilities(
const std::shared_ptr<const Capabilities>& capabilities);
136 #ifdef IMPELLER_DEBUG
137 std::shared_ptr<GPUTracerMTL> GetGPUTracer()
const;
139 const std::shared_ptr<ImpellerMetalCaptureManager> GetCaptureManager()
const;
144 const fml::closure& failure)
override;
147 class SyncSwitchObserver :
public fml::SyncSwitch::Observer {
149 explicit SyncSwitchObserver(
ContextMTL& parent);
150 virtual ~SyncSwitchObserver() =
default;
151 void OnSyncSwitchUpdate(
bool new_value)
override;
157 struct PendingTasks {
159 fml::closure failure;
162 id<MTLDevice> device_ =
nullptr;
163 id<MTLCommandQueue> command_queue_ =
nullptr;
164 std::shared_ptr<ShaderLibraryMTL> shader_library_;
165 std::shared_ptr<PipelineLibraryMTL> pipeline_library_;
166 std::shared_ptr<SamplerLibrary> sampler_library_;
167 std::shared_ptr<AllocatorMTL> resource_allocator_;
168 std::shared_ptr<const Capabilities> device_capabilities_;
169 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch_;
170 Mutex tasks_awaiting_gpu_mutex_;
171 std::deque<PendingTasks> tasks_awaiting_gpu_ IPLR_GUARDED_BY(
172 tasks_awaiting_gpu_mutex_);
173 std::unique_ptr<SyncSwitchObserver> sync_switch_observer_;
174 std::shared_ptr<CommandQueue> command_queue_ip_;
175 #ifdef IMPELLER_DEBUG
176 std::shared_ptr<GPUTracerMTL> gpu_tracer_;
177 std::shared_ptr<ImpellerMetalCaptureManager> capture_manager_;
179 bool is_valid_ =
false;
182 id<MTLCommandQueue> command_queue,
183 NSArray<id<MTLLibrary>>* shader_libraries,
184 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
185 std::optional<PixelFormat> pixel_format_override = std::nullopt);
187 std::shared_ptr<CommandBuffer> CreateCommandBufferInQueue(
188 id<MTLCommandQueue> queue)
const;
190 void FlushTasksAwaitingGPU();
To do anything rendering related with Impeller, you need a context.
std::shared_ptr< SamplerLibrary > GetSamplerLibrary() const override
Returns the library of combined image samplers used in shaders.
std::shared_ptr< CommandBuffer > CreateCommandBuffer() const override
Create a new command buffer. Command buffers can be used to encode graphics, blit,...
id< MTLCommandBuffer > CreateMTLCommandBuffer(const std::string &label) const
void SetCapabilities(const std::shared_ptr< const Capabilities > &capabilities)
std::shared_ptr< PipelineLibrary > GetPipelineLibrary() const override
Returns the library of pipelines used by render or compute commands.
std::shared_ptr< CommandQueue > GetCommandQueue() const override
Return the graphics queue for submitting command buffers.
const std::shared_ptr< const Capabilities > & GetCapabilities() const override
Get the capabilities of Impeller context. All optionally supported feature of the platform,...
std::string DescribeGpuModel() const override
RuntimeStageBackend GetRuntimeStageBackend() const override
Retrieve the runtime stage for this context type.
BackendType GetBackendType() const override
Get the graphics backend of an Impeller context.
void Shutdown() override
Force all pending asynchronous work to finish. This is achieved by deleting all owned concurrent mess...
void StoreTaskForGPU(const fml::closure &task, const fml::closure &failure) override
std::shared_ptr< ShaderLibrary > GetShaderLibrary() const override
Returns the library of shaders used to specify the programmable stages of a pipeline.
bool UpdateOffscreenLayerPixelFormat(PixelFormat format) override
static std::shared_ptr< ContextMTL > Create(const std::vector< std::string > &shader_library_paths, std::shared_ptr< const fml::SyncSwitch > is_gpu_disabled_sync_switch)
std::shared_ptr< Allocator > GetResourceAllocator() const override
Returns the allocator used to create textures and buffers on the device.
bool IsValid() const override
Determines if a context is valid. If the caller ever receives an invalid context, they must discard i...
id< MTLDevice > GetMTLDevice() const
std::shared_ptr< const fml::SyncSwitch > GetIsGpuDisabledSyncSwitch() const
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...