Store a raw pointer in Display for the framebuffer

This commit is contained in:
bjorn3
2024-03-20 18:31:28 +01:00
parent befd5175bb
commit 98c28a3d60
2 changed files with 20 additions and 19 deletions
+2 -7
View File
@@ -65,12 +65,7 @@ impl DebugDisplay {
/// Draw a character
fn char(&mut self, x: usize, y: usize, character: char, color: u32) {
if x + 8 <= self.display.width && y + 16 <= self.display.height {
let mut dst = unsafe {
self.display
.data_mut()
.as_mut_ptr()
.add(y * self.display.stride + x)
};
let mut dst = unsafe { self.display.data_mut().add(y * self.display.stride + x) };
let font_i = 16 * (character as usize);
if font_i + 16 <= FONT.len() {
@@ -94,7 +89,7 @@ impl DebugDisplay {
let offset = cmp::min(self.display.height, lines) * self.display.stride;
let size = (self.display.stride * self.display.height) - offset;
unsafe {
let ptr = self.display.data_mut().as_mut_ptr();
let ptr = self.display.data_mut();
ptr::copy(ptr.add(offset), ptr, size);
ptr::write_bytes(ptr.add(size), 0, offset);
}
+18 -12
View File
@@ -6,10 +6,12 @@ pub(super) struct Display {
pub(super) width: usize,
pub(super) height: usize,
pub(super) stride: usize,
onscreen: &'static mut [u32],
onscreen_ptr: *mut u32,
offscreen: Option<Box<[u32]>>,
}
unsafe impl Send for Display {}
impl Display {
pub(super) fn new(
width: usize,
@@ -17,28 +19,28 @@ impl Display {
stride: usize,
onscreen_ptr: *mut u32,
) -> Display {
let size = stride * height;
let onscreen = unsafe {
ptr::write_bytes(onscreen_ptr, 0, size);
slice::from_raw_parts_mut(onscreen_ptr, size)
};
unsafe {
ptr::write_bytes(onscreen_ptr, 0, stride * height);
}
Display {
width,
height,
stride,
onscreen,
onscreen_ptr,
offscreen: None,
}
}
pub(super) fn heap_init(&mut self) {
self.offscreen = Some(self.onscreen.to_vec().into_boxed_slice());
let onscreen =
unsafe { slice::from_raw_parts(self.onscreen_ptr, self.stride * self.height) };
self.offscreen = Some(onscreen.to_vec().into_boxed_slice());
}
pub(super) fn data_mut(&mut self) -> &mut [u32] {
pub(super) fn data_mut(&mut self) -> *mut u32 {
match &mut self.offscreen {
Some(offscreen) => offscreen,
None => self.onscreen,
Some(offscreen) => offscreen.as_mut_ptr(),
None => self.onscreen_ptr,
}
}
@@ -47,7 +49,11 @@ impl Display {
if let Some(offscreen) = &self.offscreen {
let mut offset = y * self.stride + x;
while h > 0 {
self.onscreen[offset..offset + w].copy_from_slice(&offscreen[offset..offset + w]);
ptr::copy(
offscreen.as_ptr().add(offset),
self.onscreen_ptr.add(offset),
w,
);
offset += self.stride;
h -= 1;
}