Receive in a thread to avoid blocking the window event handling

This commit is contained in:
Juergen Stuber 2023-04-16 19:13:11 +02:00
parent 80f8482a95
commit 534ddba827

View File

@ -1,7 +1,14 @@
use core::mem::swap;
use core::sync::atomic::AtomicBool;
use core::sync::atomic::Ordering;
use std::error; use std::error;
use std::io::Read; use std::io::Read;
use std::path; use std::path;
use std::process; use std::process;
use std::sync::Arc;
use std::sync::Mutex;
use std::thread;
use clap::Parser; use clap::Parser;
@ -28,13 +35,29 @@ struct Cli {
debug: u8, debug: u8,
} }
struct Buffer {
frame: Vec<u8>,
count: u64,
}
impl Buffer {
fn new(size: usize) -> Buffer {
Buffer {
frame: vec![0; size],
count: 0,
}
}
fn is_newer(&self, other: &Buffer) -> bool {
self.count > other.count
}
}
fn main() -> Result<(), Box<dyn error::Error>> { fn main() -> Result<(), Box<dyn error::Error>> {
let cli = Cli::parse(); let cli = Cli::parse();
if cli.debug >= 2 { if cli.debug >= 2 {
eprintln!("CLI: {:?}", cli); eprintln!("CLI: {:?}", cli);
} }
let mut child = process::Command::new(&cli.executable) let mut app = process::Command::new(&cli.executable)
.arg(cli.cols.to_string()) .arg(cli.cols.to_string())
.arg(cli.rows.to_string()) .arg(cli.rows.to_string())
.args(cli.args) .args(cli.args)
@ -44,32 +67,60 @@ fn main() -> Result<(), Box<dyn error::Error>> {
eprintln!( eprintln!(
"Running {} as a child process with pid {}", "Running {} as a child process with pid {}",
cli.executable.display(), cli.executable.display(),
child.id() app.id()
); );
} }
let mut stdout = child let mut stdout = app
.stdout .stdout
.take() .take()
.ok_or("opening child process stdout failed")?; .ok_or("opening app process stdout failed")?;
// TODO support also RGBW // TODO support also RGBW
let colors = 3; let colors = 3;
// TODO check for overflow and excessive size // TODO check for overflow and excessive size
let buffer_size = cli.cols * cli.rows * colors; let buffer_size = cli.cols * cli.rows * colors;
let mut buffer = vec![0_u8; buffer_size];
let mut frame_count = 0; let mut frame_count = 0;
let x_size = cli.cols;
let y_size = cli.rows;
let mut window: PistonWindow = WindowSettings::new( let mut window: PistonWindow = WindowSettings::new(
format!("pixelfoo-viewer {}", cli.executable.display()), format!("pixelfoo-viewer {}", cli.executable.display()),
[1600, 800], [20 * (x_size as u32), 20 * (y_size as u32)],
) )
.exit_on_esc(true) .exit_on_esc(true)
.vsync(true) .vsync(true)
.build()?; .build()?;
let x_size = cli.cols; // Shared buffer for one frame of data from the app.
let y_size = cli.rows; let buffer0 = Arc::new(Mutex::new(Buffer::new(buffer_size)));
let buffer1 = buffer0.clone();
let receiver_stop = AtomicBool::new(false);
thread::scope(|scope| {
let receiver_thread = scope.spawn(|| {
let mut buffer = Buffer::new(buffer_size);
while !receiver_stop.load(Ordering::Relaxed) {
// Read a frame from app process.
stdout
.read_exact(&mut buffer.frame)
.expect("pipe read failed");
if cli.debug >= 3 {
eprintln!("Received frame {}", frame_count);
}
frame_count += 1;
buffer.count = frame_count;
// Put the frame in the shared buffer.
{
let mut b = buffer0.lock().unwrap();
swap(&mut *b, &mut buffer);
}
}
});
let mut buffer = Buffer::new(buffer_size);
while let Some(event) = window.next() { while let Some(event) = window.next() {
window.draw_2d(&event, |context, graphics, _device| { window.draw_2d(&event, |context, graphics, _device| {
let [vsx, vsy] = context.get_view_size(); let [vsx, vsy] = context.get_view_size();
@ -81,26 +132,33 @@ fn main() -> Result<(), Box<dyn error::Error>> {
pw::clear([0.5, 0.5, 0.5, 1.0], graphics); pw::clear([0.5, 0.5, 0.5, 1.0], graphics);
stdout.read_exact(&mut buffer).expect("pipe read failed"); // Swap the shared buffer with our own buffer if it is newer.
if cli.debug >= 3 { {
eprintln!("Received frame {}", frame_count); let mut b = buffer1.lock().unwrap();
if b.is_newer(&buffer) {
swap(&mut *b, &mut buffer);
} }
};
// Display the buffer.
for y in 0..y_size { for y in 0..y_size {
for x in 0..x_size { for x in 0..x_size {
let i = colors * (y * x_size + x); let i = colors * (y * x_size + x);
let r = f32::from(buffer[i + 0]) / 255.0; let r = f32::from(buffer.frame[i + 0]) / 255.0;
let g = f32::from(buffer[i + 1]) / 255.0; let g = f32::from(buffer.frame[i + 1]) / 255.0;
let b = f32::from(buffer[i + 2]) / 255.0; let b = f32::from(buffer.frame[i + 2]) / 255.0;
let color = [r, g, b, 1.0]; let color = [r, g, b, 1.0];
let rectangle = [(x as f64) * cs, (y as f64) * cs, cs, cs]; let rectangle = [(x as f64) * cs, (y as f64) * cs, cs, cs];
pw::rectangle(color, rectangle, context.transform, graphics); pw::rectangle(color, rectangle, context.transform, graphics);
} }
} }
}); });
frame_count += 1;
} }
child.kill()?; receiver_stop.store(true, Ordering::Relaxed);
let _result = receiver_thread.join();
});
app.kill()?;
Ok(()) Ok(())
} }