feat(desktop): add GPU selection policy (#1442)
Co-authored-by: Michael Aaron Murphy <michael@mmurphy.dev>
This commit is contained in:
parent
5a8bd9482a
commit
fd182fdbb4
2 changed files with 58 additions and 0 deletions
|
|
@ -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
56
src/desktop/gpu.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue