1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use alloc::vec::Vec;

use crate::prelude::{ColorFormat, DisplayInfo, FrameBuffer};
use crate::scheme::{DisplayScheme, Scheme};

pub struct MockDisplay {
    info: DisplayInfo,
    fb: Vec<u8>,
}

impl MockDisplay {
    pub fn new(width: u32, height: u32, format: ColorFormat) -> Self {
        let fb_size = (width * height * format.bytes() as u32) as usize;
        let fb = vec![0; fb_size];
        let info = DisplayInfo {
            width,
            height,
            format,
            fb_base_vaddr: fb.as_ptr() as usize,
            fb_size,
        };
        Self { info, fb }
    }

    /// # Safety
    ///
    /// This function is unsafe, the caller must ensure the `ptr` points to the
    /// start of a valid frame buffer.
    pub unsafe fn from_raw_parts(
        width: u32,
        height: u32,
        format: ColorFormat,
        ptr: *mut u8,
    ) -> Self {
        let fb_size = (width * height * format.bytes() as u32) as usize;
        let fb = Vec::from_raw_parts(ptr, fb_size, fb_size);
        let info = DisplayInfo {
            width,
            height,
            format,
            fb_base_vaddr: fb.as_ptr() as usize,
            fb_size,
        };
        Self { info, fb }
    }
}

impl Scheme for MockDisplay {
    fn name(&self) -> &str {
        "mock-display"
    }
}

impl DisplayScheme for MockDisplay {
    #[inline]
    fn info(&self) -> DisplayInfo {
        self.info
    }

    #[inline]
    fn fb(&self) -> FrameBuffer {
        unsafe { FrameBuffer::from_raw_parts_mut(self.fb.as_ptr() as _, self.info.fb_size) }
    }
}