xref: /aosp_15_r20/external/skia/tools/window/unix/GraphiteNativeVulkanWindowContext_unix.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 #include "tools/window/unix/GraphiteNativeVulkanWindowContext_unix.h"
8*c8dee2aaSAndroid Build Coastguard Worker 
9*c8dee2aaSAndroid Build Coastguard Worker #include "tools/gpu/vk/VkTestUtils.h"
10*c8dee2aaSAndroid Build Coastguard Worker #include "tools/window/GraphiteNativeVulkanWindowContext.h"
11*c8dee2aaSAndroid Build Coastguard Worker #include "tools/window/unix/XlibWindowInfo.h"
12*c8dee2aaSAndroid Build Coastguard Worker 
13*c8dee2aaSAndroid Build Coastguard Worker #include <X11/Xlib-xcb.h>
14*c8dee2aaSAndroid Build Coastguard Worker 
15*c8dee2aaSAndroid Build Coastguard Worker namespace skwindow {
16*c8dee2aaSAndroid Build Coastguard Worker 
MakeGraphiteNativeVulkanForXlib(const XlibWindowInfo & info,std::unique_ptr<const DisplayParams> displayParams)17*c8dee2aaSAndroid Build Coastguard Worker std::unique_ptr<WindowContext> MakeGraphiteNativeVulkanForXlib(
18*c8dee2aaSAndroid Build Coastguard Worker         const XlibWindowInfo& info, std::unique_ptr<const DisplayParams> displayParams) {
19*c8dee2aaSAndroid Build Coastguard Worker     PFN_vkGetInstanceProcAddr instProc;
20*c8dee2aaSAndroid Build Coastguard Worker     if (!sk_gpu_test::LoadVkLibraryAndGetProcAddrFuncs(&instProc)) {
21*c8dee2aaSAndroid Build Coastguard Worker         SkDebugf("Could not load vulkan library\n");
22*c8dee2aaSAndroid Build Coastguard Worker         return nullptr;
23*c8dee2aaSAndroid Build Coastguard Worker     }
24*c8dee2aaSAndroid Build Coastguard Worker 
25*c8dee2aaSAndroid Build Coastguard Worker     auto createVkSurface = [&info, instProc](VkInstance instance) -> VkSurfaceKHR {
26*c8dee2aaSAndroid Build Coastguard Worker         static PFN_vkCreateXcbSurfaceKHR createXcbSurfaceKHR = nullptr;
27*c8dee2aaSAndroid Build Coastguard Worker         if (!createXcbSurfaceKHR) {
28*c8dee2aaSAndroid Build Coastguard Worker             createXcbSurfaceKHR =
29*c8dee2aaSAndroid Build Coastguard Worker                     (PFN_vkCreateXcbSurfaceKHR)instProc(instance, "vkCreateXcbSurfaceKHR");
30*c8dee2aaSAndroid Build Coastguard Worker         }
31*c8dee2aaSAndroid Build Coastguard Worker 
32*c8dee2aaSAndroid Build Coastguard Worker         VkSurfaceKHR surface;
33*c8dee2aaSAndroid Build Coastguard Worker 
34*c8dee2aaSAndroid Build Coastguard Worker         VkXcbSurfaceCreateInfoKHR surfaceCreateInfo;
35*c8dee2aaSAndroid Build Coastguard Worker         memset(&surfaceCreateInfo, 0, sizeof(VkXcbSurfaceCreateInfoKHR));
36*c8dee2aaSAndroid Build Coastguard Worker         surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
37*c8dee2aaSAndroid Build Coastguard Worker         surfaceCreateInfo.pNext = nullptr;
38*c8dee2aaSAndroid Build Coastguard Worker         surfaceCreateInfo.flags = 0;
39*c8dee2aaSAndroid Build Coastguard Worker         surfaceCreateInfo.connection = XGetXCBConnection(info.fDisplay);
40*c8dee2aaSAndroid Build Coastguard Worker         surfaceCreateInfo.window = info.fWindow;
41*c8dee2aaSAndroid Build Coastguard Worker 
42*c8dee2aaSAndroid Build Coastguard Worker         VkResult res = createXcbSurfaceKHR(instance, &surfaceCreateInfo, nullptr, &surface);
43*c8dee2aaSAndroid Build Coastguard Worker         if (res != VK_SUCCESS) {
44*c8dee2aaSAndroid Build Coastguard Worker             return VK_NULL_HANDLE;
45*c8dee2aaSAndroid Build Coastguard Worker         }
46*c8dee2aaSAndroid Build Coastguard Worker 
47*c8dee2aaSAndroid Build Coastguard Worker         return surface;
48*c8dee2aaSAndroid Build Coastguard Worker     };
49*c8dee2aaSAndroid Build Coastguard Worker 
50*c8dee2aaSAndroid Build Coastguard Worker     auto canPresent = [&info, instProc](VkInstance instance,
51*c8dee2aaSAndroid Build Coastguard Worker                                         VkPhysicalDevice physDev,
52*c8dee2aaSAndroid Build Coastguard Worker                                         uint32_t queueFamilyIndex) {
53*c8dee2aaSAndroid Build Coastguard Worker         static PFN_vkGetPhysicalDeviceXcbPresentationSupportKHR
54*c8dee2aaSAndroid Build Coastguard Worker                 getPhysicalDeviceXcbPresentationSupportKHR = nullptr;
55*c8dee2aaSAndroid Build Coastguard Worker         if (!getPhysicalDeviceXcbPresentationSupportKHR) {
56*c8dee2aaSAndroid Build Coastguard Worker             getPhysicalDeviceXcbPresentationSupportKHR =
57*c8dee2aaSAndroid Build Coastguard Worker                     (PFN_vkGetPhysicalDeviceXcbPresentationSupportKHR)instProc(
58*c8dee2aaSAndroid Build Coastguard Worker                             instance, "vkGetPhysicalDeviceXcbPresentationSupportKHR");
59*c8dee2aaSAndroid Build Coastguard Worker         }
60*c8dee2aaSAndroid Build Coastguard Worker 
61*c8dee2aaSAndroid Build Coastguard Worker         Display* display = info.fDisplay;
62*c8dee2aaSAndroid Build Coastguard Worker         VisualID visualID =
63*c8dee2aaSAndroid Build Coastguard Worker                 XVisualIDFromVisual(DefaultVisual(info.fDisplay, DefaultScreen(info.fDisplay)));
64*c8dee2aaSAndroid Build Coastguard Worker         VkBool32 check = getPhysicalDeviceXcbPresentationSupportKHR(
65*c8dee2aaSAndroid Build Coastguard Worker                 physDev, queueFamilyIndex, XGetXCBConnection(display), visualID);
66*c8dee2aaSAndroid Build Coastguard Worker         return (check != VK_FALSE);
67*c8dee2aaSAndroid Build Coastguard Worker     };
68*c8dee2aaSAndroid Build Coastguard Worker     std::unique_ptr<WindowContext> ctx(new internal::GraphiteVulkanWindowContext(
69*c8dee2aaSAndroid Build Coastguard Worker             std::move(displayParams), createVkSurface, canPresent, instProc));
70*c8dee2aaSAndroid Build Coastguard Worker     if (!ctx->isValid()) {
71*c8dee2aaSAndroid Build Coastguard Worker         return nullptr;
72*c8dee2aaSAndroid Build Coastguard Worker     }
73*c8dee2aaSAndroid Build Coastguard Worker     return ctx;
74*c8dee2aaSAndroid Build Coastguard Worker }
75*c8dee2aaSAndroid Build Coastguard Worker 
76*c8dee2aaSAndroid Build Coastguard Worker }  // namespace skwindow
77