Retain frame and extent dependencies independently

Keep the original measurement placement separate from the assigned slot.
Validate frame and extent lengths before reusing an answer or drawing, and
represent hint-only records as having no measured answer.

Retain primitive and mask coordinates with their frame/extent reference.
Forwarded children follow a reused wrapper's placement without rerunning
valid draw bodies. Keep the single Widget::draw API.

Restore the eight failing suite cases from the region/placement prototype,
with regressions for mixed coordinate references, a changed inherited
extent, the sizing-stack fraction, and an undrawn share becoming visible.

This remains experimental: nested container updates do substantially more
work than e44dea3 despite restoring the leaf and wrapper reuse guarantees.
Do not merge it as a performance improvement.
This commit is contained in:
iris-ai committed 2026-09-17 14:50:09 -04:00
1 parent 5fcace1bfa
commit efb416bbc3
14 files changed
+548 -166

No files matched your search

+176 -58
View File
@@ -2,9 +2,9 @@
use crate::layout_diagnostics::{self as diag, Counter, ReuseOutcome, TimerKind};
use crate::ui::painter::{ask_box, declared_lens, placed_box, placed_lens};
use crate::{
ActiveData, Axis, DrawLayers, Holds, IdLike, LayoutLen, Len, MaskIdx, MoveIdx, Moves, Painter,
PixelRegion, Px, PxVec2, Rel, Size, StrongWidget, UiRegion, UiRsc, UiSpan, UiVec2, Weight,
WidgetId, Widgets,
ActiveData, Axis, DrawLayers, Holds, IdLike, LayoutHolds, LayoutLen, Len, MaskIdx, MoveIdx,
Moves, Painter, PixelRegion, Px, PxVec2, Rel, Size, StrongWidget, UiRegion, UiRsc, UiSpan,
UiVec2, Weight, WidgetId, Widgets,
util::{HashMap, Vec2},
};
@@ -26,6 +26,7 @@ pub(super) struct DrawInfo {
/// carries them unchanged while its own region is the placement inside.
pub given_len: UiVec2,
pub offer_len: UiVec2,
pub offer_placement: [Option<UiSpan>; 2],
/// This ask's box in pixels, and the offer's: one multiply from the
/// parent's own, which is where every pixel length in layout comes from.
pub px: PxVec2,
@@ -142,6 +143,7 @@ impl UiRenderState {
mask: MaskIdx::NONE,
given_len: region.size(),
offer_len: UiVec2::FULL_SIZE,
offer_placement: [None; 2],
px,
offered_px: px,
placement: [None; 2],
@@ -214,7 +216,7 @@ impl UiRenderState {
info: DrawInfo,
mut old: Option<ActiveData>,
rsc: &mut dyn UiRsc,
) -> (Size, [Holds; 2]) {
) -> (Size, LayoutHolds) {
#[cfg(feature = "layout-diagnostics")]
{
diag::bump(Counter::DrawRequests);
@@ -230,7 +232,7 @@ impl UiRenderState {
true => None,
false => self
.retained_answer(id, info)
.or_else(|| self.try_reuse(id, region, info, rsc)),
.or_else(|| self.try_reuse(id, region, info.offered_placement(), info, rsc)),
};
let answer = retained.unwrap_or_else(|| {
if old.is_none() {
@@ -252,14 +254,23 @@ impl UiRenderState {
};
self.place(id, region, placement, info, rsc);
// The answer is only reusable while both parts of the operation are:
// what the widget reported, and what it drew once placed. Both are
// ranges of the region's own lengths, since that is the box neither
// ask changes.
// On axes chosen by the parent, measurement and drawing share an
// extent. Otherwise the answer fixes the final extent as a function
// of the frame, so pull that drawing's validity back through it.
let drawing_holds = self.active[&id].holds;
let mut settled = answer;
for axis in AXES {
settled.1[axis as usize] = settled.1[axis as usize].and(drawing_holds[axis as usize]);
let n = axis as usize;
settled.1.frame[n] = settled.1.frame[n].and(drawing_holds.frame[n]);
if info.placement[n].is_some() {
settled.1.extent[n] = settled.1.extent[n].and(drawing_holds.extent[n]);
} else {
settled.1.frame[n] = settled.1.frame[n]
.and(drawing_holds.extent[n].through(placement.axis(axis).len()));
}
}
if drawing_holds.placement.is_some() && info.placement.iter().any(Option::is_some) {
settled.1.placement = Some(info.offered_placement());
}
let active = self.active.get_mut(&id).unwrap();
@@ -269,7 +280,10 @@ impl UiRenderState {
active.region = region;
active.given_len = info.given_len;
active.offer_len = info.offer_len;
active.answer = settled;
if info.placement == info.offer_placement && info.px == info.offered_px {
active.answer = Some(settled);
active.offer_placement = info.offer_placement;
}
active.decided = info.decided();
active.own_align = align;
// A subtree can be reused whole under a different parent -- same box,
@@ -283,14 +297,12 @@ impl UiRenderState {
&& let Some(old_parent) = self.active.get_mut(&old_parent)
{
old_parent.children.retain(|child| *child != id);
old_parent.inherited_children.retain(|child| *child != id);
}
settled
}
/// Puts the drawing where the answer says it goes. A drawing that never
/// read its placement is the same drawing wherever it is put, so all that
/// changes is what box the widget is recorded as occupying; one that read
/// it is drawn again, and only where the placement it read has moved.
/// Recompose retained geometry when the evaluation still holds at this extent.
fn place(
&mut self,
id: WidgetId,
@@ -299,15 +311,7 @@ impl UiRenderState {
info: DrawInfo,
rsc: &mut dyn UiRsc,
) {
let active = &self.active[&id];
// A drawing that never read its placement is the same drawing
// wherever it is put, so only what the widget is recorded as
// occupying changes; one that read it stands only for the placement
// it read. Either way the drawing still has to be where the region
// now is, which is what a retained answer on its own does not do.
let stands = !active.reads_placement || active.placement == placement;
if stands && self.try_reuse(id, region, info, rsc).is_some() {
self.active.get_mut(&id).unwrap().placement = placement;
if self.try_reuse(id, region, placement, info, rsc).is_some() {
return;
}
#[cfg(feature = "layout-diagnostics")]
@@ -325,7 +329,7 @@ impl UiRenderState {
info: DrawInfo,
old: Option<ActiveData>,
rsc: &mut dyn UiRsc,
) -> (Size, [Holds; 2]) {
) -> (Size, LayoutHolds) {
let (move_idx, local, retired_move) = match info.region_node {
// Its box becomes its movable region, so it draws in that
// region's coordinates and its box is one entry to rewrite.
@@ -339,16 +343,19 @@ impl UiRenderState {
false => (info.parent_move, region, self.slots.remove(&id)),
};
let (old_children, old_answer) = match old {
Some(old) => (old.children, Some(old.answer)),
Some(old) => (old.children, old.answer),
None => (Vec::new(), None),
};
rsc.widgets_mut().needs_redraw.remove(&id);
// A box of the offered lengths asks the offer's question wherever it
// sits, since what a drawing depends on is its lengths -- and
// equality is the comparison, these being counts of a step rather
// than floats to be compared for nearness.
// Only evaluation at the original offer establishes the children's
// offers. A placing evaluation must not overwrite that question.
let px = info.px;
let at_offer = px == info.offered_px;
let at_offer = px == info.offered_px
&& placement
== UiRegion {
x: info.offer_placement[0].unwrap_or(UiSpan::FULL),
y: info.offer_placement[1].unwrap_or(UiSpan::FULL),
};
let mut painter = Painter {
state: self,
@@ -362,6 +369,8 @@ impl UiRenderState {
id,
textures: Vec::new(),
primitives: Vec::new(),
mask_region: None,
inherited_children: Vec::new(),
children: Vec::new(),
offered: Vec::new(),
offered_px: info.offered_px,
@@ -369,6 +378,8 @@ impl UiRenderState {
size_deps: Vec::new(),
own: [Holds::ANY; 2],
under: [Holds::ANY; 2],
extent_own: [Holds::ANY; 2],
extent_under: [Holds::ANY; 2],
depth: info.depth,
move_idx,
rsc,
@@ -395,6 +406,10 @@ impl UiRenderState {
mask,
textures,
primitives,
mask_region,
inherited_children,
extent_own,
extent_under,
children,
offered: _,
offered_px: _,
@@ -432,9 +447,16 @@ impl UiRenderState {
"'{}' ({id:?}) clips to {px:?} and reports {size}",
rsc.widgets().label(id),
);
let holds = [own[0].and(under[0]), own[1].and(under[1])];
let holds = LayoutHolds {
frame: [own[0].and(under[0]), own[1].and(under[1])],
extent: [
extent_own[0].and(extent_under[0]),
extent_own[1].and(extent_under[1]),
],
placement: reads_placement.then_some(placement),
};
debug_assert!(
holds[0].contains(px.x) && holds[1].contains(px.y),
holds.contains(px, placement),
"'{}' ({id:?}) drew in {px:?}, outside the ranges it reported: {holds:?}",
rsc.widgets().label(id),
);
@@ -463,6 +485,7 @@ impl UiRenderState {
mask,
given_len: UiVec2::FULL_SIZE,
offer_len: UiVec2::FULL_SIZE,
offer_placement: [None; 2],
px,
offered_px: px,
placement: [None; 2],
@@ -477,11 +500,11 @@ impl UiRenderState {
id,
region,
placement,
reads_placement,
given_len: info.given_len,
offer_len: info.offer_len,
offer_placement: info.offer_placement,
// Whoever asked writes the answer, if this was the asking.
answer: old_answer.unwrap_or((size, holds)),
answer: old_answer,
size,
holds,
drawn: true,
@@ -489,6 +512,8 @@ impl UiRenderState {
depth: info.depth,
textures,
primitives,
mask_region,
inherited_children,
children,
size_deps,
declared: declared_lens(rsc.widgets(), id),
@@ -532,18 +557,24 @@ impl UiRenderState {
&self,
id: WidgetId,
px: PxVec2,
placement: [Option<UiSpan>; 2],
parent_move: MoveIdx,
widgets: &Widgets,
) -> Option<(Size, [Holds; 2])> {
) -> Option<(Size, LayoutHolds)> {
if widgets.needs_redraw.contains(&id) {
return None;
}
let active = self.active.get(&id)?;
let (size, holds) = active.answer;
let (size, holds) = active.answer?;
let valid = active.drawn
&& active.parent_move == parent_move
&& holds[0].contains(px.x)
&& holds[1].contains(px.y);
&& holds.contains(
px,
UiRegion {
x: placement[0].unwrap_or(UiSpan::FULL),
y: placement[1].unwrap_or(UiSpan::FULL),
},
);
valid.then_some((size, holds))
}
@@ -551,7 +582,7 @@ impl UiRenderState {
/// drawing ended up. Alignment is exactly that case: the first box is the
/// question and the smaller placed box holds the drawing. Whether the
/// answer is stale at all is its caller's question, asked once there.
fn retained_answer(&self, id: WidgetId, info: DrawInfo) -> Option<(Size, [Holds; 2])> {
fn retained_answer(&self, id: WidgetId, info: DrawInfo) -> Option<(Size, LayoutHolds)> {
let active = self.active.get(&id)?;
let has_region_node = active.move_idx != active.parent_move;
if !active.drawn
@@ -560,7 +591,11 @@ impl UiRenderState {
{
return None;
}
active.answers_at(info.px).then_some(active.answer)
let answer = active.answer?;
answer
.1
.contains(info.px, info.offered_placement())
.then_some(answer)
}
/// The pixel lengths of the box a widget was given and of the box it was
@@ -600,9 +635,10 @@ impl UiRenderState {
&mut self,
id: WidgetId,
region: UiRegion,
placement: UiRegion,
info: DrawInfo,
rsc: &mut dyn UiRsc,
) -> Option<(Size, [Holds; 2])> {
) -> Option<(Size, LayoutHolds)> {
#[cfg(feature = "layout-diagnostics")]
diag::bump(Counter::ReuseAttempts);
if rsc.widgets().needs_redraw.contains(&id) {
@@ -651,7 +687,7 @@ impl UiRenderState {
// In pixels, because `region` is a fraction of the box its parent
// drew in and that box may be what changed -- an unchanged fraction
// of a box half the size is half the widget.
if !active.holds_at(info.px) {
if !active.holds.contains(info.px, placement) {
#[cfg(feature = "layout-diagnostics")]
{
diag::bump(Counter::ReuseOutside);
@@ -659,6 +695,7 @@ impl UiRenderState {
}
return None;
}
let extent_moved = active.placement != placement;
let moved = active.region != region;
let (answer, old_region, slot) =
((active.size, active.holds), active.region, active.move_idx);
@@ -670,6 +707,9 @@ impl UiRenderState {
self.remap_subtree(id, &remap, info.parent_move, rsc);
}
}
if extent_moved {
self.reposition(id, region, placement, info, rsc);
}
self.redepth(id, info.depth);
let active = self.active.get_mut(&id).unwrap();
active.region = region;
@@ -698,6 +738,70 @@ impl UiRenderState {
Some(answer)
}
fn reposition(
&mut self,
id: WidgetId,
region: UiRegion,
placement: UiRegion,
info: DrawInfo,
rsc: &mut dyn UiRsc,
) {
let active = self.active.get_mut(&id).unwrap();
active.region = region;
active.placement = placement;
let local = if info.region_node {
UiRegion::FULL
} else {
region
};
for primitive in &active.primitives {
let handle = &primitive.handle;
*self.layers[handle.layer].region_mut(handle) =
primitive.region.resolve(local, placement);
}
if let Some(mask_region) = active.mask_region {
rsc.ui_mut().masks.get_mut(active.mask).region = mask_region.resolve(local, placement);
}
let parent_move = active.move_idx;
let mask = active.mask;
let children = active.inherited_children.len();
for index in 0..children {
let child = self.active[&id].inherited_children[index];
let active = &self.active[&child];
let (child_local, chosen) = ask_box(
UiRegion::FULL,
active.declared,
active.own_align,
[Some(placement.x), Some(placement.y)],
);
let child_placement = UiRegion {
x: chosen[0].unwrap_or(UiSpan::FULL),
y: chosen[1].unwrap_or(UiSpan::FULL),
};
let child_info = DrawInfo {
layer: active.layer,
parent: Some(id),
depth: info.depth + 1,
parent_move,
region_node: active.move_idx != active.parent_move,
mask,
given_len: child_local.size(),
offer_len: active.offer_len,
offer_placement: active.offer_placement,
px: child_local.size().to_px(info.px),
offered_px: active.offer_len.to_px(info.offered_px),
placement: chosen,
};
self.place(
child,
child_local.within(&local),
child_placement,
child_info,
rsc,
);
}
}
/// A reused subtree keeps its shape, so every widget in it moves by the
/// same amount -- and where the top of it did not move, none of it did,
/// which is what makes this free in the ordinary case.
@@ -733,11 +837,12 @@ impl UiRenderState {
self.moves.set(active.move_idx, region);
return;
}
for handle in &active.primitives {
let region = self.layers[handle.layer].region_mut(handle);
*region = remap.apply(*region);
}
active.region = remap.apply(active.region);
for primitive in &active.primitives {
let handle = &primitive.handle;
*self.layers[handle.layer].region_mut(handle) =
primitive.region.resolve(active.region, active.placement);
}
let own_mask = (active.mask != active.parent_mask).then_some(active.mask);
let children = active.children.len();
// A mask the widget set itself moves with it; one it inherited
@@ -745,7 +850,10 @@ impl UiRenderState {
if let Some(idx) = own_mask {
let mask = rsc.ui_mut().masks.get_mut(idx);
debug_assert_eq!(mask.move_idx, parent_move);
mask.region = remap.apply(mask.region);
mask.region = active
.mask_region
.unwrap()
.resolve(active.region, active.placement);
}
for index in 0..children {
let child = self.active[&id].children[index];
@@ -768,8 +876,8 @@ impl UiRenderState {
fn remove(&mut self, id: WidgetId, undraw: bool, rsc: &mut dyn UiRsc) -> Option<ActiveData> {
let mut active = self.active.remove(&id);
if let Some(active) = &mut active {
for h in &active.primitives {
let mask = self.layers.free(h);
for primitive in &active.primitives {
let mask = self.layers.free(&primitive.handle);
if mask != MaskIdx::NONE {
rsc.ui_mut().masks.remove(mask);
}
@@ -825,17 +933,19 @@ impl UiRenderState {
id,
region: UiRegion::FULL,
placement: UiRegion::FULL,
reads_placement: false,
given_len: UiVec2::FULL_SIZE,
offer_len: UiVec2::FULL_SIZE,
answer: (size, [Holds::ANY; 2]),
offer_placement: [None; 2],
answer: None,
size,
holds: [Holds::ANY; 2],
holds: LayoutHolds::ANY,
drawn: false,
parent: info.parent,
depth: info.depth,
textures: Vec::new(),
primitives: Vec::new(),
mask_region: None,
inherited_children: Vec::new(),
children: Vec::new(),
size_deps: Vec::new(),
move_idx: info.parent_move,
@@ -1007,7 +1117,7 @@ impl UiRenderState {
let declared_changed = declared_lens(rsc.widgets(), id) != active.declared;
let alignment_changed = rsc.widgets().alignment(id) != active.own_align;
if let Some(parent) = active.parent
&& (declared_changed || alignment_changed || !active.drawn)
&& (declared_changed || alignment_changed || !active.drawn || active.answer.is_none())
{
if declared_changed {
self.replace_answers = true;
@@ -1063,6 +1173,7 @@ impl UiRenderState {
mask: active.parent_mask,
given_len: active.given_len,
offer_len: active.offer_len,
offer_placement: active.offer_placement,
px: given_px,
offered_px,
// The same question its parent asked: the axes its parent chose
@@ -1075,10 +1186,17 @@ impl UiRenderState {
diag::bump(Counter::LocalRedraws);
let old = self.remove(id, false, rsc);
// `draw_inner` places the answer inside that box itself, which is the
// ask that leaves the widget where its parent put it.
let answer = self.draw_inner(id, given, info, old, rsc);
if answer != was_answer {
// Refresh the original measurement before restoring the assigned slot.
// Its lengths may differ even though the fraction reference is unchanged.
let offered = DrawInfo {
placement: info.offer_placement,
..info
};
let answer = self.draw_inner(id, given, offered, old, rsc);
if info.placement != offered.placement {
self.draw_inner(id, given, info, None, rsc);
}
if Some(answer) != was_answer {
// Its parent chose its box knowing the old answer, so it lays out
// again and chooses the box the new one asks for.
#[cfg(feature = "layout-diagnostics")]