mirror of
https://github.com/bleackstorm/SteamFrame-FPV-Control-Center.git
synced 2026-12-03 07:31:57 +01:00
fix(review): apply critical code-review optimizations for locks, memory, and responsiveness
This commit is contained in:
1 parent
2d9be15713
commit
a55ca0a25d
4 files changed
+131
-45
No files matched your search
@@ -347,6 +347,7 @@ fn run_engine_worker(
|
||||
&mut telem_rx_opt,
|
||||
&mut head_tracker_opt,
|
||||
) {
|
||||
if !stop_flag.load(Ordering::Relaxed) {
|
||||
log::warn!(
|
||||
"[OpenXR] Session VR indisponible ({}). Bascule automatique en mode Monitoring Bureau / Simulation.",
|
||||
xr_err
|
||||
@@ -366,6 +367,7 @@ fn run_engine_worker(
|
||||
&mut head_tracker_opt,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
log::info!("[Moteur FPV] Tous les sous-systèmes ont été arrêtés.");
|
||||
}
|
||||
|
||||
+104
-40
@@ -1,5 +1,5 @@
|
||||
use crate::config::AppConfig;
|
||||
use crate::engine::{FpvEngineController, SubsystemStatus};
|
||||
use crate::engine::{FpvEngineController, RuntimeMonitor, SubsystemStatus};
|
||||
use crate::logger::{LogBuffer, LogEntry, LogFilter};
|
||||
use crate::nomad_elrs::{HeadPoseSample, NomadHeadTracker};
|
||||
use crate::telemetry_osd::TelemetryState;
|
||||
@@ -12,6 +12,7 @@ use image::{Frame, RgbaImage};
|
||||
use parking_lot::Mutex;
|
||||
use smallvec::smallvec;
|
||||
use std::process::Command;
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
@@ -41,8 +42,10 @@ pub enum ActiveTab {
|
||||
|
||||
pub struct SteamFrameGpuiApp {
|
||||
cfg: AppConfig,
|
||||
engine: FpvEngineController,
|
||||
engine: Arc<Mutex<FpvEngineController>>,
|
||||
engine_monitor: Arc<Mutex<RuntimeMonitor>>,
|
||||
log_buffer: Arc<Mutex<LogBuffer>>,
|
||||
log_revision: Arc<AtomicU64>,
|
||||
log_filter: LogFilter,
|
||||
current_render_image: Option<Arc<RenderImage>>,
|
||||
images_to_drop: Vec<Arc<RenderImage>>,
|
||||
@@ -66,14 +69,19 @@ pub struct SteamFrameGpuiApp {
|
||||
|
||||
impl SteamFrameGpuiApp {
|
||||
pub fn new(cfg: AppConfig, log_buffer: Arc<Mutex<LogBuffer>>) -> Self {
|
||||
let log_revision = log_buffer.lock().revision.clone();
|
||||
let mut engine =
|
||||
FpvEngineController::new(cfg.telemetry_enabled, cfg.head_tracking_enabled);
|
||||
engine.start(cfg.clone());
|
||||
let engine_monitor = Arc::clone(&engine.monitor);
|
||||
let engine = Arc::new(Mutex::new(engine));
|
||||
|
||||
Self {
|
||||
cfg,
|
||||
engine,
|
||||
engine_monitor,
|
||||
log_buffer,
|
||||
log_revision,
|
||||
log_filter: LogFilter::All,
|
||||
current_render_image: None,
|
||||
images_to_drop: Vec::new(),
|
||||
@@ -95,25 +103,62 @@ impl SteamFrameGpuiApp {
|
||||
}
|
||||
}
|
||||
|
||||
/// Synchronise l'état IHM avec le moteur FPV en tâche de fond
|
||||
fn sync_from_engine(&mut self, window: &mut Window) {
|
||||
let mon = self.engine.monitor.lock();
|
||||
/// Synchronise l'état IHM avec le moteur FPV en minimisant au maximum la contention des verrous
|
||||
fn sync_from_engine(&mut self, window: &mut Window) -> bool {
|
||||
let mut changed = false;
|
||||
|
||||
// 1. Mise à jour de la texture vidéo si une nouvelle trame est arrivée
|
||||
if mon.preview_frame_seq != self.last_frame_seq
|
||||
// 1. Snapshot rapide avec maintien minimal du verrou monitor
|
||||
let (preview_frame_seq, new_frame_data) = {
|
||||
let mon = self.engine_monitor.lock();
|
||||
|
||||
if self.usb_status != mon.usb_video_status {
|
||||
self.usb_status = mon.usb_video_status.clone();
|
||||
changed = true;
|
||||
}
|
||||
if self.xr_status != mon.openxr_status {
|
||||
self.xr_status = mon.openxr_status.clone();
|
||||
changed = true;
|
||||
}
|
||||
if self.nomad_status != mon.nomad_wifi_status {
|
||||
self.nomad_status = mon.nomad_wifi_status.clone();
|
||||
changed = true;
|
||||
}
|
||||
if self.telem_status != mon.telemetry_status {
|
||||
self.telem_status = mon.telemetry_status.clone();
|
||||
changed = true;
|
||||
}
|
||||
if (self.video_fps - mon.video_fps).abs() > 0.1 {
|
||||
self.video_fps = mon.video_fps;
|
||||
changed = true;
|
||||
}
|
||||
if self.video_frames_total != mon.video_frames_total {
|
||||
self.video_frames_total = mon.video_frames_total;
|
||||
changed = true;
|
||||
}
|
||||
self.latest_pose = mon.latest_head_pose;
|
||||
self.latest_telem = mon.latest_telemetry.clone();
|
||||
|
||||
// Ne cloner et décoder la trame QUE si l'onglet Cockpit est actif !
|
||||
let frame_data: Option<(u32, u32, Vec<u8>)> = if self.active_tab == ActiveTab::Cockpit
|
||||
&& mon.preview_frame_seq != self.last_frame_seq
|
||||
&& !mon.preview_rgba.is_empty()
|
||||
&& mon.preview_width > 0
|
||||
&& mon.preview_height > 0
|
||||
{
|
||||
if let Some(mut img_buf) = RgbaImage::from_raw(
|
||||
mon.preview_width,
|
||||
mon.preview_height,
|
||||
mon.preview_rgba.clone(),
|
||||
) {
|
||||
// Conversion RGBA -> BGRA requise par l'atlas polychrome GPUI / Vulkan
|
||||
for pixel in img_buf.chunks_exact_mut(4) {
|
||||
Some((mon.preview_width, mon.preview_height, mon.preview_rgba.clone()))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
(mon.preview_frame_seq, frame_data)
|
||||
}; // Verrou `engine_monitor` immédiatement libéré !
|
||||
|
||||
// 2. Conversion BGRA et allocation GPU hors du verrou monitor
|
||||
if let Some((w, h, mut raw_buf)) = new_frame_data {
|
||||
for pixel in raw_buf.chunks_exact_mut(4) {
|
||||
pixel.swap(0, 2);
|
||||
}
|
||||
if let Some(img_buf) = RgbaImage::from_raw(w, h, raw_buf) {
|
||||
let frame = Frame::new(img_buf);
|
||||
let new_render_image = Arc::new(RenderImage::new(smallvec![frame]));
|
||||
|
||||
@@ -121,9 +166,9 @@ impl SteamFrameGpuiApp {
|
||||
self.images_to_drop.push(old);
|
||||
}
|
||||
self.current_render_image = Some(new_render_image);
|
||||
self.last_frame_seq = mon.preview_frame_seq;
|
||||
self.last_frame_seq = preview_frame_seq;
|
||||
changed = true;
|
||||
|
||||
// Nettoyage de l'atlas texture GPU avec tampon de 2 trames
|
||||
while self.images_to_drop.len() > 2 {
|
||||
let old_img = self.images_to_drop.remove(0);
|
||||
let _ = window.drop_image(old_img);
|
||||
@@ -131,29 +176,37 @@ impl SteamFrameGpuiApp {
|
||||
}
|
||||
}
|
||||
|
||||
self.usb_status = mon.usb_video_status.clone();
|
||||
self.xr_status = mon.openxr_status.clone();
|
||||
self.nomad_status = mon.nomad_wifi_status.clone();
|
||||
self.telem_status = mon.telemetry_status.clone();
|
||||
self.video_fps = mon.video_fps;
|
||||
self.video_frames_total = mon.video_frames_total;
|
||||
self.latest_pose = mon.latest_head_pose;
|
||||
self.latest_telem = mon.latest_telemetry.clone();
|
||||
|
||||
// 2. Synchronisation sélective des logs (uniquement sur nouvelle révision)
|
||||
// 3. Synchronisation lock-free des logs via AtomicU64
|
||||
let current_log_rev = self.log_revision.load(Ordering::Acquire);
|
||||
if current_log_rev != self.last_log_revision {
|
||||
let lb = self.log_buffer.lock();
|
||||
if lb.revision != self.last_log_revision {
|
||||
self.last_log_revision = lb.revision;
|
||||
self.last_log_revision = current_log_rev;
|
||||
self.log_err_count = lb.error_count;
|
||||
self.log_warn_count = lb.warn_count;
|
||||
self.log_entries = lb.entries.iter().rev().take(60).cloned().collect();
|
||||
changed = true;
|
||||
}
|
||||
|
||||
changed
|
||||
}
|
||||
|
||||
fn run_usb_rndis_setup(&self) {
|
||||
log::info!("[USB RNDIS] Exécution du script de configuration réseau...");
|
||||
let script_path =
|
||||
concat!(env!("CARGO_MANIFEST_DIR"), "/scripts/setup_usb_rndis.sh").to_string();
|
||||
let script_path = std::env::current_exe()
|
||||
.ok()
|
||||
.and_then(|p| p.parent().map(|d| d.join("scripts/setup_usb_rndis.sh")))
|
||||
.filter(|p| p.exists())
|
||||
.unwrap_or_else(|| {
|
||||
let candidate = std::path::PathBuf::from("scripts/setup_usb_rndis.sh");
|
||||
if candidate.exists() {
|
||||
candidate
|
||||
} else {
|
||||
std::path::PathBuf::from(concat!(
|
||||
env!("CARGO_MANIFEST_DIR"),
|
||||
"/scripts/setup_usb_rndis.sh"
|
||||
))
|
||||
}
|
||||
});
|
||||
std::thread::spawn(move || {
|
||||
match Command::new("bash").arg(&script_path).output() {
|
||||
Ok(out) => {
|
||||
@@ -164,7 +217,7 @@ impl SteamFrameGpuiApp {
|
||||
stdout.trim()
|
||||
);
|
||||
}
|
||||
Err(e) => log::error!("[USB RNDIS] Échec d'exécution du script : {}", e),
|
||||
Err(e) => log::error!("[USB RNDIS] Échec d'exécution du script {:?} : {}", script_path, e),
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -188,6 +241,7 @@ impl SteamFrameGpuiApp {
|
||||
"ConnectTimeout=2",
|
||||
"-o",
|
||||
"StrictHostKeyChecking=no",
|
||||
"--",
|
||||
&format!("root@{}", ip),
|
||||
&remote_cmd,
|
||||
])
|
||||
@@ -328,6 +382,7 @@ impl SteamFrameGpuiApp {
|
||||
.on_click(cx.listener(|this, _, _, cx| {
|
||||
this.cfg.head_tracking_enabled = !this.cfg.head_tracking_enabled;
|
||||
this.engine
|
||||
.lock()
|
||||
.set_head_tracking_enabled(this.cfg.head_tracking_enabled);
|
||||
cx.notify();
|
||||
}))
|
||||
@@ -344,6 +399,7 @@ impl SteamFrameGpuiApp {
|
||||
.on_click(cx.listener(|this, _, _, cx| {
|
||||
this.cfg.telemetry_enabled = !this.cfg.telemetry_enabled;
|
||||
this.engine
|
||||
.lock()
|
||||
.set_telemetry_enabled(this.cfg.telemetry_enabled);
|
||||
cx.notify();
|
||||
}))
|
||||
@@ -366,7 +422,7 @@ impl SteamFrameGpuiApp {
|
||||
.font_weight(gpui::FontWeight::SEMIBOLD)
|
||||
.cursor_pointer()
|
||||
.on_click(cx.listener(|this, _, _, cx| {
|
||||
this.engine.request_recenter();
|
||||
this.engine.lock().request_recenter();
|
||||
cx.notify();
|
||||
}))
|
||||
.child("🎯 Tare"),
|
||||
@@ -686,8 +742,13 @@ impl SteamFrameGpuiApp {
|
||||
.font_weight(gpui::FontWeight::BOLD)
|
||||
.cursor_pointer()
|
||||
.on_click(cx.listener(|this, _, _, cx| {
|
||||
log::info!("[Réglages] Application et redémarrage du moteur FPV...");
|
||||
this.engine.start(this.cfg.clone());
|
||||
log::info!("[Réglages] Application et redémarrage asynchrone du moteur FPV...");
|
||||
let engine_clone = Arc::clone(&this.engine);
|
||||
let cfg_clone = this.cfg.clone();
|
||||
std::thread::spawn(move || {
|
||||
let mut eng = engine_clone.lock();
|
||||
eng.start(cfg_clone);
|
||||
});
|
||||
cx.notify();
|
||||
}))
|
||||
.child("🔄 Appliquer & Redémarrer"),
|
||||
@@ -899,7 +960,7 @@ impl SteamFrameGpuiApp {
|
||||
.font_weight(gpui::FontWeight::SEMIBOLD)
|
||||
.cursor_pointer()
|
||||
.on_click(cx.listener(|this, _, _, cx| {
|
||||
this.engine.request_recenter();
|
||||
this.engine.lock().request_recenter();
|
||||
cx.notify();
|
||||
}))
|
||||
.child("🎯 Réinitialiser Zéro (Tare)"),
|
||||
@@ -987,7 +1048,7 @@ impl SteamFrameGpuiApp {
|
||||
self.cfg.telemetry_enabled,
|
||||
cx.listener(|this, _, _, cx| {
|
||||
this.cfg.telemetry_enabled = true;
|
||||
this.engine.set_telemetry_enabled(true);
|
||||
this.engine.lock().set_telemetry_enabled(true);
|
||||
cx.notify();
|
||||
}),
|
||||
))
|
||||
@@ -997,7 +1058,7 @@ impl SteamFrameGpuiApp {
|
||||
!self.cfg.telemetry_enabled,
|
||||
cx.listener(|this, _, _, cx| {
|
||||
this.cfg.telemetry_enabled = false;
|
||||
this.engine.set_telemetry_enabled(false);
|
||||
this.engine.lock().set_telemetry_enabled(false);
|
||||
cx.notify();
|
||||
}),
|
||||
)),
|
||||
@@ -1017,7 +1078,8 @@ impl SteamFrameGpuiApp {
|
||||
div()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.w(px(570.0))
|
||||
.min_w(px(380.0))
|
||||
.flex_1()
|
||||
.bg(rgb(COLOR_SURFACE_ELEVATED))
|
||||
.border_1()
|
||||
.border_color(rgb(COLOR_BORDER_SUBTLE))
|
||||
@@ -1153,6 +1215,7 @@ impl SteamFrameGpuiApp {
|
||||
.child(
|
||||
div()
|
||||
.flex()
|
||||
.flex_wrap()
|
||||
.gap_3()
|
||||
.child(self.render_stat_box(
|
||||
"🔋 BATTERIE DRONE",
|
||||
@@ -1271,6 +1334,7 @@ impl SteamFrameGpuiApp {
|
||||
.flex()
|
||||
.flex_col()
|
||||
.flex_1()
|
||||
.min_w(px(200.0))
|
||||
.bg(rgb(COLOR_SURFACE_ELEVATED))
|
||||
.border_1()
|
||||
.border_color(rgb(COLOR_BORDER_SUBTLE))
|
||||
@@ -1491,7 +1555,6 @@ impl SteamFrameGpuiApp {
|
||||
.children(
|
||||
self.log_entries
|
||||
.iter()
|
||||
.rev()
|
||||
.filter(|e| match self.log_filter {
|
||||
LogFilter::All => true,
|
||||
LogFilter::WarningsAndErrors => {
|
||||
@@ -1649,8 +1712,9 @@ pub fn run_gpui_app(cfg: AppConfig, log_buffer: Arc<Mutex<LogBuffer>>) -> Result
|
||||
.update(|window, cx| {
|
||||
if let Some(view) = weak_view.upgrade() {
|
||||
view.update(cx, |this, cx| {
|
||||
this.sync_from_engine(window);
|
||||
if this.sync_from_engine(window) {
|
||||
cx.notify();
|
||||
}
|
||||
});
|
||||
true
|
||||
} else {
|
||||
|
||||
+21
-4
@@ -4,6 +4,7 @@ use parking_lot::Mutex;
|
||||
use std::collections::VecDeque;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::Write;
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::sync::Arc;
|
||||
|
||||
const MAX_LOG_ENTRIES: usize = 1000;
|
||||
@@ -23,12 +24,22 @@ pub struct LogEntry {
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct LogBuffer {
|
||||
pub entries: VecDeque<LogEntry>,
|
||||
pub error_count: usize,
|
||||
pub warn_count: usize,
|
||||
pub revision: u64,
|
||||
pub revision: Arc<AtomicU64>,
|
||||
}
|
||||
|
||||
impl Default for LogBuffer {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
entries: VecDeque::new(),
|
||||
error_count: 0,
|
||||
warn_count: 0,
|
||||
revision: Arc::new(AtomicU64::new(0)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Logger centralisé capturant tous les événements et erreurs pour l'interface graphique
|
||||
@@ -110,10 +121,16 @@ impl Log for GuiLogger {
|
||||
}
|
||||
|
||||
if lock.entries.len() >= MAX_LOG_ENTRIES {
|
||||
lock.entries.pop_front();
|
||||
if let Some(old) = lock.entries.pop_front() {
|
||||
match old.level {
|
||||
Level::Error => lock.error_count = lock.error_count.saturating_sub(1),
|
||||
Level::Warn => lock.warn_count = lock.warn_count.saturating_sub(1),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
lock.entries.push_back(entry);
|
||||
lock.revision = lock.revision.wrapping_add(1);
|
||||
lock.revision.fetch_add(1, Ordering::Release);
|
||||
}
|
||||
|
||||
fn flush(&self) {}
|
||||
|
||||
+4
-1
@@ -171,7 +171,7 @@ impl NomadHeadTracker {
|
||||
|
||||
#[inline]
|
||||
pub fn deg_to_crsf(angle_deg: f32, max_deg: f32) -> u16 {
|
||||
if angle_deg.is_nan() || max_deg <= 0.0 {
|
||||
if angle_deg.is_nan() || !(max_deg > 0.0) {
|
||||
return CRSF_CHANNEL_MID;
|
||||
}
|
||||
let norm = (angle_deg / max_deg).clamp(-1.0, 1.0);
|
||||
@@ -254,6 +254,9 @@ mod tests {
|
||||
assert_eq!(NomadHeadTracker::deg_to_crsf(90.0, 90.0), CRSF_CHANNEL_MAX);
|
||||
assert_eq!(NomadHeadTracker::deg_to_crsf(-90.0, 90.0), CRSF_CHANNEL_MIN);
|
||||
assert_eq!(NomadHeadTracker::deg_to_crsf(f32::NAN, 90.0), CRSF_CHANNEL_MID);
|
||||
assert_eq!(NomadHeadTracker::deg_to_crsf(45.0, f32::NAN), CRSF_CHANNEL_MID);
|
||||
assert_eq!(NomadHeadTracker::deg_to_crsf(45.0, 0.0), CRSF_CHANNEL_MID);
|
||||
assert_eq!(NomadHeadTracker::deg_to_crsf(45.0, -10.0), CRSF_CHANNEL_MID);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user