use std::{cell::Cell, mem::size_of};
use cranpose_core::{RecordId, collections::growth_slack::GrowthSlack};
use cranpose_ui::GraphicsLayer;
#[cfg(feature = "window-damage")]
use cranpose_ui::Rect;
#[cfg(feature = "window-damage")]
use cranpose_ui_graphics::DrawPrimitive;
#[cfg(feature = "window-damage")]
use super::ItemRef;
use super::{Item, ItemKind, LayerProps, LayerRef, RecordFlags, SceneStore};
#[cfg(feature = "window-damage")]
use crate::graph::{CONTAINED_DRAW_SLACK, inflate_rect, rect_within};
#[derive(Clone, Copy, Default)]
struct RowFacts {
children: Option<ChildFacts>,
#[cfg(feature = "window-damage")]
contained: Option<bool>,
#[cfg(feature = "window-damage")]
own: Option<OwnDrawFacts>,
}
#[cfg(feature = "window-damage")]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct OwnDrawFacts {
pub pixel_sensitive: bool,
pub drawn: bool,
}
#[derive(Clone, Copy)]
struct ChildFacts {
hit_targets: bool,
origin_sinks: bool,
space: bool,
}
#[derive(Clone, Default)]
pub(crate) struct Facts {
rows: Vec<Cell<RowFacts>>,
}
impl Facts {
pub(super) fn open_row(&mut self, index: usize) {
match self.rows.get(index) {
Some(row) => row.set(RowFacts::default()),
None => self.rows.resize_with(index + 1, Cell::default),
}
}
pub(super) fn clear(&mut self) {
self.rows.clear();
}
pub(super) fn heap_bytes(&self) -> usize {
self.rows.capacity() * size_of::<Cell<RowFacts>>()
}
pub(super) fn trim_growth_slack(&mut self) {
self.rows.trim_growth_slack();
}
pub(super) fn truncate_rows(&mut self, len: usize) {
self.rows.truncate(len);
}
}
#[cfg(feature = "window-damage")]
pub(super) fn forget_contained(store: &SceneStore, index: usize) {
forget(store, index, |row| row.contained.take().is_some());
}
pub(super) fn forget_children(store: &SceneStore, index: usize) {
forget(store, index, |row| row.children.take().is_some());
}
#[cfg(feature = "window-damage")]
pub(super) fn forget_own(store: &SceneStore, index: usize) {
let row = &store.facts.rows[index];
row.set(RowFacts {
own: None,
..row.get()
});
}
#[cfg(not(feature = "window-damage"))]
#[inline]
pub(super) fn forget_own(_store: &SceneStore, _index: usize) {}
#[cfg(feature = "window-damage")]
pub(super) fn keeps_contained(store: &SceneStore, index: usize) -> bool {
store.facts.rows[index].get().contained.is_some()
}
fn forget(store: &SceneStore, index: usize, take: impl Fn(&mut RowFacts) -> bool) {
let mut current = Some(index);
while let Some(index) = current {
let row = &store.facts.rows[index];
let mut facts = row.get();
if !take(&mut facts) {
break;
}
row.set(facts);
current = store.tree[index].parent.map(RecordId::index);
}
}
#[inline]
pub(super) fn has_hit_targets(layer: LayerRef<'_>) -> bool {
child_facts(layer).hit_targets
}
#[inline]
pub(super) fn has_origin_sinks(layer: LayerRef<'_>) -> bool {
child_facts(layer).origin_sinks
}
#[inline]
pub(super) fn has_space(layer: LayerRef<'_>) -> bool {
child_facts(layer).space
}
#[cfg(feature = "window-damage")]
#[inline]
pub(super) fn contained(layer: LayerRef<'_>) -> bool {
match layer.store.facts.rows[layer.id.index()].get().contained {
Some(contained) => contained,
None => work_out_contained(layer),
}
}
#[cfg(feature = "window-damage")]
#[inline(never)]
fn work_out_contained(layer: LayerRef<'_>) -> bool {
let row = &layer.store.facts.rows[layer.id.index()];
let contained = layer.visual_clip_rect().is_some()
|| items_draw_within(
layer,
inflate_rect(layer.local_bounds(), CONTAINED_DRAW_SLACK),
);
row.set(RowFacts {
contained: Some(contained),
..row.get()
});
contained
}
#[cfg(feature = "window-damage")]
#[inline]
pub(super) fn own_draw_facts(layer: LayerRef<'_>) -> OwnDrawFacts {
match layer.store.facts.rows[layer.id.index()].get().own {
Some(facts) => facts,
None => work_out_own_draw_facts(layer),
}
}
#[cfg(feature = "window-damage")]
#[inline(never)]
fn work_out_own_draw_facts(layer: LayerRef<'_>) -> OwnDrawFacts {
let mut facts = OwnDrawFacts::default();
for item in layer.own_draws() {
let (pixel_sensitive, drawn) = match item {
ItemRef::Text(_) => (true, true),
ItemRef::Draw(draw) => (
primitive_is_pixel_sensitive(&draw.primitive),
!matches!(
draw.primitive,
DrawPrimitive::Content | DrawPrimitive::Shadow(_)
),
),
ItemRef::Run(run) => (
run.summary.has_text || run.summary.has_pixel_sensitive,
run.summary.has_non_shadow,
),
ItemRef::Shape(shape) => (false, shape.shape_count() > 0),
ItemRef::Layer(_) | ItemRef::Leaf(_) => (false, false),
};
facts.pixel_sensitive |= pixel_sensitive;
facts.drawn |= drawn;
}
let row = &layer.store.facts.rows[layer.id.index()];
row.set(RowFacts {
own: Some(facts),
..row.get()
});
facts
}
#[cfg(feature = "window-damage")]
fn primitive_is_pixel_sensitive(primitive: &DrawPrimitive) -> bool {
match primitive {
DrawPrimitive::Blend { primitive, .. } => primitive_is_pixel_sensitive(primitive),
DrawPrimitive::Image { .. } | DrawPrimitive::Text(_) => true,
_ => false,
}
}
#[inline]
fn child_facts(layer: LayerRef<'_>) -> ChildFacts {
match layer.store.facts.rows[layer.id.index()].get().children {
Some(facts) => facts,
None => work_out_child_facts(layer),
}
}
#[inline(never)]
fn work_out_child_facts(layer: LayerRef<'_>) -> ChildFacts {
let row = &layer.store.facts.rows[layer.id.index()];
let mut facts = ChildFacts {
hit_targets: layer.hit_test().is_some(),
origin_sinks: layer.flags().contains(RecordFlags::ORIGIN_SINKS),
space: layer.graphics_layer().space.is_some(),
};
for child in layer.children() {
let child = child_facts(child);
facts.hit_targets |= child.hit_targets;
facts.origin_sinks |= child.origin_sinks;
facts.space |= child.space;
}
let mut kept = row.get();
kept.children = Some(facts);
row.set(kept);
facts
}
#[cfg(feature = "window-damage")]
fn items_draw_within(layer: LayerRef<'_>, bounds: Rect) -> bool {
layer.items().all(|item| match item {
ItemRef::Draw(_) => false,
ItemRef::Text(text) => text.draws_within(bounds),
ItemRef::Run(run) => run.draws_within(bounds),
ItemRef::Shape(shape) => shape
.coverage()
.is_none_or(|covered| rect_within(covered, bounds)),
ItemRef::Layer(child) => {
let graphics_layer = child.graphics_layer();
graphics_layer.shadow_elevation <= 0.0
&& graphics_layer.render_effect.is_none()
&& graphics_layer.backdrop_effect.is_none()
&& rect_within(
child
.transform_to_parent()
.bounds_for_rect(inflate_rect(child.local_bounds(), CONTAINED_DRAW_SLACK)),
bounds,
)
&& contained(child)
}
ItemRef::Leaf(leaf) => {
let origin = leaf.origin();
rect_within(
inflate_rect(leaf.node_rect(), CONTAINED_DRAW_SLACK).translate(origin.x, origin.y),
bounds,
) && contained(leaf)
}
})
}
pub(super) fn props_inputs(props: &LayerProps) -> (bool, Option<[u32; 4]>) {
(
props.isolation.group_opacity || props.isolation.explicit_offscreen,
props
.layer_bounds
.map(|bounds| [bounds.x, bounds.y, bounds.width, bounds.height].map(f32::to_bits)),
)
}
pub(super) fn layer_inputs(layer: &GraphicsLayer) -> (bool, bool, Option<u32>, Option<u32>) {
(
layer.clip,
layer.shadow_elevation > 0.0,
layer
.render_effect
.as_ref()
.map(|effect| (effect.input_padding() + effect.output_padding()).to_bits()),
layer
.backdrop_effect
.as_ref()
.map(|backdrop| backdrop.output_padding().to_bits()),
)
}
pub(super) fn same_children(old: &[Item], new: &[Item]) -> bool {
fn children(items: &[Item]) -> impl Iterator<Item = Item> + '_ {
items
.iter()
.copied()
.filter(|item| item.kind() == ItemKind::Child)
}
children(old).eq(children(new))
}