Truly independent web browser
LibCompositing: Let an inline clip apply in a spatial node of its own
Andreas Kling8b34bfccee2a
diff --git a/Libraries/LibCompositing/Rust/src/display_list/builder.rs b/Libraries/LibCompositing/Rust/src/display_list/builder.rsindex d38421bf415..6e2de168fca 100644--- a/Libraries/LibCompositing/Rust/src/display_list/builder.rs+++ b/Libraries/LibCompositing/Rust/src/display_list/builder.rs@@ -23,6 +23,7 @@ pub struct PendingInlineClip {path_winding_rule: WindingRule,serialized_path_bytes: Option<Rc<Vec<u8>>>,narrows_clip_rect_to_the_command_bounding_rect: bool,+ space: Option<SpatialNodeIndex>,}impl PendingInlineClip {@@ -42,6 +43,7 @@ impl PendingInlineClip {path_winding_rule: WindingRule::Nonzero,serialized_path_bytes: None,narrows_clip_rect_to_the_command_bounding_rect: false,+ space: None,}}@@ -76,7 +78,27 @@ impl PendingInlineClip {}}+ /// Whether this clip alone keeps what it clips within `rect`, in the space of the command it is recorded on.+ pub fn lies_within(&self, rect: FloatRect) -> bool {+ self.space.is_none()+ && self.mode == ClipMode::Intersect+ && rect.contains_rect(self.clip_rect_or_path_device_bounds)+ }++ /// The same clip, applied in `space` rather than in the space of the command it ends up on, unless it already has+ /// a space of its own. A clip in a space of its own does not bound the command, as the two spaces can move apart.+ pub fn in_space(&self, space: SpatialNodeIndex) -> Self {+ Self {+ narrows_clip_rect_to_the_command_bounding_rect: false,+ space: Some(self.space.unwrap_or(space)),+ ..self.clone()+ }+ }+fn header_bounding_rect_restriction(&self) -> Option<IntRect> {+ if self.space.is_some() {+ return None;+ }match self.mode {ClipMode::Intersect => Some(enclosing_int_rect(self.clip_rect_or_path_device_bounds)),ClipMode::Difference => None,@@ -418,6 +440,9 @@ impl DisplayListBuilder {path_winding_rule: clip.path_winding_rule,kind: clip.kind,mode: clip.mode,+ has_own_space: clip.space.is_some(),+ space: clip.space.unwrap_or_default(),+ padding: 0,};let entry_start = payload_start + entries_offset + index * INLINE_CLIP_ENTRY_SIZE;entry.write_ffi_bytes(&mut self.bytes[entry_start..entry_start + INLINE_CLIP_ENTRY_SIZE]);@@ -771,6 +796,9 @@ mod tests {} else {ClipMode::Difference},+ has_own_space: reader.u8_at(std::mem::offset_of!(DisplayListInlineClip, has_own_space)) != 0,+ space: SpatialNodeIndex(reader.u32_at(std::mem::offset_of!(DisplayListInlineClip, space))),+ padding: 0,}}).collect()diff --git a/Libraries/LibCompositing/Rust/src/display_list/damage.rs b/Libraries/LibCompositing/Rust/src/display_list/damage.rsindex f3d4e62cf5b..ba2e087c2b3 100644--- a/Libraries/LibCompositing/Rust/src/display_list/damage.rs+++ b/Libraries/LibCompositing/Rust/src/display_list/damage.rs@@ -8,7 +8,7 @@ use super::effect_clip_plan::EffectClipPlan;use super::effect_geometry::{Extent, SpaceMatrices, changed_extent_through_filters, nearest_spreading_effects};use super::summary::{DisplayListSummary, LiveContent};use crate::display_list::builder::{- HEADER_SIZE, for_each_command, inline_transform_entry_offset, read_command, read_header,+ HEADER_SIZE, for_each_command, inline_clips_of, inline_transform_entry_offset, read_command, read_header,};use crate::display_list::commands::{CanvasId, ClipMode, ClipNodeIndex, CompositorScrollbar, ContextRef, DeclareMaskContent, DisplayListCommandHeader,@@ -224,6 +224,8 @@ fn inline_clip_lists_are_equal(a: &CommandReference<'_>, b: &CommandReference<'_&& same_entry_field(offset_of!(DisplayListInlineClip, path_winding_rule), 4)&& same_entry_field(offset_of!(DisplayListInlineClip, kind), 1)&& same_entry_field(offset_of!(DisplayListInlineClip, mode), 1)+ && same_entry_field(offset_of!(DisplayListInlineClip, has_own_space), 1)+ && same_entry_field(offset_of!(DisplayListInlineClip, space), 4)&& first.span_bytes_at(a_entry + offset_of!(DisplayListInlineClip, path_data))== second.span_bytes_at(b_entry + offset_of!(DisplayListInlineClip, path_data))})@@ -1043,6 +1045,24 @@ impl DamageAccumulator {self.add_moved_command_damage(chains, old_command, new_command);}+ // A command that draws in a different space than one of its inline clips shows another part of+ // itself whenever one of the two spaces scrolls, though neither the command nor its context changed.+ fn add_inline_clip_scroll_damage(+ &mut self,+ chains: &TreeChainComparison<'_>,+ old_command: &CommandReference<'_>,+ new_command: &CommandReference<'_>,+ ) {+ let has_inline_clip_in_its_own_space = |command: &CommandReference<'_>| {+ inline_clips_of(&command.header, command.payload).any(|clip| clip.has_own_space)+ };+ if !has_inline_clip_in_its_own_space(old_command) && !has_inline_clip_in_its_own_space(new_command) {+ return;+ }+ self.add_old_command_damage(chains, old_command);+ self.add_new_command_damage(chains, new_command);+ }+fn add_scrollbar_scroll_damage(&mut self,chains: &TreeChainComparison<'_>,@@ -1106,6 +1126,7 @@ impl DamageAccumulator {}if scroll_offsets_differ {self.add_scrollbar_scroll_damage(chains, &old_command, &new_command);+ self.add_inline_clip_scroll_damage(chains, &old_command, &new_command);}});}@@ -1141,6 +1162,7 @@ impl DamageAccumulator {damage.add_visual_context_damage(chains, old_command, new_command);if scroll_offsets_differ {damage.add_scrollbar_scroll_damage(chains, old_command, new_command);+ damage.add_inline_clip_scroll_damage(chains, old_command, new_command);}};@@ -1783,6 +1805,9 @@ mod tests {path_winding_rule: WindingRule::Nonzero,kind: InlineClipKind::Rect,mode: ClipMode::Intersect,+ has_own_space: false,+ space: SpatialNodeIndex::default(),+ padding: 0,}}@@ -2226,6 +2251,50 @@ mod tests {);}+ #[test]+ fn changed_scroll_offset_damages_a_draw_with_an_inline_clip_in_another_space() {+ let tree = identity_tree();+ let rect = IntRect::new(10, 10, 20, 20);+ let draw_with_clip = |clip: DisplayListInlineClip| {+ let mut display_list = TestDisplayList::default();+ append_record(+ &mut display_list,+ &FillRect {+ rect,+ color: RED,+ compositing_and_blending_operator: CompositingAndBlendingOperator::Normal,+ background_color_animation_effect: EffectNodeIndex::NONE,+ },+ &[],+ Some(rect),+ ContextRef::default(),+ &[clip],+ );+ display_list+ };+ let damage_after_scroll = |display_list: &TestDisplayList| {+ damage_with_scroll_offsets(+ display_list,+ &tree,+ &[FloatPoint::default(), FloatPoint::default()],+ display_list,+ &tree,+ &[FloatPoint::default(), FloatPoint { x: 0.0, y: -40.0 }],+ IntRect::new(0, 0, 100, 100),+ )+ };++ assert!(damage_after_scroll(&draw_with_clip(rect_inline_clip(rect))).is_none_or(|damage| damage.is_empty()));+ assert_eq!(+ damage_after_scroll(&draw_with_clip(DisplayListInlineClip {+ has_own_space: true,+ space: SpatialNodeIndex(1),+ ..rect_inline_clip(rect)+ })),+ Some(IntRect::new(9, 9, 22, 22))+ );+ }+#[test]fn inactive_optional_storage_does_not_damage_scaled_images() {let tree = identity_tree();diff --git a/Libraries/LibCompositing/Rust/src/display_list/replay.rs b/Libraries/LibCompositing/Rust/src/display_list/replay.rsindex c12cb602d07..be0048d7ec2 100644--- a/Libraries/LibCompositing/Rust/src/display_list/replay.rs+++ b/Libraries/LibCompositing/Rust/src/display_list/replay.rs@@ -44,17 +44,26 @@ pub trait ReplayPainter {// `payload`; the command's spans point into `payload`.fn play_command(&mut self, command_type: DisplayListCommandType, command: &[u8], payload: &[u8]);- fn execute_run(&mut self, _run_index: usize, records: &[u8], scroll_offsets: &[FloatPoint]) {- replay_records(records, scroll_offsets, self);+ fn execute_run(+ &mut self,+ _run_index: usize,+ records: &[u8],+ scroll_offsets: &[FloatPoint],+ clip_space_matrix: &dyn Fn(SpatialNodeIndex) -> Option<FloatMatrix4x4>,+ ) {+ replay_records(records, scroll_offsets, clip_space_matrix, self);}}/// Plays one stream of records: the records of a run, or the records nested in a command. Compositor/// metadata is skipped, a command whose bounds are empty or clipped away is culled, and each command plays/// inside its inline clips and transform. A scroll bar's thumb moves by the offset of its scroll node.+/// `clip_space_matrix` gives the to-root matrix of the space an inline clip of its own space is applied in;+/// a clip whose space it does not know applies in the space of its command.pub fn replay_records<Painter: ReplayPainter + ?Sized>(records: &[u8],scroll_offsets: &[FloatPoint],+ clip_space_matrix: &dyn Fn(SpatialNodeIndex) -> Option<FloatMatrix4x4>,painter: &mut Painter,) {for_each_command(records, |header, _, payload| {@@ -64,7 +73,14 @@ pub fn replay_records<Painter: ReplayPainter + ?Sized>(if header.bounding_rect.is_empty() || painter.would_be_fully_clipped_by_painter(header.bounding_rect) {return;}+ // The matrix of the command's space, while an inline clip in a space of its own has the canvas in another.+ let mut command_space_matrix = None;for clip in inline_clips_of(header, payload) {+ let own_space_matrix = clip.has_own_space.then(|| clip_space_matrix(clip.space)).flatten();+ if let Some(matrix) = &own_space_matrix {+ command_space_matrix.get_or_insert_with(|| painter.canvas_matrix());+ painter.set_matrix(matrix);+ }match clip.kind {InlineClipKind::Path => {painter.push_clip_path_bytes(span_bytes(payload, clip.path_data), clip.path_winding_rule);@@ -75,6 +91,11 @@ pub fn replay_records<Painter: ReplayPainter + ?Sized>(mode: clip.mode,}),}+ if own_space_matrix.is_some()+ && let Some(matrix) = &command_space_matrix+ {+ painter.set_matrix(matrix);+ }}let inline_transform = inline_transform_of(header, payload);if let Some(transform) = &inline_transform {@@ -102,6 +123,10 @@ pub fn replay_records<Painter: ReplayPainter + ?Sized>(for _ in 0..header.inline_clip_count {painter.pop();}+ // Popping a clip pushed in another space restores the matrix it was pushed with.+ if let Some(matrix) = &command_space_matrix {+ painter.set_matrix(matrix);+ }});}@@ -708,7 +733,13 @@ impl<Painter: ReplayPainter> ReplayDriver<'_, Painter> {return;}let records = &self.tape[run.offset as usize..(run.offset + run.size) as usize];- self.painter.execute_run(run_index, records, self.scroll_offsets);+ let palette = &self.palette;+ let clip_space_matrix = |space: SpatialNodeIndex| {+ let draw_space = palette.draw_spaces.get(space.0 as usize)?;+ palette.to_root_matrices.get(draw_space.0 as usize).copied()+ };+ self.painter+ .execute_run(run_index, records, self.scroll_offsets, &clip_space_matrix);}fn execute(&mut self) {@@ -1015,7 +1046,13 @@ mod tests {fn play_command(&mut self, _command_type: DisplayListCommandType, _command: &[u8], _payload: &[u8]) {}- fn execute_run(&mut self, run_index: usize, _records: &[u8], _scroll_offsets: &[FloatPoint]) {+ fn execute_run(+ &mut self,+ run_index: usize,+ _records: &[u8],+ _scroll_offsets: &[FloatPoint],+ _clip_space_matrix: &dyn Fn(SpatialNodeIndex) -> Option<FloatMatrix4x4>,+ ) {self.events.push(PainterEvent::Run(run_index));}}@@ -1117,6 +1154,79 @@ mod tests {);}+ #[test]+ fn an_inline_clip_in_a_space_of_its_own_is_pushed_under_the_matrix_of_that_space() {+ let clip_space = SpatialNodeIndex(7);+ let clip_rect = FloatRect::new(1.0, 2.0, 3.0, 4.0);+ let mut builder = crate::display_list::builder::DisplayListBuilder::new();+ builder.append_with_inline_clips(+ &crate::display_list::commands::FillRect {+ rect: IntRect::new(0, 0, 10, 10),+ color: Color::default(),+ compositing_and_blending_operator: CompositingAndBlendingOperator::Normal,+ background_color_animation_effect: EffectNodeIndex::NONE,+ },+ &[],+ ContextRef::default(),+ &[+ crate::display_list::builder::PendingInlineClip::intersecting_float_rect(clip_rect),+ crate::display_list::builder::PendingInlineClip::intersecting_float_rect(clip_rect)+ .in_space(clip_space),+ ],+ );+ let clip_space_matrix = translation_matrix(0.0, -50.0, 0.0);+ let mut painter = RecordingPainter::new();+ let command_space_matrix = painter.base_matrix;+ replay_records(+ builder.bytes(),+ &[],+ &|space| (space == clip_space).then_some(clip_space_matrix),+ &mut painter,+ );+ assert_eq!(+ painter.events,+ vec![+ PainterEvent::PushClip(clip_rect),+ PainterEvent::SetMatrix(clip_space_matrix),+ PainterEvent::PushClip(clip_rect),+ PainterEvent::SetMatrix(command_space_matrix),+ PainterEvent::Pop,+ PainterEvent::Pop,+ PainterEvent::SetMatrix(command_space_matrix),+ ]+ );+ }++ #[test]+ fn an_inline_clip_in_an_unknown_space_is_pushed_in_the_space_of_its_command() {+ let mut builder = crate::display_list::builder::DisplayListBuilder::new();+ builder.append_with_inline_clips(+ &crate::display_list::commands::FillRect {+ rect: IntRect::new(0, 0, 10, 10),+ color: Color::default(),+ compositing_and_blending_operator: CompositingAndBlendingOperator::Normal,+ background_color_animation_effect: EffectNodeIndex::NONE,+ },+ &[],+ ContextRef::default(),+ &[+ crate::display_list::builder::PendingInlineClip::intersecting_float_rect(FloatRect::new(+ 1.0, 2.0, 3.0, 4.0,+ ))+ .in_space(SpatialNodeIndex(u32::MAX - 1)),+ ],+ );+ let mut painter = RecordingPainter::new();+ replay_records(builder.bytes(), &[], &|_| None, &mut painter);+ assert_eq!(+ painter.events,+ vec![+ PainterEvent::PushClip(FloatRect::new(1.0, 2.0, 3.0, 4.0)),+ PainterEvent::Pop+ ]+ );+ }+#[test]fn runs_sharing_a_stack_prefix_push_it_once_and_re_enter_their_space_after_a_pop() {let mut tree = identity_tree();diff --git a/Libraries/LibCompositing/Rust/src/display_list/tape.rs b/Libraries/LibCompositing/Rust/src/display_list/tape.rsindex f44808f7210..9c37c67e0b7 100644--- a/Libraries/LibCompositing/Rust/src/display_list/tape.rs+++ b/Libraries/LibCompositing/Rust/src/display_list/tape.rs@@ -7,7 +7,7 @@//! How records and runs are laid out on a display list tape. Only this crate reads these; C++ sees the command//! structs and opaque tapes, so these types stay out of the generated headers.-use super::commands::{ClipMode, ContextRef, DisplayListCommandType, DisplayListDataSpan};+use super::commands::{ClipMode, ContextRef, DisplayListCommandType, DisplayListDataSpan, SpatialNodeIndex};use crate::ffi_bytes_fields;use crate::ffi_enum_bytes;use libgfx_rust::*;@@ -54,6 +54,11 @@ pub struct DisplayListInlineClip {pub path_winding_rule: WindingRule,pub kind: InlineClipKind,pub mode: ClipMode,+ // Whether the clip is in `space` rather than in the space of the command it applies to. A command drawn in a+ // space above a scroll node can be clipped to a box that moves with the scroll offset this way.+ pub has_own_space: bool,+ pub space: SpatialNodeIndex,+ pub padding: u32,}ffi_bytes_fields!(DisplayListInlineClip {clip_rect_or_path_device_bounds,@@ -61,10 +66,12 @@ ffi_bytes_fields!(DisplayListInlineClip {path_data,path_winding_rule,kind,- mode+ mode,+ has_own_space,+ space});pub const INLINE_CLIP_ENTRY_SIZE: usize = std::mem::size_of::<DisplayListInlineClip>();-const _: () = assert!(INLINE_CLIP_ENTRY_SIZE == 64);+const _: () = assert!(INLINE_CLIP_ENTRY_SIZE == 72);#[derive(Clone, Copy, Debug, PartialEq)]#[repr(C)]diff --git a/Libraries/LibCompositing/Rust/src/display_list/validate.rs b/Libraries/LibCompositing/Rust/src/display_list/validate.rsindex 5df424e508b..677c2f11181 100644--- a/Libraries/LibCompositing/Rust/src/display_list/validate.rs+++ b/Libraries/LibCompositing/Rust/src/display_list/validate.rs@@ -605,6 +605,7 @@ fn validate_inline_clip(payload: Payload, offset: usize) -> Result<(), Validatiopayload.enum_at::<ClipMode>(offset + offset_of!(DisplayListInlineClip, mode))?;payload.enum_at::<WindingRule>(offset + offset_of!(DisplayListInlineClip, path_winding_rule))?;payload.span_at(offset + offset_of!(DisplayListInlineClip, path_data))?;+ payload.bool_at(offset + offset_of!(DisplayListInlineClip, has_own_space))?;Ok(())}diff --git a/Libraries/LibCompositing/Rust/src/ffi.rs b/Libraries/LibCompositing/Rust/src/ffi.rsindex d68675cf4df..bb9e1b66800 100644--- a/Libraries/LibCompositing/Rust/src/ffi.rs+++ b/Libraries/LibCompositing/Rust/src/ffi.rs@@ -196,7 +196,9 @@ pub unsafe extern "C" fn display_list_replay_records(};// SAFETY: The caller guarantees `callbacks` is live for the call.let mut painter = unsafe { *callbacks };- crate::display_list::replay::replay_records(records, scroll_offsets, &mut painter);+ // NB: Nested records share the context of the command they are nested in, and none of their inline clips has a+ // space of its own.+ crate::display_list::replay::replay_records(records, scroll_offsets, &|_| None, &mut painter);}/// # Safety