Skip to content

Craft Anything: understand how any software works, and craft your own.

Rust

Ignore switched-off screens and shared PCI ids when picking the Linux GPU (#671)

Thân Minh Nghĩa2e0d0813e0b4
diff --git a/apps/craft-anything/src/linux_gpu.rs b/apps/craft-anything/src/linux_gpu.rs
index 4dbf29bd..74907187 100644
--- a/apps/craft-anything/src/linux_gpu.rs
+++ b/apps/craft-anything/src/linux_gpu.rs
@@ -76,13 +76,16 @@ pub(crate) fn choose(candidates: &[Candidate], cards: &[DisplayCard], asked: &As
}
/// Whether `card` is the GPU behind `adapter`: by PCI address when the driver gives one, else by
-/// PCI ids; GL gives only a vendor, which counts only when no other card has that vendor.
+/// PCI ids, which count only when no other card has the same ids (two cards of one model); GL
+/// gives only a vendor, which counts only when no other card has that vendor.
fn drives(card: &DisplayCard, adapter: &Candidate, all: &[DisplayCard]) -> bool {
if !adapter.pci_slot.is_empty() && !card.pci_slot.is_empty() {
return adapter.pci_slot == card.pci_slot;
}
if adapter.device != 0 {
- return adapter.vendor == card.vendor && adapter.device == card.device;
+ return adapter.vendor == card.vendor
+ && adapter.device == card.device
+ && all.iter().filter(|c| c.vendor == card.vendor && c.device == card.device).count() == 1;
}
adapter.vendor != 0 && adapter.vendor == card.vendor && all.iter().filter(|c| c.vendor == card.vendor).count() == 1
}
@@ -251,4 +254,24 @@ mod tests {
];
assert_eq!(choose(&adapters, &[card(AMD, 1, true)], &Asked::default()), Some((1, Why::BuiltInPanel)));
}
+
+ /// #571's laptop with the lid closed and a monitor on the NVIDIA card: the switched-off panel
+ /// isn't read as one (`platform::display` skips it), so the card with the monitor draws.
+ #[test]
+ fn a_closed_laptop_draws_on_the_card_with_the_monitor() {
+ let cards = [card(NVIDIA, 1, false), card(AMD, 0, false)];
+ assert_eq!(choose(&laptop_adapters(), &cards, &Asked::default()), Some((1, Why::OnlyGpuWithDisplays)));
+ }
+
+ /// Two cards of one model have the same PCI ids: only the PCI address tells them apart. Without
+ /// one from the driver the display rule gives way to the old choice instead of guessing.
+ #[test]
+ fn identical_cards_are_told_apart_only_by_pci_address() {
+ const SECOND: (u32, u32, &str) = (0x10de, 0x25a2, "0000:02:00.0");
+ let cards = [card(NVIDIA, 0, false), card(SECOND, 1, false)];
+ let with_slots = [vulkan("NVIDIA A", NVIDIA, DeviceType::DiscreteGpu), vulkan("NVIDIA B", SECOND, DeviceType::DiscreteGpu)];
+ assert_eq!(choose(&with_slots, &cards, &Asked::default()), Some((1, Why::OnlyGpuWithDisplays)));
+ let without: Vec<Candidate> = with_slots.iter().cloned().map(|c| Candidate { pci_slot: String::new(), ..c }).collect();
+ assert_eq!(choose(&without, &cards, &Asked::default()), Some((0, Why::Fallback)));
+ }
}
diff --git a/crates/platform/src/display.rs b/crates/platform/src/display.rs
index 8ae73368..fd536d31 100644
--- a/crates/platform/src/display.rs
+++ b/crates/platform/src/display.rs
@@ -17,9 +17,10 @@ pub struct DisplayCard {
pub device: u32,
/// PCI address in the form `0000:05:00.0`, lowercase; empty when the system doesn't give one.
pub pci_slot: String,
- /// Connected displays of any kind, built-in panel included.
+ /// Connected displays that are switched on, of any kind, built-in panel included.
pub displays: u32,
- /// A connected built-in panel (eDP, LVDS or DSI): this card drives a laptop's own screen.
+ /// A connected, switched-on built-in panel (eDP, LVDS or DSI): this card drives a laptop's
+ /// own screen.
pub built_in_panel: bool,
}
@@ -47,8 +48,9 @@ pub fn display_cards() -> Vec<DisplayCard> {
/// [`display_cards`] read from a DRM folder laid out like `/sys/class/drm`: a `cardN` entry per
/// card, with the card's PCI ids in `cardN/device/uevent`, and a `cardN-<kind>-<n>` entry per
-/// connector, with `connected` or `disconnected` in its `status`. Cards without PCI ids (a
-/// firmware framebuffer) are left out.
+/// connector, with `connected` or `disconnected` in its `status` and, where the kernel gives it,
+/// `enabled` or `disabled` in its `enabled`. Cards without PCI ids (a firmware framebuffer) are
+/// left out.
pub fn display_cards_in(drm: &Path) -> Vec<DisplayCard> {
let Ok(entries) = std::fs::read_dir(drm) else {
return Vec::new();
@@ -64,8 +66,12 @@ pub fn display_cards_in(drm: &Path) -> Vec<DisplayCard> {
let prefix = format!("{card}-");
let mut out = DisplayCard { vendor, device, pci_slot, displays: 0, built_in_panel: false };
for connector in names.iter().filter_map(|n| n.strip_prefix(&prefix)) {
- let status = read_small(&drm.join(format!("{prefix}{connector}")).join("status")).unwrap_or_default();
- if status.trim() != "connected" {
+ let dir = drm.join(format!("{prefix}{connector}"));
+ let status = read_small(&dir.join("status")).unwrap_or_default();
+ // A panel can be connected but switched off (a closed lid with a monitor in use):
+ // `enabled` then reads `disabled`, and it drives nothing. No `enabled`: as before.
+ let enabled = read_small(&dir.join("enabled")).unwrap_or_default();
+ if status.trim() != "connected" || enabled.trim() == "disabled" {
continue;
}
out.displays = out.displays.saturating_add(1);
@@ -196,4 +202,18 @@ mod tests {
let d = drm(&[("card0", "1af4:1050", "0000:00:01.0")], &[("card0-Virtual-1", "unknown")]);
assert_eq!(display_cards_in(d.path()).first().map(|c| c.displays), Some(0));
}
+
+ /// A laptop with its lid closed and a monitor on the discrete card: the panel still reads
+ /// `connected` but is switched off, so it neither counts nor marks its card as the panel's.
+ #[test]
+ fn a_switched_off_panel_does_not_count() {
+ let d = drm(
+ &[("card0", "10de:25a2", "0000:01:00.0"), ("card1", "1002:1638", "0000:05:00.0")],
+ &[("card0-HDMI-A-1", "connected"), ("card1-eDP-1", "connected")],
+ );
+ fs::write(d.path().join("card0-HDMI-A-1").join("enabled"), "enabled\n").expect("enabled");
+ fs::write(d.path().join("card1-eDP-1").join("enabled"), "disabled\n").expect("enabled");
+ let cards = display_cards_in(d.path());
+ assert_eq!(cards.iter().map(|c| (c.vendor, c.displays, c.built_in_panel)).collect::<Vec<_>>(), vec![(0x10de, 1, false), (0x1002, 0, false)]);
+ }
}