use crate::region::Region;
use hashbrown::HashMap;
use vello_common::coarse::WideTilesBbox;
use vello_common::pixmap::Pixmap;
#[derive(Debug)]
pub struct LayerManager {
layers: HashMap<u32, (Pixmap, WideTilesBbox)>,
next_id: u32,
scratch_buffer: Option<Pixmap>,
}
impl Default for LayerManager {
fn default() -> Self {
Self {
layers: HashMap::new(),
next_id: 1,
scratch_buffer: None,
}
}
}
impl LayerManager {
pub fn new() -> Self {
Self::default()
}
pub fn register_layer(&mut self, layer_id: u32, wtile_bbox: WideTilesBbox, pixmap: Pixmap) {
self.layers.insert(layer_id, (pixmap, wtile_bbox));
if layer_id >= self.next_id {
self.next_id = layer_id + 1;
}
}
pub fn layer_tile_region_mut(
&mut self,
layer_id: u32,
tile_x: u16,
tile_y: u16,
) -> Option<Region<'_>> {
let (pixmap, bbox) = self.layers.get_mut(&layer_id)?;
if !bbox.contains(tile_x, tile_y) {
return None;
}
let local_x = tile_x - bbox.x0();
let local_y = tile_y - bbox.y0();
Region::from_pixmap_tile(pixmap, local_x, local_y)
}
pub fn get_scratch_buffer(&mut self, width: u16, height: u16) -> &mut Pixmap {
match &mut self.scratch_buffer {
None => {
self.scratch_buffer = Some(Pixmap::new(width, height));
}
Some(buf) if buf.width() < width || buf.height() < height => {
buf.resize(width, height);
}
Some(_) => {}
}
self.scratch_buffer.as_mut().unwrap()
}
}