diff --git a/src/drawing/device.rs b/src/drawing/device.rs index cb0938f..d74d1ef 100644 --- a/src/drawing/device.rs +++ b/src/drawing/device.rs @@ -7,6 +7,12 @@ where Self: Screen + GraphicsWriter, Color: Clone + Copy, { + /// Draws a character at the given `(x, y)` coordinant to the specified `color`. + fn draw_character(&self, x: usize, y: usize, character: char, color: Color); + + /// Draws a line from `start` to `end` with the specified `color`. + fn draw_line(&self, start: Point, end: Point, color: Color); + fn draw_triangle(&self, v0: &Point, v1: &Point, v2: &Point, color: Color) { let screen_width = self.get_width() as i32; let screen_height = self.get_height() as i32; diff --git a/src/writers/graphics_320x200x256.rs b/src/writers/graphics_320x200x256.rs index 08c8a75..e5b1a37 100644 --- a/src/writers/graphics_320x200x256.rs +++ b/src/writers/graphics_320x200x256.rs @@ -53,28 +53,7 @@ impl Screen for Graphics320x200x256 { } } -impl Device for Graphics320x200x256 {} - -impl GraphicsWriter for Graphics320x200x256 { - fn clear_screen(&self, color: u8) { - for x in 0..WIDTH { - for y in 0..HEIGHT { - self.set_pixel(x, y, color); - } - } - } - fn draw_line(&self, start: Point, end: Point, color: u8) { - for Point { x, y } in Bresenham::new(start, end) { - self.set_pixel(x as usize, y as usize, color); - } - } - fn set_pixel(&self, x: usize, y: usize, color: u8) { - let (_vga, frame_buffer) = self.get_frame_buffer(); - let offset = (y * WIDTH) + x; - unsafe { - frame_buffer.add(offset).write_volatile(color); - } - } +impl Device for Graphics320x200x256 { fn draw_character(&self, x: usize, y: usize, character: char, color: u8) { let character = match font8x8::BASIC_FONTS.get(character) { Some(character) => character, @@ -91,6 +70,29 @@ impl GraphicsWriter for Graphics320x200x256 { } } } + + fn draw_line(&self, start: Point, end: Point, color: u8) { + for Point { x, y } in Bresenham::new(start, end) { + self.set_pixel(x as usize, y as usize, color); + } + } +} + +impl GraphicsWriter for Graphics320x200x256 { + fn clear_screen(&self, color: u8) { + for x in 0..WIDTH { + for y in 0..HEIGHT { + self.set_pixel(x, y, color); + } + } + } + fn set_pixel(&self, x: usize, y: usize, color: u8) { + let frame_buffer = self.get_frame_buffer(); + let offset = (y * WIDTH) + x; + unsafe { + frame_buffer.add(offset).write_volatile(color); + } + } fn set_mode(&self) { let mut vga = VGA.lock(); vga.set_video_mode(VideoMode::Mode320x200x256); @@ -107,12 +109,7 @@ impl Graphics320x200x256 { Graphics320x200x256 {} } - /// Returns the start of the `FrameBuffer` as `*mut u8` as - /// well as a lock to the vga driver. This ensures the vga - /// driver stays locked while the frame buffer is in use. - fn get_frame_buffer(&self) -> (SpinlockGuard, *mut u8) { - let mut vga = VGA.lock(); - let frame_buffer = vga.get_frame_buffer(); - (vga, u32::from(frame_buffer) as *mut u8) + fn get_frame_buffer(&self) -> *mut u8 { + u32::from(VGA.lock().get_frame_buffer()) as *mut u8 } } diff --git a/src/writers/graphics_640x480x16.rs b/src/writers/graphics_640x480x16.rs index 54c0ef9..3d5a7ca 100644 --- a/src/writers/graphics_640x480x16.rs +++ b/src/writers/graphics_640x480x16.rs @@ -51,26 +51,7 @@ impl Screen for Graphics640x480x16 { } } -impl Device for Graphics640x480x16 {} - -impl GraphicsWriter for Graphics640x480x16 { - fn clear_screen(&self, color: Color16) { - self.set_write_mode_2(); - let (_vga, frame_buffer) = self.get_frame_buffer(); - for offset in 0..ALL_PLANES_SCREEN_SIZE { - unsafe { - frame_buffer.add(offset).write_volatile(u8::from(color)); - } - } - } - - fn draw_line(&self, start: Point, end: Point, color: Color16) { - self.set_write_mode_0(color); - for Point { x, y } in Bresenham::new(start, end) { - self._set_pixel(x as usize, y as usize, color); - } - } - +impl Device for Graphics640x480x16 { fn draw_character(&self, x: usize, y: usize, character: char, color: Color16) { self.set_write_mode_2(); let character = match font8x8::BASIC_FONTS.get(character) { @@ -89,6 +70,25 @@ impl GraphicsWriter for Graphics640x480x16 { } } + fn draw_line(&self, start: Point, end: Point, color: Color16) { + self.set_write_mode_0(color); + for Point { x, y } in Bresenham::new(start, end) { + self._set_pixel(x as usize, y as usize, color); + } + } +} + +impl GraphicsWriter for Graphics640x480x16 { + fn clear_screen(&self, color: Color16) { + self.set_write_mode_2(); + let frame_buffer = self.get_frame_buffer(); + for offset in 0..ALL_PLANES_SCREEN_SIZE { + unsafe { + frame_buffer.add(offset).write_volatile(u8::from(color)); + } + } + } + /// **Note:** This method is provided for convenience, but has terrible /// performance since it needs to ensure the correct `WriteMode` per pixel /// drawn. If you need to draw more then one pixel, consider using a method @@ -115,7 +115,7 @@ impl Graphics640x480x16 { } fn set_write_mode_0(&self, color: Color16) { - let (mut vga, _frame_buffer) = self.get_frame_buffer(); + let mut vga = VGA.lock(); vga.graphics_controller_registers.write_set_reset(color); vga.graphics_controller_registers .write_enable_set_reset(0xF); @@ -124,7 +124,7 @@ impl Graphics640x480x16 { } fn set_write_mode_2(&self) { - let (mut vga, _frame_buffer) = self.get_frame_buffer(); + let mut vga = VGA.lock(); vga.graphics_controller_registers .set_write_mode(WriteMode::Mode2); vga.graphics_controller_registers.set_bit_mask(0xFF); @@ -132,21 +132,18 @@ impl Graphics640x480x16 { .set_plane_mask(PlaneMask::ALL_PLANES); } - /// Returns the start of the `FrameBuffer` as `*mut u8` as - /// well as a lock to the vga driver. This ensures the vga - /// driver stays locked while the frame buffer is in use. - fn get_frame_buffer(&self) -> (SpinlockGuard, *mut u8) { - let mut vga = VGA.lock(); - let frame_buffer = vga.get_frame_buffer(); - (vga, u32::from(frame_buffer) as *mut u8) + fn get_frame_buffer(&self) -> *mut u8 { + u32::from(VGA.lock().get_frame_buffer()) as *mut u8 } #[inline] fn _set_pixel(&self, x: usize, y: usize, color: Color16) { - let (mut vga, frame_buffer) = self.get_frame_buffer(); + let frame_buffer = self.get_frame_buffer(); let offset = x / 8 + y * WIDTH_IN_BYTES; let pixel_mask = 0x80 >> (x & 0x07); - vga.graphics_controller_registers.set_bit_mask(pixel_mask); + VGA.lock() + .graphics_controller_registers + .set_bit_mask(pixel_mask); unsafe { frame_buffer.add(offset).read_volatile(); frame_buffer.add(offset).write_volatile(u8::from(color)); diff --git a/src/writers/mod.rs b/src/writers/mod.rs index c6c6efa..0dbec0d 100644 --- a/src/writers/mod.rs +++ b/src/writers/mod.rs @@ -185,10 +185,6 @@ pub trait TextWriter: Screen { pub trait GraphicsWriter { /// Clears the screen by setting all pixels to the specified `color`. fn clear_screen(&self, color: Color); - /// Draws a line from `start` to `end` with the specified `color`. - fn draw_line(&self, start: Point, end: Point, color: Color); - /// Draws a character at the given `(x, y)` coordinant to the specified `color`. - fn draw_character(&self, x: usize, y: usize, character: char, color: Color); /// Sets the given pixel at `(x, y)` to the given `color`. fn set_pixel(&self, x: usize, y: usize, color: Color); /// Sets the graphics device to a `VideoMode`.