1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-17 21:22:10 +00:00

chore(core): optimize stack usage during blurring

[no changelog]
This commit is contained in:
cepetr 2023-12-11 15:22:51 +01:00 committed by cepetr
parent c1fa1c0cbc
commit 6e1fe31e3f
5 changed files with 38 additions and 16 deletions

View File

@ -80,3 +80,4 @@ BUFFER(BUFFER_SECTION, text, 1);
BUFFER(NODMA_BUFFER_SECTION, jpeg, 1);
BUFFER(NODMA_BUFFER_SECTION, jpeg_work, 1);
BUFFER(NODMA_BUFFER_SECTION, blurring, 1);
BUFFER(NODMA_BUFFER_SECTION, blurring_totals, 1);

View File

@ -81,6 +81,10 @@ typedef __attribute__((aligned(4))) struct {
uint16_t buffer[10][3][BUFFER_PIXELS];
} buffer_blurring_t;
typedef __attribute__((aligned(4))) struct {
uint16_t buffer[3][BUFFER_PIXELS];
} buffer_blurring_totals_t;
extern const int32_t text_buffer_height;
extern const int32_t buffer_width;
@ -102,4 +106,7 @@ void buffers_free_jpeg_work(buffer_jpeg_work_t* buffer);
buffer_blurring_t* buffers_get_blurring(bool clear);
void buffers_free_blurring(buffer_blurring_t* buffer);
buffer_blurring_totals_t* buffers_get_blurring_totals(bool clear);
void buffers_free_blurring_totals(buffer_blurring_totals_t* buffer);
#endif // _BUFFERS_H

View File

