Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
context_mtl.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_CONTEXT_MTL_H_
6#define FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_CONTEXT_MTL_H_
7
8#include <Metal/Metal.h>
9
10#include <deque>
11#include <string>
12
13#include "flutter/fml/concurrent_message_loop.h"
14#include "flutter/fml/macros.h"
15#include "flutter/fml/synchronization/sync_switch.h"
26
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
32
33namespace impeller {
34
35class ContextMTL final : public Context,
36 public BackendCast<ContextMTL, Context>,
37 public std::enable_shared_from_this<ContextMTL> {
38 public:
39 static std::shared_ptr<ContextMTL> Create(
40 const std::vector<std::string>& shader_library_paths,
41 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch);
42
43 static std::shared_ptr<ContextMTL> Create(
44 const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data,
45 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
46 const std::string& label,
47 std::optional<PixelFormat> pixel_format_override = std::nullopt);
48
49 static std::shared_ptr<ContextMTL> Create(
50 id<MTLDevice> device,
51 id<MTLCommandQueue> command_queue,
52 const std::vector<std::shared_ptr<fml::Mapping>>& shader_libraries_data,
53 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
54 const std::string& label);
55
56 // |Context|
57 ~ContextMTL() override;
58
59 // |Context|
60 BackendType GetBackendType() const override;
61
62 id<MTLDevice> GetMTLDevice() const;
63
64 // |Context|
65 std::string DescribeGpuModel() const override;
66
67 // |Context|
68 bool IsValid() const override;
69
70 // |Context|
71 std::shared_ptr<Allocator> GetResourceAllocator() const override;
72
73 // |Context|
74 std::shared_ptr<ShaderLibrary> GetShaderLibrary() const override;
75
76 // |Context|
77 std::shared_ptr<SamplerLibrary> GetSamplerLibrary() const override;
78
79 // |Context|
80 std::shared_ptr<PipelineLibrary> GetPipelineLibrary() const override;
81
82 // |Context|
83 std::shared_ptr<CommandBuffer> CreateCommandBuffer() const override;
84
85 // |Context|
86 std::shared_ptr<CommandQueue> GetCommandQueue() const override;
87
88 // |Context|
89 const std::shared_ptr<const Capabilities>& GetCapabilities() const override;
90
91 void SetCapabilities(const std::shared_ptr<const Capabilities>& capabilities);
92
93 // |Context|
95
96 // |Context|
97 void Shutdown() override;
98
99 id<MTLCommandBuffer> CreateMTLCommandBuffer(const std::string& label) const;
100
101 std::shared_ptr<const fml::SyncSwitch> GetIsGpuDisabledSyncSwitch() const;
102
103#ifdef IMPELLER_DEBUG
104 std::shared_ptr<GPUTracerMTL> GetGPUTracer() const;
105#endif // IMPELLER_DEBUG
106
107 // |Context|
108 void StoreTaskForGPU(const std::function<void()>& task) override;
109
110 private:
111 class SyncSwitchObserver : public fml::SyncSwitch::Observer {
112 public:
113 explicit SyncSwitchObserver(ContextMTL& parent);
114 virtual ~SyncSwitchObserver() = default;
115 void OnSyncSwitchUpdate(bool new_value) override;
116
117 private:
118 ContextMTL& parent_;
119 };
120
121 id<MTLDevice> device_ = nullptr;
122 id<MTLCommandQueue> command_queue_ = nullptr;
123 std::shared_ptr<ShaderLibraryMTL> shader_library_;
124 std::shared_ptr<PipelineLibraryMTL> pipeline_library_;
125 std::shared_ptr<SamplerLibrary> sampler_library_;
126 std::shared_ptr<AllocatorMTL> resource_allocator_;
127 std::shared_ptr<const Capabilities> device_capabilities_;
128 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch_;
129#ifdef IMPELLER_DEBUG
130 std::shared_ptr<GPUTracerMTL> gpu_tracer_;
131#endif // IMPELLER_DEBUG
132 std::deque<std::function<void()>> tasks_awaiting_gpu_;
133 std::unique_ptr<SyncSwitchObserver> sync_switch_observer_;
134 std::shared_ptr<CommandQueue> command_queue_ip_;
135 bool is_valid_ = false;
136
137 ContextMTL(id<MTLDevice> device,
138 id<MTLCommandQueue> command_queue,
139 NSArray<id<MTLLibrary>>* shader_libraries,
140 std::shared_ptr<const fml::SyncSwitch> is_gpu_disabled_sync_switch,
141 std::optional<PixelFormat> pixel_format_override = std::nullopt);
142
143 std::shared_ptr<CommandBuffer> CreateCommandBufferInQueue(
144 id<MTLCommandQueue> queue) const;
145
146 void FlushTasksAwaitingGPU();
147
148 ContextMTL(const ContextMTL&) = delete;
149
150 ContextMTL& operator=(const ContextMTL&) = delete;
151};
152
153} // namespace impeller
154
155#endif // FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_CONTEXT_MTL_H_
static sk_sp< Effect > Create()
Observes changes to the SyncSwitch.
Definition sync_switch.h:25
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
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.
Definition context.h:46
VkDevice device
Definition main.cc:53
VkQueue queue
Definition main.cc:55
uint32_t uint32_t * format
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition formats.h:100