You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
2.9 KiB
100 lines
2.9 KiB
3 months ago
|
use super::zlib_cache::ZlibCache;
|
||
|
|
||
|
#[cfg(feature = "ui_blurring")]
|
||
|
use super::blur_cache::BlurCache;
|
||
|
|
||
|
#[cfg(feature = "ui_jpeg_decoder")]
|
||
|
use super::jpeg_cache::JpegCache;
|
||
|
|
||
|
use core::cell::{RefCell, RefMut};
|
||
|
use without_alloc::alloc::LocalAllocLeakExt;
|
||
|
|
||
|
const ALIGN_PAD: usize = 8;
|
||
|
|
||
|
const ZLIB_CACHE_SLOTS: usize = 4;
|
||
|
const JPEG_CACHE_SLOTS: usize = 1;
|
||
|
const RENDER_BUFF_SIZE: usize = (240 * 2 * 16) + ALIGN_PAD;
|
||
|
const IMAGE_BUFF_SIZE: usize = 2048 + ALIGN_PAD;
|
||
|
|
||
|
pub type ImageBuff = [u8; IMAGE_BUFF_SIZE];
|
||
|
pub type RenderBuff = [u8; RENDER_BUFF_SIZE];
|
||
|
|
||
|
pub type ImageBuffRef<'a> = RefMut<'a, ImageBuff>;
|
||
|
pub type RenderBuffRef<'a> = RefMut<'a, RenderBuff>;
|
||
|
|
||
|
pub struct DrawingCache<'a> {
|
||
|
zlib_cache: RefCell<ZlibCache<'a>>,
|
||
|
|
||
|
#[cfg(feature = "ui_jpeg_decoder")]
|
||
|
jpeg_cache: RefCell<JpegCache<'a>>,
|
||
|
|
||
|
#[cfg(feature = "ui_blurring")]
|
||
|
blur_cache: RefCell<BlurCache<'a>>,
|
||
|
|
||
|
render_buff: &'a RefCell<RenderBuff>,
|
||
|
image_buff: &'a RefCell<ImageBuff>,
|
||
|
}
|
||
|
|
||
|
fn alloc_buf<'a, const S: usize, B>(bump: &'a B) -> Option<&'a RefCell<[u8; S]>>
|
||
|
where
|
||
|
B: LocalAllocLeakExt<'a>,
|
||
|
{
|
||
|
Some(
|
||
|
bump.alloc_t::<RefCell<[u8; S]>>()?
|
||
|
.uninit
|
||
|
.init(RefCell::new([0; S])),
|
||
|
)
|
||
|
}
|
||
|
|
||
|
impl<'a> DrawingCache<'a> {
|
||
|
pub fn new<TA, TB>(bump_a: &'a TA, bump_b: &'a TB) -> Self
|
||
|
where
|
||
|
TA: LocalAllocLeakExt<'a>,
|
||
|
TB: LocalAllocLeakExt<'a>,
|
||
|
{
|
||
|
Self {
|
||
|
zlib_cache: RefCell::new(unwrap!(
|
||
|
ZlibCache::new(bump_a, ZLIB_CACHE_SLOTS),
|
||
|
"ZLIB cache alloc"
|
||
|
)),
|
||
|
#[cfg(feature = "ui_jpeg_decoder")]
|
||
|
jpeg_cache: RefCell::new(unwrap!(
|
||
|
JpegCache::new(bump_a, JPEG_CACHE_SLOTS),
|
||
|
"JPEG cache alloc"
|
||
|
)),
|
||
|
#[cfg(feature = "ui_blurring")]
|
||
|
blur_cache: RefCell::new(unwrap!(BlurCache::new(bump_a), "Blur cache alloc")),
|
||
|
render_buff: unwrap!(alloc_buf(bump_b), "Render buff alloc"),
|
||
|
image_buff: unwrap!(alloc_buf(bump_b), "Toif buff alloc"),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// Returns an object for decompression of TOIF images
|
||
|
pub fn zlib(&self) -> RefMut<ZlibCache<'a>> {
|
||
|
self.zlib_cache.borrow_mut()
|
||
|
}
|
||
|
|
||
|
/// Returns an object for decompression of JPEG images
|
||
|
#[cfg(feature = "ui_jpeg_decoder")]
|
||
|
pub fn jpeg(&self) -> RefMut<JpegCache<'a>> {
|
||
|
self.jpeg_cache.borrow_mut()
|
||
|
}
|
||
|
|
||
|
/// Returns an object providing blurring algorithm
|
||
|
#[cfg(feature = "ui_blurring")]
|
||
|
pub fn blur(&self) -> RefMut<BlurCache<'a>> {
|
||
|
self.blur_cache.borrow_mut()
|
||
|
}
|
||
|
|
||
|
/// Returns a buffer used for ProgressiveRenderer slice
|
||
|
pub fn render_buff(&self) -> Option<RenderBuffRef<'a>> {
|
||
|
self.render_buff.try_borrow_mut().ok()
|
||
|
}
|
||
|
|
||
|
/// Returns a buffer for intended for drawing of
|
||
|
/// QrCode or ToifImage
|
||
|
pub fn image_buff(&self) -> Option<ImageBuffRef<'a>> {
|
||
|
self.image_buff.try_borrow_mut().ok()
|
||
|
}
|
||
|
}
|