2025-11-03 02:04:21 -05:00
|
|
|
use alloc::vec;
|
|
|
|
|
use alloc::vec::Vec;
|
2026-10-04 14:22:43 -07:00
|
|
|
use anyhow::{Context, Result, bail};
|
2025-10-11 14:11:31 -07:00
|
|
|
use uefi::proto::console::gop::{BltOp, BltPixel, BltRegion, GraphicsOutput};
|
|
|
|
|
|
2026-10-04 14:22:43 -07:00
|
|
|
/// How many unchanged rows may sit between changed rows before they are blitted separately.
|
|
|
|
|
const BAND_GAP: usize = 8;
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2026-10-04 14:22:43 -07:00
|
|
|
/// Replace the pixels of the framebuffer with those of `other`, which must be the same size.
|
|
|
|
|
pub fn copy_from(&mut self, other: &Framebuffer) {
|
|
|
|
|
self.pixels.copy_from_slice(&other.pixels);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
2026-10-04 14:22:43 -07:00
|
|
|
|
|
|
|
|
/// Blit only what is different from `previous`, which must be the same size, to the specified
|
|
|
|
|
/// `gop` [GraphicsOutput]. Changes that are far apart vertically are blitted separately, so
|
|
|
|
|
/// that the pixels between them are not sent to the display.
|
|
|
|
|
pub fn blit_changes(&self, previous: &Framebuffer, gop: &mut GraphicsOutput) -> Result<()> {
|
|
|
|
|
if self.width != previous.width || self.height != previous.height {
|
|
|
|
|
bail!("framebuffer sizes differ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The top, bottom, left and right of the changed rows that haven't been blitted yet.
|
|
|
|
|
let mut band: Option<(usize, usize, usize, usize)> = None;
|
|
|
|
|
let mut quiet = 0;
|
|
|
|
|
for row in 0..self.height {
|
|
|
|
|
let range = row * self.width..(row + 1) * self.width;
|
|
|
|
|
let (now, before) = (&self.pixels[range.clone()], &previous.pixels[range]);
|
|
|
|
|
let differs = |(now, before): (&BltPixel, &BltPixel)| {
|
|
|
|
|
(now.red, now.green, now.blue) != (before.red, before.green, before.blue)
|
|
|
|
|
};
|
|
|
|
|
let first = now.iter().zip(before).position(differs);
|
|
|
|
|
let Some(first) = first else {
|
|
|
|
|
quiet += 1;
|
|
|
|
|
if quiet > BAND_GAP
|
|
|
|
|
&& let Some(band) = band.take()
|
|
|
|
|
{
|
|
|
|
|
self.blit_region(gop, band)?;
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
let last = now.iter().zip(before).rposition(differs).unwrap_or(first);
|
|
|
|
|
quiet = 0;
|
|
|
|
|
band = Some(match band {
|
|
|
|
|
Some((top, _, left, right)) => (top, row + 1, left.min(first), right.max(last + 1)),
|
|
|
|
|
None => (row, row + 1, first, last + 1),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if let Some(band) = band {
|
|
|
|
|
self.blit_region(gop, band)?;
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Blit the region of the `top`, `bottom`, `left` and `right` to the specified `gop`.
|
|
|
|
|
fn blit_region(
|
|
|
|
|
&self,
|
|
|
|
|
gop: &mut GraphicsOutput,
|
|
|
|
|
(top, bottom, left, right): (usize, usize, usize, usize),
|
|
|
|
|
) -> Result<()> {
|
|
|
|
|
gop.blt(BltOp::BufferToVideo {
|
|
|
|
|
buffer: &self.pixels,
|
|
|
|
|
src: BltRegion::SubRectangle {
|
|
|
|
|
coords: (left, top),
|
|
|
|
|
px_stride: self.width,
|
|
|
|
|
},
|
|
|
|
|
dest: (left, top),
|
|
|
|
|
dims: (right - left, bottom - top),
|
|
|
|
|
})
|
|
|
|
|
.context("unable to blit framebuffer region")?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2025-10-11 14:11:31 -07:00
|
|
|
}
|