@ -359,6 +359,8 @@ fn generate_trezorhal_bindings() {
.allowlist_function("buffers_free_jpeg_work")
.allowlist_function("buffers_get_blurring")
.allowlist_function("buffers_free_blurring")
.allowlist_function("buffers_get_blurring_totals")
.allowlist_function("buffers_free_blurring_totals")
.allowlist_var("TEXT_BUFFER_HEIGHT")
.no_copy("buffer_line_16bpp_t")
.no_copy("buffer_line_4bpp_t")
@ -366,6 +368,7 @@ fn generate_trezorhal_bindings() {
.no_copy("buffer_jpeg_t")
.no_copy("buffer_jpeg_work_t")
.no_copy("buffer_blurring_t")
.no_copy("buffer_blurring_totals_t")
//usb
.allowlist_function("usb_configured")
// touch

View File

@ -79,6 +79,12 @@ buffer_wrapper!(
buffers_get_blurring,
buffers_free_blurring
);
buffer_wrapper!(
BufferBlurringTotals,
buffer_blurring_totals_t,
buffers_get_blurring_totals,
buffers_free_blurring_totals
);
#[cfg(feature = "jpeg")]
buffer_wrapper!(
BufferJpeg,

View File

@ -1,6 +1,9 @@
use crate::{
trezorhal::{
buffers::{BufferBlurring, BufferJpeg, BufferLine16bpp, BufferLine4bpp, BufferText},
buffers::{
BufferBlurring, BufferBlurringTotals, BufferJpeg, BufferLine16bpp, BufferLine4bpp,
BufferText,
},
display,
dma2d::{dma2d_setup_4bpp_over_16bpp, dma2d_start_blend, dma2d_wait_for_transfer},
uzlib::UzlibContext,
@ -292,9 +295,9 @@ fn homescreen_line_blurred(
let coef = (65536_f32 * LOCKSCREEN_DIM) as u32;
let r = (blurring.totals[RED_IDX][x] as u32 * BLUR_DIV) >> 16;
let g = (blurring.totals[GREEN_IDX][x] as u32 * BLUR_DIV) >> 16;
let b = (blurring.totals[BLUE_IDX][x] as u32 * BLUR_DIV) >> 16;
let r = (blurring.totals.buffer[RED_IDX][x] as u32 * BLUR_DIV) >> 16;
let g = (blurring.totals.buffer[GREEN_IDX][x] as u32 * BLUR_DIV) >> 16;
let b = (blurring.totals.buffer[BLUE_IDX][x] as u32 * BLUR_DIV) >> 16;
let r = (((coef * r) >> 8) & 0xF800) as u16;
let g = (((coef * g) >> 13) & 0x07E0) as u16;
@ -304,9 +307,11 @@ fn homescreen_line_blurred(
} else {
let x = x as usize;
let r = (((blurring.totals[RED_IDX][x] as u32 * BLUR_DIV) >> 8) & 0xF800) as u16;
let g = (((blurring.totals[GREEN_IDX][x] as u32 * BLUR_DIV) >> 13) & 0x07E0) as u16;
let b = (((blurring.totals[BLUE_IDX][x] as u32 * BLUR_DIV) >> 19) & 0x001F) as u16;
let r = (((blurring.totals.buffer[RED_IDX][x] as u32 * BLUR_DIV) >> 8) & 0xF800) as u16;
let g =
(((blurring.totals.buffer[GREEN_IDX][x] as u32 * BLUR_DIV) >> 13) & 0x07E0) as u16;
let b =
(((blurring.totals.buffer[BLUE_IDX][x] as u32 * BLUR_DIV) >> 19) & 0x001F) as u16;
r | g | b
};
@ -421,7 +426,7 @@ fn update_accs_sub(data: &[u16], idx: usize, acc_r: &mut u16, acc_g: &mut u16, a
struct BlurringContext {
mem: BufferBlurring,
pub totals: [[u16; HOMESCREEN_IMAGE_WIDTH as usize]; COLORS],
pub totals: BufferBlurringTotals,
line_num: i16,
add_idx: usize,
rem_idx: usize,
@ -431,7 +436,7 @@ impl BlurringContext {
pub fn new() -> Self {
Self {
mem: BufferBlurring::get_cleared(),
totals: [[0; HOMESCREEN_IMAGE_WIDTH as usize]; COLORS],
totals: BufferBlurringTotals::get_cleared(),
line_num: 0,
add_idx: 0,
rem_idx: 0,
@ -440,7 +445,7 @@ impl BlurringContext {
fn clear(&mut self) {
let lines = &mut self.mem.buffer[0..DECOMP_LINES];
for (i, total) in self.totals.iter_mut().enumerate() {
for (i, total) in self.totals.buffer.iter_mut().enumerate() {
for line in lines.iter_mut() {
line[i].fill(0);
}
@ -480,9 +485,9 @@ impl BlurringContext {
fn vertical_avg_add(&mut self) {
let lines = &mut self.mem.buffer[0..DECOMP_LINES];
for i in 0..HOMESCREEN_IMAGE_WIDTH as usize {
self.totals[RED_IDX][i] += lines[self.add_idx][RED_IDX][i];
self.totals[GREEN_IDX][i] += lines[self.add_idx][GREEN_IDX][i];
self.totals[BLUE_IDX][i] += lines[self.add_idx][BLUE_IDX][i];
self.totals.buffer[RED_IDX][i] += lines[self.add_idx][RED_IDX][i];
self.totals.buffer[GREEN_IDX][i] += lines[self.add_idx][GREEN_IDX][i];
self.totals.buffer[BLUE_IDX][i] += lines[self.add_idx][BLUE_IDX][i];
}
}
@ -490,11 +495,11 @@ impl BlurringContext {
fn vertical_avg(&mut self) {
let lines = &mut self.mem.buffer[0..DECOMP_LINES];
for i in 0..HOMESCREEN_IMAGE_WIDTH as usize {
self.totals[RED_IDX][i] +=
self.totals.buffer[RED_IDX][i] +=
lines[self.add_idx][RED_IDX][i] - lines[self.rem_idx][RED_IDX][i];
self.totals[GREEN_IDX][i] +=
self.totals.buffer[GREEN_IDX][i] +=
lines[self.add_idx][GREEN_IDX][i] - lines[self.rem_idx][GREEN_IDX][i];
self.totals[BLUE_IDX][i] +=
self.totals.buffer[BLUE_IDX][i] +=
lines[self.add_idx][BLUE_IDX][i] - lines[self.rem_idx][BLUE_IDX][i];
}
}