gfx: Stop cloning clipping regions so much.

Clipping regions can contain vectors and so can be expensive to copy.
This commit is contained in:
Patrick Walton 2016-03-02 11:50:21 -08:00
parent 983576ebaa
commit 940bff1f9c
5 changed files with 52 additions and 52 deletions

View file

@ -761,16 +761,13 @@ impl ClippingRegion {
} }
} }
/// Returns the intersection of this clipping region and the given rectangle. /// Mutates this clipping region to intersect with the given rectangle.
/// ///
/// TODO(pcwalton): This could more eagerly eliminate complex clipping regions, at the cost of /// TODO(pcwalton): This could more eagerly eliminate complex clipping regions, at the cost of
/// complexity. /// complexity.
#[inline] #[inline]
pub fn intersect_rect(self, rect: &Rect<Au>) -> ClippingRegion { pub fn intersect_rect(&mut self, rect: &Rect<Au>) {
ClippingRegion { self.main = self.main.intersection(rect).unwrap_or(Rect::zero())
main: self.main.intersection(rect).unwrap_or(Rect::zero()),
complex: self.complex,
}
} }
/// Returns true if this clipping region might be nonempty. This can return false positives, /// Returns true if this clipping region might be nonempty. This can return false positives,

View file

@ -1954,8 +1954,10 @@ impl Flow for BlockFlow {
self.base.position.size.to_physical(self.base.writing_mode); self.base.position.size.to_physical(self.base.writing_mode);
// Compute the origin and clipping rectangle for children. // Compute the origin and clipping rectangle for children.
//
// `clip` is in the child coordinate system.
let mut clip;
let origin_for_children; let origin_for_children;
let clip_in_child_coordinate_system;
let is_stacking_context = self.fragment.establishes_stacking_context(); let is_stacking_context = self.fragment.establishes_stacking_context();
if is_stacking_context { if is_stacking_context {
// We establish a stacking context, so the position of our children is vertically // We establish a stacking context, so the position of our children is vertically
@ -1966,12 +1968,11 @@ impl Flow for BlockFlow {
// FIXME(pcwalton): Is this vertical-writing-direction-safe? // FIXME(pcwalton): Is this vertical-writing-direction-safe?
let margin = self.fragment.margin.to_physical(self.base.writing_mode); let margin = self.fragment.margin.to_physical(self.base.writing_mode);
origin_for_children = Point2D::new(-margin.left, Au(0)); origin_for_children = Point2D::new(-margin.left, Au(0));
clip_in_child_coordinate_system = clip = self.base.clip.translate(&-self.base.stacking_relative_position);
self.base.clip.translate(&-self.base.stacking_relative_position);
} else { } else {
let relative_offset = relative_offset.to_physical(self.base.writing_mode); let relative_offset = relative_offset.to_physical(self.base.writing_mode);
origin_for_children = self.base.stacking_relative_position + relative_offset; origin_for_children = self.base.stacking_relative_position + relative_offset;
clip_in_child_coordinate_system = self.base.clip.clone(); clip = self.base.clip.clone();
} }
let stacking_relative_position_of_display_port_for_children = let stacking_relative_position_of_display_port_for_children =
@ -2003,8 +2004,8 @@ impl Flow for BlockFlow {
.early_absolute_position_info .early_absolute_position_info
.relative_containing_block_mode, .relative_containing_block_mode,
CoordinateSystem::Own); CoordinateSystem::Own);
let clip = self.fragment.clipping_region_for_children( self.fragment.adjust_clipping_region_for_children(
&clip_in_child_coordinate_system, &mut clip,
&stacking_relative_border_box, &stacking_relative_border_box,
self.base.flags.contains(IS_ABSOLUTELY_POSITIONED)); self.base.flags.contains(IS_ABSOLUTELY_POSITIONED));

View file

@ -231,19 +231,17 @@ pub trait FragmentDisplayListBuilding {
clip: &ClippingRegion, clip: &ClippingRegion,
stacking_relative_display_port: &Rect<Au>); stacking_relative_display_port: &Rect<Au>);
/// Returns the appropriate clipping region for descendants of this fragment. /// Adjusts the clipping region for descendants of this fragment as appropriate.
fn clipping_region_for_children(&self, fn adjust_clipping_region_for_children(&self,
current_clip: &ClippingRegion, current_clip: &mut ClippingRegion,
stacking_relative_border_box: &Rect<Au>, stacking_relative_border_box: &Rect<Au>,
is_absolutely_positioned: bool) is_absolutely_positioned: bool);
-> ClippingRegion;
/// Calculates the clipping rectangle for a fragment, taking the `clip` property into account /// Adjusts the clipping rectangle for a fragment to take the `clip` property into account
/// per CSS 2.1 § 11.1.2. /// per CSS 2.1 § 11.1.2.
fn calculate_style_specified_clip(&self, fn adjust_clip_for_style(&self,
parent_clip: &ClippingRegion, parent_clip: &mut ClippingRegion,
stacking_relative_border_box: &Rect<Au>) stacking_relative_border_box: &Rect<Au>);
-> ClippingRegion;
/// Builds the display items necessary to paint the selection and/or caret for this fragment, /// Builds the display items necessary to paint the selection and/or caret for this fragment,
/// if any. /// if any.
@ -481,7 +479,8 @@ impl FragmentDisplayListBuilding for Fragment {
// Clip. // Clip.
// //
// TODO: Check the bounds to see if a clip item is actually required. // TODO: Check the bounds to see if a clip item is actually required.
let clip = clip.clone().intersect_rect(&bounds); let mut clip = clip.clone();
clip.intersect_rect(&bounds);
// Background image should be positioned on the padding box basis. // Background image should be positioned on the padding box basis.
let border = style.logical_border_width().to_physical(style.writing_mode); let border = style.logical_border_width().to_physical(style.writing_mode);
@ -580,7 +579,8 @@ impl FragmentDisplayListBuilding for Fragment {
clip: &ClippingRegion, clip: &ClippingRegion,
gradient: &LinearGradient, gradient: &LinearGradient,
style: &ComputedValues) { style: &ComputedValues) {
let clip = clip.clone().intersect_rect(absolute_bounds); let mut clip = clip.clone();
clip.intersect_rect(absolute_bounds);
// This is the distance between the center and the ending point; i.e. half of the distance // This is the distance between the center and the ending point; i.e. half of the distance
// between the starting point and the ending point. // between the starting point and the ending point.
@ -893,15 +893,14 @@ impl FragmentDisplayListBuilding for Fragment {
}), DisplayListSection::Content); }), DisplayListSection::Content);
} }
fn calculate_style_specified_clip(&self, fn adjust_clip_for_style(&self,
parent_clip: &ClippingRegion, parent_clip: &mut ClippingRegion,
stacking_relative_border_box: &Rect<Au>) stacking_relative_border_box: &Rect<Au>) {
-> ClippingRegion {
// Account for `clip` per CSS 2.1 § 11.1.2. // Account for `clip` per CSS 2.1 § 11.1.2.
let style_clip_rect = match (self.style().get_box().position, let style_clip_rect = match (self.style().get_box().position,
self.style().get_effects().clip.0) { self.style().get_effects().clip.0) {
(position::T::absolute, Some(style_clip_rect)) => style_clip_rect, (position::T::absolute, Some(style_clip_rect)) => style_clip_rect,
_ => return (*parent_clip).clone(), _ => return,
}; };
// FIXME(pcwalton, #2795): Get the real container size. // FIXME(pcwalton, #2795): Get the real container size.
@ -910,7 +909,7 @@ impl FragmentDisplayListBuilding for Fragment {
let right = style_clip_rect.right.unwrap_or(stacking_relative_border_box.size.width); let right = style_clip_rect.right.unwrap_or(stacking_relative_border_box.size.width);
let bottom = style_clip_rect.bottom.unwrap_or(stacking_relative_border_box.size.height); let bottom = style_clip_rect.bottom.unwrap_or(stacking_relative_border_box.size.height);
let clip_size = Size2D::new(right - clip_origin.x, bottom - clip_origin.y); let clip_size = Size2D::new(right - clip_origin.x, bottom - clip_origin.y);
(*parent_clip).clone().intersect_rect(&Rect::new(clip_origin, clip_size)) parent_clip.intersect_rect(&Rect::new(clip_origin, clip_size))
} }
fn build_display_items_for_selection_if_necessary(&self, fn build_display_items_for_selection_if_necessary(&self,
@ -995,7 +994,8 @@ impl FragmentDisplayListBuilding for Fragment {
// Calculate the clip rect. If there's nothing to render at all, don't even construct // Calculate the clip rect. If there's nothing to render at all, don't even construct
// display list items. // display list items.
let clip = self.calculate_style_specified_clip(clip, &stacking_relative_border_box); let mut clip = (*clip).clone();
self.adjust_clip_for_style(&mut clip, &stacking_relative_border_box);
if !clip.might_intersect_rect(&stacking_relative_border_box) { if !clip.might_intersect_rect(&stacking_relative_border_box) {
return; return;
} }
@ -1424,19 +1424,17 @@ impl FragmentDisplayListBuilding for Fragment {
layer_info)) layer_info))
} }
fn clipping_region_for_children(&self, fn adjust_clipping_region_for_children(&self,
current_clip: &ClippingRegion, current_clip: &mut ClippingRegion,
stacking_relative_border_box: &Rect<Au>, stacking_relative_border_box: &Rect<Au>,
is_absolutely_positioned: bool) is_absolutely_positioned: bool) {
-> ClippingRegion {
// Don't clip if we're text. // Don't clip if we're text.
if self.is_scanned_text_fragment() { if self.is_scanned_text_fragment() {
return (*current_clip).clone() return
} }
// Account for style-specified `clip`. // Account for style-specified `clip`.
let mut current_clip = self.calculate_style_specified_clip(current_clip, self.adjust_clip_for_style(current_clip, stacking_relative_border_box);
stacking_relative_border_box);
// Clip according to the values of `overflow-x` and `overflow-y`. // Clip according to the values of `overflow-x` and `overflow-y`.
// //
@ -1452,7 +1450,7 @@ impl FragmentDisplayListBuilding for Fragment {
let max_x = cmp::min(bounds.max_x(), stacking_relative_border_box.max_x()); let max_x = cmp::min(bounds.max_x(), stacking_relative_border_box.max_x());
bounds.origin.x = cmp::max(bounds.origin.x, stacking_relative_border_box.origin.x); bounds.origin.x = cmp::max(bounds.origin.x, stacking_relative_border_box.origin.x);
bounds.size.width = max_x - bounds.origin.x; bounds.size.width = max_x - bounds.origin.x;
current_clip = current_clip.intersect_rect(&bounds) current_clip.intersect_rect(&bounds)
} }
_ => {} _ => {}
} }
@ -1464,12 +1462,10 @@ impl FragmentDisplayListBuilding for Fragment {
let max_y = cmp::min(bounds.max_y(), stacking_relative_border_box.max_y()); let max_y = cmp::min(bounds.max_y(), stacking_relative_border_box.max_y());
bounds.origin.y = cmp::max(bounds.origin.y, stacking_relative_border_box.origin.y); bounds.origin.y = cmp::max(bounds.origin.y, stacking_relative_border_box.origin.y);
bounds.size.height = max_y - bounds.origin.y; bounds.size.height = max_y - bounds.origin.y;
current_clip = current_clip.intersect_rect(&bounds) current_clip.intersect_rect(&bounds)
} }
_ => {} _ => {}
} }
current_clip
} }
fn build_display_list_for_text_fragment(&self, fn build_display_list_for_text_fragment(&self,
@ -1715,10 +1711,14 @@ impl BlockFlowDisplayListBuilding for BlockFlow {
}; };
// Add the box that starts the block context. // Add the box that starts the block context.
let clip = if self.fragment.establishes_stacking_context() { let translated_clip = if self.fragment.establishes_stacking_context() {
self.base.clip.translate(&-self.base.stacking_relative_position) Some(self.base.clip.translate(&-self.base.stacking_relative_position))
} else { } else {
self.base.clip.clone() None
};
let clip = match translated_clip {
Some(ref translated_clip) => translated_clip,
None => &self.base.clip,
}; };
self.fragment self.fragment
@ -1732,7 +1732,7 @@ impl BlockFlowDisplayListBuilding for BlockFlow {
.relative_containing_block_mode, .relative_containing_block_mode,
border_painting_mode, border_painting_mode,
background_border_section, background_border_section,
&clip, clip,
&self.base.stacking_relative_position_of_display_port); &self.base.stacking_relative_position_of_display_port);
// Add children. // Add children.

