blob: ecf6d80b2c9cd521425417eb124b41b5041d42a2 [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
Greg Daniel35970ec2017-11-10 10:03:05 -050025#define GET_PROC(F) f ## F = (PFN_vk ## F) fGetInstanceProcAddr(instance, "vk" #F)
26#define GET_DEV_PROC(F) f ## F = (PFN_vk ## F) fGetDeviceProcAddr(device, "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)?
Greg Daniel35970ec2017-11-10 10:03:05 -050052 fBackendContext.reset(GrVkBackendContext::Create(fGetInstanceProcAddr, fGetDeviceProcAddr,
Jim Van Verthfbdc0802017-05-02 16:15:53 -040053 &fPresentQueueIndex, fCanPresentFn));
jvanverth9f372462016-04-06 06:08:59 -070054
jvanverthb0d43522016-04-21 11:46:23 -070055 if (!(fBackendContext->fExtensions & kKHR_surface_GrVkExtensionFlag) ||
56 !(fBackendContext->fExtensions & kKHR_swapchain_GrVkExtensionFlag)) {
57 fBackendContext.reset(nullptr);
58 return;
59 }
60
61 VkInstance instance = fBackendContext->fInstance;
62 VkDevice device = fBackendContext->fDevice;
63 GET_PROC(DestroySurfaceKHR);
64 GET_PROC(GetPhysicalDeviceSurfaceSupportKHR);
65 GET_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
66 GET_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
67 GET_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
68 GET_DEV_PROC(CreateSwapchainKHR);
69 GET_DEV_PROC(DestroySwapchainKHR);
70 GET_DEV_PROC(GetSwapchainImagesKHR);
71 GET_DEV_PROC(AcquireNextImageKHR);
72 GET_DEV_PROC(QueuePresentKHR);
Greg Daniel35970ec2017-11-10 10:03:05 -050073 GET_DEV_PROC(GetDeviceQueue);
jvanverth9f372462016-04-06 06:08:59 -070074
Brian Salomon384fab42017-12-07 12:33:05 -050075 fContext = GrContext::MakeVulkan(fBackendContext, fDisplayParams.fGrContextOptions);
jvanverth9f372462016-04-06 06:08:59 -070076
Jim Van Verthfbdc0802017-05-02 16:15:53 -040077 fSurface = fCreateVkSurfaceFn(instance);
jvanverth9f372462016-04-06 06:08:59 -070078 if (VK_NULL_HANDLE == fSurface) {
79 fBackendContext.reset(nullptr);
80 return;
81 }
82
jvanverth9f372462016-04-06 06:08:59 -070083 VkBool32 supported;
jvanverthb0d43522016-04-21 11:46:23 -070084 VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fBackendContext->fPhysicalDevice,
85 fPresentQueueIndex, fSurface,
86 &supported);
jvanverth9f372462016-04-06 06:08:59 -070087 if (VK_SUCCESS != res) {
88 this->destroyContext();
89 return;
90 }
91
Jim Van Verthfbdc0802017-05-02 16:15:53 -040092 if (!this->createSwapchain(-1, -1, fDisplayParams)) {
jvanverth9f372462016-04-06 06:08:59 -070093 this->destroyContext();
94 return;
95 }
96
97 // create presentQueue
Greg Daniel35970ec2017-11-10 10:03:05 -050098 fGetDeviceQueue(fBackendContext->fDevice, fPresentQueueIndex, 0, &fPresentQueue);
jvanverth9f372462016-04-06 06:08:59 -070099}
100
bsalomonccde4ab2016-07-27 08:50:12 -0700101bool VulkanWindowContext::createSwapchain(int width, int height,
brianosman05de2162016-05-06 13:28:57 -0700102 const DisplayParams& params) {
jvanverth9f372462016-04-06 06:08:59 -0700103 // check for capabilities
104 VkSurfaceCapabilitiesKHR caps;
jvanverthb0d43522016-04-21 11:46:23 -0700105 VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fBackendContext->fPhysicalDevice,
106 fSurface, &caps);
jvanverth9f372462016-04-06 06:08:59 -0700107 if (VK_SUCCESS != res) {
108 return false;
109 }
110
111 uint32_t surfaceFormatCount;
jvanverthb0d43522016-04-21 11:46:23 -0700112 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
113 &surfaceFormatCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700114 if (VK_SUCCESS != res) {
115 return false;
116 }
117
118 SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatKHR));
119 VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700120 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
121 &surfaceFormatCount, surfaceFormats);
jvanverth9f372462016-04-06 06:08:59 -0700122 if (VK_SUCCESS != res) {
123 return false;
124 }
125
126 uint32_t presentModeCount;
jvanverthb0d43522016-04-21 11:46:23 -0700127 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
128 &presentModeCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700129 if (VK_SUCCESS != res) {
130 return false;
131 }
132
133 SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR));
134 VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700135 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700136 &presentModeCount, presentModes);
jvanverth9f372462016-04-06 06:08:59 -0700137 if (VK_SUCCESS != res) {
138 return false;
139 }
140
141 VkExtent2D extent = caps.currentExtent;
142 // use the hints
143 if (extent.width == (uint32_t)-1) {
144 extent.width = width;
145 extent.height = height;
146 }
147
jvanverth9fab59d2016-04-06 12:08:51 -0700148 // clamp width; to protect us from broken hints
jvanverth9f372462016-04-06 06:08:59 -0700149 if (extent.width < caps.minImageExtent.width) {
150 extent.width = caps.minImageExtent.width;
151 } else if (extent.width > caps.maxImageExtent.width) {
152 extent.width = caps.maxImageExtent.width;
153 }
154 // clamp height
155 if (extent.height < caps.minImageExtent.height) {
156 extent.height = caps.minImageExtent.height;
157 } else if (extent.height > caps.maxImageExtent.height) {
158 extent.height = caps.maxImageExtent.height;
159 }
jvanverth1d155962016-05-23 13:13:36 -0700160
jvanverth9f372462016-04-06 06:08:59 -0700161 fWidth = (int)extent.width;
162 fHeight = (int)extent.height;
163
164 uint32_t imageCount = caps.minImageCount + 2;
165 if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) {
166 // Application must settle for fewer images than desired:
167 imageCount = caps.maxImageCount;
168 }
169
170 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
171 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
172 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
173 SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags);
174 SkASSERT(caps.supportedTransforms & caps.currentTransform);
175 SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
176 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR));
177 VkCompositeAlphaFlagBitsKHR composite_alpha =
178 (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ?
179 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR :
180 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
181
brianosman05de2162016-05-06 13:28:57 -0700182 // Pick our surface format. For now, just make sure it matches our sRGB request:
183 VkFormat surfaceFormat = VK_FORMAT_UNDEFINED;
184 VkColorSpaceKHR colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500185 auto srgbColorSpace = SkColorSpace::MakeSRGB();
brianosmanb109b8c2016-06-16 13:03:24 -0700186 bool wantSRGB = srgbColorSpace == params.fColorSpace;
brianosman05de2162016-05-06 13:28:57 -0700187 for (uint32_t i = 0; i < surfaceFormatCount; ++i) {
Greg Daniel81b80592017-12-13 10:20:04 -0500188 VkFormat localFormat = surfaceFormats[i].format;
189 if (GrVkFormatIsSupported(localFormat) &&
190 GrVkFormatIsSRGB(localFormat, nullptr) == wantSRGB) {
191 surfaceFormat = localFormat;
brianosman05de2162016-05-06 13:28:57 -0700192 colorSpace = surfaceFormats[i].colorSpace;
193 break;
194 }
195 }
196 fDisplayParams = params;
csmartdalton578f0642017-02-24 16:04:47 -0700197 fSampleCount = params.fMSAASampleCount;
198 fStencilBits = 8;
brianosman05de2162016-05-06 13:28:57 -0700199
200 if (VK_FORMAT_UNDEFINED == surfaceFormat) {
201 return false;
202 }
jvanvertha4b0fed2016-04-27 11:42:21 -0700203
Greg Danielfaa095e2017-12-19 13:15:02 -0500204 SkColorType colorType;
205 switch (surfaceFormat) {
206 case VK_FORMAT_R8G8B8A8_UNORM: // fall through
207 case VK_FORMAT_R8G8B8A8_SRGB:
208 colorType = kRGBA_8888_SkColorType;
209 break;
210 case VK_FORMAT_B8G8R8A8_UNORM: // fall through
211 case VK_FORMAT_B8G8R8A8_SRGB:
212 colorType = kBGRA_8888_SkColorType;
213 break;
214 default:
215 return false;
216 }
217
jvanverth3d6ed3a2016-04-07 11:09:51 -0700218 // If mailbox mode is available, use it, as it is the lowest-latency non-
219 // tearing mode. If not, fall back to FIFO which is always available.
jvanverth9f372462016-04-06 06:08:59 -0700220 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700221 for (uint32_t i = 0; i < presentModeCount; ++i) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700222 // use mailbox
223 if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) {
jvanverth9f372462016-04-06 06:08:59 -0700224 mode = presentModes[i];
jvanverth3d6ed3a2016-04-07 11:09:51 -0700225 break;
jvanverth9f372462016-04-06 06:08:59 -0700226 }
227 }
228
229 VkSwapchainCreateInfoKHR swapchainCreateInfo;
230 memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR));
231 swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
232 swapchainCreateInfo.surface = fSurface;
233 swapchainCreateInfo.minImageCount = imageCount;
jvanvertha4b0fed2016-04-27 11:42:21 -0700234 swapchainCreateInfo.imageFormat = surfaceFormat;
235 swapchainCreateInfo.imageColorSpace = colorSpace;
jvanverth9f372462016-04-06 06:08:59 -0700236 swapchainCreateInfo.imageExtent = extent;
237 swapchainCreateInfo.imageArrayLayers = 1;
238 swapchainCreateInfo.imageUsage = usageFlags;
239
jvanverthb0d43522016-04-21 11:46:23 -0700240 uint32_t queueFamilies[] = { fBackendContext->fGraphicsQueueIndex, fPresentQueueIndex };
241 if (fBackendContext->fGraphicsQueueIndex != fPresentQueueIndex) {
jvanverth9f372462016-04-06 06:08:59 -0700242 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
243 swapchainCreateInfo.queueFamilyIndexCount = 2;
244 swapchainCreateInfo.pQueueFamilyIndices = queueFamilies;
245 } else {
246 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
247 swapchainCreateInfo.queueFamilyIndexCount = 0;
248 swapchainCreateInfo.pQueueFamilyIndices = nullptr;
249 }
250
Greg Daniele897b972016-10-06 13:57:57 -0400251 swapchainCreateInfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700252 swapchainCreateInfo.compositeAlpha = composite_alpha;
253 swapchainCreateInfo.presentMode = mode;
254 swapchainCreateInfo.clipped = true;
255 swapchainCreateInfo.oldSwapchain = fSwapchain;
256
jvanverthb0d43522016-04-21 11:46:23 -0700257 res = fCreateSwapchainKHR(fBackendContext->fDevice, &swapchainCreateInfo, nullptr, &fSwapchain);
jvanverth9f372462016-04-06 06:08:59 -0700258 if (VK_SUCCESS != res) {
259 return false;
260 }
261
262 // destroy the old swapchain
263 if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) {
264 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
265
266 this->destroyBuffers();
267
jvanverthb0d43522016-04-21 11:46:23 -0700268 fDestroySwapchainKHR(fBackendContext->fDevice, swapchainCreateInfo.oldSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700269 }
270
Greg Danielfaa095e2017-12-19 13:15:02 -0500271 this->createBuffers(swapchainCreateInfo.imageFormat, colorType);
jvanverth9f372462016-04-06 06:08:59 -0700272
273 return true;
274}
275
Greg Danielfaa095e2017-12-19 13:15:02 -0500276void VulkanWindowContext::createBuffers(VkFormat format, SkColorType colorType) {
jvanverthb0d43522016-04-21 11:46:23 -0700277 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700278 SkASSERT(fImageCount);
279 fImages = new VkImage[fImageCount];
jvanverthb0d43522016-04-21 11:46:23 -0700280 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, fImages);
jvanverth9f372462016-04-06 06:08:59 -0700281
282 // set up initial image layouts and create surfaces
283 fImageLayouts = new VkImageLayout[fImageCount];
284 fSurfaces = new sk_sp<SkSurface>[fImageCount];
285 for (uint32_t i = 0; i < fImageCount; ++i) {
286 fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED;
287
egdanielb2df0c22016-05-13 11:30:37 -0700288 GrVkImageInfo info;
jvanverth9f372462016-04-06 06:08:59 -0700289 info.fImage = fImages[i];
Greg Daniel8385a8a2018-02-26 13:29:37 -0500290 info.fAlloc = GrVkAlloc();
egdaniel580fa592016-08-31 11:03:50 -0700291 info.fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
jvanverth9f372462016-04-06 06:08:59 -0700292 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
egdaniel58a8d922016-04-21 08:03:10 -0700293 info.fFormat = format;
jvanverth3622a172016-05-10 06:42:18 -0700294 info.fLevelCount = 1;
jvanverthaf236b52016-05-20 06:01:06 -0700295
Brian Salomonafdc6b12018-03-09 12:02:32 -0500296 GrBackendRenderTarget backendRT(fWidth, fHeight, fSampleCount, info);
Greg Daniele79b4732017-04-20 14:07:46 -0400297
Brian Salomonafdc6b12018-03-09 12:02:32 -0500298 fSurfaces[i] = SkSurface::MakeFromBackendRenderTarget(fContext.get(),
299 backendRT,
300 kTopLeft_GrSurfaceOrigin,
301 colorType,
302 fDisplayParams.fColorSpace,
303 &fSurfaceProps);
jvanverth9f372462016-04-06 06:08:59 -0700304 }
305
306 // create the command pool for the command buffers
307 if (VK_NULL_HANDLE == fCommandPool) {
308 VkCommandPoolCreateInfo commandPoolInfo;
309 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
310 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
311 // this needs to be on the render queue
jvanverthb0d43522016-04-21 11:46:23 -0700312 commandPoolInfo.queueFamilyIndex = fBackendContext->fGraphicsQueueIndex;
jvanverth9f372462016-04-06 06:08:59 -0700313 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
314 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
315 CreateCommandPool(fBackendContext->fDevice, &commandPoolInfo,
316 nullptr, &fCommandPool));
317 }
318
319 // set up the backbuffers
320 VkSemaphoreCreateInfo semaphoreInfo;
321 memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo));
322 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
323 semaphoreInfo.pNext = nullptr;
324 semaphoreInfo.flags = 0;
325 VkCommandBufferAllocateInfo commandBuffersInfo;
326 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
327 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
328 commandBuffersInfo.pNext = nullptr;
329 commandBuffersInfo.commandPool = fCommandPool;
330 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
331 commandBuffersInfo.commandBufferCount = 2;
332 VkFenceCreateInfo fenceInfo;
333 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
334 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
335 fenceInfo.pNext = nullptr;
336 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
337
Greg Daniel1f05f442016-10-27 16:37:17 -0400338 // we create one additional backbuffer structure here, because we want to
jvanverth9f372462016-04-06 06:08:59 -0700339 // give the command buffers they contain a chance to finish before we cycle back
340 fBackbuffers = new BackbufferInfo[fImageCount + 1];
341 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
342 fBackbuffers[i].fImageIndex = -1;
343 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
344 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
345 nullptr, &fBackbuffers[i].fAcquireSemaphore));
346 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
347 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
348 nullptr, &fBackbuffers[i].fRenderSemaphore));
349 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
350 AllocateCommandBuffers(fBackendContext->fDevice, &commandBuffersInfo,
351 fBackbuffers[i].fTransitionCmdBuffers));
352 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
353 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
354 &fBackbuffers[i].fUsageFences[0]));
355 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
356 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
357 &fBackbuffers[i].fUsageFences[1]));
358 }
359 fCurrentBackbufferIndex = fImageCount;
360}
361
jvanvertha8d0d6c2016-05-05 12:32:03 -0700362void VulkanWindowContext::destroyBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700363
364 if (fBackbuffers) {
365 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
366 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700367 WaitForFences(fBackendContext->fDevice, 2,
jvanverth9f372462016-04-06 06:08:59 -0700368 fBackbuffers[i].fUsageFences,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700369 true, UINT64_MAX));
jvanverth9f372462016-04-06 06:08:59 -0700370 fBackbuffers[i].fImageIndex = -1;
371 GR_VK_CALL(fBackendContext->fInterface,
372 DestroySemaphore(fBackendContext->fDevice,
373 fBackbuffers[i].fAcquireSemaphore,
374 nullptr));
375 GR_VK_CALL(fBackendContext->fInterface,
376 DestroySemaphore(fBackendContext->fDevice,
377 fBackbuffers[i].fRenderSemaphore,
378 nullptr));
379 GR_VK_CALL(fBackendContext->fInterface,
380 FreeCommandBuffers(fBackendContext->fDevice, fCommandPool, 2,
381 fBackbuffers[i].fTransitionCmdBuffers));
382 GR_VK_CALL(fBackendContext->fInterface,
383 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[0], 0));
384 GR_VK_CALL(fBackendContext->fInterface,
385 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[1], 0));
386 }
387 }
388
389 delete[] fBackbuffers;
390 fBackbuffers = nullptr;
391
jvanverthaf236b52016-05-20 06:01:06 -0700392 // Does this actually free the surfaces?
jvanverth9f372462016-04-06 06:08:59 -0700393 delete[] fSurfaces;
394 fSurfaces = nullptr;
395 delete[] fImageLayouts;
396 fImageLayouts = nullptr;
397 delete[] fImages;
398 fImages = nullptr;
399}
400
jvanvertha8d0d6c2016-05-05 12:32:03 -0700401VulkanWindowContext::~VulkanWindowContext() {
jvanverth9f372462016-04-06 06:08:59 -0700402 this->destroyContext();
403}
404
jvanvertha8d0d6c2016-05-05 12:32:03 -0700405void VulkanWindowContext::destroyContext() {
jvanverth9f372462016-04-06 06:08:59 -0700406 if (!fBackendContext.get()) {
407 return;
408 }
409
jvanverth85f758c2016-05-27 06:47:08 -0700410 GR_VK_CALL(fBackendContext->fInterface, QueueWaitIdle(fPresentQueue));
jvanverth9f372462016-04-06 06:08:59 -0700411 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
412
413 this->destroyBuffers();
414
415 if (VK_NULL_HANDLE != fCommandPool) {
416 GR_VK_CALL(fBackendContext->fInterface, DestroyCommandPool(fBackendContext->fDevice,
417 fCommandPool, nullptr));
418 fCommandPool = VK_NULL_HANDLE;
419 }
420
421 if (VK_NULL_HANDLE != fSwapchain) {
jvanverthb0d43522016-04-21 11:46:23 -0700422 fDestroySwapchainKHR(fBackendContext->fDevice, fSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700423 fSwapchain = VK_NULL_HANDLE;
424 }
425
426 if (VK_NULL_HANDLE != fSurface) {
jvanverthb0d43522016-04-21 11:46:23 -0700427 fDestroySurfaceKHR(fBackendContext->fInstance, fSurface, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700428 fSurface = VK_NULL_HANDLE;
429 }
430
Greg Daniel02611d92017-07-25 10:05:01 -0400431 fContext.reset();
jvanverth9f372462016-04-06 06:08:59 -0700432
433 fBackendContext.reset(nullptr);
434}
435
jvanvertha8d0d6c2016-05-05 12:32:03 -0700436VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer() {
jvanverth9f372462016-04-06 06:08:59 -0700437 SkASSERT(fBackbuffers);
438
439 ++fCurrentBackbufferIndex;
egdaniel804af7d2016-09-26 12:30:46 -0700440 if (fCurrentBackbufferIndex > fImageCount) {
jvanverth9f372462016-04-06 06:08:59 -0700441 fCurrentBackbufferIndex = 0;
442 }
443
444 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
jvanverth9f372462016-04-06 06:08:59 -0700445 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
446 WaitForFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences,
447 true, UINT64_MAX));
448 return backbuffer;
449}
450
jvanverthaf236b52016-05-20 06:01:06 -0700451sk_sp<SkSurface> VulkanWindowContext::getBackbufferSurface() {
jvanverth9f372462016-04-06 06:08:59 -0700452 BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
453 SkASSERT(backbuffer);
454
455 // reset the fence
456 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
457 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
458 // semaphores should be in unsignaled state
459
460 // acquire the image
jvanverthb0d43522016-04-21 11:46:23 -0700461 VkResult res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
462 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
463 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700464 if (VK_ERROR_SURFACE_LOST_KHR == res) {
465 // need to figure out how to create a new vkSurface without the platformData*
jvanverth3d6ed3a2016-04-07 11:09:51 -0700466 // maybe use attach somehow? but need a Window
jvanverth9f372462016-04-06 06:08:59 -0700467 return nullptr;
468 }
jvanverth3d6ed3a2016-04-07 11:09:51 -0700469 if (VK_ERROR_OUT_OF_DATE_KHR == res) {
jvanverth9f372462016-04-06 06:08:59 -0700470 // tear swapchain down and try again
Greg Daniel1f05f442016-10-27 16:37:17 -0400471 if (!this->createSwapchain(-1, -1, fDisplayParams)) {
jvanverth9f372462016-04-06 06:08:59 -0700472 return nullptr;
473 }
Jim Van Verthd63c1022017-01-05 13:50:49 -0500474 backbuffer = this->getAvailableBackbuffer();
475 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
476 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
jvanverth9f372462016-04-06 06:08:59 -0700477
478 // acquire the image
jvanvertha8d0d6c2016-05-05 12:32:03 -0700479 res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
jvanverthb0d43522016-04-21 11:46:23 -0700480 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
481 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700482
483 if (VK_SUCCESS != res) {
484 return nullptr;
485 }
486 }
487
488 // set up layout transfer from initial to color attachment
489 VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex];
egdaniel580fa592016-08-31 11:03:50 -0700490 SkASSERT(VK_IMAGE_LAYOUT_UNDEFINED == layout || VK_IMAGE_LAYOUT_PRESENT_SRC_KHR == layout);
jvanverth9f372462016-04-06 06:08:59 -0700491 VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
492 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT :
493 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
494 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700495 VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
jvanverth9f372462016-04-06 06:08:59 -0700496 0 : VK_ACCESS_MEMORY_READ_BIT;
497 VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
498
499 VkImageMemoryBarrier imageMemoryBarrier = {
500 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
501 NULL, // pNext
502 srcAccessMask, // outputMask
503 dstAccessMask, // inputMask
504 layout, // oldLayout
505 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout
506 fPresentQueueIndex, // srcQueueFamilyIndex
jvanverthb0d43522016-04-21 11:46:23 -0700507 fBackendContext->fGraphicsQueueIndex, // dstQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700508 fImages[backbuffer->fImageIndex], // image
509 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
510 };
511 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
512 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[0], 0));
513 VkCommandBufferBeginInfo info;
514 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
515 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
516 info.flags = 0;
517 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
518 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info));
519
jvanvertha8d0d6c2016-05-05 12:32:03 -0700520 GR_VK_CALL(fBackendContext->fInterface,
521 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0],
522 srcStageMask, dstStageMask, 0,
523 0, nullptr,
524 0, nullptr,
525 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700526
527 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700528 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0]));
jvanverth9f372462016-04-06 06:08:59 -0700529
egdaniel58a8d922016-04-21 08:03:10 -0700530 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanverth9f372462016-04-06 06:08:59 -0700531 // insert the layout transfer into the queue and wait on the acquire
532 VkSubmitInfo submitInfo;
533 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
534 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
535 submitInfo.waitSemaphoreCount = 1;
536 submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore;
egdaniel58a8d922016-04-21 08:03:10 -0700537 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
jvanverth9f372462016-04-06 06:08:59 -0700538 submitInfo.commandBufferCount = 1;
539 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0];
540 submitInfo.signalSemaphoreCount = 0;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700541
jvanverth9f372462016-04-06 06:08:59 -0700542 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700543 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700544 backbuffer->fUsageFences[0]));
545
egdaniel580fa592016-08-31 11:03:50 -0700546 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
Robert Phillipsba375a82018-04-11 10:08:06 -0400547 GrBackendRenderTarget backendRT = surface->getBackendRenderTarget(
548 SkSurface::kFlushRead_BackendHandleAccess);
549 backendRT.setVkImageLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
550
egdaniel580fa592016-08-31 11:03:50 -0700551
552 return sk_ref_sp(surface);
jvanverth9f372462016-04-06 06:08:59 -0700553}
554
jvanvertha8d0d6c2016-05-05 12:32:03 -0700555void VulkanWindowContext::swapBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700556
557 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
egdaniel580fa592016-08-31 11:03:50 -0700558 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
jvanverth9f372462016-04-06 06:08:59 -0700559
Robert Phillipsba375a82018-04-11 10:08:06 -0400560 GrBackendRenderTarget backendRT = surface->getBackendRenderTarget(
561 SkSurface::kFlushRead_BackendHandleAccess);
562 GrVkImageInfo imageInfo;
563 SkAssertResult(backendRT.getVkImageInfo(&imageInfo));
564 // Check to make sure we never change the actually wrapped image
565 SkASSERT(imageInfo.fImage == fImages[backbuffer->fImageIndex]);
566
567 VkImageLayout layout = imageInfo.fImageLayout;
egdaniel580fa592016-08-31 11:03:50 -0700568 VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(layout);
jvanverth9f372462016-04-06 06:08:59 -0700569 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
egdaniel580fa592016-08-31 11:03:50 -0700570 VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(layout);
jvanverth9f372462016-04-06 06:08:59 -0700571 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
572
573 VkImageMemoryBarrier imageMemoryBarrier = {
574 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
575 NULL, // pNext
576 srcAccessMask, // outputMask
577 dstAccessMask, // inputMask
578 layout, // oldLayout
579 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout
jvanverthb0d43522016-04-21 11:46:23 -0700580 fBackendContext->fGraphicsQueueIndex, // srcQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700581 fPresentQueueIndex, // dstQueueFamilyIndex
582 fImages[backbuffer->fImageIndex], // image
583 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
584 };
585 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
586 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[1], 0));
587 VkCommandBufferBeginInfo info;
588 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
589 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
590 info.flags = 0;
591 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
592 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info));
593 GR_VK_CALL(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700594 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
595 srcStageMask, dstStageMask, 0,
596 0, nullptr,
597 0, nullptr,
598 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700599 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
600 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1]));
601
602 fImageLayouts[backbuffer->fImageIndex] = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
603
604 // insert the layout transfer into the queue and wait on the acquire
605 VkSubmitInfo submitInfo;
606 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
607 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
608 submitInfo.waitSemaphoreCount = 0;
609 submitInfo.pWaitDstStageMask = 0;
610 submitInfo.commandBufferCount = 1;
611 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[1];
612 submitInfo.signalSemaphoreCount = 1;
613 submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore;
614
615 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700616 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700617 backbuffer->fUsageFences[1]));
618
619 // Submit present operation to present queue
620 const VkPresentInfoKHR presentInfo =
621 {
622 VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType
623 NULL, // pNext
624 1, // waitSemaphoreCount
625 &backbuffer->fRenderSemaphore, // pWaitSemaphores
626 1, // swapchainCount
627 &fSwapchain, // pSwapchains
628 &backbuffer->fImageIndex, // pImageIndices
629 NULL // pResults
630 };
631
jvanverthb0d43522016-04-21 11:46:23 -0700632 fQueuePresentKHR(fPresentQueue, &presentInfo);
jvanverth9f372462016-04-06 06:08:59 -0700633}
jvanvertha8d0d6c2016-05-05 12:32:03 -0700634
635} //namespace sk_app