blob: b06eb82dac797166319f01e644837c8720b0f522 [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
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050025#include <SkSurface.h>
Stan Iliev564ca3e2018-09-04 22:00:00 +000026#include <ui/Fence.h>
27#include <utils/StrongPointer.h>
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050028#include <vk/GrVkBackendContext.h>
Stan Iliev79351f32018-09-19 14:23:49 -040029#include "IRenderPipeline.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050030
Greg Daniela227dbb2018-08-20 09:19:48 -040031class GrVkExtensions;
32
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050033namespace android {
34namespace uirenderer {
35namespace renderthread {
36
37class RenderThread;
38
39class VulkanSurface {
40public:
Peiyong Lin3bff1352018-12-11 07:56:07 -080041 VulkanSurface(ColorMode colorMode, ANativeWindow* window, sk_sp<SkColorSpace> colorSpace,
42 SkColorSpace::Gamut colorGamut, SkColorType colorType)
43 : mColorMode(colorMode), mNativeWindow(window), mColorSpace(colorSpace),
44 mColorGamut(colorGamut), mColorType(colorType) {}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050045
46 sk_sp<SkSurface> getBackBufferSurface() { return mBackbuffer; }
47
Greg Danielc4076782019-01-08 16:01:18 -050048 // The width and height are are the logical width and height for when submitting draws to the
49 // surface. In reality if the window is rotated the underlying VkImage may have the width and
50 // height swapped.
51 int windowWidth() const { return mWindowWidth; }
52 int windowHeight() const { return mWindowHeight; }
53
54 SkMatrix& preTransform() { return mPreTransform; }
55
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050056private:
57 friend class VulkanManager;
58 struct BackbufferInfo {
John Reck1bcacfd2017-11-03 10:12:19 -070059 uint32_t mImageIndex; // image this is associated with
60 VkSemaphore mAcquireSemaphore; // we signal on this for acquisition of image
61 VkSemaphore mRenderSemaphore; // we wait on this for rendering to be done
62 VkCommandBuffer
63 mTransitionCmdBuffers[2]; // to transition layout between present and render
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050064 // We use these fences to make sure the above Command buffers have finished their work
65 // before attempting to reuse them or destroy them.
John Reck1bcacfd2017-11-03 10:12:19 -070066 VkFence mUsageFences[2];
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050067 };
68
Greg Danielcd558522016-11-17 13:31:40 -050069 struct ImageInfo {
70 VkImageLayout mImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
71 sk_sp<SkSurface> mSurface;
72 uint16_t mLastUsed = 0;
73 bool mInvalid = true;
74 };
75
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050076 sk_sp<SkSurface> mBackbuffer;
77
78 VkSurfaceKHR mVkSurface = VK_NULL_HANDLE;
79 VkSwapchainKHR mSwapchain = VK_NULL_HANDLE;
80
Greg Daniel74ea2012017-11-10 11:32:58 -050081 BackbufferInfo* mBackbuffers = nullptr;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050082 uint32_t mCurrentBackbufferIndex;
83
84 uint32_t mImageCount;
Greg Daniel74ea2012017-11-10 11:32:58 -050085 VkImage* mImages = nullptr;
Greg Danielcd558522016-11-17 13:31:40 -050086 ImageInfo* mImageInfos;
87 uint16_t mCurrentTime = 0;
Stan Iliev79351f32018-09-19 14:23:49 -040088 ColorMode mColorMode;
Stan Iliev305e13a2018-11-13 11:14:48 -050089 ANativeWindow* mNativeWindow;
90 int mWindowWidth = 0;
91 int mWindowHeight = 0;
Stan Iliev987a80c2018-12-04 10:07:21 -050092 sk_sp<SkColorSpace> mColorSpace;
Peiyong Lin3bff1352018-12-11 07:56:07 -080093 SkColorSpace::Gamut mColorGamut;
94 SkColorType mColorType;
Greg Danielc4076782019-01-08 16:01:18 -050095 VkSurfaceTransformFlagBitsKHR mTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
96 SkMatrix mPreTransform;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050097};
98
99// This class contains the shared global Vulkan objects, such as VkInstance, VkDevice and VkQueue,
100// which are re-used by CanvasContext. This class is created once and should be used by all vulkan
101// windowing contexts. The VulkanManager must be initialized before use.
102class VulkanManager {
103public:
104 // Sets up the vulkan context that is shared amonst all clients of the VulkanManager. This must
105 // be call once before use of the VulkanManager. Multiple calls after the first will simiply
106 // return.
107 void initialize();
108
109 // Quick check to see if the VulkanManager has been initialized.
Greg Daniel2f9d8672018-06-22 10:44:26 -0400110 bool hasVkContext() { return mDevice != VK_NULL_HANDLE; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500111
112 // Given a window this creates a new VkSurfaceKHR and VkSwapchain and stores them inside a new
113 // VulkanSurface object which is returned.
Stan Iliev987a80c2018-12-04 10:07:21 -0500114 VulkanSurface* createSurface(ANativeWindow* window, ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800115 sk_sp<SkColorSpace> surfaceColorSpace,
116 SkColorSpace::Gamut surfaceColorGamut,
117 SkColorType surfaceColorType);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500118
119 // Destroy the VulkanSurface and all associated vulkan objects.
120 void destroySurface(VulkanSurface* surface);
121
122 // Cleans up all the global state in the VulkanManger.
123 void destroy();
124
125 // No work is needed to make a VulkanSurface current, and all functions require that a
126 // VulkanSurface is passed into them so we just return true here.
127 bool isCurrent(VulkanSurface* surface) { return true; }
128
Greg Danielcd558522016-11-17 13:31:40 -0500129 int getAge(VulkanSurface* surface);
130
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500131 // Returns an SkSurface which wraps the next image returned from vkAcquireNextImageKHR. It also
132 // will transition the VkImage from a present layout to color attachment so that it can be used
133 // by the client for drawing.
Stan Iliev305e13a2018-11-13 11:14:48 -0500134 SkSurface* getBackbufferSurface(VulkanSurface** surface);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500135
136 // Presents the current VkImage.
137 void swapBuffers(VulkanSurface* surface);
138
Stan Iliev564ca3e2018-09-04 22:00:00 +0000139 // Inserts a wait on fence command into the Vulkan command buffer.
140 status_t fenceWait(sp<Fence>& fence);
141
142 // Creates a fence that is signaled, when all the pending Vulkan commands are flushed.
143 status_t createReleaseFence(sp<Fence>& nativeFence);
144
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800145 // Returned pointers are owned by VulkanManager.
146 VkFunctorInitParams getVkFunctorInitParams() const;
147
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500148private:
149 friend class RenderThread;
150
151 explicit VulkanManager(RenderThread& thread);
152 ~VulkanManager() { destroy(); }
153
Greg Daniel2ff202712018-06-14 11:50:10 -0400154 // Sets up the VkInstance and VkDevice objects. Also fills out the passed in
155 // VkPhysicalDeviceFeatures struct.
Greg Daniela227dbb2018-08-20 09:19:48 -0400156 bool setupDevice(GrVkExtensions&, VkPhysicalDeviceFeatures2&);
Greg Daniel2ff202712018-06-14 11:50:10 -0400157
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500158 void destroyBuffers(VulkanSurface* surface);
159
160 bool createSwapchain(VulkanSurface* surface);
161 void createBuffers(VulkanSurface* surface, VkFormat format, VkExtent2D extent);
162
163 VulkanSurface::BackbufferInfo* getAvailableBackbuffer(VulkanSurface* surface);
164
Greg Daniel26e0dca2018-09-18 10:33:19 -0400165 bool setupDummyCommandBuffer();
166
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500167 // simple wrapper class that exists only to initialize a pointer to NULL
John Reck1bcacfd2017-11-03 10:12:19 -0700168 template <typename FNPTR_TYPE>
169 class VkPtr {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500170 public:
171 VkPtr() : fPtr(NULL) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700172 VkPtr operator=(FNPTR_TYPE ptr) {
173 fPtr = ptr;
174 return *this;
175 }
Chih-Hung Hsiehd736d4b2018-12-20 13:55:20 -0800176 // NOLINTNEXTLINE(google-explicit-constructor)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500177 operator FNPTR_TYPE() const { return fPtr; }
John Reck1bcacfd2017-11-03 10:12:19 -0700178
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500179 private:
180 FNPTR_TYPE fPtr;
181 };
182
183 // WSI interface functions
184 VkPtr<PFN_vkCreateAndroidSurfaceKHR> mCreateAndroidSurfaceKHR;
185 VkPtr<PFN_vkDestroySurfaceKHR> mDestroySurfaceKHR;
186 VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> mGetPhysicalDeviceSurfaceSupportKHR;
187 VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> mGetPhysicalDeviceSurfaceCapabilitiesKHR;
188 VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> mGetPhysicalDeviceSurfaceFormatsKHR;
189 VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> mGetPhysicalDeviceSurfacePresentModesKHR;
190
191 VkPtr<PFN_vkCreateSwapchainKHR> mCreateSwapchainKHR;
192 VkPtr<PFN_vkDestroySwapchainKHR> mDestroySwapchainKHR;
193 VkPtr<PFN_vkGetSwapchainImagesKHR> mGetSwapchainImagesKHR;
194 VkPtr<PFN_vkAcquireNextImageKHR> mAcquireNextImageKHR;
195 VkPtr<PFN_vkQueuePresentKHR> mQueuePresentKHR;
196 VkPtr<PFN_vkCreateSharedSwapchainsKHR> mCreateSharedSwapchainsKHR;
197
Greg Daniel2ff202712018-06-14 11:50:10 -0400198 // Instance Functions
Greg Daniela227dbb2018-08-20 09:19:48 -0400199 VkPtr<PFN_vkEnumerateInstanceVersion> mEnumerateInstanceVersion;
Greg Daniel2ff202712018-06-14 11:50:10 -0400200 VkPtr<PFN_vkEnumerateInstanceExtensionProperties> mEnumerateInstanceExtensionProperties;
201 VkPtr<PFN_vkCreateInstance> mCreateInstance;
202
203 VkPtr<PFN_vkDestroyInstance> mDestroyInstance;
204 VkPtr<PFN_vkEnumeratePhysicalDevices> mEnumeratePhysicalDevices;
Greg Daniel96259622018-10-01 14:42:56 -0400205 VkPtr<PFN_vkGetPhysicalDeviceProperties> mGetPhysicalDeviceProperties;
Greg Daniel2ff202712018-06-14 11:50:10 -0400206 VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties;
Greg Daniela227dbb2018-08-20 09:19:48 -0400207 VkPtr<PFN_vkGetPhysicalDeviceFeatures2> mGetPhysicalDeviceFeatures2;
Greg Daniel2ff202712018-06-14 11:50:10 -0400208 VkPtr<PFN_vkCreateDevice> mCreateDevice;
209 VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties;
210
211 // Device Functions
212 VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue;
213 VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle;
214 VkPtr<PFN_vkDestroyDevice> mDestroyDevice;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500215 VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool;
216 VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool;
217 VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers;
218 VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers;
219 VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer;
220 VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer;
221 VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer;
222 VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier;
223
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500224 VkPtr<PFN_vkQueueSubmit> mQueueSubmit;
225 VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500226
227 VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore;
228 VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400229 VkPtr<PFN_vkImportSemaphoreFdKHR> mImportSemaphoreFdKHR;
230 VkPtr<PFN_vkGetSemaphoreFdKHR> mGetSemaphoreFdKHR;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500231 VkPtr<PFN_vkCreateFence> mCreateFence;
232 VkPtr<PFN_vkDestroyFence> mDestroyFence;
233 VkPtr<PFN_vkWaitForFences> mWaitForFences;
234 VkPtr<PFN_vkResetFences> mResetFences;
235
236 RenderThread& mRenderThread;
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.
251 uint32_t mInstanceVersion = 0u;
252 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;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500261};
262
263} /* namespace renderthread */
264} /* namespace uirenderer */
265} /* namespace android */
266
267#endif /* VULKANMANAGER_H */