26#ifdef VK_USE_PLATFORM_WIN32_KHR
31#define GET_PROC(F) f ## F = \
32 (PFN_vk ## F) backendContext.fGetProc("vk" #F, fInstance, VK_NULL_HANDLE)
33#define GET_DEV_PROC(F) f ## F = \
34 (PFN_vk ## F) backendContext.fGetProc("vk" #F, VK_NULL_HANDLE, fDevice)
38VulkanWindowContext::VulkanWindowContext(
const DisplayParams&
params,
39 CreateVkSurfaceFn createVkSurface,
40 CanPresentFn canPresent,
43 , fCreateVkSurfaceFn(std::move(createVkSurface))
44 , fCanPresentFn(std::move(canPresent))
48 , fImageLayouts(
nullptr)
50 , fBackbuffers(
nullptr) {
51 fGetInstanceProcAddr = instProc;
52 this->initializeContext();
55void VulkanWindowContext::initializeContext() {
63 if (!sk_gpu_test::CreateVkBackendContext(getInstanceProc, &backendContext, &
extensions,
64 &features, &fDebugCallback, &fPresentQueueIndex,
66 fDisplayParams.fCreateProtectedNativeBackend)) {
67 sk_gpu_test::FreeVulkanFeaturesStructs(&features);
73 sk_gpu_test::FreeVulkanFeaturesStructs(&features);
79 fDevice = backendContext.
fDevice;
81 fGraphicsQueue = backendContext.
fQueue;
85 backendContext.
fGetProc(
"vkGetPhysicalDeviceProperties",
88 if (!localGetPhysicalDeviceProperties) {
89 sk_gpu_test::FreeVulkanFeaturesStructs(&features);
93 localGetPhysicalDeviceProperties(backendContext.
fPhysicalDevice, &physDeviceProperties);
94 uint32_t physDevVersion = physDeviceProperties.
apiVersion;
105 GET_PROC(DestroyDebugReportCallbackEXT);
108 GET_PROC(GetPhysicalDeviceSurfaceSupportKHR);
109 GET_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
110 GET_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
111 GET_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
124 fSurface = fCreateVkSurfaceFn(fInstance);
126 this->destroyContext();
127 sk_gpu_test::FreeVulkanFeaturesStructs(&features);
132 VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fPhysicalDevice, fPresentQueueIndex,
133 fSurface, &supported);
135 this->destroyContext();
136 sk_gpu_test::FreeVulkanFeaturesStructs(&features);
140 if (!this->createSwapchain(-1, -1, fDisplayParams)) {
141 this->destroyContext();
142 sk_gpu_test::FreeVulkanFeaturesStructs(&features);
147 fGetDeviceQueue(fDevice, fPresentQueueIndex, 0, &fPresentQueue);
148 sk_gpu_test::FreeVulkanFeaturesStructs(&features);
151bool VulkanWindowContext::createSwapchain(
int width,
int height,
152 const DisplayParams&
params) {
155 VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fPhysicalDevice, fSurface, &caps);
160 uint32_t surfaceFormatCount;
161 res = fGetPhysicalDeviceSurfaceFormatsKHR(fPhysicalDevice, fSurface, &surfaceFormatCount,
169 res = fGetPhysicalDeviceSurfaceFormatsKHR(fPhysicalDevice, fSurface, &surfaceFormatCount,
175 uint32_t presentModeCount;
176 res = fGetPhysicalDeviceSurfacePresentModesKHR(fPhysicalDevice, fSurface, &presentModeCount,
184 res = fGetPhysicalDeviceSurfacePresentModesKHR(fPhysicalDevice, fSurface, &presentModeCount,
192 if (extent.
width == (uint32_t)-1) {
211 fHeight = (
int)extent.
height;
240 for (uint32_t
i = 0;
i < surfaceFormatCount; ++
i) {
243 surfaceFormat = localFormat;
257 switch (surfaceFormat) {
272 bool hasImmediate =
false;
273 for (uint32_t
i = 0;
i < presentModeCount; ++
i) {
282 if (
params.fDisableVsync && hasImmediate) {
289 swapchainCreateInfo.
flags = fDisplayParams.fCreateProtectedNativeBackend
292 swapchainCreateInfo.
surface = fSurface;
300 uint32_t queueFamilies[] = { fGraphicsQueueIndex, fPresentQueueIndex };
301 if (fGraphicsQueueIndex != fPresentQueueIndex) {
314 swapchainCreateInfo.
clipped =
true;
317 res = fCreateSwapchainKHR(fDevice, &swapchainCreateInfo,
nullptr, &fSwapchain);
324 fDeviceWaitIdle(fDevice);
326 this->destroyBuffers();
328 fDestroySwapchainKHR(fDevice, swapchainCreateInfo.
oldSwapchain,
nullptr);
333 fDeviceWaitIdle(fDevice);
335 this->destroyBuffers();
337 fDestroySwapchainKHR(fDevice, swapchainCreateInfo.
oldSwapchain,
nullptr);
347 fGetSwapchainImagesKHR(fDevice, fSwapchain, &fImageCount,
nullptr);
349 fImages =
new VkImage[fImageCount];
350 fGetSwapchainImagesKHR(fDevice, fSwapchain, &fImageCount, fImages);
355 for (uint32_t
i = 0;
i < fImageCount; ++
i) {
359 info.fImage = fImages[
i];
364 info.fImageUsageFlags = usageFlags;
365 info.fLevelCount = 1;
366 info.fCurrentQueueFamily = fPresentQueueIndex;
368 info.fSharingMode = sharingMode;
375 fDisplayParams.fMSAASampleCount,
377 fDisplayParams.fColorSpace,
378 &fDisplayParams.fSurfaceProps);
380 if (fDisplayParams.fMSAASampleCount > 1) {
383 info.fSampleCount = fSampleCount;
389 fDisplayParams.fColorSpace,
390 &fDisplayParams.fSurfaceProps);
401 semaphoreInfo.
pNext =
nullptr;
402 semaphoreInfo.
flags = 0;
406 fBackbuffers =
new BackbufferInfo[fImageCount + 1];
407 for (uint32_t
i = 0;
i < fImageCount + 1; ++
i) {
408 fBackbuffers[
i].fImageIndex = -1;
410 CreateSemaphore(fDevice, &semaphoreInfo,
nullptr,
411 &fBackbuffers[
i].fRenderSemaphore));
414 fCurrentBackbufferIndex = fImageCount;
418void VulkanWindowContext::destroyBuffers() {
421 for (uint32_t
i = 0;
i < fImageCount + 1; ++
i) {
422 fBackbuffers[
i].fImageIndex = -1;
424 DestroySemaphore(fDevice,
425 fBackbuffers[
i].fRenderSemaphore,
430 delete[] fBackbuffers;
431 fBackbuffers =
nullptr;
436 delete[] fImageLayouts;
437 fImageLayouts =
nullptr;
442VulkanWindowContext::~VulkanWindowContext() {
443 this->destroyContext();
446void VulkanWindowContext::destroyContext() {
447 if (this->isValid()) {
448 fQueueWaitIdle(fPresentQueue);
449 fDeviceWaitIdle(fDevice);
451 this->destroyBuffers();
454 fDestroySwapchainKHR(fDevice, fSwapchain,
nullptr);
459 fDestroySurfaceKHR(fInstance, fSurface,
nullptr);
469 fDestroyDevice(fDevice,
nullptr);
473#ifdef SK_ENABLE_VK_LAYERS
475 fDestroyDebugReportCallbackEXT(fInstance, fDebugCallback,
nullptr);
482 fDestroyInstance(fInstance,
nullptr);
487VulkanWindowContext::BackbufferInfo* VulkanWindowContext::getAvailableBackbuffer() {
490 ++fCurrentBackbufferIndex;
491 if (fCurrentBackbufferIndex > fImageCount) {
492 fCurrentBackbufferIndex = 0;
495 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
500 BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
507 semaphoreInfo.
pNext =
nullptr;
508 semaphoreInfo.
flags = 0;
509 VkSemaphore semaphore;
511 nullptr, &semaphore));
515 VkResult res = fAcquireNextImageKHR(fDevice, fSwapchain, UINT64_MAX,
517 &backbuffer->fImageIndex);
521 GR_VK_CALL(fInterface, DestroySemaphore(fDevice, semaphore,
nullptr));
526 if (!this->createSwapchain(-1, -1, fDisplayParams)) {
527 GR_VK_CALL(fInterface, DestroySemaphore(fDevice, semaphore,
nullptr));
530 backbuffer = this->getAvailableBackbuffer();
533 res = fAcquireNextImageKHR(fDevice, fSwapchain, UINT64_MAX,
535 &backbuffer->fImageIndex);
538 GR_VK_CALL(fInterface, DestroySemaphore(fDevice, semaphore,
nullptr));
547 surface->wait(1, &beSemaphore);
552void VulkanWindowContext::onSwapBuffers() {
554 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
560 info.fNumSemaphores = 1;
561 info.fSignalSemaphores = &beSemaphore;
564 auto dContext =
surface->recordingContext()->asDirectContext();
574 &backbuffer->fRenderSemaphore,
577 &backbuffer->fImageIndex,
581 fQueuePresentKHR(fPresentQueue, &presentInfo);
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
@ kTopLeft_GrSurfaceOrigin
bool GrVkFormatIsSupported(VkFormat format)
#define GR_VK_CALL(IFACE, X)
@ kBGRA_8888_SkColorType
pixel with 8 bits for blue, green, red, alpha; in 32-bit word
@ kRGBA_8888_SkColorType
pixel with 8 bits for red, green, blue, alpha; in 32-bit word
static SkColorType colorType(AImageDecoder *decoder, const AImageDecoderHeaderInfo *headerInfo)
sk_sp< T > sk_ref_sp(T *obj)
SkDEBUGCODE(SK_SPI) SkThreadID SkGetThreadID()
const EmbeddedViewParams * params
uint32_t uint32_t * format
static float max(float r, float g, float b)
SK_API GrBackendRenderTarget MakeVk(int width, int height, const GrVkImageInfo &)
SK_API GrBackendSemaphore MakeVk(VkSemaphore semaphore)
SK_API GrBackendTexture MakeVk(int width, int height, const GrVkImageInfo &, std::string_view label={})
SK_API sk_sp< GrDirectContext > MakeVulkan(const skgpu::VulkanBackendContext &, const GrContextOptions &)
SK_API sk_sp< SkSurface > WrapBackendRenderTarget(GrRecordingContext *context, const GrBackendRenderTarget &backendRenderTarget, GrSurfaceOrigin origin, SkColorType colorType, sk_sp< SkColorSpace > colorSpace, const SkSurfaceProps *surfaceProps, RenderTargetReleaseProc releaseProc=nullptr, ReleaseContext releaseContext=nullptr)
SK_API sk_sp< SkSurface > WrapBackendTexture(GrRecordingContext *context, const GrBackendTexture &backendTexture, GrSurfaceOrigin origin, int sampleCnt, SkColorType colorType, sk_sp< SkColorSpace > colorSpace, const SkSurfaceProps *surfaceProps, TextureReleaseProc textureReleaseProc=nullptr, ReleaseContext releaseContext=nullptr)
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
SK_API MutableTextureState MakeVulkan(VkImageLayout layout, uint32_t queueFamilyIndex)
VkSemaphoreCreateFlags flags
VkSurfaceTransformFlagBitsKHR currentTransform
VkCompositeAlphaFlagsKHR supportedCompositeAlpha
VkExtent2D maxImageExtent
VkExtent2D minImageExtent
VkSurfaceTransformFlagsKHR supportedTransforms
VkImageUsageFlags supportedUsageFlags
uint32_t imageArrayLayers
VkPresentModeKHR presentMode
VkImageUsageFlags imageUsage
VkSharingMode imageSharingMode
VkSwapchainCreateFlagsKHR flags
VkSwapchainKHR oldSwapchain
VkColorSpaceKHR imageColorSpace
uint32_t queueFamilyIndexCount
VkSurfaceTransformFlagBitsKHR preTransform
const uint32_t * pQueueFamilyIndices
VkCompositeAlphaFlagBitsKHR compositeAlpha
uint32_t fGraphicsQueueIndex
skgpu::VulkanGetProc fGetProc
VkPhysicalDevice fPhysicalDevice
@ VK_SWAPCHAIN_CREATE_PROTECTED_BIT_KHR
@ VK_IMAGE_LAYOUT_PRESENT_SRC_KHR
@ VK_IMAGE_LAYOUT_UNDEFINED
@ VK_SHARING_MODE_CONCURRENT
@ VK_SHARING_MODE_EXCLUSIVE
VkFlags VkImageUsageFlags
@ VK_IMAGE_TILING_OPTIMAL
#define VK_KHR_SURFACE_EXTENSION_NAME
void(VKAPI_PTR * PFN_vkGetPhysicalDeviceProperties)(VkPhysicalDevice physicalDevice, VkPhysicalDeviceProperties *pProperties)
@ VK_IMAGE_USAGE_TRANSFER_DST_BIT
@ VK_IMAGE_USAGE_SAMPLED_BIT
@ VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT
@ VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
@ VK_IMAGE_USAGE_TRANSFER_SRC_BIT
VkCompositeAlphaFlagBitsKHR
@ VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR
@ VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR
@ VK_ERROR_OUT_OF_DATE_KHR
@ VK_ERROR_SURFACE_LOST_KHR
@ VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR
@ VK_PRESENT_MODE_IMMEDIATE_KHR
@ VK_PRESENT_MODE_MAILBOX_KHR
@ VK_PRESENT_MODE_FIFO_KHR
@ VK_FORMAT_R8G8B8A8_SRGB
@ VK_FORMAT_B8G8R8A8_UNORM
@ VK_FORMAT_R8G8B8A8_UNORM
@ VK_COLORSPACE_SRGB_NONLINEAR_KHR
#define VK_KHR_SWAPCHAIN_EXTENSION_NAME
PFN_vkVoidFunction(VKAPI_PTR * PFN_vkGetInstanceProcAddr)(VkInstance instance, const char *pName)
@ VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO
@ VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR
@ VK_STRUCTURE_TYPE_PRESENT_INFO_KHR