blob: d67d2c81e95cca09fc11842dce2f59ce9d20dc6a [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 Iliev987a80c2018-12-04 10:07:21 -050041 VulkanSurface(ColorMode colorMode, ANativeWindow* window, sk_sp<SkColorSpace> colorSpace)
42 : mColorMode(colorMode), mNativeWindow(window), mColorSpace(colorSpace) {}
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;
Stan Iliev987a80c2018-12-04 10:07:21 -050082 sk_sp<SkColorSpace> mColorSpace;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050083};
84
85// This class contains the shared global Vulkan objects, such as VkInstance, VkDevice and VkQueue,
86// which are re-used by CanvasContext. This class is created once and should be used by all vulkan
87// windowing contexts. The VulkanManager must be initialized before use.
88class VulkanManager {
89public:
90 // Sets up the vulkan context that is shared amonst all clients of the VulkanManager. This must
91 // be call once before use of the VulkanManager. Multiple calls after the first will simiply
92 // return.
93 void initialize();
94
95 // Quick check to see if the VulkanManager has been initialized.
Greg Daniel2f9d8672018-06-22 10:44:26 -040096 bool hasVkContext() { return mDevice != VK_NULL_HANDLE; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050097
98 // Given a window this creates a new VkSurfaceKHR and VkSwapchain and stores them inside a new
99 // VulkanSurface object which is returned.
Stan Iliev987a80c2018-12-04 10:07:21 -0500100 VulkanSurface* createSurface(ANativeWindow* window, ColorMode colorMode,
101 sk_sp<SkColorSpace> surfaceColorSpace);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500102
103 // Destroy the VulkanSurface and all associated vulkan objects.
104 void destroySurface(VulkanSurface* surface);
105
106 // Cleans up all the global state in the VulkanManger.
107 void destroy();
108
109 // No work is needed to make a VulkanSurface current, and all functions require that a
110 // VulkanSurface is passed into them so we just return true here.
111 bool isCurrent(VulkanSurface* surface) { return true; }
112
Greg Danielcd558522016-11-17 13:31:40 -0500113 int getAge(VulkanSurface* surface);
114
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500115 // Returns an SkSurface which wraps the next image returned from vkAcquireNextImageKHR. It also
116 // will transition the VkImage from a present layout to color attachment so that it can be used
117 // by the client for drawing.
Stan Iliev305e13a2018-11-13 11:14:48 -0500118 SkSurface* getBackbufferSurface(VulkanSurface** surface);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500119
120 // Presents the current VkImage.
121 void swapBuffers(VulkanSurface* surface);
122
Stan Iliev564ca3e2018-09-04 22:00:00 +0000123 // Inserts a wait on fence command into the Vulkan command buffer.
124 status_t fenceWait(sp<Fence>& fence);
125
126 // Creates a fence that is signaled, when all the pending Vulkan commands are flushed.
127 status_t createReleaseFence(sp<Fence>& nativeFence);
128
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500129private:
130 friend class RenderThread;
131
132 explicit VulkanManager(RenderThread& thread);
133 ~VulkanManager() { destroy(); }
134
Greg Daniel2ff202712018-06-14 11:50:10 -0400135 // Sets up the VkInstance and VkDevice objects. Also fills out the passed in
136 // VkPhysicalDeviceFeatures struct.
Greg Daniela227dbb2018-08-20 09:19:48 -0400137 bool setupDevice(GrVkExtensions&, VkPhysicalDeviceFeatures2&);
Greg Daniel2ff202712018-06-14 11:50:10 -0400138
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500139 void destroyBuffers(VulkanSurface* surface);
140
141 bool createSwapchain(VulkanSurface* surface);
142 void createBuffers(VulkanSurface* surface, VkFormat format, VkExtent2D extent);
143
144 VulkanSurface::BackbufferInfo* getAvailableBackbuffer(VulkanSurface* surface);
145
Greg Daniel26e0dca2018-09-18 10:33:19 -0400146 bool setupDummyCommandBuffer();
147
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500148 // simple wrapper class that exists only to initialize a pointer to NULL
John Reck1bcacfd2017-11-03 10:12:19 -0700149 template <typename FNPTR_TYPE>
150 class VkPtr {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500151 public:
152 VkPtr() : fPtr(NULL) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700153 VkPtr operator=(FNPTR_TYPE ptr) {
154 fPtr = ptr;
155 return *this;
156 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500157 operator FNPTR_TYPE() const { return fPtr; }
John Reck1bcacfd2017-11-03 10:12:19 -0700158
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500159 private:
160 FNPTR_TYPE fPtr;
161 };
162
163 // WSI interface functions
164 VkPtr<PFN_vkCreateAndroidSurfaceKHR> mCreateAndroidSurfaceKHR;
165 VkPtr<PFN_vkDestroySurfaceKHR> mDestroySurfaceKHR;
166 VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> mGetPhysicalDeviceSurfaceSupportKHR;
167 VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> mGetPhysicalDeviceSurfaceCapabilitiesKHR;
168 VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> mGetPhysicalDeviceSurfaceFormatsKHR;
169 VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> mGetPhysicalDeviceSurfacePresentModesKHR;
170
171 VkPtr<PFN_vkCreateSwapchainKHR> mCreateSwapchainKHR;
172 VkPtr<PFN_vkDestroySwapchainKHR> mDestroySwapchainKHR;
173 VkPtr<PFN_vkGetSwapchainImagesKHR> mGetSwapchainImagesKHR;
174 VkPtr<PFN_vkAcquireNextImageKHR> mAcquireNextImageKHR;
175 VkPtr<PFN_vkQueuePresentKHR> mQueuePresentKHR;
176 VkPtr<PFN_vkCreateSharedSwapchainsKHR> mCreateSharedSwapchainsKHR;
177
Greg Daniel2ff202712018-06-14 11:50:10 -0400178 // Instance Functions
Greg Daniela227dbb2018-08-20 09:19:48 -0400179 VkPtr<PFN_vkEnumerateInstanceVersion> mEnumerateInstanceVersion;
Greg Daniel2ff202712018-06-14 11:50:10 -0400180 VkPtr<PFN_vkEnumerateInstanceExtensionProperties> mEnumerateInstanceExtensionProperties;
181 VkPtr<PFN_vkCreateInstance> mCreateInstance;
182
183 VkPtr<PFN_vkDestroyInstance> mDestroyInstance;
184 VkPtr<PFN_vkEnumeratePhysicalDevices> mEnumeratePhysicalDevices;
Greg Daniel96259622018-10-01 14:42:56 -0400185 VkPtr<PFN_vkGetPhysicalDeviceProperties> mGetPhysicalDeviceProperties;
Greg Daniel2ff202712018-06-14 11:50:10 -0400186 VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties;
Greg Daniela227dbb2018-08-20 09:19:48 -0400187 VkPtr<PFN_vkGetPhysicalDeviceFeatures2> mGetPhysicalDeviceFeatures2;
Greg Daniel2ff202712018-06-14 11:50:10 -0400188 VkPtr<PFN_vkCreateDevice> mCreateDevice;
189 VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties;
190
191 // Device Functions
192 VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue;
193 VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle;
194 VkPtr<PFN_vkDestroyDevice> mDestroyDevice;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500195 VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool;
196 VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool;
197 VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers;
198 VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers;
199 VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer;
200 VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer;
201 VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer;
202 VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier;
203
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500204 VkPtr<PFN_vkQueueSubmit> mQueueSubmit;
205 VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500206
207 VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore;
208 VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400209 VkPtr<PFN_vkImportSemaphoreFdKHR> mImportSemaphoreFdKHR;
210 VkPtr<PFN_vkGetSemaphoreFdKHR> mGetSemaphoreFdKHR;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500211 VkPtr<PFN_vkCreateFence> mCreateFence;
212 VkPtr<PFN_vkDestroyFence> mDestroyFence;
213 VkPtr<PFN_vkWaitForFences> mWaitForFences;
214 VkPtr<PFN_vkResetFences> mResetFences;
215
216 RenderThread& mRenderThread;
217
Greg Daniel2ff202712018-06-14 11:50:10 -0400218 VkInstance mInstance = VK_NULL_HANDLE;
219 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
220 VkDevice mDevice = VK_NULL_HANDLE;
221
222 uint32_t mGraphicsQueueIndex;
223 VkQueue mGraphicsQueue = VK_NULL_HANDLE;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500224 uint32_t mPresentQueueIndex;
225 VkQueue mPresentQueue = VK_NULL_HANDLE;
226 VkCommandPool mCommandPool = VK_NULL_HANDLE;
Greg Danielcd558522016-11-17 13:31:40 -0500227
Greg Daniel26e0dca2018-09-18 10:33:19 -0400228 VkCommandBuffer mDummyCB = VK_NULL_HANDLE;
229
Greg Danielcd558522016-11-17 13:31:40 -0500230 enum class SwapBehavior {
231 Discard,
232 BufferAge,
233 };
234 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500235};
236
237} /* namespace renderthread */
238} /* namespace uirenderer */
239} /* namespace android */
240
241#endif /* VULKANMANAGER_H */