|
|
|
@ -1,5 +1,5 @@
|
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
|
use std::sync::atomic::{AtomicU16, Ordering};
|
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
|
use std::thread;
|
|
|
|
|
|
|
|
|
@ -9,56 +9,17 @@ lazy_static! {
|
|
|
|
|
pub static ref REDRAW_SCHEDULER: RedrawScheduler = RedrawScheduler::new();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const BUFFER_FRAMES: u16 = 60;
|
|
|
|
|
|
|
|
|
|
pub struct RedrawScheduler {
|
|
|
|
|
window: Mutex<Option<Arc<Window>>>, // Would prefer not to have to lock this every time.
|
|
|
|
|
frame_queued: AtomicBool,
|
|
|
|
|
frames_queued: AtomicU16,
|
|
|
|
|
scheduled_frame: Mutex<Option<Instant>>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn redraw_loop() {
|
|
|
|
|
thread::spawn(|| {
|
|
|
|
|
loop {
|
|
|
|
|
let frame_start = Instant::now();
|
|
|
|
|
|
|
|
|
|
let request_redraw = {
|
|
|
|
|
if REDRAW_SCHEDULER.frame_queued.load(Ordering::Relaxed) {
|
|
|
|
|
REDRAW_SCHEDULER.frame_queued.store(false, Ordering::Relaxed);
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
let mut next_scheduled_frame = REDRAW_SCHEDULER.scheduled_frame.lock().unwrap();
|
|
|
|
|
if let Some(scheduled_frame) = next_scheduled_frame.clone() {
|
|
|
|
|
if scheduled_frame < frame_start {
|
|
|
|
|
*next_scheduled_frame = None;
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if request_redraw {
|
|
|
|
|
let window = REDRAW_SCHEDULER.window.lock().unwrap();
|
|
|
|
|
if let Some(window) = &*window {
|
|
|
|
|
window.request_redraw();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(time_to_sleep) = Duration::from_secs_f32(1.0 / 60.0).checked_sub(frame_start.elapsed()) {
|
|
|
|
|
thread::sleep(time_to_sleep)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl RedrawScheduler {
|
|
|
|
|
pub fn new() -> RedrawScheduler {
|
|
|
|
|
redraw_loop();
|
|
|
|
|
RedrawScheduler {
|
|
|
|
|
window: Mutex::new(None),
|
|
|
|
|
frame_queued: AtomicBool::new(false),
|
|
|
|
|
frames_queued: AtomicU16::new(1),
|
|
|
|
|
scheduled_frame: Mutex::new(None)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -75,11 +36,26 @@ impl RedrawScheduler {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn queue_next_frame(&self) {
|
|
|
|
|
self.frame_queued.store(true, Ordering::Relaxed);
|
|
|
|
|
self.frames_queued.store(BUFFER_FRAMES, Ordering::Relaxed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_window(&self, window: &Arc<Window>){
|
|
|
|
|
let mut window_ref = self.window.lock().unwrap();
|
|
|
|
|
window_ref.replace(window.clone());
|
|
|
|
|
pub fn should_draw(&self) -> bool {
|
|
|
|
|
let frames_queued = self.frames_queued.load(Ordering::Relaxed);
|
|
|
|
|
if frames_queued > 0 {
|
|
|
|
|
self.frames_queued.store(frames_queued - 1, Ordering::Relaxed);
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
let mut next_scheduled_frame = self.scheduled_frame.lock().unwrap();
|
|
|
|
|
if let Some(scheduled_frame) = next_scheduled_frame.clone() {
|
|
|
|
|
if scheduled_frame < Instant::now() {
|
|
|
|
|
*next_scheduled_frame = None;
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|