Graphics namespace includes rectangle, rounded rectangle, and circle

Also added methods to make writing to shm pool easier
This commit is contained in:
2025-04-27 01:12:29 -07:00
parent 1e5be37996
commit 293e03e500
5 changed files with 80 additions and 13 deletions

2
src/graphics/mod.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod rectangle;

43
src/graphics/rectangle.rs Normal file
View File

@@ -0,0 +1,43 @@
use crate::wayland::shm::ShmPool;
// l_row means local row and g_row means global row
impl ShmPool {
// x and y are topleft corner of rect
pub fn rectangle(&mut self, x: usize, y: usize, w: usize, h: usize, color: u32) {
for g_row in y..y+h {
self.write(&vec![color; w], g_row*self.width+x);
}
}
// x and y and topleft corner of rect
pub fn rounded_rectangle(
&mut self,
x: usize,
y: usize,
w: usize,
h: usize,
radius: usize,
color: u32
) {
for l_row in 0..radius+1 {
let x_diff = radius - ((radius*radius - l_row*l_row) as f64).sqrt() as usize;
self.write(&vec![color; w - (2*x_diff)], (y+radius-l_row)*self.width + x + x_diff);
self.write(&vec![color; w - (2*x_diff)], (y+h-radius+l_row)*self.width + x + x_diff);
}
for g_row in y+radius..y+h-radius {
self.write(&vec![color; w], g_row*self.width+x);
}
}
// x and y are center of circle
pub fn circle(&mut self, x: usize, y: usize, radius: usize, color: u32) {
for l_row in 0..radius {
let x_diff = ((radius*radius - l_row*l_row) as f64).sqrt();
let mut row: Vec<u32> = vec![color; 2*x_diff as usize];
self.write(&row, (y+l_row)*self.width + x - x_diff as usize);
self.write(&row, (y-l_row)*self.width + x - x_diff as usize);
}
}
}

View File

@@ -3,6 +3,7 @@ use std::error::Error;
mod wayland; mod wayland;
use wayland::wl_client::WlClient; use wayland::wl_client::WlClient;
mod graphics;
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
let mut wl_client = WlClient::new()?; let mut wl_client = WlClient::new()?;

View File

@@ -6,10 +6,13 @@ pub struct ShmPool {
pub id: u32, pub id: u32,
pub addr: *mut c_void, pub addr: *mut c_void,
pub size: usize, pub size: usize,
pub width: usize,
} }
impl ShmPool { impl ShmPool {
pub fn new(size: usize, id: u32) -> std::io::Result<ShmPool> { pub fn new(width: usize, height: usize, id: u32) -> std::io::Result<ShmPool> {
let size = width * height * 4;
let shm_path: *const i8 = b"/chlorostart\0".as_ptr() as *const i8; let shm_path: *const i8 = b"/chlorostart\0".as_ptr() as *const i8;
let fd = unsafe { shm_open(shm_path, O_RDWR | O_EXCL | O_CREAT, 0o600) }; let fd = unsafe { shm_open(shm_path, O_RDWR | O_EXCL | O_CREAT, 0o600) };
if fd == -1 { if fd == -1 {
@@ -37,7 +40,8 @@ impl ShmPool {
fd, fd,
id, id,
addr, addr,
size size,
width,
}) })
} }
@@ -50,17 +54,31 @@ impl ShmPool {
Ok(()) Ok(())
} }
pub fn write(&mut self, data: &Vec<u32>, offset: isize) -> std::io::Result<()> { pub fn write(&mut self, data: &Vec<u32>, offset: usize) {
// TODO: Bounds check if offset > self.size {
return;
}
unsafe { unsafe {
std::ptr::copy_nonoverlapping( std::ptr::copy_nonoverlapping(
data.as_ptr() as *const u32, // src: data as *const u32 data.as_ptr() as *const u32, // src: data as *const u32
self.addr.offset(offset*4) as *mut u32, // dst: ShmPool address as *mut u32 self.addr.offset(4*offset as isize) as *mut u32, // dst: ShmPool address as *mut u32
if offset + data.len() * 4 <= self.size {
data.len() data.len()
} else {
data.len() - offset
}
); );
} }
}
Ok(()) pub fn write_pixel(&mut self, data: u32, offset: isize) {
// TODO: Bounds check
unsafe {*(self.addr.offset(offset*4) as *mut u32) = data;}
}
pub fn read_pixel(&self, offset: usize) -> u32 {
// TODO: Bounds check
return unsafe {*(self.addr.offset(4*offset as isize) as *const u32)};
} }
} }

View File

@@ -11,8 +11,11 @@ impl WlClient {
pub fn wl_shm_create_pool(&mut self, width: usize, height: usize) -> Result<(), Box<dyn Error>> { pub fn wl_shm_create_pool(&mut self, width: usize, height: usize) -> Result<(), Box<dyn Error>> {
self.current_id += 1; self.current_id += 1;
self.shm_pool = Some(shm::ShmPool::new(width * height * STRIDE, self.current_id)?); self.shm_pool = Some(shm::ShmPool::new(width, height, self.current_id)?);
self.shm_pool.as_mut().unwrap().write(&vec![0xffff0000; width * height], 0)?; self.shm_pool.as_mut().unwrap().write(&vec![0xffff0000; width * height], 0);
self.shm_pool.as_mut().unwrap().rectangle(50, 50, 50, 50, 0xff00ff00);
self.shm_pool.as_mut().unwrap().circle(200, 200, 50, 0xff0000ff);
self.shm_pool.as_mut().unwrap().rounded_rectangle(400, 200, 200, 100, 14, 0xffffff00);
let object = self.shm_id.ok_or(UnsetErr("shm_id".to_string()))?; let object = self.shm_id.ok_or(UnsetErr("shm_id".to_string()))?;
const OPCODE: u16 = 0; const OPCODE: u16 = 0;