blob: f483c27163bde690ecfe25f9d85b67f0c0f6f7c9 [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"
jvanverthaf236b52016-05-20 06:01:06 -070011#include "GrRenderTarget.h"
Hal Canary95e3c052017-01-11 12:44:43 -050012#include "SkAutoMalloc.h"
jvanverth9f372462016-04-06 06:08:59 -070013#include "SkSurface.h"
jvanvertha8d0d6c2016-05-05 12:32:03 -070014#include "VulkanWindowContext.h"
jvanverth9f372462016-04-06 06:08:59 -070015
16#include "vk/GrVkInterface.h"
egdaniel580fa592016-08-31 11:03:50 -070017#include "vk/GrVkMemory.h"
jvanverth9f372462016-04-06 06:08:59 -070018#include "vk/GrVkUtil.h"
19#include "vk/GrVkTypes.h"
20
21#ifdef VK_USE_PLATFORM_WIN32_KHR
22// windows wants to define this as CreateSemaphoreA or CreateSemaphoreW
23#undef CreateSemaphore
24#endif
25
jvanverthb0d43522016-04-21 11:46:23 -070026#define GET_PROC(F) f ## F = (PFN_vk ## F) vkGetInstanceProcAddr(instance, "vk" #F)
27#define GET_DEV_PROC(F) f ## F = (PFN_vk ## F) vkGetDeviceProcAddr(device, "vk" #F)
28
jvanvertha8d0d6c2016-05-05 12:32:03 -070029namespace sk_app {
30
bsalomond1bdd1f2016-07-26 12:02:50 -070031VulkanWindowContext::VulkanWindowContext(const DisplayParams& params,
32 CreateVkSurfaceFn createVkSurface,
33 CanPresentFn canPresent)
jvanverthaf236b52016-05-20 06:01:06 -070034 : WindowContext()
35 , fSurface(VK_NULL_HANDLE)
jvanvertha8d0d6c2016-05-05 12:32:03 -070036 , fSwapchain(VK_NULL_HANDLE)
jvanverthaf236b52016-05-20 06:01:06 -070037 , fImages(nullptr)
38 , fImageLayouts(nullptr)
39 , fSurfaces(nullptr)
jvanvertha8d0d6c2016-05-05 12:32:03 -070040 , fCommandPool(VK_NULL_HANDLE)
41 , fBackbuffers(nullptr) {
jvanverth9f372462016-04-06 06:08:59 -070042
43 // any config code here (particularly for msaa)?
Brian Salomonc1889822017-04-24 16:49:05 -040044 fBackendContext.reset(GrVkBackendContext::Create(vkGetInstanceProcAddr, vkGetDeviceProcAddr,
45 &fPresentQueueIndex, canPresent));
jvanverth9f372462016-04-06 06:08:59 -070046
jvanverthb0d43522016-04-21 11:46:23 -070047 if (!(fBackendContext->fExtensions & kKHR_surface_GrVkExtensionFlag) ||
48 !(fBackendContext->fExtensions & kKHR_swapchain_GrVkExtensionFlag)) {
49 fBackendContext.reset(nullptr);
50 return;
51 }
52
53 VkInstance instance = fBackendContext->fInstance;
54 VkDevice device = fBackendContext->fDevice;
55 GET_PROC(DestroySurfaceKHR);
56 GET_PROC(GetPhysicalDeviceSurfaceSupportKHR);
57 GET_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
58 GET_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
59 GET_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
60 GET_DEV_PROC(CreateSwapchainKHR);
61 GET_DEV_PROC(DestroySwapchainKHR);
62 GET_DEV_PROC(GetSwapchainImagesKHR);
63 GET_DEV_PROC(AcquireNextImageKHR);
64 GET_DEV_PROC(QueuePresentKHR);
jvanverth9f372462016-04-06 06:08:59 -070065
csmartdalton008b9d82017-02-22 12:00:42 -070066 fContext = GrContext::Create(kVulkan_GrBackend, (GrBackendContext) fBackendContext.get(),
csmartdalton61cd31a2017-02-27 17:00:53 -070067 params.fGrContextOptions);
jvanverth9f372462016-04-06 06:08:59 -070068
bsalomond1bdd1f2016-07-26 12:02:50 -070069 fSurface = createVkSurface(instance);
jvanverth9f372462016-04-06 06:08:59 -070070 if (VK_NULL_HANDLE == fSurface) {
71 fBackendContext.reset(nullptr);
72 return;
73 }
74
jvanverth9f372462016-04-06 06:08:59 -070075 VkBool32 supported;
jvanverthb0d43522016-04-21 11:46:23 -070076 VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fBackendContext->fPhysicalDevice,
77 fPresentQueueIndex, fSurface,
78 &supported);
jvanverth9f372462016-04-06 06:08:59 -070079 if (VK_SUCCESS != res) {
80 this->destroyContext();
81 return;
82 }
83
brianosman05de2162016-05-06 13:28:57 -070084 if (!this->createSwapchain(-1, -1, params)) {
jvanverth9f372462016-04-06 06:08:59 -070085 this->destroyContext();
86 return;
87 }
88
89 // create presentQueue
90 vkGetDeviceQueue(fBackendContext->fDevice, fPresentQueueIndex, 0, &fPresentQueue);
jvanverth9f372462016-04-06 06:08:59 -070091}
92
bsalomonccde4ab2016-07-27 08:50:12 -070093bool VulkanWindowContext::createSwapchain(int width, int height,
brianosman05de2162016-05-06 13:28:57 -070094 const DisplayParams& params) {
jvanverth9f372462016-04-06 06:08:59 -070095 // check for capabilities
96 VkSurfaceCapabilitiesKHR caps;
jvanverthb0d43522016-04-21 11:46:23 -070097 VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fBackendContext->fPhysicalDevice,
98 fSurface, &caps);
jvanverth9f372462016-04-06 06:08:59 -070099 if (VK_SUCCESS != res) {
100 return false;
101 }
102
103 uint32_t surfaceFormatCount;
jvanverthb0d43522016-04-21 11:46:23 -0700104 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
105 &surfaceFormatCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700106 if (VK_SUCCESS != res) {
107 return false;
108 }
109
110 SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatKHR));
111 VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700112 res = fGetPhysicalDeviceSurfaceFormatsKHR(fBackendContext->fPhysicalDevice, fSurface,
113 &surfaceFormatCount, surfaceFormats);
jvanverth9f372462016-04-06 06:08:59 -0700114 if (VK_SUCCESS != res) {
115 return false;
116 }
117
118 uint32_t presentModeCount;
jvanverthb0d43522016-04-21 11:46:23 -0700119 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
120 &presentModeCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700121 if (VK_SUCCESS != res) {
122 return false;
123 }
124
125 SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR));
126 VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get();
jvanverthb0d43522016-04-21 11:46:23 -0700127 res = fGetPhysicalDeviceSurfacePresentModesKHR(fBackendContext->fPhysicalDevice, fSurface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700128 &presentModeCount, presentModes);
jvanverth9f372462016-04-06 06:08:59 -0700129 if (VK_SUCCESS != res) {
130 return false;
131 }
132
133 VkExtent2D extent = caps.currentExtent;
134 // use the hints
135 if (extent.width == (uint32_t)-1) {
136 extent.width = width;
137 extent.height = height;
138 }
139
jvanverth9fab59d2016-04-06 12:08:51 -0700140 // clamp width; to protect us from broken hints
jvanverth9f372462016-04-06 06:08:59 -0700141 if (extent.width < caps.minImageExtent.width) {
142 extent.width = caps.minImageExtent.width;
143 } else if (extent.width > caps.maxImageExtent.width) {
144 extent.width = caps.maxImageExtent.width;
145 }
146 // clamp height
147 if (extent.height < caps.minImageExtent.height) {
148 extent.height = caps.minImageExtent.height;
149 } else if (extent.height > caps.maxImageExtent.height) {
150 extent.height = caps.maxImageExtent.height;
151 }
jvanverth1d155962016-05-23 13:13:36 -0700152
jvanverth9f372462016-04-06 06:08:59 -0700153 fWidth = (int)extent.width;
154 fHeight = (int)extent.height;
155
156 uint32_t imageCount = caps.minImageCount + 2;
157 if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) {
158 // Application must settle for fewer images than desired:
159 imageCount = caps.maxImageCount;
160 }
161
162 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
163 VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
164 VK_IMAGE_USAGE_TRANSFER_DST_BIT;
165 SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags);
166 SkASSERT(caps.supportedTransforms & caps.currentTransform);
167 SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
168 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR));
169 VkCompositeAlphaFlagBitsKHR composite_alpha =
170 (caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ?
171 VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR :
172 VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
173
brianosman05de2162016-05-06 13:28:57 -0700174 // Pick our surface format. For now, just make sure it matches our sRGB request:
175 VkFormat surfaceFormat = VK_FORMAT_UNDEFINED;
176 VkColorSpaceKHR colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
Matt Sarett77a7a1b2017-02-07 13:56:11 -0500177 auto srgbColorSpace = SkColorSpace::MakeSRGB();
brianosmanb109b8c2016-06-16 13:03:24 -0700178 bool wantSRGB = srgbColorSpace == params.fColorSpace;
brianosman05de2162016-05-06 13:28:57 -0700179 for (uint32_t i = 0; i < surfaceFormatCount; ++i) {
Greg Daniel94403452017-04-18 15:52:36 -0400180 GrPixelConfig config = GrVkFormatToPixelConfig(surfaceFormats[i].format);
181 if (kUnknown_GrPixelConfig != config &&
brianosman05de2162016-05-06 13:28:57 -0700182 GrPixelConfigIsSRGB(config) == wantSRGB) {
183 surfaceFormat = surfaceFormats[i].format;
184 colorSpace = surfaceFormats[i].colorSpace;
185 break;
186 }
187 }
188 fDisplayParams = params;
csmartdalton578f0642017-02-24 16:04:47 -0700189 fSampleCount = params.fMSAASampleCount;
190 fStencilBits = 8;
brianosman05de2162016-05-06 13:28:57 -0700191
192 if (VK_FORMAT_UNDEFINED == surfaceFormat) {
193 return false;
194 }
jvanvertha4b0fed2016-04-27 11:42:21 -0700195
jvanverth3d6ed3a2016-04-07 11:09:51 -0700196 // If mailbox mode is available, use it, as it is the lowest-latency non-
197 // tearing mode. If not, fall back to FIFO which is always available.
jvanverth9f372462016-04-06 06:08:59 -0700198 VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700199 for (uint32_t i = 0; i < presentModeCount; ++i) {
jvanverth3d6ed3a2016-04-07 11:09:51 -0700200 // use mailbox
201 if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) {
jvanverth9f372462016-04-06 06:08:59 -0700202 mode = presentModes[i];
jvanverth3d6ed3a2016-04-07 11:09:51 -0700203 break;
jvanverth9f372462016-04-06 06:08:59 -0700204 }
205 }
206
207 VkSwapchainCreateInfoKHR swapchainCreateInfo;
208 memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR));
209 swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
210 swapchainCreateInfo.surface = fSurface;
211 swapchainCreateInfo.minImageCount = imageCount;
jvanvertha4b0fed2016-04-27 11:42:21 -0700212 swapchainCreateInfo.imageFormat = surfaceFormat;
213 swapchainCreateInfo.imageColorSpace = colorSpace;
jvanverth9f372462016-04-06 06:08:59 -0700214 swapchainCreateInfo.imageExtent = extent;
215 swapchainCreateInfo.imageArrayLayers = 1;
216 swapchainCreateInfo.imageUsage = usageFlags;
217
jvanverthb0d43522016-04-21 11:46:23 -0700218 uint32_t queueFamilies[] = { fBackendContext->fGraphicsQueueIndex, fPresentQueueIndex };
219 if (fBackendContext->fGraphicsQueueIndex != fPresentQueueIndex) {
jvanverth9f372462016-04-06 06:08:59 -0700220 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
221 swapchainCreateInfo.queueFamilyIndexCount = 2;
222 swapchainCreateInfo.pQueueFamilyIndices = queueFamilies;
223 } else {
224 swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
225 swapchainCreateInfo.queueFamilyIndexCount = 0;
226 swapchainCreateInfo.pQueueFamilyIndices = nullptr;
227 }
228
Greg Daniele897b972016-10-06 13:57:57 -0400229 swapchainCreateInfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
jvanverth9f372462016-04-06 06:08:59 -0700230 swapchainCreateInfo.compositeAlpha = composite_alpha;
231 swapchainCreateInfo.presentMode = mode;
232 swapchainCreateInfo.clipped = true;
233 swapchainCreateInfo.oldSwapchain = fSwapchain;
234
jvanverthb0d43522016-04-21 11:46:23 -0700235 res = fCreateSwapchainKHR(fBackendContext->fDevice, &swapchainCreateInfo, nullptr, &fSwapchain);
jvanverth9f372462016-04-06 06:08:59 -0700236 if (VK_SUCCESS != res) {
237 return false;
238 }
239
240 // destroy the old swapchain
241 if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) {
242 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
243
244 this->destroyBuffers();
245
jvanverthb0d43522016-04-21 11:46:23 -0700246 fDestroySwapchainKHR(fBackendContext->fDevice, swapchainCreateInfo.oldSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700247 }
248
egdaniel58a8d922016-04-21 08:03:10 -0700249 this->createBuffers(swapchainCreateInfo.imageFormat);
jvanverth9f372462016-04-06 06:08:59 -0700250
251 return true;
252}
253
jvanvertha8d0d6c2016-05-05 12:32:03 -0700254void VulkanWindowContext::createBuffers(VkFormat format) {
Greg Daniel94403452017-04-18 15:52:36 -0400255 fPixelConfig = GrVkFormatToPixelConfig(format);
256 SkASSERT(kUnknown_GrPixelConfig != fPixelConfig);
egdaniel58a8d922016-04-21 08:03:10 -0700257
jvanverthb0d43522016-04-21 11:46:23 -0700258 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700259 SkASSERT(fImageCount);
260 fImages = new VkImage[fImageCount];
jvanverthb0d43522016-04-21 11:46:23 -0700261 fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, fImages);
jvanverth9f372462016-04-06 06:08:59 -0700262
263 // set up initial image layouts and create surfaces
264 fImageLayouts = new VkImageLayout[fImageCount];
265 fSurfaces = new sk_sp<SkSurface>[fImageCount];
266 for (uint32_t i = 0; i < fImageCount; ++i) {
267 fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED;
268
egdanielb2df0c22016-05-13 11:30:37 -0700269 GrVkImageInfo info;
jvanverth9f372462016-04-06 06:08:59 -0700270 info.fImage = fImages[i];
jvanverth9d54afc2016-09-20 09:20:03 -0700271 info.fAlloc = { VK_NULL_HANDLE, 0, 0, 0 };
egdaniel580fa592016-08-31 11:03:50 -0700272 info.fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
jvanverth9f372462016-04-06 06:08:59 -0700273 info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
egdaniel58a8d922016-04-21 08:03:10 -0700274 info.fFormat = format;
jvanverth3622a172016-05-10 06:42:18 -0700275 info.fLevelCount = 1;
jvanverthaf236b52016-05-20 06:01:06 -0700276
Greg Daniele79b4732017-04-20 14:07:46 -0400277 GrBackendTexture backendTex(fWidth, fHeight, &info);
278
279 fSurfaces[i] = SkSurface::MakeFromBackendTextureAsRenderTarget(fContext, backendTex,
280 kTopLeft_GrSurfaceOrigin,
281 fSampleCount,
282 fDisplayParams.fColorSpace,
283 &fSurfaceProps);
jvanverth9f372462016-04-06 06:08:59 -0700284 }
285
286 // create the command pool for the command buffers
287 if (VK_NULL_HANDLE == fCommandPool) {
288 VkCommandPoolCreateInfo commandPoolInfo;
289 memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo));
290 commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
291 // this needs to be on the render queue
jvanverthb0d43522016-04-21 11:46:23 -0700292 commandPoolInfo.queueFamilyIndex = fBackendContext->fGraphicsQueueIndex;
jvanverth9f372462016-04-06 06:08:59 -0700293 commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
294 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
295 CreateCommandPool(fBackendContext->fDevice, &commandPoolInfo,
296 nullptr, &fCommandPool));
297 }
298
299 // set up the backbuffers
300 VkSemaphoreCreateInfo semaphoreInfo;
301 memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo));
302 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
303 semaphoreInfo.pNext = nullptr;
304 semaphoreInfo.flags = 0;
305 VkCommandBufferAllocateInfo commandBuffersInfo;
306 memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo));
307 commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
308 commandBuffersInfo.pNext = nullptr;
309 commandBuffersInfo.commandPool = fCommandPool;
310 commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
311 commandBuffersInfo.commandBufferCount = 2;
312 VkFenceCreateInfo fenceInfo;
313 memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo));
314 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
315 fenceInfo.pNext = nullptr;
316 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
317
Greg Daniel1f05f442016-10-27 16:37:17 -0400318 // we create one additional backbuffer structure here, because we want to
jvanverth9f372462016-04-06 06:08:59 -0700319 // give the command buffers they contain a chance to finish before we cycle back
320 fBackbuffers = new BackbufferInfo[fImageCount + 1];
321 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
322 fBackbuffers[i].fImageIndex = -1;
323 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
324 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
325 nullptr, &fBackbuffers[i].fAcquireSemaphore));
326 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
327 CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo,
328 nullptr, &fBackbuffers[i].fRenderSemaphore));
329 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
330 AllocateCommandBuffers(fBackendContext->fDevice, &commandBuffersInfo,
331 fBackbuffers[i].fTransitionCmdBuffers));
332 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
333 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
334 &fBackbuffers[i].fUsageFences[0]));
335 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
336 CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr,
337 &fBackbuffers[i].fUsageFences[1]));
338 }
339 fCurrentBackbufferIndex = fImageCount;
340}
341
jvanvertha8d0d6c2016-05-05 12:32:03 -0700342void VulkanWindowContext::destroyBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700343
344 if (fBackbuffers) {
345 for (uint32_t i = 0; i < fImageCount + 1; ++i) {
346 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700347 WaitForFences(fBackendContext->fDevice, 2,
jvanverth9f372462016-04-06 06:08:59 -0700348 fBackbuffers[i].fUsageFences,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700349 true, UINT64_MAX));
jvanverth9f372462016-04-06 06:08:59 -0700350 fBackbuffers[i].fImageIndex = -1;
351 GR_VK_CALL(fBackendContext->fInterface,
352 DestroySemaphore(fBackendContext->fDevice,
353 fBackbuffers[i].fAcquireSemaphore,
354 nullptr));
355 GR_VK_CALL(fBackendContext->fInterface,
356 DestroySemaphore(fBackendContext->fDevice,
357 fBackbuffers[i].fRenderSemaphore,
358 nullptr));
359 GR_VK_CALL(fBackendContext->fInterface,
360 FreeCommandBuffers(fBackendContext->fDevice, fCommandPool, 2,
361 fBackbuffers[i].fTransitionCmdBuffers));
362 GR_VK_CALL(fBackendContext->fInterface,
363 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[0], 0));
364 GR_VK_CALL(fBackendContext->fInterface,
365 DestroyFence(fBackendContext->fDevice, fBackbuffers[i].fUsageFences[1], 0));
366 }
367 }
368
369 delete[] fBackbuffers;
370 fBackbuffers = nullptr;
371
jvanverthaf236b52016-05-20 06:01:06 -0700372 // Does this actually free the surfaces?
jvanverth9f372462016-04-06 06:08:59 -0700373 delete[] fSurfaces;
374 fSurfaces = nullptr;
375 delete[] fImageLayouts;
376 fImageLayouts = nullptr;
377 delete[] fImages;
378 fImages = nullptr;
379}
380
jvanvertha8d0d6c2016-05-05 12:32:03 -0700381VulkanWindowContext::~VulkanWindowContext() {
jvanverth9f372462016-04-06 06:08:59 -0700382 this->destroyContext();
383}
384
jvanvertha8d0d6c2016-05-05 12:32:03 -0700385void VulkanWindowContext::destroyContext() {
jvanverth9f372462016-04-06 06:08:59 -0700386 if (!fBackendContext.get()) {
387 return;
388 }
389
jvanverth85f758c2016-05-27 06:47:08 -0700390 GR_VK_CALL(fBackendContext->fInterface, QueueWaitIdle(fPresentQueue));
jvanverth9f372462016-04-06 06:08:59 -0700391 GR_VK_CALL(fBackendContext->fInterface, DeviceWaitIdle(fBackendContext->fDevice));
392
393 this->destroyBuffers();
394
395 if (VK_NULL_HANDLE != fCommandPool) {
396 GR_VK_CALL(fBackendContext->fInterface, DestroyCommandPool(fBackendContext->fDevice,
397 fCommandPool, nullptr));
398 fCommandPool = VK_NULL_HANDLE;
399 }
400
401 if (VK_NULL_HANDLE != fSwapchain) {
jvanverthb0d43522016-04-21 11:46:23 -0700402 fDestroySwapchainKHR(fBackendContext->fDevice, fSwapchain, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700403 fSwapchain = VK_NULL_HANDLE;
404 }
405
406 if (VK_NULL_HANDLE != fSurface) {
jvanverthb0d43522016-04-21 11:46:23 -0700407 fDestroySurfaceKHR(fBackendContext->fInstance, fSurface, nullptr);
jvanverth9f372462016-04-06 06:08:59 -0700408 fSurface = VK_NULL_HANDLE;
409 }
410
jvanverthaf236b52016-05-20 06:01:06 -0700411 fContext->unref();
jvanverth9f372462016-04-06 06:08:59 -0700412
413 fBackendContext.reset(nullptr);
414}
415
jvanvertha8d0d6c2016-05-05 12:32:03 -0700416VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer() {
jvanverth9f372462016-04-06 06:08:59 -0700417 SkASSERT(fBackbuffers);
418
419 ++fCurrentBackbufferIndex;
egdaniel804af7d2016-09-26 12:30:46 -0700420 if (fCurrentBackbufferIndex > fImageCount) {
jvanverth9f372462016-04-06 06:08:59 -0700421 fCurrentBackbufferIndex = 0;
422 }
423
424 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
jvanverth9f372462016-04-06 06:08:59 -0700425 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
426 WaitForFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences,
427 true, UINT64_MAX));
428 return backbuffer;
429}
430
jvanverthaf236b52016-05-20 06:01:06 -0700431sk_sp<SkSurface> VulkanWindowContext::getBackbufferSurface() {
jvanverth9f372462016-04-06 06:08:59 -0700432 BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
433 SkASSERT(backbuffer);
434
435 // reset the fence
436 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
437 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
438 // semaphores should be in unsignaled state
439
440 // acquire the image
jvanverthb0d43522016-04-21 11:46:23 -0700441 VkResult res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
442 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
443 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700444 if (VK_ERROR_SURFACE_LOST_KHR == res) {
445 // need to figure out how to create a new vkSurface without the platformData*
jvanverth3d6ed3a2016-04-07 11:09:51 -0700446 // maybe use attach somehow? but need a Window
jvanverth9f372462016-04-06 06:08:59 -0700447 return nullptr;
448 }
jvanverth3d6ed3a2016-04-07 11:09:51 -0700449 if (VK_ERROR_OUT_OF_DATE_KHR == res) {
jvanverth9f372462016-04-06 06:08:59 -0700450 // tear swapchain down and try again
Greg Daniel1f05f442016-10-27 16:37:17 -0400451 if (!this->createSwapchain(-1, -1, fDisplayParams)) {
jvanverth9f372462016-04-06 06:08:59 -0700452 return nullptr;
453 }
Jim Van Verthd63c1022017-01-05 13:50:49 -0500454 backbuffer = this->getAvailableBackbuffer();
455 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
456 ResetFences(fBackendContext->fDevice, 2, backbuffer->fUsageFences));
jvanverth9f372462016-04-06 06:08:59 -0700457
458 // acquire the image
jvanvertha8d0d6c2016-05-05 12:32:03 -0700459 res = fAcquireNextImageKHR(fBackendContext->fDevice, fSwapchain, UINT64_MAX,
jvanverthb0d43522016-04-21 11:46:23 -0700460 backbuffer->fAcquireSemaphore, VK_NULL_HANDLE,
461 &backbuffer->fImageIndex);
jvanverth9f372462016-04-06 06:08:59 -0700462
463 if (VK_SUCCESS != res) {
464 return nullptr;
465 }
466 }
467
468 // set up layout transfer from initial to color attachment
469 VkImageLayout layout = fImageLayouts[backbuffer->fImageIndex];
egdaniel580fa592016-08-31 11:03:50 -0700470 SkASSERT(VK_IMAGE_LAYOUT_UNDEFINED == layout || VK_IMAGE_LAYOUT_PRESENT_SRC_KHR == layout);
jvanverth9f372462016-04-06 06:08:59 -0700471 VkPipelineStageFlags srcStageMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
472 VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT :
473 VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
474 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700475 VkAccessFlags srcAccessMask = (VK_IMAGE_LAYOUT_UNDEFINED == layout) ?
jvanverth9f372462016-04-06 06:08:59 -0700476 0 : VK_ACCESS_MEMORY_READ_BIT;
477 VkAccessFlags dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
478
479 VkImageMemoryBarrier imageMemoryBarrier = {
480 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
481 NULL, // pNext
482 srcAccessMask, // outputMask
483 dstAccessMask, // inputMask
484 layout, // oldLayout
485 VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, // newLayout
486 fPresentQueueIndex, // srcQueueFamilyIndex
jvanverthb0d43522016-04-21 11:46:23 -0700487 fBackendContext->fGraphicsQueueIndex, // dstQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700488 fImages[backbuffer->fImageIndex], // image
489 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
490 };
491 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
492 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[0], 0));
493 VkCommandBufferBeginInfo info;
494 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
495 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
496 info.flags = 0;
497 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
498 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[0], &info));
499
jvanvertha8d0d6c2016-05-05 12:32:03 -0700500 GR_VK_CALL(fBackendContext->fInterface,
501 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[0],
502 srcStageMask, dstStageMask, 0,
503 0, nullptr,
504 0, nullptr,
505 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700506
507 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700508 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[0]));
jvanverth9f372462016-04-06 06:08:59 -0700509
egdaniel58a8d922016-04-21 08:03:10 -0700510 VkPipelineStageFlags waitDstStageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
jvanverth9f372462016-04-06 06:08:59 -0700511 // insert the layout transfer into the queue and wait on the acquire
512 VkSubmitInfo submitInfo;
513 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
514 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
515 submitInfo.waitSemaphoreCount = 1;
516 submitInfo.pWaitSemaphores = &backbuffer->fAcquireSemaphore;
egdaniel58a8d922016-04-21 08:03:10 -0700517 submitInfo.pWaitDstStageMask = &waitDstStageFlags;
jvanverth9f372462016-04-06 06:08:59 -0700518 submitInfo.commandBufferCount = 1;
519 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[0];
520 submitInfo.signalSemaphoreCount = 0;
jvanvertha8d0d6c2016-05-05 12:32:03 -0700521
jvanverth9f372462016-04-06 06:08:59 -0700522 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700523 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700524 backbuffer->fUsageFences[0]));
525
egdaniel580fa592016-08-31 11:03:50 -0700526 GrVkImageInfo* imageInfo;
527 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
528 surface->getRenderTargetHandle((GrBackendObject*)&imageInfo,
529 SkSurface::kFlushRead_BackendHandleAccess);
530 imageInfo->updateImageLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
531
532 return sk_ref_sp(surface);
jvanverth9f372462016-04-06 06:08:59 -0700533}
534
jvanvertha8d0d6c2016-05-05 12:32:03 -0700535void VulkanWindowContext::swapBuffers() {
jvanverth9f372462016-04-06 06:08:59 -0700536
537 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
egdaniel580fa592016-08-31 11:03:50 -0700538 GrVkImageInfo* imageInfo;
539 SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
540 surface->getRenderTargetHandle((GrBackendObject*)&imageInfo,
541 SkSurface::kFlushRead_BackendHandleAccess);
542 // Check to make sure we never change the actually wrapped image
543 SkASSERT(imageInfo->fImage == fImages[backbuffer->fImageIndex]);
jvanverth9f372462016-04-06 06:08:59 -0700544
egdaniel580fa592016-08-31 11:03:50 -0700545 VkImageLayout layout = imageInfo->fImageLayout;
546 VkPipelineStageFlags srcStageMask = GrVkMemory::LayoutToPipelineStageFlags(layout);
jvanverth9f372462016-04-06 06:08:59 -0700547 VkPipelineStageFlags dstStageMask = VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT;
egdaniel580fa592016-08-31 11:03:50 -0700548 VkAccessFlags srcAccessMask = GrVkMemory::LayoutToSrcAccessMask(layout);
jvanverth9f372462016-04-06 06:08:59 -0700549 VkAccessFlags dstAccessMask = VK_ACCESS_MEMORY_READ_BIT;
550
551 VkImageMemoryBarrier imageMemoryBarrier = {
552 VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType
553 NULL, // pNext
554 srcAccessMask, // outputMask
555 dstAccessMask, // inputMask
556 layout, // oldLayout
557 VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, // newLayout
jvanverthb0d43522016-04-21 11:46:23 -0700558 fBackendContext->fGraphicsQueueIndex, // srcQueueFamilyIndex
jvanverth9f372462016-04-06 06:08:59 -0700559 fPresentQueueIndex, // dstQueueFamilyIndex
560 fImages[backbuffer->fImageIndex], // image
561 { VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1 } // subresourceRange
562 };
563 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
564 ResetCommandBuffer(backbuffer->fTransitionCmdBuffers[1], 0));
565 VkCommandBufferBeginInfo info;
566 memset(&info, 0, sizeof(VkCommandBufferBeginInfo));
567 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
568 info.flags = 0;
569 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
570 BeginCommandBuffer(backbuffer->fTransitionCmdBuffers[1], &info));
571 GR_VK_CALL(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700572 CmdPipelineBarrier(backbuffer->fTransitionCmdBuffers[1],
573 srcStageMask, dstStageMask, 0,
574 0, nullptr,
575 0, nullptr,
576 1, &imageMemoryBarrier));
jvanverth9f372462016-04-06 06:08:59 -0700577 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
578 EndCommandBuffer(backbuffer->fTransitionCmdBuffers[1]));
579
580 fImageLayouts[backbuffer->fImageIndex] = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
581
582 // insert the layout transfer into the queue and wait on the acquire
583 VkSubmitInfo submitInfo;
584 memset(&submitInfo, 0, sizeof(VkSubmitInfo));
585 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
586 submitInfo.waitSemaphoreCount = 0;
587 submitInfo.pWaitDstStageMask = 0;
588 submitInfo.commandBufferCount = 1;
589 submitInfo.pCommandBuffers = &backbuffer->fTransitionCmdBuffers[1];
590 submitInfo.signalSemaphoreCount = 1;
591 submitInfo.pSignalSemaphores = &backbuffer->fRenderSemaphore;
592
593 GR_VK_CALL_ERRCHECK(fBackendContext->fInterface,
jvanvertha8d0d6c2016-05-05 12:32:03 -0700594 QueueSubmit(fBackendContext->fQueue, 1, &submitInfo,
jvanverth9f372462016-04-06 06:08:59 -0700595 backbuffer->fUsageFences[1]));
596
597 // Submit present operation to present queue
598 const VkPresentInfoKHR presentInfo =
599 {
600 VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType
601 NULL, // pNext
602 1, // waitSemaphoreCount
603 &backbuffer->fRenderSemaphore, // pWaitSemaphores
604 1, // swapchainCount
605 &fSwapchain, // pSwapchains
606 &backbuffer->fImageIndex, // pImageIndices
607 NULL // pResults
608 };
609
jvanverthb0d43522016-04-21 11:46:23 -0700610 fQueuePresentKHR(fPresentQueue, &presentInfo);
jvanverth9f372462016-04-06 06:08:59 -0700611}
jvanvertha8d0d6c2016-05-05 12:32:03 -0700612
613} //namespace sk_app