blob: 3ee8cd5f4ad454648e4961cea9ed5ab890629cef [file] [log] [blame]
dyen2ce3e052016-03-09 21:03:491// 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 Huangfdcb7462019-03-18 17:25:519
Michael Spang7509e6d2018-05-16 17:08:2810#include <memory>
Peng Huangfdcb7462019-03-18 17:25:5111#include <vector>
dyen2ce3e052016-03-09 21:03:4912
Peng Huang0a13bf52020-05-05 16:01:0513#include "base/component_export.h"
Michael Spang7509e6d2018-05-16 17:08:2814#include "base/macros.h"
Sergey Ulanov6ff66f22019-08-14 00:38:3915#include "base/optional.h"
Vikas Soni7af47362018-11-27 23:20:3016#include "build/build_config.h"
Sergey Ulanovecc1be442019-04-01 23:24:2417#include "gpu/vulkan/semaphore_handle.h"
Sergey Ulanov521db4a22019-05-01 23:59:2118#include "ui/gfx/buffer_types.h"
19#include "ui/gfx/geometry/size.h"
20#include "ui/gfx/gpu_memory_buffer.h"
Michael Spang7509e6d2018-05-16 17:08:2821#include "ui/gfx/native_widget_types.h"
22
Vikas Sonia54d5762018-12-08 00:43:4523#if defined(OS_ANDROID)
24#include "base/android/scoped_hardware_buffer_handle.h"
Vikas Sonia54d5762018-12-08 00:43:4525#include "ui/gfx/geometry/size.h"
26#endif
27
Michael Spang7b9bcca2018-07-03 17:10:5028namespace gfx {
29class GpuFence;
Sergey Ulanov521db4a22019-05-01 23:59:2130struct GpuMemoryBufferHandle;
31} // namespace gfx
Michael Spang7b9bcca2018-07-03 17:10:5032
dyen2ce3e052016-03-09 21:03:4933namespace gpu {
Michael Spang7509e6d2018-05-16 17:08:2834class VulkanDeviceQueue;
35class VulkanSurface;
Peng Huangaec06f12020-03-12 19:40:2536class VulkanImage;
Sergey Ulanovaf257db2019-01-28 20:13:5237class VulkanInstance;
Peng Huangea21d6f2020-05-14 18:22:2238struct GPUInfo;
Vikas Soniff90d242019-05-09 02:53:5139struct VulkanYCbCrInfo;
Vikas Soni7018c62e2019-05-02 23:13:1140
Sergey Ulanov7b343ff2019-08-15 07:52:3741#if defined(OS_FUCHSIA)
42class SysmemBufferCollection {
43 public:
44 virtual ~SysmemBufferCollection() {}
45};
46#endif // defined(OS_FUCHSIA)
47
Vikas Sonicd499af2019-01-29 19:00:1148// 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 Huang0a13bf52020-05-05 16:01:0551class COMPONENT_EXPORT(VULKAN) VulkanImplementation {
Michael Spang7509e6d2018-05-16 17:08:2852 public:
Emircan Uysalerd41c9bf2019-08-08 20:38:3353 VulkanImplementation(bool use_swiftshader = false,
54 bool allow_protected_memory = false,
55 bool enforce_protected_memory = false);
Michael Spang7509e6d2018-05-16 17:08:2856
57 virtual ~VulkanImplementation();
58
Peng Huang556da902019-03-22 17:49:4059 // 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 Spang7509e6d2018-05-16 17:08:2863
Sergey Ulanovaf257db2019-01-28 20:13:5264 virtual VulkanInstance* GetVulkanInstance() = 0;
Michael Spang7509e6d2018-05-16 17:08:2865
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 Spang4e46bc22018-06-21 23:07:5174 virtual std::vector<const char*> GetRequiredDeviceExtensions() = 0;
Peng Huang576415de2020-03-02 20:04:3275 virtual std::vector<const char*> GetOptionalDeviceExtensions() = 0;
Michael Spang4e46bc22018-06-21 23:07:5176
Michael Spang7b9bcca2018-07-03 17:10:5077 // 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 Ulanov520fe0e2019-04-02 06:02:5587 // Creates a semaphore that can be exported using GetSemaphoreHandle().
88 virtual VkSemaphore CreateExternalSemaphore(VkDevice vk_device) = 0;
89
Sergey Ulanovecc1be442019-04-01 23:24:2490 // Import a VkSemaphore from a platform-specific handle.
Sergey Ulanov520fe0e2019-04-02 06:02:5591 // Handle types that don't allow permanent import are imported with
92 // temporary permanence (VK_SEMAPHORE_IMPORT_TEMPORARY_BIT).
Sergey Ulanovecc1be442019-04-01 23:24:2493 virtual VkSemaphore ImportSemaphoreHandle(VkDevice vk_device,
94 SemaphoreHandle handle) = 0;
Vikas Soni7af47362018-11-27 23:20:3095
Sergey Ulanovecc1be442019-04-01 23:24:2496 // 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 Sonia54d5762018-12-08 00:43:45100
Sergey Ulanova1289882019-04-04 19:36:33101 // Returns VkExternalMemoryHandleTypeFlagBits that should be set when creating
102 // external images and memory.
103 virtual VkExternalMemoryHandleTypeFlagBits GetExternalImageHandleType() = 0;
104
Sergey Ulanov521db4a22019-05-01 23:59:21105 // 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 Huangaec06f12020-03-12 19:40:25114 virtual std::unique_ptr<VulkanImage> CreateImageFromGpuMemoryHandle(
115 VulkanDeviceQueue* device_queue,
Sergey Ulanov521db4a22019-05-01 23:59:21116 gfx::GpuMemoryBufferHandle gmb_handle,
117 gfx::Size size,
Peng Huangaec06f12020-03-12 19:40:25118 VkFormat vk_formae) = 0;
Sergey Ulanov521db4a22019-05-01 23:59:21119
Peng Huangfdcb7462019-03-18 17:25:51120#if defined(OS_ANDROID)
Vikas Soni4a82f692019-05-15 02:53:41121 // 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 Soni7af47362018-11-27 23:20:30126#endif
127
Sergey Ulanov7b343ff2019-08-15 07:52:37128#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 Sami19ec2702019-05-22 19:20:31140 bool use_swiftshader() const { return use_swiftshader_; }
Emircan Uysalerd41c9bf2019-08-08 20:38:33141 bool allow_protected_memory() const { return allow_protected_memory_; }
142 bool enforce_protected_memory() const { return enforce_protected_memory_; }
Saman Sami19ec2702019-05-22 19:20:31143
Michael Spang7509e6d2018-05-16 17:08:28144 private:
Saman Sami19ec2702019-05-22 19:20:31145 const bool use_swiftshader_;
Emircan Uysalerd41c9bf2019-08-08 20:38:33146 const bool allow_protected_memory_;
147 const bool enforce_protected_memory_;
Michael Spang7509e6d2018-05-16 17:08:28148 DISALLOW_COPY_AND_ASSIGN(VulkanImplementation);
149};
150
Peng Huang0a13bf52020-05-05 16:01:05151COMPONENT_EXPORT(VULKAN)
Michael Spang7509e6d2018-05-16 17:08:28152std::unique_ptr<VulkanDeviceQueue> CreateVulkanDeviceQueue(
153 VulkanImplementation* vulkan_implementation,
Peng Huangea21d6f2020-05-14 18:22:22154 uint32_t option,
155 const GPUInfo* gpu_info = nullptr);
dyen4ec04ecc2016-03-30 22:45:46156
dyen2ce3e052016-03-09 21:03:49157} // namespace gpu
158
Michael Spang7509e6d2018-05-16 17:08:28159#endif // GPU_VULKAN_VULKAN_IMPLEMENTATION_H_