blob: 7a539ae42605e0de808c1beca22d01a96a632d75 [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
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700121 // TODO(b/115636873): Handle composition preference.
122 SkColorType getSurfaceColorType() const { return SkColorType::kN32_SkColorType; }
123 sk_sp<SkColorSpace> getSurfaceColorSpace() { return SkColorSpace::MakeSRGB(); }
124
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500125private:
126 friend class RenderThread;
127
128 explicit VulkanManager(RenderThread& thread);
129 ~VulkanManager() { destroy(); }
130
Greg Daniel2ff202712018-06-14 11:50:10 -0400131 // Sets up the VkInstance and VkDevice objects. Also fills out the passed in
132 // VkPhysicalDeviceFeatures struct.
Greg Daniela227dbb2018-08-20 09:19:48 -0400133 bool setupDevice(GrVkExtensions&, VkPhysicalDeviceFeatures2&);
Greg Daniel2ff202712018-06-14 11:50:10 -0400134
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500135 void destroyBuffers(VulkanSurface* surface);
136
137 bool createSwapchain(VulkanSurface* surface);
138 void createBuffers(VulkanSurface* surface, VkFormat format, VkExtent2D extent);
139
140 VulkanSurface::BackbufferInfo* getAvailableBackbuffer(VulkanSurface* surface);
141
142 // 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;
179 VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties;
Greg Daniela227dbb2018-08-20 09:19:48 -0400180 VkPtr<PFN_vkGetPhysicalDeviceFeatures2> mGetPhysicalDeviceFeatures2;
Greg Daniel2ff202712018-06-14 11:50:10 -0400181 VkPtr<PFN_vkCreateDevice> mCreateDevice;
182 VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties;
183
184 // Device Functions
185 VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue;
186 VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle;
187 VkPtr<PFN_vkDestroyDevice> mDestroyDevice;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500188 VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool;
189 VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool;
190 VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers;
191 VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers;
192 VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer;
193 VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer;
194 VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer;
195 VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier;
196
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500197 VkPtr<PFN_vkQueueSubmit> mQueueSubmit;
198 VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500199
200 VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore;
201 VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore;
202 VkPtr<PFN_vkCreateFence> mCreateFence;
203 VkPtr<PFN_vkDestroyFence> mDestroyFence;
204 VkPtr<PFN_vkWaitForFences> mWaitForFences;
205 VkPtr<PFN_vkResetFences> mResetFences;
206
207 RenderThread& mRenderThread;
208
Greg Daniel2ff202712018-06-14 11:50:10 -0400209 VkInstance mInstance = VK_NULL_HANDLE;
210 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
211 VkDevice mDevice = VK_NULL_HANDLE;
212
213 uint32_t mGraphicsQueueIndex;
214 VkQueue mGraphicsQueue = VK_NULL_HANDLE;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500215 uint32_t mPresentQueueIndex;
216 VkQueue mPresentQueue = VK_NULL_HANDLE;
217 VkCommandPool mCommandPool = VK_NULL_HANDLE;
Greg Danielcd558522016-11-17 13:31:40 -0500218
219 enum class SwapBehavior {
220 Discard,
221 BufferAge,
222 };
223 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500224};
225
226} /* namespace renderthread */
227} /* namespace uirenderer */
228} /* namespace android */
229
230#endif /* VULKANMANAGER_H */