vulkan/wsi/x11: add sent image counter
[mesa.git] / src / vulkan / device-select-layer / device_select_x11.c
1 /*
2 * Copyright © 2019 Red Hat
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /* connect to an X server and work out the default device. */
25
26 #include <xcb/xcb.h>
27 #include <xcb/dri3.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <fcntl.h>
31 #include <xf86drm.h>
32
33 #include "device_select.h"
34 static int
35 ds_dri3_open(xcb_connection_t *conn,
36 xcb_window_t root,
37 uint32_t provider)
38 {
39 xcb_dri3_open_cookie_t cookie;
40 xcb_dri3_open_reply_t *reply;
41 int fd;
42
43 cookie = xcb_dri3_open(conn,
44 root,
45 provider);
46
47 reply = xcb_dri3_open_reply(conn, cookie, NULL);
48 if (!reply)
49 return -1;
50
51 if (reply->nfd != 1) {
52 free(reply);
53 return -1;
54 }
55
56 fd = xcb_dri3_open_reply_fds(conn, reply)[0];
57 free(reply);
58 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
59
60 return fd;
61 }
62
63 int device_select_find_xcb_pci_default(struct device_pci_info *devices, uint32_t device_count)
64 {
65 const xcb_setup_t *setup;
66 xcb_screen_iterator_t iter;
67 int scrn;
68 xcb_connection_t *conn;
69 int default_idx = -1;
70 conn = xcb_connect(NULL, &scrn);
71 if (!conn)
72 return -1;
73
74 xcb_query_extension_cookie_t dri3_cookie;
75 xcb_query_extension_reply_t *dri3_reply;
76
77 dri3_cookie = xcb_query_extension(conn, 4, "DRI3");
78 dri3_reply = xcb_query_extension_reply(conn, dri3_cookie, NULL);
79
80 if (!dri3_reply)
81 goto out;
82
83 if (dri3_reply->present == 0)
84 goto out;
85 setup = xcb_get_setup(conn);
86 iter = xcb_setup_roots_iterator(setup);
87
88 xcb_screen_t *screen = iter.data;
89
90 int dri3_fd = ds_dri3_open(conn, screen->root, 0);
91 if (dri3_fd == -1)
92 goto out;
93
94 drmDevicePtr xdev;
95 int ret = drmGetDevice2(dri3_fd, 0, &xdev);
96 if (ret < 0)
97 goto out;
98
99 for (unsigned i = 0; i < device_count; i++) {
100 if (devices[i].has_bus_info) {
101 if (xdev->businfo.pci->domain == devices[i].bus_info.domain &&
102 xdev->businfo.pci->bus == devices[i].bus_info.bus &&
103 xdev->businfo.pci->dev == devices[i].bus_info.dev &&
104 xdev->businfo.pci->func == devices[i].bus_info.func) {
105 default_idx = i;
106 }
107 } else {
108 if (xdev->deviceinfo.pci->vendor_id == devices[i].dev_info.vendor_id &&
109 xdev->deviceinfo.pci->device_id == devices[i].dev_info.device_id)
110 default_idx = i;
111 }
112 if (default_idx != -1)
113 break;
114 }
115 out:
116 xcb_disconnect(conn);
117 return default_idx;
118 }