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 record is a leaf whose last redraw changed only where it reached
80    // at the refresh before that redraw.
81    pub(crate) const REDRAWN_WITHIN: Self = Self(1 << 12);
82    // The flags that follow a redraw through its update, which the update's
83    // end clears.
84    #[cfg(feature = "window-damage")]
85    pub(crate) const REDRAW_STATE: Self =
86        Self(Self::REDRAW_PENDING.0 | Self::INSERTED.0 | Self::REDRAWN_WITHIN.0);
87    // The flags the store keeps itself, which a flag write leaves.
88    pub(crate) const STORE_KEPT: Self = Self(
89        Self::HAS_PROPS.0
90            | Self::HAS_GRAPHICS_LAYER.0
91            | Self::HAS_LAYER_BOUNDS.0
92            | Self::REACH_STALE.0
93            | Self::REDRAWN_WITHIN.0
94            | Self::INSERTED.0
95            | Self::REDRAW_PENDING.0,
96    );
97
98    /// Whether every flag of `other` is set.
99    pub fn contains(self, other: Self) -> bool {
100        self.0 & other.0 == other.0
101    }
102
103    /// These flags with those of `other` set.
104    #[must_use]
105    pub fn with(self, other: Self) -> Self {
106        Self(self.0 | other.0)
107    }
108
109    // These flags of those of `other`.
110    pub(crate) fn only(self, other: Self) -> Self {
111        Self(self.0 & other.0)
112    }
113
114    /// These flags with those of `other` cleared.
115    #[must_use]
116    pub fn without(self, other: Self) -> Self {
117        Self(self.0 & !other.0)
118    }
119}
120
121#[derive(Clone, Copy, Debug)]
122#[repr(C, align(32))]
123pub(crate) struct TreeRecord {
124    pub(crate) parent: Option<RecordId>,
125    pub(crate) items: Span,
126    pub(crate) kind: RecordKind,
127    #[cfg(feature = "window-damage")]
128    pub(crate) reach_state: ReachState,
129    pub(crate) flags: RecordFlags,
130    pub(crate) content_rev: SceneUpdate,
131    pub(crate) place_rev: SceneUpdate,
132    pub(crate) subtree_rev: SceneUpdate,
133    pub(crate) transform: Option<TransformId>,
134}
135
136// Without window damage a record keeps no reach, and two geometry rows fill
137// what one fills with it.
138#[derive(Clone, Copy, Debug, Default)]
139#[cfg_attr(feature = "window-damage", repr(C, align(32)))]
140#[cfg_attr(not(feature = "window-damage"), repr(C, align(16)))]
141pub(crate) struct GeoRecord {
142    pub(crate) placement: [f32; 2],
143    #[cfg(feature = "window-damage")]
144    pub(crate) reach: Rect,
145    pub(crate) size: [f32; 2],
146}
147
148#[derive(Clone, Copy, Debug)]
149pub(crate) struct WarmRecord {
150    pub(crate) node: Option<RecordNode>,
151    pub(crate) props: Option<PropsId>,
152    pub(crate) graphics_layer: Option<GraphicsLayerId>,
153    pub(crate) content_offset: [f32; 2],
154    // The marks of the record's own draws as an update last left them.
155    #[cfg(feature = "window-damage")]
156    pub(crate) own_marks: Span,
157}
158
159// The node a record draws for, in one word: a `NodeId` has no spare value,
160// so the record keeps the id plus one.
161#[derive(Clone, Copy, Debug, PartialEq, Eq)]
162pub(crate) struct RecordNode(NonZeroUsize);
163
164impl RecordNode {
165    pub(crate) fn of(node: NodeId) -> Self {
166        Self(
167            node.0
168                .checked_add(1)
169                .and_then(NonZeroUsize::new)
170                .expect("a record's node id lies below usize::MAX"),
171        )
172    }
173
174    pub(crate) fn id(self) -> NodeId {
175        NodeId(self.0.get() - 1)
176    }
177}
178
179#[derive(Clone, Copy, Debug, PartialEq, Eq)]
180pub(crate) struct PropsId(NonZeroU32);
181
182#[derive(Clone, Copy, Debug, PartialEq, Eq)]
183pub(crate) struct TransformId(NonZeroU32);
184
185#[derive(Clone, Copy, Debug, PartialEq, Eq)]
186pub(crate) struct GraphicsLayerId(NonZeroU32);
187
188macro_rules! slot_id {
189    ($name:ident) => {
190        impl $name {
191            pub(crate) fn from_index(index: usize) -> Self {
192                Self(
193                    u32::try_from(index + 1)
194                        .ok()
195                        .and_then(NonZeroU32::new)
196                        .expect("a store holds fewer than 2^32 - 1 entries of a kind"),
197                )
198            }
199
200            pub(crate) fn index(self) -> usize {
201                (self.0.get() - 1) as usize
202            }
203        }
204    };
205}
206
207slot_id!(PropsId);
208slot_id!(TransformId);
209slot_id!(GraphicsLayerId);
210
211impl GeoRecord {
212    pub(crate) fn node_rect(&self) -> Rect {
213        Rect {
214            x: 0.0,
215            y: 0.0,
216            width: self.size[0],
217            height: self.size[1],
218        }
219    }
220}
221
222// A walk reads a record's rows by its index: two rows fill a 64-byte cache
223// line. A 32-bit target keeps a record's node in four bytes of the warm row.
224const _: () = assert!(
225    size_of::<TreeRecord>() == 32,
226    "a tree record fills half a cache line"
227);
228#[cfg(feature = "window-damage")]
229const _: () = assert!(
230    size_of::<GeoRecord>() == 32,
231    "a geometry record fills half a cache line"
232);
233#[cfg(not(feature = "window-damage"))]
234const _: () = assert!(
235    size_of::<GeoRecord>() == 16,
236    "a geometry record fills a quarter of a cache line"
237);
238const _: () = assert!(
239    size_of::<WarmRecord>() <= 32,
240    "a warm record fills at most half a cache line"
241);
242
243// Whether two pairs hold the same numbers, bit for bit.
244pub(super) fn same_pair(a: [f32; 2], b: [f32; 2]) -> bool {
245    a[0].to_bits() == b[0].to_bits() && a[1].to_bits() == b[1].to_bits()
246}
247
248// Whether two transforms hold the same numbers, bit for bit.
249pub(super) fn same_bits(a: ProjectiveTransform, b: ProjectiveTransform) -> bool {
250    let bits = |transform: ProjectiveTransform| {
251        let [[a, b, c], [d, e, f], [g, h, i]] = transform.matrix();
252        [a, b, c, d, e, f, g, h, i].map(f32::to_bits)
253    };
254    bits(a) == bits(b)
255}