blob: f0e33206a5761a4ec89805e8fca42be7285ad14f [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 {
40 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 mTransitionCmdBuffers[2]; // to transition layout between present and render
44 // We use these fences to make sure the above Command buffers have finished their work
45 // before attempting to reuse them or destroy them.
46 VkFence mUsageFences[2];
47 };
48
49 sk_sp<SkSurface> mBackbuffer;
50
51 VkSurfaceKHR mVkSurface = VK_NULL_HANDLE;
52 VkSwapchainKHR mSwapchain = VK_NULL_HANDLE;
53
54 BackbufferInfo* mBackbuffers;
55 uint32_t mCurrentBackbufferIndex;
56
57 uint32_t mImageCount;
58 VkImage* mImages;
59 VkImageLayout* mImageLayouts;
60 sk_sp<SkSurface>* mSurfaces;
61};
62
63// This class contains the shared global Vulkan objects, such as VkInstance, VkDevice and VkQueue,
64// which are re-used by CanvasContext. This class is created once and should be used by all vulkan
65// windowing contexts. The VulkanManager must be initialized before use.
66class VulkanManager {
67public:
68 // Sets up the vulkan context that is shared amonst all clients of the VulkanManager. This must
69 // be call once before use of the VulkanManager. Multiple calls after the first will simiply
70 // return.
71 void initialize();
72
73 // Quick check to see if the VulkanManager has been initialized.
74 bool hasVkContext() { return mBackendContext.get() != nullptr; }
75
76 // Given a window this creates a new VkSurfaceKHR and VkSwapchain and stores them inside a new
77 // VulkanSurface object which is returned.
78 VulkanSurface* createSurface(ANativeWindow* window);
79
80 // Destroy the VulkanSurface and all associated vulkan objects.
81 void destroySurface(VulkanSurface* surface);
82
83 // Cleans up all the global state in the VulkanManger.
84 void destroy();
85
86 // No work is needed to make a VulkanSurface current, and all functions require that a
87 // VulkanSurface is passed into them so we just return true here.
88 bool isCurrent(VulkanSurface* surface) { return true; }
89
90 // Returns an SkSurface which wraps the next image returned from vkAcquireNextImageKHR. It also
91 // will transition the VkImage from a present layout to color attachment so that it can be used
92 // by the client for drawing.
93 SkSurface* getBackbufferSurface(VulkanSurface* surface);
94
95 // Presents the current VkImage.
96 void swapBuffers(VulkanSurface* surface);
97
98private:
99 friend class RenderThread;
100
101 explicit VulkanManager(RenderThread& thread);
102 ~VulkanManager() { destroy(); }
103
104 void destroyBuffers(VulkanSurface* surface);
105
106 bool createSwapchain(VulkanSurface* surface);
107 void createBuffers(VulkanSurface* surface, VkFormat format, VkExtent2D extent);
108
109 VulkanSurface::BackbufferInfo* getAvailableBackbuffer(VulkanSurface* surface);
110
111 // simple wrapper class that exists only to initialize a pointer to NULL
112 template <typename FNPTR_TYPE> class VkPtr {
113 public:
114 VkPtr() : fPtr(NULL) {}
115 VkPtr operator=(FNPTR_TYPE ptr) { fPtr = ptr; return *this; }
116 operator FNPTR_TYPE() const { return fPtr; }
117 private:
118 FNPTR_TYPE fPtr;
119 };
120
121 // WSI interface functions
122 VkPtr<PFN_vkCreateAndroidSurfaceKHR> mCreateAndroidSurfaceKHR;
123 VkPtr<PFN_vkDestroySurfaceKHR> mDestroySurfaceKHR;
124 VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> mGetPhysicalDeviceSurfaceSupportKHR;
125 VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> mGetPhysicalDeviceSurfaceCapabilitiesKHR;
126 VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> mGetPhysicalDeviceSurfaceFormatsKHR;
127 VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> mGetPhysicalDeviceSurfacePresentModesKHR;
128
129 VkPtr<PFN_vkCreateSwapchainKHR> mCreateSwapchainKHR;
130 VkPtr<PFN_vkDestroySwapchainKHR> mDestroySwapchainKHR;
131 VkPtr<PFN_vkGetSwapchainImagesKHR> mGetSwapchainImagesKHR;
132 VkPtr<PFN_vkAcquireNextImageKHR> mAcquireNextImageKHR;
133 VkPtr<PFN_vkQueuePresentKHR> mQueuePresentKHR;
134 VkPtr<PFN_vkCreateSharedSwapchainsKHR> mCreateSharedSwapchainsKHR;
135
136 // Additional vulkan functions
137 VkPtr<PFN_vkCreateCommandPool> mCreateCommandPool;
138 VkPtr<PFN_vkDestroyCommandPool> mDestroyCommandPool;
139 VkPtr<PFN_vkAllocateCommandBuffers> mAllocateCommandBuffers;
140 VkPtr<PFN_vkFreeCommandBuffers> mFreeCommandBuffers;
141 VkPtr<PFN_vkResetCommandBuffer> mResetCommandBuffer;
142 VkPtr<PFN_vkBeginCommandBuffer> mBeginCommandBuffer;
143 VkPtr<PFN_vkEndCommandBuffer> mEndCommandBuffer;
144 VkPtr<PFN_vkCmdPipelineBarrier> mCmdPipelineBarrier;
145
146 VkPtr<PFN_vkGetDeviceQueue> mGetDeviceQueue;
147 VkPtr<PFN_vkQueueSubmit> mQueueSubmit;
148 VkPtr<PFN_vkQueueWaitIdle> mQueueWaitIdle;
149 VkPtr<PFN_vkDeviceWaitIdle> mDeviceWaitIdle;
150
151 VkPtr<PFN_vkCreateSemaphore> mCreateSemaphore;
152 VkPtr<PFN_vkDestroySemaphore> mDestroySemaphore;
153 VkPtr<PFN_vkCreateFence> mCreateFence;
154 VkPtr<PFN_vkDestroyFence> mDestroyFence;
155 VkPtr<PFN_vkWaitForFences> mWaitForFences;
156 VkPtr<PFN_vkResetFences> mResetFences;
157
158 RenderThread& mRenderThread;
159
160 sk_sp<const GrVkBackendContext> mBackendContext;
161 uint32_t mPresentQueueIndex;
162 VkQueue mPresentQueue = VK_NULL_HANDLE;
163 VkCommandPool mCommandPool = VK_NULL_HANDLE;
164};
165
166} /* namespace renderthread */
167} /* namespace uirenderer */
168} /* namespace android */
169
170#endif /* VULKANMANAGER_H */
171