blob: 07499789c3b47c4fbc936bb21935b92ff4c8cc42 [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
9#include "GrContext.h"
jvanverthaf236b52016-05-20 06:01:06 -070010#include "GrRenderTarget.h"
Hal Canary95e3c052017-01-11 12:44:43 -050011#include "SkAutoMalloc.h"
jvanverth9f372462016-04-06 06:08:59 -070012#include "SkSurface.h"
jvanvertha8d0d6c2016-05-05 12:32:03 -070013#include "VulkanWindowContext.h"
jvanverth9f372462016-04-06 06:08:59 -070014
15#include "vk/GrVkInterface.h"
egdaniel580fa592016-08-31 11:03:50 -070016#include "vk/GrVkMemory.h"
jvanverth9f372462016-04-06 06:08:59 -070017#include "vk/GrVkUtil.h"
18#include "vk/GrVkTypes.h"
19
20#ifdef VK_USE_PLATFORM_WIN32_KHR
21// windows wants to define this as CreateSemaphoreA or CreateSemaphoreW
22#undef CreateSemaphore
23#endif
24
jvanverthb0d43522016-04-21 11:46:23 -070025#define GET_PROC(F) f ## F = (PFN_vk ## F) vkGetInstanceProcAddr(instance, "vk" #F)
26#define GET_DEV_PROC(F) f ## F = (PFN_vk ## F) vkGetDeviceProcAddr(device, "vk" #F)
27
jvanvertha8d0d6c2016-05-05 12:32:03 -070028namespace sk_app {
29
bsalomond1bdd1f2016-07-26 12:02:50 -070030VulkanWindowContext::VulkanWindowContext(const DisplayParams& params,
31 CreateVkSurfaceFn createVkSurface,
32 CanPresentFn canPresent)
jvanverthaf236b52016-05-20 06:01:06 -070033 : WindowContext()
34 , fSurface(VK_NULL_HANDLE)
jvanvertha8d0d6c2016-05-05 12:32:03 -070035 , fSwapchain(VK_NULL_HANDLE)
jvanverthaf236b52016-05-20 06:01:06 -070036 , fImages(nullptr)
37 , fImageLayouts(nullptr)
38 , fSurfaces(nullptr)
jvanvertha8d0d6c2016-05-05 12:32:03 -070039 , fCommandPool(VK_NULL_HANDLE)
40 , fBackbuffers(nullptr) {
jvanverth9f372462016-04-06 06:08:59 -070041
42 // any config code here (particularly for msaa)?
bsalomond1bdd1f2016-07-26 12:02:50 -070043 fBackendContext.reset(GrVkBackendContext::Create(&fPresentQueueIndex, canPresent));
jvanverth9f372462016-04-06 06:08:59 -070044
jvanverthb0d43522016-04-21 11:46:23 -070045 if (!(fBackendContext->fExtensions & kKHR_surface_GrVkExtensionFlag) ||
46 !(fBackendContext->fExtensions & kKHR_swapchain_GrVkExtensionFlag)) {
47 fBackendContext.reset(nullptr);
48 return;
49 }
50
51 VkInstance instance = fBackendContext->fInstance;
52 VkDevice device = fBackendContext->fDevice;
53 GET_PROC(DestroySurfaceKHR);
54 GET_PROC(GetPhysicalDeviceSurfaceSupportKHR);
55 GET_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
56 GET_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
57 GET_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
58 GET_DEV_PROC(CreateSwapchainKHR);
59 GET_DEV_PROC(DestroySwapchainKHR);
60 GET_DEV_PROC(GetSwapchainImagesKHR);
61 GET_DEV_PROC(AcquireNextImageKHR);
62 GET_DEV_PROC(QueuePresentKHR);
jvanverth9f372462016-04-06 06:08:59 -070063
csmartdalton008b9d82017-02-22 12:00:42 -070064 fContext = GrContext::Create(kVulkan_GrBackend, (GrBackendContext) fBackendContext.get(),
csmartdalton61cd31a2017-02-27 17:00:53 -070065 params.fGrContextOptions);
jvanverth9f372462016-04-06 06:08:59 -070066
bsalomond1bdd1f2016-07-26 12:02:50 -070067 fSurface = createVkSurface(instance);
jvanverth9f372462016-04-06 06:08:59 -070068 if (VK_NULL_HANDLE == fSurface) {
69 fBackendContext.reset(nullptr);
70 return;
71 }
72
jvanverth9f372462016-04-06 06:08:59 -070073 VkBool32 supported;
jvanverthb0d43522016-04-21 11:46:23 -070074 VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fBackendContext->fPhysicalDevice,
75 fPresentQueueIndex, fSurface,
76 &supported);
jvanverth9f372462016-04-06 06:08:59 -070077 if (VK_SUCCESS != res) {
78 this->destroyContext();
79 return;
80 }
81
brianosman05de2162016-05-06 13:28:57 -070082 if (!this->createSwapchain(-1, -1, params)) {
jvanverth9f372462016-04-06 06:08:59 -070083 this->destroyContext();
84 return;
85 }
86
87 // create presentQueue
88 vkGetDeviceQueue(fBackendContext->fDevice, fPresentQueueIndex, 0, &fPresentQueue);
jvanverth9f372462016-04-06 06:08:59 -070089}
90
bsalomonccde4ab2016-07-27 08:50:12 -070091bool VulkanWindowContext::createSwapchain(int width, int height,
brianosman05de2162016-05-06 13:28:57 -070092 const DisplayParams& params) {
jvanverth9f372462016-04-06 06:08:59 -070093 // check for capabilities
94 VkSurfaceCapabilitiesKHR caps;
jvanverthb0d43522016-04-21 11:46:23 -070095 VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fBackendContext->fPhysicalDevice,
96 fSurface, &caps);
jvanverth9f372462016-04-06 06:08:59 -070097 if (VK_SUCCESS != res) {
98 return false;
99 }
100
101 uint32_t surfaceFormatCount;
jvanverthb0d43522016-04-21 11:46:23 -0700102 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
103 &surfaceFormatCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700104 if (VK_SUCCESS != res) {
105 return false;
106 }
107
108 SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatKHR));
109 VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700110 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
111 &surfaceFormatCount, surfaceFormats);
jvanverth9f372462016-04-06 06:08:59 -0700112 if (VK_SUCCESS != res) {
113 return false;
114 }
115
116 uint32_t presentModeCount;
jvanverthb0d43522016-04-21 11:46:23 -0700117 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
118 &presentModeCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700119 if (VK_SUCCESS != res) {
120 return false;
121 }
122
123 SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR));
124 VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700125 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700126 &presentModeCount, presentModes);
jvanverth9f372462016-04-06 06:08:59 -0700127 if (VK_SUCCESS != res) {
128 return false;
129 }
130
131 VkExtent2D extent = caps.currentExtent;
132 // use the hints
133 if (extent.width == (uint32_t)-1) {
134 extent.width = width;
135 extent.height = height;
136 }
137
jvanverth9fab59d2016-04-06 12:08:51 -0700138 // clamp width; to protect us from broken hints
jvanverth9f372462016-04-06 06:08:59 -0700139 if (extent.width < caps.minImageExtent.width) {
140 extent.width = caps.minImageExtent.width;
141 } else if (extent.width > caps.maxImageExtent.width) {
142 extent.width = caps.maxImageExtent.width;
143 }
144 // clamp height
145 if (extent.height < caps.minImageExtent.height) {
146 extent.height = caps.minImageExtent.height;
147 } else if (extent.height > caps.maxImageExtent.height) {
148 extent.height = caps.maxImageExtent.height;
149 }
jvanverth1d155962016-05-23 13:13:36 -0700150
jvanverth9f372462016-04-06 06:08:59 -0700151 fWidth = (int)extent.width;
152 fHeight = (int)extent.height;
153
154 uint32_t imageCount = caps.minImageCount + 2;
155 if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) {
156 // Application must settle for fewer images than desired:
157 imageCount = caps.maxImageCount;
158 }
159
160 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
161 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
162 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
163 SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags);
164 SkASSERT(caps.supportedTransforms & caps.currentTransform);
165 SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
166 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR));
167 VkCompositeAlphaFlagBitsKHR composite_alpha =
168 (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ?
169 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR :
170 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
171
brianosman05de2162016-05-06 13:28:57 -0700172 // Pick our surface format. For now, just make sure it matches our sRGB request:
173 VkFormat surfaceFormat = VK_FORMAT_UNDEFINED;
174 VkColorSpaceKHR colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500175 auto srgbColorSpace = SkColorSpace::MakeSRGB();
brianosmanb109b8c2016-06-16 13:03:24 -0700176 bool wantSRGB = srgbColorSpace == params.fColorSpace;
brianosman05de2162016-05-06 13:28:57 -0700177 for (uint32_t i = 0; i < surfaceFormatCount; ++i) {
Greg Daniel94403452017-04-18 15:52:36 -0400178 GrPixelConfig config = GrVkFormatToPixelConfig(surfaceFormats[i].format);
179 if (kUnknown_GrPixelConfig != config &&
brianosman05de2162016-05-06 13:28:57 -0700180 GrPixelConfigIsSRGB(config) == wantSRGB) {
181 surfaceFormat = surfaceFormats[i].format;
182 colorSpace = surfaceFormats[i].colorSpace;
183 break;
184 }
185 }
186 fDisplayParams = params;
csmartdalton578f0642017-02-24 16:04:47 -0700187 fSampleCount = params.fMSAASampleCount;
188 fStencilBits = 8;
brianosman05de2162016-05-06 13:28:57 -0700189
190 if (VK_FORMAT_UNDEFINED == surfaceFormat) {
191 return false;
192 }
jvanvertha4b0fed2016-04-27 11:42:21 -0700193
jvanverth3d6ed3a2016-04-07 11:09:51 -0700194 // If mailbox mode is available, use it, as it is the lowest-latency non-
195 // tearing mode. If not, fall back to FIFO which is always available.
jvanverth9f372462016-04-06 06:08:59 -0700196 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700197 for (uint32_t i = 0; i < presentModeCount; ++i) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700198 // use mailbox
199 if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) {
jvanverth9f372462016-04-06 06:08:59 -0700200 mode = presentModes[i];
jvanverth3d6ed3a2016-04-07 11:09:51 -0700201 break;
jvanverth9f372462016-04-06 06:08:59 -0700202 }
203 }
204
205 VkSwapchainCreateInfoKHR swapchainCreateInfo;
206 memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR));
207 swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
208 swapchainCreateInfo.surface = fSurface;
209 swapchainCreateInfo.minImageCount = imageCount;
jvanvertha4b0fed2016-04-27 11:42:21 -0700210 swapchainCreateInfo.imageFormat = surfaceFormat;
211 swapchainCreateInfo.imageColorSpace = colorSpace;
jvanverth9f372462016-04-06 06:08:59 -0700212 swapchainCreateInfo.imageExtent = extent;
213 swapchainCreateInfo.imageArrayLayers = 1;
214 swapchainCreateInfo.imageUsage = usageFlags;
215
jvanverthb0d43522016-04-21 11:46:23 -0700216 uint32_t queueFamilies[] = { fBackendContext->fGraphicsQueueIndex, fPresentQueueIndex };
217 if (fBackendContext->fGraphicsQueueIndex != fPresentQueueIndex) {
jvanverth9f372462016-04-06 06:08:59 -0700218 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
219 swapchainCreateInfo.queueFamilyIndexCount = 2;
220 swapchainCreateInfo.pQueueFamilyIndices = queueFamilies;
221 } else {
222 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
223 swapchainCreateInfo.queueFamilyIndexCount = 0;
224 swapchainCreateInfo.pQueueFamilyIndices = nullptr;
225 }
226
Greg Daniele897b972016-10-06 13:57:57 -0400227 swapchainCreateInfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700228 swapchainCreateInfo.compositeAlpha = composite_alpha;
229 swapchainCreateInfo.presentMode = mode;
230 swapchainCreateInfo.clipped = true;
231 swapchainCreateInfo.oldSwapchain = fSwapchain;
232
jvanverthb0d43522016-04-21 11:46:23 -0700233 res = fCreateSwapchainKHR(fBackendContext->fDevice, &swapchainCreateInfo, nullptr, &fSwapchain);
jvanverth9f372462016-04-06 06:08:59 -0700234 if (VK_SUCCESS != res) {
235 return false;
236 }
237
238 // destroy the old swapchain
239 if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) {
240 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
241
242 this->destroyBuffers();
243
jvanverthb0d43522016-04-21 11:46:23 -0700244 fDestroySwapchainKHR(fBackendContext->fDevice, swapchainCreateInfo.oldSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700245 }
246
egdaniel58a8d922016-04-21 08:03:10 -0700247 this->createBuffers(swapchainCreateInfo.imageFormat);
jvanverth9f372462016-04-06 06:08:59 -0700248
249 return true;
250}
251
jvanvertha8d0d6c2016-05-05 12:32:03 -0700252void VulkanWindowContext::createBuffers(VkFormat format) {
Greg Daniel94403452017-04-18 15:52:36 -0400253 fPixelConfig = GrVkFormatToPixelConfig(format);
254 SkASSERT(kUnknown_GrPixelConfig != fPixelConfig);
egdaniel58a8d922016-04-21 08:03:10 -0700255
jvanverthb0d43522016-04-21 11:46:23 -0700256 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700257 SkASSERT(fImageCount);
258 fImages = new VkImage[fImageCount];
jvanverthb0d43522016-04-21 11:46:23 -0700259 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, fImages);
jvanverth9f372462016-04-06 06:08:59 -0700260
261 // set up initial image layouts and create surfaces
262 fImageLayouts = new VkImageLayout[fImageCount];
263 fSurfaces = new sk_sp<SkSurface>[fImageCount];
264 for (uint32_t i = 0; i < fImageCount; ++i) {
265 fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED;
266
267 GrBackendRenderTargetDesc desc;
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;
jvanverth9f372462016-04-06 06:08:59 -0700275 desc.fWidth = fWidth;
276 desc.fHeight = fHeight;
277 desc.fConfig = fPixelConfig;
278 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
csmartdalton578f0642017-02-24 16:04:47 -0700279 desc.fSampleCnt = fSampleCount;
280 desc.fStencilBits = fStencilBits;
jvanverth9f372462016-04-06 06:08:59 -0700281 desc.fRenderTargetHandle = (GrBackendObject) &info;
jvanverthaf236b52016-05-20 06:01:06 -0700282
Brian Osmanf750fbc2017-02-08 10:47:28 -0500283 fSurfaces[i] = SkSurface::MakeFromBackendRenderTarget(fContext, desc,
284 fDisplayParams.fColorSpace,
285 &fSurfaceProps);
jvanverth9f372462016-04-06 06:08:59 -0700286 }
287
288 // create the command pool for the command buffers
289 if (VK_NULL_HANDLE == fCommandPool) {
290 VkCommandPoolCreateInfo commandPoolInfo;
291 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
292 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
293 // this needs to be on the render queue
jvanverthb0d43522016-04-21 11:46:23 -0700294 commandPoolInfo.queueFamilyIndex = fBackendContext->fGraphicsQueueIndex;
jvanverth9f372462016-04-06 06:08:59 -0700295 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
296 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
297 CreateCommandPool(fBackendContext->fDevice, &commandPoolInfo,
298 nullptr, &fCommandPool));
299 }
300
301 // set up the backbuffers
302 VkSemaphoreCreateInfo semaphoreInfo;
303 memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo));
304 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
305 semaphoreInfo.pNext = nullptr;
306 semaphoreInfo.flags = 0;
307 VkCommandBufferAllocateInfo commandBuffersInfo;
308 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
309 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
310 commandBuffersInfo.pNext = nullptr;
311 commandBuffersInfo.commandPool = fCommandPool;
312 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
313 commandBuffersInfo.commandBufferCount = 2;
314 VkFenceCreateInfo fenceInfo;
315 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
316 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
317 fenceInfo.pNext = nullptr;
318 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
319
Greg Daniel1f05f442016-10-27 16:37:17 -0400320 // we create one additional backbuffer structure here, because we want to
jvanverth9f372462016-04-06 06:08:59 -0700321 // give the command buffers they contain a chance to finish before we cycle back
322 fBackbuffers = new BackbufferInfo[fImageCount + 1];
323 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
324 fBackbuffers[i].fImageIndex = -1;
325 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
326 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
327 nullptr, &fBackbuffers[i].fAcquireSemaphore));
328 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
329 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
330 nullptr, &fBackbuffers[i].fRenderSemaphore));
331 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
332 AllocateCommandBuffers(fBackendContext->fDevice, &commandBuffersInfo,
333 fBackbuffers[i].fTransitionCmdBuffers));
334 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
335 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
336 &fBackbuffers[i].fUsageFences[0]));
337 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
338 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
339 &fBackbuffers[i].fUsageFences[1]));
340 }
341 fCurrentBackbufferIndex = fImageCount;
342}
343
jvanvertha8d0d6c2016-05-05 12:32:03 -0700344void VulkanWindowContext::destroyBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700345
346 if (fBackbuffers) {
347 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
348 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700349 WaitForFences(fBackendContext->fDevice, 2,
jvanverth9f372462016-04-06 06:08:59 -0700350 fBackbuffers[i].fUsageFences,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700351 true, UINT64_MAX));
jvanverth9f372462016-04-06 06:08:59 -0700352 fBackbuffers[i].fImageIndex = -1;
353 GR_VK_CALL(fBackendContext->fInterface,
354 DestroySemaphore(fBackendContext->fDevice,
355 fBackbuffers[i].fAcquireSemaphore,
356 nullptr));
357 GR_VK_CALL(fBackendContext->fInterface,
358 DestroySemaphore(fBackendContext->fDevice,
359 fBackbuffers[i].fRenderSemaphore,
360 nullptr));
361 GR_VK_CALL(fBackendContext->fInterface,
362 FreeCommandBuffers(fBackendContext->fDevice, fCommandPool, 2,
363 fBackbuffers[i].fTransitionCmdBuffers));
364 GR_VK_CALL(fBackendContext->fInterface,
365 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[0], 0));
366 GR_VK_CALL(fBackendContext->fInterface,
367 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[1], 0));
368 }
369 }
370
371 delete[] fBackbuffers;
372 fBackbuffers = nullptr;
373
jvanverthaf236b52016-05-20 06:01:06 -0700374 // Does this actually free the surfaces?
jvanverth9f372462016-04-06 06:08:59 -0700375 delete[] fSurfaces;
376 fSurfaces = nullptr;
377 delete[] fImageLayouts;
378 fImageLayouts = nullptr;
379 delete[] fImages;
380 fImages = nullptr;
381}
382
jvanvertha8d0d6c2016-05-05 12:32:03 -0700383VulkanWindowContext::~VulkanWindowContext() {
jvanverth9f372462016-04-06 06:08:59 -0700384 this->destroyContext();
385}
386
jvanvertha8d0d6c2016-05-05 12:32:03 -0700387void VulkanWindowContext::destroyContext() {
jvanverth9f372462016-04-06 06:08:59 -0700388 if (!fBackendContext.get()) {
389 return;
390 }
391
jvanverth85f758c2016-05-27 06:47:08 -0700392 GR_VK_CALL(fBackendContext->fInterface, QueueWaitIdle(fPresentQueue));
jvanverth9f372462016-04-06 06:08:59 -0700393 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
394
395 this->destroyBuffers();
396
397 if (VK_NULL_HANDLE != fCommandPool) {
398 GR_VK_CALL(fBackendContext->fInterface, DestroyCommandPool(fBackendContext->fDevice,
399 fCommandPool, nullptr));
400 fCommandPool = VK_NULL_HANDLE;
401 }
402
403 if (VK_NULL_HANDLE != fSwapchain) {
jvanverthb0d43522016-04-21 11:46:23 -0700404 fDestroySwapchainKHR(fBackendContext->fDevice, fSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700405 fSwapchain = VK_NULL_HANDLE;
406 }
407
408 if (VK_NULL_HANDLE != fSurface) {
jvanverthb0d43522016-04-21 11:46:23 -0700409 fDestroySurfaceKHR(fBackendContext->fInstance, fSurface, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700410 fSurface = VK_NULL_HANDLE;
411 }
412
jvanverthaf236b52016-05-20 06:01:06 -0700413 fContext->unref();
jvanverth9f372462016-04-06 06:08:59 -0700414
415 fBackendContext.reset(nullptr);
416}
417
jvanvertha8d0d6c2016-05-05 12:32:03 -0700418VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer() {
jvanverth9f372462016-04-06 06:08:59 -0700419 SkASSERT(fBackbuffers);
420
421 ++fCurrentBackbufferIndex;
egdaniel804af7d2016-09-26 12:30:46 -0700422 if (fCurrentBackbufferIndex > fImageCount) {
jvanverth9f372462016-04-06 06:08:59 -0700423 fCurrentBackbufferIndex = 0;
424 }
425
426 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
jvanverth9f372462016-04-06 06:08:59 -0700427 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
428 WaitForFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences,
429 true, UINT64_MAX));
430 return backbuffer;
431}
432
jvanverthaf236b52016-05-20 06:01:06 -0700433sk_sp<SkSurface> VulkanWindowContext::getBackbufferSurface() {
jvanverth9f372462016-04-06 06:08:59 -0700434 BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
435 SkASSERT(backbuffer);
436
437 // reset the fence
438 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
439 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
440 // semaphores should be in unsignaled state
441
442 // acquire the image
jvanverthb0d43522016-04-21 11:46:23 -0700443 VkResult res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
444 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
445 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700446 if (VK_ERROR_SURFACE_LOST_KHR == res) {
447 // need to figure out how to create a new vkSurface without the platformData*
jvanverth3d6ed3a2016-04-07 11:09:51 -0700448 // maybe use attach somehow? but need a Window
jvanverth9f372462016-04-06 06:08:59 -0700449 return nullptr;
450 }
jvanverth3d6ed3a2016-04-07 11:09:51 -0700451 if (VK_ERROR_OUT_OF_DATE_KHR == res) {
jvanverth9f372462016-04-06 06:08:59 -0700452 // tear swapchain down and try again
Greg Daniel1f05f442016-10-27 16:37:17 -0400453 if (!this->createSwapchain(-1, -1, fDisplayParams)) {
jvanverth9f372462016-04-06 06:08:59 -0700454 return nullptr;
455 }
Jim Van Verthd63c1022017-01-05 13:50:49 -0500456 backbuffer = this->getAvailableBackbuffer();
457 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
458 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
jvanverth9f372462016-04-06 06:08:59 -0700459
460 // acquire the image
jvanvertha8d0d6c2016-05-05 12:32:03 -0700461 res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
jvanverthb0d43522016-04-21 11:46:23 -0700462 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
463 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700464
465 if (VK_SUCCESS != res) {
466 return nullptr;
467 }
468 }
469
470 // set up layout transfer from initial to color attachment
471 VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex];
egdaniel580fa592016-08-31 11:03:50 -0700472 SkASSERT(VK_IMAGE_LAYOUT_UNDEFINED == layout || VK_IMAGE_LAYOUT_PRESENT_SRC_KHR == layout);
jvanverth9f372462016-04-06 06:08:59 -0700473 VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
474 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT :
475 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
476 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700477 VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
jvanverth9f372462016-04-06 06:08:59 -0700478 0 : VK_ACCESS_MEMORY_READ_BIT;
479 VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
480
481 VkImageMemoryBarrier imageMemoryBarrier = {
482 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
483 NULL, // pNext
484 srcAccessMask, // outputMask
485 dstAccessMask, // inputMask
486 layout, // oldLayout
487 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout
488 fPresentQueueIndex, // srcQueueFamilyIndex
jvanverthb0d43522016-04-21 11:46:23 -0700489 fBackendContext->fGraphicsQueueIndex, // dstQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700490 fImages[backbuffer->fImageIndex], // image
491 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
492 };
493 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
494 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[0], 0));
495 VkCommandBufferBeginInfo info;
496 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
497 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
498 info.flags = 0;
499 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
500 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info));
501
jvanvertha8d0d6c2016-05-05 12:32:03 -0700502 GR_VK_CALL(fBackendContext->fInterface,
503 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0],
504 srcStageMask, dstStageMask, 0,
505 0, nullptr,
506 0, nullptr,
507 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700508
509 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700510 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0]));
jvanverth9f372462016-04-06 06:08:59 -0700511
egdaniel58a8d922016-04-21 08:03:10 -0700512 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanverth9f372462016-04-06 06:08:59 -0700513 // insert the layout transfer into the queue and wait on the acquire
514 VkSubmitInfo submitInfo;
515 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
516 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
517 submitInfo.waitSemaphoreCount = 1;
518 submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore;
egdaniel58a8d922016-04-21 08:03:10 -0700519 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
jvanverth9f372462016-04-06 06:08:59 -0700520 submitInfo.commandBufferCount = 1;
521 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0];
522 submitInfo.signalSemaphoreCount = 0;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700523
jvanverth9f372462016-04-06 06:08:59 -0700524 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700525 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700526 backbuffer->fUsageFences[0]));
527
egdaniel580fa592016-08-31 11:03:50 -0700528 GrVkImageInfo* imageInfo;
529 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
530 surface->getRenderTargetHandle((GrBackendObject*)&imageInfo,
531 SkSurface::kFlushRead_BackendHandleAccess);
532 imageInfo->updateImageLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
533
534 return sk_ref_sp(surface);
jvanverth9f372462016-04-06 06:08:59 -0700535}
536
jvanvertha8d0d6c2016-05-05 12:32:03 -0700537void VulkanWindowContext::swapBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700538
539 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
egdaniel580fa592016-08-31 11:03:50 -0700540 GrVkImageInfo* imageInfo;
541 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
542 surface->getRenderTargetHandle((GrBackendObject*)&imageInfo,
543 SkSurface::kFlushRead_BackendHandleAccess);
544 // Check to make sure we never change the actually wrapped image
545 SkASSERT(imageInfo->fImage == fImages[backbuffer->fImageIndex]);
jvanverth9f372462016-04-06 06:08:59 -0700546
egdaniel580fa592016-08-31 11:03:50 -0700547 VkImageLayout layout = imageInfo->fImageLayout;
548 VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(layout);
jvanverth9f372462016-04-06 06:08:59 -0700549 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
egdaniel580fa592016-08-31 11:03:50 -0700550 VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(layout);
jvanverth9f372462016-04-06 06:08:59 -0700551 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
552
553 VkImageMemoryBarrier imageMemoryBarrier = {
554 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
555 NULL, // pNext
556 srcAccessMask, // outputMask
557 dstAccessMask, // inputMask
558 layout, // oldLayout
559 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout
jvanverthb0d43522016-04-21 11:46:23 -0700560 fBackendContext->fGraphicsQueueIndex, // srcQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700561 fPresentQueueIndex, // dstQueueFamilyIndex
562 fImages[backbuffer->fImageIndex], // image
563 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
564 };
565 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
566 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[1], 0));
567 VkCommandBufferBeginInfo info;
568 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
569 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
570 info.flags = 0;
571 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
572 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info));
573 GR_VK_CALL(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700574 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
575 srcStageMask, dstStageMask, 0,
576 0, nullptr,
577 0, nullptr,
578 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700579 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
580 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1]));
581
582 fImageLayouts[backbuffer->fImageIndex] = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
583
584 // insert the layout transfer into the queue and wait on the acquire
585 VkSubmitInfo submitInfo;
586 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
587 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
588 submitInfo.waitSemaphoreCount = 0;
589 submitInfo.pWaitDstStageMask = 0;
590 submitInfo.commandBufferCount = 1;
591 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[1];
592 submitInfo.signalSemaphoreCount = 1;
593 submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore;
594
595 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700596 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700597 backbuffer->fUsageFences[1]));
598
599 // Submit present operation to present queue
600 const VkPresentInfoKHR presentInfo =
601 {
602 VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType
603 NULL, // pNext
604 1, // waitSemaphoreCount
605 &backbuffer->fRenderSemaphore, // pWaitSemaphores
606 1, // swapchainCount
607 &fSwapchain, // pSwapchains
608 &backbuffer->fImageIndex, // pImageIndices
609 NULL // pResults
610 };
611
jvanverthb0d43522016-04-21 11:46:23 -0700612 fQueuePresentKHR(fPresentQueue, &presentInfo);
jvanverth9f372462016-04-06 06:08:59 -0700613}
jvanvertha8d0d6c2016-05-05 12:32:03 -0700614
615} //namespace sk_app