More stuff

This commit is contained in:
Kenny Levinsen
2023-02-02 23:52:59 +01:00
parent 7593cd48a4
commit b49b9e9764
26 changed files with 2022 additions and 803 deletions
+40 -80
View File
@@ -170,20 +170,27 @@ impl BufferManager {
}
pub struct BufferView<'a> {
buf: &'a mut MmapMut,
view: &'a mut [u32],
dimensions: (u32, u32),
subdimensions: Option<(u32, u32, u32, u32)>,
}
impl<'a> BufferView<'a> {
pub fn new(buf: &'a mut MmapMut, dimensions: (u32, u32)) -> BufferView {
let view = unsafe {
std::slice::from_raw_parts_mut(
buf.as_mut_ptr() as *mut u32,
(dimensions.0 * dimensions.1) as usize,
)
};
BufferView {
buf,
view,
dimensions,
subdimensions: None,
}
}
#[inline]
pub fn get_bounds(&self) -> (u32, u32, u32, u32) {
if let Some(subdim) = self.subdimensions {
subdim
@@ -195,22 +202,16 @@ impl<'a> BufferView<'a> {
pub fn subdimensions(
&mut self,
subdimensions: (u32, u32, u32, u32),
) -> Result<BufferView, ::std::io::Error> {
) -> BufferView {
let bounds = self.get_bounds();
if subdimensions.0 + subdimensions.2 > bounds.2
|| subdimensions.1 + subdimensions.3 > bounds.3
if cfg!(debug_assertions) && (subdimensions.0 + subdimensions.2 > bounds.2
|| subdimensions.1 + subdimensions.3 > bounds.3)
{
return Err(::std::io::Error::new(
::std::io::ErrorKind::Other,
format!(
"cannot create subdimensions larger than buffer: {:?} > {:?}",
subdimensions, bounds
),
));
panic!("cannot create subdimensions larger than buffer: {:?} > {:?}", subdimensions, bounds);
}
Ok(BufferView {
buf: self.buf,
BufferView {
view: self.view,
dimensions: self.dimensions,
subdimensions: Some((
subdimensions.0 + bounds.0,
@@ -218,27 +219,21 @@ impl<'a> BufferView<'a> {
subdimensions.2,
subdimensions.3,
)),
})
}
}
pub fn subgeometry(&mut self, geo: Geometry) -> Result<BufferView, ::std::io::Error> {
pub fn subgeometry(&mut self, geo: Geometry) -> BufferView {
self.subdimensions((geo.x, geo.y, geo.width, geo.height))
}
pub fn offset(&mut self, offset: (u32, u32)) -> Result<BufferView, ::std::io::Error> {
pub fn offset(&mut self, offset: (u32, u32)) -> BufferView {
let bounds = self.get_bounds();
if offset.0 > bounds.2 || offset.1 > bounds.3 {
return Err(::std::io::Error::new(
::std::io::ErrorKind::Other,
format!(
"cannot create offset outside buffer: {:?} > {:?}",
offset, bounds
),
));
if cfg!(debug_assertions) && (offset.0 > bounds.2 || offset.1 > bounds.3) {
panic!("cannot create offset outside buffer: {:?} > {:?}", offset, bounds);
}
Ok(BufferView {
buf: self.buf,
BufferView {
view: self.view,
dimensions: self.dimensions,
subdimensions: Some((
offset.0 + bounds.0,
@@ -246,82 +241,47 @@ impl<'a> BufferView<'a> {
bounds.2 - offset.0,
bounds.3 - offset.1,
)),
})
}
}
pub fn limit(&mut self, dimensions: (u32, u32)) -> Result<BufferView, ::std::io::Error> {
pub fn limit(&mut self, dimensions: (u32, u32)) -> BufferView {
let bounds = self.get_bounds();
if dimensions.0 > bounds.2 || dimensions.1 > bounds.3 {
return Err(::std::io::Error::new(
::std::io::ErrorKind::Other,
format!(
"cannot create offset outside buffer: {:?} > {:?}",
dimensions, bounds
),
));
if cfg!(debug_assertions) && (dimensions.0 > bounds.2 || dimensions.1 > bounds.3) {
panic!("cannot create offset outside buffer: {:?} > {:?}", dimensions, bounds);
}
Ok(BufferView {
buf: self.buf,
BufferView {
view: self.view,
dimensions: self.dimensions,
subdimensions: Some((bounds.0, bounds.1, dimensions.0, dimensions.1)),
})
}
}
pub fn memset(&mut self, c: &Color) {
let v = c.as_argb8888();
pub fn memset(&mut self, c: Color) {
if let Some(subdim) = self.subdimensions {
unsafe {
let ptr = self.buf.as_mut_ptr();
for y in subdim.1..(subdim.1 + subdim.3) {
for x in subdim.0..(subdim.0 + subdim.2) {
*((ptr as *mut u32).offset((x + y * self.dimensions.0) as isize)) = v;
}
for y in subdim.1..(subdim.1 + subdim.3) {
for x in subdim.0..(subdim.0 + subdim.2) {
self.view[(x + y * self.dimensions.0) as usize] = c.0;
}
}
} else {
unsafe {
let ptr = self.buf.as_mut_ptr();
for p in 0..(self.dimensions.0 * self.dimensions.1) {
*((ptr as *mut u32).offset(p as isize)) = v;
}
}
self.view.fill(c.0);
}
}
pub fn put(&mut self, pos: (u32, u32), c: &Color) -> Result<(), ::std::io::Error> {
pub fn put(&mut self, pos: (u32, u32), c: Color) {
let true_pos = if let Some(subdim) = self.subdimensions {
if pos.0 >= subdim.2 || pos.1 >= subdim.3 {
return Err(::std::io::Error::new(
::std::io::ErrorKind::Other,
format!(
"put({:?}) is not within subdimensions of buffer ({:?})",
pos, subdim
),
));
if cfg!(debug_assertions) && (pos.0 >= subdim.2 || pos.1 >= subdim.3) {
panic!("put({:?}) is not within subdimensions of buffer ({:?})", pos, subdim);
}
(pos.0 + subdim.0, pos.1 + subdim.1)
} else {
if pos.0 >= self.dimensions.0 || pos.1 >= self.dimensions.1 {
return Err(::std::io::Error::new(
::std::io::ErrorKind::Other,
format!(
"put({:?}) is not within dimensions of buffer ({:?})",
pos, self.dimensions
),
));
if cfg!(debug_assertions) && (pos.0 >= self.dimensions.0 || pos.1 >= self.dimensions.1) {
panic!("put({:?}) is not within dimensions of buffer ({:?})", pos, self.dimensions);
}
pos
};
unsafe {
let ptr = self
.buf
.as_mut_ptr()
.offset(4 * (true_pos.0 + (true_pos.1 * self.dimensions.0)) as isize);
*(ptr as *mut u32) = c.as_argb8888();
};
Ok(())
self.view[(true_pos.0 + (true_pos.1 * self.dimensions.0)) as usize] = c.0;
}
}