From e51ac02dc6ca24df3d05dbfa9dd5fdf1e58ce452 Mon Sep 17 00:00:00 2001 From: Victoria Fischer Date: Sat, 16 Nov 2024 12:13:49 +0100 Subject: [PATCH] render: implement surface mapping api --- src/main.rs | 5 +++-- src/platform/smart_leds_lib.rs | 40 ++++++++++++++++++++++------------ src/render.rs | 14 +++++++++--- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index 55f0810..bba86b3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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)), ]); diff --git a/src/platform/smart_leds_lib.rs b/src/platform/smart_leds_lib.rs index 5760ba4..f6ca3ec 100644 --- a/src/platform/smart_leds_lib.rs +++ b/src/platform/smart_leds_lib.rs @@ -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>, S: Surface, const PIXEL_NUM: usize> { - surfaces : SurfacePool, + surfaces : Option>, + pixmap: StrideMapping, target: T, pixbuf: [T::Color; PIXEL_NUM], max_mw: u32 @@ -31,10 +33,11 @@ impl>, S: Surface, const PIXEL_NUM: usize> Deb impl>, S: Surface, const PIXEL_NUM: usize> SmartLedDisplay { 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 Surfaces for SmartLedDisplay>, S: Surface { - fn new_surface(&mut self) -> Result { - self.surfaces.new_surface() + fn new_surface(&mut self, area: &Rectangle) -> Result { + if let Some(ref mut s) = self.surfaces { + s.new_surface(area) + } else { + panic!("Could not grab surface list") + } } } @@ -66,16 +73,21 @@ impl>, 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); } } diff --git a/src/render.rs b/src/render.rs index 21d33a7..d7897e9 100644 --- a/src/render.rs +++ b/src/render.rs @@ -21,10 +21,12 @@ pub trait Surface: Default + Clone + Debug { fn with_shader(&self, f: F); fn set_shader(&mut self, shader: Box); fn clear_shader(&mut self); + + fn rect(&self) -> Rectangle; } pub trait Surfaces: Debug { - fn new_surface(&mut self) -> Result; + fn new_surface(&mut self, area: &Rectangle) -> Result; } pub trait Display: Surfaces { @@ -69,6 +71,7 @@ impl, S: Surface> Task for Renderer { #[derive(Debug)] pub struct ShaderBinding { shader: Option>, + rect: Rectangle } #[derive(Clone)] @@ -91,12 +94,17 @@ impl Default for BoundSurface>>{ Self { binding: Rc::new(RefCell::new(ShaderBinding { shader: None, + rect: Rectangle::everything() })), } } } impl Surface for BoundSurface>> { + fn rect(&self) -> Rectangle { + self.binding.borrow().rect.clone() + } + fn with_shader(&self, mut f: F) { if let Some(ref shader) = self.binding.borrow().shader { f(shader.as_ref()); @@ -175,8 +183,8 @@ impl SurfacePool { } } -impl Surfaces for SurfacePool { - fn new_surface(&mut self) -> Result { +impl Surfaces for SurfacePool { + fn new_surface(&mut self, _area: &Rectangle) -> Result { let surface = S::default(); self.surfaces.push(surface.clone()); return Ok(surface);