blob: 13335f32ef065d22bcfc5f07c126516daf7287af [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>
Stan Iliev564ca3e2018-09-04 22:00:00 +000025#include <utils/StrongPointer.h>
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050026#include <vk/GrVkBackendContext.h>
John Reck0fa0cbc2019-04-05 16:57:46 -070027#include <vk/GrVkExtensions.h>
Stan Ilievaaa9e832019-09-17 14:07:23 -040028#include <vulkan/vulkan.h>
29
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050030#include "Frame.h"
Stan Iliev79351f32018-09-19 14:23:49 -040031#include "IRenderPipeline.h"
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050032#include "VulkanSurface.h"
Derek Sollenberger5d0ca632019-07-19 16:17:12 -040033#include "private/hwui/DrawVkInfo.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050034
Greg Daniela227dbb2018-08-20 09:19:48 -040035class GrVkExtensions;
36
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050037namespace android {
38namespace uirenderer {
39namespace renderthread {
40
41class RenderThread;
42
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050043// 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.
Derek Sollenberger802fefa2020-08-13 16:53:30 -040046class VulkanManager final : public RefBase {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050047public:
Derek Sollenberger802fefa2020-08-13 16:53:30 -040048 static sp<VulkanManager> getInstance();
Stan Iliev981afe72019-02-13 14:24:33 -050049
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050050 // Sets up the vulkan context that is shared amonst all clients of the VulkanManager. This must
51 // be call once before use of the VulkanManager. Multiple calls after the first will simiply
52 // return.
53 void initialize();
54
55 // Quick check to see if the VulkanManager has been initialized.
Greg Daniel2f9d8672018-06-22 10:44:26 -040056 bool hasVkContext() { return mDevice != VK_NULL_HANDLE; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050057
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050058 // Create and destroy functions for wrapping an ANativeWindow in a VulkanSurface
Adlai Hollerf8c434e2020-07-27 11:42:45 -040059 VulkanSurface* createSurface(ANativeWindow* window,
60 ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -080061 sk_sp<SkColorSpace> surfaceColorSpace,
Adlai Hollerf8c434e2020-07-27 11:42:45 -040062 SkColorType surfaceColorType,
63 GrDirectContext* grContext,
John Reck0fa0cbc2019-04-05 16:57:46 -070064 uint32_t extraBuffers);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050065 void destroySurface(VulkanSurface* surface);
66
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050067 Frame dequeueNextBuffer(VulkanSurface* surface);
68 void swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect);
69
Stan Iliev564ca3e2018-09-04 22:00:00 +000070 // Inserts a wait on fence command into the Vulkan command buffer.
Adlai Hollerf8c434e2020-07-27 11:42:45 -040071 status_t fenceWait(int fence, GrDirectContext* grContext);
Stan Iliev564ca3e2018-09-04 22:00:00 +000072
Greg Danield92a9b12019-04-23 10:11:04 -040073 // Creates a fence that is signaled when all the pending Vulkan commands are finished on the
74 // GPU.
Adlai Hollerf8c434e2020-07-27 11:42:45 -040075 status_t createReleaseFence(int* nativeFence, GrDirectContext* grContext);
Stan Iliev564ca3e2018-09-04 22:00:00 +000076
Bo Liu7b8c1eb2019-01-08 20:17:55 -080077 // Returned pointers are owned by VulkanManager.
Roman Kiryanov74ace839e2019-03-07 18:22:19 -080078 // An instance of VkFunctorInitParams returned from getVkFunctorInitParams refers to
79 // the internal state of VulkanManager: VulkanManager must be alive to use the returned value.
Bo Liu7b8c1eb2019-01-08 20:17:55 -080080 VkFunctorInitParams getVkFunctorInitParams() const;
81
Derek Sollenberger802fefa2020-08-13 16:53:30 -040082
83 enum class ContextType {
84 kRenderThread,
85 kUploadThread
86 };
87
88 // returns a Skia graphic context used to draw content on the specified thread
89 sk_sp<GrDirectContext> createContext(const GrContextOptions& options,
90 ContextType contextType = ContextType::kRenderThread);
Stan Iliev981afe72019-02-13 14:24:33 -050091
Stan Ilievbf99c442019-03-29 11:09:11 -040092 uint32_t getDriverVersion() const { return mDriverVersion; }
93
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050094private:
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050095 friend class VulkanSurface;
Derek Sollenberger802fefa2020-08-13 16:53:30 -040096
97 explicit VulkanManager() {}
98 ~VulkanManager();
99
Greg Daniel2ff202712018-06-14 11:50:10 -0400100 // Sets up the VkInstance and VkDevice objects. Also fills out the passed in
101 // VkPhysicalDeviceFeatures struct.
Stan Iliev90276c82019-02-03 18:01:02 -0500102 void setupDevice(GrVkExtensions&, VkPhysicalDeviceFeatures2&);
Greg Daniel26e0dca2018-09-18 10:33:19 -0400103
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500104 // simple wrapper class that exists only to initialize a pointer to NULL
John Reck1bcacfd2017-11-03 10:12:19 -0700105 template <typename FNPTR_TYPE>
106 class VkPtr {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500107 public:
108 VkPtr() : fPtr(NULL) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700109 VkPtr operator=(FNPTR_TYPE ptr) {
110 fPtr = ptr;
111 return *this;
112 }
Chih-Hung Hsiehd736d4b2018-12-20 13:55:20 -0800113 // NOLINTNEXTLINE(google-explicit-constructor)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500114 operator FNPTR_TYPE() const { return fPtr; }
John Reck1bcacfd2017-11-03 10:12:19 -0700115
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500116 private:
117 FNPTR_TYPE fPtr;
118 };
119
Greg Daniel2ff202712018-06-14 11:50:10 -0400120 // Instance Functions
Greg Daniela227dbb2018-08-20 09:19:48 -0400121 VkPtr<PFN_vkEnumerateInstanceVersion> mEnumerateInstanceVersion;
Greg Daniel2ff202712018-06-14 11:50:10 -0400122 VkPtr<PFN_vkEnumerateInstanceExtensionProperties> mEnumerateInstanceExtensionProperties;
123 VkPtr<PFN_vkCreateInstance> mCreateInstance;
124
125 VkPtr<PFN_vkDestroyInstance> mDestroyInstance;
126 VkPtr<PFN_vkEnumeratePhysicalDevices> mEnumeratePhysicalDevices;
Greg Daniel96259622018-10-01 14:42:56 -0400127 VkPtr<PFN_vkGetPhysicalDeviceProperties> mGetPhysicalDeviceProperties;
Greg Daniel2ff202712018-06-14 11:50:10 -0400128 VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties;
Greg Daniela227dbb2018-08-20 09:19:48 -0400129 VkPtr<PFN_vkGetPhysicalDeviceFeatures2> mGetPhysicalDeviceFeatures2;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500130 VkPtr<PFN_vkGetPhysicalDeviceImageFormatProperties2> mGetPhysicalDeviceImageFormatProperties2;
Greg Daniel2ff202712018-06-14 11:50:10 -0400131 VkPtr<PFN_vkCreateDevice> mCreateDevice;
132 VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties;
133
134 // Device Functions
135 VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue;
136 VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle;
137 VkPtr<PFN_vkDestroyDevice> mDestroyDevice;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500138 VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool;
139 VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool;
140 VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers;
141 VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers;
142 VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer;
143 VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer;
144 VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer;
145 VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier;
146
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500147 VkPtr<PFN_vkQueueSubmit> mQueueSubmit;
148 VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500149
150 VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore;
151 VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400152 VkPtr<PFN_vkImportSemaphoreFdKHR> mImportSemaphoreFdKHR;
153 VkPtr<PFN_vkGetSemaphoreFdKHR> mGetSemaphoreFdKHR;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500154 VkPtr<PFN_vkCreateFence> mCreateFence;
155 VkPtr<PFN_vkDestroyFence> mDestroyFence;
156 VkPtr<PFN_vkWaitForFences> mWaitForFences;
157 VkPtr<PFN_vkResetFences> mResetFences;
158
Greg Daniel2ff202712018-06-14 11:50:10 -0400159 VkInstance mInstance = VK_NULL_HANDLE;
160 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
161 VkDevice mDevice = VK_NULL_HANDLE;
162
163 uint32_t mGraphicsQueueIndex;
164 VkQueue mGraphicsQueue = VK_NULL_HANDLE;
Derek Sollenberger802fefa2020-08-13 16:53:30 -0400165 VkQueue mAHBUploadQueue = VK_NULL_HANDLE;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500166 uint32_t mPresentQueueIndex;
167 VkQueue mPresentQueue = VK_NULL_HANDLE;
168 VkCommandPool mCommandPool = VK_NULL_HANDLE;
Greg Danielcd558522016-11-17 13:31:40 -0500169
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800170 // Variables saved to populate VkFunctorInitParams.
Greg Danieleaf310e2019-01-28 16:10:32 -0500171 static const uint32_t mAPIVersion = VK_MAKE_VERSION(1, 1, 0);
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800172 std::vector<VkExtensionProperties> mInstanceExtensionsOwner;
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800173 std::vector<const char*> mInstanceExtensions;
Roman Kiryanov74ace839e2019-03-07 18:22:19 -0800174 std::vector<VkExtensionProperties> mDeviceExtensionsOwner;
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800175 std::vector<const char*> mDeviceExtensions;
176 VkPhysicalDeviceFeatures2 mPhysicalDeviceFeatures2{};
177
Greg Danielcd558522016-11-17 13:31:40 -0500178 enum class SwapBehavior {
179 Discard,
180 BufferAge,
181 };
182 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
Stan Iliev981afe72019-02-13 14:24:33 -0500183 GrVkExtensions mExtensions;
Stan Ilievbf99c442019-03-29 11:09:11 -0400184 uint32_t mDriverVersion = 0;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500185};
186
187} /* namespace renderthread */
188} /* namespace uirenderer */
189} /* namespace android */
190
191#endif /* VULKANMANAGER_H */