buffers: split out concrete surface and pixbuf implementations into a buffers module
This commit is contained in:
parent
821924cddb
commit
e651608ecc
140
src/buffers.rs
Normal file
140
src/buffers.rs
Normal file
@ -0,0 +1,140 @@
|
||||
use crate::geometry::*;
|
||||
use crate::render::{Surface, Shader, Surfaces};
|
||||
|
||||
use std::fmt::{Debug, Formatter};
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
use std::io;
|
||||
|
||||
#[cfg(feature="threads")]
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ShaderBinding {
|
||||
shader: Option<Box<dyn Shader>>,
|
||||
rect: Rectangle<u8, Virtual>
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct BoundSurface<T> {
|
||||
pub binding: T
|
||||
}
|
||||
|
||||
impl Debug for BoundSurface<Rc<RefCell<ShaderBinding>>> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("BoundSurface")
|
||||
.field("shader", &self.binding.borrow().shader)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
pub type SimpleSurface = BoundSurface<Rc<RefCell<ShaderBinding>>>;
|
||||
|
||||
impl Default for BoundSurface<Rc<RefCell<ShaderBinding>>>{
|
||||
fn default() -> Self {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
fn set_shader(&mut self, shader: Box<dyn Shader>) {
|
||||
self.binding.borrow_mut().shader = Some(shader);
|
||||
}
|
||||
|
||||
fn clear_shader(&mut self) {
|
||||
self.binding.borrow_mut().shader = None;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature="threads")]
|
||||
pub type SharedSurface = BoundSurface<Arc<Mutex<ShaderBinding>>>;
|
||||
|
||||
#[cfg(feature="threads")]
|
||||
impl Default for BoundSurface<Arc<Mutex<ShaderBinding>>> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
binding: Arc::new(Mutex::new(ShaderBinding {
|
||||
shader: None,
|
||||
rect: Rectangle::everything()
|
||||
})),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature="threads")]
|
||||
impl Surface for BoundSurface<Arc<Mutex<ShaderBinding>>> {
|
||||
fn rect(&self) -> Rectangle<u8, Virtual> {
|
||||
let r = self.binding.lock().unwrap();
|
||||
r.rect.clone()
|
||||
//self.binding.lock().unwrap().rect.clone()
|
||||
}
|
||||
|
||||
fn with_shader<F: FnMut(&dyn Shader)>(&self, mut f: F) {
|
||||
if let Some(ref shader) = self.binding.lock().unwrap().shader {
|
||||
f(shader.as_ref());
|
||||
}
|
||||
}
|
||||
|
||||
fn set_shader(&mut self, shader: Box<dyn Shader>) {
|
||||
self.binding.lock().unwrap().shader = Some(shader);
|
||||
}
|
||||
|
||||
fn clear_shader(&mut self) {
|
||||
self.binding.lock().unwrap().shader = None;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature="threads")]
|
||||
impl Debug for BoundSurface<Arc<Mutex<ShaderBinding>>> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("BoundSurface")
|
||||
.field("shader", &self.binding.lock().unwrap().shader)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SurfacePool<S: Surface + Default> {
|
||||
surfaces: Vec<S>
|
||||
}
|
||||
|
||||
impl<S: Surface + Debug> Debug for SurfacePool<S> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.surfaces.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Surface + Default> SurfacePool<S> {
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
surfaces: Vec::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> std::slice::Iter<S> {
|
||||
self.surfaces.iter()
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
@ -7,6 +7,7 @@ mod geometry;
|
||||
mod platform;
|
||||
mod animations;
|
||||
mod mappings;
|
||||
mod buffers;
|
||||
|
||||
use crate::platform::DisplayInit;
|
||||
use crate::render::Surfaces;
|
||||
@ -25,10 +26,10 @@ use ws2812_esp32_rmt_driver::lib_smart_leds::Ws2812Esp32Rmt as DisplayType;
|
||||
use crate::platform::smart_leds_lib::spi::SPIDisplay as DisplayType;
|
||||
|
||||
#[cfg(feature="threads")]
|
||||
use crate::render::SharedSurface as SurfaceType;
|
||||
use crate::buffers::SharedSurface as SurfaceType;
|
||||
|
||||
#[cfg(not(feature="threads"))]
|
||||
use crate::render::SimpleSurface as SurfaceType;
|
||||
use crate::buffers::SimpleSurface as SurfaceType;
|
||||
|
||||
use crate::render::Renderer;
|
||||
|
||||
|
@ -1,7 +1,8 @@
|
||||
use smart_leds_trait::SmartLedsWrite;
|
||||
|
||||
use crate::lib8::Rgb8Blend;
|
||||
use crate::render::{Surface, SurfacePool, Display, Surfaces};
|
||||
use crate::render::{Framed, Surface, Display, Surfaces};
|
||||
use crate::buffers::SurfacePool;
|
||||
use crate::power::{brightness_for_mw, AsMilliwatts};
|
||||
use crate::geometry::*;
|
||||
use crate::mappings::*;
|
||||
@ -96,20 +97,8 @@ T: SmartLedsWrite,
|
||||
S: Surface,
|
||||
T::Color: HardwarePixel,
|
||||
[T::Color; PIXEL_NUM]: Pixbuf<T::Color>,
|
||||
Rgb<u8>: From<T::Color> {
|
||||
|
||||
fn start_frame(&mut self) {
|
||||
self.pixbuf.blank();
|
||||
}
|
||||
|
||||
fn end_frame(&mut self) {
|
||||
let b = brightness_for_mw(self.pixbuf.as_milliwatts(), 255, self.max_mw);
|
||||
if let Err(_) = self.target.write(brightness(self.pixbuf.iter().cloned().map(|x| { x.into() }), b)) {
|
||||
//if let Err(_) = self.target.write(self.pixbuf.iter().cloned()) {
|
||||
panic!("Could not write frame");
|
||||
}
|
||||
}
|
||||
|
||||
Rgb<u8>: From<T::Color>
|
||||
{
|
||||
fn render_frame(&mut self) {
|
||||
let surfaces = self.surfaces.take().unwrap();
|
||||
for surface in surfaces.iter() {
|
||||
@ -129,6 +118,25 @@ Rgb<u8>: From<T::Color> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S, const PIXEL_NUM: usize> Framed for SmartLedDisplay<T, S, PIXEL_NUM> where
|
||||
T: SmartLedsWrite,
|
||||
S: Surface,
|
||||
T::Color: HardwarePixel,
|
||||
[T::Color; PIXEL_NUM]: Pixbuf<T::Color>,
|
||||
Rgb<u8>: From<T::Color> {
|
||||
|
||||
fn start_frame(&mut self) {
|
||||
self.pixbuf.blank();
|
||||
}
|
||||
|
||||
fn end_frame(&mut self) {
|
||||
let b = brightness_for_mw(self.pixbuf.as_milliwatts(), 255, self.max_mw);
|
||||
if let Err(_) = self.target.write(brightness(self.pixbuf.iter().cloned().map(|x| { x.into() }), b)) {
|
||||
panic!("Could not write frame");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature="rmt")]
|
||||
pub mod rmt {
|
||||
use esp_idf_svc::hal::prelude::Peripherals;
|
||||
|
141
src/render.rs
141
src/render.rs
@ -1,11 +1,6 @@
|
||||
use std::rc::Rc;
|
||||
use std::cell::RefCell;
|
||||
use std::io;
|
||||
use rgb::RGB8;
|
||||
|
||||
#[cfg(feature="threads")]
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use crate::geometry::*;
|
||||
use crate::task::Task;
|
||||
use crate::time::Periodically;
|
||||
@ -17,6 +12,10 @@ pub trait Shader: Send + Debug {
|
||||
fn draw(&self, surface_coords: &VirtualCoordinates) -> RGB8;
|
||||
}
|
||||
|
||||
pub trait Surfaces<T: Surface>: Debug {
|
||||
fn new_surface(&mut self, area: &Rectangle<u8, Virtual>) -> Result<T, io::Error>;
|
||||
}
|
||||
|
||||
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>);
|
||||
@ -25,13 +24,12 @@ pub trait Surface: Default + Clone + Debug {
|
||||
fn rect(&self) -> Rectangle<u8, Virtual>;
|
||||
}
|
||||
|
||||
pub trait Surfaces<T: Surface>: Debug {
|
||||
fn new_surface(&mut self, area: &Rectangle<u8, Virtual>) -> Result<T, io::Error>;
|
||||
}
|
||||
|
||||
pub trait Display<T: Surface>: Surfaces<T> {
|
||||
pub trait Framed {
|
||||
fn start_frame(&mut self) {}
|
||||
fn end_frame(&mut self) {}
|
||||
}
|
||||
|
||||
pub trait Display<T: Surface>: Surfaces<T> + Framed {
|
||||
fn render_frame(&mut self);
|
||||
}
|
||||
|
||||
@ -67,126 +65,3 @@ 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)]
|
||||
pub struct BoundSurface<T> {
|
||||
pub binding: T
|
||||
}
|
||||
|
||||
impl Debug for BoundSurface<Rc<RefCell<ShaderBinding>>> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("BoundSurface")
|
||||
.field("shader", &self.binding.borrow().shader)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
pub type SimpleSurface = BoundSurface<Rc<RefCell<ShaderBinding>>>;
|
||||
|
||||
impl Default for BoundSurface<Rc<RefCell<ShaderBinding>>>{
|
||||
fn default() -> Self {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
fn set_shader(&mut self, shader: Box<dyn Shader>) {
|
||||
self.binding.borrow_mut().shader = Some(shader);
|
||||
}
|
||||
|
||||
fn clear_shader(&mut self) {
|
||||
self.binding.borrow_mut().shader = None;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature="threads")]
|
||||
pub type SharedSurface = BoundSurface<Arc<Mutex<ShaderBinding>>>;
|
||||
|
||||
#[cfg(feature="threads")]
|
||||
impl Default for BoundSurface<Arc<Mutex<ShaderBinding>>> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
binding: Arc::new(Mutex::new(ShaderBinding {
|
||||
shader: None,
|
||||
})),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature="threads")]
|
||||
impl Surface for BoundSurface<Arc<Mutex<ShaderBinding>>> {
|
||||
fn with_shader<F: FnMut(&dyn Shader)>(&self, mut f: F) {
|
||||
if let Some(ref shader) = self.binding.lock().unwrap().shader {
|
||||
f(shader.as_ref());
|
||||
}
|
||||
}
|
||||
|
||||
fn set_shader(&mut self, shader: Box<dyn Shader>) {
|
||||
self.binding.lock().unwrap().shader = Some(shader);
|
||||
}
|
||||
|
||||
fn clear_shader(&mut self) {
|
||||
self.binding.lock().unwrap().shader = None;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature="threads")]
|
||||
impl Debug for BoundSurface<Arc<Mutex<ShaderBinding>>> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("BoundSurface")
|
||||
.field("shader", &self.binding.lock().unwrap().shader)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SurfacePool<S: Surface + Default> {
|
||||
surfaces: Vec<S>
|
||||
}
|
||||
|
||||
impl<S: Surface + Debug> Debug for SurfacePool<S> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.surfaces.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: Surface + Default> SurfacePool<S> {
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
surfaces: Vec::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> std::slice::Iter<S> {
|
||||
self.surfaces.iter()
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user