blob: 35177494f76a6d0f53dd423bdd5a6ccae67b4749 [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 Daniel94403452017-04-18 15:52:36 -0400188 GrPixelConfig config = GrVkFormatToPixelConfig(surfaceFormats[i].format);
189 if (kUnknown_GrPixelConfig != config &&
brianosman05de2162016-05-06 13:28:57 -0700190 GrPixelConfigIsSRGB(config) == wantSRGB) {
191 surfaceFormat = surfaceFormats[i].format;
192 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
jvanverth3d6ed3a2016-04-07 11:09:51 -0700204 // If mailbox mode is available, use it, as it is the lowest-latency non-
205 // tearing mode. If not, fall back to FIFO which is always available.
jvanverth9f372462016-04-06 06:08:59 -0700206 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700207 for (uint32_t i = 0; i < presentModeCount; ++i) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700208 // use mailbox
209 if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) {
jvanverth9f372462016-04-06 06:08:59 -0700210 mode = presentModes[i];
jvanverth3d6ed3a2016-04-07 11:09:51 -0700211 break;
jvanverth9f372462016-04-06 06:08:59 -0700212 }
213 }
214
215 VkSwapchainCreateInfoKHR swapchainCreateInfo;
216 memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR));
217 swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
218 swapchainCreateInfo.surface = fSurface;
219 swapchainCreateInfo.minImageCount = imageCount;
jvanvertha4b0fed2016-04-27 11:42:21 -0700220 swapchainCreateInfo.imageFormat = surfaceFormat;
221 swapchainCreateInfo.imageColorSpace = colorSpace;
jvanverth9f372462016-04-06 06:08:59 -0700222 swapchainCreateInfo.imageExtent = extent;
223 swapchainCreateInfo.imageArrayLayers = 1;
224 swapchainCreateInfo.imageUsage = usageFlags;
225
jvanverthb0d43522016-04-21 11:46:23 -0700226 uint32_t queueFamilies[] = { fBackendContext->fGraphicsQueueIndex, fPresentQueueIndex };
227 if (fBackendContext->fGraphicsQueueIndex != fPresentQueueIndex) {
jvanverth9f372462016-04-06 06:08:59 -0700228 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
229 swapchainCreateInfo.queueFamilyIndexCount = 2;
230 swapchainCreateInfo.pQueueFamilyIndices = queueFamilies;
231 } else {
232 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
233 swapchainCreateInfo.queueFamilyIndexCount = 0;
234 swapchainCreateInfo.pQueueFamilyIndices = nullptr;
235 }
236
Greg Daniele897b972016-10-06 13:57:57 -0400237 swapchainCreateInfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700238 swapchainCreateInfo.compositeAlpha = composite_alpha;
239 swapchainCreateInfo.presentMode = mode;
240 swapchainCreateInfo.clipped = true;
241 swapchainCreateInfo.oldSwapchain = fSwapchain;
242
jvanverthb0d43522016-04-21 11:46:23 -0700243 res = fCreateSwapchainKHR(fBackendContext->fDevice, &swapchainCreateInfo, nullptr, &fSwapchain);
jvanverth9f372462016-04-06 06:08:59 -0700244 if (VK_SUCCESS != res) {
245 return false;
246 }
247
248 // destroy the old swapchain
249 if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) {
250 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
251
252 this->destroyBuffers();
253
jvanverthb0d43522016-04-21 11:46:23 -0700254 fDestroySwapchainKHR(fBackendContext->fDevice, swapchainCreateInfo.oldSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700255 }
256
egdaniel58a8d922016-04-21 08:03:10 -0700257 this->createBuffers(swapchainCreateInfo.imageFormat);
jvanverth9f372462016-04-06 06:08:59 -0700258
259 return true;
260}
261
jvanvertha8d0d6c2016-05-05 12:32:03 -0700262void VulkanWindowContext::createBuffers(VkFormat format) {
Greg Daniel94403452017-04-18 15:52:36 -0400263 fPixelConfig = GrVkFormatToPixelConfig(format);
264 SkASSERT(kUnknown_GrPixelConfig != fPixelConfig);
egdaniel58a8d922016-04-21 08:03:10 -0700265
jvanverthb0d43522016-04-21 11:46:23 -0700266 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700267 SkASSERT(fImageCount);
268 fImages = new VkImage[fImageCount];
jvanverthb0d43522016-04-21 11:46:23 -0700269 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, fImages);
jvanverth9f372462016-04-06 06:08:59 -0700270
271 // set up initial image layouts and create surfaces
272 fImageLayouts = new VkImageLayout[fImageCount];
273 fSurfaces = new sk_sp<SkSurface>[fImageCount];
274 for (uint32_t i = 0; i < fImageCount; ++i) {
275 fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED;
276
egdanielb2df0c22016-05-13 11:30:37 -0700277 GrVkImageInfo info;
jvanverth9f372462016-04-06 06:08:59 -0700278 info.fImage = fImages[i];
jvanverth9d54afc2016-09-20 09:20:03 -0700279 info.fAlloc = { VK_NULL_HANDLE, 0, 0, 0 };
egdaniel580fa592016-08-31 11:03:50 -0700280 info.fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
jvanverth9f372462016-04-06 06:08:59 -0700281 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
egdaniel58a8d922016-04-21 08:03:10 -0700282 info.fFormat = format;
jvanverth3622a172016-05-10 06:42:18 -0700283 info.fLevelCount = 1;
jvanverthaf236b52016-05-20 06:01:06 -0700284
Greg Daniel207282e2017-04-26 13:29:21 -0400285 GrBackendTexture backendTex(fWidth, fHeight, info);
Greg Daniele79b4732017-04-20 14:07:46 -0400286
Greg Daniel02611d92017-07-25 10:05:01 -0400287 fSurfaces[i] = SkSurface::MakeFromBackendTextureAsRenderTarget(fContext.get(), backendTex,
Greg Daniele79b4732017-04-20 14:07:46 -0400288 kTopLeft_GrSurfaceOrigin,
289 fSampleCount,
290 fDisplayParams.fColorSpace,
291 &fSurfaceProps);
jvanverth9f372462016-04-06 06:08:59 -0700292 }
293
294 // create the command pool for the command buffers
295 if (VK_NULL_HANDLE == fCommandPool) {
296 VkCommandPoolCreateInfo commandPoolInfo;
297 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
298 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
299 // this needs to be on the render queue
jvanverthb0d43522016-04-21 11:46:23 -0700300 commandPoolInfo.queueFamilyIndex = fBackendContext->fGraphicsQueueIndex;
jvanverth9f372462016-04-06 06:08:59 -0700301 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
302 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
303 CreateCommandPool(fBackendContext->fDevice, &commandPoolInfo,
304 nullptr, &fCommandPool));
305 }
306
307 // set up the backbuffers
308 VkSemaphoreCreateInfo semaphoreInfo;
309 memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo));
310 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
311 semaphoreInfo.pNext = nullptr;
312 semaphoreInfo.flags = 0;
313 VkCommandBufferAllocateInfo commandBuffersInfo;
314 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
315 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
316 commandBuffersInfo.pNext = nullptr;
317 commandBuffersInfo.commandPool = fCommandPool;
318 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
319 commandBuffersInfo.commandBufferCount = 2;
320 VkFenceCreateInfo fenceInfo;
321 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
322 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
323 fenceInfo.pNext = nullptr;
324 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
325
Greg Daniel1f05f442016-10-27 16:37:17 -0400326 // we create one additional backbuffer structure here, because we want to
jvanverth9f372462016-04-06 06:08:59 -0700327 // give the command buffers they contain a chance to finish before we cycle back
328 fBackbuffers = new BackbufferInfo[fImageCount + 1];
329 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
330 fBackbuffers[i].fImageIndex = -1;
331 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
332 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
333 nullptr, &fBackbuffers[i].fAcquireSemaphore));
334 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
335 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
336 nullptr, &fBackbuffers[i].fRenderSemaphore));
337 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
338 AllocateCommandBuffers(fBackendContext->fDevice, &commandBuffersInfo,
339 fBackbuffers[i].fTransitionCmdBuffers));
340 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
341 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
342 &fBackbuffers[i].fUsageFences[0]));
343 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
344 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
345 &fBackbuffers[i].fUsageFences[1]));
346 }
347 fCurrentBackbufferIndex = fImageCount;
348}
349
jvanvertha8d0d6c2016-05-05 12:32:03 -0700350void VulkanWindowContext::destroyBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700351
352 if (fBackbuffers) {
353 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
354 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700355 WaitForFences(fBackendContext->fDevice, 2,
jvanverth9f372462016-04-06 06:08:59 -0700356 fBackbuffers[i].fUsageFences,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700357 true, UINT64_MAX));
jvanverth9f372462016-04-06 06:08:59 -0700358 fBackbuffers[i].fImageIndex = -1;
359 GR_VK_CALL(fBackendContext->fInterface,
360 DestroySemaphore(fBackendContext->fDevice,
361 fBackbuffers[i].fAcquireSemaphore,
362 nullptr));
363 GR_VK_CALL(fBackendContext->fInterface,
364 DestroySemaphore(fBackendContext->fDevice,
365 fBackbuffers[i].fRenderSemaphore,
366 nullptr));
367 GR_VK_CALL(fBackendContext->fInterface,
368 FreeCommandBuffers(fBackendContext->fDevice, fCommandPool, 2,
369 fBackbuffers[i].fTransitionCmdBuffers));
370 GR_VK_CALL(fBackendContext->fInterface,
371 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[0], 0));
372 GR_VK_CALL(fBackendContext->fInterface,
373 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[1], 0));
374 }
375 }
376
377 delete[] fBackbuffers;
378 fBackbuffers = nullptr;
379
jvanverthaf236b52016-05-20 06:01:06 -0700380 // Does this actually free the surfaces?
jvanverth9f372462016-04-06 06:08:59 -0700381 delete[] fSurfaces;
382 fSurfaces = nullptr;
383 delete[] fImageLayouts;
384 fImageLayouts = nullptr;
385 delete[] fImages;
386 fImages = nullptr;
387}
388
jvanvertha8d0d6c2016-05-05 12:32:03 -0700389VulkanWindowContext::~VulkanWindowContext() {
jvanverth9f372462016-04-06 06:08:59 -0700390 this->destroyContext();
391}
392
jvanvertha8d0d6c2016-05-05 12:32:03 -0700393void VulkanWindowContext::destroyContext() {
jvanverth9f372462016-04-06 06:08:59 -0700394 if (!fBackendContext.get()) {
395 return;
396 }
397
jvanverth85f758c2016-05-27 06:47:08 -0700398 GR_VK_CALL(fBackendContext->fInterface, QueueWaitIdle(fPresentQueue));
jvanverth9f372462016-04-06 06:08:59 -0700399 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
400
401 this->destroyBuffers();
402
403 if (VK_NULL_HANDLE != fCommandPool) {
404 GR_VK_CALL(fBackendContext->fInterface, DestroyCommandPool(fBackendContext->fDevice,
405 fCommandPool, nullptr));
406 fCommandPool = VK_NULL_HANDLE;
407 }
408
409 if (VK_NULL_HANDLE != fSwapchain) {
jvanverthb0d43522016-04-21 11:46:23 -0700410 fDestroySwapchainKHR(fBackendContext->fDevice, fSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700411 fSwapchain = VK_NULL_HANDLE;
412 }
413
414 if (VK_NULL_HANDLE != fSurface) {
jvanverthb0d43522016-04-21 11:46:23 -0700415 fDestroySurfaceKHR(fBackendContext->fInstance, fSurface, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700416 fSurface = VK_NULL_HANDLE;
417 }
418
Greg Daniel02611d92017-07-25 10:05:01 -0400419 fContext.reset();
jvanverth9f372462016-04-06 06:08:59 -0700420
421 fBackendContext.reset(nullptr);
422}
423
jvanvertha8d0d6c2016-05-05 12:32:03 -0700424VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer() {
jvanverth9f372462016-04-06 06:08:59 -0700425 SkASSERT(fBackbuffers);
426
427 ++fCurrentBackbufferIndex;
egdaniel804af7d2016-09-26 12:30:46 -0700428 if (fCurrentBackbufferIndex > fImageCount) {
jvanverth9f372462016-04-06 06:08:59 -0700429 fCurrentBackbufferIndex = 0;
430 }
431
432 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
jvanverth9f372462016-04-06 06:08:59 -0700433 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
434 WaitForFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences,
435 true, UINT64_MAX));
436 return backbuffer;
437}
438
jvanverthaf236b52016-05-20 06:01:06 -0700439sk_sp<SkSurface> VulkanWindowContext::getBackbufferSurface() {
jvanverth9f372462016-04-06 06:08:59 -0700440 BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
441 SkASSERT(backbuffer);
442
443 // reset the fence
444 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
445 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
446 // semaphores should be in unsignaled state
447
448 // acquire the image
jvanverthb0d43522016-04-21 11:46:23 -0700449 VkResult res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
450 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
451 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700452 if (VK_ERROR_SURFACE_LOST_KHR == res) {
453 // need to figure out how to create a new vkSurface without the platformData*
jvanverth3d6ed3a2016-04-07 11:09:51 -0700454 // maybe use attach somehow? but need a Window
jvanverth9f372462016-04-06 06:08:59 -0700455 return nullptr;
456 }
jvanverth3d6ed3a2016-04-07 11:09:51 -0700457 if (VK_ERROR_OUT_OF_DATE_KHR == res) {
jvanverth9f372462016-04-06 06:08:59 -0700458 // tear swapchain down and try again
Greg Daniel1f05f442016-10-27 16:37:17 -0400459 if (!this->createSwapchain(-1, -1, fDisplayParams)) {
jvanverth9f372462016-04-06 06:08:59 -0700460 return nullptr;
461 }
Jim Van Verthd63c1022017-01-05 13:50:49 -0500462 backbuffer = this->getAvailableBackbuffer();
463 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
464 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
jvanverth9f372462016-04-06 06:08:59 -0700465
466 // acquire the image
jvanvertha8d0d6c2016-05-05 12:32:03 -0700467 res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
jvanverthb0d43522016-04-21 11:46:23 -0700468 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
469 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700470
471 if (VK_SUCCESS != res) {
472 return nullptr;
473 }
474 }
475
476 // set up layout transfer from initial to color attachment
477 VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex];
egdaniel580fa592016-08-31 11:03:50 -0700478 SkASSERT(VK_IMAGE_LAYOUT_UNDEFINED == layout || VK_IMAGE_LAYOUT_PRESENT_SRC_KHR == layout);
jvanverth9f372462016-04-06 06:08:59 -0700479 VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
480 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT :
481 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
482 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700483 VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
jvanverth9f372462016-04-06 06:08:59 -0700484 0 : VK_ACCESS_MEMORY_READ_BIT;
485 VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
486
487 VkImageMemoryBarrier imageMemoryBarrier = {
488 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
489 NULL, // pNext
490 srcAccessMask, // outputMask
491 dstAccessMask, // inputMask
492 layout, // oldLayout
493 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout
494 fPresentQueueIndex, // srcQueueFamilyIndex
jvanverthb0d43522016-04-21 11:46:23 -0700495 fBackendContext->fGraphicsQueueIndex, // dstQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700496 fImages[backbuffer->fImageIndex], // image
497 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
498 };
499 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
500 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[0], 0));
501 VkCommandBufferBeginInfo info;
502 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
503 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
504 info.flags = 0;
505 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
506 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info));
507
jvanvertha8d0d6c2016-05-05 12:32:03 -0700508 GR_VK_CALL(fBackendContext->fInterface,
509 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0],
510 srcStageMask, dstStageMask, 0,
511 0, nullptr,
512 0, nullptr,
513 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700514
515 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700516 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0]));
jvanverth9f372462016-04-06 06:08:59 -0700517
egdaniel58a8d922016-04-21 08:03:10 -0700518 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanverth9f372462016-04-06 06:08:59 -0700519 // insert the layout transfer into the queue and wait on the acquire
520 VkSubmitInfo submitInfo;
521 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
522 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
523 submitInfo.waitSemaphoreCount = 1;
524 submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore;
egdaniel58a8d922016-04-21 08:03:10 -0700525 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
jvanverth9f372462016-04-06 06:08:59 -0700526 submitInfo.commandBufferCount = 1;
527 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0];
528 submitInfo.signalSemaphoreCount = 0;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700529
jvanverth9f372462016-04-06 06:08:59 -0700530 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700531 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700532 backbuffer->fUsageFences[0]));
533
egdaniel580fa592016-08-31 11:03:50 -0700534 GrVkImageInfo* imageInfo;
535 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
536 surface->getRenderTargetHandle((GrBackendObject*)&imageInfo,
537 SkSurface::kFlushRead_BackendHandleAccess);
538 imageInfo->updateImageLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
539
540 return sk_ref_sp(surface);
jvanverth9f372462016-04-06 06:08:59 -0700541}
542
jvanvertha8d0d6c2016-05-05 12:32:03 -0700543void VulkanWindowContext::swapBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700544
545 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
egdaniel580fa592016-08-31 11:03:50 -0700546 GrVkImageInfo* imageInfo;
547 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
548 surface->getRenderTargetHandle((GrBackendObject*)&imageInfo,
549 SkSurface::kFlushRead_BackendHandleAccess);
550 // Check to make sure we never change the actually wrapped image
551 SkASSERT(imageInfo->fImage == fImages[backbuffer->fImageIndex]);
jvanverth9f372462016-04-06 06:08:59 -0700552
egdaniel580fa592016-08-31 11:03:50 -0700553 VkImageLayout layout = imageInfo->fImageLayout;
554 VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(layout);
jvanverth9f372462016-04-06 06:08:59 -0700555 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
egdaniel580fa592016-08-31 11:03:50 -0700556 VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(layout);
jvanverth9f372462016-04-06 06:08:59 -0700557 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
558
559 VkImageMemoryBarrier imageMemoryBarrier = {
560 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
561 NULL, // pNext
562 srcAccessMask, // outputMask
563 dstAccessMask, // inputMask
564 layout, // oldLayout
565 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout
jvanverthb0d43522016-04-21 11:46:23 -0700566 fBackendContext->fGraphicsQueueIndex, // srcQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700567 fPresentQueueIndex, // dstQueueFamilyIndex
568 fImages[backbuffer->fImageIndex], // image
569 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
570 };
571 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
572 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[1], 0));
573 VkCommandBufferBeginInfo info;
574 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
575 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
576 info.flags = 0;
577 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
578 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info));
579 GR_VK_CALL(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700580 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
581 srcStageMask, dstStageMask, 0,
582 0, nullptr,
583 0, nullptr,
584 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700585 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
586 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1]));
587
588 fImageLayouts[backbuffer->fImageIndex] = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
589
590 // insert the layout transfer into the queue and wait on the acquire
591 VkSubmitInfo submitInfo;
592 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
593 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
594 submitInfo.waitSemaphoreCount = 0;
595 submitInfo.pWaitDstStageMask = 0;
596 submitInfo.commandBufferCount = 1;
597 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[1];
598 submitInfo.signalSemaphoreCount = 1;
599 submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore;
600
601 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700602 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700603 backbuffer->fUsageFences[1]));
604
605 // Submit present operation to present queue
606 const VkPresentInfoKHR presentInfo =
607 {
608 VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType
609 NULL, // pNext
610 1, // waitSemaphoreCount
611 &backbuffer->fRenderSemaphore, // pWaitSemaphores
612 1, // swapchainCount
613 &fSwapchain, // pSwapchains
614 &backbuffer->fImageIndex, // pImageIndices
615 NULL // pResults
616 };
617
jvanverthb0d43522016-04-21 11:46:23 -0700618 fQueuePresentKHR(fPresentQueue, &presentInfo);
jvanverth9f372462016-04-06 06:08:59 -0700619}
jvanvertha8d0d6c2016-05-05 12:32:03 -0700620
621} //namespace sk_app