blob: e54eb6a0c5f1ac74ba6a6bbc710e96398c11270f [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>
Stan Iliev79351f32018-09-19 14:23:49 -040029#include "IRenderPipeline.h"
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050030
Greg Daniela227dbb2018-08-20 09:19:48 -040031class GrVkExtensions;
32
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050033namespace android {
34namespace uirenderer {
35namespace renderthread {
36
37class RenderThread;
38
39class VulkanSurface {
40public:
Stan Iliev79351f32018-09-19 14:23:49 -040041 VulkanSurface(ColorMode colorMode) : mColorMode(colorMode) {}
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050042
43 sk_sp<SkSurface> getBackBufferSurface() { return mBackbuffer; }
44
45private:
46 friend class VulkanManager;
47 struct BackbufferInfo {
John Reck1bcacfd2017-11-03 10:12:19 -070048 uint32_t mImageIndex; // image this is associated with
49 VkSemaphore mAcquireSemaphore; // we signal on this for acquisition of image
50 VkSemaphore mRenderSemaphore; // we wait on this for rendering to be done
51 VkCommandBuffer
52 mTransitionCmdBuffers[2]; // to transition layout between present and render
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050053 // We use these fences to make sure the above Command buffers have finished their work
54 // before attempting to reuse them or destroy them.
John Reck1bcacfd2017-11-03 10:12:19 -070055 VkFence mUsageFences[2];
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050056 };
57
Greg Danielcd558522016-11-17 13:31:40 -050058 struct ImageInfo {
59 VkImageLayout mImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
60 sk_sp<SkSurface> mSurface;
61 uint16_t mLastUsed = 0;
62 bool mInvalid = true;
63 };
64
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050065 sk_sp<SkSurface> mBackbuffer;
66
67 VkSurfaceKHR mVkSurface = VK_NULL_HANDLE;
68 VkSwapchainKHR mSwapchain = VK_NULL_HANDLE;
69
Greg Daniel74ea2012017-11-10 11:32:58 -050070 BackbufferInfo* mBackbuffers = nullptr;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050071 uint32_t mCurrentBackbufferIndex;
72
73 uint32_t mImageCount;
Greg Daniel74ea2012017-11-10 11:32:58 -050074 VkImage* mImages = nullptr;
Greg Danielcd558522016-11-17 13:31:40 -050075 ImageInfo* mImageInfos;
76 uint16_t mCurrentTime = 0;
Stan Iliev79351f32018-09-19 14:23:49 -040077 ColorMode mColorMode;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050078};
79
80// This class contains the shared global Vulkan objects, such as VkInstance, VkDevice and VkQueue,
81// which are re-used by CanvasContext. This class is created once and should be used by all vulkan
82// windowing contexts. The VulkanManager must be initialized before use.
83class VulkanManager {
84public:
85 // Sets up the vulkan context that is shared amonst all clients of the VulkanManager. This must
86 // be call once before use of the VulkanManager. Multiple calls after the first will simiply
87 // return.
88 void initialize();
89
90 // Quick check to see if the VulkanManager has been initialized.
Greg Daniel2f9d8672018-06-22 10:44:26 -040091 bool hasVkContext() { return mDevice != VK_NULL_HANDLE; }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050092
93 // Given a window this creates a new VkSurfaceKHR and VkSwapchain and stores them inside a new
94 // VulkanSurface object which is returned.
Stan Iliev79351f32018-09-19 14:23:49 -040095 VulkanSurface* createSurface(ANativeWindow* window, ColorMode colorMode);
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050096
97 // Destroy the VulkanSurface and all associated vulkan objects.
98 void destroySurface(VulkanSurface* surface);
99
100 // Cleans up all the global state in the VulkanManger.
101 void destroy();
102
103 // No work is needed to make a VulkanSurface current, and all functions require that a
104 // VulkanSurface is passed into them so we just return true here.
105 bool isCurrent(VulkanSurface* surface) { return true; }
106
Greg Danielcd558522016-11-17 13:31:40 -0500107 int getAge(VulkanSurface* surface);
108
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500109 // Returns an SkSurface which wraps the next image returned from vkAcquireNextImageKHR. It also
110 // will transition the VkImage from a present layout to color attachment so that it can be used
111 // by the client for drawing.
112 SkSurface* getBackbufferSurface(VulkanSurface* surface);
113
114 // Presents the current VkImage.
115 void swapBuffers(VulkanSurface* surface);
116
Stan Iliev564ca3e2018-09-04 22:00:00 +0000117 // Inserts a wait on fence command into the Vulkan command buffer.
118 status_t fenceWait(sp<Fence>& fence);
119
120 // Creates a fence that is signaled, when all the pending Vulkan commands are flushed.
121 status_t createReleaseFence(sp<Fence>& nativeFence);
122
Peiyong Lin1f6aa122018-09-10 16:28:08 -0700123 // TODO(b/115636873): Handle composition preference.
124 SkColorType getSurfaceColorType() const { return SkColorType::kN32_SkColorType; }
125 sk_sp<SkColorSpace> getSurfaceColorSpace() { return SkColorSpace::MakeSRGB(); }
126
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500127private:
128 friend class RenderThread;
129
130 explicit VulkanManager(RenderThread& thread);
131 ~VulkanManager() { destroy(); }
132
Greg Daniel2ff202712018-06-14 11:50:10 -0400133 // Sets up the VkInstance and VkDevice objects. Also fills out the passed in
134 // VkPhysicalDeviceFeatures struct.
Greg Daniela227dbb2018-08-20 09:19:48 -0400135 bool setupDevice(GrVkExtensions&, VkPhysicalDeviceFeatures2&);
Greg Daniel2ff202712018-06-14 11:50:10 -0400136
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500137 void destroyBuffers(VulkanSurface* surface);
138
139 bool createSwapchain(VulkanSurface* surface);
140 void createBuffers(VulkanSurface* surface, VkFormat format, VkExtent2D extent);
141
142 VulkanSurface::BackbufferInfo* getAvailableBackbuffer(VulkanSurface* surface);
143
Greg Daniel26e0dca2018-09-18 10:33:19 -0400144 bool setupDummyCommandBuffer();
145
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500146 // simple wrapper class that exists only to initialize a pointer to NULL
John Reck1bcacfd2017-11-03 10:12:19 -0700147 template <typename FNPTR_TYPE>
148 class VkPtr {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500149 public:
150 VkPtr() : fPtr(NULL) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700151 VkPtr operator=(FNPTR_TYPE ptr) {
152 fPtr = ptr;
153 return *this;
154 }
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500155 operator FNPTR_TYPE() const { return fPtr; }
John Reck1bcacfd2017-11-03 10:12:19 -0700156
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500157 private:
158 FNPTR_TYPE fPtr;
159 };
160
161 // WSI interface functions
162 VkPtr<PFN_vkCreateAndroidSurfaceKHR> mCreateAndroidSurfaceKHR;
163 VkPtr<PFN_vkDestroySurfaceKHR> mDestroySurfaceKHR;
164 VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> mGetPhysicalDeviceSurfaceSupportKHR;
165 VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> mGetPhysicalDeviceSurfaceCapabilitiesKHR;
166 VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> mGetPhysicalDeviceSurfaceFormatsKHR;
167 VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> mGetPhysicalDeviceSurfacePresentModesKHR;
168
169 VkPtr<PFN_vkCreateSwapchainKHR> mCreateSwapchainKHR;
170 VkPtr<PFN_vkDestroySwapchainKHR> mDestroySwapchainKHR;
171 VkPtr<PFN_vkGetSwapchainImagesKHR> mGetSwapchainImagesKHR;
172 VkPtr<PFN_vkAcquireNextImageKHR> mAcquireNextImageKHR;
173 VkPtr<PFN_vkQueuePresentKHR> mQueuePresentKHR;
174 VkPtr<PFN_vkCreateSharedSwapchainsKHR> mCreateSharedSwapchainsKHR;
175
Greg Daniel2ff202712018-06-14 11:50:10 -0400176 // Instance Functions
Greg Daniela227dbb2018-08-20 09:19:48 -0400177 VkPtr<PFN_vkEnumerateInstanceVersion> mEnumerateInstanceVersion;
Greg Daniel2ff202712018-06-14 11:50:10 -0400178 VkPtr<PFN_vkEnumerateInstanceExtensionProperties> mEnumerateInstanceExtensionProperties;
179 VkPtr<PFN_vkCreateInstance> mCreateInstance;
180
181 VkPtr<PFN_vkDestroyInstance> mDestroyInstance;
182 VkPtr<PFN_vkEnumeratePhysicalDevices> mEnumeratePhysicalDevices;
183 VkPtr<PFN_vkGetPhysicalDeviceQueueFamilyProperties> mGetPhysicalDeviceQueueFamilyProperties;
Greg Daniela227dbb2018-08-20 09:19:48 -0400184 VkPtr<PFN_vkGetPhysicalDeviceFeatures2> mGetPhysicalDeviceFeatures2;
Greg Daniel2ff202712018-06-14 11:50:10 -0400185 VkPtr<PFN_vkCreateDevice> mCreateDevice;
186 VkPtr<PFN_vkEnumerateDeviceExtensionProperties> mEnumerateDeviceExtensionProperties;
187
188 // Device Functions
189 VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue;
190 VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle;
191 VkPtr<PFN_vkDestroyDevice> mDestroyDevice;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500192 VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool;
193 VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool;
194 VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers;
195 VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers;
196 VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer;
197 VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer;
198 VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer;
199 VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier;
200
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500201 VkPtr<PFN_vkQueueSubmit> mQueueSubmit;
202 VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500203
204 VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore;
205 VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore;
Greg Daniel26e0dca2018-09-18 10:33:19 -0400206 VkPtr<PFN_vkImportSemaphoreFdKHR> mImportSemaphoreFdKHR;
207 VkPtr<PFN_vkGetSemaphoreFdKHR> mGetSemaphoreFdKHR;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500208 VkPtr<PFN_vkCreateFence> mCreateFence;
209 VkPtr<PFN_vkDestroyFence> mDestroyFence;
210 VkPtr<PFN_vkWaitForFences> mWaitForFences;
211 VkPtr<PFN_vkResetFences> mResetFences;
212
213 RenderThread& mRenderThread;
214
Greg Daniel2ff202712018-06-14 11:50:10 -0400215 VkInstance mInstance = VK_NULL_HANDLE;
216 VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
217 VkDevice mDevice = VK_NULL_HANDLE;
218
219 uint32_t mGraphicsQueueIndex;
220 VkQueue mGraphicsQueue = VK_NULL_HANDLE;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500221 uint32_t mPresentQueueIndex;
222 VkQueue mPresentQueue = VK_NULL_HANDLE;
223 VkCommandPool mCommandPool = VK_NULL_HANDLE;
Greg Danielcd558522016-11-17 13:31:40 -0500224
Greg Daniel26e0dca2018-09-18 10:33:19 -0400225 VkCommandBuffer mDummyCB = VK_NULL_HANDLE;
226
Greg Danielcd558522016-11-17 13:31:40 -0500227 enum class SwapBehavior {
228 Discard,
229 BufferAge,
230 };
231 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500232};
233
234} /* namespace renderthread */
235} /* namespace uirenderer */
236} /* namespace android */
237
238#endif /* VULKANMANAGER_H */