render: effectively rename Display to Output, push remaining common code into Renderer task
This commit is contained in:
@@ -8,7 +8,6 @@ use crate::power::AsMilliwatts;
|
||||
use crate::task::Task;
|
||||
use crate::time::Periodically;
|
||||
use running_average::RealTimeRunningAverage;
|
||||
use std::marker::PhantomData;
|
||||
use std::fmt::Debug;
|
||||
|
||||
pub trait HardwarePixel: Send + Sync + Rgb8Blend + Copy + AsMilliwatts + Default + From<Rgb<u8>> + Fract8Ops {}
|
||||
@@ -30,8 +29,10 @@ pub trait Shader: Send + Debug {
|
||||
fn draw(&self, surface_coords: &VirtualCoordinates, frame: usize) -> Rgb<u8>;
|
||||
}
|
||||
|
||||
pub trait Surfaces<T: Surface>: Debug {
|
||||
fn new_surface(&mut self, area: &Rectangle<Virtual>) -> Result<T, io::Error>;
|
||||
pub trait Surfaces: Debug {
|
||||
type Surface: Surface;
|
||||
fn new_surface(&mut self, area: &Rectangle<Virtual>) -> Result<Self::Surface, io::Error>;
|
||||
fn render_to<S: Sample>(&self, output: &mut S, frame: usize);
|
||||
}
|
||||
|
||||
pub trait Surface: Default + Clone + Debug {
|
||||
@@ -45,41 +46,46 @@ pub trait Surface: Default + Clone + Debug {
|
||||
fn set_opacity(&mut self, opacity: u8);
|
||||
}
|
||||
|
||||
pub trait Display<T: Surface>: Surfaces<T> {
|
||||
fn start_frame(&mut self) {}
|
||||
fn render_frame(&mut self);
|
||||
fn end_frame(&mut self) {}
|
||||
pub trait Output: Sample {
|
||||
fn blank(&mut self);
|
||||
fn commit(&mut self);
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Renderer<T: Display<S>, S: Surface> {
|
||||
display: T,
|
||||
pub struct Renderer<T: Output, S: Surfaces> {
|
||||
output: T,
|
||||
surfaces: S,
|
||||
fps: RealTimeRunningAverage<u32>,
|
||||
fps_display: Periodically,
|
||||
_sfc: PhantomData<S>
|
||||
frame: usize
|
||||
}
|
||||
|
||||
impl<T: Display<S>, S: Surface> Renderer<T, S> {
|
||||
pub fn new(display: T) -> Self {
|
||||
impl<T: Output, S: Surfaces> Renderer<T, S> {
|
||||
pub fn new(output: T, surfaces: S) -> Self {
|
||||
Self {
|
||||
display,
|
||||
output,
|
||||
surfaces: surfaces,
|
||||
fps: RealTimeRunningAverage::default(),
|
||||
fps_display: Periodically::new_every_n_seconds(5),
|
||||
_sfc: PhantomData
|
||||
frame: 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Display<S>, S: Surface> Task for Renderer<T, S> {
|
||||
impl<T: Output + Debug, S: Surfaces> Task for Renderer<T, S> {
|
||||
fn name(&self) -> &'static str { "Renderer" }
|
||||
|
||||
fn tick(&mut self) {
|
||||
self.display.start_frame();
|
||||
self.display.render_frame();
|
||||
self.display.end_frame();
|
||||
self.output.blank();
|
||||
|
||||
self.surfaces.render_to(&mut self.output, self.frame);
|
||||
|
||||
self.output.commit();
|
||||
|
||||
self.fps.insert(1);
|
||||
self.frame += 1;
|
||||
self.fps_display.run(|| {
|
||||
log::info!("FPS: {} {:?}", self.fps.measurement(), self.display);
|
||||
log::info!("FPS: {}", self.fps.measurement());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user