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;
53  /// If validations are requested but cannot be enabled, log a fatal error.
55 
56  Settings() = default;
57 
58  Settings(Settings&&) = default;
59  };
60 
61  /// Choose the number of worker threads the context_vk will create.
62  ///
63  /// Visible for testing.
64  static size_t ChooseThreadCountForWorkers(size_t hardware_concurrency);
65 
66  static std::shared_ptr<ContextVK> Create(Settings settings);
67 
68  uint64_t GetHash() const { return hash_; }
69 
70  // |Context|
71  ~ContextVK() override;
72 
73  // |Context|
74  BackendType GetBackendType() const override;
75 
76  // |Context|
77  std::string DescribeGpuModel() const override;
78 
79  // |Context|
80  bool IsValid() const override;
81 
82  // |Context|
83  std::shared_ptr<Allocator> GetResourceAllocator() const override;
84 
85  // |Context|
86  std::shared_ptr<ShaderLibrary> GetShaderLibrary() const override;
87 
88  // |Context|
89  std::shared_ptr<SamplerLibrary> GetSamplerLibrary() const override;
90 
91  // |Context|
92  std::shared_ptr<PipelineLibrary> GetPipelineLibrary() const override;
93 
94  // |Context|
95  std::shared_ptr<CommandBuffer> CreateCommandBuffer() const override;
96 
97  // |Context|
98  const std::shared_ptr<const Capabilities>& GetCapabilities() const override;
99 
100  const std::shared_ptr<YUVConversionLibraryVK>& GetYUVConversionLibrary()
101  const;
102 
103  // |Context|
104  void Shutdown() override;
105 
106  void SetOffscreenFormat(PixelFormat pixel_format);
107 
108  template <typename T>
109  bool SetDebugName(T handle, std::string_view label) const {
110  return SetDebugName(GetDevice(), handle, label);
111  }
112 
113  template <typename T>
114  static bool SetDebugName(const vk::Device& device,
115  T handle,
116  std::string_view label) {
117  if (!HasValidationLayers()) {
118  // No-op if validation layers are not enabled.
119  return true;
120  }
121 
122  auto c_handle = static_cast<typename T::CType>(handle);
123 
124  vk::DebugUtilsObjectNameInfoEXT info;
125  info.objectType = T::objectType;
126  info.pObjectName = label.data();
127  info.objectHandle = reinterpret_cast<decltype(info.objectHandle)>(c_handle);
128 
129  if (device.setDebugUtilsObjectNameEXT(info) != vk::Result::eSuccess) {
130  VALIDATION_LOG << "Unable to set debug name: " << label;
131  return false;
132  }
133 
134  return true;
135  }
136 
137  std::shared_ptr<DeviceHolderVK> GetDeviceHolder() const {
138  return device_holder_;
139  }
140 
141  vk::Instance GetInstance() const;
142 
143  const vk::Device& GetDevice() const;
144 
145  const std::unique_ptr<DriverInfoVK>& GetDriverInfo() const;
146 
147  const std::shared_ptr<fml::ConcurrentTaskRunner>
149 
150  std::shared_ptr<SurfaceContextVK> CreateSurfaceContext();
151 
152  const std::shared_ptr<QueueVK>& GetGraphicsQueue() const;
153 
154  vk::PhysicalDevice GetPhysicalDevice() const;
155 
156  std::shared_ptr<FenceWaiterVK> GetFenceWaiter() const;
157 
158  std::shared_ptr<ResourceManagerVK> GetResourceManager() const;
159 
160  std::shared_ptr<CommandPoolRecyclerVK> GetCommandPoolRecycler() const;
161 
162  std::shared_ptr<DescriptorPoolRecyclerVK> GetDescriptorPoolRecycler() const;
163 
164  std::shared_ptr<CommandQueue> GetCommandQueue() const override;
165 
166  std::shared_ptr<GPUTracerVK> GetGPUTracer() const;
167 
168  void RecordFrameEndTime() const;
169 
170  // |Context|
171  void InitializeCommonlyUsedShadersIfNeeded() const override;
172 
173  // |Context|
174  void DisposeThreadLocalCachedResources() override;
175 
176  /// @brief Whether the Android Surface control based swapchain should be
177  /// disabled, even if the device is capable of supporting it.
179 
180  private:
181  struct DeviceHolderImpl : public DeviceHolderVK {
182  // |DeviceHolder|
183  const vk::Device& GetDevice() const override { return device.get(); }
184  // |DeviceHolder|
185  const vk::PhysicalDevice& GetPhysicalDevice() const override {
186  return physical_device;
187  }
188 
189  vk::UniqueInstance instance;
190  vk::PhysicalDevice physical_device;
191  vk::UniqueDevice device;
192  };
193 
194  std::shared_ptr<DeviceHolderImpl> device_holder_;
195  std::unique_ptr<DriverInfoVK> driver_info_;
196  std::unique_ptr<DebugReportVK> debug_report_;
197  std::shared_ptr<Allocator> allocator_;
198  std::shared_ptr<ShaderLibraryVK> shader_library_;
199  std::shared_ptr<SamplerLibraryVK> sampler_library_;
200  std::shared_ptr<PipelineLibraryVK> pipeline_library_;
201  std::shared_ptr<YUVConversionLibraryVK> yuv_conversion_library_;
202  QueuesVK queues_;
203  std::shared_ptr<const Capabilities> device_capabilities_;
204  std::shared_ptr<FenceWaiterVK> fence_waiter_;
205  std::shared_ptr<ResourceManagerVK> resource_manager_;
206  std::shared_ptr<CommandPoolRecyclerVK> command_pool_recycler_;
207  std::string device_name_;
208  std::shared_ptr<fml::ConcurrentMessageLoop> raster_message_loop_;
209  std::shared_ptr<GPUTracerVK> gpu_tracer_;
210  std::shared_ptr<DescriptorPoolRecyclerVK> descriptor_pool_recycler_;
211  std::shared_ptr<CommandQueue> command_queue_vk_;
212  bool should_disable_surface_control_ = false;
213 
214  const uint64_t hash_;
215 
216  bool is_valid_ = false;
217 
218  ContextVK();
219 
220  void Setup(Settings settings);
221 
222  ContextVK(const ContextVK&) = delete;
223 
224  ContextVK& operator=(const ContextVK&) = delete;
225 };
226 
227 } // namespace impeller
228 
229 #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:555
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:535
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:137
impeller::ContextVK::SetDebugName
static bool SetDebugName(const vk::Device &device, T handle, std::string_view label)
Definition: context_vk.h:114
impeller::ContextVK::GetCommandQueue
std::shared_ptr< CommandQueue > GetCommandQueue() const override
Return the graphics queue for submitting command buffers.
Definition: context_vk.cc:589
impeller::Context::BackendType
BackendType
Definition: context.h:48
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:472
impeller::ContextVK::GetInstance
vk::Instance GetInstance() const
Definition: context_vk.cc:526
impeller::ContextVK::Settings::enable_validation
bool enable_validation
Definition: context_vk.h:50
impeller::ContextVK::GetPhysicalDevice
vk::PhysicalDevice GetPhysicalDevice() const
Definition: context_vk.cc:563
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:476
impeller::ContextVK::GetBackendType
BackendType GetBackendType() const override
Get the graphics backend of an Impeller context.
Definition: context_vk.cc:132
command_queue.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:492
formats.h
impeller::ContextVK::GetHash
uint64_t GetHash() const
Definition: context_vk.h:68
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
impeller::ContextVK::Settings::disable_surface_control
bool disable_surface_control
Definition: context_vk.h:52
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:54
impeller::ContextVK::CreateSurfaceContext
std::shared_ptr< SurfaceContextVK > CreateSurfaceContext()
Definition: context_vk.cc:551
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:480
impeller::ContextVK::GetShouldDisableSurfaceControlSwapchain
bool GetShouldDisableSurfaceControlSwapchain() const
Whether the Android Surface control based swapchain should be disabled, even if the device is capable...
Definition: context_vk.cc:645
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:559
impeller::ContextVK::GetDescriptorPoolRecycler
std::shared_ptr< DescriptorPoolRecyclerVK > GetDescriptorPoolRecycler() const
Definition: context_vk.cc:584
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:98
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:109
impeller::ContextVK::GetCommandPoolRecycler
std::shared_ptr< CommandPoolRecyclerVK > GetCommandPoolRecycler() const
Definition: context_vk.cc:575
impeller::ContextVK::GetGPUTracer
std::shared_ptr< GPUTracerVK > GetGPUTracer() const
Definition: context_vk.cc:580
shader_library_vk.h
impeller::ContextVK
Definition: context_vk.h:42
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91
impeller::ContextVK::InitializeCommonlyUsedShadersIfNeeded
void InitializeCommonlyUsedShadersIfNeeded() const override
Definition: context_vk.cc:596
impeller::ContextVK::~ContextVK
~ContextVK() override
Definition: context_vk.cc:125
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:484
impeller::Context
To do anything rendering related with Impeller, you need a context.
Definition: context.h:46
impeller::ContextVK::GetDevice
const vk::Device & GetDevice() const
Definition: context_vk.cc:530
impeller::BackendCast
Definition: backend_cast.h:11
impeller::ContextVK::ChooseThreadCountForWorkers
static size_t ChooseThreadCountForWorkers(size_t hardware_concurrency)
Definition: context_vk.cc:108
impeller::ContextVK::GetResourceManager
std::shared_ptr< ResourceManagerVK > GetResourceManager() const
Definition: context_vk.cc:571
impeller::ContextVK::GetDriverInfo
const std::unique_ptr< DriverInfoVK > & GetDriverInfo() const
Definition: context_vk.cc:641
context.h
impeller::ContextVK::GetYUVConversionLibrary
const std::shared_ptr< YUVConversionLibraryVK > & GetYUVConversionLibrary() const
Definition: context_vk.cc:637
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:488
impeller::ContextVK::DescribeGpuModel
std::string DescribeGpuModel() const override
Definition: context_vk.cc:468
impeller::ContextVK::GetFenceWaiter
std::shared_ptr< FenceWaiterVK > GetFenceWaiter() const
Definition: context_vk.cc:567
impeller::ContextVK::SetOffscreenFormat
void SetOffscreenFormat(PixelFormat pixel_format)
Definition: context_vk.cc:463
sampler_library_vk.h
impeller::HasValidationLayers
bool HasValidationLayers()
Definition: context_vk.cc:46
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:539
impeller
Definition: allocation.cc:12
impeller::ContextVK::DisposeThreadLocalCachedResources
void DisposeThreadLocalCachedResources() override
Definition: context_vk.cc:632