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 REDRAWN_WITHIN: Self = Self(1 << 12);
82 #[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 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 pub fn contains(self, other: Self) -> bool {
100 self.0 & other.0 == other.0
101 }
102
103 #[must_use]
105 pub fn with(self, other: Self) -> Self {
106 Self(self.0 | other.0)
107 }
108
109 pub(crate) fn only(self, other: Self) -> Self {
111 Self(self.0 & other.0)
112 }
113
114 #[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#[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 #[cfg(feature = "window-damage")]
156 pub(crate) own_marks: Span,
157}
158
159#[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
222const _: () = 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
243pub(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
248pub(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}