cranpose_render_common/scene_store/
records.rs1use 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#[derive(Clone, Copy, Debug, PartialEq, Eq)]
14#[repr(u8)]
15pub enum RecordKind {
16 Layer,
18 Wrapper,
21 DrawGroup,
24 Leaf,
27}
28
29#[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#[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 pub const CLIP_TO_BOUNDS: Self = Self(1);
54 pub const MOTION_ANIMATED: Self = Self(1 << 1);
56 pub const TRANSLATED_CONTENT: Self = Self(1 << 2);
58 pub const ORIGIN_SINKS: Self = Self(1 << 3);
61 pub const CASTS_SHADOW: Self = Self(1 << 4);
63 pub const AFTER_CHILDREN: Self = Self(1 << 5);
66 pub const HAS_GRAPHICS_LAYER: Self = Self(1 << 6);
68 pub const HAS_LAYER_BOUNDS: Self = Self(1 << 7);
70 pub(crate) const REACH_STALE: Self = Self(1 << 8);
72 pub(crate) const INSERTED: Self = Self(1 << 9);
74 pub(crate) const REDRAW_PENDING: Self = Self(1 << 10);
77 pub(crate) const HAS_PROPS: Self = Self(1 << 11);
79 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 pub fn contains(self, other: Self) -> bool {
91 self.0 & other.0 == other.0
92 }
93
94 #[must_use]
96 pub fn with(self, other: Self) -> Self {
97 Self(self.0 | other.0)
98 }
99
100 pub(crate) fn only(self, other: Self) -> Self {
102 Self(self.0 & other.0)
103 }
104
105 #[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#[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 #[cfg(feature = "window-damage")]
147 pub(crate) own_marks: Span,
148}
149
150#[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
213const _: () = 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
234pub(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
239pub(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}