Truly independent web browser
LibWeb: Keep the layout arena's trace and SVG paint resources plain
Aliaksandr Kalenik541626650199
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rsindex b7219fbc0a5..3e31d4a8246 100644--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs@@ -3522,7 +3522,7 @@ impl LayoutStageJob {}notifications.prepend_layout_messages(messages);let (arena, scratch) = state.arena_mut_and_layout_scratch();- scratch.layout_trace.hand_over_to(&arena.layout_trace);+ scratch.layout_trace.hand_back_to(&mut arena.layout_trace);scratch.end_layout_pass(arena);arena.reset_layout_update_flags_in_subtree(root);arena.leave_active_layout_pass();@@ -3659,7 +3659,7 @@ fn prepare_layout_stage(state: &mut super::ArenaHandle, kind: LayoutStageKind, r}drop_intrinsic_size_cache_entries_of_released_slots(arena, scratch);scratch.apply_memo_invalidations(arena);- arena.layout_trace.hand_over_to(&scratch.layout_trace);+ scratch.layout_trace.take_over_from(&mut arena.layout_trace);arena.begin_active_layout_pass();}diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rsindex b26a100dfc1..814c5e8528b 100644--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs@@ -1250,7 +1250,7 @@ pub(crate) struct LayoutNodeArena {next_rows_built_for_same_node: Vec<NodeSlotId>,/// What writes since the layout stage last looked did to the boxes its run memos hold entries of.memo_invalidations: super::memo_invalidations::MemoInvalidations,- pub(super) layout_trace: super::trace::LayoutTrace,+ pub(super) layout_trace: super::trace::TraceRecord,pub(crate) paintable_rows: crate::painting::paintable_rows::PaintableRowStore,paint_state: RefCell<crate::painting::paint_state::PaintState>,// Hit testing can measure overflow and invalidate painting state while querying this list.@@ -1365,7 +1365,7 @@ impl LayoutNodeArena {svg_paint_resources: crate::painting::svg_paint_resources::SvgPaintResources::default(),next_rows_built_for_same_node: Vec::new(),memo_invalidations: Default::default(),- layout_trace: super::trace::LayoutTrace::default(),+ layout_trace: super::trace::TraceRecord::default(),paintable_rows: crate::painting::paintable_rows::PaintableRowStore::default(),paint_state: RefCell::new(crate::painting::paint_state::PaintState::default()),scrollable_overflow: Default::default(),@@ -2893,7 +2893,7 @@ impl LayoutNodeArena {self.set_needs_layout_update(slot, !confined);}- pub(crate) fn enroll_node_for_svg_paint_resources_sync(&self, id: NodeSlotId) {+ pub(crate) fn enroll_node_for_svg_paint_resources_sync(&mut self, id: NodeSlotId) {use crate::painting::svg_paint_resources::SvgPaintResourceKind;let Some(style) = self.node_style_if_live(id) else {return;@@ -2922,6 +2922,10 @@ impl LayoutNodeArena {&self.svg_paint_resources}+ pub(crate) fn svg_paint_resources_mut(&mut self) -> &mut crate::painting::svg_paint_resources::SvgPaintResources {+ &mut self.svg_paint_resources+ }+/// A fork of the arena, linked to `engine`, the fork of its style engine: its chunks have addresses of their own,/// and the flags the arena shares with the host are its own. See [`crate::fork`].pub(crate) fn fork(&self, engine: crate::css::style::StyleEngineHandle) -> Self {diff --git a/Libraries/LibWeb/Rust/src/layout/trace.rs b/Libraries/LibWeb/Rust/src/layout/trace.rsindex e75b8c599a3..ac33480179c 100644--- a/Libraries/LibWeb/Rust/src/layout/trace.rs+++ b/Libraries/LibWeb/Rust/src/layout/trace.rs@@ -38,7 +38,12 @@ struct Line {}/// Observation belongs to the document, not to a single pass: geometry reads and-/// style stabilization can cause several passes within one measured mutation.+/// style stabilization can cause several passes within one measured mutation. The document keeps+/// the trace here between passes, and the layout stage's recorder takes it over for each pass.+#[derive(Clone, Default)]+pub(crate) struct TraceRecord(Option<Trace>);++/// What a layout pass records its events into: the trace it took over from the document.#[derive(Clone, Default)]pub(crate) struct LayoutTrace(RefCell<Option<Trace>>);@@ -63,40 +68,47 @@ impl Drop for Scope<'_> {}}-impl LayoutTrace {- pub(super) fn begin(&self) {- assert!(self.0.borrow().as_ref().is_none_or(|trace| trace.depth == 0));- *self.0.borrow_mut() = Some(Trace {- lines: Vec::new(),- depth: 0,- });+fn begun_trace(previous: Option<&Trace>) -> Trace {+ assert!(previous.is_none_or(|trace| trace.depth == 0));+ Trace {+ lines: Vec::new(),+ depth: 0,}+}- pub(super) fn take(&self) -> String {- let Some(trace) = self.0.borrow_mut().take() else {- return String::new();- };- assert_eq!(trace.depth, 0, "incomplete layout trace");- let mut text = String::new();- for line in trace.lines {- writeln!(- text,- "{}{}{}{}",- " ".repeat(line.depth),- line.prefix,- line.owner_name.unwrap_or_default(),- line.text- )- .unwrap();- }- text+fn text_of(trace: Option<Trace>) -> String {+ let Some(trace) = trace else {+ return String::new();+ };+ assert_eq!(trace.depth, 0, "incomplete layout trace");+ let mut text = String::new();+ for line in trace.lines {+ writeln!(+ text,+ "{}{}{}{}",+ " ".repeat(line.depth),+ line.prefix,+ line.owner_name.unwrap_or_default(),+ line.text+ )+ .unwrap();+ }+ text+}++impl TraceRecord {+ pub(super) fn begin(&mut self) {+ self.0 = Some(begun_trace(self.0.as_ref()));+ }++ pub(super) fn take(&mut self) -> String {+ text_of(self.0.take())}/// The boxes the traced events name and that are not named yet, each by its line. A box whose row says what it is/// is named from the row; the host describes the rest.fn unnamed_owners(&self, arena: &LayoutNodeArena) -> Vec<UnnamedOwner> {- let state = self.0.borrow();- let Some(trace) = state.as_ref() else {+ let Some(trace) = self.0.as_ref() else {return Vec::new();};trace@@ -116,15 +128,27 @@ impl LayoutTrace {}/// Names the boxes of the traced events at the lines `names` gives.- fn name_owners(&self, names: Vec<(usize, String)>) {- let mut state = self.0.borrow_mut();- let Some(trace) = state.as_mut() else {+ fn name_owners(&mut self, names: Vec<(usize, String)>) {+ let Some(trace) = self.0.as_mut() else {return;};for (line, name) in names {trace.lines[line].owner_name = Some(name);}}+}++impl LayoutTrace {+ #[cfg(test)]+ fn begin(&self) {+ let begun = begun_trace(self.0.borrow().as_ref());+ *self.0.borrow_mut() = Some(begun);+ }++ #[cfg(test)]+ fn take(&self) -> String {+ text_of(self.0.borrow_mut().take())+ }fn scope(&self,@@ -146,9 +170,14 @@ impl LayoutTrace {Some(Scope { trace: self, line })}- /// Moves what this trace holds to `recorder`, which goes on with it.- pub(super) fn hand_over_to(&self, recorder: &LayoutTrace) {- *recorder.0.borrow_mut() = self.0.take();+ /// Takes over the trace the document kept in `record`, for a pass to go on with it.+ pub(super) fn take_over_from(&self, record: &mut TraceRecord) {+ *self.0.borrow_mut() = record.0.take();+ }++ /// Hands the trace back to `record` once the pass is over.+ pub(super) fn hand_back_to(&self, record: &mut TraceRecord) {+ record.0 = self.0.take();}/// Runs `untraced` with nothing it does traced.@@ -286,7 +315,7 @@ fn describe_node(impl LayoutNodeArena {/// Names the boxes of the traced events at the lines `names` gives.- pub(crate) fn name_layout_trace_owners(&self, names: Vec<(usize, String)>) {+ pub(crate) fn name_layout_trace_owners(&mut self, names: Vec<(usize, String)>) {self.layout_trace.name_owners(names);}}@@ -405,11 +434,10 @@ mod tests {arena.layout_trace.begin();arena.begin_active_layout_pass();- drop(- arena- .layout_trace- .run(owner.slot, FormattingContextType::Block, || "RUN"),- );+ let recorder = LayoutTrace::default();+ recorder.take_over_from(&mut arena.layout_trace);+ drop(recorder.run(owner.slot, FormattingContextType::Block, || "RUN"));+ recorder.hand_back_to(&mut arena.layout_trace);assert!(arena.leave_active_layout_pass());let names = arena.layout_tracediff --git a/Libraries/LibWeb/Rust/src/painting/paint_changes.rs b/Libraries/LibWeb/Rust/src/painting/paint_changes.rsindex 3d28ecc383c..2868fc1b385 100644--- a/Libraries/LibWeb/Rust/src/painting/paint_changes.rs+++ b/Libraries/LibWeb/Rust/src/painting/paint_changes.rs@@ -220,7 +220,7 @@ impl PaintChange {Self::InvalidateAllPaintCaches => arena.push_all_paint_damage(),Self::RequestFullVisualContextRebuild(scope) => arena.request_full_visual_context_rebuild(scope),Self::SvgPaintResourcesChanged => {- arena.svg_paint_resources().note_changed();+ arena.svg_paint_resources_mut().note_changed();}Self::SetRecordingTraceEnabled(enabled) => arena.paint_state().borrow_mut().trace_recordings = enabled,Self::TakeInRecording {diff --git a/Libraries/LibWeb/Rust/src/painting/paint_passes.rs b/Libraries/LibWeb/Rust/src/painting/paint_passes.rsindex f0176805e46..8b6bc09f7cd 100644--- a/Libraries/LibWeb/Rust/src/painting/paint_passes.rs+++ b/Libraries/LibWeb/Rust/src/painting/paint_passes.rs@@ -545,15 +545,14 @@ pub(crate) enum ResolvedSvgPaintResource {/// What the host resolves of the enrolled SVG paint resources, if they changed since they were/// resolved last. A row that went away, or whose filters name no SVG filter any more, leaves them.-pub(crate) fn svg_paint_resource_requests(arena: &LayoutNodeArena) -> Option<Vec<SvgPaintResourceRequest>> {- let resources = arena.svg_paint_resources();- if !resources.take_needs_sync() {+pub(crate) fn svg_paint_resource_requests(arena: &mut LayoutNodeArena) -> Option<Vec<SvgPaintResourceRequest>> {+ if !arena.svg_paint_resources_mut().take_needs_sync() {return None;}let mut requests = Vec::new();- for (slot, kind) in resources.enrolled_entries() {+ for (slot, kind) in arena.svg_paint_resources().enrolled_entries() {let Some(style) = arena.node_style_if_live(slot) else {- resources.forget_slot(slot);+ arena.svg_paint_resources_mut().forget_slot(slot);continue;};if matches!(kind, SvgPaintResourceKind::Fill | SvgPaintResourceKind::Stroke) {@@ -567,7 +566,7 @@ pub(crate) fn svg_paint_resource_requests(arena: &LayoutNodeArena) -> Option<VecSvgPaintResourceKind::Fill | SvgPaintResourceKind::Stroke => unreachable!(),};if !crate::painting::css_filter::contains_url(filter_list) {- resources.withdraw(slot, kind);+ arena.svg_paint_resources_mut().withdraw(slot, kind);continue;}let urls = filter_list@@ -593,22 +592,25 @@ pub(crate) fn svg_paint_resource_requests(arena: &LayoutNodeArena) -> Option<Vec/// Publishes what the host resolved of the enrolled SVG paint resources, and answers whether any/// changed.pub(crate) fn publish_resolved_svg_paint_resources(- arena: &LayoutNodeArena,+ arena: &mut LayoutNodeArena,resolved: Vec<ResolvedSvgPaintResource>,) -> bool {use crate::painting::record::damage::PaintDamage;- let resources = arena.svg_paint_resources();let mut any_changed = false;for resolved in resolved {match resolved {ResolvedSvgPaintResource::PaintServer { slot, kind, published } => {- if arena.slot_is_live(slot) && resources.publish_paint_server(slot, kind, published) {+ if arena.slot_is_live(slot)+ && arena+ .svg_paint_resources_mut()+ .publish_paint_server(slot, kind, published)+ {any_changed = true;arena.push_paint_damage(slot, PaintDamage::SVG | PaintDamage::SCOPE_PREAMBLE);}}ResolvedSvgPaintResource::Filter { slot, kind, published } => {- if !arena.slot_is_live(slot) || !resources.publish_filter(slot, kind, published) {+ if !arena.slot_is_live(slot) || !arena.svg_paint_resources_mut().publish_filter(slot, kind, published) {continue;}any_changed = true;diff --git a/Libraries/LibWeb/Rust/src/painting/svg_paint_resources.rs b/Libraries/LibWeb/Rust/src/painting/svg_paint_resources.rsindex 61e62f981ed..1b286b2778e 100644--- a/Libraries/LibWeb/Rust/src/painting/svg_paint_resources.rs+++ b/Libraries/LibWeb/Rust/src/painting/svg_paint_resources.rs@@ -4,7 +4,6 @@* SPDX-License-Identifier: BSD-2-Clause*/-use std::cell::{Cell, RefCell};use std::collections::HashMap;use std::sync::Arc;use std::sync::atomic::{AtomicBool, Ordering};@@ -124,8 +123,8 @@ pub(crate) type SvgPaintResourceRows = HashMap<NodeSlotId, SvgPaintResourceRow>;#[derive(Clone, Default)]pub(crate) struct SvgPaintResources {- rows: RefCell<Arc<SvgPaintResourceRows>>,- needs_sync: Cell<bool>,+ rows: Arc<SvgPaintResourceRows>,+ needs_sync: bool,/// Whether any row enrolled a resource, which the document's host reads beside a frame in flight. A row the frame/// enrolls asks for its resources to be resolved again itself.enrolled: Arc<AtomicBool>,@@ -172,46 +171,40 @@ pub(crate) fn published_paint_server_in(}impl SvgPaintResources {- fn rows_mut(&self) -> std::cell::RefMut<'_, SvgPaintResourceRows> {- std::cell::RefMut::map(self.rows.borrow_mut(), Arc::make_mut)+ fn rows_mut(&mut self) -> &mut SvgPaintResourceRows {+ Arc::make_mut(&mut self.rows)}/// The table as it is now, for a recording to read.pub(crate) fn publish(&self) -> Arc<SvgPaintResourceRows> {- self.rows.borrow().clone()+ self.rows.clone()}/// Where the table is, which moves when a write copies the one a publication shares.pub(crate) fn address(&self) -> usize {- Arc::as_ptr(&self.rows.borrow()).addr()+ Arc::as_ptr(&self.rows).addr()}- pub(crate) fn set_enrolled_kinds(&self, slot: NodeSlotId, kinds: u8) {+ pub(crate) fn set_enrolled_kinds(&mut self, slot: NodeSlotId, kinds: u8) {if kinds == 0 {self.forget_slot(slot);return;}- self.needs_sync.set(true);- if self- .rows- .borrow()- .get(&slot)- .is_some_and(|row| row.enrolled_kinds == kinds)- {+ self.needs_sync = true;+ if self.rows.get(&slot).is_some_and(|row| row.enrolled_kinds == kinds) {return;}- let mut rows = self.rows_mut();- let row = rows.entry(slot).or_default();+ let row = self.rows_mut().entry(slot).or_default();let previous_kinds = std::mem::replace(&mut row.enrolled_kinds, kinds);row.forget_published(previous_kinds & !kinds);self.enrolled.store(true, Ordering::Relaxed);}- pub(crate) fn withdraw(&self, slot: NodeSlotId, kind: SvgPaintResourceKind) {- if !self.rows.borrow().contains_key(&slot) {+ pub(crate) fn withdraw(&mut self, slot: NodeSlotId, kind: SvgPaintResourceKind) {+ if !self.rows.contains_key(&slot) {return;}- let mut rows = self.rows_mut();+ let rows = self.rows_mut();let Some(row) = rows.get_mut(&slot) else {return;};@@ -219,15 +212,17 @@ impl SvgPaintResources {row.forget_published(kind.bit());if row.enrolled_kinds == 0 {rows.remove(&slot);- self.enrolled.store(!rows.is_empty(), Ordering::Relaxed);+ let any_enrolled = !rows.is_empty();+ self.enrolled.store(any_enrolled, Ordering::Relaxed);}}- pub(crate) fn forget_slot(&self, slot: NodeSlotId) {- if self.rows.borrow().contains_key(&slot) {- let mut rows = self.rows_mut();+ pub(crate) fn forget_slot(&mut self, slot: NodeSlotId) {+ if self.rows.contains_key(&slot) {+ let rows = self.rows_mut();rows.remove(&slot);- self.enrolled.store(!rows.is_empty(), Ordering::Relaxed);+ let any_enrolled = !rows.is_empty();+ self.enrolled.store(any_enrolled, Ordering::Relaxed);}}@@ -244,29 +239,28 @@ impl SvgPaintResources {}pub(crate) fn has_enrolled_entries(&self) -> bool {- !self.rows.borrow().is_empty()+ !self.rows.is_empty()}- pub(crate) fn note_changed(&self) -> bool {+ pub(crate) fn note_changed(&mut self) -> bool {if !self.has_enrolled_entries() {return false;}- self.needs_sync.set(true);+ self.needs_sync = true;true}/// Whether the host is to resolve the enrolled resources again.pub(crate) fn needs_sync(&self) -> bool {- self.needs_sync.get()+ self.needs_sync}- pub(crate) fn take_needs_sync(&self) -> bool {- self.needs_sync.replace(false)+ pub(crate) fn take_needs_sync(&mut self) -> bool {+ std::mem::take(&mut self.needs_sync)}pub(crate) fn enrolled_entries(&self) -> Vec<(NodeSlotId, SvgPaintResourceKind)> {self.rows- .borrow().iter().flat_map(|(slot, row)| {SvgPaintResourceKind::ALL@@ -282,7 +276,7 @@ impl SvgPaintResources {slot: NodeSlotId,kind: SvgPaintResourceKind,) -> Option<Arc<PublishedSvgFilter>> {- published_filter_in(&self.rows.borrow(), slot, kind)+ published_filter_in(&self.rows, slot, kind)}pub(crate) fn published_paint_server(@@ -290,16 +284,16 @@ impl SvgPaintResources {slot: NodeSlotId,kind: SvgPaintResourceKind,) -> Option<Arc<PublishedSvgPaintServer>> {- published_paint_server_in(&self.rows.borrow(), slot, kind)+ published_paint_server_in(&self.rows, slot, kind)}pub(crate) fn publish_paint_server(- &self,+ &mut self,slot: NodeSlotId,kind: SvgPaintResourceKind,paint_server: PublishedSvgPaintServer,) -> bool {- match self.rows.borrow().get(&slot) {+ match self.rows.get(&slot) {None => return false,Some(row)if row@@ -320,12 +314,12 @@ impl SvgPaintResources {}pub(crate) fn publish_filter(- &self,+ &mut self,slot: NodeSlotId,kind: SvgPaintResourceKind,filter: PublishedSvgFilter,) -> bool {- match self.rows.borrow().get(&slot) {+ match self.rows.get(&slot) {None => return false,Some(row) if row.published_filter(kind).is_some_and(|previous| *previous == filter) => return false,Some(_) => {}@@ -345,7 +339,7 @@ mod tests {#[test]fn a_published_table_keeps_what_it_held_when_a_row_changes() {- let resources = SvgPaintResources::default();+ let mut resources = SvgPaintResources::default();let slot = NodeSlotId::new(1, 1);let filter = |failed| PublishedSvgFilter {failed,