xref: /aosp_15_r20/external/skia/tools/window/win/GraphiteVulkanWindowContext_win.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1*c8dee2aaSAndroid Build Coastguard Worker /*
2*c8dee2aaSAndroid Build Coastguard Worker  * Copyright 2023 Google Inc.
3*c8dee2aaSAndroid Build Coastguard Worker  *
4*c8dee2aaSAndroid Build Coastguard Worker  * Use of this source code is governed by a BSD-style license that can be
5*c8dee2aaSAndroid Build Coastguard Worker  * found in the LICENSE file.
6*c8dee2aaSAndroid Build Coastguard Worker  */
7*c8dee2aaSAndroid Build Coastguard Worker 
8*c8dee2aaSAndroid Build Coastguard Worker #include "tools/window/win/WindowContextFactory_win.h"
9*c8dee2aaSAndroid Build Coastguard Worker 
10*c8dee2aaSAndroid Build Coastguard Worker #include "tools/sk_app/win/Window_win.h"
11*c8dee2aaSAndroid Build Coastguard Worker #include "tools/window/GraphiteNativeVulkanWindowContext.h"
12*c8dee2aaSAndroid Build Coastguard Worker 
13*c8dee2aaSAndroid Build Coastguard Worker #include "tools/gpu/vk/VkTestUtils.h"
14*c8dee2aaSAndroid Build Coastguard Worker 
15*c8dee2aaSAndroid Build Coastguard Worker #include <Windows.h>
16*c8dee2aaSAndroid Build Coastguard Worker 
17*c8dee2aaSAndroid Build Coastguard Worker namespace skwindow {
18*c8dee2aaSAndroid Build Coastguard Worker 
MakeGraphiteVulkanForWin(HWND hwnd,std::unique_ptr<const DisplayParams> params)19*c8dee2aaSAndroid Build Coastguard Worker std::unique_ptr<WindowContext> MakeGraphiteVulkanForWin(
20*c8dee2aaSAndroid Build Coastguard Worker         HWND hwnd, std::unique_ptr<const DisplayParams> params) {
21*c8dee2aaSAndroid Build Coastguard Worker     PFN_vkGetInstanceProcAddr instProc;
22*c8dee2aaSAndroid Build Coastguard Worker     if (!sk_gpu_test::LoadVkLibraryAndGetProcAddrFuncs(&instProc)) {
23*c8dee2aaSAndroid Build Coastguard Worker         return nullptr;
24*c8dee2aaSAndroid Build Coastguard Worker     }
25*c8dee2aaSAndroid Build Coastguard Worker 
26*c8dee2aaSAndroid Build Coastguard Worker     auto createVkSurface = [hwnd, instProc] (VkInstance instance) -> VkSurfaceKHR {
27*c8dee2aaSAndroid Build Coastguard Worker         static PFN_vkCreateWin32SurfaceKHR createWin32SurfaceKHR = nullptr;
28*c8dee2aaSAndroid Build Coastguard Worker         if (!createWin32SurfaceKHR) {
29*c8dee2aaSAndroid Build Coastguard Worker             createWin32SurfaceKHR = (PFN_vkCreateWin32SurfaceKHR)
30*c8dee2aaSAndroid Build Coastguard Worker                 instProc(instance, "vkCreateWin32SurfaceKHR");
31*c8dee2aaSAndroid Build Coastguard Worker         }
32*c8dee2aaSAndroid Build Coastguard Worker         HINSTANCE hinstance = GetModuleHandle(0);
33*c8dee2aaSAndroid Build Coastguard Worker         VkSurfaceKHR surface;
34*c8dee2aaSAndroid Build Coastguard Worker 
35*c8dee2aaSAndroid Build Coastguard Worker         VkWin32SurfaceCreateInfoKHR surfaceCreateInfo;
36*c8dee2aaSAndroid Build Coastguard Worker         memset(&surfaceCreateInfo, 0, sizeof(VkWin32SurfaceCreateInfoKHR));
37*c8dee2aaSAndroid Build Coastguard Worker         surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
38*c8dee2aaSAndroid Build Coastguard Worker         surfaceCreateInfo.pNext = nullptr;
39*c8dee2aaSAndroid Build Coastguard Worker         surfaceCreateInfo.flags = 0;
40*c8dee2aaSAndroid Build Coastguard Worker         surfaceCreateInfo.hinstance = hinstance;
41*c8dee2aaSAndroid Build Coastguard Worker         surfaceCreateInfo.hwnd = hwnd;
42*c8dee2aaSAndroid Build Coastguard Worker 
43*c8dee2aaSAndroid Build Coastguard Worker         VkResult res = createWin32SurfaceKHR(instance, &surfaceCreateInfo, nullptr, &surface);
44*c8dee2aaSAndroid Build Coastguard Worker         if (VK_SUCCESS != res) {
45*c8dee2aaSAndroid Build Coastguard Worker             return VK_NULL_HANDLE;
46*c8dee2aaSAndroid Build Coastguard Worker         }
47*c8dee2aaSAndroid Build Coastguard Worker 
48*c8dee2aaSAndroid Build Coastguard Worker         return surface;
49*c8dee2aaSAndroid Build Coastguard Worker     };
50*c8dee2aaSAndroid Build Coastguard Worker 
51*c8dee2aaSAndroid Build Coastguard Worker     auto canPresent = [instProc] (VkInstance instance, VkPhysicalDevice physDev,
52*c8dee2aaSAndroid Build Coastguard Worker                                   uint32_t queueFamilyIndex) {
53*c8dee2aaSAndroid Build Coastguard Worker         static PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR
54*c8dee2aaSAndroid Build Coastguard Worker                                             getPhysicalDeviceWin32PresentationSupportKHR = nullptr;
55*c8dee2aaSAndroid Build Coastguard Worker         if (!getPhysicalDeviceWin32PresentationSupportKHR) {
56*c8dee2aaSAndroid Build Coastguard Worker             getPhysicalDeviceWin32PresentationSupportKHR =
57*c8dee2aaSAndroid Build Coastguard Worker                 (PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR)
58*c8dee2aaSAndroid Build Coastguard Worker                     instProc(instance, "vkGetPhysicalDeviceWin32PresentationSupportKHR");
59*c8dee2aaSAndroid Build Coastguard Worker         }
60*c8dee2aaSAndroid Build Coastguard Worker 
61*c8dee2aaSAndroid Build Coastguard Worker         VkBool32 check = getPhysicalDeviceWin32PresentationSupportKHR(physDev, queueFamilyIndex);
62*c8dee2aaSAndroid Build Coastguard Worker         return (VK_FALSE != check);
63*c8dee2aaSAndroid Build Coastguard Worker     };
64*c8dee2aaSAndroid Build Coastguard Worker 
65*c8dee2aaSAndroid Build Coastguard Worker     std::unique_ptr<WindowContext> ctx(new internal::GraphiteVulkanWindowContext(
66*c8dee2aaSAndroid Build Coastguard Worker             std::move(params), createVkSurface, canPresent, instProc));
67*c8dee2aaSAndroid Build Coastguard Worker     if (!ctx->isValid()) {
68*c8dee2aaSAndroid Build Coastguard Worker         return nullptr;
69*c8dee2aaSAndroid Build Coastguard Worker     }
70*c8dee2aaSAndroid Build Coastguard Worker     return ctx;
71*c8dee2aaSAndroid Build Coastguard Worker }
72*c8dee2aaSAndroid Build Coastguard Worker 
73*c8dee2aaSAndroid Build Coastguard Worker }  // namespace skwindow
74