2016-04-06 13:08:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/sk_app/VulkanWindowContext.h"
|
|
|
|
|
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "include/gpu/GrBackendSemaphore.h"
|
|
|
|
#include "include/gpu/GrBackendSurface.h"
|
|
|
|
#include "include/gpu/GrContext.h"
|
|
|
|
#include "src/core/SkAutoMalloc.h"
|
|
|
|
|
|
|
|
#include "include/gpu/vk/GrVkExtensions.h"
|
|
|
|
#include "include/gpu/vk/GrVkTypes.h"
|
|
|
|
#include "src/gpu/vk/GrVkImage.h"
|
|
|
|
#include "src/gpu/vk/GrVkUtil.h"
|
2016-04-06 13:08:59 +00:00
|
|
|
|
|
|
|
#ifdef VK_USE_PLATFORM_WIN32_KHR
|
|
|
|
// windows wants to define this as CreateSemaphoreA or CreateSemaphoreW
|
|
|
|
#undef CreateSemaphore
|
|
|
|
#endif
|
|
|
|
|
2018-07-02 20:15:37 +00:00
|
|
|
#define GET_PROC(F) f ## F = (PFN_vk ## F) fGetInstanceProcAddr(fInstance, "vk" #F)
|
|
|
|
#define GET_DEV_PROC(F) f ## F = (PFN_vk ## F) fGetDeviceProcAddr(fDevice, "vk" #F)
|
2016-04-21 18:46:23 +00:00
|
|
|
|
2016-05-05 19:32:03 +00:00
|
|
|
namespace sk_app {
|
|
|
|
|
2016-07-26 19:02:50 +00:00
|
|
|
VulkanWindowContext::VulkanWindowContext(const DisplayParams& params,
|
|
|
|
CreateVkSurfaceFn createVkSurface,
|
2017-11-10 15:03:05 +00:00
|
|
|
CanPresentFn canPresent,
|
|
|
|
PFN_vkGetInstanceProcAddr instProc,
|
|
|
|
PFN_vkGetDeviceProcAddr devProc)
|
2017-05-02 20:15:53 +00:00
|
|
|
: WindowContext(params)
|
|
|
|
, fCreateVkSurfaceFn(createVkSurface)
|
|
|
|
, fCanPresentFn(canPresent)
|
2016-05-20 13:01:06 +00:00
|
|
|
, fSurface(VK_NULL_HANDLE)
|
2016-05-05 19:32:03 +00:00
|
|
|
, fSwapchain(VK_NULL_HANDLE)
|
2016-05-20 13:01:06 +00:00
|
|
|
, fImages(nullptr)
|
|
|
|
, fImageLayouts(nullptr)
|
|
|
|
, fSurfaces(nullptr)
|
2016-05-05 19:32:03 +00:00
|
|
|
, fBackbuffers(nullptr) {
|
2017-11-10 15:03:05 +00:00
|
|
|
fGetInstanceProcAddr = instProc;
|
|
|
|
fGetDeviceProcAddr = devProc;
|
2017-05-02 20:15:53 +00:00
|
|
|
this->initializeContext();
|
|
|
|
}
|
2016-04-06 13:08:59 +00:00
|
|
|
|
2017-05-02 20:15:53 +00:00
|
|
|
void VulkanWindowContext::initializeContext() {
|
2016-04-06 13:08:59 +00:00
|
|
|
// any config code here (particularly for msaa)?
|
|
|
|
|
2018-08-01 13:19:45 +00:00
|
|
|
PFN_vkGetInstanceProcAddr getInstanceProc = fGetInstanceProcAddr;
|
|
|
|
PFN_vkGetDeviceProcAddr getDeviceProc = fGetDeviceProcAddr;
|
|
|
|
auto getProc = [getInstanceProc, getDeviceProc](const char* proc_name,
|
|
|
|
VkInstance instance, VkDevice device) {
|
|
|
|
if (device != VK_NULL_HANDLE) {
|
|
|
|
return getDeviceProc(device, proc_name);
|
|
|
|
}
|
|
|
|
return getInstanceProc(instance, proc_name);
|
|
|
|
};
|
2018-07-02 20:15:37 +00:00
|
|
|
GrVkBackendContext backendContext;
|
2018-08-01 17:25:41 +00:00
|
|
|
GrVkExtensions extensions;
|
2018-08-08 13:23:18 +00:00
|
|
|
VkPhysicalDeviceFeatures2 features;
|
|
|
|
if (!sk_gpu_test::CreateVkBackendContext(getProc, &backendContext, &extensions, &features,
|
|
|
|
&fDebugCallback, &fPresentQueueIndex, fCanPresentFn)) {
|
|
|
|
sk_gpu_test::FreeVulkanFeaturesStructs(&features);
|
2016-04-21 18:46:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-01 20:48:52 +00:00
|
|
|
if (!extensions.hasExtension(VK_KHR_SURFACE_EXTENSION_NAME, 25) ||
|
|
|
|
!extensions.hasExtension(VK_KHR_SWAPCHAIN_EXTENSION_NAME, 68)) {
|
2018-08-08 13:23:18 +00:00
|
|
|
sk_gpu_test::FreeVulkanFeaturesStructs(&features);
|
2018-07-02 20:15:37 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fInstance = backendContext.fInstance;
|
|
|
|
fPhysicalDevice = backendContext.fPhysicalDevice;
|
|
|
|
fDevice = backendContext.fDevice;
|
|
|
|
fGraphicsQueueIndex = backendContext.fGraphicsQueueIndex;
|
|
|
|
fGraphicsQueue = backendContext.fQueue;
|
2018-08-03 18:41:15 +00:00
|
|
|
|
|
|
|
PFN_vkGetPhysicalDeviceProperties localGetPhysicalDeviceProperties =
|
|
|
|
reinterpret_cast<PFN_vkGetPhysicalDeviceProperties>(
|
|
|
|
backendContext.fGetProc("vkGetPhysicalDeviceProperties",
|
|
|
|
backendContext.fInstance,
|
|
|
|
VK_NULL_HANDLE));
|
|
|
|
if (!localGetPhysicalDeviceProperties) {
|
2018-08-08 13:23:18 +00:00
|
|
|
sk_gpu_test::FreeVulkanFeaturesStructs(&features);
|
2018-08-03 18:41:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
VkPhysicalDeviceProperties physDeviceProperties;
|
|
|
|
localGetPhysicalDeviceProperties(backendContext.fPhysicalDevice, &physDeviceProperties);
|
|
|
|
uint32_t physDevVersion = physDeviceProperties.apiVersion;
|
|
|
|
|
2018-07-12 14:02:37 +00:00
|
|
|
fInterface.reset(new GrVkInterface(backendContext.fGetProc, fInstance, fDevice,
|
2018-08-03 18:41:15 +00:00
|
|
|
backendContext.fInstanceVersion, physDevVersion,
|
2018-08-01 17:25:41 +00:00
|
|
|
&extensions));
|
2018-07-02 20:15:37 +00:00
|
|
|
|
|
|
|
GET_PROC(DestroyInstance);
|
2018-08-01 20:48:52 +00:00
|
|
|
if (fDebugCallback != VK_NULL_HANDLE) {
|
|
|
|
GET_PROC(DestroyDebugReportCallbackEXT);
|
|
|
|
}
|
2016-04-21 18:46:23 +00:00
|
|
|
GET_PROC(DestroySurfaceKHR);
|
|
|
|
GET_PROC(GetPhysicalDeviceSurfaceSupportKHR);
|
|
|
|
GET_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
|
|
|
|
GET_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
|
|
|
|
GET_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
|
2018-07-02 20:15:37 +00:00
|
|
|
GET_DEV_PROC(DeviceWaitIdle);
|
|
|
|
GET_DEV_PROC(QueueWaitIdle);
|
|
|
|
GET_DEV_PROC(DestroyDevice);
|
2016-04-21 18:46:23 +00:00
|
|
|
GET_DEV_PROC(CreateSwapchainKHR);
|
|
|
|
GET_DEV_PROC(DestroySwapchainKHR);
|
|
|
|
GET_DEV_PROC(GetSwapchainImagesKHR);
|
|
|
|
GET_DEV_PROC(AcquireNextImageKHR);
|
|
|
|
GET_DEV_PROC(QueuePresentKHR);
|
2017-11-10 15:03:05 +00:00
|
|
|
GET_DEV_PROC(GetDeviceQueue);
|
2016-04-06 13:08:59 +00:00
|
|
|
|
2018-07-02 20:15:37 +00:00
|
|
|
fContext = GrContext::MakeVulkan(backendContext, fDisplayParams.fGrContextOptions);
|
2016-04-06 13:08:59 +00:00
|
|
|
|
2018-07-02 20:15:37 +00:00
|
|
|
fSurface = fCreateVkSurfaceFn(fInstance);
|
2016-04-06 13:08:59 +00:00
|
|
|
if (VK_NULL_HANDLE == fSurface) {
|
2018-07-02 20:15:37 +00:00
|
|
|
this->destroyContext();
|
2018-08-08 13:23:18 +00:00
|
|
|
sk_gpu_test::FreeVulkanFeaturesStructs(&features);
|
2016-04-06 13:08:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
VkBool32 supported;
|
2018-07-02 20:15:37 +00:00
|
|
|
VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fPhysicalDevice, fPresentQueueIndex,
|
|
|
|
fSurface, &supported);
|
2016-04-06 13:08:59 +00:00
|
|
|
if (VK_SUCCESS != res) {
|
|
|
|
this->destroyContext();
|
2018-08-08 13:23:18 +00:00
|
|
|
sk_gpu_test::FreeVulkanFeaturesStructs(&features);
|
2016-04-06 13:08:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-02 20:15:53 +00:00
|
|
|
if (!this->createSwapchain(-1, -1, fDisplayParams)) {
|
2016-04-06 13:08:59 +00:00
|
|
|
this->destroyContext();
|
2018-08-08 13:23:18 +00:00
|
|
|
sk_gpu_test::FreeVulkanFeaturesStructs(&features);
|
2016-04-06 13:08:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create presentQueue
|
2018-07-02 20:15:37 +00:00
|
|
|
fGetDeviceQueue(fDevice, fPresentQueueIndex, 0, &fPresentQueue);
|
2018-08-08 13:23:18 +00:00
|
|
|
sk_gpu_test::FreeVulkanFeaturesStructs(&features);
|
2016-04-06 13:08:59 +00:00
|
|
|
}
|
|
|
|
|
2016-07-27 15:50:12 +00:00
|
|
|
bool VulkanWindowContext::createSwapchain(int width, int height,
|
2016-05-06 20:28:57 +00:00
|
|
|
const DisplayParams& params) {
|
2016-04-06 13:08:59 +00:00
|
|
|
// check for capabilities
|
|
|
|
VkSurfaceCapabilitiesKHR caps;
|
2018-07-02 20:15:37 +00:00
|
|
|
VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fPhysicalDevice, fSurface, &caps);
|
2016-04-06 13:08:59 +00:00
|
|
|
if (VK_SUCCESS != res) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t surfaceFormatCount;
|
2018-07-02 20:15:37 +00:00
|
|
|
res = fGetPhysicalDeviceSurfaceFormatsKHR(fPhysicalDevice, fSurface, &surfaceFormatCount,
|
|
|
|
nullptr);
|
2016-04-06 13:08:59 +00:00
|
|
|
if (VK_SUCCESS != res) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkAutoMalloc surfaceFormatAlloc(surfaceFormatCount * sizeof(VkSurfaceFormatKHR));
|
|
|
|
VkSurfaceFormatKHR* surfaceFormats = (VkSurfaceFormatKHR*)surfaceFormatAlloc.get();
|
2018-07-02 20:15:37 +00:00
|
|
|
res = fGetPhysicalDeviceSurfaceFormatsKHR(fPhysicalDevice, fSurface, &surfaceFormatCount,
|
|
|
|
surfaceFormats);
|
2016-04-06 13:08:59 +00:00
|
|
|
if (VK_SUCCESS != res) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t presentModeCount;
|
2018-07-02 20:15:37 +00:00
|
|
|
res = fGetPhysicalDeviceSurfacePresentModesKHR(fPhysicalDevice, fSurface, &presentModeCount,
|
|
|
|
nullptr);
|
2016-04-06 13:08:59 +00:00
|
|
|
if (VK_SUCCESS != res) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkAutoMalloc presentModeAlloc(presentModeCount * sizeof(VkPresentModeKHR));
|
|
|
|
VkPresentModeKHR* presentModes = (VkPresentModeKHR*)presentModeAlloc.get();
|
2018-07-02 20:15:37 +00:00
|
|
|
res = fGetPhysicalDeviceSurfacePresentModesKHR(fPhysicalDevice, fSurface, &presentModeCount,
|
|
|
|
presentModes);
|
2016-04-06 13:08:59 +00:00
|
|
|
if (VK_SUCCESS != res) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
VkExtent2D extent = caps.currentExtent;
|
|
|
|
// use the hints
|
|
|
|
if (extent.width == (uint32_t)-1) {
|
|
|
|
extent.width = width;
|
|
|
|
extent.height = height;
|
|
|
|
}
|
|
|
|
|
2016-04-06 19:08:51 +00:00
|
|
|
// clamp width; to protect us from broken hints
|
2016-04-06 13:08:59 +00:00
|
|
|
if (extent.width < caps.minImageExtent.width) {
|
|
|
|
extent.width = caps.minImageExtent.width;
|
|
|
|
} else if (extent.width > caps.maxImageExtent.width) {
|
|
|
|
extent.width = caps.maxImageExtent.width;
|
|
|
|
}
|
|
|
|
// clamp height
|
|
|
|
if (extent.height < caps.minImageExtent.height) {
|
|
|
|
extent.height = caps.minImageExtent.height;
|
|
|
|
} else if (extent.height > caps.maxImageExtent.height) {
|
|
|
|
extent.height = caps.maxImageExtent.height;
|
|
|
|
}
|
2016-05-23 20:13:36 +00:00
|
|
|
|
2016-04-06 13:08:59 +00:00
|
|
|
fWidth = (int)extent.width;
|
|
|
|
fHeight = (int)extent.height;
|
|
|
|
|
|
|
|
uint32_t imageCount = caps.minImageCount + 2;
|
|
|
|
if (caps.maxImageCount > 0 && imageCount > caps.maxImageCount) {
|
|
|
|
// Application must settle for fewer images than desired:
|
|
|
|
imageCount = caps.maxImageCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
|
|
|
|
VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
|
|
|
|
VK_IMAGE_USAGE_TRANSFER_DST_BIT;
|
|
|
|
SkASSERT((caps.supportedUsageFlags & usageFlags) == usageFlags);
|
|
|
|
SkASSERT(caps.supportedTransforms & caps.currentTransform);
|
|
|
|
SkASSERT(caps.supportedCompositeAlpha & (VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR |
|
|
|
|
VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR));
|
|
|
|
VkCompositeAlphaFlagBitsKHR composite_alpha =
|
|
|
|
(caps.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR) ?
|
|
|
|
VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR :
|
|
|
|
VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
|
|
|
|
2018-07-09 14:23:19 +00:00
|
|
|
// Pick our surface format.
|
2016-05-06 20:28:57 +00:00
|
|
|
VkFormat surfaceFormat = VK_FORMAT_UNDEFINED;
|
|
|
|
VkColorSpaceKHR colorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
|
|
|
|
for (uint32_t i = 0; i < surfaceFormatCount; ++i) {
|
2017-12-13 15:20:04 +00:00
|
|
|
VkFormat localFormat = surfaceFormats[i].format;
|
Dest color space no longer impacts mipmaps or texture sampling
PS5: Removes SkDestinationSurfaceColorMode, tracking of mipmap
mode on GrTexture, sRGB decode state per-texture. Because we
were often choosing sRGB configs for RGB color types, legacy
rendering would then be incorrect (too dark). So...
PS7: Stops ever using sRGB pixel configs when translating
image info or color type. Also removes a bunch of GrCaps bits
and a GrContextOption that are no longer relevant.
PS9: Adjusts surface creation unit test expectations, and
changes the raster rules accordingly.
At this point, sRGB configs are (obviously) going to be broken.
Locally, I ran 8888, gl, and the gbr- versions of both. Across
all GMs x configs, there are 13 diffs. 12 are GMs that create
surfaces with a color-space attached (and thus, the offscreen
is no longer getting sRGB pixel config). The only remainder
constructs an SkPictureImageGenerator, (with an attached color
space) and renders it to the gbr-gl canvas, which triggers a
a tagged surface inside the generator.
Bug: skia:
Change-Id: Ie5edfa157dd799f3121e8173fc4f97f6c8ed6789
Reviewed-on: https://skia-review.googlesource.com/131282
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: Mike Klein <mtklein@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
2018-06-01 16:25:08 +00:00
|
|
|
if (GrVkFormatIsSupported(localFormat)) {
|
2017-12-13 15:20:04 +00:00
|
|
|
surfaceFormat = localFormat;
|
2016-05-06 20:28:57 +00:00
|
|
|
colorSpace = surfaceFormats[i].colorSpace;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fDisplayParams = params;
|
2019-10-07 20:25:41 +00:00
|
|
|
fSampleCount = SkTMax(1, params.fMSAASampleCount);
|
2017-02-24 23:04:47 +00:00
|
|
|
fStencilBits = 8;
|
2016-05-06 20:28:57 +00:00
|
|
|
|
|
|
|
if (VK_FORMAT_UNDEFINED == surfaceFormat) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-27 18:42:21 +00:00
|
|
|
|
2017-12-19 18:15:02 +00:00
|
|
|
SkColorType colorType;
|
|
|
|
switch (surfaceFormat) {
|
|
|
|
case VK_FORMAT_R8G8B8A8_UNORM: // fall through
|
|
|
|
case VK_FORMAT_R8G8B8A8_SRGB:
|
|
|
|
colorType = kRGBA_8888_SkColorType;
|
|
|
|
break;
|
|
|
|
case VK_FORMAT_B8G8R8A8_UNORM: // fall through
|
|
|
|
colorType = kBGRA_8888_SkColorType;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-07 18:09:51 +00:00
|
|
|
// If mailbox mode is available, use it, as it is the lowest-latency non-
|
|
|
|
// tearing mode. If not, fall back to FIFO which is always available.
|
2016-04-06 13:08:59 +00:00
|
|
|
VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;
|
2019-03-25 17:42:33 +00:00
|
|
|
bool hasImmediate = false;
|
2016-04-06 13:08:59 +00:00
|
|
|
for (uint32_t i = 0; i < presentModeCount; ++i) {
|
2016-04-07 18:09:51 +00:00
|
|
|
// use mailbox
|
|
|
|
if (VK_PRESENT_MODE_MAILBOX_KHR == presentModes[i]) {
|
2019-03-25 17:42:33 +00:00
|
|
|
mode = VK_PRESENT_MODE_MAILBOX_KHR;
|
|
|
|
}
|
|
|
|
if (VK_PRESENT_MODE_IMMEDIATE_KHR == presentModes[i]) {
|
|
|
|
hasImmediate = true;
|
2016-04-06 13:08:59 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-25 17:42:33 +00:00
|
|
|
if (params.fDisableVsync && hasImmediate) {
|
|
|
|
mode = VK_PRESENT_MODE_IMMEDIATE_KHR;
|
|
|
|
}
|
2016-04-06 13:08:59 +00:00
|
|
|
|
|
|
|
VkSwapchainCreateInfoKHR swapchainCreateInfo;
|
|
|
|
memset(&swapchainCreateInfo, 0, sizeof(VkSwapchainCreateInfoKHR));
|
|
|
|
swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
|
|
|
|
swapchainCreateInfo.surface = fSurface;
|
|
|
|
swapchainCreateInfo.minImageCount = imageCount;
|
2016-04-27 18:42:21 +00:00
|
|
|
swapchainCreateInfo.imageFormat = surfaceFormat;
|
|
|
|
swapchainCreateInfo.imageColorSpace = colorSpace;
|
2016-04-06 13:08:59 +00:00
|
|
|
swapchainCreateInfo.imageExtent = extent;
|
|
|
|
swapchainCreateInfo.imageArrayLayers = 1;
|
|
|
|
swapchainCreateInfo.imageUsage = usageFlags;
|
|
|
|
|
2018-07-02 20:15:37 +00:00
|
|
|
uint32_t queueFamilies[] = { fGraphicsQueueIndex, fPresentQueueIndex };
|
|
|
|
if (fGraphicsQueueIndex != fPresentQueueIndex) {
|
2016-04-06 13:08:59 +00:00
|
|
|
swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
|
|
|
|
swapchainCreateInfo.queueFamilyIndexCount = 2;
|
|
|
|
swapchainCreateInfo.pQueueFamilyIndices = queueFamilies;
|
|
|
|
} else {
|
|
|
|
swapchainCreateInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
|
|
swapchainCreateInfo.queueFamilyIndexCount = 0;
|
|
|
|
swapchainCreateInfo.pQueueFamilyIndices = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-10-06 17:57:57 +00:00
|
|
|
swapchainCreateInfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
|
2016-04-06 13:08:59 +00:00
|
|
|
swapchainCreateInfo.compositeAlpha = composite_alpha;
|
|
|
|
swapchainCreateInfo.presentMode = mode;
|
|
|
|
swapchainCreateInfo.clipped = true;
|
|
|
|
swapchainCreateInfo.oldSwapchain = fSwapchain;
|
|
|
|
|
2018-07-02 20:15:37 +00:00
|
|
|
res = fCreateSwapchainKHR(fDevice, &swapchainCreateInfo, nullptr, &fSwapchain);
|
2016-04-06 13:08:59 +00:00
|
|
|
if (VK_SUCCESS != res) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// destroy the old swapchain
|
|
|
|
if (swapchainCreateInfo.oldSwapchain != VK_NULL_HANDLE) {
|
2018-07-02 20:15:37 +00:00
|
|
|
fDeviceWaitIdle(fDevice);
|
2016-04-06 13:08:59 +00:00
|
|
|
|
|
|
|
this->destroyBuffers();
|
|
|
|
|
2018-07-02 20:15:37 +00:00
|
|
|
fDestroySwapchainKHR(fDevice, swapchainCreateInfo.oldSwapchain, nullptr);
|
2016-04-06 13:08:59 +00:00
|
|
|
}
|
|
|
|
|
2017-12-19 18:15:02 +00:00
|
|
|
this->createBuffers(swapchainCreateInfo.imageFormat, colorType);
|
2016-04-06 13:08:59 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-19 18:15:02 +00:00
|
|
|
void VulkanWindowContext::createBuffers(VkFormat format, SkColorType colorType) {
|
2018-07-02 20:15:37 +00:00
|
|
|
fGetSwapchainImagesKHR(fDevice, fSwapchain, &fImageCount, nullptr);
|
2016-04-06 13:08:59 +00:00
|
|
|
SkASSERT(fImageCount);
|
|
|
|
fImages = new VkImage[fImageCount];
|
2018-07-02 20:15:37 +00:00
|
|
|
fGetSwapchainImagesKHR(fDevice, fSwapchain, &fImageCount, fImages);
|
2016-04-06 13:08:59 +00:00
|
|
|
|
|
|
|
// set up initial image layouts and create surfaces
|
|
|
|
fImageLayouts = new VkImageLayout[fImageCount];
|
|
|
|
fSurfaces = new sk_sp<SkSurface>[fImageCount];
|
|
|
|
for (uint32_t i = 0; i < fImageCount; ++i) {
|
|
|
|
fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED;
|
|
|
|
|
2016-05-13 18:30:37 +00:00
|
|
|
GrVkImageInfo info;
|
2016-04-06 13:08:59 +00:00
|
|
|
info.fImage = fImages[i];
|
2018-02-26 18:29:37 +00:00
|
|
|
info.fAlloc = GrVkAlloc();
|
2016-08-31 18:03:50 +00:00
|
|
|
info.fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
2016-04-06 13:08:59 +00:00
|
|
|
info.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
|
2016-04-21 15:03:10 +00:00
|
|
|
info.fFormat = format;
|
2016-05-10 13:42:18 +00:00
|
|
|
info.fLevelCount = 1;
|
2019-03-01 20:24:35 +00:00
|
|
|
info.fCurrentQueueFamily = fPresentQueueIndex;
|
2017-04-20 18:07:46 +00:00
|
|
|
|
2019-07-09 14:43:59 +00:00
|
|
|
if (fSampleCount == 1) {
|
|
|
|
GrBackendRenderTarget backendRT(fWidth, fHeight, fSampleCount, info);
|
|
|
|
|
|
|
|
fSurfaces[i] = SkSurface::MakeFromBackendRenderTarget(
|
|
|
|
fContext.get(), backendRT, kTopLeft_GrSurfaceOrigin, colorType,
|
|
|
|
fDisplayParams.fColorSpace, &fDisplayParams.fSurfaceProps);
|
|
|
|
} else {
|
|
|
|
GrBackendTexture backendTexture(fWidth, fHeight, info);
|
|
|
|
|
2019-10-07 20:25:41 +00:00
|
|
|
// We don't set the sampled usage bit on the swapchain so this can't be a GrTexture.
|
|
|
|
fSurfaces[i] = SkSurface::MakeFromBackendTextureAsRenderTarget(
|
2019-07-09 14:43:59 +00:00
|
|
|
fContext.get(), backendTexture, kTopLeft_GrSurfaceOrigin, fSampleCount,
|
|
|
|
colorType, fDisplayParams.fColorSpace, &fDisplayParams.fSurfaceProps);
|
|
|
|
|
|
|
|
}
|
2016-04-06 13:08:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// set up the backbuffers
|
|
|
|
VkSemaphoreCreateInfo semaphoreInfo;
|
|
|
|
memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo));
|
|
|
|
semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
|
|
|
semaphoreInfo.pNext = nullptr;
|
|
|
|
semaphoreInfo.flags = 0;
|
|
|
|
|
2016-10-27 20:37:17 +00:00
|
|
|
// we create one additional backbuffer structure here, because we want to
|
2016-04-06 13:08:59 +00:00
|
|
|
// give the command buffers they contain a chance to finish before we cycle back
|
|
|
|
fBackbuffers = new BackbufferInfo[fImageCount + 1];
|
|
|
|
for (uint32_t i = 0; i < fImageCount + 1; ++i) {
|
|
|
|
fBackbuffers[i].fImageIndex = -1;
|
2019-11-05 17:36:42 +00:00
|
|
|
SkDEBUGCODE(VkResult result = )GR_VK_CALL(fInterface,
|
|
|
|
CreateSemaphore(fDevice, &semaphoreInfo, nullptr,
|
|
|
|
&fBackbuffers[i].fRenderSemaphore));
|
|
|
|
SkASSERT(result == VK_SUCCESS);
|
2016-04-06 13:08:59 +00:00
|
|
|
}
|
|
|
|
fCurrentBackbufferIndex = fImageCount;
|
|
|
|
}
|
|
|
|
|
2016-05-05 19:32:03 +00:00
|
|
|
void VulkanWindowContext::destroyBuffers() {
|
2016-04-06 13:08:59 +00:00
|
|
|
|
|
|
|
if (fBackbuffers) {
|
|
|
|
for (uint32_t i = 0; i < fImageCount + 1; ++i) {
|
|
|
|
fBackbuffers[i].fImageIndex = -1;
|
2018-07-02 20:15:37 +00:00
|
|
|
GR_VK_CALL(fInterface,
|
|
|
|
DestroySemaphore(fDevice,
|
2016-04-06 13:08:59 +00:00
|
|
|
fBackbuffers[i].fRenderSemaphore,
|
|
|
|
nullptr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete[] fBackbuffers;
|
|
|
|
fBackbuffers = nullptr;
|
|
|
|
|
2016-05-20 13:01:06 +00:00
|
|
|
// Does this actually free the surfaces?
|
2016-04-06 13:08:59 +00:00
|
|
|
delete[] fSurfaces;
|
|
|
|
fSurfaces = nullptr;
|
|
|
|
delete[] fImageLayouts;
|
|
|
|
fImageLayouts = nullptr;
|
|
|
|
delete[] fImages;
|
|
|
|
fImages = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-05-05 19:32:03 +00:00
|
|
|
VulkanWindowContext::~VulkanWindowContext() {
|
2016-04-06 13:08:59 +00:00
|
|
|
this->destroyContext();
|
|
|
|
}
|
|
|
|
|
2016-05-05 19:32:03 +00:00
|
|
|
void VulkanWindowContext::destroyContext() {
|
2018-07-02 20:16:44 +00:00
|
|
|
if (this->isValid()) {
|
|
|
|
fQueueWaitIdle(fPresentQueue);
|
|
|
|
fDeviceWaitIdle(fDevice);
|
2016-04-06 13:08:59 +00:00
|
|
|
|
2018-07-02 20:16:44 +00:00
|
|
|
this->destroyBuffers();
|
2016-04-06 13:08:59 +00:00
|
|
|
|
2018-07-02 20:16:44 +00:00
|
|
|
if (VK_NULL_HANDLE != fSwapchain) {
|
|
|
|
fDestroySwapchainKHR(fDevice, fSwapchain, nullptr);
|
|
|
|
fSwapchain = VK_NULL_HANDLE;
|
|
|
|
}
|
2018-06-28 23:30:23 +00:00
|
|
|
|
2018-07-02 20:16:44 +00:00
|
|
|
if (VK_NULL_HANDLE != fSurface) {
|
|
|
|
fDestroySurfaceKHR(fInstance, fSurface, nullptr);
|
|
|
|
fSurface = VK_NULL_HANDLE;
|
|
|
|
}
|
2016-04-06 13:08:59 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 14:05:01 +00:00
|
|
|
fContext.reset();
|
2018-07-02 20:15:37 +00:00
|
|
|
fInterface.reset();
|
|
|
|
|
|
|
|
if (VK_NULL_HANDLE != fDevice) {
|
|
|
|
fDestroyDevice(fDevice, nullptr);
|
|
|
|
fDevice = VK_NULL_HANDLE;
|
|
|
|
}
|
2018-07-02 20:16:44 +00:00
|
|
|
|
|
|
|
#ifdef SK_ENABLE_VK_LAYERS
|
|
|
|
if (fDebugCallback != VK_NULL_HANDLE) {
|
2018-08-01 20:48:52 +00:00
|
|
|
fDestroyDebugReportCallbackEXT(fInstance, fDebugCallback, nullptr);
|
2018-07-02 20:16:44 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-07-02 20:15:37 +00:00
|
|
|
fPhysicalDevice = VK_NULL_HANDLE;
|
2016-04-06 13:08:59 +00:00
|
|
|
|
2018-07-02 20:15:37 +00:00
|
|
|
if (VK_NULL_HANDLE != fInstance) {
|
|
|
|
fDestroyInstance(fInstance, nullptr);
|
|
|
|
fInstance = VK_NULL_HANDLE;
|
|
|
|
}
|
2016-04-06 13:08:59 +00:00
|
|
|
}
|
|
|
|
|
2016-05-05 19:32:03 +00:00
|
|
|
VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer() {
|
2016-04-06 13:08:59 +00:00
|
|
|
SkASSERT(fBackbuffers);
|
|
|
|
|
|
|
|
++fCurrentBackbufferIndex;
|
2016-09-26 19:30:46 +00:00
|
|
|
if (fCurrentBackbufferIndex > fImageCount) {
|
2016-04-06 13:08:59 +00:00
|
|
|
fCurrentBackbufferIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
|
|
|
|
return backbuffer;
|
|
|
|
}
|
|
|
|
|
2016-05-20 13:01:06 +00:00
|
|
|
sk_sp<SkSurface> VulkanWindowContext::getBackbufferSurface() {
|
2016-04-06 13:08:59 +00:00
|
|
|
BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
|
|
|
|
SkASSERT(backbuffer);
|
|
|
|
|
|
|
|
// semaphores should be in unsignaled state
|
2019-02-25 16:36:53 +00:00
|
|
|
VkSemaphoreCreateInfo semaphoreInfo;
|
|
|
|
memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo));
|
|
|
|
semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
|
|
|
semaphoreInfo.pNext = nullptr;
|
|
|
|
semaphoreInfo.flags = 0;
|
|
|
|
VkSemaphore semaphore;
|
2019-11-05 17:36:42 +00:00
|
|
|
SkDEBUGCODE(VkResult result = )GR_VK_CALL(fInterface, CreateSemaphore(fDevice, &semaphoreInfo,
|
|
|
|
nullptr, &semaphore));
|
|
|
|
SkASSERT(result == VK_SUCCESS);
|
2019-02-25 16:36:53 +00:00
|
|
|
|
2016-04-06 13:08:59 +00:00
|
|
|
// acquire the image
|
2018-07-02 20:15:37 +00:00
|
|
|
VkResult res = fAcquireNextImageKHR(fDevice, fSwapchain, UINT64_MAX,
|
2019-02-25 16:36:53 +00:00
|
|
|
semaphore, VK_NULL_HANDLE,
|
2016-04-21 18:46:23 +00:00
|
|
|
&backbuffer->fImageIndex);
|
2016-04-06 13:08:59 +00:00
|
|
|
if (VK_ERROR_SURFACE_LOST_KHR == res) {
|
|
|
|
// need to figure out how to create a new vkSurface without the platformData*
|
2016-04-07 18:09:51 +00:00
|
|
|
// maybe use attach somehow? but need a Window
|
2019-02-25 16:36:53 +00:00
|
|
|
GR_VK_CALL(fInterface, DestroySemaphore(fDevice, semaphore, nullptr));
|
2016-04-06 13:08:59 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2016-04-07 18:09:51 +00:00
|
|
|
if (VK_ERROR_OUT_OF_DATE_KHR == res) {
|
2016-04-06 13:08:59 +00:00
|
|
|
// tear swapchain down and try again
|
2016-10-27 20:37:17 +00:00
|
|
|
if (!this->createSwapchain(-1, -1, fDisplayParams)) {
|
2019-02-25 16:36:53 +00:00
|
|
|
GR_VK_CALL(fInterface, DestroySemaphore(fDevice, semaphore, nullptr));
|
2016-04-06 13:08:59 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-01-05 18:50:49 +00:00
|
|
|
backbuffer = this->getAvailableBackbuffer();
|
2016-04-06 13:08:59 +00:00
|
|
|
|
|
|
|
// acquire the image
|
2018-07-02 20:15:37 +00:00
|
|
|
res = fAcquireNextImageKHR(fDevice, fSwapchain, UINT64_MAX,
|
2019-02-25 16:36:53 +00:00
|
|
|
semaphore, VK_NULL_HANDLE,
|
2016-04-21 18:46:23 +00:00
|
|
|
&backbuffer->fImageIndex);
|
2016-04-06 13:08:59 +00:00
|
|
|
|
|
|
|
if (VK_SUCCESS != res) {
|
2019-02-25 16:36:53 +00:00
|
|
|
GR_VK_CALL(fInterface, DestroySemaphore(fDevice, semaphore, nullptr));
|
2016-04-06 13:08:59 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-31 18:03:50 +00:00
|
|
|
SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
|
2018-04-11 14:08:06 +00:00
|
|
|
|
2019-02-25 16:36:53 +00:00
|
|
|
GrBackendSemaphore beSemaphore;
|
|
|
|
beSemaphore.initVulkan(semaphore);
|
|
|
|
|
|
|
|
surface->wait(1, &beSemaphore);
|
2016-08-31 18:03:50 +00:00
|
|
|
|
|
|
|
return sk_ref_sp(surface);
|
2016-04-06 13:08:59 +00:00
|
|
|
}
|
|
|
|
|
2016-05-05 19:32:03 +00:00
|
|
|
void VulkanWindowContext::swapBuffers() {
|
2016-04-06 13:08:59 +00:00
|
|
|
|
|
|
|
BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
|
2016-08-31 18:03:50 +00:00
|
|
|
SkSurface* surface = fSurfaces[backbuffer->fImageIndex].get();
|
2018-04-11 14:08:06 +00:00
|
|
|
|
2019-03-01 20:24:35 +00:00
|
|
|
GrBackendSemaphore beSemaphore;
|
|
|
|
beSemaphore.initVulkan(backbuffer->fRenderSemaphore);
|
|
|
|
|
2019-04-17 19:26:11 +00:00
|
|
|
GrFlushInfo info;
|
|
|
|
info.fNumSemaphores = 1;
|
|
|
|
info.fSignalSemaphores = &beSemaphore;
|
|
|
|
surface->flush(SkSurface::BackendSurfaceAccess::kPresent, info);
|
2016-04-06 13:08:59 +00:00
|
|
|
|
|
|
|
// Submit present operation to present queue
|
|
|
|
const VkPresentInfoKHR presentInfo =
|
|
|
|
{
|
|
|
|
VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, // sType
|
|
|
|
NULL, // pNext
|
|
|
|
1, // waitSemaphoreCount
|
|
|
|
&backbuffer->fRenderSemaphore, // pWaitSemaphores
|
|
|
|
1, // swapchainCount
|
|
|
|
&fSwapchain, // pSwapchains
|
|
|
|
&backbuffer->fImageIndex, // pImageIndices
|
|
|
|
NULL // pResults
|
|
|
|
};
|
|
|
|
|
2016-04-21 18:46:23 +00:00
|
|
|
fQueuePresentKHR(fPresentQueue, &presentInfo);
|
2016-04-06 13:08:59 +00:00
|
|
|
}
|
2016-05-05 19:32:03 +00:00
|
|
|
|
|
|
|
} //namespace sk_app
|