Receive in a thread to avoid blocking the window event handling
This commit is contained in:
parent
80f8482a95
commit
534ddba827
126
src/main.rs
126
src/main.rs
@ -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,63 +67,98 @@ 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)));
|
||||||
while let Some(event) = window.next() {
|
let buffer1 = buffer0.clone();
|
||||||
window.draw_2d(&event, |context, graphics, _device| {
|
|
||||||
let [vsx, vsy] = context.get_view_size();
|
|
||||||
|
|
||||||
// Compute cell size
|
let receiver_stop = AtomicBool::new(false);
|
||||||
let csx = (vsx as f64) / (x_size as f64);
|
thread::scope(|scope| {
|
||||||
let csy = (vsy as f64) / (y_size as f64);
|
let receiver_thread = scope.spawn(|| {
|
||||||
let cs = csx.min(csy);
|
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;
|
||||||
|
|
||||||
pw::clear([0.5, 0.5, 0.5, 1.0], graphics);
|
// Put the frame in the shared buffer.
|
||||||
|
{
|
||||||
stdout.read_exact(&mut buffer).expect("pipe read failed");
|
let mut b = buffer0.lock().unwrap();
|
||||||
if cli.debug >= 3 {
|
swap(&mut *b, &mut buffer);
|
||||||
eprintln!("Received frame {}", frame_count);
|
|
||||||
}
|
|
||||||
|
|
||||||
for y in 0..y_size {
|
|
||||||
for x in 0..x_size {
|
|
||||||
let i = colors * (y * x_size + x);
|
|
||||||
let r = f32::from(buffer[i + 0]) / 255.0;
|
|
||||||
let g = f32::from(buffer[i + 1]) / 255.0;
|
|
||||||
let b = f32::from(buffer[i + 2]) / 255.0;
|
|
||||||
let color = [r, g, b, 1.0];
|
|
||||||
let rectangle = [(x as f64) * cs, (y as f64) * cs, cs, cs];
|
|
||||||
pw::rectangle(color, rectangle, context.transform, graphics);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
frame_count += 1;
|
|
||||||
}
|
let mut buffer = Buffer::new(buffer_size);
|
||||||
child.kill()?;
|
while let Some(event) = window.next() {
|
||||||
|
window.draw_2d(&event, |context, graphics, _device| {
|
||||||
|
let [vsx, vsy] = context.get_view_size();
|
||||||
|
|
||||||
|
// Compute cell size
|
||||||
|
let csx = (vsx as f64) / (x_size as f64);
|
||||||
|
let csy = (vsy as f64) / (y_size as f64);
|
||||||
|
let cs = csx.min(csy);
|
||||||
|
|
||||||
|
pw::clear([0.5, 0.5, 0.5, 1.0], graphics);
|
||||||
|
|
||||||
|
// Swap the shared buffer with our own buffer if it is newer.
|
||||||
|
{
|
||||||
|
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 x in 0..x_size {
|
||||||
|
let i = colors * (y * x_size + x);
|
||||||
|
let r = f32::from(buffer.frame[i + 0]) / 255.0;
|
||||||
|
let g = f32::from(buffer.frame[i + 1]) / 255.0;
|
||||||
|
let b = f32::from(buffer.frame[i + 2]) / 255.0;
|
||||||
|
let color = [r, g, b, 1.0];
|
||||||
|
let rectangle = [(x as f64) * cs, (y as f64) * cs, cs, cs];
|
||||||
|
pw::rectangle(color, rectangle, context.transform, graphics);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
receiver_stop.store(true, Ordering::Relaxed);
|
||||||
|
let _result = receiver_thread.join();
|
||||||
|
});
|
||||||
|
|
||||||
|
app.kill()?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user