blob: 95c9630fb728b77188a421345aa8829e3c7d97f6 [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>
Stan Iliev79351f32018-09-19 14:23:49 -040031#include "IRenderPipeline.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050032
Greg Daniela227dbb2018-08-20 09:19:48 -040033class GrVkExtensions;
34
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050035namespace android {
36namespace uirenderer {
37namespace renderthread {
38
39class RenderThread;
40
41class VulkanSurface {
42public:
Peiyong Lin3bff1352018-12-11 07:56:07 -080043 VulkanSurface(ColorMode colorMode, ANativeWindow* window, sk_sp<SkColorSpace> colorSpace,
Stan Iliev981afe72019-02-13 14:24:33 -050044 SkColorType colorType, GrContext* grContext)
Peiyong Lin3bff1352018-12-11 07:56:07 -080045 : mColorMode(colorMode), mNativeWindow(window), mColorSpace(colorSpace),
Stan Iliev981afe72019-02-13 14:24:33 -050046 mColorType(colorType), mGrContext(grContext) {}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050047
48 sk_sp<SkSurface> getBackBufferSurface() { return mBackbuffer; }
49
Greg Danielc4076782019-01-08 16:01:18 -050050 // The width and height are are the logical width and height for when submitting draws to the
51 // surface. In reality if the window is rotated the underlying VkImage may have the width and
52 // height swapped.
53 int windowWidth() const { return mWindowWidth; }
54 int windowHeight() const { return mWindowHeight; }
55
56 SkMatrix& preTransform() { return mPreTransform; }
57
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050058private:
59 friend class VulkanManager;
60 struct BackbufferInfo {
John Reck1bcacfd2017-11-03 10:12:19 -070061 uint32_t mImageIndex; // image this is associated with
62 VkSemaphore mAcquireSemaphore; // we signal on this for acquisition of image
63 VkSemaphore mRenderSemaphore; // we wait on this for rendering to be done
64 VkCommandBuffer
65 mTransitionCmdBuffers[2]; // to transition layout between present and render
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050066 // We use these fences to make sure the above Command buffers have finished their work
67 // before attempting to reuse them or destroy them.
John Reck1bcacfd2017-11-03 10:12:19 -070068 VkFence mUsageFences[2];
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050069 };
70
Greg Danielcd558522016-11-17 13:31:40 -050071 struct ImageInfo {
72 VkImageLayout mImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
73 sk_sp<SkSurface> mSurface;
74 uint16_t mLastUsed = 0;
75 bool mInvalid = true;
76 };
77
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050078 sk_sp<SkSurface> mBackbuffer;
79
80 VkSurfaceKHR mVkSurface = VK_NULL_HANDLE;
81 VkSwapchainKHR mSwapchain = VK_NULL_HANDLE;
82
Greg Daniel74ea2012017-11-10 11:32:58 -050083 BackbufferInfo* mBackbuffers = nullptr;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050084 uint32_t mCurrentBackbufferIndex;
85
86 uint32_t mImageCount;
Greg Daniel74ea2012017-11-10 11:32:58 -050087 VkImage* mImages = nullptr;
Greg Danielcd558522016-11-17 13:31:40 -050088 ImageInfo* mImageInfos;
89 uint16_t mCurrentTime = 0;
Stan Iliev79351f32018-09-19 14:23:49 -040090 ColorMode mColorMode;
Stan Iliev305e13a2018-11-13 11:14:48 -050091 ANativeWindow* mNativeWindow;
92 int mWindowWidth = 0;
93 int mWindowHeight = 0;
Stan Iliev987a80c2018-12-04 10:07:21 -050094 sk_sp<SkColorSpace> mColorSpace;
Peiyong Lin3bff1352018-12-11 07:56:07 -080095 SkColorType mColorType;
Greg Danielc4076782019-01-08 16:01:18 -050096 VkSurfaceTransformFlagBitsKHR mTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
97 SkMatrix mPreTransform;
Stan Iliev981afe72019-02-13 14:24:33 -050098 GrContext* mGrContext;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050099};
100
101// This class contains the shared global Vulkan objects, such as VkInstance, VkDevice and VkQueue,
102// which are re-used by CanvasContext. This class is created once and should be used by all vulkan
103// windowing contexts. The VulkanManager must be initialized before use.
104class VulkanManager {
105public:
Stan Iliev981afe72019-02-13 14:24:33 -0500106 explicit VulkanManager() {}
107 ~VulkanManager() { destroy(); }
108
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500109 // Sets up the vulkan context that is shared amonst all clients of the VulkanManager. This must
110 // be call once before use of the VulkanManager. Multiple calls after the first will simiply
111 // return.
112 void initialize();
113
114 // Quick check to see if the VulkanManager has been initialized.
Greg Daniel2f9d8672018-06-22 10:44:26 -0400115 bool hasVkContext() { return mDevice != VK_NULL_HANDLE; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500116
117 // Given a window this creates a new VkSurfaceKHR and VkSwapchain and stores them inside a new
118 // VulkanSurface object which is returned.
Stan Iliev987a80c2018-12-04 10:07:21 -0500119 VulkanSurface* createSurface(ANativeWindow* window, ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800120 sk_sp<SkColorSpace> surfaceColorSpace,
Stan Iliev981afe72019-02-13 14:24:33 -0500121 SkColorType surfaceColorType,
122 GrContext* grContext);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500123
124 // Destroy the VulkanSurface and all associated vulkan objects.
125 void destroySurface(VulkanSurface* surface);
126
127 // Cleans up all the global state in the VulkanManger.
128 void destroy();
129
130 // No work is needed to make a VulkanSurface current, and all functions require that a
131 // VulkanSurface is passed into them so we just return true here.
132 bool isCurrent(VulkanSurface* surface) { return true; }
133
Greg Danielcd558522016-11-17 13:31:40 -0500134 int getAge(VulkanSurface* surface);
135
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500136 // Returns an SkSurface which wraps the next image returned from vkAcquireNextImageKHR. It also
137 // will transition the VkImage from a present layout to color attachment so that it can be used
138 // by the client for drawing.
Stan Iliev305e13a2018-11-13 11:14:48 -0500139 SkSurface* getBackbufferSurface(VulkanSurface** surface);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500140
141 // Presents the current VkImage.
142 void swapBuffers(VulkanSurface* surface);
143
Stan Iliev564ca3e2018-09-04 22:00:00 +0000144 // Inserts a wait on fence command into the Vulkan command buffer.
145 status_t fenceWait(sp<Fence>& fence);
146
147 // Creates a fence that is signaled, when all the pending Vulkan commands are flushed.
148 status_t createReleaseFence(sp<Fence>& nativeFence);
149
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800150 // Returned pointers are owned by VulkanManager.
151 VkFunctorInitParams getVkFunctorInitParams() const;
152
Stan Iliev898123b2019-02-14 14:57:44 -0500153 sk_sp<GrContext> createContext(const GrContextOptions& options);
Stan Iliev981afe72019-02-13 14:24:33 -0500154
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500155private:
Greg Daniel2ff202712018-06-14 11:50:10 -0400156 // Sets up the VkInstance and VkDevice objects. Also fills out the passed in
157 // VkPhysicalDeviceFeatures struct.
Stan Iliev90276c82019-02-03 18:01:02 -0500158 void setupDevice(GrVkExtensions&, VkPhysicalDeviceFeatures2&);
Greg Daniel2ff202712018-06-14 11:50:10 -0400159
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500160 void destroyBuffers(VulkanSurface* surface);
161
162 bool createSwapchain(VulkanSurface* surface);
163 void createBuffers(VulkanSurface* surface, VkFormat format, VkExtent2D extent);
164
165 VulkanSurface::BackbufferInfo* getAvailableBackbuffer(VulkanSurface* surface);
166
Greg Daniel26e0dca2018-09-18 10:33:19 -0400167 bool setupDummyCommandBuffer();
168
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500169 // simple wrapper class that exists only to initialize a pointer to NULL
John Reck1bcacfd2017-11-03 10:12:19 -0700170 template <typename FNPTR_TYPE>
171 class VkPtr {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500172 public:
173 VkPtr() : fPtr(NULL) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700174 VkPtr operator=(FNPTR_TYPE ptr) {
175 fPtr = ptr;
176 return *this;
177 }
Chih-Hung Hsiehd736d4b2018-12-20 13:55:20 -0800178 // NOLINTNEXTLINE(google-explicit-constructor)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500179 operator FNPTR_TYPE() const { return fPtr; }
John Reck1bcacfd2017-11-03 10:12:19 -0700180
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500181 private:
182 FNPTR_TYPE fPtr;
183 };
184
185 // WSI interface functions
186 VkPtr<PFN_vkCreateAndroidSurfaceKHR> mCreateAndroidSurfaceKHR;
187 VkPtr<PFN_vkDestroySurfaceKHR> mDestroySurfaceKHR;
188 VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> mGetPhysicalDeviceSurfaceSupportKHR;
189 VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> mGetPhysicalDeviceSurfaceCapabilitiesKHR;
190 VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> mGetPhysicalDeviceSurfaceFormatsKHR;
191 VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> mGetPhysicalDeviceSurfacePresentModesKHR;
192
193 VkPtr<PFN_vkCreateSwapchainKHR> mCreateSwapchainKHR;
194 VkPtr<PFN_vkDestroySwapchainKHR> mDestroySwapchainKHR;
195 VkPtr<PFN_vkGetSwapchainImagesKHR> mGetSwapchainImagesKHR;
196 VkPtr<PFN_vkAcquireNextImageKHR> mAcquireNextImageKHR;
197 VkPtr<PFN_vkQueuePresentKHR> mQueuePresentKHR;
198 VkPtr<PFN_vkCreateSharedSwapchainsKHR> mCreateSharedSwapchainsKHR;
199
Greg Daniel2ff202712018-06-14 11:50:10 -0400200 // Instance Functions
Greg Daniela227dbb2018-08-20 09:19:48 -0400201 VkPtr<PFN_vkEnumerateInstanceVersion> mEnumerateInstanceVersion;
Greg Daniel2ff202712018-06-14 11:50:10 -0400202 VkPtr<PFN_vkEnumerateInstanceExtensionProperties> mEnumerateInstanceExtensionProperties;
203 VkPtr<PFN_vkCreateInstance> mCreateInstance;
204
205 VkPtr<PFN_vkDestroyInstance> mDestroyInstance;
206 VkPtr<PFN_vkEnumeratePhysicalDevices> mEnumeratePhysicalDevices;
Greg Daniel96259622018-10-01 14:42:56 -0400207 VkPtr<PFN_vkGetPhysicalDeviceProperties> mGetPhysicalDeviceProperties;
Greg Daniel2ff202712018-06-14 11:50:10 -0400208 VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties;
Greg Daniela227dbb2018-08-20 09:19:48 -0400209 VkPtr<PFN_vkGetPhysicalDeviceFeatures2> mGetPhysicalDeviceFeatures2;
Greg Daniel2ff202712018-06-14 11:50:10 -0400210 VkPtr<PFN_vkCreateDevice> mCreateDevice;
211 VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties;
212
213 // Device Functions
214 VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue;
215 VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle;
216 VkPtr<PFN_vkDestroyDevice> mDestroyDevice;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500217 VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool;
218 VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool;
219 VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers;
220 VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers;
221 VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer;
222 VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer;
223 VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer;
224 VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier;
225
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500226 VkPtr<PFN_vkQueueSubmit> mQueueSubmit;
227 VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500228
229 VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore;
230 VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400231 VkPtr<PFN_vkImportSemaphoreFdKHR> mImportSemaphoreFdKHR;
232 VkPtr<PFN_vkGetSemaphoreFdKHR> mGetSemaphoreFdKHR;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500233 VkPtr<PFN_vkCreateFence> mCreateFence;
234 VkPtr<PFN_vkDestroyFence> mDestroyFence;
235 VkPtr<PFN_vkWaitForFences> mWaitForFences;
236 VkPtr<PFN_vkResetFences> mResetFences;
237
Greg Daniel2ff202712018-06-14 11:50:10 -0400238 VkInstance mInstance = VK_NULL_HANDLE;
239 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
240 VkDevice mDevice = VK_NULL_HANDLE;
241
242 uint32_t mGraphicsQueueIndex;
243 VkQueue mGraphicsQueue = VK_NULL_HANDLE;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500244 uint32_t mPresentQueueIndex;
245 VkQueue mPresentQueue = VK_NULL_HANDLE;
246 VkCommandPool mCommandPool = VK_NULL_HANDLE;
Greg Danielcd558522016-11-17 13:31:40 -0500247
Greg Daniel26e0dca2018-09-18 10:33:19 -0400248 VkCommandBuffer mDummyCB = VK_NULL_HANDLE;
249
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800250 // Variables saved to populate VkFunctorInitParams.
Greg Danieleaf310e2019-01-28 16:10:32 -0500251 static const uint32_t mAPIVersion = VK_MAKE_VERSION(1, 1, 0);
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800252 std::vector<const char*> mInstanceExtensions;
253 std::vector<const char*> mDeviceExtensions;
254 VkPhysicalDeviceFeatures2 mPhysicalDeviceFeatures2{};
255
Greg Danielcd558522016-11-17 13:31:40 -0500256 enum class SwapBehavior {
257 Discard,
258 BufferAge,
259 };
260 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
Stan Iliev981afe72019-02-13 14:24:33 -0500261 GrVkExtensions mExtensions;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500262};
263
264} /* namespace renderthread */
265} /* namespace uirenderer */
266} /* namespace android */
267
268#endif /* VULKANMANAGER_H */