blob: b4e6676335d236c42bd51c6463649d55f6a1d738 [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
jvanvertha8d0d6c2016-05-05 12:32:03 -070064 fContext = GrContext::Create(kVulkan_GrBackend, (GrBackendContext) fBackendContext.get());
jvanverth9f372462016-04-06 06:08:59 -070065
bsalomond1bdd1f2016-07-26 12:02:50 -070066 fSurface = createVkSurface(instance);
jvanverth9f372462016-04-06 06:08:59 -070067 if (VK_NULL_HANDLE == fSurface) {
68 fBackendContext.reset(nullptr);
69 return;
70 }
71
jvanverth9f372462016-04-06 06:08:59 -070072 VkBool32 supported;
jvanverthb0d43522016-04-21 11:46:23 -070073 VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fBackendContext->fPhysicalDevice,
74 fPresentQueueIndex, fSurface,
75 &supported);
jvanverth9f372462016-04-06 06:08:59 -070076 if (VK_SUCCESS != res) {
77 this->destroyContext();
78 return;
79 }
80
brianosman05de2162016-05-06 13:28:57 -070081 if (!this->createSwapchain(-1, -1, params)) {
jvanverth9f372462016-04-06 06:08:59 -070082 this->destroyContext();
83 return;
84 }
85
86 // create presentQueue
87 vkGetDeviceQueue(fBackendContext->fDevice, fPresentQueueIndex, 0, &fPresentQueue);
jvanverth9f372462016-04-06 06:08:59 -070088}
89
bsalomonccde4ab2016-07-27 08:50:12 -070090bool VulkanWindowContext::createSwapchain(int width, int height,
brianosman05de2162016-05-06 13:28:57 -070091 const DisplayParams& params) {
jvanverth9f372462016-04-06 06:08:59 -070092 // check for capabilities
93 VkSurfaceCapabilitiesKHR caps;
jvanverthb0d43522016-04-21 11:46:23 -070094 VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fBackendContext->fPhysicalDevice,
95 fSurface, &caps);
jvanverth9f372462016-04-06 06:08:59 -070096 if (VK_SUCCESS != res) {
97 return false;
98 }
99
100 uint32_t surfaceFormatCount;
jvanverthb0d43522016-04-21 11:46:23 -0700101 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
102 &surfaceFormatCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700103 if (VK_SUCCESS != res) {
104 return false;
105 }
106
107 SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatKHR));
108 VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700109 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
110 &surfaceFormatCount, surfaceFormats);
jvanverth9f372462016-04-06 06:08:59 -0700111 if (VK_SUCCESS != res) {
112 return false;
113 }
114
115 uint32_t presentModeCount;
jvanverthb0d43522016-04-21 11:46:23 -0700116 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
117 &presentModeCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700118 if (VK_SUCCESS != res) {
119 return false;
120 }
121
122 SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR));
123 VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700124 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700125 &presentModeCount, presentModes);
jvanverth9f372462016-04-06 06:08:59 -0700126 if (VK_SUCCESS != res) {
127 return false;
128 }
129
130 VkExtent2D extent = caps.currentExtent;
131 // use the hints
132 if (extent.width == (uint32_t)-1) {
133 extent.width = width;
134 extent.height = height;
135 }
136
jvanverth9fab59d2016-04-06 12:08:51 -0700137 // clamp width; to protect us from broken hints
jvanverth9f372462016-04-06 06:08:59 -0700138 if (extent.width < caps.minImageExtent.width) {
139 extent.width = caps.minImageExtent.width;
140 } else if (extent.width > caps.maxImageExtent.width) {
141 extent.width = caps.maxImageExtent.width;
142 }
143 // clamp height
144 if (extent.height < caps.minImageExtent.height) {
145 extent.height = caps.minImageExtent.height;
146 } else if (extent.height > caps.maxImageExtent.height) {
147 extent.height = caps.maxImageExtent.height;
148 }
jvanverth1d155962016-05-23 13:13:36 -0700149
jvanverth9f372462016-04-06 06:08:59 -0700150 fWidth = (int)extent.width;
151 fHeight = (int)extent.height;
152
153 uint32_t imageCount = caps.minImageCount + 2;
154 if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) {
155 // Application must settle for fewer images than desired:
156 imageCount = caps.maxImageCount;
157 }
158
159 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
160 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
161 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
162 SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags);
163 SkASSERT(caps.supportedTransforms & caps.currentTransform);
164 SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
165 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR));
166 VkCompositeAlphaFlagBitsKHR composite_alpha =
167 (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ?
168 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR :
169 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
170
brianosman05de2162016-05-06 13:28:57 -0700171 // Pick our surface format. For now, just make sure it matches our sRGB request:
172 VkFormat surfaceFormat = VK_FORMAT_UNDEFINED;
173 VkColorSpaceKHR colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500174 auto srgbColorSpace = SkColorSpace::MakeSRGB();
brianosmanb109b8c2016-06-16 13:03:24 -0700175 bool wantSRGB = srgbColorSpace == params.fColorSpace;
brianosman05de2162016-05-06 13:28:57 -0700176 for (uint32_t i = 0; i < surfaceFormatCount; ++i) {
177 GrPixelConfig config;
178 if (GrVkFormatToPixelConfig(surfaceFormats[i].format, &config) &&
179 GrPixelConfigIsSRGB(config) == wantSRGB) {
180 surfaceFormat = surfaceFormats[i].format;
181 colorSpace = surfaceFormats[i].colorSpace;
182 break;
183 }
184 }
185 fDisplayParams = params;
186
187 if (VK_FORMAT_UNDEFINED == surfaceFormat) {
188 return false;
189 }
jvanvertha4b0fed2016-04-27 11:42:21 -0700190
jvanverth3d6ed3a2016-04-07 11:09:51 -0700191 // If mailbox mode is available, use it, as it is the lowest-latency non-
192 // tearing mode. If not, fall back to FIFO which is always available.
jvanverth9f372462016-04-06 06:08:59 -0700193 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700194 for (uint32_t i = 0; i < presentModeCount; ++i) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700195 // use mailbox
196 if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) {
jvanverth9f372462016-04-06 06:08:59 -0700197 mode = presentModes[i];
jvanverth3d6ed3a2016-04-07 11:09:51 -0700198 break;
jvanverth9f372462016-04-06 06:08:59 -0700199 }
200 }
201
202 VkSwapchainCreateInfoKHR swapchainCreateInfo;
203 memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR));
204 swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
205 swapchainCreateInfo.surface = fSurface;
206 swapchainCreateInfo.minImageCount = imageCount;
jvanvertha4b0fed2016-04-27 11:42:21 -0700207 swapchainCreateInfo.imageFormat = surfaceFormat;
208 swapchainCreateInfo.imageColorSpace = colorSpace;
jvanverth9f372462016-04-06 06:08:59 -0700209 swapchainCreateInfo.imageExtent = extent;
210 swapchainCreateInfo.imageArrayLayers = 1;
211 swapchainCreateInfo.imageUsage = usageFlags;
212
jvanverthb0d43522016-04-21 11:46:23 -0700213 uint32_t queueFamilies[] = { fBackendContext->fGraphicsQueueIndex, fPresentQueueIndex };
214 if (fBackendContext->fGraphicsQueueIndex != fPresentQueueIndex) {
jvanverth9f372462016-04-06 06:08:59 -0700215 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
216 swapchainCreateInfo.queueFamilyIndexCount = 2;
217 swapchainCreateInfo.pQueueFamilyIndices = queueFamilies;
218 } else {
219 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
220 swapchainCreateInfo.queueFamilyIndexCount = 0;
221 swapchainCreateInfo.pQueueFamilyIndices = nullptr;
222 }
223
Greg Daniele897b972016-10-06 13:57:57 -0400224 swapchainCreateInfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700225 swapchainCreateInfo.compositeAlpha = composite_alpha;
226 swapchainCreateInfo.presentMode = mode;
227 swapchainCreateInfo.clipped = true;
228 swapchainCreateInfo.oldSwapchain = fSwapchain;
229
jvanverthb0d43522016-04-21 11:46:23 -0700230 res = fCreateSwapchainKHR(fBackendContext->fDevice, &swapchainCreateInfo, nullptr, &fSwapchain);
jvanverth9f372462016-04-06 06:08:59 -0700231 if (VK_SUCCESS != res) {
232 return false;
233 }
234
235 // destroy the old swapchain
236 if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) {
237 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
238
239 this->destroyBuffers();
240
jvanverthb0d43522016-04-21 11:46:23 -0700241 fDestroySwapchainKHR(fBackendContext->fDevice, swapchainCreateInfo.oldSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700242 }
243
egdaniel58a8d922016-04-21 08:03:10 -0700244 this->createBuffers(swapchainCreateInfo.imageFormat);
jvanverth9f372462016-04-06 06:08:59 -0700245
246 return true;
247}
248
jvanvertha8d0d6c2016-05-05 12:32:03 -0700249void VulkanWindowContext::createBuffers(VkFormat format) {
egdaniel58a8d922016-04-21 08:03:10 -0700250 GrVkFormatToPixelConfig(format, &fPixelConfig);
251
jvanverthb0d43522016-04-21 11:46:23 -0700252 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700253 SkASSERT(fImageCount);
254 fImages = new VkImage[fImageCount];
jvanverthb0d43522016-04-21 11:46:23 -0700255 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, fImages);
jvanverth9f372462016-04-06 06:08:59 -0700256
257 // set up initial image layouts and create surfaces
258 fImageLayouts = new VkImageLayout[fImageCount];
259 fSurfaces = new sk_sp<SkSurface>[fImageCount];
260 for (uint32_t i = 0; i < fImageCount; ++i) {
261 fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED;
262
263 GrBackendRenderTargetDesc desc;
egdanielb2df0c22016-05-13 11:30:37 -0700264 GrVkImageInfo info;
jvanverth9f372462016-04-06 06:08:59 -0700265 info.fImage = fImages[i];
jvanverth9d54afc2016-09-20 09:20:03 -0700266 info.fAlloc = { VK_NULL_HANDLE, 0, 0, 0 };
egdaniel580fa592016-08-31 11:03:50 -0700267 info.fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
jvanverth9f372462016-04-06 06:08:59 -0700268 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
egdaniel58a8d922016-04-21 08:03:10 -0700269 info.fFormat = format;
jvanverth3622a172016-05-10 06:42:18 -0700270 info.fLevelCount = 1;
jvanverth9f372462016-04-06 06:08:59 -0700271 desc.fWidth = fWidth;
272 desc.fHeight = fHeight;
273 desc.fConfig = fPixelConfig;
274 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
275 desc.fSampleCnt = 0;
276 desc.fStencilBits = 0;
277 desc.fRenderTargetHandle = (GrBackendObject) &info;
jvanverthaf236b52016-05-20 06:01:06 -0700278
Brian Osmanf750fbc2017-02-08 10:47:28 -0500279 fSurfaces[i] = SkSurface::MakeFromBackendRenderTarget(fContext, desc,
280 fDisplayParams.fColorSpace,
281 &fSurfaceProps);
jvanverth9f372462016-04-06 06:08:59 -0700282 }
283
284 // create the command pool for the command buffers
285 if (VK_NULL_HANDLE == fCommandPool) {
286 VkCommandPoolCreateInfo commandPoolInfo;
287 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
288 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
289 // this needs to be on the render queue
jvanverthb0d43522016-04-21 11:46:23 -0700290 commandPoolInfo.queueFamilyIndex = fBackendContext->fGraphicsQueueIndex;
jvanverth9f372462016-04-06 06:08:59 -0700291 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
292 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
293 CreateCommandPool(fBackendContext->fDevice, &commandPoolInfo,
294 nullptr, &fCommandPool));
295 }
296
297 // set up the backbuffers
298 VkSemaphoreCreateInfo semaphoreInfo;
299 memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo));
300 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
301 semaphoreInfo.pNext = nullptr;
302 semaphoreInfo.flags = 0;
303 VkCommandBufferAllocateInfo commandBuffersInfo;
304 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
305 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
306 commandBuffersInfo.pNext = nullptr;
307 commandBuffersInfo.commandPool = fCommandPool;
308 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
309 commandBuffersInfo.commandBufferCount = 2;
310 VkFenceCreateInfo fenceInfo;
311 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
312 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
313 fenceInfo.pNext = nullptr;
314 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
315
Greg Daniel1f05f442016-10-27 16:37:17 -0400316 // we create one additional backbuffer structure here, because we want to
jvanverth9f372462016-04-06 06:08:59 -0700317 // give the command buffers they contain a chance to finish before we cycle back
318 fBackbuffers = new BackbufferInfo[fImageCount + 1];
319 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
320 fBackbuffers[i].fImageIndex = -1;
321 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
322 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
323 nullptr, &fBackbuffers[i].fAcquireSemaphore));
324 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
325 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
326 nullptr, &fBackbuffers[i].fRenderSemaphore));
327 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
328 AllocateCommandBuffers(fBackendContext->fDevice, &commandBuffersInfo,
329 fBackbuffers[i].fTransitionCmdBuffers));
330 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
331 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
332 &fBackbuffers[i].fUsageFences[0]));
333 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
334 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
335 &fBackbuffers[i].fUsageFences[1]));
336 }
337 fCurrentBackbufferIndex = fImageCount;
338}
339
jvanvertha8d0d6c2016-05-05 12:32:03 -0700340void VulkanWindowContext::destroyBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700341
342 if (fBackbuffers) {
343 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
344 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700345 WaitForFences(fBackendContext->fDevice, 2,
jvanverth9f372462016-04-06 06:08:59 -0700346 fBackbuffers[i].fUsageFences,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700347 true, UINT64_MAX));
jvanverth9f372462016-04-06 06:08:59 -0700348 fBackbuffers[i].fImageIndex = -1;
349 GR_VK_CALL(fBackendContext->fInterface,
350 DestroySemaphore(fBackendContext->fDevice,
351 fBackbuffers[i].fAcquireSemaphore,
352 nullptr));
353 GR_VK_CALL(fBackendContext->fInterface,
354 DestroySemaphore(fBackendContext->fDevice,
355 fBackbuffers[i].fRenderSemaphore,
356 nullptr));
357 GR_VK_CALL(fBackendContext->fInterface,
358 FreeCommandBuffers(fBackendContext->fDevice, fCommandPool, 2,
359 fBackbuffers[i].fTransitionCmdBuffers));
360 GR_VK_CALL(fBackendContext->fInterface,
361 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[0], 0));
362 GR_VK_CALL(fBackendContext->fInterface,
363 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[1], 0));
364 }
365 }
366
367 delete[] fBackbuffers;
368 fBackbuffers = nullptr;
369
jvanverthaf236b52016-05-20 06:01:06 -0700370 // Does this actually free the surfaces?
jvanverth9f372462016-04-06 06:08:59 -0700371 delete[] fSurfaces;
372 fSurfaces = nullptr;
373 delete[] fImageLayouts;
374 fImageLayouts = nullptr;
375 delete[] fImages;
376 fImages = nullptr;
377}
378
jvanvertha8d0d6c2016-05-05 12:32:03 -0700379VulkanWindowContext::~VulkanWindowContext() {
jvanverth9f372462016-04-06 06:08:59 -0700380 this->destroyContext();
381}
382
jvanvertha8d0d6c2016-05-05 12:32:03 -0700383void VulkanWindowContext::destroyContext() {
jvanverth9f372462016-04-06 06:08:59 -0700384 if (!fBackendContext.get()) {
385 return;
386 }
387
jvanverth85f758c2016-05-27 06:47:08 -0700388 GR_VK_CALL(fBackendContext->fInterface, QueueWaitIdle(fPresentQueue));
jvanverth9f372462016-04-06 06:08:59 -0700389 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
390
391 this->destroyBuffers();
392
393 if (VK_NULL_HANDLE != fCommandPool) {
394 GR_VK_CALL(fBackendContext->fInterface, DestroyCommandPool(fBackendContext->fDevice,
395 fCommandPool, nullptr));
396 fCommandPool = VK_NULL_HANDLE;
397 }
398
399 if (VK_NULL_HANDLE != fSwapchain) {
jvanverthb0d43522016-04-21 11:46:23 -0700400 fDestroySwapchainKHR(fBackendContext->fDevice, fSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700401 fSwapchain = VK_NULL_HANDLE;
402 }
403
404 if (VK_NULL_HANDLE != fSurface) {
jvanverthb0d43522016-04-21 11:46:23 -0700405 fDestroySurfaceKHR(fBackendContext->fInstance, fSurface, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700406 fSurface = VK_NULL_HANDLE;
407 }
408
jvanverthaf236b52016-05-20 06:01:06 -0700409 fContext->unref();
jvanverth9f372462016-04-06 06:08:59 -0700410
411 fBackendContext.reset(nullptr);
412}
413
jvanvertha8d0d6c2016-05-05 12:32:03 -0700414VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer() {
jvanverth9f372462016-04-06 06:08:59 -0700415 SkASSERT(fBackbuffers);
416
417 ++fCurrentBackbufferIndex;
egdaniel804af7d2016-09-26 12:30:46 -0700418 if (fCurrentBackbufferIndex > fImageCount) {
jvanverth9f372462016-04-06 06:08:59 -0700419 fCurrentBackbufferIndex = 0;
420 }
421
422 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
jvanverth9f372462016-04-06 06:08:59 -0700423 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
424 WaitForFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences,
425 true, UINT64_MAX));
426 return backbuffer;
427}
428
jvanverthaf236b52016-05-20 06:01:06 -0700429sk_sp<SkSurface> VulkanWindowContext::getBackbufferSurface() {
jvanverth9f372462016-04-06 06:08:59 -0700430 BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
431 SkASSERT(backbuffer);
432
433 // reset the fence
434 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
435 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
436 // semaphores should be in unsignaled state
437
438 // acquire the image
jvanverthb0d43522016-04-21 11:46:23 -0700439 VkResult res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
440 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
441 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700442 if (VK_ERROR_SURFACE_LOST_KHR == res) {
443 // need to figure out how to create a new vkSurface without the platformData*
jvanverth3d6ed3a2016-04-07 11:09:51 -0700444 // maybe use attach somehow? but need a Window
jvanverth9f372462016-04-06 06:08:59 -0700445 return nullptr;
446 }
jvanverth3d6ed3a2016-04-07 11:09:51 -0700447 if (VK_ERROR_OUT_OF_DATE_KHR == res) {
jvanverth9f372462016-04-06 06:08:59 -0700448 // tear swapchain down and try again
Greg Daniel1f05f442016-10-27 16:37:17 -0400449 if (!this->createSwapchain(-1, -1, fDisplayParams)) {
jvanverth9f372462016-04-06 06:08:59 -0700450 return nullptr;
451 }
Jim Van Verthd63c1022017-01-05 13:50:49 -0500452 backbuffer = this->getAvailableBackbuffer();
453 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
454 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
jvanverth9f372462016-04-06 06:08:59 -0700455
456 // acquire the image
jvanvertha8d0d6c2016-05-05 12:32:03 -0700457 res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
jvanverthb0d43522016-04-21 11:46:23 -0700458 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
459 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700460
461 if (VK_SUCCESS != res) {
462 return nullptr;
463 }
464 }
465
466 // set up layout transfer from initial to color attachment
467 VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex];
egdaniel580fa592016-08-31 11:03:50 -0700468 SkASSERT(VK_IMAGE_LAYOUT_UNDEFINED == layout || VK_IMAGE_LAYOUT_PRESENT_SRC_KHR == layout);
jvanverth9f372462016-04-06 06:08:59 -0700469 VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
470 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT :
471 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
472 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700473 VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
jvanverth9f372462016-04-06 06:08:59 -0700474 0 : VK_ACCESS_MEMORY_READ_BIT;
475 VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
476
477 VkImageMemoryBarrier imageMemoryBarrier = {
478 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
479 NULL, // pNext
480 srcAccessMask, // outputMask
481 dstAccessMask, // inputMask
482 layout, // oldLayout
483 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout
484 fPresentQueueIndex, // srcQueueFamilyIndex
jvanverthb0d43522016-04-21 11:46:23 -0700485 fBackendContext->fGraphicsQueueIndex, // dstQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700486 fImages[backbuffer->fImageIndex], // image
487 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
488 };
489 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
490 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[0], 0));
491 VkCommandBufferBeginInfo info;
492 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
493 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
494 info.flags = 0;
495 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
496 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info));
497
jvanvertha8d0d6c2016-05-05 12:32:03 -0700498 GR_VK_CALL(fBackendContext->fInterface,
499 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0],
500 srcStageMask, dstStageMask, 0,
501 0, nullptr,
502 0, nullptr,
503 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700504
505 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700506 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0]));
jvanverth9f372462016-04-06 06:08:59 -0700507
egdaniel58a8d922016-04-21 08:03:10 -0700508 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanverth9f372462016-04-06 06:08:59 -0700509 // insert the layout transfer into the queue and wait on the acquire
510 VkSubmitInfo submitInfo;
511 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
512 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
513 submitInfo.waitSemaphoreCount = 1;
514 submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore;
egdaniel58a8d922016-04-21 08:03:10 -0700515 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
jvanverth9f372462016-04-06 06:08:59 -0700516 submitInfo.commandBufferCount = 1;
517 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0];
518 submitInfo.signalSemaphoreCount = 0;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700519
jvanverth9f372462016-04-06 06:08:59 -0700520 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700521 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700522 backbuffer->fUsageFences[0]));
523
egdaniel580fa592016-08-31 11:03:50 -0700524 GrVkImageInfo* imageInfo;
525 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
526 surface->getRenderTargetHandle((GrBackendObject*)&imageInfo,
527 SkSurface::kFlushRead_BackendHandleAccess);
528 imageInfo->updateImageLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
529
530 return sk_ref_sp(surface);
jvanverth9f372462016-04-06 06:08:59 -0700531}
532
jvanvertha8d0d6c2016-05-05 12:32:03 -0700533void VulkanWindowContext::swapBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700534
535 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
egdaniel580fa592016-08-31 11:03:50 -0700536 GrVkImageInfo* imageInfo;
537 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
538 surface->getRenderTargetHandle((GrBackendObject*)&imageInfo,
539 SkSurface::kFlushRead_BackendHandleAccess);
540 // Check to make sure we never change the actually wrapped image
541 SkASSERT(imageInfo->fImage == fImages[backbuffer->fImageIndex]);
jvanverth9f372462016-04-06 06:08:59 -0700542
egdaniel580fa592016-08-31 11:03:50 -0700543 VkImageLayout layout = imageInfo->fImageLayout;
544 VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(layout);
jvanverth9f372462016-04-06 06:08:59 -0700545 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
egdaniel580fa592016-08-31 11:03:50 -0700546 VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(layout);
jvanverth9f372462016-04-06 06:08:59 -0700547 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
548
549 VkImageMemoryBarrier imageMemoryBarrier = {
550 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
551 NULL, // pNext
552 srcAccessMask, // outputMask
553 dstAccessMask, // inputMask
554 layout, // oldLayout
555 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout
jvanverthb0d43522016-04-21 11:46:23 -0700556 fBackendContext->fGraphicsQueueIndex, // srcQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700557 fPresentQueueIndex, // dstQueueFamilyIndex
558 fImages[backbuffer->fImageIndex], // image
559 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
560 };
561 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
562 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[1], 0));
563 VkCommandBufferBeginInfo info;
564 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
565 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
566 info.flags = 0;
567 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
568 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info));
569 GR_VK_CALL(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700570 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
571 srcStageMask, dstStageMask, 0,
572 0, nullptr,
573 0, nullptr,
574 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700575 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
576 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1]));
577
578 fImageLayouts[backbuffer->fImageIndex] = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
579
580 // insert the layout transfer into the queue and wait on the acquire
581 VkSubmitInfo submitInfo;
582 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
583 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
584 submitInfo.waitSemaphoreCount = 0;
585 submitInfo.pWaitDstStageMask = 0;
586 submitInfo.commandBufferCount = 1;
587 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[1];
588 submitInfo.signalSemaphoreCount = 1;
589 submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore;
590
591 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700592 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700593 backbuffer->fUsageFences[1]));
594
595 // Submit present operation to present queue
596 const VkPresentInfoKHR presentInfo =
597 {
598 VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType
599 NULL, // pNext
600 1, // waitSemaphoreCount
601 &backbuffer->fRenderSemaphore, // pWaitSemaphores
602 1, // swapchainCount
603 &fSwapchain, // pSwapchains
604 &backbuffer->fImageIndex, // pImageIndices
605 NULL // pResults
606 };
607
jvanverthb0d43522016-04-21 11:46:23 -0700608 fQueuePresentKHR(fPresentQueue, &presentInfo);
jvanverth9f372462016-04-06 06:08:59 -0700609}
jvanvertha8d0d6c2016-05-05 12:32:03 -0700610
611} //namespace sk_app