diff --git a/src/writers/graphics_320x200x256.rs b/src/writers/graphics_320x200x256.rs index e2c8cc0..48cd4ee 100644 --- a/src/writers/graphics_320x200x256.rs +++ b/src/writers/graphics_320x200x256.rs @@ -44,10 +44,8 @@ impl Screen 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); - } + unsafe { + self.get_frame_buffer().write_bytes(color, Self::SIZE); } } fn draw_line(&self, start: Point, end: Point, color: u8) { @@ -56,10 +54,9 @@ impl GraphicsWriter for Graphics320x200x256 { } } 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); + self.get_frame_buffer().add(offset).write_volatile(color); } } fn draw_character(&self, x: usize, y: usize, character: char, color: u8) { @@ -93,13 +90,4 @@ impl Graphics320x200x256 { pub const fn new() -> 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<'static, Vga>, *mut u8) { - let mut vga = VGA.lock(); - let frame_buffer = vga.get_frame_buffer(); - (vga, u32::from(frame_buffer) as *mut u8) - } } diff --git a/src/writers/graphics_320x240x256.rs b/src/writers/graphics_320x240x256.rs index fbaaad2..a3ec1d1 100644 --- a/src/writers/graphics_320x240x256.rs +++ b/src/writers/graphics_320x240x256.rs @@ -23,9 +23,8 @@ impl Screen for Graphics320x240x256 { impl GraphicsWriter for Graphics320x240x256 { fn clear_screen(&self, color: u8) { - let (_vga, frame_buffer) = self.get_frame_buffer(); unsafe { - frame_buffer.write_bytes(color, Self::SIZE); + self.get_frame_buffer().write_bytes(color, Self::SIZE); } } fn draw_line(&self, start: Point, end: Point, color: u8) { @@ -34,11 +33,12 @@ impl GraphicsWriter for Graphics320x240x256 { } } fn set_pixel(&self, x: usize, y: usize, color: u8) { - let (mut vga, frame_buffer) = self.get_frame_buffer(); + let frame_buffer = self.get_frame_buffer(); unsafe { let offset = (WIDTH * y + x) / 4; let plane_mask = 0x1 << (x & 3); - vga.sequencer_registers + VGA.lock() + .sequencer_registers .set_plane_mask(PlaneMask::from_bits(plane_mask).unwrap()); frame_buffer.add(offset).write_volatile(color); } @@ -73,13 +73,4 @@ impl Graphics320x240x256 { pub const fn new() -> Graphics320x240x256 { Graphics320x240x256 } - - /// 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<'static, Vga>, *mut u8) { - let mut vga = VGA.lock(); - let frame_buffer = vga.get_frame_buffer(); - (vga, u32::from(frame_buffer) as *mut u8) - } } diff --git a/src/writers/graphics_640x480x16.rs b/src/writers/graphics_640x480x16.rs index 43a42b0..80a015f 100644 --- a/src/writers/graphics_640x480x16.rs +++ b/src/writers/graphics_640x480x16.rs @@ -3,15 +3,13 @@ use crate::{ colors::{Color16, DEFAULT_PALETTE}, drawing::{Bresenham, Point}, registers::{PlaneMask, WriteMode}, - vga::{Vga, VideoMode, VGA}, + vga::{VideoMode, VGA}, }; use font8x8::UnicodeFonts; -use spinning_top::SpinlockGuard; const WIDTH: usize = 640; const HEIGHT: usize = 480; -const SIZE: usize = WIDTH * HEIGHT; -const ALL_PLANES_SCREEN_SIZE: usize = (WIDTH * HEIGHT) / 8; +const SIZE: usize = (WIDTH * HEIGHT) / 8; const WIDTH_IN_BYTES: usize = WIDTH / 8; /// A basic interface for interacting with vga graphics mode 640x480x16 @@ -48,11 +46,9 @@ impl Screen 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)); - } + unsafe { + self.get_frame_buffer() + .write_bytes(u8::from(color), Self::SIZE); } } @@ -107,7 +103,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); @@ -116,7 +112,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); @@ -124,21 +120,14 @@ 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<'static, Vga>, *mut u8) { - let mut vga = VGA.lock(); - let frame_buffer = vga.get_frame_buffer(); - (vga, u32::from(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 3d83742..1ace5cf 100644 --- a/src/writers/mod.rs +++ b/src/writers/mod.rs @@ -199,4 +199,8 @@ pub trait GraphicsWriter { fn set_pixel(&self, x: usize, y: usize, color: Color); /// Sets the graphics device to a `VideoMode`. fn set_mode(&self); + /// Returns the frame buffer for this vga mode. + fn get_frame_buffer(&self) -> *mut u8 { + u32::from(VGA.lock().get_frame_buffer()) as *mut u8 + } }