blob: abe78efc917408c81d71484d2cdeb440e572ab7b [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,
Brian Osmane0cf5972019-01-23 10:41:20 -050042 SkColorType colorType)
Peiyong Lin3bff1352018-12-11 07:56:07 -080043 : mColorMode(colorMode), mNativeWindow(window), mColorSpace(colorSpace),
Brian Osmane0cf5972019-01-23 10:41:20 -050044 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 SkColorType mColorType;
Greg Danielc4076782019-01-08 16:01:18 -050094 VkSurfaceTransformFlagBitsKHR mTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
95 SkMatrix mPreTransform;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050096};
97
98// This class contains the shared global Vulkan objects, such as VkInstance, VkDevice and VkQueue,
99// which are re-used by CanvasContext. This class is created once and should be used by all vulkan
100// windowing contexts. The VulkanManager must be initialized before use.
101class VulkanManager {
102public:
103 // Sets up the vulkan context that is shared amonst all clients of the VulkanManager. This must
104 // be call once before use of the VulkanManager. Multiple calls after the first will simiply
105 // return.
106 void initialize();
107
108 // Quick check to see if the VulkanManager has been initialized.
Greg Daniel2f9d8672018-06-22 10:44:26 -0400109 bool hasVkContext() { return mDevice != VK_NULL_HANDLE; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500110
111 // Given a window this creates a new VkSurfaceKHR and VkSwapchain and stores them inside a new
112 // VulkanSurface object which is returned.
Stan Iliev987a80c2018-12-04 10:07:21 -0500113 VulkanSurface* createSurface(ANativeWindow* window, ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800114 sk_sp<SkColorSpace> surfaceColorSpace,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800115 SkColorType surfaceColorType);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500116
117 // Destroy the VulkanSurface and all associated vulkan objects.
118 void destroySurface(VulkanSurface* surface);
119
120 // Cleans up all the global state in the VulkanManger.
121 void destroy();
122
123 // No work is needed to make a VulkanSurface current, and all functions require that a
124 // VulkanSurface is passed into them so we just return true here.
125 bool isCurrent(VulkanSurface* surface) { return true; }
126
Greg Danielcd558522016-11-17 13:31:40 -0500127 int getAge(VulkanSurface* surface);
128
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500129 // Returns an SkSurface which wraps the next image returned from vkAcquireNextImageKHR. It also
130 // will transition the VkImage from a present layout to color attachment so that it can be used
131 // by the client for drawing.
Stan Iliev305e13a2018-11-13 11:14:48 -0500132 SkSurface* getBackbufferSurface(VulkanSurface** surface);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500133
134 // Presents the current VkImage.
135 void swapBuffers(VulkanSurface* surface);
136
Stan Iliev564ca3e2018-09-04 22:00:00 +0000137 // Inserts a wait on fence command into the Vulkan command buffer.
138 status_t fenceWait(sp<Fence>& fence);
139
140 // Creates a fence that is signaled, when all the pending Vulkan commands are flushed.
141 status_t createReleaseFence(sp<Fence>& nativeFence);
142
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800143 // Returned pointers are owned by VulkanManager.
144 VkFunctorInitParams getVkFunctorInitParams() const;
145
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500146private:
147 friend class RenderThread;
148
149 explicit VulkanManager(RenderThread& thread);
150 ~VulkanManager() { destroy(); }
151
Greg Daniel2ff202712018-06-14 11:50:10 -0400152 // Sets up the VkInstance and VkDevice objects. Also fills out the passed in
153 // VkPhysicalDeviceFeatures struct.
Greg Daniela227dbb2018-08-20 09:19:48 -0400154 bool setupDevice(GrVkExtensions&, VkPhysicalDeviceFeatures2&);
Greg Daniel2ff202712018-06-14 11:50:10 -0400155
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500156 void destroyBuffers(VulkanSurface* surface);
157
158 bool createSwapchain(VulkanSurface* surface);
159 void createBuffers(VulkanSurface* surface, VkFormat format, VkExtent2D extent);
160
161 VulkanSurface::BackbufferInfo* getAvailableBackbuffer(VulkanSurface* surface);
162
Greg Daniel26e0dca2018-09-18 10:33:19 -0400163 bool setupDummyCommandBuffer();
164
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500165 // simple wrapper class that exists only to initialize a pointer to NULL
John Reck1bcacfd2017-11-03 10:12:19 -0700166 template <typename FNPTR_TYPE>
167 class VkPtr {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500168 public:
169 VkPtr() : fPtr(NULL) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700170 VkPtr operator=(FNPTR_TYPE ptr) {
171 fPtr = ptr;
172 return *this;
173 }
Chih-Hung Hsiehd736d4b2018-12-20 13:55:20 -0800174 // NOLINTNEXTLINE(google-explicit-constructor)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500175 operator FNPTR_TYPE() const { return fPtr; }
John Reck1bcacfd2017-11-03 10:12:19 -0700176
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500177 private:
178 FNPTR_TYPE fPtr;
179 };
180
181 // WSI interface functions
182 VkPtr<PFN_vkCreateAndroidSurfaceKHR> mCreateAndroidSurfaceKHR;
183 VkPtr<PFN_vkDestroySurfaceKHR> mDestroySurfaceKHR;
184 VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> mGetPhysicalDeviceSurfaceSupportKHR;
185 VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> mGetPhysicalDeviceSurfaceCapabilitiesKHR;
186 VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> mGetPhysicalDeviceSurfaceFormatsKHR;
187 VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> mGetPhysicalDeviceSurfacePresentModesKHR;
188
189 VkPtr<PFN_vkCreateSwapchainKHR> mCreateSwapchainKHR;
190 VkPtr<PFN_vkDestroySwapchainKHR> mDestroySwapchainKHR;
191 VkPtr<PFN_vkGetSwapchainImagesKHR> mGetSwapchainImagesKHR;
192 VkPtr<PFN_vkAcquireNextImageKHR> mAcquireNextImageKHR;
193 VkPtr<PFN_vkQueuePresentKHR> mQueuePresentKHR;
194 VkPtr<PFN_vkCreateSharedSwapchainsKHR> mCreateSharedSwapchainsKHR;
195
Greg Daniel2ff202712018-06-14 11:50:10 -0400196 // Instance Functions
Greg Daniela227dbb2018-08-20 09:19:48 -0400197 VkPtr<PFN_vkEnumerateInstanceVersion> mEnumerateInstanceVersion;
Greg Daniel2ff202712018-06-14 11:50:10 -0400198 VkPtr<PFN_vkEnumerateInstanceExtensionProperties> mEnumerateInstanceExtensionProperties;
199 VkPtr<PFN_vkCreateInstance> mCreateInstance;
200
201 VkPtr<PFN_vkDestroyInstance> mDestroyInstance;
202 VkPtr<PFN_vkEnumeratePhysicalDevices> mEnumeratePhysicalDevices;
Greg Daniel96259622018-10-01 14:42:56 -0400203 VkPtr<PFN_vkGetPhysicalDeviceProperties> mGetPhysicalDeviceProperties;
Greg Daniel2ff202712018-06-14 11:50:10 -0400204 VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties;
Greg Daniela227dbb2018-08-20 09:19:48 -0400205 VkPtr<PFN_vkGetPhysicalDeviceFeatures2> mGetPhysicalDeviceFeatures2;
Greg Daniel2ff202712018-06-14 11:50:10 -0400206 VkPtr<PFN_vkCreateDevice> mCreateDevice;
207 VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties;
208
209 // Device Functions
210 VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue;
211 VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle;
212 VkPtr<PFN_vkDestroyDevice> mDestroyDevice;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500213 VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool;
214 VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool;
215 VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers;
216 VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers;
217 VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer;
218 VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer;
219 VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer;
220 VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier;
221
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500222 VkPtr<PFN_vkQueueSubmit> mQueueSubmit;
223 VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500224
225 VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore;
226 VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400227 VkPtr<PFN_vkImportSemaphoreFdKHR> mImportSemaphoreFdKHR;
228 VkPtr<PFN_vkGetSemaphoreFdKHR> mGetSemaphoreFdKHR;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500229 VkPtr<PFN_vkCreateFence> mCreateFence;
230 VkPtr<PFN_vkDestroyFence> mDestroyFence;
231 VkPtr<PFN_vkWaitForFences> mWaitForFences;
232 VkPtr<PFN_vkResetFences> mResetFences;
233
234 RenderThread& mRenderThread;
235
Greg Daniel2ff202712018-06-14 11:50:10 -0400236 VkInstance mInstance = VK_NULL_HANDLE;
237 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
238 VkDevice mDevice = VK_NULL_HANDLE;
239
240 uint32_t mGraphicsQueueIndex;
241 VkQueue mGraphicsQueue = VK_NULL_HANDLE;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500242 uint32_t mPresentQueueIndex;
243 VkQueue mPresentQueue = VK_NULL_HANDLE;
244 VkCommandPool mCommandPool = VK_NULL_HANDLE;
Greg Danielcd558522016-11-17 13:31:40 -0500245
Greg Daniel26e0dca2018-09-18 10:33:19 -0400246 VkCommandBuffer mDummyCB = VK_NULL_HANDLE;
247
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800248 // Variables saved to populate VkFunctorInitParams.
249 uint32_t mInstanceVersion = 0u;
250 std::vector<const char*> mInstanceExtensions;
251 std::vector<const char*> mDeviceExtensions;
252 VkPhysicalDeviceFeatures2 mPhysicalDeviceFeatures2{};
253
Greg Danielcd558522016-11-17 13:31:40 -0500254 enum class SwapBehavior {
255 Discard,
256 BufferAge,
257 };
258 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500259};
260
261} /* namespace renderthread */
262} /* namespace uirenderer */
263} /* namespace android */
264
265#endif /* VULKANMANAGER_H */