Skip to main content

cranpose_render_common/scene_store/
journal.rs

1// The changes of a store's records in its latest updates, read by update
2// number: a reader keeps the version of the store it read last and reads
3// the changes of the updates after it. A reader further behind than the
4// journal keeps works from the store as it is.
5
6use std::ops::BitOr;
7
8use cranpose_core::{NodeId, RecordRef, SceneUpdate};
9
10use super::{
11    Reach,
12    update_log::{UpdateLog, UpdatesKept},
13};
14
15/// What an update did to a record.
16#[derive(Clone, Copy, Debug, PartialEq)]
17pub enum RecordEvent {
18    /// The record was made, drawing for its node.
19    Inserted,
20    /// The record's properties changed, or the order of the children it
21    /// keeps: all it draws may change.
22    Changed,
23    /// The record moved in its parent.
24    Moved,
25    /// The record and the subtree beneath it went.
26    Removed,
27    /// The record stopped drawing for the node the entry names, or as what
28    /// it drew.
29    Released,
30    /// The record started drawing for the node the entry names, or as what
31    /// it draws now.
32    Claimed,
33    /// What the record draws of its own, beside its children, changed
34    /// within the reach, in the record's space.
35    Redrawn(Reach),
36}
37
38impl RecordEvent {
39    const fn bit(self) -> u16 {
40        1 << match self {
41            Self::Inserted => 0,
42            Self::Changed => 1,
43            Self::Moved => 2,
44            Self::Removed => 3,
45            Self::Released => 4,
46            Self::Claimed => 5,
47            Self::Redrawn(_) => 6,
48        }
49    }
50
51    /// Whether the entry's node no longer draws through the record.
52    pub fn leaves(self) -> bool {
53        matches!(self, Self::Removed | Self::Released)
54    }
55}
56
57// The updates whose entries the journal keeps, and the most entries a record
58// row and a store keep: their readers read each frame. Past them a reader
59// works out what it follows again, as it would walk most of the store
60// anyway.
61const ENTRY_UPDATES: UpdatesKept = UpdatesKept { most: 8, kept: 2 };
62const ROWS_KEPT: usize = 4;
63const ENTRIES_KEPT: usize = 256;
64// The updates whose stamps the journal keeps, and the most records it keeps
65// and an update stamps: a reader of stamps may lag by a few pictures.
66const STAMP_UPDATES: UpdatesKept = UpdatesKept { most: 32, kept: 8 };
67const MOST_STAMPS: usize = 256;
68
69// The bit of a set of changes that asks for the reach of each redraw.
70#[cfg(feature = "window-damage")]
71const REDRAWN_REACH: u16 = 1 << 7;
72// The bit of a set of changes that asks for the records each update stamped.
73const STAMPED: u16 = 1 << 8;
74
75/// The kinds of change a store's journal keeps for its readers.
76#[derive(Clone, Copy, Debug, PartialEq, Eq)]
77pub struct RecordEvents(u16);
78
79impl RecordEvents {
80    /// No change.
81    pub const NONE: Self = Self(0);
82    /// Every change as an entry.
83    pub const ALL: Self = Self(!STAMPED);
84    /// The changes that take a node's record away: [`RecordEvent::Removed`]
85    /// and [`RecordEvent::Released`].
86    pub const LEAVES: Self = Self(RecordEvent::Removed.bit() | RecordEvent::Released.bit());
87    /// The records each update made, changed or moved, as handles alone.
88    pub const STAMPS: Self = Self(STAMPED);
89
90    /// Whether the set holds `event`.
91    pub fn contains(self, event: RecordEvent) -> bool {
92        self.0 & event.bit() != 0
93    }
94}
95
96impl BitOr for RecordEvents {
97    type Output = Self;
98
99    fn bitor(self, other: Self) -> Self {
100        Self(self.0 | other.0)
101    }
102}
103
104/// One change to one record.
105#[derive(Clone, Copy, Debug, PartialEq)]
106pub struct JournalEntry {
107    /// The update that made the change.
108    pub update: SceneUpdate,
109    /// What the change did.
110    pub event: RecordEvent,
111    /// The record, with the generation its row had in the change.
112    pub record: RecordRef,
113    /// The record that held it.
114    pub parent: Option<RecordRef>,
115    /// The layout node the record draws for in the change.
116    pub node: Option<NodeId>,
117}
118
119/// What a reader learns from a read.
120#[derive(Clone, Copy, Debug)]
121pub enum JournalRead<'a> {
122    /// The changes of the updates after the reader's version, oldest first,
123    /// of the kinds the store follows.
124    Changes(&'a [JournalEntry]),
125    /// The changes are not known: the store was cleared or replaced since
126    /// the reader's version, or the reader fell further behind than the
127    /// journal keeps. The reader works from the store as it is.
128    Lost,
129}
130
131#[derive(Clone, Debug, Default)]
132pub(super) struct Journal {
133    entries: UpdateLog<JournalEntry>,
134    // The records each update made, changed or moved, while the store
135    // follows the stamps.
136    stamps: UpdateLog<RecordRef>,
137    followed: RecordEvents,
138    // The latest update that changed a record, whatever the store follows.
139    changed: SceneUpdate,
140    // An update that cleared the store: what it writes after is left out,
141    // as every reader has lost the store's earlier state anyway.
142    cleared: Option<SceneUpdate>,
143}
144
145impl Default for RecordEvents {
146    fn default() -> Self {
147        Self::NONE
148    }
149}
150
151impl Journal {
152    // Whether an entry of `event` in `update` is kept: the update did not
153    // clear the store, and the store follows such entries.
154    #[inline]
155    pub(super) fn wants(&self, event: RecordEvent, update: SceneUpdate) -> bool {
156        self.cleared != Some(update) && self.follows(event)
157    }
158
159    // Notes that `update` changed a record.
160    #[inline]
161    pub(super) fn note_change(&mut self, update: SceneUpdate) {
162        self.changed = update;
163    }
164
165    // Whether an update after `read` changed a record.
166    #[inline]
167    pub(super) fn changed_since(&self, read: SceneUpdate) -> bool {
168        self.changed.is_after(read)
169    }
170
171    // Whether the store follows each change of the kind of `event`.
172    #[inline]
173    pub(super) fn follows(&self, event: RecordEvent) -> bool {
174        self.followed.0 & event.bit() != 0
175    }
176
177    // Whether the store follows where the records `update` redraws changed.
178    #[cfg(feature = "window-damage")]
179    #[inline]
180    pub(super) fn wants_redrawn_reach(&self, update: SceneUpdate) -> bool {
181        self.followed.0 & REDRAWN_REACH != 0
182            && self.wants(RecordEvent::Redrawn(Reach::Empty), update)
183    }
184
185    pub(super) fn followed(&self) -> RecordEvents {
186        self.followed
187    }
188
189    // Keeps the changes of the kinds in `events` from now on. Stamps kept
190    // before a time the store did not follow them leave a gap: they go.
191    pub(super) fn follow(&mut self, events: RecordEvents) {
192        if !self.follows_stamps() {
193            self.stamps.clear();
194        }
195        self.followed = events;
196    }
197
198    // Starts the changes of `update`, forgetting the oldest beyond what a
199    // store of `rows` record rows keeps.
200    pub(super) fn begin(&mut self, update: SceneUpdate, rows: usize) {
201        self.entries
202            .begin(update, ENTRY_UPDATES, ROWS_KEPT * rows + ENTRIES_KEPT);
203        if self.follows_stamps() {
204            self.stamps.begin(update, STAMP_UPDATES, MOST_STAMPS);
205        }
206    }
207
208    pub(super) fn log(&mut self, entry: JournalEntry) {
209        self.entries.push(entry, usize::MAX);
210    }
211
212    // Whether the store follows the records each update stamps.
213    #[inline]
214    pub(super) fn follows_stamps(&self) -> bool {
215        self.followed.0 & STAMPED != 0
216    }
217
218    // Lists `record` among those the update in progress stamped.
219    pub(super) fn stamp(&mut self, record: RecordRef) {
220        self.stamps.push(record, MOST_STAMPS);
221    }
222
223    // Forgets every change: a reader of an earlier version finds its
224    // changes lost. `update` is the store's update from now on.
225    pub(super) fn clear(&mut self, update: SceneUpdate) {
226        self.entries.clear();
227        self.stamps.clear();
228        self.cleared = Some(update);
229        self.changed = update;
230    }
231
232    // Gives back at rest the changes and the stamps it keeps: every reader
233    // that draws read the latest update before the rest ended, and one
234    // further behind works from the store as it is.
235    pub(super) fn trim(&mut self) {
236        self.entries.trim();
237        self.stamps.trim();
238    }
239
240    pub(super) fn heap_bytes(&self) -> usize {
241        self.entries.heap_bytes() + self.stamps.heap_bytes()
242    }
243
244    // The changes of the updates after `read`, the update a reader read
245    // last, when the store's update is `now`.
246    pub(super) fn since(&self, read: SceneUpdate, now: SceneUpdate) -> JournalRead<'_> {
247        self.entries
248            .since(read, now)
249            .map_or(JournalRead::Lost, JournalRead::Changes)
250    }
251
252    // The records the updates after `read` stamped, when the store's update
253    // is `now`; `None` when the journal lost some of them.
254    pub(super) fn stamps_since(&self, read: SceneUpdate, now: SceneUpdate) -> Option<&[RecordRef]> {
255        self.stamps.since(read, now)
256    }
257}