View file

@ -1184,7 +1184,8 @@ impl BaseFlow {
for item in items.iter() { for item in items.iter() {
let base_item = item.item.base(); let base_item = item.item.base();
let paint_bounds = base_item.clip.clone().intersect_rect(&base_item.bounds); let mut paint_bounds = base_item.clip.clone();
paint_bounds.intersect_rect(&base_item.bounds);
if !paint_bounds.might_be_nonempty() { if !paint_bounds.might_be_nonempty() {
continue; continue;
} }

View file

@ -1680,9 +1680,10 @@ impl Flow for InlineFlow {
CoordinateSystem::Parent); CoordinateSystem::Parent);
let stacking_relative_content_box = let stacking_relative_content_box =
fragment.stacking_relative_content_box(&stacking_relative_border_box); fragment.stacking_relative_content_box(&stacking_relative_border_box);
let clip = fragment.clipping_region_for_children(&self.base.clip, let mut clip = self.base.clip.clone();
&stacking_relative_border_box, fragment.adjust_clipping_region_for_children(&mut clip,
false); &stacking_relative_border_box,
false);
let is_positioned = fragment.is_positioned(); let is_positioned = fragment.is_positioned();
match fragment.specific { match fragment.specific {
SpecificFragmentInfo::InlineBlock(ref mut info) => { SpecificFragmentInfo::InlineBlock(ref mut info) => {