blob: 589a7b8c1ce063e0df01b2c8ae2ba2068bfcac64 [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"
egdaniel580fa592016-08-31 11:03:50 -070015#include "vk/GrVkMemory.h"
jvanverth9f372462016-04-06 06:08:59 -070016#include "vk/GrVkUtil.h"
17#include "vk/GrVkTypes.h"
18
19#ifdef VK_USE_PLATFORM_WIN32_KHR
20// windows wants to define this as CreateSemaphoreA or CreateSemaphoreW
21#undef CreateSemaphore
22#endif
23
jvanverthb0d43522016-04-21 11:46:23 -070024#define GET_PROC(F) f ## F = (PFN_vk ## F) vkGetInstanceProcAddr(instance, "vk" #F)
25#define GET_DEV_PROC(F) f ## F = (PFN_vk ## F) vkGetDeviceProcAddr(device, "vk" #F)
26
jvanvertha8d0d6c2016-05-05 12:32:03 -070027namespace sk_app {
28
bsalomond1bdd1f2016-07-26 12:02:50 -070029VulkanWindowContext::VulkanWindowContext(const DisplayParams& params,
30 CreateVkSurfaceFn createVkSurface,
31 CanPresentFn canPresent)
jvanverthaf236b52016-05-20 06:01:06 -070032 : WindowContext()
33 , fSurface(VK_NULL_HANDLE)
jvanvertha8d0d6c2016-05-05 12:32:03 -070034 , fSwapchain(VK_NULL_HANDLE)
jvanverthaf236b52016-05-20 06:01:06 -070035 , fImages(nullptr)
36 , fImageLayouts(nullptr)
37 , fSurfaces(nullptr)
jvanvertha8d0d6c2016-05-05 12:32:03 -070038 , fCommandPool(VK_NULL_HANDLE)
39 , fBackbuffers(nullptr) {
jvanverth9f372462016-04-06 06:08:59 -070040
41 // any config code here (particularly for msaa)?
bsalomond1bdd1f2016-07-26 12:02:50 -070042 fBackendContext.reset(GrVkBackendContext::Create(&fPresentQueueIndex, canPresent));
jvanverth9f372462016-04-06 06:08:59 -070043
jvanverthb0d43522016-04-21 11:46:23 -070044 if (!(fBackendContext->fExtensions & kKHR_surface_GrVkExtensionFlag) ||
45 !(fBackendContext->fExtensions & kKHR_swapchain_GrVkExtensionFlag)) {
46 fBackendContext.reset(nullptr);
47 return;
48 }
49
50 VkInstance instance = fBackendContext->fInstance;
51 VkDevice device = fBackendContext->fDevice;
52 GET_PROC(DestroySurfaceKHR);
53 GET_PROC(GetPhysicalDeviceSurfaceSupportKHR);
54 GET_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
55 GET_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
56 GET_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
57 GET_DEV_PROC(CreateSwapchainKHR);
58 GET_DEV_PROC(DestroySwapchainKHR);
59 GET_DEV_PROC(GetSwapchainImagesKHR);
60 GET_DEV_PROC(AcquireNextImageKHR);
61 GET_DEV_PROC(QueuePresentKHR);
jvanverth9f372462016-04-06 06:08:59 -070062
jvanvertha8d0d6c2016-05-05 12:32:03 -070063 fContext = GrContext::Create(kVulkan_GrBackend, (GrBackendContext) fBackendContext.get());
jvanverth9f372462016-04-06 06:08:59 -070064
bsalomond1bdd1f2016-07-26 12:02:50 -070065 fSurface = createVkSurface(instance);
jvanverth9f372462016-04-06 06:08:59 -070066 if (VK_NULL_HANDLE == fSurface) {
67 fBackendContext.reset(nullptr);
68 return;
69 }
70
jvanverth9f372462016-04-06 06:08:59 -070071 VkBool32 supported;
jvanverthb0d43522016-04-21 11:46:23 -070072 VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fBackendContext->fPhysicalDevice,
73 fPresentQueueIndex, fSurface,
74 &supported);
jvanverth9f372462016-04-06 06:08:59 -070075 if (VK_SUCCESS != res) {
76 this->destroyContext();
77 return;
78 }
79
brianosman05de2162016-05-06 13:28:57 -070080 if (!this->createSwapchain(-1, -1, params)) {
jvanverth9f372462016-04-06 06:08:59 -070081 this->destroyContext();
82 return;
83 }
84
85 // create presentQueue
86 vkGetDeviceQueue(fBackendContext->fDevice, fPresentQueueIndex, 0, &fPresentQueue);
jvanverth9f372462016-04-06 06:08:59 -070087}
88
bsalomonccde4ab2016-07-27 08:50:12 -070089bool VulkanWindowContext::createSwapchain(int width, int height,
brianosman05de2162016-05-06 13:28:57 -070090 const DisplayParams& params) {
jvanverth9f372462016-04-06 06:08:59 -070091 // check for capabilities
92 VkSurfaceCapabilitiesKHR caps;
jvanverthb0d43522016-04-21 11:46:23 -070093 VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fBackendContext->fPhysicalDevice,
94 fSurface, &caps);
jvanverth9f372462016-04-06 06:08:59 -070095 if (VK_SUCCESS != res) {
96 return false;
97 }
98
99 uint32_t surfaceFormatCount;
jvanverthb0d43522016-04-21 11:46:23 -0700100 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
101 &surfaceFormatCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700102 if (VK_SUCCESS != res) {
103 return false;
104 }
105
106 SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatKHR));
107 VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700108 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
109 &surfaceFormatCount, surfaceFormats);
jvanverth9f372462016-04-06 06:08:59 -0700110 if (VK_SUCCESS != res) {
111 return false;
112 }
113
114 uint32_t presentModeCount;
jvanverthb0d43522016-04-21 11:46:23 -0700115 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
116 &presentModeCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700117 if (VK_SUCCESS != res) {
118 return false;
119 }
120
121 SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR));
122 VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700123 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700124 &presentModeCount, presentModes);
jvanverth9f372462016-04-06 06:08:59 -0700125 if (VK_SUCCESS != res) {
126 return false;
127 }
128
129 VkExtent2D extent = caps.currentExtent;
130 // use the hints
131 if (extent.width == (uint32_t)-1) {
132 extent.width = width;
133 extent.height = height;
134 }
135
jvanverth9fab59d2016-04-06 12:08:51 -0700136 // clamp width; to protect us from broken hints
jvanverth9f372462016-04-06 06:08:59 -0700137 if (extent.width < caps.minImageExtent.width) {
138 extent.width = caps.minImageExtent.width;
139 } else if (extent.width > caps.maxImageExtent.width) {
140 extent.width = caps.maxImageExtent.width;
141 }
142 // clamp height
143 if (extent.height < caps.minImageExtent.height) {
144 extent.height = caps.minImageExtent.height;
145 } else if (extent.height > caps.maxImageExtent.height) {
146 extent.height = caps.maxImageExtent.height;
147 }
jvanverth1d155962016-05-23 13:13:36 -0700148
jvanverth9f372462016-04-06 06:08:59 -0700149 fWidth = (int)extent.width;
150 fHeight = (int)extent.height;
151
152 uint32_t imageCount = caps.minImageCount + 2;
153 if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) {
154 // Application must settle for fewer images than desired:
155 imageCount = caps.maxImageCount;
156 }
157
158 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
159 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
160 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
161 SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags);
162 SkASSERT(caps.supportedTransforms & caps.currentTransform);
163 SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
164 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR));
165 VkCompositeAlphaFlagBitsKHR composite_alpha =
166 (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ?
167 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR :
168 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
169
brianosman05de2162016-05-06 13:28:57 -0700170 // Pick our surface format. For now, just make sure it matches our sRGB request:
171 VkFormat surfaceFormat = VK_FORMAT_UNDEFINED;
172 VkColorSpaceKHR colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
brianosmanb109b8c2016-06-16 13:03:24 -0700173 auto srgbColorSpace = SkColorSpace::NewNamed(SkColorSpace::kSRGB_Named);
174 bool wantSRGB = srgbColorSpace == params.fColorSpace;
brianosman05de2162016-05-06 13:28:57 -0700175 for (uint32_t i = 0; i < surfaceFormatCount; ++i) {
176 GrPixelConfig config;
177 if (GrVkFormatToPixelConfig(surfaceFormats[i].format, &config) &&
178 GrPixelConfigIsSRGB(config) == wantSRGB) {
179 surfaceFormat = surfaceFormats[i].format;
180 colorSpace = surfaceFormats[i].colorSpace;
181 break;
182 }
183 }
184 fDisplayParams = params;
185
186 if (VK_FORMAT_UNDEFINED == surfaceFormat) {
187 return false;
188 }
jvanvertha4b0fed2016-04-27 11:42:21 -0700189
jvanverth3d6ed3a2016-04-07 11:09:51 -0700190 // If mailbox mode is available, use it, as it is the lowest-latency non-
191 // tearing mode. If not, fall back to FIFO which is always available.
jvanverth9f372462016-04-06 06:08:59 -0700192 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700193 for (uint32_t i = 0; i < presentModeCount; ++i) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700194 // use mailbox
195 if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) {
jvanverth9f372462016-04-06 06:08:59 -0700196 mode = presentModes[i];
jvanverth3d6ed3a2016-04-07 11:09:51 -0700197 break;
jvanverth9f372462016-04-06 06:08:59 -0700198 }
199 }
200
201 VkSwapchainCreateInfoKHR swapchainCreateInfo;
202 memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR));
203 swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
204 swapchainCreateInfo.surface = fSurface;
205 swapchainCreateInfo.minImageCount = imageCount;
jvanvertha4b0fed2016-04-27 11:42:21 -0700206 swapchainCreateInfo.imageFormat = surfaceFormat;
207 swapchainCreateInfo.imageColorSpace = colorSpace;
jvanverth9f372462016-04-06 06:08:59 -0700208 swapchainCreateInfo.imageExtent = extent;
209 swapchainCreateInfo.imageArrayLayers = 1;
210 swapchainCreateInfo.imageUsage = usageFlags;
211
jvanverthb0d43522016-04-21 11:46:23 -0700212 uint32_t queueFamilies[] = { fBackendContext->fGraphicsQueueIndex, fPresentQueueIndex };
213 if (fBackendContext->fGraphicsQueueIndex != fPresentQueueIndex) {
jvanverth9f372462016-04-06 06:08:59 -0700214 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
215 swapchainCreateInfo.queueFamilyIndexCount = 2;
216 swapchainCreateInfo.pQueueFamilyIndices = queueFamilies;
217 } else {
218 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
219 swapchainCreateInfo.queueFamilyIndexCount = 0;
220 swapchainCreateInfo.pQueueFamilyIndices = nullptr;
221 }
222
223 swapchainCreateInfo.preTransform = caps.currentTransform;;
224 swapchainCreateInfo.compositeAlpha = composite_alpha;
225 swapchainCreateInfo.presentMode = mode;
226 swapchainCreateInfo.clipped = true;
227 swapchainCreateInfo.oldSwapchain = fSwapchain;
228
jvanverthb0d43522016-04-21 11:46:23 -0700229 res = fCreateSwapchainKHR(fBackendContext->fDevice, &swapchainCreateInfo, nullptr, &fSwapchain);
jvanverth9f372462016-04-06 06:08:59 -0700230 if (VK_SUCCESS != res) {
231 return false;
232 }
233
234 // destroy the old swapchain
235 if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) {
236 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
237
238 this->destroyBuffers();
239
jvanverthb0d43522016-04-21 11:46:23 -0700240 fDestroySwapchainKHR(fBackendContext->fDevice, swapchainCreateInfo.oldSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700241 }
242
egdaniel58a8d922016-04-21 08:03:10 -0700243 this->createBuffers(swapchainCreateInfo.imageFormat);
jvanverth9f372462016-04-06 06:08:59 -0700244
245 return true;
246}
247
jvanvertha8d0d6c2016-05-05 12:32:03 -0700248void VulkanWindowContext::createBuffers(VkFormat format) {
egdaniel58a8d922016-04-21 08:03:10 -0700249 GrVkFormatToPixelConfig(format, &fPixelConfig);
250
jvanverthb0d43522016-04-21 11:46:23 -0700251 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700252 SkASSERT(fImageCount);
253 fImages = new VkImage[fImageCount];
jvanverthb0d43522016-04-21 11:46:23 -0700254 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, fImages);
jvanverth9f372462016-04-06 06:08:59 -0700255
256 // set up initial image layouts and create surfaces
257 fImageLayouts = new VkImageLayout[fImageCount];
258 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 };
egdaniel580fa592016-08-31 11:03:50 -0700266 info.fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
jvanverth9f372462016-04-06 06:08:59 -0700267 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;
370 delete[] fImageLayouts;
371 fImageLayouts = nullptr;
372 delete[] fImages;
373 fImages = nullptr;
374}
375
jvanvertha8d0d6c2016-05-05 12:32:03 -0700376VulkanWindowContext::~VulkanWindowContext() {
jvanverth9f372462016-04-06 06:08:59 -0700377 this->destroyContext();
378}
379
jvanvertha8d0d6c2016-05-05 12:32:03 -0700380void VulkanWindowContext::destroyContext() {
jvanverth9f372462016-04-06 06:08:59 -0700381 if (!fBackendContext.get()) {
382 return;
383 }
384
jvanverth85f758c2016-05-27 06:47:08 -0700385 GR_VK_CALL(fBackendContext->fInterface, QueueWaitIdle(fPresentQueue));
jvanverth9f372462016-04-06 06:08:59 -0700386 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
387
388 this->destroyBuffers();
389
390 if (VK_NULL_HANDLE != fCommandPool) {
391 GR_VK_CALL(fBackendContext->fInterface, DestroyCommandPool(fBackendContext->fDevice,
392 fCommandPool, nullptr));
393 fCommandPool = VK_NULL_HANDLE;
394 }
395
396 if (VK_NULL_HANDLE != fSwapchain) {
jvanverthb0d43522016-04-21 11:46:23 -0700397 fDestroySwapchainKHR(fBackendContext->fDevice, fSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700398 fSwapchain = VK_NULL_HANDLE;
399 }
400
401 if (VK_NULL_HANDLE != fSurface) {
jvanverthb0d43522016-04-21 11:46:23 -0700402 fDestroySurfaceKHR(fBackendContext->fInstance, fSurface, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700403 fSurface = VK_NULL_HANDLE;
404 }
405
jvanverthaf236b52016-05-20 06:01:06 -0700406 fContext->unref();
jvanverth9f372462016-04-06 06:08:59 -0700407
408 fBackendContext.reset(nullptr);
409}
410
jvanvertha8d0d6c2016-05-05 12:32:03 -0700411VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer() {
jvanverth9f372462016-04-06 06:08:59 -0700412 SkASSERT(fBackbuffers);
413
414 ++fCurrentBackbufferIndex;
415 if (fCurrentBackbufferIndex > fImageCount) {
416 fCurrentBackbufferIndex = 0;
417 }
418
419 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
420
421 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
brianosman05de2162016-05-06 13:28:57 -0700447 if (!this->createSwapchain(0, 0, fDisplayParams)) {
jvanverth9f372462016-04-06 06:08:59 -0700448 return nullptr;
449 }
450
451 // acquire the image
jvanvertha8d0d6c2016-05-05 12:32:03 -0700452 res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
jvanverthb0d43522016-04-21 11:46:23 -0700453 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
454 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700455
456 if (VK_SUCCESS != res) {
457 return nullptr;
458 }
459 }
460
461 // set up layout transfer from initial to color attachment
462 VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex];
egdaniel580fa592016-08-31 11:03:50 -0700463 SkASSERT(VK_IMAGE_LAYOUT_UNDEFINED == layout || VK_IMAGE_LAYOUT_PRESENT_SRC_KHR == layout);
jvanverth9f372462016-04-06 06:08:59 -0700464 VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
465 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT :
466 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
467 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700468 VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
jvanverth9f372462016-04-06 06:08:59 -0700469 0 : VK_ACCESS_MEMORY_READ_BIT;
470 VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
471
472 VkImageMemoryBarrier imageMemoryBarrier = {
473 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
474 NULL, // pNext
475 srcAccessMask, // outputMask
476 dstAccessMask, // inputMask
477 layout, // oldLayout
478 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout
479 fPresentQueueIndex, // srcQueueFamilyIndex
jvanverthb0d43522016-04-21 11:46:23 -0700480 fBackendContext->fGraphicsQueueIndex, // dstQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700481 fImages[backbuffer->fImageIndex], // image
482 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
483 };
484 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
485 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[0], 0));
486 VkCommandBufferBeginInfo info;
487 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
488 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
489 info.flags = 0;
490 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
491 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info));
492
jvanvertha8d0d6c2016-05-05 12:32:03 -0700493 GR_VK_CALL(fBackendContext->fInterface,
494 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0],
495 srcStageMask, dstStageMask, 0,
496 0, nullptr,
497 0, nullptr,
498 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700499
500 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700501 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0]));
jvanverth9f372462016-04-06 06:08:59 -0700502
egdaniel58a8d922016-04-21 08:03:10 -0700503 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanverth9f372462016-04-06 06:08:59 -0700504 // insert the layout transfer into the queue and wait on the acquire
505 VkSubmitInfo submitInfo;
506 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
507 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
508 submitInfo.waitSemaphoreCount = 1;
509 submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore;
egdaniel58a8d922016-04-21 08:03:10 -0700510 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
jvanverth9f372462016-04-06 06:08:59 -0700511 submitInfo.commandBufferCount = 1;
512 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0];
513 submitInfo.signalSemaphoreCount = 0;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700514
jvanverth9f372462016-04-06 06:08:59 -0700515 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700516 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700517 backbuffer->fUsageFences[0]));
518
egdaniel580fa592016-08-31 11:03:50 -0700519 GrVkImageInfo* imageInfo;
520 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
521 surface->getRenderTargetHandle((GrBackendObject*)&imageInfo,
522 SkSurface::kFlushRead_BackendHandleAccess);
523 imageInfo->updateImageLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
524
525 return sk_ref_sp(surface);
jvanverth9f372462016-04-06 06:08:59 -0700526}
527
jvanvertha8d0d6c2016-05-05 12:32:03 -0700528void VulkanWindowContext::swapBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700529
530 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
egdaniel580fa592016-08-31 11:03:50 -0700531 GrVkImageInfo* imageInfo;
532 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
533 surface->getRenderTargetHandle((GrBackendObject*)&imageInfo,
534 SkSurface::kFlushRead_BackendHandleAccess);
535 // Check to make sure we never change the actually wrapped image
536 SkASSERT(imageInfo->fImage == fImages[backbuffer->fImageIndex]);
jvanverth9f372462016-04-06 06:08:59 -0700537
egdaniel580fa592016-08-31 11:03:50 -0700538 VkImageLayout layout = imageInfo->fImageLayout;
539 VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(layout);
jvanverth9f372462016-04-06 06:08:59 -0700540 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
egdaniel580fa592016-08-31 11:03:50 -0700541 VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(layout);
jvanverth9f372462016-04-06 06:08:59 -0700542 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
543
544 VkImageMemoryBarrier imageMemoryBarrier = {
545 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
546 NULL, // pNext
547 srcAccessMask, // outputMask
548 dstAccessMask, // inputMask
549 layout, // oldLayout
550 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout
jvanverthb0d43522016-04-21 11:46:23 -0700551 fBackendContext->fGraphicsQueueIndex, // srcQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700552 fPresentQueueIndex, // dstQueueFamilyIndex
553 fImages[backbuffer->fImageIndex], // image
554 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
555 };
556 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
557 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[1], 0));
558 VkCommandBufferBeginInfo info;
559 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
560 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
561 info.flags = 0;
562 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
563 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info));
564 GR_VK_CALL(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700565 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
566 srcStageMask, dstStageMask, 0,
567 0, nullptr,
568 0, nullptr,
569 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700570 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
571 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1]));
572
573 fImageLayouts[backbuffer->fImageIndex] = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
574
575 // insert the layout transfer into the queue and wait on the acquire
576 VkSubmitInfo submitInfo;
577 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
578 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
579 submitInfo.waitSemaphoreCount = 0;
580 submitInfo.pWaitDstStageMask = 0;
581 submitInfo.commandBufferCount = 1;
582 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[1];
583 submitInfo.signalSemaphoreCount = 1;
584 submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore;
585
586 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700587 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700588 backbuffer->fUsageFences[1]));
589
590 // Submit present operation to present queue
591 const VkPresentInfoKHR presentInfo =
592 {
593 VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType
594 NULL, // pNext
595 1, // waitSemaphoreCount
596 &backbuffer->fRenderSemaphore, // pWaitSemaphores
597 1, // swapchainCount
598 &fSwapchain, // pSwapchains
599 &backbuffer->fImageIndex, // pImageIndices
600 NULL // pResults
601 };
602
jvanverthb0d43522016-04-21 11:46:23 -0700603 fQueuePresentKHR(fPresentQueue, &presentInfo);
jvanverth9f372462016-04-06 06:08:59 -0700604}
jvanvertha8d0d6c2016-05-05 12:32:03 -0700605
606} //namespace sk_app