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> |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 25 | #include <utils/StrongPointer.h> |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 26 | #include <vk/GrVkBackendContext.h> |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 27 | #include <vk/GrVkExtensions.h> |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 28 | #include <vulkan/vulkan.h> |
| 29 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 30 | #include "Frame.h" |
Stan Iliev | 79351f3 | 2018-09-19 14:23:49 -0400 | [diff] [blame] | 31 | #include "IRenderPipeline.h" |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 32 | #include "VulkanSurface.h" |
Derek Sollenberger | 5d0ca63 | 2019-07-19 16:17:12 -0400 | [diff] [blame] | 33 | #include "private/hwui/DrawVkInfo.h" |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 34 | |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 35 | class GrVkExtensions; |
| 36 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 37 | namespace android { |
| 38 | namespace uirenderer { |
| 39 | namespace renderthread { |
| 40 | |
| 41 | class RenderThread; |
| 42 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 43 | // This class contains the shared global Vulkan objects, such as VkInstance, VkDevice and VkQueue, |
| 44 | // which are re-used by CanvasContext. This class is created once and should be used by all vulkan |
| 45 | // windowing contexts. The VulkanManager must be initialized before use. |
| 46 | class VulkanManager { |
| 47 | public: |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 48 | explicit VulkanManager() {} |
| 49 | ~VulkanManager() { destroy(); } |
| 50 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 51 | // Sets up the vulkan context that is shared amonst all clients of the VulkanManager. This must |
| 52 | // be call once before use of the VulkanManager. Multiple calls after the first will simiply |
| 53 | // return. |
| 54 | void initialize(); |
| 55 | |
| 56 | // Quick check to see if the VulkanManager has been initialized. |
Greg Daniel | 2f9d867 | 2018-06-22 10:44:26 -0400 | [diff] [blame] | 57 | bool hasVkContext() { return mDevice != VK_NULL_HANDLE; } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 58 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 59 | // Create and destroy functions for wrapping an ANativeWindow in a VulkanSurface |
Stan Iliev | 987a80c0 | 2018-12-04 10:07:21 -0500 | [diff] [blame] | 60 | VulkanSurface* createSurface(ANativeWindow* window, ColorMode colorMode, |
Peiyong Lin | 3bff135 | 2018-12-11 07:56:07 -0800 | [diff] [blame] | 61 | sk_sp<SkColorSpace> surfaceColorSpace, |
John Reck | 0fa0cbc | 2019-04-05 16:57:46 -0700 | [diff] [blame] | 62 | SkColorType surfaceColorType, GrContext* grContext, |
| 63 | uint32_t extraBuffers); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 64 | void destroySurface(VulkanSurface* surface); |
| 65 | |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 66 | Frame dequeueNextBuffer(VulkanSurface* surface); |
| 67 | void swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect); |
| 68 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 69 | // Cleans up all the global state in the VulkanManger. |
| 70 | void destroy(); |
| 71 | |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 72 | // Inserts a wait on fence command into the Vulkan command buffer. |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 73 | status_t fenceWait(int fence, GrContext* grContext); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 74 | |
Greg Daniel | d92a9b1 | 2019-04-23 10:11:04 -0400 | [diff] [blame] | 75 | // Creates a fence that is signaled when all the pending Vulkan commands are finished on the |
| 76 | // GPU. |
Stan Iliev | aaa9e83 | 2019-09-17 14:07:23 -0400 | [diff] [blame] | 77 | status_t createReleaseFence(int* nativeFence, GrContext* grContext); |
Stan Iliev | 564ca3e | 2018-09-04 22:00:00 +0000 | [diff] [blame] | 78 | |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 79 | // Returned pointers are owned by VulkanManager. |
Roman Kiryanov | 74ace839e | 2019-03-07 18:22:19 -0800 | [diff] [blame] | 80 | // An instance of VkFunctorInitParams returned from getVkFunctorInitParams refers to |
| 81 | // 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] | 82 | VkFunctorInitParams getVkFunctorInitParams() const; |
| 83 | |
Stan Iliev | 898123b | 2019-02-14 14:57:44 -0500 | [diff] [blame] | 84 | sk_sp<GrContext> createContext(const GrContextOptions& options); |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 85 | |
Stan Iliev | bf99c44 | 2019-03-29 11:09:11 -0400 | [diff] [blame] | 86 | uint32_t getDriverVersion() const { return mDriverVersion; } |
| 87 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 88 | private: |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 89 | friend class VulkanSurface; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 90 | // Sets up the VkInstance and VkDevice objects. Also fills out the passed in |
| 91 | // VkPhysicalDeviceFeatures struct. |
Stan Iliev | 90276c8 | 2019-02-03 18:01:02 -0500 | [diff] [blame] | 92 | void setupDevice(GrVkExtensions&, VkPhysicalDeviceFeatures2&); |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 93 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 94 | // simple wrapper class that exists only to initialize a pointer to NULL |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 95 | template <typename FNPTR_TYPE> |
| 96 | class VkPtr { |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 97 | public: |
| 98 | VkPtr() : fPtr(NULL) {} |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 99 | VkPtr operator=(FNPTR_TYPE ptr) { |
| 100 | fPtr = ptr; |
| 101 | return *this; |
| 102 | } |
Chih-Hung Hsieh | d736d4b | 2018-12-20 13:55:20 -0800 | [diff] [blame] | 103 | // NOLINTNEXTLINE(google-explicit-constructor) |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 104 | operator FNPTR_TYPE() const { return fPtr; } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 105 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 106 | private: |
| 107 | FNPTR_TYPE fPtr; |
| 108 | }; |
| 109 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 110 | // Instance Functions |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 111 | VkPtr<PFN_vkEnumerateInstanceVersion> mEnumerateInstanceVersion; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 112 | VkPtr<PFN_vkEnumerateInstanceExtensionProperties> mEnumerateInstanceExtensionProperties; |
| 113 | VkPtr<PFN_vkCreateInstance> mCreateInstance; |
| 114 | |
| 115 | VkPtr<PFN_vkDestroyInstance> mDestroyInstance; |
| 116 | VkPtr<PFN_vkEnumeratePhysicalDevices> mEnumeratePhysicalDevices; |
Greg Daniel | 9625962 | 2018-10-01 14:42:56 -0400 | [diff] [blame] | 117 | VkPtr<PFN_vkGetPhysicalDeviceProperties> mGetPhysicalDeviceProperties; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 118 | VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties; |
Greg Daniel | a227dbb | 2018-08-20 09:19:48 -0400 | [diff] [blame] | 119 | VkPtr<PFN_vkGetPhysicalDeviceFeatures2> mGetPhysicalDeviceFeatures2; |
Derek Sollenberger | a19b71a | 2019-02-15 16:36:30 -0500 | [diff] [blame] | 120 | VkPtr<PFN_vkGetPhysicalDeviceImageFormatProperties2> mGetPhysicalDeviceImageFormatProperties2; |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 121 | VkPtr<PFN_vkCreateDevice> mCreateDevice; |
| 122 | VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties; |
| 123 | |
| 124 | // Device Functions |
| 125 | VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue; |
| 126 | VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle; |
| 127 | VkPtr<PFN_vkDestroyDevice> mDestroyDevice; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 128 | VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool; |
| 129 | VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool; |
| 130 | VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers; |
| 131 | VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers; |
| 132 | VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer; |
| 133 | VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer; |
| 134 | VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer; |
| 135 | VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier; |
| 136 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 137 | VkPtr<PFN_vkQueueSubmit> mQueueSubmit; |
| 138 | VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 139 | |
| 140 | VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore; |
| 141 | VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore; |
Greg Daniel | 26e0dca | 2018-09-18 10:33:19 -0400 | [diff] [blame] | 142 | VkPtr<PFN_vkImportSemaphoreFdKHR> mImportSemaphoreFdKHR; |
| 143 | VkPtr<PFN_vkGetSemaphoreFdKHR> mGetSemaphoreFdKHR; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 144 | VkPtr<PFN_vkCreateFence> mCreateFence; |
| 145 | VkPtr<PFN_vkDestroyFence> mDestroyFence; |
| 146 | VkPtr<PFN_vkWaitForFences> mWaitForFences; |
| 147 | VkPtr<PFN_vkResetFences> mResetFences; |
| 148 | |
Greg Daniel | 2ff20271 | 2018-06-14 11:50:10 -0400 | [diff] [blame] | 149 | VkInstance mInstance = VK_NULL_HANDLE; |
| 150 | VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE; |
| 151 | VkDevice mDevice = VK_NULL_HANDLE; |
| 152 | |
| 153 | uint32_t mGraphicsQueueIndex; |
| 154 | VkQueue mGraphicsQueue = VK_NULL_HANDLE; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 155 | uint32_t mPresentQueueIndex; |
| 156 | VkQueue mPresentQueue = VK_NULL_HANDLE; |
| 157 | VkCommandPool mCommandPool = VK_NULL_HANDLE; |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 158 | |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 159 | // Variables saved to populate VkFunctorInitParams. |
Greg Daniel | eaf310e | 2019-01-28 16:10:32 -0500 | [diff] [blame] | 160 | static const uint32_t mAPIVersion = VK_MAKE_VERSION(1, 1, 0); |
Roman Kiryanov | 74ace839e | 2019-03-07 18:22:19 -0800 | [diff] [blame] | 161 | std::vector<VkExtensionProperties> mInstanceExtensionsOwner; |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 162 | std::vector<const char*> mInstanceExtensions; |
Roman Kiryanov | 74ace839e | 2019-03-07 18:22:19 -0800 | [diff] [blame] | 163 | std::vector<VkExtensionProperties> mDeviceExtensionsOwner; |
Bo Liu | 7b8c1eb | 2019-01-08 20:17:55 -0800 | [diff] [blame] | 164 | std::vector<const char*> mDeviceExtensions; |
| 165 | VkPhysicalDeviceFeatures2 mPhysicalDeviceFeatures2{}; |
| 166 | |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 167 | enum class SwapBehavior { |
| 168 | Discard, |
| 169 | BufferAge, |
| 170 | }; |
| 171 | SwapBehavior mSwapBehavior = SwapBehavior::Discard; |
Stan Iliev | 981afe7 | 2019-02-13 14:24:33 -0500 | [diff] [blame] | 172 | GrVkExtensions mExtensions; |
Stan Iliev | bf99c44 | 2019-03-29 11:09:11 -0400 | [diff] [blame] | 173 | uint32_t mDriverVersion = 0; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | } /* namespace renderthread */ |
| 177 | } /* namespace uirenderer */ |
| 178 | } /* namespace android */ |
| 179 | |
| 180 | #endif /* VULKANMANAGER_H */ |