blob: 7051ee686659c67dea32265f471acf3fbf38cb8f [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 Ilievc8e22a62018-08-14 13:30:17 -040026#include <ui/Fence.h>
27#include <utils/StrongPointer.h>
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050028#include <vk/GrVkBackendContext.h>
29
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050030namespace android {
31namespace uirenderer {
32namespace renderthread {
33
34class RenderThread;
35
36class VulkanSurface {
37public:
38 VulkanSurface() {}
39
40 sk_sp<SkSurface> getBackBufferSurface() { return mBackbuffer; }
41
42private:
43 friend class VulkanManager;
44 struct BackbufferInfo {
John Reck1bcacfd2017-11-03 10:12:19 -070045 uint32_t mImageIndex; // image this is associated with
46 VkSemaphore mAcquireSemaphore; // we signal on this for acquisition of image
47 VkSemaphore mRenderSemaphore; // we wait on this for rendering to be done
48 VkCommandBuffer
49 mTransitionCmdBuffers[2]; // to transition layout between present and render
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050050 // We use these fences to make sure the above Command buffers have finished their work
51 // before attempting to reuse them or destroy them.
John Reck1bcacfd2017-11-03 10:12:19 -070052 VkFence mUsageFences[2];
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050053 };
54
Greg Danielcd558522016-11-17 13:31:40 -050055 struct ImageInfo {
56 VkImageLayout mImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
57 sk_sp<SkSurface> mSurface;
58 uint16_t mLastUsed = 0;
59 bool mInvalid = true;
60 };
61
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050062 sk_sp<SkSurface> mBackbuffer;
63
64 VkSurfaceKHR mVkSurface = VK_NULL_HANDLE;
65 VkSwapchainKHR mSwapchain = VK_NULL_HANDLE;
66
Greg Daniel74ea2012017-11-10 11:32:58 -050067 BackbufferInfo* mBackbuffers = nullptr;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050068 uint32_t mCurrentBackbufferIndex;
69
70 uint32_t mImageCount;
Greg Daniel74ea2012017-11-10 11:32:58 -050071 VkImage* mImages = nullptr;
Greg Danielcd558522016-11-17 13:31:40 -050072 ImageInfo* mImageInfos;
73 uint16_t mCurrentTime = 0;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050074};
75
76// This class contains the shared global Vulkan objects, such as VkInstance, VkDevice and VkQueue,
77// which are re-used by CanvasContext. This class is created once and should be used by all vulkan
78// windowing contexts. The VulkanManager must be initialized before use.
79class VulkanManager {
80public:
81 // Sets up the vulkan context that is shared amonst all clients of the VulkanManager. This must
82 // be call once before use of the VulkanManager. Multiple calls after the first will simiply
83 // return.
84 void initialize();
85
86 // Quick check to see if the VulkanManager has been initialized.
Greg Daniel2f9d8672018-06-22 10:44:26 -040087 bool hasVkContext() { return mDevice != VK_NULL_HANDLE; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050088
89 // Given a window this creates a new VkSurfaceKHR and VkSwapchain and stores them inside a new
90 // VulkanSurface object which is returned.
91 VulkanSurface* createSurface(ANativeWindow* window);
92
93 // Destroy the VulkanSurface and all associated vulkan objects.
94 void destroySurface(VulkanSurface* surface);
95
96 // Cleans up all the global state in the VulkanManger.
97 void destroy();
98
99 // No work is needed to make a VulkanSurface current, and all functions require that a
100 // VulkanSurface is passed into them so we just return true here.
101 bool isCurrent(VulkanSurface* surface) { return true; }
102
Greg Danielcd558522016-11-17 13:31:40 -0500103 int getAge(VulkanSurface* surface);
104
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500105 // Returns an SkSurface which wraps the next image returned from vkAcquireNextImageKHR. It also
106 // will transition the VkImage from a present layout to color attachment so that it can be used
107 // by the client for drawing.
108 SkSurface* getBackbufferSurface(VulkanSurface* surface);
109
110 // Presents the current VkImage.
111 void swapBuffers(VulkanSurface* surface);
112
Stan Ilievc8e22a62018-08-14 13:30:17 -0400113 // Inserts a wait on fence command into the Vulkan command buffer.
114 status_t fenceWait(sp<Fence>& fence);
115
116 // Creates a fence that is signaled, when all the pending Vulkan commands are flushed.
117 status_t createReleaseFence(sp<Fence>& nativeFence);
118
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500119private:
120 friend class RenderThread;
121
122 explicit VulkanManager(RenderThread& thread);
123 ~VulkanManager() { destroy(); }
124
Greg Daniel2ff202712018-06-14 11:50:10 -0400125 // Sets up the VkInstance and VkDevice objects. Also fills out the passed in
126 // VkPhysicalDeviceFeatures struct.
127 bool setupDevice(VkPhysicalDeviceFeatures& deviceFeatures);
128
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500129 void destroyBuffers(VulkanSurface* surface);
130
131 bool createSwapchain(VulkanSurface* surface);
132 void createBuffers(VulkanSurface* surface, VkFormat format, VkExtent2D extent);
133
134 VulkanSurface::BackbufferInfo* getAvailableBackbuffer(VulkanSurface* surface);
135
136 // simple wrapper class that exists only to initialize a pointer to NULL
John Reck1bcacfd2017-11-03 10:12:19 -0700137 template <typename FNPTR_TYPE>
138 class VkPtr {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500139 public:
140 VkPtr() : fPtr(NULL) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700141 VkPtr operator=(FNPTR_TYPE ptr) {
142 fPtr = ptr;
143 return *this;
144 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500145 operator FNPTR_TYPE() const { return fPtr; }
John Reck1bcacfd2017-11-03 10:12:19 -0700146
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500147 private:
148 FNPTR_TYPE fPtr;
149 };
150
151 // WSI interface functions
152 VkPtr<PFN_vkCreateAndroidSurfaceKHR> mCreateAndroidSurfaceKHR;
153 VkPtr<PFN_vkDestroySurfaceKHR> mDestroySurfaceKHR;
154 VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> mGetPhysicalDeviceSurfaceSupportKHR;
155 VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> mGetPhysicalDeviceSurfaceCapabilitiesKHR;
156 VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> mGetPhysicalDeviceSurfaceFormatsKHR;
157 VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> mGetPhysicalDeviceSurfacePresentModesKHR;
158
159 VkPtr<PFN_vkCreateSwapchainKHR> mCreateSwapchainKHR;
160 VkPtr<PFN_vkDestroySwapchainKHR> mDestroySwapchainKHR;
161 VkPtr<PFN_vkGetSwapchainImagesKHR> mGetSwapchainImagesKHR;
162 VkPtr<PFN_vkAcquireNextImageKHR> mAcquireNextImageKHR;
163 VkPtr<PFN_vkQueuePresentKHR> mQueuePresentKHR;
164 VkPtr<PFN_vkCreateSharedSwapchainsKHR> mCreateSharedSwapchainsKHR;
165
Greg Daniel2ff202712018-06-14 11:50:10 -0400166 // Instance Functions
167 VkPtr<PFN_vkEnumerateInstanceExtensionProperties> mEnumerateInstanceExtensionProperties;
168 VkPtr<PFN_vkCreateInstance> mCreateInstance;
169
170 VkPtr<PFN_vkDestroyInstance> mDestroyInstance;
171 VkPtr<PFN_vkEnumeratePhysicalDevices> mEnumeratePhysicalDevices;
172 VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties;
173 VkPtr<PFN_vkGetPhysicalDeviceFeatures> mGetPhysicalDeviceFeatures;
174 VkPtr<PFN_vkCreateDevice> mCreateDevice;
175 VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties;
176
177 // Device Functions
178 VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue;
179 VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle;
180 VkPtr<PFN_vkDestroyDevice> mDestroyDevice;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500181 VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool;
182 VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool;
183 VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers;
184 VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers;
185 VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer;
186 VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer;
187 VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer;
188 VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier;
189
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500190 VkPtr<PFN_vkQueueSubmit> mQueueSubmit;
191 VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500192
193 VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore;
194 VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore;
195 VkPtr<PFN_vkCreateFence> mCreateFence;
196 VkPtr<PFN_vkDestroyFence> mDestroyFence;
197 VkPtr<PFN_vkWaitForFences> mWaitForFences;
198 VkPtr<PFN_vkResetFences> mResetFences;
199
200 RenderThread& mRenderThread;
201
Greg Daniel2ff202712018-06-14 11:50:10 -0400202 VkInstance mInstance = VK_NULL_HANDLE;
203 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
204 VkDevice mDevice = VK_NULL_HANDLE;
205
206 uint32_t mGraphicsQueueIndex;
207 VkQueue mGraphicsQueue = VK_NULL_HANDLE;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500208 uint32_t mPresentQueueIndex;
209 VkQueue mPresentQueue = VK_NULL_HANDLE;
210 VkCommandPool mCommandPool = VK_NULL_HANDLE;
Greg Danielcd558522016-11-17 13:31:40 -0500211
212 enum class SwapBehavior {
213 Discard,
214 BufferAge,
215 };
216 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500217};
218
219} /* namespace renderthread */
220} /* namespace uirenderer */
221} /* namespace android */
222
223#endif /* VULKANMANAGER_H */