From 1be00d543c48ce0a1557325af22ec86c198eec27 Mon Sep 17 00:00:00 2001 From: cepetr Date: Tue, 12 Mar 2024 10:45:43 +0100 Subject: [PATCH] WIP - replace painter by jpeg and bar components --- core/embed/rust/src/ui/component/bar.rs | 62 ++++++++++++ core/embed/rust/src/ui/component/jpeg.rs | 96 +++++++++++++++++++ core/embed/rust/src/ui/component/mod.rs | 10 +- core/embed/rust/src/ui/component/painter.rs | 88 ----------------- .../model_tt/component/coinjoin_progress.rs | 5 +- core/embed/rust/src/ui/model_tt/layout.rs | 39 +++----- 6 files changed, 181 insertions(+), 119 deletions(-) create mode 100644 core/embed/rust/src/ui/component/bar.rs create mode 100644 core/embed/rust/src/ui/component/jpeg.rs delete mode 100644 core/embed/rust/src/ui/component/painter.rs diff --git a/core/embed/rust/src/ui/component/bar.rs b/core/embed/rust/src/ui/component/bar.rs new file mode 100644 index 000000000..604d382ca --- /dev/null +++ b/core/embed/rust/src/ui/component/bar.rs @@ -0,0 +1,62 @@ +use crate::ui::{ + component::{Component, Event, EventCtx, Never}, + display, + display::Color, + geometry::Rect, + shape, + shape::Renderer, +}; + +pub struct Bar { + area: Rect, + color: Color, + bg_color: Color, + radius: i16, +} + +impl Bar { + pub fn new(color: Color, bg_color: Color, radius: i16) -> Self { + Self { + area: Rect::zero(), + color, + bg_color, + radius, + } + } +} + +impl Component for Bar { + type Msg = Never; + + fn place(&mut self, bounds: Rect) -> Rect { + self.area = bounds; + self.area + } + + fn event(&mut self, _ctx: &mut EventCtx, _event: Event) -> Option { + None + } + + fn paint(&mut self) { + display::rect_fill_rounded(self.area, self.color, self.bg_color, self.radius as u8); + } + + fn render<'s>(&'s self, target: &mut impl Renderer<'s>) { + shape::Bar::new(self.area) + .with_bg(self.color) + .with_radius(self.radius) + .render(target); + } + + #[cfg(feature = "ui_bounds")] + fn bounds(&self, sink: &mut dyn FnMut(Rect)) { + sink(self.area) + } +} + +#[cfg(feature = "ui_debug")] +impl crate::trace::Trace for Bar { + fn trace(&self, t: &mut dyn crate::trace::Tracer) { + t.component("Bar"); + } +} diff --git a/core/embed/rust/src/ui/component/jpeg.rs b/core/embed/rust/src/ui/component/jpeg.rs new file mode 100644 index 000000000..89903aa8d --- /dev/null +++ b/core/embed/rust/src/ui/component/jpeg.rs @@ -0,0 +1,96 @@ +use crate::error::Error; +use crate::micropython::{buffer::get_buffer, obj::Obj}; + +use crate::ui::{ + component::{Component, Event, EventCtx, Never}, + display, + geometry::{Alignment2D, Offset, Rect}, + shape, + shape::Renderer, +}; + +pub enum ImageBuffer { + Object { obj: Obj }, + Slice { data: &'static [u8] }, +} + +impl ImageBuffer { + pub fn from_object(obj: Obj) -> Result { + if !obj.is_bytes() { + return Err(Error::TypeError); + } + Ok(ImageBuffer::Object { obj }) + } + + pub fn from_slice(data: &'static [u8]) -> Self { + ImageBuffer::Slice { data } + } + + pub fn is_empty(&self) -> bool { + self.data().is_empty() + } + + pub fn data(&self) -> &[u8] { + match self { + // SAFETY: We expect no existing mutable reference. Resulting reference is + // discarded before returning to micropython. + ImageBuffer::Object { obj } => unsafe { unwrap!(get_buffer(*obj)) }, + ImageBuffer::Slice { data } => data, + } + } +} + +pub struct Jpeg { + area: Rect, + data: ImageBuffer, + scale: u8, +} + +impl Jpeg { + pub fn new(data: ImageBuffer, scale: u8) -> Self { + Self { + area: Rect::zero(), + data, + scale, + } + } +} + +impl Component for Jpeg { + type Msg = Never; + + fn place(&mut self, bounds: Rect) -> Rect { + self.area = bounds; + self.area + } + + fn event(&mut self, _ctx: &mut EventCtx, _event: Event) -> Option { + None + } + + fn paint(&mut self) { + if let Some((size, _)) = display::tjpgd::jpeg_info(self.data.data()) { + let off = Offset::new(size.x / (2 << self.scale), size.y / (2 << self.scale)); + display::tjpgd::jpeg(self.data.data(), self.area.center() - off, self.scale); + } + } + + fn render<'s>(&'s self, target: &mut impl Renderer<'s>) { + shape::JpegImage::new(self.area.center(), self.data.data()) + .with_align(Alignment2D::CENTER) + .with_scale(self.scale) + .render(target); + } + + #[cfg(feature = "ui_bounds")] + fn bounds(&self, sink: &mut dyn FnMut(Rect)) { + sink(self.area) + } +} + +#[cfg(feature = "ui_debug")] +impl crate::trace::Trace for Jpeg { + fn trace(&self, t: &mut dyn crate::trace::Tracer) { + t.component("Jpeg"); + } +} diff --git a/core/embed/rust/src/ui/component/mod.rs b/core/embed/rust/src/ui/component/mod.rs index bc54734d9..0859d6858 100644 --- a/core/embed/rust/src/ui/component/mod.rs +++ b/core/embed/rust/src/ui/component/mod.rs @@ -1,32 +1,36 @@ -#![forbid(unsafe_code)] +//#![forbid(unsafe_code)] +pub mod bar; pub mod base; pub mod border; pub mod connect; pub mod empty; pub mod image; +#[cfg(all(feature = "jpeg", feature = "micropython"))] +pub mod jpeg; pub mod label; pub mod map; pub mod marquee; pub mod maybe; pub mod pad; pub mod paginated; -pub mod painter; pub mod placed; pub mod qr_code; pub mod text; pub mod timeout; +pub use bar::Bar; pub use base::{Child, Component, ComponentExt, Event, EventCtx, Never, Root, TimerToken}; pub use border::Border; pub use empty::Empty; +#[cfg(all(feature = "jpeg", feature = "micropython"))] +pub use jpeg::Jpeg; pub use label::Label; pub use map::MsgMap; pub use marquee::Marquee; pub use maybe::Maybe; pub use pad::Pad; pub use paginated::{PageMsg, Paginate}; -pub use painter::Painter; pub use placed::{FixedHeightBar, Floating, GridPlaced, Split}; pub use qr_code::Qr; pub use text::{ diff --git a/core/embed/rust/src/ui/component/painter.rs b/core/embed/rust/src/ui/component/painter.rs deleted file mode 100644 index d361ef177..000000000 --- a/core/embed/rust/src/ui/component/painter.rs +++ /dev/null @@ -1,88 +0,0 @@ -#[cfg(feature = "jpeg")] -use crate::ui::geometry::Offset; -use crate::ui::{ - component::{image::Image, Component, Event, EventCtx, Never}, - display, - geometry::{Alignment2D, Rect}, - shape, - shape::Renderer, -}; - -pub struct Painter { - area: Rect, - func: F, -} - -impl Painter { - pub fn new(func: F) -> Self { - Self { - func, - area: Rect::zero(), - } - } -} - -impl Component for Painter -where - F: FnMut(Rect), -{ - type Msg = Never; - - fn place(&mut self, bounds: Rect) -> Rect { - self.area = bounds; - self.area - } - - fn event(&mut self, _ctx: &mut EventCtx, _event: Event) -> Option { - None - } - - fn paint(&mut self) { - (self.func)(self.area); - } - - fn render<'s>(&'s self, target: &mut impl Renderer<'s>) { - let area = self.area; - shape::Bar::new(area) - .with_thickness(1) - .with_fg(display::Color::white()) - .render(target); - shape::Text::new(area.top_left(), "Paint").render(target); // !@# replace - } - - #[cfg(feature = "ui_bounds")] - fn bounds(&self, sink: &mut dyn FnMut(Rect)) { - sink(self.area) - } -} - -#[cfg(feature = "ui_debug")] -impl crate::trace::Trace for Painter { - fn trace(&self, t: &mut dyn crate::trace::Tracer) { - t.component("Painter"); - } -} - -pub fn image_painter(image: Image) -> Painter { - let f = move |area: Rect| image.draw(area.center(), Alignment2D::CENTER); - Painter::new(f) -} - -#[cfg(feature = "jpeg")] -pub fn jpeg_painter<'a>( - image: impl Fn() -> &'a [u8], - size: Offset, - scale: u8, -) -> Painter { - let off = Offset::new(size.x / (2 << scale), size.y / (2 << scale)); - #[cfg(not(feature = "new_rendering"))] - let f = move |area: Rect| display::tjpgd::jpeg(image(), area.center() - off, scale); - #[cfg(feature = "new_rendering")] - let f = move |area: Rect| {}; - Painter::new(f) -} - -pub fn rect_painter(fg: display::Color, bg: display::Color) -> Painter { - let f = move |area: Rect| display::rect_fill_rounded(area, fg, bg, 2); - Painter::new(f) -} diff --git a/core/embed/rust/src/ui/model_tt/component/coinjoin_progress.rs b/core/embed/rust/src/ui/model_tt/component/coinjoin_progress.rs index 1e451d42c..36d0424f8 100644 --- a/core/embed/rust/src/ui/model_tt/component/coinjoin_progress.rs +++ b/core/embed/rust/src/ui/model_tt/component/coinjoin_progress.rs @@ -8,8 +8,7 @@ use crate::{ ui::{ canvas::algo::PI4, component::{ - base::Never, painter, Child, Component, ComponentExt, Empty, Event, EventCtx, Label, - Split, + base::Never, Bar, Child, Component, ComponentExt, Empty, Event, EventCtx, Label, Split, }, constant, display::loader::{loader_circular_uncompress, LoaderDimensions}, @@ -55,7 +54,7 @@ where style, ) .vertically_centered(); - let bg = painter::rect_painter(style.background_color, theme::BG); + let bg = Bar::new(style.background_color, theme::BG, 2); let inner = (bg, label); CoinJoinProgress::with_background(text, inner, indeterminate) } diff --git a/core/embed/rust/src/ui/model_tt/layout.rs b/core/embed/rust/src/ui/model_tt/layout.rs index 846f9449e..4ad9eb877 100644 --- a/core/embed/rust/src/ui/model_tt/layout.rs +++ b/core/embed/rust/src/ui/model_tt/layout.rs @@ -21,8 +21,8 @@ use crate::{ base::ComponentExt, connect::Connect, image::BlendedImage, + jpeg::{ImageBuffer, Jpeg}, paginated::{PageMsg, Paginate}, - painter, placed::GridPlaced, text::{ op::OpTextLayout, @@ -211,10 +211,7 @@ where } } -impl ComponentMsgObj for painter::Painter -where - F: FnMut(geometry::Rect), -{ +impl ComponentMsgObj for Jpeg { fn msg_try_into_obj(&self, _msg: Self::Msg) -> Result { unreachable!() } @@ -648,26 +645,18 @@ extern "C" fn new_confirm_properties(n_args: usize, args: *const Obj, kwargs: *m extern "C" fn new_confirm_homescreen(n_args: usize, args: *const Obj, kwargs: *mut Map) -> Obj { let block = move |_args: &[Obj], kwargs: &Map| { let title: StrBuffer = kwargs.get(Qstr::MP_QSTR_title)?.try_into()?; - let data: Obj = kwargs.get(Qstr::MP_QSTR_image)?; - - // Layout needs to hold the Obj to play nice with GC. Obj is resolved to &[u8] - // in every paint pass. - let buffer_func = move || { - // SAFETY: We expect no existing mutable reference. Resulting reference is - // discarded before returning to micropython. - let buffer = unsafe { unwrap!(get_buffer(data)) }; - // Incoming data may be empty, meaning we should display default homescreen - // image. - if buffer.is_empty() { - theme::IMAGE_HOMESCREEN - } else { - buffer - } - }; + let image: Obj = kwargs.get(Qstr::MP_QSTR_image)?; + + let mut jpeg = unwrap!(ImageBuffer::from_object(image)); + + if jpeg.is_empty() { + // Incoming data may be empty, meaning we should + // display default homescreen image. + jpeg = ImageBuffer::from_slice(theme::IMAGE_HOMESCREEN); + } - let size = match jpeg_info(buffer_func()) { - Some(info) => info.0, - _ => return Err(value_error!("Invalid image.")), + if let None = jpeg_info(jpeg.data()) { + return Err(value_error!("Invalid image.")); }; let tr_change: StrBuffer = TR::buttons__change.try_into()?; @@ -675,7 +664,7 @@ extern "C" fn new_confirm_homescreen(n_args: usize, args: *const Obj, kwargs: *m let obj = LayoutObj::new(Frame::centered( theme::label_title(), title, - Dialog::new(painter::jpeg_painter(buffer_func, size, 1), buttons), + Dialog::new(Jpeg::new(jpeg, 1), buttons), ))?; Ok(obj.into()) };