From 7607b0f9eed1a58d570d2f570076b81f6f97a769 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Fri, 30 Oct 2020 10:21:06 +0800 Subject: [PATCH] graphics-server: remove `embedded-graphics` from backends Use native drawing for these platforms. Signed-off-by: Sean Cross --- .../graphics-server/src/backend/betrusted.rs | 54 ++----------- .../graphics-server/src/backend/minifb.rs | 78 ++++++++++++------- 2 files changed, 60 insertions(+), 72 deletions(-) diff --git a/services/graphics-server/src/backend/betrusted.rs b/services/graphics-server/src/backend/betrusted.rs index 7c33d9f74..492c37d5c 100644 --- a/services/graphics-server/src/backend/betrusted.rs +++ b/services/graphics-server/src/backend/betrusted.rs @@ -1,4 +1,3 @@ -use embedded_graphics::{drawable::Pixel, geometry::Size, pixelcolor::BinaryColor, DrawTarget}; use xous::MemoryRange; use utralib::generated::*; @@ -26,6 +25,9 @@ impl XousDisplay { xous::MemoryFlags::R | xous::MemoryFlags::W, ) .expect("couldn't map frame buffer"); + for mem_offset in 0..(FB_SIZE / 4) { + unsafe { fb.as_mut_ptr().add(mem_offset).write_volatile(0) }; + } let control = xous::syscall::map_memory( xous::MemoryAddress::new(HW_MEMLCD_BASE), @@ -49,7 +51,11 @@ impl XousDisplay { pub fn update(&mut self) {} - pub fn force_bitmap(&mut self, bmp: [u32; FB_SIZE]) { + pub fn native_buffer(&mut self) -> &mut [u32; FB_SIZE] { + unsafe { &mut *(self.fb.as_mut_ptr() as *mut [u32; FB_SIZE]) } + } + + pub fn blit_screen(&mut self, bmp: [u32; FB_SIZE]) { let framebuffer = self.fb.as_mut_ptr() as *mut u32; for words in 0..FB_SIZE { @@ -113,47 +119,3 @@ impl XousDisplay { } } } - -impl DrawTarget for XousDisplay { - type Error = core::convert::Infallible; - - /// Draw a `Pixel` that has a color defined as `BinaryColor`. - fn draw_pixel(&mut self, pixel: Pixel) -> Result<(), Self::Error> { - let Pixel(coord, color) = pixel; - if coord.x < 0 || coord.y < 0 { - return Ok(()) - } - if coord.x < FB_WIDTH_PIXELS as _ && coord.y < FB_LINES as _ { - let framebuffer = self.fb.as_mut_ptr() as *mut u32; - let offset = (coord.x / 32 + coord.y * FB_WIDTH_WORDS as i32) as usize; - match color { - BinaryColor::Off => { - unsafe { - framebuffer.add(offset).write_volatile( - framebuffer.add(offset).read_volatile() | (1 << (coord.x % 32)), - ) - }; - } - BinaryColor::On => { - unsafe { - framebuffer.add(offset).write_volatile( - framebuffer.add(offset).read_volatile() & !(1 << (coord.x % 32)), - ) - }; - } - } - - // set the dirty bit on the line - unsafe { - framebuffer - .add(offset) - .write_volatile(framebuffer.add(offset).read_volatile() | 0x1_0000) - }; - } - Ok(()) - } - - fn size(&self) -> Size { - Size::new(FB_WIDTH_PIXELS as _, FB_LINES as _) - } -} diff --git a/services/graphics-server/src/backend/minifb.rs b/services/graphics-server/src/backend/minifb.rs index 3d1a51c9a..c23651370 100644 --- a/services/graphics-server/src/backend/minifb.rs +++ b/services/graphics-server/src/backend/minifb.rs @@ -1,14 +1,19 @@ -use embedded_graphics::{drawable::Pixel, geometry::Size, pixelcolor::BinaryColor, DrawTarget}; use minifb::{Key, Window, WindowOptions}; const WIDTH: usize = 336; const HEIGHT: usize = 536; + +/// Width of the screen in 32-bit words +const WIDTH_WORDS: usize = 11; +const FB_SIZE: usize = WIDTH_WORDS * HEIGHT; // 44 bytes by 536 lines + const MAX_FPS: u64 = 15; const DARK_COLOUR: u32 = 0xB5B5AD; const LIGHT_COLOUR: u32 = 0x1B1B19; pub struct XousDisplay { - buffer: Vec, //[u32; WIDTH * HEIGHT], + native_buffer: Vec, //[u32; WIDTH * HEIGHT], + emulated_buffer: [u32; FB_SIZE], window: Window, } @@ -33,45 +38,66 @@ impl XousDisplay { 1000 * 1000 / MAX_FPS, ))); - let buffer = vec![DARK_COLOUR; WIDTH * HEIGHT]; - window.update_with_buffer(&buffer, WIDTH, HEIGHT).unwrap(); + let native_buffer = vec![DARK_COLOUR; WIDTH * HEIGHT]; + window + .update_with_buffer(&native_buffer, WIDTH, HEIGHT) + .unwrap(); - XousDisplay { buffer, window } + XousDisplay { + native_buffer, + window, + emulated_buffer: [0u32; FB_SIZE], + } + } + + pub fn blit_screen(&mut self, bmp: [u32; FB_SIZE]) { + for (dest, src) in self.emulated_buffer.iter_mut().zip(bmp.iter()) { + *dest = *src; + } + } + + pub fn native_buffer(&mut self) -> &mut [u32; FB_SIZE] { + &mut self.emulated_buffer } pub fn redraw(&mut self) { + self.emulated_to_native(); self.window - .update_with_buffer(&self.buffer, WIDTH, HEIGHT) + .update_with_buffer(&self.native_buffer, WIDTH, HEIGHT) .unwrap(); } pub fn update(&mut self) { + self.emulated_to_native(); self.window.update(); if !self.window.is_open() || self.window.is_key_down(Key::Escape) { std::process::exit(0); } } -} -impl DrawTarget for XousDisplay { - type Error = core::convert::Infallible; - - /// Draw a `Pixel` that has a color defined as `Gray8`. - fn draw_pixel(&mut self, pixel: Pixel) -> Result<(), Self::Error> { - let Pixel(point, color) = pixel; - let w = WIDTH as _; - let h = HEIGHT as _; - if point.x < w && point.y < h && point.x >= 0 && point.y >= 0 { - self.buffer[(point.y * w + point.x) as usize] = if color == BinaryColor::On { - LIGHT_COLOUR - } else { - DARK_COLOUR - }; + fn emulated_to_native(&mut self) { + for y in 0..HEIGHT { + for x in 0..WIDTH { + // print!("({}, {}): {} @ {}: ", x, y, (x + y * 44 * 8) / 8, self.emulated_buffer.len()); + // println!("{:08x}", self.emulated_buffer[(x + y * 44 * 8) / 8]); + if ((x + y * 11 * 32) / 32) >= self.emulated_buffer.len() { + panic!( + "Value exceeds src buffer ({}, {}) @ {}", + x, + y, + self.emulated_buffer.len() + ); + } + if (x + y * WIDTH) > self.native_buffer.len() { + panic!("Value exceeds dest buffer"); + } + self.native_buffer[x + y * WIDTH] = + if ((self.emulated_buffer[(x + y * 11 * 32) / 32] >> (x % 32)) & 1) > 0 { + DARK_COLOUR + } else { + LIGHT_COLOUR + }; + } } - Ok(()) - } - - fn size(&self) -> Size { - Size::new(WIDTH as _, HEIGHT as _) } }