mappings: split coord mapping from pixel mapping
This commit is contained in:
@ -1,8 +1,7 @@
|
||||
use smart_leds_trait::SmartLedsWrite;
|
||||
|
||||
use crate::lib8::Rgb8Blend;
|
||||
use crate::lib8::interpolate::Fract8Ops;
|
||||
use crate::render::{Surface, Display, Surfaces};
|
||||
use crate::render::{Display, HardwarePixel, PixelView, Sample, Surface, Surfaces};
|
||||
use crate::buffers::SurfacePool;
|
||||
use crate::power::{brightness_for_mw, AsMilliwatts};
|
||||
use crate::geometry::*;
|
||||
@ -10,13 +9,8 @@ use crate::mappings::*;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
|
||||
use std::io;
|
||||
|
||||
use rgb::Rgb;
|
||||
use std::ops::IndexMut;
|
||||
|
||||
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 Pixbuf: AsMilliwatts + IndexMut<usize, Output=Self::Pixel> {
|
||||
type Pixel: HardwarePixel;
|
||||
fn new() -> Self;
|
||||
@ -24,6 +18,38 @@ pub trait Pixbuf: AsMilliwatts + IndexMut<usize, Output=Self::Pixel> {
|
||||
fn iter_with_brightness(&self, brightness: u8) -> impl Iterator<Item = Self::Pixel> + Send;
|
||||
}
|
||||
|
||||
struct StrideSampler<'a, P: Pixbuf> {
|
||||
pixbuf: &'a mut P,
|
||||
selection: StrideView<'a>
|
||||
}
|
||||
|
||||
impl<'a, P: Pixbuf> PixelView for StrideSampler<'a, P> {
|
||||
type Pixel = P::Pixel;
|
||||
fn next(&mut self) -> Option<(Coordinates<Virtual>, &mut Self::Pixel)> {
|
||||
if let Some((virt, coords)) = self.selection.next() {
|
||||
let idx = self.selection.map.strides[coords.x as usize].pixel_idx_for_offset(coords.y);
|
||||
Some((virt, &mut self.pixbuf[idx]))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct StrideOutput<P: Pixbuf> {
|
||||
pixbuf: P,
|
||||
stride_map: StrideMapping
|
||||
}
|
||||
|
||||
impl<P: Pixbuf> Sample for StrideOutput<P> {
|
||||
type Pixel = P::Pixel;
|
||||
fn sample(&mut self, rect: &Rectangle<Virtual>) -> impl PixelView<Pixel = Self::Pixel> {
|
||||
StrideSampler {
|
||||
pixbuf: &mut self.pixbuf,
|
||||
selection: self.stride_map.select(rect)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: HardwarePixel, const PIXEL_NUM: usize> Pixbuf for [T; PIXEL_NUM] {
|
||||
type Pixel = T;
|
||||
fn new() -> Self {
|
||||
@ -41,9 +67,8 @@ impl<T: HardwarePixel, const PIXEL_NUM: usize> Pixbuf for [T; PIXEL_NUM] {
|
||||
|
||||
struct SmartLedDisplay<T: FastWrite, S: Surface, P: Pixbuf<Pixel = T::Color>> {
|
||||
surfaces : Option<SurfacePool<S>>,
|
||||
pixmap: StrideMapping,
|
||||
output: StrideOutput<P>,
|
||||
target: T,
|
||||
pixbuf: P,
|
||||
max_mw: u32,
|
||||
frame: usize
|
||||
}
|
||||
@ -51,7 +76,7 @@ struct SmartLedDisplay<T: FastWrite, S: Surface, P: Pixbuf<Pixel = T::Color>> {
|
||||
impl<T: FastWrite, S: Surface, P: Pixbuf<Pixel = T::Color>> Debug for SmartLedDisplay<T, S, P> {
|
||||
fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
|
||||
f.debug_struct("SmartLedDisplay")
|
||||
.field("total_mw", &self.pixbuf.as_milliwatts())
|
||||
.field("total_mw", &self.output.pixbuf.as_milliwatts())
|
||||
.field("surfaces", &self.surfaces)
|
||||
.finish()
|
||||
}
|
||||
@ -60,11 +85,10 @@ impl<T: FastWrite, S: Surface, P: Pixbuf<Pixel = T::Color>> Debug for SmartLedDi
|
||||
impl<T: FastWrite, S: Surface, P: Pixbuf<Pixel = T::Color>> SmartLedDisplay<T, S, P> {
|
||||
fn new(target: T, max_mw: u32, pixmap: StrideMapping, pixbuf: P) -> Self {
|
||||
SmartLedDisplay {
|
||||
pixbuf,
|
||||
output: StrideOutput { pixbuf: pixbuf, stride_map: pixmap },
|
||||
surfaces: Some(SurfacePool::new()),
|
||||
target,
|
||||
max_mw,
|
||||
pixmap,
|
||||
frame: 0
|
||||
}
|
||||
}
|
||||
@ -88,20 +112,19 @@ T: FastWrite,
|
||||
S: Surface,
|
||||
P: Pixbuf<Pixel = T::Color> {
|
||||
fn start_frame(&mut self) {
|
||||
self.pixbuf.blank();
|
||||
self.output.pixbuf.blank();
|
||||
}
|
||||
|
||||
fn render_frame(&mut self) {
|
||||
let surfaces = self.surfaces.take().unwrap();
|
||||
for surface in surfaces.iter() {
|
||||
let rect = surface.rect().clone();
|
||||
let mut sel = self.pixmap.select(&rect);
|
||||
let rect = surface.rect();
|
||||
let opacity = surface.opacity();
|
||||
if opacity > 0 {
|
||||
let mut sample = self.output.sample(&rect);
|
||||
surface.with_shader(|shader| {
|
||||
while let Some((virt_coords, phys_coords)) = sel.next() {
|
||||
let idx = self.pixmap.to_idx(&phys_coords);
|
||||
self.pixbuf[idx] = self.pixbuf[idx].blend8(shader.draw(&virt_coords, self.frame).into(), opacity);
|
||||
while let Some((virt_coords, pixel)) = sample.next() {
|
||||
*pixel = pixel.blend8(shader.draw(&virt_coords, self.frame).into(), opacity);
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -110,10 +133,10 @@ P: Pixbuf<Pixel = T::Color> {
|
||||
}
|
||||
|
||||
fn end_frame(&mut self) {
|
||||
let b = brightness_for_mw(self.pixbuf.as_milliwatts(), 255, self.max_mw);
|
||||
if let Err(_) = self.target.fast_write(self.pixbuf.iter_with_brightness(b)) {
|
||||
let b = brightness_for_mw(self.output.pixbuf.as_milliwatts(), 255, self.max_mw);
|
||||
if self.target.fast_write(self.output.pixbuf.iter_with_brightness(b)).is_err() {
|
||||
panic!("Could not write frame!");
|
||||
}
|
||||
};
|
||||
self.frame += 1;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user