1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-08-02 11:58:32 +00:00

fixup! feat(core): introduce new drawing library

This commit is contained in:
cepetr 2024-05-02 13:20:43 +02:00
parent 473a4035c7
commit c66be66eba

View File

@ -166,46 +166,66 @@ impl<'a> Bitmap<'a> {
/// Returns None if row is out of range. /// Returns None if row is out of range.
pub fn row<T>(&self, row: i16) -> Option<&[T]> { pub fn row<T>(&self, row: i16) -> Option<&[T]> {
if row >= 0 && row < self.size.y { if row >= 0 && row < self.size.y {
self.wait_for_dma();
let offset = row as usize * (self.stride / core::mem::size_of::<T>()); let offset = row as usize * (self.stride / core::mem::size_of::<T>());
Some(unsafe { 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( core::slice::from_raw_parts(
(self.ptr as *const T).add(offset), (self.ptr as *const T).add(offset),
self.stride / core::mem::size_of::<T>(), self.stride / core::mem::size_of::<T>(),
) )
}) });
} else {
None
} }
} }
None
}
/// Returns the specified row as a mutable slice. /// Returns the specified row as a mutable slice.
/// ///
/// Returns None if row is out of range. /// 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]> { pub fn row_mut<T>(&mut self, row: i16) -> Option<&mut [T]> {
if row >= 0 && row < self.size.y { if self.mutable && row >= 0 && row < self.size.y {
self.wait_for_dma();
let offset = row as usize * (self.stride / core::mem::size_of::<T>()); let offset = row as usize * (self.stride / core::mem::size_of::<T>());
Some(unsafe { 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( core::slice::from_raw_parts_mut(
(self.ptr as *mut T).add(offset), (self.ptr as *mut T).add(offset),
self.stride / core::mem::size_of::<T>(), self.stride / core::mem::size_of::<T>(),
) )
}) });
} else {
None
} }
} }
None
}
/// Returns specified consecutive rows as a mutable slice /// Returns specified consecutive rows as a mutable slice
/// ///
/// Returns None if any of requested row is out of range. /// 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]> { pub fn rows_mut<T>(&mut self, row: i16, height: i16) -> Option<&mut [T]> {
if row >= 0 && height > 0 && row < self.size.y && row + height <= self.size.y { if self.mutable
self.wait_for_dma(); && row >= 0
&& height > 0
&& row < self.size.y
&& row + height <= self.size.y
{
let offset = self.stride * row as usize; let offset = self.stride * row as usize;
if offset % core::mem::align_of::<T>() == 0 {
let len = self.stride * height as usize; let len = self.stride * height as usize;
// SAFETY:
// The bitmap is mutable.
// The resulting slice is inside the bitmap and properly aligned.
// Potential DMA operation is finished.
let array = unsafe { let array = unsafe {
core::slice::from_raw_parts_mut( core::slice::from_raw_parts_mut(
self.ptr as *mut T, self.ptr as *mut T,
@ -213,11 +233,13 @@ impl<'a> Bitmap<'a> {
) )
}; };
Some(&mut array[offset..offset + len]) self.wait_for_dma();
} else {
None return Some(&mut array[offset..offset + len]);
} }
} }
None
}
/// Return raw mut pointer to the specified bitmap row. /// Return raw mut pointer to the specified bitmap row.
/// ///