1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
// One record of a store as its readers see it: the layer a renderer draws,
// hit testing walks and the inspector shows, under the names a layer of the
// render graph had.
use cranpose_core::{NodeId, RecordId, RecordRef, SceneUpdate, StoreId};
use cranpose_ui::{GraphicsLayer, Point, Rect, RenderEffect, Size};
use cranpose_ui_graphics::{BlendMode, ColorFilter, CompositingStrategy};
#[cfg(feature = "window-damage")]
use super::{Extent, OwnDrawFacts, Reach};
use super::{Item, ItemKind, LayerProps, Placement, RecordFlags, RecordKind, SceneStore, ShapeRef};
use crate::{
graph::{
CachePolicy, DrawPrimitiveNode, DrawRunNode, HitTestNode, IsolationReasons, PrimitivePhase,
ProjectiveTransform, TextPrimitiveNode,
},
raster_cache::LayerRasterCacheHashes,
};
/// One record of a [`SceneStore`]: a layer, a wrapper, a draw group or a
/// leaf drawn in its parent's layer.
#[derive(Clone, Copy)]
pub struct LayerRef<'a> {
pub(super) store: &'a SceneStore,
pub(super) id: RecordId,
}
/// One item of a record, in draw order.
#[derive(Clone, Copy)]
pub enum ItemRef<'a> {
/// A child layer: a node's layer, a wrapper or a draw group.
Layer(LayerRef<'a>),
/// A layout node drawn in this record's layer, placed by a translation.
Leaf(LayerRef<'a>),
/// A draw primitive with its clip.
Draw(&'a DrawPrimitiveNode),
/// A text.
Text(&'a TextPrimitiveNode),
/// A run of one draw command's primitives.
Run(&'a DrawRunNode),
/// Shapes the store keeps in a chunk it shares among shape items.
Shape(ShapeRef<'a>),
}
/// What a record and its subtree were at their last changes, and where the
/// record sat in its parent's layer: two values taken of one record agree
/// where nothing changed in between.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct RecordChanges {
store: StoreId,
record: RecordRef,
subtree: SceneUpdate,
own: SceneUpdate,
// The record's placement in its parent and its parent's content offset,
// which together put it in its parent's layer.
placed: SceneUpdate,
parent_placed: SceneUpdate,
}
impl RecordChanges {
/// The record the changes are of, with the generation of its row.
#[inline]
pub fn record(self) -> RecordRef {
self.record
}
/// Whether nothing in the record's subtree changed between `self` and
/// `other`.
#[inline]
pub fn same_subtree(self, other: Self) -> bool {
(self.store, self.record, self.subtree) == (other.store, other.record, other.subtree)
}
/// Whether the record itself, its properties, draws and items, did not
/// change between `self` and `other`, whatever changed beneath it.
#[inline]
pub fn same_own(self, other: Self) -> bool {
(self.store, self.record, self.own) == (other.store, other.record, other.own)
}
/// Whether the record sat in one place in its parent's layer between
/// `self` and `other`: neither it nor its parent's content moved.
#[inline]
pub fn same_place(self, other: Self) -> bool {
(self.store, self.record, self.placed, self.parent_placed)
== (other.store, other.record, other.placed, other.parent_placed)
}
}
impl<'a> LayerRef<'a> {
#[inline]
pub(crate) fn new(store: &'a SceneStore, id: RecordId) -> Self {
Self { store, id }
}
/// The store that holds the record.
#[inline]
pub fn store(self) -> &'a SceneStore {
self.store
}
/// The record's id in its store.
#[inline]
pub fn id(self) -> RecordId {
self.id
}
/// What the record draws for.
#[inline]
pub fn kind(self) -> RecordKind {
self.store.tree[self.id.index()].kind
}
/// The layout node of a node's layer or leaf; `None` for a wrapper, a
/// draw group and an overlay.
#[inline]
pub fn node_id(self) -> Option<NodeId> {
match self.kind() {
RecordKind::Layer | RecordKind::Leaf => self.owner(),
RecordKind::Wrapper | RecordKind::DrawGroup => None,
}
}
/// The node whose outer draws a wrapper holds.
#[inline]
pub fn wraps(self) -> Option<NodeId> {
match self.kind() {
RecordKind::Wrapper => self.owner(),
RecordKind::Layer | RecordKind::Leaf | RecordKind::DrawGroup => None,
}
}
/// [`Self::node_id`] and [`Self::wraps`] from one read of the record.
#[inline]
pub fn identity(self) -> (Option<NodeId>, Option<NodeId>) {
let owner = self.owner();
match self.kind() {
RecordKind::Layer | RecordKind::Leaf => (owner, None),
RecordKind::Wrapper => (None, owner),
RecordKind::DrawGroup => (None, None),
}
}
/// The layout node the record draws for, whatever its kind.
#[inline]
pub fn owner(self) -> Option<NodeId> {
self.store.node(self.id)
}
/// The record that holds this one.
#[inline]
pub fn parent(self) -> Option<Self> {
self.store.tree[self.id.index()]
.parent
.map(|parent| Self::new(self.store, parent))
}
/// The properties the record keeps beyond its fixed-size parts.
#[inline]
pub fn props(self) -> Option<&'a LayerProps> {
self.store.props(self.id)
}
/// The record's flags.
#[inline]
pub fn flags(self) -> RecordFlags {
self.store.tree[self.id.index()].flags
}
/// The node's rect, `(0, 0, size)`, where its pointer handlers are.
#[inline]
pub fn node_rect(self) -> Rect {
self.store.geo[self.id.index()].node_rect()
}
/// The node's size.
#[inline]
pub fn size(self) -> Size {
let [width, height] = self.store.geo[self.id.index()].size;
Size { width, height }
}
#[expect(
clippy::inline_always,
reason = "the collect walk and the update call it for every record; out of line the gauntlet frame took 2% more instructions"
)]
/// The bounds of the layer, where its clip cuts, its transforms pivot and
/// an offscreen pass draws it.
#[inline(always)]
pub fn local_bounds(self) -> Rect {
if !self.flags().contains(RecordFlags::HAS_LAYER_BOUNDS) {
return self.node_rect();
}
self.props()
.and_then(|props| props.layer_bounds)
.unwrap_or_else(|| self.node_rect())
}
/// The node's rect when it differs from [`Self::local_bounds`].
#[inline]
pub fn node_bounds(self) -> Option<Rect> {
self.props()
.and_then(|props| props.layer_bounds)
.map(|_| self.node_rect())
}
/// The record's top left in its parent's layer: its placement and the
/// parent's content offset. A leaf's transform moves it there.
#[inline]
pub fn origin(self) -> Point {
self.store.origin(self.id)
}
/// Maps the record's space to its parent's content space, with the
/// placement, the graphics layer transform and the parent's content
/// offset applied.
#[inline]
pub fn transform_to_parent(self) -> ProjectiveTransform {
self.store.transform_to_parent(self.id)
}
/// Where the record sits in its parent's layer: [`Self::transform_to_parent`]
/// without making a transform of a point.
#[inline]
pub fn placement(self) -> Placement {
self.store.placement(self.id)
}
/// Offset of the node's content box from its origin, where its children
/// are placed.
#[inline]
pub fn content_offset(self) -> Point {
self.store.content_offset(self.id)
}
/// Where layout placed the record within its parent's content, before
/// the parent's content offset.
#[inline]
pub fn origin_in_parent(self) -> Point {
self.store.origin_in_parent(self.id)
}
/// Whether the record lies in a motion context such as an animated
/// scroll, where a renderer must not snap its content rigidly to the
/// pixel grid.
#[inline]
pub fn motion_context_animated(self) -> bool {
self.flags().contains(RecordFlags::MOTION_ANIMATED)
}
/// Whether the record moves its content as one translated block, so a
/// renderer may snap that content to the pixel grid as a whole.
#[inline]
pub fn translated_content_context(self) -> bool {
self.flags().contains(RecordFlags::TRANSLATED_CONTENT)
}
/// Translation of the content in a translated content context.
#[inline]
pub fn translated_content_offset(self) -> Point {
self.props()
.map_or_default(|props| props.translated_content_offset)
}
/// The graphics layer properties; defaults when none were set.
#[inline]
pub fn graphics_layer(self) -> &'a GraphicsLayer {
self.store
.graphics_layer(self.id)
.unwrap_or(&GraphicsLayer::DEFAULT)
}
/// Whether the record cuts its content to its local bounds.
#[inline]
pub fn clip_to_bounds(self) -> bool {
self.flags().contains(RecordFlags::CLIP_TO_BOUNDS)
}
/// Pointer handling of the node.
#[inline]
pub fn hit_test(self) -> Option<&'a HitTestNode> {
self.props().and_then(|props| props.hit_test.as_ref())
}
/// Whether the record or a record beneath it handles pointers.
#[inline]
pub fn has_hit_targets(self) -> bool {
super::facts::has_hit_targets(self)
}
/// Whether the record's subtree publishes live window origins: a text
/// field's popup anchor, a scroll container's viewport rect.
#[inline]
pub fn has_origin_sinks(self) -> bool {
super::facts::has_origin_sinks(self)
}
/// Whether the record or a record beneath it is a scene, a mesh surface
/// or a 3D transform.
#[inline]
pub fn has_space(self) -> bool {
super::facts::has_space(self)
}
/// What the record's own draws hold that decides how a renderer snaps
/// them to the pixel grid.
#[cfg(feature = "window-damage")]
#[inline]
pub fn own_draw_facts(self) -> OwnDrawFacts {
super::facts::own_draw_facts(self)
}
/// Where the record's subtree draws, in its own space: what its items
/// draw and its children's extents, within its clip. A change beneath
/// the record since [`SceneStore::refresh_reach`] leaves it unknown.
#[cfg(feature = "window-damage")]
pub fn reach(self) -> Reach {
if self.flags().contains(RecordFlags::REACH_STALE) {
return Reach::Unknown;
}
self.store.reach(self.id.index())
}
/// Whether the record is a leaf whose last redraw changed only where it
/// reached at the [`SceneStore::refresh_reach`] before that redraw. Such
/// a leaf redraws within where it drew then, as long as it did not move.
#[cfg(feature = "window-damage")]
#[inline]
pub fn redrawn_within(self) -> bool {
self.flags().contains(RecordFlags::REDRAWN_WITHIN)
}
/// Where the record's own draws reach, beside its children's, in its
/// own space and within its clip. A change beneath the record since
/// [`SceneStore::refresh_reach`] leaves it unknown.
#[cfg(feature = "window-damage")]
pub fn own_reach(self) -> Reach {
if self.flags().contains(RecordFlags::REACH_STALE) {
return Reach::Unknown;
}
let marks = self.store.warm[self.id.index()].own_marks;
super::own_marks::reach(self.store.marks.pool.span(marks)).within(self.visual_clip_rect())
}
/// What the record draws in its parent's space.
#[cfg(feature = "window-damage")]
pub fn extent(self) -> Extent {
super::reach::extent(self)
}
/// Whether everything the record's subtree draws lies within its local
/// bounds, give or take [`crate::graph::CONTAINED_DRAW_SLACK`].
#[cfg(feature = "window-damage")]
#[inline]
pub fn draws_within_bounds(self) -> bool {
super::facts::contained(self)
}
/// The reasons the record is drawn through its own offscreen buffer.
#[inline]
pub fn isolation(self) -> IsolationReasons {
self.props().map_or_default(|props| props.isolation)
}
/// Whether the record's own composition needs its content on a texture
/// of its own: a reason to draw offscreen, or a graphics layer that
/// composites apart.
#[inline]
pub fn needs_surface(self) -> bool {
// A record with neither properties nor a graphics layer draws in
// place.
let kept = self
.flags()
.only(RecordFlags::HAS_PROPS | RecordFlags::HAS_GRAPHICS_LAYER);
kept != RecordFlags::default() && self.composes_apart()
}
#[inline(never)]
fn composes_apart(self) -> bool {
let graphics = self.graphics_layer();
let isolation = self.isolation();
isolation.explicit_offscreen
|| isolation.effect
|| isolation.blend_mode
|| isolation.group_opacity
|| graphics.compositing_strategy == CompositingStrategy::Offscreen
|| crate::layer_composition::layer_requires_isolation(graphics)
}
/// The phase a draw group paints in among its node's own draws; `None`
/// for every other kind.
pub fn draw_phase(self) -> Option<PrimitivePhase> {
(self.kind() == RecordKind::DrawGroup).then(|| {
if self.flags().contains(RecordFlags::AFTER_CHILDREN) {
PrimitivePhase::AfterChildren
} else {
PrimitivePhase::BeforeChildren
}
})
}
/// Whether a renderer may keep the record's pixels between frames.
#[inline]
pub fn cache_policy(self) -> CachePolicy {
self.props().map_or_default(|props| props.cache_policy)
}
#[expect(
clippy::inline_always,
reason = "the collect walk and the update call it for every record; out of line the gauntlet frame took 2% more instructions"
)]
/// The rectangle that cuts the content and pointer hits: the local
/// bounds when the record clips to them or its graphics layer clips.
#[inline(always)]
pub fn clip_rect(self) -> Option<Rect> {
(self.clip_to_bounds() || self.graphics_layer().clip).then(|| self.local_bounds())
}
/// Where the record's own shadow is cut: its bounds when it clips its
/// content to them. A graphics-layer clip leaves the shadow whole.
#[inline]
pub fn shadow_clip(self) -> Option<Rect> {
self.clip_to_bounds().then(|| self.local_bounds())
}
#[expect(
clippy::inline_always,
reason = "the collect walk and the update call it for every record; out of line the gauntlet frame took 2% more instructions"
)]
/// Where the record's drawing is cut: its clip or, when it composites
/// through an offscreen buffer for its alpha or by request, its bounds.
#[inline(always)]
pub fn visual_clip_rect(self) -> Option<Rect> {
let clip = self.clip_rect();
if clip.is_some() {
return clip;
}
self.cut_by_isolation().then(|| self.local_bounds())
}
#[expect(
clippy::inline_always,
reason = "the collect walk calls it for every record; out of line the gauntlet frame took 2.5 M more instructions"
)]
/// [`Self::visual_clip_rect`] of the record whose graphics layer
/// `graphics` and local bounds `bounds` a reader holds already.
#[inline(always)]
pub fn visual_clip_of(self, graphics: &GraphicsLayer, bounds: Rect) -> Option<Rect> {
(self.clip_to_bounds() || graphics.clip || self.cut_by_isolation()).then_some(bounds)
}
// Whether the record composites through an offscreen buffer for its
// alpha or by request, which cuts its drawing to its bounds.
#[inline]
fn cut_by_isolation(self) -> bool {
let isolation = self.isolation();
isolation.group_opacity || isolation.explicit_offscreen
}
/// The render effect applied to the record's content.
#[inline]
pub fn effect(self) -> Option<&'a RenderEffect> {
self.graphics_layer().render_effect.as_ref()
}
/// The effect applied to what lies behind the record.
#[inline]
pub fn backdrop(self) -> Option<&'a RenderEffect> {
self.graphics_layer().backdrop_effect.as_ref()
}
/// Alpha of the record from 0.0 (transparent) to 1.0 (opaque).
#[inline]
pub fn opacity(self) -> f32 {
self.graphics_layer().alpha
}
/// How the record composites onto what lies beneath it.
#[inline]
pub fn blend_mode(self) -> BlendMode {
self.graphics_layer().blend_mode
}
/// The color filter applied to the record's content.
#[inline]
pub fn color_filter(self) -> Option<&'a ColorFilter> {
self.graphics_layer().color_filter.as_ref()
}
/// Hash of everything the record draws into its target. Equal hashes
/// mean cached pixels are still valid.
pub fn target_content_hash(self) -> u64 {
self.raster_cache_hashes().target_content
}
/// Hash of the record's content like [`Self::target_content_hash`],
/// without the translated content offset, so it stays equal while the
/// content only moves.
pub fn motion_source_content_hash(self) -> u64 {
super::content_hash::motion_source_content_hash(self)
}
/// Hash of the record's render effect, for keying cached effect output.
pub fn effect_hash(self) -> u64 {
self.raster_cache_hashes().effect
}
fn raster_cache_hashes(self) -> LayerRasterCacheHashes {
let index = self.id.index();
let subtree_rev = self.store.tree[index].subtree_rev;
self.store.hashes.hashes(index, subtree_rev, || {
super::content_hash::raster_cache_hashes(self)
})
}
/// Whether what the record draws changed in scene update `update` and in
/// the one before it, so a raster cache keyed on its content would miss
/// again.
#[cfg(feature = "window-damage")]
pub fn content_churns(self, update: SceneUpdate) -> bool {
self.props()
.is_some_and(|props| props.content_run.churns(update))
}
/// What the record and its subtree were at their last changes, and
/// where it sat in its parent's layer.
#[inline]
pub fn changes(self) -> RecordChanges {
let row = &self.store.tree[self.id.index()];
let parent_placed = row.parent.map_or_else(SceneUpdate::default, |parent| {
self.store.tree[parent.index()].place_rev
});
RecordChanges {
store: self.store.id(),
record: self.store.record_ref(self.id),
subtree: row.subtree_rev,
own: row.content_rev,
placed: row.place_rev,
parent_placed,
}
}
/// Whether the record itself, its properties, draws and items, changed
/// since `held` at most by the offset at which it moves its translated
/// content, whatever changed beneath it. A record that draws nothing of
/// its own keeps its children through such a change.
#[inline]
pub fn only_translated_since(self, held: RecordChanges) -> bool {
let now = self.changes();
now.same_own(held)
|| self
.props()
.is_some_and(|props| props.translate_run.covers(held.own, now.own))
}
/// How many items the record holds.
#[inline]
pub fn item_count(self) -> usize {
self.store.items(self.id).len()
}
/// The record's item at `index` in draw order, read without the items
/// before it.
#[inline]
pub fn item(self, index: usize) -> Option<ItemRef<'a>> {
let item = *self.store.items(self.id).get(index)?;
Some(ItemRef::of(self.store, item))
}
/// The record's items in draw order.
#[inline]
pub fn items(self) -> impl DoubleEndedIterator<Item = ItemRef<'a>> + ExactSizeIterator + 'a {
let store = self.store;
store
.items(self.id)
.iter()
.map(move |item| ItemRef::of(store, *item))
}
/// The kinds of the record's items in draw order, read without the
/// items themselves.
#[inline]
pub fn item_kinds(self) -> impl Iterator<Item = ItemKind> + 'a {
self.store.items(self.id).iter().map(|item| item.kind())
}
/// The record's own draws in draw order: its items without its children.
#[inline]
pub fn own_draws(self) -> impl Iterator<Item = ItemRef<'a>> + 'a {
let store = self.store;
store
.items(self.id)
.iter()
.filter(|item| item.kind() != ItemKind::Child)
.map(move |item| ItemRef::of(store, *item))
}
/// The child records among the items, in draw order.
#[inline]
pub fn children(self) -> impl DoubleEndedIterator<Item = Self> + 'a {
let store = self.store;
store
.items(self.id)
.iter()
.filter_map(move |item| item.as_child().map(|child| Self::new(store, child)))
}
}
impl<'a> ItemRef<'a> {
#[expect(
clippy::inline_always,
reason = "the collect walk and the update call it for every record; out of line the gauntlet frame took 2% more instructions"
)]
#[inline(always)]
pub(crate) fn of(store: &'a SceneStore, item: Item) -> Self {
match item.kind() {
ItemKind::Child => {
let child = LayerRef::new(
store,
RecordId::from_index(item.index()).expect("a child item names a record row"),
);
match child.kind() {
RecordKind::Leaf => Self::Leaf(child),
RecordKind::Layer | RecordKind::Wrapper | RecordKind::DrawGroup => {
Self::Layer(child)
}
}
}
ItemKind::Draw => Self::Draw(store.draw(item)),
ItemKind::Text => Self::Text(store.text(item)),
ItemKind::Run => Self::Run(store.run(item)),
ItemKind::Shape => Self::Shape(store.shapes(item)),
}
}
}