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 #pragma once
6 
7 #include <memory>
8 
9 #include "flutter/fml/concurrent_message_loop.h"
10 #include "flutter/fml/macros.h"
11 #include "flutter/fml/mapping.h"
12 #include "flutter/fml/unique_fd.h"
14 #include "impeller/core/formats.h"
23 
24 namespace impeller {
25 
26 bool HasValidationLayers();
27 
28 class CommandEncoderFactoryVK;
29 class CommandEncoderVK;
30 class CommandPoolRecyclerVK;
31 class DebugReportVK;
32 class FenceWaiterVK;
33 class ResourceManagerVK;
34 class SurfaceContextVK;
35 
36 class ContextVK final : public Context,
37  public BackendCast<ContextVK, Context>,
38  public std::enable_shared_from_this<ContextVK> {
39  public:
40  struct Settings {
41  PFN_vkGetInstanceProcAddr proc_address_callback = nullptr;
42  std::vector<std::shared_ptr<fml::Mapping>> shader_libraries_data;
43  fml::UniqueFD cache_directory;
44  bool enable_validation = false;
45 
46  Settings() = default;
47 
48  Settings(Settings&&) = default;
49  };
50 
51  static std::shared_ptr<ContextVK> Create(Settings settings);
52 
53  uint64_t GetHash() const { return hash_; }
54 
55  // |Context|
56  ~ContextVK() override;
57 
58  // |Context|
59  BackendType GetBackendType() const override;
60 
61  // |Context|
62  std::string DescribeGpuModel() const override;
63 
64  // |Context|
65  bool IsValid() const override;
66 
67  // |Context|
68  std::shared_ptr<Allocator> GetResourceAllocator() const override;
69 
70  // |Context|
71  std::shared_ptr<ShaderLibrary> GetShaderLibrary() const override;
72 
73  // |Context|
74  std::shared_ptr<SamplerLibrary> GetSamplerLibrary() const override;
75 
76  // |Context|
77  std::shared_ptr<PipelineLibrary> GetPipelineLibrary() const override;
78 
79  // |Context|
80  std::shared_ptr<CommandBuffer> CreateCommandBuffer() const override;
81 
82  // |Context|
83  const std::shared_ptr<const Capabilities>& GetCapabilities() const override;
84 
85  // |Context|
86  void Shutdown() override;
87 
88  // |Context|
89  void SetSyncPresentation(bool value) override { sync_presentation_ = value; }
90 
91  bool GetSyncPresentation() const { return sync_presentation_; }
92 
93  void SetOffscreenFormat(PixelFormat pixel_format);
94 
95  template <typename T>
96  bool SetDebugName(T handle, std::string_view label) const {
97  return SetDebugName(GetDevice(), handle, label);
98  }
99 
100  template <typename T>
101  static bool SetDebugName(const vk::Device& device,
102  T handle,
103  std::string_view label) {
104  if (!HasValidationLayers()) {
105  // No-op if validation layers are not enabled.
106  return true;
107  }
108 
109  auto c_handle = static_cast<typename T::CType>(handle);
110 
111  vk::DebugUtilsObjectNameInfoEXT info;
112  info.objectType = T::objectType;
113  info.pObjectName = label.data();
114  info.objectHandle = reinterpret_cast<decltype(info.objectHandle)>(c_handle);
115 
116  if (device.setDebugUtilsObjectNameEXT(info) != vk::Result::eSuccess) {
117  VALIDATION_LOG << "Unable to set debug name: " << label;
118  return false;
119  }
120 
121  return true;
122  }
123 
124  std::shared_ptr<DeviceHolder> GetDeviceHolder() const {
125  return device_holder_;
126  }
127 
128  vk::Instance GetInstance() const;
129 
130  const vk::Device& GetDevice() const;
131 
132  const std::shared_ptr<fml::ConcurrentTaskRunner>
134 
135  std::shared_ptr<SurfaceContextVK> CreateSurfaceContext();
136 
137  const std::shared_ptr<QueueVK>& GetGraphicsQueue() const;
138 
139  vk::PhysicalDevice GetPhysicalDevice() const;
140 
141  std::shared_ptr<FenceWaiterVK> GetFenceWaiter() const;
142 
143  std::shared_ptr<ResourceManagerVK> GetResourceManager() const;
144 
145  std::shared_ptr<CommandPoolRecyclerVK> GetCommandPoolRecycler() const;
146 
147  private:
148  struct DeviceHolderImpl : public DeviceHolder {
149  // |DeviceHolder|
150  const vk::Device& GetDevice() const override { return device.get(); }
151  // |DeviceHolder|
152  const vk::PhysicalDevice& GetPhysicalDevice() const override {
153  return physical_device;
154  }
155 
156  vk::UniqueInstance instance;
157  vk::PhysicalDevice physical_device;
158  vk::UniqueDevice device;
159  };
160 
161  std::shared_ptr<DeviceHolderImpl> device_holder_;
162  std::unique_ptr<DebugReportVK> debug_report_;
163  std::shared_ptr<Allocator> allocator_;
164  std::shared_ptr<ShaderLibraryVK> shader_library_;
165  std::shared_ptr<SamplerLibraryVK> sampler_library_;
166  std::shared_ptr<PipelineLibraryVK> pipeline_library_;
167  QueuesVK queues_;
168  std::shared_ptr<const Capabilities> device_capabilities_;
169  std::shared_ptr<FenceWaiterVK> fence_waiter_;
170  std::shared_ptr<ResourceManagerVK> resource_manager_;
171  std::shared_ptr<CommandPoolRecyclerVK> command_pool_recycler_;
172  std::string device_name_;
173  std::shared_ptr<fml::ConcurrentMessageLoop> raster_message_loop_;
174  bool sync_presentation_ = false;
175  const uint64_t hash_;
176 
177  bool is_valid_ = false;
178 
179  ContextVK();
180 
181  void Setup(Settings settings);
182 
183  std::unique_ptr<CommandEncoderFactoryVK> CreateGraphicsCommandEncoderFactory()
184  const;
185 
186  FML_DISALLOW_COPY_AND_ASSIGN(ContextVK);
187 };
188 
189 } // namespace impeller
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:505
impeller::ContextVK::GetConcurrentWorkerTaskRunner
const std::shared_ptr< fml::ConcurrentTaskRunner > GetConcurrentWorkerTaskRunner() const
Definition: context_vk.cc:485
impeller::ContextVK::SetDebugName
static bool SetDebugName(const vk::Device &device, T handle, std::string_view label)
Definition: context_vk.h:101
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:449
impeller::ContextVK::GetInstance
vk::Instance GetInstance() const
Definition: context_vk.cc:476
impeller::ContextVK::Settings::enable_validation
bool enable_validation
Definition: context_vk.h:44
device_holder.h
impeller::ContextVK::GetPhysicalDevice
vk::PhysicalDevice GetPhysicalDevice() const
Definition: context_vk.cc:513
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:453
impeller::DeviceHolder
Definition: device_holder.h:11
impeller::ContextVK::GetBackendType
BackendType GetBackendType() const override
Get the graphics backend of an Impeller context.
Definition: context_vk.cc:119
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:469
impeller::ContextVK::GetHash
uint64_t GetHash() const
Definition: context_vk.h:53
command_pool_vk.h
pipeline_library_vk.h
impeller::ContextVK::Settings
Definition: context_vk.h:40
impeller::ContextVK::CreateSurfaceContext
std::shared_ptr< SurfaceContextVK > CreateSurfaceContext()
Definition: context_vk.cc:501
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:457
impeller::ContextVK::Settings::proc_address_callback
PFN_vkGetInstanceProcAddr proc_address_callback
Definition: context_vk.h:41
impeller::ContextVK::GetGraphicsQueue
const std::shared_ptr< QueueVK > & GetGraphicsQueue() const
Definition: context_vk.cc:509
impeller::ContextVK::GetDeviceHolder
std::shared_ptr< DeviceHolder > GetDeviceHolder() const
Definition: context_vk.h:124
backend_cast.h
impeller::ContextVK::Settings::shader_libraries_data
std::vector< std::shared_ptr< fml::Mapping > > shader_libraries_data
Definition: context_vk.h:42
queue_vk.h
capabilities.h
impeller::ContextVK::Create
static std::shared_ptr< ContextVK > Create(Settings settings)
Definition: context_vk.cc:92
impeller::ContextVK::Settings::Settings
Settings()=default
impeller::ContextVK::SetDebugName
bool SetDebugName(T handle, std::string_view label) const
Definition: context_vk.h:96
impeller::ContextVK::GetCommandPoolRecycler
std::shared_ptr< CommandPoolRecyclerVK > GetCommandPoolRecycler() const
Definition: context_vk.cc:525
shader_library_vk.h
impeller::ContextVK
Definition: context_vk.h:36
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:60
impeller::ContextVK::~ContextVK
~ContextVK() override
Definition: context_vk.cc:112
impeller::ContextVK::Settings::cache_directory
fml::UniqueFD cache_directory
Definition: context_vk.h:43
impeller::ContextVK::GetSamplerLibrary
std::shared_ptr< SamplerLibrary > GetSamplerLibrary() const override
Returns the library of combined image samplers used in shaders.
Definition: context_vk.cc:461
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:480
impeller::BackendCast
Definition: backend_cast.h:12
impeller::ContextVK::GetResourceManager
std::shared_ptr< ResourceManagerVK > GetResourceManager() const
Definition: context_vk.cc:521
impeller::ContextVK::GetSyncPresentation
bool GetSyncPresentation() const
Definition: context_vk.h:91
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:465
impeller::ContextVK::DescribeGpuModel
std::string DescribeGpuModel() const override
Definition: context_vk.cc:445
impeller::ContextVK::GetFenceWaiter
std::shared_ptr< FenceWaiterVK > GetFenceWaiter() const
Definition: context_vk.cc:517
impeller::PixelFormat
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:94
impeller::ContextVK::SetOffscreenFormat
void SetOffscreenFormat(PixelFormat pixel_format)
Definition: context_vk.cc:440
sampler_library_vk.h
impeller::HasValidationLayers
bool HasValidationLayers()
Definition: context_vk.cc:40
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:489
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:89