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/mapping.h"
12 #include "flutter/fml/unique_fd.h"
13 #include "fml/thread.h"
15 #include "impeller/core/formats.h"
26 
27 namespace impeller {
28 
29 bool HasValidationLayers();
30 
31 class CommandEncoderFactoryVK;
32 class CommandEncoderVK;
33 class CommandPoolRecyclerVK;
34 class DebugReportVK;
35 class FenceWaiterVK;
36 class ResourceManagerVK;
37 class SurfaceContextVK;
38 class GPUTracerVK;
39 class DescriptorPoolRecyclerVK;
40 class CommandQueueVK;
41 
42 class ContextVK final : public Context,
43  public BackendCast<ContextVK, Context>,
44  public std::enable_shared_from_this<ContextVK> {
45  public:
46  struct Settings {
47  PFN_vkGetInstanceProcAddr proc_address_callback = nullptr;
48  std::vector<std::shared_ptr<fml::Mapping>> shader_libraries_data;
49  fml::UniqueFD cache_directory;
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_
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:538
impeller::DeviceHolderVK
Holds a strong reference to the underlying logical Vulkan device. This comes in handy when the contex...
Definition: device_holder_vk.h:18
impeller::ContextVK::GetConcurrentWorkerTaskRunner
const std::shared_ptr< fml::ConcurrentTaskRunner > GetConcurrentWorkerTaskRunner() const
Definition: context_vk.cc:518
impeller::ContextVK::Settings::enable_gpu_tracing
bool enable_gpu_tracing
Definition: context_vk.h:51
impeller::ContextVK::GetDeviceHolder
std::shared_ptr< DeviceHolderVK > GetDeviceHolder() const
Definition: context_vk.h:136
impeller::ContextVK::SetDebugName
static bool SetDebugName(const vk::Device &device, T handle, std::string_view label)
Definition: context_vk.h:113
impeller::ContextVK::GetCommandQueue
std::shared_ptr< CommandQueue > GetCommandQueue() const override
Return the graphics queue for submitting command buffers.
Definition: context_vk.cc:577
impeller::Context::BackendType
BackendType
Definition: context.h:47
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:482
impeller::ContextVK::GetInstance
vk::Instance GetInstance() const
Definition: context_vk.cc:509
impeller::ContextVK::Settings::enable_validation
bool enable_validation
Definition: context_vk.h:50
impeller::ContextVK::GetPhysicalDevice
vk::PhysicalDevice GetPhysicalDevice() const
Definition: context_vk.cc:546
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:486
impeller::ContextVK::GetBackendType
BackendType GetBackendType() const override
Get the graphics backend of an Impeller context.
Definition: context_vk.cc:134
command_queue.h
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:502
impeller::ContextVK::GetHash
uint64_t GetHash() const
Definition: context_vk.h:67
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:99
device_holder_vk.h
command_pool_vk.h
pipeline_library_vk.h
impeller::ContextVK::Settings
Definition: context_vk.h:46
impeller::ContextVK::Settings::fatal_missing_validations
bool fatal_missing_validations
If validations are requested but cannot be enabled, log a fatal error.
Definition: context_vk.h:53
impeller::ContextVK::CreateSurfaceContext
std::shared_ptr< SurfaceContextVK > CreateSurfaceContext()
Definition: context_vk.cc:534
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:490
impeller::ContextVK::Settings::proc_address_callback
PFN_vkGetInstanceProcAddr proc_address_callback
Definition: context_vk.h:47
impeller::ContextVK::GetGraphicsQueue
const std::shared_ptr< QueueVK > & GetGraphicsQueue() const
Definition: context_vk.cc:542
impeller::ContextVK::GetDescriptorPoolRecycler
std::shared_ptr< DescriptorPoolRecyclerVK > GetDescriptorPoolRecycler() const
Definition: context_vk.cc:572
backend_cast.h
impeller::ContextVK::Settings::shader_libraries_data
std::vector< std::shared_ptr< fml::Mapping > > shader_libraries_data
Definition: context_vk.h:48
queue_vk.h
capabilities.h
impeller::ContextVK::Create
static std::shared_ptr< ContextVK > Create(Settings settings)
Definition: context_vk.cc:100
impeller::ContextVK::Settings::Settings
Settings()=default
driver_info_vk.h
impeller::ContextVK::SetDebugName
bool SetDebugName(T handle, std::string_view label) const
Definition: context_vk.h:108
impeller::ContextVK::GetCommandPoolRecycler
std::shared_ptr< CommandPoolRecyclerVK > GetCommandPoolRecycler() const
Definition: context_vk.cc:558
impeller::ContextVK::GetGPUTracer
std::shared_ptr< GPUTracerVK > GetGPUTracer() const
Definition: context_vk.cc:568
shader_library_vk.h
impeller::ContextVK
Definition: context_vk.h:42
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:73
impeller::ContextVK::InitializeCommonlyUsedShadersIfNeeded
void InitializeCommonlyUsedShadersIfNeeded() const override
Definition: context_vk.cc:584
impeller::ContextVK::~ContextVK
~ContextVK() override
Definition: context_vk.cc:127
impeller::ContextVK::Settings::cache_directory
fml::UniqueFD cache_directory
Definition: context_vk.h:49
impeller::ContextVK::GetSamplerLibrary
std::shared_ptr< SamplerLibrary > GetSamplerLibrary() const override
Returns the library of combined image samplers used in shaders.
Definition: context_vk.cc:494
impeller::Context
To do anything rendering related with Impeller, you need a context.
Definition: context.h:45
impeller::ContextVK::GetDevice
const vk::Device & GetDevice() const
Definition: context_vk.cc:513
impeller::BackendCast
Definition: backend_cast.h:11
impeller::ContextVK::ChooseThreadCountForWorkers
static size_t ChooseThreadCountForWorkers(size_t hardware_concurrency)
Definition: context_vk.cc:110
impeller::ContextVK::GetResourceManager
std::shared_ptr< ResourceManagerVK > GetResourceManager() const
Definition: context_vk.cc:554
impeller::ContextVK::GetDriverInfo
const std::unique_ptr< DriverInfoVK > & GetDriverInfo() const
Definition: context_vk.cc:625
context.h
impeller::ContextVK::GetYUVConversionLibrary
const std::shared_ptr< YUVConversionLibraryVK > & GetYUVConversionLibrary() const
Definition: context_vk.cc:621
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:498
impeller::ContextVK::DescribeGpuModel
std::string DescribeGpuModel() const override
Definition: context_vk.cc:478
impeller::ContextVK::GetFenceWaiter
std::shared_ptr< FenceWaiterVK > GetFenceWaiter() const
Definition: context_vk.cc:550
impeller::ContextVK::SetOffscreenFormat
void SetOffscreenFormat(PixelFormat pixel_format)
Definition: context_vk.cc:473
sampler_library_vk.h
impeller::HasValidationLayers
bool HasValidationLayers()
Definition: context_vk.cc:48
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:522
impeller
Definition: aiks_blend_unittests.cc:18