Merge pull request #853 from betrusted-io/transition

Add bitmap transition
This commit is contained in:
bunnie 2026-04-24 03:18:30 +08:00 committed by GitHub
commit b0f5c5a4ce
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 209 additions and 2 deletions

3
Cargo.lock generated
View file

@ -397,6 +397,7 @@ dependencies = [
"bao1x-hal-service",
"base64 0.22.1",
"blitstr2",
"getrandom 0.2.12",
"libm 0.2.8",
"locales",
"log",
@ -404,6 +405,8 @@ dependencies = [
"nalgebra",
"num-derive 0.4.2",
"num-traits",
"rand 0.8.5",
"rand_core 0.6.4",
"rkyv 0.8.8",
"rqrr",
"utralib 0.1.27",

View file

@ -609,6 +609,170 @@ impl<'a> Oled128x128<'a> {
}
}
}
/// A diffusion-transition effect courtesy of Claude Sonnet 4.6. Query fed in `render_bitmap` code
/// and asked for a variant that does a "diffusive dither" transition. Applied largely without changes;
/// the main change was adjusting the seed to be passed, in instead of fixed, so the transition can be
/// different every call.
#[cfg(feature = "std")]
pub fn render_bitmap_diffuse(
&mut self,
bitmap: &ux_api::service::api::BaosecBitmap,
frames: usize,
seed: u64,
) -> Result<(), xous::Error> {
use std::collections::VecDeque;
const W: isize = WIDTH as isize;
const H: isize = LINES as isize;
// -- 1. Build per-pixel metadata for the destination region --------------
//
// We work in framebuffer linear-index space (0..WIDTH*LINES).
// Three bitmasks, each 512 u32 words == 16 384 bits == one bit per pixel.
let mut in_region = [0u32; 512]; // 1 = this fb pixel is touched by the bitmap
let mut target_bit = [0u32; 512]; // desired final value for pixels in region
let mut settled = [0u32; 512]; // 1 = pixel already written this transition
// Clamp the source bounding box (mirrors render_bitmap).
let src_x0 = bitmap.bounding_box.tl.x.max(0);
let src_y0 = bitmap.bounding_box.tl.y.max(0);
let src_x1 = bitmap.bounding_box.br.x.min(W);
let src_y1 = bitmap.bounding_box.br.y.min(H);
let mut total: usize = 0;
for src_y in src_y0..src_y1 {
let dst_y = src_y + bitmap.top_left.y;
if dst_y < 0 || dst_y >= H {
continue;
}
for src_x in src_x0..src_x1 {
let dst_x = src_x + bitmap.top_left.x;
if dst_x < 0 || dst_x >= W {
continue;
}
let src_lin = (src_x + src_y * W) as usize;
let dst_lin = (dst_x + dst_y * W) as usize;
in_region[dst_lin >> 5] |= 1 << (dst_lin & 31);
total += 1;
let bit = (bitmap.bits[src_lin >> 5] >> (src_lin & 31)) & 1;
if bit != 0 {
target_bit[dst_lin >> 5] |= 1 << (dst_lin & 31);
}
}
}
if total == 0 || frames == 0 {
return Ok(());
}
// -- 2. Collect region pixel indices for random seeding ------------------
let mut region_pixels: Vec<usize> = Vec::with_capacity(total);
for i in 0..(WIDTH as usize * LINES as usize) {
if (in_region[i >> 5] >> (i & 31)) & 1 != 0 {
region_pixels.push(i);
}
}
// -- 3. Tiny xorshift-64 RNG (no std::collections dependency needed) -----
let mut rng: u64 = seed ^ total as u64;
macro_rules! rand {
() => {{
rng ^= rng << 13;
rng ^= rng >> 7;
rng ^= rng << 17;
rng
}};
}
// -- 4. Diffusion loop ----------------------------------------------------
let pixels_per_frame = (total / frames).max(1);
// Scatter a handful of seeds so diffusion starts from multiple origins.
let mut frontier: VecDeque<usize> = VecDeque::new();
let n_seeds = (total / 32).clamp(1, 64);
for _ in 0..n_seeds {
let dl = region_pixels[(rand!() as usize) % total];
if (settled[dl >> 5] >> (dl & 31)) & 1 == 0 {
settled[dl >> 5] |= 1 << (dl & 31);
frontier.push_back(dl);
}
}
let mut committed: usize = 0;
while committed < total {
let mut this_frame: usize = 0;
while this_frame < pixels_per_frame {
// If the frontier runs dry, plant a new random seed.
if frontier.is_empty() {
let mut planted = false;
for _ in 0..64 {
let dl = region_pixels[(rand!() as usize) % total];
if (settled[dl >> 5] >> (dl & 31)) & 1 == 0 {
settled[dl >> 5] |= 1 << (dl & 31);
frontier.push_back(dl);
planted = true;
break;
}
}
if !planted {
break;
} // all pixels settled
}
// -- commit the next frontier pixel -------------------------------
let dl = frontier.pop_front().unwrap();
let on = (target_bit[dl >> 5] >> (dl & 31)) & 1;
if on != 0 {
self.buffer[dl >> 5] |= 1 << (dl & 31);
} else {
self.buffer[dl >> 5] &= !(1 << (dl & 31));
}
this_frame += 1;
// -- expand to unsettled cardinal neighbours in the region ---------
let dst_x = (dl % WIDTH as usize) as isize;
let dst_y = (dl / WIDTH as usize) as isize;
for (nx, ny) in
[(dst_x - 1, dst_y), (dst_x + 1, dst_y), (dst_x, dst_y - 1), (dst_x, dst_y + 1)]
{
if nx < 0 || nx >= W || ny < 0 || ny >= H {
continue;
}
let ndl = (nx + ny * W) as usize;
if (in_region[ndl >> 5] >> (ndl & 31)) & 1 == 0 {
continue;
}
if (settled[ndl >> 5] >> (ndl & 31)) & 1 != 0 {
continue;
}
settled[ndl >> 5] |= 1 << (ndl & 31);
frontier.push_back(ndl);
}
}
committed += this_frame;
if this_frame > 0 {
self.redraw()?;
}
if this_frame == 0 {
break;
} // guard against impossible stuck state
}
Ok(())
}
}
impl<'a> FrameBuffer for Oled128x128<'a> {

View file

@ -111,6 +111,9 @@ pub enum GfxOpcode {
/// This call is specific and highly optimized to the display on Baosec
BaosecBitmap,
#[cfg(feature = "board-baosec")]
/// This call is specific and highly optimized to the display on Baosec
BaosecBitmapDiffuse,
#[cfg(feature = "board-baosec")]
Brightness,
#[cfg(feature = "board-baosec")]
FlipScreen,

View file

@ -792,6 +792,29 @@ impl Gfx {
Ok(())
}
#[cfg(feature = "board-baosec")]
pub fn bitmap_diffusion(
&self,
bitmap: &[u32],
top_left: Option<Point>,
bounding_box: Option<Rectangle>,
) -> Result<(), xous::Error> {
let mut bmp = BaosecBitmap {
bits: [0u32; 512],
top_left: top_left.unwrap_or(Point::new(0, 0)),
bounding_box: bounding_box.unwrap_or(Rectangle {
tl: Point::new(0, 0),
// br is *exclusive*
br: Point::new(128, 128),
style: DrawStyle { fill_color: None, stroke_color: None, stroke_width: 0 },
}),
};
bmp.bits[..bitmap.len().min(512)].copy_from_slice(&bitmap[..bitmap.len().min(512)]);
let mut buf = Buffer::into_buf(bmp).unwrap();
buf.lend_mut(self.conn, GfxOpcode::BaosecBitmapDiffuse.to_u32().unwrap())?;
Ok(())
}
#[cfg(feature = "board-baosec")]
pub fn brightness(&self, level: u8) -> Result<(), xous::Error> {
send_message(

View file

@ -43,12 +43,12 @@ pub const APP_UART_IFRAM_ADDR: usize = bao1x_hal::board::APP_UART_IFRAM_ADDR;
pub const GUARD_MEMORY_BYTES: usize = 3 * crate::PAGE_SIZE;
/// Amount of space for loader stack only, with swap
#[cfg(all(feature = "swap", not(feature = "resume"), not(feature = "bao1x")))]
pub const GUARD_MEMORY_BYTES: usize = 8 * crate::PAGE_SIZE;
pub const GUARD_MEMORY_BYTES: usize = 9 * crate::PAGE_SIZE;
#[cfg(all(feature = "swap", not(feature = "resume"), feature = "bao1x"))]
pub const GUARD_MEMORY_BYTES: usize = 9 * crate::PAGE_SIZE;
/// Amount of space for loader stack plus clean suspend, with swap. +1 page cost for the loading bar graphics
#[cfg(all(feature = "swap", feature = "resume"))]
pub const GUARD_MEMORY_BYTES: usize = 9 * crate::PAGE_SIZE; // 1 extra page for clean suspend
pub const GUARD_MEMORY_BYTES: usize = 10 * crate::PAGE_SIZE; // 1 extra page for clean suspend
#[cfg(feature = "swap")]
pub const SWAPPER_PID: u8 = 2;

View file

@ -35,6 +35,11 @@ base64 = { version = "0.22.1", optional = true }
locales = { path = "../../locales" }
# diffusion dithering
rand = { version = "0.8.5", features = ["getrandom"] }
rand_core = "0.6.4"
getrandom = "=0.2.12"
[features]
no-gam = ["modals/no-gam"]
gfx-testing = []

View file

@ -932,6 +932,15 @@ pub fn wrapped_main(main_thread_token: MainThreadToken) -> ! {
display.render_bitmap(bitmap);
}
#[cfg(feature = "board-baosec")]
GfxOpcode::BaosecBitmapDiffuse => {
use rand::Rng;
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
let bitmap = buffer.to_original::<BaosecBitmap, _>().unwrap();
display
.render_bitmap_diffuse(&bitmap, 10, rand::thread_rng().gen::<u64>())
.unwrap_or_else(|_| display_timeout_handler(&udma_global, &mut display));
}
#[cfg(feature = "board-baosec")]
GfxOpcode::Brightness => {
if let Some(scalar) = msg.body.scalar_message_mut() {
let brightness = scalar.arg1.min(255) as u8;