From 4b013092168e8f58971ea3a71278b2d5ea76b788 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 | 98 +++++++++++++++++++ core/embed/rust/src/ui/component/mod.rs | 10 +- core/embed/rust/src/ui/component/painter.rs | 88 ----------------- .../src/ui/model_tr/component/homescreen.rs | 1 + .../model_tt/component/coinjoin_progress.rs | 5 +- core/embed/rust/src/ui/model_tt/layout.rs | 37 +++---- 7 files changed, 183 insertions(+), 118 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..fd504b9aa --- /dev/null +++ b/core/embed/rust/src/ui/component/jpeg.rs @@ -0,0 +1,98 @@ +use crate::{ + error::Error, + 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_tr/component/homescreen.rs b/core/embed/rust/src/ui/model_tr/component/homescreen.rs index 5205961e2..415eb3381 100644 --- a/core/embed/rust/src/ui/model_tr/component/homescreen.rs +++ b/core/embed/rust/src/ui/model_tr/component/homescreen.rs @@ -1,4 +1,5 @@ use crate::{ + micropython::{buffer::get_buffer, gc::Gc, obj::Obj}, strutil::TString, translations::TR, trezorhal::usb::usb_configured, 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 bfc77d67b..d53d075b1 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 @@ -7,8 +7,7 @@ use crate::{ translations::TR, ui::{ 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}, @@ -45,7 +44,7 @@ impl CoinJoinProgress { let style = theme::label_coinjoin_progress(); let label = Label::centered(TR::coinjoin__title_do_not_disconnect.into(), 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 2bcb17728..f384c85c7 100644 --- a/core/embed/rust/src/ui/model_tt/layout.rs +++ b/core/embed/rust/src/ui/model_tt/layout.rs @@ -14,8 +14,8 @@ use crate::{ base::ComponentExt, connect::Connect, image::BlendedImage, + jpeg::{ImageBuffer, Jpeg}, paginated::{PageMsg, Paginate}, - painter, placed::GridPlaced, text::{ op::OpTextLayout, @@ -196,10 +196,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!() } @@ -617,33 +614,25 @@ 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: TString = kwargs.get(Qstr::MP_QSTR_title)?.try_into()?; - let data: Obj = kwargs.get(Qstr::MP_QSTR_image)?; + let image: 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 mut jpeg = unwrap!(ImageBuffer::from_object(image)); - let size = match jpeg_info(buffer_func()) { - Some(info) => info.0, - _ => return Err(value_error!("Invalid image.")), + if jpeg.is_empty() { + // Incoming data may be empty, meaning we should + // display default homescreen image. + jpeg = ImageBuffer::from_slice(theme::IMAGE_HOMESCREEN); + } + + if jpeg_info(jpeg.data()).is_none() { + return Err(value_error!("Invalid image.")); }; let buttons = Button::cancel_confirm_text(None, Some(TR::buttons__change.into())); 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()) };