blob: ebc11a50685ecee42d0e2d257c313eccee096ea6 [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>
29
Greg Daniela227dbb2018-08-20 09:19:48 -040030class GrVkExtensions;
31
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050032namespace android {
33namespace uirenderer {
34namespace renderthread {
35
36class RenderThread;
37
38class VulkanSurface {
39public:
40 VulkanSurface() {}
41
42 sk_sp<SkSurface> getBackBufferSurface() { return mBackbuffer; }
43
44private:
45 friend class VulkanManager;
46 struct BackbufferInfo {
John Reck1bcacfd2017-11-03 10:12:19 -070047 uint32_t mImageIndex; // image this is associated with
48 VkSemaphore mAcquireSemaphore; // we signal on this for acquisition of image
49 VkSemaphore mRenderSemaphore; // we wait on this for rendering to be done
50 VkCommandBuffer
51 mTransitionCmdBuffers[2]; // to transition layout between present and render
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050052 // We use these fences to make sure the above Command buffers have finished their work
53 // before attempting to reuse them or destroy them.
John Reck1bcacfd2017-11-03 10:12:19 -070054 VkFence mUsageFences[2];
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050055 };
56
Greg Danielcd558522016-11-17 13:31:40 -050057 struct ImageInfo {
58 VkImageLayout mImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
59 sk_sp<SkSurface> mSurface;
60 uint16_t mLastUsed = 0;
61 bool mInvalid = true;
62 };
63
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050064 sk_sp<SkSurface> mBackbuffer;
65
66 VkSurfaceKHR mVkSurface = VK_NULL_HANDLE;
67 VkSwapchainKHR mSwapchain = VK_NULL_HANDLE;
68
Greg Daniel74ea2012017-11-10 11:32:58 -050069 BackbufferInfo* mBackbuffers = nullptr;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050070 uint32_t mCurrentBackbufferIndex;
71
72 uint32_t mImageCount;
Greg Daniel74ea2012017-11-10 11:32:58 -050073 VkImage* mImages = nullptr;
Greg Danielcd558522016-11-17 13:31:40 -050074 ImageInfo* mImageInfos;
75 uint16_t mCurrentTime = 0;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050076};
77
78// This class contains the shared global Vulkan objects, such as VkInstance, VkDevice and VkQueue,
79// which are re-used by CanvasContext. This class is created once and should be used by all vulkan
80// windowing contexts. The VulkanManager must be initialized before use.
81class VulkanManager {
82public:
83 // Sets up the vulkan context that is shared amonst all clients of the VulkanManager. This must
84 // be call once before use of the VulkanManager. Multiple calls after the first will simiply
85 // return.
86 void initialize();
87
88 // Quick check to see if the VulkanManager has been initialized.
Greg Daniel2f9d8672018-06-22 10:44:26 -040089 bool hasVkContext() { return mDevice != VK_NULL_HANDLE; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050090
91 // Given a window this creates a new VkSurfaceKHR and VkSwapchain and stores them inside a new
92 // VulkanSurface object which is returned.
93 VulkanSurface* createSurface(ANativeWindow* window);
94
95 // Destroy the VulkanSurface and all associated vulkan objects.
96 void destroySurface(VulkanSurface* surface);
97
98 // Cleans up all the global state in the VulkanManger.
99 void destroy();
100
101 // No work is needed to make a VulkanSurface current, and all functions require that a
102 // VulkanSurface is passed into them so we just return true here.
103 bool isCurrent(VulkanSurface* surface) { return true; }
104
Greg Danielcd558522016-11-17 13:31:40 -0500105 int getAge(VulkanSurface* surface);
106
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500107 // Returns an SkSurface which wraps the next image returned from vkAcquireNextImageKHR. It also
108 // will transition the VkImage from a present layout to color attachment so that it can be used
109 // by the client for drawing.
110 SkSurface* getBackbufferSurface(VulkanSurface* surface);
111
112 // Presents the current VkImage.
113 void swapBuffers(VulkanSurface* surface);
114
Stan Iliev564ca3e2018-09-04 22:00:00 +0000115 // Inserts a wait on fence command into the Vulkan command buffer.
116 status_t fenceWait(sp<Fence>& fence);
117
118 // Creates a fence that is signaled, when all the pending Vulkan commands are flushed.
119 status_t createReleaseFence(sp<Fence>& nativeFence);
120
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500121private:
122 friend class RenderThread;
123
124 explicit VulkanManager(RenderThread& thread);
125 ~VulkanManager() { destroy(); }
126
Greg Daniel2ff202712018-06-14 11:50:10 -0400127 // Sets up the VkInstance and VkDevice objects. Also fills out the passed in
128 // VkPhysicalDeviceFeatures struct.
Greg Daniela227dbb2018-08-20 09:19:48 -0400129 bool setupDevice(GrVkExtensions&, VkPhysicalDeviceFeatures2&);
Greg Daniel2ff202712018-06-14 11:50:10 -0400130
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500131 void destroyBuffers(VulkanSurface* surface);
132
133 bool createSwapchain(VulkanSurface* surface);
134 void createBuffers(VulkanSurface* surface, VkFormat format, VkExtent2D extent);
135
136 VulkanSurface::BackbufferInfo* getAvailableBackbuffer(VulkanSurface* surface);
137
138 // simple wrapper class that exists only to initialize a pointer to NULL
John Reck1bcacfd2017-11-03 10:12:19 -0700139 template <typename FNPTR_TYPE>
140 class VkPtr {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500141 public:
142 VkPtr() : fPtr(NULL) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700143 VkPtr operator=(FNPTR_TYPE ptr) {
144 fPtr = ptr;
145 return *this;
146 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500147 operator FNPTR_TYPE() const { return fPtr; }
John Reck1bcacfd2017-11-03 10:12:19 -0700148
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500149 private:
150 FNPTR_TYPE fPtr;
151 };
152
153 // WSI interface functions
154 VkPtr<PFN_vkCreateAndroidSurfaceKHR> mCreateAndroidSurfaceKHR;
155 VkPtr<PFN_vkDestroySurfaceKHR> mDestroySurfaceKHR;
156 VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> mGetPhysicalDeviceSurfaceSupportKHR;
157 VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> mGetPhysicalDeviceSurfaceCapabilitiesKHR;
158 VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> mGetPhysicalDeviceSurfaceFormatsKHR;
159 VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> mGetPhysicalDeviceSurfacePresentModesKHR;
160
161 VkPtr<PFN_vkCreateSwapchainKHR> mCreateSwapchainKHR;
162 VkPtr<PFN_vkDestroySwapchainKHR> mDestroySwapchainKHR;
163 VkPtr<PFN_vkGetSwapchainImagesKHR> mGetSwapchainImagesKHR;
164 VkPtr<PFN_vkAcquireNextImageKHR> mAcquireNextImageKHR;
165 VkPtr<PFN_vkQueuePresentKHR> mQueuePresentKHR;
166 VkPtr<PFN_vkCreateSharedSwapchainsKHR> mCreateSharedSwapchainsKHR;
167
Greg Daniel2ff202712018-06-14 11:50:10 -0400168 // Instance Functions
Greg Daniela227dbb2018-08-20 09:19:48 -0400169 VkPtr<PFN_vkEnumerateInstanceVersion> mEnumerateInstanceVersion;
Greg Daniel2ff202712018-06-14 11:50:10 -0400170 VkPtr<PFN_vkEnumerateInstanceExtensionProperties> mEnumerateInstanceExtensionProperties;
171 VkPtr<PFN_vkCreateInstance> mCreateInstance;
172
173 VkPtr<PFN_vkDestroyInstance> mDestroyInstance;
174 VkPtr<PFN_vkEnumeratePhysicalDevices> mEnumeratePhysicalDevices;
175 VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties;
Greg Daniela227dbb2018-08-20 09:19:48 -0400176 VkPtr<PFN_vkGetPhysicalDeviceFeatures2> mGetPhysicalDeviceFeatures2;
Greg Daniel2ff202712018-06-14 11:50:10 -0400177 VkPtr<PFN_vkCreateDevice> mCreateDevice;
178 VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties;
179
180 // Device Functions
181 VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue;
182 VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle;
183 VkPtr<PFN_vkDestroyDevice> mDestroyDevice;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500184 VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool;
185 VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool;
186 VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers;
187 VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers;
188 VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer;
189 VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer;
190 VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer;
191 VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier;
192
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500193 VkPtr<PFN_vkQueueSubmit> mQueueSubmit;
194 VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500195
196 VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore;
197 VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore;
198 VkPtr<PFN_vkCreateFence> mCreateFence;
199 VkPtr<PFN_vkDestroyFence> mDestroyFence;
200 VkPtr<PFN_vkWaitForFences> mWaitForFences;
201 VkPtr<PFN_vkResetFences> mResetFences;
202
203 RenderThread& mRenderThread;
204
Greg Daniel2ff202712018-06-14 11:50:10 -0400205 VkInstance mInstance = VK_NULL_HANDLE;
206 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
207 VkDevice mDevice = VK_NULL_HANDLE;
208
209 uint32_t mGraphicsQueueIndex;
210 VkQueue mGraphicsQueue = VK_NULL_HANDLE;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500211 uint32_t mPresentQueueIndex;
212 VkQueue mPresentQueue = VK_NULL_HANDLE;
213 VkCommandPool mCommandPool = VK_NULL_HANDLE;
Greg Danielcd558522016-11-17 13:31:40 -0500214
215 enum class SwapBehavior {
216 Discard,
217 BufferAge,
218 };
219 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500220};
221
222} /* namespace renderthread */
223} /* namespace uirenderer */
224} /* namespace android */
225
226#endif /* VULKANMANAGER_H */