blob: a9124d5e413293e8aa13275de9777649e5a1ea0e [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
Greg Daniele79b4732017-04-20 14:07:46 -04009#include "GrBackendSurface.h"
jvanverth9f372462016-04-06 06:08:59 -070010#include "GrContext.h"
Hal Canary95e3c052017-01-11 12:44:43 -050011#include "SkAutoMalloc.h"
jvanverth9f372462016-04-06 06:08:59 -070012#include "SkSurface.h"
jvanvertha8d0d6c2016-05-05 12:32:03 -070013#include "VulkanWindowContext.h"
jvanverth9f372462016-04-06 06:08:59 -070014
15#include "vk/GrVkInterface.h"
egdaniel580fa592016-08-31 11:03:50 -070016#include "vk/GrVkMemory.h"
jvanverth9f372462016-04-06 06:08:59 -070017#include "vk/GrVkUtil.h"
18#include "vk/GrVkTypes.h"
19
20#ifdef VK_USE_PLATFORM_WIN32_KHR
21// windows wants to define this as CreateSemaphoreA or CreateSemaphoreW
22#undef CreateSemaphore
23#endif
24
jvanverthb0d43522016-04-21 11:46:23 -070025#define GET_PROC(F) f ## F = (PFN_vk ## F) vkGetInstanceProcAddr(instance, "vk" #F)
26#define GET_DEV_PROC(F) f ## F = (PFN_vk ## F) vkGetDeviceProcAddr(device, "vk" #F)
27
jvanvertha8d0d6c2016-05-05 12:32:03 -070028namespace sk_app {
29
bsalomond1bdd1f2016-07-26 12:02:50 -070030VulkanWindowContext::VulkanWindowContext(const DisplayParams& params,
31 CreateVkSurfaceFn createVkSurface,
32 CanPresentFn canPresent)
Jim Van Verthfbdc0802017-05-02 16:15:53 -040033 : WindowContext(params)
34 , fCreateVkSurfaceFn(createVkSurface)
35 , fCanPresentFn(canPresent)
jvanverthaf236b52016-05-20 06:01:06 -070036 , fSurface(VK_NULL_HANDLE)
jvanvertha8d0d6c2016-05-05 12:32:03 -070037 , fSwapchain(VK_NULL_HANDLE)
jvanverthaf236b52016-05-20 06:01:06 -070038 , fImages(nullptr)
39 , fImageLayouts(nullptr)
40 , fSurfaces(nullptr)
jvanvertha8d0d6c2016-05-05 12:32:03 -070041 , fCommandPool(VK_NULL_HANDLE)
42 , fBackbuffers(nullptr) {
Jim Van Verthfbdc0802017-05-02 16:15:53 -040043 this->initializeContext();
44}
jvanverth9f372462016-04-06 06:08:59 -070045
Jim Van Verthfbdc0802017-05-02 16:15:53 -040046void VulkanWindowContext::initializeContext() {
jvanverth9f372462016-04-06 06:08:59 -070047 // any config code here (particularly for msaa)?
Brian Salomonc1889822017-04-24 16:49:05 -040048 fBackendContext.reset(GrVkBackendContext::Create(vkGetInstanceProcAddr, vkGetDeviceProcAddr,
Jim Van Verthfbdc0802017-05-02 16:15:53 -040049 &fPresentQueueIndex, fCanPresentFn));
jvanverth9f372462016-04-06 06:08:59 -070050
jvanverthb0d43522016-04-21 11:46:23 -070051 if (!(fBackendContext->fExtensions & kKHR_surface_GrVkExtensionFlag) ||
52 !(fBackendContext->fExtensions & kKHR_swapchain_GrVkExtensionFlag)) {
53 fBackendContext.reset(nullptr);
54 return;
55 }
56
57 VkInstance instance = fBackendContext->fInstance;
58 VkDevice device = fBackendContext->fDevice;
59 GET_PROC(DestroySurfaceKHR);
60 GET_PROC(GetPhysicalDeviceSurfaceSupportKHR);
61 GET_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
62 GET_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
63 GET_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
64 GET_DEV_PROC(CreateSwapchainKHR);
65 GET_DEV_PROC(DestroySwapchainKHR);
66 GET_DEV_PROC(GetSwapchainImagesKHR);
67 GET_DEV_PROC(AcquireNextImageKHR);
68 GET_DEV_PROC(QueuePresentKHR);
jvanverth9f372462016-04-06 06:08:59 -070069
Greg Daniel02611d92017-07-25 10:05:01 -040070 fContext = GrContext::MakeVulkan(fBackendContext.get(), fDisplayParams.fGrContextOptions);
jvanverth9f372462016-04-06 06:08:59 -070071
Jim Van Verthfbdc0802017-05-02 16:15:53 -040072 fSurface = fCreateVkSurfaceFn(instance);
jvanverth9f372462016-04-06 06:08:59 -070073 if (VK_NULL_HANDLE == fSurface) {
74 fBackendContext.reset(nullptr);
75 return;
76 }
77
jvanverth9f372462016-04-06 06:08:59 -070078 VkBool32 supported;
jvanverthb0d43522016-04-21 11:46:23 -070079 VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fBackendContext->fPhysicalDevice,
80 fPresentQueueIndex, fSurface,
81 &supported);
jvanverth9f372462016-04-06 06:08:59 -070082 if (VK_SUCCESS != res) {
83 this->destroyContext();
84 return;
85 }
86
Jim Van Verthfbdc0802017-05-02 16:15:53 -040087 if (!this->createSwapchain(-1, -1, fDisplayParams)) {
jvanverth9f372462016-04-06 06:08:59 -070088 this->destroyContext();
89 return;
90 }
91
92 // create presentQueue
93 vkGetDeviceQueue(fBackendContext->fDevice, fPresentQueueIndex, 0, &fPresentQueue);
jvanverth9f372462016-04-06 06:08:59 -070094}
95
bsalomonccde4ab2016-07-27 08:50:12 -070096bool VulkanWindowContext::createSwapchain(int width, int height,
brianosman05de2162016-05-06 13:28:57 -070097 const DisplayParams& params) {
jvanverth9f372462016-04-06 06:08:59 -070098 // check for capabilities
99 VkSurfaceCapabilitiesKHR caps;
jvanverthb0d43522016-04-21 11:46:23 -0700100 VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fBackendContext->fPhysicalDevice,
101 fSurface, &caps);
jvanverth9f372462016-04-06 06:08:59 -0700102 if (VK_SUCCESS != res) {
103 return false;
104 }
105
106 uint32_t surfaceFormatCount;
jvanverthb0d43522016-04-21 11:46:23 -0700107 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
108 &surfaceFormatCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700109 if (VK_SUCCESS != res) {
110 return false;
111 }
112
113 SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatKHR));
114 VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700115 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
116 &surfaceFormatCount, surfaceFormats);
jvanverth9f372462016-04-06 06:08:59 -0700117 if (VK_SUCCESS != res) {
118 return false;
119 }
120
121 uint32_t presentModeCount;
jvanverthb0d43522016-04-21 11:46:23 -0700122 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
123 &presentModeCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700124 if (VK_SUCCESS != res) {
125 return false;
126 }
127
128 SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR));
129 VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700130 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700131 &presentModeCount, presentModes);
jvanverth9f372462016-04-06 06:08:59 -0700132 if (VK_SUCCESS != res) {
133 return false;
134 }
135
136 VkExtent2D extent = caps.currentExtent;
137 // use the hints
138 if (extent.width == (uint32_t)-1) {
139 extent.width = width;
140 extent.height = height;
141 }
142
jvanverth9fab59d2016-04-06 12:08:51 -0700143 // clamp width; to protect us from broken hints
jvanverth9f372462016-04-06 06:08:59 -0700144 if (extent.width < caps.minImageExtent.width) {
145 extent.width = caps.minImageExtent.width;
146 } else if (extent.width > caps.maxImageExtent.width) {
147 extent.width = caps.maxImageExtent.width;
148 }
149 // clamp height
150 if (extent.height < caps.minImageExtent.height) {
151 extent.height = caps.minImageExtent.height;
152 } else if (extent.height > caps.maxImageExtent.height) {
153 extent.height = caps.maxImageExtent.height;
154 }
jvanverth1d155962016-05-23 13:13:36 -0700155
jvanverth9f372462016-04-06 06:08:59 -0700156 fWidth = (int)extent.width;
157 fHeight = (int)extent.height;
158
159 uint32_t imageCount = caps.minImageCount + 2;
160 if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) {
161 // Application must settle for fewer images than desired:
162 imageCount = caps.maxImageCount;
163 }
164
165 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
166 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
167 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
168 SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags);
169 SkASSERT(caps.supportedTransforms & caps.currentTransform);
170 SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
171 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR));
172 VkCompositeAlphaFlagBitsKHR composite_alpha =
173 (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ?
174 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR :
175 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
176
brianosman05de2162016-05-06 13:28:57 -0700177 // Pick our surface format. For now, just make sure it matches our sRGB request:
178 VkFormat surfaceFormat = VK_FORMAT_UNDEFINED;
179 VkColorSpaceKHR colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500180 auto srgbColorSpace = SkColorSpace::MakeSRGB();
brianosmanb109b8c2016-06-16 13:03:24 -0700181 bool wantSRGB = srgbColorSpace == params.fColorSpace;
brianosman05de2162016-05-06 13:28:57 -0700182 for (uint32_t i = 0; i < surfaceFormatCount; ++i) {
Greg Daniel94403452017-04-18 15:52:36 -0400183 GrPixelConfig config = GrVkFormatToPixelConfig(surfaceFormats[i].format);
184 if (kUnknown_GrPixelConfig != config &&
brianosman05de2162016-05-06 13:28:57 -0700185 GrPixelConfigIsSRGB(config) == wantSRGB) {
186 surfaceFormat = surfaceFormats[i].format;
187 colorSpace = surfaceFormats[i].colorSpace;
188 break;
189 }
190 }
191 fDisplayParams = params;
csmartdalton578f0642017-02-24 16:04:47 -0700192 fSampleCount = params.fMSAASampleCount;
193 fStencilBits = 8;
brianosman05de2162016-05-06 13:28:57 -0700194
195 if (VK_FORMAT_UNDEFINED == surfaceFormat) {
196 return false;
197 }
jvanvertha4b0fed2016-04-27 11:42:21 -0700198
jvanverth3d6ed3a2016-04-07 11:09:51 -0700199 // If mailbox mode is available, use it, as it is the lowest-latency non-
200 // tearing mode. If not, fall back to FIFO which is always available.
jvanverth9f372462016-04-06 06:08:59 -0700201 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700202 for (uint32_t i = 0; i < presentModeCount; ++i) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700203 // use mailbox
204 if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) {
jvanverth9f372462016-04-06 06:08:59 -0700205 mode = presentModes[i];
jvanverth3d6ed3a2016-04-07 11:09:51 -0700206 break;
jvanverth9f372462016-04-06 06:08:59 -0700207 }
208 }
209
210 VkSwapchainCreateInfoKHR swapchainCreateInfo;
211 memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR));
212 swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
213 swapchainCreateInfo.surface = fSurface;
214 swapchainCreateInfo.minImageCount = imageCount;
jvanvertha4b0fed2016-04-27 11:42:21 -0700215 swapchainCreateInfo.imageFormat = surfaceFormat;
216 swapchainCreateInfo.imageColorSpace = colorSpace;
jvanverth9f372462016-04-06 06:08:59 -0700217 swapchainCreateInfo.imageExtent = extent;
218 swapchainCreateInfo.imageArrayLayers = 1;
219 swapchainCreateInfo.imageUsage = usageFlags;
220
jvanverthb0d43522016-04-21 11:46:23 -0700221 uint32_t queueFamilies[] = { fBackendContext->fGraphicsQueueIndex, fPresentQueueIndex };
222 if (fBackendContext->fGraphicsQueueIndex != fPresentQueueIndex) {
jvanverth9f372462016-04-06 06:08:59 -0700223 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
224 swapchainCreateInfo.queueFamilyIndexCount = 2;
225 swapchainCreateInfo.pQueueFamilyIndices = queueFamilies;
226 } else {
227 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
228 swapchainCreateInfo.queueFamilyIndexCount = 0;
229 swapchainCreateInfo.pQueueFamilyIndices = nullptr;
230 }
231
Greg Daniele897b972016-10-06 13:57:57 -0400232 swapchainCreateInfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700233 swapchainCreateInfo.compositeAlpha = composite_alpha;
234 swapchainCreateInfo.presentMode = mode;
235 swapchainCreateInfo.clipped = true;
236 swapchainCreateInfo.oldSwapchain = fSwapchain;
237
jvanverthb0d43522016-04-21 11:46:23 -0700238 res = fCreateSwapchainKHR(fBackendContext->fDevice, &swapchainCreateInfo, nullptr, &fSwapchain);
jvanverth9f372462016-04-06 06:08:59 -0700239 if (VK_SUCCESS != res) {
240 return false;
241 }
242
243 // destroy the old swapchain
244 if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) {
245 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
246
247 this->destroyBuffers();
248
jvanverthb0d43522016-04-21 11:46:23 -0700249 fDestroySwapchainKHR(fBackendContext->fDevice, swapchainCreateInfo.oldSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700250 }
251
egdaniel58a8d922016-04-21 08:03:10 -0700252 this->createBuffers(swapchainCreateInfo.imageFormat);
jvanverth9f372462016-04-06 06:08:59 -0700253
254 return true;
255}
256
jvanvertha8d0d6c2016-05-05 12:32:03 -0700257void VulkanWindowContext::createBuffers(VkFormat format) {
Greg Daniel94403452017-04-18 15:52:36 -0400258 fPixelConfig = GrVkFormatToPixelConfig(format);
259 SkASSERT(kUnknown_GrPixelConfig != fPixelConfig);
egdaniel58a8d922016-04-21 08:03:10 -0700260
jvanverthb0d43522016-04-21 11:46:23 -0700261 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700262 SkASSERT(fImageCount);
263 fImages = new VkImage[fImageCount];
jvanverthb0d43522016-04-21 11:46:23 -0700264 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, fImages);
jvanverth9f372462016-04-06 06:08:59 -0700265
266 // set up initial image layouts and create surfaces
267 fImageLayouts = new VkImageLayout[fImageCount];
268 fSurfaces = new sk_sp<SkSurface>[fImageCount];
269 for (uint32_t i = 0; i < fImageCount; ++i) {
270 fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED;
271
egdanielb2df0c22016-05-13 11:30:37 -0700272 GrVkImageInfo info;
jvanverth9f372462016-04-06 06:08:59 -0700273 info.fImage = fImages[i];
jvanverth9d54afc2016-09-20 09:20:03 -0700274 info.fAlloc = { VK_NULL_HANDLE, 0, 0, 0 };
egdaniel580fa592016-08-31 11:03:50 -0700275 info.fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
jvanverth9f372462016-04-06 06:08:59 -0700276 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
egdaniel58a8d922016-04-21 08:03:10 -0700277 info.fFormat = format;
jvanverth3622a172016-05-10 06:42:18 -0700278 info.fLevelCount = 1;
jvanverthaf236b52016-05-20 06:01:06 -0700279
Greg Daniel207282e2017-04-26 13:29:21 -0400280 GrBackendTexture backendTex(fWidth, fHeight, info);
Greg Daniele79b4732017-04-20 14:07:46 -0400281
Greg Daniel02611d92017-07-25 10:05:01 -0400282 fSurfaces[i] = SkSurface::MakeFromBackendTextureAsRenderTarget(fContext.get(), backendTex,
Greg Daniele79b4732017-04-20 14:07:46 -0400283 kTopLeft_GrSurfaceOrigin,
284 fSampleCount,
285 fDisplayParams.fColorSpace,
286 &fSurfaceProps);
jvanverth9f372462016-04-06 06:08:59 -0700287 }
288
289 // create the command pool for the command buffers
290 if (VK_NULL_HANDLE == fCommandPool) {
291 VkCommandPoolCreateInfo commandPoolInfo;
292 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
293 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
294 // this needs to be on the render queue
jvanverthb0d43522016-04-21 11:46:23 -0700295 commandPoolInfo.queueFamilyIndex = fBackendContext->fGraphicsQueueIndex;
jvanverth9f372462016-04-06 06:08:59 -0700296 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
297 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
298 CreateCommandPool(fBackendContext->fDevice, &commandPoolInfo,
299 nullptr, &fCommandPool));
300 }
301
302 // set up the backbuffers
303 VkSemaphoreCreateInfo semaphoreInfo;
304 memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo));
305 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
306 semaphoreInfo.pNext = nullptr;
307 semaphoreInfo.flags = 0;
308 VkCommandBufferAllocateInfo commandBuffersInfo;
309 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
310 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
311 commandBuffersInfo.pNext = nullptr;
312 commandBuffersInfo.commandPool = fCommandPool;
313 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
314 commandBuffersInfo.commandBufferCount = 2;
315 VkFenceCreateInfo fenceInfo;
316 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
317 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
318 fenceInfo.pNext = nullptr;
319 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
320
Greg Daniel1f05f442016-10-27 16:37:17 -0400321 // we create one additional backbuffer structure here, because we want to
jvanverth9f372462016-04-06 06:08:59 -0700322 // give the command buffers they contain a chance to finish before we cycle back
323 fBackbuffers = new BackbufferInfo[fImageCount + 1];
324 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
325 fBackbuffers[i].fImageIndex = -1;
326 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
327 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
328 nullptr, &fBackbuffers[i].fAcquireSemaphore));
329 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
330 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
331 nullptr, &fBackbuffers[i].fRenderSemaphore));
332 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
333 AllocateCommandBuffers(fBackendContext->fDevice, &commandBuffersInfo,
334 fBackbuffers[i].fTransitionCmdBuffers));
335 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
336 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
337 &fBackbuffers[i].fUsageFences[0]));
338 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
339 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
340 &fBackbuffers[i].fUsageFences[1]));
341 }
342 fCurrentBackbufferIndex = fImageCount;
343}
344
jvanvertha8d0d6c2016-05-05 12:32:03 -0700345void VulkanWindowContext::destroyBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700346
347 if (fBackbuffers) {
348 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
349 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700350 WaitForFences(fBackendContext->fDevice, 2,
jvanverth9f372462016-04-06 06:08:59 -0700351 fBackbuffers[i].fUsageFences,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700352 true, UINT64_MAX));
jvanverth9f372462016-04-06 06:08:59 -0700353 fBackbuffers[i].fImageIndex = -1;
354 GR_VK_CALL(fBackendContext->fInterface,
355 DestroySemaphore(fBackendContext->fDevice,
356 fBackbuffers[i].fAcquireSemaphore,
357 nullptr));
358 GR_VK_CALL(fBackendContext->fInterface,
359 DestroySemaphore(fBackendContext->fDevice,
360 fBackbuffers[i].fRenderSemaphore,
361 nullptr));
362 GR_VK_CALL(fBackendContext->fInterface,
363 FreeCommandBuffers(fBackendContext->fDevice, fCommandPool, 2,
364 fBackbuffers[i].fTransitionCmdBuffers));
365 GR_VK_CALL(fBackendContext->fInterface,
366 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[0], 0));
367 GR_VK_CALL(fBackendContext->fInterface,
368 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[1], 0));
369 }
370 }
371
372 delete[] fBackbuffers;
373 fBackbuffers = nullptr;
374
jvanverthaf236b52016-05-20 06:01:06 -0700375 // Does this actually free the surfaces?
jvanverth9f372462016-04-06 06:08:59 -0700376 delete[] fSurfaces;
377 fSurfaces = nullptr;
378 delete[] fImageLayouts;
379 fImageLayouts = nullptr;
380 delete[] fImages;
381 fImages = nullptr;
382}
383
jvanvertha8d0d6c2016-05-05 12:32:03 -0700384VulkanWindowContext::~VulkanWindowContext() {
jvanverth9f372462016-04-06 06:08:59 -0700385 this->destroyContext();
386}
387
jvanvertha8d0d6c2016-05-05 12:32:03 -0700388void VulkanWindowContext::destroyContext() {
jvanverth9f372462016-04-06 06:08:59 -0700389 if (!fBackendContext.get()) {
390 return;
391 }
392
jvanverth85f758c2016-05-27 06:47:08 -0700393 GR_VK_CALL(fBackendContext->fInterface, QueueWaitIdle(fPresentQueue));
jvanverth9f372462016-04-06 06:08:59 -0700394 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
395
396 this->destroyBuffers();
397
398 if (VK_NULL_HANDLE != fCommandPool) {
399 GR_VK_CALL(fBackendContext->fInterface, DestroyCommandPool(fBackendContext->fDevice,
400 fCommandPool, nullptr));
401 fCommandPool = VK_NULL_HANDLE;
402 }
403
404 if (VK_NULL_HANDLE != fSwapchain) {
jvanverthb0d43522016-04-21 11:46:23 -0700405 fDestroySwapchainKHR(fBackendContext->fDevice, fSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700406 fSwapchain = VK_NULL_HANDLE;
407 }
408
409 if (VK_NULL_HANDLE != fSurface) {
jvanverthb0d43522016-04-21 11:46:23 -0700410 fDestroySurfaceKHR(fBackendContext->fInstance, fSurface, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700411 fSurface = VK_NULL_HANDLE;
412 }
413
Greg Daniel02611d92017-07-25 10:05:01 -0400414 fContext.reset();
jvanverth9f372462016-04-06 06:08:59 -0700415
416 fBackendContext.reset(nullptr);
417}
418
jvanvertha8d0d6c2016-05-05 12:32:03 -0700419VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer() {
jvanverth9f372462016-04-06 06:08:59 -0700420 SkASSERT(fBackbuffers);
421
422 ++fCurrentBackbufferIndex;
egdaniel804af7d2016-09-26 12:30:46 -0700423 if (fCurrentBackbufferIndex > fImageCount) {
jvanverth9f372462016-04-06 06:08:59 -0700424 fCurrentBackbufferIndex = 0;
425 }
426
427 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
jvanverth9f372462016-04-06 06:08:59 -0700428 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
429 WaitForFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences,
430 true, UINT64_MAX));
431 return backbuffer;
432}
433
jvanverthaf236b52016-05-20 06:01:06 -0700434sk_sp<SkSurface> VulkanWindowContext::getBackbufferSurface() {
jvanverth9f372462016-04-06 06:08:59 -0700435 BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
436 SkASSERT(backbuffer);
437
438 // reset the fence
439 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
440 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
441 // semaphores should be in unsignaled state
442
443 // acquire the image
jvanverthb0d43522016-04-21 11:46:23 -0700444 VkResult res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
445 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
446 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700447 if (VK_ERROR_SURFACE_LOST_KHR == res) {
448 // need to figure out how to create a new vkSurface without the platformData*
jvanverth3d6ed3a2016-04-07 11:09:51 -0700449 // maybe use attach somehow? but need a Window
jvanverth9f372462016-04-06 06:08:59 -0700450 return nullptr;
451 }
jvanverth3d6ed3a2016-04-07 11:09:51 -0700452 if (VK_ERROR_OUT_OF_DATE_KHR == res) {
jvanverth9f372462016-04-06 06:08:59 -0700453 // tear swapchain down and try again
Greg Daniel1f05f442016-10-27 16:37:17 -0400454 if (!this->createSwapchain(-1, -1, fDisplayParams)) {
jvanverth9f372462016-04-06 06:08:59 -0700455 return nullptr;
456 }
Jim Van Verthd63c1022017-01-05 13:50:49 -0500457 backbuffer = this->getAvailableBackbuffer();
458 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
459 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
jvanverth9f372462016-04-06 06:08:59 -0700460
461 // acquire the image
jvanvertha8d0d6c2016-05-05 12:32:03 -0700462 res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
jvanverthb0d43522016-04-21 11:46:23 -0700463 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
464 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700465
466 if (VK_SUCCESS != res) {
467 return nullptr;
468 }
469 }
470
471 // set up layout transfer from initial to color attachment
472 VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex];
egdaniel580fa592016-08-31 11:03:50 -0700473 SkASSERT(VK_IMAGE_LAYOUT_UNDEFINED == layout || VK_IMAGE_LAYOUT_PRESENT_SRC_KHR == layout);
jvanverth9f372462016-04-06 06:08:59 -0700474 VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
475 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT :
476 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
477 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700478 VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
jvanverth9f372462016-04-06 06:08:59 -0700479 0 : VK_ACCESS_MEMORY_READ_BIT;
480 VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
481
482 VkImageMemoryBarrier imageMemoryBarrier = {
483 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
484 NULL, // pNext
485 srcAccessMask, // outputMask
486 dstAccessMask, // inputMask
487 layout, // oldLayout
488 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout
489 fPresentQueueIndex, // srcQueueFamilyIndex
jvanverthb0d43522016-04-21 11:46:23 -0700490 fBackendContext->fGraphicsQueueIndex, // dstQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700491 fImages[backbuffer->fImageIndex], // image
492 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
493 };
494 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
495 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[0], 0));
496 VkCommandBufferBeginInfo info;
497 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
498 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
499 info.flags = 0;
500 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
501 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info));
502
jvanvertha8d0d6c2016-05-05 12:32:03 -0700503 GR_VK_CALL(fBackendContext->fInterface,
504 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0],
505 srcStageMask, dstStageMask, 0,
506 0, nullptr,
507 0, nullptr,
508 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700509
510 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700511 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0]));
jvanverth9f372462016-04-06 06:08:59 -0700512
egdaniel58a8d922016-04-21 08:03:10 -0700513 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanverth9f372462016-04-06 06:08:59 -0700514 // insert the layout transfer into the queue and wait on the acquire
515 VkSubmitInfo submitInfo;
516 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
517 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
518 submitInfo.waitSemaphoreCount = 1;
519 submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore;
egdaniel58a8d922016-04-21 08:03:10 -0700520 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
jvanverth9f372462016-04-06 06:08:59 -0700521 submitInfo.commandBufferCount = 1;
522 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0];
523 submitInfo.signalSemaphoreCount = 0;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700524
jvanverth9f372462016-04-06 06:08:59 -0700525 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700526 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700527 backbuffer->fUsageFences[0]));
528
egdaniel580fa592016-08-31 11:03:50 -0700529 GrVkImageInfo* imageInfo;
530 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
531 surface->getRenderTargetHandle((GrBackendObject*)&imageInfo,
532 SkSurface::kFlushRead_BackendHandleAccess);
533 imageInfo->updateImageLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
534
535 return sk_ref_sp(surface);
jvanverth9f372462016-04-06 06:08:59 -0700536}
537
jvanvertha8d0d6c2016-05-05 12:32:03 -0700538void VulkanWindowContext::swapBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700539
540 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
egdaniel580fa592016-08-31 11:03:50 -0700541 GrVkImageInfo* imageInfo;
542 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
543 surface->getRenderTargetHandle((GrBackendObject*)&imageInfo,
544 SkSurface::kFlushRead_BackendHandleAccess);
545 // Check to make sure we never change the actually wrapped image
546 SkASSERT(imageInfo->fImage == fImages[backbuffer->fImageIndex]);
jvanverth9f372462016-04-06 06:08:59 -0700547
egdaniel580fa592016-08-31 11:03:50 -0700548 VkImageLayout layout = imageInfo->fImageLayout;
549 VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(layout);
jvanverth9f372462016-04-06 06:08:59 -0700550 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
egdaniel580fa592016-08-31 11:03:50 -0700551 VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(layout);
jvanverth9f372462016-04-06 06:08:59 -0700552 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
553
554 VkImageMemoryBarrier imageMemoryBarrier = {
555 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
556 NULL, // pNext
557 srcAccessMask, // outputMask
558 dstAccessMask, // inputMask
559 layout, // oldLayout
560 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout
jvanverthb0d43522016-04-21 11:46:23 -0700561 fBackendContext->fGraphicsQueueIndex, // srcQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700562 fPresentQueueIndex, // dstQueueFamilyIndex
563 fImages[backbuffer->fImageIndex], // image
564 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
565 };
566 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
567 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[1], 0));
568 VkCommandBufferBeginInfo info;
569 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
570 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
571 info.flags = 0;
572 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
573 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info));
574 GR_VK_CALL(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700575 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
576 srcStageMask, dstStageMask, 0,
577 0, nullptr,
578 0, nullptr,
579 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700580 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
581 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1]));
582
583 fImageLayouts[backbuffer->fImageIndex] = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
584
585 // insert the layout transfer into the queue and wait on the acquire
586 VkSubmitInfo submitInfo;
587 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
588 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
589 submitInfo.waitSemaphoreCount = 0;
590 submitInfo.pWaitDstStageMask = 0;
591 submitInfo.commandBufferCount = 1;
592 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[1];
593 submitInfo.signalSemaphoreCount = 1;
594 submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore;
595
596 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700597 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700598 backbuffer->fUsageFences[1]));
599
600 // Submit present operation to present queue
601 const VkPresentInfoKHR presentInfo =
602 {
603 VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType
604 NULL, // pNext
605 1, // waitSemaphoreCount
606 &backbuffer->fRenderSemaphore, // pWaitSemaphores
607 1, // swapchainCount
608 &fSwapchain, // pSwapchains
609 &backbuffer->fImageIndex, // pImageIndices
610 NULL // pResults
611 };
612
jvanverthb0d43522016-04-21 11:46:23 -0700613 fQueuePresentKHR(fPresentQueue, &presentInfo);
jvanverth9f372462016-04-06 06:08:59 -0700614}
jvanvertha8d0d6c2016-05-05 12:32:03 -0700615
616} //namespace sk_app