feat(desktop): add GPU selection policy (#1442)

Co-authored-by: Michael Aaron Murphy <michael@mmurphy.dev>
This commit is contained in:
Fred 2026-10-06 22:55:28 +02:00 • committed by GitHub
parent 5a8bd9482a
commit fd182fdbb4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 58 additions and 0 deletions

View file

@ -1,6 +1,8 @@
#[cfg(not(windows))]
pub use freedesktop_desktop_entry as fde;
#[cfg(not(windows))]
pub mod gpu;
#[cfg(not(windows))]
pub use mime::Mime;
use std::path::{Path, PathBuf};
#[cfg(not(windows))]

56
src/desktop/gpu.rs Normal file
View file

@ -0,0 +1,56 @@
// Copyright 2026 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
use std::collections::HashMap;
/// An available GPU that can be selected by a launcher.
#[derive(Debug, Clone)]
pub struct Descriptor {
pub name: String,
pub environment: HashMap<String, String>,
pub default: bool,
pub discrete: bool,
}
/// A preference to use when choosing a GPU to launch with.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum Preference {
#[default]
Default,
NonDefault,
}
impl Preference {
/// Find the preferred default GPU for this preference.
pub fn preferred(self, gpus: &[Descriptor]) -> Option<usize> {
let default_idx = gpus.iter().position(|gpu| gpu.default);
if matches!(self, Preference::Default) {
return default_idx;
}
let default_discrete = gpus.iter().position(|gpu| gpu.default && gpu.discrete);
if default_discrete.is_some() {
return default_discrete;
}
let non_default_discrete = gpus.iter().position(|gpu| gpu.discrete);
let non_default = || gpus.iter().position(|gpu| !gpu.default);
non_default_discrete.or_else(non_default).or(default_idx)
}
/// Select the GPU override to launch with.
pub fn select(self, gpus: &[Descriptor]) -> Option<usize> {
if matches!(self, Preference::Default) {
return None;
}
let default_discrete = gpus.iter().position(|gpu| gpu.default && gpu.discrete);
if default_discrete.is_some() {
return None;
}
let non_default_discrete = gpus.iter().position(|gpu| gpu.discrete);
let non_default = || gpus.iter().position(|gpu| !gpu.default);
non_default_discrete.or_else(non_default)
}
}