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

View File

@ -4,6 +4,7 @@ use crate::lib8::Rgb8Blend;
use crate::render::{Surface, SurfacePool, Display, Surfaces}; use crate::render::{Surface, SurfacePool, Display, Surfaces};
use crate::power::{brightness_for_mw, AsMilliwatts}; use crate::power::{brightness_for_mw, AsMilliwatts};
use crate::geometry::*; use crate::geometry::*;
use crate::mappings::*;
use std::fmt::{Debug, Formatter}; use std::fmt::{Debug, Formatter};
use smart_leds::brightness; use smart_leds::brightness;
@ -13,7 +14,8 @@ use std::io;
use rgb::Rgb; use rgb::Rgb;
pub struct SmartLedDisplay<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface, const PIXEL_NUM: usize> { 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, target: T,
pixbuf: [T::Color; PIXEL_NUM], pixbuf: [T::Color; PIXEL_NUM],
max_mw: u32 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> { 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 { pub fn new(target: T, max_mw: u32) -> Self {
SmartLedDisplay { SmartLedDisplay {
surfaces: SurfacePool::new(),
pixbuf: [Rgb::new(0, 0, 0); PIXEL_NUM], pixbuf: [Rgb::new(0, 0, 0); PIXEL_NUM],
surfaces: Some(SurfacePool::new()),
target: target, target: target,
max_mw: max_mw, 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 where
T: SmartLedsWrite<Color = Rgb<u8>>, T: SmartLedsWrite<Color = Rgb<u8>>,
S: Surface { S: Surface {
fn new_surface(&mut self) -> Result<S, io::Error> { fn new_surface(&mut self, area: &Rectangle<u8, Virtual>) -> Result<S, io::Error> {
self.surfaces.new_surface() 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) { fn render_frame(&mut self) {
for x in 0..self.pixbuf.len() { let surfaces = self.surfaces.take().unwrap();
let virt_coords = VirtualCoordinates::new(x as u8, 0); for surface in surfaces.iter() {
let mut pixel = Rgb::new(0, 0, 0); let rect = surface.rect().clone();
for surface in self.surfaces.iter() { let mut sel = self.pixmap.select(&rect);
surface.with_shader(|shader| { surface.with_shader(|shader| {
pixel = pixel.saturating_add(shader.draw(&virt_coords)); 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.pixbuf[x] = Rgb::new(pixel.r, pixel.g, pixel.b); 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 with_shader<F: FnMut(&dyn Shader)>(&self, f: F);
fn set_shader(&mut self, shader: Box<dyn Shader>); fn set_shader(&mut self, shader: Box<dyn Shader>);
fn clear_shader(&mut self); fn clear_shader(&mut self);
fn rect(&self) -> Rectangle<u8, Virtual>;
} }
pub trait Surfaces<T: Surface>: Debug { 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> { pub trait Display<T: Surface>: Surfaces<T> {
@ -69,6 +71,7 @@ impl<T: Display<S>, S: Surface> Task for Renderer<T, S> {
#[derive(Debug)] #[derive(Debug)]
pub struct ShaderBinding { pub struct ShaderBinding {
shader: Option<Box<dyn Shader>>, shader: Option<Box<dyn Shader>>,
rect: Rectangle<u8, Virtual>
} }
#[derive(Clone)] #[derive(Clone)]
@ -91,12 +94,17 @@ impl Default for BoundSurface<Rc<RefCell<ShaderBinding>>>{
Self { Self {
binding: Rc::new(RefCell::new(ShaderBinding { binding: Rc::new(RefCell::new(ShaderBinding {
shader: None, shader: None,
rect: Rectangle::everything()
})), })),
} }
} }
} }
impl Surface for BoundSurface<Rc<RefCell<ShaderBinding>>> { 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) { fn with_shader<F: FnMut(&dyn Shader)>(&self, mut f: F) {
if let Some(ref shader) = self.binding.borrow().shader { if let Some(ref shader) = self.binding.borrow().shader {
f(shader.as_ref()); f(shader.as_ref());
@ -175,8 +183,8 @@ impl<S: Surface + Default> SurfacePool<S> {
} }
} }
impl<S: Surface + Default + Clone> Surfaces<S> for SurfacePool<S> { impl<S: Surface + Default> Surfaces<S> for SurfacePool<S> {
fn new_surface(&mut self) -> Result<S, io::Error> { fn new_surface(&mut self, _area: &Rectangle<u8, Virtual>) -> Result<S, io::Error> {
let surface = S::default(); let surface = S::default();
self.surfaces.push(surface.clone()); self.surfaces.push(surface.clone());
return Ok(surface); return Ok(surface);