diff --git a/Cargo.lock b/Cargo.lock index a6378afc057..86f0fef0ee5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2751,6 +2751,7 @@ dependencies = [ "libc", "msg", "net_traits", + "once_cell", "parking_lot", "range", "rayon", @@ -3709,6 +3710,12 @@ dependencies = [ "objc", ] +[[package]] +name = "once_cell" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "891f486f630e5c5a4916c7e16c4b24a53e78c860b646e9f8e005e4f16847bfed" + [[package]] name = "opaque-debug" version = "0.2.1" diff --git a/components/layout_2020/Cargo.toml b/components/layout_2020/Cargo.toml index e28e482c13d..49c99b47661 100644 --- a/components/layout_2020/Cargo.toml +++ b/components/layout_2020/Cargo.toml @@ -25,6 +25,7 @@ ipc-channel = "0.12" libc = "0.2" msg = {path = "../msg"} net_traits = {path = "../net_traits"} +once_cell = "1.2.0" parking_lot = "0.9" range = {path = "../range"} rayon = "1" diff --git a/components/layout_2020/display_list.rs b/components/layout_2020/display_list.rs index 8d0ba44ca18..23aea622e8d 100644 --- a/components/layout_2020/display_list.rs +++ b/components/layout_2020/display_list.rs @@ -9,6 +9,7 @@ use embedder_traits::Cursor; use euclid::{Point2D, SideOffsets2D, Size2D, Vector2D}; use gfx::text::glyph::GlyphStore; use net_traits::image_cache::UsePlaceholder; +use once_cell::unsync::OnceCell; use std::sync::Arc; use style::dom::OpaqueNode; use style::properties::ComputedValues; @@ -128,12 +129,9 @@ struct BuilderForBoxFragment<'a> { fragment: &'a BoxFragment, containing_block: &'a Rect, border_rect: units::LayoutRect, - padding_rect: Option, + padding_rect: OnceCell, border_radius: wr::BorderRadius, - - // Outer `Option` is `None`: not initialized yet - // Inner `Option` is `None`: no border radius, no need to clip - border_edge_clip_id: Option>, + border_edge_clip_id: OnceCell>, } impl<'a> BuilderForBoxFragment<'a> { @@ -168,19 +166,17 @@ impl<'a> BuilderForBoxFragment<'a> { containing_block, border_rect, border_radius, - padding_rect: None, - border_edge_clip_id: None, + padding_rect: OnceCell::new(), + border_edge_clip_id: OnceCell::new(), } } - fn padding_rect(&mut self) -> &units::LayoutRect { - let fragment = &self.fragment; - let containing_block = &self.containing_block; - self.padding_rect.get_or_insert_with(|| { - fragment + fn padding_rect(&self) -> &units::LayoutRect { + self.padding_rect.get_or_init(|| { + self.fragment .padding_rect() - .to_physical(fragment.style.writing_mode, containing_block) - .translate(&containing_block.top_left) + .to_physical(self.fragment.style.writing_mode, self.containing_block) + .translate(&self.containing_block.top_left) .into() }) } @@ -190,18 +186,16 @@ impl<'a> BuilderForBoxFragment<'a> { builder: &mut DisplayListBuilder, common: &mut wr::CommonItemProperties, ) { - let border_radius = &self.border_radius; - let border_rect = &self.border_rect; - let initialized = self.border_edge_clip_id.get_or_insert_with(|| { - if border_radius.is_zero() { + let initialized = self.border_edge_clip_id.get_or_init(|| { + if self.border_radius.is_zero() { None } else { Some(builder.wr.define_clip( &builder.current_space_and_clip, - *border_rect, + self.border_rect, Some(wr::ComplexClipRegion { - rect: *border_rect, - radii: *border_radius, + rect: self.border_rect, + radii: self.border_radius, mode: wr::ClipMode::Clip, }), None,