2025-11-03 02:04:21 -05:00
|
|
|
use alloc::vec;
|
|
|
|
|
use alloc::vec::Vec;
|
2025-10-11 14:35:29 -07:00
|
|
|
use anyhow::{Context, Result};
|
2025-10-11 14:11:31 -07:00
|
|
|
use uefi::proto::console::gop::{BltOp, BltPixel, BltRegion, GraphicsOutput};
|
|
|
|
|
|
2025-10-19 23:03:28 -07:00
|
|
|
/// Represents the EFI framebuffer.
|
2025-10-11 14:11:31 -07:00
|
|
|
pub struct Framebuffer {
|
2025-10-19 23:03:28 -07:00
|
|
|
/// The width of the framebuffer in pixels.
|
2025-10-11 14:11:31 -07:00
|
|
|
width: usize,
|
2025-10-19 23:03:28 -07:00
|
|
|
/// The height of the framebuffer in pixels.
|
2025-10-11 14:11:31 -07:00
|
|
|
height: usize,
|
2025-10-19 23:03:28 -07:00
|
|
|
/// The pixels of the framebuffer.
|
2025-10-11 14:11:31 -07:00
|
|
|
pixels: Vec<BltPixel>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Framebuffer {
|
2025-10-19 23:03:28 -07:00
|
|
|
/// Creates a new framebuffer of the specified `width` and `height`.
|
2025-10-24 19:44:26 -07:00
|
|
|
pub fn new(width: usize, height: usize) -> Result<Self> {
|
|
|
|
|
// Verify that the size is valid during multiplication.
|
|
|
|
|
let size = width
|
|
|
|
|
.checked_mul(height)
|
|
|
|
|
.context("framebuffer size overflow")?;
|
|
|
|
|
|
|
|
|
|
// Initialize the pixel buffer with black pixels, with the verified size.
|
|
|
|
|
let pixels = vec![BltPixel::new(0, 0, 0); size];
|
|
|
|
|
|
|
|
|
|
Ok(Framebuffer {
|
2025-10-11 14:11:31 -07:00
|
|
|
width,
|
|
|
|
|
height,
|
2025-10-24 19:44:26 -07:00
|
|
|
pixels,
|
|
|
|
|
})
|
2026-10-03 17:01:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The width of the framebuffer in pixels.
|
|
|
|
|
pub fn width(&self) -> usize {
|
|
|
|
|
self.width
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// The height of the framebuffer in pixels.
|
|
|
|
|
pub fn height(&self) -> usize {
|
|
|
|
|
self.height
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Fill the rectangle at `x` and `y` of `width` and `height` with `color`.
|
|
|
|
|
/// The parts of the rectangle outside of the framebuffer are not drawn.
|
|
|
|
|
pub fn fill_rect(&mut self, x: usize, y: usize, width: usize, height: usize, color: BltPixel) {
|
|
|
|
|
let end_x = x.saturating_add(width).min(self.width);
|
|
|
|
|
let end_y = y.saturating_add(height).min(self.height);
|
2026-10-03 17:47:42 -07:00
|
|
|
if x >= end_x {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-10-03 17:01:48 -07:00
|
|
|
for row in y..end_y {
|
2026-10-03 17:47:42 -07:00
|
|
|
// The row and columns are in bounds, so its range is within the pixels.
|
2026-10-03 17:01:48 -07:00
|
|
|
self.pixels[row * self.width + x..row * self.width + end_x].fill(color);
|
|
|
|
|
}
|
2025-10-11 14:11:31 -07:00
|
|
|
}
|
|
|
|
|
|
2025-10-19 23:03:28 -07:00
|
|
|
/// Mutably acquires a pixel of the framebuffer at the specified `x` and `y` coordinate.
|
2025-10-11 14:11:31 -07:00
|
|
|
pub fn pixel(&mut self, x: usize, y: usize) -> Option<&mut BltPixel> {
|
2025-10-24 19:54:28 -07:00
|
|
|
// Verify that the coordinates are within the bounds of the framebuffer.
|
|
|
|
|
if x >= self.width || y >= self.height {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-24 19:44:26 -07:00
|
|
|
// Calculate the index of the pixel safely, returning None if it overflows.
|
|
|
|
|
let index = y.checked_mul(self.width)?.checked_add(x)?;
|
|
|
|
|
// Return the pixel at the index. If the index is out of bounds, this will return None.
|
|
|
|
|
self.pixels.get_mut(index)
|
2025-10-11 14:11:31 -07:00
|
|
|
}
|
|
|
|
|
|
2025-10-19 23:03:28 -07:00
|
|
|
/// Blit the framebuffer to the specified `gop` [GraphicsOutput].
|
2025-10-11 14:35:29 -07:00
|
|
|
pub fn blit(&self, gop: &mut GraphicsOutput) -> Result<()> {
|
2025-10-11 14:11:31 -07:00
|
|
|
gop.blt(BltOp::BufferToVideo {
|
|
|
|
|
buffer: &self.pixels,
|
|
|
|
|
src: BltRegion::Full,
|
|
|
|
|
dest: (0, 0),
|
|
|
|
|
dims: (self.width, self.height),
|
|
|
|
|
})
|
2025-10-14 12:47:33 -07:00
|
|
|
.context("unable to blit framebuffer")?;
|
2025-10-11 14:35:29 -07:00
|
|
|
Ok(())
|
2025-10-11 14:11:31 -07:00
|
|
|
}
|
|
|
|
|
}
|