Make get_frame_buffer more generic

This commit is contained in:
Ryan Kennedy 2020-04-16 15:47:46 -05:00
parent 6708631bbe
commit c3155975ad
4 changed files with 18 additions and 9 deletions

View file

@ -44,7 +44,7 @@ impl Screen for Graphics320x200x256 {
impl GraphicsWriter<u8> for Graphics320x200x256 { impl GraphicsWriter<u8> for Graphics320x200x256 {
fn clear_screen(&self, color: u8) { fn clear_screen(&self, color: u8) {
unsafe { unsafe {
self.get_frame_buffer().write_bytes(color, Self::SIZE); self.get_frame_buffer::<u8>().write_bytes(color, Self::SIZE);
} }
} }
fn draw_line(&self, start: Point<isize>, end: Point<isize>, color: u8) { fn draw_line(&self, start: Point<isize>, end: Point<isize>, color: u8) {
@ -55,7 +55,9 @@ impl GraphicsWriter<u8> for Graphics320x200x256 {
fn set_pixel(&self, x: usize, y: usize, color: u8) { fn set_pixel(&self, x: usize, y: usize, color: u8) {
let offset = (y * WIDTH) + x; let offset = (y * WIDTH) + x;
unsafe { unsafe {
self.get_frame_buffer().add(offset).write_volatile(color); self.get_frame_buffer::<u8>()
.add(offset)
.write_volatile(color);
} }
} }
fn draw_character(&self, x: usize, y: usize, character: char, color: u8) { fn draw_character(&self, x: usize, y: usize, character: char, color: u8) {
@ -82,6 +84,9 @@ impl GraphicsWriter<u8> for Graphics320x200x256 {
// so explicitly set it. // so explicitly set it.
vga.color_palette_registers.load_palette(&DEFAULT_PALETTE); vga.color_palette_registers.load_palette(&DEFAULT_PALETTE);
} }
fn get_frame_buffer<T>(&self) -> *mut T {
u32::from(VGA.lock().get_frame_buffer()) as *mut T
}
} }
impl Graphics320x200x256 { impl Graphics320x200x256 {

View file

@ -44,7 +44,7 @@ impl Screen for Graphics320x240x256 {
impl GraphicsWriter<u8> for Graphics320x240x256 { impl GraphicsWriter<u8> for Graphics320x240x256 {
fn clear_screen(&self, color: u8) { fn clear_screen(&self, color: u8) {
let frame_buffer = self.get_frame_buffer(); let frame_buffer = self.get_frame_buffer::<u8>();
VGA.lock() VGA.lock()
.sequencer_registers .sequencer_registers
.set_plane_mask(PlaneMask::ALL_PLANES); .set_plane_mask(PlaneMask::ALL_PLANES);
@ -58,7 +58,7 @@ impl GraphicsWriter<u8> for Graphics320x240x256 {
} }
} }
fn set_pixel(&self, x: usize, y: usize, color: u8) { fn set_pixel(&self, x: usize, y: usize, color: u8) {
let frame_buffer = self.get_frame_buffer(); let frame_buffer = self.get_frame_buffer::<u8>();
unsafe { unsafe {
let offset = (WIDTH * y + x) / 4; let offset = (WIDTH * y + x) / 4;
let plane_mask = 0x1 << (x & 3); let plane_mask = 0x1 << (x & 3);
@ -92,6 +92,9 @@ impl GraphicsWriter<u8> for Graphics320x240x256 {
// so explicitly set it. // so explicitly set it.
vga.color_palette_registers.load_palette(&DEFAULT_PALETTE); vga.color_palette_registers.load_palette(&DEFAULT_PALETTE);
} }
fn get_frame_buffer<T>(&self) -> *mut T {
u32::from(VGA.lock().get_frame_buffer()) as *mut T
}
} }
impl Graphics320x240x256 { impl Graphics320x240x256 {

View file

@ -47,7 +47,7 @@ impl GraphicsWriter<Color16> for Graphics640x480x16 {
fn clear_screen(&self, color: Color16) { fn clear_screen(&self, color: Color16) {
self.set_write_mode_2(); self.set_write_mode_2();
unsafe { unsafe {
self.get_frame_buffer() self.get_frame_buffer::<u8>()
.write_bytes(u8::from(color), Self::SIZE); .write_bytes(u8::from(color), Self::SIZE);
} }
} }
@ -94,6 +94,9 @@ impl GraphicsWriter<Color16> for Graphics640x480x16 {
// so explicitly set it. // so explicitly set it.
vga.color_palette_registers.load_palette(&DEFAULT_PALETTE); vga.color_palette_registers.load_palette(&DEFAULT_PALETTE);
} }
fn get_frame_buffer<T>(&self) -> *mut T {
u32::from(VGA.lock().get_frame_buffer()) as *mut T
}
} }
impl Graphics640x480x16 { impl Graphics640x480x16 {
@ -122,7 +125,7 @@ impl Graphics640x480x16 {
#[inline] #[inline]
fn _set_pixel(self, x: usize, y: usize, color: Color16) { fn _set_pixel(self, x: usize, y: usize, color: Color16) {
let frame_buffer = self.get_frame_buffer(); let frame_buffer = self.get_frame_buffer::<u8>();
let offset = x / 8 + y * WIDTH_IN_BYTES; let offset = x / 8 + y * WIDTH_IN_BYTES;
let pixel_mask = 0x80 >> (x & 0x07); let pixel_mask = 0x80 >> (x & 0x07);
VGA.lock() VGA.lock()

View file

@ -200,7 +200,5 @@ pub trait GraphicsWriter<Color> {
/// Sets the graphics device to a `VideoMode`. /// Sets the graphics device to a `VideoMode`.
fn set_mode(&self); fn set_mode(&self);
/// Returns the frame buffer for this vga mode. /// Returns the frame buffer for this vga mode.
fn get_frame_buffer(&self) -> *mut u8 { fn get_frame_buffer<T>(&self) -> *mut T;
u32::from(VGA.lock().get_frame_buffer()) as *mut u8
}
} }