use crate::LayoutId;
#[derive(Clone, Copy, Default)]
struct Entry<V> {
node: u64,
pass: u32,
value: V,
}
pub(crate) struct LayoutIdMap<V> {
entries: Vec<Entry<V>>,
pass: u32,
}
impl<V> Default for LayoutIdMap<V> {
fn default() -> Self {
Self {
entries: Vec::new(),
pass: 1,
}
}
}
#[inline]
fn raw(id: &LayoutId) -> u64 {
u64::from(taffy::NodeId::from(*id))
}
#[inline]
fn slot(raw: u64) -> usize {
raw as u32 as usize
}
impl<V: Copy + Default> LayoutIdMap<V> {
#[inline]
pub(crate) fn get(&self, id: &LayoutId) -> Option<&V> {
let raw = raw(id);
self.entries
.get(slot(raw))
.filter(|entry| entry.pass == self.pass && entry.node == raw)
.map(|entry| &entry.value)
}
#[inline]
pub(crate) fn insert(&mut self, id: LayoutId, value: V) {
let raw = raw(&id);
let slot = slot(raw);
if slot >= self.entries.len() {
self.entries.resize(slot + 1, Entry::default());
}
self.entries[slot] = Entry {
node: raw,
pass: self.pass,
value,
};
}
#[inline]
pub(crate) fn remove(&mut self, id: &LayoutId) {
let raw = raw(id);
if let Some(entry) = self.entries.get_mut(slot(raw))
&& entry.node == raw
{
entry.pass = 0;
}
}
pub(crate) fn clear(&mut self) {
self.pass = self.pass.wrapping_add(1);
if self.pass == 0 {
self.entries.fill(Entry::default());
self.pass = 1;
}
}
}
#[cfg(test)]
mod tests {
use super::LayoutIdMap;
use crate::LayoutId;
use taffy::{TaffyTree, style::Style};
#[test]
fn entries_last_one_pass_and_belong_to_one_node_of_a_slot() {
let mut taffy: TaffyTree<()> = TaffyTree::new();
let first: LayoutId = taffy.new_leaf(Style::default()).unwrap().into();
let mut map = LayoutIdMap::<u32>::default();
map.insert(first, 7);
assert_eq!(map.get(&first), Some(&7));
taffy.remove(first.into()).unwrap();
let second: LayoutId = taffy.new_leaf(Style::default()).unwrap().into();
assert_ne!(first, second);
assert_eq!(map.get(&second), None);
map.remove(&second);
assert_eq!(map.get(&first), Some(&7));
map.insert(second, 8);
assert_eq!(map.get(&second), Some(&8));
assert_eq!(map.get(&first), None);
map.remove(&first);
assert_eq!(
map.get(&second),
Some(&8),
"a stale version must not remove the new slot occupant"
);
map.remove(&second);
assert_eq!(map.get(&second), None);
map.insert(second, 9);
map.clear();
assert_eq!(map.get(&second), None);
map.insert(second, 10);
assert_eq!(map.get(&second), Some(&10));
}
#[test]
fn pass_wrap_does_not_resurrect_entries_and_keeps_allocated_capacity() {
let mut taffy: TaffyTree<()> = TaffyTree::new();
let node: LayoutId = taffy.new_leaf(Style::default()).unwrap().into();
let mut map = LayoutIdMap::<u32>::default();
map.insert(node, 17);
let capacity = map.entries.capacity();
map.pass = u32::MAX;
map.clear();
assert_eq!(map.pass, 1);
assert_eq!(
map.get(&node),
None,
"pass one from before the wrap is stale"
);
assert_eq!(map.entries.capacity(), capacity);
map.insert(node, 23);
map.clear();
assert_eq!(map.get(&node), None);
assert_eq!(map.entries.capacity(), capacity);
}
fn fractional_tree() -> (super::super::TaffyLayoutEngine, LayoutId) {
use taffy::style::{Dimension, LengthPercentageAuto, Position};
let mut engine = super::super::TaffyLayoutEngine::new();
let mut child_style = Style::default();
child_style.position = Position::Absolute;
child_style.inset.left = LengthPercentageAuto::length(4.375);
child_style.inset.top = LengthPercentageAuto::length(1.125);
child_style.size.width = Dimension::length(27.75);
child_style.size.height = Dimension::length(13.25);
let child = engine.taffy.new_leaf(child_style).unwrap();
let mut parent_style = Style::default();
parent_style.position = Position::Absolute;
parent_style.inset.left = LengthPercentageAuto::length(11.25);
parent_style.inset.top = LengthPercentageAuto::length(23.5);
parent_style.size.width = Dimension::length(101.25);
parent_style.size.height = Dimension::length(54.5);
let parent = engine
.taffy
.new_with_children(parent_style, &[child])
.unwrap();
let mut root_style = Style::default();
root_style.size.width = Dimension::length(400.);
root_style.size.height = Dimension::length(200.);
let root = engine
.taffy
.new_with_children(root_style, &[parent])
.unwrap();
engine
.taffy
.compute_layout(
root,
taffy::geometry::Size {
width: taffy::style::AvailableSpace::MaxContent,
height: taffy::style::AvailableSpace::MaxContent,
},
)
.unwrap();
(engine, child.into())
}
#[test]
fn cached_fractional_bounds_preserve_absolute_edge_rounding_at_hidpi() {
use crate::{Bounds, point, px, size};
let (mut engine, child) = fractional_tree();
for scale in [1.0, 1.25, 1.5, 2.0] {
engine.absolute_layout_bounds.clear();
engine.absolute_outer_origins.clear();
let expected = Bounds::new(
point(px(16. / scale), px(25. / scale)),
size(px(27. / scale), px(13. / scale)),
);
assert_eq!(engine.layout_bounds(child, scale), expected);
assert_eq!(
engine.layout_bounds(child, scale),
expected,
"the cached read changed geometry"
);
}
}
#[test]
fn cached_bounds_keep_fractional_affine_paint_coordinates() {
use crate::{Bounds, ScaledPixels, TransformationMatrix, point, size};
let (mut engine, child) = fractional_tree();
let bounds = engine.layout_bounds(child, 1.0);
assert_eq!(engine.layout_bounds(child, 1.0), bounds);
let transform = TransformationMatrix {
rotation_scale: [[-1.5, 0.25], [-0.5, 2.0]],
translation: [-7.125, 3.875],
};
let transformed =
crate::scene::transformed_bounds(bounds.map(|value| ScaledPixels(value.0)), transform);
assert_eq!(
transformed,
Bounds::new(
point(ScaledPixels(-65.375), ScaledPixels(32.375)),
size(ScaledPixels(43.75), ScaledPixels(39.5)),
)
);
assert_eq!(
engine.layout_bounds(child, 1.0),
bounds,
"paint transforms must not enter layout coordinates"
);
}
}