92 lines
2.4 KiB
Rust
92 lines
2.4 KiB
Rust
use std::io;
|
|
use rgb::Rgb;
|
|
|
|
use crate::geometry::*;
|
|
use crate::lib8::Rgb8Blend;
|
|
use crate::lib8::interpolate::Fract8Ops;
|
|
use crate::power::AsMilliwatts;
|
|
use crate::task::Task;
|
|
use crate::time::Periodically;
|
|
use running_average::RealTimeRunningAverage;
|
|
use std::fmt::Debug;
|
|
|
|
pub trait HardwarePixel: Send + Sync + Rgb8Blend + Copy + AsMilliwatts + Default + From<Rgb<u8>> + Fract8Ops {}
|
|
impl<T> HardwarePixel for T where T: Send + Sync + Rgb8Blend + Copy + AsMilliwatts + Default + From<Rgb<u8>> + Fract8Ops {}
|
|
|
|
pub trait PixelView {
|
|
type Pixel: HardwarePixel;
|
|
fn next(&mut self) -> Option<(Coordinates<Virtual>, &mut Self::Pixel)>;
|
|
}
|
|
|
|
pub trait Sample {
|
|
type Pixel: HardwarePixel;
|
|
|
|
fn sample(&mut self, rect: &Rectangle<Virtual>) -> impl PixelView<Pixel = Self::Pixel>;
|
|
}
|
|
|
|
|
|
pub trait Shader: Send + Debug {
|
|
fn draw(&self, surface_coords: &VirtualCoordinates, frame: usize) -> Rgb<u8>;
|
|
}
|
|
|
|
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 {
|
|
fn with_shader<F: FnMut(&dyn Shader)>(&self, f: F);
|
|
fn set_shader(&mut self, shader: Box<dyn Shader>);
|
|
fn clear_shader(&mut self);
|
|
|
|
fn rect(&self) -> Rectangle<Virtual>;
|
|
fn set_rect(&mut self, rect: &Rectangle<Virtual>);
|
|
fn opacity(&self) -> u8;
|
|
fn set_opacity(&mut self, opacity: u8);
|
|
}
|
|
|
|
pub trait Output: Sample {
|
|
fn blank(&mut self);
|
|
fn commit(&mut self);
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct Renderer<T: Output, S: Surfaces> {
|
|
output: T,
|
|
surfaces: S,
|
|
fps: RealTimeRunningAverage<u32>,
|
|
fps_display: Periodically,
|
|
frame: usize
|
|
}
|
|
|
|
impl<T: Output, S: Surfaces> Renderer<T, S> {
|
|
pub fn new(output: T, surfaces: S) -> Self {
|
|
Self {
|
|
output,
|
|
surfaces: surfaces,
|
|
fps: RealTimeRunningAverage::default(),
|
|
fps_display: Periodically::new_every_n_seconds(5),
|
|
frame: 0
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<T: Output + Debug, S: Surfaces> Task for Renderer<T, S> {
|
|
fn name(&self) -> &'static str { "Renderer" }
|
|
|
|
fn tick(&mut self) {
|
|
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());
|
|
});
|
|
}
|
|
}
|