use std::num::{NonZeroU32, NonZeroUsize};
use cranpose_core::{NodeId, RecordId, SceneUpdate};
use cranpose_ui::Rect;
use super::Span;
use crate::graph::ProjectiveTransform;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum RecordKind {
Layer,
Wrapper,
DrawGroup,
Leaf,
}
#[cfg(feature = "window-damage")]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub(crate) enum ReachState {
Empty,
Finite,
Unknown,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct RecordFlags(u16);
impl std::ops::BitOr for RecordFlags {
type Output = Self;
fn bitor(self, other: Self) -> Self {
Self(self.0 | other.0)
}
}
impl RecordFlags {
pub const CLIP_TO_BOUNDS: Self = Self(1);
pub const MOTION_ANIMATED: Self = Self(1 << 1);
pub const TRANSLATED_CONTENT: Self = Self(1 << 2);
pub const ORIGIN_SINKS: Self = Self(1 << 3);
pub const CASTS_SHADOW: Self = Self(1 << 4);
pub const AFTER_CHILDREN: Self = Self(1 << 5);
pub const HAS_GRAPHICS_LAYER: Self = Self(1 << 6);
pub const HAS_LAYER_BOUNDS: Self = Self(1 << 7);
pub(crate) const REACH_STALE: Self = Self(1 << 8);
pub(crate) const INSERTED: Self = Self(1 << 9);
pub(crate) const REDRAW_PENDING: Self = Self(1 << 10);
pub(crate) const HAS_PROPS: Self = Self(1 << 11);
pub(crate) const STORE_KEPT: Self = Self(
Self::HAS_PROPS.0
| Self::HAS_GRAPHICS_LAYER.0
| Self::HAS_LAYER_BOUNDS.0
| Self::REACH_STALE.0
| Self::INSERTED.0
| Self::REDRAW_PENDING.0,
);
pub fn contains(self, other: Self) -> bool {
self.0 & other.0 == other.0
}
#[must_use]
pub fn with(self, other: Self) -> Self {
Self(self.0 | other.0)
}
pub(crate) fn only(self, other: Self) -> Self {
Self(self.0 & other.0)
}
#[must_use]
pub fn without(self, other: Self) -> Self {
Self(self.0 & !other.0)
}
}
#[derive(Clone, Copy, Debug)]
#[repr(C, align(32))]
pub(crate) struct TreeRecord {
pub(crate) parent: Option<RecordId>,
pub(crate) items: Span,
pub(crate) kind: RecordKind,
#[cfg(feature = "window-damage")]
pub(crate) reach_state: ReachState,
pub(crate) flags: RecordFlags,
pub(crate) content_rev: SceneUpdate,
pub(crate) place_rev: SceneUpdate,
pub(crate) subtree_rev: SceneUpdate,
pub(crate) transform: Option<TransformId>,
}
#[derive(Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "window-damage", repr(C, align(32)))]
#[cfg_attr(not(feature = "window-damage"), repr(C, align(16)))]
pub(crate) struct GeoRecord {
pub(crate) placement: [f32; 2],
#[cfg(feature = "window-damage")]
pub(crate) reach: Rect,
pub(crate) size: [f32; 2],
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct WarmRecord {
pub(crate) node: Option<RecordNode>,
pub(crate) props: Option<PropsId>,
pub(crate) graphics_layer: Option<GraphicsLayerId>,
pub(crate) content_offset: [f32; 2],
#[cfg(feature = "window-damage")]
pub(crate) own_marks: Span,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct RecordNode(NonZeroUsize);
impl RecordNode {
pub(crate) fn of(node: NodeId) -> Self {
Self(
node.0
.checked_add(1)
.and_then(NonZeroUsize::new)
.expect("a record's node id lies below usize::MAX"),
)
}
pub(crate) fn id(self) -> NodeId {
NodeId(self.0.get() - 1)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct PropsId(NonZeroU32);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct TransformId(NonZeroU32);
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct GraphicsLayerId(NonZeroU32);
macro_rules! slot_id {
($name:ident) => {
impl $name {
pub(crate) fn from_index(index: usize) -> Self {
Self(
u32::try_from(index + 1)
.ok()
.and_then(NonZeroU32::new)
.expect("a store holds fewer than 2^32 - 1 entries of a kind"),
)
}
pub(crate) fn index(self) -> usize {
(self.0.get() - 1) as usize
}
}
};
}
slot_id!(PropsId);
slot_id!(TransformId);
slot_id!(GraphicsLayerId);
impl GeoRecord {
pub(crate) fn node_rect(&self) -> Rect {
Rect {
x: 0.0,
y: 0.0,
width: self.size[0],
height: self.size[1],
}
}
}
const _: () = assert!(
size_of::<TreeRecord>() == 32,
"a tree record fills half a cache line"
);
#[cfg(feature = "window-damage")]
const _: () = assert!(
size_of::<GeoRecord>() == 32,
"a geometry record fills half a cache line"
);
#[cfg(not(feature = "window-damage"))]
const _: () = assert!(
size_of::<GeoRecord>() == 16,
"a geometry record fills a quarter of a cache line"
);
const _: () = assert!(
size_of::<WarmRecord>() <= 32,
"a warm record fills at most half a cache line"
);
pub(super) fn same_pair(a: [f32; 2], b: [f32; 2]) -> bool {
a[0].to_bits() == b[0].to_bits() && a[1].to_bits() == b[1].to_bits()
}
pub(super) fn same_bits(a: ProjectiveTransform, b: ProjectiveTransform) -> bool {
let bits = |transform: ProjectiveTransform| {
let [[a, b, c], [d, e, f], [g, h, i]] = transform.matrix();
[a, b, c, d, e, f, g, h, i].map(f32::to_bits)
};
bits(a) == bits(b)
}