diff --git a/Cargo.lock b/Cargo.lock index 644bdc6..5a965b2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -109,16 +109,6 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4445909572dbd556c457c849c4ca58623d84b27c8fff1e74b0b4227d8b90d17b" -[[package]] -name = "fontdue" -version = "0.9.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efe23d02309319171d00d794c9ff48d4f903c0e481375b1b04b017470838af04" -dependencies = [ - "hashbrown", - "ttf-parser", -] - [[package]] name = "gimli" version = "0.28.1" @@ -164,7 +154,6 @@ dependencies = [ "crossbeam-queue", "derive-try-from-primitive", "elf", - "fontdue", "hashbrown", "humansize", "intrusive-collections", @@ -376,12 +365,6 @@ dependencies = [ "num-traits", ] -[[package]] -name = "ttf-parser" -version = "0.21.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c591d83f69777866b9126b24c6dd9a18351f177e49d625920d19f989fd31cf8" - [[package]] name = "uart_16550" version = "0.3.0" diff --git a/Cargo.toml b/Cargo.toml index 453e4cc..c07c795 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,10 +28,3 @@ saturating_cast = "0.1.0" humansize = "2.1.3" cast = "0.3.0" az = "1.2.1" -fontdue = { version = "0.9.2", default-features = false, features = ["hashbrown"] } - -[profile.release] -strip = true -lto = true -# opt-level = "z" -codegen-units=1 diff --git a/FiraCode-Regular.ttf b/FiraCode-Regular.ttf deleted file mode 100644 index bd73685..0000000 Binary files a/FiraCode-Regular.ttf and /dev/null differ diff --git a/src/fbuffer.rs b/src/fbuffer.rs deleted file mode 100644 index 24302b3..0000000 --- a/src/fbuffer.rs +++ /dev/null @@ -1,181 +0,0 @@ -use crate::bootinfo::BOOTINFO; -use alloc::{ - borrow::Cow, string::{String, ToString}, vec::Vec, vec -}; -use bootloader_api::info::FrameBufferInfo; -use core::{fmt, slice}; -use fontdue::{ - layout::{CoordinateSystem, GlyphRasterConfig, Layout, LayoutSettings, TextStyle}, - Font, FontSettings -}; -use hashbrown::HashMap; -use spin::{Lazy, Mutex}; - -struct Framebuffer<'a> { - info: FrameBufferInfo, - buffer: &'a mut [u8], - draw_buffer: Vec, - scroll_offset: usize, -} - -impl<'a> Framebuffer<'a> { - fn new(info: FrameBufferInfo, buffer: &'a mut [u8]) -> Self { - let draw_buffer = vec![0; 2 * buffer.len()]; - Self { info, buffer, draw_buffer, scroll_offset: 0 } - } - - fn get_row_buf(&mut self, row: usize) -> &mut [u8] { - &mut self.draw_buffer[(self.info.stride * self.info.bytes_per_pixel * row)..] - [..(self.info.bytes_per_pixel * self.info.width)] - } - - fn get_pixel_buf(&mut self, row: usize, col: usize) -> &mut [u8] { - let pbuf_start = col * self.info.bytes_per_pixel; - let pbuf_len = self.info.bytes_per_pixel; - &mut self.get_row_buf(row)[pbuf_start..][..pbuf_len] - } - - fn set_pixel(&mut self, row: usize, col: usize, value: u8) { - if row >= self.info.height || col >= self.info.width { - return; - } - self.get_pixel_buf((row + self.scroll_offset) % (self.info.height * 2), col).fill(value); - } - - fn clear(&mut self) { - for row in 0..self.info.height * 2 { - self.get_row_buf(row).fill(0); - } - } - - fn update_screen(&mut self) { - let scroll_top_byte_offset = self.row_byte_offset(self.scroll_offset); - let scroll_bot_byte_offset = self.row_byte_offset(self.scroll_offset + self.info.height); - self.buffer - .copy_from_slice(&self.draw_buffer[scroll_top_byte_offset..scroll_bot_byte_offset]); - } - - fn row_byte_offset(&self, row: usize) -> usize { - self.info.stride * self.info.bytes_per_pixel * row - } - - fn scroll_by(&mut self, offset: usize) { - self.scroll_offset = (self.scroll_offset + offset) % (self.info.height * 2); - } -} - -pub static FRAMEBUFFER_WRITER: Lazy> = Lazy::new(|| { - let fbuf = BOOTINFO.framebuffer.as_ref().unwrap(); - - // No good way to get mutable buffer, so do this. No, its probably not safe. Too lazy to do it - // right. - let buf_start = fbuf.buffer().as_ptr(); - let fbuf_buf = unsafe { slice::from_raw_parts_mut(buf_start as *mut u8, fbuf.buffer().len()) }; - let fbuf_info = fbuf.info(); - - let mut fbuf = Framebuffer::new(fbuf_info, fbuf_buf); - fbuf.clear(); - fbuf.update_screen(); - let font = Font::from_bytes( - include_bytes!("../FiraCode-Regular.ttf").as_slice(), - FontSettings::default(), - ) - .unwrap(); - Mutex::new(FramebufferWriter::new(fbuf, font)) -}); - -pub struct FramebufferWriter { - fbuf: Framebuffer<'static>, - out_string: String, - font: Font, - layout: Layout, - next_line_y: usize, - glyph_cache: HashMap>, -} - -impl FramebufferWriter { - fn new(fbuf: Framebuffer<'static>, font: Font) -> Self { - let mut layout = Layout::new(CoordinateSystem::PositiveYDown); - layout.reset(&LayoutSettings { - max_width: Some(fbuf.info.width as f32), - max_height: Some(fbuf.info.height as f32), - ..Default::default() - }); - Self { - fbuf, - out_string: String::new(), - font, - layout, - next_line_y: 0, - glyph_cache: HashMap::new(), - } - } - - fn update_screen(&mut self) { - self.layout.clear(); - self.layout.append(&[&self.font], &TextStyle::new(&self.out_string, 12.0, 0)); - - if (self.next_line_y + self.layout.height() as usize) > self.fbuf.info.height { - let excess_y = - self.layout.height() as usize - (self.fbuf.info.height - self.next_line_y); - self.fbuf.scroll_by(excess_y); - self.next_line_y = self.fbuf.info.height - self.layout.height() as usize; - } - - let glyphs = self.layout.glyphs(); - - let mut first_line_top_offset = None; - - for line in self.layout.lines().unwrap() { - if first_line_top_offset.is_none() { - first_line_top_offset = Some((line.baseline_y - line.max_ascent) as usize); - } - let first_line_top_offset = first_line_top_offset.unwrap(); - for glyph_pos in &glyphs[line.glyph_start..=line.glyph_end] { - if glyph_pos.width == 0 || glyph_pos.height == 0 { - continue; - } - let bitmap: Cow<[u8]> = if let Some(bitmap) = self.glyph_cache.get(&glyph_pos.key) { - bitmap.into() - } else { - let bitmap = self.font.rasterize_config(glyph_pos.key).1; - self.glyph_cache.insert(glyph_pos.key, bitmap.clone()); - bitmap.into() - }; - for (i, &pixel) in bitmap.iter().enumerate() { - let row = i / glyph_pos.width; - let col = i % glyph_pos.width; - self.fbuf.set_pixel( - row + glyph_pos.y as usize - first_line_top_offset + self.next_line_y, - col + glyph_pos.x as usize, - pixel, - ); - } - } - } - self.fbuf.update_screen(); - if self.out_string.ends_with('\n') { - self.out_string.clear(); - self.next_line_y += self.layout.height() as usize; - } else { - self.out_string = self.out_string.split('\n').last().unwrap().to_string(); - } - } -} - -impl fmt::Write for FramebufferWriter { - fn write_char(&mut self, c: char) -> fmt::Result { - self.out_string.push(c); - if c == '\n' { - self.update_screen(); - } - Ok(()) - } - fn write_str(&mut self, s: &str) -> core::fmt::Result { - self.out_string.push_str(s); - if s.contains('\n') { - self.update_screen(); - } - Ok(()) - } -} diff --git a/src/interrupts.rs b/src/interrupts.rs index ca7fcaf..cef282a 100644 --- a/src/interrupts.rs +++ b/src/interrupts.rs @@ -1,13 +1,12 @@ use crate::{ bootinfo::BOOTINFO, - fbuffer::FRAMEBUFFER_WRITER, print, println, serial::SECOND_PORT, tasking::SleepReason, virtual_memory::{ASpaceMutex, AddressSpace, ACTIVE_SPACE, KERNEL_SPACE}, TASKING, }; -use alloc::{boxed::Box, string::ToString, vec::Vec}; +use alloc::{boxed::Box, vec::Vec}; use az::WrappingCast; use cast::{u64, usize}; use core::{arch::asm, ffi::CStr, fmt::Write, ptr, slice}; @@ -252,7 +251,6 @@ extern "C" fn syscall_handler() { match regs.rax { 0 => { let rval = if let Some(chr) = char::from_u32(regs.rcx.wrapping_cast()) { - let _ = FRAMEBUFFER_WRITER.lock().write_char(chr); // writer never gives an error if chr == '\n' { print!("\r\n"); } else { diff --git a/src/main.rs b/src/main.rs index bf30068..78d5725 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,7 +73,6 @@ extern crate alloc; mod bootinfo; -mod fbuffer; mod gdt; mod interrupts; mod kernel_heap; @@ -99,7 +98,6 @@ use elf::{ endian::AnyEndian, ElfBytes, }; -use fbuffer::FRAMEBUFFER_WRITER; use physical_memory::PHYSICAL_MEMORY; use serial::SECOND_PORT; use spin::lazy::Lazy; @@ -130,7 +128,6 @@ pub fn main() { Lazy::force(&PHYSICAL_MEMORY); Lazy::force(&KERNEL_SPACE); Lazy::force(&ACTIVE_SPACE); - Lazy::force(&FRAMEBUFFER_WRITER); interrupts::init(); pit::init(100); let initrd = unsafe {