From 4d1dc33ef10a1484eef950b0397146eea7bfc575 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 23 Apr 2026 15:14:27 +0800 Subject: [PATCH] add a diffusive transition option to the graphics library makes changing between bitmaps less jarring --- Cargo.lock | 3 + libs/bao1x-hal/src/sh1107.rs | 164 +++++++++++++++++++++++++++++++++ libs/ux-api/src/service/api.rs | 3 + libs/ux-api/src/service/gfx.rs | 23 +++++ services/bao-video/Cargo.toml | 5 + services/bao-video/src/main.rs | 9 ++ 6 files changed, 207 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index a06f9bbd6..056c68448 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/libs/bao1x-hal/src/sh1107.rs b/libs/bao1x-hal/src/sh1107.rs index db841d674..48e579c21 100644 --- a/libs/bao1x-hal/src/sh1107.rs +++ b/libs/bao1x-hal/src/sh1107.rs @@ -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 = 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 = 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> { diff --git a/libs/ux-api/src/service/api.rs b/libs/ux-api/src/service/api.rs index 236439f5f..344dfc561 100644 --- a/libs/ux-api/src/service/api.rs +++ b/libs/ux-api/src/service/api.rs @@ -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, diff --git a/libs/ux-api/src/service/gfx.rs b/libs/ux-api/src/service/gfx.rs index 07f1b730c..383c5ace8 100644 --- a/libs/ux-api/src/service/gfx.rs +++ b/libs/ux-api/src/service/gfx.rs @@ -792,6 +792,29 @@ impl Gfx { Ok(()) } + #[cfg(feature = "board-baosec")] + pub fn bitmap_diffusion( + &self, + bitmap: &[u32], + top_left: Option, + bounding_box: Option, + ) -> 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( diff --git a/services/bao-video/Cargo.toml b/services/bao-video/Cargo.toml index a764b225e..6daaf0042 100644 --- a/services/bao-video/Cargo.toml +++ b/services/bao-video/Cargo.toml @@ -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 = [] diff --git a/services/bao-video/src/main.rs b/services/bao-video/src/main.rs index 6f71149ce..01238d6e0 100644 --- a/services/bao-video/src/main.rs +++ b/services/bao-video/src/main.rs @@ -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::().unwrap(); + display + .render_bitmap_diffuse(&bitmap, 10, rand::thread_rng().gen::()) + .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;