blob: bc9f8d59983d863a569856e2936d7abdedfc7db5 [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 Daniel6ddbafc2018-05-24 12:34:29 -040015#include "vk/GrVkImage.h"
jvanverth9f372462016-04-06 06:08:59 -070016#include "vk/GrVkUtil.h"
17#include "vk/GrVkTypes.h"
18
19#ifdef VK_USE_PLATFORM_WIN32_KHR
20// windows wants to define this as CreateSemaphoreA or CreateSemaphoreW
21#undef CreateSemaphore
22#endif
23
Greg Danielf730c182018-07-02 20:15:37 +000024#define GET_PROC(F) f ## F = (PFN_vk ## F) fGetInstanceProcAddr(fInstance, "vk" #F)
25#define GET_DEV_PROC(F) f ## F = (PFN_vk ## F) fGetDeviceProcAddr(fDevice, "vk" #F)
jvanverthb0d43522016-04-21 11:46:23 -070026
jvanvertha8d0d6c2016-05-05 12:32:03 -070027namespace sk_app {
28
bsalomond1bdd1f2016-07-26 12:02:50 -070029VulkanWindowContext::VulkanWindowContext(const DisplayParams& params,
30 CreateVkSurfaceFn createVkSurface,
Greg Daniel35970ec2017-11-10 10:03:05 -050031 CanPresentFn canPresent,
32 PFN_vkGetInstanceProcAddr instProc,
33 PFN_vkGetDeviceProcAddr devProc)
Jim Van Verthfbdc0802017-05-02 16:15:53 -040034 : WindowContext(params)
35 , fCreateVkSurfaceFn(createVkSurface)
36 , fCanPresentFn(canPresent)
jvanverthaf236b52016-05-20 06:01:06 -070037 , fSurface(VK_NULL_HANDLE)
jvanvertha8d0d6c2016-05-05 12:32:03 -070038 , fSwapchain(VK_NULL_HANDLE)
jvanverthaf236b52016-05-20 06:01:06 -070039 , fImages(nullptr)
40 , fImageLayouts(nullptr)
41 , fSurfaces(nullptr)
jvanvertha8d0d6c2016-05-05 12:32:03 -070042 , fCommandPool(VK_NULL_HANDLE)
43 , fBackbuffers(nullptr) {
Greg Daniel35970ec2017-11-10 10:03:05 -050044 fGetInstanceProcAddr = instProc;
45 fGetDeviceProcAddr = devProc;
Jim Van Verthfbdc0802017-05-02 16:15:53 -040046 this->initializeContext();
47}
jvanverth9f372462016-04-06 06:08:59 -070048
Jim Van Verthfbdc0802017-05-02 16:15:53 -040049void VulkanWindowContext::initializeContext() {
jvanverth9f372462016-04-06 06:08:59 -070050 // any config code here (particularly for msaa)?
jvanverth9f372462016-04-06 06:08:59 -070051
Greg Danielf730c182018-07-02 20:15:37 +000052 GrVkBackendContext backendContext;
53 if (!sk_gpu_test::CreateVkBackendContext(fGetInstanceProcAddr, fGetDeviceProcAddr,
54 &backendContext, &fPresentQueueIndex, fCanPresentFn)) {
jvanverthb0d43522016-04-21 11:46:23 -070055 return;
56 }
57
Greg Danielf730c182018-07-02 20:15:37 +000058 if (!(backendContext.fExtensions & kKHR_surface_GrVkExtensionFlag) ||
59 !(backendContext.fExtensions & kKHR_swapchain_GrVkExtensionFlag)) {
60 return;
61 }
62
63 fInstance = backendContext.fInstance;
64 fPhysicalDevice = backendContext.fPhysicalDevice;
65 fDevice = backendContext.fDevice;
66 fGraphicsQueueIndex = backendContext.fGraphicsQueueIndex;
67 fGraphicsQueue = backendContext.fQueue;
68 fInterface = backendContext.fInterface;
69
70 GET_PROC(DestroyInstance);
jvanverthb0d43522016-04-21 11:46:23 -070071 GET_PROC(DestroySurfaceKHR);
72 GET_PROC(GetPhysicalDeviceSurfaceSupportKHR);
73 GET_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
74 GET_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
75 GET_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
Greg Danielf730c182018-07-02 20:15:37 +000076 GET_DEV_PROC(DeviceWaitIdle);
77 GET_DEV_PROC(QueueWaitIdle);
78 GET_DEV_PROC(DestroyDevice);
jvanverthb0d43522016-04-21 11:46:23 -070079 GET_DEV_PROC(CreateSwapchainKHR);
80 GET_DEV_PROC(DestroySwapchainKHR);
81 GET_DEV_PROC(GetSwapchainImagesKHR);
82 GET_DEV_PROC(AcquireNextImageKHR);
83 GET_DEV_PROC(QueuePresentKHR);
Greg Daniel35970ec2017-11-10 10:03:05 -050084 GET_DEV_PROC(GetDeviceQueue);
jvanverth9f372462016-04-06 06:08:59 -070085
Greg Danielf730c182018-07-02 20:15:37 +000086 fContext = GrContext::MakeVulkan(backendContext, fDisplayParams.fGrContextOptions);
jvanverth9f372462016-04-06 06:08:59 -070087
Greg Danielf730c182018-07-02 20:15:37 +000088 fSurface = fCreateVkSurfaceFn(fInstance);
jvanverth9f372462016-04-06 06:08:59 -070089 if (VK_NULL_HANDLE == fSurface) {
Greg Danielf730c182018-07-02 20:15:37 +000090 this->destroyContext();
jvanverth9f372462016-04-06 06:08:59 -070091 return;
92 }
93
jvanverth9f372462016-04-06 06:08:59 -070094 VkBool32 supported;
Greg Danielf730c182018-07-02 20:15:37 +000095 VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fPhysicalDevice, fPresentQueueIndex,
96 fSurface, &supported);
jvanverth9f372462016-04-06 06:08:59 -070097 if (VK_SUCCESS != res) {
98 this->destroyContext();
99 return;
100 }
101
Jim Van Verthfbdc0802017-05-02 16:15:53 -0400102 if (!this->createSwapchain(-1, -1, fDisplayParams)) {
jvanverth9f372462016-04-06 06:08:59 -0700103 this->destroyContext();
104 return;
105 }
106
107 // create presentQueue
Greg Danielf730c182018-07-02 20:15:37 +0000108 fGetDeviceQueue(fDevice, fPresentQueueIndex, 0, &fPresentQueue);
jvanverth9f372462016-04-06 06:08:59 -0700109}
110
bsalomonccde4ab2016-07-27 08:50:12 -0700111bool VulkanWindowContext::createSwapchain(int width, int height,
brianosman05de2162016-05-06 13:28:57 -0700112 const DisplayParams& params) {
jvanverth9f372462016-04-06 06:08:59 -0700113 // check for capabilities
114 VkSurfaceCapabilitiesKHR caps;
Greg Danielf730c182018-07-02 20:15:37 +0000115 VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fPhysicalDevice, fSurface, &caps);
jvanverth9f372462016-04-06 06:08:59 -0700116 if (VK_SUCCESS != res) {
117 return false;
118 }
119
120 uint32_t surfaceFormatCount;
Greg Danielf730c182018-07-02 20:15:37 +0000121 res = fGetPhysicalDeviceSurfaceFormatsKHR(fPhysicalDevice, fSurface, &surfaceFormatCount,
122 nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700123 if (VK_SUCCESS != res) {
124 return false;
125 }
126
127 SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatKHR));
128 VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc.get();
Greg Danielf730c182018-07-02 20:15:37 +0000129 res = fGetPhysicalDeviceSurfaceFormatsKHR(fPhysicalDevice, fSurface, &surfaceFormatCount,
130 surfaceFormats);
jvanverth9f372462016-04-06 06:08:59 -0700131 if (VK_SUCCESS != res) {
132 return false;
133 }
134
135 uint32_t presentModeCount;
Greg Danielf730c182018-07-02 20:15:37 +0000136 res = fGetPhysicalDeviceSurfacePresentModesKHR(fPhysicalDevice, fSurface, &presentModeCount,
137 nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700138 if (VK_SUCCESS != res) {
139 return false;
140 }
141
142 SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR));
143 VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get();
Greg Danielf730c182018-07-02 20:15:37 +0000144 res = fGetPhysicalDeviceSurfacePresentModesKHR(fPhysicalDevice, fSurface, &presentModeCount,
145 presentModes);
jvanverth9f372462016-04-06 06:08:59 -0700146 if (VK_SUCCESS != res) {
147 return false;
148 }
149
150 VkExtent2D extent = caps.currentExtent;
151 // use the hints
152 if (extent.width == (uint32_t)-1) {
153 extent.width = width;
154 extent.height = height;
155 }
156
jvanverth9fab59d2016-04-06 12:08:51 -0700157 // clamp width; to protect us from broken hints
jvanverth9f372462016-04-06 06:08:59 -0700158 if (extent.width < caps.minImageExtent.width) {
159 extent.width = caps.minImageExtent.width;
160 } else if (extent.width > caps.maxImageExtent.width) {
161 extent.width = caps.maxImageExtent.width;
162 }
163 // clamp height
164 if (extent.height < caps.minImageExtent.height) {
165 extent.height = caps.minImageExtent.height;
166 } else if (extent.height > caps.maxImageExtent.height) {
167 extent.height = caps.maxImageExtent.height;
168 }
jvanverth1d155962016-05-23 13:13:36 -0700169
jvanverth9f372462016-04-06 06:08:59 -0700170 fWidth = (int)extent.width;
171 fHeight = (int)extent.height;
172
173 uint32_t imageCount = caps.minImageCount + 2;
174 if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) {
175 // Application must settle for fewer images than desired:
176 imageCount = caps.maxImageCount;
177 }
178
179 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
180 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
181 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
182 SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags);
183 SkASSERT(caps.supportedTransforms & caps.currentTransform);
184 SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
185 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR));
186 VkCompositeAlphaFlagBitsKHR composite_alpha =
187 (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ?
188 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR :
189 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
190
Brian Osman294fe292018-07-09 10:23:19 -0400191 // Pick our surface format.
brianosman05de2162016-05-06 13:28:57 -0700192 VkFormat surfaceFormat = VK_FORMAT_UNDEFINED;
193 VkColorSpaceKHR colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
brianosman05de2162016-05-06 13:28:57 -0700194 for (uint32_t i = 0; i < surfaceFormatCount; ++i) {
Greg Daniel81b80592017-12-13 10:20:04 -0500195 VkFormat localFormat = surfaceFormats[i].format;
Brian Osman2b23c4b2018-06-01 12:25:08 -0400196 if (GrVkFormatIsSupported(localFormat)) {
Greg Daniel81b80592017-12-13 10:20:04 -0500197 surfaceFormat = localFormat;
brianosman05de2162016-05-06 13:28:57 -0700198 colorSpace = surfaceFormats[i].colorSpace;
199 break;
200 }
201 }
202 fDisplayParams = params;
csmartdalton578f0642017-02-24 16:04:47 -0700203 fSampleCount = params.fMSAASampleCount;
204 fStencilBits = 8;
brianosman05de2162016-05-06 13:28:57 -0700205
206 if (VK_FORMAT_UNDEFINED == surfaceFormat) {
207 return false;
208 }
jvanvertha4b0fed2016-04-27 11:42:21 -0700209
Greg Danielfaa095e2017-12-19 13:15:02 -0500210 SkColorType colorType;
211 switch (surfaceFormat) {
212 case VK_FORMAT_R8G8B8A8_UNORM: // fall through
213 case VK_FORMAT_R8G8B8A8_SRGB:
214 colorType = kRGBA_8888_SkColorType;
215 break;
216 case VK_FORMAT_B8G8R8A8_UNORM: // fall through
217 case VK_FORMAT_B8G8R8A8_SRGB:
218 colorType = kBGRA_8888_SkColorType;
219 break;
220 default:
221 return false;
222 }
223
jvanverth3d6ed3a2016-04-07 11:09:51 -0700224 // If mailbox mode is available, use it, as it is the lowest-latency non-
225 // tearing mode. If not, fall back to FIFO which is always available.
jvanverth9f372462016-04-06 06:08:59 -0700226 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700227 for (uint32_t i = 0; i < presentModeCount; ++i) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700228 // use mailbox
229 if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) {
jvanverth9f372462016-04-06 06:08:59 -0700230 mode = presentModes[i];
jvanverth3d6ed3a2016-04-07 11:09:51 -0700231 break;
jvanverth9f372462016-04-06 06:08:59 -0700232 }
233 }
234
235 VkSwapchainCreateInfoKHR swapchainCreateInfo;
236 memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR));
237 swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
238 swapchainCreateInfo.surface = fSurface;
239 swapchainCreateInfo.minImageCount = imageCount;
jvanvertha4b0fed2016-04-27 11:42:21 -0700240 swapchainCreateInfo.imageFormat = surfaceFormat;
241 swapchainCreateInfo.imageColorSpace = colorSpace;
jvanverth9f372462016-04-06 06:08:59 -0700242 swapchainCreateInfo.imageExtent = extent;
243 swapchainCreateInfo.imageArrayLayers = 1;
244 swapchainCreateInfo.imageUsage = usageFlags;
245
Greg Danielf730c182018-07-02 20:15:37 +0000246 uint32_t queueFamilies[] = { fGraphicsQueueIndex, fPresentQueueIndex };
247 if (fGraphicsQueueIndex != fPresentQueueIndex) {
jvanverth9f372462016-04-06 06:08:59 -0700248 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
249 swapchainCreateInfo.queueFamilyIndexCount = 2;
250 swapchainCreateInfo.pQueueFamilyIndices = queueFamilies;
251 } else {
252 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
253 swapchainCreateInfo.queueFamilyIndexCount = 0;
254 swapchainCreateInfo.pQueueFamilyIndices = nullptr;
255 }
256
Greg Daniele897b972016-10-06 13:57:57 -0400257 swapchainCreateInfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700258 swapchainCreateInfo.compositeAlpha = composite_alpha;
259 swapchainCreateInfo.presentMode = mode;
260 swapchainCreateInfo.clipped = true;
261 swapchainCreateInfo.oldSwapchain = fSwapchain;
262
Greg Danielf730c182018-07-02 20:15:37 +0000263 res = fCreateSwapchainKHR(fDevice, &swapchainCreateInfo, nullptr, &fSwapchain);
jvanverth9f372462016-04-06 06:08:59 -0700264 if (VK_SUCCESS != res) {
265 return false;
266 }
267
268 // destroy the old swapchain
269 if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) {
Greg Danielf730c182018-07-02 20:15:37 +0000270 fDeviceWaitIdle(fDevice);
jvanverth9f372462016-04-06 06:08:59 -0700271
272 this->destroyBuffers();
273
Greg Danielf730c182018-07-02 20:15:37 +0000274 fDestroySwapchainKHR(fDevice, swapchainCreateInfo.oldSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700275 }
276
Greg Danielfaa095e2017-12-19 13:15:02 -0500277 this->createBuffers(swapchainCreateInfo.imageFormat, colorType);
jvanverth9f372462016-04-06 06:08:59 -0700278
279 return true;
280}
281
Greg Danielfaa095e2017-12-19 13:15:02 -0500282void VulkanWindowContext::createBuffers(VkFormat format, SkColorType colorType) {
Greg Danielf730c182018-07-02 20:15:37 +0000283 fGetSwapchainImagesKHR(fDevice, fSwapchain, &fImageCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700284 SkASSERT(fImageCount);
285 fImages = new VkImage[fImageCount];
Greg Danielf730c182018-07-02 20:15:37 +0000286 fGetSwapchainImagesKHR(fDevice, fSwapchain, &fImageCount, fImages);
jvanverth9f372462016-04-06 06:08:59 -0700287
288 // set up initial image layouts and create surfaces
289 fImageLayouts = new VkImageLayout[fImageCount];
290 fSurfaces = new sk_sp<SkSurface>[fImageCount];
291 for (uint32_t i = 0; i < fImageCount; ++i) {
292 fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED;
293
egdanielb2df0c22016-05-13 11:30:37 -0700294 GrVkImageInfo info;
jvanverth9f372462016-04-06 06:08:59 -0700295 info.fImage = fImages[i];
Greg Daniel8385a8a2018-02-26 13:29:37 -0500296 info.fAlloc = GrVkAlloc();
egdaniel580fa592016-08-31 11:03:50 -0700297 info.fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
jvanverth9f372462016-04-06 06:08:59 -0700298 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
egdaniel58a8d922016-04-21 08:03:10 -0700299 info.fFormat = format;
jvanverth3622a172016-05-10 06:42:18 -0700300 info.fLevelCount = 1;
jvanverthaf236b52016-05-20 06:01:06 -0700301
Brian Salomonafdc6b12018-03-09 12:02:32 -0500302 GrBackendRenderTarget backendRT(fWidth, fHeight, fSampleCount, info);
Greg Daniele79b4732017-04-20 14:07:46 -0400303
Brian Salomonafdc6b12018-03-09 12:02:32 -0500304 fSurfaces[i] = SkSurface::MakeFromBackendRenderTarget(fContext.get(),
305 backendRT,
306 kTopLeft_GrSurfaceOrigin,
307 colorType,
308 fDisplayParams.fColorSpace,
Ben Wagner37c54032018-04-13 14:30:23 -0400309 &fDisplayParams.fSurfaceProps);
jvanverth9f372462016-04-06 06:08:59 -0700310 }
311
312 // create the command pool for the command buffers
313 if (VK_NULL_HANDLE == fCommandPool) {
314 VkCommandPoolCreateInfo commandPoolInfo;
315 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
316 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
317 // this needs to be on the render queue
Greg Danielf730c182018-07-02 20:15:37 +0000318 commandPoolInfo.queueFamilyIndex = fGraphicsQueueIndex;
jvanverth9f372462016-04-06 06:08:59 -0700319 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
Greg Danielf730c182018-07-02 20:15:37 +0000320 GR_VK_CALL_ERRCHECK(fInterface,
321 CreateCommandPool(fDevice, &commandPoolInfo,
jvanverth9f372462016-04-06 06:08:59 -0700322 nullptr, &fCommandPool));
323 }
324
325 // set up the backbuffers
326 VkSemaphoreCreateInfo semaphoreInfo;
327 memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo));
328 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
329 semaphoreInfo.pNext = nullptr;
330 semaphoreInfo.flags = 0;
331 VkCommandBufferAllocateInfo commandBuffersInfo;
332 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
333 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
334 commandBuffersInfo.pNext = nullptr;
335 commandBuffersInfo.commandPool = fCommandPool;
336 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
337 commandBuffersInfo.commandBufferCount = 2;
338 VkFenceCreateInfo fenceInfo;
339 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
340 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
341 fenceInfo.pNext = nullptr;
342 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
343
Greg Daniel1f05f442016-10-27 16:37:17 -0400344 // we create one additional backbuffer structure here, because we want to
jvanverth9f372462016-04-06 06:08:59 -0700345 // give the command buffers they contain a chance to finish before we cycle back
346 fBackbuffers = new BackbufferInfo[fImageCount + 1];
347 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
348 fBackbuffers[i].fImageIndex = -1;
Greg Danielf730c182018-07-02 20:15:37 +0000349 GR_VK_CALL_ERRCHECK(fInterface,
350 CreateSemaphore(fDevice, &semaphoreInfo,
jvanverth9f372462016-04-06 06:08:59 -0700351 nullptr, &fBackbuffers[i].fAcquireSemaphore));
Greg Danielf730c182018-07-02 20:15:37 +0000352 GR_VK_CALL_ERRCHECK(fInterface,
353 CreateSemaphore(fDevice, &semaphoreInfo,
jvanverth9f372462016-04-06 06:08:59 -0700354 nullptr, &fBackbuffers[i].fRenderSemaphore));
Greg Danielf730c182018-07-02 20:15:37 +0000355 GR_VK_CALL_ERRCHECK(fInterface,
356 AllocateCommandBuffers(fDevice, &commandBuffersInfo,
jvanverth9f372462016-04-06 06:08:59 -0700357 fBackbuffers[i].fTransitionCmdBuffers));
Greg Danielf730c182018-07-02 20:15:37 +0000358 GR_VK_CALL_ERRCHECK(fInterface,
359 CreateFence(fDevice, &fenceInfo, nullptr,
jvanverth9f372462016-04-06 06:08:59 -0700360 &fBackbuffers[i].fUsageFences[0]));
Greg Danielf730c182018-07-02 20:15:37 +0000361 GR_VK_CALL_ERRCHECK(fInterface,
362 CreateFence(fDevice, &fenceInfo, nullptr,
jvanverth9f372462016-04-06 06:08:59 -0700363 &fBackbuffers[i].fUsageFences[1]));
364 }
365 fCurrentBackbufferIndex = fImageCount;
366}
367
jvanvertha8d0d6c2016-05-05 12:32:03 -0700368void VulkanWindowContext::destroyBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700369
370 if (fBackbuffers) {
371 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
Greg Danielf730c182018-07-02 20:15:37 +0000372 GR_VK_CALL_ERRCHECK(fInterface,
373 WaitForFences(fDevice, 2,
jvanverth9f372462016-04-06 06:08:59 -0700374 fBackbuffers[i].fUsageFences,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700375 true, UINT64_MAX));
jvanverth9f372462016-04-06 06:08:59 -0700376 fBackbuffers[i].fImageIndex = -1;
Greg Danielf730c182018-07-02 20:15:37 +0000377 GR_VK_CALL(fInterface,
378 DestroySemaphore(fDevice,
jvanverth9f372462016-04-06 06:08:59 -0700379 fBackbuffers[i].fAcquireSemaphore,
380 nullptr));
Greg Danielf730c182018-07-02 20:15:37 +0000381 GR_VK_CALL(fInterface,
382 DestroySemaphore(fDevice,
jvanverth9f372462016-04-06 06:08:59 -0700383 fBackbuffers[i].fRenderSemaphore,
384 nullptr));
Greg Danielf730c182018-07-02 20:15:37 +0000385 GR_VK_CALL(fInterface,
386 FreeCommandBuffers(fDevice, fCommandPool, 2,
jvanverth9f372462016-04-06 06:08:59 -0700387 fBackbuffers[i].fTransitionCmdBuffers));
Greg Danielf730c182018-07-02 20:15:37 +0000388 GR_VK_CALL(fInterface,
389 DestroyFence(fDevice, fBackbuffers[i].fUsageFences[0], 0));
390 GR_VK_CALL(fInterface,
391 DestroyFence(fDevice, fBackbuffers[i].fUsageFences[1], 0));
jvanverth9f372462016-04-06 06:08:59 -0700392 }
393 }
394
395 delete[] fBackbuffers;
396 fBackbuffers = nullptr;
397
jvanverthaf236b52016-05-20 06:01:06 -0700398 // Does this actually free the surfaces?
jvanverth9f372462016-04-06 06:08:59 -0700399 delete[] fSurfaces;
400 fSurfaces = nullptr;
401 delete[] fImageLayouts;
402 fImageLayouts = nullptr;
403 delete[] fImages;
404 fImages = nullptr;
405}
406
jvanvertha8d0d6c2016-05-05 12:32:03 -0700407VulkanWindowContext::~VulkanWindowContext() {
jvanverth9f372462016-04-06 06:08:59 -0700408 this->destroyContext();
409}
410
jvanvertha8d0d6c2016-05-05 12:32:03 -0700411void VulkanWindowContext::destroyContext() {
Greg Danielf730c182018-07-02 20:15:37 +0000412 if (!this->isValid()) {
Greg Daniel3148f802018-06-28 23:30:23 +0000413 return;
414 }
jvanverth9f372462016-04-06 06:08:59 -0700415
Greg Danielf730c182018-07-02 20:15:37 +0000416 fQueueWaitIdle(fPresentQueue);
417 fDeviceWaitIdle(fDevice);
jvanverth9f372462016-04-06 06:08:59 -0700418
Greg Daniel3148f802018-06-28 23:30:23 +0000419 this->destroyBuffers();
jvanverth9f372462016-04-06 06:08:59 -0700420
Greg Daniel3148f802018-06-28 23:30:23 +0000421 if (VK_NULL_HANDLE != fCommandPool) {
Greg Danielf730c182018-07-02 20:15:37 +0000422 GR_VK_CALL(fInterface, DestroyCommandPool(fDevice, fCommandPool, nullptr));
Greg Daniel3148f802018-06-28 23:30:23 +0000423 fCommandPool = VK_NULL_HANDLE;
424 }
jvanverth9f372462016-04-06 06:08:59 -0700425
Greg Daniel3148f802018-06-28 23:30:23 +0000426 if (VK_NULL_HANDLE != fSwapchain) {
Greg Danielf730c182018-07-02 20:15:37 +0000427 fDestroySwapchainKHR(fDevice, fSwapchain, nullptr);
Greg Daniel3148f802018-06-28 23:30:23 +0000428 fSwapchain = VK_NULL_HANDLE;
429 }
430
431 if (VK_NULL_HANDLE != fSurface) {
Greg Danielf730c182018-07-02 20:15:37 +0000432 fDestroySurfaceKHR(fInstance, fSurface, nullptr);
Greg Daniel3148f802018-06-28 23:30:23 +0000433 fSurface = VK_NULL_HANDLE;
jvanverth9f372462016-04-06 06:08:59 -0700434 }
435
Greg Daniel02611d92017-07-25 10:05:01 -0400436 fContext.reset();
Greg Danielf730c182018-07-02 20:15:37 +0000437 fInterface.reset();
jvanverth9f372462016-04-06 06:08:59 -0700438
Greg Danielf730c182018-07-02 20:15:37 +0000439 if (VK_NULL_HANDLE != fDevice) {
440 fDestroyDevice(fDevice, nullptr);
441 fDevice = VK_NULL_HANDLE;
442 }
443 fPhysicalDevice = VK_NULL_HANDLE;
444
445 if (VK_NULL_HANDLE != fInstance) {
446 fDestroyInstance(fInstance, nullptr);
447 fInstance = VK_NULL_HANDLE;
448 }
jvanverth9f372462016-04-06 06:08:59 -0700449}
450
jvanvertha8d0d6c2016-05-05 12:32:03 -0700451VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer() {
jvanverth9f372462016-04-06 06:08:59 -0700452 SkASSERT(fBackbuffers);
453
454 ++fCurrentBackbufferIndex;
egdaniel804af7d2016-09-26 12:30:46 -0700455 if (fCurrentBackbufferIndex > fImageCount) {
jvanverth9f372462016-04-06 06:08:59 -0700456 fCurrentBackbufferIndex = 0;
457 }
458
459 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
Greg Danielf730c182018-07-02 20:15:37 +0000460 GR_VK_CALL_ERRCHECK(fInterface,
461 WaitForFences(fDevice, 2, backbuffer->fUsageFences,
jvanverth9f372462016-04-06 06:08:59 -0700462 true, UINT64_MAX));
463 return backbuffer;
464}
465
jvanverthaf236b52016-05-20 06:01:06 -0700466sk_sp<SkSurface> VulkanWindowContext::getBackbufferSurface() {
jvanverth9f372462016-04-06 06:08:59 -0700467 BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
468 SkASSERT(backbuffer);
469
470 // reset the fence
Greg Danielf730c182018-07-02 20:15:37 +0000471 GR_VK_CALL_ERRCHECK(fInterface,
472 ResetFences(fDevice, 2, backbuffer->fUsageFences));
jvanverth9f372462016-04-06 06:08:59 -0700473 // semaphores should be in unsignaled state
474
475 // acquire the image
Greg Danielf730c182018-07-02 20:15:37 +0000476 VkResult res = fAcquireNextImageKHR(fDevice, fSwapchain, UINT64_MAX,
jvanverthb0d43522016-04-21 11:46:23 -0700477 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
478 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700479 if (VK_ERROR_SURFACE_LOST_KHR == res) {
480 // need to figure out how to create a new vkSurface without the platformData*
jvanverth3d6ed3a2016-04-07 11:09:51 -0700481 // maybe use attach somehow? but need a Window
jvanverth9f372462016-04-06 06:08:59 -0700482 return nullptr;
483 }
jvanverth3d6ed3a2016-04-07 11:09:51 -0700484 if (VK_ERROR_OUT_OF_DATE_KHR == res) {
jvanverth9f372462016-04-06 06:08:59 -0700485 // tear swapchain down and try again
Greg Daniel1f05f442016-10-27 16:37:17 -0400486 if (!this->createSwapchain(-1, -1, fDisplayParams)) {
jvanverth9f372462016-04-06 06:08:59 -0700487 return nullptr;
488 }
Jim Van Verthd63c1022017-01-05 13:50:49 -0500489 backbuffer = this->getAvailableBackbuffer();
Greg Danielf730c182018-07-02 20:15:37 +0000490 GR_VK_CALL_ERRCHECK(fInterface,
491 ResetFences(fDevice, 2, backbuffer->fUsageFences));
jvanverth9f372462016-04-06 06:08:59 -0700492
493 // acquire the image
Greg Danielf730c182018-07-02 20:15:37 +0000494 res = fAcquireNextImageKHR(fDevice, fSwapchain, UINT64_MAX,
jvanverthb0d43522016-04-21 11:46:23 -0700495 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
496 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700497
498 if (VK_SUCCESS != res) {
499 return nullptr;
500 }
501 }
502
503 // set up layout transfer from initial to color attachment
504 VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex];
egdaniel580fa592016-08-31 11:03:50 -0700505 SkASSERT(VK_IMAGE_LAYOUT_UNDEFINED == layout || VK_IMAGE_LAYOUT_PRESENT_SRC_KHR == layout);
jvanverth9f372462016-04-06 06:08:59 -0700506 VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
507 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT :
508 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
509 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700510 VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
jvanverth9f372462016-04-06 06:08:59 -0700511 0 : VK_ACCESS_MEMORY_READ_BIT;
512 VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
513
514 VkImageMemoryBarrier imageMemoryBarrier = {
515 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
516 NULL, // pNext
517 srcAccessMask, // outputMask
518 dstAccessMask, // inputMask
519 layout, // oldLayout
520 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout
521 fPresentQueueIndex, // srcQueueFamilyIndex
Greg Danielf730c182018-07-02 20:15:37 +0000522 fGraphicsQueueIndex, // dstQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700523 fImages[backbuffer->fImageIndex], // image
524 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
525 };
Greg Danielf730c182018-07-02 20:15:37 +0000526 GR_VK_CALL_ERRCHECK(fInterface,
jvanverth9f372462016-04-06 06:08:59 -0700527 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[0], 0));
528 VkCommandBufferBeginInfo info;
529 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
530 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
531 info.flags = 0;
Greg Danielf730c182018-07-02 20:15:37 +0000532 GR_VK_CALL_ERRCHECK(fInterface,
jvanverth9f372462016-04-06 06:08:59 -0700533 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info));
534
Greg Danielf730c182018-07-02 20:15:37 +0000535 GR_VK_CALL(fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700536 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0],
537 srcStageMask, dstStageMask, 0,
538 0, nullptr,
539 0, nullptr,
540 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700541
Greg Danielf730c182018-07-02 20:15:37 +0000542 GR_VK_CALL_ERRCHECK(fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700543 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0]));
jvanverth9f372462016-04-06 06:08:59 -0700544
egdaniel58a8d922016-04-21 08:03:10 -0700545 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanverth9f372462016-04-06 06:08:59 -0700546 // insert the layout transfer into the queue and wait on the acquire
547 VkSubmitInfo submitInfo;
548 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
549 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
550 submitInfo.waitSemaphoreCount = 1;
551 submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore;
egdaniel58a8d922016-04-21 08:03:10 -0700552 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
jvanverth9f372462016-04-06 06:08:59 -0700553 submitInfo.commandBufferCount = 1;
554 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0];
555 submitInfo.signalSemaphoreCount = 0;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700556
Greg Danielf730c182018-07-02 20:15:37 +0000557 GR_VK_CALL_ERRCHECK(fInterface,
558 QueueSubmit(fGraphicsQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700559 backbuffer->fUsageFences[0]));
560
egdaniel580fa592016-08-31 11:03:50 -0700561 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
Robert Phillipsba375a82018-04-11 10:08:06 -0400562 GrBackendRenderTarget backendRT = surface->getBackendRenderTarget(
563 SkSurface::kFlushRead_BackendHandleAccess);
564 backendRT.setVkImageLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
565
egdaniel580fa592016-08-31 11:03:50 -0700566
567 return sk_ref_sp(surface);
jvanverth9f372462016-04-06 06:08:59 -0700568}
569
jvanvertha8d0d6c2016-05-05 12:32:03 -0700570void VulkanWindowContext::swapBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700571
572 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
egdaniel580fa592016-08-31 11:03:50 -0700573 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
jvanverth9f372462016-04-06 06:08:59 -0700574
Robert Phillipsba375a82018-04-11 10:08:06 -0400575 GrBackendRenderTarget backendRT = surface->getBackendRenderTarget(
576 SkSurface::kFlushRead_BackendHandleAccess);
577 GrVkImageInfo imageInfo;
578 SkAssertResult(backendRT.getVkImageInfo(&imageInfo));
579 // Check to make sure we never change the actually wrapped image
580 SkASSERT(imageInfo.fImage == fImages[backbuffer->fImageIndex]);
581
582 VkImageLayout layout = imageInfo.fImageLayout;
Greg Daniel6ddbafc2018-05-24 12:34:29 -0400583 VkPipelineStageFlags srcStageMask = GrVkImage::LayoutToPipelineStageFlags(layout);
jvanverth9f372462016-04-06 06:08:59 -0700584 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
Greg Daniel6ddbafc2018-05-24 12:34:29 -0400585 VkAccessFlags srcAccessMask = GrVkImage::LayoutToSrcAccessMask(layout);
jvanverth9f372462016-04-06 06:08:59 -0700586 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
587
588 VkImageMemoryBarrier imageMemoryBarrier = {
589 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
590 NULL, // pNext
591 srcAccessMask, // outputMask
592 dstAccessMask, // inputMask
593 layout, // oldLayout
594 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout
Greg Danielf730c182018-07-02 20:15:37 +0000595 fGraphicsQueueIndex, // srcQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700596 fPresentQueueIndex, // dstQueueFamilyIndex
597 fImages[backbuffer->fImageIndex], // image
598 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
599 };
Greg Danielf730c182018-07-02 20:15:37 +0000600 GR_VK_CALL_ERRCHECK(fInterface,
jvanverth9f372462016-04-06 06:08:59 -0700601 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[1], 0));
602 VkCommandBufferBeginInfo info;
603 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
604 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
605 info.flags = 0;
Greg Danielf730c182018-07-02 20:15:37 +0000606 GR_VK_CALL_ERRCHECK(fInterface,
jvanverth9f372462016-04-06 06:08:59 -0700607 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info));
Greg Danielf730c182018-07-02 20:15:37 +0000608 GR_VK_CALL(fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700609 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
610 srcStageMask, dstStageMask, 0,
611 0, nullptr,
612 0, nullptr,
613 1, &imageMemoryBarrier));
Greg Danielf730c182018-07-02 20:15:37 +0000614 GR_VK_CALL_ERRCHECK(fInterface,
jvanverth9f372462016-04-06 06:08:59 -0700615 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1]));
616
617 fImageLayouts[backbuffer->fImageIndex] = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
618
619 // insert the layout transfer into the queue and wait on the acquire
620 VkSubmitInfo submitInfo;
621 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
622 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
623 submitInfo.waitSemaphoreCount = 0;
624 submitInfo.pWaitDstStageMask = 0;
625 submitInfo.commandBufferCount = 1;
626 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[1];
627 submitInfo.signalSemaphoreCount = 1;
628 submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore;
629
Greg Danielf730c182018-07-02 20:15:37 +0000630 GR_VK_CALL_ERRCHECK(fInterface,
631 QueueSubmit(fGraphicsQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700632 backbuffer->fUsageFences[1]));
633
634 // Submit present operation to present queue
635 const VkPresentInfoKHR presentInfo =
636 {
637 VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType
638 NULL, // pNext
639 1, // waitSemaphoreCount
640 &backbuffer->fRenderSemaphore, // pWaitSemaphores
641 1, // swapchainCount
642 &fSwapchain, // pSwapchains
643 &backbuffer->fImageIndex, // pImageIndices
644 NULL // pResults
645 };
646
jvanverthb0d43522016-04-21 11:46:23 -0700647 fQueuePresentKHR(fPresentQueue, &presentInfo);
jvanverth9f372462016-04-06 06:08:59 -0700648}
jvanvertha8d0d6c2016-05-05 12:32:03 -0700649
650} //namespace sk_app