Skip to content

Truly independent web browser

LibWeb: Keep paint-order damage marks in the recording's scratch

Aliaksandr Kalenik7325c653f5b8
diff --git a/Libraries/LibWeb/Rust/src/painting/record/assemble.rs b/Libraries/LibWeb/Rust/src/painting/record/assemble.rs
index df24a1f2736..48a30589cc4 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/assemble.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/assemble.rs
@@ -10,7 +10,7 @@
//! way: when assembly starts, the damage set is complete by construction.
use super::PaintPhase;
-use super::order_tree::{ChildEntry, ChildKey, OutputSize, PaintOrderTree, ProducerKind, ScopeId};
+use super::order_tree::{ChildEntry, ChildKey, OutputSize, PaintOrderTree, ProducerKind, ScopeId, ScopeMarks};
use crate::css::style::fast_hash::FastMap;
use crate::layout::node_data::NodeSlotId;
use crate::painting::paint_order_plan::{PaintOrderItem, PaintProducer, PaintScope, PaintScopeKind};
@@ -141,6 +141,7 @@ pub(crate) fn frame_is_unchanged(tree: &PaintOrderTree, has_damage: bool, window
pub(crate) struct Assembler<'a, H: AssemblyHost> {
host: &'a mut H,
tree: &'a mut PaintOrderTree,
+ marks: &'a mut ScopeMarks,
// The byte offset of the published root scope, when a published recording can be copied from.
source_prologue_bytes: Option<u32>,
// Off for a frame that publishes nothing: it still marks damaged paths, but the tree's
@@ -155,6 +156,7 @@ impl<'a, H: AssemblyHost> Assembler<'a, H> {
pub(crate) fn new(
host: &'a mut H,
tree: &'a mut PaintOrderTree,
+ marks: &'a mut ScopeMarks,
source_prologue_bytes: Option<u32>,
rewrites_tree: bool,
window_focus_changed: bool,
@@ -162,6 +164,7 @@ impl<'a, H: AssemblyHost> Assembler<'a, H> {
Self {
host,
tree,
+ marks,
source_prologue_bytes,
rewrites_tree,
window_focus_changed,
@@ -179,13 +182,13 @@ impl<'a, H: AssemblyHost> Assembler<'a, H> {
/// rewrites the tree leaves the assembled structure there as pending changes for the caller
/// to publish or discard.
pub(crate) fn assemble_root(mut self, root_scope: PaintScope) -> OutputSize {
- self.tree.begin_recording();
+ self.marks.begin_recording();
// Damage only steers what a frame copies; a frame without a published recording to copy
// from records everything and never consults it.
if self.source_prologue_bytes.is_some() {
for row in self.host.damaged_rows() {
for scope in self.tree.scopes_owned_by(row) {
- self.tree.mark_path_to_root_damaged(scope);
+ self.tree.mark_path_to_root_damaged(scope, self.marks);
}
}
}
@@ -240,7 +243,7 @@ impl<'a, H: AssemblyHost> Assembler<'a, H> {
let published_output = entry.output();
let assembled = match entry.key() {
ChildKey::Scope(child) => {
- if self.tree.is_on_damaged_path(child) || self.records_again(&entry) {
+ if self.marks.is_on_damaged_path(child) || self.records_again(&entry) {
self.assemble_scope(child, child_cursor)
} else {
self.copy(child_cursor, published_output);
@@ -348,7 +351,7 @@ impl<'a, H: AssemblyHost> Assembler<'a, H> {
let assembled = match found {
Some((child, index)) => {
matched[index] = true;
- if self.tree.is_on_damaged_path(child) || self.records_again(&published[index]) {
+ if self.marks.is_on_damaged_path(child) || self.records_again(&published[index]) {
self.assemble_scope(child, published_cursors[index])
} else {
self.copy(published_cursors[index], published[index].output());
@@ -769,8 +772,9 @@ mod tests {
);
let prologue_bytes = host.output.recorder.byte_size() as u32;
let source_prologue_bytes = host.source.as_ref().map(|frame| frame.prologue_bytes);
- let root_output =
- Assembler::new(host, tree, source_prologue_bytes, rewrites_tree, false).assemble_root(context(row(0)));
+ let mut marks = ScopeMarks::default();
+ let root_output = Assembler::new(host, tree, &mut marks, source_prologue_bytes, rewrites_tree, false)
+ .assemble_root(context(row(0)));
let output = std::mem::replace(&mut host.output, new_output());
let hit_test_items = std::sync::Arc::try_unwrap(output.hit_test_list.items).expect("items are unshared");
Frame {
diff --git a/Libraries/LibWeb/Rust/src/painting/record/order_tree.rs b/Libraries/LibWeb/Rust/src/painting/record/order_tree.rs
index d97c14c4cb2..23338c1830f 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/order_tree.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/order_tree.rs
@@ -260,16 +260,13 @@ struct ScopeNode {
parent: ScopeId,
children_start: u32,
children_len: u32,
- // Equal to the tree's assembly stamp while the node is on a damaged path of the current
- // recording, which is what makes an ancestor recurse into it instead of copying it.
- recursion_stamp: u32,
kind: u8,
flags: u8,
generation: u8,
_padding: u8,
}
-const _: () = assert!(std::mem::size_of::<ScopeNode>() == 24);
+const _: () = assert!(std::mem::size_of::<ScopeNode>() == 20);
fn scope_kind_code(kind: PaintScopeKind) -> u8 {
match kind {
@@ -304,7 +301,39 @@ pub(crate) struct PaintOrderTree {
duplicate_scopes: FastMap<(NodeSlotId, u8), SmallVec<[ScopeId; 2]>>,
root: ScopeId,
root_entry: ChildEntry,
- assembly_stamp: u32,
+}
+
+/// The scopes on a damaged path of the current recording, which is what makes an ancestor recurse into a scope instead
+/// of copying it. A scope counts as on one while its stamp equals the current stamp.
+#[derive(Default)]
+pub(crate) struct ScopeMarks {
+ stamps: Vec<u32>,
+ stamp: u32,
+}
+
+impl ScopeMarks {
+ /// Starts a recording: the stamp advances so no scope counts as being on a damaged path yet.
+ pub(crate) fn begin_recording(&mut self) {
+ self.stamp = match self.stamp.checked_add(1) {
+ Some(stamp) => stamp,
+ None => {
+ self.stamps.clear();
+ 1
+ }
+ };
+ }
+
+ /// Marks a scope, answering whether it was not marked yet.
+ fn mark(&mut self, id: ScopeId) -> bool {
+ if self.stamps.len() <= id.index() {
+ self.stamps.resize(id.index() + 1, 0);
+ }
+ std::mem::replace(&mut self.stamps[id.index()], self.stamp) != self.stamp
+ }
+
+ pub(crate) fn is_on_damaged_path(&self, id: ScopeId) -> bool {
+ self.stamps.get(id.index()) == Some(&self.stamp)
+ }
}
impl PaintOrderTree {
@@ -456,38 +485,19 @@ impl PaintOrderTree {
}
}
- /// Starts a recording: the assembly stamp advances so no node counts as being on a damaged
- /// path yet.
- pub(crate) fn begin_recording(&mut self) -> u32 {
- self.assembly_stamp = self.assembly_stamp.wrapping_add(1);
- if self.assembly_stamp == 0 {
- for node in &mut self.nodes {
- node.recursion_stamp = 0;
- }
- self.assembly_stamp = 1;
- }
- self.assembly_stamp
- }
-
/// Marks the path from a scope to the root as damaged for this recording, stopping at the
/// first ancestor already marked.
- pub(crate) fn mark_path_to_root_damaged(&mut self, id: ScopeId) {
- let stamp = self.assembly_stamp;
+ pub(crate) fn mark_path_to_root_damaged(&self, id: ScopeId, marks: &mut ScopeMarks) {
let mut current = id;
while !current.is_none() {
- let node = self.node_mut(current);
- if node.recursion_stamp == stamp {
+ let parent = self.node(current).parent;
+ if !marks.mark(current) {
break;
}
- node.recursion_stamp = stamp;
- current = node.parent;
+ current = parent;
}
}
- pub(crate) fn is_on_damaged_path(&self, id: ScopeId) -> bool {
- self.node(id).recursion_stamp == self.assembly_stamp
- }
-
/// Allocates a scope for this recording and registers it for its owner and kind.
pub(crate) fn allocate_scope(&mut self, scope: PaintScope, parent: ScopeId) -> ScopeId {
let node = ScopeNode {
@@ -495,7 +505,6 @@ impl PaintOrderTree {
parent,
children_start: 0,
children_len: 0,
- recursion_stamp: self.assembly_stamp,
kind: scope_kind_code(scope.kind),
flags: NODE_OCCUPIED,
generation: 1,
@@ -621,7 +630,6 @@ mod tests {
// A root context with one foreground scope holding `leaf_count` leaf scopes of one
// producer each, recorded as the first frame.
fn record_first_frame(tree: &mut PaintOrderTree, leaf_count: u32) -> (ScopeId, ScopeId, Vec<ScopeId>) {
- tree.begin_recording();
let root = tree.allocate_scope(context_scope(row(0)), ScopeId::NONE);
let foreground = tree.allocate_scope(foreground_scope(row(0)), root);
let mut leaves = Vec::new();
@@ -673,24 +681,24 @@ mod tests {
fn damaged_paths_are_marked_up_to_the_first_marked_ancestor() {
let mut tree = PaintOrderTree::default();
let (root, foreground, leaves) = record_first_frame(&mut tree, 2);
- tree.begin_recording();
+ let mut marks = ScopeMarks::default();
+ marks.begin_recording();
- tree.mark_path_to_root_damaged(leaves[0]);
- assert!(tree.is_on_damaged_path(leaves[0]));
- assert!(tree.is_on_damaged_path(foreground));
- assert!(tree.is_on_damaged_path(root));
- assert!(!tree.is_on_damaged_path(leaves[1]));
+ tree.mark_path_to_root_damaged(leaves[0], &mut marks);
+ assert!(marks.is_on_damaged_path(leaves[0]));
+ assert!(marks.is_on_damaged_path(foreground));
+ assert!(marks.is_on_damaged_path(root));
+ assert!(!marks.is_on_damaged_path(leaves[1]));
- tree.begin_recording();
- assert!(!tree.is_on_damaged_path(leaves[0]));
- assert!(!tree.is_on_damaged_path(root));
+ marks.begin_recording();
+ assert!(!marks.is_on_damaged_path(leaves[0]));
+ assert!(!marks.is_on_damaged_path(root));
}
#[test]
fn a_recording_replaces_child_lists_and_retires_detached_subtrees_at_once() {
let mut tree = PaintOrderTree::default();
let (root, foreground, leaves) = record_first_frame(&mut tree, 2);
- tree.begin_recording();
let added = tree.allocate_scope(foreground_scope(row(9)), foreground);
tree.set_children(
@@ -712,7 +720,6 @@ mod tests {
assert_eq!(tree.published_scope(foreground_scope(row(9)), foreground), Some(added));
// The retired slot is reused with a new generation, so the old id can never match.
- tree.begin_recording();
let reused = tree.allocate_scope(foreground_scope(row(7)), foreground);
assert_eq!(reused.index(), leaves[0].index());
assert_ne!(reused, leaves[0]);
@@ -722,7 +729,6 @@ mod tests {
fn a_scope_moving_to_another_parent_takes_over_the_slot_its_predecessor_leaves() {
let mut tree = PaintOrderTree::default();
let (root, foreground, leaves) = record_first_frame(&mut tree, 2);
- tree.begin_recording();
// Row 1's foreground scope is listed under the root now; the old one is still there.
let moved = tree.allocate_scope(foreground_scope(row(1)), root);
@@ -744,7 +750,6 @@ mod tests {
let mut tree = PaintOrderTree::default();
let (root, foreground, _) = record_first_frame(&mut tree, 1);
for round in 0..300u32 {
- tree.begin_recording();
for child in tree.children(foreground).to_vec() {
if let ChildKey::Scope(previous) = child.key() {
tree.detach_scope(previous);
@@ -772,7 +777,6 @@ mod tests {
let mut tree = PaintOrderTree::default();
let (root, foreground, leaves) = record_first_frame(&mut tree, 2);
let capacity = tree.child_entry_capacity();
- tree.begin_recording();
let grown = output(24, 1);
tree.set_children(leaves[0], &[ChildEntry::producer(ProducerKind::DrawForeground, grown)]);
@@ -793,7 +797,6 @@ mod tests {
let (root, foreground, leaves) = record_first_frame(&mut tree, 8);
let mut peak = 0;
for round in 0..4000u32 {
- tree.begin_recording();
let leaf = leaves[(round % 8) as usize];
let leaf_output = output(16 + round % 3, 1);
// Every other round the leaf gains or loses a producer, so its list changes length.
diff --git a/Libraries/LibWeb/Rust/src/painting/record/scratch.rs b/Libraries/LibWeb/Rust/src/painting/record/scratch.rs
index 30c57212e2b..aa7bf58d4ba 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/scratch.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/scratch.rs
@@ -7,6 +7,7 @@
use crate::layout::node_data::NodeSlotId;
use crate::painting::display_list::commands::SpatialNodeIndex;
use crate::painting::record::hit_test_items::HitTestFacts;
+use crate::painting::record::order_tree::ScopeMarks;
use crate::painting::record::paint::text::SelectionStyleAnswer;
use crate::painting::record::{BasePaintFacts, PatternTileKey};
use std::collections::HashMap;
@@ -36,6 +37,7 @@ pub(crate) struct RecordingScratch {
recording_stamp: u32,
base_paint_facts: Vec<StampedEntry<BasePaintFacts>>,
hit_test_facts: Vec<StampedEntry<HitTestFacts>>,
+ pub(super) scope_marks: ScopeMarks,
pub(super) pattern_tile_records: HashMap<PatternTileKey, Arc<Vec<u8>>>,
pub(super) selection_style_cache: HashMap<u32, Arc<SelectionStyleAnswer>>,
pub(super) search_text_style_cache: HashMap<u32, Arc<SelectionStyleAnswer>>,
diff --git a/Libraries/LibWeb/Rust/src/painting/record/traversal.rs b/Libraries/LibWeb/Rust/src/painting/record/traversal.rs
index 30066d4dd71..46f6e7075d8 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/traversal.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/traversal.rs
@@ -156,14 +156,17 @@ fn record_display_list_impl<O: Observer>(
}
None => {
let source_prologue_bytes = recorder.source_recording.as_ref().map(|frame| frame.prologue_bytes);
+ let mut marks = std::mem::take(&mut recorder.scratch.scope_marks);
Assembler::new(
&mut recorder,
tree,
+ &mut marks,
source_prologue_bytes,
inputs.publishes_recording,
window_focus_changed,
)
.assemble_root(root_scope);
+ recorder.scratch.scope_marks = marks;
if has_inspector_overlays {
recorder.trace_paint(
Operation::Named(None, "inspector-overlays"),