use std::ops::BitOr;
use cranpose_core::{NodeId, RecordRef, SceneUpdate};
use super::{
Reach,
update_log::{UpdateLog, UpdatesKept},
};
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum RecordEvent {
Inserted,
Changed,
Moved,
Removed,
Released,
Claimed,
Redrawn(Reach),
}
impl RecordEvent {
const fn bit(self) -> u16 {
1 << match self {
Self::Inserted => 0,
Self::Changed => 1,
Self::Moved => 2,
Self::Removed => 3,
Self::Released => 4,
Self::Claimed => 5,
Self::Redrawn(_) => 6,
}
}
pub fn leaves(self) -> bool {
matches!(self, Self::Removed | Self::Released)
}
}
const ENTRY_UPDATES: UpdatesKept = UpdatesKept { most: 8, kept: 2 };
const ROWS_KEPT: usize = 4;
const ENTRIES_KEPT: usize = 256;
const STAMP_UPDATES: UpdatesKept = UpdatesKept { most: 32, kept: 8 };
const MOST_STAMPS: usize = 256;
#[cfg(feature = "window-damage")]
const REDRAWN_REACH: u16 = 1 << 7;
const STAMPED: u16 = 1 << 8;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct RecordEvents(u16);
impl RecordEvents {
pub const NONE: Self = Self(0);
pub const ALL: Self = Self(!STAMPED);
pub const LEAVES: Self = Self(RecordEvent::Removed.bit() | RecordEvent::Released.bit());
pub const STAMPS: Self = Self(STAMPED);
pub fn contains(self, event: RecordEvent) -> bool {
self.0 & event.bit() != 0
}
}
impl BitOr for RecordEvents {
type Output = Self;
fn bitor(self, other: Self) -> Self {
Self(self.0 | other.0)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct JournalEntry {
pub update: SceneUpdate,
pub event: RecordEvent,
pub record: RecordRef,
pub parent: Option<RecordRef>,
pub node: Option<NodeId>,
}
#[derive(Clone, Copy, Debug)]
pub enum JournalRead<'a> {
Changes(&'a [JournalEntry]),
Lost,
}
#[derive(Clone, Debug, Default)]
pub(super) struct Journal {
entries: UpdateLog<JournalEntry>,
stamps: UpdateLog<RecordRef>,
followed: RecordEvents,
changed: SceneUpdate,
cleared: Option<SceneUpdate>,
}
impl Default for RecordEvents {
fn default() -> Self {
Self::NONE
}
}
impl Journal {
#[inline]
pub(super) fn wants(&self, event: RecordEvent, update: SceneUpdate) -> bool {
self.cleared != Some(update) && self.follows(event)
}
#[inline]
pub(super) fn note_change(&mut self, update: SceneUpdate) {
self.changed = update;
}
#[inline]
pub(super) fn changed_since(&self, read: SceneUpdate) -> bool {
self.changed.is_after(read)
}
#[inline]
pub(super) fn follows(&self, event: RecordEvent) -> bool {
self.followed.0 & event.bit() != 0
}
#[cfg(feature = "window-damage")]
#[inline]
pub(super) fn wants_redrawn_reach(&self, update: SceneUpdate) -> bool {
self.followed.0 & REDRAWN_REACH != 0
&& self.wants(RecordEvent::Redrawn(Reach::Empty), update)
}
pub(super) fn followed(&self) -> RecordEvents {
self.followed
}
pub(super) fn follow(&mut self, events: RecordEvents) {
if !self.follows_stamps() {
self.stamps.clear();
}
self.followed = events;
}
pub(super) fn begin(&mut self, update: SceneUpdate, rows: usize) {
self.entries
.begin(update, ENTRY_UPDATES, ROWS_KEPT * rows + ENTRIES_KEPT);
if self.follows_stamps() {
self.stamps.begin(update, STAMP_UPDATES, MOST_STAMPS);
}
}
pub(super) fn log(&mut self, entry: JournalEntry) {
self.entries.push(entry, usize::MAX);
}
#[inline]
pub(super) fn follows_stamps(&self) -> bool {
self.followed.0 & STAMPED != 0
}
pub(super) fn stamp(&mut self, record: RecordRef) {
self.stamps.push(record, MOST_STAMPS);
}
pub(super) fn clear(&mut self, update: SceneUpdate) {
self.entries.clear();
self.stamps.clear();
self.cleared = Some(update);
self.changed = update;
}
pub(super) fn trim(&mut self) {
self.entries.trim();
self.stamps.trim();
}
pub(super) fn heap_bytes(&self) -> usize {
self.entries.heap_bytes() + self.stamps.heap_bytes()
}
pub(super) fn since(&self, read: SceneUpdate, now: SceneUpdate) -> JournalRead<'_> {
self.entries
.since(read, now)
.map_or(JournalRead::Lost, JournalRead::Changes)
}
pub(super) fn stamps_since(&self, read: SceneUpdate, now: SceneUpdate) -> Option<&[RecordRef]> {
self.stamps.since(read, now)
}
}