Repository navigation
Expand file tree
/
Copy pathbuffer.rs
More file actions
72 lines (64 loc) · 2.29 KB
/
Copy pathbuffer.rs
File metadata and controls
72 lines (64 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use alloc::vec;
use alloc::vec::Vec;
use embedded_graphics::{pixelcolor::Rgb888, prelude::*};
#[allow(unused_imports)]
use log::{debug, error, info, trace};
use uefi::proto::console::gop::{BltOp, BltPixel, BltRegion, GraphicsOutput};
pub struct Buffer {
width: usize,
height: usize,
pixels: Vec<BltPixel>,
}
impl OriginDimensions for Buffer {
fn size(&self) -> Size {
Size::new(self.width as u32, self.height as u32)
}
}
impl DrawTarget for Buffer {
type Color = Rgb888;
// `ExampleDisplay` uses a framebuffer and doesn't need to communicate with the display
// controller to draw pixel, which means that drawing operations can never fail. To reflect
// this the type `Infallible` was chosen as the `Error` type.
type Error = core::convert::Infallible;
fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
where
I: IntoIterator<Item = Pixel<Self::Color>>,
{
for Pixel(coord, color) in pixels.into_iter() {
// Check if the pixel coordinates are out of bounds (negative or greater than
// (63,63)). `DrawTarget` implementation are required to discard any out of bounds
// pixels without returning an error or causing a panic.
if coord.x < self.width as i32 && coord.y < self.height as i32 {
// Calculate the index in the framebuffer.
let pixel = self.pixel(coord.x as usize, coord.y as usize).unwrap();
pixel.red = color.r();
pixel.green = color.g();
pixel.blue = color.b();
}
}
Ok(())
}
}
impl Buffer {
/// Create a new `Buffer`.
pub fn new(width: usize, height: usize) -> Self {
Buffer {
width,
height,
pixels: vec![BltPixel::new(0, 0, 0); width * height],
}
}
/// Get a single pixel.
pub fn pixel(&mut self, x: usize, y: usize) -> Option<&mut BltPixel> {
self.pixels.get_mut(y * self.width + x)
}
/// Blit the buffer to the framebuffer.
pub fn blit(&self, gop: &mut GraphicsOutput) -> uefi::Result {
gop.blt(BltOp::BufferToVideo {
buffer: &self.pixels,
src: BltRegion::Full,
dest: (0, 0),
dims: (self.width, self.height),
})
}
}