use std::collections::HashMap;
use incular_core::Transform;
use crate::{BackdropKey, GaussianBlur, LayerId};
#[must_use]
pub fn backdrop_filter_is_active(
blur: GaussianBlur,
enabled: bool,
matrix: Option<Transform>,
) -> bool {
enabled && (matrix.is_some() || blur.sigma_x > f32::EPSILON || blur.sigma_y > f32::EPSILON)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BackdropSample {
Capture,
Shared(LayerId),
}
#[derive(Clone, Debug, Default)]
pub struct BackdropCaptures {
captured: HashMap<(u64, BackdropKey), LayerId>,
}
impl BackdropCaptures {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn clear(&mut self) {
self.captured.clear();
}
pub fn sample(
&mut self,
layer: LayerId,
key: Option<BackdropKey>,
scope: u64,
) -> BackdropSample {
let Some(key) = key else {
return BackdropSample::Capture;
};
match self.captured.get(&(scope, key)) {
Some(&first) => BackdropSample::Shared(first),
None => {
self.captured.insert((scope, key), layer);
BackdropSample::Capture
}
}
}
}
#[must_use]
pub fn backdrop_matrix_sampling(
stage: Transform,
matrix: Transform,
scale: f32,
) -> Option<Transform> {
let device = Transform::scale(scale);
let stage_inverse = stage.inverse()?;
let matrix_inverse = matrix.inverse()?;
let device_inverse = device.inverse()?;
Some(
device
.then(stage)
.then(matrix_inverse)
.then(stage_inverse)
.then(device_inverse),
)
}