use std::rc::Rc;
use crate::{
Bounds, ScaledPixels, Scene,
fast::layers::{background::draw_order, scene::visible_bounds},
point,
scene::{PaintOperation, Primitive},
};
pub(crate) fn reach(primitive: &Primitive) -> Bounds<ScaledPixels> {
let visible = visible_bounds(primitive);
match primitive {
Primitive::Path(_) => {
let edge = ScaledPixels(1.);
Bounds::from_corners(
visible.origin - point(edge, edge),
visible.bottom_right() + point(edge, edge),
)
}
_ => visible,
}
}
fn rank(primitive: &Primitive) -> u8 {
match primitive {
Primitive::Shadow(_) => 0,
Primitive::Quad(_) => 1,
Primitive::Path(_) => 2,
Primitive::Underline(_) => 3,
Primitive::MonochromeSprite(_) => 4,
Primitive::SubpixelSprite(_) => 5,
Primitive::PolychromeSprite(_) => 6,
Primitive::Surface(_) => 7,
Primitive::NativeView(_) => 8,
}
}
pub(crate) fn split(
operations: &[PaintOperation],
) -> Option<(Vec<PaintOperation>, Vec<Primitive>)> {
let painted_path = operations
.iter()
.any(|operation| matches!(operation, PaintOperation::Primitive(Primitive::Path(_))));
if !painted_path {
return None;
}
if operations
.iter()
.any(|operation| matches!(operation, PaintOperation::FastRoundedClip(_)))
{
return None;
}
let mut sequence: Vec<((u32, u8, usize), &Primitive)> = operations
.iter()
.enumerate()
.filter_map(|(ix, operation)| match operation {
PaintOperation::Primitive(primitive) => {
Some(((draw_order(primitive), rank(primitive), ix), primitive))
}
_ => None,
})
.collect();
sequence.sort_unstable_by_key(|(key, _)| *key);
let mut in_overlay = vec![false; operations.len()];
let mut reaches: Vec<Bounds<ScaledPixels>> = Vec::new();
let mut union: Option<Bounds<ScaledPixels>> = None;
let mut overlay = Vec::new();
for ((_, _, ix), primitive) in sequence {
let reach = reach(primitive);
if matches!(primitive, Primitive::Path(_))
|| union.is_some_and(|union| union.intersects(&reach))
&& reaches.iter().any(|earlier| earlier.intersects(&reach))
{
in_overlay[ix] = true;
union = Some(union.map_or(reach, |union| union.union(&reach)));
reaches.push(reach);
overlay.push(primitive.clone());
}
}
let tiles = operations
.iter()
.zip(&in_overlay)
.filter(|(_, in_overlay)| !**in_overlay)
.map(|(operation, _)| match operation {
PaintOperation::Primitive(primitive) => PaintOperation::Primitive(primitive.clone()),
PaintOperation::StartLayer(bounds) => PaintOperation::StartLayer(*bounds),
PaintOperation::EndLayer => PaintOperation::EndLayer,
PaintOperation::FastRoundedClip(clip) => PaintOperation::FastRoundedClip(*clip),
})
.collect();
Some((tiles, overlay))
}
pub(crate) fn lift(content: Scene) -> (Scene, Rc<[Primitive]>) {
let Some((tiles, overlay)) = split(&content.paint_operations) else {
return (content, Rc::from([]));
};
let mut rest = Scene::default();
for operation in tiles {
match operation {
PaintOperation::Primitive(primitive) => rest.insert_primitive(primitive),
PaintOperation::StartLayer(bounds) => rest.push_layer(bounds),
PaintOperation::EndLayer => rest.pop_layer(),
PaintOperation::FastRoundedClip(clip) => {
crate::fast::rounded_clip::record(&mut rest, clip)
}
}
}
rest.finish();
(rest, overlay.into())
}