blob: b92ebb3cdf71d616a27bc6162675eebac8ca7618 [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
Greg Daniel22cc59d2018-07-24 13:46:10 -040020#if !defined(VK_USE_PLATFORM_ANDROID_KHR)
John Reck0fa0cbc2019-04-05 16:57:46 -070021#define VK_USE_PLATFORM_ANDROID_KHR
Greg Daniel22cc59d2018-07-24 13:46:10 -040022#endif
Stan Iliev981afe72019-02-13 14:24:33 -050023#include <GrContextOptions.h>
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050024#include <SkSurface.h>
John Reck5d3fac12023-11-08 23:08:10 -050025#include <android-base/unique_fd.h>
Stan Iliev564ca3e2018-09-04 22:00:00 +000026#include <utils/StrongPointer.h>
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050027#include <vk/GrVkBackendContext.h>
John Reck0fa0cbc2019-04-05 16:57:46 -070028#include <vk/GrVkExtensions.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040029#include <vulkan/vulkan.h>
30
Hugues Evrardb9510a02021-03-01 14:35:22 +000031// VK_ANDROID_frame_boundary is a bespoke extension defined by AGI
32// (https://github.com/google/agi) to enable profiling of apps rendering via
33// HWUI. This extension is not defined in Khronos, hence the need to declare it
34// manually here. There's a superseding extension (VK_EXT_frame_boundary) being
35// discussed in Khronos, but in the meantime we use the bespoke
36// VK_ANDROID_frame_boundary. This is a device extension that is implemented by
37// AGI's Vulkan capture layer, such that it is only supported by devices when
38// AGI is doing a capture of the app.
39//
40// TODO(b/182165045): use the Khronos blessed VK_EXT_frame_boudary once it has
41// landed in the spec.
42typedef void(VKAPI_PTR* PFN_vkFrameBoundaryANDROID)(VkDevice device, VkSemaphore semaphore,
43 VkImage image);
44#define VK_ANDROID_FRAME_BOUNDARY_EXTENSION_NAME "VK_ANDROID_frame_boundary"
45
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050046#include "Frame.h"
Stan Iliev79351f32018-09-19 14:23:49 -040047#include "IRenderPipeline.h"
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050048#include "VulkanSurface.h"
Derek Sollenberger5d0ca632019-07-19 16:17:12 -040049#include "private/hwui/DrawVkInfo.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050050
Kevin Lubick07d6aae2022-04-01 14:03:11 -040051#include <SkColorSpace.h>
52#include <SkRefCnt.h>
53
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050054namespace android {
55namespace uirenderer {
56namespace renderthread {
57
58class RenderThread;
59
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050060// This class contains the shared global Vulkan objects, such as VkInstance, VkDevice and VkQueue,
61// which are re-used by CanvasContext. This class is created once and should be used by all vulkan
62// windowing contexts. The VulkanManager must be initialized before use.
Derek Sollenberger802fefa2020-08-13 16:53:30 -040063class VulkanManager final : public RefBase {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050064public:
Derek Sollenberger802fefa2020-08-13 16:53:30 -040065 static sp<VulkanManager> getInstance();
John Reck9fc3d272023-05-01 16:33:22 -040066 static sp<VulkanManager> peekInstance();
Stan Iliev981afe72019-02-13 14:24:33 -050067
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050068 // Sets up the vulkan context that is shared amonst all clients of the VulkanManager. This must
69 // be call once before use of the VulkanManager. Multiple calls after the first will simiply
70 // return.
71 void initialize();
72
73 // Quick check to see if the VulkanManager has been initialized.
Alec Mouri671a9f62023-08-25 17:18:02 +000074 bool hasVkContext() { return mInitialized; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050075
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050076 // Create and destroy functions for wrapping an ANativeWindow in a VulkanSurface
Adlai Hollerf8c434e2020-07-27 11:42:45 -040077 VulkanSurface* createSurface(ANativeWindow* window,
78 ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -080079 sk_sp<SkColorSpace> surfaceColorSpace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -040080 SkColorType surfaceColorType,
81 GrDirectContext* grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -070082 uint32_t extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050083 void destroySurface(VulkanSurface* surface);
84
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050085 Frame dequeueNextBuffer(VulkanSurface* surface);
John Reck5d3fac12023-11-08 23:08:10 -050086
87 struct VkDrawResult {
88 // The estimated start time for intiating GPU work, -1 if unknown.
89 nsecs_t submissionTime;
90 android::base::unique_fd presentFence;
91 };
92
Alec Mouri3afb3972022-05-27 22:03:11 +000093 // Finishes the frame and submits work to the GPU
John Reck5d3fac12023-11-08 23:08:10 -050094 VkDrawResult finishFrame(SkSurface* surface);
95 void swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect,
96 android::base::unique_fd&& presentFence);
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050097
Stan Iliev564ca3e2018-09-04 22:00:00 +000098 // Inserts a wait on fence command into the Vulkan command buffer.
Adlai Hollerf8c434e2020-07-27 11:42:45 -040099 status_t fenceWait(int fence, GrDirectContext* grContext);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000100
Greg Danield92a9b12019-04-23 10:11:04 -0400101 // Creates a fence that is signaled when all the pending Vulkan commands are finished on the
102 // GPU.
Adlai Hollerf8c434e2020-07-27 11:42:45 -0400103 status_t createReleaseFence(int* nativeFence, GrDirectContext* grContext);
Stan Iliev564ca3e2018-09-04 22:00:00 +0000104
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800105 // Returned pointers are owned by VulkanManager.
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800106 // An instance of VkFunctorInitParams returned from getVkFunctorInitParams refers to
107 // the internal state of VulkanManager: VulkanManager must be alive to use the returned value.
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800108 VkFunctorInitParams getVkFunctorInitParams() const;
109
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400110
111 enum class ContextType {
112 kRenderThread,
113 kUploadThread
114 };
115
116 // returns a Skia graphic context used to draw content on the specified thread
John Reck9fc3d272023-05-01 16:33:22 -0400117 sk_sp<GrDirectContext> createContext(GrContextOptions& options,
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400118 ContextType contextType = ContextType::kRenderThread);
Stan Iliev981afe72019-02-13 14:24:33 -0500119
Stan Ilievbf99c442019-03-29 11:09:11 -0400120 uint32_t getDriverVersion() const { return mDriverVersion; }
121
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500122private:
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500123 friend class VulkanSurface;
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400124
125 explicit VulkanManager() {}
126 ~VulkanManager();
127
Greg Daniel2ff202712018-06-14 11:50:10 -0400128 // Sets up the VkInstance and VkDevice objects. Also fills out the passed in
129 // VkPhysicalDeviceFeatures struct.
Stan Iliev90276c82019-02-03 18:01:02 -0500130 void setupDevice(GrVkExtensions&, VkPhysicalDeviceFeatures2&);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400131
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500132 // simple wrapper class that exists only to initialize a pointer to NULL
John Reck1bcacfd2017-11-03 10:12:19 -0700133 template <typename FNPTR_TYPE>
134 class VkPtr {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500135 public:
136 VkPtr() : fPtr(NULL) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700137 VkPtr operator=(FNPTR_TYPE ptr) {
138 fPtr = ptr;
139 return *this;
140 }
Chih-Hung Hsiehd736d4b2018-12-20 13:55:20 -0800141 // NOLINTNEXTLINE(google-explicit-constructor)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500142 operator FNPTR_TYPE() const { return fPtr; }
John Reck1bcacfd2017-11-03 10:12:19 -0700143
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500144 private:
145 FNPTR_TYPE fPtr;
146 };
147
Greg Daniel2ff202712018-06-14 11:50:10 -0400148 // Instance Functions
Greg Daniela227dbb2018-08-20 09:19:48 -0400149 VkPtr<PFN_vkEnumerateInstanceVersion> mEnumerateInstanceVersion;
Greg Daniel2ff202712018-06-14 11:50:10 -0400150 VkPtr<PFN_vkEnumerateInstanceExtensionProperties> mEnumerateInstanceExtensionProperties;
151 VkPtr<PFN_vkCreateInstance> mCreateInstance;
152
153 VkPtr<PFN_vkDestroyInstance> mDestroyInstance;
154 VkPtr<PFN_vkEnumeratePhysicalDevices> mEnumeratePhysicalDevices;
Greg Daniel96259622018-10-01 14:42:56 -0400155 VkPtr<PFN_vkGetPhysicalDeviceProperties> mGetPhysicalDeviceProperties;
Greg Daniel2ff202712018-06-14 11:50:10 -0400156 VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties;
Greg Daniela227dbb2018-08-20 09:19:48 -0400157 VkPtr<PFN_vkGetPhysicalDeviceFeatures2> mGetPhysicalDeviceFeatures2;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500158 VkPtr<PFN_vkGetPhysicalDeviceImageFormatProperties2> mGetPhysicalDeviceImageFormatProperties2;
Greg Daniel2ff202712018-06-14 11:50:10 -0400159 VkPtr<PFN_vkCreateDevice> mCreateDevice;
160 VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties;
161
162 // Device Functions
163 VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue;
164 VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle;
165 VkPtr<PFN_vkDestroyDevice> mDestroyDevice;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500166 VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool;
167 VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool;
168 VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers;
169 VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers;
170 VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer;
171 VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer;
172 VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer;
173 VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier;
174
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500175 VkPtr<PFN_vkQueueSubmit> mQueueSubmit;
176 VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500177
178 VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore;
179 VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400180 VkPtr<PFN_vkImportSemaphoreFdKHR> mImportSemaphoreFdKHR;
181 VkPtr<PFN_vkGetSemaphoreFdKHR> mGetSemaphoreFdKHR;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500182 VkPtr<PFN_vkCreateFence> mCreateFence;
183 VkPtr<PFN_vkDestroyFence> mDestroyFence;
184 VkPtr<PFN_vkWaitForFences> mWaitForFences;
185 VkPtr<PFN_vkResetFences> mResetFences;
Hugues Evrardb9510a02021-03-01 14:35:22 +0000186 VkPtr<PFN_vkFrameBoundaryANDROID> mFrameBoundaryANDROID;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500187
Greg Daniel2ff202712018-06-14 11:50:10 -0400188 VkInstance mInstance = VK_NULL_HANDLE;
189 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
190 VkDevice mDevice = VK_NULL_HANDLE;
191
192 uint32_t mGraphicsQueueIndex;
193 VkQueue mGraphicsQueue = VK_NULL_HANDLE;
Alec Mouri219997a2023-05-23 17:25:19 +0000194 VkQueue mAHBUploadQueue = VK_NULL_HANDLE;
Greg Danielcd558522016-11-17 13:31:40 -0500195
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800196 // Variables saved to populate VkFunctorInitParams.
Greg Danieleaf310e2019-01-28 16:10:32 -0500197 static const uint32_t mAPIVersion = VK_MAKE_VERSION(1, 1, 0);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800198 std::vector<VkExtensionProperties> mInstanceExtensionsOwner;
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800199 std::vector<const char*> mInstanceExtensions;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800200 std::vector<VkExtensionProperties> mDeviceExtensionsOwner;
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800201 std::vector<const char*> mDeviceExtensions;
202 VkPhysicalDeviceFeatures2 mPhysicalDeviceFeatures2{};
203
Greg Danielcd558522016-11-17 13:31:40 -0500204 enum class SwapBehavior {
205 Discard,
206 BufferAge,
207 };
208 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
Stan Iliev981afe72019-02-13 14:24:33 -0500209 GrVkExtensions mExtensions;
Stan Ilievbf99c442019-03-29 11:09:11 -0400210 uint32_t mDriverVersion = 0;
Greg Danielbe2803a2021-02-19 18:32:16 -0500211
Alec Mouri671a9f62023-08-25 17:18:02 +0000212 std::once_flag mInitFlag;
213 std::atomic_bool mInitialized = false;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500214};
215
216} /* namespace renderthread */
217} /* namespace uirenderer */
218} /* namespace android */
219
220#endif /* VULKANMANAGER_H */