cranpose_render_common/scene_store/
layer_ref.rs1use cranpose_core::{NodeId, RecordId, RecordRef, SceneUpdate, StoreId};
6use cranpose_ui::{GraphicsLayer, Point, Rect, RenderEffect, Size};
7use cranpose_ui_graphics::{BlendMode, ColorFilter, CompositingStrategy};
8
9#[cfg(feature = "window-damage")]
10use super::{Extent, OwnDrawFacts, Reach};
11use super::{Item, ItemKind, LayerProps, Placement, RecordFlags, RecordKind, SceneStore, ShapeRef};
12use crate::{
13 graph::{
14 CachePolicy, DrawPrimitiveNode, DrawRunNode, HitTestNode, IsolationReasons, PrimitivePhase,
15 ProjectiveTransform, TextPrimitiveNode,
16 },
17 raster_cache::LayerRasterCacheHashes,
18};
19
20#[derive(Clone, Copy)]
23pub struct LayerRef<'a> {
24 pub(super) store: &'a SceneStore,
25 pub(super) id: RecordId,
26}
27
28#[derive(Clone, Copy)]
30pub enum ItemRef<'a> {
31 Layer(LayerRef<'a>),
33 Leaf(LayerRef<'a>),
35 Draw(&'a DrawPrimitiveNode),
37 Text(&'a TextPrimitiveNode),
39 Run(&'a DrawRunNode),
41 Shape(ShapeRef<'a>),
43}
44
45#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
49pub struct RecordChanges {
50 store: StoreId,
51 record: RecordRef,
52 subtree: SceneUpdate,
53 own: SceneUpdate,
54 placed: SceneUpdate,
57 parent_placed: SceneUpdate,
58}
59
60impl RecordChanges {
61 #[inline]
63 pub fn record(self) -> RecordRef {
64 self.record
65 }
66
67 #[inline]
70 pub fn same_subtree(self, other: Self) -> bool {
71 (self.store, self.record, self.subtree) == (other.store, other.record, other.subtree)
72 }
73
74 #[inline]
77 pub fn same_own(self, other: Self) -> bool {
78 (self.store, self.record, self.own) == (other.store, other.record, other.own)
79 }
80
81 #[inline]
84 pub fn same_place(self, other: Self) -> bool {
85 (self.store, self.record, self.placed, self.parent_placed)
86 == (other.store, other.record, other.placed, other.parent_placed)
87 }
88}
89
90impl<'a> LayerRef<'a> {
91 #[inline]
92 pub(crate) fn new(store: &'a SceneStore, id: RecordId) -> Self {
93 Self { store, id }
94 }
95
96 #[inline]
98 pub fn store(self) -> &'a SceneStore {
99 self.store
100 }
101
102 #[inline]
104 pub fn id(self) -> RecordId {
105 self.id
106 }
107
108 #[inline]
110 pub fn kind(self) -> RecordKind {
111 self.store.tree[self.id.index()].kind
112 }
113
114 #[inline]
117 pub fn node_id(self) -> Option<NodeId> {
118 match self.kind() {
119 RecordKind::Layer | RecordKind::Leaf => self.owner(),
120 RecordKind::Wrapper | RecordKind::DrawGroup => None,
121 }
122 }
123
124 #[inline]
126 pub fn wraps(self) -> Option<NodeId> {
127 match self.kind() {
128 RecordKind::Wrapper => self.owner(),
129 RecordKind::Layer | RecordKind::Leaf | RecordKind::DrawGroup => None,
130 }
131 }
132
133 #[inline]
135 pub fn identity(self) -> (Option<NodeId>, Option<NodeId>) {
136 let owner = self.owner();
137 match self.kind() {
138 RecordKind::Layer | RecordKind::Leaf => (owner, None),
139 RecordKind::Wrapper => (None, owner),
140 RecordKind::DrawGroup => (None, None),
141 }
142 }
143
144 #[inline]
146 pub fn owner(self) -> Option<NodeId> {
147 self.store.node(self.id)
148 }
149
150 #[inline]
152 pub fn parent(self) -> Option<Self> {
153 self.store.tree[self.id.index()]
154 .parent
155 .map(|parent| Self::new(self.store, parent))
156 }
157
158 #[inline]
160 pub fn props(self) -> Option<&'a LayerProps> {
161 self.store.props(self.id)
162 }
163
164 #[inline]
166 pub fn flags(self) -> RecordFlags {
167 self.store.tree[self.id.index()].flags
168 }
169
170 #[inline]
172 pub fn node_rect(self) -> Rect {
173 self.store.geo[self.id.index()].node_rect()
174 }
175
176 #[inline]
178 pub fn size(self) -> Size {
179 let [width, height] = self.store.geo[self.id.index()].size;
180 Size { width, height }
181 }
182
183 #[expect(
184 clippy::inline_always,
185 reason = "the collect walk and the update call it for every record; out of line the gauntlet frame took 2% more instructions"
186 )]
187 #[inline(always)]
190 pub fn local_bounds(self) -> Rect {
191 if !self.flags().contains(RecordFlags::HAS_LAYER_BOUNDS) {
192 return self.node_rect();
193 }
194 self.props()
195 .and_then(|props| props.layer_bounds)
196 .unwrap_or_else(|| self.node_rect())
197 }
198
199 #[inline]
201 pub fn node_bounds(self) -> Option<Rect> {
202 self.props()
203 .and_then(|props| props.layer_bounds)
204 .map(|_| self.node_rect())
205 }
206
207 #[inline]
210 pub fn origin(self) -> Point {
211 self.store.origin(self.id)
212 }
213
214 #[inline]
218 pub fn transform_to_parent(self) -> ProjectiveTransform {
219 self.store.transform_to_parent(self.id)
220 }
221
222 #[inline]
225 pub fn placement(self) -> Placement {
226 self.store.placement(self.id)
227 }
228
229 #[inline]
232 pub fn content_offset(self) -> Point {
233 self.store.content_offset(self.id)
234 }
235
236 #[inline]
239 pub fn origin_in_parent(self) -> Point {
240 self.store.origin_in_parent(self.id)
241 }
242
243 #[inline]
247 pub fn motion_context_animated(self) -> bool {
248 self.flags().contains(RecordFlags::MOTION_ANIMATED)
249 }
250
251 #[inline]
254 pub fn translated_content_context(self) -> bool {
255 self.flags().contains(RecordFlags::TRANSLATED_CONTENT)
256 }
257
258 #[inline]
260 pub fn translated_content_offset(self) -> Point {
261 self.props()
262 .map(|props| props.translated_content_offset)
263 .unwrap_or_default()
264 }
265
266 #[inline]
268 pub fn graphics_layer(self) -> &'a GraphicsLayer {
269 self.store
270 .graphics_layer(self.id)
271 .unwrap_or(&GraphicsLayer::DEFAULT)
272 }
273
274 #[inline]
276 pub fn clip_to_bounds(self) -> bool {
277 self.flags().contains(RecordFlags::CLIP_TO_BOUNDS)
278 }
279
280 #[inline]
282 pub fn hit_test(self) -> Option<&'a HitTestNode> {
283 self.props().and_then(|props| props.hit_test.as_ref())
284 }
285
286 #[inline]
288 pub fn has_hit_targets(self) -> bool {
289 super::facts::has_hit_targets(self)
290 }
291
292 #[inline]
295 pub fn has_origin_sinks(self) -> bool {
296 super::facts::has_origin_sinks(self)
297 }
298
299 #[inline]
302 pub fn has_space(self) -> bool {
303 super::facts::has_space(self)
304 }
305
306 #[cfg(feature = "window-damage")]
309 #[inline]
310 pub fn own_draw_facts(self) -> OwnDrawFacts {
311 super::facts::own_draw_facts(self)
312 }
313
314 #[cfg(feature = "window-damage")]
318 pub fn reach(self) -> Reach {
319 if self.flags().contains(RecordFlags::REACH_STALE) {
320 return Reach::Unknown;
321 }
322 self.store.reach(self.id.index())
323 }
324
325 #[cfg(feature = "window-damage")]
329 pub fn own_reach(self) -> Reach {
330 if self.flags().contains(RecordFlags::REACH_STALE) {
331 return Reach::Unknown;
332 }
333 let marks = self.store.warm[self.id.index()].own_marks;
334 super::own_marks::reach(self.store.marks.pool.span(marks)).within(self.visual_clip_rect())
335 }
336
337 #[cfg(feature = "window-damage")]
339 pub fn extent(self) -> Extent {
340 super::reach::extent(self)
341 }
342
343 #[cfg(feature = "window-damage")]
346 #[inline]
347 pub fn draws_within_bounds(self) -> bool {
348 super::facts::contained(self)
349 }
350
351 #[inline]
353 pub fn isolation(self) -> IsolationReasons {
354 self.props()
355 .map(|props| props.isolation)
356 .unwrap_or_default()
357 }
358
359 #[inline]
363 pub fn needs_surface(self) -> bool {
364 let kept = self
367 .flags()
368 .only(RecordFlags::HAS_PROPS | RecordFlags::HAS_GRAPHICS_LAYER);
369 kept != RecordFlags::default() && self.composes_apart()
370 }
371
372 #[inline(never)]
373 fn composes_apart(self) -> bool {
374 let graphics = self.graphics_layer();
375 let isolation = self.isolation();
376 isolation.explicit_offscreen
377 || isolation.effect
378 || isolation.blend_mode
379 || isolation.group_opacity
380 || graphics.compositing_strategy == CompositingStrategy::Offscreen
381 || crate::layer_composition::layer_requires_isolation(graphics)
382 }
383
384 pub fn draw_phase(self) -> Option<PrimitivePhase> {
387 (self.kind() == RecordKind::DrawGroup).then(|| {
388 if self.flags().contains(RecordFlags::AFTER_CHILDREN) {
389 PrimitivePhase::AfterChildren
390 } else {
391 PrimitivePhase::BeforeChildren
392 }
393 })
394 }
395
396 #[inline]
398 pub fn cache_policy(self) -> CachePolicy {
399 self.props()
400 .map(|props| props.cache_policy)
401 .unwrap_or_default()
402 }
403
404 #[expect(
405 clippy::inline_always,
406 reason = "the collect walk and the update call it for every record; out of line the gauntlet frame took 2% more instructions"
407 )]
408 #[inline(always)]
411 pub fn clip_rect(self) -> Option<Rect> {
412 (self.clip_to_bounds() || self.graphics_layer().clip).then(|| self.local_bounds())
413 }
414
415 #[inline]
418 pub fn shadow_clip(self) -> Option<Rect> {
419 self.clip_to_bounds().then(|| self.local_bounds())
420 }
421
422 #[expect(
423 clippy::inline_always,
424 reason = "the collect walk and the update call it for every record; out of line the gauntlet frame took 2% more instructions"
425 )]
426 #[inline(always)]
429 pub fn visual_clip_rect(self) -> Option<Rect> {
430 let clip = self.clip_rect();
431 if clip.is_some() {
432 return clip;
433 }
434 self.cut_by_isolation().then(|| self.local_bounds())
435 }
436
437 #[expect(
438 clippy::inline_always,
439 reason = "the collect walk calls it for every record; out of line the gauntlet frame took 2.5 M more instructions"
440 )]
441 #[inline(always)]
444 pub fn visual_clip_of(self, graphics: &GraphicsLayer, bounds: Rect) -> Option<Rect> {
445 (self.clip_to_bounds() || graphics.clip || self.cut_by_isolation()).then_some(bounds)
446 }
447
448 #[inline]
451 fn cut_by_isolation(self) -> bool {
452 let isolation = self.isolation();
453 isolation.group_opacity || isolation.explicit_offscreen
454 }
455
456 #[inline]
458 pub fn effect(self) -> Option<&'a RenderEffect> {
459 self.graphics_layer().render_effect.as_ref()
460 }
461
462 #[inline]
464 pub fn backdrop(self) -> Option<&'a RenderEffect> {
465 self.graphics_layer().backdrop_effect.as_ref()
466 }
467
468 #[inline]
470 pub fn opacity(self) -> f32 {
471 self.graphics_layer().alpha
472 }
473
474 #[inline]
476 pub fn blend_mode(self) -> BlendMode {
477 self.graphics_layer().blend_mode
478 }
479
480 #[inline]
482 pub fn color_filter(self) -> Option<&'a ColorFilter> {
483 self.graphics_layer().color_filter.as_ref()
484 }
485
486 pub fn target_content_hash(self) -> u64 {
489 self.raster_cache_hashes().target_content
490 }
491
492 pub fn motion_source_content_hash(self) -> u64 {
496 super::content_hash::motion_source_content_hash(self)
497 }
498
499 pub fn effect_hash(self) -> u64 {
501 self.raster_cache_hashes().effect
502 }
503
504 fn raster_cache_hashes(self) -> LayerRasterCacheHashes {
505 let index = self.id.index();
506 let subtree_rev = self.store.tree[index].subtree_rev;
507 self.store.hashes.hashes(index, subtree_rev, || {
508 super::content_hash::raster_cache_hashes(self)
509 })
510 }
511
512 #[cfg(feature = "window-damage")]
516 pub fn content_churns(self, update: SceneUpdate) -> bool {
517 self.props()
518 .is_some_and(|props| props.content_run.churns(update))
519 }
520
521 #[inline]
524 pub fn changes(self) -> RecordChanges {
525 let row = &self.store.tree[self.id.index()];
526 let parent_placed = row.parent.map_or_else(SceneUpdate::default, |parent| {
527 self.store.tree[parent.index()].place_rev
528 });
529 RecordChanges {
530 store: self.store.id(),
531 record: self.store.record_ref(self.id),
532 subtree: row.subtree_rev,
533 own: row.content_rev,
534 placed: row.place_rev,
535 parent_placed,
536 }
537 }
538
539 #[inline]
544 pub fn only_translated_since(self, held: RecordChanges) -> bool {
545 let now = self.changes();
546 now.same_own(held)
547 || self
548 .props()
549 .is_some_and(|props| props.translate_run.covers(held.own, now.own))
550 }
551
552 #[inline]
554 pub fn item_count(self) -> usize {
555 self.store.items(self.id).len()
556 }
557
558 #[inline]
561 pub fn item(self, index: usize) -> Option<ItemRef<'a>> {
562 let item = *self.store.items(self.id).get(index)?;
563 Some(ItemRef::of(self.store, item))
564 }
565
566 #[inline]
568 pub fn items(self) -> impl DoubleEndedIterator<Item = ItemRef<'a>> + ExactSizeIterator + 'a {
569 let store = self.store;
570 store
571 .items(self.id)
572 .iter()
573 .map(move |item| ItemRef::of(store, *item))
574 }
575
576 #[inline]
579 pub fn item_kinds(self) -> impl Iterator<Item = ItemKind> + 'a {
580 self.store.items(self.id).iter().map(|item| item.kind())
581 }
582
583 #[inline]
585 pub fn own_draws(self) -> impl Iterator<Item = ItemRef<'a>> + 'a {
586 let store = self.store;
587 store
588 .items(self.id)
589 .iter()
590 .filter(|item| item.kind() != ItemKind::Child)
591 .map(move |item| ItemRef::of(store, *item))
592 }
593
594 #[inline]
596 pub fn children(self) -> impl DoubleEndedIterator<Item = Self> + 'a {
597 let store = self.store;
598 store
599 .items(self.id)
600 .iter()
601 .filter_map(move |item| item.as_child().map(|child| Self::new(store, child)))
602 }
603}
604
605impl<'a> ItemRef<'a> {
606 #[expect(
607 clippy::inline_always,
608 reason = "the collect walk and the update call it for every record; out of line the gauntlet frame took 2% more instructions"
609 )]
610 #[inline(always)]
611 pub(crate) fn of(store: &'a SceneStore, item: Item) -> Self {
612 match item.kind() {
613 ItemKind::Child => {
614 let child = LayerRef::new(
615 store,
616 RecordId::from_index(item.index()).expect("a child item names a record row"),
617 );
618 match child.kind() {
619 RecordKind::Leaf => Self::Leaf(child),
620 RecordKind::Layer | RecordKind::Wrapper | RecordKind::DrawGroup => {
621 Self::Layer(child)
622 }
623 }
624 }
625 ItemKind::Draw => Self::Draw(store.draw(item)),
626 ItemKind::Text => Self::Text(store.text(item)),
627 ItemKind::Run => Self::Run(store.run(item)),
628 ItemKind::Shape => Self::Shape(store.shapes(item)),
629 }
630 }
631}