Truly independent web browser
LibCompositing: Keep the chunk names of a display list with its tape
Aliaksandr Kalenik2c25802919cf
diff --git a/Libraries/LibCompositing/Rust/src/display_list/chunks.rs b/Libraries/LibCompositing/Rust/src/display_list/chunks.rsindex 2806b8eaee2..6e8b1c4db93 100644--- a/Libraries/LibCompositing/Rust/src/display_list/chunks.rs+++ b/Libraries/LibCompositing/Rust/src/display_list/chunks.rs@@ -10,9 +10,15 @@//! that replaces another is then mostly the same chunks, a recording reuses whole chunks of the one before it, and a//! chunk is named by what it holds, so two tapes can be told apart chunk by chunk.-use super::commands::DisplayListCommandRun;+use super::builder::{for_each_command, has_inline_clip_in_its_own_space, read_command};+use super::commands::{+ CompositorScrollbar, DeclareMaskContent, DisplayListCommandRun, DisplayListCommandType, EffectNodeIndex,+ PaintScrollBar, SpatialNodeIndex,+};+use super::nested_records::{for_each_nested_record_span, span_bytes};use super::storage::write_run;use std::hash::{BuildHasher, Hasher};+use std::ops::Range;/// Names a chunk by what it holds: chunks with one id hold the same records in the same runs.#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]@@ -140,12 +146,201 @@ pub fn chunks_of<'a>(})}+#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub enum ReachOutsideChunk {+ ScrollbarThumbFollowsScrollOffsetOf(SpatialNodeIndex),+ InlineClipInItsOwnSpace,+ DeclaresMaskContentOf(EffectNodeIndex),+ ScrollbarPaintedByCompositor,+}++#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub struct TopLevelRecordReachingOutsideItsChunk {+ pub record_offset: u32,+ pub run_index: u32,+ pub reach: ReachOutsideChunk,+}++pub fn top_level_records_reaching_outside_their_chunk(+ bytes: &[u8],+ runs: &[DisplayListCommandRun],+) -> Vec<TopLevelRecordReachingOutsideItsChunk> {+ let mut records = Vec::new();+ for (run_index, run) in runs.iter().enumerate() {+ let run_start = run.offset as usize;+ for_each_command(+ &bytes[run_start..run_start + run.size as usize],+ |header, offset_in_run, payload| {+ let mut note = |reach| {+ records.push(TopLevelRecordReachingOutsideItsChunk {+ record_offset: (run_start + offset_in_run) as u32,+ run_index: run_index as u32,+ reach,+ });+ };+ match header.command_type {+ DisplayListCommandType::PaintScrollBar => {+ note(ReachOutsideChunk::ScrollbarThumbFollowsScrollOffsetOf(+ read_command::<PaintScrollBar>(payload).scroll_node_index,+ ));+ }+ DisplayListCommandType::CompositorScrollbar+ if read_command::<CompositorScrollbar>(payload).is_painted_by_compositor =>+ {+ note(ReachOutsideChunk::ScrollbarPaintedByCompositor);+ }+ _ => {}+ }+ if header.inline_clip_count != 0 && has_inline_clip_in_its_own_space(header, payload) {+ note(ReachOutsideChunk::InlineClipInItsOwnSpace);+ }+ for_each_mask_content_declared_in_record(header.command_type, payload, &mut |effect| {+ note(ReachOutsideChunk::DeclaresMaskContentOf(effect));+ });+ },+ );+ }+ records+}++fn for_each_mask_content_declared_in_record(+ command_type: DisplayListCommandType,+ payload: &[u8],+ visit: &mut impl FnMut(EffectNodeIndex),+) {+ if command_type == DisplayListCommandType::DeclareMaskContent {+ visit(read_command::<DeclareMaskContent>(payload).effect);+ }+ for_each_nested_record_span(command_type, payload, |_, span| {+ for_each_command(span_bytes(payload, span), |nested_header, _, nested_payload| {+ for_each_mask_content_declared_in_record(nested_header.command_type, nested_payload, visit);+ });+ });+}++#[derive(Clone, Copy, Debug, PartialEq, Eq)]+struct ChunkOfTape {+ id: ChunkId,+ run_end: u32,+ reaching_records_end: u32,+}++#[derive(Debug, Default, PartialEq, Eq)]+pub struct TapeChunks {+ chunks_in_paint_order: Vec<ChunkOfTape>,+ records_reaching_outside_their_chunks: Vec<TopLevelRecordReachingOutsideItsChunk>,+}++impl TapeChunks {+ pub fn len(&self) -> usize {+ self.chunks_in_paint_order.len()+ }++ pub fn is_empty(&self) -> bool {+ self.chunks_in_paint_order.is_empty()+ }++ pub fn id(&self, chunk_index: usize) -> ChunkId {+ self.chunks_in_paint_order[chunk_index].id+ }++ pub fn runs_of(&self, chunk_index: usize) -> Range<usize> {+ let start = chunk_index+ .checked_sub(1)+ .map_or(0, |previous| self.chunks_in_paint_order[previous].run_end as usize);+ start..self.chunks_in_paint_order[chunk_index].run_end as usize+ }++ pub fn records_reaching_outside(&self, chunk_index: usize) -> &[TopLevelRecordReachingOutsideItsChunk] {+ let start = chunk_index.checked_sub(1).map_or(0, |previous| {+ self.chunks_in_paint_order[previous].reaching_records_end as usize+ });+ &self.records_reaching_outside_their_chunks+ [start..self.chunks_in_paint_order[chunk_index].reaching_records_end as usize]+ }++ pub fn try_reserve(&mut self, chunk_count: usize, reaching_record_count: usize) -> Result<(), &'static str> {+ self.chunks_in_paint_order+ .try_reserve_exact(chunk_count)+ .and_then(|()| {+ self.records_reaching_outside_their_chunks+ .try_reserve_exact(reaching_record_count)+ })+ .map_err(|_| "Display list has too many chunks to receive")+ }++ pub fn push_chunk(+ &mut self,+ id: ChunkId,+ first_run_index: u32,+ run_count: usize,+ first_record_offset: u32,+ records_reaching_outside: &[TopLevelRecordReachingOutsideItsChunk],+ ) {+ debug_assert_eq!(+ first_run_index as usize,+ self.chunks_in_paint_order+ .last()+ .map_or(0, |chunk| chunk.run_end as usize)+ );+ self.records_reaching_outside_their_chunks+ .extend(+ records_reaching_outside+ .iter()+ .map(|record| TopLevelRecordReachingOutsideItsChunk {+ record_offset: record.record_offset + first_record_offset,+ run_index: record.run_index + first_run_index,+ ..*record+ }),+ );+ self.chunks_in_paint_order.push(ChunkOfTape {+ id,+ run_end: first_run_index + run_count as u32,+ reaching_records_end: self.records_reaching_outside_their_chunks.len() as u32,+ });+ }++ pub fn of_tape(bytes: &[u8], runs: &[DisplayListCommandRun], chunks: &[DisplayListChunk]) -> Self {+ let mut tape_chunks = Self::default();+ let mut covered_run_count = 0;+ let mut covered_byte_count = 0;+ for (chunk, _, chunk_runs) in chunks_of(bytes, runs, chunks) {+ tape_chunks.push_chunk(+ chunk.id,+ covered_run_count,+ chunk_runs.len(),+ 0,+ &top_level_records_reaching_outside_their_chunk(bytes, chunk_runs),+ );+ covered_run_count = chunk.run_end;+ covered_byte_count = chunk.byte_end;+ }+ let uncovered_runs = &runs[covered_run_count as usize..];+ if !uncovered_runs.is_empty() {+ tape_chunks.push_chunk(+ chunk_id(+ &bytes[covered_byte_count as usize..],+ uncovered_runs,+ covered_byte_count,+ ),+ covered_run_count,+ uncovered_runs.len(),+ 0,+ &top_level_records_reaching_outside_their_chunk(bytes, uncovered_runs),+ );+ }+ tape_chunks+ }+}+#[cfg(test)]mod tests {use super::*;- use crate::display_list::builder::{DisplayListBuilder, RecordedDisplayList};- use crate::display_list::commands::{ClipNodeIndex, ContextRef, EffectNodeIndex, FillRect, SpatialNodeIndex};- use libgfx_rust::{Color, CompositingAndBlendingOperator, IntRect};+ use crate::display_list::builder::{DisplayListBuilder, PendingInlineClip, RecordedDisplayList};+ use crate::display_list::commands::{+ ClipNodeIndex, ContextRef, DisplayListDataSpan, DrawIsolatedGroup, EffectNodeIndex, FillRect, SpatialNodeIndex,+ };+ use libgfx_rust::{Color, CompositingAndBlendingOperator, FloatRect, IntRect, MaskKind};fn context(spatial: u32) -> ContextRef {ContextRef {@@ -207,4 +402,121 @@ mod tests {builder.append(&fill_rect(11), &[], context(3));assert_ne!(builder.finish().chunks[0].id, tape.chunks[0].id);}++ fn compositor_scrollbar(is_painted_by_compositor: bool) -> CompositorScrollbar {+ CompositorScrollbar {+ scroll_node_index: SpatialNodeIndex(1),+ gutter_rect: IntRect::new(90, 0, 10, 100),+ thumb_rect: IntRect::new(90, 0, 10, 20),+ track_rect: IntRect::new(90, 0, 10, 100),+ expanded_gutter_rect: IntRect::new(86, 0, 14, 100),+ expanded_thumb_rect: IntRect::new(86, 0, 14, 20),+ scroll_size: 0.5,+ expanded_scroll_size: 0.5,+ min_scroll_offset: 0.0,+ max_scroll_offset: 100.0,+ thumb_color: Color::default(),+ thumb_outline_color: Color::default(),+ track_color: Color::default(),+ vertical: true,+ is_painted_by_compositor,+ }+ }++ fn declare_mask_content(builder: &mut DisplayListBuilder, effect: EffectNodeIndex, context: ContextRef) {+ let declaration = builder.begin_group::<DeclareMaskContent>(Vec::new());+ builder.append(&fill_rect(0), &[], context);+ let content = builder.group_content_span(&declaration);+ builder.finish_group(+ declaration,+ &DeclareMaskContent {+ rect: IntRect::new(0, 0, 10, 10),+ effect,+ content,+ },+ context,+ );+ }++ #[test]+ fn records_that_reach_outside_their_chunk_are_found_where_they_are_or_where_they_are_nested() {+ let mut builder = DisplayListBuilder::new();+ builder.append(&fill_rect(0), &[], context(1));+ let paint_scrollbar_offset = builder.byte_size() as u32;+ builder.append(+ &PaintScrollBar {+ scroll_node_index: SpatialNodeIndex(5),+ gutter_rect: IntRect::new(90, 0, 10, 100),+ thumb_rect: IntRect::new(90, 0, 10, 20),+ track_rect: IntRect::new(90, 0, 10, 100),+ expanded_gutter_rect: IntRect::new(86, 0, 14, 100),+ scroll_size: 0.5,+ thumb_color: Color::default(),+ thumb_outline_color: Color::default(),+ track_color: Color::default(),+ vertical: true,+ },+ &[],+ context(1),+ );+ let compositor_scrollbar_offset = builder.byte_size() as u32;+ builder.append(&compositor_scrollbar(true), &[], context(1));+ builder.append(&compositor_scrollbar(false), &[], context(1));+ let own_space_clip_offset = builder.byte_size() as u32;+ let clip = PendingInlineClip::intersecting_float_rect(FloatRect::new(0.0, 0.0, 5.0, 5.0));+ builder.append_with_inline_clips(&fill_rect(20), &[], context(2), &[clip.in_space(SpatialNodeIndex(4))]);+ builder.append_with_inline_clips(&fill_rect(30), &[], context(2), &[clip]);+ let mask_offset = builder.byte_size() as u32;+ declare_mask_content(&mut builder, EffectNodeIndex(1), context(2));+ let group_offset = builder.byte_size() as u32;+ let group = builder.begin_group::<DrawIsolatedGroup>(Vec::new());+ declare_mask_content(&mut builder, EffectNodeIndex(2), context(2));+ let content = builder.group_content_span(&group);+ builder.finish_group(+ group,+ &DrawIsolatedGroup {+ clip_rect: FloatRect::new(0.0, 0.0, 10.0, 10.0),+ content,+ mask: DisplayListDataSpan::default(),+ filter: DisplayListDataSpan::default(),+ opacity: 1.0,+ compositing_and_blending_operator: CompositingAndBlendingOperator::Normal,+ mask_kind: MaskKind::Alpha,+ },+ context(2),+ );+ let chunk = builder.take_chunk();++ let record = |record_offset, run_index, reach| TopLevelRecordReachingOutsideItsChunk {+ record_offset,+ run_index,+ reach,+ };+ assert_eq!(+ top_level_records_reaching_outside_their_chunk(&chunk.records, &chunk.runs),+ [+ record(+ paint_scrollbar_offset,+ 0,+ ReachOutsideChunk::ScrollbarThumbFollowsScrollOffsetOf(SpatialNodeIndex(5))+ ),+ record(+ compositor_scrollbar_offset,+ 0,+ ReachOutsideChunk::ScrollbarPaintedByCompositor+ ),+ record(own_space_clip_offset, 1, ReachOutsideChunk::InlineClipInItsOwnSpace),+ record(+ mask_offset,+ 1,+ ReachOutsideChunk::DeclaresMaskContentOf(EffectNodeIndex(1))+ ),+ record(+ group_offset,+ 1,+ ReachOutsideChunk::DeclaresMaskContentOf(EffectNodeIndex(2))+ ),+ ]+ );+ }}diff --git a/Libraries/LibCompositing/Rust/src/display_list/storage.rs b/Libraries/LibCompositing/Rust/src/display_list/storage.rsindex ceb08132b01..5a0ce57e1ec 100644--- a/Libraries/LibCompositing/Rust/src/display_list/storage.rs+++ b/Libraries/LibCompositing/Rust/src/display_list/storage.rs@@ -8,7 +8,10 @@//! or a tape that came from another process and passed validation.use super::builder::RecordedDisplayList;-use super::chunks::{Chunk, ChunkId, chunk_id};+use super::chunks::{+ Chunk, ChunkId, TapeChunks, TopLevelRecordReachingOutsideItsChunk, chunk_id,+ top_level_records_reaching_outside_their_chunk,+};use super::commands::{ContextRef, DisplayListCommandRun};use super::effect_clip_plan::EffectClipPlan;use super::summary::{DisplayListSummary, summarize};@@ -59,15 +62,17 @@ pub struct DisplayListStorage {// one plan serves every replay.effect_clip_plan: OnceLock<EffectClipPlan>,summary: OnceLock<DisplayListSummary>,+ chunks: OnceLock<TapeChunks>,}impl DisplayListStorage {- fn new(tape: Tape, visual_context_tree: Option<Arc<VisualContextTree>>) -> Arc<Self> {+ fn new(tape: Tape, visual_context_tree: Option<Arc<VisualContextTree>>, chunks: OnceLock<TapeChunks>) -> Arc<Self> {Arc::new(Self {tape,visual_context_tree,effect_clip_plan: OnceLock::new(),summary: OnceLock::new(),+ chunks,})}@@ -101,17 +106,31 @@ impl DisplayListStorage {EffectClipPlan::new(tree, self.command_runs()).expect("a display list's runs name live nodes of its tree")})}++ pub fn chunks(&self) -> &TapeChunks {+ self.chunks.get_or_init(|| {+ let recorded_chunks = match &self.tape {+ Tape::Recorded(recorded) => &recorded.chunks[..],+ Tape::Empty | Tape::Received { .. } => &[],+ };+ TapeChunks::of_tape(self.bytes(), self.command_runs(), recorded_chunks)+ })+ }}impl DisplayListStorage {/// A list this process recorded against `visual_context_tree`.pub fn recorded(recorded: Arc<RecordedDisplayList>, visual_context_tree: Arc<VisualContextTree>) -> Arc<Self> {- Self::new(Tape::Recorded(recorded), Some(visual_context_tree))+ Self::new(Tape::Recorded(recorded), Some(visual_context_tree), OnceLock::new())}/// An empty list, recorded against a tree with no node but the visual viewport.pub fn empty() -> Arc<Self> {- Self::new(Tape::Empty, Some(Arc::new(VisualContextTree::create_identity())))+ Self::new(+ Tape::Empty,+ Some(Arc::new(VisualContextTree::create_identity())),+ OnceLock::new(),+ )}}@@ -214,6 +233,7 @@ pub struct ReceivedChunk {pub id: ChunkId,records: AlignedBytes,runs: Vec<DisplayListCommandRun>,+ records_reaching_outside: Vec<TopLevelRecordReachingOutsideItsChunk>,}impl ReceivedChunk {@@ -224,6 +244,10 @@ impl ReceivedChunk {pub fn runs(&self) -> &[DisplayListCommandRun] {&self.runs}++ pub fn records_reaching_outside(&self) -> &[TopLevelRecordReachingOutsideItsChunk] {+ &self.records_reaching_outside+ }}/// Copies the chunks packed one after another in the `packed_size_in_bytes` bytes at `packed`, whose sizes `sizes` lists@@ -271,6 +295,7 @@ pub unsafe fn receive_packed_chunks_from_shared_memory(let runs = validate_tape(records.as_slice(), &packed_runs)?;chunks.push(ReceivedChunk {id: chunk_id(records.as_slice(), &runs, 0),+ records_reaching_outside: top_level_records_reaching_outside_their_chunk(records.as_slice(), &runs),records,runs,});@@ -301,17 +326,33 @@ impl DisplayListStorage {command_runs.try_reserve_exact(chunks.iter().map(|chunk| chunk.runs().len()).sum()).map_err(|_| "Display list is too large to receive")?;+ let mut tape_chunks = TapeChunks::default();+ tape_chunks.try_reserve(+ chunks.len(),+ chunks.iter().map(|chunk| chunk.records_reaching_outside().len()).sum(),+ )?;let mut chunk_start = 0;for chunk in chunks {let records = chunk.records();bytes.as_mut_slice()[chunk_start..chunk_start + records.len()].copy_from_slice(records);+ tape_chunks.push_chunk(+ chunk.id,+ command_runs.len() as u32,+ chunk.runs().len(),+ chunk_start as u32,+ chunk.records_reaching_outside(),+ );command_runs.extend(chunk.runs().iter().map(|run| DisplayListCommandRun {offset: run.offset + chunk_start as u32,..*run}));chunk_start += records.len();}- Ok(DisplayListStorage::new(Tape::Received { bytes, command_runs }, None))+ Ok(DisplayListStorage::new(+ Tape::Received { bytes, command_runs },+ None,+ OnceLock::from(tape_chunks),+ ))}}@@ -322,7 +363,8 @@ mod tests {#[test]fn chunks_packed_for_transport_come_back_as_the_tape_they_make() {use super::super::builder::DisplayListBuilder;- use super::super::commands::{ClipNodeIndex, EffectNodeIndex, FillRect, SpatialNodeIndex};+ use super::super::chunks::ReachOutsideChunk;+ use super::super::commands::{ClipNodeIndex, EffectNodeIndex, FillRect, PaintScrollBar, SpatialNodeIndex};use libgfx_rust::{Color, CompositingAndBlendingOperator, IntRect};let context = |spatial| ContextRef {spatial: SpatialNodeIndex(spatial),@@ -340,6 +382,23 @@ mod tests {let first = builder.take_chunk();builder.append(&fill_rect(10), &[], context(1));builder.append(&fill_rect(20), &[], context(2));+ let scrollbar_offset_in_second_chunk = builder.byte_size() as u32;+ builder.append(+ &PaintScrollBar {+ scroll_node_index: SpatialNodeIndex(3),+ gutter_rect: IntRect::new(90, 0, 10, 100),+ thumb_rect: IntRect::new(90, 0, 10, 20),+ track_rect: IntRect::new(90, 0, 10, 100),+ expanded_gutter_rect: IntRect::new(86, 0, 14, 100),+ scroll_size: 0.5,+ thumb_color: Color::default(),+ thumb_outline_color: Color::default(),+ track_color: Color::default(),+ vertical: true,+ },+ &[],+ context(2),+ );let second = builder.take_chunk();let chunks = [&first, &second];let mut packed = vec![0; packed_size_of_chunks(&chunks)];@@ -358,6 +417,26 @@ mod tests {assert_eq!(storage.bytes(), &tape.bytes[..]);assert_eq!(storage.command_runs(), &tape.command_runs[..]);+ let tape_chunks = storage.chunks();+ assert_eq!(+ tape_chunks,+ &TapeChunks::of_tape(&tape.bytes, &tape.command_runs, &tape.chunks)+ );+ assert_eq!(+ (tape_chunks.len(), tape_chunks.id(0), tape_chunks.id(1)),+ (2, first.id, second.id)+ );+ assert_eq!((tape_chunks.runs_of(0), tape_chunks.runs_of(1)), (0..1, 1..3));+ assert!(tape_chunks.records_reaching_outside(0).is_empty());+ assert_eq!(+ tape_chunks.records_reaching_outside(1),+ [TopLevelRecordReachingOutsideItsChunk {+ record_offset: first.records.len() as u32 + scrollbar_offset_in_second_chunk,+ run_index: 2,+ reach: ReachOutsideChunk::ScrollbarThumbFollowsScrollOffsetOf(SpatialNodeIndex(3)),+ }]+ );+// Sizes that leave bytes over or claim more than the buffer holds are refused.assert!(receive_packed_chunks(&packed, &sizes[..1]).is_err());assert!(receive_packed_chunks(&packed[..packed.len() - 8], &sizes).is_err());