diff --git a/services/graphics-server/src/backend/betrusted.rs b/services/graphics-server/src/backend/betrusted.rs index cde3f41df..108ce75f1 100644 --- a/services/graphics-server/src/backend/betrusted.rs +++ b/services/graphics-server/src/backend/betrusted.rs @@ -1,13 +1,12 @@ use susres::{RegManager, RegOrField, SuspendResume}; use utralib::generated::*; -use ux_api::minigfx::Point; -use ux_api::platform::{LINES, WIDTH}; +use ux_api::minigfx::{ColorNative, FrameBuffer, PixelColor, Point}; +use ux_api::platform::*; use xous::MemoryRange; -pub const FB_WIDTH_WORDS: usize = 11; -pub const FB_WIDTH_PIXELS: usize = WIDTH as usize; -pub const FB_LINES: usize = LINES as usize; -pub const FB_SIZE: usize = FB_WIDTH_WORDS * FB_LINES; // 44 bytes by 536 lines +const LCD_WORDS_PER_LINE: usize = FB_WIDTH_WORDS; +const LCD_PX_PER_LINE: isize = WIDTH as isize; +const LCD_LINES: isize = FB_LINES as isize; const CONFIG_CLOCK_FREQUENCY: u32 = 100_000_000; pub struct MainThreadToken(()); @@ -277,3 +276,109 @@ impl XousDisplay { } } } + +impl FrameBuffer for XousDisplay { + /// Puts a pixel of ColorNative at x, y. (0, 0) is defined as the lower left corner. + fn put_pixel(&mut self, p: Point, color: ColorNative) { + let fb: &mut [u32] = unsafe { self.fb.as_slice_mut() }; + let clip_y: usize; + if p.y >= LCD_LINES { + clip_y = LCD_LINES as usize - 1; + } else if p.y < 0 { + clip_y = 0; + } else { + clip_y = p.y as usize; + } + + let clip_x: usize; + if p.x >= LCD_PX_PER_LINE { + clip_x = LCD_PX_PER_LINE as usize - 1; + } else if p.x < 0 { + clip_x = 0; + } else { + clip_x = p.x as usize; + } + + let pc = PixelColor::from(color.0); + + if pc == PixelColor::Light { + fb[(clip_x + clip_y * LCD_WORDS_PER_LINE * 32) / 32] |= 1 << (clip_x % 32) + } else { + fb[(clip_x + clip_y * LCD_WORDS_PER_LINE * 32) / 32] &= !(1 << (clip_x % 32)) + } + // set the dirty bit on the line that contains the pixel + fb[clip_y * LCD_WORDS_PER_LINE + (LCD_WORDS_PER_LINE - 1)] |= 0x1_0000; + } + + /// Retrieves a pixel value from the frame buffer; returns None if the point is out of bounds. + /// + /// Note: this has not be carefully tested as this API is not used by the legacy code base. + /// Anyone using this API for the first time may benefit in checking that it is correct. + fn get_pixel(&mut self, p: Point) -> Option { + let fb: &mut [u32] = unsafe { self.fb.as_slice_mut() }; + let clip_y: usize; + if p.y >= LCD_LINES { + return None; + } else if p.y < 0 { + return None; + } else { + clip_y = p.y as usize; + } + + let clip_x: usize; + if p.x >= LCD_PX_PER_LINE { + return None; + } else if p.x < 0 { + return None; + } else { + clip_x = p.x as usize; + } + Some(ColorNative::from( + (fb[(clip_x + clip_y * LCD_WORDS_PER_LINE * 32) / 32] & (1 << (clip_x % 32))) as usize, + )); + todo!("Check that this API is implemented correctly before using it!"); + } + + /// XORs a pixel to what is in the existing frame buffer. The exact definition of "XOR" is somewhat + /// ambiguous for full color systems but is generally meant to imply a light/dark swap of foreground + /// and background colors for a color theme. + fn xor_pixel(&mut self, p: Point) { + let fb: &mut [u32] = unsafe { self.fb.as_slice_mut() }; + let clip_y: usize; + if p.y >= LCD_LINES { + return; + } else if p.y < 0 { + return; + } else { + clip_y = p.y as usize; + } + + let clip_x: usize; + if p.x >= LCD_PX_PER_LINE { + return; + } else if p.x < 0 { + return; + } else { + clip_x = p.x as usize; + } + + fb[(clip_x + clip_y * LCD_WORDS_PER_LINE * 32) / 32] ^= 1 << (clip_x % 32); + // set the dirty bit on the line that contains the pixel + fb[clip_y * LCD_WORDS_PER_LINE + (LCD_WORDS_PER_LINE - 1)] |= 0x1_0000; + } + + /// Swaps the drawable buffer to the screen and sends it to the hardware + fn draw(&mut self) { self.redraw(); } + + /// Clears the drawable buffer + fn clear(&mut self) { + let fb: &mut [u32] = unsafe { self.fb.as_slice_mut() }; + fb.fill(0xFFFF_FFFF); + } + + /// Returns the size of the frame buffer as a Point + fn dimensions(&self) -> Point { self.screen_size() } + + /// Returns a raw pointer to the frame buffer + unsafe fn raw_mut(&mut self) -> &mut ux_api::platform::FbRaw { self.native_buffer() } +} diff --git a/services/graphics-server/src/backend/cramium.rs b/services/graphics-server/src/backend/cramium.rs index 46eb4981d..6b034999d 100644 --- a/services/graphics-server/src/backend/cramium.rs +++ b/services/graphics-server/src/backend/cramium.rs @@ -322,3 +322,5 @@ impl XousDisplay { } } } + +impl FrameBuffer for XousDisplay {} diff --git a/services/graphics-server/src/backend/minifb.rs b/services/graphics-server/src/backend/minifb.rs index 36d5c2964..c8eb9b5b5 100644 --- a/services/graphics-server/src/backend/minifb.rs +++ b/services/graphics-server/src/backend/minifb.rs @@ -3,17 +3,12 @@ use std::sync::{Arc, Mutex, mpsc}; use minifb::{Key, Window, WindowOptions}; -use ux_api::minigfx::Point; -use ux_api::platform::{LINES, WIDTH}; +use ux_api::minigfx::{ColorNative, FrameBuffer, PixelColor, Point}; +use ux_api::platform::*; -const HEIGHT: isize = LINES as isize; - -/// Width of the screen in 32-bit words -const WIDTH_WORDS: usize = 11; -pub const FB_WIDTH_WORDS: usize = WIDTH_WORDS; -pub const FB_WIDTH_PIXELS: usize = WIDTH as usize; -pub const FB_LINES: usize = HEIGHT as usize; -pub const FB_SIZE: usize = WIDTH_WORDS * HEIGHT as usize; // 44 bytes by 536 lines +const LCD_WORDS_PER_LINE: usize = FB_WIDTH_WORDS; +const LCD_PX_PER_LINE: isize = WIDTH as isize; +const LCD_LINES: isize = FB_LINES as isize; const MAX_FPS: usize = 60; const DARK_COLOUR: u32 = 0xB5B5AD; @@ -130,7 +125,7 @@ impl XousDisplay { let mut native_buffer = self.native_buffer.lock().unwrap(); let mut row = 0; for (dest_row, src_row) in - native_buffer.chunks_mut(WIDTH as _).zip(self.emulated_buffer.chunks(WIDTH_WORDS as _)) + native_buffer.chunks_mut(WIDTH as _).zip(self.emulated_buffer.chunks(FB_WIDTH_WORDS as _)) { for (dest_cell, src_cell) in dest_row.chunks_mut(32).zip(src_row) { for (bit, dest) in dest_cell.iter_mut().enumerate() { @@ -147,6 +142,112 @@ impl XousDisplay { } } +impl FrameBuffer for XousDisplay { + /// Puts a pixel of ColorNative at x, y. (0, 0) is defined as the lower left corner. + fn put_pixel(&mut self, p: Point, color: ColorNative) { + let fb: &mut [u32] = &mut self.emulated_buffer; + let clip_y: usize; + if p.y >= LCD_LINES { + clip_y = LCD_LINES as usize - 1; + } else if p.y < 0 { + clip_y = 0; + } else { + clip_y = p.y as usize; + } + + let clip_x: usize; + if p.x >= LCD_PX_PER_LINE { + clip_x = LCD_PX_PER_LINE as usize - 1; + } else if p.x < 0 { + clip_x = 0; + } else { + clip_x = p.x as usize; + } + + let pc = PixelColor::from(color.0); + + if pc == PixelColor::Light { + fb[(clip_x + clip_y * LCD_WORDS_PER_LINE * 32) / 32] |= 1 << (clip_x % 32) + } else { + fb[(clip_x + clip_y * LCD_WORDS_PER_LINE * 32) / 32] &= !(1 << (clip_x % 32)) + } + // set the dirty bit on the line that contains the pixel + fb[clip_y * LCD_WORDS_PER_LINE + (LCD_WORDS_PER_LINE - 1)] |= 0x1_0000; + } + + /// Retrieves a pixel value from the frame buffer; returns None if the point is out of bounds. + /// + /// Note: this has not be carefully tested as this API is not used by the legacy code base. + /// Anyone using this API for the first time may benefit in checking that it is correct. + fn get_pixel(&mut self, p: Point) -> Option { + let fb: &mut [u32] = &mut self.emulated_buffer; + let clip_y: usize; + if p.y >= LCD_LINES { + return None; + } else if p.y < 0 { + return None; + } else { + clip_y = p.y as usize; + } + + let clip_x: usize; + if p.x >= LCD_PX_PER_LINE { + return None; + } else if p.x < 0 { + return None; + } else { + clip_x = p.x as usize; + } + Some(ColorNative::from( + (fb[(clip_x + clip_y * LCD_WORDS_PER_LINE * 32) / 32] & (1 << (clip_x % 32))) as usize, + )); + todo!("Check that this API is implemented correctly before using it!"); + } + + /// XORs a pixel to what is in the existing frame buffer. The exact definition of "XOR" is somewhat + /// ambiguous for full color systems but is generally meant to imply a light/dark swap of foreground + /// and background colors for a color theme. + fn xor_pixel(&mut self, p: Point) { + let fb: &mut [u32] = &mut self.emulated_buffer; + let clip_y: usize; + if p.y >= LCD_LINES { + return; + } else if p.y < 0 { + return; + } else { + clip_y = p.y as usize; + } + + let clip_x: usize; + if p.x >= LCD_PX_PER_LINE { + return; + } else if p.x < 0 { + return; + } else { + clip_x = p.x as usize; + } + + fb[(clip_x + clip_y * LCD_WORDS_PER_LINE * 32) / 32] ^= 1 << (clip_x % 32); + // set the dirty bit on the line that contains the pixel + fb[clip_y * LCD_WORDS_PER_LINE + (LCD_WORDS_PER_LINE - 1)] |= 0x1_0000; + } + + /// Swaps the drawable buffer to the screen and sends it to the hardware + fn draw(&mut self) { self.redraw(); } + + /// Clears the drawable buffer + fn clear(&mut self) { + let fb: &mut [u32] = &mut self.emulated_buffer; + fb.fill(0xFFFF_FFFF); + } + + /// Returns the size of the frame buffer as a Point + fn dimensions(&self) -> Point { self.screen_size() } + + /// Returns a raw pointer to the frame buffer + unsafe fn raw_mut(&mut self) -> &mut ux_api::platform::FbRaw { self.native_buffer() } +} + impl MinifbThread { pub fn run_while(self, mut predicate: impl FnMut() -> bool) { let mut window = Window::new(