use super::{visual::VisualMesh, Paint};
use crate::{
context::{geometry::Element, Id},
shapes::Mesh,
Rect,
};
use std::{
collections::{HashMap, HashSet},
sync::Arc,
};
pub struct CachedElement {
pub paint: Vec<Paint>,
pub scale: f32,
pub mesh: Arc<Mesh>,
pub bounds: Option<Rect>,
pub last_frame: u64,
}
pub(crate) struct PaintState {
pub(in crate::context) cache: HashMap<Id, CachedElement>,
pub(in crate::context) visual_meshes: HashMap<Id, VisualMesh>,
pub(super) materializing: bool,
pub(super) image_visual_scale: f32,
pub(in crate::context) elements: Vec<Element>,
pub(in crate::context) order: HashMap<Id, usize>,
pub(in crate::context) seen: HashSet<Id>,
pub(in crate::context) modified: HashSet<Id>,
}
impl Default for PaintState {
fn default() -> Self {
Self {
cache: HashMap::new(),
visual_meshes: HashMap::new(),
materializing: false,
image_visual_scale: 1.0,
elements: Vec::new(),
order: HashMap::new(),
seen: HashSet::new(),
modified: HashSet::new(),
}
}
}
impl PaintState {
pub(in crate::context) fn begin_pass(&mut self) {
self.elements.clear();
self.order.clear();
self.seen.clear();
self.modified.clear();
}
pub(in crate::context) fn retire(&mut self, frame: u64) {
let seen = &self.seen;
self.visual_meshes.retain(|id, _| seen.contains(id));
self.cache.retain(|_, element| element.last_frame == frame);
}
pub(in crate::context) fn record_order(&mut self, id: Id) {
let order = self.order.len();
self.order.entry(id).or_insert(order);
}
pub(in crate::context) fn image_visual_scale(&self) -> f32 {
self.image_visual_scale
}
pub(in crate::context) fn painted(&self, id: Id) -> bool {
self.seen.contains(&id)
}
pub(super) fn claim(&mut self, id: Id) -> Result<Id, Id> {
if self.seen.insert(id) {
return Ok(id);
}
let mut n = 1_u32;
let alias = loop {
let alias = id.with(("duplicate", n));
if self.seen.insert(alias) {
break alias;
}
n += 1;
};
self.record_order(alias);
Err(alias)
}
pub(in crate::context) fn last_element_mut(&mut self, id: Id) -> Option<&mut Element> {
self.elements.last_mut().filter(|element| element.id == id)
}
pub(in crate::context) fn sort(&mut self, ranks: &HashMap<Id, usize>) {
let order = &self.order;
self.elements.sort_by_key(|e| {
(
ranks.get(&e.layer).copied().unwrap_or(0),
order.get(&e.id).copied().unwrap_or(0),
)
});
}
pub(super) fn drop_visual(&mut self, id: Id) {
if !self.materializing && self.visual_meshes.remove(&id).is_some() {
self.modified.insert(id);
}
}
pub(super) fn emit(&mut self, id: Id, layer: Id, clip: Rect, cull: bool) {
let cached = &self.cache[&id];
if cull
&& cached
.bounds
.is_some_and(|bounds| bounds.intersect(clip).is_empty())
{
return;
}
self.elements.push(Element {
id,
layer,
clip,
mesh: Arc::clone(&cached.mesh),
blur: None,
scroll_hint: super::data::is_scroll_hint(&cached.paint),
material: None,
});
}
}