blob: 74c0674f2ed071f67e9a45210ff9c1d81662e27a [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"
jvanverth9f372462016-04-06 06:08:59 -070011#include "SkSurface.h"
jvanvertha8d0d6c2016-05-05 12:32:03 -070012#include "VulkanWindowContext.h"
jvanverth9f372462016-04-06 06:08:59 -070013
14#include "vk/GrVkInterface.h"
15#include "vk/GrVkUtil.h"
16#include "vk/GrVkTypes.h"
17
18#ifdef VK_USE_PLATFORM_WIN32_KHR
19// windows wants to define this as CreateSemaphoreA or CreateSemaphoreW
20#undef CreateSemaphore
21#endif
22
jvanverthb0d43522016-04-21 11:46:23 -070023#define GET_PROC(F) f ## F = (PFN_vk ## F) vkGetInstanceProcAddr(instance, "vk" #F)
24#define GET_DEV_PROC(F) f ## F = (PFN_vk ## F) vkGetDeviceProcAddr(device, "vk" #F)
25
jvanvertha8d0d6c2016-05-05 12:32:03 -070026namespace sk_app {
27
bsalomond1bdd1f2016-07-26 12:02:50 -070028VulkanWindowContext::VulkanWindowContext(const DisplayParams& params,
29 CreateVkSurfaceFn createVkSurface,
30 CanPresentFn canPresent)
jvanverthaf236b52016-05-20 06:01:06 -070031 : WindowContext()
32 , fSurface(VK_NULL_HANDLE)
jvanvertha8d0d6c2016-05-05 12:32:03 -070033 , fSwapchain(VK_NULL_HANDLE)
jvanverthaf236b52016-05-20 06:01:06 -070034 , fImages(nullptr)
35 , fImageLayouts(nullptr)
36 , fSurfaces(nullptr)
jvanvertha8d0d6c2016-05-05 12:32:03 -070037 , fCommandPool(VK_NULL_HANDLE)
38 , fBackbuffers(nullptr) {
jvanverth9f372462016-04-06 06:08:59 -070039
40 // any config code here (particularly for msaa)?
bsalomond1bdd1f2016-07-26 12:02:50 -070041 fBackendContext.reset(GrVkBackendContext::Create(&fPresentQueueIndex, canPresent));
jvanverth9f372462016-04-06 06:08:59 -070042
jvanverthb0d43522016-04-21 11:46:23 -070043 if (!(fBackendContext->fExtensions & kKHR_surface_GrVkExtensionFlag) ||
44 !(fBackendContext->fExtensions & kKHR_swapchain_GrVkExtensionFlag)) {
45 fBackendContext.reset(nullptr);
46 return;
47 }
48
49 VkInstance instance = fBackendContext->fInstance;
50 VkDevice device = fBackendContext->fDevice;
51 GET_PROC(DestroySurfaceKHR);
52 GET_PROC(GetPhysicalDeviceSurfaceSupportKHR);
53 GET_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
54 GET_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
55 GET_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
56 GET_DEV_PROC(CreateSwapchainKHR);
57 GET_DEV_PROC(DestroySwapchainKHR);
58 GET_DEV_PROC(GetSwapchainImagesKHR);
59 GET_DEV_PROC(AcquireNextImageKHR);
60 GET_DEV_PROC(QueuePresentKHR);
jvanverth9f372462016-04-06 06:08:59 -070061
jvanvertha8d0d6c2016-05-05 12:32:03 -070062 fContext = GrContext::Create(kVulkan_GrBackend, (GrBackendContext) fBackendContext.get());
jvanverth9f372462016-04-06 06:08:59 -070063
bsalomond1bdd1f2016-07-26 12:02:50 -070064 fSurface = createVkSurface(instance);
jvanverth9f372462016-04-06 06:08:59 -070065 if (VK_NULL_HANDLE == fSurface) {
66 fBackendContext.reset(nullptr);
67 return;
68 }
69
jvanverth9f372462016-04-06 06:08:59 -070070 VkBool32 supported;
jvanverthb0d43522016-04-21 11:46:23 -070071 VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fBackendContext->fPhysicalDevice,
72 fPresentQueueIndex, fSurface,
73 &supported);
jvanverth9f372462016-04-06 06:08:59 -070074 if (VK_SUCCESS != res) {
75 this->destroyContext();
76 return;
77 }
78
brianosman05de2162016-05-06 13:28:57 -070079 if (!this->createSwapchain(-1, -1, params)) {
jvanverth9f372462016-04-06 06:08:59 -070080 this->destroyContext();
81 return;
82 }
83
84 // create presentQueue
85 vkGetDeviceQueue(fBackendContext->fDevice, fPresentQueueIndex, 0, &fPresentQueue);
jvanverth9f372462016-04-06 06:08:59 -070086}
87
brianosman05de2162016-05-06 13:28:57 -070088bool VulkanWindowContext::createSwapchain(uint32_t width, uint32_t height,
89 const DisplayParams& params) {
jvanverth9f372462016-04-06 06:08:59 -070090 // check for capabilities
91 VkSurfaceCapabilitiesKHR caps;
jvanverthb0d43522016-04-21 11:46:23 -070092 VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fBackendContext->fPhysicalDevice,
93 fSurface, &caps);
jvanverth9f372462016-04-06 06:08:59 -070094 if (VK_SUCCESS != res) {
95 return false;
96 }
97
98 uint32_t surfaceFormatCount;
jvanverthb0d43522016-04-21 11:46:23 -070099 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
100 &surfaceFormatCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700101 if (VK_SUCCESS != res) {
102 return false;
103 }
104
105 SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatKHR));
106 VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700107 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
108 &surfaceFormatCount, surfaceFormats);
jvanverth9f372462016-04-06 06:08:59 -0700109 if (VK_SUCCESS != res) {
110 return false;
111 }
112
113 uint32_t presentModeCount;
jvanverthb0d43522016-04-21 11:46:23 -0700114 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
115 &presentModeCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700116 if (VK_SUCCESS != res) {
117 return false;
118 }
119
120 SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR));
121 VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700122 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700123 &presentModeCount, presentModes);
jvanverth9f372462016-04-06 06:08:59 -0700124 if (VK_SUCCESS != res) {
125 return false;
126 }
127
128 VkExtent2D extent = caps.currentExtent;
129 // use the hints
130 if (extent.width == (uint32_t)-1) {
131 extent.width = width;
132 extent.height = height;
133 }
134
jvanverth9fab59d2016-04-06 12:08:51 -0700135 // clamp width; to protect us from broken hints
jvanverth9f372462016-04-06 06:08:59 -0700136 if (extent.width < caps.minImageExtent.width) {
137 extent.width = caps.minImageExtent.width;
138 } else if (extent.width > caps.maxImageExtent.width) {
139 extent.width = caps.maxImageExtent.width;
140 }
141 // clamp height
142 if (extent.height < caps.minImageExtent.height) {
143 extent.height = caps.minImageExtent.height;
144 } else if (extent.height > caps.maxImageExtent.height) {
145 extent.height = caps.maxImageExtent.height;
146 }
jvanverth1d155962016-05-23 13:13:36 -0700147
jvanverth9f372462016-04-06 06:08:59 -0700148 fWidth = (int)extent.width;
149 fHeight = (int)extent.height;
150
151 uint32_t imageCount = caps.minImageCount + 2;
152 if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) {
153 // Application must settle for fewer images than desired:
154 imageCount = caps.maxImageCount;
155 }
156
157 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
158 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
159 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
160 SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags);
161 SkASSERT(caps.supportedTransforms & caps.currentTransform);
162 SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
163 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR));
164 VkCompositeAlphaFlagBitsKHR composite_alpha =
165 (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ?
166 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR :
167 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
168
brianosman05de2162016-05-06 13:28:57 -0700169 // Pick our surface format. For now, just make sure it matches our sRGB request:
170 VkFormat surfaceFormat = VK_FORMAT_UNDEFINED;
171 VkColorSpaceKHR colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
brianosmanb109b8c2016-06-16 13:03:24 -0700172 auto srgbColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
173 bool wantSRGB = srgbColorSpace == params.fColorSpace;
brianosman05de2162016-05-06 13:28:57 -0700174 for (uint32_t i = 0; i < surfaceFormatCount; ++i) {
175 GrPixelConfig config;
176 if (GrVkFormatToPixelConfig(surfaceFormats[i].format, &config) &&
177 GrPixelConfigIsSRGB(config) == wantSRGB) {
178 surfaceFormat = surfaceFormats[i].format;
179 colorSpace = surfaceFormats[i].colorSpace;
180 break;
181 }
182 }
183 fDisplayParams = params;
184
185 if (VK_FORMAT_UNDEFINED == surfaceFormat) {
186 return false;
187 }
jvanvertha4b0fed2016-04-27 11:42:21 -0700188
jvanverth3d6ed3a2016-04-07 11:09:51 -0700189 // If mailbox mode is available, use it, as it is the lowest-latency non-
190 // tearing mode. If not, fall back to FIFO which is always available.
jvanverth9f372462016-04-06 06:08:59 -0700191 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700192 for (uint32_t i = 0; i < presentModeCount; ++i) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700193 // use mailbox
194 if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) {
jvanverth9f372462016-04-06 06:08:59 -0700195 mode = presentModes[i];
jvanverth3d6ed3a2016-04-07 11:09:51 -0700196 break;
jvanverth9f372462016-04-06 06:08:59 -0700197 }
198 }
199
200 VkSwapchainCreateInfoKHR swapchainCreateInfo;
201 memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR));
202 swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
203 swapchainCreateInfo.surface = fSurface;
204 swapchainCreateInfo.minImageCount = imageCount;
jvanvertha4b0fed2016-04-27 11:42:21 -0700205 swapchainCreateInfo.imageFormat = surfaceFormat;
206 swapchainCreateInfo.imageColorSpace = colorSpace;
jvanverth9f372462016-04-06 06:08:59 -0700207 swapchainCreateInfo.imageExtent = extent;
208 swapchainCreateInfo.imageArrayLayers = 1;
209 swapchainCreateInfo.imageUsage = usageFlags;
210
jvanverthb0d43522016-04-21 11:46:23 -0700211 uint32_t queueFamilies[] = { fBackendContext->fGraphicsQueueIndex, fPresentQueueIndex };
212 if (fBackendContext->fGraphicsQueueIndex != fPresentQueueIndex) {
jvanverth9f372462016-04-06 06:08:59 -0700213 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
214 swapchainCreateInfo.queueFamilyIndexCount = 2;
215 swapchainCreateInfo.pQueueFamilyIndices = queueFamilies;
216 } else {
217 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
218 swapchainCreateInfo.queueFamilyIndexCount = 0;
219 swapchainCreateInfo.pQueueFamilyIndices = nullptr;
220 }
221
222 swapchainCreateInfo.preTransform = caps.currentTransform;;
223 swapchainCreateInfo.compositeAlpha = composite_alpha;
224 swapchainCreateInfo.presentMode = mode;
225 swapchainCreateInfo.clipped = true;
226 swapchainCreateInfo.oldSwapchain = fSwapchain;
227
jvanverthb0d43522016-04-21 11:46:23 -0700228 res = fCreateSwapchainKHR(fBackendContext->fDevice, &swapchainCreateInfo, nullptr, &fSwapchain);
jvanverth9f372462016-04-06 06:08:59 -0700229 if (VK_SUCCESS != res) {
230 return false;
231 }
232
233 // destroy the old swapchain
234 if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) {
235 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
236
237 this->destroyBuffers();
238
jvanverthb0d43522016-04-21 11:46:23 -0700239 fDestroySwapchainKHR(fBackendContext->fDevice, swapchainCreateInfo.oldSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700240 }
241
egdaniel58a8d922016-04-21 08:03:10 -0700242 this->createBuffers(swapchainCreateInfo.imageFormat);
jvanverth9f372462016-04-06 06:08:59 -0700243
244 return true;
245}
246
jvanvertha8d0d6c2016-05-05 12:32:03 -0700247void VulkanWindowContext::createBuffers(VkFormat format) {
egdaniel58a8d922016-04-21 08:03:10 -0700248 GrVkFormatToPixelConfig(format, &fPixelConfig);
249
jvanverthb0d43522016-04-21 11:46:23 -0700250 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700251 SkASSERT(fImageCount);
252 fImages = new VkImage[fImageCount];
jvanverthb0d43522016-04-21 11:46:23 -0700253 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, fImages);
jvanverth9f372462016-04-06 06:08:59 -0700254
255 // set up initial image layouts and create surfaces
256 fImageLayouts = new VkImageLayout[fImageCount];
jvanverthaf236b52016-05-20 06:01:06 -0700257 fRenderTargets = new sk_sp<GrRenderTarget>[fImageCount];
jvanverth9f372462016-04-06 06:08:59 -0700258 fSurfaces = new sk_sp<SkSurface>[fImageCount];
259 for (uint32_t i = 0; i < fImageCount; ++i) {
260 fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED;
261
262 GrBackendRenderTargetDesc desc;
egdanielb2df0c22016-05-13 11:30:37 -0700263 GrVkImageInfo info;
jvanverth9f372462016-04-06 06:08:59 -0700264 info.fImage = fImages[i];
jvanverth6b6ffc42016-06-13 14:28:07 -0700265 info.fAlloc = { VK_NULL_HANDLE, 0, 0 };
jvanverth9f372462016-04-06 06:08:59 -0700266 info.fImageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
267 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
egdaniel58a8d922016-04-21 08:03:10 -0700268 info.fFormat = format;
jvanverth3622a172016-05-10 06:42:18 -0700269 info.fLevelCount = 1;
jvanverth9f372462016-04-06 06:08:59 -0700270 desc.fWidth = fWidth;
271 desc.fHeight = fHeight;
272 desc.fConfig = fPixelConfig;
273 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
274 desc.fSampleCnt = 0;
275 desc.fStencilBits = 0;
276 desc.fRenderTargetHandle = (GrBackendObject) &info;
jvanverthaf236b52016-05-20 06:01:06 -0700277
bsalomonc7b4b282016-07-19 09:46:29 -0700278 fSurfaces[i] = this->createRenderSurface(desc, 24);
jvanverth9f372462016-04-06 06:08:59 -0700279 }
280
281 // create the command pool for the command buffers
282 if (VK_NULL_HANDLE == fCommandPool) {
283 VkCommandPoolCreateInfo commandPoolInfo;
284 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
285 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
286 // this needs to be on the render queue
jvanverthb0d43522016-04-21 11:46:23 -0700287 commandPoolInfo.queueFamilyIndex = fBackendContext->fGraphicsQueueIndex;
jvanverth9f372462016-04-06 06:08:59 -0700288 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
289 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
290 CreateCommandPool(fBackendContext->fDevice, &commandPoolInfo,
291 nullptr, &fCommandPool));
292 }
293
294 // set up the backbuffers
295 VkSemaphoreCreateInfo semaphoreInfo;
296 memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo));
297 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
298 semaphoreInfo.pNext = nullptr;
299 semaphoreInfo.flags = 0;
300 VkCommandBufferAllocateInfo commandBuffersInfo;
301 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
302 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
303 commandBuffersInfo.pNext = nullptr;
304 commandBuffersInfo.commandPool = fCommandPool;
305 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
306 commandBuffersInfo.commandBufferCount = 2;
307 VkFenceCreateInfo fenceInfo;
308 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
309 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
310 fenceInfo.pNext = nullptr;
311 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
312
313 // we create one additional backbuffer structure here, because we want to
314 // give the command buffers they contain a chance to finish before we cycle back
315 fBackbuffers = new BackbufferInfo[fImageCount + 1];
316 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
317 fBackbuffers[i].fImageIndex = -1;
318 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
319 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
320 nullptr, &fBackbuffers[i].fAcquireSemaphore));
321 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
322 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
323 nullptr, &fBackbuffers[i].fRenderSemaphore));
324 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
325 AllocateCommandBuffers(fBackendContext->fDevice, &commandBuffersInfo,
326 fBackbuffers[i].fTransitionCmdBuffers));
327 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
328 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
329 &fBackbuffers[i].fUsageFences[0]));
330 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
331 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
332 &fBackbuffers[i].fUsageFences[1]));
333 }
334 fCurrentBackbufferIndex = fImageCount;
335}
336
jvanvertha8d0d6c2016-05-05 12:32:03 -0700337void VulkanWindowContext::destroyBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700338
339 if (fBackbuffers) {
340 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
341 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700342 WaitForFences(fBackendContext->fDevice, 2,
jvanverth9f372462016-04-06 06:08:59 -0700343 fBackbuffers[i].fUsageFences,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700344 true, UINT64_MAX));
jvanverth9f372462016-04-06 06:08:59 -0700345 fBackbuffers[i].fImageIndex = -1;
346 GR_VK_CALL(fBackendContext->fInterface,
347 DestroySemaphore(fBackendContext->fDevice,
348 fBackbuffers[i].fAcquireSemaphore,
349 nullptr));
350 GR_VK_CALL(fBackendContext->fInterface,
351 DestroySemaphore(fBackendContext->fDevice,
352 fBackbuffers[i].fRenderSemaphore,
353 nullptr));
354 GR_VK_CALL(fBackendContext->fInterface,
355 FreeCommandBuffers(fBackendContext->fDevice, fCommandPool, 2,
356 fBackbuffers[i].fTransitionCmdBuffers));
357 GR_VK_CALL(fBackendContext->fInterface,
358 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[0], 0));
359 GR_VK_CALL(fBackendContext->fInterface,
360 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[1], 0));
361 }
362 }
363
364 delete[] fBackbuffers;
365 fBackbuffers = nullptr;
366
jvanverthaf236b52016-05-20 06:01:06 -0700367 // Does this actually free the surfaces?
jvanverth9f372462016-04-06 06:08:59 -0700368 delete[] fSurfaces;
369 fSurfaces = nullptr;
jvanverthaf236b52016-05-20 06:01:06 -0700370 delete[] fRenderTargets;
371 fRenderTargets = nullptr;
jvanverth9f372462016-04-06 06:08:59 -0700372 delete[] fImageLayouts;
373 fImageLayouts = nullptr;
374 delete[] fImages;
375 fImages = nullptr;
376}
377
jvanvertha8d0d6c2016-05-05 12:32:03 -0700378VulkanWindowContext::~VulkanWindowContext() {
jvanverth9f372462016-04-06 06:08:59 -0700379 this->destroyContext();
380}
381
jvanvertha8d0d6c2016-05-05 12:32:03 -0700382void VulkanWindowContext::destroyContext() {
jvanverth9f372462016-04-06 06:08:59 -0700383 if (!fBackendContext.get()) {
384 return;
385 }
386
jvanverth85f758c2016-05-27 06:47:08 -0700387 GR_VK_CALL(fBackendContext->fInterface, QueueWaitIdle(fPresentQueue));
jvanverth9f372462016-04-06 06:08:59 -0700388 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
389
390 this->destroyBuffers();
391
392 if (VK_NULL_HANDLE != fCommandPool) {
393 GR_VK_CALL(fBackendContext->fInterface, DestroyCommandPool(fBackendContext->fDevice,
394 fCommandPool, nullptr));
395 fCommandPool = VK_NULL_HANDLE;
396 }
397
398 if (VK_NULL_HANDLE != fSwapchain) {
jvanverthb0d43522016-04-21 11:46:23 -0700399 fDestroySwapchainKHR(fBackendContext->fDevice, fSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700400 fSwapchain = VK_NULL_HANDLE;
401 }
402
403 if (VK_NULL_HANDLE != fSurface) {
jvanverthb0d43522016-04-21 11:46:23 -0700404 fDestroySurfaceKHR(fBackendContext->fInstance, fSurface, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700405 fSurface = VK_NULL_HANDLE;
406 }
407
jvanverthaf236b52016-05-20 06:01:06 -0700408 fContext->unref();
jvanverth9f372462016-04-06 06:08:59 -0700409
410 fBackendContext.reset(nullptr);
411}
412
jvanvertha8d0d6c2016-05-05 12:32:03 -0700413VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer() {
jvanverth9f372462016-04-06 06:08:59 -0700414 SkASSERT(fBackbuffers);
415
416 ++fCurrentBackbufferIndex;
417 if (fCurrentBackbufferIndex > fImageCount) {
418 fCurrentBackbufferIndex = 0;
419 }
420
421 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
422
423 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
brianosman05de2162016-05-06 13:28:57 -0700449 if (!this->createSwapchain(0, 0, fDisplayParams)) {
jvanverth9f372462016-04-06 06:08:59 -0700450 return nullptr;
451 }
452
453 // acquire the image
jvanvertha8d0d6c2016-05-05 12:32:03 -0700454 res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
jvanverthb0d43522016-04-21 11:46:23 -0700455 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
456 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700457
458 if (VK_SUCCESS != res) {
459 return nullptr;
460 }
461 }
462
463 // set up layout transfer from initial to color attachment
464 VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex];
465 VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
466 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT :
467 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
468 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700469 VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
jvanverth9f372462016-04-06 06:08:59 -0700470 0 : VK_ACCESS_MEMORY_READ_BIT;
471 VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
472
473 VkImageMemoryBarrier imageMemoryBarrier = {
474 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
475 NULL, // pNext
476 srcAccessMask, // outputMask
477 dstAccessMask, // inputMask
478 layout, // oldLayout
479 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout
480 fPresentQueueIndex, // srcQueueFamilyIndex
jvanverthb0d43522016-04-21 11:46:23 -0700481 fBackendContext->fGraphicsQueueIndex, // dstQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700482 fImages[backbuffer->fImageIndex], // image
483 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
484 };
485 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
486 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[0], 0));
487 VkCommandBufferBeginInfo info;
488 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
489 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
490 info.flags = 0;
491 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
492 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info));
493
jvanvertha8d0d6c2016-05-05 12:32:03 -0700494 GR_VK_CALL(fBackendContext->fInterface,
495 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0],
496 srcStageMask, dstStageMask, 0,
497 0, nullptr,
498 0, nullptr,
499 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700500
501 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700502 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0]));
jvanverth9f372462016-04-06 06:08:59 -0700503
egdaniel58a8d922016-04-21 08:03:10 -0700504 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanverth9f372462016-04-06 06:08:59 -0700505 // insert the layout transfer into the queue and wait on the acquire
506 VkSubmitInfo submitInfo;
507 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
508 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
509 submitInfo.waitSemaphoreCount = 1;
510 submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore;
egdaniel58a8d922016-04-21 08:03:10 -0700511 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
jvanverth9f372462016-04-06 06:08:59 -0700512 submitInfo.commandBufferCount = 1;
513 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0];
514 submitInfo.signalSemaphoreCount = 0;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700515
jvanverth9f372462016-04-06 06:08:59 -0700516 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700517 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700518 backbuffer->fUsageFences[0]));
519
jvanverthaf236b52016-05-20 06:01:06 -0700520 return sk_ref_sp(fSurfaces[backbuffer->fImageIndex].get());
jvanverth9f372462016-04-06 06:08:59 -0700521}
522
jvanvertha8d0d6c2016-05-05 12:32:03 -0700523void VulkanWindowContext::swapBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700524
525 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
526
jvanverthaf236b52016-05-20 06:01:06 -0700527 this->presentRenderSurface(fSurfaces[backbuffer->fImageIndex],
528 fRenderTargets[backbuffer->fImageIndex], 24);
529
jvanverth9f372462016-04-06 06:08:59 -0700530 VkImageLayout layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
531 VkPipelineStageFlags srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
532 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
533 VkAccessFlags srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
534 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
535
536 VkImageMemoryBarrier imageMemoryBarrier = {
537 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
538 NULL, // pNext
539 srcAccessMask, // outputMask
540 dstAccessMask, // inputMask
541 layout, // oldLayout
542 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout
jvanverthb0d43522016-04-21 11:46:23 -0700543 fBackendContext->fGraphicsQueueIndex, // srcQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700544 fPresentQueueIndex, // dstQueueFamilyIndex
545 fImages[backbuffer->fImageIndex], // image
546 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
547 };
548 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
549 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[1], 0));
550 VkCommandBufferBeginInfo info;
551 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
552 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
553 info.flags = 0;
554 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
555 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info));
556 GR_VK_CALL(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700557 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
558 srcStageMask, dstStageMask, 0,
559 0, nullptr,
560 0, nullptr,
561 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700562 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
563 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1]));
564
565 fImageLayouts[backbuffer->fImageIndex] = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
566
567 // insert the layout transfer into the queue and wait on the acquire
568 VkSubmitInfo submitInfo;
569 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
570 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
571 submitInfo.waitSemaphoreCount = 0;
572 submitInfo.pWaitDstStageMask = 0;
573 submitInfo.commandBufferCount = 1;
574 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[1];
575 submitInfo.signalSemaphoreCount = 1;
576 submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore;
577
578 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700579 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700580 backbuffer->fUsageFences[1]));
581
582 // Submit present operation to present queue
583 const VkPresentInfoKHR presentInfo =
584 {
585 VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType
586 NULL, // pNext
587 1, // waitSemaphoreCount
588 &backbuffer->fRenderSemaphore, // pWaitSemaphores
589 1, // swapchainCount
590 &fSwapchain, // pSwapchains
591 &backbuffer->fImageIndex, // pImageIndices
592 NULL // pResults
593 };
594
jvanverthb0d43522016-04-21 11:46:23 -0700595 fQueuePresentKHR(fPresentQueue, &presentInfo);
jvanverth9f372462016-04-06 06:08:59 -0700596
597}
jvanvertha8d0d6c2016-05-05 12:32:03 -0700598
599} //namespace sk_app