Skip to content

Truly independent web browser

LibWeb: Measure flex items ahead on the style and layout threads

Aliaksandr Kalenikb7aa59873c2c
diff --git a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
index 16f33bfb717..c9b581f615d 100644
--- a/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
+++ b/Libraries/LibWeb/Rust/src/layout/fc_run_cache.rs
@@ -880,6 +880,39 @@ impl FcRunCacheAttempt {
})
}
+ pub(super) fn would_reuse_stored_run(
+ box_: Node,
+ builds_fragments: bool,
+ should_collect_devtools_layout_data: bool,
+ measurement_root_keeps_its_result: bool,
+ callbacks: &LayoutPass<'_>,
+ probe: &FcRunCacheProbe<'_>,
+ ) -> bool {
+ if !can_describe_run(
+ box_,
+ false,
+ should_collect_devtools_layout_data,
+ measurement_root_keeps_its_result,
+ callbacks,
+ probe,
+ ) {
+ return false;
+ }
+ let validity = run_root_validity(callbacks, box_);
+ callbacks
+ .fc_run_cache_store()
+ .first_entry_where_summary_allows(
+ box_.slot_index(),
+ |summary| summary.may_answer_probe(validity, builds_fragments),
+ |entry| {
+ entry.validity == validity
+ && (!builds_fragments || entry.outputs.root.is_some())
+ && entry.answers_probe_for(probe)
+ },
+ )
+ .is_some()
+ }
+
pub(super) fn previous_line_data(&self) -> Option<std::sync::Arc<inline_content::InlineContent>> {
let Self::Store {
structurally_damaged_entry: Some(entry),
diff --git a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
index df1a45a5eee..90c7468e383 100644
--- a/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/flex_formatting_context.rs
@@ -2229,6 +2229,19 @@ impl<'pass> FlexFormattingContext<'pass> {
self.flex_items[index].hypothetical_cross_size = css_clamp(size, min, max);
}
+ fn hypothetical_cross_size_is_fit_content_block_size(&self, index: usize) -> bool {
+ if self.cross_axis_is_horizontal()
+ || self.has_definite_cross_size(index)
+ || self.flex_item_details[index].inputs.preferred_aspect_ratio.is_some()
+ {
+ return false;
+ }
+ let computed = self.computed_cross_size(index);
+ !(computed.is_stretch() && self.cross_axis_stretch_basis().is_some())
+ && !computed.is_min_content()
+ && !computed.is_max_content()
+ }
+
fn fork_candidates(&self) -> Vec<usize> {
self.items_to_fork
.get_or_init(|| {
@@ -2239,6 +2252,57 @@ impl<'pass> FlexFormattingContext<'pass> {
.clone()
}
+ fn measure_content_inline_sizes_of_items_ahead(&self) {
+ let candidates = self.fork_candidates();
+ if candidates.is_empty() {
+ return;
+ }
+ let sizing = self.sizing();
+ let constraints = self.item_containing_block_constraints();
+ let inline_size_is_main_size = self.main_axis_is_horizontal();
+ let measurements = candidates
+ .into_iter()
+ .filter(|&index| {
+ if inline_size_is_main_size {
+ !self.has_definite_main_size(index)
+ } else {
+ !self.has_definite_cross_size(index)
+ }
+ })
+ .filter_map(|index| {
+ let cells = &self.flex_item_details[index].cells;
+ sizing.content_inline_size_measurement_ahead(
+ sizing_context::SizedBox::new(self.flex_items[index].box_, cells),
+ constraints,
+ cells.has_definite_block_size.then_some(cells.content_block_size),
+ )
+ })
+ .collect();
+ sizing.measure_content_inline_sizes_ahead(measurements);
+ }
+
+ fn lay_out_fit_content_cross_size_measurements_ahead(&self) {
+ let candidates = self.fork_candidates();
+ if candidates.is_empty() {
+ return;
+ }
+ let sizing = self.sizing();
+ let constraints = self.item_containing_block_constraints();
+ let shapes = candidates
+ .into_iter()
+ .filter(|&index| self.hypothetical_cross_size_is_fit_content_block_size(index))
+ .filter_map(|index| {
+ let inline_size = clamp_to_max_dimension_value(self.flex_items[index].main_size?);
+ sizing.max_content_block_size_run_shape(
+ sizing_context::SizedBox::new(self.flex_items[index].box_, &self.flex_item_details[index].cells),
+ inline_size,
+ constraints,
+ )
+ })
+ .collect();
+ parallel_layout::lay_out_shapes_ahead(&self.run.callbacks, shapes);
+ }
+
// https://drafts.csswg.org/css-flexbox/#hypothetical-cross-size
fn determine_hypothetical_cross_size_of_item(&mut self, index: usize) {
// Determine the hypothetical cross size of each item by performing layout as if it were an in-flow block-level box
@@ -4155,6 +4219,7 @@ impl<'pass> FlexFormattingContext<'pass> {
}
self.offer_runs_of_items_measured_in_their_main_axis();
+ self.measure_content_inline_sizes_of_items_ahead();
// 4. Determine the main size of the flex container
// Determine the main size of the flex container using the rules of the formatting context in which it participates.
@@ -4224,6 +4289,7 @@ impl<'pass> FlexFormattingContext<'pass> {
// Cross Size Determination
self.offer_runs_of_items_measured_at_their_main_size();
// 7. Determine the hypothetical cross size of each item
+ self.lay_out_fit_content_cross_size_measurements_ahead();
for index in 0..self.flex_items.len() {
self.determine_hypothetical_cross_size_of_item(index);
}
diff --git a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
index 7f5a3055b6b..34d22a75d6e 100644
--- a/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/formatting_context.rs
@@ -3303,6 +3303,17 @@ pub(crate) fn lay_out_child_run_job(callbacks: LayoutPass<'_>, job: ChildRunJob)
)
}
+pub(crate) fn shape_would_reuse_stored_run(callbacks: &LayoutPass<'_>, shape: &ChildRunShape) -> bool {
+ fc_run_cache::FcRunCacheAttempt::would_reuse_stored_run(
+ shape.node,
+ run_builds_fragments(&shape.input, false),
+ false,
+ true,
+ callbacks,
+ &fc_run_cache::FcRunCacheProbe::new(shape.fc_type, &shape.input, shape.root_cells),
+ )
+}
+
/// Hands what the layout of a child handed its placement to the run's fragment builder, where the child is laid out
/// but not placed.
pub(crate) fn leave_child_unplaced(
diff --git a/Libraries/LibWeb/Rust/src/layout/parallel_layout.rs b/Libraries/LibWeb/Rust/src/layout/parallel_layout.rs
index e9fd94988e7..e3de8562b10 100644
--- a/Libraries/LibWeb/Rust/src/layout/parallel_layout.rs
+++ b/Libraries/LibWeb/Rust/src/layout/parallel_layout.rs
@@ -93,6 +93,17 @@ pub(crate) fn fork_candidates<T: Copy>(
candidates
}
+pub(crate) fn lay_out_shapes_ahead(pass: &LayoutPass<'_>, shapes: Vec<formatting_context::ChildRunShape>) {
+ let _ = lay_out_in_parallel(
+ pass,
+ shapes,
+ |shape| shape.node,
+ |callbacks, shape| {
+ formatting_context::lay_out_shape_for_its_result(callbacks, shape);
+ },
+ );
+}
+
pub(crate) struct ForkedTask<R> {
pub(crate) result: R,
pub(crate) host_findings: run_state::HostFindings,
diff --git a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
index 33460e714bf..2a24bc3faf8 100644
--- a/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/sizing_context.rs
@@ -224,6 +224,29 @@ impl<'cells> SizedCells<'cells> {
}
}
+pub(crate) fn measure_ahead<J: Send>(
+ callbacks: &LayoutPass<'_>,
+ jobs: Vec<J>,
+ root_of: impl Fn(&J) -> Node + Sync,
+ measure: impl Fn(&SizingContext<'_, '_>, J) + Sync,
+) {
+ let _ = parallel_layout::lay_out_in_parallel(callbacks, jobs, &root_of, |callbacks, job| {
+ let root = root_of(&job);
+ RunState::with_content_inline_sizes_root(
+ callbacks,
+ root,
+ callbacks.in_flow_containing_block(root),
+ |state, _| measure(&SizingContext::new(state, callbacks), job),
+ );
+ });
+}
+
+pub(crate) struct ContentInlineSizeMeasurementAhead {
+ node: Node,
+ constraints: ContainingBlockConstraints,
+ block_size: Option<CssPixels>,
+}
+
pub(crate) struct SizingContext<'pass, 'container> {
state: &'pass RunState,
callbacks: LayoutPass<'pass>,
@@ -3223,6 +3246,61 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
None
}
+ pub(crate) fn content_inline_size_measurement_ahead(
+ &self,
+ sized: SizedBox<'_>,
+ constraints: ContainingBlockConstraints,
+ block_size: Option<CssPixels>,
+ ) -> Option<ContentInlineSizeMeasurementAhead> {
+ let node = sized.node;
+ let key = self.intrinsic_size_cache_key(block_size, constraints);
+ if self
+ .callbacks
+ .intrinsic_size_caches()
+ .intrinsic_inline_size_measurement_cache_get(node, key)
+ .is_some()
+ || self.content_inline_size_ignoring_content(node).is_some()
+ || (block_size.is_none()
+ && self
+ .definite_block_size_of_replaced_box_in_held_metrics(sized)
+ .is_some())
+ {
+ return None;
+ }
+ let answered_without_measuring = |kind| {
+ self.content_inline_size_answered_without_measuring_content(sized, constraints, block_size, kind)
+ .is_some()
+ };
+ if answered_without_measuring(IntrinsicSizeCacheKind::MinContentInline)
+ && answered_without_measuring(IntrinsicSizeCacheKind::MaxContentInline)
+ {
+ return None;
+ }
+ Some(ContentInlineSizeMeasurementAhead {
+ node,
+ constraints,
+ block_size,
+ })
+ }
+
+ pub(crate) fn measure_content_inline_sizes(&self, measurement: ContentInlineSizeMeasurementAhead) {
+ self.measure_intrinsic_inline_size(
+ measurement.node,
+ measurement.constraints,
+ measurement.block_size,
+ IntrinsicSizeCacheKind::MinContentInline,
+ );
+ }
+
+ pub(crate) fn measure_content_inline_sizes_ahead(&self, measurements: Vec<ContentInlineSizeMeasurementAhead>) {
+ measure_ahead(
+ &self.callbacks,
+ measurements,
+ |measurement| measurement.node,
+ |sizing, measurement| sizing.measure_content_inline_sizes(measurement),
+ );
+ }
+
// Answers from the measurement cache or from the first source that sizes `node`'s content, caching both of its
// content inline sizes. `kind` names the size the caller asked for first, which the trace reports.
fn measure_intrinsic_inline_size(
@@ -3363,12 +3441,51 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
value
}
- fn measure_block_size_with_normal_layout(
+ pub(crate) fn max_content_block_size_run_shape(
+ &self,
+ sized: SizedBox<'_>,
+ inline_size: CssPixels,
+ constraints: ContainingBlockConstraints,
+ ) -> Option<formatting_context::ChildRunShape> {
+ let node = sized.node;
+ let key = self.intrinsic_size_cache_key(Some(inline_size), constraints);
+ if self
+ .callbacks
+ .intrinsic_size_caches()
+ .max_content_block_size_cache_get(node, key)
+ .is_some()
+ || self.establishes_orthogonal_flow(node)
+ || self.facts(node).has_preferred_aspect_ratio()
+ {
+ return None;
+ }
+ let auto_size = self.auto_content_size(node);
+ if auto_size.height.is_some()
+ || self
+ .max_content_size_for_replaced_element_without_natural_size(
+ sized,
+ auto_size,
+ formatting_context::SizeDimension::Block,
+ formatting_context::ReplacedMaxContentSizeLimits::default(),
+ )
+ .is_some()
+ || (!self.has_children(node) && !self.has_size_without_children(node))
+ {
+ return None;
+ }
+ let shape = self
+ .child_run_shapes
+ .and_then(|child_run_shapes| child_run_shapes.child_run_shape_at(node, inline_size, constraints))
+ .unwrap_or_else(|| self.normal_layout_measurement_shape(node, inline_size, constraints));
+ (!formatting_context::shape_would_reuse_stored_run(&self.callbacks, &shape)).then_some(shape)
+ }
+
+ fn normal_layout_measurement_shape(
&self,
node: Node,
inline_size: CssPixels,
constraints: ContainingBlockConstraints,
- ) -> formatting_context::ChildLayoutResult {
+ ) -> formatting_context::ChildRunShape {
let mut root = used_values::cells_before_sizing(&self.callbacks, node, constraints);
root.has_definite_block_size = false;
root.set_content_inline_size(inline_size);
@@ -3384,7 +3501,19 @@ impl<'pass, 'container> SizingContext<'pass, 'container> {
formatting_context::independent_formatting_context_type(node, &self.callbacks),
formatting_context::FormattingContextType::Flex | formatting_context::FormattingContextType::Grid
);
- formatting_context::lay_out_measurement_root_for_its_result(self.callbacks, node, root, input)
+ formatting_context::ChildRunShape::of_measurement_root(&self.callbacks, node, root, input)
+ }
+
+ fn measure_block_size_with_normal_layout(
+ &self,
+ node: Node,
+ inline_size: CssPixels,
+ constraints: ContainingBlockConstraints,
+ ) -> formatting_context::ChildLayoutResult {
+ formatting_context::lay_out_shape_for_its_result(
+ self.callbacks,
+ self.normal_layout_measurement_shape(node, inline_size, constraints),
+ )
}
/// The min-content or max-content block size of a box that establishes an orthogonal flow: its block axis here is