Download kernel/src/graphics/surface.rs from Jaswanth-2008/AeroOS: direct link, hf CLI and curl.
- Browser
- Download file 12.6 kB
-
https://huggingface.co/Jaswanth-2008/AeroOS/resolve/main/kernel/src/graphics/surface.rs
- Command line
-
hf download hf://Jaswanth-2008/AeroOS/kernel/src/graphics/surface.rs
-
curl -L -o surface.rs https://huggingface.co/Jaswanth-2008/AeroOS/resolve/main/kernel/src/graphics/surface.rs
12.6 kB
| extern crate alloc; | |
| use alloc::vec; | |
| use alloc::vec::Vec; | |
| use bootloader_api::info::PixelFormat; | |
| use crate::drivers::framebuffer::FramebufferConfig; | |
| use crate::graphics::color::Color; | |
| use crate::graphics::font::{get_glyph, FONT_HEIGHT, FONT_WIDTH}; | |
| use crate::graphics::icons::{get_icon_16, IconType, MOUSE_CURSOR, MOUSE_CURSOR_HEIGHT, MOUSE_CURSOR_WIDTH}; | |
| pub struct Surface { | |
| pub width: usize, | |
| pub height: usize, | |
| pub buffer: Vec<u32>, | |
| } | |
| impl Surface { | |
| pub fn new(width: usize, height: usize) -> Self { | |
| Self { | |
| width, | |
| height, | |
| buffer: vec![0; width * height], | |
| } | |
| } | |
| pub fn clear(&mut self, color: Color) { | |
| let val = color.to_u32_argb(); | |
| self.buffer.fill(val); | |
| } | |
| pub fn set_pixel(&mut self, x: i32, y: i32, color: Color) { | |
| if x < 0 || y < 0 || (x as usize) >= self.width || (y as usize) >= self.height { | |
| return; | |
| } | |
| let idx = (y as usize) * self.width + (x as usize); | |
| if color.a == 255 { | |
| self.buffer[idx] = color.to_u32_argb(); | |
| } else if color.a > 0 { | |
| let bg_val = self.buffer[idx]; | |
| let bg = Color::rgba( | |
| ((bg_val >> 16) & 0xFF) as u8, | |
| ((bg_val >> 8) & 0xFF) as u8, | |
| (bg_val & 0xFF) as u8, | |
| ((bg_val >> 24) & 0xFF) as u8, | |
| ); | |
| let blended = color.blend(bg); | |
| self.buffer[idx] = blended.to_u32_argb(); | |
| } | |
| } | |
| pub fn draw_rect(&mut self, x: i32, y: i32, w: usize, h: usize, color: Color) { | |
| for dy in 0..h { | |
| for dx in 0..w { | |
| self.set_pixel(x + dx as i32, y + dy as i32, color); | |
| } | |
| } | |
| } | |
| pub fn draw_rect_outline(&mut self, x: i32, y: i32, w: usize, h: usize, color: Color, thickness: usize) { | |
| for t in 0..thickness { | |
| let offset = t as i32; | |
| let current_w = if w > 2 * t { w - 2 * t } else { 0 }; | |
| let current_h = if h > 2 * t { h - 2 * t } else { 0 }; | |
| for dx in 0..current_w { | |
| self.set_pixel(x + offset + dx as i32, y + offset, color); | |
| self.set_pixel(x + offset + dx as i32, y + offset + current_h as i32 - 1, color); | |
| } | |
| for dy in 0..current_h { | |
| self.set_pixel(x + offset, y + offset + dy as i32, color); | |
| self.set_pixel(x + offset + current_w as i32 - 1, y + offset + dy as i32, color); | |
| } | |
| } | |
| } | |
| pub fn draw_rounded_rect(&mut self, x: i32, y: i32, w: usize, h: usize, radius: i32, color: Color) { | |
| let r = radius.min((w / 2) as i32).min((h / 2) as i32); | |
| let r_sq = r * r; | |
| for dy in 0..h as i32 { | |
| for dx in 0..w as i32 { | |
| let in_corner_tl = dx < r && dy < r; | |
| let in_corner_tr = dx >= (w as i32 - r) && dy < r; | |
| let in_corner_bl = dx < r && dy >= (h as i32 - r); | |
| let in_corner_br = dx >= (w as i32 - r) && dy >= (h as i32 - r); | |
| let draw = if in_corner_tl { | |
| let cx = r - dx; | |
| let cy = r - dy; | |
| cx * cx + cy * cy <= r_sq | |
| } else if in_corner_tr { | |
| let cx = dx - (w as i32 - r - 1); | |
| let cy = r - dy; | |
| cx * cx + cy * cy <= r_sq | |
| } else if in_corner_bl { | |
| let cx = r - dx; | |
| let cy = dy - (h as i32 - r - 1); | |
| cx * cx + cy * cy <= r_sq | |
| } else if in_corner_br { | |
| let cx = dx - (w as i32 - r - 1); | |
| let cy = dy - (h as i32 - r - 1); | |
| cx * cx + cy * cy <= r_sq | |
| } else { | |
| true | |
| }; | |
| if draw { | |
| self.set_pixel(x + dx, y + dy, color); | |
| } | |
| } | |
| } | |
| } | |
| pub fn draw_gradient_v(&mut self, x: i32, y: i32, w: usize, h: usize, top: Color, bottom: Color) { | |
| if h == 0 || w == 0 { | |
| return; | |
| } | |
| let total = (h - 1).max(1) as u32; | |
| for dy in 0..h { | |
| let py = y + dy as i32; | |
| if py < 0 || py as usize >= self.height { | |
| continue; | |
| } | |
| let factor = dy as u32; | |
| let inv_factor = (h - 1 - dy) as u32; | |
| let r = ((top.r as u32 * inv_factor + bottom.r as u32 * factor) / total) as u8; | |
| let g = ((top.g as u32 * inv_factor + bottom.g as u32 * factor) / total) as u8; | |
| let b = ((top.b as u32 * inv_factor + bottom.b as u32 * factor) / total) as u8; | |
| let a = ((top.a as u32 * inv_factor + bottom.a as u32 * factor) / total) as u8; | |
| let val = Color::rgba(r, g, b, a).to_u32_argb(); | |
| let x0 = x.max(0) as usize; | |
| let x1 = ((x + w as i32).max(0) as usize).min(self.width); | |
| if x1 > x0 { | |
| let row_offset = py as usize * self.width; | |
| self.buffer[row_offset + x0..row_offset + x1].fill(val); | |
| } | |
| } | |
| } | |
| pub fn draw_drop_shadow(&mut self, x: i32, y: i32, w: usize, h: usize, radius: i32, blur: usize) { | |
| let max_blur = blur.min(4); | |
| for b in (1..=max_blur).rev() { | |
| let alpha = ((20 * (max_blur - b + 1)) / max_blur) as u8; | |
| let shadow_color = Color::rgba(0, 0, 0, alpha); | |
| let offset = b as i32; | |
| self.draw_rounded_rect( | |
| x - offset, | |
| y - offset + 2, | |
| w + (b * 2), | |
| h + (b * 2), | |
| radius + offset, | |
| shadow_color, | |
| ); | |
| } | |
| } | |
| pub fn draw_text(&mut self, text: &str, start_x: i32, start_y: i32, color: Color) { | |
| let mut cur_x = start_x; | |
| let mut cur_y = start_y; | |
| for c in text.chars() { | |
| if c == '\n' { | |
| cur_x = start_x; | |
| cur_y += FONT_HEIGHT as i32; | |
| continue; | |
| } | |
| let glyph = get_glyph(c); | |
| for (row_idx, &row_byte) in glyph.iter().enumerate() { | |
| for bit in 0..8 { | |
| if (row_byte & (0x80 >> bit)) != 0 { | |
| self.set_pixel(cur_x + bit, cur_y + row_idx as i32, color); | |
| } | |
| } | |
| } | |
| cur_x += FONT_WIDTH as i32; | |
| } | |
| } | |
| pub fn draw_text_with_shadow(&mut self, text: &str, x: i32, y: i32, color: Color, shadow_color: Color) { | |
| self.draw_text(text, x + 1, y + 1, shadow_color); | |
| self.draw_text(text, x, y, color); | |
| } | |
| pub fn draw_icon_16(&mut self, icon: IconType, x: i32, y: i32, color: Color) { | |
| let bitmap = get_icon_16(icon); | |
| for row in 0..16 { | |
| let row_val = bitmap[row]; | |
| for col in 0..16 { | |
| if (row_val & (0x8000 >> col)) != 0 { | |
| self.set_pixel(x + col, y + row as i32, color); | |
| } | |
| } | |
| } | |
| } | |
| pub fn draw_close_cross(&mut self, cx: i32, cy: i32, size: i32, thickness: i32, color: Color) { | |
| for d in -size..=size { | |
| for t in 0..thickness { | |
| self.set_pixel(cx + d + t, cy + d, color); | |
| self.set_pixel(cx + d + t, cy - d, color); | |
| } | |
| } | |
| } | |
| pub fn draw_mouse_cursor(&mut self, x: i32, y: i32) { | |
| for dy in 0..MOUSE_CURSOR_HEIGHT { | |
| for dx in 0..MOUSE_CURSOR_WIDTH { | |
| let pixel = MOUSE_CURSOR[dy][dx]; | |
| if pixel == 1 { | |
| self.set_pixel(x + dx as i32, y + dy as i32, Color::BLACK); | |
| } else if pixel == 2 { | |
| self.set_pixel(x + dx as i32, y + dy as i32, Color::WHITE); | |
| } | |
| } | |
| } | |
| } | |
| pub fn copy_from(&mut self, src: &Surface) { | |
| let copy_len = self.buffer.len().min(src.buffer.len()); | |
| self.buffer[..copy_len].copy_from_slice(&src.buffer[..copy_len]); | |
| } | |
| pub fn copy_rect_from(&mut self, src: &Surface, rx: i32, ry: i32, rw: usize, rh: usize) { | |
| let x0 = rx.max(0) as usize; | |
| let y0 = ry.max(0) as usize; | |
| let x1 = ((rx + rw as i32).max(0) as usize).min(self.width).min(src.width); | |
| let y1 = ((ry + rh as i32).max(0) as usize).min(self.height).min(src.height); | |
| if x1 <= x0 || y1 <= y0 { | |
| return; | |
| } | |
| for y in y0..y1 { | |
| let row_offset = y * self.width; | |
| let src_offset = y * src.width; | |
| self.buffer[row_offset + x0..row_offset + x1] | |
| .copy_from_slice(&src.buffer[src_offset + x0..src_offset + x1]); | |
| } | |
| } | |
| pub fn blit_rect_to_framebuffer(&self, fb: &mut FramebufferConfig, rx: i32, ry: i32, rw: usize, rh: usize) { | |
| let x0 = rx.max(0) as usize; | |
| let y0 = ry.max(0) as usize; | |
| let x1 = ((rx + rw as i32).max(0) as usize).min(self.width).min(fb.width); | |
| let y1 = ((ry + rh as i32).max(0) as usize).min(self.height).min(fb.height); | |
| if x1 <= x0 || y1 <= y0 { | |
| return; | |
| } | |
| let bpp = fb.bytes_per_pixel; | |
| let is_bgr = fb.pixel_format == PixelFormat::Bgr; | |
| // Ultra-fast 32-bit aligned word blit (avoids 4 separate MMIO traps per pixel in VirtualBox IEM) | |
| if is_bgr && bpp == 4 { | |
| for y in y0..y1 { | |
| let src_row = &self.buffer[y * self.width + x0..y * self.width + x1]; | |
| let dst_ptr = unsafe { (fb.buffer as *mut u32).add(y * fb.stride + x0) }; | |
| for (i, &val) in src_row.iter().enumerate() { | |
| unsafe { | |
| *dst_ptr.add(i) = val | 0xFF000000; | |
| } | |
| } | |
| } | |
| return; | |
| } | |
| for y in y0..y1 { | |
| let src_row_start = y * self.width; | |
| let dst_row_start = y * fb.stride * bpp; | |
| for x in x0..x1 { | |
| let val = self.buffer[src_row_start + x]; | |
| let r = ((val >> 16) & 0xFF) as u8; | |
| let g = ((val >> 8) & 0xFF) as u8; | |
| let b = (val & 0xFF) as u8; | |
| let dst_offset = dst_row_start + x * bpp; | |
| unsafe { | |
| let ptr = fb.buffer.add(dst_offset); | |
| if is_bgr { | |
| *ptr = b; | |
| *ptr.add(1) = g; | |
| *ptr.add(2) = r; | |
| } else { | |
| *ptr = r; | |
| *ptr.add(1) = g; | |
| *ptr.add(2) = b; | |
| } | |
| if bpp == 4 { | |
| *ptr.add(3) = 0xFF; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| pub fn blit_to_framebuffer(&self, fb: &mut FramebufferConfig) { | |
| let copy_width = self.width.min(fb.width); | |
| let copy_height = self.height.min(fb.height); | |
| let bpp = fb.bytes_per_pixel; | |
| let is_bgr = fb.pixel_format == PixelFormat::Bgr; | |
| // Ultra-fast 32-bit aligned full-screen blit | |
| if is_bgr && bpp == 4 { | |
| for y in 0..copy_height { | |
| let src_row = &self.buffer[y * self.width..y * self.width + copy_width]; | |
| let dst_ptr = unsafe { (fb.buffer as *mut u32).add(y * fb.stride) }; | |
| for (i, &val) in src_row.iter().enumerate() { | |
| unsafe { | |
| *dst_ptr.add(i) = val | 0xFF000000; | |
| } | |
| } | |
| } | |
| return; | |
| } | |
| for y in 0..copy_height { | |
| let src_row_start = y * self.width; | |
| let dst_row_start = y * fb.stride * bpp; | |
| for x in 0..copy_width { | |
| let val = self.buffer[src_row_start + x]; | |
| let r = ((val >> 16) & 0xFF) as u8; | |
| let g = ((val >> 8) & 0xFF) as u8; | |
| let b = (val & 0xFF) as u8; | |
| let dst_offset = dst_row_start + x * bpp; | |
| unsafe { | |
| let ptr = fb.buffer.add(dst_offset); | |
| if is_bgr { | |
| *ptr = b; | |
| *ptr.add(1) = g; | |
| *ptr.add(2) = r; | |
| } else { | |
| *ptr = r; | |
| *ptr.add(1) = g; | |
| *ptr.add(2) = b; | |
| } | |
| if bpp == 4 { | |
| *ptr.add(3) = 0xFF; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |