diff --git a/services/graphics-server/src/backend/betrusted.rs b/services/graphics-server/src/backend/betrusted.rs index 120c87571..7b741c087 100644 --- a/services/graphics-server/src/backend/betrusted.rs +++ b/services/graphics-server/src/backend/betrusted.rs @@ -212,9 +212,6 @@ impl XousDisplay { log::trace!("redraw {}/{}", busy_count, dirty_count); } - // note: this API is used by emulation, don't remove calls to it - pub fn update(&mut self) {} - pub fn native_buffer(&mut self) -> &mut [u32; FB_SIZE] { unsafe { &mut *(self.fb.as_mut_ptr() as *mut [u32; FB_SIZE]) } } diff --git a/services/graphics-server/src/backend/minifb.rs b/services/graphics-server/src/backend/minifb.rs index 86e42fbe1..426b9ace3 100644 --- a/services/graphics-server/src/backend/minifb.rs +++ b/services/graphics-server/src/backend/minifb.rs @@ -3,6 +3,7 @@ use crate::api::Point; use minifb::{Key, Window, WindowOptions}; use crate::api::{LINES, WIDTH}; +use std::sync::{Arc, Mutex}; const HEIGHT: i16 = LINES; @@ -18,13 +19,18 @@ const DARK_COLOUR: u32 = 0xB5B5AD; const LIGHT_COLOUR: u32 = 0x1B1B19; pub struct XousDisplay { - native_buffer: Vec, //[u32; WIDTH * HEIGHT], + native_buffer: Arc>>, //[u32; WIDTH * HEIGHT], emulated_buffer: [u32; FB_SIZE], srfb: [u32; FB_SIZE], - window: Window, devboot: bool, } +/// Encapsulates the data passed to the thread handling minifb screen updates +/// and input events. +struct MinifbThread { + native_buffer: Arc>>, +} + struct XousKeyboardHandler { kbd: keyboard::Keyboard, left_shift: bool, @@ -33,43 +39,20 @@ struct XousKeyboardHandler { impl XousDisplay { pub fn new() -> XousDisplay { - let mut window = Window::new( - "Precursor", - WIDTH as usize, - HEIGHT as usize, - WindowOptions { - scale_mode: minifb::ScaleMode::AspectRatioStretch, - resize: true, - ..WindowOptions::default() - }, - ) - .unwrap_or_else(|e| { - panic!("{}", e); - }); - - // // Limit the maximum refresh rate - // window.limit_update_rate(Some(std::time::Duration::from_micros( - // 1000 * 1000 / MAX_FPS, - // ))); - let native_buffer = vec![DARK_COLOUR; WIDTH as usize * HEIGHT as usize]; - window - .update_with_buffer(&native_buffer, WIDTH as usize, HEIGHT as usize) - .unwrap(); + let native_buffer = Arc::new(Mutex::new(native_buffer)); - let xns = xous_names::XousNames::new().unwrap(); - let kbd = - keyboard::Keyboard::new(&xns).expect("GFX|hosted can't connect to KBD for emulation"); - let keyboard_handler = Box::new(XousKeyboardHandler { - kbd: kbd, - left_shift: false, - right_shift: false, - }); - window.set_input_callback(keyboard_handler); + // Spawn a GUI thread + let thread_params = MinifbThread { + native_buffer: Arc::clone(&native_buffer), + }; + std::thread::Builder::new() + .name("minifb".into()) + .spawn(move || thread_params.run()) + .unwrap(); XousDisplay { native_buffer, - window, emulated_buffer: [0u32; FB_SIZE], srfb: [0u32; FB_SIZE], devboot: true, @@ -88,9 +71,9 @@ impl XousDisplay { self.srfb.copy_from_slice(&self.emulated_buffer); } pub fn pop(&mut self) { - self.emulated_buffer[FB_WIDTH_WORDS*32..].copy_from_slice(&self.srfb[FB_WIDTH_WORDS*32..]); + self.emulated_buffer[FB_WIDTH_WORDS * 32..] + .copy_from_slice(&self.srfb[FB_WIDTH_WORDS * 32..]); self.redraw(); - self.update(); } pub fn screen_size(&self) -> Point { @@ -101,6 +84,7 @@ impl XousDisplay { for (dest, src) in self.emulated_buffer.iter_mut().zip(bmp.iter()) { *dest = *src; } + self.emulated_to_native(); } pub fn as_slice(&self) -> &[u32] { &self.emulated_buffer @@ -112,24 +96,13 @@ impl XousDisplay { pub fn redraw(&mut self) { self.emulated_to_native(); - self.window - .update_with_buffer(&self.native_buffer, WIDTH as usize, HEIGHT as usize) - .unwrap(); - } - - pub fn update(&mut self) { - self.emulated_to_native(); - self.window.update(); - if !self.window.is_open() || self.window.is_key_down(Key::Escape) { - std::process::exit(0); - } } fn emulated_to_native(&mut self) { const DEVBOOT_LINE: usize = 7; + let mut native_buffer = self.native_buffer.lock().unwrap(); let mut row = 0; - for (dest_row, src_row) in self - .native_buffer + for (dest_row, src_row) in native_buffer .chunks_mut(WIDTH as _) .zip(self.emulated_buffer.chunks(WIDTH_WORDS as _)) { @@ -152,6 +125,58 @@ impl XousDisplay { } } +impl MinifbThread { + pub fn run(self) -> ! { + let mut window = Window::new( + "Precursor", + WIDTH as usize, + HEIGHT as usize, + WindowOptions { + scale_mode: minifb::ScaleMode::AspectRatioStretch, + resize: true, + ..WindowOptions::default() + }, + ) + .unwrap_or_else(|e| { + log::error!("{e:?}"); + std::process::abort(); + }); + + // Limit the maximum update rate + window.limit_update_rate(Some(std::time::Duration::from_micros( + 1000 * 1000 / MAX_FPS, + ))); + + let xns = xous_names::XousNames::new().unwrap(); + let kbd = + keyboard::Keyboard::new(&xns).expect("GFX|hosted can't connect to KBD for emulation"); + let keyboard_handler = Box::new(XousKeyboardHandler { + kbd, + left_shift: false, + right_shift: false, + }); + window.set_input_callback(keyboard_handler); + + let mut native_buffer = Vec::new(); + + loop { + // Copy the contents of `native_buffer`. Release the lock + // immediately so as not to starve the server thread. + native_buffer.clear(); + native_buffer.extend_from_slice(&self.native_buffer.lock().unwrap()); + + // Render the contents of the minifb window and handle input events. + // This may block to regulate the update rate. + window + .update_with_buffer(&native_buffer, WIDTH as usize, HEIGHT as usize) + .unwrap(); + if !window.is_open() || window.is_key_down(Key::Escape) { + std::process::exit(0); + } + } + } +} + impl XousKeyboardHandler { fn decode_key(&mut self, k: Key) -> char { let shift = self.left_shift || self.right_shift; diff --git a/services/graphics-server/src/main.rs b/services/graphics-server/src/main.rs index a0bb3181f..9f9e53921 100644 --- a/services/graphics-server/src/main.rs +++ b/services/graphics-server/src/main.rs @@ -392,7 +392,6 @@ fn wrapped_main() -> ! { } Some(Opcode::Flush) => { log::trace!("***gfx flush*** redraw##"); - display.update(); display.redraw(); } Some(Opcode::Clear) => { @@ -455,12 +454,10 @@ fn wrapped_main() -> ! { }), Some(Opcode::DrawSleepScreen) => msg_scalar_unpack!(msg, _, _, _, _, { display.blit_screen(&logo::LOGO_MAP); - display.update(); display.redraw(); }), Some(Opcode::DrawBootLogo) => msg_scalar_unpack!(msg, _, _, _, _, { display.blit_screen(&poweron::LOGO_MAP); - display.update(); display.redraw(); }), Some(Opcode::Devboot) => msg_scalar_unpack!(msg, ena, _, _, _, { @@ -543,7 +540,6 @@ fn wrapped_main() -> ! { testpat[lines * backend::FB_WIDTH_WORDS + (backend::FB_WIDTH_WORDS - 1)] |= 0x1_0000; } display.blit_screen(testpat); - display.update(); display.redraw(); ticktimer.sleep_ms(DWELL).unwrap(); @@ -553,7 +549,6 @@ fn wrapped_main() -> ! { } // dirty bits already set display.blit_screen(testpat); - display.update(); display.redraw(); ticktimer.sleep_ms(DWELL).unwrap(); @@ -564,7 +559,6 @@ fn wrapped_main() -> ! { } } display.blit_screen(testpat); - display.update(); display.redraw(); ticktimer.sleep_ms(DWELL).unwrap(); @@ -574,7 +568,6 @@ fn wrapped_main() -> ! { } } display.blit_screen(testpat); - display.update(); display.redraw(); ticktimer.sleep_ms(DWELL).unwrap(); @@ -590,7 +583,6 @@ fn wrapped_main() -> ! { testpat[lines * backend::FB_WIDTH_WORDS + (backend::FB_WIDTH_WORDS - 1)] |= 0x1_0000; } display.blit_screen(testpat); - display.update(); display.redraw(); ticktimer.sleep_ms(DWELL).unwrap(); @@ -605,7 +597,6 @@ fn wrapped_main() -> ! { testpat[lines * backend::FB_WIDTH_WORDS + (backend::FB_WIDTH_WORDS - 1)] |= 0x1_0000; } display.blit_screen(testpat); - display.update(); display.redraw(); ticktimer.sleep_ms(DWELL).unwrap();