uefi-rt/raytracer/src/buffer.rs

56 lines
1.5 KiB
Rust

use uefi::proto::console::gop::{BltOp, BltPixel, BltRegion, GraphicsOutput};
extern crate alloc;
use alloc::vec;
use alloc::vec::Vec;
use uefi::Error;
pub struct Buffer {
pub width: usize,
pub height: usize,
pixels: Vec<BltPixel>,
}
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 get_pixel(&mut self, x: usize, y: usize) -> Option<&mut BltPixel> {
self.pixels.get_mut(y * self.width + x)
}
/// Set a single pixel.
pub fn set_pixel(&mut self, x: usize, y: usize, pixel: BltPixel) {
self.pixels[y * self.width + x] = pixel;
}
/// Blit the buffer to the framebuffer.
pub fn blit(&self, gop: &mut GraphicsOutput) -> Result<(), Error> {
gop.blt(BltOp::BufferToVideo {
buffer: &self.pixels,
src: BltRegion::Full,
dest: (0, 0),
dims: (self.width, self.height),
})
}
/// Update only a pixel to the framebuffer.
fn blit_pixel(&self, gop: &mut GraphicsOutput, coords: (usize, usize)) -> Result<(), Error> {
gop.blt(BltOp::BufferToVideo {
buffer: &self.pixels,
src: BltRegion::SubRectangle {
coords,
px_stride: self.width,
},
dest: coords,
dims: (1, 1),
})
}
}