dyen | 2ce3e05 | 2016-03-09 21:03:49 | [diff] [blame] | 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 | |
| 5 | #ifndef GPU_VULKAN_VULKAN_IMPLEMENTATION_H_ |
| 6 | #define GPU_VULKAN_VULKAN_IMPLEMENTATION_H_ |
| 7 | |
| 8 | #include <vulkan/vulkan.h> |
Peng Huang | fdcb746 | 2019-03-18 17:25:51 | [diff] [blame] | 9 | |
Michael Spang | 7509e6d | 2018-05-16 17:08:28 | [diff] [blame] | 10 | #include <memory> |
Peng Huang | fdcb746 | 2019-03-18 17:25:51 | [diff] [blame] | 11 | #include <vector> |
dyen | 2ce3e05 | 2016-03-09 21:03:49 | [diff] [blame] | 12 | |
Peng Huang | 0a13bf5 | 2020-05-05 16:01:05 | [diff] [blame] | 13 | #include "base/component_export.h" |
Michael Spang | 7509e6d | 2018-05-16 17:08:28 | [diff] [blame] | 14 | #include "base/macros.h" |
Sergey Ulanov | 6ff66f2 | 2019-08-14 00:38:39 | [diff] [blame] | 15 | #include "base/optional.h" |
Vikas Soni | 7af4736 | 2018-11-27 23:20:30 | [diff] [blame] | 16 | #include "build/build_config.h" |
Sergey Ulanov | ecc1be44 | 2019-04-01 23:24:24 | [diff] [blame] | 17 | #include "gpu/vulkan/semaphore_handle.h" |
Sergey Ulanov | 521db4a2 | 2019-05-01 23:59:21 | [diff] [blame] | 18 | #include "ui/gfx/buffer_types.h" |
| 19 | #include "ui/gfx/geometry/size.h" |
| 20 | #include "ui/gfx/gpu_memory_buffer.h" |
Michael Spang | 7509e6d | 2018-05-16 17:08:28 | [diff] [blame] | 21 | #include "ui/gfx/native_widget_types.h" |
| 22 | |
Vikas Soni | a54d576 | 2018-12-08 00:43:45 | [diff] [blame] | 23 | #if defined(OS_ANDROID) |
| 24 | #include "base/android/scoped_hardware_buffer_handle.h" |
Vikas Soni | a54d576 | 2018-12-08 00:43:45 | [diff] [blame] | 25 | #include "ui/gfx/geometry/size.h" |
| 26 | #endif |
| 27 | |
Michael Spang | 7b9bcca | 2018-07-03 17:10:50 | [diff] [blame] | 28 | namespace gfx { |
| 29 | class GpuFence; |
Sergey Ulanov | 521db4a2 | 2019-05-01 23:59:21 | [diff] [blame] | 30 | struct GpuMemoryBufferHandle; |
| 31 | } // namespace gfx |
Michael Spang | 7b9bcca | 2018-07-03 17:10:50 | [diff] [blame] | 32 | |
dyen | 2ce3e05 | 2016-03-09 21:03:49 | [diff] [blame] | 33 | namespace gpu { |
Michael Spang | 7509e6d | 2018-05-16 17:08:28 | [diff] [blame] | 34 | class VulkanDeviceQueue; |
| 35 | class VulkanSurface; |
Peng Huang | aec06f1 | 2020-03-12 19:40:25 | [diff] [blame] | 36 | class VulkanImage; |
Sergey Ulanov | af257db | 2019-01-28 20:13:52 | [diff] [blame] | 37 | class VulkanInstance; |
Peng Huang | ea21d6f | 2020-05-14 18:22:22 | [diff] [blame] | 38 | struct GPUInfo; |
Vikas Soni | ff90d24 | 2019-05-09 02:53:51 | [diff] [blame] | 39 | struct VulkanYCbCrInfo; |
Vikas Soni | 7018c62e | 2019-05-02 23:13:11 | [diff] [blame] | 40 | |
Sergey Ulanov | 7b343ff | 2019-08-15 07:52:37 | [diff] [blame] | 41 | #if defined(OS_FUCHSIA) |
| 42 | class SysmemBufferCollection { |
| 43 | public: |
| 44 | virtual ~SysmemBufferCollection() {} |
| 45 | }; |
| 46 | #endif // defined(OS_FUCHSIA) |
| 47 | |
Vikas Soni | cd499af | 2019-01-29 19:00:11 | [diff] [blame] | 48 | // Base class which provides functions for creating vulkan objects for different |
| 49 | // platforms that use platform-specific extensions (e.g. for creation of |
| 50 | // VkSurfaceKHR objects). It also provides helper/utility functions. |
Peng Huang | 0a13bf5 | 2020-05-05 16:01:05 | [diff] [blame] | 51 | class COMPONENT_EXPORT(VULKAN) VulkanImplementation { |
Michael Spang | 7509e6d | 2018-05-16 17:08:28 | [diff] [blame] | 52 | public: |
Emircan Uysaler | d41c9bf | 2019-08-08 20:38:33 | [diff] [blame] | 53 | VulkanImplementation(bool use_swiftshader = false, |
| 54 | bool allow_protected_memory = false, |
| 55 | bool enforce_protected_memory = false); |
Michael Spang | 7509e6d | 2018-05-16 17:08:28 | [diff] [blame] | 56 | |
| 57 | virtual ~VulkanImplementation(); |
| 58 | |
Peng Huang | 556da90 | 2019-03-22 17:49:40 | [diff] [blame] | 59 | // Initialize VulkanInstance. If using_surface, VK_KHR_surface instance |
| 60 | // extension will be required when initialize VkInstance. This extension is |
| 61 | // required for presenting with VkSwapchain API. |
| 62 | virtual bool InitializeVulkanInstance(bool using_surface = true) = 0; |
Michael Spang | 7509e6d | 2018-05-16 17:08:28 | [diff] [blame] | 63 | |
Sergey Ulanov | af257db | 2019-01-28 20:13:52 | [diff] [blame] | 64 | virtual VulkanInstance* GetVulkanInstance() = 0; |
Michael Spang | 7509e6d | 2018-05-16 17:08:28 | [diff] [blame] | 65 | |
| 66 | virtual std::unique_ptr<VulkanSurface> CreateViewSurface( |
| 67 | gfx::AcceleratedWidget window) = 0; |
| 68 | |
| 69 | virtual bool GetPhysicalDevicePresentationSupport( |
| 70 | VkPhysicalDevice device, |
| 71 | const std::vector<VkQueueFamilyProperties>& queue_family_properties, |
| 72 | uint32_t queue_family_index) = 0; |
| 73 | |
Michael Spang | 4e46bc2 | 2018-06-21 23:07:51 | [diff] [blame] | 74 | virtual std::vector<const char*> GetRequiredDeviceExtensions() = 0; |
Peng Huang | 576415de | 2020-03-02 20:04:32 | [diff] [blame] | 75 | virtual std::vector<const char*> GetOptionalDeviceExtensions() = 0; |
Michael Spang | 4e46bc2 | 2018-06-21 23:07:51 | [diff] [blame] | 76 | |
Michael Spang | 7b9bcca | 2018-07-03 17:10:50 | [diff] [blame] | 77 | // Creates a VkFence that is exportable to a gfx::GpuFence. |
| 78 | virtual VkFence CreateVkFenceForGpuFence(VkDevice vk_device) = 0; |
| 79 | |
| 80 | // Exports a VkFence to a gfx::GpuFence. |
| 81 | // |
| 82 | // The fence should have been created via CreateVkFenceForGpuFence(). |
| 83 | virtual std::unique_ptr<gfx::GpuFence> ExportVkFenceToGpuFence( |
| 84 | VkDevice vk_device, |
| 85 | VkFence vk_fence) = 0; |
| 86 | |
Sergey Ulanov | 520fe0e | 2019-04-02 06:02:55 | [diff] [blame] | 87 | // Creates a semaphore that can be exported using GetSemaphoreHandle(). |
| 88 | virtual VkSemaphore CreateExternalSemaphore(VkDevice vk_device) = 0; |
| 89 | |
Sergey Ulanov | ecc1be44 | 2019-04-01 23:24:24 | [diff] [blame] | 90 | // Import a VkSemaphore from a platform-specific handle. |
Sergey Ulanov | 520fe0e | 2019-04-02 06:02:55 | [diff] [blame] | 91 | // Handle types that don't allow permanent import are imported with |
| 92 | // temporary permanence (VK_SEMAPHORE_IMPORT_TEMPORARY_BIT). |
Sergey Ulanov | ecc1be44 | 2019-04-01 23:24:24 | [diff] [blame] | 93 | virtual VkSemaphore ImportSemaphoreHandle(VkDevice vk_device, |
| 94 | SemaphoreHandle handle) = 0; |
Vikas Soni | 7af4736 | 2018-11-27 23:20:30 | [diff] [blame] | 95 | |
Sergey Ulanov | ecc1be44 | 2019-04-01 23:24:24 | [diff] [blame] | 96 | // Export a platform-specific handle for a Vulkan semaphore. Returns a null |
| 97 | // handle in case of a failure. |
| 98 | virtual SemaphoreHandle GetSemaphoreHandle(VkDevice vk_device, |
| 99 | VkSemaphore vk_semaphore) = 0; |
Vikas Soni | a54d576 | 2018-12-08 00:43:45 | [diff] [blame] | 100 | |
Sergey Ulanov | a128988 | 2019-04-04 19:36:33 | [diff] [blame] | 101 | // Returns VkExternalMemoryHandleTypeFlagBits that should be set when creating |
| 102 | // external images and memory. |
| 103 | virtual VkExternalMemoryHandleTypeFlagBits GetExternalImageHandleType() = 0; |
| 104 | |
Sergey Ulanov | 521db4a2 | 2019-05-01 23:59:21 | [diff] [blame] | 105 | // Returns true if the GpuMemoryBuffer of the specified type can be imported |
| 106 | // into VkImage using CreateImageFromGpuMemoryHandle(). |
| 107 | virtual bool CanImportGpuMemoryBuffer( |
| 108 | gfx::GpuMemoryBufferType memory_buffer_type) = 0; |
| 109 | |
| 110 | // Creates a VkImage from a GpuMemoryBuffer. If successful it initializes |
| 111 | // |vk_image|, |vk_image_info|, |vk_device_memory| and |mem_allocation_size|. |
| 112 | // Implementation must verify that the specified |size| fits in the size |
| 113 | // specified when |gmb_handle| was allocated. |
Peng Huang | aec06f1 | 2020-03-12 19:40:25 | [diff] [blame] | 114 | virtual std::unique_ptr<VulkanImage> CreateImageFromGpuMemoryHandle( |
| 115 | VulkanDeviceQueue* device_queue, |
Sergey Ulanov | 521db4a2 | 2019-05-01 23:59:21 | [diff] [blame] | 116 | gfx::GpuMemoryBufferHandle gmb_handle, |
| 117 | gfx::Size size, |
Peng Huang | aec06f1 | 2020-03-12 19:40:25 | [diff] [blame] | 118 | VkFormat vk_formae) = 0; |
Sergey Ulanov | 521db4a2 | 2019-05-01 23:59:21 | [diff] [blame] | 119 | |
Peng Huang | fdcb746 | 2019-03-18 17:25:51 | [diff] [blame] | 120 | #if defined(OS_ANDROID) |
Vikas Soni | 4a82f69 | 2019-05-15 02:53:41 | [diff] [blame] | 121 | // Get the sampler ycbcr conversion information from the AHB. |
| 122 | virtual bool GetSamplerYcbcrConversionInfo( |
| 123 | const VkDevice& vk_device, |
| 124 | base::android::ScopedHardwareBufferHandle ahb_handle, |
| 125 | VulkanYCbCrInfo* ycbcr_info) = 0; |
Vikas Soni | 7af4736 | 2018-11-27 23:20:30 | [diff] [blame] | 126 | #endif |
| 127 | |
Sergey Ulanov | 7b343ff | 2019-08-15 07:52:37 | [diff] [blame] | 128 | #if defined(OS_FUCHSIA) |
| 129 | // Registers as sysmem buffer collection. The collection can be released by |
| 130 | // destroying the returned SysmemBufferCollection object. Once a collection is |
| 131 | // registered the individual buffers in the collection can be referenced by |
| 132 | // using the |id| as |buffer_collection_id| in |gmb_handle| passed to |
| 133 | // CreateImageFromGpuMemoryHandle(). |
| 134 | virtual std::unique_ptr<SysmemBufferCollection> |
| 135 | RegisterSysmemBufferCollection(VkDevice device, |
| 136 | gfx::SysmemBufferCollectionId id, |
| 137 | zx::channel token) = 0; |
| 138 | #endif // defined(OS_FUCHSIA) |
| 139 | |
Saman Sami | 19ec270 | 2019-05-22 19:20:31 | [diff] [blame] | 140 | bool use_swiftshader() const { return use_swiftshader_; } |
Emircan Uysaler | d41c9bf | 2019-08-08 20:38:33 | [diff] [blame] | 141 | bool allow_protected_memory() const { return allow_protected_memory_; } |
| 142 | bool enforce_protected_memory() const { return enforce_protected_memory_; } |
Saman Sami | 19ec270 | 2019-05-22 19:20:31 | [diff] [blame] | 143 | |
Michael Spang | 7509e6d | 2018-05-16 17:08:28 | [diff] [blame] | 144 | private: |
Saman Sami | 19ec270 | 2019-05-22 19:20:31 | [diff] [blame] | 145 | const bool use_swiftshader_; |
Emircan Uysaler | d41c9bf | 2019-08-08 20:38:33 | [diff] [blame] | 146 | const bool allow_protected_memory_; |
| 147 | const bool enforce_protected_memory_; |
Michael Spang | 7509e6d | 2018-05-16 17:08:28 | [diff] [blame] | 148 | DISALLOW_COPY_AND_ASSIGN(VulkanImplementation); |
| 149 | }; |
| 150 | |
Peng Huang | 0a13bf5 | 2020-05-05 16:01:05 | [diff] [blame] | 151 | COMPONENT_EXPORT(VULKAN) |
Michael Spang | 7509e6d | 2018-05-16 17:08:28 | [diff] [blame] | 152 | std::unique_ptr<VulkanDeviceQueue> CreateVulkanDeviceQueue( |
| 153 | VulkanImplementation* vulkan_implementation, |
Peng Huang | ea21d6f | 2020-05-14 18:22:22 | [diff] [blame] | 154 | uint32_t option, |
| 155 | const GPUInfo* gpu_info = nullptr); |
dyen | 4ec04ecc | 2016-03-30 22:45:46 | [diff] [blame] | 156 | |
dyen | 2ce3e05 | 2016-03-09 21:03:49 | [diff] [blame] | 157 | } // namespace gpu |
| 158 | |
Michael Spang | 7509e6d | 2018-05-16 17:08:28 | [diff] [blame] | 159 | #endif // GPU_VULKAN_VULKAN_IMPLEMENTATION_H_ |