32#ifdef VK_USE_PLATFORM_WIN32_KHR
37#define GET_PROC(F) f ## F = \
38 (PFN_vk ## F) backendContext.fGetProc("vk" #F, fInstance, VK_NULL_HANDLE)
39#define GET_DEV_PROC(F) f ## F = \
40 (PFN_vk ## F) backendContext.fGetProc("vk" #F, VK_NULL_HANDLE, fDevice)
44GraphiteVulkanWindowContext::GraphiteVulkanWindowContext(
const DisplayParams&
params,
45 CreateVkSurfaceFn createVkSurface,
46 CanPresentFn canPresent,
49 , fCreateVkSurfaceFn(std::move(createVkSurface))
50 , fCanPresentFn(std::move(canPresent))
54 , fImageLayouts(
nullptr)
56 , fBackbuffers(
nullptr) {
57 fGetInstanceProcAddr = instProc;
58 this->initializeContext();
61void GraphiteVulkanWindowContext::initializeContext() {
62 SkASSERT(!fGraphiteContext && !fGraphiteRecorder);
69 if (!sk_gpu_test::CreateVkBackendContext(getInstanceProc, &backendContext, &
extensions,
70 &features, &fDebugCallback, &fPresentQueueIndex,
72 fDisplayParams.fCreateProtectedNativeBackend)) {
73 sk_gpu_test::FreeVulkanFeaturesStructs(&features);
79 sk_gpu_test::FreeVulkanFeaturesStructs(&features);
85 fDevice = backendContext.
fDevice;
87 fGraphicsQueue = backendContext.
fQueue;
91 backendContext.
fGetProc(
"vkGetPhysicalDeviceProperties",
94 if (!localGetPhysicalDeviceProperties) {
95 sk_gpu_test::FreeVulkanFeaturesStructs(&features);
99 localGetPhysicalDeviceProperties(backendContext.
fPhysicalDevice, &physDeviceProperties);
100 uint32_t physDevVersion = physDeviceProperties.
apiVersion;
108 GET_PROC(DestroyDebugReportCallbackEXT);
111 GET_PROC(GetPhysicalDeviceSurfaceSupportKHR);
112 GET_PROC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
113 GET_PROC(GetPhysicalDeviceSurfaceFormatsKHR);
114 GET_PROC(GetPhysicalDeviceSurfacePresentModesKHR);
131 fGraphiteRecorder = fGraphiteContext->makeRecorder(ToolUtils::CreateTestingRecorderOptions());
133 fSurface = fCreateVkSurfaceFn(fInstance);
135 this->destroyContext();
136 sk_gpu_test::FreeVulkanFeaturesStructs(&features);
141 VkResult res = fGetPhysicalDeviceSurfaceSupportKHR(fPhysicalDevice, fPresentQueueIndex,
142 fSurface, &supported);
144 this->destroyContext();
145 sk_gpu_test::FreeVulkanFeaturesStructs(&features);
149 if (!this->createSwapchain(-1, -1, fDisplayParams)) {
150 this->destroyContext();
151 sk_gpu_test::FreeVulkanFeaturesStructs(&features);
156 fGetDeviceQueue(fDevice, fPresentQueueIndex, 0, &fPresentQueue);
157 sk_gpu_test::FreeVulkanFeaturesStructs(&features);
160bool GraphiteVulkanWindowContext::createSwapchain(
int width,
int height,
161 const DisplayParams&
params) {
164 VkResult res = fGetPhysicalDeviceSurfaceCapabilitiesKHR(fPhysicalDevice, fSurface, &caps);
169 uint32_t surfaceFormatCount;
170 res = fGetPhysicalDeviceSurfaceFormatsKHR(fPhysicalDevice, fSurface, &surfaceFormatCount,
178 res = fGetPhysicalDeviceSurfaceFormatsKHR(fPhysicalDevice, fSurface, &surfaceFormatCount,
184 uint32_t presentModeCount;
185 res = fGetPhysicalDeviceSurfacePresentModesKHR(fPhysicalDevice, fSurface, &presentModeCount,
193 res = fGetPhysicalDeviceSurfacePresentModesKHR(fPhysicalDevice, fSurface, &presentModeCount,
201 if (extent.
width == (uint32_t)-1) {
220 fHeight = (
int)extent.
height;
249 for (uint32_t
i = 0;
i < surfaceFormatCount; ++
i) {
252 surfaceFormat = localFormat;
266 switch (surfaceFormat) {
281 bool hasImmediate =
false;
282 for (uint32_t
i = 0;
i < presentModeCount; ++
i) {
291 if (
params.fDisableVsync && hasImmediate) {
298 swapchainCreateInfo.
flags = fDisplayParams.fCreateProtectedNativeBackend
301 swapchainCreateInfo.
surface = fSurface;
309 uint32_t queueFamilies[] = { fGraphicsQueueIndex, fPresentQueueIndex };
310 if (fGraphicsQueueIndex != fPresentQueueIndex) {
323 swapchainCreateInfo.
clipped =
true;
326 res = fCreateSwapchainKHR(fDevice, &swapchainCreateInfo,
nullptr, &fSwapchain);
333 fDeviceWaitIdle(fDevice);
335 this->destroyBuffers();
337 fDestroySwapchainKHR(fDevice, swapchainCreateInfo.
oldSwapchain,
nullptr);
343 fDeviceWaitIdle(fDevice);
345 this->destroyBuffers();
347 fDestroySwapchainKHR(fDevice, swapchainCreateInfo.
oldSwapchain,
nullptr);
358 fGetSwapchainImagesKHR(fDevice, fSwapchain, &fImageCount,
nullptr);
360 fImages =
new VkImage[fImageCount];
361 fGetSwapchainImagesKHR(fDevice, fSwapchain, &fImageCount, fImages);
366 for (uint32_t
i = 0;
i < fImageCount; ++
i) {
372 info.fImageUsageFlags = usageFlags;
373 info.fSharingMode = sharingMode;
387 fDisplayParams.fColorSpace,
388 &fDisplayParams.fSurfaceProps);
399 semaphoreInfo.
pNext =
nullptr;
400 semaphoreInfo.
flags = 0;
404 fBackbuffers =
new BackbufferInfo[fImageCount + 1];
405 for (uint32_t
i = 0;
i < fImageCount + 1; ++
i) {
406 fBackbuffers[
i].fImageIndex = -1;
412 fDevice, &semaphoreInfo,
nullptr, &fBackbuffers[
i].fRenderSemaphore));
414 fCurrentBackbufferIndex = fImageCount;
419void GraphiteVulkanWindowContext::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;
442GraphiteVulkanWindowContext::~GraphiteVulkanWindowContext() {
443 this->destroyContext();
446void GraphiteVulkanWindowContext::destroyContext() {
447 if (this->isValid()) {
448 fQueueWaitIdle(fPresentQueue);
449 fDeviceWaitIdle(fDevice);
452 VULKAN_CALL(fInterface, DestroySemaphore(fDevice, fWaitSemaphore,
nullptr));
456 this->destroyBuffers();
459 fDestroySwapchainKHR(fDevice, fSwapchain,
nullptr);
464 fDestroySurfaceKHR(fInstance, fSurface,
nullptr);
469 if (fGraphiteContext) {
470 fGraphiteRecorder.reset();
471 fGraphiteContext.reset();
476 fDestroyDevice(fDevice,
nullptr);
480#ifdef SK_ENABLE_VK_LAYERS
482 fDestroyDebugReportCallbackEXT(fInstance, fDebugCallback,
nullptr);
489 fDestroyInstance(fInstance,
nullptr);
494GraphiteVulkanWindowContext::BackbufferInfo* GraphiteVulkanWindowContext::getAvailableBackbuffer() {
497 ++fCurrentBackbufferIndex;
498 if (fCurrentBackbufferIndex > fImageCount) {
499 fCurrentBackbufferIndex = 0;
502 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
507 BackbufferInfo* backbuffer = this->getAvailableBackbuffer();
514 semaphoreInfo.
pNext =
nullptr;
515 semaphoreInfo.
flags = 0;
518 fInterface,
result, CreateSemaphore(fDevice, &semaphoreInfo,
nullptr, &fWaitSemaphore));
521 VkResult res = fAcquireNextImageKHR(fDevice, fSwapchain, UINT64_MAX,
523 &backbuffer->fImageIndex);
526 VULKAN_CALL(fInterface, DestroySemaphore(fDevice, fWaitSemaphore,
nullptr));
531 if (!this->createSwapchain(-1, -1, fDisplayParams)) {
532 VULKAN_CALL(fInterface, DestroySemaphore(fDevice, fWaitSemaphore,
nullptr));
535 backbuffer = this->getAvailableBackbuffer();
538 res = fAcquireNextImageKHR(fDevice, fSwapchain, UINT64_MAX,
540 &backbuffer->fImageIndex);
543 VULKAN_CALL(fInterface, DestroySemaphore(fDevice, fWaitSemaphore,
nullptr));
553void GraphiteVulkanWindowContext::onSwapBuffers() {
554 if (!fGraphiteContext) {
559 BackbufferInfo* backbuffer = fBackbuffers + fCurrentBackbufferIndex;
563 std::unique_ptr<skgpu::graphite::Recording> recording = fGraphiteRecorder->snap();
566 info.fRecording = recording.get();
569 info.fTargetSurface = fSurfaces[backbuffer->fImageIndex].get();
572 info.fTargetTextureState = &presentState;
576 info.fNumWaitSemaphores = 1;
577 info.fWaitSemaphores = &beWaitSemaphore;
579 info.fNumSignalSemaphores = 1;
580 info.fSignalSemaphores = &beSignalSemaphore;
583 struct FinishContext {
586 VkSemaphore waitSemaphore;
588 auto* finishContext =
new FinishContext{fInterface, fDevice, fWaitSemaphore};
592 const auto* context =
reinterpret_cast<const FinishContext*
>(c);
594 DestroySemaphore(context->device, context->waitSemaphore,
nullptr));
596 info.fFinishedContext = finishContext;
597 info.fFinishedProc = finishCallback;
599 fGraphiteContext->insertRecording(
info);
610 &backbuffer->fRenderSemaphore,
613 &backbuffer->fImageIndex,
617 fQueuePresentKHR(fPresentQueue, &presentInfo);
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
@ 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)
#define VULKAN_CALL(IFACE, X)
#define VULKAN_CALL_RESULT_NOCHECK(IFACE, RESULT, X)
const EmbeddedViewParams * params
uint32_t uint32_t * format
static float max(float r, float g, float b)
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)
SK_API std::unique_ptr< Context > MakeVulkan(const VulkanBackendContext &, const ContextOptions &)
bool vkFormatIsSupported(VkFormat format)
void * GpuFinishedContext
void(*)(GpuFinishedContext finishedContext, CallbackResult) GpuFinishedProc
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
bool fStoreContextRefInRecorder
ContextOptionsPriv * fOptionsPriv
@ 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_CREATE_PROTECTED_BIT
@ 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