Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/backends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ pub trait FrameBufferBackend {
/// Returns a pixels color
fn get(&self, index: usize) -> Self::Color;

/// Fills a line with a color, may be used with the slice copy methods for memory optimization
fn set_hline(&mut self, start: usize, width: usize, color: Self::Color) {
for pixel in start..width {
self.set(pixel, color);
}
}

/// Nr of elements in the backend
fn nr_elements(&self) -> usize;
}
Expand All @@ -55,6 +62,10 @@ impl<C: PixelColor, const N: usize> FrameBufferBackend for &mut [C; N] {
self[index]
}

fn set_hline(&mut self, start: usize, width: usize, color: Self::Color) {
self[start..start + width].fill(color)
}

fn nr_elements(&self) -> usize {
self.len()
}
Expand All @@ -70,6 +81,10 @@ impl<C: PixelColor, const N: usize> FrameBufferBackend for [C; N] {
self[index]
}

fn set_hline(&mut self, start: usize, width: usize, color: Self::Color) {
self[start..start + width].fill(color)
}

fn nr_elements(&self) -> usize {
self.len()
}
Expand All @@ -86,6 +101,10 @@ impl<C: PixelColor> FrameBufferBackend for &mut [C] {
self[index]
}

fn set_hline(&mut self, start: usize, width: usize, color: Self::Color) {
self[start..start + width].fill(color)
}

fn nr_elements(&self) -> usize {
self.len()
}
Expand Down
43 changes: 34 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@
use embedded_dma::{ReadBuffer, WriteBuffer};
use embedded_graphics::{
draw_target::DrawTarget,
geometry::OriginDimensions,
geometry::{Dimensions, OriginDimensions},
prelude::{PixelColor, Point, Size},
primitives::Rectangle,
Pixel,
};

Expand Down Expand Up @@ -236,14 +237,33 @@ impl<C: PixelColor, B: FrameBufferBackend<Color = C>> DrawTarget for FrameBuf<C,
Ok(())
}

fn clear(&mut self, color: Self::Color) -> Result<(), Self::Error> {
for y in 0..self.height {
for x in 0..self.width {
self.set_color_at(Point::new(x as i32, y as i32), color);
}
fn fill_solid(&mut self, area: &Rectangle, color: Self::Color) -> Result<(), Self::Error> {
// Clip Rectangle
let area = if self.bounding_box().intersection(area) != *area {
self.bounding_box().intersection(area)
} else {
*area
};

let Rectangle {
top_left: Point { x, y },
size: Size { width, height },
} = area;

for row in (y as u32)..(y as u32) + height {
self.data.set_hline(
self.point_to_index(Point::new(x, row as i32)),
width as usize,
color,
);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is absolutely fine as is, but I was wondering if that function shouldn't be moved into the trait as well. Then one could potentially write a backend that uses SIMD instructions (or whatever funky optimizations one could come up with) for further optimizations across the rows.

Ok(())
}

fn clear(&mut self, color: Self::Color) -> Result<(), Self::Error> {
self.fill_solid(&self.bounding_box(), color)
}
}

/// An iterator for all [Pixels](Pixel) in the framebuffer.
Expand Down Expand Up @@ -372,16 +392,21 @@ mod tests {
.draw(&mut fbuf)
.unwrap();

Rectangle::new(Point::new(5, 5), Size::new(3, 3))
.into_styled(PrimitiveStyle::with_fill(BinaryColor::On))
.draw(&mut fbuf)
.unwrap();

display.draw_iter(fbuf.into_iter()).unwrap();
display.assert_pattern(&[
"............",
"..#########.",
"..#########.",
"............",
"............",
".###........",
".###........",
".###........",
".###.###....",
".###.###....",
".###.###....",
".###........",
".###........",
".###........",
Expand Down
Loading