use std::mem;
use crate::fast::rounded_clip::{ClipOperation, Shape};
use crate::{
AtlasTextureId, AtlasTextureKind, AtlasTile, ContentMask, LayerContent, LayerFrame, LayerKey,
PolychromeSprite, Rgba, ScaledPixels, Scene, TileId, bounds_tree::BoundsTree,
scene::PaintOperation,
};
const CLIP_TEXTURE_BASE: u32 = 0xF100_0000;
const GEOMETRY_KEY: LayerKey = LayerKey(u32::MAX);
pub(crate) fn compile(scene: &mut Scene) {
if scene.fast_rounded_clip.scopes.is_empty() || scene.fast_rounded_clip.compiled {
return;
}
scene.native_views.sort_by_key(|view| view.order);
scene.keep_first_painted_native_views();
let operations = mem::take(&mut scene.paint_operations);
let layers: Vec<LayerFrame> = scene
.layers
.frames
.iter()
.filter(|frame| frame.tile_size != 0)
.cloned()
.collect();
let mut trees = mem::take(&mut scene.fast_rounded_clip.trees);
let new_scene = |tree| {
let mut scene = Scene::default();
scene.layers.frames = layers.clone();
scene.primitive_bounds = tree;
scene
};
let mut opened = 0;
let mut slots = Vec::new();
let mut stack = vec![new_scene(trees.take(None))];
let mut shapes = Vec::new();
let mut redundant = Vec::new();
for operation in &operations {
match operation {
PaintOperation::FastRoundedClip(ClipOperation::Begin(shape)) => {
let already_active = shapes.contains(shape);
redundant.push(already_active);
shapes.push(*shape);
if !already_active {
stack.push(new_scene(trees.take(Some(opened))));
slots.push(opened);
opened += 1;
}
}
PaintOperation::FastRoundedClip(ClipOperation::End) => {
let shape = shapes.pop().expect("balanced rounded clips");
if redundant.pop().expect("rounded clip ownership") {
continue;
}
let mut child = stack.pop().expect("rounded clip scene");
let slot = slots.pop().expect("rounded clip tree");
trees.put(Some(slot), mem::take(&mut child.primitive_bounds));
if child.is_empty() {
continue;
}
child.layers.frames.push(geometry(shape));
child.finish();
let parent = stack.last_mut().expect("parent rounded clip scene");
let key = LayerKey(0x0100_0000 + parent.layers.frames.len() as u32);
parent.layers.frames.push(LayerFrame {
key,
generation: 0,
background: Rgba::default(),
tile_size: 0,
content: LayerContent::from(child),
dirty_tiles: Vec::new(),
});
parent.insert_primitive(PolychromeSprite {
order: 0,
pad: 0,
grayscale: false.into(),
opacity: 1.,
bounds: shape.bounds,
content_mask: ContentMask {
bounds: shape.bounds,
},
corner_radii: shape.radii.map(|radius| radius.width),
tile: AtlasTile {
texture_id: AtlasTextureId {
index: CLIP_TEXTURE_BASE + key.0 - 0x0100_0000,
kind: AtlasTextureKind::Polychrome,
},
tile_id: TileId(0),
padding: 0,
bounds: Default::default(),
},
});
}
PaintOperation::Primitive(crate::Primitive::NativeView(view)) => {
if scene
.native_views
.iter()
.any(|first| first.id == view.id && first.hitbox_id == view.hitbox_id)
{
stack.last_mut().unwrap().insert_primitive(*view);
}
}
PaintOperation::Primitive(primitive) => stack
.last_mut()
.unwrap()
.insert_primitive(primitive.clone()),
PaintOperation::StartLayer(bounds) => stack.last_mut().unwrap().push_layer(*bounds),
PaintOperation::EndLayer => stack.last_mut().unwrap().pop_layer(),
}
}
assert!(
shapes.is_empty() && stack.len() == 1,
"balanced rounded clips"
);
let mut compiled = stack.pop().unwrap();
compiled.paint_operations = operations;
compiled.fast_rounded_clip = mem::take(&mut scene.fast_rounded_clip);
let root = mem::replace(
&mut compiled.primitive_bounds,
mem::take(&mut scene.primitive_bounds),
);
trees.put(None, root);
compiled.fast_rounded_clip.trees = trees;
if scene.native_views.len() != compiled.native_views.len() {
let mut direct = mem::replace(
&mut compiled.native_views,
mem::take(&mut scene.native_views),
);
direct.sort_by_key(|view| view.order);
let indices = direct
.iter()
.map(|view| {
compiled
.native_views
.iter()
.position(|placement| {
placement.id == view.id && placement.hitbox_id == view.hitbox_id
})
.expect("every directly drawn native view has a placement")
})
.collect();
compiled.fast_rounded_clip.native_batches =
Some(Box::new(NativeBatches { direct, indices }));
}
compiled.fast_rounded_clip.compiled = true;
compiled.sort_scratch = mem::take(&mut scene.sort_scratch);
*scene = compiled;
}
#[derive(Default)]
pub(crate) struct ClipTrees {
root: BoundsTree<ScaledPixels>,
clips: Vec<BoundsTree<ScaledPixels>>,
}
impl ClipTrees {
fn take(&mut self, slot: Option<usize>) -> BoundsTree<ScaledPixels> {
let mut tree = match slot {
None => mem::take(&mut self.root),
Some(slot) => self.clips.get_mut(slot).map(mem::take).unwrap_or_default(),
};
tree.clear();
tree
}
fn put(&mut self, slot: Option<usize>, tree: BoundsTree<ScaledPixels>) {
match slot {
None => self.root = tree,
Some(slot) => {
if self.clips.len() <= slot {
self.clips.resize_with(slot + 1, Default::default);
}
self.clips[slot] = tree;
}
}
}
}
pub(crate) struct NativeBatches {
direct: Vec<crate::PaintNativeView>,
indices: Vec<usize>,
}
pub(crate) fn native_batches(scene: &Scene) -> (&[crate::PaintNativeView], Option<&[usize]>) {
match scene.fast_rounded_clip.native_batches.as_deref() {
Some(batches) => (&batches.direct, Some(&batches.indices)),
None => (&scene.native_views, None),
}
}
fn geometry(shape: Shape) -> LayerFrame {
let r = shape.radii;
LayerFrame {
key: GEOMETRY_KEY,
generation: 0,
background: Rgba {
r: r.top_left.height.0,
g: r.top_right.height.0,
b: r.bottom_right.height.0,
a: r.bottom_left.height.0,
},
tile_size: 0,
content: Scene::default().into(),
dirty_tiles: Vec::new(),
}
}