Move drawing to device

This commit is contained in:
Ryan Kennedy 2020-03-28 21:45:03 -05:00
parent 6ba531a957
commit 87cbf76faf
4 changed files with 60 additions and 64 deletions

View file

@ -7,6 +7,12 @@ where
Self: Screen + GraphicsWriter<Color>,
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<isize>, end: Point<isize>, color: Color);
fn draw_triangle(&self, v0: &Point<i32>, v1: &Point<i32>, v2: &Point<i32>, color: Color) {
let screen_width = self.get_width() as i32;
let screen_height = self.get_height() as i32;

View file

@ -53,28 +53,7 @@ impl Screen for Graphics320x200x256 {
}
}
impl Device<u8> for Graphics320x200x256 {}
impl GraphicsWriter<u8> 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<isize>, end: Point<isize>, 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<u8> 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<u8> for Graphics320x200x256 {
}
}
}
fn draw_line(&self, start: Point<isize>, end: Point<isize>, color: u8) {
for Point { x, y } in Bresenham::new(start, end) {
self.set_pixel(x as usize, y as usize, color);
}
}
}
impl GraphicsWriter<u8> 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<Vga>, *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
}
}

View file

@ -51,26 +51,7 @@ impl Screen for Graphics640x480x16 {
}
}
impl Device<Color16> for Graphics640x480x16 {}
impl GraphicsWriter<Color16> 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<isize>, end: Point<isize>, 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<Color16> 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<Color16> for Graphics640x480x16 {
}
}
fn draw_line(&self, start: Point<isize>, end: Point<isize>, 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<Color16> 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<Vga>, *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));

View file

@ -185,10 +185,6 @@ pub trait TextWriter: Screen {
pub trait GraphicsWriter<Color> {
/// 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<isize>, end: Point<isize>, 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`.