blob: c3d28917657699161b7364034191212483e05365 [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)
21# define VK_USE_PLATFORM_ANDROID_KHR
22#endif
23#include <vulkan/vulkan.h>
24
Stan Iliev981afe72019-02-13 14:24:33 -050025#include <GrContextOptions.h>
26#include <vk/GrVkExtensions.h>
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050027#include <SkSurface.h>
Stan Iliev564ca3e2018-09-04 22:00:00 +000028#include <ui/Fence.h>
29#include <utils/StrongPointer.h>
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050030#include <vk/GrVkBackendContext.h>
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050031#include "Frame.h"
Stan Iliev79351f32018-09-19 14:23:49 -040032#include "IRenderPipeline.h"
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050033#include "VulkanSurface.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.
46class VulkanManager {
47public:
Stan Iliev981afe72019-02-13 14:24:33 -050048 explicit VulkanManager() {}
49 ~VulkanManager() { destroy(); }
50
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050051 // 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 Daniel2f9d8672018-06-22 10:44:26 -040057 bool hasVkContext() { return mDevice != VK_NULL_HANDLE; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050058
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050059 // Create and destroy functions for wrapping an ANativeWindow in a VulkanSurface
Stan Iliev987a80c2018-12-04 10:07:21 -050060 VulkanSurface* createSurface(ANativeWindow* window, ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -080061 sk_sp<SkColorSpace> surfaceColorSpace,
Stan Iliev981afe72019-02-13 14:24:33 -050062 SkColorType surfaceColorType,
63 GrContext* grContext);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050064 void destroySurface(VulkanSurface* surface);
65
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050066 Frame dequeueNextBuffer(VulkanSurface* surface);
67 void swapBuffers(VulkanSurface* surface, const SkRect& dirtyRect);
68
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050069 // Cleans up all the global state in the VulkanManger.
70 void destroy();
71
Stan Iliev564ca3e2018-09-04 22:00:00 +000072 // Inserts a wait on fence command into the Vulkan command buffer.
73 status_t fenceWait(sp<Fence>& fence);
74
75 // Creates a fence that is signaled, when all the pending Vulkan commands are flushed.
76 status_t createReleaseFence(sp<Fence>& nativeFence);
77
Bo Liu7b8c1eb2019-01-08 20:17:55 -080078 // Returned pointers are owned by VulkanManager.
79 VkFunctorInitParams getVkFunctorInitParams() const;
80
Stan Iliev898123b2019-02-14 14:57:44 -050081 sk_sp<GrContext> createContext(const GrContextOptions& options);
Stan Iliev981afe72019-02-13 14:24:33 -050082
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050083private:
Derek Sollenbergera19b71a2019-02-15 16:36:30 -050084 friend class VulkanSurface;
Greg Daniel2ff202712018-06-14 11:50:10 -040085 // Sets up the VkInstance and VkDevice objects. Also fills out the passed in
86 // VkPhysicalDeviceFeatures struct.
Stan Iliev90276c82019-02-03 18:01:02 -050087 void setupDevice(GrVkExtensions&, VkPhysicalDeviceFeatures2&);
Greg Daniel26e0dca2018-09-18 10:33:19 -040088 bool setupDummyCommandBuffer();
89
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050090 // simple wrapper class that exists only to initialize a pointer to NULL
John Reck1bcacfd2017-11-03 10:12:19 -070091 template <typename FNPTR_TYPE>
92 class VkPtr {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050093 public:
94 VkPtr() : fPtr(NULL) {}
John Reck1bcacfd2017-11-03 10:12:19 -070095 VkPtr operator=(FNPTR_TYPE ptr) {
96 fPtr = ptr;
97 return *this;
98 }
Chih-Hung Hsiehd736d4b2018-12-20 13:55:20 -080099 // NOLINTNEXTLINE(google-explicit-constructor)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500100 operator FNPTR_TYPE() const { return fPtr; }
John Reck1bcacfd2017-11-03 10:12:19 -0700101
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500102 private:
103 FNPTR_TYPE fPtr;
104 };
105
106 // WSI interface functions
107 VkPtr<PFN_vkCreateAndroidSurfaceKHR> mCreateAndroidSurfaceKHR;
108 VkPtr<PFN_vkDestroySurfaceKHR> mDestroySurfaceKHR;
109 VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> mGetPhysicalDeviceSurfaceSupportKHR;
110 VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> mGetPhysicalDeviceSurfaceCapabilitiesKHR;
111 VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> mGetPhysicalDeviceSurfaceFormatsKHR;
112 VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> mGetPhysicalDeviceSurfacePresentModesKHR;
113
Greg Daniel2ff202712018-06-14 11:50:10 -0400114 // Instance Functions
Greg Daniela227dbb2018-08-20 09:19:48 -0400115 VkPtr<PFN_vkEnumerateInstanceVersion> mEnumerateInstanceVersion;
Greg Daniel2ff202712018-06-14 11:50:10 -0400116 VkPtr<PFN_vkEnumerateInstanceExtensionProperties> mEnumerateInstanceExtensionProperties;
117 VkPtr<PFN_vkCreateInstance> mCreateInstance;
118
119 VkPtr<PFN_vkDestroyInstance> mDestroyInstance;
120 VkPtr<PFN_vkEnumeratePhysicalDevices> mEnumeratePhysicalDevices;
Greg Daniel96259622018-10-01 14:42:56 -0400121 VkPtr<PFN_vkGetPhysicalDeviceProperties> mGetPhysicalDeviceProperties;
Greg Daniel2ff202712018-06-14 11:50:10 -0400122 VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties;
Greg Daniela227dbb2018-08-20 09:19:48 -0400123 VkPtr<PFN_vkGetPhysicalDeviceFeatures2> mGetPhysicalDeviceFeatures2;
Derek Sollenbergera19b71a2019-02-15 16:36:30 -0500124 VkPtr<PFN_vkGetPhysicalDeviceImageFormatProperties2> mGetPhysicalDeviceImageFormatProperties2;
Greg Daniel2ff202712018-06-14 11:50:10 -0400125 VkPtr<PFN_vkCreateDevice> mCreateDevice;
126 VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties;
127
128 // Device Functions
129 VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue;
130 VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle;
131 VkPtr<PFN_vkDestroyDevice> mDestroyDevice;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500132 VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool;
133 VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool;
134 VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers;
135 VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers;
136 VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer;
137 VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer;
138 VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer;
139 VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier;
140
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500141 VkPtr<PFN_vkQueueSubmit> mQueueSubmit;
142 VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500143
144 VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore;
145 VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400146 VkPtr<PFN_vkImportSemaphoreFdKHR> mImportSemaphoreFdKHR;
147 VkPtr<PFN_vkGetSemaphoreFdKHR> mGetSemaphoreFdKHR;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500148 VkPtr<PFN_vkCreateFence> mCreateFence;
149 VkPtr<PFN_vkDestroyFence> mDestroyFence;
150 VkPtr<PFN_vkWaitForFences> mWaitForFences;
151 VkPtr<PFN_vkResetFences> mResetFences;
152
Greg Daniel2ff202712018-06-14 11:50:10 -0400153 VkInstance mInstance = VK_NULL_HANDLE;
154 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
155 VkDevice mDevice = VK_NULL_HANDLE;
156
157 uint32_t mGraphicsQueueIndex;
158 VkQueue mGraphicsQueue = VK_NULL_HANDLE;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500159 uint32_t mPresentQueueIndex;
160 VkQueue mPresentQueue = VK_NULL_HANDLE;
161 VkCommandPool mCommandPool = VK_NULL_HANDLE;
Greg Danielcd558522016-11-17 13:31:40 -0500162
Greg Daniel26e0dca2018-09-18 10:33:19 -0400163 VkCommandBuffer mDummyCB = VK_NULL_HANDLE;
164
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800165 // Variables saved to populate VkFunctorInitParams.
Greg Danieleaf310e2019-01-28 16:10:32 -0500166 static const uint32_t mAPIVersion = VK_MAKE_VERSION(1, 1, 0);
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800167 std::vector<const char*> mInstanceExtensions;
168 std::vector<const char*> mDeviceExtensions;
169 VkPhysicalDeviceFeatures2 mPhysicalDeviceFeatures2{};
170
Greg Danielcd558522016-11-17 13:31:40 -0500171 enum class SwapBehavior {
172 Discard,
173 BufferAge,
174 };
175 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
Stan Iliev981afe72019-02-13 14:24:33 -0500176 GrVkExtensions mExtensions;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500177};
178
179} /* namespace renderthread */
180} /* namespace uirenderer */
181} /* namespace android */
182
183#endif /* VULKANMANAGER_H */