Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
context_vk.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_VULKAN_CONTEXT_VK_H_
6#define FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_CONTEXT_VK_H_
7
8#include <format>
9#include <memory>
10
12#include "flutter/fml/mapping.h"
29
30namespace impeller {
31
33
34class CommandEncoderFactoryVK;
35class CommandEncoderVK;
36class CommandPoolRecyclerVK;
37class DebugReportVK;
38class FenceWaiterVK;
39class ResourceManagerVK;
40class SurfaceContextVK;
41class GPUTracerVK;
42class DescriptorPoolRecyclerVK;
43class CommandQueueVK;
44class DescriptorPoolVK;
45
46class IdleWaiterVK : public IdleWaiter {
47 public:
48 explicit IdleWaiterVK(std::weak_ptr<DeviceHolderVK> device_holder)
49 : device_holder_(std::move(device_holder)) {}
50
51 void WaitIdle() const override {
52 std::shared_ptr<DeviceHolderVK> strong_device_holder_ =
53 device_holder_.lock();
54 if (strong_device_holder_ && strong_device_holder_->GetDevice()) {
55 [[maybe_unused]] auto result =
56 strong_device_holder_->GetDevice().waitIdle();
57 }
58 }
59
60 private:
61 std::weak_ptr<DeviceHolderVK> device_holder_;
62};
63
64class ContextVK final : public Context,
65 public BackendCast<ContextVK, Context>,
66 public std::enable_shared_from_this<ContextVK> {
67 public:
68 // Mutable tracker for command buffer submission bookkeeping.
69 const std::shared_ptr<GpuSubmissionTracker>& GetMutableSubmissionTracker()
70 const;
71
72 /// Embedder Stuff
73 struct EmbedderData {
74 VkInstance instance;
75 VkPhysicalDevice physical_device;
76 VkDevice device;
78 VkQueue queue;
79 std::vector<std::string> instance_extensions;
80 std::vector<std::string> device_extensions;
81 };
82
83 struct Settings {
84 PFN_vkGetInstanceProcAddr proc_address_callback = nullptr;
85 std::vector<std::shared_ptr<fml::Mapping>> shader_libraries_data;
87 bool enable_validation = false;
88 bool enable_gpu_tracing = false;
90 /// If validations are requested but cannot be enabled, log a fatal error.
93
94 std::optional<EmbedderData> embedder_data;
95
96 Settings() = default;
97
98 Settings(Settings&&) = default;
99 };
100
101 /// Choose the number of worker threads the context_vk will create.
102 ///
103 /// Visible for testing.
104 static size_t ChooseThreadCountForWorkers(size_t hardware_concurrency);
105
106 static std::shared_ptr<ContextVK> Create(Settings settings);
107
108 uint64_t GetHash() const { return hash_; }
109
110 // |Context|
111 ~ContextVK() override;
112
113 // |Context|
114 BackendType GetBackendType() const override;
115
116 // |Context|
117 std::string DescribeGpuModel() const override;
118
119 // |Context|
120 bool IsValid() const override;
121
122 // |Context|
123 std::shared_ptr<Allocator> GetResourceAllocator() const override;
124
125 // |Context|
126 std::shared_ptr<const GpuSubmissionTracker> GetSubmissionTracker()
127 const override;
128
129 // |Context|
130 std::shared_ptr<ShaderLibrary> GetShaderLibrary() const override;
131
132 // |Context|
133 std::shared_ptr<SamplerLibrary> GetSamplerLibrary() const override;
134
135 // |Context|
136 std::shared_ptr<PipelineLibrary> GetPipelineLibrary() const override;
137
138 // |Context|
139 std::shared_ptr<CommandBuffer> CreateCommandBuffer() const override;
140
141 // |Context|
142 const std::shared_ptr<const Capabilities>& GetCapabilities() const override;
143
144 // |Context|
145 virtual bool SubmitOnscreen(
146 std::shared_ptr<CommandBuffer> cmd_buffer) override;
147
148 const std::shared_ptr<YUVConversionLibraryVK>& GetYUVConversionLibrary()
149 const;
150
151 // |Context|
152 void Shutdown() override;
153
154 const WorkaroundsVK& GetWorkarounds() const;
155
156 void SetOffscreenFormat(PixelFormat pixel_format);
157
158 template <typename T>
159 bool SetDebugName(T handle, std::string_view label) const {
160 return SetDebugName(GetDevice(), handle, label);
161 }
162
163 template <typename T>
164 bool SetDebugName(T handle,
165 std::string_view label,
166 std::string_view trailing) const {
167 if (!HasValidationLayers()) {
168 // No-op if validation layers are not enabled.
169 return true;
170 }
171 std::string combined = std::format("{} {}", label, trailing);
172 return SetDebugName(GetDevice(), handle, combined);
173 }
174
175 template <typename T>
176 static bool SetDebugName(const vk::Device& device,
177 T handle,
178 std::string_view label) {
179 if (!HasValidationLayers()) {
180 // No-op if validation layers are not enabled.
181 return true;
182 }
183
184 auto c_handle = static_cast<typename T::CType>(handle);
185
186 vk::DebugUtilsObjectNameInfoEXT info;
187 info.objectType = T::objectType;
188 info.pObjectName = label.data();
189 info.objectHandle = reinterpret_cast<decltype(info.objectHandle)>(c_handle);
190
191 if (device.setDebugUtilsObjectNameEXT(info) != vk::Result::eSuccess) {
192 VALIDATION_LOG << "Unable to set debug name: " << label;
193 return false;
194 }
195
196 return true;
197 }
198
199 std::shared_ptr<DeviceHolderVK> GetDeviceHolder() const {
200 return device_holder_;
201 }
202
203 vk::Instance GetInstance() const;
204
205 const vk::Device& GetDevice() const;
206
207 const std::unique_ptr<DriverInfoVK>& GetDriverInfo() const;
208
209 const std::shared_ptr<fml::ConcurrentTaskRunner>
211
212 std::shared_ptr<SurfaceContextVK> CreateSurfaceContext();
213
214 const std::shared_ptr<QueueVK>& GetGraphicsQueue() const;
215
216 vk::PhysicalDevice GetPhysicalDevice() const;
217
218 std::shared_ptr<FenceWaiterVK> GetFenceWaiter() const;
219
220 std::shared_ptr<ResourceManagerVK> GetResourceManager() const;
221
222 std::shared_ptr<CommandPoolRecyclerVK> GetCommandPoolRecycler() const;
223
224 std::shared_ptr<DescriptorPoolRecyclerVK> GetDescriptorPoolRecycler() const;
225
226 std::shared_ptr<CommandQueue> GetCommandQueue() const override;
227
228 std::shared_ptr<GPUTracerVK> GetGPUTracer() const;
229
230 void RecordFrameEndTime() const;
231
232 // |Context|
233 void InitializeCommonlyUsedShadersIfNeeded() const override;
234
235 // |Context|
236 void DisposeThreadLocalCachedResources() override;
237
238 /// @brief Whether the Android Surface control based swapchain should be
239 /// enabled
241
242 // | Context |
244 std::shared_ptr<CommandBuffer> command_buffer) override;
245
246 // | Context |
247 bool FlushCommandBuffers() override;
248
249 // | Context |
250 [[nodiscard]] bool FinishQueue() override;
251
253
254 std::shared_ptr<const IdleWaiter> GetIdleWaiter() const override {
255 return idle_waiter_vk_;
256 }
257
258 private:
259 struct DeviceHolderImpl : public DeviceHolderVK {
260 // |DeviceHolder|
261 const vk::Device& GetDevice() const override { return device.get(); }
262 // |DeviceHolder|
263 const vk::PhysicalDevice& GetPhysicalDevice() const override {
264 return physical_device;
265 }
266
267 ~DeviceHolderImpl() {
268 if (!owned) {
269 instance.release();
270 device.release();
271 }
272 }
273
274 vk::UniqueInstance instance;
275 vk::PhysicalDevice physical_device;
276 vk::UniqueDevice device;
277 bool owned = true;
278 };
279
280 std::shared_ptr<DeviceHolderImpl> device_holder_;
281 std::unique_ptr<DriverInfoVK> driver_info_;
282 std::unique_ptr<DebugReportVK> debug_report_;
283 std::shared_ptr<Allocator> allocator_;
284 std::shared_ptr<ShaderLibraryVK> shader_library_;
285 std::shared_ptr<SamplerLibraryVK> sampler_library_;
286 std::shared_ptr<PipelineLibraryVK> pipeline_library_;
287 std::shared_ptr<YUVConversionLibraryVK> yuv_conversion_library_;
288 QueuesVK queues_;
289 std::shared_ptr<const Capabilities> device_capabilities_;
290 std::shared_ptr<FenceWaiterVK> fence_waiter_;
291 std::shared_ptr<GpuSubmissionTracker> submission_tracker_ =
292 std::make_shared<GpuSubmissionTracker>();
293 std::shared_ptr<ResourceManagerVK> resource_manager_;
294 std::shared_ptr<DescriptorPoolRecyclerVK> descriptor_pool_recycler_;
295 std::shared_ptr<CommandPoolRecyclerVK> command_pool_recycler_;
296 std::string device_name_;
297 std::shared_ptr<fml::ConcurrentMessageLoop> raster_message_loop_;
298 std::shared_ptr<GPUTracerVK> gpu_tracer_;
299 std::shared_ptr<CommandQueue> command_queue_vk_;
300 std::shared_ptr<const IdleWaiter> idle_waiter_vk_;
301 WorkaroundsVK workarounds_;
302
303 using DescriptorPoolMap =
304 std::unordered_map<std::thread::id, std::shared_ptr<DescriptorPoolVK>>;
305
306 mutable Mutex desc_pool_mutex_;
307 mutable DescriptorPoolMap IPLR_GUARDED_BY(desc_pool_mutex_)
308 cached_descriptor_pool_;
309 bool should_enable_surface_control_ = false;
310 bool should_batch_cmd_buffers_ = false;
311 std::vector<std::shared_ptr<CommandBuffer>> pending_command_buffers_;
312
313 const uint64_t hash_;
314
315 bool is_valid_ = false;
316
317 explicit ContextVK(const Flags& flags);
318
319 void Setup(Settings settings);
320
321 ContextVK(const ContextVK&) = delete;
322
323 ContextVK& operator=(const ContextVK&) = delete;
324};
325
326} // namespace impeller
327
328#endif // FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_CONTEXT_VK_H_
To do anything rendering related with Impeller, you need a context.
Definition context.h:70
void SetOffscreenFormat(PixelFormat pixel_format)
std::shared_ptr< const IdleWaiter > GetIdleWaiter() const override
Definition context_vk.h:254
std::shared_ptr< Allocator > GetResourceAllocator() const override
Returns the allocator used to create textures and buffers on the device.
std::shared_ptr< ResourceManagerVK > GetResourceManager() const
vk::PhysicalDevice GetPhysicalDevice() const
const std::shared_ptr< YUVConversionLibraryVK > & GetYUVConversionLibrary() const
bool SetDebugName(T handle, std::string_view label) const
Definition context_vk.h:159
std::shared_ptr< DeviceHolderVK > GetDeviceHolder() const
Definition context_vk.h:199
bool EnqueueCommandBuffer(std::shared_ptr< CommandBuffer > command_buffer) override
Enqueue command_buffer for submission by the end of the frame.
void RecordFrameEndTime() const
const vk::Device & GetDevice() const
bool FlushCommandBuffers() override
Flush all pending command buffers.
bool IsValid() const override
Determines if a context is valid. If the caller ever receives an invalid context, they must discard i...
const std::unique_ptr< DriverInfoVK > & GetDriverInfo() const
void DisposeThreadLocalCachedResources() override
std::shared_ptr< CommandBuffer > CreateCommandBuffer() const override
Create a new command buffer. Command buffers can be used to encode graphics, blit,...
virtual bool SubmitOnscreen(std::shared_ptr< CommandBuffer > cmd_buffer) override
Submit the command buffer that renders to the onscreen surface.
std::shared_ptr< SamplerLibrary > GetSamplerLibrary() const override
Returns the library of combined image samplers used in shaders.
static std::shared_ptr< ContextVK > Create(Settings settings)
std::shared_ptr< PipelineLibrary > GetPipelineLibrary() const override
Returns the library of pipelines used by render or compute commands.
const std::shared_ptr< QueueVK > & GetGraphicsQueue() const
const std::shared_ptr< GpuSubmissionTracker > & GetMutableSubmissionTracker() const
const std::shared_ptr< const Capabilities > & GetCapabilities() const override
Get the capabilities of Impeller context. All optionally supported feature of the platform,...
RuntimeStageBackend GetRuntimeStageBackend() const override
Retrieve the runtime stage for this context type.
std::shared_ptr< CommandPoolRecyclerVK > GetCommandPoolRecycler() const
std::shared_ptr< CommandQueue > GetCommandQueue() const override
Return the graphics queue for submitting command buffers.
void InitializeCommonlyUsedShadersIfNeeded() const override
std::shared_ptr< FenceWaiterVK > GetFenceWaiter() const
bool GetShouldEnableSurfaceControlSwapchain() const
Whether the Android Surface control based swapchain should be enabled.
std::shared_ptr< GPUTracerVK > GetGPUTracer() const
BackendType GetBackendType() const override
Get the graphics backend of an Impeller context.
~ContextVK() override
std::string DescribeGpuModel() const override
static bool SetDebugName(const vk::Device &device, T handle, std::string_view label)
Definition context_vk.h:176
const WorkaroundsVK & GetWorkarounds() const
const std::shared_ptr< fml::ConcurrentTaskRunner > GetConcurrentWorkerTaskRunner() const
static size_t ChooseThreadCountForWorkers(size_t hardware_concurrency)
std::shared_ptr< ShaderLibrary > GetShaderLibrary() const override
Returns the library of shaders used to specify the programmable stages of a pipeline.
vk::Instance GetInstance() const
std::shared_ptr< const GpuSubmissionTracker > GetSubmissionTracker() const override
Returns the tracker recording GPU completion of command buffer submissions, or nullptr if the backend...
bool SetDebugName(T handle, std::string_view label, std::string_view trailing) const
Definition context_vk.h:164
uint64_t GetHash() const
Definition context_vk.h:108
void Shutdown() override
Force all pending asynchronous work to finish. This is achieved by deleting all owned concurrent mess...
bool FinishQueue() override
Wait until all previously submitted command buffers are processed and displayed by the GPU.
std::shared_ptr< DescriptorPoolRecyclerVK > GetDescriptorPoolRecycler() const
std::shared_ptr< SurfaceContextVK > CreateSurfaceContext()
Holds a strong reference to the underlying logical Vulkan device. This comes in handy when the contex...
IdleWaiterVK(std::weak_ptr< DeviceHolderVK > device_holder)
Definition context_vk.h:48
void WaitIdle() const override
Definition context_vk.h:51
VkPhysicalDevice physical_device
Definition main.cc:67
VkDevice device
Definition main.cc:69
VkInstance instance
Definition main.cc:64
bool HasValidationLayers()
Definition context_vk.cc:53
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition formats.h:99
Definition ref_ptr.h:261
std::shared_ptr< CommandBuffer > command_buffer
std::vector< std::string > device_extensions
Definition context_vk.h:80
std::vector< std::string > instance_extensions
Definition context_vk.h:79
Settings(Settings &&)=default
std::vector< std::shared_ptr< fml::Mapping > > shader_libraries_data
Definition context_vk.h:85
PFN_vkGetInstanceProcAddr proc_address_callback
Definition context_vk.h:84
bool fatal_missing_validations
If validations are requested but cannot be enabled, log a fatal error.
Definition context_vk.h:91
std::optional< EmbedderData > embedder_data
Definition context_vk.h:94
A non-exhaustive set of driver specific workarounds.
#define IPLR_GUARDED_BY(x)
#define VALIDATION_LOG
Definition validation.h:91