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 
53  Settings() = default;
54 
55  Settings(Settings&&) = default;
56  };
57 
58  /// Choose the number of worker threads the context_vk will create.
59  ///
60  /// Visible for testing.
61  static size_t ChooseThreadCountForWorkers(size_t hardware_concurrency);
62 
63  static std::shared_ptr<ContextVK> Create(Settings settings);
64 
65  uint64_t GetHash() const { return hash_; }
66 
67  // |Context|
68  ~ContextVK() override;
69 
70  // |Context|
71  BackendType GetBackendType() const override;
72 
73  // |Context|
74  std::string DescribeGpuModel() const override;
75 
76  // |Context|
77  bool IsValid() const override;
78 
79  // |Context|
80  std::shared_ptr<Allocator> GetResourceAllocator() const override;
81 
82  // |Context|
83  std::shared_ptr<ShaderLibrary> GetShaderLibrary() const override;
84 
85  // |Context|
86  std::shared_ptr<SamplerLibrary> GetSamplerLibrary() const override;
87 
88  // |Context|
89  std::shared_ptr<PipelineLibrary> GetPipelineLibrary() const override;
90 
91  // |Context|
92  std::shared_ptr<CommandBuffer> CreateCommandBuffer() const override;
93 
94  // |Context|
95  const std::shared_ptr<const Capabilities>& GetCapabilities() const override;
96 
97  const std::shared_ptr<YUVConversionLibraryVK>& GetYUVConversionLibrary()
98  const;
99 
100  // |Context|
101  void Shutdown() override;
102 
103  void SetOffscreenFormat(PixelFormat pixel_format);
104 
105  template <typename T>
106  bool SetDebugName(T handle, std::string_view label) const {
107  return SetDebugName(GetDevice(), handle, label);
108  }
109 
110  template <typename T>
111  static bool SetDebugName(const vk::Device& device,
112  T handle,
113  std::string_view label) {
114  if (!HasValidationLayers()) {
115  // No-op if validation layers are not enabled.
116  return true;
117  }
118 
119  auto c_handle = static_cast<typename T::CType>(handle);
120 
121  vk::DebugUtilsObjectNameInfoEXT info;
122  info.objectType = T::objectType;
123  info.pObjectName = label.data();
124  info.objectHandle = reinterpret_cast<decltype(info.objectHandle)>(c_handle);
125 
126  if (device.setDebugUtilsObjectNameEXT(info) != vk::Result::eSuccess) {
127  VALIDATION_LOG << "Unable to set debug name: " << label;
128  return false;
129  }
130 
131  return true;
132  }
133 
134  std::shared_ptr<DeviceHolderVK> GetDeviceHolder() const {
135  return device_holder_;
136  }
137 
138  vk::Instance GetInstance() const;
139 
140  const vk::Device& GetDevice() const;
141 
142  const std::unique_ptr<DriverInfoVK>& GetDriverInfo() const;
143 
144  const std::shared_ptr<fml::ConcurrentTaskRunner>
146 
147  std::shared_ptr<SurfaceContextVK> CreateSurfaceContext();
148 
149  const std::shared_ptr<QueueVK>& GetGraphicsQueue() const;
150 
151  vk::PhysicalDevice GetPhysicalDevice() const;
152 
153  std::shared_ptr<FenceWaiterVK> GetFenceWaiter() const;
154 
155  std::shared_ptr<ResourceManagerVK> GetResourceManager() const;
156 
157  std::shared_ptr<CommandPoolRecyclerVK> GetCommandPoolRecycler() const;
158 
159  std::shared_ptr<DescriptorPoolRecyclerVK> GetDescriptorPoolRecycler() const;
160 
161  std::shared_ptr<CommandQueue> GetCommandQueue() const override;
162 
163  std::shared_ptr<GPUTracerVK> GetGPUTracer() const;
164 
165  void RecordFrameEndTime() const;
166 
167  void InitializeCommonlyUsedShadersIfNeeded() const override;
168 
169  private:
170  struct DeviceHolderImpl : public DeviceHolderVK {
171  // |DeviceHolder|
172  const vk::Device& GetDevice() const override { return device.get(); }
173  // |DeviceHolder|
174  const vk::PhysicalDevice& GetPhysicalDevice() const override {
175  return physical_device;
176  }
177 
178  vk::UniqueInstance instance;
179  vk::PhysicalDevice physical_device;
180  vk::UniqueDevice device;
181  };
182 
183  std::shared_ptr<DeviceHolderImpl> device_holder_;
184  std::unique_ptr<DriverInfoVK> driver_info_;
185  std::unique_ptr<DebugReportVK> debug_report_;
186  std::shared_ptr<Allocator> allocator_;
187  std::shared_ptr<ShaderLibraryVK> shader_library_;
188  std::shared_ptr<SamplerLibraryVK> sampler_library_;
189  std::shared_ptr<PipelineLibraryVK> pipeline_library_;
190  std::shared_ptr<YUVConversionLibraryVK> yuv_conversion_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::shared_ptr<GPUTracerVK> gpu_tracer_;
199  std::shared_ptr<DescriptorPoolRecyclerVK> descriptor_pool_recycler_;
200  std::shared_ptr<CommandQueue> command_queue_vk_;
201 
202  const uint64_t hash_;
203 
204  bool is_valid_ = false;
205 
206  ContextVK();
207 
208  void Setup(Settings settings);
209 
210  std::unique_ptr<CommandEncoderFactoryVK> CreateGraphicsCommandEncoderFactory()
211  const;
212 
213  ContextVK(const ContextVK&) = delete;
214 
215  ContextVK& operator=(const ContextVK&) = delete;
216 };
217 
218 } // namespace impeller
219 
220 #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:539
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:519
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:134
impeller::ContextVK::SetDebugName
static bool SetDebugName(const vk::Device &device, T handle, std::string_view label)
Definition: context_vk.h:111
impeller::ContextVK::GetCommandQueue
std::shared_ptr< CommandQueue > GetCommandQueue() const override
Return the graphics queue for submitting command buffers.
Definition: context_vk.cc:578
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:483
impeller::ContextVK::GetInstance
vk::Instance GetInstance() const
Definition: context_vk.cc:510
impeller::ContextVK::Settings::enable_validation
bool enable_validation
Definition: context_vk.h:50
impeller::ContextVK::GetPhysicalDevice
vk::PhysicalDevice GetPhysicalDevice() const
Definition: context_vk.cc:547
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:487
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:503
impeller::ContextVK::GetHash
uint64_t GetHash() const
Definition: context_vk.h:65
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:100
device_holder_vk.h
command_pool_vk.h
pipeline_library_vk.h
impeller::ContextVK::Settings
Definition: context_vk.h:46
impeller::ContextVK::CreateSurfaceContext
std::shared_ptr< SurfaceContextVK > CreateSurfaceContext()
Definition: context_vk.cc:535
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:491
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:543
impeller::ContextVK::GetDescriptorPoolRecycler
std::shared_ptr< DescriptorPoolRecyclerVK > GetDescriptorPoolRecycler() const
Definition: context_vk.cc:573
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:106
impeller::ContextVK::GetCommandPoolRecycler
std::shared_ptr< CommandPoolRecyclerVK > GetCommandPoolRecycler() const
Definition: context_vk.cc:559
impeller::ContextVK::GetGPUTracer
std::shared_ptr< GPUTracerVK > GetGPUTracer() const
Definition: context_vk.cc:569
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:585
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:495
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:514
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:555
impeller::ContextVK::GetDriverInfo
const std::unique_ptr< DriverInfoVK > & GetDriverInfo() const
Definition: context_vk.cc:626
context.h
impeller::ContextVK::GetYUVConversionLibrary
const std::shared_ptr< YUVConversionLibraryVK > & GetYUVConversionLibrary() const
Definition: context_vk.cc:622
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:499
impeller::ContextVK::DescribeGpuModel
std::string DescribeGpuModel() const override
Definition: context_vk.cc:479
impeller::ContextVK::GetFenceWaiter
std::shared_ptr< FenceWaiterVK > GetFenceWaiter() const
Definition: context_vk.cc:551
impeller::ContextVK::SetOffscreenFormat
void SetOffscreenFormat(PixelFormat pixel_format)
Definition: context_vk.cc:474
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:523
impeller
Definition: aiks_blur_unittests.cc:20