diff --git a/apps/wordcraft/src/linux_gpu.rs b/apps/wordcraft/src/linux_gpu.rs new file mode 100644 index 0000000000..198708f366 --- /dev/null +++ b/apps/wordcraft/src/linux_gpu.rs @@ -0,0 +1,298 @@ +//! Linux: draw on the GPU that drives the screen. +//! +//! egui-wgpu picks the discrete GPU by default (`PowerPreference::HighPerformance`). On a laptop +//! whose integrated GPU drives the panel, every frame drawn on the discrete one is copied across +//! to it, and the discrete GPU leaves its idle state whenever the window redraws: on an Intel HD +//! 530 + NVIDIA Quadro M4000M laptop, from 7 W at P8 to over 20 W at P0/P1. Forcing the integrated +//! GPU instead breaks desktops whose screen is on the discrete card (storytold/pdfcraft#96), and a +//! desktop with a monitor on each card needs the discrete one (storytold/pdfcraft#445). So the +//! choice follows the connected displays, as in craft-anything (storytold/craft-anything#571): +//! +//! 1. `WGPU_ADAPTER_NAME` (part of the adapter's name) or `WGPU_POWER_PREF` (`high`, `low`, +//! `none`), when set, decide; +//! 2. a connected built-in panel: the GPU behind it; +//! 3. displays on only one GPU: that GPU; +//! 4. otherwise (displays on several GPUs, or nothing readable): the discrete GPU, as before. + +mod drm; + +use drm::DisplayCard; +use eframe::wgpu::{DeviceType, PowerPreference}; + +/// What we know of one adapter wgpu offers. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct Candidate { + pub name: String, + pub vendor: u32, + pub device: u32, + /// `0000:05:00.0`, lowercase; empty when the driver doesn't say (always, under GL). + pub pci_slot: String, + pub device_type: DeviceType, + pub gl: bool, +} + +/// What the person asked for through wgpu's environment variables. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub(crate) struct Asked { + pub adapter_name: Option, + pub power: Option, +} + +/// Why an adapter was chosen, for the log. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum Why { + AskedByName, + AskedPower, + BuiltInPanel, + OnlyGpuWithDisplays, + Fallback, +} + +/// The index of the adapter to draw with, and why; `None` when there is none. +pub(crate) fn choose(candidates: &[Candidate], cards: &[DisplayCard], asked: &Asked) -> Option<(usize, Why)> { + if let Some(want) = asked.adapter_name.as_deref().map(str::to_lowercase).filter(|w| !w.is_empty()) { + // As wgpu's own `WGPU_ADAPTER_NAME`: part of the name, any case. No match: the usual choice. + if let Some(i) = candidates.iter().position(|c| c.name.to_lowercase().contains(&want)) { + return Some((i, Why::AskedByName)); + } + log::warn!("WGPU_ADAPTER_NAME={want}: no graphics adapter has that in its name"); + } + if let Some(power) = asked.power { + return by_kind(candidates, power).map(|i| (i, Why::AskedPower)); + } + let panels: Vec<&DisplayCard> = cards.iter().filter(|c| c.built_in_panel).collect(); + let with_displays: Vec<&DisplayCard> = cards.iter().filter(|c| c.displays > 0).collect(); + let target = if !panels.is_empty() { + Some((panels, Why::BuiltInPanel)) + } else if with_displays.len() == 1 { + Some((with_displays, Why::OnlyGpuWithDisplays)) + } else { + None + }; + if let Some((wanted, why)) = target { + // Vulkan before GL for the same card; otherwise wgpu's order. + let found = candidates.iter().enumerate().filter(|(_, c)| wanted.iter().any(|card| drives(card, c, cards))).min_by_key(|(_, c)| c.gl); + if let Some((i, _)) = found { + return Some((i, why)); + } + } + by_kind(candidates, PowerPreference::HighPerformance).map(|i| (i, Why::Fallback)) +} + +/// Whether `card` is the GPU behind `adapter`: by PCI address when the driver gives one, else by +/// 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 + && 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 +} + +/// The first adapter in wgpu's own order for that power preference. +fn by_kind(candidates: &[Candidate], power: PowerPreference) -> Option { + let rank = |t: DeviceType| match (power, t) { + (PowerPreference::None, _) => 0, + (PowerPreference::LowPower, DeviceType::IntegratedGpu) | (PowerPreference::HighPerformance, DeviceType::DiscreteGpu) => 1, + (_, DeviceType::IntegratedGpu | DeviceType::DiscreteGpu) => 2, + (_, DeviceType::Other) => 3, + (_, DeviceType::VirtualGpu) => 4, + (_, DeviceType::Cpu) => 5, + }; + candidates.iter().enumerate().min_by_key(|(_, c)| rank(c.device_type)).map(|(i, _)| i) +} + +#[cfg(target_os = "linux")] +impl Candidate { + fn of(info: &eframe::wgpu::AdapterInfo) -> Self { + Candidate { + name: info.name.clone(), + vendor: info.vendor, + device: info.device, + pci_slot: info.device_pci_bus_id.to_ascii_lowercase(), + device_type: info.device_type, + gl: info.backend == eframe::wgpu::Backend::Gl, + } + } +} + +#[cfg(target_os = "linux")] +impl Asked { + fn from_env() -> Self { + Asked { adapter_name: std::env::var("WGPU_ADAPTER_NAME").ok(), power: PowerPreference::from_env() } + } +} + +/// The adapter that draws the window, among those that can draw to it. +#[cfg(target_os = "linux")] +pub(crate) fn pick_adapter(adapters: &[eframe::wgpu::Adapter], surface: Option<&eframe::wgpu::Surface<'_>>) -> Result { + let usable: Vec<&eframe::wgpu::Adapter> = adapters.iter().filter(|a| surface.is_none_or(|s| a.is_surface_supported(s))).collect(); + let candidates: Vec = usable.iter().map(|a| Candidate::of(&a.get_info())).collect(); + let cards = drm::display_cards(); + let picked = choose(&candidates, &cards, &Asked::from_env()).and_then(|(i, why)| Some((*usable.get(i)?, why))); + match picked { + Some((a, why)) => { + log::info!("drawing with {:?} ({why:?}); displays: {cards:?}", a.get_info()); + Ok(a.clone()) + } + None => Err(format!("None of the {} graphics adapters found can draw this window.", adapters.len())), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + const AMD: (u32, u32, &str) = (0x1002, 0x1638, "0000:05:00.0"); + const NVIDIA: (u32, u32, &str) = (0x10de, 0x25a2, "0000:01:00.0"); + const INTEL: (u32, u32, &str) = (0x8086, 0xa780, "0000:00:02.0"); + + fn vulkan(name: &str, (vendor, device, slot): (u32, u32, &str), device_type: DeviceType) -> Candidate { + Candidate { name: name.into(), vendor, device, pci_slot: slot.into(), device_type, gl: false } + } + + fn gl(name: &str, vendor: u32, device_type: DeviceType) -> Candidate { + Candidate { name: name.into(), vendor, device: 0, pci_slot: String::new(), device_type, gl: true } + } + + fn card((vendor, device, slot): (u32, u32, &str), displays: u32, built_in_panel: bool) -> DisplayCard { + DisplayCard { vendor, device, pci_slot: slot.into(), displays, built_in_panel } + } + + /// What wgpu offers on a laptop whose AMD iGPU drives the panel, in its order (Vulkan, then GL). + fn laptop_adapters() -> Vec { + vec![ + vulkan("AMD Radeon Graphics (RADV RENOIR)", AMD, DeviceType::IntegratedGpu), + vulkan("NVIDIA GeForce RTX 3050 Laptop GPU", NVIDIA, DeviceType::DiscreteGpu), + gl("AMD Radeon Graphics (radeonsi, renoir)", 0x1002, DeviceType::IntegratedGpu), + ] + } + + /// The iGPU drives the panel; egui-wgpu's default drew on the discrete GPU. + #[test] + fn a_laptop_draws_on_the_gpu_behind_its_panel() { + let cards = [card(NVIDIA, 0, false), card(AMD, 1, true)]; + assert_eq!(choose(&laptop_adapters(), &cards, &Asked::default()), Some((0, Why::BuiltInPanel))); + // The same with the discrete GPU listed first. + let mut swapped = laptop_adapters(); + swapped.swap(0, 1); + assert_eq!(choose(&swapped, &cards, &Asked::default()), Some((1, Why::BuiltInPanel))); + } + + /// The panel's GPU stays the choice with an external monitor on the discrete card as well. + #[test] + fn the_panel_wins_over_a_monitor_on_the_other_card() { + let cards = [card(NVIDIA, 1, false), card(AMD, 1, true)]; + assert_eq!(choose(&laptop_adapters(), &cards, &Asked::default()), Some((0, Why::BuiltInPanel))); + } + + /// A desktop with its monitor on the discrete card and the iGPU left on (a low-power default + /// draws on the iGPU there, and the window can't start: the case the panel rule must not break). + #[test] + fn a_desktop_draws_on_the_only_card_with_a_monitor() { + let adapters = + [vulkan("Intel(R) Graphics (RPL-S)", INTEL, DeviceType::IntegratedGpu), vulkan("NVIDIA GeForce RTX", NVIDIA, DeviceType::DiscreteGpu)]; + let cards = [card(INTEL, 0, false), card(NVIDIA, 1, false)]; + assert_eq!(choose(&adapters, &cards, &Asked::default()), Some((1, Why::OnlyGpuWithDisplays))); + // And on the iGPU when that's where the only monitor is. + let cards = [card(INTEL, 2, false), card(NVIDIA, 0, false)]; + assert_eq!(choose(&adapters, &cards, &Asked::default()), Some((0, Why::OnlyGpuWithDisplays))); + } + + /// A desktop with a monitor on each card: the discrete GPU, as before (an iGPU choice there + /// lost its surface as soon as it was set up). + #[test] + fn monitors_on_two_cards_keep_the_discrete_gpu() { + let adapters = + [vulkan("Intel(R) Graphics (RPL-S)", INTEL, DeviceType::IntegratedGpu), vulkan("NVIDIA GeForce RTX", NVIDIA, DeviceType::DiscreteGpu)]; + let cards = [card(INTEL, 1, false), card(NVIDIA, 1, false)]; + assert_eq!(choose(&adapters, &cards, &Asked::default()), Some((1, Why::Fallback))); + } + + /// Nothing readable (no `/sys`, or a card the drivers don't name): the discrete GPU, as before. + #[test] + fn without_display_facts_the_old_choice_stands() { + assert_eq!(choose(&laptop_adapters(), &[], &Asked::default()), Some((1, Why::Fallback))); + let unknown = [card((0x1234, 0x1, "0000:09:00.0"), 1, true)]; + assert_eq!(choose(&laptop_adapters(), &unknown, &Asked::default()), Some((1, Why::Fallback))); + assert_eq!(choose(&[], &[card(AMD, 1, true)], &Asked::default()), None); + } + + /// The environment variables still decide: prime-run style requests reach the NVIDIA card. + #[test] + fn wgpu_environment_variables_decide_when_set() { + let cards = [card(NVIDIA, 0, false), card(AMD, 1, true)]; + let asked = |name: Option<&str>, power: Option| Asked { adapter_name: name.map(Into::into), power }; + assert_eq!(choose(&laptop_adapters(), &cards, &asked(None, Some(PowerPreference::HighPerformance))), Some((1, Why::AskedPower))); + assert_eq!(choose(&laptop_adapters(), &cards, &asked(None, Some(PowerPreference::LowPower))), Some((0, Why::AskedPower))); + assert_eq!(choose(&laptop_adapters(), &cards, &asked(None, Some(PowerPreference::None))), Some((0, Why::AskedPower))); + assert_eq!(choose(&laptop_adapters(), &cards, &asked(Some("nvidia"), None)), Some((1, Why::AskedByName))); + assert_eq!(choose(&laptop_adapters(), &cards, &asked(Some("RadeonSI"), Some(PowerPreference::HighPerformance))), Some((2, Why::AskedByName))); + // A name nothing has: the usual choice, not a failure. + assert_eq!(choose(&laptop_adapters(), &cards, &asked(Some("matrox"), None)), Some((0, Why::BuiltInPanel))); + assert_eq!(choose(&laptop_adapters(), &cards, &asked(Some(""), None)), Some((0, Why::BuiltInPanel))); + } + + /// Under `WGPU_BACKEND=gl` there's no PCI address or device id: the vendor alone finds the + /// card, but only when no other card shares it. + #[test] + fn gl_adapters_are_matched_by_vendor_only_when_unambiguous() { + let adapters = + [gl("NVIDIA GeForce RTX 3050", 0x10de, DeviceType::Other), gl("AMD Radeon Graphics (radeonsi)", 0x1002, DeviceType::IntegratedGpu)]; + let cards = [card(NVIDIA, 0, false), card(AMD, 1, true)]; + assert_eq!(choose(&adapters, &cards, &Asked::default()), Some((1, Why::BuiltInPanel))); + // Two AMD cards: the vendor can't tell them apart, so the old choice stands. + let two_amd = [card((0x1002, 0x73df, "0000:03:00.0"), 0, false), card(AMD, 1, true)]; + let adapters = [gl("AMD Radeon RX 6700", 0x1002, DeviceType::Other), gl("AMD Radeon Graphics", 0x1002, DeviceType::IntegratedGpu)]; + assert_eq!(choose(&adapters, &two_amd, &Asked::default()), Some((1, Why::Fallback))); + } + + /// Two adapters for the panel's card (Vulkan and GL): Vulkan, whatever the order. + #[test] + fn vulkan_comes_before_gl_for_the_same_card() { + let adapters = [ + gl("AMD Radeon Graphics (radeonsi)", 0x1002, DeviceType::IntegratedGpu), + vulkan("AMD Radeon Graphics (RADV RENOIR)", AMD, DeviceType::IntegratedGpu), + ]; + assert_eq!(choose(&adapters, &[card(AMD, 1, true)], &Asked::default()), Some((1, Why::BuiltInPanel))); + } + + /// A laptop with the lid closed and a monitor on the NVIDIA card: the switched-off panel isn't + /// read as one (`drm` 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 = with_slots.iter().cloned().map(|c| Candidate { pci_slot: String::new(), ..c }).collect(); + assert_eq!(choose(&without, &cards, &Asked::default()), Some((0, Why::Fallback))); + } + + /// The laptop this was measured on: Intel HD 530 behind the panel (`card2-eDP-1`), a Quadro + /// M4000M with nothing connected, and the Quadro offered first by wgpu. + #[test] + fn an_intel_and_nvidia_laptop_draws_on_intel() { + const HD530: (u32, u32, &str) = (0x8086, 0x191b, "0000:00:02.0"); + const M4000M: (u32, u32, &str) = (0x10de, 0x13f9, "0000:01:00.0"); + let adapters = [ + vulkan("Quadro M4000M", M4000M, DeviceType::DiscreteGpu), + vulkan("Intel(R) HD Graphics 530 (SKL GT2)", HD530, DeviceType::IntegratedGpu), + ]; + let cards = [card(M4000M, 0, false), card(HD530, 1, true)]; + assert_eq!(choose(&adapters, &cards, &Asked::default()), Some((1, Why::BuiltInPanel))); + } +} diff --git a/apps/wordcraft/src/linux_gpu/drm.rs b/apps/wordcraft/src/linux_gpu/drm.rs new file mode 100644 index 0000000000..cee58e0bb0 --- /dev/null +++ b/apps/wordcraft/src/linux_gpu/drm.rs @@ -0,0 +1,237 @@ +//! Which graphics cards drive displays, as the system reports them (Linux: `/sys/class/drm`). +//! +//! A window drawn on one card and shown on a screen attached to another is copied across on every +//! frame, so the app draws on the card that drives the screen (see the parent module). + +use std::io::Read as _; +use std::path::Path; + +/// A graphics card and the displays connected to it. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct DisplayCard { + /// PCI vendor id: 0x1002 AMD, 0x10de NVIDIA, 0x8086 Intel. + pub vendor: u32, + /// PCI device id. + 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 that are switched on, of any kind, built-in panel included. + pub displays: u32, + /// A connected, switched-on built-in panel (eDP, LVDS or DSI): this card drives a laptop's + /// own screen. + pub built_in_panel: bool, +} + +/// Connector kinds that are a computer's own screen rather than a monitor plugged into it. +const BUILT_IN_PANELS: &[&str] = &["eDP", "LVDS", "DSI"]; + +/// At most this many entries are read from the DRM folder. +const MAX_ENTRIES: usize = 512; + +/// At most this many bytes are read from one sysfs file. +const MAX_FILE: u64 = 4096; + +/// The graphics cards on this computer and what's connected to them. Empty where the system +/// doesn't say (a sandbox without `/sys`). +#[cfg(target_os = "linux")] +pub(crate) fn display_cards() -> Vec { + display_cards_in(Path::new("/sys/class/drm")) +} + +/// [`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--` entry per +/// 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(crate) fn display_cards_in(drm: &Path) -> Vec { + let Ok(entries) = std::fs::read_dir(drm) else { + return Vec::new(); + }; + let mut names: Vec = entries.take(MAX_ENTRIES).filter_map(|e| e.ok()?.file_name().into_string().ok()).collect(); + names.sort(); + let cards = names.iter().filter(|n| is_card(n)); + cards + .filter_map(|card| { + let uevent = read_small(&drm.join(card).join("device").join("uevent"))?; + let (vendor, device) = pci_ids(&uevent)?; + let pci_slot = uevent_value(&uevent, "PCI_SLOT_NAME").unwrap_or_default().to_ascii_lowercase(); + 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 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); + if BUILT_IN_PANELS.contains(&connector_kind(connector)) { + out.built_in_panel = true; + } + } + Some(out) + }) + .collect() +} + +/// `card0`, `card12`: a card, not a connector (`card0-eDP-1`) or a render node. +fn is_card(name: &str) -> bool { + name.strip_prefix("card").is_some_and(|n| !n.is_empty() && n.bytes().all(|b| b.is_ascii_digit())) +} + +/// `eDP-1` → `eDP`, `HDMI-A-1` → `HDMI-A`: the connector's name without its number. +fn connector_kind(connector: &str) -> &str { + match connector.rsplit_once('-') { + Some((kind, n)) if !n.is_empty() && n.bytes().all(|b| b.is_ascii_digit()) => kind, + _ => connector, + } +} + +/// `PCI_ID=1002:1638` → (0x1002, 0x1638). +fn pci_ids(uevent: &str) -> Option<(u32, u32)> { + let (vendor, device) = uevent_value(uevent, "PCI_ID")?.split_once(':')?; + Some((u32::from_str_radix(vendor.trim(), 16).ok()?, u32::from_str_radix(device.trim(), 16).ok()?)) +} + +fn uevent_value<'a>(uevent: &'a str, key: &str) -> Option<&'a str> { + uevent.lines().find_map(|l| l.strip_prefix(key)?.strip_prefix('=')).map(str::trim) +} + +fn read_small(path: &Path) -> Option { + let mut text = String::new(); + std::fs::File::open(path).ok()?.take(MAX_FILE).read_to_string(&mut text).ok()?; + Some(text) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::fs; + use std::path::PathBuf; + use std::sync::atomic::{AtomicUsize, Ordering}; + + /// A folder of its own under the system's temporary folder, removed when dropped. + struct TempDir(PathBuf); + + impl TempDir { + fn new() -> Self { + static NEXT: AtomicUsize = AtomicUsize::new(0); + let n = NEXT.fetch_add(1, Ordering::Relaxed); + let dir = std::env::temp_dir().join(format!("wordcraft-drm-test-{}-{n}", std::process::id())); + let _ = fs::remove_dir_all(&dir); + fs::create_dir_all(&dir).expect("temp dir"); + TempDir(dir) + } + + fn path(&self) -> &Path { + &self.0 + } + } + + impl Drop for TempDir { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.0); + } + } + + /// A fake `/sys/class/drm`: cards as (name, PCI_ID, PCI_SLOT_NAME), connectors as + /// (name, status). + fn drm(cards: &[(&str, &str, &str)], connectors: &[(&str, &str)]) -> TempDir { + let dir = TempDir::new(); + for (card, id, slot) in cards { + let device = dir.path().join(card).join("device"); + fs::create_dir_all(&device).expect("card folder"); + fs::write(device.join("uevent"), format!("DRIVER=x\nPCI_CLASS=30000\nPCI_ID={id}\nPCI_SLOT_NAME={slot}\n")).expect("uevent"); + } + for (connector, status) in connectors { + let c = dir.path().join(connector); + fs::create_dir_all(&c).expect("connector folder"); + fs::write(c.join("status"), format!("{status}\n")).expect("status"); + } + fs::create_dir_all(dir.path().join("renderD128")).expect("render node"); + fs::write(dir.path().join("version"), "drm 1.1.0\n").expect("version"); + dir + } + + /// A laptop whose AMD iGPU drives the panel, with an NVIDIA card that has no outputs. + #[test] + fn a_laptop_panel_is_found_on_the_integrated_gpu() { + let d = drm( + &[("card0", "10DE:25A2", "0000:01:00.0"), ("card1", "1002:1638", "0000:05:00.0")], + &[("card1-DP-1", "disconnected"), ("card1-eDP-1", "connected"), ("card1-HDMI-A-1", "disconnected")], + ); + let cards = display_cards_in(d.path()); + assert_eq!( + cards, + vec![ + DisplayCard { vendor: 0x10de, device: 0x25a2, pci_slot: "0000:01:00.0".into(), displays: 0, built_in_panel: false }, + DisplayCard { vendor: 0x1002, device: 0x1638, pci_slot: "0000:05:00.0".into(), displays: 1, built_in_panel: true }, + ] + ); + } + + /// A desktop with a monitor on each card: displays on both, no built-in panel. + #[test] + fn monitors_on_two_cards_are_counted_on_each() { + let d = drm( + &[("card0", "8086:A780", "0000:00:02.0"), ("card1", "10de:2206", "0000:01:00.0")], + &[("card0-HDMI-A-1", "connected"), ("card1-DP-1", "connected"), ("card1-DP-2", "connected"), ("card1-DP-3", "disconnected")], + ); + let cards = display_cards_in(d.path()); + assert_eq!(cards.iter().map(|c| (c.displays, c.built_in_panel)).collect::>(), vec![(1, false), (2, false)]); + } + + /// `card10` is a different card from `card1`, and its connectors aren't `card1`'s. + #[test] + fn card_numbers_are_not_prefixes_of_each_other() { + let d = drm(&[("card1", "1002:1638", "0000:05:00.0"), ("card10", "10de:25a2", "0000:01:00.0")], &[("card10-DP-1", "connected")]); + let cards = display_cards_in(d.path()); + assert_eq!(cards.iter().map(|c| (c.vendor, c.displays)).collect::>(), vec![(0x1002, 0), (0x10de, 1)]); + } + + /// LVDS and DSI panels are built in too; HDMI-A is a monitor, whatever its number. + #[test] + fn built_in_panels_are_told_from_monitors_by_connector_kind() { + assert_eq!(connector_kind("eDP-1"), "eDP"); + assert_eq!(connector_kind("HDMI-A-1"), "HDMI-A"); + assert_eq!(connector_kind("DSI-12"), "DSI"); + assert_eq!(connector_kind("Virtual"), "Virtual"); + let d = drm(&[("card0", "8086:9a49", "0000:00:02.0")], &[("card0-LVDS-1", "connected")]); + assert!(display_cards_in(d.path()).iter().all(|c| c.built_in_panel)); + } + + /// A firmware framebuffer has no PCI ids; unreadable or odd entries are skipped, never a panic. + #[test] + fn what_cant_be_read_is_left_out() { + let d = drm(&[("card0", "nonsense", ""), ("card1", "1002:", "")], &[("card0-Virtual-1", "connected")]); + fs::create_dir_all(d.path().join("card2")).expect("card without device"); + fs::create_dir_all(d.path().join("card")).expect("bare name"); + assert!(display_cards_in(d.path()).is_empty()); + assert!(display_cards_in(&d.path().join("missing")).is_empty()); + assert_eq!(pci_ids("PCI_ID=10de:25a2"), Some((0x10de, 0x25a2))); + assert_eq!(pci_ids("PCI_IDX=10de:25a2"), None); + } + + /// A status of `unknown` (some virtual outputs) isn't a connected display. + #[test] + fn only_connected_counts() { + 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![(0x10de, 1, false), (0x1002, 0, false)]); + } +} diff --git a/apps/wordcraft/src/main.rs b/apps/wordcraft/src/main.rs index 302b97d4f7..7f81caa91e 100644 --- a/apps/wordcraft/src/main.rs +++ b/apps/wordcraft/src/main.rs @@ -20,6 +20,8 @@ mod default_apps; mod file_dialogs; #[cfg(any(target_os = "windows", target_os = "macos", test))] mod graphics; +#[cfg(any(target_os = "linux", test))] +mod linux_gpu; mod logging; #[cfg(all(unix, not(target_os = "macos")))] mod portal_save; @@ -685,6 +687,13 @@ fn main() -> eframe::Result { })); } } + // egui-wgpu draws on the discrete GPU by default; on a laptop whose integrated GPU drives the + // panel, that wakes the discrete GPU whenever the window redraws. Draw on the GPU that drives + // the screen; `WGPU_ADAPTER_NAME` and `WGPU_POWER_PREF` still decide (see linux_gpu.rs). + #[cfg(target_os = "linux")] + if let eframe::egui_wgpu::WgpuSetup::CreateNew(setup) = &mut options.wgpu_options.wgpu_setup { + setup.native_adapter_selector = Some(std::sync::Arc::new(linux_gpu::pick_adapter)); + } // Registered before the event loop starts, so it catches the Finder event that launched us as // well as later ones. Lives until the event loop returns; the app creator only borrows it. #[cfg(target_os = "macos")]