render: implement surface mapping api

This commit is contained in:
Victoria Fischer 2024-11-16 12:13:49 +01:00
parent 10f0eaa75e
commit e51ac02dc6
3 changed files with 40 additions and 19 deletions

View File

@ -10,6 +10,7 @@ mod mappings;
use crate::platform::DisplayInit;
use crate::render::Surfaces;
use crate::geometry::Rectangle;
#[cfg(feature="embedded-graphics")]
#[cfg(feature="rmt")]
@ -47,8 +48,8 @@ fn main() {
log::info!("Creating runner");
let mut runner = task::Scheduler::new(vec![
Box::new(animations::IdleTask::new(display.new_surface().unwrap())),
Box::new(animations::TestPattern::new(display.new_surface().unwrap())),
Box::new(animations::IdleTask::new(display.new_surface(&Rectangle::everything()).unwrap())),
Box::new(animations::TestPattern::new(display.new_surface(&Rectangle::everything()).unwrap())),
Box::new(Renderer::new(display)),
]);

View File

@ -4,6 +4,7 @@ use crate::lib8::Rgb8Blend;
use crate::render::{Surface, SurfacePool, Display, Surfaces};
use crate::power::{brightness_for_mw, AsMilliwatts};
use crate::geometry::*;
use crate::mappings::*;
use std::fmt::{Debug, Formatter};
use smart_leds::brightness;
@ -13,7 +14,8 @@ use std::io;
use rgb::Rgb;
pub struct SmartLedDisplay<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface, const PIXEL_NUM: usize> {
surfaces : SurfacePool<S>,
surfaces : Option<SurfacePool<S>>,
pixmap: StrideMapping,
target: T,
pixbuf: [T::Color; PIXEL_NUM],
max_mw: u32
@ -31,10 +33,11 @@ impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface, const PIXEL_NUM: usize> Deb
impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface, const PIXEL_NUM: usize> SmartLedDisplay<T, S, PIXEL_NUM> {
pub fn new(target: T, max_mw: u32) -> Self {
SmartLedDisplay {
surfaces: SurfacePool::new(),
pixbuf: [Rgb::new(0, 0, 0); PIXEL_NUM],
surfaces: Some(SurfacePool::new()),
target: target,
max_mw: max_mw,
pixmap: StrideMapping::new()
}
}
}
@ -49,8 +52,12 @@ impl<T, S, const PIXEL_NUM: usize> Surfaces<S> for SmartLedDisplay<T, S, PIXEL_N
where
T: SmartLedsWrite<Color = Rgb<u8>>,
S: Surface {
fn new_surface(&mut self) -> Result<S, io::Error> {
self.surfaces.new_surface()
fn new_surface(&mut self, area: &Rectangle<u8, Virtual>) -> Result<S, io::Error> {
if let Some(ref mut s) = self.surfaces {
s.new_surface(area)
} else {
panic!("Could not grab surface list")
}
}
}
@ -66,16 +73,21 @@ impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface, const PIXEL_NUM: usize> Dis
}
fn render_frame(&mut self) {
for x in 0..self.pixbuf.len() {
let virt_coords = VirtualCoordinates::new(x as u8, 0);
let mut pixel = Rgb::new(0, 0, 0);
for surface in self.surfaces.iter() {
surface.with_shader(|shader| {
pixel = pixel.saturating_add(shader.draw(&virt_coords));
})
}
self.pixbuf[x] = Rgb::new(pixel.r, pixel.g, pixel.b);
};
let surfaces = self.surfaces.take().unwrap();
for surface in surfaces.iter() {
let rect = surface.rect().clone();
let mut sel = self.pixmap.select(&rect);
surface.with_shader(|shader| {
while let Some((virt_coords, phys_coords)) = sel.next() {
let idx = phys_coords.x as usize;
if idx >= PIXEL_NUM {
continue;
}
self.pixbuf[idx] = self.pixbuf[idx].saturating_add(shader.draw(&virt_coords));
}
})
}
self.surfaces = Some(surfaces);
}
}

View File

@ -21,10 +21,12 @@ 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<u8, Virtual>;
}
pub trait Surfaces<T: Surface>: Debug {
fn new_surface(&mut self) -> Result<T, io::Error>;
fn new_surface(&mut self, area: &Rectangle<u8, Virtual>) -> Result<T, io::Error>;
}
pub trait Display<T: Surface>: Surfaces<T> {
@ -69,6 +71,7 @@ impl<T: Display<S>, S: Surface> Task for Renderer<T, S> {
#[derive(Debug)]
pub struct ShaderBinding {
shader: Option<Box<dyn Shader>>,
rect: Rectangle<u8, Virtual>
}
#[derive(Clone)]
@ -91,12 +94,17 @@ impl Default for BoundSurface<Rc<RefCell<ShaderBinding>>>{
Self {
binding: Rc::new(RefCell::new(ShaderBinding {
shader: None,
rect: Rectangle::everything()
})),
}
}
}
impl Surface for BoundSurface<Rc<RefCell<ShaderBinding>>> {
fn rect(&self) -> Rectangle<u8, Virtual> {
self.binding.borrow().rect.clone()
}
fn with_shader<F: FnMut(&dyn Shader)>(&self, mut f: F) {
if let Some(ref shader) = self.binding.borrow().shader {
f(shader.as_ref());
@ -175,8 +183,8 @@ impl<S: Surface + Default> SurfacePool<S> {
}
}
impl<S: Surface + Default + Clone> Surfaces<S> for SurfacePool<S> {
fn new_surface(&mut self) -> Result<S, io::Error> {
impl<S: Surface + Default> Surfaces<S> for SurfacePool<S> {
fn new_surface(&mut self, _area: &Rectangle<u8, Virtual>) -> Result<S, io::Error> {
let surface = S::default();
self.surfaces.push(surface.clone());
return Ok(surface);