blob: b8071d3bc3f0ef499fffef2bf9daba1c26914d18 [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"
csmartdalton008b9d82017-02-22 12:00:42 -070011#include "SkCommonFlagsPathRenderer.h"
Hal Canary95e3c052017-01-11 12:44:43 -050012#include "SkAutoMalloc.h"
jvanverth9f372462016-04-06 06:08:59 -070013#include "SkSurface.h"
jvanvertha8d0d6c2016-05-05 12:32:03 -070014#include "VulkanWindowContext.h"
jvanverth9f372462016-04-06 06:08:59 -070015
16#include "vk/GrVkInterface.h"
egdaniel580fa592016-08-31 11:03:50 -070017#include "vk/GrVkMemory.h"
jvanverth9f372462016-04-06 06:08:59 -070018#include "vk/GrVkUtil.h"
19#include "vk/GrVkTypes.h"
20
21#ifdef VK_USE_PLATFORM_WIN32_KHR
22// windows wants to define this as CreateSemaphoreA or CreateSemaphoreW
23#undef CreateSemaphore
24#endif
25
jvanverthb0d43522016-04-21 11:46:23 -070026#define GET_PROC(F) f ## F = (PFN_vk ## F) vkGetInstanceProcAddr(instance, "vk" #F)
27#define GET_DEV_PROC(F) f ## F = (PFN_vk ## F) vkGetDeviceProcAddr(device, "vk" #F)
28
jvanvertha8d0d6c2016-05-05 12:32:03 -070029namespace sk_app {
30
bsalomond1bdd1f2016-07-26 12:02:50 -070031VulkanWindowContext::VulkanWindowContext(const DisplayParams& params,
32 CreateVkSurfaceFn createVkSurface,
33 CanPresentFn canPresent)
jvanverthaf236b52016-05-20 06:01:06 -070034 : WindowContext()
35 , fSurface(VK_NULL_HANDLE)
jvanvertha8d0d6c2016-05-05 12:32:03 -070036 , fSwapchain(VK_NULL_HANDLE)
jvanverthaf236b52016-05-20 06:01:06 -070037 , fImages(nullptr)
38 , fImageLayouts(nullptr)
39 , fSurfaces(nullptr)
jvanvertha8d0d6c2016-05-05 12:32:03 -070040 , fCommandPool(VK_NULL_HANDLE)
41 , fBackbuffers(nullptr) {
jvanverth9f372462016-04-06 06:08:59 -070042
43 // any config code here (particularly for msaa)?
bsalomond1bdd1f2016-07-26 12:02:50 -070044 fBackendContext.reset(GrVkBackendContext::Create(&fPresentQueueIndex, canPresent));
jvanverth9f372462016-04-06 06:08:59 -070045
jvanverthb0d43522016-04-21 11:46:23 -070046 if (!(fBackendContext->fExtensions & kKHR_surface_GrVkExtensionFlag) ||
47 !(fBackendContext->fExtensions & kKHR_swapchain_GrVkExtensionFlag)) {
48 fBackendContext.reset(nullptr);
49 return;
50 }
51
52 VkInstance instance = fBackendContext->fInstance;
53 VkDevice device = fBackendContext->fDevice;
54 GET_PROC(DestroySurfaceKHR);
55 GET_PROC(GetPhysicalDeviceSurfaceSupportKHR);
56 GET_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
57 GET_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
58 GET_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
59 GET_DEV_PROC(CreateSwapchainKHR);
60 GET_DEV_PROC(DestroySwapchainKHR);
61 GET_DEV_PROC(GetSwapchainImagesKHR);
62 GET_DEV_PROC(AcquireNextImageKHR);
63 GET_DEV_PROC(QueuePresentKHR);
jvanverth9f372462016-04-06 06:08:59 -070064
csmartdalton008b9d82017-02-22 12:00:42 -070065 GrContextOptions ctxOptions;
66 ctxOptions.fGpuPathRenderers = CollectGpuPathRenderersFromFlags();
67 fContext = GrContext::Create(kVulkan_GrBackend, (GrBackendContext) fBackendContext.get(),
68 ctxOptions);
jvanverth9f372462016-04-06 06:08:59 -070069
bsalomond1bdd1f2016-07-26 12:02:50 -070070 fSurface = createVkSurface(instance);
jvanverth9f372462016-04-06 06:08:59 -070071 if (VK_NULL_HANDLE == fSurface) {
72 fBackendContext.reset(nullptr);
73 return;
74 }
75
jvanverth9f372462016-04-06 06:08:59 -070076 VkBool32 supported;
jvanverthb0d43522016-04-21 11:46:23 -070077 VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fBackendContext->fPhysicalDevice,
78 fPresentQueueIndex, fSurface,
79 &supported);
jvanverth9f372462016-04-06 06:08:59 -070080 if (VK_SUCCESS != res) {
81 this->destroyContext();
82 return;
83 }
84
brianosman05de2162016-05-06 13:28:57 -070085 if (!this->createSwapchain(-1, -1, params)) {
jvanverth9f372462016-04-06 06:08:59 -070086 this->destroyContext();
87 return;
88 }
89
90 // create presentQueue
91 vkGetDeviceQueue(fBackendContext->fDevice, fPresentQueueIndex, 0, &fPresentQueue);
jvanverth9f372462016-04-06 06:08:59 -070092}
93
bsalomonccde4ab2016-07-27 08:50:12 -070094bool VulkanWindowContext::createSwapchain(int width, int height,
brianosman05de2162016-05-06 13:28:57 -070095 const DisplayParams& params) {
jvanverth9f372462016-04-06 06:08:59 -070096 // check for capabilities
97 VkSurfaceCapabilitiesKHR caps;
jvanverthb0d43522016-04-21 11:46:23 -070098 VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fBackendContext->fPhysicalDevice,
99 fSurface, &caps);
jvanverth9f372462016-04-06 06:08:59 -0700100 if (VK_SUCCESS != res) {
101 return false;
102 }
103
104 uint32_t surfaceFormatCount;
jvanverthb0d43522016-04-21 11:46:23 -0700105 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
106 &surfaceFormatCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700107 if (VK_SUCCESS != res) {
108 return false;
109 }
110
111 SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatKHR));
112 VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700113 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
114 &surfaceFormatCount, surfaceFormats);
jvanverth9f372462016-04-06 06:08:59 -0700115 if (VK_SUCCESS != res) {
116 return false;
117 }
118
119 uint32_t presentModeCount;
jvanverthb0d43522016-04-21 11:46:23 -0700120 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
121 &presentModeCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700122 if (VK_SUCCESS != res) {
123 return false;
124 }
125
126 SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR));
127 VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700128 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700129 &presentModeCount, presentModes);
jvanverth9f372462016-04-06 06:08:59 -0700130 if (VK_SUCCESS != res) {
131 return false;
132 }
133
134 VkExtent2D extent = caps.currentExtent;
135 // use the hints
136 if (extent.width == (uint32_t)-1) {
137 extent.width = width;
138 extent.height = height;
139 }
140
jvanverth9fab59d2016-04-06 12:08:51 -0700141 // clamp width; to protect us from broken hints
jvanverth9f372462016-04-06 06:08:59 -0700142 if (extent.width < caps.minImageExtent.width) {
143 extent.width = caps.minImageExtent.width;
144 } else if (extent.width > caps.maxImageExtent.width) {
145 extent.width = caps.maxImageExtent.width;
146 }
147 // clamp height
148 if (extent.height < caps.minImageExtent.height) {
149 extent.height = caps.minImageExtent.height;
150 } else if (extent.height > caps.maxImageExtent.height) {
151 extent.height = caps.maxImageExtent.height;
152 }
jvanverth1d155962016-05-23 13:13:36 -0700153
jvanverth9f372462016-04-06 06:08:59 -0700154 fWidth = (int)extent.width;
155 fHeight = (int)extent.height;
156
157 uint32_t imageCount = caps.minImageCount + 2;
158 if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) {
159 // Application must settle for fewer images than desired:
160 imageCount = caps.maxImageCount;
161 }
162
163 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
164 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
165 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
166 SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags);
167 SkASSERT(caps.supportedTransforms & caps.currentTransform);
168 SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
169 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR));
170 VkCompositeAlphaFlagBitsKHR composite_alpha =
171 (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ?
172 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR :
173 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
174
brianosman05de2162016-05-06 13:28:57 -0700175 // Pick our surface format. For now, just make sure it matches our sRGB request:
176 VkFormat surfaceFormat = VK_FORMAT_UNDEFINED;
177 VkColorSpaceKHR colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500178 auto srgbColorSpace = SkColorSpace::MakeSRGB();
brianosmanb109b8c2016-06-16 13:03:24 -0700179 bool wantSRGB = srgbColorSpace == params.fColorSpace;
brianosman05de2162016-05-06 13:28:57 -0700180 for (uint32_t i = 0; i < surfaceFormatCount; ++i) {
181 GrPixelConfig config;
182 if (GrVkFormatToPixelConfig(surfaceFormats[i].format, &config) &&
183 GrPixelConfigIsSRGB(config) == wantSRGB) {
184 surfaceFormat = surfaceFormats[i].format;
185 colorSpace = surfaceFormats[i].colorSpace;
186 break;
187 }
188 }
189 fDisplayParams = params;
csmartdalton578f0642017-02-24 16:04:47 -0700190 fSampleCount = params.fMSAASampleCount;
191 fStencilBits = 8;
brianosman05de2162016-05-06 13:28:57 -0700192
193 if (VK_FORMAT_UNDEFINED == surfaceFormat) {
194 return false;
195 }
jvanvertha4b0fed2016-04-27 11:42:21 -0700196
jvanverth3d6ed3a2016-04-07 11:09:51 -0700197 // If mailbox mode is available, use it, as it is the lowest-latency non-
198 // tearing mode. If not, fall back to FIFO which is always available.
jvanverth9f372462016-04-06 06:08:59 -0700199 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700200 for (uint32_t i = 0; i < presentModeCount; ++i) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700201 // use mailbox
202 if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) {
jvanverth9f372462016-04-06 06:08:59 -0700203 mode = presentModes[i];
jvanverth3d6ed3a2016-04-07 11:09:51 -0700204 break;
jvanverth9f372462016-04-06 06:08:59 -0700205 }
206 }
207
208 VkSwapchainCreateInfoKHR swapchainCreateInfo;
209 memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR));
210 swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
211 swapchainCreateInfo.surface = fSurface;
212 swapchainCreateInfo.minImageCount = imageCount;
jvanvertha4b0fed2016-04-27 11:42:21 -0700213 swapchainCreateInfo.imageFormat = surfaceFormat;
214 swapchainCreateInfo.imageColorSpace = colorSpace;
jvanverth9f372462016-04-06 06:08:59 -0700215 swapchainCreateInfo.imageExtent = extent;
216 swapchainCreateInfo.imageArrayLayers = 1;
217 swapchainCreateInfo.imageUsage = usageFlags;
218
jvanverthb0d43522016-04-21 11:46:23 -0700219 uint32_t queueFamilies[] = { fBackendContext->fGraphicsQueueIndex, fPresentQueueIndex };
220 if (fBackendContext->fGraphicsQueueIndex != fPresentQueueIndex) {
jvanverth9f372462016-04-06 06:08:59 -0700221 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
222 swapchainCreateInfo.queueFamilyIndexCount = 2;
223 swapchainCreateInfo.pQueueFamilyIndices = queueFamilies;
224 } else {
225 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
226 swapchainCreateInfo.queueFamilyIndexCount = 0;
227 swapchainCreateInfo.pQueueFamilyIndices = nullptr;
228 }
229
Greg Daniele897b972016-10-06 13:57:57 -0400230 swapchainCreateInfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700231 swapchainCreateInfo.compositeAlpha = composite_alpha;
232 swapchainCreateInfo.presentMode = mode;
233 swapchainCreateInfo.clipped = true;
234 swapchainCreateInfo.oldSwapchain = fSwapchain;
235
jvanverthb0d43522016-04-21 11:46:23 -0700236 res = fCreateSwapchainKHR(fBackendContext->fDevice, &swapchainCreateInfo, nullptr, &fSwapchain);
jvanverth9f372462016-04-06 06:08:59 -0700237 if (VK_SUCCESS != res) {
238 return false;
239 }
240
241 // destroy the old swapchain
242 if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) {
243 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
244
245 this->destroyBuffers();
246
jvanverthb0d43522016-04-21 11:46:23 -0700247 fDestroySwapchainKHR(fBackendContext->fDevice, swapchainCreateInfo.oldSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700248 }
249
egdaniel58a8d922016-04-21 08:03:10 -0700250 this->createBuffers(swapchainCreateInfo.imageFormat);
jvanverth9f372462016-04-06 06:08:59 -0700251
252 return true;
253}
254
jvanvertha8d0d6c2016-05-05 12:32:03 -0700255void VulkanWindowContext::createBuffers(VkFormat format) {
egdaniel58a8d922016-04-21 08:03:10 -0700256 GrVkFormatToPixelConfig(format, &fPixelConfig);
257
jvanverthb0d43522016-04-21 11:46:23 -0700258 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700259 SkASSERT(fImageCount);
260 fImages = new VkImage[fImageCount];
jvanverthb0d43522016-04-21 11:46:23 -0700261 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, fImages);
jvanverth9f372462016-04-06 06:08:59 -0700262
263 // set up initial image layouts and create surfaces
264 fImageLayouts = new VkImageLayout[fImageCount];
265 fSurfaces = new sk_sp<SkSurface>[fImageCount];
266 for (uint32_t i = 0; i < fImageCount; ++i) {
267 fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED;
268
269 GrBackendRenderTargetDesc desc;
egdanielb2df0c22016-05-13 11:30:37 -0700270 GrVkImageInfo info;
jvanverth9f372462016-04-06 06:08:59 -0700271 info.fImage = fImages[i];
jvanverth9d54afc2016-09-20 09:20:03 -0700272 info.fAlloc = { VK_NULL_HANDLE, 0, 0, 0 };
egdaniel580fa592016-08-31 11:03:50 -0700273 info.fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
jvanverth9f372462016-04-06 06:08:59 -0700274 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
egdaniel58a8d922016-04-21 08:03:10 -0700275 info.fFormat = format;
jvanverth3622a172016-05-10 06:42:18 -0700276 info.fLevelCount = 1;
jvanverth9f372462016-04-06 06:08:59 -0700277 desc.fWidth = fWidth;
278 desc.fHeight = fHeight;
279 desc.fConfig = fPixelConfig;
280 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
csmartdalton578f0642017-02-24 16:04:47 -0700281 desc.fSampleCnt = fSampleCount;
282 desc.fStencilBits = fStencilBits;
jvanverth9f372462016-04-06 06:08:59 -0700283 desc.fRenderTargetHandle = (GrBackendObject) &info;
jvanverthaf236b52016-05-20 06:01:06 -0700284
Brian Osmanf750fbc2017-02-08 10:47:28 -0500285 fSurfaces[i] = SkSurface::MakeFromBackendRenderTarget(fContext, desc,
286 fDisplayParams.fColorSpace,
287 &fSurfaceProps);
jvanverth9f372462016-04-06 06:08:59 -0700288 }
289
290 // create the command pool for the command buffers
291 if (VK_NULL_HANDLE == fCommandPool) {
292 VkCommandPoolCreateInfo commandPoolInfo;
293 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
294 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
295 // this needs to be on the render queue
jvanverthb0d43522016-04-21 11:46:23 -0700296 commandPoolInfo.queueFamilyIndex = fBackendContext->fGraphicsQueueIndex;
jvanverth9f372462016-04-06 06:08:59 -0700297 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
298 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
299 CreateCommandPool(fBackendContext->fDevice, &commandPoolInfo,
300 nullptr, &fCommandPool));
301 }
302
303 // set up the backbuffers
304 VkSemaphoreCreateInfo semaphoreInfo;
305 memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo));
306 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
307 semaphoreInfo.pNext = nullptr;
308 semaphoreInfo.flags = 0;
309 VkCommandBufferAllocateInfo commandBuffersInfo;
310 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
311 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
312 commandBuffersInfo.pNext = nullptr;
313 commandBuffersInfo.commandPool = fCommandPool;
314 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
315 commandBuffersInfo.commandBufferCount = 2;
316 VkFenceCreateInfo fenceInfo;
317 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
318 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
319 fenceInfo.pNext = nullptr;
320 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
321
Greg Daniel1f05f442016-10-27 16:37:17 -0400322 // we create one additional backbuffer structure here, because we want to
jvanverth9f372462016-04-06 06:08:59 -0700323 // give the command buffers they contain a chance to finish before we cycle back
324 fBackbuffers = new BackbufferInfo[fImageCount + 1];
325 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
326 fBackbuffers[i].fImageIndex = -1;
327 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
328 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
329 nullptr, &fBackbuffers[i].fAcquireSemaphore));
330 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
331 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
332 nullptr, &fBackbuffers[i].fRenderSemaphore));
333 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
334 AllocateCommandBuffers(fBackendContext->fDevice, &commandBuffersInfo,
335 fBackbuffers[i].fTransitionCmdBuffers));
336 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
337 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
338 &fBackbuffers[i].fUsageFences[0]));
339 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
340 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
341 &fBackbuffers[i].fUsageFences[1]));
342 }
343 fCurrentBackbufferIndex = fImageCount;
344}
345
jvanvertha8d0d6c2016-05-05 12:32:03 -0700346void VulkanWindowContext::destroyBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700347
348 if (fBackbuffers) {
349 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
350 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700351 WaitForFences(fBackendContext->fDevice, 2,
jvanverth9f372462016-04-06 06:08:59 -0700352 fBackbuffers[i].fUsageFences,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700353 true, UINT64_MAX));
jvanverth9f372462016-04-06 06:08:59 -0700354 fBackbuffers[i].fImageIndex = -1;
355 GR_VK_CALL(fBackendContext->fInterface,
356 DestroySemaphore(fBackendContext->fDevice,
357 fBackbuffers[i].fAcquireSemaphore,
358 nullptr));
359 GR_VK_CALL(fBackendContext->fInterface,
360 DestroySemaphore(fBackendContext->fDevice,
361 fBackbuffers[i].fRenderSemaphore,
362 nullptr));
363 GR_VK_CALL(fBackendContext->fInterface,
364 FreeCommandBuffers(fBackendContext->fDevice, fCommandPool, 2,
365 fBackbuffers[i].fTransitionCmdBuffers));
366 GR_VK_CALL(fBackendContext->fInterface,
367 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[0], 0));
368 GR_VK_CALL(fBackendContext->fInterface,
369 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[1], 0));
370 }
371 }
372
373 delete[] fBackbuffers;
374 fBackbuffers = nullptr;
375
jvanverthaf236b52016-05-20 06:01:06 -0700376 // Does this actually free the surfaces?
jvanverth9f372462016-04-06 06:08:59 -0700377 delete[] fSurfaces;
378 fSurfaces = nullptr;
379 delete[] fImageLayouts;
380 fImageLayouts = nullptr;
381 delete[] fImages;
382 fImages = nullptr;
383}
384
jvanvertha8d0d6c2016-05-05 12:32:03 -0700385VulkanWindowContext::~VulkanWindowContext() {
jvanverth9f372462016-04-06 06:08:59 -0700386 this->destroyContext();
387}
388
jvanvertha8d0d6c2016-05-05 12:32:03 -0700389void VulkanWindowContext::destroyContext() {
jvanverth9f372462016-04-06 06:08:59 -0700390 if (!fBackendContext.get()) {
391 return;
392 }
393
jvanverth85f758c2016-05-27 06:47:08 -0700394 GR_VK_CALL(fBackendContext->fInterface, QueueWaitIdle(fPresentQueue));
jvanverth9f372462016-04-06 06:08:59 -0700395 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
396
397 this->destroyBuffers();
398
399 if (VK_NULL_HANDLE != fCommandPool) {
400 GR_VK_CALL(fBackendContext->fInterface, DestroyCommandPool(fBackendContext->fDevice,
401 fCommandPool, nullptr));
402 fCommandPool = VK_NULL_HANDLE;
403 }
404
405 if (VK_NULL_HANDLE != fSwapchain) {
jvanverthb0d43522016-04-21 11:46:23 -0700406 fDestroySwapchainKHR(fBackendContext->fDevice, fSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700407 fSwapchain = VK_NULL_HANDLE;
408 }
409
410 if (VK_NULL_HANDLE != fSurface) {
jvanverthb0d43522016-04-21 11:46:23 -0700411 fDestroySurfaceKHR(fBackendContext->fInstance, fSurface, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700412 fSurface = VK_NULL_HANDLE;
413 }
414
jvanverthaf236b52016-05-20 06:01:06 -0700415 fContext->unref();
jvanverth9f372462016-04-06 06:08:59 -0700416
417 fBackendContext.reset(nullptr);
418}
419
jvanvertha8d0d6c2016-05-05 12:32:03 -0700420VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer() {
jvanverth9f372462016-04-06 06:08:59 -0700421 SkASSERT(fBackbuffers);
422
423 ++fCurrentBackbufferIndex;
egdaniel804af7d2016-09-26 12:30:46 -0700424 if (fCurrentBackbufferIndex > fImageCount) {
jvanverth9f372462016-04-06 06:08:59 -0700425 fCurrentBackbufferIndex = 0;
426 }
427
428 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
jvanverth9f372462016-04-06 06:08:59 -0700429 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
430 WaitForFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences,
431 true, UINT64_MAX));
432 return backbuffer;
433}
434
jvanverthaf236b52016-05-20 06:01:06 -0700435sk_sp<SkSurface> VulkanWindowContext::getBackbufferSurface() {
jvanverth9f372462016-04-06 06:08:59 -0700436 BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
437 SkASSERT(backbuffer);
438
439 // reset the fence
440 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
441 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
442 // semaphores should be in unsignaled state
443
444 // acquire the image
jvanverthb0d43522016-04-21 11:46:23 -0700445 VkResult res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
446 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
447 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700448 if (VK_ERROR_SURFACE_LOST_KHR == res) {
449 // need to figure out how to create a new vkSurface without the platformData*
jvanverth3d6ed3a2016-04-07 11:09:51 -0700450 // maybe use attach somehow? but need a Window
jvanverth9f372462016-04-06 06:08:59 -0700451 return nullptr;
452 }
jvanverth3d6ed3a2016-04-07 11:09:51 -0700453 if (VK_ERROR_OUT_OF_DATE_KHR == res) {
jvanverth9f372462016-04-06 06:08:59 -0700454 // tear swapchain down and try again
Greg Daniel1f05f442016-10-27 16:37:17 -0400455 if (!this->createSwapchain(-1, -1, fDisplayParams)) {
jvanverth9f372462016-04-06 06:08:59 -0700456 return nullptr;
457 }
Jim Van Verthd63c1022017-01-05 13:50:49 -0500458 backbuffer = this->getAvailableBackbuffer();
459 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
460 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
jvanverth9f372462016-04-06 06:08:59 -0700461
462 // acquire the image
jvanvertha8d0d6c2016-05-05 12:32:03 -0700463 res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
jvanverthb0d43522016-04-21 11:46:23 -0700464 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
465 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700466
467 if (VK_SUCCESS != res) {
468 return nullptr;
469 }
470 }
471
472 // set up layout transfer from initial to color attachment
473 VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex];
egdaniel580fa592016-08-31 11:03:50 -0700474 SkASSERT(VK_IMAGE_LAYOUT_UNDEFINED == layout || VK_IMAGE_LAYOUT_PRESENT_SRC_KHR == layout);
jvanverth9f372462016-04-06 06:08:59 -0700475 VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
476 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT :
477 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
478 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700479 VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
jvanverth9f372462016-04-06 06:08:59 -0700480 0 : VK_ACCESS_MEMORY_READ_BIT;
481 VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
482
483 VkImageMemoryBarrier imageMemoryBarrier = {
484 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
485 NULL, // pNext
486 srcAccessMask, // outputMask
487 dstAccessMask, // inputMask
488 layout, // oldLayout
489 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout
490 fPresentQueueIndex, // srcQueueFamilyIndex
jvanverthb0d43522016-04-21 11:46:23 -0700491 fBackendContext->fGraphicsQueueIndex, // dstQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700492 fImages[backbuffer->fImageIndex], // image
493 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
494 };
495 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
496 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[0], 0));
497 VkCommandBufferBeginInfo info;
498 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
499 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
500 info.flags = 0;
501 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
502 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info));
503
jvanvertha8d0d6c2016-05-05 12:32:03 -0700504 GR_VK_CALL(fBackendContext->fInterface,
505 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0],
506 srcStageMask, dstStageMask, 0,
507 0, nullptr,
508 0, nullptr,
509 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700510
511 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700512 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0]));
jvanverth9f372462016-04-06 06:08:59 -0700513
egdaniel58a8d922016-04-21 08:03:10 -0700514 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanverth9f372462016-04-06 06:08:59 -0700515 // insert the layout transfer into the queue and wait on the acquire
516 VkSubmitInfo submitInfo;
517 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
518 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
519 submitInfo.waitSemaphoreCount = 1;
520 submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore;
egdaniel58a8d922016-04-21 08:03:10 -0700521 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
jvanverth9f372462016-04-06 06:08:59 -0700522 submitInfo.commandBufferCount = 1;
523 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0];
524 submitInfo.signalSemaphoreCount = 0;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700525
jvanverth9f372462016-04-06 06:08:59 -0700526 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700527 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700528 backbuffer->fUsageFences[0]));
529
egdaniel580fa592016-08-31 11:03:50 -0700530 GrVkImageInfo* imageInfo;
531 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
532 surface->getRenderTargetHandle((GrBackendObject*)&imageInfo,
533 SkSurface::kFlushRead_BackendHandleAccess);
534 imageInfo->updateImageLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
535
536 return sk_ref_sp(surface);
jvanverth9f372462016-04-06 06:08:59 -0700537}
538
jvanvertha8d0d6c2016-05-05 12:32:03 -0700539void VulkanWindowContext::swapBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700540
541 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
egdaniel580fa592016-08-31 11:03:50 -0700542 GrVkImageInfo* imageInfo;
543 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
544 surface->getRenderTargetHandle((GrBackendObject*)&imageInfo,
545 SkSurface::kFlushRead_BackendHandleAccess);
546 // Check to make sure we never change the actually wrapped image
547 SkASSERT(imageInfo->fImage == fImages[backbuffer->fImageIndex]);
jvanverth9f372462016-04-06 06:08:59 -0700548
egdaniel580fa592016-08-31 11:03:50 -0700549 VkImageLayout layout = imageInfo->fImageLayout;
550 VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(layout);
jvanverth9f372462016-04-06 06:08:59 -0700551 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
egdaniel580fa592016-08-31 11:03:50 -0700552 VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(layout);
jvanverth9f372462016-04-06 06:08:59 -0700553 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
554
555 VkImageMemoryBarrier imageMemoryBarrier = {
556 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
557 NULL, // pNext
558 srcAccessMask, // outputMask
559 dstAccessMask, // inputMask
560 layout, // oldLayout
561 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout
jvanverthb0d43522016-04-21 11:46:23 -0700562 fBackendContext->fGraphicsQueueIndex, // srcQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700563 fPresentQueueIndex, // dstQueueFamilyIndex
564 fImages[backbuffer->fImageIndex], // image
565 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
566 };
567 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
568 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[1], 0));
569 VkCommandBufferBeginInfo info;
570 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
571 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
572 info.flags = 0;
573 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
574 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info));
575 GR_VK_CALL(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700576 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
577 srcStageMask, dstStageMask, 0,
578 0, nullptr,
579 0, nullptr,
580 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700581 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
582 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1]));
583
584 fImageLayouts[backbuffer->fImageIndex] = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
585
586 // insert the layout transfer into the queue and wait on the acquire
587 VkSubmitInfo submitInfo;
588 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
589 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
590 submitInfo.waitSemaphoreCount = 0;
591 submitInfo.pWaitDstStageMask = 0;
592 submitInfo.commandBufferCount = 1;
593 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[1];
594 submitInfo.signalSemaphoreCount = 1;
595 submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore;
596
597 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700598 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700599 backbuffer->fUsageFences[1]));
600
601 // Submit present operation to present queue
602 const VkPresentInfoKHR presentInfo =
603 {
604 VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType
605 NULL, // pNext
606 1, // waitSemaphoreCount
607 &backbuffer->fRenderSemaphore, // pWaitSemaphores
608 1, // swapchainCount
609 &fSwapchain, // pSwapchains
610 &backbuffer->fImageIndex, // pImageIndices
611 NULL // pResults
612 };
613
jvanverthb0d43522016-04-21 11:46:23 -0700614 fQueuePresentKHR(fPresentQueue, &presentInfo);
jvanverth9f372462016-04-06 06:08:59 -0700615}
jvanvertha8d0d6c2016-05-05 12:32:03 -0700616
617} //namespace sk_app