blob: 0912369b611d6b8d4212f1e00611f463787c7da2 [file] [log] [blame]
Derek Sollenberger0e3cba32016-11-09 11:58:36 -05001/*
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
Alec Mouriaa3e4982020-12-14 14:47:57 -080020#include <functional>
21#include <mutex>
22
23#include "vulkan/vulkan_core.h"
Greg Daniel22cc59d2018-07-24 13:46:10 -040024#if !defined(VK_USE_PLATFORM_ANDROID_KHR)
John Reck0fa0cbc2019-04-05 16:57:46 -070025#define VK_USE_PLATFORM_ANDROID_KHR
Greg Daniel22cc59d2018-07-24 13:46:10 -040026#endif
Stan Iliev981afe72019-02-13 14:24:33 -050027#include <GrContextOptions.h>
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050028#include <SkSurface.h>
Stan Iliev564ca3e2018-09-04 22:00:00 +000029#include <utils/StrongPointer.h>
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050030#include <vk/GrVkBackendContext.h>
John Reck0fa0cbc2019-04-05 16:57:46 -070031#include <vk/GrVkExtensions.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040032#include <vulkan/vulkan.h>
33
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050034#include "Frame.h"
Stan Iliev79351f32018-09-19 14:23:49 -040035#include "IRenderPipeline.h"
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050036#include "VulkanSurface.h"
Derek Sollenberger5d0ca632019-07-19 16:17:12 -040037#include "private/hwui/DrawVkInfo.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050038
Greg Daniela227dbb2018-08-20 09:19:48 -040039class GrVkExtensions;
40
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050041namespace android {
42namespace uirenderer {
43namespace renderthread {
44
45class RenderThread;
46
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050047// This class contains the shared global Vulkan objects, such as VkInstance, VkDevice and VkQueue,
48// which are re-used by CanvasContext. This class is created once and should be used by all vulkan
49// windowing contexts. The VulkanManager must be initialized before use.
Derek Sollenberger802fefa2020-08-13 16:53:30 -040050class VulkanManager final : public RefBase {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050051public:
Derek Sollenberger802fefa2020-08-13 16:53:30 -040052 static sp<VulkanManager> getInstance();
Stan Iliev981afe72019-02-13 14:24:33 -050053
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050054 // Sets up the vulkan context that is shared amonst all clients of the VulkanManager. This must
55 // be call once before use of the VulkanManager. Multiple calls after the first will simiply
56 // return.
57 void initialize();
58
59 // Quick check to see if the VulkanManager has been initialized.
Greg Daniel2f9d8672018-06-22 10:44:26 -040060 bool hasVkContext() { return mDevice != VK_NULL_HANDLE; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050061
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050062 // Create and destroy functions for wrapping an ANativeWindow in a VulkanSurface
Adlai Hollerf8c434e2020-07-27 11:42:45 -040063 VulkanSurface* createSurface(ANativeWindow* window,
64 ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -080065 sk_sp<SkColorSpace> surfaceColorSpace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -040066 SkColorType surfaceColorType,
67 GrDirectContext* grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -070068 uint32_t extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050069 void destroySurface(VulkanSurface* surface);
70
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050071 Frame dequeueNextBuffer(VulkanSurface* surface);
Greg Danielbe2803a2021-02-19 18:32:16 -050072 void finishFrame(SkSurface* surface);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050073 void swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect);
74
Stan Iliev564ca3e2018-09-04 22:00:00 +000075 // Inserts a wait on fence command into the Vulkan command buffer.
Adlai Hollerf8c434e2020-07-27 11:42:45 -040076 status_t fenceWait(int fence, GrDirectContext* grContext);
Stan Iliev564ca3e2018-09-04 22:00:00 +000077
Greg Danield92a9b12019-04-23 10:11:04 -040078 // Creates a fence that is signaled when all the pending Vulkan commands are finished on the
79 // GPU.
Adlai Hollerf8c434e2020-07-27 11:42:45 -040080 status_t createReleaseFence(int* nativeFence, GrDirectContext* grContext);
Stan Iliev564ca3e2018-09-04 22:00:00 +000081
Bo Liu7b8c1eb2019-01-08 20:17:55 -080082 // Returned pointers are owned by VulkanManager.
Roman Kiryanov74ace839e2019-03-07 18:22:19 -080083 // An instance of VkFunctorInitParams returned from getVkFunctorInitParams refers to
84 // the internal state of VulkanManager: VulkanManager must be alive to use the returned value.
Bo Liu7b8c1eb2019-01-08 20:17:55 -080085 VkFunctorInitParams getVkFunctorInitParams() const;
86
Derek Sollenberger802fefa2020-08-13 16:53:30 -040087
88 enum class ContextType {
89 kRenderThread,
90 kUploadThread
91 };
92
93 // returns a Skia graphic context used to draw content on the specified thread
94 sk_sp<GrDirectContext> createContext(const GrContextOptions& options,
95 ContextType contextType = ContextType::kRenderThread);
Stan Iliev981afe72019-02-13 14:24:33 -050096
Stan Ilievbf99c442019-03-29 11:09:11 -040097 uint32_t getDriverVersion() const { return mDriverVersion; }
98
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050099private:
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500100 friend class VulkanSurface;
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400101
102 explicit VulkanManager() {}
103 ~VulkanManager();
104
Greg Daniel2ff202712018-06-14 11:50:10 -0400105 // Sets up the VkInstance and VkDevice objects. Also fills out the passed in
106 // VkPhysicalDeviceFeatures struct.
Stan Iliev90276c82019-02-03 18:01:02 -0500107 void setupDevice(GrVkExtensions&, VkPhysicalDeviceFeatures2&);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400108
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500109 // simple wrapper class that exists only to initialize a pointer to NULL
John Reck1bcacfd2017-11-03 10:12:19 -0700110 template <typename FNPTR_TYPE>
111 class VkPtr {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500112 public:
113 VkPtr() : fPtr(NULL) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700114 VkPtr operator=(FNPTR_TYPE ptr) {
115 fPtr = ptr;
116 return *this;
117 }
Chih-Hung Hsiehd736d4b2018-12-20 13:55:20 -0800118 // NOLINTNEXTLINE(google-explicit-constructor)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500119 operator FNPTR_TYPE() const { return fPtr; }
John Reck1bcacfd2017-11-03 10:12:19 -0700120
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500121 private:
122 FNPTR_TYPE fPtr;
123 };
124
Greg Daniel2ff202712018-06-14 11:50:10 -0400125 // Instance Functions
Greg Daniela227dbb2018-08-20 09:19:48 -0400126 VkPtr<PFN_vkEnumerateInstanceVersion> mEnumerateInstanceVersion;
Greg Daniel2ff202712018-06-14 11:50:10 -0400127 VkPtr<PFN_vkEnumerateInstanceExtensionProperties> mEnumerateInstanceExtensionProperties;
128 VkPtr<PFN_vkCreateInstance> mCreateInstance;
129
130 VkPtr<PFN_vkDestroyInstance> mDestroyInstance;
131 VkPtr<PFN_vkEnumeratePhysicalDevices> mEnumeratePhysicalDevices;
Greg Daniel96259622018-10-01 14:42:56 -0400132 VkPtr<PFN_vkGetPhysicalDeviceProperties> mGetPhysicalDeviceProperties;
Greg Daniel2ff202712018-06-14 11:50:10 -0400133 VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties;
Greg Daniela227dbb2018-08-20 09:19:48 -0400134 VkPtr<PFN_vkGetPhysicalDeviceFeatures2> mGetPhysicalDeviceFeatures2;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500135 VkPtr<PFN_vkGetPhysicalDeviceImageFormatProperties2> mGetPhysicalDeviceImageFormatProperties2;
Greg Daniel2ff202712018-06-14 11:50:10 -0400136 VkPtr<PFN_vkCreateDevice> mCreateDevice;
137 VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties;
138
139 // Device Functions
140 VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue;
141 VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle;
142 VkPtr<PFN_vkDestroyDevice> mDestroyDevice;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500143 VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool;
144 VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool;
145 VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers;
146 VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers;
147 VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer;
148 VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer;
149 VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer;
150 VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier;
151
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500152 VkPtr<PFN_vkQueueSubmit> mQueueSubmit;
153 VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500154
155 VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore;
156 VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400157 VkPtr<PFN_vkImportSemaphoreFdKHR> mImportSemaphoreFdKHR;
158 VkPtr<PFN_vkGetSemaphoreFdKHR> mGetSemaphoreFdKHR;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500159 VkPtr<PFN_vkCreateFence> mCreateFence;
160 VkPtr<PFN_vkDestroyFence> mDestroyFence;
161 VkPtr<PFN_vkWaitForFences> mWaitForFences;
162 VkPtr<PFN_vkResetFences> mResetFences;
163
Greg Daniel2ff202712018-06-14 11:50:10 -0400164 VkInstance mInstance = VK_NULL_HANDLE;
165 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
166 VkDevice mDevice = VK_NULL_HANDLE;
167
168 uint32_t mGraphicsQueueIndex;
Alec Mouriaa3e4982020-12-14 14:47:57 -0800169
170 std::mutex mGraphicsQueueMutex;
Greg Daniel2ff202712018-06-14 11:50:10 -0400171 VkQueue mGraphicsQueue = VK_NULL_HANDLE;
Alec Mouriaa3e4982020-12-14 14:47:57 -0800172
173 static VKAPI_ATTR VkResult interceptedVkQueueSubmit(VkQueue queue, uint32_t submitCount,
174 const VkSubmitInfo* pSubmits,
175 VkFence fence) {
176 sp<VulkanManager> manager = VulkanManager::getInstance();
177 std::lock_guard<std::mutex> lock(manager->mGraphicsQueueMutex);
178 return manager->mQueueSubmit(queue, submitCount, pSubmits, fence);
179 }
180
181 static VKAPI_ATTR VkResult interceptedVkQueueWaitIdle(VkQueue queue) {
182 sp<VulkanManager> manager = VulkanManager::getInstance();
183 std::lock_guard<std::mutex> lock(manager->mGraphicsQueueMutex);
184 return manager->mQueueWaitIdle(queue);
185 }
186
187 static GrVkGetProc sSkiaGetProp;
Greg Danielcd558522016-11-17 13:31:40 -0500188
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800189 // Variables saved to populate VkFunctorInitParams.
Greg Danieleaf310e2019-01-28 16:10:32 -0500190 static const uint32_t mAPIVersion = VK_MAKE_VERSION(1, 1, 0);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800191 std::vector<VkExtensionProperties> mInstanceExtensionsOwner;
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800192 std::vector<const char*> mInstanceExtensions;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800193 std::vector<VkExtensionProperties> mDeviceExtensionsOwner;
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800194 std::vector<const char*> mDeviceExtensions;
195 VkPhysicalDeviceFeatures2 mPhysicalDeviceFeatures2{};
196
Greg Danielcd558522016-11-17 13:31:40 -0500197 enum class SwapBehavior {
198 Discard,
199 BufferAge,
200 };
201 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
Stan Iliev981afe72019-02-13 14:24:33 -0500202 GrVkExtensions mExtensions;
Stan Ilievbf99c442019-03-29 11:09:11 -0400203 uint32_t mDriverVersion = 0;
Greg Danielbe2803a2021-02-19 18:32:16 -0500204
205 VkSemaphore mSwapSemaphore = VK_NULL_HANDLE;
206 void* mDestroySemaphoreContext = nullptr;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500207};
208
209} /* namespace renderthread */
210} /* namespace uirenderer */
211} /* namespace android */
212
213#endif /* VULKANMANAGER_H */