Flutter Engine
The Flutter Engine
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 <memory>
9
10#include "flutter/fml/concurrent_message_loop.h"
11#include "flutter/fml/mapping.h"
12#include "flutter/fml/unique_fd.h"
13#include "fml/thread.h"
26
27namespace impeller {
28
30
31class CommandEncoderFactoryVK;
32class CommandEncoderVK;
33class CommandPoolRecyclerVK;
34class DebugReportVK;
35class FenceWaiterVK;
36class ResourceManagerVK;
37class SurfaceContextVK;
38class GPUTracerVK;
39class DescriptorPoolRecyclerVK;
40class CommandQueueVK;
41
42class ContextVK final : public Context,
43 public BackendCast<ContextVK, Context>,
44 public std::enable_shared_from_this<ContextVK> {
45 public:
46 struct Settings {
48 std::vector<std::shared_ptr<fml::Mapping>> shader_libraries_data;
50 bool enable_validation = false;
51 bool enable_gpu_tracing = false;
52 /// If validations are requested but cannot be enabled, log a fatal error.
54
55 Settings() = default;
56
57 Settings(Settings&&) = default;
58 };
59
60 /// Choose the number of worker threads the context_vk will create.
61 ///
62 /// Visible for testing.
63 static size_t ChooseThreadCountForWorkers(size_t hardware_concurrency);
64
65 static std::shared_ptr<ContextVK> Create(Settings settings);
66
67 uint64_t GetHash() const { return hash_; }
68
69 // |Context|
70 ~ContextVK() override;
71
72 // |Context|
73 BackendType GetBackendType() const override;
74
75 // |Context|
76 std::string DescribeGpuModel() const override;
77
78 // |Context|
79 bool IsValid() const override;
80
81 // |Context|
82 std::shared_ptr<Allocator> GetResourceAllocator() const override;
83
84 // |Context|
85 std::shared_ptr<ShaderLibrary> GetShaderLibrary() const override;
86
87 // |Context|
88 std::shared_ptr<SamplerLibrary> GetSamplerLibrary() const override;
89
90 // |Context|
91 std::shared_ptr<PipelineLibrary> GetPipelineLibrary() const override;
92
93 // |Context|
94 std::shared_ptr<CommandBuffer> CreateCommandBuffer() const override;
95
96 // |Context|
97 const std::shared_ptr<const Capabilities>& GetCapabilities() const override;
98
99 const std::shared_ptr<YUVConversionLibraryVK>& GetYUVConversionLibrary()
100 const;
101
102 // |Context|
103 void Shutdown() override;
104
105 void SetOffscreenFormat(PixelFormat pixel_format);
106
107 template <typename T>
108 bool SetDebugName(T handle, std::string_view label) const {
109 return SetDebugName(GetDevice(), handle, label);
110 }
111
112 template <typename T>
113 static bool SetDebugName(const vk::Device& device,
114 T handle,
115 std::string_view label) {
116 if (!HasValidationLayers()) {
117 // No-op if validation layers are not enabled.
118 return true;
119 }
120
121 auto c_handle = static_cast<typename T::CType>(handle);
122
123 vk::DebugUtilsObjectNameInfoEXT info;
124 info.objectType = T::objectType;
125 info.pObjectName = label.data();
126 info.objectHandle = reinterpret_cast<decltype(info.objectHandle)>(c_handle);
127
128 if (device.setDebugUtilsObjectNameEXT(info) != vk::Result::eSuccess) {
129 VALIDATION_LOG << "Unable to set debug name: " << label;
130 return false;
131 }
132
133 return true;
134 }
135
136 std::shared_ptr<DeviceHolderVK> GetDeviceHolder() const {
137 return device_holder_;
138 }
139
140 vk::Instance GetInstance() const;
141
142 const vk::Device& GetDevice() const;
143
144 const std::unique_ptr<DriverInfoVK>& GetDriverInfo() const;
145
146 const std::shared_ptr<fml::ConcurrentTaskRunner>
148
149 std::shared_ptr<SurfaceContextVK> CreateSurfaceContext();
150
151 const std::shared_ptr<QueueVK>& GetGraphicsQueue() const;
152
153 vk::PhysicalDevice GetPhysicalDevice() const;
154
155 std::shared_ptr<FenceWaiterVK> GetFenceWaiter() const;
156
157 std::shared_ptr<ResourceManagerVK> GetResourceManager() const;
158
159 std::shared_ptr<CommandPoolRecyclerVK> GetCommandPoolRecycler() const;
160
161 std::shared_ptr<DescriptorPoolRecyclerVK> GetDescriptorPoolRecycler() const;
162
163 std::shared_ptr<CommandQueue> GetCommandQueue() const override;
164
165 std::shared_ptr<GPUTracerVK> GetGPUTracer() const;
166
167 void RecordFrameEndTime() const;
168
169 void InitializeCommonlyUsedShadersIfNeeded() const override;
170
171 private:
172 struct DeviceHolderImpl : public DeviceHolderVK {
173 // |DeviceHolder|
174 const vk::Device& GetDevice() const override { return device.get(); }
175 // |DeviceHolder|
176 const vk::PhysicalDevice& GetPhysicalDevice() const override {
177 return physical_device;
178 }
179
180 vk::UniqueInstance instance;
181 vk::PhysicalDevice physical_device;
182 vk::UniqueDevice device;
183 };
184
185 std::shared_ptr<DeviceHolderImpl> device_holder_;
186 std::unique_ptr<DriverInfoVK> driver_info_;
187 std::unique_ptr<DebugReportVK> debug_report_;
188 std::shared_ptr<Allocator> allocator_;
189 std::shared_ptr<ShaderLibraryVK> shader_library_;
190 std::shared_ptr<SamplerLibraryVK> sampler_library_;
191 std::shared_ptr<PipelineLibraryVK> pipeline_library_;
192 std::shared_ptr<YUVConversionLibraryVK> yuv_conversion_library_;
193 QueuesVK queues_;
194 std::shared_ptr<const Capabilities> device_capabilities_;
195 std::shared_ptr<FenceWaiterVK> fence_waiter_;
196 std::shared_ptr<ResourceManagerVK> resource_manager_;
197 std::shared_ptr<CommandPoolRecyclerVK> command_pool_recycler_;
198 std::string device_name_;
199 std::shared_ptr<fml::ConcurrentMessageLoop> raster_message_loop_;
200 std::shared_ptr<GPUTracerVK> gpu_tracer_;
201 std::shared_ptr<DescriptorPoolRecyclerVK> descriptor_pool_recycler_;
202 std::shared_ptr<CommandQueue> command_queue_vk_;
203
204 const uint64_t hash_;
205
206 bool is_valid_ = false;
207
208 ContextVK();
209
210 void Setup(Settings settings);
211
212 std::unique_ptr<CommandEncoderFactoryVK> CreateGraphicsCommandEncoderFactory()
213 const;
214
215 ContextVK(const ContextVK&) = delete;
216
217 ContextVK& operator=(const ContextVK&) = delete;
218};
219
220} // namespace impeller
221
222#endif // FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_CONTEXT_VK_H_
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
void SetOffscreenFormat(PixelFormat pixel_format)
Definition: context_vk.cc:473
std::shared_ptr< Allocator > GetResourceAllocator() const override
Returns the allocator used to create textures and buffers on the device.
Definition: context_vk.cc:486
std::shared_ptr< ResourceManagerVK > GetResourceManager() const
Definition: context_vk.cc:554
vk::PhysicalDevice GetPhysicalDevice() const
Definition: context_vk.cc:546
const std::shared_ptr< YUVConversionLibraryVK > & GetYUVConversionLibrary() const
Definition: context_vk.cc:621
bool SetDebugName(T handle, std::string_view label) const
Definition: context_vk.h:108
std::shared_ptr< DeviceHolderVK > GetDeviceHolder() const
Definition: context_vk.h:136
void RecordFrameEndTime() const
const vk::Device & GetDevice() const
Definition: context_vk.cc:513
bool IsValid() const override
Determines if a context is valid. If the caller ever receives an invalid context, they must discard i...
Definition: context_vk.cc:482
const std::unique_ptr< DriverInfoVK > & GetDriverInfo() const
Definition: context_vk.cc:625
std::shared_ptr< CommandBuffer > CreateCommandBuffer() const override
Create a new command buffer. Command buffers can be used to encode graphics, blit,...
Definition: context_vk.cc:502
std::shared_ptr< SamplerLibrary > GetSamplerLibrary() const override
Returns the library of combined image samplers used in shaders.
Definition: context_vk.cc:494
static std::shared_ptr< ContextVK > Create(Settings settings)
Definition: context_vk.cc:100
std::shared_ptr< PipelineLibrary > GetPipelineLibrary() const override
Returns the library of pipelines used by render or compute commands.
Definition: context_vk.cc:498
const std::shared_ptr< QueueVK > & GetGraphicsQueue() const
Definition: context_vk.cc:542
const std::shared_ptr< const Capabilities > & GetCapabilities() const override
Get the capabilities of Impeller context. All optionally supported feature of the platform,...
Definition: context_vk.cc:538
std::shared_ptr< CommandPoolRecyclerVK > GetCommandPoolRecycler() const
Definition: context_vk.cc:558
std::shared_ptr< CommandQueue > GetCommandQueue() const override
Return the graphics queue for submitting command buffers.
Definition: context_vk.cc:577
void InitializeCommonlyUsedShadersIfNeeded() const override
Definition: context_vk.cc:584
std::shared_ptr< FenceWaiterVK > GetFenceWaiter() const
Definition: context_vk.cc:550
std::shared_ptr< GPUTracerVK > GetGPUTracer() const
Definition: context_vk.cc:568
BackendType GetBackendType() const override
Get the graphics backend of an Impeller context.
Definition: context_vk.cc:134
~ContextVK() override
Definition: context_vk.cc:127
std::string DescribeGpuModel() const override
Definition: context_vk.cc:478
static bool SetDebugName(const vk::Device &device, T handle, std::string_view label)
Definition: context_vk.h:113
const std::shared_ptr< fml::ConcurrentTaskRunner > GetConcurrentWorkerTaskRunner() const
Definition: context_vk.cc:518
static size_t ChooseThreadCountForWorkers(size_t hardware_concurrency)
Definition: context_vk.cc:110
std::shared_ptr< ShaderLibrary > GetShaderLibrary() const override
Returns the library of shaders used to specify the programmable stages of a pipeline.
Definition: context_vk.cc:490
vk::Instance GetInstance() const
Definition: context_vk.cc:509
uint64_t GetHash() const
Definition: context_vk.h:67
void Shutdown() override
Force all pending asynchronous work to finish. This is achieved by deleting all owned concurrent mess...
Definition: context_vk.cc:522
std::shared_ptr< DescriptorPoolRecyclerVK > GetDescriptorPoolRecycler() const
Definition: context_vk.cc:572
std::shared_ptr< SurfaceContextVK > CreateSurfaceContext()
Definition: context_vk.cc:534
To do anything rendering related with Impeller, you need a context.
Definition: context.h:45
Holds a strong reference to the underlying logical Vulkan device. This comes in handy when the contex...
VkPhysicalDevice physical_device
Definition: main.cc:51
VkDevice device
Definition: main.cc:53
VkInstance instance
Definition: main.cc:48
bool HasValidationLayers()
Definition: context_vk.cc:48
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:99
#define T
Definition: precompiler.cc:65
Settings(Settings &&)=default
std::vector< std::shared_ptr< fml::Mapping > > shader_libraries_data
Definition: context_vk.h:48
PFN_vkGetInstanceProcAddr proc_address_callback
Definition: context_vk.h:47
bool fatal_missing_validations
If validations are requested but cannot be enabled, log a fatal error.
Definition: context_vk.h:53
fml::UniqueFD cache_directory
Definition: context_vk.h:49
#define VALIDATION_LOG
Definition: validation.h:73
PFN_vkVoidFunction(VKAPI_PTR * PFN_vkGetInstanceProcAddr)(VkInstance instance, const char *pName)
Definition: vulkan_core.h:3989