| Flutter Impeller |

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/macros.h"
12 #include "flutter/fml/mapping.h"
13 #include "flutter/fml/unique_fd.h"
14 #include "fml/thread.h"
15 #include "impeller/base/backend_cast.h"
16 #include "impeller/core/formats.h"
17 #include "impeller/renderer/backend/vulkan/command_pool_vk.h"
18 #include "impeller/renderer/backend/vulkan/device_holder.h"
19 #include "impeller/renderer/backend/vulkan/pipeline_library_vk.h"
20 #include "impeller/renderer/backend/vulkan/queue_vk.h"
21 #include "impeller/renderer/backend/vulkan/sampler_library_vk.h"
22 #include "impeller/renderer/backend/vulkan/shader_library_vk.h"
23 #include "impeller/renderer/capabilities.h"
24 #include "impeller/renderer/context.h"
25
26 namespace impeller {
27
28 bool HasValidationLayers();
29
30 class CommandEncoderFactoryVK;
31 class CommandEncoderVK;
32 class CommandPoolRecyclerVK;
33 class DebugReportVK;
34 class FenceWaiterVK;
35 class ResourceManagerVK;
36 class SurfaceContextVK;
37 class GPUTracerVK;
38 class DescriptorPoolRecyclerVK;
39
40 class ContextVK final : public Context,
41 public BackendCast<ContextVK, Context>,
42 public std::enable_shared_from_this<ContextVK> {
43 public:
44 struct Settings {
45 PFN_vkGetInstanceProcAddr proc_address_callback = nullptr;
46 std::vector<std::shared_ptr<fml::Mapping>> shader_libraries_data;
47 fml::UniqueFD cache_directory;
48 bool enable_validation = false;
49
50 Settings() = default;
51
52 Settings(Settings&&) = default;
53 };
54
55 static std::shared_ptr<ContextVK> Create(Settings settings);
56
57 uint64_t GetHash() const { return hash_; }
58
59 // |Context|
60 ~ContextVK() override;
61
62 // |Context|
63 BackendType GetBackendType() const override;
64
65 // |Context|
66 std::string DescribeGpuModel() const override;
67
68 // |Context|
69 bool IsValid() const override;
70
71 // |Context|
72 std::shared_ptr<Allocator> GetResourceAllocator() const override;
73
74 // |Context|
75 std::shared_ptr<ShaderLibrary> GetShaderLibrary() const override;
76
77 // |Context|
78 std::shared_ptr<SamplerLibrary> GetSamplerLibrary() const override;
79
80 // |Context|
81 std::shared_ptr<PipelineLibrary> GetPipelineLibrary() const override;
82
83 // |Context|
84 std::shared_ptr<CommandBuffer> CreateCommandBuffer() const override;
85
86 // |Context|
87 const std::shared_ptr<const Capabilities>& GetCapabilities() const override;
88
89 // |Context|
90 void Shutdown() override;
91
92 // |Context|
93 void SetSyncPresentation(bool value) override { sync_presentation_ = value; }
94
95 bool GetSyncPresentation() const { return sync_presentation_; }
96
97 void SetOffscreenFormat(PixelFormat pixel_format);
98
99 template <typename T>
100 bool SetDebugName(T handle, std::string_view label) const {
101 return SetDebugName(GetDevice(), handle, label);
102 }
103
104 template <typename T>
105 static bool SetDebugName(const vk::Device& device,
106 T handle,
107 std::string_view label) {
108 if (!HasValidationLayers()) {
109 // No-op if validation layers are not enabled.
110 return true;
111 }
112
113 auto c_handle = static_cast<typename T::CType>(handle);
114
115 vk::DebugUtilsObjectNameInfoEXT info;
116 info.objectType = T::objectType;
117 info.pObjectName = label.data();
118 info.objectHandle = reinterpret_cast<decltype(info.objectHandle)>(c_handle);
119
120 if (device.setDebugUtilsObjectNameEXT(info) != vk::Result::eSuccess) {
121 VALIDATION_LOG << "Unable to set debug name: " << label;
122 return false;
123 }
124
125 return true;
126 }
127
128 std::shared_ptr<DeviceHolder> GetDeviceHolder() const {
129 return device_holder_;
130 }
131
132 vk::Instance GetInstance() const;
133
134 const vk::Device& GetDevice() const;
135
136 const std::shared_ptr<fml::ConcurrentTaskRunner>
137 GetConcurrentWorkerTaskRunner() const;
138
139 /// @brief A single-threaded task runner that should only be used for
140 /// submitKHR.
141 ///
142 /// SubmitKHR will block until all previously submitted command buffers have
143 /// been scheduled. If there are no platform views in the scene (excluding
144 /// texture backed platform views). Then it is safe for SwapchainImpl::Present
145 /// to return before submit has completed. To do so, we offload the submit
146 /// command to a specialized single threaded task runner. The single thread
147 /// ensures that we do not queue up too much work and that the submissions
148 /// proceed in order.
149 const fml::RefPtr<fml::TaskRunner> GetQueueSubmitRunner() const;
150
151 std::shared_ptr<SurfaceContextVK> CreateSurfaceContext();
152
153 const std::shared_ptr<QueueVK>& GetGraphicsQueue() const;
154
155 vk::PhysicalDevice GetPhysicalDevice() const;
156
157 std::shared_ptr<FenceWaiterVK> GetFenceWaiter() const;
158
159 std::shared_ptr<ResourceManagerVK> GetResourceManager() const;
160
161 std::shared_ptr<CommandPoolRecyclerVK> GetCommandPoolRecycler() const;
162
163 std::shared_ptr<DescriptorPoolRecyclerVK> GetDescriptorPoolRecycler() const {
164 return descriptor_pool_recycler_;
165 }
166
167 std::shared_ptr<GPUTracerVK> GetGPUTracer() const;
168
169 void RecordFrameEndTime() const;
170
171 private:
172 struct DeviceHolderImpl : public DeviceHolder {
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<DebugReportVK> debug_report_;
187 std::shared_ptr<Allocator> allocator_;
188 std::shared_ptr<ShaderLibraryVK> shader_library_;
189 std::shared_ptr<SamplerLibraryVK> sampler_library_;
190 std::shared_ptr<PipelineLibraryVK> pipeline_library_;
191 QueuesVK queues_;
192 std::shared_ptr<const Capabilities> device_capabilities_;
193 std::shared_ptr<FenceWaiterVK> fence_waiter_;
194 std::shared_ptr<ResourceManagerVK> resource_manager_;
195 std::shared_ptr<CommandPoolRecyclerVK> command_pool_recycler_;
196 std::string device_name_;
197 std::shared_ptr<fml::ConcurrentMessageLoop> raster_message_loop_;
198 std::unique_ptr<fml::Thread> queue_submit_thread_;
199 std::shared_ptr<GPUTracerVK> gpu_tracer_;
200 std::shared_ptr<DescriptorPoolRecyclerVK> descriptor_pool_recycler_;
201
202 bool sync_presentation_ = false;
203 const uint64_t hash_;
204
205 bool is_valid_ = false;
206
207 ContextVK();
208
209 void Setup(Settings settings);
210
211 std::unique_ptr<CommandEncoderFactoryVK> CreateGraphicsCommandEncoderFactory()
212 const;
213
214 ContextVK(const ContextVK&) = delete;
215
216 ContextVK& operator=(const ContextVK&) = delete;
217 };
218
219 } // namespace impeller
220
221 #endif // FLUTTER_IMPELLER_RENDERER_BACKEND_VULKAN_CONTEXT_VK_H_
impeller::ContextVK::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_vk.cc:531
impeller::ContextVK::GetConcurrentWorkerTaskRunner
const std::shared_ptr< fml::ConcurrentTaskRunner > GetConcurrentWorkerTaskRunner() const
Definition: context_vk.cc:510
impeller::ContextVK::SetDebugName
static bool SetDebugName(const vk::Device &device, T handle, std::string_view label)
Definition: context_vk.h:105
impeller::Context::BackendType
BackendType
Definition: context.h:49
impeller::ContextVK::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_vk.cc:470
impeller::ContextVK::GetInstance
vk::Instance GetInstance() const
Definition: context_vk.cc:497
impeller::ContextVK::Settings::enable_validation
bool enable_validation
Definition: context_vk.h:48
device_holder.h
impeller::ContextVK::GetPhysicalDevice
vk::PhysicalDevice GetPhysicalDevice() const
Definition: context_vk.cc:539
impeller::ContextVK::GetResourceAllocator
std::shared_ptr< Allocator > GetResourceAllocator() const override
Returns the allocator used to create textures and buffers on the device.
Definition: context_vk.cc:474
impeller::DeviceHolder
Definition: device_holder.h:12
impeller::ContextVK::GetBackendType
BackendType GetBackendType() const override
Get the graphics backend of an Impeller context.
Definition: context_vk.cc:122
formats.h
impeller::ContextVK::CreateCommandBuffer
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:490
impeller::ContextVK::GetHash
uint64_t GetHash() const
Definition: context_vk.h:57
impeller::ContextVK::RecordFrameEndTime
void RecordFrameEndTime() const
impeller::PixelFormat
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:94
command_pool_vk.h
pipeline_library_vk.h
impeller::ContextVK::Settings
Definition: context_vk.h:44
impeller::ContextVK::CreateSurfaceContext
std::shared_ptr< SurfaceContextVK > CreateSurfaceContext()
Definition: context_vk.cc:527
impeller::ContextVK::GetShaderLibrary
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:478
impeller::ContextVK::Settings::proc_address_callback
PFN_vkGetInstanceProcAddr proc_address_callback
Definition: context_vk.h:45
impeller::ContextVK::GetGraphicsQueue
const std::shared_ptr< QueueVK > & GetGraphicsQueue() const
Definition: context_vk.cc:535
impeller::ContextVK::GetDeviceHolder
std::shared_ptr< DeviceHolder > GetDeviceHolder() const
Definition: context_vk.h:128
backend_cast.h
impeller::ContextVK::Settings::shader_libraries_data
std::vector< std::shared_ptr< fml::Mapping > > shader_libraries_data
Definition: context_vk.h:46
queue_vk.h
capabilities.h
impeller::ContextVK::Create
static std::shared_ptr< ContextVK > Create(Settings settings)
Definition: context_vk.cc:95
impeller::ContextVK::Settings::Settings
Settings()=default
impeller::ContextVK::SetDebugName
bool SetDebugName(T handle, std::string_view label) const
Definition: context_vk.h:100
impeller::ContextVK::GetCommandPoolRecycler
std::shared_ptr< CommandPoolRecyclerVK > GetCommandPoolRecycler() const
Definition: context_vk.cc:551
impeller::ContextVK::GetGPUTracer
std::shared_ptr< GPUTracerVK > GetGPUTracer() const
Definition: context_vk.cc:561
shader_library_vk.h
impeller::ContextVK
Definition: context_vk.h:40
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:67
impeller::ContextVK::~ContextVK
~ContextVK() override
Definition: context_vk.cc:115
impeller::ContextVK::Settings::cache_directory
fml::UniqueFD cache_directory
Definition: context_vk.h:47
impeller::ContextVK::GetSamplerLibrary
std::shared_ptr< SamplerLibrary > GetSamplerLibrary() const override
Returns the library of combined image samplers used in shaders.
Definition: context_vk.cc:482
impeller::Context
To do anything rendering related with Impeller, you need a context.
Definition: context.h:47
impeller::ContextVK::GetDevice
const vk::Device & GetDevice() const
Definition: context_vk.cc:501
impeller::BackendCast
Definition: backend_cast.h:13
impeller::ContextVK::GetDescriptorPoolRecycler
std::shared_ptr< DescriptorPoolRecyclerVK > GetDescriptorPoolRecycler() const
Definition: context_vk.h:163
impeller::ContextVK::GetResourceManager
std::shared_ptr< ResourceManagerVK > GetResourceManager() const
Definition: context_vk.cc:547
impeller::ContextVK::GetSyncPresentation
bool GetSyncPresentation() const
Definition: context_vk.h:95
context.h
impeller::ContextVK::GetPipelineLibrary
std::shared_ptr< PipelineLibrary > GetPipelineLibrary() const override
Returns the library of pipelines used by render or compute commands.
Definition: context_vk.cc:486
impeller::ContextVK::DescribeGpuModel
std::string DescribeGpuModel() const override
Definition: context_vk.cc:466
impeller::ContextVK::GetFenceWaiter
std::shared_ptr< FenceWaiterVK > GetFenceWaiter() const
Definition: context_vk.cc:543
impeller::ContextVK::SetOffscreenFormat
void SetOffscreenFormat(PixelFormat pixel_format)
Definition: context_vk.cc:461
sampler_library_vk.h
impeller::ContextVK::GetQueueSubmitRunner
const fml::RefPtr< fml::TaskRunner > GetQueueSubmitRunner() const
A single-threaded task runner that should only be used for submitKHR.
Definition: context_vk.cc:505
impeller::HasValidationLayers
bool HasValidationLayers()
Definition: context_vk.cc:43
impeller::ContextVK::Shutdown
void Shutdown() override
Force all pending asynchronous work to finish. This is achieved by deleting all owned concurrent mess...
Definition: context_vk.cc:514
impeller
Definition: aiks_context.cc:10
impeller::ContextVK::SetSyncPresentation
void SetSyncPresentation(bool value) override
Force the Vulkan presentation (submitKHR) to be performed on the raster task runner.
Definition: context_vk.h:93