|
|
@ -1,5 +1,5 @@
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
use std::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
use std::sync::atomic::{AtomicU16, Ordering};
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
use std::thread;
|
|
|
|
use std::thread;
|
|
|
|
|
|
|
|
|
|
|
@ -9,56 +9,17 @@ lazy_static! {
|
|
|
|
pub static ref REDRAW_SCHEDULER: RedrawScheduler = RedrawScheduler::new();
|
|
|
|
pub static ref REDRAW_SCHEDULER: RedrawScheduler = RedrawScheduler::new();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const BUFFER_FRAMES: u16 = 60;
|
|
|
|
|
|
|
|
|
|
|
|
pub struct RedrawScheduler {
|
|
|
|
pub struct RedrawScheduler {
|
|
|
|
window: Mutex<Option<Arc<Window>>>, // Would prefer not to have to lock this every time.
|
|
|
|
frames_queued: AtomicU16,
|
|
|
|
frame_queued: AtomicBool,
|
|
|
|
|
|
|
|
scheduled_frame: Mutex<Option<Instant>>
|
|
|
|
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 {
|
|
|
|
impl RedrawScheduler {
|
|
|
|
pub fn new() -> RedrawScheduler {
|
|
|
|
pub fn new() -> RedrawScheduler {
|
|
|
|
redraw_loop();
|
|
|
|
|
|
|
|
RedrawScheduler {
|
|
|
|
RedrawScheduler {
|
|
|
|
window: Mutex::new(None),
|
|
|
|
frames_queued: AtomicU16::new(1),
|
|
|
|
frame_queued: AtomicBool::new(false),
|
|
|
|
|
|
|
|
scheduled_frame: Mutex::new(None)
|
|
|
|
scheduled_frame: Mutex::new(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -75,11 +36,26 @@ impl RedrawScheduler {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn queue_next_frame(&self) {
|
|
|
|
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>){
|
|
|
|
pub fn should_draw(&self) -> bool {
|
|
|
|
let mut window_ref = self.window.lock().unwrap();
|
|
|
|
let frames_queued = self.frames_queued.load(Ordering::Relaxed);
|
|
|
|
window_ref.replace(window.clone());
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|