blob: 831c7ae0c2710cb9b23703b07c440d6f4aea134f [file] [log] [blame]
jvanverth9f372462016-04-06 06:08:59 -07001
2/*
3 * Copyright 2015 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
Greg Daniele79b4732017-04-20 14:07:46 -04009#include "GrBackendSurface.h"
jvanverth9f372462016-04-06 06:08:59 -070010#include "GrContext.h"
jvanverthaf236b52016-05-20 06:01:06 -070011#include "GrRenderTarget.h"
Hal Canary95e3c052017-01-11 12:44:43 -050012#include "SkAutoMalloc.h"
jvanverth9f372462016-04-06 06:08:59 -070013#include "SkSurface.h"
jvanvertha8d0d6c2016-05-05 12:32:03 -070014#include "VulkanWindowContext.h"
jvanverth9f372462016-04-06 06:08:59 -070015
16#include "vk/GrVkInterface.h"
egdaniel580fa592016-08-31 11:03:50 -070017#include "vk/GrVkMemory.h"
jvanverth9f372462016-04-06 06:08:59 -070018#include "vk/GrVkUtil.h"
19#include "vk/GrVkTypes.h"
20
21#ifdef VK_USE_PLATFORM_WIN32_KHR
22// windows wants to define this as CreateSemaphoreA or CreateSemaphoreW
23#undef CreateSemaphore
24#endif
25
jvanverthb0d43522016-04-21 11:46:23 -070026#define GET_PROC(F) f ## F = (PFN_vk ## F) vkGetInstanceProcAddr(instance, "vk" #F)
27#define GET_DEV_PROC(F) f ## F = (PFN_vk ## F) vkGetDeviceProcAddr(device, "vk" #F)
28
jvanvertha8d0d6c2016-05-05 12:32:03 -070029namespace sk_app {
30
bsalomond1bdd1f2016-07-26 12:02:50 -070031VulkanWindowContext::VulkanWindowContext(const DisplayParams& params,
32 CreateVkSurfaceFn createVkSurface,
33 CanPresentFn canPresent)
jvanverthaf236b52016-05-20 06:01:06 -070034 : WindowContext()
35 , fSurface(VK_NULL_HANDLE)
jvanvertha8d0d6c2016-05-05 12:32:03 -070036 , fSwapchain(VK_NULL_HANDLE)
jvanverthaf236b52016-05-20 06:01:06 -070037 , fImages(nullptr)
38 , fImageLayouts(nullptr)
39 , fSurfaces(nullptr)
jvanvertha8d0d6c2016-05-05 12:32:03 -070040 , fCommandPool(VK_NULL_HANDLE)
41 , fBackbuffers(nullptr) {
jvanverth9f372462016-04-06 06:08:59 -070042
43 // any config code here (particularly for msaa)?
bsalomond1bdd1f2016-07-26 12:02:50 -070044 fBackendContext.reset(GrVkBackendContext::Create(&fPresentQueueIndex, canPresent));
jvanverth9f372462016-04-06 06:08:59 -070045
jvanverthb0d43522016-04-21 11:46:23 -070046 if (!(fBackendContext->fExtensions & kKHR_surface_GrVkExtensionFlag) ||
47 !(fBackendContext->fExtensions & kKHR_swapchain_GrVkExtensionFlag)) {
48 fBackendContext.reset(nullptr);
49 return;
50 }
51
52 VkInstance instance = fBackendContext->fInstance;
53 VkDevice device = fBackendContext->fDevice;
54 GET_PROC(DestroySurfaceKHR);
55 GET_PROC(GetPhysicalDeviceSurfaceSupportKHR);
56 GET_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
57 GET_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
58 GET_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
59 GET_DEV_PROC(CreateSwapchainKHR);
60 GET_DEV_PROC(DestroySwapchainKHR);
61 GET_DEV_PROC(GetSwapchainImagesKHR);
62 GET_DEV_PROC(AcquireNextImageKHR);
63 GET_DEV_PROC(QueuePresentKHR);
jvanverth9f372462016-04-06 06:08:59 -070064
csmartdalton008b9d82017-02-22 12:00:42 -070065 fContext = GrContext::Create(kVulkan_GrBackend, (GrBackendContext) fBackendContext.get(),
csmartdalton61cd31a2017-02-27 17:00:53 -070066 params.fGrContextOptions);
jvanverth9f372462016-04-06 06:08:59 -070067
bsalomond1bdd1f2016-07-26 12:02:50 -070068 fSurface = createVkSurface(instance);
jvanverth9f372462016-04-06 06:08:59 -070069 if (VK_NULL_HANDLE == fSurface) {
70 fBackendContext.reset(nullptr);
71 return;
72 }
73
jvanverth9f372462016-04-06 06:08:59 -070074 VkBool32 supported;
jvanverthb0d43522016-04-21 11:46:23 -070075 VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fBackendContext->fPhysicalDevice,
76 fPresentQueueIndex, fSurface,
77 &supported);
jvanverth9f372462016-04-06 06:08:59 -070078 if (VK_SUCCESS != res) {
79 this->destroyContext();
80 return;
81 }
82
brianosman05de2162016-05-06 13:28:57 -070083 if (!this->createSwapchain(-1, -1, params)) {
jvanverth9f372462016-04-06 06:08:59 -070084 this->destroyContext();
85 return;
86 }
87
88 // create presentQueue
89 vkGetDeviceQueue(fBackendContext->fDevice, fPresentQueueIndex, 0, &fPresentQueue);
jvanverth9f372462016-04-06 06:08:59 -070090}
91
bsalomonccde4ab2016-07-27 08:50:12 -070092bool VulkanWindowContext::createSwapchain(int width, int height,
brianosman05de2162016-05-06 13:28:57 -070093 const DisplayParams& params) {
jvanverth9f372462016-04-06 06:08:59 -070094 // check for capabilities
95 VkSurfaceCapabilitiesKHR caps;
jvanverthb0d43522016-04-21 11:46:23 -070096 VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fBackendContext->fPhysicalDevice,
97 fSurface, &caps);
jvanverth9f372462016-04-06 06:08:59 -070098 if (VK_SUCCESS != res) {
99 return false;
100 }
101
102 uint32_t surfaceFormatCount;
jvanverthb0d43522016-04-21 11:46:23 -0700103 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
104 &surfaceFormatCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700105 if (VK_SUCCESS != res) {
106 return false;
107 }
108
109 SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatKHR));
110 VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700111 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
112 &surfaceFormatCount, surfaceFormats);
jvanverth9f372462016-04-06 06:08:59 -0700113 if (VK_SUCCESS != res) {
114 return false;
115 }
116
117 uint32_t presentModeCount;
jvanverthb0d43522016-04-21 11:46:23 -0700118 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
119 &presentModeCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700120 if (VK_SUCCESS != res) {
121 return false;
122 }
123
124 SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR));
125 VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700126 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700127 &presentModeCount, presentModes);
jvanverth9f372462016-04-06 06:08:59 -0700128 if (VK_SUCCESS != res) {
129 return false;
130 }
131
132 VkExtent2D extent = caps.currentExtent;
133 // use the hints
134 if (extent.width == (uint32_t)-1) {
135 extent.width = width;
136 extent.height = height;
137 }
138
jvanverth9fab59d2016-04-06 12:08:51 -0700139 // clamp width; to protect us from broken hints
jvanverth9f372462016-04-06 06:08:59 -0700140 if (extent.width < caps.minImageExtent.width) {
141 extent.width = caps.minImageExtent.width;
142 } else if (extent.width > caps.maxImageExtent.width) {
143 extent.width = caps.maxImageExtent.width;
144 }
145 // clamp height
146 if (extent.height < caps.minImageExtent.height) {
147 extent.height = caps.minImageExtent.height;
148 } else if (extent.height > caps.maxImageExtent.height) {
149 extent.height = caps.maxImageExtent.height;
150 }
jvanverth1d155962016-05-23 13:13:36 -0700151
jvanverth9f372462016-04-06 06:08:59 -0700152 fWidth = (int)extent.width;
153 fHeight = (int)extent.height;
154
155 uint32_t imageCount = caps.minImageCount + 2;
156 if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) {
157 // Application must settle for fewer images than desired:
158 imageCount = caps.maxImageCount;
159 }
160
161 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
162 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
163 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
164 SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags);
165 SkASSERT(caps.supportedTransforms & caps.currentTransform);
166 SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
167 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR));
168 VkCompositeAlphaFlagBitsKHR composite_alpha =
169 (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ?
170 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR :
171 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
172
brianosman05de2162016-05-06 13:28:57 -0700173 // Pick our surface format. For now, just make sure it matches our sRGB request:
174 VkFormat surfaceFormat = VK_FORMAT_UNDEFINED;
175 VkColorSpaceKHR colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500176 auto srgbColorSpace = SkColorSpace::MakeSRGB();
brianosmanb109b8c2016-06-16 13:03:24 -0700177 bool wantSRGB = srgbColorSpace == params.fColorSpace;
brianosman05de2162016-05-06 13:28:57 -0700178 for (uint32_t i = 0; i < surfaceFormatCount; ++i) {
Greg Daniel94403452017-04-18 15:52:36 -0400179 GrPixelConfig config = GrVkFormatToPixelConfig(surfaceFormats[i].format);
180 if (kUnknown_GrPixelConfig != config &&
brianosman05de2162016-05-06 13:28:57 -0700181 GrPixelConfigIsSRGB(config) == wantSRGB) {
182 surfaceFormat = surfaceFormats[i].format;
183 colorSpace = surfaceFormats[i].colorSpace;
184 break;
185 }
186 }
187 fDisplayParams = params;
csmartdalton578f0642017-02-24 16:04:47 -0700188 fSampleCount = params.fMSAASampleCount;
189 fStencilBits = 8;
brianosman05de2162016-05-06 13:28:57 -0700190
191 if (VK_FORMAT_UNDEFINED == surfaceFormat) {
192 return false;
193 }
jvanvertha4b0fed2016-04-27 11:42:21 -0700194
jvanverth3d6ed3a2016-04-07 11:09:51 -0700195 // If mailbox mode is available, use it, as it is the lowest-latency non-
196 // tearing mode. If not, fall back to FIFO which is always available.
jvanverth9f372462016-04-06 06:08:59 -0700197 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700198 for (uint32_t i = 0; i < presentModeCount; ++i) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700199 // use mailbox
200 if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) {
jvanverth9f372462016-04-06 06:08:59 -0700201 mode = presentModes[i];
jvanverth3d6ed3a2016-04-07 11:09:51 -0700202 break;
jvanverth9f372462016-04-06 06:08:59 -0700203 }
204 }
205
206 VkSwapchainCreateInfoKHR swapchainCreateInfo;
207 memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR));
208 swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
209 swapchainCreateInfo.surface = fSurface;
210 swapchainCreateInfo.minImageCount = imageCount;
jvanvertha4b0fed2016-04-27 11:42:21 -0700211 swapchainCreateInfo.imageFormat = surfaceFormat;
212 swapchainCreateInfo.imageColorSpace = colorSpace;
jvanverth9f372462016-04-06 06:08:59 -0700213 swapchainCreateInfo.imageExtent = extent;
214 swapchainCreateInfo.imageArrayLayers = 1;
215 swapchainCreateInfo.imageUsage = usageFlags;
216
jvanverthb0d43522016-04-21 11:46:23 -0700217 uint32_t queueFamilies[] = { fBackendContext->fGraphicsQueueIndex, fPresentQueueIndex };
218 if (fBackendContext->fGraphicsQueueIndex != fPresentQueueIndex) {
jvanverth9f372462016-04-06 06:08:59 -0700219 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
220 swapchainCreateInfo.queueFamilyIndexCount = 2;
221 swapchainCreateInfo.pQueueFamilyIndices = queueFamilies;
222 } else {
223 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
224 swapchainCreateInfo.queueFamilyIndexCount = 0;
225 swapchainCreateInfo.pQueueFamilyIndices = nullptr;
226 }
227
Greg Daniele897b972016-10-06 13:57:57 -0400228 swapchainCreateInfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700229 swapchainCreateInfo.compositeAlpha = composite_alpha;
230 swapchainCreateInfo.presentMode = mode;
231 swapchainCreateInfo.clipped = true;
232 swapchainCreateInfo.oldSwapchain = fSwapchain;
233
jvanverthb0d43522016-04-21 11:46:23 -0700234 res = fCreateSwapchainKHR(fBackendContext->fDevice, &swapchainCreateInfo, nullptr, &fSwapchain);
jvanverth9f372462016-04-06 06:08:59 -0700235 if (VK_SUCCESS != res) {
236 return false;
237 }
238
239 // destroy the old swapchain
240 if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) {
241 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
242
243 this->destroyBuffers();
244
jvanverthb0d43522016-04-21 11:46:23 -0700245 fDestroySwapchainKHR(fBackendContext->fDevice, swapchainCreateInfo.oldSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700246 }
247
egdaniel58a8d922016-04-21 08:03:10 -0700248 this->createBuffers(swapchainCreateInfo.imageFormat);
jvanverth9f372462016-04-06 06:08:59 -0700249
250 return true;
251}
252
jvanvertha8d0d6c2016-05-05 12:32:03 -0700253void VulkanWindowContext::createBuffers(VkFormat format) {
Greg Daniel94403452017-04-18 15:52:36 -0400254 fPixelConfig = GrVkFormatToPixelConfig(format);
255 SkASSERT(kUnknown_GrPixelConfig != fPixelConfig);
egdaniel58a8d922016-04-21 08:03:10 -0700256
jvanverthb0d43522016-04-21 11:46:23 -0700257 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700258 SkASSERT(fImageCount);
259 fImages = new VkImage[fImageCount];
jvanverthb0d43522016-04-21 11:46:23 -0700260 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, fImages);
jvanverth9f372462016-04-06 06:08:59 -0700261
262 // set up initial image layouts and create surfaces
263 fImageLayouts = new VkImageLayout[fImageCount];
264 fSurfaces = new sk_sp<SkSurface>[fImageCount];
265 for (uint32_t i = 0; i < fImageCount; ++i) {
266 fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED;
267
egdanielb2df0c22016-05-13 11:30:37 -0700268 GrVkImageInfo info;
jvanverth9f372462016-04-06 06:08:59 -0700269 info.fImage = fImages[i];
jvanverth9d54afc2016-09-20 09:20:03 -0700270 info.fAlloc = { VK_NULL_HANDLE, 0, 0, 0 };
egdaniel580fa592016-08-31 11:03:50 -0700271 info.fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
jvanverth9f372462016-04-06 06:08:59 -0700272 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
egdaniel58a8d922016-04-21 08:03:10 -0700273 info.fFormat = format;
jvanverth3622a172016-05-10 06:42:18 -0700274 info.fLevelCount = 1;
jvanverthaf236b52016-05-20 06:01:06 -0700275
Greg Daniele79b4732017-04-20 14:07:46 -0400276 GrBackendTexture backendTex(fWidth, fHeight, &info);
277
278 fSurfaces[i] = SkSurface::MakeFromBackendTextureAsRenderTarget(fContext, backendTex,
279 kTopLeft_GrSurfaceOrigin,
280 fSampleCount,
281 fDisplayParams.fColorSpace,
282 &fSurfaceProps);
jvanverth9f372462016-04-06 06:08:59 -0700283 }
284
285 // create the command pool for the command buffers
286 if (VK_NULL_HANDLE == fCommandPool) {
287 VkCommandPoolCreateInfo commandPoolInfo;
288 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
289 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
290 // this needs to be on the render queue
jvanverthb0d43522016-04-21 11:46:23 -0700291 commandPoolInfo.queueFamilyIndex = fBackendContext->fGraphicsQueueIndex;
jvanverth9f372462016-04-06 06:08:59 -0700292 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
293 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
294 CreateCommandPool(fBackendContext->fDevice, &commandPoolInfo,
295 nullptr, &fCommandPool));
296 }
297
298 // set up the backbuffers
299 VkSemaphoreCreateInfo semaphoreInfo;
300 memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo));
301 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
302 semaphoreInfo.pNext = nullptr;
303 semaphoreInfo.flags = 0;
304 VkCommandBufferAllocateInfo commandBuffersInfo;
305 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
306 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
307 commandBuffersInfo.pNext = nullptr;
308 commandBuffersInfo.commandPool = fCommandPool;
309 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
310 commandBuffersInfo.commandBufferCount = 2;
311 VkFenceCreateInfo fenceInfo;
312 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
313 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
314 fenceInfo.pNext = nullptr;
315 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
316
Greg Daniel1f05f442016-10-27 16:37:17 -0400317 // we create one additional backbuffer structure here, because we want to
jvanverth9f372462016-04-06 06:08:59 -0700318 // give the command buffers they contain a chance to finish before we cycle back
319 fBackbuffers = new BackbufferInfo[fImageCount + 1];
320 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
321 fBackbuffers[i].fImageIndex = -1;
322 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
323 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
324 nullptr, &fBackbuffers[i].fAcquireSemaphore));
325 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
326 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
327 nullptr, &fBackbuffers[i].fRenderSemaphore));
328 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
329 AllocateCommandBuffers(fBackendContext->fDevice, &commandBuffersInfo,
330 fBackbuffers[i].fTransitionCmdBuffers));
331 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
332 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
333 &fBackbuffers[i].fUsageFences[0]));
334 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
335 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
336 &fBackbuffers[i].fUsageFences[1]));
337 }
338 fCurrentBackbufferIndex = fImageCount;
339}
340
jvanvertha8d0d6c2016-05-05 12:32:03 -0700341void VulkanWindowContext::destroyBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700342
343 if (fBackbuffers) {
344 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
345 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700346 WaitForFences(fBackendContext->fDevice, 2,
jvanverth9f372462016-04-06 06:08:59 -0700347 fBackbuffers[i].fUsageFences,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700348 true, UINT64_MAX));
jvanverth9f372462016-04-06 06:08:59 -0700349 fBackbuffers[i].fImageIndex = -1;
350 GR_VK_CALL(fBackendContext->fInterface,
351 DestroySemaphore(fBackendContext->fDevice,
352 fBackbuffers[i].fAcquireSemaphore,
353 nullptr));
354 GR_VK_CALL(fBackendContext->fInterface,
355 DestroySemaphore(fBackendContext->fDevice,
356 fBackbuffers[i].fRenderSemaphore,
357 nullptr));
358 GR_VK_CALL(fBackendContext->fInterface,
359 FreeCommandBuffers(fBackendContext->fDevice, fCommandPool, 2,
360 fBackbuffers[i].fTransitionCmdBuffers));
361 GR_VK_CALL(fBackendContext->fInterface,
362 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[0], 0));
363 GR_VK_CALL(fBackendContext->fInterface,
364 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[1], 0));
365 }
366 }
367
368 delete[] fBackbuffers;
369 fBackbuffers = nullptr;
370
jvanverthaf236b52016-05-20 06:01:06 -0700371 // Does this actually free the surfaces?
jvanverth9f372462016-04-06 06:08:59 -0700372 delete[] fSurfaces;
373 fSurfaces = nullptr;
374 delete[] fImageLayouts;
375 fImageLayouts = nullptr;
376 delete[] fImages;
377 fImages = nullptr;
378}
379
jvanvertha8d0d6c2016-05-05 12:32:03 -0700380VulkanWindowContext::~VulkanWindowContext() {
jvanverth9f372462016-04-06 06:08:59 -0700381 this->destroyContext();
382}
383
jvanvertha8d0d6c2016-05-05 12:32:03 -0700384void VulkanWindowContext::destroyContext() {
jvanverth9f372462016-04-06 06:08:59 -0700385 if (!fBackendContext.get()) {
386 return;
387 }
388
jvanverth85f758c2016-05-27 06:47:08 -0700389 GR_VK_CALL(fBackendContext->fInterface, QueueWaitIdle(fPresentQueue));
jvanverth9f372462016-04-06 06:08:59 -0700390 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
391
392 this->destroyBuffers();
393
394 if (VK_NULL_HANDLE != fCommandPool) {
395 GR_VK_CALL(fBackendContext->fInterface, DestroyCommandPool(fBackendContext->fDevice,
396 fCommandPool, nullptr));
397 fCommandPool = VK_NULL_HANDLE;
398 }
399
400 if (VK_NULL_HANDLE != fSwapchain) {
jvanverthb0d43522016-04-21 11:46:23 -0700401 fDestroySwapchainKHR(fBackendContext->fDevice, fSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700402 fSwapchain = VK_NULL_HANDLE;
403 }
404
405 if (VK_NULL_HANDLE != fSurface) {
jvanverthb0d43522016-04-21 11:46:23 -0700406 fDestroySurfaceKHR(fBackendContext->fInstance, fSurface, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700407 fSurface = VK_NULL_HANDLE;
408 }
409
jvanverthaf236b52016-05-20 06:01:06 -0700410 fContext->unref();
jvanverth9f372462016-04-06 06:08:59 -0700411
412 fBackendContext.reset(nullptr);
413}
414
jvanvertha8d0d6c2016-05-05 12:32:03 -0700415VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer() {
jvanverth9f372462016-04-06 06:08:59 -0700416 SkASSERT(fBackbuffers);
417
418 ++fCurrentBackbufferIndex;
egdaniel804af7d2016-09-26 12:30:46 -0700419 if (fCurrentBackbufferIndex > fImageCount) {
jvanverth9f372462016-04-06 06:08:59 -0700420 fCurrentBackbufferIndex = 0;
421 }
422
423 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
jvanverth9f372462016-04-06 06:08:59 -0700424 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
425 WaitForFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences,
426 true, UINT64_MAX));
427 return backbuffer;
428}
429
jvanverthaf236b52016-05-20 06:01:06 -0700430sk_sp<SkSurface> VulkanWindowContext::getBackbufferSurface() {
jvanverth9f372462016-04-06 06:08:59 -0700431 BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
432 SkASSERT(backbuffer);
433
434 // reset the fence
435 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
436 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
437 // semaphores should be in unsignaled state
438
439 // acquire the image
jvanverthb0d43522016-04-21 11:46:23 -0700440 VkResult res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
441 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
442 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700443 if (VK_ERROR_SURFACE_LOST_KHR == res) {
444 // need to figure out how to create a new vkSurface without the platformData*
jvanverth3d6ed3a2016-04-07 11:09:51 -0700445 // maybe use attach somehow? but need a Window
jvanverth9f372462016-04-06 06:08:59 -0700446 return nullptr;
447 }
jvanverth3d6ed3a2016-04-07 11:09:51 -0700448 if (VK_ERROR_OUT_OF_DATE_KHR == res) {
jvanverth9f372462016-04-06 06:08:59 -0700449 // tear swapchain down and try again
Greg Daniel1f05f442016-10-27 16:37:17 -0400450 if (!this->createSwapchain(-1, -1, fDisplayParams)) {
jvanverth9f372462016-04-06 06:08:59 -0700451 return nullptr;
452 }
Jim Van Verthd63c1022017-01-05 13:50:49 -0500453 backbuffer = this->getAvailableBackbuffer();
454 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
455 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
jvanverth9f372462016-04-06 06:08:59 -0700456
457 // acquire the image
jvanvertha8d0d6c2016-05-05 12:32:03 -0700458 res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
jvanverthb0d43522016-04-21 11:46:23 -0700459 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
460 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700461
462 if (VK_SUCCESS != res) {
463 return nullptr;
464 }
465 }
466
467 // set up layout transfer from initial to color attachment
468 VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex];
egdaniel580fa592016-08-31 11:03:50 -0700469 SkASSERT(VK_IMAGE_LAYOUT_UNDEFINED == layout || VK_IMAGE_LAYOUT_PRESENT_SRC_KHR == layout);
jvanverth9f372462016-04-06 06:08:59 -0700470 VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
471 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT :
472 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
473 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700474 VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
jvanverth9f372462016-04-06 06:08:59 -0700475 0 : VK_ACCESS_MEMORY_READ_BIT;
476 VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
477
478 VkImageMemoryBarrier imageMemoryBarrier = {
479 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
480 NULL, // pNext
481 srcAccessMask, // outputMask
482 dstAccessMask, // inputMask
483 layout, // oldLayout
484 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout
485 fPresentQueueIndex, // srcQueueFamilyIndex
jvanverthb0d43522016-04-21 11:46:23 -0700486 fBackendContext->fGraphicsQueueIndex, // dstQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700487 fImages[backbuffer->fImageIndex], // image
488 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
489 };
490 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
491 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[0], 0));
492 VkCommandBufferBeginInfo info;
493 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
494 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
495 info.flags = 0;
496 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
497 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info));
498
jvanvertha8d0d6c2016-05-05 12:32:03 -0700499 GR_VK_CALL(fBackendContext->fInterface,
500 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0],
501 srcStageMask, dstStageMask, 0,
502 0, nullptr,
503 0, nullptr,
504 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700505
506 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700507 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0]));
jvanverth9f372462016-04-06 06:08:59 -0700508
egdaniel58a8d922016-04-21 08:03:10 -0700509 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanverth9f372462016-04-06 06:08:59 -0700510 // insert the layout transfer into the queue and wait on the acquire
511 VkSubmitInfo submitInfo;
512 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
513 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
514 submitInfo.waitSemaphoreCount = 1;
515 submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore;
egdaniel58a8d922016-04-21 08:03:10 -0700516 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
jvanverth9f372462016-04-06 06:08:59 -0700517 submitInfo.commandBufferCount = 1;
518 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0];
519 submitInfo.signalSemaphoreCount = 0;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700520
jvanverth9f372462016-04-06 06:08:59 -0700521 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700522 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700523 backbuffer->fUsageFences[0]));
524
egdaniel580fa592016-08-31 11:03:50 -0700525 GrVkImageInfo* imageInfo;
526 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
527 surface->getRenderTargetHandle((GrBackendObject*)&imageInfo,
528 SkSurface::kFlushRead_BackendHandleAccess);
529 imageInfo->updateImageLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
530
531 return sk_ref_sp(surface);
jvanverth9f372462016-04-06 06:08:59 -0700532}
533
jvanvertha8d0d6c2016-05-05 12:32:03 -0700534void VulkanWindowContext::swapBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700535
536 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
egdaniel580fa592016-08-31 11:03:50 -0700537 GrVkImageInfo* imageInfo;
538 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
539 surface->getRenderTargetHandle((GrBackendObject*)&imageInfo,
540 SkSurface::kFlushRead_BackendHandleAccess);
541 // Check to make sure we never change the actually wrapped image
542 SkASSERT(imageInfo->fImage == fImages[backbuffer->fImageIndex]);
jvanverth9f372462016-04-06 06:08:59 -0700543
egdaniel580fa592016-08-31 11:03:50 -0700544 VkImageLayout layout = imageInfo->fImageLayout;
545 VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(layout);
jvanverth9f372462016-04-06 06:08:59 -0700546 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
egdaniel580fa592016-08-31 11:03:50 -0700547 VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(layout);
jvanverth9f372462016-04-06 06:08:59 -0700548 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
549
550 VkImageMemoryBarrier imageMemoryBarrier = {
551 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
552 NULL, // pNext
553 srcAccessMask, // outputMask
554 dstAccessMask, // inputMask
555 layout, // oldLayout
556 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout
jvanverthb0d43522016-04-21 11:46:23 -0700557 fBackendContext->fGraphicsQueueIndex, // srcQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700558 fPresentQueueIndex, // dstQueueFamilyIndex
559 fImages[backbuffer->fImageIndex], // image
560 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
561 };
562 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
563 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[1], 0));
564 VkCommandBufferBeginInfo info;
565 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
566 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
567 info.flags = 0;
568 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
569 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info));
570 GR_VK_CALL(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700571 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
572 srcStageMask, dstStageMask, 0,
573 0, nullptr,
574 0, nullptr,
575 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700576 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
577 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1]));
578
579 fImageLayouts[backbuffer->fImageIndex] = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
580
581 // insert the layout transfer into the queue and wait on the acquire
582 VkSubmitInfo submitInfo;
583 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
584 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
585 submitInfo.waitSemaphoreCount = 0;
586 submitInfo.pWaitDstStageMask = 0;
587 submitInfo.commandBufferCount = 1;
588 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[1];
589 submitInfo.signalSemaphoreCount = 1;
590 submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore;
591
592 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700593 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700594 backbuffer->fUsageFences[1]));
595
596 // Submit present operation to present queue
597 const VkPresentInfoKHR presentInfo =
598 {
599 VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType
600 NULL, // pNext
601 1, // waitSemaphoreCount
602 &backbuffer->fRenderSemaphore, // pWaitSemaphores
603 1, // swapchainCount
604 &fSwapchain, // pSwapchains
605 &backbuffer->fImageIndex, // pImageIndices
606 NULL // pResults
607 };
608
jvanverthb0d43522016-04-21 11:46:23 -0700609 fQueuePresentKHR(fPresentQueue, &presentInfo);
jvanverth9f372462016-04-06 06:08:59 -0700610}
jvanvertha8d0d6c2016-05-05 12:32:03 -0700611
612} //namespace sk_app