Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef VULKANMANAGER_H |
| 18 | #define VULKANMANAGER_H |
| 19 | |
Greg Daniel | 22cc59d | 2018-07-24 13:46:10 -0400 | [diff] [blame] | 20 | #if !defined(VK_USE_PLATFORM_ANDROID_KHR) |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 21 | #define VK_USE_PLATFORM_ANDROID_KHR |
Greg Daniel | 22cc59d | 2018-07-24 13:46:10 -0400 | [diff] [blame] | 22 | #endif |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 23 | #include <GrContextOptions.h> |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 24 | #include <SkSurface.h> |
John Reck | 5d3fac1 | 2023-11-08 23:08:10 -0500 | [diff] [blame] | 25 | #include <android-base/unique_fd.h> |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 26 | #include <utils/StrongPointer.h> |
Kaylee Lubick | ab1f35f | 2024-06-20 17:19:31 +0000 | [diff] [blame] | 27 | #include <vk/VulkanExtensions.h> |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 28 | #include <vulkan/vulkan.h> |
| 29 | |
Hugues Evrard | b9510a0 | 2021-03-01 14:35:22 +0000 | [diff] [blame] | 30 | // VK_ANDROID_frame_boundary is a bespoke extension defined by AGI |
| 31 | // (https://github.com/google/agi) to enable profiling of apps rendering via |
| 32 | // HWUI. This extension is not defined in Khronos, hence the need to declare it |
| 33 | // manually here. There's a superseding extension (VK_EXT_frame_boundary) being |
| 34 | // discussed in Khronos, but in the meantime we use the bespoke |
| 35 | // VK_ANDROID_frame_boundary. This is a device extension that is implemented by |
| 36 | // AGI's Vulkan capture layer, such that it is only supported by devices when |
| 37 | // AGI is doing a capture of the app. |
| 38 | // |
| 39 | // TODO(b/182165045): use the Khronos blessed VK_EXT_frame_boudary once it has |
| 40 | // landed in the spec. |
| 41 | typedef void(VKAPI_PTR* PFN_vkFrameBoundaryANDROID)(VkDevice device, VkSemaphore semaphore, |
| 42 | VkImage image); |
| 43 | #define VK_ANDROID_FRAME_BOUNDARY_EXTENSION_NAME "VK_ANDROID_frame_boundary" |
| 44 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 45 | #include "Frame.h" |
Stan Iliev | 79351f3 | 2018-09-19 14:23:49 -0400 | [diff] [blame] | 46 | #include "IRenderPipeline.h" |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 47 | #include "VulkanSurface.h" |
Derek Sollenberger | 5d0ca63 | 2019-07-19 16:17:12 -0400 | [diff] [blame] | 48 | #include "private/hwui/DrawVkInfo.h" |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 49 | |
Kevin Lubick | 07d6aae | 2022-04-01 14:03:11 -0400 | [diff] [blame] | 50 | #include <SkColorSpace.h> |
| 51 | #include <SkRefCnt.h> |
| 52 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 53 | namespace android { |
| 54 | namespace uirenderer { |
| 55 | namespace renderthread { |
| 56 | |
| 57 | class RenderThread; |
| 58 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 59 | // This class contains the shared global Vulkan objects, such as VkInstance, VkDevice and VkQueue, |
| 60 | // which are re-used by CanvasContext. This class is created once and should be used by all vulkan |
| 61 | // windowing contexts. The VulkanManager must be initialized before use. |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 62 | class VulkanManager final : public RefBase { |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 63 | public: |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 64 | static sp<VulkanManager> getInstance(); |
John Reck | 9fc3d27 | 2023-05-01 16:33:22 -0400 | [diff] [blame] | 65 | static sp<VulkanManager> peekInstance(); |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 66 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 67 | // Sets up the vulkan context that is shared amonst all clients of the VulkanManager. This must |
| 68 | // be call once before use of the VulkanManager. Multiple calls after the first will simiply |
| 69 | // return. |
| 70 | void initialize(); |
| 71 | |
| 72 | // Quick check to see if the VulkanManager has been initialized. |
Alec Mouri | 671a9f6 | 2023-08-25 17:18:02 +0000 | [diff] [blame] | 73 | bool hasVkContext() { return mInitialized; } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 74 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 75 | // Create and destroy functions for wrapping an ANativeWindow in a VulkanSurface |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 76 | VulkanSurface* createSurface(ANativeWindow* window, |
| 77 | ColorMode colorMode, |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 78 | sk_sp<SkColorSpace> surfaceColorSpace, |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 79 | SkColorType surfaceColorType, |
| 80 | GrDirectContext* grContext, |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 81 | uint32_t extraBuffers); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 82 | void destroySurface(VulkanSurface* surface); |
| 83 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 84 | Frame dequeueNextBuffer(VulkanSurface* surface); |
John Reck | 5d3fac1 | 2023-11-08 23:08:10 -0500 | [diff] [blame] | 85 | |
| 86 | struct VkDrawResult { |
| 87 | // The estimated start time for intiating GPU work, -1 if unknown. |
| 88 | nsecs_t submissionTime; |
| 89 | android::base::unique_fd presentFence; |
| 90 | }; |
| 91 | |
Alec Mouri | 3afb397 | 2022-05-27 22:03:11 +0000 | [diff] [blame] | 92 | // Finishes the frame and submits work to the GPU |
John Reck | 5d3fac1 | 2023-11-08 23:08:10 -0500 | [diff] [blame] | 93 | VkDrawResult finishFrame(SkSurface* surface); |
| 94 | void swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect, |
| 95 | android::base::unique_fd&& presentFence); |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 96 | |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 97 | // Inserts a wait on fence command into the Vulkan command buffer. |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 98 | status_t fenceWait(int fence, GrDirectContext* grContext); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 99 | |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 100 | // Creates a fence that is signaled when all the pending Vulkan commands are finished on the |
| 101 | // GPU. |
Adlai Holler | f8c434e | 2020-07-27 11:42:45 -0400 | [diff] [blame] | 102 | status_t createReleaseFence(int* nativeFence, GrDirectContext* grContext); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 103 | |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 104 | // Returned pointers are owned by VulkanManager. |
Roman Kiryanov | 74ace839e | 2019-03-07 18:22:19 -0800 | [diff] [blame] | 105 | // An instance of VkFunctorInitParams returned from getVkFunctorInitParams refers to |
| 106 | // the internal state of VulkanManager: VulkanManager must be alive to use the returned value. |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 107 | VkFunctorInitParams getVkFunctorInitParams() const; |
| 108 | |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 109 | |
| 110 | enum class ContextType { |
| 111 | kRenderThread, |
| 112 | kUploadThread |
| 113 | }; |
| 114 | |
| 115 | // returns a Skia graphic context used to draw content on the specified thread |
John Reck | 9fc3d27 | 2023-05-01 16:33:22 -0400 | [diff] [blame] | 116 | sk_sp<GrDirectContext> createContext(GrContextOptions& options, |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 117 | ContextType contextType = ContextType::kRenderThread); |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 118 | |
Stan Iliev | bf99c44 | 2019-03-29 11:09:11 -0400 | [diff] [blame] | 119 | uint32_t getDriverVersion() const { return mDriverVersion; } |
| 120 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 121 | private: |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 122 | friend class VulkanSurface; |
Derek Sollenberger | 802fefa | 2020-08-13 16:53:30 -0400 | [diff] [blame] | 123 | |
| 124 | explicit VulkanManager() {} |
| 125 | ~VulkanManager(); |
| 126 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 127 | // Sets up the VkInstance and VkDevice objects. Also fills out the passed in |
| 128 | // VkPhysicalDeviceFeatures struct. |
Kaylee Lubick | ab1f35f | 2024-06-20 17:19:31 +0000 | [diff] [blame] | 129 | void setupDevice(skgpu::VulkanExtensions&, VkPhysicalDeviceFeatures2&); |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 130 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 131 | // simple wrapper class that exists only to initialize a pointer to NULL |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 132 | template <typename FNPTR_TYPE> |
| 133 | class VkPtr { |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 134 | public: |
| 135 | VkPtr() : fPtr(NULL) {} |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 136 | VkPtr operator=(FNPTR_TYPE ptr) { |
| 137 | fPtr = ptr; |
| 138 | return *this; |
| 139 | } |
Chih-Hung Hsieh | d736d4b | 2018-12-20 13:55:20 -0800 | [diff] [blame] | 140 | // NOLINTNEXTLINE(google-explicit-constructor) |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 141 | operator FNPTR_TYPE() const { return fPtr; } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 142 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 143 | private: |
| 144 | FNPTR_TYPE fPtr; |
| 145 | }; |
| 146 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 147 | // Instance Functions |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 148 | VkPtr<PFN_vkEnumerateInstanceVersion> mEnumerateInstanceVersion; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 149 | VkPtr<PFN_vkEnumerateInstanceExtensionProperties> mEnumerateInstanceExtensionProperties; |
| 150 | VkPtr<PFN_vkCreateInstance> mCreateInstance; |
| 151 | |
| 152 | VkPtr<PFN_vkDestroyInstance> mDestroyInstance; |
| 153 | VkPtr<PFN_vkEnumeratePhysicalDevices> mEnumeratePhysicalDevices; |
Greg Daniel | 9625962 | 2018-10-01 14:42:56 -0400 | [diff] [blame] | 154 | VkPtr<PFN_vkGetPhysicalDeviceProperties> mGetPhysicalDeviceProperties; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 155 | VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties; |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 156 | VkPtr<PFN_vkGetPhysicalDeviceFeatures2> mGetPhysicalDeviceFeatures2; |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 157 | VkPtr<PFN_vkGetPhysicalDeviceImageFormatProperties2> mGetPhysicalDeviceImageFormatProperties2; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 158 | VkPtr<PFN_vkCreateDevice> mCreateDevice; |
| 159 | VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties; |
| 160 | |
| 161 | // Device Functions |
| 162 | VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue; |
| 163 | VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle; |
| 164 | VkPtr<PFN_vkDestroyDevice> mDestroyDevice; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 165 | VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool; |
| 166 | VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool; |
| 167 | VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers; |
| 168 | VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers; |
| 169 | VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer; |
| 170 | VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer; |
| 171 | VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer; |
| 172 | VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier; |
| 173 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 174 | VkPtr<PFN_vkQueueSubmit> mQueueSubmit; |
| 175 | VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 176 | |
| 177 | VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore; |
| 178 | VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore; |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 179 | VkPtr<PFN_vkImportSemaphoreFdKHR> mImportSemaphoreFdKHR; |
| 180 | VkPtr<PFN_vkGetSemaphoreFdKHR> mGetSemaphoreFdKHR; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 181 | VkPtr<PFN_vkCreateFence> mCreateFence; |
| 182 | VkPtr<PFN_vkDestroyFence> mDestroyFence; |
| 183 | VkPtr<PFN_vkWaitForFences> mWaitForFences; |
| 184 | VkPtr<PFN_vkResetFences> mResetFences; |
Hugues Evrard | b9510a0 | 2021-03-01 14:35:22 +0000 | [diff] [blame] | 185 | VkPtr<PFN_vkFrameBoundaryANDROID> mFrameBoundaryANDROID; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 186 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 187 | VkInstance mInstance = VK_NULL_HANDLE; |
| 188 | VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE; |
| 189 | VkDevice mDevice = VK_NULL_HANDLE; |
| 190 | |
| 191 | uint32_t mGraphicsQueueIndex; |
| 192 | VkQueue mGraphicsQueue = VK_NULL_HANDLE; |
Alec Mouri | 219997a | 2023-05-23 17:25:19 +0000 | [diff] [blame] | 193 | VkQueue mAHBUploadQueue = VK_NULL_HANDLE; |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 194 | |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 195 | // Variables saved to populate VkFunctorInitParams. |
Greg Daniel | eaf310e | 2019-01-28 16:10:32 -0500 | [diff] [blame] | 196 | static const uint32_t mAPIVersion = VK_MAKE_VERSION(1, 1, 0); |
Roman Kiryanov | 74ace839e | 2019-03-07 18:22:19 -0800 | [diff] [blame] | 197 | std::vector<VkExtensionProperties> mInstanceExtensionsOwner; |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 198 | std::vector<const char*> mInstanceExtensions; |
Roman Kiryanov | 74ace839e | 2019-03-07 18:22:19 -0800 | [diff] [blame] | 199 | std::vector<VkExtensionProperties> mDeviceExtensionsOwner; |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 200 | std::vector<const char*> mDeviceExtensions; |
| 201 | VkPhysicalDeviceFeatures2 mPhysicalDeviceFeatures2{}; |
| 202 | |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 203 | enum class SwapBehavior { |
| 204 | Discard, |
| 205 | BufferAge, |
| 206 | }; |
| 207 | SwapBehavior mSwapBehavior = SwapBehavior::Discard; |
Kaylee Lubick | ab1f35f | 2024-06-20 17:19:31 +0000 | [diff] [blame] | 208 | skgpu::VulkanExtensions mExtensions; |
Stan Iliev | bf99c44 | 2019-03-29 11:09:11 -0400 | [diff] [blame] | 209 | uint32_t mDriverVersion = 0; |
Greg Daniel | be2803a | 2021-02-19 18:32:16 -0500 | [diff] [blame] | 210 | |
Alec Mouri | 671a9f6 | 2023-08-25 17:18:02 +0000 | [diff] [blame] | 211 | std::once_flag mInitFlag; |
| 212 | std::atomic_bool mInitialized = false; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 213 | }; |
| 214 | |
| 215 | } /* namespace renderthread */ |
| 216 | } /* namespace uirenderer */ |
| 217 | } /* namespace android */ |
| 218 | |
| 219 | #endif /* VULKANMANAGER_H */ |