| Flutter Impeller |

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"
16 #include "impeller/base/backend_cast.h"
17 #include "impeller/core/sampler.h"
18 #include "impeller/renderer/backend/metal/allocator_mtl.h"
19 #include "impeller/renderer/backend/metal/command_buffer_mtl.h"
20 #include "impeller/renderer/backend/metal/gpu_tracer_mtl.h"
21 #include "impeller/renderer/backend/metal/pipeline_library_mtl.h"
22 #include "impeller/renderer/backend/metal/shader_library_mtl.h"
23 #include "impeller/renderer/capabilities.h"
24 #include "impeller/renderer/command_queue.h"
25 #include "impeller/renderer/context.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
33 namespace impeller {
34
35 class 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
48 static std::shared_ptr<ContextMTL> Create(
49 id<MTLDevice> device,
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);
54
55 // |Context|
56 ~ContextMTL() override;
57
58 // |Context|
59 BackendType GetBackendType() const override;
60
61 id<MTLDevice> GetMTLDevice() const;
62
63 // |Context|
64 std::string DescribeGpuModel() const override;
65
66 // |Context|
67 bool IsValid() const override;
68
69 // |Context|
70 std::shared_ptr<Allocator> GetResourceAllocator() const override;
71
72 // |Context|
73 std::shared_ptr<ShaderLibrary> GetShaderLibrary() const override;
74
75 // |Context|
76 std::shared_ptr<SamplerLibrary> GetSamplerLibrary() const override;
77
78 // |Context|
79 std::shared_ptr<PipelineLibrary> GetPipelineLibrary() const override;
80
81 // |Context|
82 std::shared_ptr<CommandBuffer> CreateCommandBuffer() const override;
83
84 // |Context|
85 std::shared_ptr<CommandQueue> GetCommandQueue() const override;
86
87 // |Context|
88 const std::shared_ptr<const Capabilities>& GetCapabilities() const override;
89
90 void SetCapabilities(const std::shared_ptr<const Capabilities>& capabilities);
91
92 // |Context|
93 bool UpdateOffscreenLayerPixelFormat(PixelFormat format) override;
94
95 // |Context|
96 void Shutdown() override;
97
98 id<MTLCommandBuffer> CreateMTLCommandBuffer(const std::string& label) const;
99
100 std::shared_ptr<const fml::SyncSwitch> GetIsGpuDisabledSyncSwitch() const;
101
102 #ifdef IMPELLER_DEBUG
103 std::shared_ptr<GPUTracerMTL> GetGPUTracer() const;
104 #endif // IMPELLER_DEBUG
105
106 // |Context|
107 void StoreTaskForGPU(const std::function<void()>& task) override;
108
109 private:
110 class SyncSwitchObserver : public fml::SyncSwitch::Observer {
111 public:
112 explicit SyncSwitchObserver(ContextMTL& parent);
113 virtual ~SyncSwitchObserver() = default;
114 void OnSyncSwitchUpdate(bool new_value) override;
115
116 private:
117 ContextMTL& parent_;
118 };
119
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_;
128 #ifdef IMPELLER_DEBUG
129 std::shared_ptr<GPUTracerMTL> gpu_tracer_;
130 #endif // IMPELLER_DEBUG
131 std::deque<std::function<void()>> tasks_awaiting_gpu_;
132 std::unique_ptr<SyncSwitchObserver> sync_switch_observer_;
133 std::shared_ptr<CommandQueue> command_queue_ip_;
134 bool is_valid_ = false;
135
136 ContextMTL(
137 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
142 std::shared_ptr<CommandBuffer> CreateCommandBufferInQueue(
143 id<MTLCommandQueue> queue) const;
144
145 void FlushTasksAwaitingGPU();
146
147 ContextMTL(const ContextMTL&) = delete;
148
149 ContextMTL& operator=(const ContextMTL&) = delete;
150 };
151
152 } // namespace impeller
153
154 #endif // FLUTTER_IMPELLER_RENDERER_BACKEND_METAL_CONTEXT_MTL_H_
impeller::ContextMTL::GetIsGpuDisabledSyncSwitch
std::shared_ptr< const fml::SyncSwitch > GetIsGpuDisabledSyncSwitch() const
Definition: context_mtl.mm:324
impeller::Context::BackendType
BackendType
Definition: context.h:48
command_buffer_mtl.h
command_queue.h
impeller::ContextMTL::GetSamplerLibrary
std::shared_ptr< SamplerLibrary > GetSamplerLibrary() const override
Returns the library of combined image samplers used in shaders.
Definition: context_mtl.mm:306
allocator_mtl.h
impeller::ContextMTL::GetBackendType
BackendType GetBackendType() const override
Get the graphics backend of an Impeller context.
Definition: context_mtl.mm:281
impeller::ContextMTL::Create
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)
Definition: context_mtl.mm:219
sampler.h
impeller::PixelFormat
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:100
impeller::ContextMTL::GetResourceAllocator
std::shared_ptr< Allocator > GetResourceAllocator() const override
Returns the allocator used to create textures and buffers on the device.
Definition: context_mtl.mm:343
impeller::ContextMTL::SetCapabilities
void SetCapabilities(const std::shared_ptr< const Capabilities > &capabilities)
Definition: context_mtl.mm:355
shader_library_mtl.h
impeller::ContextMTL::UpdateOffscreenLayerPixelFormat
bool UpdateOffscreenLayerPixelFormat(PixelFormat format) override
Definition: context_mtl.mm:361
backend_cast.h
capabilities.h
impeller::ContextMTL::Shutdown
void Shutdown() override
Force all pending asynchronous work to finish. This is achieved by deleting all owned concurrent mess...
Definition: context_mtl.mm:316
impeller::ContextMTL::IsValid
bool IsValid() const override
Determines if a context is valid. If the caller ever receives an invalid context, they must discard i...
Definition: context_mtl.mm:291
pipeline_library_mtl.h
impeller::ContextMTL::GetMTLDevice
id< MTLDevice > GetMTLDevice() const
Definition: context_mtl.mm:347
impeller::Context
To do anything rendering related with Impeller, you need a context.
Definition: context.h:46
impeller::BackendCast
Definition: backend_cast.h:11
impeller::ContextMTL::StoreTaskForGPU
void StoreTaskForGPU(const std::function< void()> &task) override
Definition: context_mtl.mm:375
impeller::ContextMTL::GetPipelineLibrary
std::shared_ptr< PipelineLibrary > GetPipelineLibrary() const override
Returns the library of pipelines used by render or compute commands.
Definition: context_mtl.mm:301
impeller::ContextMTL::DescribeGpuModel
std::string DescribeGpuModel() const override
Definition: context_mtl.mm:286
impeller::ContextMTL::CreateMTLCommandBuffer
id< MTLCommandBuffer > CreateMTLCommandBuffer(const std::string &label) const
Definition: context_mtl.mm:366
impeller::ContextMTL::GetCommandQueue
std::shared_ptr< CommandQueue > GetCommandQueue() const override
Return the graphics queue for submitting command buffers.
Definition: context_mtl.mm:400
gpu_tracer_mtl.h
context.h
impeller::ContextMTL::GetCapabilities
const std::shared_ptr< const Capabilities > & GetCapabilities() const override
Get the capabilities of Impeller context. All optionally supported feature of the platform,...
Definition: context_mtl.mm:351
impeller::ContextMTL
Definition: context_mtl.h:35
impeller
Definition: aiks_blur_unittests.cc:20
impeller::ContextMTL::~ContextMTL
~ContextMTL() override
Definition: context_mtl.mm:277
impeller::ContextMTL::CreateCommandBuffer
std::shared_ptr< CommandBuffer > CreateCommandBuffer() const override
Create a new command buffer. Command buffers can be used to encode graphics, blit,...
Definition: context_mtl.mm:311
impeller::ContextMTL::GetShaderLibrary
std::shared_ptr< ShaderLibrary > GetShaderLibrary() const override
Returns the library of shaders used to specify the programmable stages of a pipeline.
Definition: context_mtl.mm:296