From 3cd7e7c55ffc52f35fa4f6e4006af6a1b9367657 Mon Sep 17 00:00:00 2001 From: vicrodh <103399283+vicrodh@users.noreply.github.com> Date: Mon, 9 Feb 2026 02:11:25 -0600 Subject: [PATCH] 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. --- src-tauri/src/config/graphics_settings.rs | 119 ++++++++++++++++++++++ src-tauri/src/config/mod.rs | 8 ++ src-tauri/src/lib.rs | 9 ++ src-tauri/src/main.rs | 31 +++++- 4 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 src-tauri/src/config/graphics_settings.rs diff --git a/src-tauri/src/config/graphics_settings.rs b/src-tauri/src/config/graphics_settings.rs new file mode 100644 index 000000000..14b448ec9 --- /dev/null +++ b/src-tauri/src/config/graphics_settings.rs @@ -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 { + 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 { + 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>>, +} + +impl GraphicsSettingsState { + pub fn new() -> Result { + 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 { + 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) +} diff --git a/src-tauri/src/config/mod.rs b/src-tauri/src/config/mod.rs index 1f4df4cbe..8d50d39dc 100644 --- a/src-tauri/src/config/mod.rs +++ b/src-tauri/src/config/mod.rs @@ -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, +}; diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 8192eb348..401a001d3 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -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, diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 5b1196769..8c1cc8301 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -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"); } }