blob: 8594a1bd4339c12ef4ac8a8d84dcfc73ca17f457 [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:
Stan Iliev305e13a2018-11-13 11:14:48 -050041 VulkanSurface(ColorMode colorMode, ANativeWindow* window)
42 : mColorMode(colorMode), mNativeWindow(window) {}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050043
44 sk_sp<SkSurface> getBackBufferSurface() { return mBackbuffer; }
45
46private:
47 friend class VulkanManager;
48 struct BackbufferInfo {
John Reck1bcacfd2017-11-03 10:12:19 -070049 uint32_t mImageIndex; // image this is associated with
50 VkSemaphore mAcquireSemaphore; // we signal on this for acquisition of image
51 VkSemaphore mRenderSemaphore; // we wait on this for rendering to be done
52 VkCommandBuffer
53 mTransitionCmdBuffers[2]; // to transition layout between present and render
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050054 // We use these fences to make sure the above Command buffers have finished their work
55 // before attempting to reuse them or destroy them.
John Reck1bcacfd2017-11-03 10:12:19 -070056 VkFence mUsageFences[2];
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050057 };
58
Greg Danielcd558522016-11-17 13:31:40 -050059 struct ImageInfo {
60 VkImageLayout mImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
61 sk_sp<SkSurface> mSurface;
62 uint16_t mLastUsed = 0;
63 bool mInvalid = true;
64 };
65
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050066 sk_sp<SkSurface> mBackbuffer;
67
68 VkSurfaceKHR mVkSurface = VK_NULL_HANDLE;
69 VkSwapchainKHR mSwapchain = VK_NULL_HANDLE;
70
Greg Daniel74ea2012017-11-10 11:32:58 -050071 BackbufferInfo* mBackbuffers = nullptr;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050072 uint32_t mCurrentBackbufferIndex;
73
74 uint32_t mImageCount;
Greg Daniel74ea2012017-11-10 11:32:58 -050075 VkImage* mImages = nullptr;
Greg Danielcd558522016-11-17 13:31:40 -050076 ImageInfo* mImageInfos;
77 uint16_t mCurrentTime = 0;
Stan Iliev79351f32018-09-19 14:23:49 -040078 ColorMode mColorMode;
Stan Iliev305e13a2018-11-13 11:14:48 -050079 ANativeWindow* mNativeWindow;
80 int mWindowWidth = 0;
81 int mWindowHeight = 0;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050082};
83
84// This class contains the shared global Vulkan objects, such as VkInstance, VkDevice and VkQueue,
85// which are re-used by CanvasContext. This class is created once and should be used by all vulkan
86// windowing contexts. The VulkanManager must be initialized before use.
87class VulkanManager {
88public:
89 // Sets up the vulkan context that is shared amonst all clients of the VulkanManager. This must
90 // be call once before use of the VulkanManager. Multiple calls after the first will simiply
91 // return.
92 void initialize();
93
94 // Quick check to see if the VulkanManager has been initialized.
Greg Daniel2f9d8672018-06-22 10:44:26 -040095 bool hasVkContext() { return mDevice != VK_NULL_HANDLE; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050096
97 // Given a window this creates a new VkSurfaceKHR and VkSwapchain and stores them inside a new
98 // VulkanSurface object which is returned.
Stan Iliev79351f32018-09-19 14:23:49 -040099 VulkanSurface* createSurface(ANativeWindow* window, ColorMode colorMode);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500100
101 // Destroy the VulkanSurface and all associated vulkan objects.
102 void destroySurface(VulkanSurface* surface);
103
104 // Cleans up all the global state in the VulkanManger.
105 void destroy();
106
107 // No work is needed to make a VulkanSurface current, and all functions require that a
108 // VulkanSurface is passed into them so we just return true here.
109 bool isCurrent(VulkanSurface* surface) { return true; }
110
Greg Danielcd558522016-11-17 13:31:40 -0500111 int getAge(VulkanSurface* surface);
112
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500113 // Returns an SkSurface which wraps the next image returned from vkAcquireNextImageKHR. It also
114 // will transition the VkImage from a present layout to color attachment so that it can be used
115 // by the client for drawing.
Stan Iliev305e13a2018-11-13 11:14:48 -0500116 SkSurface* getBackbufferSurface(VulkanSurface** surface);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500117
118 // Presents the current VkImage.
119 void swapBuffers(VulkanSurface* surface);
120
Stan Iliev564ca3e2018-09-04 22:00:00 +0000121 // Inserts a wait on fence command into the Vulkan command buffer.
122 status_t fenceWait(sp<Fence>& fence);
123
124 // Creates a fence that is signaled, when all the pending Vulkan commands are flushed.
125 status_t createReleaseFence(sp<Fence>& nativeFence);
126
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500127private:
128 friend class RenderThread;
129
130 explicit VulkanManager(RenderThread& thread);
131 ~VulkanManager() { destroy(); }
132
Greg Daniel2ff202712018-06-14 11:50:10 -0400133 // Sets up the VkInstance and VkDevice objects. Also fills out the passed in
134 // VkPhysicalDeviceFeatures struct.
Greg Daniela227dbb2018-08-20 09:19:48 -0400135 bool setupDevice(GrVkExtensions&, VkPhysicalDeviceFeatures2&);
Greg Daniel2ff202712018-06-14 11:50:10 -0400136
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500137 void destroyBuffers(VulkanSurface* surface);
138
139 bool createSwapchain(VulkanSurface* surface);
140 void createBuffers(VulkanSurface* surface, VkFormat format, VkExtent2D extent);
141
142 VulkanSurface::BackbufferInfo* getAvailableBackbuffer(VulkanSurface* surface);
143
Greg Daniel26e0dca2018-09-18 10:33:19 -0400144 bool setupDummyCommandBuffer();
145
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500146 // simple wrapper class that exists only to initialize a pointer to NULL
John Reck1bcacfd2017-11-03 10:12:19 -0700147 template <typename FNPTR_TYPE>
148 class VkPtr {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500149 public:
150 VkPtr() : fPtr(NULL) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700151 VkPtr operator=(FNPTR_TYPE ptr) {
152 fPtr = ptr;
153 return *this;
154 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500155 operator FNPTR_TYPE() const { return fPtr; }
John Reck1bcacfd2017-11-03 10:12:19 -0700156
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500157 private:
158 FNPTR_TYPE fPtr;
159 };
160
161 // WSI interface functions
162 VkPtr<PFN_vkCreateAndroidSurfaceKHR> mCreateAndroidSurfaceKHR;
163 VkPtr<PFN_vkDestroySurfaceKHR> mDestroySurfaceKHR;
164 VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> mGetPhysicalDeviceSurfaceSupportKHR;
165 VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> mGetPhysicalDeviceSurfaceCapabilitiesKHR;
166 VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> mGetPhysicalDeviceSurfaceFormatsKHR;
167 VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> mGetPhysicalDeviceSurfacePresentModesKHR;
168
169 VkPtr<PFN_vkCreateSwapchainKHR> mCreateSwapchainKHR;
170 VkPtr<PFN_vkDestroySwapchainKHR> mDestroySwapchainKHR;
171 VkPtr<PFN_vkGetSwapchainImagesKHR> mGetSwapchainImagesKHR;
172 VkPtr<PFN_vkAcquireNextImageKHR> mAcquireNextImageKHR;
173 VkPtr<PFN_vkQueuePresentKHR> mQueuePresentKHR;
174 VkPtr<PFN_vkCreateSharedSwapchainsKHR> mCreateSharedSwapchainsKHR;
175
Greg Daniel2ff202712018-06-14 11:50:10 -0400176 // Instance Functions
Greg Daniela227dbb2018-08-20 09:19:48 -0400177 VkPtr<PFN_vkEnumerateInstanceVersion> mEnumerateInstanceVersion;
Greg Daniel2ff202712018-06-14 11:50:10 -0400178 VkPtr<PFN_vkEnumerateInstanceExtensionProperties> mEnumerateInstanceExtensionProperties;
179 VkPtr<PFN_vkCreateInstance> mCreateInstance;
180
181 VkPtr<PFN_vkDestroyInstance> mDestroyInstance;
182 VkPtr<PFN_vkEnumeratePhysicalDevices> mEnumeratePhysicalDevices;
Greg Daniel96259622018-10-01 14:42:56 -0400183 VkPtr<PFN_vkGetPhysicalDeviceProperties> mGetPhysicalDeviceProperties;
Greg Daniel2ff202712018-06-14 11:50:10 -0400184 VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties;
Greg Daniela227dbb2018-08-20 09:19:48 -0400185 VkPtr<PFN_vkGetPhysicalDeviceFeatures2> mGetPhysicalDeviceFeatures2;
Greg Daniel2ff202712018-06-14 11:50:10 -0400186 VkPtr<PFN_vkCreateDevice> mCreateDevice;
187 VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties;
188
189 // Device Functions
190 VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue;
191 VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle;
192 VkPtr<PFN_vkDestroyDevice> mDestroyDevice;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500193 VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool;
194 VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool;
195 VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers;
196 VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers;
197 VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer;
198 VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer;
199 VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer;
200 VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier;
201
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500202 VkPtr<PFN_vkQueueSubmit> mQueueSubmit;
203 VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500204
205 VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore;
206 VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400207 VkPtr<PFN_vkImportSemaphoreFdKHR> mImportSemaphoreFdKHR;
208 VkPtr<PFN_vkGetSemaphoreFdKHR> mGetSemaphoreFdKHR;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500209 VkPtr<PFN_vkCreateFence> mCreateFence;
210 VkPtr<PFN_vkDestroyFence> mDestroyFence;
211 VkPtr<PFN_vkWaitForFences> mWaitForFences;
212 VkPtr<PFN_vkResetFences> mResetFences;
213
214 RenderThread& mRenderThread;
215
Greg Daniel2ff202712018-06-14 11:50:10 -0400216 VkInstance mInstance = VK_NULL_HANDLE;
217 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
218 VkDevice mDevice = VK_NULL_HANDLE;
219
220 uint32_t mGraphicsQueueIndex;
221 VkQueue mGraphicsQueue = VK_NULL_HANDLE;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500222 uint32_t mPresentQueueIndex;
223 VkQueue mPresentQueue = VK_NULL_HANDLE;
224 VkCommandPool mCommandPool = VK_NULL_HANDLE;
Greg Danielcd558522016-11-17 13:31:40 -0500225
Greg Daniel26e0dca2018-09-18 10:33:19 -0400226 VkCommandBuffer mDummyCB = VK_NULL_HANDLE;
227
Greg Danielcd558522016-11-17 13:31:40 -0500228 enum class SwapBehavior {
229 Discard,
230 BufferAge,
231 };
232 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500233};
234
235} /* namespace renderthread */
236} /* namespace uirenderer */
237} /* namespace android */
238
239#endif /* VULKANMANAGER_H */