Be able to use a trait object

This commit is contained in:
Ryan Kennedy 2020-04-16 20:52:07 -05:00
parent 30728c0534
commit 103fba0eac
5 changed files with 15 additions and 17 deletions

View file

@ -233,7 +233,7 @@ impl GraphicsWriter<u32> for BochsDevice {
.write_volatile(color); .write_volatile(color);
} }
} }
fn get_frame_buffer<T>(&self) -> *mut T { fn get_frame_buffer(&self) -> *mut u32 {
self.virtual_address.as_mut_ptr() self.virtual_address.as_mut_ptr()
} }
} }

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::<u8>().write_bytes(color, Self::SIZE); self.get_frame_buffer().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,9 +55,7 @@ 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::<u8>() self.get_frame_buffer().add(offset).write_volatile(color);
.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) {
@ -76,8 +74,8 @@ impl GraphicsWriter<u8> for Graphics320x200x256 {
} }
} }
} }
fn get_frame_buffer<T>(&self) -> *mut T { fn get_frame_buffer(&self) -> *mut u8 {
u32::from(VGA.lock().get_frame_buffer()) as *mut T u32::from(VGA.lock().get_frame_buffer()) as *mut u8
} }
} }

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::<u8>(); let frame_buffer = self.get_frame_buffer();
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::<u8>(); let frame_buffer = self.get_frame_buffer();
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);
@ -84,8 +84,8 @@ impl GraphicsWriter<u8> for Graphics320x240x256 {
} }
} }
} }
fn get_frame_buffer<T>(&self) -> *mut T { fn get_frame_buffer(&self) -> *mut u8 {
u32::from(VGA.lock().get_frame_buffer()) as *mut T u32::from(VGA.lock().get_frame_buffer()) as *mut u8
} }
} }

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::<u8>() self.get_frame_buffer()
.write_bytes(u8::from(color), Self::SIZE); .write_bytes(u8::from(color), Self::SIZE);
} }
} }
@ -86,8 +86,8 @@ impl GraphicsWriter<Color16> for Graphics640x480x16 {
self._set_pixel(x, y, color); self._set_pixel(x, y, color);
} }
fn get_frame_buffer<T>(&self) -> *mut T { fn get_frame_buffer(&self) -> *mut Color16 {
u32::from(VGA.lock().get_frame_buffer()) as *mut T u32::from(VGA.lock().get_frame_buffer()) as *mut Color16
} }
} }
@ -127,7 +127,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::<u8>(); let frame_buffer = self.get_frame_buffer();
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()
@ -135,7 +135,7 @@ impl Graphics640x480x16 {
.set_bit_mask(pixel_mask); .set_bit_mask(pixel_mask);
unsafe { unsafe {
frame_buffer.add(offset).read_volatile(); frame_buffer.add(offset).read_volatile();
frame_buffer.add(offset).write_volatile(u8::from(color)); frame_buffer.add(offset).write_volatile(color);
} }
} }
} }

View file

@ -198,5 +198,5 @@ pub trait GraphicsWriter<Color> {
/// Sets the given pixel at `(x, y)` to the given `color`. /// Sets the given pixel at `(x, y)` to the given `color`.
fn set_pixel(&self, x: usize, y: usize, color: Color); fn set_pixel(&self, x: usize, y: usize, color: Color);
/// Returns the frame buffer for this vga mode. /// Returns the frame buffer for this vga mode.
fn get_frame_buffer<T>(&self) -> *mut T; fn get_frame_buffer(&self) -> *mut Color;
} }