Skip to main content

cranpose_render_common/scene_store/
records.rs

1// The fixed-size records of a scene store, one of each per row: what every
2// walk reads, what damage and culling read, and what the scene update reads.
3
4use std::num::{NonZeroU32, NonZeroUsize};
5
6use cranpose_core::{NodeId, RecordId, SceneUpdate};
7use cranpose_ui::Rect;
8
9use super::Span;
10use crate::graph::ProjectiveTransform;
11
12/// What a record draws for.
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14#[repr(u8)]
15pub enum RecordKind {
16    /// The layer of a layout node, with its children.
17    Layer,
18    /// The layer around a node's layer that holds the draws chained before
19    /// the node's graphics layer.
20    Wrapper,
21    /// Part of one draw command's draws that a `DrawScope` transform or clip
22    /// groups, in the phase they paint in among its node's own draws.
23    DrawGroup,
24    /// A layout node drawn in its parent's layer: a leaf with no layer
25    /// properties, pointer handling or window origins to publish.
26    Leaf,
27}
28
29// Which reach a record's rect stands for.
30#[cfg(feature = "window-damage")]
31#[derive(Clone, Copy, Debug, PartialEq, Eq)]
32#[repr(u8)]
33pub(crate) enum ReachState {
34    Empty,
35    Finite,
36    Unknown,
37}
38
39/// Facts about a record that its walks branch on.
40#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
41pub struct RecordFlags(u16);
42
43impl std::ops::BitOr for RecordFlags {
44    type Output = Self;
45
46    fn bitor(self, other: Self) -> Self {
47        Self(self.0 | other.0)
48    }
49}
50
51impl RecordFlags {
52    /// The record cuts its content to its layer bounds.
53    pub const CLIP_TO_BOUNDS: Self = Self(1);
54    /// The record lies in a motion context such as an animated scroll.
55    pub const MOTION_ANIMATED: Self = Self(1 << 1);
56    /// The record moves its content as one translated block.
57    pub const TRANSLATED_CONTENT: Self = Self(1 << 2);
58    /// The record's node publishes live window origins: a text field's
59    /// popup anchor, a scroll container's viewport rect.
60    pub const ORIGIN_SINKS: Self = Self(1 << 3);
61    /// The record casts an elevation shadow.
62    pub const CASTS_SHADOW: Self = Self(1 << 4);
63    /// The record draws its draws after its children: a draw group of the
64    /// draws after the children.
65    pub const AFTER_CHILDREN: Self = Self(1 << 5);
66    /// The record has a graphics layer of its own.
67    pub const HAS_GRAPHICS_LAYER: Self = Self(1 << 6);
68    /// The record's layer bounds differ from its node rect.
69    pub const HAS_LAYER_BOUNDS: Self = Self(1 << 7);
70    // A change beneath the record left its reach to be worked out again.
71    pub(crate) const REACH_STALE: Self = Self(1 << 8);
72    // The update in progress made the record.
73    pub(crate) const INSERTED: Self = Self(1 << 9);
74    // The update in progress may have changed the record's own draws, which
75    // its end compares with what they were.
76    pub(crate) const REDRAW_PENDING: Self = Self(1 << 10);
77    // The record keeps properties in the store's side slab.
78    pub(crate) const HAS_PROPS: Self = Self(1 << 11);
79    // The flags the store keeps itself, which a flag write leaves.
80    pub(crate) const STORE_KEPT: Self = Self(
81        Self::HAS_PROPS.0
82            | Self::HAS_GRAPHICS_LAYER.0
83            | Self::HAS_LAYER_BOUNDS.0
84            | Self::REACH_STALE.0
85            | Self::INSERTED.0
86            | Self::REDRAW_PENDING.0,
87    );
88
89    /// Whether every flag of `other` is set.
90    pub fn contains(self, other: Self) -> bool {
91        self.0 & other.0 == other.0
92    }
93
94    /// These flags with those of `other` set.
95    #[must_use]
96    pub fn with(self, other: Self) -> Self {
97        Self(self.0 | other.0)
98    }
99
100    // These flags of those of `other`.
101    pub(crate) fn only(self, other: Self) -> Self {
102        Self(self.0 & other.0)
103    }
104
105    /// These flags with those of `other` cleared.
106    #[must_use]
107    pub fn without(self, other: Self) -> Self {
108        Self(self.0 & !other.0)
109    }
110}
111
112#[derive(Clone, Copy, Debug)]
113#[repr(C, align(32))]
114pub(crate) struct TreeRecord {
115    pub(crate) parent: Option<RecordId>,
116    pub(crate) items: Span,
117    pub(crate) kind: RecordKind,
118    #[cfg(feature = "window-damage")]
119    pub(crate) reach_state: ReachState,
120    pub(crate) flags: RecordFlags,
121    pub(crate) content_rev: SceneUpdate,
122    pub(crate) place_rev: SceneUpdate,
123    pub(crate) subtree_rev: SceneUpdate,
124    pub(crate) transform: Option<TransformId>,
125}
126
127// Without window damage a record keeps no reach, and two geometry rows fill
128// what one fills with it.
129#[derive(Clone, Copy, Debug, Default)]
130#[cfg_attr(feature = "window-damage", repr(C, align(32)))]
131#[cfg_attr(not(feature = "window-damage"), repr(C, align(16)))]
132pub(crate) struct GeoRecord {
133    pub(crate) placement: [f32; 2],
134    #[cfg(feature = "window-damage")]
135    pub(crate) reach: Rect,
136    pub(crate) size: [f32; 2],
137}
138
139#[derive(Clone, Copy, Debug)]
140pub(crate) struct WarmRecord {
141    pub(crate) node: Option<RecordNode>,
142    pub(crate) props: Option<PropsId>,
143    pub(crate) graphics_layer: Option<GraphicsLayerId>,
144    pub(crate) content_offset: [f32; 2],
145    // The marks of the record's own draws as an update last left them.
146    #[cfg(feature = "window-damage")]
147    pub(crate) own_marks: Span,
148}
149
150// The node a record draws for, in one word: a `NodeId` has no spare value,
151// so the record keeps the id plus one.
152#[derive(Clone, Copy, Debug, PartialEq, Eq)]
153pub(crate) struct RecordNode(NonZeroUsize);
154
155impl RecordNode {
156    pub(crate) fn of(node: NodeId) -> Self {
157        Self(
158            node.0
159                .checked_add(1)
160                .and_then(NonZeroUsize::new)
161                .expect("a record's node id lies below usize::MAX"),
162        )
163    }
164
165    pub(crate) fn id(self) -> NodeId {
166        NodeId(self.0.get() - 1)
167    }
168}
169
170#[derive(Clone, Copy, Debug, PartialEq, Eq)]
171pub(crate) struct PropsId(NonZeroU32);
172
173#[derive(Clone, Copy, Debug, PartialEq, Eq)]
174pub(crate) struct TransformId(NonZeroU32);
175
176#[derive(Clone, Copy, Debug, PartialEq, Eq)]
177pub(crate) struct GraphicsLayerId(NonZeroU32);
178
179macro_rules! slot_id {
180    ($name:ident) => {
181        impl $name {
182            pub(crate) fn from_index(index: usize) -> Self {
183                Self(
184                    u32::try_from(index + 1)
185                        .ok()
186                        .and_then(NonZeroU32::new)
187                        .expect("a store holds fewer than 2^32 - 1 entries of a kind"),
188                )
189            }
190
191            pub(crate) fn index(self) -> usize {
192                (self.0.get() - 1) as usize
193            }
194        }
195    };
196}
197
198slot_id!(PropsId);
199slot_id!(TransformId);
200slot_id!(GraphicsLayerId);
201
202impl GeoRecord {
203    pub(crate) fn node_rect(&self) -> Rect {
204        Rect {
205            x: 0.0,
206            y: 0.0,
207            width: self.size[0],
208            height: self.size[1],
209        }
210    }
211}
212
213// A walk reads a record's rows by its index: two rows fill a 64-byte cache
214// line. A 32-bit target keeps a record's node in four bytes of the warm row.
215const _: () = assert!(
216    size_of::<TreeRecord>() == 32,
217    "a tree record fills half a cache line"
218);
219#[cfg(feature = "window-damage")]
220const _: () = assert!(
221    size_of::<GeoRecord>() == 32,
222    "a geometry record fills half a cache line"
223);
224#[cfg(not(feature = "window-damage"))]
225const _: () = assert!(
226    size_of::<GeoRecord>() == 16,
227    "a geometry record fills a quarter of a cache line"
228);
229const _: () = assert!(
230    size_of::<WarmRecord>() <= 32,
231    "a warm record fills at most half a cache line"
232);
233
234// Whether two pairs hold the same numbers, bit for bit.
235pub(super) fn same_pair(a: [f32; 2], b: [f32; 2]) -> bool {
236    a[0].to_bits() == b[0].to_bits() && a[1].to_bits() == b[1].to_bits()
237}
238
239// Whether two transforms hold the same numbers, bit for bit.
240pub(super) fn same_bits(a: ProjectiveTransform, b: ProjectiveTransform) -> bool {
241    let bits = |transform: ProjectiveTransform| {
242        let [[a, b, c], [d, e, f], [g, h, i]] = transform.matrix();
243        [a, b, c, d, e, f, g, h, i].map(f32::to_bits)
244    };
245    bits(a) == bits(b)
246}