fixup! feat(core): introduce new drawing library

matejcik/lifetimehell
cepetr 2 weeks ago
parent 2f46beee66
commit b95cf995b4

@ -173,22 +173,27 @@ impl<'a> Bitmap<'a> {
///
/// Returns None if row is out of range.
pub fn row<T>(&self, row: i16) -> Option<&[T]> {
if row >= 0 && row < self.size.y {
let offset = row as usize * (self.stride / core::mem::size_of::<T>());
if offset % core::mem::align_of::<T>() == 0 {
self.wait_for_dma();
// SAFETY:
// The resulting slice is inside the bitmap and properly aligned.
// Potential DMA operation is finished.
return Some(unsafe {
core::slice::from_raw_parts(
(self.ptr as *const T).add(offset),
self.stride / core::mem::size_of::<T>(),
)
});
}
if row < 0 || row >= self.size.y {
return None;
}
let offset = row as usize * (self.stride / core::mem::size_of::<T>());
if offset % core::mem::align_of::<T>() != 0 {
return None;
}
None
self.wait_for_dma();
// SAFETY:
// The resulting slice is inside the bitmap and properly aligned.
// Potential DMA operation is finished.
Some(unsafe {
core::slice::from_raw_parts(
(self.ptr as *const T).add(offset),
self.stride / core::mem::size_of::<T>(),
)
})
}
/// Returns the specified row as a mutable slice.
@ -196,23 +201,32 @@ impl<'a> Bitmap<'a> {
/// Returns None if row is out of range or
/// the bitmap is not set as mutable.
pub fn row_mut<T>(&mut self, row: i16) -> Option<&mut [T]> {
if self.mutable && row >= 0 && row < self.size.y {
let offset = row as usize * (self.stride / core::mem::size_of::<T>());
if offset % core::mem::align_of::<T>() == 0 {
self.wait_for_dma();
// SAFETY:
// The bitmap is mutable.
// The resulting slice is inside the bitmap and properly aligned.
// Potential DMA operation is finished.
return Some(unsafe {
core::slice::from_raw_parts_mut(
(self.ptr as *mut T).add(offset),
self.stride / core::mem::size_of::<T>(),
)
});
}
if !self.mutable {
return None;
}
if row < 0 || row >= self.size.y {
return None;
}
let offset = row as usize * (self.stride / core::mem::size_of::<T>());
if offset % core::mem::align_of::<T>() != 0 {
return None;
}
None
self.wait_for_dma();
// SAFETY:
// The bitmap is mutable.
// The resulting slice is inside the bitmap and properly aligned.
// Potential DMA operation is finished.
Some(unsafe {
core::slice::from_raw_parts_mut(
(self.ptr as *mut T).add(offset),
self.stride / core::mem::size_of::<T>(),
)
})
}
/// Returns specified consecutive rows as a mutable slice
@ -220,31 +234,36 @@ impl<'a> Bitmap<'a> {
/// Returns None if any of requested row is out of range or
/// the bitmap is not set as mutable.
pub fn rows_mut<T>(&mut self, row: i16, height: i16) -> Option<&mut [T]> {
if self.mutable
&& row >= 0
&& height > 0
&& row < self.size.y
&& row + height <= self.size.y
{
let offset = self.stride * row as usize;
if offset % core::mem::align_of::<T>() == 0 {
let len = self.stride * height as usize;
self.wait_for_dma();
// SAFETY:
// The bitmap is mutable.
// The resulting slice is inside the bitmap and properly aligned.
// Potential DMA operation is finished.
let array = unsafe {
core::slice::from_raw_parts_mut(
self.ptr as *mut T,
self.size.y as usize * self.stride / core::mem::size_of::<T>(),
)
};
return Some(&mut array[offset..offset + len]);
}
if !self.mutable {
return None;
}
None
if row < 0 || height <= 0 || row + height > self.size.y {
return None;
}
let offset = self.stride * row as usize;
if offset % core::mem::align_of::<T>() != 0 {
return None;
}
self.wait_for_dma();
// SAFETY:
// The bitmap is mutable.
// The resulting slice is inside the bitmap and properly aligned.
// Potential DMA operation is finished.
let array = unsafe {
core::slice::from_raw_parts_mut(
self.ptr as *mut T,
self.size.y as usize * self.stride / core::mem::size_of::<T>(),
)
};
let len = self.stride * height as usize;
Some(&mut array[offset..offset + len])
}
/// Return raw mut pointer to the specified bitmap row.

Loading…
Cancel
Save