-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ShadowBuffer to improve drawing performance (osbook_day09c)
- Loading branch information
Showing
7 changed files
with
121 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ mod mouse; | |
mod paging; | ||
mod pci; | ||
mod prelude; | ||
mod shadow_buffer; | ||
mod sync; | ||
mod timer; | ||
mod window; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use crate::{ | ||
framebuffer::{self, PixelDraw}, | ||
graphics::{Color, Draw, Point, Size}, | ||
}; | ||
use alloc::{vec, vec::Vec}; | ||
use core::convert::TryFrom; | ||
|
||
pub(crate) struct ShadowBuffer { | ||
size: Size<i32>, | ||
bytes_per_pixel: i32, | ||
data: Vec<u8>, | ||
pixel_drawer: &'static (dyn PixelDraw + Send + Sync), | ||
} | ||
|
||
impl Draw for ShadowBuffer { | ||
fn size(&self) -> Size<i32> { | ||
self.size | ||
} | ||
|
||
fn draw(&mut self, p: Point<i32>, c: Color) { | ||
if let Some(pixel_index) = self.pixel_index(p) { | ||
self.pixel_drawer.pixel_draw(&mut self.data, pixel_index, c) | ||
} | ||
} | ||
} | ||
|
||
impl ShadowBuffer { | ||
pub(crate) fn new(size: Size<i32>) -> Self { | ||
let screen_info = *framebuffer::info(); | ||
|
||
#[allow(clippy::unwrap_used)] | ||
let data = vec![0; usize::try_from(size.x * size.y * screen_info.bytes_per_pixel).unwrap()]; | ||
let bytes_per_pixel = screen_info.bytes_per_pixel; | ||
#[allow(clippy::unwrap_used)] | ||
let pixel_drawer = framebuffer::select_pixel_drawer(screen_info.pixel_format).unwrap(); | ||
|
||
Self { | ||
size, | ||
bytes_per_pixel, | ||
data, | ||
pixel_drawer, | ||
} | ||
} | ||
|
||
pub(crate) fn data(&self) -> &[u8] { | ||
&self.data | ||
} | ||
|
||
fn pixel_index(&self, p: Point<i32>) -> Option<usize> { | ||
if !self.area().contains(&p) { | ||
return None; | ||
} | ||
usize::try_from((p.y * self.size.x + p.x) * self.bytes_per_pixel).ok() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters