blob: 69641d57ccbb751d063ff20c8481d869e2285370 [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
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050028namespace android {
29namespace uirenderer {
30namespace renderthread {
31
32class RenderThread;
33
34class VulkanSurface {
35public:
36 VulkanSurface() {}
37
38 sk_sp<SkSurface> getBackBufferSurface() { return mBackbuffer; }
39
40private:
41 friend class VulkanManager;
42 struct BackbufferInfo {
John Reck1bcacfd2017-11-03 10:12:19 -070043 uint32_t mImageIndex; // image this is associated with
44 VkSemaphore mAcquireSemaphore; // we signal on this for acquisition of image
45 VkSemaphore mRenderSemaphore; // we wait on this for rendering to be done
46 VkCommandBuffer
47 mTransitionCmdBuffers[2]; // to transition layout between present and render
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050048 // We use these fences to make sure the above Command buffers have finished their work
49 // before attempting to reuse them or destroy them.
John Reck1bcacfd2017-11-03 10:12:19 -070050 VkFence mUsageFences[2];
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050051 };
52
Greg Danielcd558522016-11-17 13:31:40 -050053 struct ImageInfo {
54 VkImageLayout mImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
55 sk_sp<SkSurface> mSurface;
56 uint16_t mLastUsed = 0;
57 bool mInvalid = true;
58 };
59
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050060 sk_sp<SkSurface> mBackbuffer;
61
62 VkSurfaceKHR mVkSurface = VK_NULL_HANDLE;
63 VkSwapchainKHR mSwapchain = VK_NULL_HANDLE;
64
Greg Daniel74ea2012017-11-10 11:32:58 -050065 BackbufferInfo* mBackbuffers = nullptr;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050066 uint32_t mCurrentBackbufferIndex;
67
68 uint32_t mImageCount;
Greg Daniel74ea2012017-11-10 11:32:58 -050069 VkImage* mImages = nullptr;
Greg Danielcd558522016-11-17 13:31:40 -050070 ImageInfo* mImageInfos;
71 uint16_t mCurrentTime = 0;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050072};
73
74// This class contains the shared global Vulkan objects, such as VkInstance, VkDevice and VkQueue,
75// which are re-used by CanvasContext. This class is created once and should be used by all vulkan
76// windowing contexts. The VulkanManager must be initialized before use.
77class VulkanManager {
78public:
79 // Sets up the vulkan context that is shared amonst all clients of the VulkanManager. This must
80 // be call once before use of the VulkanManager. Multiple calls after the first will simiply
81 // return.
82 void initialize();
83
84 // Quick check to see if the VulkanManager has been initialized.
Greg Daniel2f9d8672018-06-22 10:44:26 -040085 bool hasVkContext() { return mDevice != VK_NULL_HANDLE; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050086
87 // Given a window this creates a new VkSurfaceKHR and VkSwapchain and stores them inside a new
88 // VulkanSurface object which is returned.
89 VulkanSurface* createSurface(ANativeWindow* window);
90
91 // Destroy the VulkanSurface and all associated vulkan objects.
92 void destroySurface(VulkanSurface* surface);
93
94 // Cleans up all the global state in the VulkanManger.
95 void destroy();
96
97 // No work is needed to make a VulkanSurface current, and all functions require that a
98 // VulkanSurface is passed into them so we just return true here.
99 bool isCurrent(VulkanSurface* surface) { return true; }
100
Greg Danielcd558522016-11-17 13:31:40 -0500101 int getAge(VulkanSurface* surface);
102
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500103 // Returns an SkSurface which wraps the next image returned from vkAcquireNextImageKHR. It also
104 // will transition the VkImage from a present layout to color attachment so that it can be used
105 // by the client for drawing.
106 SkSurface* getBackbufferSurface(VulkanSurface* surface);
107
108 // Presents the current VkImage.
109 void swapBuffers(VulkanSurface* surface);
110
111private:
112 friend class RenderThread;
113
114 explicit VulkanManager(RenderThread& thread);
115 ~VulkanManager() { destroy(); }
116
Greg Daniel2ff202712018-06-14 11:50:10 -0400117 // Sets up the VkInstance and VkDevice objects. Also fills out the passed in
118 // VkPhysicalDeviceFeatures struct.
119 bool setupDevice(VkPhysicalDeviceFeatures& deviceFeatures);
120
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500121 void destroyBuffers(VulkanSurface* surface);
122
123 bool createSwapchain(VulkanSurface* surface);
124 void createBuffers(VulkanSurface* surface, VkFormat format, VkExtent2D extent);
125
126 VulkanSurface::BackbufferInfo* getAvailableBackbuffer(VulkanSurface* surface);
127
128 // simple wrapper class that exists only to initialize a pointer to NULL
John Reck1bcacfd2017-11-03 10:12:19 -0700129 template <typename FNPTR_TYPE>
130 class VkPtr {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500131 public:
132 VkPtr() : fPtr(NULL) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700133 VkPtr operator=(FNPTR_TYPE ptr) {
134 fPtr = ptr;
135 return *this;
136 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500137 operator FNPTR_TYPE() const { return fPtr; }
John Reck1bcacfd2017-11-03 10:12:19 -0700138
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500139 private:
140 FNPTR_TYPE fPtr;
141 };
142
143 // WSI interface functions
144 VkPtr<PFN_vkCreateAndroidSurfaceKHR> mCreateAndroidSurfaceKHR;
145 VkPtr<PFN_vkDestroySurfaceKHR> mDestroySurfaceKHR;
146 VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> mGetPhysicalDeviceSurfaceSupportKHR;
147 VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> mGetPhysicalDeviceSurfaceCapabilitiesKHR;
148 VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> mGetPhysicalDeviceSurfaceFormatsKHR;
149 VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> mGetPhysicalDeviceSurfacePresentModesKHR;
150
151 VkPtr<PFN_vkCreateSwapchainKHR> mCreateSwapchainKHR;
152 VkPtr<PFN_vkDestroySwapchainKHR> mDestroySwapchainKHR;
153 VkPtr<PFN_vkGetSwapchainImagesKHR> mGetSwapchainImagesKHR;
154 VkPtr<PFN_vkAcquireNextImageKHR> mAcquireNextImageKHR;
155 VkPtr<PFN_vkQueuePresentKHR> mQueuePresentKHR;
156 VkPtr<PFN_vkCreateSharedSwapchainsKHR> mCreateSharedSwapchainsKHR;
157
Greg Daniel2ff202712018-06-14 11:50:10 -0400158 // Instance Functions
159 VkPtr<PFN_vkEnumerateInstanceExtensionProperties> mEnumerateInstanceExtensionProperties;
160 VkPtr<PFN_vkCreateInstance> mCreateInstance;
161
162 VkPtr<PFN_vkDestroyInstance> mDestroyInstance;
163 VkPtr<PFN_vkEnumeratePhysicalDevices> mEnumeratePhysicalDevices;
164 VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties;
165 VkPtr<PFN_vkGetPhysicalDeviceFeatures> mGetPhysicalDeviceFeatures;
166 VkPtr<PFN_vkCreateDevice> mCreateDevice;
167 VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties;
168
169 // Device Functions
170 VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue;
171 VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle;
172 VkPtr<PFN_vkDestroyDevice> mDestroyDevice;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500173 VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool;
174 VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool;
175 VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers;
176 VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers;
177 VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer;
178 VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer;
179 VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer;
180 VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier;
181
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500182 VkPtr<PFN_vkQueueSubmit> mQueueSubmit;
183 VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500184
185 VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore;
186 VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore;
187 VkPtr<PFN_vkCreateFence> mCreateFence;
188 VkPtr<PFN_vkDestroyFence> mDestroyFence;
189 VkPtr<PFN_vkWaitForFences> mWaitForFences;
190 VkPtr<PFN_vkResetFences> mResetFences;
191
192 RenderThread& mRenderThread;
193
Greg Daniel2ff202712018-06-14 11:50:10 -0400194 VkInstance mInstance = VK_NULL_HANDLE;
195 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
196 VkDevice mDevice = VK_NULL_HANDLE;
197
198 uint32_t mGraphicsQueueIndex;
199 VkQueue mGraphicsQueue = VK_NULL_HANDLE;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500200 uint32_t mPresentQueueIndex;
201 VkQueue mPresentQueue = VK_NULL_HANDLE;
202 VkCommandPool mCommandPool = VK_NULL_HANDLE;
Greg Danielcd558522016-11-17 13:31:40 -0500203
204 enum class SwapBehavior {
205 Discard,
206 BufferAge,
207 };
208 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500209};
210
211} /* namespace renderthread */
212} /* namespace uirenderer */
213} /* namespace android */
214
215#endif /* VULKANMANAGER_H */