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) |
| 21 | # define VK_USE_PLATFORM_ANDROID_KHR |
| 22 | #endif |
| 23 | #include <vulkan/vulkan.h> |
| 24 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 25 | #include <SkSurface.h> |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 26 | #include <ui/Fence.h> |
| 27 | #include <utils/StrongPointer.h> |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 28 | #include <vk/GrVkBackendContext.h> |
Stan Iliev | 79351f3 | 2018-09-19 14:23:49 -0400 | [diff] [blame] | 29 | #include "IRenderPipeline.h" |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 30 | |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 31 | class GrVkExtensions; |
| 32 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 33 | namespace android { |
| 34 | namespace uirenderer { |
| 35 | namespace renderthread { |
| 36 | |
| 37 | class RenderThread; |
| 38 | |
| 39 | class VulkanSurface { |
| 40 | public: |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame^] | 41 | VulkanSurface(ColorMode colorMode, ANativeWindow* window, sk_sp<SkColorSpace> colorSpace, |
| 42 | SkColorSpace::Gamut colorGamut, SkColorType colorType) |
| 43 | : mColorMode(colorMode), mNativeWindow(window), mColorSpace(colorSpace), |
| 44 | mColorGamut(colorGamut), mColorType(colorType) {} |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 45 | |
| 46 | sk_sp<SkSurface> getBackBufferSurface() { return mBackbuffer; } |
| 47 | |
| 48 | private: |
| 49 | friend class VulkanManager; |
| 50 | struct BackbufferInfo { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 51 | uint32_t mImageIndex; // image this is associated with |
| 52 | VkSemaphore mAcquireSemaphore; // we signal on this for acquisition of image |
| 53 | VkSemaphore mRenderSemaphore; // we wait on this for rendering to be done |
| 54 | VkCommandBuffer |
| 55 | mTransitionCmdBuffers[2]; // to transition layout between present and render |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 56 | // We use these fences to make sure the above Command buffers have finished their work |
| 57 | // before attempting to reuse them or destroy them. |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 58 | VkFence mUsageFences[2]; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 59 | }; |
| 60 | |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 61 | struct ImageInfo { |
| 62 | VkImageLayout mImageLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 63 | sk_sp<SkSurface> mSurface; |
| 64 | uint16_t mLastUsed = 0; |
| 65 | bool mInvalid = true; |
| 66 | }; |
| 67 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 68 | sk_sp<SkSurface> mBackbuffer; |
| 69 | |
| 70 | VkSurfaceKHR mVkSurface = VK_NULL_HANDLE; |
| 71 | VkSwapchainKHR mSwapchain = VK_NULL_HANDLE; |
| 72 | |
Greg Daniel | 74ea201 | 2017-11-10 11:32:58 -0500 | [diff] [blame] | 73 | BackbufferInfo* mBackbuffers = nullptr; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 74 | uint32_t mCurrentBackbufferIndex; |
| 75 | |
| 76 | uint32_t mImageCount; |
Greg Daniel | 74ea201 | 2017-11-10 11:32:58 -0500 | [diff] [blame] | 77 | VkImage* mImages = nullptr; |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 78 | ImageInfo* mImageInfos; |
| 79 | uint16_t mCurrentTime = 0; |
Stan Iliev | 79351f3 | 2018-09-19 14:23:49 -0400 | [diff] [blame] | 80 | ColorMode mColorMode; |
Stan Iliev | 305e13a | 2018-11-13 11:14:48 -0500 | [diff] [blame] | 81 | ANativeWindow* mNativeWindow; |
| 82 | int mWindowWidth = 0; |
| 83 | int mWindowHeight = 0; |
Stan Iliev | 987a80c0 | 2018-12-04 10:07:21 -0500 | [diff] [blame] | 84 | sk_sp<SkColorSpace> mColorSpace; |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame^] | 85 | SkColorSpace::Gamut mColorGamut; |
| 86 | SkColorType mColorType; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | // This class contains the shared global Vulkan objects, such as VkInstance, VkDevice and VkQueue, |
| 90 | // which are re-used by CanvasContext. This class is created once and should be used by all vulkan |
| 91 | // windowing contexts. The VulkanManager must be initialized before use. |
| 92 | class VulkanManager { |
| 93 | public: |
| 94 | // Sets up the vulkan context that is shared amonst all clients of the VulkanManager. This must |
| 95 | // be call once before use of the VulkanManager. Multiple calls after the first will simiply |
| 96 | // return. |
| 97 | void initialize(); |
| 98 | |
| 99 | // Quick check to see if the VulkanManager has been initialized. |
Greg Daniel | 2f9d867 | 2018-06-22 10:44:26 -0400 | [diff] [blame] | 100 | bool hasVkContext() { return mDevice != VK_NULL_HANDLE; } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 101 | |
| 102 | // Given a window this creates a new VkSurfaceKHR and VkSwapchain and stores them inside a new |
| 103 | // VulkanSurface object which is returned. |
Stan Iliev | 987a80c0 | 2018-12-04 10:07:21 -0500 | [diff] [blame] | 104 | VulkanSurface* createSurface(ANativeWindow* window, ColorMode colorMode, |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame^] | 105 | sk_sp<SkColorSpace> surfaceColorSpace, |
| 106 | SkColorSpace::Gamut surfaceColorGamut, |
| 107 | SkColorType surfaceColorType); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 108 | |
| 109 | // Destroy the VulkanSurface and all associated vulkan objects. |
| 110 | void destroySurface(VulkanSurface* surface); |
| 111 | |
| 112 | // Cleans up all the global state in the VulkanManger. |
| 113 | void destroy(); |
| 114 | |
| 115 | // No work is needed to make a VulkanSurface current, and all functions require that a |
| 116 | // VulkanSurface is passed into them so we just return true here. |
| 117 | bool isCurrent(VulkanSurface* surface) { return true; } |
| 118 | |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 119 | int getAge(VulkanSurface* surface); |
| 120 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 121 | // Returns an SkSurface which wraps the next image returned from vkAcquireNextImageKHR. It also |
| 122 | // will transition the VkImage from a present layout to color attachment so that it can be used |
| 123 | // by the client for drawing. |
Stan Iliev | 305e13a | 2018-11-13 11:14:48 -0500 | [diff] [blame] | 124 | SkSurface* getBackbufferSurface(VulkanSurface** surface); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 125 | |
| 126 | // Presents the current VkImage. |
| 127 | void swapBuffers(VulkanSurface* surface); |
| 128 | |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 129 | // Inserts a wait on fence command into the Vulkan command buffer. |
| 130 | status_t fenceWait(sp<Fence>& fence); |
| 131 | |
| 132 | // Creates a fence that is signaled, when all the pending Vulkan commands are flushed. |
| 133 | status_t createReleaseFence(sp<Fence>& nativeFence); |
| 134 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 135 | private: |
| 136 | friend class RenderThread; |
| 137 | |
| 138 | explicit VulkanManager(RenderThread& thread); |
| 139 | ~VulkanManager() { destroy(); } |
| 140 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 141 | // Sets up the VkInstance and VkDevice objects. Also fills out the passed in |
| 142 | // VkPhysicalDeviceFeatures struct. |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 143 | bool setupDevice(GrVkExtensions&, VkPhysicalDeviceFeatures2&); |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 144 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 145 | void destroyBuffers(VulkanSurface* surface); |
| 146 | |
| 147 | bool createSwapchain(VulkanSurface* surface); |
| 148 | void createBuffers(VulkanSurface* surface, VkFormat format, VkExtent2D extent); |
| 149 | |
| 150 | VulkanSurface::BackbufferInfo* getAvailableBackbuffer(VulkanSurface* surface); |
| 151 | |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 152 | bool setupDummyCommandBuffer(); |
| 153 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 154 | // simple wrapper class that exists only to initialize a pointer to NULL |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 155 | template <typename FNPTR_TYPE> |
| 156 | class VkPtr { |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 157 | public: |
| 158 | VkPtr() : fPtr(NULL) {} |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 159 | VkPtr operator=(FNPTR_TYPE ptr) { |
| 160 | fPtr = ptr; |
| 161 | return *this; |
| 162 | } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 163 | operator FNPTR_TYPE() const { return fPtr; } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 164 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 165 | private: |
| 166 | FNPTR_TYPE fPtr; |
| 167 | }; |
| 168 | |
| 169 | // WSI interface functions |
| 170 | VkPtr<PFN_vkCreateAndroidSurfaceKHR> mCreateAndroidSurfaceKHR; |
| 171 | VkPtr<PFN_vkDestroySurfaceKHR> mDestroySurfaceKHR; |
| 172 | VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> mGetPhysicalDeviceSurfaceSupportKHR; |
| 173 | VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> mGetPhysicalDeviceSurfaceCapabilitiesKHR; |
| 174 | VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> mGetPhysicalDeviceSurfaceFormatsKHR; |
| 175 | VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> mGetPhysicalDeviceSurfacePresentModesKHR; |
| 176 | |
| 177 | VkPtr<PFN_vkCreateSwapchainKHR> mCreateSwapchainKHR; |
| 178 | VkPtr<PFN_vkDestroySwapchainKHR> mDestroySwapchainKHR; |
| 179 | VkPtr<PFN_vkGetSwapchainImagesKHR> mGetSwapchainImagesKHR; |
| 180 | VkPtr<PFN_vkAcquireNextImageKHR> mAcquireNextImageKHR; |
| 181 | VkPtr<PFN_vkQueuePresentKHR> mQueuePresentKHR; |
| 182 | VkPtr<PFN_vkCreateSharedSwapchainsKHR> mCreateSharedSwapchainsKHR; |
| 183 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 184 | // Instance Functions |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 185 | VkPtr<PFN_vkEnumerateInstanceVersion> mEnumerateInstanceVersion; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 186 | VkPtr<PFN_vkEnumerateInstanceExtensionProperties> mEnumerateInstanceExtensionProperties; |
| 187 | VkPtr<PFN_vkCreateInstance> mCreateInstance; |
| 188 | |
| 189 | VkPtr<PFN_vkDestroyInstance> mDestroyInstance; |
| 190 | VkPtr<PFN_vkEnumeratePhysicalDevices> mEnumeratePhysicalDevices; |
Greg Daniel | 9625962 | 2018-10-01 14:42:56 -0400 | [diff] [blame] | 191 | VkPtr<PFN_vkGetPhysicalDeviceProperties> mGetPhysicalDeviceProperties; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 192 | VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties; |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 193 | VkPtr<PFN_vkGetPhysicalDeviceFeatures2> mGetPhysicalDeviceFeatures2; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 194 | VkPtr<PFN_vkCreateDevice> mCreateDevice; |
| 195 | VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties; |
| 196 | |
| 197 | // Device Functions |
| 198 | VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue; |
| 199 | VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle; |
| 200 | VkPtr<PFN_vkDestroyDevice> mDestroyDevice; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 201 | VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool; |
| 202 | VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool; |
| 203 | VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers; |
| 204 | VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers; |
| 205 | VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer; |
| 206 | VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer; |
| 207 | VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer; |
| 208 | VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier; |
| 209 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 210 | VkPtr<PFN_vkQueueSubmit> mQueueSubmit; |
| 211 | VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 212 | |
| 213 | VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore; |
| 214 | VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore; |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 215 | VkPtr<PFN_vkImportSemaphoreFdKHR> mImportSemaphoreFdKHR; |
| 216 | VkPtr<PFN_vkGetSemaphoreFdKHR> mGetSemaphoreFdKHR; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 217 | VkPtr<PFN_vkCreateFence> mCreateFence; |
| 218 | VkPtr<PFN_vkDestroyFence> mDestroyFence; |
| 219 | VkPtr<PFN_vkWaitForFences> mWaitForFences; |
| 220 | VkPtr<PFN_vkResetFences> mResetFences; |
| 221 | |
| 222 | RenderThread& mRenderThread; |
| 223 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 224 | VkInstance mInstance = VK_NULL_HANDLE; |
| 225 | VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE; |
| 226 | VkDevice mDevice = VK_NULL_HANDLE; |
| 227 | |
| 228 | uint32_t mGraphicsQueueIndex; |
| 229 | VkQueue mGraphicsQueue = VK_NULL_HANDLE; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 230 | uint32_t mPresentQueueIndex; |
| 231 | VkQueue mPresentQueue = VK_NULL_HANDLE; |
| 232 | VkCommandPool mCommandPool = VK_NULL_HANDLE; |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 233 | |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 234 | VkCommandBuffer mDummyCB = VK_NULL_HANDLE; |
| 235 | |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 236 | enum class SwapBehavior { |
| 237 | Discard, |
| 238 | BufferAge, |
| 239 | }; |
| 240 | SwapBehavior mSwapBehavior = SwapBehavior::Discard; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | } /* namespace renderthread */ |
| 244 | } /* namespace uirenderer */ |
| 245 | } /* namespace android */ |
| 246 | |
| 247 | #endif /* VULKANMANAGER_H */ |