• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright 2016 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 
9 #include "include/gpu/vk/GrVkVulkan.h"
10 
11 #include "src/gpu/vk/GrVkInterface.h"
12 #include "src/gpu/vk/GrVkUtil.h"
13 
14 #include "tools/gpu/vk/VkTestUtils.h"
15 
16 #include "tools/sk_app/VulkanWindowContext.h"
17 #include "tools/sk_app/unix/WindowContextFactory_unix.h"
18 
19 #include <X11/Xlib-xcb.h>
20 
21 namespace sk_app {
22 
23 namespace window_context_factory {
24 
MakeVulkanForXlib(const XlibWindowInfo & info,const DisplayParams & displayParams)25 std::unique_ptr<WindowContext> MakeVulkanForXlib(const XlibWindowInfo& info,
26                                                  const DisplayParams& displayParams) {
27     PFN_vkGetInstanceProcAddr instProc;
28     PFN_vkGetDeviceProcAddr devProc;
29     if (!sk_gpu_test::LoadVkLibraryAndGetProcAddrFuncs(&instProc, &devProc)) {
30         return nullptr;
31     }
32 
33     auto createVkSurface = [&info, instProc](VkInstance instance) -> VkSurfaceKHR {
34         static PFN_vkCreateXcbSurfaceKHR createXcbSurfaceKHR = nullptr;
35         if (!createXcbSurfaceKHR) {
36             createXcbSurfaceKHR =
37                     (PFN_vkCreateXcbSurfaceKHR) instProc(instance, "vkCreateXcbSurfaceKHR");
38         }
39 
40         VkSurfaceKHR surface;
41 
42         VkXcbSurfaceCreateInfoKHR surfaceCreateInfo;
43         memset(&surfaceCreateInfo, 0, sizeof(VkXcbSurfaceCreateInfoKHR));
44         surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
45         surfaceCreateInfo.pNext = nullptr;
46         surfaceCreateInfo.flags = 0;
47         surfaceCreateInfo.connection = XGetXCBConnection(info.fDisplay);
48         surfaceCreateInfo.window = info.fWindow;
49 
50         VkResult res = createXcbSurfaceKHR(instance, &surfaceCreateInfo, nullptr, &surface);
51         if (VK_SUCCESS != res) {
52             return VK_NULL_HANDLE;
53         }
54 
55         return surface;
56     };
57 
58     auto canPresent = [&info, instProc](VkInstance instance, VkPhysicalDevice physDev,
59                               uint32_t queueFamilyIndex) {
60         static PFN_vkGetPhysicalDeviceXcbPresentationSupportKHR
61                                                 getPhysicalDeviceXcbPresentationSupportKHR = nullptr;
62         if (!getPhysicalDeviceXcbPresentationSupportKHR) {
63             getPhysicalDeviceXcbPresentationSupportKHR =
64                 (PFN_vkGetPhysicalDeviceXcbPresentationSupportKHR)
65                     instProc(instance, "vkGetPhysicalDeviceXcbPresentationSupportKHR");
66         }
67 
68 
69         Display* display = info.fDisplay;
70         VisualID visualID = info.fVisualInfo->visualid;
71         VkBool32 check = getPhysicalDeviceXcbPresentationSupportKHR(physDev,
72                                                                     queueFamilyIndex,
73                                                                     XGetXCBConnection(display),
74                                                                     visualID);
75         return (VK_FALSE != check);
76     };
77     std::unique_ptr<WindowContext> ctx(
78             new VulkanWindowContext(displayParams, createVkSurface, canPresent, instProc, devProc));
79     if (!ctx->isValid()) {
80         return nullptr;
81     }
82     return ctx;
83 }
84 
85 }  // namespace window_context_factory
86 
87 }  // namespace sk_app
88