Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 1 | /* |
| 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 | #include "VulkanManager.h" |
| 18 | |
| 19 | #include "DeviceInfo.h" |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 20 | #include "Properties.h" |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 21 | #include "RenderThread.h" |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 22 | #include "renderstate/RenderState.h" |
Ben Wagner | eec27d5 | 2017-01-11 15:32:07 -0500 | [diff] [blame] | 23 | #include "utils/FatVector.h" |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 24 | |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 25 | #include <GrBackendSurface.h> |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 26 | #include <GrContext.h> |
| 27 | #include <GrTypes.h> |
| 28 | #include <vk/GrVkTypes.h> |
| 29 | |
| 30 | namespace android { |
| 31 | namespace uirenderer { |
| 32 | namespace renderthread { |
| 33 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 34 | #define GET_PROC(F) m##F = (PFN_vk##F)vkGetInstanceProcAddr(instance, "vk" #F) |
| 35 | #define GET_DEV_PROC(F) m##F = (PFN_vk##F)vkGetDeviceProcAddr(device, "vk" #F) |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 36 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 37 | VulkanManager::VulkanManager(RenderThread& thread) : mRenderThread(thread) {} |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 38 | |
| 39 | void VulkanManager::destroy() { |
| 40 | if (!hasVkContext()) return; |
| 41 | |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 42 | mRenderThread.renderState().onVkContextDestroyed(); |
| 43 | mRenderThread.setGrContext(nullptr); |
| 44 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 45 | if (VK_NULL_HANDLE != mCommandPool) { |
| 46 | mDestroyCommandPool(mBackendContext->fDevice, mCommandPool, nullptr); |
| 47 | mCommandPool = VK_NULL_HANDLE; |
| 48 | } |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 49 | mBackendContext.reset(); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void VulkanManager::initialize() { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 53 | if (hasVkContext()) { |
| 54 | return; |
| 55 | } |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 56 | |
| 57 | auto canPresent = [](VkInstance, VkPhysicalDevice, uint32_t) { return true; }; |
| 58 | |
Greg Daniel | 53a3543 | 2017-04-25 13:44:00 -0400 | [diff] [blame] | 59 | mBackendContext.reset(GrVkBackendContext::Create(vkGetInstanceProcAddr, vkGetDeviceProcAddr, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 60 | &mPresentQueueIndex, canPresent)); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 61 | |
| 62 | // Get all the addresses of needed vulkan functions |
| 63 | VkInstance instance = mBackendContext->fInstance; |
| 64 | VkDevice device = mBackendContext->fDevice; |
| 65 | GET_PROC(CreateAndroidSurfaceKHR); |
| 66 | GET_PROC(DestroySurfaceKHR); |
| 67 | GET_PROC(GetPhysicalDeviceSurfaceSupportKHR); |
| 68 | GET_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR); |
| 69 | GET_PROC(GetPhysicalDeviceSurfaceFormatsKHR); |
| 70 | GET_PROC(GetPhysicalDeviceSurfacePresentModesKHR); |
| 71 | GET_DEV_PROC(CreateSwapchainKHR); |
| 72 | GET_DEV_PROC(DestroySwapchainKHR); |
| 73 | GET_DEV_PROC(GetSwapchainImagesKHR); |
| 74 | GET_DEV_PROC(AcquireNextImageKHR); |
| 75 | GET_DEV_PROC(QueuePresentKHR); |
| 76 | GET_DEV_PROC(CreateCommandPool); |
| 77 | GET_DEV_PROC(DestroyCommandPool); |
| 78 | GET_DEV_PROC(AllocateCommandBuffers); |
| 79 | GET_DEV_PROC(FreeCommandBuffers); |
| 80 | GET_DEV_PROC(ResetCommandBuffer); |
| 81 | GET_DEV_PROC(BeginCommandBuffer); |
| 82 | GET_DEV_PROC(EndCommandBuffer); |
| 83 | GET_DEV_PROC(CmdPipelineBarrier); |
| 84 | GET_DEV_PROC(GetDeviceQueue); |
| 85 | GET_DEV_PROC(QueueSubmit); |
| 86 | GET_DEV_PROC(QueueWaitIdle); |
| 87 | GET_DEV_PROC(DeviceWaitIdle); |
| 88 | GET_DEV_PROC(CreateSemaphore); |
| 89 | GET_DEV_PROC(DestroySemaphore); |
| 90 | GET_DEV_PROC(CreateFence); |
| 91 | GET_DEV_PROC(DestroyFence); |
| 92 | GET_DEV_PROC(WaitForFences); |
| 93 | GET_DEV_PROC(ResetFences); |
| 94 | |
| 95 | // create the command pool for the command buffers |
| 96 | if (VK_NULL_HANDLE == mCommandPool) { |
| 97 | VkCommandPoolCreateInfo commandPoolInfo; |
| 98 | memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo)); |
| 99 | commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; |
| 100 | // this needs to be on the render queue |
| 101 | commandPoolInfo.queueFamilyIndex = mBackendContext->fGraphicsQueueIndex; |
| 102 | commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 103 | SkDEBUGCODE(VkResult res =) mCreateCommandPool(mBackendContext->fDevice, &commandPoolInfo, |
| 104 | nullptr, &mCommandPool); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 105 | SkASSERT(VK_SUCCESS == res); |
| 106 | } |
| 107 | |
| 108 | mGetDeviceQueue(mBackendContext->fDevice, mPresentQueueIndex, 0, &mPresentQueue); |
| 109 | |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 110 | GrContextOptions options; |
| 111 | options.fDisableDistanceFieldPaths = true; |
| 112 | mRenderThread.cacheManager().configureContext(&options); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 113 | mRenderThread.setGrContext( |
Stan Iliev | d495f43 | 2017-10-09 15:49:32 -0400 | [diff] [blame] | 114 | GrContext::Create(kVulkan_GrBackend, (GrBackendContext)mBackendContext.get(), options)); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 115 | DeviceInfo::initialize(mRenderThread.getGrContext()->caps()->maxRenderTargetSize()); |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 116 | |
| 117 | if (Properties::enablePartialUpdates && Properties::useBufferAge) { |
| 118 | mSwapBehavior = SwapBehavior::BufferAge; |
| 119 | } |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 120 | |
| 121 | mRenderThread.renderState().onVkContextCreated(); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // Returns the next BackbufferInfo to use for the next draw. The function will make sure all |
| 125 | // previous uses have finished before returning. |
| 126 | VulkanSurface::BackbufferInfo* VulkanManager::getAvailableBackbuffer(VulkanSurface* surface) { |
| 127 | SkASSERT(surface->mBackbuffers); |
| 128 | |
| 129 | ++surface->mCurrentBackbufferIndex; |
| 130 | if (surface->mCurrentBackbufferIndex > surface->mImageCount) { |
| 131 | surface->mCurrentBackbufferIndex = 0; |
| 132 | } |
| 133 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 134 | VulkanSurface::BackbufferInfo* backbuffer = |
| 135 | surface->mBackbuffers + surface->mCurrentBackbufferIndex; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 136 | |
| 137 | // Before we reuse a backbuffer, make sure its fences have all signaled so that we can safely |
| 138 | // reuse its commands buffers. |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 139 | VkResult res = |
| 140 | mWaitForFences(mBackendContext->fDevice, 2, backbuffer->mUsageFences, true, UINT64_MAX); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 141 | if (res != VK_SUCCESS) { |
| 142 | return nullptr; |
| 143 | } |
| 144 | |
| 145 | return backbuffer; |
| 146 | } |
| 147 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 148 | SkSurface* VulkanManager::getBackbufferSurface(VulkanSurface* surface) { |
| 149 | VulkanSurface::BackbufferInfo* backbuffer = getAvailableBackbuffer(surface); |
| 150 | SkASSERT(backbuffer); |
| 151 | |
| 152 | VkResult res; |
| 153 | |
| 154 | res = mResetFences(mBackendContext->fDevice, 2, backbuffer->mUsageFences); |
| 155 | SkASSERT(VK_SUCCESS == res); |
| 156 | |
| 157 | // The acquire will signal the attached mAcquireSemaphore. We use this to know the image has |
| 158 | // finished presenting and that it is safe to begin sending new commands to the returned image. |
| 159 | res = mAcquireNextImageKHR(mBackendContext->fDevice, surface->mSwapchain, UINT64_MAX, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 160 | backbuffer->mAcquireSemaphore, VK_NULL_HANDLE, |
| 161 | &backbuffer->mImageIndex); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 162 | |
| 163 | if (VK_ERROR_SURFACE_LOST_KHR == res) { |
| 164 | // need to figure out how to create a new vkSurface without the platformData* |
| 165 | // maybe use attach somehow? but need a Window |
| 166 | return nullptr; |
| 167 | } |
| 168 | if (VK_ERROR_OUT_OF_DATE_KHR == res) { |
| 169 | // tear swapchain down and try again |
| 170 | if (!createSwapchain(surface)) { |
| 171 | return nullptr; |
| 172 | } |
Greg Daniel | 45ec62b | 2017-01-04 14:27:00 -0500 | [diff] [blame] | 173 | backbuffer = getAvailableBackbuffer(surface); |
| 174 | res = mResetFences(mBackendContext->fDevice, 2, backbuffer->mUsageFences); |
| 175 | SkASSERT(VK_SUCCESS == res); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 176 | |
| 177 | // acquire the image |
| 178 | res = mAcquireNextImageKHR(mBackendContext->fDevice, surface->mSwapchain, UINT64_MAX, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 179 | backbuffer->mAcquireSemaphore, VK_NULL_HANDLE, |
| 180 | &backbuffer->mImageIndex); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 181 | |
| 182 | if (VK_SUCCESS != res) { |
| 183 | return nullptr; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // set up layout transfer from initial to color attachment |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 188 | VkImageLayout layout = surface->mImageInfos[backbuffer->mImageIndex].mImageLayout; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 189 | SkASSERT(VK_IMAGE_LAYOUT_UNDEFINED == layout || VK_IMAGE_LAYOUT_PRESENT_SRC_KHR == layout); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 190 | VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) |
| 191 | ? VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT |
| 192 | : VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 193 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 194 | VkAccessFlags srcAccessMask = |
| 195 | (VK_IMAGE_LAYOUT_UNDEFINED == layout) ? 0 : VK_ACCESS_MEMORY_READ_BIT; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 196 | VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
| 197 | |
| 198 | VkImageMemoryBarrier imageMemoryBarrier = { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 199 | VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType |
| 200 | NULL, // pNext |
| 201 | srcAccessMask, // outputMask |
| 202 | dstAccessMask, // inputMask |
| 203 | layout, // oldLayout |
| 204 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout |
| 205 | mPresentQueueIndex, // srcQueueFamilyIndex |
| 206 | mBackendContext->fGraphicsQueueIndex, // dstQueueFamilyIndex |
| 207 | surface->mImages[backbuffer->mImageIndex], // image |
| 208 | {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1} // subresourceRange |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 209 | }; |
| 210 | mResetCommandBuffer(backbuffer->mTransitionCmdBuffers[0], 0); |
| 211 | |
| 212 | VkCommandBufferBeginInfo info; |
| 213 | memset(&info, 0, sizeof(VkCommandBufferBeginInfo)); |
| 214 | info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 215 | info.flags = 0; |
| 216 | mBeginCommandBuffer(backbuffer->mTransitionCmdBuffers[0], &info); |
| 217 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 218 | mCmdPipelineBarrier(backbuffer->mTransitionCmdBuffers[0], srcStageMask, dstStageMask, 0, 0, |
| 219 | nullptr, 0, nullptr, 1, &imageMemoryBarrier); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 220 | |
| 221 | mEndCommandBuffer(backbuffer->mTransitionCmdBuffers[0]); |
| 222 | |
| 223 | VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT; |
| 224 | // insert the layout transfer into the queue and wait on the acquire |
| 225 | VkSubmitInfo submitInfo; |
| 226 | memset(&submitInfo, 0, sizeof(VkSubmitInfo)); |
| 227 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 228 | submitInfo.waitSemaphoreCount = 1; |
| 229 | // Wait to make sure aquire semaphore set above has signaled. |
| 230 | submitInfo.pWaitSemaphores = &backbuffer->mAcquireSemaphore; |
| 231 | submitInfo.pWaitDstStageMask = &waitDstStageFlags; |
| 232 | submitInfo.commandBufferCount = 1; |
| 233 | submitInfo.pCommandBuffers = &backbuffer->mTransitionCmdBuffers[0]; |
| 234 | submitInfo.signalSemaphoreCount = 0; |
| 235 | |
| 236 | // Attach first fence to submission here so we can track when the command buffer finishes. |
| 237 | mQueueSubmit(mBackendContext->fQueue, 1, &submitInfo, backbuffer->mUsageFences[0]); |
| 238 | |
| 239 | // We need to notify Skia that we changed the layout of the wrapped VkImage |
| 240 | GrVkImageInfo* imageInfo; |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 241 | sk_sp<SkSurface> skSurface = surface->mImageInfos[backbuffer->mImageIndex].mSurface; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 242 | skSurface->getRenderTargetHandle((GrBackendObject*)&imageInfo, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 243 | SkSurface::kFlushRead_BackendHandleAccess); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 244 | imageInfo->updateImageLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); |
| 245 | |
| 246 | surface->mBackbuffer = std::move(skSurface); |
| 247 | return surface->mBackbuffer.get(); |
| 248 | } |
| 249 | |
| 250 | void VulkanManager::destroyBuffers(VulkanSurface* surface) { |
| 251 | if (surface->mBackbuffers) { |
| 252 | for (uint32_t i = 0; i < surface->mImageCount + 1; ++i) { |
| 253 | mWaitForFences(mBackendContext->fDevice, 2, surface->mBackbuffers[i].mUsageFences, true, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 254 | UINT64_MAX); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 255 | surface->mBackbuffers[i].mImageIndex = -1; |
| 256 | mDestroySemaphore(mBackendContext->fDevice, surface->mBackbuffers[i].mAcquireSemaphore, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 257 | nullptr); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 258 | mDestroySemaphore(mBackendContext->fDevice, surface->mBackbuffers[i].mRenderSemaphore, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 259 | nullptr); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 260 | mFreeCommandBuffers(mBackendContext->fDevice, mCommandPool, 2, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 261 | surface->mBackbuffers[i].mTransitionCmdBuffers); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 262 | mDestroyFence(mBackendContext->fDevice, surface->mBackbuffers[i].mUsageFences[0], 0); |
| 263 | mDestroyFence(mBackendContext->fDevice, surface->mBackbuffers[i].mUsageFences[1], 0); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | delete[] surface->mBackbuffers; |
| 268 | surface->mBackbuffers = nullptr; |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 269 | delete[] surface->mImageInfos; |
| 270 | surface->mImageInfos = nullptr; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 271 | delete[] surface->mImages; |
| 272 | surface->mImages = nullptr; |
| 273 | } |
| 274 | |
| 275 | void VulkanManager::destroySurface(VulkanSurface* surface) { |
| 276 | // Make sure all submit commands have finished before starting to destroy objects. |
| 277 | if (VK_NULL_HANDLE != mPresentQueue) { |
| 278 | mQueueWaitIdle(mPresentQueue); |
| 279 | } |
| 280 | mDeviceWaitIdle(mBackendContext->fDevice); |
| 281 | |
| 282 | destroyBuffers(surface); |
| 283 | |
| 284 | if (VK_NULL_HANDLE != surface->mSwapchain) { |
| 285 | mDestroySwapchainKHR(mBackendContext->fDevice, surface->mSwapchain, nullptr); |
| 286 | surface->mSwapchain = VK_NULL_HANDLE; |
| 287 | } |
| 288 | |
| 289 | if (VK_NULL_HANDLE != surface->mVkSurface) { |
| 290 | mDestroySurfaceKHR(mBackendContext->fInstance, surface->mVkSurface, nullptr); |
| 291 | surface->mVkSurface = VK_NULL_HANDLE; |
| 292 | } |
| 293 | delete surface; |
| 294 | } |
| 295 | |
| 296 | void VulkanManager::createBuffers(VulkanSurface* surface, VkFormat format, VkExtent2D extent) { |
| 297 | mGetSwapchainImagesKHR(mBackendContext->fDevice, surface->mSwapchain, &surface->mImageCount, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 298 | nullptr); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 299 | SkASSERT(surface->mImageCount); |
| 300 | surface->mImages = new VkImage[surface->mImageCount]; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 301 | mGetSwapchainImagesKHR(mBackendContext->fDevice, surface->mSwapchain, &surface->mImageCount, |
| 302 | surface->mImages); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 303 | |
| 304 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
| 305 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 306 | // set up initial image layouts and create surfaces |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 307 | surface->mImageInfos = new VulkanSurface::ImageInfo[surface->mImageCount]; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 308 | for (uint32_t i = 0; i < surface->mImageCount; ++i) { |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 309 | GrVkImageInfo info; |
| 310 | info.fImage = surface->mImages[i]; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 311 | info.fAlloc = {VK_NULL_HANDLE, 0, 0, 0}; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 312 | info.fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 313 | info.fImageTiling = VK_IMAGE_TILING_OPTIMAL; |
| 314 | info.fFormat = format; |
| 315 | info.fLevelCount = 1; |
| 316 | |
Greg Daniel | ac2d232 | 2017-07-12 11:30:15 -0400 | [diff] [blame] | 317 | GrBackendRenderTarget backendRT(extent.width, extent.height, 0, 0, info); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 318 | |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 319 | VulkanSurface::ImageInfo& imageInfo = surface->mImageInfos[i]; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 320 | imageInfo.mSurface = SkSurface::MakeFromBackendRenderTarget( |
| 321 | mRenderThread.getGrContext(), backendRT, kTopLeft_GrSurfaceOrigin, nullptr, &props); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | SkASSERT(mCommandPool != VK_NULL_HANDLE); |
| 325 | |
| 326 | // set up the backbuffers |
| 327 | VkSemaphoreCreateInfo semaphoreInfo; |
| 328 | memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo)); |
| 329 | semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; |
| 330 | semaphoreInfo.pNext = nullptr; |
| 331 | semaphoreInfo.flags = 0; |
| 332 | VkCommandBufferAllocateInfo commandBuffersInfo; |
| 333 | memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo)); |
| 334 | commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; |
| 335 | commandBuffersInfo.pNext = nullptr; |
| 336 | commandBuffersInfo.commandPool = mCommandPool; |
| 337 | commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; |
| 338 | commandBuffersInfo.commandBufferCount = 2; |
| 339 | VkFenceCreateInfo fenceInfo; |
| 340 | memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo)); |
| 341 | fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 342 | fenceInfo.pNext = nullptr; |
| 343 | fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT; |
| 344 | |
| 345 | // we create one additional backbuffer structure here, because we want to |
| 346 | // give the command buffers they contain a chance to finish before we cycle back |
| 347 | surface->mBackbuffers = new VulkanSurface::BackbufferInfo[surface->mImageCount + 1]; |
| 348 | for (uint32_t i = 0; i < surface->mImageCount + 1; ++i) { |
| 349 | SkDEBUGCODE(VkResult res); |
| 350 | surface->mBackbuffers[i].mImageIndex = -1; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 351 | SkDEBUGCODE(res =) mCreateSemaphore(mBackendContext->fDevice, &semaphoreInfo, nullptr, |
| 352 | &surface->mBackbuffers[i].mAcquireSemaphore); |
| 353 | SkDEBUGCODE(res =) mCreateSemaphore(mBackendContext->fDevice, &semaphoreInfo, nullptr, |
| 354 | &surface->mBackbuffers[i].mRenderSemaphore); |
| 355 | SkDEBUGCODE(res =) mAllocateCommandBuffers(mBackendContext->fDevice, &commandBuffersInfo, |
| 356 | surface->mBackbuffers[i].mTransitionCmdBuffers); |
| 357 | SkDEBUGCODE(res =) mCreateFence(mBackendContext->fDevice, &fenceInfo, nullptr, |
| 358 | &surface->mBackbuffers[i].mUsageFences[0]); |
| 359 | SkDEBUGCODE(res =) mCreateFence(mBackendContext->fDevice, &fenceInfo, nullptr, |
| 360 | &surface->mBackbuffers[i].mUsageFences[1]); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 361 | SkASSERT(VK_SUCCESS == res); |
| 362 | } |
| 363 | surface->mCurrentBackbufferIndex = surface->mImageCount; |
| 364 | } |
| 365 | |
| 366 | bool VulkanManager::createSwapchain(VulkanSurface* surface) { |
| 367 | // check for capabilities |
| 368 | VkSurfaceCapabilitiesKHR caps; |
| 369 | VkResult res = mGetPhysicalDeviceSurfaceCapabilitiesKHR(mBackendContext->fPhysicalDevice, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 370 | surface->mVkSurface, &caps); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 371 | if (VK_SUCCESS != res) { |
| 372 | return false; |
| 373 | } |
| 374 | |
| 375 | uint32_t surfaceFormatCount; |
| 376 | res = mGetPhysicalDeviceSurfaceFormatsKHR(mBackendContext->fPhysicalDevice, surface->mVkSurface, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 377 | &surfaceFormatCount, nullptr); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 378 | if (VK_SUCCESS != res) { |
| 379 | return false; |
| 380 | } |
| 381 | |
Ben Wagner | eec27d5 | 2017-01-11 15:32:07 -0500 | [diff] [blame] | 382 | FatVector<VkSurfaceFormatKHR, 4> surfaceFormats(surfaceFormatCount); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 383 | res = mGetPhysicalDeviceSurfaceFormatsKHR(mBackendContext->fPhysicalDevice, surface->mVkSurface, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 384 | &surfaceFormatCount, surfaceFormats.data()); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 385 | if (VK_SUCCESS != res) { |
| 386 | return false; |
| 387 | } |
| 388 | |
| 389 | uint32_t presentModeCount; |
| 390 | res = mGetPhysicalDeviceSurfacePresentModesKHR(mBackendContext->fPhysicalDevice, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 391 | surface->mVkSurface, &presentModeCount, nullptr); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 392 | if (VK_SUCCESS != res) { |
| 393 | return false; |
| 394 | } |
| 395 | |
Ben Wagner | eec27d5 | 2017-01-11 15:32:07 -0500 | [diff] [blame] | 396 | FatVector<VkPresentModeKHR, VK_PRESENT_MODE_RANGE_SIZE_KHR> presentModes(presentModeCount); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 397 | res = mGetPhysicalDeviceSurfacePresentModesKHR(mBackendContext->fPhysicalDevice, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 398 | surface->mVkSurface, &presentModeCount, |
| 399 | presentModes.data()); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 400 | if (VK_SUCCESS != res) { |
| 401 | return false; |
| 402 | } |
| 403 | |
| 404 | VkExtent2D extent = caps.currentExtent; |
| 405 | // clamp width; to handle currentExtent of -1 and protect us from broken hints |
| 406 | if (extent.width < caps.minImageExtent.width) { |
| 407 | extent.width = caps.minImageExtent.width; |
| 408 | } |
| 409 | SkASSERT(extent.width <= caps.maxImageExtent.width); |
| 410 | // clamp height |
| 411 | if (extent.height < caps.minImageExtent.height) { |
| 412 | extent.height = caps.minImageExtent.height; |
| 413 | } |
| 414 | SkASSERT(extent.height <= caps.maxImageExtent.height); |
| 415 | |
| 416 | uint32_t imageCount = caps.minImageCount + 2; |
| 417 | if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) { |
| 418 | // Application must settle for fewer images than desired: |
| 419 | imageCount = caps.maxImageCount; |
| 420 | } |
| 421 | |
| 422 | // Currently Skia requires the images to be color attchments and support all transfer |
| 423 | // operations. |
| 424 | VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | |
| 425 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | |
| 426 | VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
| 427 | SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags); |
| 428 | SkASSERT(caps.supportedTransforms & caps.currentTransform); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 429 | SkASSERT(caps.supportedCompositeAlpha & |
| 430 | (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR | VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR)); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 431 | VkCompositeAlphaFlagBitsKHR composite_alpha = |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 432 | (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) |
| 433 | ? VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR |
| 434 | : VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 435 | |
| 436 | // Pick our surface format. For now, just make sure it matches our sRGB request: |
| 437 | VkFormat surfaceFormat = VK_FORMAT_UNDEFINED; |
| 438 | VkColorSpaceKHR colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR; |
| 439 | |
| 440 | bool wantSRGB = false; |
| 441 | #ifdef ANDROID_ENABLE_LINEAR_BLENDING |
| 442 | wantSRGB = true; |
| 443 | #endif |
| 444 | for (uint32_t i = 0; i < surfaceFormatCount; ++i) { |
| 445 | // We are assuming we can get either R8G8B8A8_UNORM or R8G8B8A8_SRGB |
| 446 | VkFormat desiredFormat = wantSRGB ? VK_FORMAT_R8G8B8A8_SRGB : VK_FORMAT_R8G8B8A8_UNORM; |
| 447 | if (desiredFormat == surfaceFormats[i].format) { |
| 448 | surfaceFormat = surfaceFormats[i].format; |
| 449 | colorSpace = surfaceFormats[i].colorSpace; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | if (VK_FORMAT_UNDEFINED == surfaceFormat) { |
| 454 | return false; |
| 455 | } |
| 456 | |
| 457 | // If mailbox mode is available, use it, as it is the lowest-latency non- |
| 458 | // tearing mode. If not, fall back to FIFO which is always available. |
| 459 | VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR; |
| 460 | for (uint32_t i = 0; i < presentModeCount; ++i) { |
| 461 | // use mailbox |
| 462 | if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) { |
| 463 | mode = presentModes[i]; |
| 464 | break; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | VkSwapchainCreateInfoKHR swapchainCreateInfo; |
| 469 | memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR)); |
| 470 | swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; |
| 471 | swapchainCreateInfo.surface = surface->mVkSurface; |
| 472 | swapchainCreateInfo.minImageCount = imageCount; |
| 473 | swapchainCreateInfo.imageFormat = surfaceFormat; |
| 474 | swapchainCreateInfo.imageColorSpace = colorSpace; |
| 475 | swapchainCreateInfo.imageExtent = extent; |
| 476 | swapchainCreateInfo.imageArrayLayers = 1; |
| 477 | swapchainCreateInfo.imageUsage = usageFlags; |
| 478 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 479 | uint32_t queueFamilies[] = {mBackendContext->fGraphicsQueueIndex, mPresentQueueIndex}; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 480 | if (mBackendContext->fGraphicsQueueIndex != mPresentQueueIndex) { |
| 481 | swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT; |
| 482 | swapchainCreateInfo.queueFamilyIndexCount = 2; |
| 483 | swapchainCreateInfo.pQueueFamilyIndices = queueFamilies; |
| 484 | } else { |
| 485 | swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 486 | swapchainCreateInfo.queueFamilyIndexCount = 0; |
| 487 | swapchainCreateInfo.pQueueFamilyIndices = nullptr; |
| 488 | } |
| 489 | |
| 490 | swapchainCreateInfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; |
| 491 | swapchainCreateInfo.compositeAlpha = composite_alpha; |
| 492 | swapchainCreateInfo.presentMode = mode; |
| 493 | swapchainCreateInfo.clipped = true; |
| 494 | swapchainCreateInfo.oldSwapchain = surface->mSwapchain; |
| 495 | |
| 496 | res = mCreateSwapchainKHR(mBackendContext->fDevice, &swapchainCreateInfo, nullptr, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 497 | &surface->mSwapchain); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 498 | if (VK_SUCCESS != res) { |
| 499 | return false; |
| 500 | } |
| 501 | |
| 502 | // destroy the old swapchain |
| 503 | if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) { |
| 504 | mDeviceWaitIdle(mBackendContext->fDevice); |
| 505 | |
| 506 | destroyBuffers(surface); |
| 507 | |
| 508 | mDestroySwapchainKHR(mBackendContext->fDevice, swapchainCreateInfo.oldSwapchain, nullptr); |
| 509 | } |
| 510 | |
| 511 | createBuffers(surface, surfaceFormat, extent); |
| 512 | |
| 513 | return true; |
| 514 | } |
| 515 | |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 516 | VulkanSurface* VulkanManager::createSurface(ANativeWindow* window) { |
| 517 | initialize(); |
| 518 | |
| 519 | if (!window) { |
| 520 | return nullptr; |
| 521 | } |
| 522 | |
| 523 | VulkanSurface* surface = new VulkanSurface(); |
| 524 | |
| 525 | VkAndroidSurfaceCreateInfoKHR surfaceCreateInfo; |
| 526 | memset(&surfaceCreateInfo, 0, sizeof(VkAndroidSurfaceCreateInfoKHR)); |
| 527 | surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR; |
| 528 | surfaceCreateInfo.pNext = nullptr; |
| 529 | surfaceCreateInfo.flags = 0; |
| 530 | surfaceCreateInfo.window = window; |
| 531 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 532 | VkResult res = mCreateAndroidSurfaceKHR(mBackendContext->fInstance, &surfaceCreateInfo, nullptr, |
| 533 | &surface->mVkSurface); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 534 | if (VK_SUCCESS != res) { |
| 535 | delete surface; |
| 536 | return nullptr; |
| 537 | } |
| 538 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 539 | SkDEBUGCODE(VkBool32 supported; res = mGetPhysicalDeviceSurfaceSupportKHR( |
| 540 | mBackendContext->fPhysicalDevice, mPresentQueueIndex, |
| 541 | surface->mVkSurface, &supported); |
| 542 | // All physical devices and queue families on Android must be capable of |
| 543 | // presentation with any |
| 544 | // native window. |
| 545 | SkASSERT(VK_SUCCESS == res && supported);); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 546 | |
| 547 | if (!createSwapchain(surface)) { |
| 548 | destroySurface(surface); |
| 549 | return nullptr; |
| 550 | } |
| 551 | |
| 552 | return surface; |
| 553 | } |
| 554 | |
| 555 | // Helper to know which src stage flags we need to set when transitioning to the present layout |
| 556 | static VkPipelineStageFlags layoutToPipelineStageFlags(const VkImageLayout layout) { |
| 557 | if (VK_IMAGE_LAYOUT_GENERAL == layout) { |
| 558 | return VK_PIPELINE_STAGE_ALL_COMMANDS_BIT; |
| 559 | } else if (VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == layout || |
| 560 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL == layout) { |
| 561 | return VK_PIPELINE_STAGE_TRANSFER_BIT; |
| 562 | } else if (VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL == layout || |
| 563 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL == layout || |
| 564 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL == layout || |
| 565 | VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == layout) { |
| 566 | return VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT; |
| 567 | } else if (VK_IMAGE_LAYOUT_PREINITIALIZED == layout) { |
| 568 | return VK_PIPELINE_STAGE_HOST_BIT; |
| 569 | } |
| 570 | |
| 571 | SkASSERT(VK_IMAGE_LAYOUT_UNDEFINED == layout); |
| 572 | return VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; |
| 573 | } |
| 574 | |
| 575 | // Helper to know which src access mask we need to set when transitioning to the present layout |
| 576 | static VkAccessFlags layoutToSrcAccessMask(const VkImageLayout layout) { |
| 577 | VkAccessFlags flags = 0; |
| 578 | if (VK_IMAGE_LAYOUT_GENERAL == layout) { |
| 579 | flags = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 580 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | VK_ACCESS_TRANSFER_WRITE_BIT | |
| 581 | VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_HOST_WRITE_BIT | |
| 582 | VK_ACCESS_HOST_READ_BIT; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 583 | } else if (VK_IMAGE_LAYOUT_PREINITIALIZED == layout) { |
| 584 | flags = VK_ACCESS_HOST_WRITE_BIT; |
| 585 | } else if (VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL == layout) { |
| 586 | flags = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT; |
| 587 | } else if (VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL == layout) { |
| 588 | flags = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT; |
| 589 | } else if (VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL == layout) { |
| 590 | flags = VK_ACCESS_TRANSFER_WRITE_BIT; |
| 591 | } else if (VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL == layout) { |
| 592 | flags = VK_ACCESS_TRANSFER_READ_BIT; |
| 593 | } else if (VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL == layout) { |
| 594 | flags = VK_ACCESS_SHADER_READ_BIT; |
| 595 | } |
| 596 | return flags; |
| 597 | } |
| 598 | |
| 599 | void VulkanManager::swapBuffers(VulkanSurface* surface) { |
Greg Daniel | 4f70887 | 2017-02-03 10:23:39 -0500 | [diff] [blame] | 600 | if (CC_UNLIKELY(Properties::waitForGpuCompletion)) { |
| 601 | ATRACE_NAME("Finishing GPU work"); |
| 602 | mDeviceWaitIdle(mBackendContext->fDevice); |
| 603 | } |
| 604 | |
Greg Daniel | 74ea201 | 2017-11-10 11:32:58 -0500 | [diff] [blame^] | 605 | SkASSERT(surface->mBackbuffers); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 606 | VulkanSurface::BackbufferInfo* backbuffer = |
| 607 | surface->mBackbuffers + surface->mCurrentBackbufferIndex; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 608 | GrVkImageInfo* imageInfo; |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 609 | SkSurface* skSurface = surface->mImageInfos[backbuffer->mImageIndex].mSurface.get(); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 610 | skSurface->getRenderTargetHandle((GrBackendObject*)&imageInfo, |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 611 | SkSurface::kFlushRead_BackendHandleAccess); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 612 | // Check to make sure we never change the actually wrapped image |
| 613 | SkASSERT(imageInfo->fImage == surface->mImages[backbuffer->mImageIndex]); |
| 614 | |
| 615 | // We need to transition the image to VK_IMAGE_LAYOUT_PRESENT_SRC_KHR and make sure that all |
| 616 | // previous work is complete for before presenting. So we first add the necessary barrier here. |
| 617 | VkImageLayout layout = imageInfo->fImageLayout; |
| 618 | VkPipelineStageFlags srcStageMask = layoutToPipelineStageFlags(layout); |
| 619 | VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT; |
| 620 | VkAccessFlags srcAccessMask = layoutToSrcAccessMask(layout); |
| 621 | VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT; |
| 622 | |
| 623 | VkImageMemoryBarrier imageMemoryBarrier = { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 624 | VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType |
| 625 | NULL, // pNext |
| 626 | srcAccessMask, // outputMask |
| 627 | dstAccessMask, // inputMask |
| 628 | layout, // oldLayout |
| 629 | VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout |
| 630 | mBackendContext->fGraphicsQueueIndex, // srcQueueFamilyIndex |
| 631 | mPresentQueueIndex, // dstQueueFamilyIndex |
| 632 | surface->mImages[backbuffer->mImageIndex], // image |
| 633 | {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1} // subresourceRange |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 634 | }; |
| 635 | |
| 636 | mResetCommandBuffer(backbuffer->mTransitionCmdBuffers[1], 0); |
| 637 | VkCommandBufferBeginInfo info; |
| 638 | memset(&info, 0, sizeof(VkCommandBufferBeginInfo)); |
| 639 | info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 640 | info.flags = 0; |
| 641 | mBeginCommandBuffer(backbuffer->mTransitionCmdBuffers[1], &info); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 642 | mCmdPipelineBarrier(backbuffer->mTransitionCmdBuffers[1], srcStageMask, dstStageMask, 0, 0, |
| 643 | nullptr, 0, nullptr, 1, &imageMemoryBarrier); |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 644 | mEndCommandBuffer(backbuffer->mTransitionCmdBuffers[1]); |
| 645 | |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 646 | surface->mImageInfos[backbuffer->mImageIndex].mImageLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 647 | |
| 648 | // insert the layout transfer into the queue and wait on the acquire |
| 649 | VkSubmitInfo submitInfo; |
| 650 | memset(&submitInfo, 0, sizeof(VkSubmitInfo)); |
| 651 | submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 652 | submitInfo.waitSemaphoreCount = 0; |
| 653 | submitInfo.pWaitDstStageMask = 0; |
| 654 | submitInfo.commandBufferCount = 1; |
| 655 | submitInfo.pCommandBuffers = &backbuffer->mTransitionCmdBuffers[1]; |
| 656 | submitInfo.signalSemaphoreCount = 1; |
| 657 | // When this command buffer finishes we will signal this semaphore so that we know it is now |
| 658 | // safe to present the image to the screen. |
| 659 | submitInfo.pSignalSemaphores = &backbuffer->mRenderSemaphore; |
| 660 | |
| 661 | // Attach second fence to submission here so we can track when the command buffer finishes. |
| 662 | mQueueSubmit(mBackendContext->fQueue, 1, &submitInfo, backbuffer->mUsageFences[1]); |
| 663 | |
| 664 | // Submit present operation to present queue. We use a semaphore here to make sure all rendering |
| 665 | // to the image is complete and that the layout has been change to present on the graphics |
| 666 | // queue. |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 667 | const VkPresentInfoKHR presentInfo = { |
| 668 | VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType |
| 669 | NULL, // pNext |
| 670 | 1, // waitSemaphoreCount |
| 671 | &backbuffer->mRenderSemaphore, // pWaitSemaphores |
| 672 | 1, // swapchainCount |
| 673 | &surface->mSwapchain, // pSwapchains |
| 674 | &backbuffer->mImageIndex, // pImageIndices |
| 675 | NULL // pResults |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 676 | }; |
| 677 | |
| 678 | mQueuePresentKHR(mPresentQueue, &presentInfo); |
| 679 | |
| 680 | surface->mBackbuffer.reset(); |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 681 | surface->mImageInfos[backbuffer->mImageIndex].mLastUsed = surface->mCurrentTime; |
| 682 | surface->mImageInfos[backbuffer->mImageIndex].mInvalid = false; |
| 683 | surface->mCurrentTime++; |
| 684 | } |
| 685 | |
| 686 | int VulkanManager::getAge(VulkanSurface* surface) { |
Greg Daniel | 74ea201 | 2017-11-10 11:32:58 -0500 | [diff] [blame^] | 687 | SkASSERT(surface->mBackbuffers); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 688 | VulkanSurface::BackbufferInfo* backbuffer = |
| 689 | surface->mBackbuffers + surface->mCurrentBackbufferIndex; |
| 690 | if (mSwapBehavior == SwapBehavior::Discard || |
| 691 | surface->mImageInfos[backbuffer->mImageIndex].mInvalid) { |
Greg Daniel | cd55852 | 2016-11-17 13:31:40 -0500 | [diff] [blame] | 692 | return 0; |
| 693 | } |
| 694 | uint16_t lastUsed = surface->mImageInfos[backbuffer->mImageIndex].mLastUsed; |
| 695 | return surface->mCurrentTime - lastUsed; |
Derek Sollenberger | 0e3cba3 | 2016-11-09 11:58:36 -0500 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | } /* namespace renderthread */ |
| 699 | } /* namespace uirenderer */ |
| 700 | } /* namespace android */ |