vulkan_device_queue.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright (c) 2016 The Chromium 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. #ifndef GPU_VULKAN_VULKAN_DEVICE_QUEUE_H_
  5. #define GPU_VULKAN_VULKAN_DEVICE_QUEUE_H_
  6. #include <vulkan/vulkan_core.h>
  7. #include <memory>
  8. #include "base/callback.h"
  9. #include "base/check_op.h"
  10. #include "base/component_export.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "build/build_config.h"
  13. #include "gpu/vulkan/vma_wrapper.h"
  14. #include "gpu/vulkan/vulkan_instance.h"
  15. #include "ui/gfx/extension_set.h"
  16. namespace gpu {
  17. class VulkanCommandPool;
  18. class VulkanFenceHelper;
  19. struct GPUInfo;
  20. class COMPONENT_EXPORT(VULKAN) VulkanDeviceQueue {
  21. public:
  22. enum DeviceQueueOption {
  23. GRAPHICS_QUEUE_FLAG = 0x01,
  24. PRESENTATION_SUPPORT_QUEUE_FLAG = 0x02,
  25. };
  26. explicit VulkanDeviceQueue(VkInstance vk_instance);
  27. explicit VulkanDeviceQueue(VulkanInstance* instance);
  28. VulkanDeviceQueue(const VulkanDeviceQueue&) = delete;
  29. VulkanDeviceQueue& operator=(const VulkanDeviceQueue&) = delete;
  30. ~VulkanDeviceQueue();
  31. using GetPresentationSupportCallback =
  32. base::RepeatingCallback<bool(VkPhysicalDevice,
  33. const std::vector<VkQueueFamilyProperties>&,
  34. uint32_t queue_family_index)>;
  35. bool Initialize(
  36. uint32_t options,
  37. const GPUInfo* gpu_info,
  38. const std::vector<const char*>& required_extensions,
  39. const std::vector<const char*>& optional_extensions,
  40. bool allow_protected_memory,
  41. const GetPresentationSupportCallback& get_presentation_support,
  42. uint32_t heap_memory_limit);
  43. bool InitializeFromANGLE();
  44. bool InitializeForWebView(VkPhysicalDevice vk_physical_device,
  45. VkDevice vk_device,
  46. VkQueue vk_queue,
  47. uint32_t vk_queue_index,
  48. gfx::ExtensionSet enabled_extensions);
  49. // To be used by CompositorGpuThread when DrDc is enabled. CompositorGpuThread
  50. // will use same |vk_device| and |vk_queue| as the gpu main thread but will
  51. // have its own instance of VulkanFenceHelper and VmaAllocator. Also note that
  52. // this CompositorGpuThread does not own the |vk_device| and |vk_queue| and
  53. // hence will not destroy them.
  54. bool InitializeForCompositorGpuThread(
  55. VkPhysicalDevice vk_physical_device,
  56. VkDevice vk_device,
  57. VkQueue vk_queue,
  58. uint32_t vk_queue_index,
  59. gfx::ExtensionSet enabled_extensions,
  60. const VkPhysicalDeviceFeatures2& vk_physical_device_features2);
  61. const gfx::ExtensionSet& enabled_extensions() const {
  62. return enabled_extensions_;
  63. }
  64. void Destroy();
  65. VkPhysicalDevice GetVulkanPhysicalDevice() const {
  66. DCHECK_NE(static_cast<VkPhysicalDevice>(VK_NULL_HANDLE),
  67. vk_physical_device_);
  68. return vk_physical_device_;
  69. }
  70. const VkPhysicalDeviceProperties& vk_physical_device_properties() const {
  71. return vk_physical_device_properties_;
  72. }
  73. const VkPhysicalDeviceDriverProperties& vk_physical_device_driver_properties()
  74. const {
  75. return vk_physical_device_driver_properties_;
  76. }
  77. VkDevice GetVulkanDevice() const {
  78. DCHECK_NE(static_cast<VkDevice>(VK_NULL_HANDLE), vk_device_);
  79. return vk_device_;
  80. }
  81. VkQueue GetVulkanQueue() const {
  82. DCHECK_NE(static_cast<VkQueue>(VK_NULL_HANDLE), vk_queue_);
  83. return vk_queue_;
  84. }
  85. VkInstance GetVulkanInstance() const { return vk_instance_; }
  86. uint32_t GetVulkanQueueIndex() const { return vk_queue_index_; }
  87. std::unique_ptr<gpu::VulkanCommandPool> CreateCommandPool();
  88. VmaAllocator vma_allocator() const { return vma_allocator_; }
  89. VulkanFenceHelper* GetFenceHelper() const { return cleanup_helper_.get(); }
  90. const VkPhysicalDeviceFeatures2& enabled_device_features_2() const {
  91. if (enabled_device_features_2_from_angle_)
  92. return *enabled_device_features_2_from_angle_;
  93. return enabled_device_features_2_;
  94. }
  95. const VkPhysicalDeviceFeatures& enabled_device_features() const {
  96. if (enabled_device_features_2_from_angle_)
  97. return enabled_device_features_2_from_angle_->features;
  98. return enabled_device_features_2_.features;
  99. }
  100. bool allow_protected_memory() const { return allow_protected_memory_; }
  101. private:
  102. // Common Init method to be used by both webview and compositor gpu thread.
  103. bool InitCommon(VkPhysicalDevice vk_physical_device,
  104. VkDevice vk_device,
  105. VkQueue vk_queue,
  106. uint32_t vk_queue_index,
  107. gfx::ExtensionSet enabled_extensions);
  108. gfx::ExtensionSet enabled_extensions_;
  109. VkPhysicalDevice vk_physical_device_ = VK_NULL_HANDLE;
  110. VkPhysicalDeviceProperties vk_physical_device_properties_;
  111. VkPhysicalDeviceDriverProperties vk_physical_device_driver_properties_;
  112. VkDevice owned_vk_device_ = VK_NULL_HANDLE;
  113. VkDevice vk_device_ = VK_NULL_HANDLE;
  114. VkQueue vk_queue_ = VK_NULL_HANDLE;
  115. uint32_t vk_queue_index_ = 0;
  116. VkInstance vk_instance_ = VK_NULL_HANDLE;
  117. raw_ptr<VulkanInstance> instance_ = nullptr;
  118. VmaAllocator vma_allocator_ = VK_NULL_HANDLE;
  119. std::unique_ptr<VulkanFenceHelper> cleanup_helper_;
  120. VkPhysicalDeviceFeatures2 enabled_device_features_2_{
  121. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2};
  122. raw_ptr<const VkPhysicalDeviceFeatures2>
  123. enabled_device_features_2_from_angle_ = nullptr;
  124. bool allow_protected_memory_ = false;
  125. #if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_FUCHSIA) || BUILDFLAG(IS_LINUX)
  126. VkPhysicalDeviceSamplerYcbcrConversionFeatures
  127. sampler_ycbcr_conversion_features_{
  128. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES};
  129. #endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_FUCHSIA) || BUILDFLAG(IS_LINUX)
  130. VkPhysicalDeviceProtectedMemoryFeatures protected_memory_features_{
  131. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES};
  132. };
  133. } // namespace gpu
  134. #endif // GPU_VULKAN_VULKAN_DEVICE_QUEUE_H_