blob: 261206c2211c6a354c0449ed7d2efd5f6af113bb [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
csmartdalton008b9d82017-02-22 12:00:42 -070070 fContext = GrContext::Create(kVulkan_GrBackend, (GrBackendContext) fBackendContext.get(),
Jim Van Verthfbdc0802017-05-02 16:15:53 -040071 fDisplayParams.fGrContextOptions);
jvanverth9f372462016-04-06 06:08:59 -070072
Jim Van Verthfbdc0802017-05-02 16:15:53 -040073 fSurface = fCreateVkSurfaceFn(instance);
jvanverth9f372462016-04-06 06:08:59 -070074 if (VK_NULL_HANDLE == fSurface) {
75 fBackendContext.reset(nullptr);
76 return;
77 }
78
jvanverth9f372462016-04-06 06:08:59 -070079 VkBool32 supported;
jvanverthb0d43522016-04-21 11:46:23 -070080 VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fBackendContext->fPhysicalDevice,
81 fPresentQueueIndex, fSurface,
82 &supported);
jvanverth9f372462016-04-06 06:08:59 -070083 if (VK_SUCCESS != res) {
84 this->destroyContext();
85 return;
86 }
87
Jim Van Verthfbdc0802017-05-02 16:15:53 -040088 if (!this->createSwapchain(-1, -1, fDisplayParams)) {
jvanverth9f372462016-04-06 06:08:59 -070089 this->destroyContext();
90 return;
91 }
92
93 // create presentQueue
94 vkGetDeviceQueue(fBackendContext->fDevice, fPresentQueueIndex, 0, &fPresentQueue);
jvanverth9f372462016-04-06 06:08:59 -070095}
96
bsalomonccde4ab2016-07-27 08:50:12 -070097bool VulkanWindowContext::createSwapchain(int width, int height,
brianosman05de2162016-05-06 13:28:57 -070098 const DisplayParams& params) {
jvanverth9f372462016-04-06 06:08:59 -070099 // check for capabilities
100 VkSurfaceCapabilitiesKHR caps;
jvanverthb0d43522016-04-21 11:46:23 -0700101 VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fBackendContext->fPhysicalDevice,
102 fSurface, &caps);
jvanverth9f372462016-04-06 06:08:59 -0700103 if (VK_SUCCESS != res) {
104 return false;
105 }
106
107 uint32_t surfaceFormatCount;
jvanverthb0d43522016-04-21 11:46:23 -0700108 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
109 &surfaceFormatCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700110 if (VK_SUCCESS != res) {
111 return false;
112 }
113
114 SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatKHR));
115 VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700116 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
117 &surfaceFormatCount, surfaceFormats);
jvanverth9f372462016-04-06 06:08:59 -0700118 if (VK_SUCCESS != res) {
119 return false;
120 }
121
122 uint32_t presentModeCount;
jvanverthb0d43522016-04-21 11:46:23 -0700123 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
124 &presentModeCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700125 if (VK_SUCCESS != res) {
126 return false;
127 }
128
129 SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR));
130 VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700131 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700132 &presentModeCount, presentModes);
jvanverth9f372462016-04-06 06:08:59 -0700133 if (VK_SUCCESS != res) {
134 return false;
135 }
136
137 VkExtent2D extent = caps.currentExtent;
138 // use the hints
139 if (extent.width == (uint32_t)-1) {
140 extent.width = width;
141 extent.height = height;
142 }
143
jvanverth9fab59d2016-04-06 12:08:51 -0700144 // clamp width; to protect us from broken hints
jvanverth9f372462016-04-06 06:08:59 -0700145 if (extent.width < caps.minImageExtent.width) {
146 extent.width = caps.minImageExtent.width;
147 } else if (extent.width > caps.maxImageExtent.width) {
148 extent.width = caps.maxImageExtent.width;
149 }
150 // clamp height
151 if (extent.height < caps.minImageExtent.height) {
152 extent.height = caps.minImageExtent.height;
153 } else if (extent.height > caps.maxImageExtent.height) {
154 extent.height = caps.maxImageExtent.height;
155 }
jvanverth1d155962016-05-23 13:13:36 -0700156
jvanverth9f372462016-04-06 06:08:59 -0700157 fWidth = (int)extent.width;
158 fHeight = (int)extent.height;
159
160 uint32_t imageCount = caps.minImageCount + 2;
161 if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) {
162 // Application must settle for fewer images than desired:
163 imageCount = caps.maxImageCount;
164 }
165
166 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
167 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
168 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
169 SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags);
170 SkASSERT(caps.supportedTransforms & caps.currentTransform);
171 SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
172 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR));
173 VkCompositeAlphaFlagBitsKHR composite_alpha =
174 (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ?
175 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR :
176 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
177
brianosman05de2162016-05-06 13:28:57 -0700178 // Pick our surface format. For now, just make sure it matches our sRGB request:
179 VkFormat surfaceFormat = VK_FORMAT_UNDEFINED;
180 VkColorSpaceKHR colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500181 auto srgbColorSpace = SkColorSpace::MakeSRGB();
brianosmanb109b8c2016-06-16 13:03:24 -0700182 bool wantSRGB = srgbColorSpace == params.fColorSpace;
brianosman05de2162016-05-06 13:28:57 -0700183 for (uint32_t i = 0; i < surfaceFormatCount; ++i) {
Greg Daniel94403452017-04-18 15:52:36 -0400184 GrPixelConfig config = GrVkFormatToPixelConfig(surfaceFormats[i].format);
185 if (kUnknown_GrPixelConfig != config &&
brianosman05de2162016-05-06 13:28:57 -0700186 GrPixelConfigIsSRGB(config) == wantSRGB) {
187 surfaceFormat = surfaceFormats[i].format;
188 colorSpace = surfaceFormats[i].colorSpace;
189 break;
190 }
191 }
192 fDisplayParams = params;
csmartdalton578f0642017-02-24 16:04:47 -0700193 fSampleCount = params.fMSAASampleCount;
194 fStencilBits = 8;
brianosman05de2162016-05-06 13:28:57 -0700195
196 if (VK_FORMAT_UNDEFINED == surfaceFormat) {
197 return false;
198 }
jvanvertha4b0fed2016-04-27 11:42:21 -0700199
jvanverth3d6ed3a2016-04-07 11:09:51 -0700200 // If mailbox mode is available, use it, as it is the lowest-latency non-
201 // tearing mode. If not, fall back to FIFO which is always available.
jvanverth9f372462016-04-06 06:08:59 -0700202 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700203 for (uint32_t i = 0; i < presentModeCount; ++i) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700204 // use mailbox
205 if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) {
jvanverth9f372462016-04-06 06:08:59 -0700206 mode = presentModes[i];
jvanverth3d6ed3a2016-04-07 11:09:51 -0700207 break;
jvanverth9f372462016-04-06 06:08:59 -0700208 }
209 }
210
211 VkSwapchainCreateInfoKHR swapchainCreateInfo;
212 memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR));
213 swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
214 swapchainCreateInfo.surface = fSurface;
215 swapchainCreateInfo.minImageCount = imageCount;
jvanvertha4b0fed2016-04-27 11:42:21 -0700216 swapchainCreateInfo.imageFormat = surfaceFormat;
217 swapchainCreateInfo.imageColorSpace = colorSpace;
jvanverth9f372462016-04-06 06:08:59 -0700218 swapchainCreateInfo.imageExtent = extent;
219 swapchainCreateInfo.imageArrayLayers = 1;
220 swapchainCreateInfo.imageUsage = usageFlags;
221
jvanverthb0d43522016-04-21 11:46:23 -0700222 uint32_t queueFamilies[] = { fBackendContext->fGraphicsQueueIndex, fPresentQueueIndex };
223 if (fBackendContext->fGraphicsQueueIndex != fPresentQueueIndex) {
jvanverth9f372462016-04-06 06:08:59 -0700224 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
225 swapchainCreateInfo.queueFamilyIndexCount = 2;
226 swapchainCreateInfo.pQueueFamilyIndices = queueFamilies;
227 } else {
228 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
229 swapchainCreateInfo.queueFamilyIndexCount = 0;
230 swapchainCreateInfo.pQueueFamilyIndices = nullptr;
231 }
232
Greg Daniele897b972016-10-06 13:57:57 -0400233 swapchainCreateInfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700234 swapchainCreateInfo.compositeAlpha = composite_alpha;
235 swapchainCreateInfo.presentMode = mode;
236 swapchainCreateInfo.clipped = true;
237 swapchainCreateInfo.oldSwapchain = fSwapchain;
238
jvanverthb0d43522016-04-21 11:46:23 -0700239 res = fCreateSwapchainKHR(fBackendContext->fDevice, &swapchainCreateInfo, nullptr, &fSwapchain);
jvanverth9f372462016-04-06 06:08:59 -0700240 if (VK_SUCCESS != res) {
241 return false;
242 }
243
244 // destroy the old swapchain
245 if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) {
246 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
247
248 this->destroyBuffers();
249
jvanverthb0d43522016-04-21 11:46:23 -0700250 fDestroySwapchainKHR(fBackendContext->fDevice, swapchainCreateInfo.oldSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700251 }
252
egdaniel58a8d922016-04-21 08:03:10 -0700253 this->createBuffers(swapchainCreateInfo.imageFormat);
jvanverth9f372462016-04-06 06:08:59 -0700254
255 return true;
256}
257
jvanvertha8d0d6c2016-05-05 12:32:03 -0700258void VulkanWindowContext::createBuffers(VkFormat format) {
Greg Daniel94403452017-04-18 15:52:36 -0400259 fPixelConfig = GrVkFormatToPixelConfig(format);
260 SkASSERT(kUnknown_GrPixelConfig != fPixelConfig);
egdaniel58a8d922016-04-21 08:03:10 -0700261
jvanverthb0d43522016-04-21 11:46:23 -0700262 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700263 SkASSERT(fImageCount);
264 fImages = new VkImage[fImageCount];
jvanverthb0d43522016-04-21 11:46:23 -0700265 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, fImages);
jvanverth9f372462016-04-06 06:08:59 -0700266
267 // set up initial image layouts and create surfaces
268 fImageLayouts = new VkImageLayout[fImageCount];
269 fSurfaces = new sk_sp<SkSurface>[fImageCount];
270 for (uint32_t i = 0; i < fImageCount; ++i) {
271 fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED;
272
egdanielb2df0c22016-05-13 11:30:37 -0700273 GrVkImageInfo info;
jvanverth9f372462016-04-06 06:08:59 -0700274 info.fImage = fImages[i];
jvanverth9d54afc2016-09-20 09:20:03 -0700275 info.fAlloc = { VK_NULL_HANDLE, 0, 0, 0 };
egdaniel580fa592016-08-31 11:03:50 -0700276 info.fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
jvanverth9f372462016-04-06 06:08:59 -0700277 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
egdaniel58a8d922016-04-21 08:03:10 -0700278 info.fFormat = format;
jvanverth3622a172016-05-10 06:42:18 -0700279 info.fLevelCount = 1;
jvanverthaf236b52016-05-20 06:01:06 -0700280
Greg Daniel207282e2017-04-26 13:29:21 -0400281 GrBackendTexture backendTex(fWidth, fHeight, info);
Greg Daniele79b4732017-04-20 14:07:46 -0400282
283 fSurfaces[i] = SkSurface::MakeFromBackendTextureAsRenderTarget(fContext, backendTex,
284 kTopLeft_GrSurfaceOrigin,
285 fSampleCount,
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