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