From 5e53d9637fb7ac6234293af781169b9276d37621 Mon Sep 17 00:00:00 2001 From: Fufu Fang Date: Thu, 11 Sep 2025 10:50:01 +0000 Subject: [PATCH] Scan DRM device nodes for the VK_KHR_display backend To specify a DRM device node for the VK_KHR_display backend, WSI_DISPLAY_DRI_DEV environment variable needs to be set. If WSI_DISPLAY_DRI_DEV is not set, the VK_KHR_display backend now scans all DRM device node, and uses the first node that has a display connected. Signed-off-by: Fufu Fang Change-Id: Idbcda60cf3b1656784e6d3b0547cc70e99f0fc52 --- wsi/display/drm_display.cpp | 42 +++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/wsi/display/drm_display.cpp b/wsi/display/drm_display.cpp index c53018a..4c6158a 100644 --- a/wsi/display/drm_display.cpp +++ b/wsi/display/drm_display.cpp @@ -34,14 +34,14 @@ #include #include #include +#include + namespace wsi { namespace display { -const std::string default_dri_device_name{ "/dev/dri/card0" }; - drm_display::drm_display(util::fd_owner drm_fd, int crtc_id, drm_connector_owner drm_connector, util::unique_ptr> supported_formats, util::unique_ptr display_modes, size_t num_display_modes, uint32_t max_width, @@ -355,12 +355,42 @@ std::optional &drm_display::get_display() std::call_once(flag, []() { const char *dri_device = std::getenv("WSI_DISPLAY_DRI_DEV"); - if (!dri_device) + if (dri_device) { - dri_device = default_dri_device_name.c_str(); + display = drm_display::make_display(util::allocator::get_generic(), dri_device); + if (!display.has_value()) + { + WSI_LOG_ERROR("Failed to open DRM device: %s", dri_device); + } + else + { + WSI_LOG_INFO("Using DRM device from WSI_DISPLAY_DRI_DEV: %s", dri_device); + } + } + else + { + const char *dri_dir = "/dev/dri"; + DIR *dir = opendir(dri_dir); + if (!dir) + { + return; + } + struct dirent *entry; + while ((entry = readdir(dir)) != nullptr) + { + if (strncmp(entry->d_name, "card", 4) == 0) + { + std::string path = std::string(dri_dir) + "/" + entry->d_name; + display = drm_display::make_display(util::allocator::get_generic(), path.c_str()); + if (display.has_value()) + { + WSI_LOG_INFO("Using DRM device: %s", path.c_str()); + break; + } + } + } + closedir(dir); } - - display = drm_display::make_display(util::allocator::get_generic(), dri_device); }); return display; }