blob: b8c2bdf112f87bd3a7ab11ddfec9ef3f4fd7bc8e [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
Hugues Evrardb9510a02021-03-01 14:35:22 +000034// VK_ANDROID_frame_boundary is a bespoke extension defined by AGI
35// (https://github.com/google/agi) to enable profiling of apps rendering via
36// HWUI. This extension is not defined in Khronos, hence the need to declare it
37// manually here. There's a superseding extension (VK_EXT_frame_boundary) being
38// discussed in Khronos, but in the meantime we use the bespoke
39// VK_ANDROID_frame_boundary. This is a device extension that is implemented by
40// AGI's Vulkan capture layer, such that it is only supported by devices when
41// AGI is doing a capture of the app.
42//
43// TODO(b/182165045): use the Khronos blessed VK_EXT_frame_boudary once it has
44// landed in the spec.
45typedef void(VKAPI_PTR* PFN_vkFrameBoundaryANDROID)(VkDevice device, VkSemaphore semaphore,
46 VkImage image);
47#define VK_ANDROID_FRAME_BOUNDARY_EXTENSION_NAME "VK_ANDROID_frame_boundary"
48
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050049#include "Frame.h"
Stan Iliev79351f32018-09-19 14:23:49 -040050#include "IRenderPipeline.h"
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050051#include "VulkanSurface.h"
Derek Sollenberger5d0ca632019-07-19 16:17:12 -040052#include "private/hwui/DrawVkInfo.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050053
Greg Daniela227dbb2018-08-20 09:19:48 -040054class GrVkExtensions;
55
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050056namespace android {
57namespace uirenderer {
58namespace renderthread {
59
60class RenderThread;
61
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050062// This class contains the shared global Vulkan objects, such as VkInstance, VkDevice and VkQueue,
63// which are re-used by CanvasContext. This class is created once and should be used by all vulkan
64// windowing contexts. The VulkanManager must be initialized before use.
Derek Sollenberger802fefa2020-08-13 16:53:30 -040065class VulkanManager final : public RefBase {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050066public:
Derek Sollenberger802fefa2020-08-13 16:53:30 -040067 static sp<VulkanManager> getInstance();
Stan Iliev981afe72019-02-13 14:24:33 -050068
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050069 // Sets up the vulkan context that is shared amonst all clients of the VulkanManager. This must
70 // be call once before use of the VulkanManager. Multiple calls after the first will simiply
71 // return.
72 void initialize();
73
74 // Quick check to see if the VulkanManager has been initialized.
Greg Daniel2f9d8672018-06-22 10:44:26 -040075 bool hasVkContext() { return mDevice != VK_NULL_HANDLE; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050076
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050077 // Create and destroy functions for wrapping an ANativeWindow in a VulkanSurface
Adlai Hollerf8c434e2020-07-27 11:42:45 -040078 VulkanSurface* createSurface(ANativeWindow* window,
79 ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -080080 sk_sp<SkColorSpace> surfaceColorSpace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -040081 SkColorType surfaceColorType,
82 GrDirectContext* grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -070083 uint32_t extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050084 void destroySurface(VulkanSurface* surface);
85
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050086 Frame dequeueNextBuffer(VulkanSurface* surface);
Alec Mouri3afb3972022-05-27 22:03:11 +000087 // Finishes the frame and submits work to the GPU
88 // Returns the estimated start time for intiating GPU work, -1 otherwise.
89 nsecs_t finishFrame(SkSurface* surface);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050090 void swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect);
91
Stan Iliev564ca3e2018-09-04 22:00:00 +000092 // Inserts a wait on fence command into the Vulkan command buffer.
Adlai Hollerf8c434e2020-07-27 11:42:45 -040093 status_t fenceWait(int fence, GrDirectContext* grContext);
Stan Iliev564ca3e2018-09-04 22:00:00 +000094
Greg Danield92a9b12019-04-23 10:11:04 -040095 // Creates a fence that is signaled when all the pending Vulkan commands are finished on the
96 // GPU.
Adlai Hollerf8c434e2020-07-27 11:42:45 -040097 status_t createReleaseFence(int* nativeFence, GrDirectContext* grContext);
Stan Iliev564ca3e2018-09-04 22:00:00 +000098
Bo Liu7b8c1eb2019-01-08 20:17:55 -080099 // Returned pointers are owned by VulkanManager.
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800100 // An instance of VkFunctorInitParams returned from getVkFunctorInitParams refers to
101 // the internal state of VulkanManager: VulkanManager must be alive to use the returned value.
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800102 VkFunctorInitParams getVkFunctorInitParams() const;
103
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400104
105 enum class ContextType {
106 kRenderThread,
107 kUploadThread
108 };
109
110 // returns a Skia graphic context used to draw content on the specified thread
111 sk_sp<GrDirectContext> createContext(const GrContextOptions& options,
112 ContextType contextType = ContextType::kRenderThread);
Stan Iliev981afe72019-02-13 14:24:33 -0500113
Stan Ilievbf99c442019-03-29 11:09:11 -0400114 uint32_t getDriverVersion() const { return mDriverVersion; }
115
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500116private:
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500117 friend class VulkanSurface;
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400118
119 explicit VulkanManager() {}
120 ~VulkanManager();
121
Greg Daniel2ff202712018-06-14 11:50:10 -0400122 // Sets up the VkInstance and VkDevice objects. Also fills out the passed in
123 // VkPhysicalDeviceFeatures struct.
Stan Iliev90276c82019-02-03 18:01:02 -0500124 void setupDevice(GrVkExtensions&, VkPhysicalDeviceFeatures2&);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400125
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500126 // simple wrapper class that exists only to initialize a pointer to NULL
John Reck1bcacfd2017-11-03 10:12:19 -0700127 template <typename FNPTR_TYPE>
128 class VkPtr {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500129 public:
130 VkPtr() : fPtr(NULL) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700131 VkPtr operator=(FNPTR_TYPE ptr) {
132 fPtr = ptr;
133 return *this;
134 }
Chih-Hung Hsiehd736d4b2018-12-20 13:55:20 -0800135 // NOLINTNEXTLINE(google-explicit-constructor)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500136 operator FNPTR_TYPE() const { return fPtr; }
John Reck1bcacfd2017-11-03 10:12:19 -0700137
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500138 private:
139 FNPTR_TYPE fPtr;
140 };
141
Greg Daniel2ff202712018-06-14 11:50:10 -0400142 // Instance Functions
Greg Daniela227dbb2018-08-20 09:19:48 -0400143 VkPtr<PFN_vkEnumerateInstanceVersion> mEnumerateInstanceVersion;
Greg Daniel2ff202712018-06-14 11:50:10 -0400144 VkPtr<PFN_vkEnumerateInstanceExtensionProperties> mEnumerateInstanceExtensionProperties;
145 VkPtr<PFN_vkCreateInstance> mCreateInstance;
146
147 VkPtr<PFN_vkDestroyInstance> mDestroyInstance;
148 VkPtr<PFN_vkEnumeratePhysicalDevices> mEnumeratePhysicalDevices;
Greg Daniel96259622018-10-01 14:42:56 -0400149 VkPtr<PFN_vkGetPhysicalDeviceProperties> mGetPhysicalDeviceProperties;
Greg Daniel2ff202712018-06-14 11:50:10 -0400150 VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties;
Greg Daniela227dbb2018-08-20 09:19:48 -0400151 VkPtr<PFN_vkGetPhysicalDeviceFeatures2> mGetPhysicalDeviceFeatures2;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500152 VkPtr<PFN_vkGetPhysicalDeviceImageFormatProperties2> mGetPhysicalDeviceImageFormatProperties2;
Greg Daniel2ff202712018-06-14 11:50:10 -0400153 VkPtr<PFN_vkCreateDevice> mCreateDevice;
154 VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties;
155
156 // Device Functions
157 VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue;
158 VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle;
159 VkPtr<PFN_vkDestroyDevice> mDestroyDevice;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500160 VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool;
161 VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool;
162 VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers;
163 VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers;
164 VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer;
165 VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer;
166 VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer;
167 VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier;
168
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500169 VkPtr<PFN_vkQueueSubmit> mQueueSubmit;
170 VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500171
172 VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore;
173 VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400174 VkPtr<PFN_vkImportSemaphoreFdKHR> mImportSemaphoreFdKHR;
175 VkPtr<PFN_vkGetSemaphoreFdKHR> mGetSemaphoreFdKHR;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500176 VkPtr<PFN_vkCreateFence> mCreateFence;
177 VkPtr<PFN_vkDestroyFence> mDestroyFence;
178 VkPtr<PFN_vkWaitForFences> mWaitForFences;
179 VkPtr<PFN_vkResetFences> mResetFences;
Hugues Evrardb9510a02021-03-01 14:35:22 +0000180 VkPtr<PFN_vkFrameBoundaryANDROID> mFrameBoundaryANDROID;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500181
Greg Daniel2ff202712018-06-14 11:50:10 -0400182 VkInstance mInstance = VK_NULL_HANDLE;
183 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
184 VkDevice mDevice = VK_NULL_HANDLE;
185
186 uint32_t mGraphicsQueueIndex;
Alec Mouriaa3e4982020-12-14 14:47:57 -0800187
188 std::mutex mGraphicsQueueMutex;
Greg Daniel2ff202712018-06-14 11:50:10 -0400189 VkQueue mGraphicsQueue = VK_NULL_HANDLE;
Alec Mouriaa3e4982020-12-14 14:47:57 -0800190
191 static VKAPI_ATTR VkResult interceptedVkQueueSubmit(VkQueue queue, uint32_t submitCount,
192 const VkSubmitInfo* pSubmits,
193 VkFence fence) {
194 sp<VulkanManager> manager = VulkanManager::getInstance();
195 std::lock_guard<std::mutex> lock(manager->mGraphicsQueueMutex);
196 return manager->mQueueSubmit(queue, submitCount, pSubmits, fence);
197 }
198
199 static VKAPI_ATTR VkResult interceptedVkQueueWaitIdle(VkQueue queue) {
200 sp<VulkanManager> manager = VulkanManager::getInstance();
201 std::lock_guard<std::mutex> lock(manager->mGraphicsQueueMutex);
202 return manager->mQueueWaitIdle(queue);
203 }
204
205 static GrVkGetProc sSkiaGetProp;
Greg Danielcd558522016-11-17 13:31:40 -0500206
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800207 // Variables saved to populate VkFunctorInitParams.
Greg Danieleaf310e2019-01-28 16:10:32 -0500208 static const uint32_t mAPIVersion = VK_MAKE_VERSION(1, 1, 0);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800209 std::vector<VkExtensionProperties> mInstanceExtensionsOwner;
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800210 std::vector<const char*> mInstanceExtensions;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800211 std::vector<VkExtensionProperties> mDeviceExtensionsOwner;
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800212 std::vector<const char*> mDeviceExtensions;
213 VkPhysicalDeviceFeatures2 mPhysicalDeviceFeatures2{};
214
Greg Danielcd558522016-11-17 13:31:40 -0500215 enum class SwapBehavior {
216 Discard,
217 BufferAge,
218 };
219 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
Stan Iliev981afe72019-02-13 14:24:33 -0500220 GrVkExtensions mExtensions;
Stan Ilievbf99c442019-03-29 11:09:11 -0400221 uint32_t mDriverVersion = 0;
Greg Danielbe2803a2021-02-19 18:32:16 -0500222
223 VkSemaphore mSwapSemaphore = VK_NULL_HANDLE;
224 void* mDestroySemaphoreContext = nullptr;
Greg Daniel1d9de712021-05-14 09:28:34 -0400225
226 std::mutex mInitializeLock;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500227};
228
229} /* namespace renderthread */
230} /* namespace uirenderer */
231} /* namespace android */
232
233#endif /* VULKANMANAGER_H */