Skip to main content

kernel/drivers/gpu/basic_fb/
framebuffer.rs

1// SPDX-License-Identifier: GPL-3.0-only
2//! Linear framebuffer wrapper for visual output for early boot and panic.
3//!
4//! Authors: MarioS271
5
6use crate::lib::panic::kernel_panic;
7use crate::lib::panic_codes::PanicCode;
8use crate::lib::types::boot_info::FramebufferInfo;
9// TODO: do RGB/BGR check
10
11/// A thin wrapper around the bootloader-provided framebuffer
12pub struct BasicFramebuffer {
13    pub fb_pointer: *mut u32,
14    pub pixels_per_row: usize,
15    pub width: usize,
16    pub height: usize,
17}
18
19/// Safety: `BasicFramebuffer::fb_pointer` points to the valid limine-allocated fb which has a static lifetime
20unsafe impl Send for BasicFramebuffer {}
21
22impl BasicFramebuffer {
23    /// Constructor; returns a `BasicFramebuffer` constructed from [`FramebufferInfo`]
24    pub fn new(fb: &FramebufferInfo) -> Self {
25        if fb.bpp != 32 {
26            kernel_panic(
27                PanicCode::InitFailure,
28                "The bootloader provided a bpp value that is not 32, which the kernel doesn't support"
29            )
30        }
31        Self {
32            fb_pointer: fb.addr,
33            pixels_per_row: fb.pixels_per_row as usize,
34            width: fb.width as usize,
35            height: fb.height as usize
36        }
37    }
38
39    /// Write zero (black) to every pixel in the framebuffer
40    pub fn clear(&self) {
41        // Safety: fb_pointer is valid and points to the limine fb with static lifetime and
42        // pixels_per_row * height does not exceed the buffer size
43        unsafe {
44            core::ptr::write_bytes(
45                self.fb_pointer,
46                0u8,
47                self.pixels_per_row * self.height
48            );
49        }
50    }
51}