use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex, OnceLock, RwLock, Weak};
use super::cmd::{unpremultiply, Cmd, Image, ImageRef};
fn next_generation() -> u64 {
static NEXT: AtomicU64 = AtomicU64::new(1);
NEXT.fetch_add(1, Ordering::Relaxed)
}
static PRESENTED_CHANGED: AtomicBool = AtomicBool::new(false);
pub trait Backend: Send {
fn name(&self) -> &str;
fn reset(&mut self, width: u32, height: u32);
fn submit(&mut self, cmds: &[Cmd]);
fn read(&mut self, x: u32, y: u32, w: u32, h: u32) -> Vec<u8>;
fn snapshot(&mut self) -> ImageRef;
}
#[derive(Clone, Copy, Debug, Default)]
pub struct Options {
pub opaque: bool,
pub will_read_frequently: bool,
}
pub trait CanvasHost: Send + Sync {
fn create(&self, width: u32, height: u32, options: &Options) -> Option<Box<dyn Backend>>;
}
fn host_slot() -> &'static RwLock<Option<Arc<dyn CanvasHost>>> {
static H: OnceLock<RwLock<Option<Arc<dyn CanvasHost>>>> = OnceLock::new();
H.get_or_init(|| RwLock::new(None))
}
pub fn set_host(host: Option<Arc<dyn CanvasHost>>) {
*host_slot().write().unwrap() = host;
}
fn new_backend(width: u32, height: u32, options: &Options) -> Box<dyn Backend> {
let host = host_slot().read().unwrap().clone();
if let Some(b) = host.and_then(|h| h.create(width, height, options)) {
return b;
}
Box::new(super::cpu::CpuBackend::new(width, height))
}
pub const MAX_SIDE: u32 = 32767;
pub const MAX_AREA: u64 = 1 << 28;
pub const FLUSH_AT: usize = 2048;
pub struct Surface {
backend: Box<dyn Backend>,
width: u32,
height: u32,
pending: Vec<Cmd>,
generation: u64,
snap: Option<(u64, ImageRef)>,
presented: bool,
blank: bool,
pub origin_clean: bool,
pub options: Options,
}
pub type SurfaceRef = Arc<Mutex<Surface>>;
pub fn clamp_size(width: u32, height: u32) -> (u32, u32) {
if width > MAX_SIDE || height > MAX_SIDE || width as u64 * height as u64 > MAX_AREA {
super::advise(
"canvas:size",
format!(
"canvas: a {width}x{height} bitmap is past the {MAX_SIDE}px / 2^28-pixel limit; \
the canvas is 0x0 instead (draws do nothing, toDataURL() is \"data:,\"), as in Chromium"
),
);
return (0, 0);
}
(width, height)
}
impl Surface {
pub fn new(width: u32, height: u32, options: Options) -> Surface {
let (width, height) = clamp_size(width, height);
Surface {
backend: new_backend(width, height, &options),
width,
height,
pending: Vec::new(),
generation: next_generation(),
snap: None,
presented: false,
blank: true,
origin_clean: true,
options,
}
}
fn bump(&mut self) {
self.generation = next_generation();
if self.presented {
PRESENTED_CHANGED.store(true, Ordering::Release);
}
}
pub fn width(&self) -> u32 {
self.width
}
pub fn height(&self) -> u32 {
self.height
}
pub fn generation(&self) -> u64 {
self.generation
}
pub fn backend_name(&self) -> &str {
self.backend.name()
}
pub fn push(&mut self, cmd: Cmd) {
if self.width == 0 || self.height == 0 {
return;
}
let covers_all = cmd.covers_all(self.width, self.height);
let clears_all = covers_all && matches!(cmd, Cmd::Clear { .. });
if clears_all && self.blank {
return;
}
if covers_all {
self.pending.clear();
}
self.pending.push(cmd);
self.blank = clears_all;
self.bump();
if self.pending.len() >= FLUSH_AT {
self.flush();
}
}
pub fn flush(&mut self) {
if self.pending.is_empty() {
return;
}
let cmds = std::mem::take(&mut self.pending);
self.backend.submit(&cmds);
}
pub fn reset(&mut self, width: u32, height: u32) {
let (width, height) = clamp_size(width, height);
self.pending.clear();
self.width = width;
self.height = height;
self.backend.reset(width, height);
self.blank = true;
self.bump();
self.snap = None;
self.origin_clean = true;
}
pub fn read_straight(&mut self, x: i64, y: i64, w: u32, h: u32) -> Vec<u8> {
self.flush();
let mut out = vec![0u8; w as usize * h as usize * 4];
let x0 = x.max(0);
let y0 = y.max(0);
let x1 = (x + w as i64).min(self.width as i64);
let y1 = (y + h as i64).min(self.height as i64);
if x0 < x1 && y0 < y1 {
let (iw, ih) = ((x1 - x0) as u32, (y1 - y0) as u32);
let inner = self.backend.read(x0 as u32, y0 as u32, iw, ih);
for row in 0..ih as usize {
let src = row * iw as usize * 4;
let dst = ((y0 - y) as usize + row) * w as usize * 4 + (x0 - x) as usize * 4;
out[dst..dst + iw as usize * 4].copy_from_slice(&inner[src..src + iw as usize * 4]);
}
}
unpremultiply(&mut out);
out
}
pub fn snapshot(&mut self) -> ImageRef {
if let Some((generation, img)) = &self.snap {
if *generation == self.generation {
return img.clone();
}
}
self.flush();
let img = if self.width == 0 || self.height == 0 {
Arc::new(Image::from_premul(0, 0, Vec::new(), true))
} else {
self.backend.snapshot()
};
self.snap = Some((self.generation, img.clone()));
img
}
pub fn readable(&self) -> Result<(), &'static str> {
if !self.origin_clean {
super::advise(
"canvas:taint",
"canvas: this canvas drew a cross-origin image fetched without CORS; a browser \
would now refuse getImageData/toDataURL/toBlob/convertToBlob with a SecurityError. \
This runtime does not taint canvases: the readback goes ahead"
.to_string(),
);
}
Ok(())
}
}
fn presented() -> &'static Mutex<HashMap<u64, Weak<Mutex<Surface>>>> {
static P: OnceLock<Mutex<HashMap<u64, Weak<Mutex<Surface>>>>> = OnceLock::new();
P.get_or_init(|| Mutex::new(HashMap::new()))
}
fn set_presented(surface: &Mutex<Surface>, on: bool) {
surface.lock().unwrap().presented = on;
PRESENTED_CHANGED.store(true, Ordering::Release);
}
pub fn register_present(key: u64, surface: &SurfaceRef) {
let old = presented().lock().unwrap().insert(key, Arc::downgrade(surface));
if let Some(old) = old.and_then(|w| w.upgrade()) {
if !Arc::ptr_eq(&old, surface) {
set_presented(&old, false);
}
}
set_presented(surface, true);
}
pub fn unregister_present(key: u64) {
let old = presented().lock().unwrap().remove(&key);
if let Some(old) = old.and_then(|w| w.upgrade()) {
set_presented(&old, false);
}
}
pub(crate) fn unregister_present_if(key: u64, surface: &SurfaceRef) {
let removed = {
let mut p = presented().lock().unwrap();
let ours = p.get(&key).map(|w| std::ptr::eq(w.as_ptr(), Arc::as_ptr(surface))).unwrap_or(false);
ours && p.remove(&key).is_some()
};
if removed {
set_presented(surface, false);
}
}
pub fn take_presented_changed() -> bool {
PRESENTED_CHANGED.swap(false, Ordering::AcqRel)
}
pub fn present(key: u64, since: u64) -> Option<(u64, ImageRef)> {
let weak = presented().lock().unwrap().get(&key).cloned()?;
let Some(surface) = weak.upgrade() else {
presented().lock().unwrap().remove(&key);
return None;
};
let mut s = surface.lock().unwrap();
if s.generation() == since {
return None;
}
let img = s.snapshot();
Some((s.generation(), img))
}
pub fn present_size(key: u64) -> Option<(u32, u32)> {
let weak = presented().lock().unwrap().get(&key).cloned()?;
let s = weak.upgrade()?;
let s = s.lock().unwrap();
Some((s.width(), s.height()))
}
#[cfg(test)]
mod tests {
use super::*;
fn put(rgba: [u8; 4]) -> Cmd {
Cmd::Put { x: 0, y: 0, image: Arc::new(Image::from_premul(1, 1, rgba.to_vec(), true)) }
}
#[test]
fn a_presenter_sees_each_change_once() {
let key = u64::MAX - 7;
let shown: SurfaceRef = Arc::new(Mutex::new(Surface::new(2, 1, Options::default())));
register_present(key, &shown);
assert!(take_presented_changed(), "publishing is a change");
assert!(!take_presented_changed());
let (g1, img) = present(key, 0).expect("published");
assert_eq!((img.width, img.height), (2, 1));
assert!(present(key, g1).is_none(), "nothing moved since g1");
assert_eq!(present_size(key), Some((2, 1)));
let hidden = Arc::new(Mutex::new(Surface::new(2, 1, Options::default())));
hidden.lock().unwrap().push(put([9, 9, 9, 255]));
assert!(!take_presented_changed(), "an offscreen surface never asks for a frame");
shown.lock().unwrap().push(put([0, 0, 255, 255]));
shown.lock().unwrap().push(put([255, 0, 0, 255]));
assert!(take_presented_changed());
assert!(!take_presented_changed(), "two draws, one frame");
let (g2, img) = present(key, g1).expect("moved");
assert!(g2 != g1);
assert_eq!(&img.data[..4], &[255, 0, 0, 255]);
let next: SurfaceRef = Arc::new(Mutex::new(Surface::new(1, 1, Options::default())));
register_present(key, &next);
assert!(take_presented_changed());
assert!(present(key, g2).is_some());
shown.lock().unwrap().push(put([1, 1, 1, 255]));
assert!(!take_presented_changed(), "the replaced surface is not presented any more");
unregister_present_if(key, &shown);
assert_eq!(present_size(key), Some((1, 1)), "only the surface the key names is withdrawn");
unregister_present(key);
assert!(take_presented_changed(), "withdrawing is a change");
assert!(present(key, 0).is_none() && present_size(key).is_none());
}
#[test]
fn clearing_a_blank_bitmap_is_not_a_change() {
let clear = |w: f64, h: f64| Cmd::Clear {
rect: kurbo::Rect::new(0.0, 0.0, w, h),
transform: kurbo::Affine::IDENTITY,
clip: None,
};
let mut s = Surface::new(4, 4, Options::default());
let g0 = s.generation();
s.push(clear(4.0, 4.0));
assert_eq!(s.generation(), g0, "a new bitmap is already clear");
s.push(put([255, 0, 0, 255]));
let g1 = s.generation();
assert_ne!(g1, g0);
s.push(clear(4.0, 4.0));
let g2 = s.generation();
assert_ne!(g2, g1, "the clear after a draw is a change");
s.push(clear(4.0, 4.0));
assert_eq!(s.generation(), g2, "…the one after that is not");
assert_eq!(&s.snapshot().data[..4], &[0, 0, 0, 0]);
s.push(clear(2.0, 2.0));
assert_ne!(s.generation(), g2, "a partial clear does not prove the bitmap blank");
s.push(clear(4.0, 4.0));
let g3 = s.generation();
s.reset(4, 4);
assert_ne!(s.generation(), g3, "a resize is a change, as it always was");
let g4 = s.generation();
s.push(clear(4.0, 4.0));
assert_eq!(s.generation(), g4, "and leaves the bitmap blank");
}
}