| 1 | // Copyright 2013 The Flutter 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 | #pragma once |
| 6 | |
| 7 | #include "flutter/fml/macros.h" |
| 8 | #include "flutter/fml/memory/ref_counted.h" |
| 9 | #include "flutter/fml/memory/ref_ptr.h" |
| 10 | #include "flutter/fml/native_library.h" |
| 11 | #include "flutter/vulkan/procs/vulkan_handle.h" |
| 12 | #include "flutter/vulkan/procs/vulkan_interface.h" |
| 13 | |
| 14 | namespace vulkan { |
| 15 | |
| 16 | class VulkanProcTable : public fml::RefCountedThreadSafe<VulkanProcTable> { |
| 17 | FML_FRIEND_REF_COUNTED_THREAD_SAFE(VulkanProcTable); |
| 18 | FML_FRIEND_MAKE_REF_COUNTED(VulkanProcTable); |
| 19 | |
| 20 | public: |
| 21 | template <class T> |
| 22 | class Proc { |
| 23 | public: |
| 24 | using Proto = T; |
| 25 | |
| 26 | explicit Proc(T proc = nullptr) : proc_(proc) {} |
| 27 | |
| 28 | ~Proc() { proc_ = nullptr; } |
| 29 | |
| 30 | Proc operator=(T proc) { |
| 31 | proc_ = proc; |
| 32 | return *this; |
| 33 | } |
| 34 | |
| 35 | Proc operator=(PFN_vkVoidFunction proc) { |
| 36 | proc_ = reinterpret_cast<Proto>(proc); |
| 37 | return *this; |
| 38 | } |
| 39 | |
| 40 | explicit operator bool() const { return proc_ != nullptr; } |
| 41 | |
| 42 | operator T() const { return proc_; } // NOLINT(google-explicit-constructor) |
| 43 | |
| 44 | private: |
| 45 | T proc_; |
| 46 | }; |
| 47 | |
| 48 | VulkanProcTable(); |
| 49 | explicit VulkanProcTable(const char* so_path); |
| 50 | explicit VulkanProcTable(PFN_vkGetInstanceProcAddr get_instance_proc_addr); |
| 51 | ~VulkanProcTable(); |
| 52 | |
| 53 | bool HasAcquiredMandatoryProcAddresses() const; |
| 54 | |
| 55 | bool IsValid() const; |
| 56 | |
| 57 | bool AreInstanceProcsSetup() const; |
| 58 | |
| 59 | bool AreDeviceProcsSetup() const; |
| 60 | |
| 61 | bool SetupInstanceProcAddresses(const VulkanHandle<VkInstance>& instance); |
| 62 | |
| 63 | bool SetupDeviceProcAddresses(const VulkanHandle<VkDevice>& device); |
| 64 | |
| 65 | PFN_vkGetInstanceProcAddr GetInstanceProcAddr = nullptr; |
| 66 | |
| 67 | #define DEFINE_PROC(name) Proc<PFN_vk##name> name; |
| 68 | |
| 69 | DEFINE_PROC(AcquireNextImageKHR); |
| 70 | DEFINE_PROC(AllocateCommandBuffers); |
| 71 | DEFINE_PROC(AllocateMemory); |
| 72 | DEFINE_PROC(BeginCommandBuffer); |
| 73 | DEFINE_PROC(BindImageMemory); |
| 74 | DEFINE_PROC(CmdPipelineBarrier); |
| 75 | DEFINE_PROC(CreateCommandPool); |
| 76 | DEFINE_PROC(CreateDebugReportCallbackEXT); |
| 77 | DEFINE_PROC(CreateDevice); |
| 78 | DEFINE_PROC(CreateFence); |
| 79 | DEFINE_PROC(CreateImage); |
| 80 | DEFINE_PROC(CreateInstance); |
| 81 | DEFINE_PROC(CreateSemaphore); |
| 82 | DEFINE_PROC(CreateSwapchainKHR); |
| 83 | DEFINE_PROC(DestroyCommandPool); |
| 84 | DEFINE_PROC(DestroyDebugReportCallbackEXT); |
| 85 | DEFINE_PROC(DestroyDevice); |
| 86 | DEFINE_PROC(DestroyFence); |
| 87 | DEFINE_PROC(DestroyImage); |
| 88 | DEFINE_PROC(DestroyInstance); |
| 89 | DEFINE_PROC(DestroySemaphore); |
| 90 | DEFINE_PROC(DestroySurfaceKHR); |
| 91 | DEFINE_PROC(DestroySwapchainKHR); |
| 92 | DEFINE_PROC(DeviceWaitIdle); |
| 93 | DEFINE_PROC(EndCommandBuffer); |
| 94 | DEFINE_PROC(EnumerateDeviceLayerProperties); |
| 95 | DEFINE_PROC(EnumerateInstanceExtensionProperties); |
| 96 | DEFINE_PROC(EnumerateInstanceLayerProperties); |
| 97 | DEFINE_PROC(EnumeratePhysicalDevices); |
| 98 | DEFINE_PROC(FreeCommandBuffers); |
| 99 | DEFINE_PROC(FreeMemory); |
| 100 | DEFINE_PROC(GetDeviceProcAddr); |
| 101 | DEFINE_PROC(GetDeviceQueue); |
| 102 | DEFINE_PROC(GetImageMemoryRequirements); |
| 103 | DEFINE_PROC(GetPhysicalDeviceFeatures); |
| 104 | DEFINE_PROC(GetPhysicalDeviceQueueFamilyProperties); |
| 105 | DEFINE_PROC(QueueSubmit); |
| 106 | DEFINE_PROC(QueueWaitIdle); |
| 107 | DEFINE_PROC(ResetCommandBuffer); |
| 108 | DEFINE_PROC(ResetFences); |
| 109 | DEFINE_PROC(WaitForFences); |
| 110 | DEFINE_PROC(GetPhysicalDeviceProperties); |
| 111 | DEFINE_PROC(GetPhysicalDeviceMemoryProperties); |
| 112 | DEFINE_PROC(MapMemory); |
| 113 | DEFINE_PROC(UnmapMemory); |
| 114 | DEFINE_PROC(FlushMappedMemoryRanges); |
| 115 | DEFINE_PROC(InvalidateMappedMemoryRanges); |
| 116 | DEFINE_PROC(BindBufferMemory); |
| 117 | DEFINE_PROC(GetBufferMemoryRequirements); |
| 118 | DEFINE_PROC(CreateBuffer); |
| 119 | DEFINE_PROC(DestroyBuffer); |
| 120 | DEFINE_PROC(CmdCopyBuffer); |
| 121 | |
| 122 | DEFINE_PROC(GetPhysicalDeviceMemoryProperties2); |
| 123 | DEFINE_PROC(GetPhysicalDeviceMemoryProperties2KHR); |
| 124 | |
| 125 | DEFINE_PROC(GetBufferMemoryRequirements2); |
| 126 | DEFINE_PROC(GetBufferMemoryRequirements2KHR); |
| 127 | DEFINE_PROC(GetImageMemoryRequirements2); |
| 128 | DEFINE_PROC(GetImageMemoryRequirements2KHR); |
| 129 | DEFINE_PROC(BindBufferMemory2); |
| 130 | DEFINE_PROC(BindBufferMemory2KHR); |
| 131 | DEFINE_PROC(BindImageMemory2); |
| 132 | DEFINE_PROC(BindImageMemory2KHR); |
| 133 | |
| 134 | #ifndef TEST_VULKAN_PROCS |
| 135 | #if FML_OS_ANDROID |
| 136 | DEFINE_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR); |
| 137 | DEFINE_PROC(GetPhysicalDeviceSurfaceFormatsKHR); |
| 138 | DEFINE_PROC(GetPhysicalDeviceSurfacePresentModesKHR); |
| 139 | DEFINE_PROC(GetPhysicalDeviceSurfaceSupportKHR); |
| 140 | DEFINE_PROC(GetSwapchainImagesKHR); |
| 141 | DEFINE_PROC(QueuePresentKHR); |
| 142 | DEFINE_PROC(CreateAndroidSurfaceKHR); |
| 143 | #endif // FML_OS_ANDROID |
| 144 | #if OS_FUCHSIA |
| 145 | DEFINE_PROC(ImportSemaphoreZirconHandleFUCHSIA); |
| 146 | DEFINE_PROC(GetSemaphoreZirconHandleFUCHSIA); |
| 147 | DEFINE_PROC(GetMemoryZirconHandleFUCHSIA); |
| 148 | DEFINE_PROC(CreateBufferCollectionFUCHSIA); |
| 149 | DEFINE_PROC(DestroyBufferCollectionFUCHSIA); |
| 150 | DEFINE_PROC(SetBufferCollectionImageConstraintsFUCHSIA); |
| 151 | DEFINE_PROC(GetBufferCollectionPropertiesFUCHSIA); |
| 152 | #endif // OS_FUCHSIA |
| 153 | #endif // TEST_VULKAN_PROCS |
| 154 | |
| 155 | #undef DEFINE_PROC |
| 156 | |
| 157 | PFN_vkGetInstanceProcAddr NativeGetInstanceProcAddr() const; |
| 158 | |
| 159 | PFN_vkVoidFunction AcquireProc( |
| 160 | const char* proc_name, |
| 161 | const VulkanHandle<VkInstance>& instance) const; |
| 162 | |
| 163 | PFN_vkVoidFunction AcquireProc(const char* proc_name, |
| 164 | const VulkanHandle<VkDevice>& device) const; |
| 165 | |
| 166 | private: |
| 167 | fml::RefPtr<fml::NativeLibrary> handle_; |
| 168 | bool acquired_mandatory_proc_addresses_; |
| 169 | VulkanHandle<VkInstance> instance_; |
| 170 | VulkanHandle<VkDevice> device_; |
| 171 | |
| 172 | bool OpenLibraryHandle(const char* path); |
| 173 | bool SetupGetInstanceProcAddress(); |
| 174 | bool SetupLoaderProcAddresses(); |
| 175 | bool CloseLibraryHandle(); |
| 176 | |
| 177 | FML_DISALLOW_COPY_AND_ASSIGN(VulkanProcTable); |
| 178 | }; |
| 179 | |
| 180 | } // namespace vulkan |
| 181 |
