cranpose_render_common/scene_store/
facts.rs1use std::{cell::Cell, mem::size_of};
14
15use cranpose_core::{RecordId, collections::growth_slack::GrowthSlack};
16use cranpose_ui::GraphicsLayer;
17#[cfg(feature = "window-damage")]
18use cranpose_ui::Rect;
19#[cfg(feature = "window-damage")]
20use cranpose_ui_graphics::DrawPrimitive;
21
22#[cfg(feature = "window-damage")]
23use super::ItemRef;
24use super::{Item, ItemKind, LayerProps, LayerRef, RecordFlags, SceneStore};
25#[cfg(feature = "window-damage")]
26use crate::graph::{CONTAINED_DRAW_SLACK, inflate_rect, rect_within};
27
28#[derive(Clone, Copy, Default)]
29struct RowFacts {
30 children: Option<ChildFacts>,
31 #[cfg(feature = "window-damage")]
32 contained: Option<bool>,
33 #[cfg(feature = "window-damage")]
34 own: Option<OwnDrawFacts>,
35}
36
37#[cfg(feature = "window-damage")]
40#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
41pub struct OwnDrawFacts {
42 pub pixel_sensitive: bool,
45 pub drawn: bool,
48}
49
50#[derive(Clone, Copy)]
54struct ChildFacts {
55 hit_targets: bool,
56 origin_sinks: bool,
57 space: bool,
58}
59
60#[derive(Clone, Default)]
61pub(crate) struct Facts {
62 rows: Vec<Cell<RowFacts>>,
63}
64
65impl Facts {
66 pub(super) fn open_row(&mut self, index: usize) {
69 match self.rows.get(index) {
70 Some(row) => row.set(RowFacts::default()),
71 None => self.rows.resize_with(index + 1, Cell::default),
72 }
73 }
74
75 pub(super) fn clear(&mut self) {
76 self.rows.clear();
77 }
78
79 pub(super) fn reserve_rows(&mut self, rows: usize) {
81 self.rows
82 .reserve_exact(rows.saturating_sub(self.rows.len()));
83 }
84
85 pub(super) fn heap_bytes(&self) -> usize {
86 self.rows.capacity() * size_of::<Cell<RowFacts>>()
87 }
88
89 pub(super) fn trim_growth_slack(&mut self) {
90 self.rows.trim_growth_slack();
91 }
92
93 pub(super) fn cut_rows(&mut self, len: usize) {
94 super::growth_slack::cut_rows(&mut self.rows, len);
95 }
96}
97
98#[cfg(feature = "window-damage")]
101pub(super) fn forget_contained(store: &SceneStore, index: usize) {
102 forget(store, index, |row| row.contained.take().is_some());
103}
104
105pub(super) fn forget_children(store: &SceneStore, index: usize) {
108 forget(store, index, |row| row.children.take().is_some());
109}
110
111#[cfg(feature = "window-damage")]
114pub(super) fn forget_own(store: &SceneStore, index: usize) {
115 let row = &store.facts.rows[index];
116 row.set(RowFacts {
117 own: None,
118 ..row.get()
119 });
120}
121
122#[cfg(not(feature = "window-damage"))]
124#[inline]
125pub(super) fn forget_own(_store: &SceneStore, _index: usize) {}
126
127#[cfg(feature = "window-damage")]
129pub(super) fn keeps_contained(store: &SceneStore, index: usize) -> bool {
130 store.facts.rows[index].get().contained.is_some()
131}
132
133fn forget(store: &SceneStore, index: usize, take: impl Fn(&mut RowFacts) -> bool) {
136 let mut current = Some(index);
137 while let Some(index) = current {
138 let row = &store.facts.rows[index];
139 let mut facts = row.get();
140 if !take(&mut facts) {
141 break;
142 }
143 row.set(facts);
144 current = store.tree[index].parent.map(RecordId::index);
145 }
146}
147
148#[inline]
149pub(super) fn has_hit_targets(layer: LayerRef<'_>) -> bool {
150 child_facts(layer).hit_targets
151}
152
153#[inline]
154pub(super) fn has_origin_sinks(layer: LayerRef<'_>) -> bool {
155 child_facts(layer).origin_sinks
156}
157
158#[inline]
159pub(super) fn has_space(layer: LayerRef<'_>) -> bool {
160 child_facts(layer).space
161}
162
163#[cfg(feature = "window-damage")]
166#[inline]
167pub(super) fn contained(layer: LayerRef<'_>) -> bool {
168 match layer.store.facts.rows[layer.id.index()].get().contained {
169 Some(contained) => contained,
170 None => work_out_contained(layer),
171 }
172}
173
174#[cfg(feature = "window-damage")]
175#[inline(never)]
176fn work_out_contained(layer: LayerRef<'_>) -> bool {
177 let row = &layer.store.facts.rows[layer.id.index()];
178 let contained = layer.visual_clip_rect().is_some()
179 || items_draw_within(
180 layer,
181 inflate_rect(layer.local_bounds(), CONTAINED_DRAW_SLACK),
182 );
183 row.set(RowFacts {
184 contained: Some(contained),
185 ..row.get()
186 });
187 contained
188}
189
190#[cfg(feature = "window-damage")]
191#[inline]
192pub(super) fn own_draw_facts(layer: LayerRef<'_>) -> OwnDrawFacts {
193 match layer.store.facts.rows[layer.id.index()].get().own {
194 Some(facts) => facts,
195 None => work_out_own_draw_facts(layer),
196 }
197}
198
199#[cfg(feature = "window-damage")]
200#[inline(never)]
201fn work_out_own_draw_facts(layer: LayerRef<'_>) -> OwnDrawFacts {
202 let mut facts = OwnDrawFacts::default();
203 for item in layer.own_draws() {
204 let (pixel_sensitive, drawn) = match item {
205 ItemRef::Text(_) => (true, true),
206 ItemRef::Draw(draw) => (
207 primitive_is_pixel_sensitive(&draw.primitive),
208 !matches!(
209 draw.primitive,
210 DrawPrimitive::Content | DrawPrimitive::Shadow(_)
211 ),
212 ),
213 ItemRef::Run(run) => (
214 run.summary.has_text || run.summary.has_pixel_sensitive,
215 run.summary.has_non_shadow,
216 ),
217 ItemRef::Shape(shape) => (false, shape.shape_count() > 0),
218 ItemRef::Layer(_) | ItemRef::Leaf(_) => (false, false),
219 };
220 facts.pixel_sensitive |= pixel_sensitive;
221 facts.drawn |= drawn;
222 }
223 let row = &layer.store.facts.rows[layer.id.index()];
224 row.set(RowFacts {
225 own: Some(facts),
226 ..row.get()
227 });
228 facts
229}
230
231#[cfg(feature = "window-damage")]
232fn primitive_is_pixel_sensitive(primitive: &DrawPrimitive) -> bool {
233 match primitive {
234 DrawPrimitive::Blend { primitive, .. } => primitive_is_pixel_sensitive(primitive),
235 DrawPrimitive::Image { .. } | DrawPrimitive::Text(_) => true,
236 _ => false,
237 }
238}
239
240#[inline]
241fn child_facts(layer: LayerRef<'_>) -> ChildFacts {
242 match layer.store.facts.rows[layer.id.index()].get().children {
243 Some(facts) => facts,
244 None => work_out_child_facts(layer),
245 }
246}
247
248#[inline(never)]
249fn work_out_child_facts(layer: LayerRef<'_>) -> ChildFacts {
250 let row = &layer.store.facts.rows[layer.id.index()];
251 let mut facts = ChildFacts {
252 hit_targets: layer.hit_test().is_some(),
253 origin_sinks: layer.flags().contains(RecordFlags::ORIGIN_SINKS),
254 space: layer.graphics_layer().space.is_some(),
255 };
256 for child in layer.children() {
257 let child = child_facts(child);
258 facts.hit_targets |= child.hit_targets;
259 facts.origin_sinks |= child.origin_sinks;
260 facts.space |= child.space;
261 }
262 let mut kept = row.get();
263 kept.children = Some(facts);
264 row.set(kept);
265 facts
266}
267
268#[cfg(feature = "window-damage")]
270fn items_draw_within(layer: LayerRef<'_>, bounds: Rect) -> bool {
271 layer.items().all(|item| match item {
272 ItemRef::Draw(_) => false,
273 ItemRef::Text(text) => text.draws_within(bounds),
274 ItemRef::Run(run) => run.draws_within(bounds),
275 ItemRef::Shape(shape) => shape
276 .coverage()
277 .is_none_or(|covered| rect_within(covered, bounds)),
278 ItemRef::Layer(child) => {
279 let graphics_layer = child.graphics_layer();
280 graphics_layer.shadow_elevation <= 0.0
281 && graphics_layer.render_effect.is_none()
282 && graphics_layer.backdrop_effect.is_none()
283 && rect_within(
284 child
285 .transform_to_parent()
286 .bounds_for_rect(inflate_rect(child.local_bounds(), CONTAINED_DRAW_SLACK)),
287 bounds,
288 )
289 && contained(child)
290 }
291 ItemRef::Leaf(leaf) => {
292 let origin = leaf.origin();
293 rect_within(
294 inflate_rect(leaf.node_rect(), CONTAINED_DRAW_SLACK).translate(origin.x, origin.y),
295 bounds,
296 ) && contained(leaf)
297 }
298 })
299}
300
301pub(super) fn props_inputs(props: &LayerProps) -> (bool, Option<[u32; 4]>) {
304 (
305 props.isolation.group_opacity || props.isolation.explicit_offscreen,
306 props
307 .layer_bounds
308 .map(|bounds| [bounds.x, bounds.y, bounds.width, bounds.height].map(f32::to_bits)),
309 )
310}
311
312pub(super) fn layer_inputs(layer: &GraphicsLayer) -> (bool, bool, Option<u32>, Option<u32>) {
316 (
317 layer.clip,
318 layer.shadow_elevation > 0.0,
319 layer
320 .render_effect
321 .as_ref()
322 .map(|effect| (effect.input_padding() + effect.output_padding()).to_bits()),
323 layer
324 .backdrop_effect
325 .as_ref()
326 .map(|backdrop| backdrop.output_padding().to_bits()),
327 )
328}
329
330pub(super) fn same_children(old: &[Item], new: &[Item]) -> bool {
332 fn children(items: &[Item]) -> impl Iterator<Item = Item> + '_ {
333 items
334 .iter()
335 .copied()
336 .filter(|item| item.kind() == ItemKind::Child)
337 }
338 children(old).eq(children(new))
339}