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 heap_bytes(&self) -> usize {
80 self.rows.capacity() * size_of::<Cell<RowFacts>>()
81 }
82
83 pub(super) fn trim_growth_slack(&mut self) {
84 self.rows.trim_growth_slack();
85 }
86
87 pub(super) fn truncate_rows(&mut self, len: usize) {
88 self.rows.truncate(len);
89 }
90}
91
92#[cfg(feature = "window-damage")]
95pub(super) fn forget_contained(store: &SceneStore, index: usize) {
96 forget(store, index, |row| row.contained.take().is_some());
97}
98
99pub(super) fn forget_children(store: &SceneStore, index: usize) {
102 forget(store, index, |row| row.children.take().is_some());
103}
104
105#[cfg(feature = "window-damage")]
108pub(super) fn forget_own(store: &SceneStore, index: usize) {
109 let row = &store.facts.rows[index];
110 row.set(RowFacts {
111 own: None,
112 ..row.get()
113 });
114}
115
116#[cfg(not(feature = "window-damage"))]
118#[inline]
119pub(super) fn forget_own(_store: &SceneStore, _index: usize) {}
120
121#[cfg(feature = "window-damage")]
123pub(super) fn keeps_contained(store: &SceneStore, index: usize) -> bool {
124 store.facts.rows[index].get().contained.is_some()
125}
126
127fn forget(store: &SceneStore, index: usize, take: impl Fn(&mut RowFacts) -> bool) {
130 let mut current = Some(index);
131 while let Some(index) = current {
132 let row = &store.facts.rows[index];
133 let mut facts = row.get();
134 if !take(&mut facts) {
135 break;
136 }
137 row.set(facts);
138 current = store.tree[index].parent.map(RecordId::index);
139 }
140}
141
142#[inline]
143pub(super) fn has_hit_targets(layer: LayerRef<'_>) -> bool {
144 child_facts(layer).hit_targets
145}
146
147#[inline]
148pub(super) fn has_origin_sinks(layer: LayerRef<'_>) -> bool {
149 child_facts(layer).origin_sinks
150}
151
152#[inline]
153pub(super) fn has_space(layer: LayerRef<'_>) -> bool {
154 child_facts(layer).space
155}
156
157#[cfg(feature = "window-damage")]
160#[inline]
161pub(super) fn contained(layer: LayerRef<'_>) -> bool {
162 match layer.store.facts.rows[layer.id.index()].get().contained {
163 Some(contained) => contained,
164 None => work_out_contained(layer),
165 }
166}
167
168#[cfg(feature = "window-damage")]
169#[inline(never)]
170fn work_out_contained(layer: LayerRef<'_>) -> bool {
171 let row = &layer.store.facts.rows[layer.id.index()];
172 let contained = layer.visual_clip_rect().is_some()
173 || items_draw_within(
174 layer,
175 inflate_rect(layer.local_bounds(), CONTAINED_DRAW_SLACK),
176 );
177 row.set(RowFacts {
178 contained: Some(contained),
179 ..row.get()
180 });
181 contained
182}
183
184#[cfg(feature = "window-damage")]
185#[inline]
186pub(super) fn own_draw_facts(layer: LayerRef<'_>) -> OwnDrawFacts {
187 match layer.store.facts.rows[layer.id.index()].get().own {
188 Some(facts) => facts,
189 None => work_out_own_draw_facts(layer),
190 }
191}
192
193#[cfg(feature = "window-damage")]
194#[inline(never)]
195fn work_out_own_draw_facts(layer: LayerRef<'_>) -> OwnDrawFacts {
196 let mut facts = OwnDrawFacts::default();
197 for item in layer.own_draws() {
198 let (pixel_sensitive, drawn) = match item {
199 ItemRef::Text(_) => (true, true),
200 ItemRef::Draw(draw) => (
201 primitive_is_pixel_sensitive(&draw.primitive),
202 !matches!(
203 draw.primitive,
204 DrawPrimitive::Content | DrawPrimitive::Shadow(_)
205 ),
206 ),
207 ItemRef::Run(run) => (
208 run.summary.has_text || run.summary.has_pixel_sensitive,
209 run.summary.has_non_shadow,
210 ),
211 ItemRef::Shape(shape) => (false, shape.shape_count() > 0),
212 ItemRef::Layer(_) | ItemRef::Leaf(_) => (false, false),
213 };
214 facts.pixel_sensitive |= pixel_sensitive;
215 facts.drawn |= drawn;
216 }
217 let row = &layer.store.facts.rows[layer.id.index()];
218 row.set(RowFacts {
219 own: Some(facts),
220 ..row.get()
221 });
222 facts
223}
224
225#[cfg(feature = "window-damage")]
226fn primitive_is_pixel_sensitive(primitive: &DrawPrimitive) -> bool {
227 match primitive {
228 DrawPrimitive::Blend { primitive, .. } => primitive_is_pixel_sensitive(primitive),
229 DrawPrimitive::Image { .. } | DrawPrimitive::Text(_) => true,
230 _ => false,
231 }
232}
233
234#[inline]
235fn child_facts(layer: LayerRef<'_>) -> ChildFacts {
236 match layer.store.facts.rows[layer.id.index()].get().children {
237 Some(facts) => facts,
238 None => work_out_child_facts(layer),
239 }
240}
241
242#[inline(never)]
243fn work_out_child_facts(layer: LayerRef<'_>) -> ChildFacts {
244 let row = &layer.store.facts.rows[layer.id.index()];
245 let mut facts = ChildFacts {
246 hit_targets: layer.hit_test().is_some(),
247 origin_sinks: layer.flags().contains(RecordFlags::ORIGIN_SINKS),
248 space: layer.graphics_layer().space.is_some(),
249 };
250 for child in layer.children() {
251 let child = child_facts(child);
252 facts.hit_targets |= child.hit_targets;
253 facts.origin_sinks |= child.origin_sinks;
254 facts.space |= child.space;
255 }
256 let mut kept = row.get();
257 kept.children = Some(facts);
258 row.set(kept);
259 facts
260}
261
262#[cfg(feature = "window-damage")]
264fn items_draw_within(layer: LayerRef<'_>, bounds: Rect) -> bool {
265 layer.items().all(|item| match item {
266 ItemRef::Draw(_) => false,
267 ItemRef::Text(text) => text.draws_within(bounds),
268 ItemRef::Run(run) => run.draws_within(bounds),
269 ItemRef::Shape(shape) => shape
270 .coverage()
271 .is_none_or(|covered| rect_within(covered, bounds)),
272 ItemRef::Layer(child) => {
273 let graphics_layer = child.graphics_layer();
274 graphics_layer.shadow_elevation <= 0.0
275 && graphics_layer.render_effect.is_none()
276 && graphics_layer.backdrop_effect.is_none()
277 && rect_within(
278 child
279 .transform_to_parent()
280 .bounds_for_rect(inflate_rect(child.local_bounds(), CONTAINED_DRAW_SLACK)),
281 bounds,
282 )
283 && contained(child)
284 }
285 ItemRef::Leaf(leaf) => {
286 let origin = leaf.origin();
287 rect_within(
288 inflate_rect(leaf.node_rect(), CONTAINED_DRAW_SLACK).translate(origin.x, origin.y),
289 bounds,
290 ) && contained(leaf)
291 }
292 })
293}
294
295pub(super) fn props_inputs(props: &LayerProps) -> (bool, Option<[u32; 4]>) {
298 (
299 props.isolation.group_opacity || props.isolation.explicit_offscreen,
300 props
301 .layer_bounds
302 .map(|bounds| [bounds.x, bounds.y, bounds.width, bounds.height].map(f32::to_bits)),
303 )
304}
305
306pub(super) fn layer_inputs(layer: &GraphicsLayer) -> (bool, bool, Option<u32>, Option<u32>) {
310 (
311 layer.clip,
312 layer.shadow_elevation > 0.0,
313 layer
314 .render_effect
315 .as_ref()
316 .map(|effect| (effect.input_padding() + effect.output_padding()).to_bits()),
317 layer
318 .backdrop_effect
319 .as_ref()
320 .map(|backdrop| backdrop.output_padding().to_bits()),
321 )
322}
323
324pub(super) fn same_children(old: &[Item], new: &[Item]) -> bool {
326 fn children(items: &[Item]) -> impl Iterator<Item = Item> + '_ {
327 items
328 .iter()
329 .copied()
330 .filter(|item| item.kind() == ItemKind::Child)
331 }
332 children(old).eq(children(new))
333}