emerald_kernel_user_link/
graphics.rs

1#[repr(u64)]
2#[derive(Debug, Clone, Copy)]
3#[non_exhaustive]
4pub enum GraphicsCommand {
5    /// Take ownership of the graphics device
6    /// No arguments
7    TakeOwnership,
8    /// Release ownership of the graphics device
9    /// No arguments
10    ReleaseOwnership,
11    /// Get information about the framebuffer
12    /// &mut FrameBufferInfo
13    GetFrameBufferInfo,
14    /// Blit a region from userspace memory into the graphics framebuffer
15    /// (must have ownership of the graphics device)
16    /// &BlitCommand
17    Blit,
18}
19
20impl GraphicsCommand {
21    pub fn from_u64(value: u64) -> Option<Self> {
22        match value {
23            0 => Some(Self::TakeOwnership),
24            1 => Some(Self::ReleaseOwnership),
25            2 => Some(Self::GetFrameBufferInfo),
26            3 => Some(Self::Blit),
27            _ => None,
28        }
29    }
30
31    pub fn to_u64(self) -> u64 {
32        self as u64
33    }
34}
35
36#[repr(C)]
37#[derive(Debug, Clone, Copy)]
38pub struct FrameBufferInfo {
39    pub pitch: usize,
40    pub height: usize,
41    pub width: usize,
42    pub field_pos: (u8, u8, u8),
43    pub mask: (u8, u8, u8),
44    pub byte_per_pixel: u8,
45}
46
47impl FrameBufferInfo {
48    /// The size of the memory buffer required to hold the framebuffer
49    pub fn memory_size(&self) -> usize {
50        self.pitch * self.height
51    }
52
53    /// Get the position in the memory buffer for a given pixel
54    /// Returns None if the position is out of bounds
55    pub fn get_arr_pos(&self, pos: (usize, usize)) -> Option<usize> {
56        if pos.0 >= self.width || pos.1 >= self.height {
57            return None;
58        }
59        Some(pos.0 * self.byte_per_pixel as usize + pos.1 * self.pitch)
60    }
61
62    /// Get the pixel slice at a given position (read-only)
63    pub fn pixel_mem<'a>(&self, memory: &'a [u8], pos: (usize, usize)) -> Option<&'a [u8]> {
64        let i = self.get_arr_pos(pos)?;
65        Some(&memory[i..i + self.byte_per_pixel as usize])
66    }
67
68    /// Get the pixel slice at a given position
69    pub fn pixel_mem_mut<'a>(
70        &self,
71        memory: &'a mut [u8],
72        pos: (usize, usize),
73    ) -> Option<&'a mut [u8]> {
74        let i = self.get_arr_pos(pos)?;
75        Some(&mut memory[i..i + self.byte_per_pixel as usize])
76    }
77}
78
79#[repr(C)]
80#[derive(Debug, Clone, Copy)]
81pub struct BlitCommand {
82    /// The memory buffer to blit from, this represent the whole framebuffer
83    /// even if we are only blitting a part of it
84    pub memory: *const u8,
85    /// The framebuffer info of the source memory
86    /// i.e. metadata about `memory`
87    pub src_framebuffer_info: FrameBufferInfo,
88    /// The position in the source framebuffer to start blitting from
89    pub src: (usize, usize),
90    /// The position in the destination framebuffer to start blitting to
91    /// this destination is the kernel's framebuffer
92    pub dst: (usize, usize),
93    /// The size of the region to blit (width, height)
94    pub size: (usize, usize),
95}