Isolate rendering state per window presentation
Verify Tessera GUI / verify (push) Failing after 7m26s

This commit is contained in:
2026-10-21 17:42:11 +08:00
parent 96319b13a3
commit f8173168fd
10 changed files with 285 additions and 88 deletions
+75 -85
View File
@@ -169,25 +169,26 @@ pub enum UiKind {
#[derive(Debug)]
struct Session {
desktop: ObjectRef,
tabs: ObjectRef,
active: Option<ObjectRef>,
pending: Option<Option<ObjectRef>>,
// Weak visual-object registry belongs to the VM, not a particular window.
nodes: Vec<WeakObjectRef>,
last_snapshot: Option<Arc<UiNode>>,
tab_ids: Vec<(WeakObjectRef, u64)>,
next_tab: u64,
last_tabs: Vec<crate::tabs::Tab>,
render_cache: crate::snapshot::RenderCache,
layout: crate::layout::LayoutRuntime,
mounts: crate::mount::MountTable,
published_version: u64,
force_frame: bool,
paint_reads: std::collections::BTreeMap<ObjectRef, u64>,
layout_reads: std::collections::BTreeMap<ObjectRef, u64>,
measure_cache: crate::source_layout::Cache,
snapshot_visited: usize,
snapshot_reused: usize,
presentations: std::collections::BTreeMap<u64, crate::presentation::Presentation>,
current: u64,
}
impl std::ops::Deref for Session {
type Target = crate::presentation::Presentation;
fn deref(&self) -> &Self::Target {
self.presentations
.get(&self.current)
.expect("selected presentation")
}
}
impl std::ops::DerefMut for Session {
fn deref_mut(&mut self) -> &mut Self::Target {
self.presentations
.get_mut(&self.current)
.expect("selected presentation")
}
}
type Shared = Arc<Mutex<Session>>;
@@ -560,30 +561,9 @@ impl NativeCallable for Function {
affected.insert(block);
}
}
if reason != "paint" {
session.measure_cache.invalidate(&affected);
for block in &affected {
session.layout.invalidate(&block.to_string());
}
for presentation in session.presentations.values_mut() {
presentation.invalidate(cx, &affected, &reason)?;
}
if reason == "content" {
session
.render_cache
.invalidate_objects(affected.iter().copied());
}
let mut visible = false;
for weak in session.mounts.active.values() {
if let Some(mount) = cx.upgrade(*weak)?
&& affected.contains(
&cx.store()
.payload::<crate::mount::Mount>(mount, "Mount")?
.block,
)
{
visible = true;
}
}
session.force_frame |= visible;
return Ok(NativeResult::Return(Value::Nil));
}
if matches!(self.operation, Mounts) {
@@ -593,21 +573,23 @@ impl NativeCallable for Function {
.lock()
.map_err(|_| invalid("UI session poisoned"))?;
let mut positions = Vec::new();
for (id, weak) in &session.mounts.active {
if let Some(object) = cx.upgrade(*weak)?
&& cx
.store()
.payload::<crate::mount::Mount>(object, "Mount")?
.block
== owner
{
positions.push((*id, object));
for (window, presentation) in &session.presentations {
for (id, weak) in &presentation.mounts.active {
if let Some(object) = cx.upgrade(*weak)?
&& cx
.store()
.payload::<crate::mount::Mount>(object, "Mount")?
.block
== owner
{
positions.push((*window, *id, object));
}
}
}
positions.sort_unstable_by_key(|(id, _)| *id);
positions.sort_unstable_by_key(|(window, id, _)| (*window, *id));
let set = crate::mount::collection(
cx,
positions.into_iter().map(|(_, object)| object).collect(),
positions.into_iter().map(|(_, _, object)| object).collect(),
session.desktop,
)?;
return Ok(NativeResult::Return(Value::Object(set)));
@@ -1081,7 +1063,10 @@ pub struct UiBridge {
session: Shared,
}
fn project_tabs(vm: &Vm, state: &mut Session) -> Result<Vec<crate::tabs::Tab>> {
fn project_tabs(
vm: &Vm,
state: &mut crate::presentation::Presentation,
) -> Result<Vec<crate::tabs::Tab>> {
let window = object(Some(&vm.get_attr(state.desktop, "window")?))?;
let pages = crate::window::pages(vm.object_store(), window)?;
let active = state.pending.unwrap_or(state.active);
@@ -1236,6 +1221,15 @@ impl UiBridge {
input
.validate()
.map_err(|message| anyhow::anyhow!(message))?;
if self
.session
.lock()
.map_err(|_| anyhow::anyhow!("UI session poisoned"))?
.current
!= input.window
{
bail!("event belongs to another window");
}
if matches!(input.kind.as_str(), "tab-activate" | "tab-close") {
return self.dispatch_tab(vm, input);
}
@@ -1327,7 +1321,9 @@ impl UiBridge {
}
}
state.nodes = live;
state.render_cache.prune(vm.object_store());
for presentation in state.presentations.values_mut() {
presentation.render_cache.prune(vm.object_store());
}
Ok(())
}
pub fn discard_pending(&self, vm: &mut Vm) -> Result<()> {
@@ -1335,10 +1331,15 @@ impl UiBridge {
.session
.lock()
.map_err(|_| anyhow::anyhow!("UI session poisoned"))?;
state.pending = None;
vm.set_attr(state.desktop, "pending", Value::Nil)?;
if state.active.is_some_and(|v| vm.object(v).is_err()) {
state.active = None;
for presentation in state.presentations.values_mut() {
presentation.pending = None;
vm.set_attr(presentation.desktop, "pending", Value::Nil)?;
if presentation
.active
.is_some_and(|page| vm.object(page).is_err())
{
presentation.active = None;
}
}
Ok(())
}
@@ -1349,15 +1350,11 @@ impl UiBridge {
.lock()
.map_err(|_| anyhow::anyhow!("UI session poisoned"))?;
let active = state.pending.unwrap_or(state.active);
if state
.paint_reads
.iter()
.chain(state.layout_reads.iter())
.any(|(object, version)| vm.change_version(*object).ok() != Some(*version))
{
state.force_frame = true;
// One VM change drain feeds every presentation before selecting a root.
// Otherwise a window refreshed first could consume another window's work.
for presentation in state.presentations.values_mut() {
presentation.observe_changes(vm, &changes);
}
state.render_cache.invalidate(changes);
let root = active
.map(|v| state.render_cache.snapshot(vm.object_store(), v))
.transpose()?;
@@ -1500,6 +1497,7 @@ impl UiBridge {
.checked_add(1)
.ok_or_else(|| anyhow::anyhow!("layout version exhausted"))?;
crate::document::Frame {
window: state.current,
layout_version: state.layout.version,
tabs: project_tabs(vm, &mut state)?,
..crate::document::Frame::default()
@@ -1556,6 +1554,11 @@ impl UiBridge {
// Native layout may replace the structural Frame; attach chrome only
// after either layout path has produced the final page geometry.
frame.tabs = tabs;
frame.window = self
.session
.lock()
.map_err(|_| anyhow::anyhow!("UI session poisoned"))?
.current;
// Capture versions before lifecycle hooks can change a dependency.
let reads = reads
.into_iter()
@@ -1623,25 +1626,12 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<UiBridge> {
let ui = vm.new_object()?;
roots.push(ui);
let session = Arc::new(Mutex::new(Session {
desktop,
tabs,
active: None,
pending: None,
nodes: vec![],
last_snapshot: None,
tab_ids: Vec::new(),
next_tab: 0,
last_tabs: Vec::new(),
render_cache: crate::snapshot::RenderCache::default(),
layout: crate::layout::LayoutRuntime::default(),
mounts: crate::mount::MountTable::default(),
published_version: 0,
force_frame: false,
paint_reads: std::collections::BTreeMap::new(),
layout_reads: std::collections::BTreeMap::new(),
measure_cache: crate::source_layout::Cache::default(),
snapshot_visited: 0,
snapshot_reused: 0,
nodes: Vec::new(),
presentations: std::collections::BTreeMap::from([(
crate::window::MAIN_WINDOW,
crate::presentation::Presentation::new(desktop, tabs),
)]),
current: crate::window::MAIN_WINDOW,
}));
for (name, operation) in [
("isInstance", Operation::IsInstance),