blob: 69ca23acb1fdc0c7b4ab663df5197691c8ead7bc [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
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500135private:
136 friend class RenderThread;
137
138 explicit VulkanManager(RenderThread& thread);
139 ~VulkanManager() { destroy(); }
140
Greg Daniel2ff202712018-06-14 11:50:10 -0400141 // Sets up the VkInstance and VkDevice objects. Also fills out the passed in
142 // VkPhysicalDeviceFeatures struct.
Greg Daniela227dbb2018-08-20 09:19:48 -0400143 bool setupDevice(GrVkExtensions&, VkPhysicalDeviceFeatures2&);
Greg Daniel2ff202712018-06-14 11:50:10 -0400144
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500145 void destroyBuffers(VulkanSurface* surface);
146
147 bool createSwapchain(VulkanSurface* surface);
148 void createBuffers(VulkanSurface* surface, VkFormat format, VkExtent2D extent);
149
150 VulkanSurface::BackbufferInfo* getAvailableBackbuffer(VulkanSurface* surface);
151
Greg Daniel26e0dca2018-09-18 10:33:19 -0400152 bool setupDummyCommandBuffer();
153
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500154 // simple wrapper class that exists only to initialize a pointer to NULL
John Reck1bcacfd2017-11-03 10:12:19 -0700155 template <typename FNPTR_TYPE>
156 class VkPtr {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500157 public:
158 VkPtr() : fPtr(NULL) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700159 VkPtr operator=(FNPTR_TYPE ptr) {
160 fPtr = ptr;
161 return *this;
162 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500163 operator FNPTR_TYPE() const { return fPtr; }
John Reck1bcacfd2017-11-03 10:12:19 -0700164
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500165 private:
166 FNPTR_TYPE fPtr;
167 };
168
169 // WSI interface functions
170 VkPtr<PFN_vkCreateAndroidSurfaceKHR> mCreateAndroidSurfaceKHR;
171 VkPtr<PFN_vkDestroySurfaceKHR> mDestroySurfaceKHR;
172 VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> mGetPhysicalDeviceSurfaceSupportKHR;
173 VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> mGetPhysicalDeviceSurfaceCapabilitiesKHR;
174 VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> mGetPhysicalDeviceSurfaceFormatsKHR;
175 VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> mGetPhysicalDeviceSurfacePresentModesKHR;
176
177 VkPtr<PFN_vkCreateSwapchainKHR> mCreateSwapchainKHR;
178 VkPtr<PFN_vkDestroySwapchainKHR> mDestroySwapchainKHR;
179 VkPtr<PFN_vkGetSwapchainImagesKHR> mGetSwapchainImagesKHR;
180 VkPtr<PFN_vkAcquireNextImageKHR> mAcquireNextImageKHR;
181 VkPtr<PFN_vkQueuePresentKHR> mQueuePresentKHR;
182 VkPtr<PFN_vkCreateSharedSwapchainsKHR> mCreateSharedSwapchainsKHR;
183
Greg Daniel2ff202712018-06-14 11:50:10 -0400184 // Instance Functions
Greg Daniela227dbb2018-08-20 09:19:48 -0400185 VkPtr<PFN_vkEnumerateInstanceVersion> mEnumerateInstanceVersion;
Greg Daniel2ff202712018-06-14 11:50:10 -0400186 VkPtr<PFN_vkEnumerateInstanceExtensionProperties> mEnumerateInstanceExtensionProperties;
187 VkPtr<PFN_vkCreateInstance> mCreateInstance;
188
189 VkPtr<PFN_vkDestroyInstance> mDestroyInstance;
190 VkPtr<PFN_vkEnumeratePhysicalDevices> mEnumeratePhysicalDevices;
Greg Daniel96259622018-10-01 14:42:56 -0400191 VkPtr<PFN_vkGetPhysicalDeviceProperties> mGetPhysicalDeviceProperties;
Greg Daniel2ff202712018-06-14 11:50:10 -0400192 VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties;
Greg Daniela227dbb2018-08-20 09:19:48 -0400193 VkPtr<PFN_vkGetPhysicalDeviceFeatures2> mGetPhysicalDeviceFeatures2;
Greg Daniel2ff202712018-06-14 11:50:10 -0400194 VkPtr<PFN_vkCreateDevice> mCreateDevice;
195 VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties;
196
197 // Device Functions
198 VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue;
199 VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle;
200 VkPtr<PFN_vkDestroyDevice> mDestroyDevice;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500201 VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool;
202 VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool;
203 VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers;
204 VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers;
205 VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer;
206 VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer;
207 VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer;
208 VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier;
209
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500210 VkPtr<PFN_vkQueueSubmit> mQueueSubmit;
211 VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500212
213 VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore;
214 VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400215 VkPtr<PFN_vkImportSemaphoreFdKHR> mImportSemaphoreFdKHR;
216 VkPtr<PFN_vkGetSemaphoreFdKHR> mGetSemaphoreFdKHR;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500217 VkPtr<PFN_vkCreateFence> mCreateFence;
218 VkPtr<PFN_vkDestroyFence> mDestroyFence;
219 VkPtr<PFN_vkWaitForFences> mWaitForFences;
220 VkPtr<PFN_vkResetFences> mResetFences;
221
222 RenderThread& mRenderThread;
223
Greg Daniel2ff202712018-06-14 11:50:10 -0400224 VkInstance mInstance = VK_NULL_HANDLE;
225 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
226 VkDevice mDevice = VK_NULL_HANDLE;
227
228 uint32_t mGraphicsQueueIndex;
229 VkQueue mGraphicsQueue = VK_NULL_HANDLE;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500230 uint32_t mPresentQueueIndex;
231 VkQueue mPresentQueue = VK_NULL_HANDLE;
232 VkCommandPool mCommandPool = VK_NULL_HANDLE;
Greg Danielcd558522016-11-17 13:31:40 -0500233
Greg Daniel26e0dca2018-09-18 10:33:19 -0400234 VkCommandBuffer mDummyCB = VK_NULL_HANDLE;
235
Greg Danielcd558522016-11-17 13:31:40 -0500236 enum class SwapBehavior {
237 Discard,
238 BufferAge,
239 };
240 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500241};
242
243} /* namespace renderthread */
244} /* namespace uirenderer */
245} /* namespace android */
246
247#endif /* VULKANMANAGER_H */