summaryrefslogtreecommitdiff
path: root/chromium/gpu/vulkan/vulkan_implementation.h
blob: 72586d0fb6be0766340d3a951cc90d30be80a0c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef GPU_VULKAN_VULKAN_IMPLEMENTATION_H_
#define GPU_VULKAN_VULKAN_IMPLEMENTATION_H_

#include <vulkan/vulkan.h>

#include <memory>
#include <vector>

#include "base/component_export.h"
#include "base/macros.h"
#include "build/build_config.h"
#include "gpu/vulkan/semaphore_handle.h"
#include "ui/gfx/buffer_types.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/gpu_memory_buffer.h"
#include "ui/gfx/native_widget_types.h"

#if defined(OS_ANDROID)
#include "base/android/scoped_hardware_buffer_handle.h"
#include "ui/gfx/geometry/size.h"
#endif

namespace gfx {
class GpuFence;
struct GpuMemoryBufferHandle;
}  // namespace gfx

namespace gpu {
class VulkanDeviceQueue;
class VulkanSurface;
class VulkanImage;
class VulkanInstance;
struct GPUInfo;
struct VulkanYCbCrInfo;

#if defined(OS_FUCHSIA)
class SysmemBufferCollection {
 public:
  virtual ~SysmemBufferCollection() {}
};
#endif  // defined(OS_FUCHSIA)

// Base class which provides functions for creating vulkan objects for different
// platforms that use platform-specific extensions (e.g. for creation of
// VkSurfaceKHR objects). It also provides helper/utility functions.
class COMPONENT_EXPORT(VULKAN) VulkanImplementation {
 public:
  explicit VulkanImplementation(bool use_swiftshader = false,
                                bool allow_protected_memory = false);

  virtual ~VulkanImplementation();

  // Initialize VulkanInstance. If using_surface, VK_KHR_surface instance
  // extension will be required when initialize VkInstance. This extension is
  // required for presenting with VkSwapchain API.
  virtual bool InitializeVulkanInstance(bool using_surface = true) = 0;

  virtual VulkanInstance* GetVulkanInstance() = 0;

  virtual std::unique_ptr<VulkanSurface> CreateViewSurface(
      gfx::AcceleratedWidget window) = 0;

  virtual bool GetPhysicalDevicePresentationSupport(
      VkPhysicalDevice device,
      const std::vector<VkQueueFamilyProperties>& queue_family_properties,
      uint32_t queue_family_index) = 0;

  virtual std::vector<const char*> GetRequiredDeviceExtensions() = 0;
  virtual std::vector<const char*> GetOptionalDeviceExtensions() = 0;

  // Creates a VkFence that is exportable to a gfx::GpuFence.
  virtual VkFence CreateVkFenceForGpuFence(VkDevice vk_device) = 0;

  // Exports a VkFence to a gfx::GpuFence.
  //
  // The fence should have been created via CreateVkFenceForGpuFence().
  virtual std::unique_ptr<gfx::GpuFence> ExportVkFenceToGpuFence(
      VkDevice vk_device,
      VkFence vk_fence) = 0;

  // Creates a semaphore that can be exported using GetSemaphoreHandle().
  virtual VkSemaphore CreateExternalSemaphore(VkDevice vk_device) = 0;

  // Import a VkSemaphore from a platform-specific handle.
  // Handle types that don't allow permanent import are imported with
  // temporary permanence (VK_SEMAPHORE_IMPORT_TEMPORARY_BIT).
  virtual VkSemaphore ImportSemaphoreHandle(VkDevice vk_device,
                                            SemaphoreHandle handle) = 0;

  // Export a platform-specific handle for a Vulkan semaphore. Returns a null
  // handle in case of a failure.
  virtual SemaphoreHandle GetSemaphoreHandle(VkDevice vk_device,
                                             VkSemaphore vk_semaphore) = 0;

  // Returns VkExternalMemoryHandleTypeFlagBits that should be set when creating
  // external images and memory.
  virtual VkExternalMemoryHandleTypeFlagBits GetExternalImageHandleType() = 0;

  // Returns true if the GpuMemoryBuffer of the specified type can be imported
  // into VkImage using CreateImageFromGpuMemoryHandle().
  virtual bool CanImportGpuMemoryBuffer(
      gfx::GpuMemoryBufferType memory_buffer_type) = 0;

  // Creates a VkImage from a GpuMemoryBuffer. If successful it initializes
  // |vk_image|, |vk_image_info|, |vk_device_memory| and |mem_allocation_size|.
  // Implementation must verify that the specified |size| fits in the size
  // specified when |gmb_handle| was allocated.
  virtual std::unique_ptr<VulkanImage> CreateImageFromGpuMemoryHandle(
      VulkanDeviceQueue* device_queue,
      gfx::GpuMemoryBufferHandle gmb_handle,
      gfx::Size size,
      VkFormat vk_formae) = 0;

#if defined(OS_ANDROID)
  // Get the sampler ycbcr conversion information from the AHB.
  virtual bool GetSamplerYcbcrConversionInfo(
      const VkDevice& vk_device,
      base::android::ScopedHardwareBufferHandle ahb_handle,
      VulkanYCbCrInfo* ycbcr_info) = 0;
#endif

#if defined(OS_FUCHSIA)
  // Registers as sysmem buffer collection. The collection can be released by
  // destroying the returned SysmemBufferCollection object. Once a collection is
  // registered the individual buffers in the collection can be referenced by
  // using the |id| as |buffer_collection_id| in |gmb_handle| passed to
  // CreateImageFromGpuMemoryHandle().
  virtual std::unique_ptr<SysmemBufferCollection>
  RegisterSysmemBufferCollection(VkDevice device,
                                 gfx::SysmemBufferCollectionId id,
                                 zx::channel token,
                                 gfx::BufferFormat format,
                                 gfx::BufferUsage usage,
                                 gfx::Size size,
                                 size_t min_buffer_count,
                                 bool register_with_image_pipe) = 0;
#endif  // defined(OS_FUCHSIA)

  bool use_swiftshader() const { return use_swiftshader_; }
  bool allow_protected_memory() const { return allow_protected_memory_; }

 private:
  const bool use_swiftshader_;
  const bool allow_protected_memory_;
  DISALLOW_COPY_AND_ASSIGN(VulkanImplementation);
};

COMPONENT_EXPORT(VULKAN)
std::unique_ptr<VulkanDeviceQueue> CreateVulkanDeviceQueue(
    VulkanImplementation* vulkan_implementation,
    uint32_t option,
    const GPUInfo* gpu_info = nullptr,
    uint32_t heap_memory_limit = 0);

}  // namespace gpu

#endif  // GPU_VULKAN_VULKAN_IMPLEMENTATION_H_