blob: 4802e214b4eaba72fac5f1c69d07ebdb50888a6c [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 Sarett1f2fff22017-02-07 17:06:09 +0000174 auto srgbColorSpace = SkColorSpace::MakeNamed(SkColorSpace::kSRGB_Named);
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
bsalomonc7b4b282016-07-19 09:46:29 -0700279 fSurfaces[i] = this->createRenderSurface(desc, 24);
jvanverth9f372462016-04-06 06:08:59 -0700280 }
281
282 // create the command pool for the command buffers
283 if (VK_NULL_HANDLE == fCommandPool) {
284 VkCommandPoolCreateInfo commandPoolInfo;
285 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
286 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
287 // this needs to be on the render queue
jvanverthb0d43522016-04-21 11:46:23 -0700288 commandPoolInfo.queueFamilyIndex = fBackendContext->fGraphicsQueueIndex;
jvanverth9f372462016-04-06 06:08:59 -0700289 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
290 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
291 CreateCommandPool(fBackendContext->fDevice, &commandPoolInfo,
292 nullptr, &fCommandPool));
293 }
294
295 // set up the backbuffers
296 VkSemaphoreCreateInfo semaphoreInfo;
297 memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo));
298 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
299 semaphoreInfo.pNext = nullptr;
300 semaphoreInfo.flags = 0;
301 VkCommandBufferAllocateInfo commandBuffersInfo;
302 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
303 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
304 commandBuffersInfo.pNext = nullptr;
305 commandBuffersInfo.commandPool = fCommandPool;
306 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
307 commandBuffersInfo.commandBufferCount = 2;
308 VkFenceCreateInfo fenceInfo;
309 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
310 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
311 fenceInfo.pNext = nullptr;
312 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
313
Greg Daniel1f05f442016-10-27 16:37:17 -0400314 // we create one additional backbuffer structure here, because we want to
jvanverth9f372462016-04-06 06:08:59 -0700315 // give the command buffers they contain a chance to finish before we cycle back
316 fBackbuffers = new BackbufferInfo[fImageCount + 1];
317 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
318 fBackbuffers[i].fImageIndex = -1;
319 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
320 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
321 nullptr, &fBackbuffers[i].fAcquireSemaphore));
322 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
323 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
324 nullptr, &fBackbuffers[i].fRenderSemaphore));
325 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
326 AllocateCommandBuffers(fBackendContext->fDevice, &commandBuffersInfo,
327 fBackbuffers[i].fTransitionCmdBuffers));
328 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
329 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
330 &fBackbuffers[i].fUsageFences[0]));
331 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
332 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
333 &fBackbuffers[i].fUsageFences[1]));
334 }
335 fCurrentBackbufferIndex = fImageCount;
336}
337
jvanvertha8d0d6c2016-05-05 12:32:03 -0700338void VulkanWindowContext::destroyBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700339
340 if (fBackbuffers) {
341 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
342 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700343 WaitForFences(fBackendContext->fDevice, 2,
jvanverth9f372462016-04-06 06:08:59 -0700344 fBackbuffers[i].fUsageFences,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700345 true, UINT64_MAX));
jvanverth9f372462016-04-06 06:08:59 -0700346 fBackbuffers[i].fImageIndex = -1;
347 GR_VK_CALL(fBackendContext->fInterface,
348 DestroySemaphore(fBackendContext->fDevice,
349 fBackbuffers[i].fAcquireSemaphore,
350 nullptr));
351 GR_VK_CALL(fBackendContext->fInterface,
352 DestroySemaphore(fBackendContext->fDevice,
353 fBackbuffers[i].fRenderSemaphore,
354 nullptr));
355 GR_VK_CALL(fBackendContext->fInterface,
356 FreeCommandBuffers(fBackendContext->fDevice, fCommandPool, 2,
357 fBackbuffers[i].fTransitionCmdBuffers));
358 GR_VK_CALL(fBackendContext->fInterface,
359 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[0], 0));
360 GR_VK_CALL(fBackendContext->fInterface,
361 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[1], 0));
362 }
363 }
364
365 delete[] fBackbuffers;
366 fBackbuffers = nullptr;
367
jvanverthaf236b52016-05-20 06:01:06 -0700368 // Does this actually free the surfaces?
jvanverth9f372462016-04-06 06:08:59 -0700369 delete[] fSurfaces;
370 fSurfaces = nullptr;
371 delete[] fImageLayouts;
372 fImageLayouts = nullptr;
373 delete[] fImages;
374 fImages = nullptr;
375}
376
jvanvertha8d0d6c2016-05-05 12:32:03 -0700377VulkanWindowContext::~VulkanWindowContext() {
jvanverth9f372462016-04-06 06:08:59 -0700378 this->destroyContext();
379}
380
jvanvertha8d0d6c2016-05-05 12:32:03 -0700381void VulkanWindowContext::destroyContext() {
jvanverth9f372462016-04-06 06:08:59 -0700382 if (!fBackendContext.get()) {
383 return;
384 }
385
jvanverth85f758c2016-05-27 06:47:08 -0700386 GR_VK_CALL(fBackendContext->fInterface, QueueWaitIdle(fPresentQueue));
jvanverth9f372462016-04-06 06:08:59 -0700387 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
388
389 this->destroyBuffers();
390
391 if (VK_NULL_HANDLE != fCommandPool) {
392 GR_VK_CALL(fBackendContext->fInterface, DestroyCommandPool(fBackendContext->fDevice,
393 fCommandPool, nullptr));
394 fCommandPool = VK_NULL_HANDLE;
395 }
396
397 if (VK_NULL_HANDLE != fSwapchain) {
jvanverthb0d43522016-04-21 11:46:23 -0700398 fDestroySwapchainKHR(fBackendContext->fDevice, fSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700399 fSwapchain = VK_NULL_HANDLE;
400 }
401
402 if (VK_NULL_HANDLE != fSurface) {
jvanverthb0d43522016-04-21 11:46:23 -0700403 fDestroySurfaceKHR(fBackendContext->fInstance, fSurface, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700404 fSurface = VK_NULL_HANDLE;
405 }
406
jvanverthaf236b52016-05-20 06:01:06 -0700407 fContext->unref();
jvanverth9f372462016-04-06 06:08:59 -0700408
409 fBackendContext.reset(nullptr);
410}
411
jvanvertha8d0d6c2016-05-05 12:32:03 -0700412VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer() {
jvanverth9f372462016-04-06 06:08:59 -0700413 SkASSERT(fBackbuffers);
414
415 ++fCurrentBackbufferIndex;
egdaniel804af7d2016-09-26 12:30:46 -0700416 if (fCurrentBackbufferIndex > fImageCount) {
jvanverth9f372462016-04-06 06:08:59 -0700417 fCurrentBackbufferIndex = 0;
418 }
419
420 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
jvanverth9f372462016-04-06 06:08:59 -0700421 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
422 WaitForFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences,
423 true, UINT64_MAX));
424 return backbuffer;
425}
426
jvanverthaf236b52016-05-20 06:01:06 -0700427sk_sp<SkSurface> VulkanWindowContext::getBackbufferSurface() {
jvanverth9f372462016-04-06 06:08:59 -0700428 BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
429 SkASSERT(backbuffer);
430
431 // reset the fence
432 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
433 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
434 // semaphores should be in unsignaled state
435
436 // acquire the image
jvanverthb0d43522016-04-21 11:46:23 -0700437 VkResult res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
438 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
439 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700440 if (VK_ERROR_SURFACE_LOST_KHR == res) {
441 // need to figure out how to create a new vkSurface without the platformData*
jvanverth3d6ed3a2016-04-07 11:09:51 -0700442 // maybe use attach somehow? but need a Window
jvanverth9f372462016-04-06 06:08:59 -0700443 return nullptr;
444 }
jvanverth3d6ed3a2016-04-07 11:09:51 -0700445 if (VK_ERROR_OUT_OF_DATE_KHR == res) {
jvanverth9f372462016-04-06 06:08:59 -0700446 // tear swapchain down and try again
Greg Daniel1f05f442016-10-27 16:37:17 -0400447 if (!this->createSwapchain(-1, -1, fDisplayParams)) {
jvanverth9f372462016-04-06 06:08:59 -0700448 return nullptr;
449 }
Jim Van Verthd63c1022017-01-05 13:50:49 -0500450 backbuffer = this->getAvailableBackbuffer();
451 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
452 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
jvanverth9f372462016-04-06 06:08:59 -0700453
454 // acquire the image
jvanvertha8d0d6c2016-05-05 12:32:03 -0700455 res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
jvanverthb0d43522016-04-21 11:46:23 -0700456 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
457 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700458
459 if (VK_SUCCESS != res) {
460 return nullptr;
461 }
462 }
463
464 // set up layout transfer from initial to color attachment
465 VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex];
egdaniel580fa592016-08-31 11:03:50 -0700466 SkASSERT(VK_IMAGE_LAYOUT_UNDEFINED == layout || VK_IMAGE_LAYOUT_PRESENT_SRC_KHR == layout);
jvanverth9f372462016-04-06 06:08:59 -0700467 VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
468 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT :
469 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
470 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700471 VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
jvanverth9f372462016-04-06 06:08:59 -0700472 0 : VK_ACCESS_MEMORY_READ_BIT;
473 VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
474
475 VkImageMemoryBarrier imageMemoryBarrier = {
476 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
477 NULL, // pNext
478 srcAccessMask, // outputMask
479 dstAccessMask, // inputMask
480 layout, // oldLayout
481 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout
482 fPresentQueueIndex, // srcQueueFamilyIndex
jvanverthb0d43522016-04-21 11:46:23 -0700483 fBackendContext->fGraphicsQueueIndex, // dstQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700484 fImages[backbuffer->fImageIndex], // image
485 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
486 };
487 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
488 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[0], 0));
489 VkCommandBufferBeginInfo info;
490 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
491 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
492 info.flags = 0;
493 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
494 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info));
495
jvanvertha8d0d6c2016-05-05 12:32:03 -0700496 GR_VK_CALL(fBackendContext->fInterface,
497 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0],
498 srcStageMask, dstStageMask, 0,
499 0, nullptr,
500 0, nullptr,
501 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700502
503 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700504 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0]));
jvanverth9f372462016-04-06 06:08:59 -0700505
egdaniel58a8d922016-04-21 08:03:10 -0700506 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanverth9f372462016-04-06 06:08:59 -0700507 // insert the layout transfer into the queue and wait on the acquire
508 VkSubmitInfo submitInfo;
509 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
510 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
511 submitInfo.waitSemaphoreCount = 1;
512 submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore;
egdaniel58a8d922016-04-21 08:03:10 -0700513 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
jvanverth9f372462016-04-06 06:08:59 -0700514 submitInfo.commandBufferCount = 1;
515 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0];
516 submitInfo.signalSemaphoreCount = 0;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700517
jvanverth9f372462016-04-06 06:08:59 -0700518 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700519 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700520 backbuffer->fUsageFences[0]));
521
egdaniel580fa592016-08-31 11:03:50 -0700522 GrVkImageInfo* imageInfo;
523 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
524 surface->getRenderTargetHandle((GrBackendObject*)&imageInfo,
525 SkSurface::kFlushRead_BackendHandleAccess);
526 imageInfo->updateImageLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
527
528 return sk_ref_sp(surface);
jvanverth9f372462016-04-06 06:08:59 -0700529}
530
jvanvertha8d0d6c2016-05-05 12:32:03 -0700531void VulkanWindowContext::swapBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700532
533 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
egdaniel580fa592016-08-31 11:03:50 -0700534 GrVkImageInfo* imageInfo;
535 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
536 surface->getRenderTargetHandle((GrBackendObject*)&imageInfo,
537 SkSurface::kFlushRead_BackendHandleAccess);
538 // Check to make sure we never change the actually wrapped image
539 SkASSERT(imageInfo->fImage == fImages[backbuffer->fImageIndex]);
jvanverth9f372462016-04-06 06:08:59 -0700540
egdaniel580fa592016-08-31 11:03:50 -0700541 VkImageLayout layout = imageInfo->fImageLayout;
542 VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(layout);
jvanverth9f372462016-04-06 06:08:59 -0700543 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
egdaniel580fa592016-08-31 11:03:50 -0700544 VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(layout);
jvanverth9f372462016-04-06 06:08:59 -0700545 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
546
547 VkImageMemoryBarrier imageMemoryBarrier = {
548 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
549 NULL, // pNext
550 srcAccessMask, // outputMask
551 dstAccessMask, // inputMask
552 layout, // oldLayout
553 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout
jvanverthb0d43522016-04-21 11:46:23 -0700554 fBackendContext->fGraphicsQueueIndex, // srcQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700555 fPresentQueueIndex, // dstQueueFamilyIndex
556 fImages[backbuffer->fImageIndex], // image
557 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
558 };
559 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
560 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[1], 0));
561 VkCommandBufferBeginInfo info;
562 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
563 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
564 info.flags = 0;
565 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
566 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info));
567 GR_VK_CALL(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700568 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
569 srcStageMask, dstStageMask, 0,
570 0, nullptr,
571 0, nullptr,
572 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700573 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
574 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1]));
575
576 fImageLayouts[backbuffer->fImageIndex] = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
577
578 // insert the layout transfer into the queue and wait on the acquire
579 VkSubmitInfo submitInfo;
580 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
581 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
582 submitInfo.waitSemaphoreCount = 0;
583 submitInfo.pWaitDstStageMask = 0;
584 submitInfo.commandBufferCount = 1;
585 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[1];
586 submitInfo.signalSemaphoreCount = 1;
587 submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore;
588
589 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700590 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700591 backbuffer->fUsageFences[1]));
592
593 // Submit present operation to present queue
594 const VkPresentInfoKHR presentInfo =
595 {
596 VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType
597 NULL, // pNext
598 1, // waitSemaphoreCount
599 &backbuffer->fRenderSemaphore, // pWaitSemaphores
600 1, // swapchainCount
601 &fSwapchain, // pSwapchains
602 &backbuffer->fImageIndex, // pImageIndices
603 NULL // pResults
604 };
605
jvanverthb0d43522016-04-21 11:46:23 -0700606 fQueuePresentKHR(fPresentQueue, &presentInfo);
jvanverth9f372462016-04-06 06:08:59 -0700607}
jvanvertha8d0d6c2016-05-05 12:32:03 -0700608
609} //namespace sk_app