Path: blob/21.2-virgl/src/vulkan/device-select-layer/device_select_x11.c
7130 views
/*1* Copyright © 2019 Red Hat2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223/* connect to an X server and work out the default device. */2425#include <xcb/xcb.h>26#include <xcb/dri3.h>27#include <unistd.h>28#include <stdlib.h>29#include <fcntl.h>30#include <xf86drm.h>3132#include "device_select.h"33static int34ds_dri3_open(xcb_connection_t *conn,35xcb_window_t root,36uint32_t provider)37{38xcb_dri3_open_cookie_t cookie;39xcb_dri3_open_reply_t *reply;40int fd;4142cookie = xcb_dri3_open(conn,43root,44provider);4546reply = xcb_dri3_open_reply(conn, cookie, NULL);47if (!reply)48return -1;4950if (reply->nfd != 1) {51free(reply);52return -1;53}5455fd = xcb_dri3_open_reply_fds(conn, reply)[0];56free(reply);57fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);5859return fd;60}6162int device_select_find_xcb_pci_default(struct device_pci_info *devices, uint32_t device_count)63{64const xcb_setup_t *setup;65xcb_screen_iterator_t iter;66int scrn;67xcb_connection_t *conn;68int default_idx = -1;69conn = xcb_connect(NULL, &scrn);70if (!conn)71return -1;7273xcb_query_extension_cookie_t dri3_cookie;74xcb_query_extension_reply_t *dri3_reply;7576dri3_cookie = xcb_query_extension(conn, 4, "DRI3");77dri3_reply = xcb_query_extension_reply(conn, dri3_cookie, NULL);7879if (!dri3_reply)80goto out;8182if (dri3_reply->present == 0)83goto out;84setup = xcb_get_setup(conn);85iter = xcb_setup_roots_iterator(setup);8687xcb_screen_t *screen = iter.data;8889int dri3_fd = ds_dri3_open(conn, screen->root, 0);90if (dri3_fd == -1)91goto out;9293drmDevicePtr xdev;94int ret = drmGetDevice2(dri3_fd, 0, &xdev);95if (ret < 0)96goto out;9798for (unsigned i = 0; i < device_count; i++) {99if (devices[i].has_bus_info) {100if (xdev->businfo.pci->domain == devices[i].bus_info.domain &&101xdev->businfo.pci->bus == devices[i].bus_info.bus &&102xdev->businfo.pci->dev == devices[i].bus_info.dev &&103xdev->businfo.pci->func == devices[i].bus_info.func) {104default_idx = i;105}106} else {107if (xdev->deviceinfo.pci->vendor_id == devices[i].dev_info.vendor_id &&108xdev->deviceinfo.pci->device_id == devices[i].dev_info.device_id)109default_idx = i;110}111if (default_idx != -1)112break;113}114out:115xcb_disconnect(conn);116return default_idx;117}118119120