use nativeview_abi::ColorRgba;
use nativeview_store::{NodeId, NodeStore};
use nativeview_tables::tables::Display;
mod build;
mod style;
pub use style::{
SceneStackingStyleSource, StackingNodeRole, StackingOverrides, StackingStyleSource,
bucket_for_sc_z, classify_node_sc_with_parent_display, resolve_node_sc_state,
};
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct ScopeIdx(pub u32);
impl ScopeIdx {
pub const NONE: Self = Self(u32::MAX);
#[inline]
pub const fn is_none(self) -> bool {
self.0 == u32::MAX
}
#[inline]
pub const fn is_some(self) -> bool {
self.0 != u32::MAX
}
#[inline]
pub const fn index(self) -> usize {
self.0 as usize
}
}
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct ZOrder(pub i32);
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct ItemCount(pub u32);
impl ItemCount {
pub const ZERO: Self = Self(0);
#[inline]
pub const fn get(self) -> u32 {
self.0
}
#[inline]
pub const fn as_usize(self) -> usize {
self.0 as usize
}
#[inline]
pub const fn is_empty(self) -> bool {
self.0 == 0
}
}
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub(crate) struct ScopeSortFlags(u8);
impl ScopeSortFlags {
const NEEDS_SORT: u8 = 1 << 0;
const HAS_NEG: u8 = 1 << 1;
#[inline]
pub(crate) const fn needs_sort(self) -> bool {
(self.0 & Self::NEEDS_SORT) != 0
}
#[inline]
pub(crate) const fn has_neg(self) -> bool {
(self.0 & Self::HAS_NEG) != 0
}
#[inline]
pub(crate) fn mark_needs_sort(&mut self) {
self.0 |= Self::NEEDS_SORT;
}
#[inline]
pub(crate) fn mark_has_neg(&mut self) {
self.0 |= Self::NEEDS_SORT | Self::HAS_NEG;
}
}
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct ClipScrollIdx(pub u32);
impl ClipScrollIdx {
pub const NONE: Self = Self(u32::MAX);
#[inline]
pub const fn is_none(self) -> bool {
self.0 == u32::MAX
}
#[inline]
pub const fn is_some(self) -> bool {
self.0 != u32::MAX
}
#[inline]
pub const fn index(self) -> usize {
self.0 as usize
}
}
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScopeKind {
RealStackingContext = 0,
PseudoStackingContext = 1,
}
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StackingBucket {
NegSc = 0,
InFlowBlock = 1,
InFlowInline = 2,
Step8 = 3,
PosSc = 4,
}
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ItemPaintPhase {
Full = 0,
BgOnly = 1,
FgOnly = 2,
}
pub const CS_CLIPS: u8 = 1 << 0;
pub const CS_HAS_SCROLL: u8 = 1 << 1;
pub const CS_IS_STICKY: u8 = 1 << 2;
pub const CS_IS_TOP_LAYER: u8 = 1 << 3;
pub const CS_IS_TRANSFORM: u8 = 1 << 4;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ClipScrollNode {
pub parent: ClipScrollIdx,
pub owner: NodeId,
pub depth: u16,
pub flags: u8,
}
impl ClipScrollNode {
#[inline]
pub const fn clips(self) -> bool {
(self.flags & CS_CLIPS) != 0
}
#[inline]
pub const fn has_scroll(self) -> bool {
(self.flags & CS_HAS_SCROLL) != 0
}
#[inline]
pub const fn is_sticky(self) -> bool {
(self.flags & CS_IS_STICKY) != 0
}
#[inline]
pub const fn is_top_layer(self) -> bool {
(self.flags & CS_IS_TOP_LAYER) != 0
}
#[inline]
pub const fn is_transform(self) -> bool {
(self.flags & CS_IS_TRANSFORM) != 0
}
}
pub const SCOPE_IS_TOP_LAYER: u8 = 1 << 0;
pub const SCOPE_HAS_SCROLL: u8 = 1 << 1;
pub const SCOPE_IS_STICKY: u8 = 1 << 2;
pub const SCOPE_IS_MODAL: u8 = 1 << 3;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StackingItem {
pub node_id: NodeId,
pub child_scope: ScopeIdx,
pub clip_scroll: ClipScrollIdx,
pub z_index: ZOrder,
pub tree_order: u32,
pub bucket: StackingBucket,
pub paint_phase: ItemPaintPhase,
pub hidden: bool,
}
impl Default for StackingItem {
fn default() -> Self {
Self {
node_id: NodeId::NULL,
child_scope: ScopeIdx::NONE,
clip_scroll: ClipScrollIdx::NONE,
z_index: ZOrder(0),
tree_order: 0,
bucket: StackingBucket::InFlowBlock,
paint_phase: ItemPaintPhase::Full,
hidden: false,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct StackingScope {
pub node_id: NodeId,
pub parent_scope: ScopeIdx,
pub clip_scroll: ClipScrollIdx,
pub min_descendant_clip: ClipScrollIdx,
pub items_start: u32,
pub items_len: ItemCount,
pub neg_end: u32,
pub kind: ScopeKind,
pub hidden: bool,
pub has_transform: bool,
pub flags: u8,
}
impl StackingScope {
#[inline]
pub const fn is_top_layer(self) -> bool {
(self.flags & SCOPE_IS_TOP_LAYER) != 0
}
#[inline]
pub const fn has_scroll(self) -> bool {
(self.flags & SCOPE_HAS_SCROLL) != 0
}
#[inline]
pub const fn is_sticky(self) -> bool {
(self.flags & SCOPE_IS_STICKY) != 0
}
#[inline]
pub const fn is_modal(self) -> bool {
(self.flags & SCOPE_IS_MODAL) != 0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TopLayerScope {
pub scope: ScopeIdx,
pub node_id: NodeId,
pub is_modal: bool,
pub backdrop: Option<ColorRgba>,
}
const _: () = assert!(std::mem::size_of::<ClipScrollNode>() <= 16);
const _: () = assert!(std::mem::size_of::<StackingItem>() <= 24);
const _: () = assert!(std::mem::size_of::<StackingScope>() <= 32);
#[derive(Debug, Clone, Copy)]
pub(crate) struct DfsFrame {
pub(crate) id: NodeId,
pub(crate) cur_sc: ScopeIdx,
pub(crate) cur_inflow_owner: ScopeIdx,
pub(crate) box_parent_display: Display,
pub(crate) box_clip: ClipScrollIdx,
pub(crate) abs_clip: ClipScrollIdx,
pub(crate) fixed_clip: ClipScrollIdx,
pub(crate) inherited_hidden: bool,
pub(crate) depth: u16,
pub(crate) is_root_of_pass: bool,
pub(crate) is_top_layer_root: bool,
}
pub(crate) const BUCKET_NONE: u8 = u8::MAX;
#[derive(Debug, Clone, Default)]
pub struct StackingPlan {
pub root: Option<ScopeIdx>,
pub top_layers: Vec<TopLayerScope>,
pub scopes: Vec<StackingScope>,
pub ordered_items: Vec<StackingItem>,
pub clips: Vec<ClipScrollNode>,
pub node_to_scope: Vec<ScopeIdx>,
pub node_to_item: Vec<u32>,
pub(crate) dfs_stack: Vec<DfsFrame>,
pub(crate) staging: Vec<(ScopeIdx, StackingItem)>,
pub(crate) scope_counts: Vec<u32>,
pub(crate) write_cursors: Vec<u32>,
pub(crate) scope_sort_flags: Vec<ScopeSortFlags>,
pub(crate) scope_last_bucket: Vec<u8>,
}
impl StackingPlan {
pub fn clear(&mut self) {
self.root = None;
self.top_layers.clear();
self.scopes.clear();
self.ordered_items.clear();
self.clips.clear();
self.node_to_scope.fill(ScopeIdx::NONE);
self.node_to_item.fill(u32::MAX);
self.dfs_stack.clear();
self.staging.clear();
self.scope_counts.clear();
self.write_cursors.clear();
self.scope_sort_flags.clear();
self.scope_last_bucket.clear();
}
#[inline]
pub fn is_empty(&self) -> bool {
self.root.is_none() && self.top_layers.is_empty()
}
#[inline]
pub fn scope_items(&self, scope: ScopeIdx) -> &[StackingItem] {
let Some(s) = self.scopes.get(scope.index()) else {
return &[];
};
let start = s.items_start as usize;
let end = start.saturating_add(s.items_len.as_usize());
self.ordered_items.get(start..end).unwrap_or(&[])
}
#[inline]
pub fn scope_for_node(&self, id: NodeId) -> Option<ScopeIdx> {
let &s = self.node_to_scope.get(id.index())?;
if s.is_none() {
return None;
}
(self.scopes.get(s.index()).is_some_and(|sc| sc.node_id == id)).then_some(s)
}
#[inline]
pub fn item_idx_for_node(&self, id: NodeId) -> Option<usize> {
let &idx = self.node_to_item.get(id.index())?;
if idx == u32::MAX {
return None;
}
let i = idx as usize;
(self.ordered_items.get(i).is_some_and(|it| it.node_id == id)).then_some(i)
}
#[inline]
pub fn clip_depth(&self, idx: ClipScrollIdx) -> u16 {
if idx.is_none() {
0
} else {
self.clips.get(idx.index()).map_or(0, |c| c.depth)
}
}
#[inline]
pub fn lca_clip(&self, mut a: ClipScrollIdx, mut b: ClipScrollIdx) -> ClipScrollIdx {
if a == b {
return a;
}
if a.is_none() || b.is_none() {
return ClipScrollIdx::NONE;
}
let mut da = self.clip_depth(a);
let mut db = self.clip_depth(b);
while da > db && a.is_some() {
a = self.clips[a.index()].parent;
da -= 1;
}
while db > da && b.is_some() {
b = self.clips[b.index()].parent;
db -= 1;
}
while a != b && a.is_some() && b.is_some() {
a = self.clips[a.index()].parent;
b = self.clips[b.index()].parent;
}
if a == b { a } else { ClipScrollIdx::NONE }
}
pub fn resort_scope_in_place(&mut self, scope: ScopeIdx) {
let Some(s) = self.scopes.get(scope.index()).copied() else {
return;
};
let start = s.items_start as usize;
let end = start.saturating_add(s.items_len.as_usize());
let Some(span) = self.ordered_items.get_mut(start..end) else {
return;
};
if span.len() > 1 {
span.sort_unstable_by_key(|it| (it.bucket as u8, it.z_index.0, it.tree_order));
}
let mut neg_end = 0u32;
for (offset, it) in span.iter().enumerate() {
if it.bucket == StackingBucket::NegSc {
neg_end = (offset + 1) as u32;
}
if it.paint_phase != ItemPaintPhase::FgOnly {
let nidx = it.node_id.index();
if nidx < self.node_to_item.len() {
self.node_to_item[nidx] = (start + offset) as u32;
}
}
}
self.scopes[scope.index()].neg_end = neg_end;
}
#[inline]
pub fn sc_item_z_changed(&self, id: NodeId, new_z: i32) -> bool {
let Some(item_idx) = self.item_idx_for_node(id) else {
return false;
};
let item = &self.ordered_items[item_idx];
let new_bucket = bucket_for_sc_z(new_z);
item.z_index.0 != new_z || item.bucket != new_bucket
}
#[inline]
pub fn update_sc_item_z(&mut self, id: NodeId, new_z: i32) -> Option<ScopeIdx> {
let item_idx = self.item_idx_for_node(id)?;
let item = &mut self.ordered_items[item_idx];
let new_bucket = bucket_for_sc_z(new_z);
if item.z_index.0 == new_z && item.bucket == new_bucket {
return None;
}
item.z_index = ZOrder(new_z);
item.bucket = new_bucket;
let child_scope = item.child_scope;
self.scopes
.get(child_scope.index())
.map(|sc| sc.parent_scope)
.filter(|s| s.is_some())
}
pub fn has_transform_mismatch<S: StackingStyleSource>(
&self,
store: &NodeStore,
style: &S,
) -> bool {
for sc in &self.scopes {
if sc.has_transform != style.has_transform(store, sc.node_id) {
return true;
}
}
for item in &self.ordered_items {
if item.child_scope.is_none() && style.has_transform(store, item.node_id) {
return true;
}
}
false
}
#[inline]
pub(crate) fn recompute_scope_neg_end(&mut self, scope: ScopeIdx) {
let s = self.scopes[scope.index()];
let start = s.items_start as usize;
let end = start + s.items_len.as_usize();
let mut neg_end = 0u32;
for (off, it) in self.ordered_items[start..end].iter().enumerate() {
if it.bucket == StackingBucket::NegSc {
neg_end = (off + 1) as u32;
} else {
break;
}
}
self.scopes[scope.index()].neg_end = neg_end;
}
#[inline]
pub(crate) fn alloc_scope(&mut self, scope: StackingScope) -> ScopeIdx {
let idx = ScopeIdx(self.scopes.len() as u32);
let nidx = scope.node_id.index();
if nidx < self.node_to_scope.len() {
self.node_to_scope[nidx] = idx;
}
self.scopes.push(scope);
self.scope_counts.push(0);
self.scope_sort_flags.push(ScopeSortFlags::default());
self.scope_last_bucket.push(BUCKET_NONE);
idx
}
#[inline]
pub(crate) fn push_item(&mut self, owner: ScopeIdx, item: StackingItem) {
let oi = owner.index();
self.staging.push((owner, item));
self.scope_counts[oi] += 1;
let b = item.bucket as u8;
let last = self.scope_last_bucket[oi];
if item.bucket == StackingBucket::NegSc {
self.scope_sort_flags[oi].mark_has_neg();
} else if item.z_index.0 != 0 || (last != BUCKET_NONE && b < last) {
self.scope_sort_flags[oi].mark_needs_sort();
}
self.scope_last_bucket[oi] = b;
let cur_min = self.scopes[oi].min_descendant_clip;
if item.clip_scroll != cur_min {
let lca = self.lca_clip(cur_min, item.clip_scroll);
self.scopes[oi].min_descendant_clip = lca;
}
}
}