breakwater_parser/framebuffer/
mod.rs

1pub mod shared_memory;
2pub mod simple;
3
4pub const FB_BYTES_PER_PIXEL: usize = std::mem::size_of::<u32>();
5
6pub trait FrameBuffer {
7    /// Width in pixels
8    fn get_width(&self) -> usize;
9
10    /// Height in pixels
11    fn get_height(&self) -> usize;
12
13    /// Returns the number of pixels (not bytes)
14    #[inline(always)]
15    fn get_size(&self) -> usize {
16        self.get_width() * self.get_height()
17    }
18
19    /// In case the coordinates are within the framebuffers area, [`Some`] with
20    /// the current color is returned, [`None`] otherwise.
21    #[inline(always)]
22    fn get(&self, x: usize, y: usize) -> Option<u32> {
23        if x < self.get_width() && y < self.get_height() {
24            Some(unsafe { self.get_unchecked(x, y) })
25        } else {
26            None
27        }
28    }
29
30    /// # Safety
31    /// make sure x and y are in bounds
32    unsafe fn get_unchecked(&self, x: usize, y: usize) -> u32;
33
34    fn set(&self, x: usize, y: usize, rgba: u32);
35
36    /// We can *not* take an `&[u32]` for the pixel here, as `std::slice::from_raw_parts` requires the data to be
37    /// aligned. As the data already is stored in a buffer we can not guarantee it's correctly aligned, so let's just
38    /// treat the pixels as raw bytes.
39    ///
40    /// Returns the coordinates where we landed after filling
41    #[inline(always)]
42    fn set_multi(&self, start_x: usize, start_y: usize, pixels: &[u8]) -> (usize, usize) {
43        let starting_index = start_x + start_y * self.get_width();
44        let pixels_copied = self.set_multi_from_start_index(starting_index, pixels);
45
46        let new_x = (start_x + pixels_copied) % self.get_width();
47        let new_y = start_y + (pixels_copied / self.get_width());
48
49        (new_x, new_y)
50    }
51
52    /// Returns the number of pixels copied
53    fn set_multi_from_start_index(&self, starting_index: usize, pixels: &[u8]) -> usize;
54
55    /// As the pixel memory doesn't necessarily need to be aligned (think of using shared memory for
56    /// that), we can only return it as a list of bytes, not a list of pixels.
57    fn as_bytes(&self) -> &[u8];
58}