blob: ea6970469e0306c11006c1a2828f2a5524f19ef4 [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
20#include <SkSurface.h>
21#include <vk/GrVkBackendContext.h>
22
23#include <vulkan/vulkan.h>
24
25namespace android {
26namespace uirenderer {
27namespace renderthread {
28
29class RenderThread;
30
31class VulkanSurface {
32public:
33 VulkanSurface() {}
34
35 sk_sp<SkSurface> getBackBufferSurface() { return mBackbuffer; }
36
37private:
38 friend class VulkanManager;
39 struct BackbufferInfo {
John Reck1bcacfd2017-11-03 10:12:19 -070040 uint32_t mImageIndex; // image this is associated with
41 VkSemaphore mAcquireSemaphore; // we signal on this for acquisition of image
42 VkSemaphore mRenderSemaphore; // we wait on this for rendering to be done
43 VkCommandBuffer
44 mTransitionCmdBuffers[2]; // to transition layout between present and render
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050045 // We use these fences to make sure the above Command buffers have finished their work
46 // before attempting to reuse them or destroy them.
John Reck1bcacfd2017-11-03 10:12:19 -070047 VkFence mUsageFences[2];
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050048 };
49
Greg Danielcd558522016-11-17 13:31:40 -050050 struct ImageInfo {
51 VkImageLayout mImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
52 sk_sp<SkSurface> mSurface;
53 uint16_t mLastUsed = 0;
54 bool mInvalid = true;
55 };
56
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050057 sk_sp<SkSurface> mBackbuffer;
58
59 VkSurfaceKHR mVkSurface = VK_NULL_HANDLE;
60 VkSwapchainKHR mSwapchain = VK_NULL_HANDLE;
61
Greg Daniel74ea2012017-11-10 11:32:58 -050062 BackbufferInfo* mBackbuffers = nullptr;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050063 uint32_t mCurrentBackbufferIndex;
64
65 uint32_t mImageCount;
Greg Daniel74ea2012017-11-10 11:32:58 -050066 VkImage* mImages = nullptr;
Greg Danielcd558522016-11-17 13:31:40 -050067 ImageInfo* mImageInfos;
68 uint16_t mCurrentTime = 0;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -050069};
70
71// This class contains the shared global Vulkan objects, such as VkInstance, VkDevice and VkQueue,
72// which are re-used by CanvasContext. This class is created once and should be used by all vulkan
73// windowing contexts. The VulkanManager must be initialized before use.
74class VulkanManager {
75public:
76 // Sets up the vulkan context that is shared amonst all clients of the VulkanManager. This must
77 // be call once before use of the VulkanManager. Multiple calls after the first will simiply
78 // return.
79 void initialize();
80
81 // Quick check to see if the VulkanManager has been initialized.
82 bool hasVkContext() { return mBackendContext.get() != nullptr; }
83
84 // Given a window this creates a new VkSurfaceKHR and VkSwapchain and stores them inside a new
85 // VulkanSurface object which is returned.
86 VulkanSurface* createSurface(ANativeWindow* window);
87
88 // Destroy the VulkanSurface and all associated vulkan objects.
89 void destroySurface(VulkanSurface* surface);
90
91 // Cleans up all the global state in the VulkanManger.
92 void destroy();
93
94 // No work is needed to make a VulkanSurface current, and all functions require that a
95 // VulkanSurface is passed into them so we just return true here.
96 bool isCurrent(VulkanSurface* surface) { return true; }
97
Greg Danielcd558522016-11-17 13:31:40 -050098 int getAge(VulkanSurface* surface);
99
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500100 // Returns an SkSurface which wraps the next image returned from vkAcquireNextImageKHR. It also
101 // will transition the VkImage from a present layout to color attachment so that it can be used
102 // by the client for drawing.
103 SkSurface* getBackbufferSurface(VulkanSurface* surface);
104
105 // Presents the current VkImage.
106 void swapBuffers(VulkanSurface* surface);
107
108private:
109 friend class RenderThread;
110
111 explicit VulkanManager(RenderThread& thread);
112 ~VulkanManager() { destroy(); }
113
114 void destroyBuffers(VulkanSurface* surface);
115
116 bool createSwapchain(VulkanSurface* surface);
117 void createBuffers(VulkanSurface* surface, VkFormat format, VkExtent2D extent);
118
119 VulkanSurface::BackbufferInfo* getAvailableBackbuffer(VulkanSurface* surface);
120
121 // simple wrapper class that exists only to initialize a pointer to NULL
John Reck1bcacfd2017-11-03 10:12:19 -0700122 template <typename FNPTR_TYPE>
123 class VkPtr {
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500124 public:
125 VkPtr() : fPtr(NULL) {}
John Reck1bcacfd2017-11-03 10:12:19 -0700126 VkPtr operator=(FNPTR_TYPE ptr) {
127 fPtr = ptr;
128 return *this;
129 }
Chih-Hung Hsiehd736d4b2018-12-20 13:55:20 -0800130 // NOLINTNEXTLINE(google-explicit-constructor)
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500131 operator FNPTR_TYPE() const { return fPtr; }
John Reck1bcacfd2017-11-03 10:12:19 -0700132
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500133 private:
134 FNPTR_TYPE fPtr;
135 };
136
137 // WSI interface functions
138 VkPtr<PFN_vkCreateAndroidSurfaceKHR> mCreateAndroidSurfaceKHR;
139 VkPtr<PFN_vkDestroySurfaceKHR> mDestroySurfaceKHR;
140 VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> mGetPhysicalDeviceSurfaceSupportKHR;
141 VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> mGetPhysicalDeviceSurfaceCapabilitiesKHR;
142 VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> mGetPhysicalDeviceSurfaceFormatsKHR;
143 VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> mGetPhysicalDeviceSurfacePresentModesKHR;
144
145 VkPtr<PFN_vkCreateSwapchainKHR> mCreateSwapchainKHR;
146 VkPtr<PFN_vkDestroySwapchainKHR> mDestroySwapchainKHR;
147 VkPtr<PFN_vkGetSwapchainImagesKHR> mGetSwapchainImagesKHR;
148 VkPtr<PFN_vkAcquireNextImageKHR> mAcquireNextImageKHR;
149 VkPtr<PFN_vkQueuePresentKHR> mQueuePresentKHR;
150 VkPtr<PFN_vkCreateSharedSwapchainsKHR> mCreateSharedSwapchainsKHR;
151
152 // Additional vulkan functions
153 VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool;
154 VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool;
155 VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers;
156 VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers;
157 VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer;
158 VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer;
159 VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer;
160 VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier;
161
162 VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue;
163 VkPtr<PFN_vkQueueSubmit> mQueueSubmit;
164 VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle;
165 VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle;
166
167 VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore;
168 VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore;
169 VkPtr<PFN_vkCreateFence> mCreateFence;
170 VkPtr<PFN_vkDestroyFence> mDestroyFence;
171 VkPtr<PFN_vkWaitForFences> mWaitForFences;
172 VkPtr<PFN_vkResetFences> mResetFences;
173
174 RenderThread& mRenderThread;
175
176 sk_sp<const GrVkBackendContext> mBackendContext;
177 uint32_t mPresentQueueIndex;
178 VkQueue mPresentQueue = VK_NULL_HANDLE;
179 VkCommandPool mCommandPool = VK_NULL_HANDLE;
Greg Danielcd558522016-11-17 13:31:40 -0500180
181 enum class SwapBehavior {
182 Discard,
183 BufferAge,
184 };
185 SwapBehavior mSwapBehavior = SwapBehavior::Discard;
Derek Sollenberger0e3cba32016-11-09 11:58:36 -0500186};
187
188} /* namespace renderthread */
189} /* namespace uirenderer */
190} /* namespace android */
191
192#endif /* VULKANMANAGER_H */