blob: 5524c39d7a0c2e11e2e1616bbce1f3feac16d253 [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>
26#include <vk/GrVkBackendContext.h>
27
Greg Daniela227dbb2018-08-20 09:19:48 -040028class GrVkExtensions;
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
113private:
114 friend class RenderThread;
115
116 explicit VulkanManager(RenderThread& thread);
117 ~VulkanManager() { destroy(); }
118
Greg Daniel2ff202712018-06-14 11:50:10 -0400119 // Sets up the VkInstance and VkDevice objects. Also fills out the passed in
120 // VkPhysicalDeviceFeatures struct.
Greg Daniela227dbb2018-08-20 09:19:48 -0400121 bool setupDevice(GrVkExtensions&, VkPhysicalDeviceFeatures2&);
Greg Daniel2ff202712018-06-14 11:50:10 -0400122
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500123 void destroyBuffers(VulkanSurface* surface);
124
125 bool createSwapchain(VulkanSurface* surface);
126 void createBuffers(VulkanSurface* surface, VkFormat format, VkExtent2D extent);
127
128 VulkanSurface::BackbufferInfo* getAvailableBackbuffer(VulkanSurface* surface);
129
130 // simple wrapper class that exists only to initialize a pointer to NULL
John Reck1bcacfd2017-11-03 10:12:19 -0700131 template <typename FNPTR_TYPE>
132 class VkPtr {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500133 public:
134 VkPtr() : fPtr(NULL) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700135 VkPtr operator=(FNPTR_TYPE ptr) {
136 fPtr = ptr;
137 return *this;
138 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500139 operator FNPTR_TYPE() const { return fPtr; }
John Reck1bcacfd2017-11-03 10:12:19 -0700140
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500141 private:
142 FNPTR_TYPE fPtr;
143 };
144
145 // WSI interface functions
146 VkPtr<PFN_vkCreateAndroidSurfaceKHR> mCreateAndroidSurfaceKHR;
147 VkPtr<PFN_vkDestroySurfaceKHR> mDestroySurfaceKHR;
148 VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> mGetPhysicalDeviceSurfaceSupportKHR;
149 VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> mGetPhysicalDeviceSurfaceCapabilitiesKHR;
150 VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> mGetPhysicalDeviceSurfaceFormatsKHR;
151 VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> mGetPhysicalDeviceSurfacePresentModesKHR;
152
153 VkPtr<PFN_vkCreateSwapchainKHR> mCreateSwapchainKHR;
154 VkPtr<PFN_vkDestroySwapchainKHR> mDestroySwapchainKHR;
155 VkPtr<PFN_vkGetSwapchainImagesKHR> mGetSwapchainImagesKHR;
156 VkPtr<PFN_vkAcquireNextImageKHR> mAcquireNextImageKHR;
157 VkPtr<PFN_vkQueuePresentKHR> mQueuePresentKHR;
158 VkPtr<PFN_vkCreateSharedSwapchainsKHR> mCreateSharedSwapchainsKHR;
159
Greg Daniel2ff202712018-06-14 11:50:10 -0400160 // Instance Functions
Greg Daniela227dbb2018-08-20 09:19:48 -0400161 VkPtr<PFN_vkEnumerateInstanceVersion> mEnumerateInstanceVersion;
Greg Daniel2ff202712018-06-14 11:50:10 -0400162 VkPtr<PFN_vkEnumerateInstanceExtensionProperties> mEnumerateInstanceExtensionProperties;
163 VkPtr<PFN_vkCreateInstance> mCreateInstance;
164
165 VkPtr<PFN_vkDestroyInstance> mDestroyInstance;
166 VkPtr<PFN_vkEnumeratePhysicalDevices> mEnumeratePhysicalDevices;
167 VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties;
Greg Daniela227dbb2018-08-20 09:19:48 -0400168 VkPtr<PFN_vkGetPhysicalDeviceFeatures2> mGetPhysicalDeviceFeatures2;
Greg Daniel2ff202712018-06-14 11:50:10 -0400169 VkPtr<PFN_vkCreateDevice> mCreateDevice;
170 VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties;
171
172 // Device Functions
173 VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue;
174 VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle;
175 VkPtr<PFN_vkDestroyDevice> mDestroyDevice;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500176 VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool;
177 VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool;
178 VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers;
179 VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers;
180 VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer;
181 VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer;
182 VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer;
183 VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier;
184
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500185 VkPtr<PFN_vkQueueSubmit> mQueueSubmit;
186 VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500187
188 VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore;
189 VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore;
190 VkPtr<PFN_vkCreateFence> mCreateFence;
191 VkPtr<PFN_vkDestroyFence> mDestroyFence;
192 VkPtr<PFN_vkWaitForFences> mWaitForFences;
193 VkPtr<PFN_vkResetFences> mResetFences;
194
195 RenderThread& mRenderThread;
196
Greg Daniel2ff202712018-06-14 11:50:10 -0400197 VkInstance mInstance = VK_NULL_HANDLE;
198 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
199 VkDevice mDevice = VK_NULL_HANDLE;
200
201 uint32_t mGraphicsQueueIndex;
202 VkQueue mGraphicsQueue = VK_NULL_HANDLE;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500203 uint32_t mPresentQueueIndex;
204 VkQueue mPresentQueue = VK_NULL_HANDLE;
205 VkCommandPool mCommandPool = VK_NULL_HANDLE;
Greg Danielcd558522016-11-17 13:31:40 -0500206
207 enum class SwapBehavior {
208 Discard,
209 BufferAge,
210 };
211 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500212};
213
214} /* namespace renderthread */
215} /* namespace uirenderer */
216} /* namespace android */
217
218#endif /* VULKANMANAGER_H */