use alloc::collections::BTreeMap;
use super::{Cells, Pixels, Surface};
use crate::error::{Error, Result};
use crate::geometry::{Geometry, Rect};
use crate::host;
#[derive(Clone, Copy, Debug)]
struct Sent {
place: Rect,
size: (u32, u32),
hash: u64,
drawn: bool,
}
#[derive(Debug, Default)]
pub(super) struct SentGraphics {
sent: BTreeMap<u32, Sent>,
cleared: bool,
changed: bool,
}
impl SentGraphics {
pub(super) fn clear(&mut self) {
self.cleared = true;
for graphic in self.sent.values_mut() {
graphic.drawn = false;
}
}
pub(super) fn rebound(&mut self, geometry: Geometry) {
let cell = (geometry.cell_px_w, geometry.cell_px_h);
self.sent.retain(|_, held| {
let place = held.place;
place.x + place.w <= geometry.cols
&& place.y + place.h <= geometry.rows
&& held.size == (place.w * cell.0, place.h * cell.1)
});
}
pub(super) fn remove_undrawn(&mut self, id: &str) -> Result<()> {
if !core::mem::take(&mut self.cleared) {
return Ok(());
}
let undrawn: alloc::vec::Vec<u32> = self
.sent
.iter()
.filter(|(_, graphic)| !graphic.drawn)
.map(|(key, _)| *key)
.collect();
for key in undrawn {
host::surface_remove_graphic(id, key)?;
self.sent.remove(&key);
self.changed = true;
}
Ok(())
}
pub(super) fn changed(&self) -> bool {
self.changed
}
pub(super) fn published(&mut self) {
self.changed = false;
}
}
fn hash(bytes: &[u8]) -> u64 {
const FACTOR: u64 = 0x9E37_79B9_7F4A_7C15;
let mut hash = bytes.len() as u64;
let (words, rest) = bytes.as_chunks::<8>();
for word in words {
hash = (hash.rotate_left(5) ^ u64::from_le_bytes(*word)).wrapping_mul(FACTOR);
}
for byte in rest {
hash = (hash.rotate_left(5) ^ u64::from(*byte)).wrapping_mul(FACTOR);
}
hash
}
impl Surface<Cells> {
pub fn draw_graphic(
&mut self,
key: u32,
place: Rect,
draw: impl FnOnce(&mut Surface<Pixels>),
) -> Result<bool> {
let Some(geometry) = self.geometry() else {
return Ok(false);
};
let cell_px = (geometry.cell_px_w, geometry.cell_px_h);
if !host::capabilities().graphics || cell_px.0 == 0 || cell_px.1 == 0 {
return Ok(false);
}
let mut canvas = Surface::<Pixels>::offscreen(Geometry::cells(place.w, place.h, cell_px));
draw(&mut canvas);
let (width, height) = canvas.size();
let pixels = canvas.pixels_untracked();
let hash = hash(&pixels);
let id = self.id();
let mut inner = self.inner.borrow_mut();
let held = inner.graphics.sent.get(&key).copied();
let same = held.is_some_and(|held| held.hash == hash && held.size == (width, height));
if !same || held.is_some_and(|held| held.place != place) {
let image = (!same).then_some(&pixels[..]);
match host::surface_set_graphic(&id, key, place, (width, height), image) {
Err(Error::Unavailable(_)) => return Ok(false),
sent => sent?,
}
inner.graphics.changed = true;
}
inner.graphics.sent.insert(
key,
Sent {
place,
size: (width, height),
hash,
drawn: true,
},
);
Ok(true)
}
pub fn move_graphic(&mut self, key: u32, place: Rect) -> Result<bool> {
let id = self.id();
let mut inner = self.inner.borrow_mut();
let Some(held) = inner.graphics.sent.get(&key).copied() else {
return Ok(false);
};
if (held.place.w, held.place.h) != (place.w, place.h) {
return Ok(false);
}
if held.place != place {
host::surface_set_graphic(&id, key, place, held.size, None)?;
inner.graphics.changed = true;
}
inner.graphics.sent.insert(
key,
Sent {
place,
drawn: true,
..held
},
);
Ok(true)
}
pub fn remove_graphic(&mut self, key: u32) -> Result<()> {
let id = self.id();
let mut inner = self.inner.borrow_mut();
if inner.graphics.sent.remove(&key).is_some() {
host::surface_remove_graphic(&id, key)?;
inner.graphics.changed = true;
}
Ok(())
}
}