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"
26#if TARGET_OS_SIMULATOR
27#define IMPELLER_CA_METAL_LAYER_AVAILABLE API_AVAILABLE(macos(10.11), ios(13.0))
29#define IMPELLER_CA_METAL_LAYER_AVAILABLE API_AVAILABLE(macos(10.11), ios(8.0))
36 public std::enable_shared_from_this<ContextMTL> {
38 static std::shared_ptr<ContextMTL>
Create(
39 const std::vector<std::string>& shader_library_paths,
40 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch);
42 static std::shared_ptr<ContextMTL>
Create(
43 const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data,
44 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
45 const std::string& label,
46 std::optional<PixelFormat> pixel_format_override = std::nullopt);
48 static std::shared_ptr<ContextMTL>
Create(
50 id<MTLCommandQueue> command_queue,
51 const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data,
52 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
53 const std::string& label);
88 const std::shared_ptr<const Capabilities>&
GetCapabilities()
const override;
90 void SetCapabilities(
const std::shared_ptr<const Capabilities>& capabilities);
103 std::shared_ptr<GPUTracerMTL> GetGPUTracer()
const;
112 explicit SyncSwitchObserver(
ContextMTL& parent);
113 virtual ~SyncSwitchObserver() =
default;
114 void OnSyncSwitchUpdate(
bool new_value)
override;
120 id<MTLDevice> device_ =
nullptr;
121 id<MTLCommandQueue> command_queue_ =
nullptr;
122 std::shared_ptr<ShaderLibraryMTL> shader_library_;
123 std::shared_ptr<PipelineLibraryMTL> pipeline_library_;
124 std::shared_ptr<SamplerLibrary> sampler_library_;
125 std::shared_ptr<AllocatorMTL> resource_allocator_;
126 std::shared_ptr<const Capabilities> device_capabilities_;
127 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch_;
129 std::shared_ptr<GPUTracerMTL> gpu_tracer_;
132 std::unique_ptr<SyncSwitchObserver> sync_switch_observer_;
133 std::shared_ptr<CommandQueue> command_queue_ip_;
134 bool is_valid_ =
false;
137 id<MTLCommandQueue> command_queue,
138 NSArray<id<MTLLibrary>>* shader_libraries,
139 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
140 std::optional<PixelFormat> pixel_format_override = std::nullopt);
142 std::shared_ptr<CommandBuffer> CreateCommandBufferInQueue(
143 id<MTLCommandQueue>
queue)
const;
145 void FlushTasksAwaitingGPU();
Observes changes to the SyncSwitch.
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
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...
std::shared_ptr< ShaderLibrary > GetShaderLibrary() const override
Returns the library of shaders used to specify the programmable stages of a pipeline.
void StoreTaskForGPU(const std::function< void()> &task) override
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
To do anything rendering related with Impeller, you need a context.
uint32_t uint32_t * format
Dart_NativeFunction function
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...