blob: 105ee093a0573e6a1c30eb391759d7118539f011 [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
48private:
49 friend class VulkanManager;
50 struct BackbufferInfo {
John Reck1bcacfd2017-11-03 10:12:19 -070051 uint32_t mImageIndex; // image this is associated with
52 VkSemaphore mAcquireSemaphore; // we signal on this for acquisition of image
53 VkSemaphore mRenderSemaphore; // we wait on this for rendering to be done
54 VkCommandBuffer
55 mTransitionCmdBuffers[2]; // to transition layout between present and render
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050056 // We use these fences to make sure the above Command buffers have finished their work
57 // before attempting to reuse them or destroy them.
John Reck1bcacfd2017-11-03 10:12:19 -070058 VkFence mUsageFences[2];
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050059 };
60
Greg Danielcd558522016-11-17 13:31:40 -050061 struct ImageInfo {
62 VkImageLayout mImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
63 sk_sp<SkSurface> mSurface;
64 uint16_t mLastUsed = 0;
65 bool mInvalid = true;
66 };
67
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050068 sk_sp<SkSurface> mBackbuffer;
69
70 VkSurfaceKHR mVkSurface = VK_NULL_HANDLE;
71 VkSwapchainKHR mSwapchain = VK_NULL_HANDLE;
72
Greg Daniel74ea2012017-11-10 11:32:58 -050073 BackbufferInfo* mBackbuffers = nullptr;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050074 uint32_t mCurrentBackbufferIndex;
75
76 uint32_t mImageCount;
Greg Daniel74ea2012017-11-10 11:32:58 -050077 VkImage* mImages = nullptr;
Greg Danielcd558522016-11-17 13:31:40 -050078 ImageInfo* mImageInfos;
79 uint16_t mCurrentTime = 0;
Stan Iliev79351f32018-09-19 14:23:49 -040080 ColorMode mColorMode;
Stan Iliev305e13a2018-11-13 11:14:48 -050081 ANativeWindow* mNativeWindow;
82 int mWindowWidth = 0;
83 int mWindowHeight = 0;
Stan Iliev987a80c2018-12-04 10:07:21 -050084 sk_sp<SkColorSpace> mColorSpace;
Peiyong Lin3bff1352018-12-11 07:56:07 -080085 SkColorSpace::Gamut mColorGamut;
86 SkColorType mColorType;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050087};
88
89// This class contains the shared global Vulkan objects, such as VkInstance, VkDevice and VkQueue,
90// which are re-used by CanvasContext. This class is created once and should be used by all vulkan
91// windowing contexts. The VulkanManager must be initialized before use.
92class VulkanManager {
93public:
94 // Sets up the vulkan context that is shared amonst all clients of the VulkanManager. This must
95 // be call once before use of the VulkanManager. Multiple calls after the first will simiply
96 // return.
97 void initialize();
98
99 // Quick check to see if the VulkanManager has been initialized.
Greg Daniel2f9d8672018-06-22 10:44:26 -0400100 bool hasVkContext() { return mDevice != VK_NULL_HANDLE; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500101
102 // Given a window this creates a new VkSurfaceKHR and VkSwapchain and stores them inside a new
103 // VulkanSurface object which is returned.
Stan Iliev987a80c2018-12-04 10:07:21 -0500104 VulkanSurface* createSurface(ANativeWindow* window, ColorMode colorMode,
Peiyong Lin3bff1352018-12-11 07:56:07 -0800105 sk_sp<SkColorSpace> surfaceColorSpace,
106 SkColorSpace::Gamut surfaceColorGamut,
107 SkColorType surfaceColorType);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500108
109 // Destroy the VulkanSurface and all associated vulkan objects.
110 void destroySurface(VulkanSurface* surface);
111
112 // Cleans up all the global state in the VulkanManger.
113 void destroy();
114
115 // No work is needed to make a VulkanSurface current, and all functions require that a
116 // VulkanSurface is passed into them so we just return true here.
117 bool isCurrent(VulkanSurface* surface) { return true; }
118
Greg Danielcd558522016-11-17 13:31:40 -0500119 int getAge(VulkanSurface* surface);
120
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500121 // Returns an SkSurface which wraps the next image returned from vkAcquireNextImageKHR. It also
122 // will transition the VkImage from a present layout to color attachment so that it can be used
123 // by the client for drawing.
Stan Iliev305e13a2018-11-13 11:14:48 -0500124 SkSurface* getBackbufferSurface(VulkanSurface** surface);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500125
126 // Presents the current VkImage.
127 void swapBuffers(VulkanSurface* surface);
128
Stan Iliev564ca3e2018-09-04 22:00:00 +0000129 // Inserts a wait on fence command into the Vulkan command buffer.
130 status_t fenceWait(sp<Fence>& fence);
131
132 // Creates a fence that is signaled, when all the pending Vulkan commands are flushed.
133 status_t createReleaseFence(sp<Fence>& nativeFence);
134
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800135 // Returned pointers are owned by VulkanManager.
136 VkFunctorInitParams getVkFunctorInitParams() const;
137
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500138private:
139 friend class RenderThread;
140
141 explicit VulkanManager(RenderThread& thread);
142 ~VulkanManager() { destroy(); }
143
Greg Daniel2ff202712018-06-14 11:50:10 -0400144 // Sets up the VkInstance and VkDevice objects. Also fills out the passed in
145 // VkPhysicalDeviceFeatures struct.
Greg Daniela227dbb2018-08-20 09:19:48 -0400146 bool setupDevice(GrVkExtensions&, VkPhysicalDeviceFeatures2&);
Greg Daniel2ff202712018-06-14 11:50:10 -0400147
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500148 void destroyBuffers(VulkanSurface* surface);
149
150 bool createSwapchain(VulkanSurface* surface);
151 void createBuffers(VulkanSurface* surface, VkFormat format, VkExtent2D extent);
152
153 VulkanSurface::BackbufferInfo* getAvailableBackbuffer(VulkanSurface* surface);
154
Greg Daniel26e0dca2018-09-18 10:33:19 -0400155 bool setupDummyCommandBuffer();
156
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500157 // simple wrapper class that exists only to initialize a pointer to NULL
John Reck1bcacfd2017-11-03 10:12:19 -0700158 template <typename FNPTR_TYPE>
159 class VkPtr {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500160 public:
161 VkPtr() : fPtr(NULL) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700162 VkPtr operator=(FNPTR_TYPE ptr) {
163 fPtr = ptr;
164 return *this;
165 }
Chih-Hung Hsiehd736d4b2018-12-20 13:55:20 -0800166 // NOLINTNEXTLINE(google-explicit-constructor)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500167 operator FNPTR_TYPE() const { return fPtr; }
John Reck1bcacfd2017-11-03 10:12:19 -0700168
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500169 private:
170 FNPTR_TYPE fPtr;
171 };
172
173 // WSI interface functions
174 VkPtr<PFN_vkCreateAndroidSurfaceKHR> mCreateAndroidSurfaceKHR;
175 VkPtr<PFN_vkDestroySurfaceKHR> mDestroySurfaceKHR;
176 VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> mGetPhysicalDeviceSurfaceSupportKHR;
177 VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> mGetPhysicalDeviceSurfaceCapabilitiesKHR;
178 VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> mGetPhysicalDeviceSurfaceFormatsKHR;
179 VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> mGetPhysicalDeviceSurfacePresentModesKHR;
180
181 VkPtr<PFN_vkCreateSwapchainKHR> mCreateSwapchainKHR;
182 VkPtr<PFN_vkDestroySwapchainKHR> mDestroySwapchainKHR;
183 VkPtr<PFN_vkGetSwapchainImagesKHR> mGetSwapchainImagesKHR;
184 VkPtr<PFN_vkAcquireNextImageKHR> mAcquireNextImageKHR;
185 VkPtr<PFN_vkQueuePresentKHR> mQueuePresentKHR;
186 VkPtr<PFN_vkCreateSharedSwapchainsKHR> mCreateSharedSwapchainsKHR;
187
Greg Daniel2ff202712018-06-14 11:50:10 -0400188 // Instance Functions
Greg Daniela227dbb2018-08-20 09:19:48 -0400189 VkPtr<PFN_vkEnumerateInstanceVersion> mEnumerateInstanceVersion;
Greg Daniel2ff202712018-06-14 11:50:10 -0400190 VkPtr<PFN_vkEnumerateInstanceExtensionProperties> mEnumerateInstanceExtensionProperties;
191 VkPtr<PFN_vkCreateInstance> mCreateInstance;
192
193 VkPtr<PFN_vkDestroyInstance> mDestroyInstance;
194 VkPtr<PFN_vkEnumeratePhysicalDevices> mEnumeratePhysicalDevices;
Greg Daniel96259622018-10-01 14:42:56 -0400195 VkPtr<PFN_vkGetPhysicalDeviceProperties> mGetPhysicalDeviceProperties;
Greg Daniel2ff202712018-06-14 11:50:10 -0400196 VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties;
Greg Daniela227dbb2018-08-20 09:19:48 -0400197 VkPtr<PFN_vkGetPhysicalDeviceFeatures2> mGetPhysicalDeviceFeatures2;
Greg Daniel2ff202712018-06-14 11:50:10 -0400198 VkPtr<PFN_vkCreateDevice> mCreateDevice;
199 VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties;
200
201 // Device Functions
202 VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue;
203 VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle;
204 VkPtr<PFN_vkDestroyDevice> mDestroyDevice;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500205 VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool;
206 VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool;
207 VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers;
208 VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers;
209 VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer;
210 VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer;
211 VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer;
212 VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier;
213
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500214 VkPtr<PFN_vkQueueSubmit> mQueueSubmit;
215 VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500216
217 VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore;
218 VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400219 VkPtr<PFN_vkImportSemaphoreFdKHR> mImportSemaphoreFdKHR;
220 VkPtr<PFN_vkGetSemaphoreFdKHR> mGetSemaphoreFdKHR;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500221 VkPtr<PFN_vkCreateFence> mCreateFence;
222 VkPtr<PFN_vkDestroyFence> mDestroyFence;
223 VkPtr<PFN_vkWaitForFences> mWaitForFences;
224 VkPtr<PFN_vkResetFences> mResetFences;
225
226 RenderThread& mRenderThread;
227
Greg Daniel2ff202712018-06-14 11:50:10 -0400228 VkInstance mInstance = VK_NULL_HANDLE;
229 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
230 VkDevice mDevice = VK_NULL_HANDLE;
231
232 uint32_t mGraphicsQueueIndex;
233 VkQueue mGraphicsQueue = VK_NULL_HANDLE;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500234 uint32_t mPresentQueueIndex;
235 VkQueue mPresentQueue = VK_NULL_HANDLE;
236 VkCommandPool mCommandPool = VK_NULL_HANDLE;
Greg Danielcd558522016-11-17 13:31:40 -0500237
Greg Daniel26e0dca2018-09-18 10:33:19 -0400238 VkCommandBuffer mDummyCB = VK_NULL_HANDLE;
239
Bo Liu7b8c1eb2019-01-08 20:17:55 -0800240 // Variables saved to populate VkFunctorInitParams.
241 uint32_t mInstanceVersion = 0u;
242 std::vector<const char*> mInstanceExtensions;
243 std::vector<const char*> mDeviceExtensions;
244 VkPhysicalDeviceFeatures2 mPhysicalDeviceFeatures2{};
245
Greg Danielcd558522016-11-17 13:31:40 -0500246 enum class SwapBehavior {
247 Discard,
248 BufferAge,
249 };
250 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500251};
252
253} /* namespace renderthread */
254} /* namespace uirenderer */
255} /* namespace android */
256
257#endif /* VULKANMANAGER_H */