Add graphics settings backend for hardware acceleration opt-in
Hardware acceleration is OFF by default for maximum AppImage compatibility. Users can enable it in Settings > Appearance. Env var QBZ_HARDWARE_ACCEL=1|0 always overrides the DB value, providing crash recovery from the terminal.
This commit is contained in:
1 parent
b1d47ded31
commit
3cd7e7c55f
4 files changed
+165
-2
No files matched your search
@@ -0,0 +1,119 @@
|
||||
//! Graphics settings persistence
|
||||
//!
|
||||
//! Stores GPU/rendering preferences that take effect before WebView initialization.
|
||||
//! These settings are device-level (not per-user) and persist across sessions.
|
||||
//!
|
||||
//! - hardware_acceleration: opt-in GPU rendering (default: off)
|
||||
//! Env var QBZ_HARDWARE_ACCEL=1|0 always overrides the stored value.
|
||||
|
||||
use rusqlite::{Connection, params};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct GraphicsSettings {
|
||||
/// Enable hardware-accelerated rendering (requires restart)
|
||||
pub hardware_acceleration: bool,
|
||||
}
|
||||
|
||||
impl Default for GraphicsSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
hardware_acceleration: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct GraphicsSettingsStore {
|
||||
conn: Connection,
|
||||
}
|
||||
|
||||
impl GraphicsSettingsStore {
|
||||
pub fn new() -> Result<Self, String> {
|
||||
let data_dir = dirs::data_dir()
|
||||
.ok_or("Could not determine data directory")?
|
||||
.join("qbz");
|
||||
|
||||
std::fs::create_dir_all(&data_dir)
|
||||
.map_err(|e| format!("Failed to create data directory: {}", e))?;
|
||||
|
||||
let db_path = data_dir.join("graphics_settings.db");
|
||||
let conn = Connection::open(&db_path)
|
||||
.map_err(|e| format!("Failed to open graphics settings database: {}", e))?;
|
||||
|
||||
conn.execute_batch(
|
||||
"CREATE TABLE IF NOT EXISTS graphics_settings (
|
||||
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||
hardware_acceleration INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
INSERT OR IGNORE INTO graphics_settings (id, hardware_acceleration) VALUES (1, 0);"
|
||||
).map_err(|e| format!("Failed to create graphics settings table: {}", e))?;
|
||||
|
||||
Ok(Self { conn })
|
||||
}
|
||||
|
||||
pub fn get_settings(&self) -> Result<GraphicsSettings, String> {
|
||||
self.conn
|
||||
.query_row(
|
||||
"SELECT hardware_acceleration FROM graphics_settings WHERE id = 1",
|
||||
[],
|
||||
|row| {
|
||||
Ok(GraphicsSettings {
|
||||
hardware_acceleration: row.get::<_, i64>(0)? != 0,
|
||||
})
|
||||
},
|
||||
)
|
||||
.map_err(|e| format!("Failed to get graphics settings: {}", e))
|
||||
}
|
||||
|
||||
pub fn set_hardware_acceleration(&self, enabled: bool) -> Result<(), String> {
|
||||
self.conn
|
||||
.execute(
|
||||
"UPDATE graphics_settings SET hardware_acceleration = ?1 WHERE id = 1",
|
||||
params![enabled as i64],
|
||||
)
|
||||
.map_err(|e| format!("Failed to set hardware_acceleration: {}", e))?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Thread-safe wrapper for Tauri state management
|
||||
pub struct GraphicsSettingsState {
|
||||
pub store: Arc<Mutex<Option<GraphicsSettingsStore>>>,
|
||||
}
|
||||
|
||||
impl GraphicsSettingsState {
|
||||
pub fn new() -> Result<Self, String> {
|
||||
Ok(Self {
|
||||
store: Arc::new(Mutex::new(Some(GraphicsSettingsStore::new()?))),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn new_empty() -> Self {
|
||||
Self {
|
||||
store: Arc::new(Mutex::new(None)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Tauri commands
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_graphics_settings(
|
||||
state: tauri::State<'_, GraphicsSettingsState>,
|
||||
) -> Result<GraphicsSettings, String> {
|
||||
let guard = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
|
||||
let store = guard.as_ref().ok_or("Graphics settings store not initialized")?;
|
||||
store.get_settings()
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn set_hardware_acceleration(
|
||||
state: tauri::State<'_, GraphicsSettingsState>,
|
||||
enabled: bool,
|
||||
) -> Result<(), String> {
|
||||
log::info!("[GraphicsSettings] Setting hardware_acceleration to {} (restart required)", enabled);
|
||||
let guard = state.store.lock().map_err(|e| format!("Lock error: {}", e))?;
|
||||
let store = guard.as_ref().ok_or("Graphics settings store not initialized")?;
|
||||
store.set_hardware_acceleration(enabled)
|
||||
}
|
||||
@@ -12,6 +12,7 @@
|
||||
pub mod audio_settings;
|
||||
pub mod developer_settings;
|
||||
pub mod download_settings;
|
||||
pub mod graphics_settings;
|
||||
pub mod playback_preferences;
|
||||
pub mod favorites_preferences;
|
||||
pub mod favorites_cache;
|
||||
@@ -111,3 +112,10 @@ pub use developer_settings::{
|
||||
get_developer_settings,
|
||||
set_developer_force_dmabuf,
|
||||
};
|
||||
|
||||
pub use graphics_settings::{
|
||||
GraphicsSettings,
|
||||
GraphicsSettingsState,
|
||||
get_graphics_settings,
|
||||
set_hardware_acceleration,
|
||||
};
|
||||
@@ -241,6 +241,11 @@ pub fn run() {
|
||||
log::warn!("Failed to initialize developer settings: {}. Using empty state.", e);
|
||||
config::developer_settings::DeveloperSettingsState::new_empty()
|
||||
});
|
||||
let graphics_settings_state = config::graphics_settings::GraphicsSettingsState::new()
|
||||
.unwrap_or_else(|e| {
|
||||
log::warn!("Failed to initialize graphics settings: {}. Using empty state.", e);
|
||||
config::graphics_settings::GraphicsSettingsState::new_empty()
|
||||
});
|
||||
|
||||
// Clone settings for use in closures
|
||||
let enable_tray = tray_settings.enable_tray;
|
||||
@@ -418,6 +423,7 @@ pub fn run() {
|
||||
.manage(artist_vectors_state)
|
||||
.manage(blacklist_state)
|
||||
.manage(developer_settings_state)
|
||||
.manage(graphics_settings_state)
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
// Auth commands
|
||||
commands::init_client,
|
||||
@@ -903,6 +909,9 @@ pub fn run() {
|
||||
// Developer settings commands
|
||||
config::developer_settings::get_developer_settings,
|
||||
config::developer_settings::set_developer_force_dmabuf,
|
||||
// Graphics settings commands
|
||||
config::graphics_settings::get_graphics_settings,
|
||||
config::graphics_settings::set_hardware_acceleration,
|
||||
// Log capture commands
|
||||
logging::get_backend_logs,
|
||||
logging::upload_logs_to_paste,
|
||||
|
||||
+29
-2
@@ -123,6 +123,27 @@ fn main() {
|
||||
let is_vm = is_virtual_machine();
|
||||
let force_software = std::env::var("QBZ_SOFTWARE_RENDER").as_deref() == Ok("1");
|
||||
|
||||
// Graphics settings: hardware acceleration opt-in (from Settings > Appearance)
|
||||
// Default is OFF — safest for AppImage across heterogeneous hardware.
|
||||
// Env var QBZ_HARDWARE_ACCEL=1|0 ALWAYS overrides the DB value (crash recovery).
|
||||
let hw_accel_db = qbz_nix_lib::config::graphics_settings::GraphicsSettingsStore::new()
|
||||
.ok()
|
||||
.and_then(|store| store.get_settings().ok())
|
||||
.map(|s| s.hardware_acceleration)
|
||||
.unwrap_or(false);
|
||||
let hardware_accel = match std::env::var("QBZ_HARDWARE_ACCEL").as_deref() {
|
||||
Ok("1") => {
|
||||
qbz_nix_lib::logging::log_startup("[QBZ] Env override: QBZ_HARDWARE_ACCEL=1 (GPU rendering forced on)");
|
||||
true
|
||||
}
|
||||
Ok("0") => {
|
||||
qbz_nix_lib::logging::log_startup("[QBZ] Env override: QBZ_HARDWARE_ACCEL=0 (GPU rendering forced off)");
|
||||
false
|
||||
}
|
||||
_ => hw_accel_db,
|
||||
};
|
||||
qbz_nix_lib::logging::log_startup(&format!("[QBZ] Hardware acceleration: {}", if hardware_accel { "enabled" } else { "disabled (default)" }));
|
||||
|
||||
// Developer settings: force_dmabuf override (from Settings > Developer Mode)
|
||||
let dev_force_dmabuf = qbz_nix_lib::config::developer_settings::DeveloperSettingsStore::new()
|
||||
.ok()
|
||||
@@ -172,18 +193,24 @@ fn main() {
|
||||
// --- DMA-BUF renderer control ---
|
||||
// NVIDIA GPUs have known issues with WebKit's DMA-BUF renderer on
|
||||
// Wayland, causing fatal protocol errors (Error 71).
|
||||
// When hardware_accel is off, we also disable the DMA-BUF renderer
|
||||
// as an extra safety layer for AppImage compatibility.
|
||||
if force_dmabuf {
|
||||
qbz_nix_lib::logging::log_startup("[QBZ] User override: Forcing DMA-BUF renderer enabled (QBZ_FORCE_DMABUF=1)");
|
||||
qbz_nix_lib::logging::log_startup("[QBZ] Warning: This may cause crashes on some Wayland configurations");
|
||||
} else if disable_dmabuf {
|
||||
qbz_nix_lib::logging::log_startup("[QBZ] User override: Forcing DMA-BUF renderer disabled (QBZ_DISABLE_DMABUF=1)");
|
||||
std::env::set_var("WEBKIT_DISABLE_DMABUF_RENDERER", "1");
|
||||
} else if !hardware_accel {
|
||||
qbz_nix_lib::logging::log_startup("[QBZ] Hardware acceleration disabled: disabling WebKit DMA-BUF renderer");
|
||||
qbz_nix_lib::logging::log_startup("[QBZ] To enable: Settings > Appearance > Hardware Acceleration");
|
||||
std::env::set_var("WEBKIT_DISABLE_DMABUF_RENDERER", "1");
|
||||
} else if has_nvidia {
|
||||
qbz_nix_lib::logging::log_startup("[QBZ] NVIDIA GPU detected: disabling WebKit DMA-BUF renderer");
|
||||
qbz_nix_lib::logging::log_startup("[QBZ] To override: set QBZ_FORCE_DMABUF=1 (not recommended)");
|
||||
std::env::set_var("WEBKIT_DISABLE_DMABUF_RENDERER", "1");
|
||||
} else if std::env::var_os("WEBKIT_DISABLE_DMABUF_RENDERER").is_none() {
|
||||
qbz_nix_lib::logging::log_startup("[QBZ] Using default WebKit renderer (hardware accelerated)");
|
||||
} else {
|
||||
qbz_nix_lib::logging::log_startup("[QBZ] Hardware acceleration enabled, using default WebKit renderer");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user