vulkan_implementation.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_IMPLEMENTATION_H_
  5. #define GPU_VULKAN_VULKAN_IMPLEMENTATION_H_
  6. #include <vulkan/vulkan_core.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/component_export.h"
  10. #include "build/build_config.h"
  11. #include "gpu/vulkan/semaphore_handle.h"
  12. #include "ui/gfx/buffer_types.h"
  13. #include "ui/gfx/geometry/size.h"
  14. #include "ui/gfx/gpu_memory_buffer.h"
  15. #include "ui/gfx/native_widget_types.h"
  16. #if BUILDFLAG(IS_ANDROID)
  17. #include "base/android/scoped_hardware_buffer_handle.h"
  18. #include "ui/gfx/geometry/size.h"
  19. #endif
  20. namespace gfx {
  21. class GpuFence;
  22. struct GpuMemoryBufferHandle;
  23. } // namespace gfx
  24. namespace gpu {
  25. class VulkanDeviceQueue;
  26. class VulkanSurface;
  27. class VulkanImage;
  28. class VulkanInstance;
  29. struct GPUInfo;
  30. struct VulkanYCbCrInfo;
  31. #if BUILDFLAG(IS_FUCHSIA)
  32. class SysmemBufferCollection {
  33. public:
  34. virtual ~SysmemBufferCollection() {}
  35. };
  36. #endif // BUILDFLAG(IS_FUCHSIA)
  37. // Base class which provides functions for creating vulkan objects for different
  38. // platforms that use platform-specific extensions (e.g. for creation of
  39. // VkSurfaceKHR objects). It also provides helper/utility functions.
  40. class COMPONENT_EXPORT(VULKAN) VulkanImplementation {
  41. public:
  42. explicit VulkanImplementation(bool use_swiftshader = false,
  43. bool allow_protected_memory = false);
  44. VulkanImplementation(const VulkanImplementation&) = delete;
  45. VulkanImplementation& operator=(const VulkanImplementation&) = delete;
  46. virtual ~VulkanImplementation();
  47. // Initialize VulkanInstance. If using_surface, VK_KHR_surface instance
  48. // extension will be required when initialize VkInstance. This extension is
  49. // required for presenting with VkSwapchain API.
  50. virtual bool InitializeVulkanInstance(bool using_surface = true) = 0;
  51. virtual VulkanInstance* GetVulkanInstance() = 0;
  52. virtual std::unique_ptr<VulkanSurface> CreateViewSurface(
  53. gfx::AcceleratedWidget window) = 0;
  54. virtual bool GetPhysicalDevicePresentationSupport(
  55. VkPhysicalDevice device,
  56. const std::vector<VkQueueFamilyProperties>& queue_family_properties,
  57. uint32_t queue_family_index) = 0;
  58. virtual std::vector<const char*> GetRequiredDeviceExtensions() = 0;
  59. virtual std::vector<const char*> GetOptionalDeviceExtensions() = 0;
  60. // Creates a VkFence that is exportable to a gfx::GpuFence.
  61. virtual VkFence CreateVkFenceForGpuFence(VkDevice vk_device) = 0;
  62. // Exports a VkFence to a gfx::GpuFence.
  63. //
  64. // The fence should have been created via CreateVkFenceForGpuFence().
  65. virtual std::unique_ptr<gfx::GpuFence> ExportVkFenceToGpuFence(
  66. VkDevice vk_device,
  67. VkFence vk_fence) = 0;
  68. // Creates a semaphore that can be exported using GetSemaphoreHandle().
  69. virtual VkSemaphore CreateExternalSemaphore(VkDevice vk_device) = 0;
  70. // Import a VkSemaphore from a platform-specific handle.
  71. // Handle types that don't allow permanent import are imported with
  72. // temporary permanence (VK_SEMAPHORE_IMPORT_TEMPORARY_BIT).
  73. virtual VkSemaphore ImportSemaphoreHandle(VkDevice vk_device,
  74. SemaphoreHandle handle) = 0;
  75. // Export a platform-specific handle for a Vulkan semaphore. Returns a null
  76. // handle in case of a failure.
  77. virtual SemaphoreHandle GetSemaphoreHandle(VkDevice vk_device,
  78. VkSemaphore vk_semaphore) = 0;
  79. // Returns VkExternalMemoryHandleTypeFlagBits that should be set when creating
  80. // external images and memory.
  81. virtual VkExternalMemoryHandleTypeFlagBits GetExternalImageHandleType() = 0;
  82. // Returns true if the GpuMemoryBuffer of the specified type can be imported
  83. // into VkImage using CreateImageFromGpuMemoryHandle().
  84. virtual bool CanImportGpuMemoryBuffer(
  85. VulkanDeviceQueue* device_queue,
  86. gfx::GpuMemoryBufferType memory_buffer_type) = 0;
  87. // Creates a VkImage from a GpuMemoryBuffer. Implementation must verify that
  88. // the specified |size| fits in the size specified when |gmb_handle| was
  89. // allocated.
  90. virtual std::unique_ptr<VulkanImage> CreateImageFromGpuMemoryHandle(
  91. VulkanDeviceQueue* device_queue,
  92. gfx::GpuMemoryBufferHandle gmb_handle,
  93. gfx::Size size,
  94. VkFormat vk_format) = 0;
  95. #if BUILDFLAG(IS_ANDROID)
  96. // Get the sampler ycbcr conversion information from the AHB.
  97. virtual bool GetSamplerYcbcrConversionInfo(
  98. const VkDevice& vk_device,
  99. base::android::ScopedHardwareBufferHandle ahb_handle,
  100. VulkanYCbCrInfo* ycbcr_info) = 0;
  101. #endif
  102. #if BUILDFLAG(IS_FUCHSIA)
  103. // Registers as sysmem buffer collection. The collection can be released by
  104. // destroying the returned SysmemBufferCollection object. Once a collection is
  105. // registered the individual buffers in the collection can be referenced by
  106. // using the |id| as |buffer_collection_id| in |gmb_handle| passed to
  107. // CreateImageFromGpuMemoryHandle().
  108. virtual std::unique_ptr<SysmemBufferCollection>
  109. RegisterSysmemBufferCollection(VkDevice device,
  110. gfx::SysmemBufferCollectionId id,
  111. zx::channel token,
  112. gfx::BufferFormat format,
  113. gfx::BufferUsage usage,
  114. gfx::Size size,
  115. size_t min_buffer_count,
  116. bool register_with_image_pipe) = 0;
  117. #endif // BUILDFLAG(IS_FUCHSIA)
  118. bool use_swiftshader() const { return use_swiftshader_; }
  119. bool allow_protected_memory() const { return allow_protected_memory_; }
  120. private:
  121. const bool use_swiftshader_;
  122. const bool allow_protected_memory_;
  123. };
  124. COMPONENT_EXPORT(VULKAN)
  125. std::unique_ptr<VulkanDeviceQueue> CreateVulkanDeviceQueue(
  126. VulkanImplementation* vulkan_implementation,
  127. uint32_t option,
  128. const GPUInfo* gpu_info = nullptr,
  129. uint32_t heap_memory_limit = 0);
  130. } // namespace gpu
  131. #endif // GPU_VULKAN_VULKAN_IMPLEMENTATION_H_