blob: 2e2b8a988c01205f23d1e9bc6c64b93dd4d8ebc8 [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
brianosman05de2162016-05-06 13:28:57 -070028VulkanWindowContext::VulkanWindowContext(void* platformData, const DisplayParams& params)
jvanverthaf236b52016-05-20 06:01:06 -070029 : WindowContext()
30 , fSurface(VK_NULL_HANDLE)
jvanvertha8d0d6c2016-05-05 12:32:03 -070031 , fSwapchain(VK_NULL_HANDLE)
jvanverthaf236b52016-05-20 06:01:06 -070032 , fImages(nullptr)
33 , fImageLayouts(nullptr)
34 , fSurfaces(nullptr)
jvanvertha8d0d6c2016-05-05 12:32:03 -070035 , fCommandPool(VK_NULL_HANDLE)
36 , fBackbuffers(nullptr) {
jvanverth9f372462016-04-06 06:08:59 -070037
38 // any config code here (particularly for msaa)?
39
brianosman05de2162016-05-06 13:28:57 -070040 this->initializeContext(platformData, params);
jvanverth9f372462016-04-06 06:08:59 -070041}
42
brianosman05de2162016-05-06 13:28:57 -070043void VulkanWindowContext::initializeContext(void* platformData, const DisplayParams& params) {
jvanverth1d155962016-05-23 13:13:36 -070044 fBackendContext.reset(GrVkBackendContext::Create(&fPresentQueueIndex, canPresent,
45 platformData));
jvanverth9f372462016-04-06 06:08:59 -070046
jvanverthb0d43522016-04-21 11:46:23 -070047 if (!(fBackendContext->fExtensions & kKHR_surface_GrVkExtensionFlag) ||
48 !(fBackendContext->fExtensions & kKHR_swapchain_GrVkExtensionFlag)) {
49 fBackendContext.reset(nullptr);
50 return;
51 }
52
53 VkInstance instance = fBackendContext->fInstance;
54 VkDevice device = fBackendContext->fDevice;
55 GET_PROC(DestroySurfaceKHR);
56 GET_PROC(GetPhysicalDeviceSurfaceSupportKHR);
57 GET_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
58 GET_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
59 GET_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
60 GET_DEV_PROC(CreateSwapchainKHR);
61 GET_DEV_PROC(DestroySwapchainKHR);
62 GET_DEV_PROC(GetSwapchainImagesKHR);
63 GET_DEV_PROC(AcquireNextImageKHR);
64 GET_DEV_PROC(QueuePresentKHR);
jvanverth9f372462016-04-06 06:08:59 -070065
jvanvertha8d0d6c2016-05-05 12:32:03 -070066 fContext = GrContext::Create(kVulkan_GrBackend, (GrBackendContext) fBackendContext.get());
jvanverth9f372462016-04-06 06:08:59 -070067
jvanverthb0d43522016-04-21 11:46:23 -070068 fSurface = createVkSurface(instance, platformData);
jvanverth9f372462016-04-06 06:08:59 -070069 if (VK_NULL_HANDLE == fSurface) {
70 fBackendContext.reset(nullptr);
71 return;
72 }
73
jvanverth9f372462016-04-06 06:08:59 -070074 VkBool32 supported;
jvanverthb0d43522016-04-21 11:46:23 -070075 VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fBackendContext->fPhysicalDevice,
76 fPresentQueueIndex, fSurface,
77 &supported);
jvanverth9f372462016-04-06 06:08:59 -070078 if (VK_SUCCESS != res) {
79 this->destroyContext();
80 return;
81 }
82
brianosman05de2162016-05-06 13:28:57 -070083 if (!this->createSwapchain(-1, -1, params)) {
jvanverth9f372462016-04-06 06:08:59 -070084 this->destroyContext();
85 return;
86 }
87
88 // create presentQueue
89 vkGetDeviceQueue(fBackendContext->fDevice, fPresentQueueIndex, 0, &fPresentQueue);
jvanverth9f372462016-04-06 06:08:59 -070090}
91
brianosman05de2162016-05-06 13:28:57 -070092bool VulkanWindowContext::createSwapchain(uint32_t width, uint32_t height,
93 const DisplayParams& params) {
jvanverth9f372462016-04-06 06:08:59 -070094 // check for capabilities
95 VkSurfaceCapabilitiesKHR caps;
jvanverthb0d43522016-04-21 11:46:23 -070096 VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fBackendContext->fPhysicalDevice,
97 fSurface, &caps);
jvanverth9f372462016-04-06 06:08:59 -070098 if (VK_SUCCESS != res) {
99 return false;
100 }
101
102 uint32_t surfaceFormatCount;
jvanverthb0d43522016-04-21 11:46:23 -0700103 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
104 &surfaceFormatCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700105 if (VK_SUCCESS != res) {
106 return false;
107 }
108
109 SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatKHR));
110 VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700111 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
112 &surfaceFormatCount, surfaceFormats);
jvanverth9f372462016-04-06 06:08:59 -0700113 if (VK_SUCCESS != res) {
114 return false;
115 }
116
117 uint32_t presentModeCount;
jvanverthb0d43522016-04-21 11:46:23 -0700118 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
119 &presentModeCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700120 if (VK_SUCCESS != res) {
121 return false;
122 }
123
124 SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR));
125 VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700126 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700127 &presentModeCount, presentModes);
jvanverth9f372462016-04-06 06:08:59 -0700128 if (VK_SUCCESS != res) {
129 return false;
130 }
131
132 VkExtent2D extent = caps.currentExtent;
133 // use the hints
134 if (extent.width == (uint32_t)-1) {
135 extent.width = width;
136 extent.height = height;
137 }
138
jvanverth9fab59d2016-04-06 12:08:51 -0700139 // clamp width; to protect us from broken hints
jvanverth9f372462016-04-06 06:08:59 -0700140 if (extent.width < caps.minImageExtent.width) {
141 extent.width = caps.minImageExtent.width;
142 } else if (extent.width > caps.maxImageExtent.width) {
143 extent.width = caps.maxImageExtent.width;
144 }
145 // clamp height
146 if (extent.height < caps.minImageExtent.height) {
147 extent.height = caps.minImageExtent.height;
148 } else if (extent.height > caps.maxImageExtent.height) {
149 extent.height = caps.maxImageExtent.height;
150 }
jvanverth1d155962016-05-23 13:13:36 -0700151
jvanverth9f372462016-04-06 06:08:59 -0700152 fWidth = (int)extent.width;
153 fHeight = (int)extent.height;
154
155 uint32_t imageCount = caps.minImageCount + 2;
156 if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) {
157 // Application must settle for fewer images than desired:
158 imageCount = caps.maxImageCount;
159 }
160
161 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
162 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
163 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
164 SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags);
165 SkASSERT(caps.supportedTransforms & caps.currentTransform);
166 SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
167 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR));
168 VkCompositeAlphaFlagBitsKHR composite_alpha =
169 (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ?
170 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR :
171 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
172
brianosman05de2162016-05-06 13:28:57 -0700173 // Pick our surface format. For now, just make sure it matches our sRGB request:
174 VkFormat surfaceFormat = VK_FORMAT_UNDEFINED;
175 VkColorSpaceKHR colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
176 bool wantSRGB = kSRGB_SkColorProfileType == params.fProfileType;
177 for (uint32_t i = 0; i < surfaceFormatCount; ++i) {
178 GrPixelConfig config;
179 if (GrVkFormatToPixelConfig(surfaceFormats[i].format, &config) &&
180 GrPixelConfigIsSRGB(config) == wantSRGB) {
181 surfaceFormat = surfaceFormats[i].format;
182 colorSpace = surfaceFormats[i].colorSpace;
183 break;
184 }
185 }
186 fDisplayParams = params;
187
188 if (VK_FORMAT_UNDEFINED == surfaceFormat) {
189 return false;
190 }
jvanvertha4b0fed2016-04-27 11:42:21 -0700191
jvanverth3d6ed3a2016-04-07 11:09:51 -0700192 // If mailbox mode is available, use it, as it is the lowest-latency non-
193 // tearing mode. If not, fall back to FIFO which is always available.
jvanverth9f372462016-04-06 06:08:59 -0700194 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700195 for (uint32_t i = 0; i < presentModeCount; ++i) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700196 // use mailbox
197 if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) {
jvanverth9f372462016-04-06 06:08:59 -0700198 mode = presentModes[i];
jvanverth3d6ed3a2016-04-07 11:09:51 -0700199 break;
jvanverth9f372462016-04-06 06:08:59 -0700200 }
201 }
202
203 VkSwapchainCreateInfoKHR swapchainCreateInfo;
204 memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR));
205 swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
206 swapchainCreateInfo.surface = fSurface;
207 swapchainCreateInfo.minImageCount = imageCount;
jvanvertha4b0fed2016-04-27 11:42:21 -0700208 swapchainCreateInfo.imageFormat = surfaceFormat;
209 swapchainCreateInfo.imageColorSpace = colorSpace;
jvanverth9f372462016-04-06 06:08:59 -0700210 swapchainCreateInfo.imageExtent = extent;
211 swapchainCreateInfo.imageArrayLayers = 1;
212 swapchainCreateInfo.imageUsage = usageFlags;
213
jvanverthb0d43522016-04-21 11:46:23 -0700214 uint32_t queueFamilies[] = { fBackendContext->fGraphicsQueueIndex, fPresentQueueIndex };
215 if (fBackendContext->fGraphicsQueueIndex != fPresentQueueIndex) {
jvanverth9f372462016-04-06 06:08:59 -0700216 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
217 swapchainCreateInfo.queueFamilyIndexCount = 2;
218 swapchainCreateInfo.pQueueFamilyIndices = queueFamilies;
219 } else {
220 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
221 swapchainCreateInfo.queueFamilyIndexCount = 0;
222 swapchainCreateInfo.pQueueFamilyIndices = nullptr;
223 }
224
225 swapchainCreateInfo.preTransform = caps.currentTransform;;
226 swapchainCreateInfo.compositeAlpha = composite_alpha;
227 swapchainCreateInfo.presentMode = mode;
228 swapchainCreateInfo.clipped = true;
229 swapchainCreateInfo.oldSwapchain = fSwapchain;
230
jvanverthb0d43522016-04-21 11:46:23 -0700231 res = fCreateSwapchainKHR(fBackendContext->fDevice, &swapchainCreateInfo, nullptr, &fSwapchain);
jvanverth9f372462016-04-06 06:08:59 -0700232 if (VK_SUCCESS != res) {
233 return false;
234 }
235
236 // destroy the old swapchain
237 if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) {
238 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
239
240 this->destroyBuffers();
241
jvanverthb0d43522016-04-21 11:46:23 -0700242 fDestroySwapchainKHR(fBackendContext->fDevice, swapchainCreateInfo.oldSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700243 }
244
egdaniel58a8d922016-04-21 08:03:10 -0700245 this->createBuffers(swapchainCreateInfo.imageFormat);
jvanverth9f372462016-04-06 06:08:59 -0700246
247 return true;
248}
249
jvanvertha8d0d6c2016-05-05 12:32:03 -0700250void VulkanWindowContext::createBuffers(VkFormat format) {
egdaniel58a8d922016-04-21 08:03:10 -0700251 GrVkFormatToPixelConfig(format, &fPixelConfig);
252
jvanverthb0d43522016-04-21 11:46:23 -0700253 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700254 SkASSERT(fImageCount);
255 fImages = new VkImage[fImageCount];
jvanverthb0d43522016-04-21 11:46:23 -0700256 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, fImages);
jvanverth9f372462016-04-06 06:08:59 -0700257
258 // set up initial image layouts and create surfaces
259 fImageLayouts = new VkImageLayout[fImageCount];
jvanverthaf236b52016-05-20 06:01:06 -0700260 fRenderTargets = new sk_sp<GrRenderTarget>[fImageCount];
jvanverth9f372462016-04-06 06:08:59 -0700261 fSurfaces = new sk_sp<SkSurface>[fImageCount];
262 for (uint32_t i = 0; i < fImageCount; ++i) {
263 fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED;
264
265 GrBackendRenderTargetDesc desc;
egdanielb2df0c22016-05-13 11:30:37 -0700266 GrVkImageInfo info;
jvanverth9f372462016-04-06 06:08:59 -0700267 info.fImage = fImages[i];
brianosman419ca642016-05-04 08:19:44 -0700268 info.fAlloc = VK_NULL_HANDLE;
jvanverth9f372462016-04-06 06:08:59 -0700269 info.fImageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
270 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
egdaniel58a8d922016-04-21 08:03:10 -0700271 info.fFormat = format;
jvanverth3622a172016-05-10 06:42:18 -0700272 info.fLevelCount = 1;
jvanverth9f372462016-04-06 06:08:59 -0700273 desc.fWidth = fWidth;
274 desc.fHeight = fHeight;
275 desc.fConfig = fPixelConfig;
276 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
277 desc.fSampleCnt = 0;
278 desc.fStencilBits = 0;
279 desc.fRenderTargetHandle = (GrBackendObject) &info;
jvanverthaf236b52016-05-20 06:01:06 -0700280 fRenderTargets[i].reset(fContext->textureProvider()->wrapBackendRenderTarget(desc));
281
282 fSurfaces[i] = this->createRenderSurface(fRenderTargets[i], 24);
jvanverth9f372462016-04-06 06:08:59 -0700283 }
284
285 // create the command pool for the command buffers
286 if (VK_NULL_HANDLE == fCommandPool) {
287 VkCommandPoolCreateInfo commandPoolInfo;
288 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
289 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
290 // this needs to be on the render queue
jvanverthb0d43522016-04-21 11:46:23 -0700291 commandPoolInfo.queueFamilyIndex = fBackendContext->fGraphicsQueueIndex;
jvanverth9f372462016-04-06 06:08:59 -0700292 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
293 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
294 CreateCommandPool(fBackendContext->fDevice, &commandPoolInfo,
295 nullptr, &fCommandPool));
296 }
297
298 // set up the backbuffers
299 VkSemaphoreCreateInfo semaphoreInfo;
300 memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo));
301 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
302 semaphoreInfo.pNext = nullptr;
303 semaphoreInfo.flags = 0;
304 VkCommandBufferAllocateInfo commandBuffersInfo;
305 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
306 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
307 commandBuffersInfo.pNext = nullptr;
308 commandBuffersInfo.commandPool = fCommandPool;
309 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
310 commandBuffersInfo.commandBufferCount = 2;
311 VkFenceCreateInfo fenceInfo;
312 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
313 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
314 fenceInfo.pNext = nullptr;
315 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
316
317 // we create one additional backbuffer structure here, because we want to
318 // give the command buffers they contain a chance to finish before we cycle back
319 fBackbuffers = new BackbufferInfo[fImageCount + 1];
320 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
321 fBackbuffers[i].fImageIndex = -1;
322 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
323 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
324 nullptr, &fBackbuffers[i].fAcquireSemaphore));
325 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
326 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
327 nullptr, &fBackbuffers[i].fRenderSemaphore));
328 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
329 AllocateCommandBuffers(fBackendContext->fDevice, &commandBuffersInfo,
330 fBackbuffers[i].fTransitionCmdBuffers));
331 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
332 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
333 &fBackbuffers[i].fUsageFences[0]));
334 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
335 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
336 &fBackbuffers[i].fUsageFences[1]));
337 }
338 fCurrentBackbufferIndex = fImageCount;
339}
340
jvanvertha8d0d6c2016-05-05 12:32:03 -0700341void VulkanWindowContext::destroyBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700342
343 if (fBackbuffers) {
344 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
345 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700346 WaitForFences(fBackendContext->fDevice, 2,
jvanverth9f372462016-04-06 06:08:59 -0700347 fBackbuffers[i].fUsageFences,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700348 true, UINT64_MAX));
jvanverth9f372462016-04-06 06:08:59 -0700349 fBackbuffers[i].fImageIndex = -1;
350 GR_VK_CALL(fBackendContext->fInterface,
351 DestroySemaphore(fBackendContext->fDevice,
352 fBackbuffers[i].fAcquireSemaphore,
353 nullptr));
354 GR_VK_CALL(fBackendContext->fInterface,
355 DestroySemaphore(fBackendContext->fDevice,
356 fBackbuffers[i].fRenderSemaphore,
357 nullptr));
358 GR_VK_CALL(fBackendContext->fInterface,
359 FreeCommandBuffers(fBackendContext->fDevice, fCommandPool, 2,
360 fBackbuffers[i].fTransitionCmdBuffers));
361 GR_VK_CALL(fBackendContext->fInterface,
362 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[0], 0));
363 GR_VK_CALL(fBackendContext->fInterface,
364 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[1], 0));
365 }
366 }
367
368 delete[] fBackbuffers;
369 fBackbuffers = nullptr;
370
jvanverthaf236b52016-05-20 06:01:06 -0700371 // Does this actually free the surfaces?
jvanverth9f372462016-04-06 06:08:59 -0700372 delete[] fSurfaces;
373 fSurfaces = nullptr;
jvanverthaf236b52016-05-20 06:01:06 -0700374 delete[] fRenderTargets;
375 fRenderTargets = nullptr;
jvanverth9f372462016-04-06 06:08:59 -0700376 delete[] fImageLayouts;
377 fImageLayouts = nullptr;
378 delete[] fImages;
379 fImages = nullptr;
380}
381
jvanvertha8d0d6c2016-05-05 12:32:03 -0700382VulkanWindowContext::~VulkanWindowContext() {
jvanverth9f372462016-04-06 06:08:59 -0700383 this->destroyContext();
384}
385
jvanvertha8d0d6c2016-05-05 12:32:03 -0700386void VulkanWindowContext::destroyContext() {
jvanverth9f372462016-04-06 06:08:59 -0700387 if (!fBackendContext.get()) {
388 return;
389 }
390
391 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
392
393 this->destroyBuffers();
394
395 if (VK_NULL_HANDLE != fCommandPool) {
396 GR_VK_CALL(fBackendContext->fInterface, DestroyCommandPool(fBackendContext->fDevice,
397 fCommandPool, nullptr));
398 fCommandPool = VK_NULL_HANDLE;
399 }
400
401 if (VK_NULL_HANDLE != fSwapchain) {
jvanverthb0d43522016-04-21 11:46:23 -0700402 fDestroySwapchainKHR(fBackendContext->fDevice, fSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700403 fSwapchain = VK_NULL_HANDLE;
404 }
405
406 if (VK_NULL_HANDLE != fSurface) {
jvanverthb0d43522016-04-21 11:46:23 -0700407 fDestroySurfaceKHR(fBackendContext->fInstance, fSurface, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700408 fSurface = VK_NULL_HANDLE;
409 }
410
jvanverthaf236b52016-05-20 06:01:06 -0700411 fContext->abandonContext();
412 fContext->unref();
jvanverth9f372462016-04-06 06:08:59 -0700413
414 fBackendContext.reset(nullptr);
415}
416
jvanvertha8d0d6c2016-05-05 12:32:03 -0700417VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer() {
jvanverth9f372462016-04-06 06:08:59 -0700418 SkASSERT(fBackbuffers);
419
420 ++fCurrentBackbufferIndex;
421 if (fCurrentBackbufferIndex > fImageCount) {
422 fCurrentBackbufferIndex = 0;
423 }
424
425 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
426
427 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
428 WaitForFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences,
429 true, UINT64_MAX));
430 return backbuffer;
431}
432
jvanverthaf236b52016-05-20 06:01:06 -0700433sk_sp<SkSurface> VulkanWindowContext::getBackbufferSurface() {
jvanverth9f372462016-04-06 06:08:59 -0700434 BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
435 SkASSERT(backbuffer);
436
437 // reset the fence
438 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
439 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
440 // semaphores should be in unsignaled state
441
442 // acquire the image
jvanverthb0d43522016-04-21 11:46:23 -0700443 VkResult res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
444 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
445 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700446 if (VK_ERROR_SURFACE_LOST_KHR == res) {
447 // need to figure out how to create a new vkSurface without the platformData*
jvanverth3d6ed3a2016-04-07 11:09:51 -0700448 // maybe use attach somehow? but need a Window
jvanverth9f372462016-04-06 06:08:59 -0700449 return nullptr;
450 }
jvanverth3d6ed3a2016-04-07 11:09:51 -0700451 if (VK_ERROR_OUT_OF_DATE_KHR == res) {
jvanverth9f372462016-04-06 06:08:59 -0700452 // tear swapchain down and try again
brianosman05de2162016-05-06 13:28:57 -0700453 if (!this->createSwapchain(0, 0, fDisplayParams)) {
jvanverth9f372462016-04-06 06:08:59 -0700454 return nullptr;
455 }
456
457 // acquire the image
jvanvertha8d0d6c2016-05-05 12:32:03 -0700458 res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
jvanverthb0d43522016-04-21 11:46:23 -0700459 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
460 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700461
462 if (VK_SUCCESS != res) {
463 return nullptr;
464 }
465 }
466
467 // set up layout transfer from initial to color attachment
468 VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex];
469 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
jvanverthaf236b52016-05-20 06:01:06 -0700524 return sk_ref_sp(fSurfaces[backbuffer->fImageIndex].get());
jvanverth9f372462016-04-06 06:08:59 -0700525}
526
jvanvertha8d0d6c2016-05-05 12:32:03 -0700527void VulkanWindowContext::swapBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700528
529 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
530
jvanverthaf236b52016-05-20 06:01:06 -0700531 this->presentRenderSurface(fSurfaces[backbuffer->fImageIndex],
532 fRenderTargets[backbuffer->fImageIndex], 24);
533
jvanverth9f372462016-04-06 06:08:59 -0700534 VkImageLayout layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
535 VkPipelineStageFlags srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
536 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
537 VkAccessFlags srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
538 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
539
540 VkImageMemoryBarrier imageMemoryBarrier = {
541 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
542 NULL, // pNext
543 srcAccessMask, // outputMask
544 dstAccessMask, // inputMask
545 layout, // oldLayout
546 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout
jvanverthb0d43522016-04-21 11:46:23 -0700547 fBackendContext->fGraphicsQueueIndex, // srcQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700548 fPresentQueueIndex, // dstQueueFamilyIndex
549 fImages[backbuffer->fImageIndex], // image
550 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
551 };
552 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
553 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[1], 0));
554 VkCommandBufferBeginInfo info;
555 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
556 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
557 info.flags = 0;
558 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
559 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info));
560 GR_VK_CALL(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700561 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
562 srcStageMask, dstStageMask, 0,
563 0, nullptr,
564 0, nullptr,
565 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700566 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
567 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1]));
568
569 fImageLayouts[backbuffer->fImageIndex] = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
570
571 // insert the layout transfer into the queue and wait on the acquire
572 VkSubmitInfo submitInfo;
573 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
574 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
575 submitInfo.waitSemaphoreCount = 0;
576 submitInfo.pWaitDstStageMask = 0;
577 submitInfo.commandBufferCount = 1;
578 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[1];
579 submitInfo.signalSemaphoreCount = 1;
580 submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore;
581
582 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700583 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700584 backbuffer->fUsageFences[1]));
585
586 // Submit present operation to present queue
587 const VkPresentInfoKHR presentInfo =
588 {
589 VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType
590 NULL, // pNext
591 1, // waitSemaphoreCount
592 &backbuffer->fRenderSemaphore, // pWaitSemaphores
593 1, // swapchainCount
594 &fSwapchain, // pSwapchains
595 &backbuffer->fImageIndex, // pImageIndices
596 NULL // pResults
597 };
598
jvanverthb0d43522016-04-21 11:46:23 -0700599 fQueuePresentKHR(fPresentQueue, &presentInfo);
jvanverth9f372462016-04-06 06:08:59 -0700600
601}
jvanvertha8d0d6c2016-05-05 12:32:03 -0700602
603} //namespace sk_app