| 1 |
/* |
| 2 |
* Copyright 2023 Google LLC |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "tools/window/VulkanWindowContext.h" |
| 9 |
|
| 10 |
#include "include/core/SkSurface.h" |
| 11 |
#include "include/gpu/ganesh/GrBackendSemaphore.h" |
| 12 |
#include "include/gpu/ganesh/GrBackendSurface.h" |
| 13 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 14 |
#include "include/gpu/ganesh/SkSurfaceGanesh.h" |
| 15 |
#include "include/gpu/ganesh/vk/GrVkBackendSemaphore.h" |
| 16 |
#include "include/gpu/ganesh/vk/GrVkBackendSurface.h" |
| 17 |
#include "include/gpu/ganesh/vk/GrVkDirectContext.h" |
| 18 |
#include "include/gpu/ganesh/vk/GrVkTypes.h" |
| 19 |
#include "include/gpu/vk/VulkanExtensions.h" |
| 20 |
#include "src/gpu/GpuTypesPriv.h" |
| 21 |
#include "include/gpu/vk/VulkanMutableTextureState.h" |
| 22 |
#include "src/base/SkAutoMalloc.h" |
| 23 |
#include "src/gpu/ganesh/vk/GrVkImage.h" |
| 24 |
#include "src/gpu/ganesh/vk/GrVkUtil.h" |
| 25 |
#include "src/gpu/vk/VulkanInterface.h" |
| 26 |
#include "src/gpu/vk/vulkanmemoryallocator/VulkanMemoryAllocatorPriv.h" |
| 27 |
|
| 28 |
#ifdef VK_USE_PLATFORM_WIN32_KHR |
| 29 |
// windows wants to define this as CreateSemaphoreA or CreateSemaphoreW |
| 30 |
#undef CreateSemaphore |
| 31 |
#endif |
| 32 |
|
| 33 |
#define GET_PROC(F) f ## F = \ |
| 34 |
(PFN_vk ## F) backendContext.fGetProc("vk" #F, fInstance, VK_NULL_HANDLE) |
| 35 |
#define GET_DEV_PROC(F) f ## F = \ |
| 36 |
(PFN_vk ## F) backendContext.fGetProc("vk" #F, VK_NULL_HANDLE, fDevice) |
| 37 |
|
| 38 |
namespace skwindow::internal { |
| 39 |
|
| 40 |
VulkanWindowContext::VulkanWindowContext(std::unique_ptr<const DisplayParams> params, |
| 41 |
CreateVkSurfaceFn createVkSurface, |
| 42 |
CanPresentFn canPresent, |
| 43 |
PFN_vkGetInstanceProcAddr instProc) |
| 44 |
: WindowContext(std::move(params)) |
| 45 |
, fDeviceSurface(VK_NULL_HANDLE) |
| 46 |
, fSwapchain(VK_NULL_HANDLE) |
| 47 |
, fCreateVkSurfaceFn(std::move(createVkSurface)) |
| 48 |
, fCanPresentFn(std::move(canPresent)) { |
| 49 |
fGetInstanceProcAddr = instProc; |
| 50 |
this->initializeContext(); |
| 51 |
} |
| 52 |
|
| 53 |
void VulkanWindowContext::initializeContext() { |
| 54 |
SkASSERT(!fContext); |
| 55 |
// Any config code here (particularly for msaa)? |
| 56 |
|
| 57 |
PFN_vkGetInstanceProcAddr getInstanceProc = fGetInstanceProcAddr; |
| 58 |
skgpu::VulkanBackendContext backendContext; |
| 59 |
skgpu::VulkanExtensions extensions; |
| 60 |
sk_gpu_test::TestVkFeatures features; |
| 61 |
if (!sk_gpu_test::CreateVkBackendContext(getInstanceProc, |
| 62 |
&backendContext, |
| 63 |
&extensions, |
| 64 |
&features, |
| 65 |
&fDebugMessenger, |
| 66 |
&fPresentQueueIndex, |
| 67 |
fCanPresentFn, |
| 68 |
fDisplayParams->createProtectedNativeBackend())) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
if (!extensions.hasExtension(VK_KHR_SURFACE_EXTENSION_NAME, 25) || |
| 73 |
!extensions.hasExtension(VK_KHR_SWAPCHAIN_EXTENSION_NAME, 68)) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
fInstance = backendContext.fInstance; |
| 78 |
fPhysicalDevice = backendContext.fPhysicalDevice; |
| 79 |
fDevice = backendContext.fDevice; |
| 80 |
fGraphicsQueueIndex = backendContext.fGraphicsQueueIndex; |
| 81 |
fGraphicsQueue = backendContext.fQueue; |
| 82 |
|
| 83 |
PFN_vkGetPhysicalDeviceProperties localGetPhysicalDeviceProperties = |
| 84 |
reinterpret_cast<PFN_vkGetPhysicalDeviceProperties>( |
| 85 |
backendContext.fGetProc("vkGetPhysicalDeviceProperties", |
| 86 |
backendContext.fInstance, |
| 87 |
VK_NULL_HANDLE)); |
| 88 |
if (!localGetPhysicalDeviceProperties) { |
| 89 |
return; |
| 90 |
} |
| 91 |
VkPhysicalDeviceProperties physDeviceProperties; |
| 92 |
localGetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &physDeviceProperties); |
| 93 |
uint32_t physDevVersion = physDeviceProperties.apiVersion; |
| 94 |
|
| 95 |
fInterface.reset(new skgpu::VulkanInterface(backendContext.fGetProc, |
| 96 |
fInstance, |
| 97 |
fDevice, |
| 98 |
backendContext.fMaxAPIVersion, |
| 99 |
physDevVersion, |
| 100 |
&extensions)); |
| 101 |
|
| 102 |
GET_PROC(DestroyInstance); |
| 103 |
if (fDebugMessenger != VK_NULL_HANDLE) { |
| 104 |
GET_PROC(DestroyDebugUtilsMessengerEXT); |
| 105 |
} |
| 106 |
GET_PROC(DestroySurfaceKHR); |
| 107 |
GET_PROC(GetPhysicalDeviceSurfaceSupportKHR); |
| 108 |
GET_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR); |
| 109 |
GET_PROC(GetPhysicalDeviceSurfaceFormatsKHR); |
| 110 |
GET_PROC(GetPhysicalDeviceSurfacePresentModesKHR); |
| 111 |
GET_DEV_PROC(DeviceWaitIdle); |
| 112 |
GET_DEV_PROC(QueueWaitIdle); |
| 113 |
GET_DEV_PROC(DestroyDevice); |
| 114 |
GET_DEV_PROC(CreateSwapchainKHR); |
| 115 |
GET_DEV_PROC(DestroySwapchainKHR); |
| 116 |
GET_DEV_PROC(GetSwapchainImagesKHR); |
| 117 |
GET_DEV_PROC(AcquireNextImageKHR); |
| 118 |
GET_DEV_PROC(QueuePresentKHR); |
| 119 |
GET_DEV_PROC(GetDeviceQueue); |
| 120 |
|
| 121 |
backendContext.fMemoryAllocator = |
| 122 |
skgpu::VulkanMemoryAllocators::Make(backendContext, skgpu::ThreadSafe::kNo); |
| 123 |
|
| 124 |
fContext = GrDirectContexts::MakeVulkan(backendContext, fDisplayParams->grContextOptions()); |
| 125 |
|
| 126 |
fDeviceSurface = fCreateVkSurfaceFn(fInstance); |
| 127 |
if (VK_NULL_HANDLE == fDeviceSurface) { |
| 128 |
this->destroyContext(); |
| 129 |
return; |
| 130 |
} |
| 131 |
|
| 132 |
VkBool32 supported; |
| 133 |
VkResult res = fGetPhysicalDeviceSurfaceSupportKHR( |
| 134 |
fPhysicalDevice, fPresentQueueIndex, fDeviceSurface, &supported); |
| 135 |
if (VK_SUCCESS != res) { |
| 136 |
this->destroyContext(); |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
if (!this->createSwapchain(-1, -1)) { |
| 141 |
this->destroyContext(); |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
fGetDeviceQueue(fDevice, fPresentQueueIndex, /*queueIndex=*/0, &fPresentQueue); |
| 146 |
} |
| 147 |
|
| 148 |
bool VulkanWindowContext::createSwapchain(int width, int height) { |
| 149 |
// Check surface capabilities |
| 150 |
VkSurfaceCapabilitiesKHR caps; |
| 151 |
VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fPhysicalDevice, fDeviceSurface, &caps); |
| 152 |
if (VK_SUCCESS != res) { |
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
uint32_t surfaceFormatCount; |
| 157 |
res = fGetPhysicalDeviceSurfaceFormatsKHR(fPhysicalDevice, fDeviceSurface, &surfaceFormatCount, |
| 158 |
nullptr); |
| 159 |
if (VK_SUCCESS != res) { |
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatKHR)); |
| 164 |
VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc.get(); |
| 165 |
res = fGetPhysicalDeviceSurfaceFormatsKHR(fPhysicalDevice, fDeviceSurface, &surfaceFormatCount, |
| 166 |
surfaceFormats); |
| 167 |
if (VK_SUCCESS != res) { |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
uint32_t presentModeCount; |
| 172 |
res = fGetPhysicalDeviceSurfacePresentModesKHR(fPhysicalDevice, fDeviceSurface, &presentModeCount, |
| 173 |
nullptr); |
| 174 |
if (VK_SUCCESS != res) { |
| 175 |
return false; |
| 176 |
} |
| 177 |
|
| 178 |
SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR)); |
| 179 |
VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get(); |
| 180 |
res = fGetPhysicalDeviceSurfacePresentModesKHR(fPhysicalDevice, fDeviceSurface, &presentModeCount, |
| 181 |
presentModes); |
| 182 |
if (VK_SUCCESS != res) { |
| 183 |
return false; |
| 184 |
} |
| 185 |
|
| 186 |
VkExtent2D extent = caps.currentExtent; |
| 187 |
// Use the hints for width + height |
| 188 |
if (extent.width == (uint32_t)-1) { |
| 189 |
extent.width = width; |
| 190 |
extent.height = height; |
| 191 |
} |
| 192 |
// Clamp the values to protect from broken hints |
| 193 |
if (extent.width < caps.minImageExtent.width) { |
| 194 |
extent.width = caps.minImageExtent.width; |
| 195 |
} else if (extent.width > caps.maxImageExtent.width) { |
| 196 |
extent.width = caps.maxImageExtent.width; |
| 197 |
} |
| 198 |
if (extent.height < caps.minImageExtent.height) { |
| 199 |
extent.height = caps.minImageExtent.height; |
| 200 |
} else if (extent.height > caps.maxImageExtent.height) { |
| 201 |
extent.height = caps.maxImageExtent.height; |
| 202 |
} |
| 203 |
fWidth = (int)extent.width; |
| 204 |
fHeight = (int)extent.height; |
| 205 |
|
| 206 |
uint32_t imageCount = caps.minImageCount + 2; |
| 207 |
if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) { |
| 208 |
// Application must settle for fewer images than desired: |
| 209 |
imageCount = caps.maxImageCount; |
| 210 |
} |
| 211 |
|
| 212 |
VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | |
| 213 |
VK_IMAGE_USAGE_TRANSFER_SRC_BIT | |
| 214 |
VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
| 215 |
SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags); |
| 216 |
if (caps.supportedUsageFlags & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT) { |
| 217 |
usageFlags |= VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT; |
| 218 |
} |
| 219 |
if (caps.supportedUsageFlags & VK_IMAGE_USAGE_SAMPLED_BIT) { |
| 220 |
usageFlags |= VK_IMAGE_USAGE_SAMPLED_BIT; |
| 221 |
} |
| 222 |
SkASSERT(caps.supportedTransforms & caps.currentTransform); |
| 223 |
SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR | |
| 224 |
VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR)); |
| 225 |
VkCompositeAlphaFlagBitsKHR composite_alpha = |
| 226 |
(caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ? |
| 227 |
VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR : |
| 228 |
VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; |
| 229 |
|
| 230 |
// Pick our surface format. |
| 231 |
VkFormat surfaceFormat = VK_FORMAT_UNDEFINED; |
| 232 |
VkColorSpaceKHR colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR; |
| 233 |
for (uint32_t i = 0; i < surfaceFormatCount; ++i) { |
| 234 |
VkFormat localFormat = surfaceFormats[i].format; |
| 235 |
if (GrVkFormatIsSupported(localFormat)) { |
| 236 |
surfaceFormat = localFormat; |
| 237 |
colorSpace = surfaceFormats[i].colorSpace; |
| 238 |
break; |
| 239 |
} |
| 240 |
} |
| 241 |
fSampleCount = std::max(1, fDisplayParams->msaaSampleCount()); |
| 242 |
fStencilBits = 8; |
| 243 |
|
| 244 |
if (VK_FORMAT_UNDEFINED == surfaceFormat) { |
| 245 |
return false; |
| 246 |
} |
| 247 |
|
| 248 |
SkColorType colorType; |
| 249 |
switch (surfaceFormat) { |
| 250 |
case VK_FORMAT_R8G8B8A8_UNORM: // fall through |
| 251 |
case VK_FORMAT_R8G8B8A8_SRGB: |
| 252 |
colorType = kRGBA_8888_SkColorType; |
| 253 |
break; |
| 254 |
case VK_FORMAT_B8G8R8A8_UNORM: // fall through |
| 255 |
colorType = kBGRA_8888_SkColorType; |
| 256 |
break; |
| 257 |
default: |
| 258 |
return false; |
| 259 |
} |
| 260 |
|
| 261 |
// If mailbox mode is available, use it, as it is the lowest-latency non- |
| 262 |
// tearing mode. If not, fall back to FIFO which is always available. |
| 263 |
VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR; |
| 264 |
bool hasImmediate = false; |
| 265 |
for (uint32_t i = 0; i < presentModeCount; ++i) { |
| 266 |
// use mailbox |
| 267 |
if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) { |
| 268 |
mode = VK_PRESENT_MODE_MAILBOX_KHR; |
| 269 |
} |
| 270 |
if (VK_PRESENT_MODE_IMMEDIATE_KHR == presentModes[i]) { |
| 271 |
hasImmediate = true; |
| 272 |
} |
| 273 |
} |
| 274 |
if (fDisplayParams->disableVsync() && hasImmediate) { |
| 275 |
mode = VK_PRESENT_MODE_IMMEDIATE_KHR; |
| 276 |
} |
| 277 |
|
| 278 |
VkSwapchainCreateInfoKHR swapchainCreateInfo; |
| 279 |
memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR)); |
| 280 |
swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; |
| 281 |
swapchainCreateInfo.flags = fDisplayParams->createProtectedNativeBackend() |
| 282 |
? VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR |
| 283 |
: 0; |
| 284 |
swapchainCreateInfo.surface = fDeviceSurface; |
| 285 |
swapchainCreateInfo.minImageCount = imageCount; |
| 286 |
swapchainCreateInfo.imageFormat = surfaceFormat; |
| 287 |
swapchainCreateInfo.imageColorSpace = colorSpace; |
| 288 |
swapchainCreateInfo.imageExtent = extent; |
| 289 |
swapchainCreateInfo.imageArrayLayers = 1; |
| 290 |
swapchainCreateInfo.imageUsage = usageFlags; |
| 291 |
|
| 292 |
uint32_t queueFamilies[] = { fGraphicsQueueIndex, fPresentQueueIndex }; |
| 293 |
if (fGraphicsQueueIndex != fPresentQueueIndex) { |
| 294 |
swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT; |
| 295 |
swapchainCreateInfo.queueFamilyIndexCount = 2; |
| 296 |
swapchainCreateInfo.pQueueFamilyIndices = queueFamilies; |
| 297 |
} else { |
| 298 |
swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 299 |
swapchainCreateInfo.queueFamilyIndexCount = 0; |
| 300 |
swapchainCreateInfo.pQueueFamilyIndices = nullptr; |
| 301 |
} |
| 302 |
|
| 303 |
swapchainCreateInfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR; |
| 304 |
swapchainCreateInfo.compositeAlpha = composite_alpha; |
| 305 |
swapchainCreateInfo.presentMode = mode; |
| 306 |
swapchainCreateInfo.clipped = true; |
| 307 |
swapchainCreateInfo.oldSwapchain = fSwapchain; |
| 308 |
|
| 309 |
res = fCreateSwapchainKHR(fDevice, &swapchainCreateInfo, nullptr, &fSwapchain); |
| 310 |
if (VK_SUCCESS != res) { |
| 311 |
return false; |
| 312 |
} |
| 313 |
|
| 314 |
// Destroy the old swapchain |
| 315 |
if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) { |
| 316 |
fDeviceWaitIdle(fDevice); |
| 317 |
this->resetSwapchainImages(); |
| 318 |
fDestroySwapchainKHR(fDevice, swapchainCreateInfo.oldSwapchain, nullptr); |
| 319 |
swapchainCreateInfo.oldSwapchain = VK_NULL_HANDLE; |
| 320 |
} |
| 321 |
// If buffer creation fails, destroy the swapchain. |
| 322 |
if (!this->populateSwapchainImages(swapchainCreateInfo.imageFormat, |
| 323 |
usageFlags, |
| 324 |
colorType, |
| 325 |
swapchainCreateInfo.imageSharingMode)) { |
| 326 |
fDeviceWaitIdle(fDevice); |
| 327 |
this->resetSwapchainImages(); |
| 328 |
fDestroySwapchainKHR(fDevice, swapchainCreateInfo.oldSwapchain, nullptr); |
| 329 |
swapchainCreateInfo.oldSwapchain = VK_NULL_HANDLE; |
| 330 |
return false; |
| 331 |
} |
| 332 |
|
| 333 |
return true; |
| 334 |
} |
| 335 |
|
| 336 |
bool VulkanWindowContext::populateSwapchainImages(VkFormat format, |
| 337 |
VkImageUsageFlags usageFlags, |
| 338 |
SkColorType colorType, |
| 339 |
VkSharingMode sharingMode) { |
| 340 |
// Determine number of swapchain images |
| 341 |
uint32_t swapchainImgCount; |
| 342 |
fGetSwapchainImagesKHR(fDevice, fSwapchain, &swapchainImgCount, /*pSwapchainImages*/nullptr); |
| 343 |
SkASSERT(swapchainImgCount); |
| 344 |
|
| 345 |
// Define an array of VkImages and query the driver to populate it |
| 346 |
skia_private::AutoTArray<VkImage> vkImages((size_t)swapchainImgCount); |
| 347 |
std::fill_n(vkImages.get(), swapchainImgCount, VK_NULL_HANDLE); |
| 348 |
fGetSwapchainImagesKHR(fDevice, fSwapchain, &swapchainImgCount, vkImages.get()); |
| 349 |
|
| 350 |
// Populate all swapchain image representations |
| 351 |
fImages = skia_private::AutoTArray<SwapchainImage>((size_t)swapchainImgCount); |
| 352 |
for (uint32_t i = 0; i < swapchainImgCount; ++i) { |
| 353 |
// Make sure we were provided a valid VkImage handle |
| 354 |
SkASSERT(vkImages[i] != VK_NULL_HANDLE); |
| 355 |
fImages[i].fVkImage = vkImages[i]; |
| 356 |
|
| 357 |
// Create the semaphore that will be signaled once the image done being rendered |
| 358 |
static const VkSemaphoreCreateInfo submitSemInfo = |
| 359 |
{VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, /*pNext=*/nullptr, /*flags=*/0}; |
| 360 |
SkDEBUGCODE(VkResult result = )GR_VK_CALL( |
| 361 |
fInterface, |
| 362 |
CreateSemaphore(fDevice, |
| 363 |
&submitSemInfo, |
| 364 |
/*pAllocator=*/nullptr, |
| 365 |
&fImages[i].fRenderCompletionSemaphore)); |
| 366 |
SkASSERT(result == VK_SUCCESS && fImages[i].fRenderCompletionSemaphore != VK_NULL_HANDLE); |
| 367 |
|
| 368 |
// Populate GrVkImageInfo for Surface creation |
| 369 |
GrVkImageInfo info; |
| 370 |
info.fImage = vkImages[i]; |
| 371 |
info.fAlloc = skgpu::VulkanAlloc(); |
| 372 |
info.fFormat = format; |
| 373 |
info.fImageUsageFlags = usageFlags; |
| 374 |
info.fLevelCount = 1; |
| 375 |
info.fCurrentQueueFamily = fPresentQueueIndex; |
| 376 |
info.fProtected = skgpu::Protected(fDisplayParams->createProtectedNativeBackend()); |
| 377 |
info.fSharingMode = sharingMode; |
| 378 |
|
| 379 |
// Based on whether the image can be sampled, create a Surface to present each image |
| 380 |
if (usageFlags & VK_IMAGE_USAGE_SAMPLED_BIT) { |
| 381 |
GrBackendTexture backendTexture = GrBackendTextures::MakeVk(fWidth, fHeight, info); |
| 382 |
fImages[i].fSurface = |
| 383 |
SkSurfaces::WrapBackendTexture(fContext.get(), |
| 384 |
backendTexture, |
| 385 |
kTopLeft_GrSurfaceOrigin, |
| 386 |
fDisplayParams->msaaSampleCount(), |
| 387 |
colorType, |
| 388 |
fDisplayParams->colorSpace(), |
| 389 |
&fDisplayParams->surfaceProps()); |
| 390 |
} else { |
| 391 |
if (fDisplayParams->msaaSampleCount() > 1) { |
| 392 |
return false; |
| 393 |
} |
| 394 |
info.fSampleCount = fSampleCount; |
| 395 |
GrBackendRenderTarget backendRT = GrBackendRenderTargets::MakeVk(fWidth, fHeight, info); |
| 396 |
fImages[i].fSurface = |
| 397 |
SkSurfaces::WrapBackendRenderTarget(fContext.get(), |
| 398 |
backendRT, |
| 399 |
kTopLeft_GrSurfaceOrigin, |
| 400 |
colorType, |
| 401 |
fDisplayParams->colorSpace(), |
| 402 |
&fDisplayParams->surfaceProps()); |
| 403 |
} |
| 404 |
|
| 405 |
// If surface creation was unsuccessful, return false to indicate failure |
| 406 |
if (!fImages[i].fSurface) { |
| 407 |
// Clean up any previously-created semaphores before returning |
| 408 |
this->resetSwapchainImages(); |
| 409 |
return false; |
| 410 |
} |
| 411 |
} |
| 412 |
return true; |
| 413 |
} |
| 414 |
|
| 415 |
void VulkanWindowContext::submitToGpu() { |
| 416 |
SkSurface* surface = fImages[fCurrentImageIndex].fSurface.get(); |
| 417 |
|
| 418 |
GrBackendSemaphore backendRenderSemaphore = |
| 419 |
GrBackendSemaphores::MakeVk(fImages[fCurrentImageIndex].fRenderCompletionSemaphore); |
| 420 |
|
| 421 |
GrFlushInfo info; |
| 422 |
info.fNumSemaphores = 1; |
| 423 |
info.fSignalSemaphores = &backendRenderSemaphore; |
| 424 |
skgpu::MutableTextureState presentState = skgpu::MutableTextureStates::MakeVulkan( |
| 425 |
VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, fPresentQueueIndex); |
| 426 |
auto dContext = surface->recordingContext()->asDirectContext(); |
| 427 |
dContext->flush(surface, info, &presentState); |
| 428 |
dContext->submit(); |
| 429 |
} |
| 430 |
|
| 431 |
void VulkanWindowContext::resetSwapchainImages() { |
| 432 |
if (fImages.empty()) { |
| 433 |
return; |
| 434 |
} |
| 435 |
|
| 436 |
// Clean up the image's semaphores |
| 437 |
for (size_t i = 0; i < fImages.size(); i++) { |
| 438 |
if (fImages[i].fRenderCompletionSemaphore != VK_NULL_HANDLE) { |
| 439 |
GR_VK_CALL(fInterface, DestroySemaphore(fDevice, |
| 440 |
fImages[i].fRenderCompletionSemaphore, |
| 441 |
/*pAllocator=*/nullptr)); |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
fImages.reset(); |
| 446 |
} |
| 447 |
|
| 448 |
VulkanWindowContext::~VulkanWindowContext() { |
| 449 |
this->destroyContext(); |
| 450 |
} |
| 451 |
|
| 452 |
void VulkanWindowContext::destroyContext() { |
| 453 |
if (this->isValid()) { |
| 454 |
fQueueWaitIdle(fPresentQueue); |
| 455 |
fDeviceWaitIdle(fDevice); |
| 456 |
|
| 457 |
this->resetSwapchainImages(); |
| 458 |
|
| 459 |
if (VK_NULL_HANDLE != fSwapchain) { |
| 460 |
fDestroySwapchainKHR(fDevice, fSwapchain, nullptr); |
| 461 |
fSwapchain = VK_NULL_HANDLE; |
| 462 |
} |
| 463 |
|
| 464 |
if (VK_NULL_HANDLE != fDeviceSurface) { |
| 465 |
fDestroySurfaceKHR(fInstance, fDeviceSurface, nullptr); |
| 466 |
fDeviceSurface = VK_NULL_HANDLE; |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
SkASSERT(fContext->unique()); |
| 471 |
fContext.reset(); |
| 472 |
fInterface.reset(); |
| 473 |
|
| 474 |
if (VK_NULL_HANDLE != fDevice) { |
| 475 |
fDestroyDevice(fDevice, nullptr); |
| 476 |
fDevice = VK_NULL_HANDLE; |
| 477 |
} |
| 478 |
|
| 479 |
#ifdef SK_ENABLE_VK_LAYERS |
| 480 |
if (fDebugMessenger != VK_NULL_HANDLE) { |
| 481 |
fDestroyDebugUtilsMessengerEXT(fInstance, fDebugMessenger, nullptr); |
| 482 |
} |
| 483 |
#endif |
| 484 |
|
| 485 |
fPhysicalDevice = VK_NULL_HANDLE; |
| 486 |
|
| 487 |
if (VK_NULL_HANDLE != fInstance) { |
| 488 |
fDestroyInstance(fInstance, nullptr); |
| 489 |
fInstance = VK_NULL_HANDLE; |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
sk_sp<SkSurface> VulkanWindowContext::getBackbufferSurface() { |
| 494 |
// Create a new, unsignaled semaphore to be signaled upon swapchain image acquisition. |
| 495 |
static const VkSemaphoreCreateInfo acquireSemInfo = |
| 496 |
{ VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, /*pNext=*/nullptr, /*flags=*/0 }; |
| 497 |
SkDEBUGCODE(VkResult result = )GR_VK_CALL( |
| 498 |
fInterface, |
| 499 |
CreateSemaphore(fDevice, &acquireSemInfo, /*pAllocator=*/nullptr, &fAcquireSemaphore)); |
| 500 |
SkASSERT(result == VK_SUCCESS && fAcquireSemaphore != VK_NULL_HANDLE); |
| 501 |
|
| 502 |
// Acquire the next available presentable image's index. |
| 503 |
VkResult res = fAcquireNextImageKHR(fDevice, |
| 504 |
fSwapchain, |
| 505 |
/*timeout=*/UINT64_MAX, |
| 506 |
fAcquireSemaphore, |
| 507 |
/*VkFence=*/VK_NULL_HANDLE, |
| 508 |
&fCurrentImageIndex); |
| 509 |
if (VK_ERROR_SURFACE_LOST_KHR == res) { |
| 510 |
// TODO: Recreate fDeviceSurface using fCreateVkSurfaceFn, and then rebuild the swapchain |
| 511 |
GR_VK_CALL(fInterface, |
| 512 |
DestroySemaphore(fDevice, fAcquireSemaphore, /*pAllocator=*/nullptr)); |
| 513 |
return nullptr; |
| 514 |
} |
| 515 |
if (VK_ERROR_OUT_OF_DATE_KHR == res) { |
| 516 |
if (!this->createSwapchain(-1, -1)) { |
| 517 |
GR_VK_CALL(fInterface, |
| 518 |
DestroySemaphore(fDevice, fAcquireSemaphore, /*pAllocator=*/nullptr)); |
| 519 |
return nullptr; |
| 520 |
} |
| 521 |
|
| 522 |
res = fAcquireNextImageKHR(fDevice, |
| 523 |
fSwapchain, |
| 524 |
UINT64_MAX, |
| 525 |
fAcquireSemaphore, |
| 526 |
/*VkFence=*/VK_NULL_HANDLE, |
| 527 |
&fCurrentImageIndex); |
| 528 |
if (VK_SUCCESS != res) { |
| 529 |
GR_VK_CALL(fInterface, |
| 530 |
DestroySemaphore(fDevice, fAcquireSemaphore, /*pAllocator=*/nullptr)); |
| 531 |
return nullptr; |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
// Set up the SkSurface associated with the current backbuffer's image and return it. |
| 536 |
SkSurface* surface = fImages[fCurrentImageIndex].fSurface.get(); |
| 537 |
GrBackendSemaphore beSemaphore = GrBackendSemaphores::MakeVk(fAcquireSemaphore); |
| 538 |
surface->wait(/*numSemaphores=*/1, &beSemaphore); |
| 539 |
|
| 540 |
return sk_ref_sp(surface); |
| 541 |
} |
| 542 |
|
| 543 |
void VulkanWindowContext::onSwapBuffers() { |
| 544 |
// Submit the GPU work associated with the current backbuffer |
| 545 |
this->submitToGpu(); |
| 546 |
|
| 547 |
// Submit present operation to present queue |
| 548 |
auto renderCompletionSemaphore = &fImages[fCurrentImageIndex].fRenderCompletionSemaphore; |
| 549 |
const VkPresentInfoKHR presentInfo = {VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType |
| 550 |
nullptr, // pNext |
| 551 |
1, // waitSemaphoreCount |
| 552 |
renderCompletionSemaphore, // pWaitSemaphores |
| 553 |
1, // swapchainCount |
| 554 |
&fSwapchain, // pSwapchains |
| 555 |
&fCurrentImageIndex, // pImageIndices |
| 556 |
nullptr}; // pResults |
| 557 |
fQueuePresentKHR(fPresentQueue, &presentInfo); |
| 558 |
} |
| 559 |
|
| 560 |
} // namespace skwindow::internal |