blob: 6702649402e6a3cddc1bc0735cdc07711dffb400 [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 Iliev79351f32018-09-19 14:23:49 -040041 VulkanSurface(ColorMode colorMode) : mColorMode(colorMode) {}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050042
43 sk_sp<SkSurface> getBackBufferSurface() { return mBackbuffer; }
44
45private:
46 friend class VulkanManager;
47 struct BackbufferInfo {
John Reck1bcacfd2017-11-03 10:12:19 -070048 uint32_t mImageIndex; // image this is associated with
49 VkSemaphore mAcquireSemaphore; // we signal on this for acquisition of image
50 VkSemaphore mRenderSemaphore; // we wait on this for rendering to be done
51 VkCommandBuffer
52 mTransitionCmdBuffers[2]; // to transition layout between present and render
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050053 // We use these fences to make sure the above Command buffers have finished their work
54 // before attempting to reuse them or destroy them.
John Reck1bcacfd2017-11-03 10:12:19 -070055 VkFence mUsageFences[2];
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050056 };
57
Greg Danielcd558522016-11-17 13:31:40 -050058 struct ImageInfo {
59 VkImageLayout mImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
60 sk_sp<SkSurface> mSurface;
61 uint16_t mLastUsed = 0;
62 bool mInvalid = true;
63 };
64
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050065 sk_sp<SkSurface> mBackbuffer;
66
67 VkSurfaceKHR mVkSurface = VK_NULL_HANDLE;
68 VkSwapchainKHR mSwapchain = VK_NULL_HANDLE;
69
Greg Daniel74ea2012017-11-10 11:32:58 -050070 BackbufferInfo* mBackbuffers = nullptr;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050071 uint32_t mCurrentBackbufferIndex;
72
73 uint32_t mImageCount;
Greg Daniel74ea2012017-11-10 11:32:58 -050074 VkImage* mImages = nullptr;
Greg Danielcd558522016-11-17 13:31:40 -050075 ImageInfo* mImageInfos;
76 uint16_t mCurrentTime = 0;
Stan Iliev79351f32018-09-19 14:23:49 -040077 ColorMode mColorMode;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050078};
79
80// This class contains the shared global Vulkan objects, such as VkInstance, VkDevice and VkQueue,
81// which are re-used by CanvasContext. This class is created once and should be used by all vulkan
82// windowing contexts. The VulkanManager must be initialized before use.
83class VulkanManager {
84public:
85 // Sets up the vulkan context that is shared amonst all clients of the VulkanManager. This must
86 // be call once before use of the VulkanManager. Multiple calls after the first will simiply
87 // return.
88 void initialize();
89
90 // Quick check to see if the VulkanManager has been initialized.
Greg Daniel2f9d8672018-06-22 10:44:26 -040091 bool hasVkContext() { return mDevice != VK_NULL_HANDLE; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050092
93 // Given a window this creates a new VkSurfaceKHR and VkSwapchain and stores them inside a new
94 // VulkanSurface object which is returned.
Stan Iliev79351f32018-09-19 14:23:49 -040095 VulkanSurface* createSurface(ANativeWindow* window, ColorMode colorMode);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050096
97 // Destroy the VulkanSurface and all associated vulkan objects.
98 void destroySurface(VulkanSurface* surface);
99
100 // Cleans up all the global state in the VulkanManger.
101 void destroy();
102
103 // No work is needed to make a VulkanSurface current, and all functions require that a
104 // VulkanSurface is passed into them so we just return true here.
105 bool isCurrent(VulkanSurface* surface) { return true; }
106
Greg Danielcd558522016-11-17 13:31:40 -0500107 int getAge(VulkanSurface* surface);
108
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500109 // Returns an SkSurface which wraps the next image returned from vkAcquireNextImageKHR. It also
110 // will transition the VkImage from a present layout to color attachment so that it can be used
111 // by the client for drawing.
112 SkSurface* getBackbufferSurface(VulkanSurface* surface);
113
114 // Presents the current VkImage.
115 void swapBuffers(VulkanSurface* surface);
116
Stan Iliev564ca3e2018-09-04 22:00:00 +0000117 // Inserts a wait on fence command into the Vulkan command buffer.
118 status_t fenceWait(sp<Fence>& fence);
119
120 // Creates a fence that is signaled, when all the pending Vulkan commands are flushed.
121 status_t createReleaseFence(sp<Fence>& nativeFence);
122
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500123private:
124 friend class RenderThread;
125
126 explicit VulkanManager(RenderThread& thread);
127 ~VulkanManager() { destroy(); }
128
Greg Daniel2ff202712018-06-14 11:50:10 -0400129 // Sets up the VkInstance and VkDevice objects. Also fills out the passed in
130 // VkPhysicalDeviceFeatures struct.
Greg Daniela227dbb2018-08-20 09:19:48 -0400131 bool setupDevice(GrVkExtensions&, VkPhysicalDeviceFeatures2&);
Greg Daniel2ff202712018-06-14 11:50:10 -0400132
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500133 void destroyBuffers(VulkanSurface* surface);
134
135 bool createSwapchain(VulkanSurface* surface);
136 void createBuffers(VulkanSurface* surface, VkFormat format, VkExtent2D extent);
137
138 VulkanSurface::BackbufferInfo* getAvailableBackbuffer(VulkanSurface* surface);
139
Greg Daniel26e0dca2018-09-18 10:33:19 -0400140 bool setupDummyCommandBuffer();
141
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500142 // simple wrapper class that exists only to initialize a pointer to NULL
John Reck1bcacfd2017-11-03 10:12:19 -0700143 template <typename FNPTR_TYPE>
144 class VkPtr {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500145 public:
146 VkPtr() : fPtr(NULL) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700147 VkPtr operator=(FNPTR_TYPE ptr) {
148 fPtr = ptr;
149 return *this;
150 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500151 operator FNPTR_TYPE() const { return fPtr; }
John Reck1bcacfd2017-11-03 10:12:19 -0700152
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500153 private:
154 FNPTR_TYPE fPtr;
155 };
156
157 // WSI interface functions
158 VkPtr<PFN_vkCreateAndroidSurfaceKHR> mCreateAndroidSurfaceKHR;
159 VkPtr<PFN_vkDestroySurfaceKHR> mDestroySurfaceKHR;
160 VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> mGetPhysicalDeviceSurfaceSupportKHR;
161 VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> mGetPhysicalDeviceSurfaceCapabilitiesKHR;
162 VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> mGetPhysicalDeviceSurfaceFormatsKHR;
163 VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> mGetPhysicalDeviceSurfacePresentModesKHR;
164
165 VkPtr<PFN_vkCreateSwapchainKHR> mCreateSwapchainKHR;
166 VkPtr<PFN_vkDestroySwapchainKHR> mDestroySwapchainKHR;
167 VkPtr<PFN_vkGetSwapchainImagesKHR> mGetSwapchainImagesKHR;
168 VkPtr<PFN_vkAcquireNextImageKHR> mAcquireNextImageKHR;
169 VkPtr<PFN_vkQueuePresentKHR> mQueuePresentKHR;
170 VkPtr<PFN_vkCreateSharedSwapchainsKHR> mCreateSharedSwapchainsKHR;
171
Greg Daniel2ff202712018-06-14 11:50:10 -0400172 // Instance Functions
Greg Daniela227dbb2018-08-20 09:19:48 -0400173 VkPtr<PFN_vkEnumerateInstanceVersion> mEnumerateInstanceVersion;
Greg Daniel2ff202712018-06-14 11:50:10 -0400174 VkPtr<PFN_vkEnumerateInstanceExtensionProperties> mEnumerateInstanceExtensionProperties;
175 VkPtr<PFN_vkCreateInstance> mCreateInstance;
176
177 VkPtr<PFN_vkDestroyInstance> mDestroyInstance;
178 VkPtr<PFN_vkEnumeratePhysicalDevices> mEnumeratePhysicalDevices;
Greg Daniel96259622018-10-01 14:42:56 -0400179 VkPtr<PFN_vkGetPhysicalDeviceProperties> mGetPhysicalDeviceProperties;
Greg Daniel2ff202712018-06-14 11:50:10 -0400180 VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties;
Greg Daniela227dbb2018-08-20 09:19:48 -0400181 VkPtr<PFN_vkGetPhysicalDeviceFeatures2> mGetPhysicalDeviceFeatures2;
Greg Daniel2ff202712018-06-14 11:50:10 -0400182 VkPtr<PFN_vkCreateDevice> mCreateDevice;
183 VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties;
184
185 // Device Functions
186 VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue;
187 VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle;
188 VkPtr<PFN_vkDestroyDevice> mDestroyDevice;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500189 VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool;
190 VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool;
191 VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers;
192 VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers;
193 VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer;
194 VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer;
195 VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer;
196 VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier;
197
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500198 VkPtr<PFN_vkQueueSubmit> mQueueSubmit;
199 VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500200
201 VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore;
202 VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400203 VkPtr<PFN_vkImportSemaphoreFdKHR> mImportSemaphoreFdKHR;
204 VkPtr<PFN_vkGetSemaphoreFdKHR> mGetSemaphoreFdKHR;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500205 VkPtr<PFN_vkCreateFence> mCreateFence;
206 VkPtr<PFN_vkDestroyFence> mDestroyFence;
207 VkPtr<PFN_vkWaitForFences> mWaitForFences;
208 VkPtr<PFN_vkResetFences> mResetFences;
209
210 RenderThread& mRenderThread;
211
Greg Daniel2ff202712018-06-14 11:50:10 -0400212 VkInstance mInstance = VK_NULL_HANDLE;
213 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
214 VkDevice mDevice = VK_NULL_HANDLE;
215
216 uint32_t mGraphicsQueueIndex;
217 VkQueue mGraphicsQueue = VK_NULL_HANDLE;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500218 uint32_t mPresentQueueIndex;
219 VkQueue mPresentQueue = VK_NULL_HANDLE;
220 VkCommandPool mCommandPool = VK_NULL_HANDLE;
Greg Danielcd558522016-11-17 13:31:40 -0500221
Greg Daniel26e0dca2018-09-18 10:33:19 -0400222 VkCommandBuffer mDummyCB = VK_NULL_HANDLE;
223
Greg Danielcd558522016-11-17 13:31:40 -0500224 enum class SwapBehavior {
225 Discard,
226 BufferAge,
227 };
228 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500229};
230
231} /* namespace renderthread */
232} /* namespace uirenderer */
233} /* namespace android */
234
235#endif /* VULKANMANAGER_H */