blob: d038c97e347291cdc935ca0e0a7a7bc71676306f [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
Greg Daniela782dcb2018-06-28 20:18:59 +000015#include "vk/GrVkExtensions.h"
Greg Daniel6ddbafc2018-05-24 12:34:29 -040016#include "vk/GrVkImage.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
Greg Daniel0db3a882018-06-28 16:47:48 +000025#define GET_PROC(F) f ## F = (PFN_vk ## F) fGetInstanceProcAddr(fInstance, "vk" #F)
26#define GET_DEV_PROC(F) f ## F = (PFN_vk ## F) fGetDeviceProcAddr(fDevice, "vk" #F)
jvanverthb0d43522016-04-21 11:46:23 -070027
jvanvertha8d0d6c2016-05-05 12:32:03 -070028namespace sk_app {
29
bsalomond1bdd1f2016-07-26 12:02:50 -070030VulkanWindowContext::VulkanWindowContext(const DisplayParams& params,
31 CreateVkSurfaceFn createVkSurface,
Greg Daniel35970ec2017-11-10 10:03:05 -050032 CanPresentFn canPresent,
33 PFN_vkGetInstanceProcAddr instProc,
34 PFN_vkGetDeviceProcAddr devProc)
Jim Van Verthfbdc0802017-05-02 16:15:53 -040035 : WindowContext(params)
36 , fCreateVkSurfaceFn(createVkSurface)
37 , fCanPresentFn(canPresent)
jvanverthaf236b52016-05-20 06:01:06 -070038 , fSurface(VK_NULL_HANDLE)
jvanvertha8d0d6c2016-05-05 12:32:03 -070039 , fSwapchain(VK_NULL_HANDLE)
jvanverthaf236b52016-05-20 06:01:06 -070040 , fImages(nullptr)
41 , fImageLayouts(nullptr)
42 , fSurfaces(nullptr)
jvanvertha8d0d6c2016-05-05 12:32:03 -070043 , fCommandPool(VK_NULL_HANDLE)
44 , fBackbuffers(nullptr) {
Greg Daniel35970ec2017-11-10 10:03:05 -050045 fGetInstanceProcAddr = instProc;
46 fGetDeviceProcAddr = devProc;
Jim Van Verthfbdc0802017-05-02 16:15:53 -040047 this->initializeContext();
48}
jvanverth9f372462016-04-06 06:08:59 -070049
Jim Van Verthfbdc0802017-05-02 16:15:53 -040050void VulkanWindowContext::initializeContext() {
jvanverth9f372462016-04-06 06:08:59 -070051 // any config code here (particularly for msaa)?
jvanverth9f372462016-04-06 06:08:59 -070052
Greg Daniel0db3a882018-06-28 16:47:48 +000053 GrVkBackendContext backendContext;
54 if (!sk_gpu_test::CreateVkBackendContext(fGetInstanceProcAddr, fGetDeviceProcAddr,
Greg Danielbe0ab882018-06-28 17:34:28 +000055 &backendContext, &fDebugCallback,
56 &fPresentQueueIndex, fCanPresentFn)) {
jvanverthb0d43522016-04-21 11:46:23 -070057 return;
58 }
59
Greg Daniela782dcb2018-06-28 20:18:59 +000060 if (!backendContext.fInterface->fExtensions.hasExtension(VK_KHR_SURFACE_EXTENSION_NAME) ||
61 !backendContext.fInterface->fExtensions.hasExtension(VK_KHR_SWAPCHAIN_EXTENSION_NAME)) {
Greg Daniel0db3a882018-06-28 16:47:48 +000062 return;
63 }
64
65 fInstance = backendContext.fInstance;
66 fPhysicalDevice = backendContext.fPhysicalDevice;
67 fDevice = backendContext.fDevice;
68 fGraphicsQueueIndex = backendContext.fGraphicsQueueIndex;
69 fGraphicsQueue = backendContext.fQueue;
70 fInterface = backendContext.fInterface;
71
72 GET_PROC(DestroyInstance);
jvanverthb0d43522016-04-21 11:46:23 -070073 GET_PROC(DestroySurfaceKHR);
74 GET_PROC(GetPhysicalDeviceSurfaceSupportKHR);
75 GET_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
76 GET_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
77 GET_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
Greg Daniel0db3a882018-06-28 16:47:48 +000078 GET_DEV_PROC(DeviceWaitIdle);
79 GET_DEV_PROC(QueueWaitIdle);
80 GET_DEV_PROC(DestroyDevice);
jvanverthb0d43522016-04-21 11:46:23 -070081 GET_DEV_PROC(CreateSwapchainKHR);
82 GET_DEV_PROC(DestroySwapchainKHR);
83 GET_DEV_PROC(GetSwapchainImagesKHR);
84 GET_DEV_PROC(AcquireNextImageKHR);
85 GET_DEV_PROC(QueuePresentKHR);
Greg Daniel35970ec2017-11-10 10:03:05 -050086 GET_DEV_PROC(GetDeviceQueue);
jvanverth9f372462016-04-06 06:08:59 -070087
Greg Daniel0db3a882018-06-28 16:47:48 +000088 fContext = GrContext::MakeVulkan(backendContext, fDisplayParams.fGrContextOptions);
jvanverth9f372462016-04-06 06:08:59 -070089
Greg Daniel0db3a882018-06-28 16:47:48 +000090 fSurface = fCreateVkSurfaceFn(fInstance);
jvanverth9f372462016-04-06 06:08:59 -070091 if (VK_NULL_HANDLE == fSurface) {
Greg Daniel0db3a882018-06-28 16:47:48 +000092 this->destroyContext();
jvanverth9f372462016-04-06 06:08:59 -070093 return;
94 }
95
jvanverth9f372462016-04-06 06:08:59 -070096 VkBool32 supported;
Greg Daniel0db3a882018-06-28 16:47:48 +000097 VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fPhysicalDevice, fPresentQueueIndex,
98 fSurface, &supported);
jvanverth9f372462016-04-06 06:08:59 -070099 if (VK_SUCCESS != res) {
100 this->destroyContext();
101 return;
102 }
103
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400104 if (!this->createSwapchain(-1, -1, fDisplayParams)) {
jvanverth9f372462016-04-06 06:08:59 -0700105 this->destroyContext();
106 return;
107 }
108
109 // create presentQueue
Greg Daniel0db3a882018-06-28 16:47:48 +0000110 fGetDeviceQueue(fDevice, fPresentQueueIndex, 0, &fPresentQueue);
jvanverth9f372462016-04-06 06:08:59 -0700111}
112
bsalomonccde4ab2016-07-27 08:50:12 -0700113bool VulkanWindowContext::createSwapchain(int width, int height,
brianosman05de2162016-05-06 13:28:57 -0700114 const DisplayParams& params) {
jvanverth9f372462016-04-06 06:08:59 -0700115 // check for capabilities
116 VkSurfaceCapabilitiesKHR caps;
Greg Daniel0db3a882018-06-28 16:47:48 +0000117 VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fPhysicalDevice, fSurface, &caps);
jvanverth9f372462016-04-06 06:08:59 -0700118 if (VK_SUCCESS != res) {
119 return false;
120 }
121
122 uint32_t surfaceFormatCount;
Greg Daniel0db3a882018-06-28 16:47:48 +0000123 res = fGetPhysicalDeviceSurfaceFormatsKHR(fPhysicalDevice, fSurface, &surfaceFormatCount,
124 nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700125 if (VK_SUCCESS != res) {
126 return false;
127 }
128
129 SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatKHR));
130 VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc.get();
Greg Daniel0db3a882018-06-28 16:47:48 +0000131 res = fGetPhysicalDeviceSurfaceFormatsKHR(fPhysicalDevice, fSurface, &surfaceFormatCount,
132 surfaceFormats);
jvanverth9f372462016-04-06 06:08:59 -0700133 if (VK_SUCCESS != res) {
134 return false;
135 }
136
137 uint32_t presentModeCount;
Greg Daniel0db3a882018-06-28 16:47:48 +0000138 res = fGetPhysicalDeviceSurfacePresentModesKHR(fPhysicalDevice, fSurface, &presentModeCount,
139 nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700140 if (VK_SUCCESS != res) {
141 return false;
142 }
143
144 SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR));
145 VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get();
Greg Daniel0db3a882018-06-28 16:47:48 +0000146 res = fGetPhysicalDeviceSurfacePresentModesKHR(fPhysicalDevice, fSurface, &presentModeCount,
147 presentModes);
jvanverth9f372462016-04-06 06:08:59 -0700148 if (VK_SUCCESS != res) {
149 return false;
150 }
151
152 VkExtent2D extent = caps.currentExtent;
153 // use the hints
154 if (extent.width == (uint32_t)-1) {
155 extent.width = width;
156 extent.height = height;
157 }
158
jvanverth9fab59d2016-04-06 12:08:51 -0700159 // clamp width; to protect us from broken hints
jvanverth9f372462016-04-06 06:08:59 -0700160 if (extent.width < caps.minImageExtent.width) {
161 extent.width = caps.minImageExtent.width;
162 } else if (extent.width > caps.maxImageExtent.width) {
163 extent.width = caps.maxImageExtent.width;
164 }
165 // clamp height
166 if (extent.height < caps.minImageExtent.height) {
167 extent.height = caps.minImageExtent.height;
168 } else if (extent.height > caps.maxImageExtent.height) {
169 extent.height = caps.maxImageExtent.height;
170 }
jvanverth1d155962016-05-23 13:13:36 -0700171
jvanverth9f372462016-04-06 06:08:59 -0700172 fWidth = (int)extent.width;
173 fHeight = (int)extent.height;
174
175 uint32_t imageCount = caps.minImageCount + 2;
176 if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) {
177 // Application must settle for fewer images than desired:
178 imageCount = caps.maxImageCount;
179 }
180
181 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
182 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
183 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
184 SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags);
185 SkASSERT(caps.supportedTransforms & caps.currentTransform);
186 SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
187 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR));
188 VkCompositeAlphaFlagBitsKHR composite_alpha =
189 (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ?
190 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR :
191 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
192
brianosman05de2162016-05-06 13:28:57 -0700193 // Pick our surface format. For now, just make sure it matches our sRGB request:
194 VkFormat surfaceFormat = VK_FORMAT_UNDEFINED;
195 VkColorSpaceKHR colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
brianosman05de2162016-05-06 13:28:57 -0700196 for (uint32_t i = 0; i < surfaceFormatCount; ++i) {
Greg Daniel81b80592017-12-13 10:20:04 -0500197 VkFormat localFormat = surfaceFormats[i].format;
Brian Osman2b23c4b2018-06-01 12:25:08 -0400198 if (GrVkFormatIsSupported(localFormat)) {
Greg Daniel81b80592017-12-13 10:20:04 -0500199 surfaceFormat = localFormat;
brianosman05de2162016-05-06 13:28:57 -0700200 colorSpace = surfaceFormats[i].colorSpace;
201 break;
202 }
203 }
204 fDisplayParams = params;
csmartdalton578f0642017-02-24 16:04:47 -0700205 fSampleCount = params.fMSAASampleCount;
206 fStencilBits = 8;
brianosman05de2162016-05-06 13:28:57 -0700207
208 if (VK_FORMAT_UNDEFINED == surfaceFormat) {
209 return false;
210 }
jvanvertha4b0fed2016-04-27 11:42:21 -0700211
Greg Danielfaa095e2017-12-19 13:15:02 -0500212 SkColorType colorType;
213 switch (surfaceFormat) {
214 case VK_FORMAT_R8G8B8A8_UNORM: // fall through
215 case VK_FORMAT_R8G8B8A8_SRGB:
216 colorType = kRGBA_8888_SkColorType;
217 break;
218 case VK_FORMAT_B8G8R8A8_UNORM: // fall through
219 case VK_FORMAT_B8G8R8A8_SRGB:
220 colorType = kBGRA_8888_SkColorType;
221 break;
222 default:
223 return false;
224 }
225
jvanverth3d6ed3a2016-04-07 11:09:51 -0700226 // If mailbox mode is available, use it, as it is the lowest-latency non-
227 // tearing mode. If not, fall back to FIFO which is always available.
jvanverth9f372462016-04-06 06:08:59 -0700228 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700229 for (uint32_t i = 0; i < presentModeCount; ++i) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700230 // use mailbox
231 if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) {
jvanverth9f372462016-04-06 06:08:59 -0700232 mode = presentModes[i];
jvanverth3d6ed3a2016-04-07 11:09:51 -0700233 break;
jvanverth9f372462016-04-06 06:08:59 -0700234 }
235 }
236
237 VkSwapchainCreateInfoKHR swapchainCreateInfo;
238 memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR));
239 swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
240 swapchainCreateInfo.surface = fSurface;
241 swapchainCreateInfo.minImageCount = imageCount;
jvanvertha4b0fed2016-04-27 11:42:21 -0700242 swapchainCreateInfo.imageFormat = surfaceFormat;
243 swapchainCreateInfo.imageColorSpace = colorSpace;
jvanverth9f372462016-04-06 06:08:59 -0700244 swapchainCreateInfo.imageExtent = extent;
245 swapchainCreateInfo.imageArrayLayers = 1;
246 swapchainCreateInfo.imageUsage = usageFlags;
247
Greg Daniel0db3a882018-06-28 16:47:48 +0000248 uint32_t queueFamilies[] = { fGraphicsQueueIndex, fPresentQueueIndex };
249 if (fGraphicsQueueIndex != fPresentQueueIndex) {
jvanverth9f372462016-04-06 06:08:59 -0700250 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
251 swapchainCreateInfo.queueFamilyIndexCount = 2;
252 swapchainCreateInfo.pQueueFamilyIndices = queueFamilies;
253 } else {
254 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
255 swapchainCreateInfo.queueFamilyIndexCount = 0;
256 swapchainCreateInfo.pQueueFamilyIndices = nullptr;
257 }
258
Greg Daniele897b972016-10-06 13:57:57 -0400259 swapchainCreateInfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700260 swapchainCreateInfo.compositeAlpha = composite_alpha;
261 swapchainCreateInfo.presentMode = mode;
262 swapchainCreateInfo.clipped = true;
263 swapchainCreateInfo.oldSwapchain = fSwapchain;
264
Greg Daniel0db3a882018-06-28 16:47:48 +0000265 res = fCreateSwapchainKHR(fDevice, &swapchainCreateInfo, nullptr, &fSwapchain);
jvanverth9f372462016-04-06 06:08:59 -0700266 if (VK_SUCCESS != res) {
267 return false;
268 }
269
270 // destroy the old swapchain
271 if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) {
Greg Daniel0db3a882018-06-28 16:47:48 +0000272 fDeviceWaitIdle(fDevice);
jvanverth9f372462016-04-06 06:08:59 -0700273
274 this->destroyBuffers();
275
Greg Daniel0db3a882018-06-28 16:47:48 +0000276 fDestroySwapchainKHR(fDevice, swapchainCreateInfo.oldSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700277 }
278
Greg Danielfaa095e2017-12-19 13:15:02 -0500279 this->createBuffers(swapchainCreateInfo.imageFormat, colorType);
jvanverth9f372462016-04-06 06:08:59 -0700280
281 return true;
282}
283
Greg Danielfaa095e2017-12-19 13:15:02 -0500284void VulkanWindowContext::createBuffers(VkFormat format, SkColorType colorType) {
Greg Daniel0db3a882018-06-28 16:47:48 +0000285 fGetSwapchainImagesKHR(fDevice, fSwapchain, &fImageCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700286 SkASSERT(fImageCount);
287 fImages = new VkImage[fImageCount];
Greg Daniel0db3a882018-06-28 16:47:48 +0000288 fGetSwapchainImagesKHR(fDevice, fSwapchain, &fImageCount, fImages);
jvanverth9f372462016-04-06 06:08:59 -0700289
290 // set up initial image layouts and create surfaces
291 fImageLayouts = new VkImageLayout[fImageCount];
292 fSurfaces = new sk_sp<SkSurface>[fImageCount];
293 for (uint32_t i = 0; i < fImageCount; ++i) {
294 fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED;
295
egdanielb2df0c22016-05-13 11:30:37 -0700296 GrVkImageInfo info;
jvanverth9f372462016-04-06 06:08:59 -0700297 info.fImage = fImages[i];
Greg Daniel8385a8a2018-02-26 13:29:37 -0500298 info.fAlloc = GrVkAlloc();
egdaniel580fa592016-08-31 11:03:50 -0700299 info.fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
jvanverth9f372462016-04-06 06:08:59 -0700300 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
egdaniel58a8d922016-04-21 08:03:10 -0700301 info.fFormat = format;
jvanverth3622a172016-05-10 06:42:18 -0700302 info.fLevelCount = 1;
jvanverthaf236b52016-05-20 06:01:06 -0700303
Brian Salomonafdc6b12018-03-09 12:02:32 -0500304 GrBackendRenderTarget backendRT(fWidth, fHeight, fSampleCount, info);
Greg Daniele79b4732017-04-20 14:07:46 -0400305
Brian Salomonafdc6b12018-03-09 12:02:32 -0500306 fSurfaces[i] = SkSurface::MakeFromBackendRenderTarget(fContext.get(),
307 backendRT,
308 kTopLeft_GrSurfaceOrigin,
309 colorType,
310 fDisplayParams.fColorSpace,
Ben Wagner37c54032018-04-13 14:30:23 -0400311 &fDisplayParams.fSurfaceProps);
jvanverth9f372462016-04-06 06:08:59 -0700312 }
313
314 // create the command pool for the command buffers
315 if (VK_NULL_HANDLE == fCommandPool) {
316 VkCommandPoolCreateInfo commandPoolInfo;
317 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
318 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
319 // this needs to be on the render queue
Greg Daniel0db3a882018-06-28 16:47:48 +0000320 commandPoolInfo.queueFamilyIndex = fGraphicsQueueIndex;
jvanverth9f372462016-04-06 06:08:59 -0700321 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
Greg Daniel0db3a882018-06-28 16:47:48 +0000322 GR_VK_CALL_ERRCHECK(fInterface,
323 CreateCommandPool(fDevice, &commandPoolInfo,
jvanverth9f372462016-04-06 06:08:59 -0700324 nullptr, &fCommandPool));
325 }
326
327 // set up the backbuffers
328 VkSemaphoreCreateInfo semaphoreInfo;
329 memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo));
330 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
331 semaphoreInfo.pNext = nullptr;
332 semaphoreInfo.flags = 0;
333 VkCommandBufferAllocateInfo commandBuffersInfo;
334 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
335 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
336 commandBuffersInfo.pNext = nullptr;
337 commandBuffersInfo.commandPool = fCommandPool;
338 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
339 commandBuffersInfo.commandBufferCount = 2;
340 VkFenceCreateInfo fenceInfo;
341 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
342 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
343 fenceInfo.pNext = nullptr;
344 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
345
Greg Daniel1f05f442016-10-27 16:37:17 -0400346 // we create one additional backbuffer structure here, because we want to
jvanverth9f372462016-04-06 06:08:59 -0700347 // give the command buffers they contain a chance to finish before we cycle back
348 fBackbuffers = new BackbufferInfo[fImageCount + 1];
349 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
350 fBackbuffers[i].fImageIndex = -1;
Greg Daniel0db3a882018-06-28 16:47:48 +0000351 GR_VK_CALL_ERRCHECK(fInterface,
352 CreateSemaphore(fDevice, &semaphoreInfo,
jvanverth9f372462016-04-06 06:08:59 -0700353 nullptr, &fBackbuffers[i].fAcquireSemaphore));
Greg Daniel0db3a882018-06-28 16:47:48 +0000354 GR_VK_CALL_ERRCHECK(fInterface,
355 CreateSemaphore(fDevice, &semaphoreInfo,
jvanverth9f372462016-04-06 06:08:59 -0700356 nullptr, &fBackbuffers[i].fRenderSemaphore));
Greg Daniel0db3a882018-06-28 16:47:48 +0000357 GR_VK_CALL_ERRCHECK(fInterface,
358 AllocateCommandBuffers(fDevice, &commandBuffersInfo,
jvanverth9f372462016-04-06 06:08:59 -0700359 fBackbuffers[i].fTransitionCmdBuffers));
Greg Daniel0db3a882018-06-28 16:47:48 +0000360 GR_VK_CALL_ERRCHECK(fInterface,
361 CreateFence(fDevice, &fenceInfo, nullptr,
jvanverth9f372462016-04-06 06:08:59 -0700362 &fBackbuffers[i].fUsageFences[0]));
Greg Daniel0db3a882018-06-28 16:47:48 +0000363 GR_VK_CALL_ERRCHECK(fInterface,
364 CreateFence(fDevice, &fenceInfo, nullptr,
jvanverth9f372462016-04-06 06:08:59 -0700365 &fBackbuffers[i].fUsageFences[1]));
366 }
367 fCurrentBackbufferIndex = fImageCount;
368}
369
jvanvertha8d0d6c2016-05-05 12:32:03 -0700370void VulkanWindowContext::destroyBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700371
372 if (fBackbuffers) {
373 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
Greg Daniel0db3a882018-06-28 16:47:48 +0000374 GR_VK_CALL_ERRCHECK(fInterface,
375 WaitForFences(fDevice, 2,
jvanverth9f372462016-04-06 06:08:59 -0700376 fBackbuffers[i].fUsageFences,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700377 true, UINT64_MAX));
jvanverth9f372462016-04-06 06:08:59 -0700378 fBackbuffers[i].fImageIndex = -1;
Greg Daniel0db3a882018-06-28 16:47:48 +0000379 GR_VK_CALL(fInterface,
380 DestroySemaphore(fDevice,
jvanverth9f372462016-04-06 06:08:59 -0700381 fBackbuffers[i].fAcquireSemaphore,
382 nullptr));
Greg Daniel0db3a882018-06-28 16:47:48 +0000383 GR_VK_CALL(fInterface,
384 DestroySemaphore(fDevice,
jvanverth9f372462016-04-06 06:08:59 -0700385 fBackbuffers[i].fRenderSemaphore,
386 nullptr));
Greg Daniel0db3a882018-06-28 16:47:48 +0000387 GR_VK_CALL(fInterface,
388 FreeCommandBuffers(fDevice, fCommandPool, 2,
jvanverth9f372462016-04-06 06:08:59 -0700389 fBackbuffers[i].fTransitionCmdBuffers));
Greg Daniel0db3a882018-06-28 16:47:48 +0000390 GR_VK_CALL(fInterface,
391 DestroyFence(fDevice, fBackbuffers[i].fUsageFences[0], 0));
392 GR_VK_CALL(fInterface,
393 DestroyFence(fDevice, fBackbuffers[i].fUsageFences[1], 0));
jvanverth9f372462016-04-06 06:08:59 -0700394 }
395 }
396
397 delete[] fBackbuffers;
398 fBackbuffers = nullptr;
399
jvanverthaf236b52016-05-20 06:01:06 -0700400 // Does this actually free the surfaces?
jvanverth9f372462016-04-06 06:08:59 -0700401 delete[] fSurfaces;
402 fSurfaces = nullptr;
403 delete[] fImageLayouts;
404 fImageLayouts = nullptr;
405 delete[] fImages;
406 fImages = nullptr;
407}
408
jvanvertha8d0d6c2016-05-05 12:32:03 -0700409VulkanWindowContext::~VulkanWindowContext() {
jvanverth9f372462016-04-06 06:08:59 -0700410 this->destroyContext();
411}
412
jvanvertha8d0d6c2016-05-05 12:32:03 -0700413void VulkanWindowContext::destroyContext() {
Greg Danielbe0ab882018-06-28 17:34:28 +0000414 if (this->isValid()) {
415 fQueueWaitIdle(fPresentQueue);
416 fDeviceWaitIdle(fDevice);
jvanverth9f372462016-04-06 06:08:59 -0700417
Greg Danielbe0ab882018-06-28 17:34:28 +0000418 this->destroyBuffers();
jvanverth9f372462016-04-06 06:08:59 -0700419
Greg Danielbe0ab882018-06-28 17:34:28 +0000420 if (VK_NULL_HANDLE != fCommandPool) {
421 GR_VK_CALL(fInterface, DestroyCommandPool(fDevice, fCommandPool, nullptr));
422 fCommandPool = VK_NULL_HANDLE;
423 }
jvanverth9f372462016-04-06 06:08:59 -0700424
Greg Danielbe0ab882018-06-28 17:34:28 +0000425 if (VK_NULL_HANDLE != fSwapchain) {
426 fDestroySwapchainKHR(fDevice, fSwapchain, nullptr);
427 fSwapchain = VK_NULL_HANDLE;
428 }
jvanverth9f372462016-04-06 06:08:59 -0700429
Greg Danielbe0ab882018-06-28 17:34:28 +0000430 if (VK_NULL_HANDLE != fSurface) {
431 fDestroySurfaceKHR(fInstance, fSurface, nullptr);
432 fSurface = VK_NULL_HANDLE;
433 }
jvanverth9f372462016-04-06 06:08:59 -0700434 }
435
Greg Daniel02611d92017-07-25 10:05:01 -0400436 fContext.reset();
Greg Daniel0db3a882018-06-28 16:47:48 +0000437 fInterface.reset();
jvanverth9f372462016-04-06 06:08:59 -0700438
Greg Daniel0db3a882018-06-28 16:47:48 +0000439 if (VK_NULL_HANDLE != fDevice) {
440 fDestroyDevice(fDevice, nullptr);
441 fDevice = VK_NULL_HANDLE;
442 }
Greg Danielbe0ab882018-06-28 17:34:28 +0000443
444#ifdef SK_ENABLE_VK_LAYERS
445 if (fDebugCallback != VK_NULL_HANDLE) {
446 GR_VK_CALL(fInterface, DestroyDebugReportCallbackEXT(fInstance, fDebugCallback,
447 nullptr));
448 }
449#endif
450
Greg Daniel0db3a882018-06-28 16:47:48 +0000451 fPhysicalDevice = VK_NULL_HANDLE;
452
453 if (VK_NULL_HANDLE != fInstance) {
454 fDestroyInstance(fInstance, nullptr);
455 fInstance = VK_NULL_HANDLE;
456 }
jvanverth9f372462016-04-06 06:08:59 -0700457}
458
jvanvertha8d0d6c2016-05-05 12:32:03 -0700459VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer() {
jvanverth9f372462016-04-06 06:08:59 -0700460 SkASSERT(fBackbuffers);
461
462 ++fCurrentBackbufferIndex;
egdaniel804af7d2016-09-26 12:30:46 -0700463 if (fCurrentBackbufferIndex > fImageCount) {
jvanverth9f372462016-04-06 06:08:59 -0700464 fCurrentBackbufferIndex = 0;
465 }
466
467 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
Greg Daniel0db3a882018-06-28 16:47:48 +0000468 GR_VK_CALL_ERRCHECK(fInterface,
469 WaitForFences(fDevice, 2, backbuffer->fUsageFences,
jvanverth9f372462016-04-06 06:08:59 -0700470 true, UINT64_MAX));
471 return backbuffer;
472}
473
jvanverthaf236b52016-05-20 06:01:06 -0700474sk_sp<SkSurface> VulkanWindowContext::getBackbufferSurface() {
jvanverth9f372462016-04-06 06:08:59 -0700475 BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
476 SkASSERT(backbuffer);
477
478 // reset the fence
Greg Daniel0db3a882018-06-28 16:47:48 +0000479 GR_VK_CALL_ERRCHECK(fInterface,
480 ResetFences(fDevice, 2, backbuffer->fUsageFences));
jvanverth9f372462016-04-06 06:08:59 -0700481 // semaphores should be in unsignaled state
482
483 // acquire the image
Greg Daniel0db3a882018-06-28 16:47:48 +0000484 VkResult res = fAcquireNextImageKHR(fDevice, fSwapchain, UINT64_MAX,
jvanverthb0d43522016-04-21 11:46:23 -0700485 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
486 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700487 if (VK_ERROR_SURFACE_LOST_KHR == res) {
488 // need to figure out how to create a new vkSurface without the platformData*
jvanverth3d6ed3a2016-04-07 11:09:51 -0700489 // maybe use attach somehow? but need a Window
jvanverth9f372462016-04-06 06:08:59 -0700490 return nullptr;
491 }
jvanverth3d6ed3a2016-04-07 11:09:51 -0700492 if (VK_ERROR_OUT_OF_DATE_KHR == res) {
jvanverth9f372462016-04-06 06:08:59 -0700493 // tear swapchain down and try again
Greg Daniel1f05f442016-10-27 16:37:17 -0400494 if (!this->createSwapchain(-1, -1, fDisplayParams)) {
jvanverth9f372462016-04-06 06:08:59 -0700495 return nullptr;
496 }
Jim Van Verthd63c1022017-01-05 13:50:49 -0500497 backbuffer = this->getAvailableBackbuffer();
Greg Daniel0db3a882018-06-28 16:47:48 +0000498 GR_VK_CALL_ERRCHECK(fInterface,
499 ResetFences(fDevice, 2, backbuffer->fUsageFences));
jvanverth9f372462016-04-06 06:08:59 -0700500
501 // acquire the image
Greg Daniel0db3a882018-06-28 16:47:48 +0000502 res = fAcquireNextImageKHR(fDevice, fSwapchain, UINT64_MAX,
jvanverthb0d43522016-04-21 11:46:23 -0700503 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
504 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700505
506 if (VK_SUCCESS != res) {
507 return nullptr;
508 }
509 }
510
511 // set up layout transfer from initial to color attachment
512 VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex];
egdaniel580fa592016-08-31 11:03:50 -0700513 SkASSERT(VK_IMAGE_LAYOUT_UNDEFINED == layout || VK_IMAGE_LAYOUT_PRESENT_SRC_KHR == layout);
jvanverth9f372462016-04-06 06:08:59 -0700514 VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
515 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT :
516 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
517 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700518 VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
jvanverth9f372462016-04-06 06:08:59 -0700519 0 : VK_ACCESS_MEMORY_READ_BIT;
520 VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
521
522 VkImageMemoryBarrier imageMemoryBarrier = {
523 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
524 NULL, // pNext
525 srcAccessMask, // outputMask
526 dstAccessMask, // inputMask
527 layout, // oldLayout
528 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout
529 fPresentQueueIndex, // srcQueueFamilyIndex
Greg Daniel0db3a882018-06-28 16:47:48 +0000530 fGraphicsQueueIndex, // dstQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700531 fImages[backbuffer->fImageIndex], // image
532 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
533 };
Greg Daniel0db3a882018-06-28 16:47:48 +0000534 GR_VK_CALL_ERRCHECK(fInterface,
jvanverth9f372462016-04-06 06:08:59 -0700535 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[0], 0));
536 VkCommandBufferBeginInfo info;
537 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
538 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
539 info.flags = 0;
Greg Daniel0db3a882018-06-28 16:47:48 +0000540 GR_VK_CALL_ERRCHECK(fInterface,
jvanverth9f372462016-04-06 06:08:59 -0700541 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info));
542
Greg Daniel0db3a882018-06-28 16:47:48 +0000543 GR_VK_CALL(fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700544 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0],
545 srcStageMask, dstStageMask, 0,
546 0, nullptr,
547 0, nullptr,
548 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700549
Greg Daniel0db3a882018-06-28 16:47:48 +0000550 GR_VK_CALL_ERRCHECK(fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700551 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0]));
jvanverth9f372462016-04-06 06:08:59 -0700552
egdaniel58a8d922016-04-21 08:03:10 -0700553 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanverth9f372462016-04-06 06:08:59 -0700554 // insert the layout transfer into the queue and wait on the acquire
555 VkSubmitInfo submitInfo;
556 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
557 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
558 submitInfo.waitSemaphoreCount = 1;
559 submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore;
egdaniel58a8d922016-04-21 08:03:10 -0700560 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
jvanverth9f372462016-04-06 06:08:59 -0700561 submitInfo.commandBufferCount = 1;
562 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0];
563 submitInfo.signalSemaphoreCount = 0;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700564
Greg Daniel0db3a882018-06-28 16:47:48 +0000565 GR_VK_CALL_ERRCHECK(fInterface,
566 QueueSubmit(fGraphicsQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700567 backbuffer->fUsageFences[0]));
568
egdaniel580fa592016-08-31 11:03:50 -0700569 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
Robert Phillipsba375a82018-04-11 10:08:06 -0400570 GrBackendRenderTarget backendRT = surface->getBackendRenderTarget(
571 SkSurface::kFlushRead_BackendHandleAccess);
572 backendRT.setVkImageLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
573
egdaniel580fa592016-08-31 11:03:50 -0700574
575 return sk_ref_sp(surface);
jvanverth9f372462016-04-06 06:08:59 -0700576}
577
jvanvertha8d0d6c2016-05-05 12:32:03 -0700578void VulkanWindowContext::swapBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700579
580 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
egdaniel580fa592016-08-31 11:03:50 -0700581 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
jvanverth9f372462016-04-06 06:08:59 -0700582
Robert Phillipsba375a82018-04-11 10:08:06 -0400583 GrBackendRenderTarget backendRT = surface->getBackendRenderTarget(
584 SkSurface::kFlushRead_BackendHandleAccess);
585 GrVkImageInfo imageInfo;
586 SkAssertResult(backendRT.getVkImageInfo(&imageInfo));
587 // Check to make sure we never change the actually wrapped image
588 SkASSERT(imageInfo.fImage == fImages[backbuffer->fImageIndex]);
589
590 VkImageLayout layout = imageInfo.fImageLayout;
Greg Daniel6ddbafc2018-05-24 12:34:29 -0400591 VkPipelineStageFlags srcStageMask = GrVkImage::LayoutToPipelineStageFlags(layout);
jvanverth9f372462016-04-06 06:08:59 -0700592 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
Greg Daniel6ddbafc2018-05-24 12:34:29 -0400593 VkAccessFlags srcAccessMask = GrVkImage::LayoutToSrcAccessMask(layout);
jvanverth9f372462016-04-06 06:08:59 -0700594 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
595
596 VkImageMemoryBarrier imageMemoryBarrier = {
597 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
598 NULL, // pNext
599 srcAccessMask, // outputMask
600 dstAccessMask, // inputMask
601 layout, // oldLayout
602 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout
Greg Daniel0db3a882018-06-28 16:47:48 +0000603 fGraphicsQueueIndex, // srcQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700604 fPresentQueueIndex, // dstQueueFamilyIndex
605 fImages[backbuffer->fImageIndex], // image
606 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
607 };
Greg Daniel0db3a882018-06-28 16:47:48 +0000608 GR_VK_CALL_ERRCHECK(fInterface,
jvanverth9f372462016-04-06 06:08:59 -0700609 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[1], 0));
610 VkCommandBufferBeginInfo info;
611 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
612 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
613 info.flags = 0;
Greg Daniel0db3a882018-06-28 16:47:48 +0000614 GR_VK_CALL_ERRCHECK(fInterface,
jvanverth9f372462016-04-06 06:08:59 -0700615 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info));
Greg Daniel0db3a882018-06-28 16:47:48 +0000616 GR_VK_CALL(fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700617 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
618 srcStageMask, dstStageMask, 0,
619 0, nullptr,
620 0, nullptr,
621 1, &imageMemoryBarrier));
Greg Daniel0db3a882018-06-28 16:47:48 +0000622 GR_VK_CALL_ERRCHECK(fInterface,
jvanverth9f372462016-04-06 06:08:59 -0700623 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1]));
624
625 fImageLayouts[backbuffer->fImageIndex] = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
626
627 // insert the layout transfer into the queue and wait on the acquire
628 VkSubmitInfo submitInfo;
629 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
630 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
631 submitInfo.waitSemaphoreCount = 0;
632 submitInfo.pWaitDstStageMask = 0;
633 submitInfo.commandBufferCount = 1;
634 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[1];
635 submitInfo.signalSemaphoreCount = 1;
636 submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore;
637
Greg Daniel0db3a882018-06-28 16:47:48 +0000638 GR_VK_CALL_ERRCHECK(fInterface,
639 QueueSubmit(fGraphicsQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700640 backbuffer->fUsageFences[1]));
641
642 // Submit present operation to present queue
643 const VkPresentInfoKHR presentInfo =
644 {
645 VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType
646 NULL, // pNext
647 1, // waitSemaphoreCount
648 &backbuffer->fRenderSemaphore, // pWaitSemaphores
649 1, // swapchainCount
650 &fSwapchain, // pSwapchains
651 &backbuffer->fImageIndex, // pImageIndices
652 NULL // pResults
653 };
654
jvanverthb0d43522016-04-21 11:46:23 -0700655 fQueuePresentKHR(fPresentQueue, &presentInfo);
jvanverth9f372462016-04-06 06:08:59 -0700656}
jvanvertha8d0d6c2016-05-05 12:32:03 -0700657
658} //namespace sk_app