Skip to content

Truly independent web browser

LibWeb: Share the layout-input style comparison with Rust

Aliaksandr Kalenika5e53b7e18cc
diff --git a/Libraries/LibWeb/CSS/ComputedValues.cpp b/Libraries/LibWeb/CSS/ComputedValues.cpp
index 13bd7c45012..05da4e2c969 100644
--- a/Libraries/LibWeb/CSS/ComputedValues.cpp
+++ b/Libraries/LibWeb/CSS/ComputedValues.cpp
@@ -139,76 +139,11 @@ static_assert(to_underlying(MathShift::Compact) == 1);
static_assert(to_underlying(MathStyle::Normal) == 0);
static_assert(to_underlying(MathStyle::Compact) == 1);
-bool ComputedValues::layout_affecting_group_payloads_differ(void const* const* a, void const* const* b)
-{
- auto differs = [&]<typename T>() {
- auto const* mine = static_cast<T const*>(a[T::style_group_index]);
- auto const* theirs = static_cast<T const*>(b[T::style_group_index]);
- if (mine == theirs)
- return false;
- // OPTIMIZATION: Paint values share groups with text metrics, border dimensions and containing-block effects.
- // Changing those paint values does not invalidate intrinsic sizes.
- if constexpr (IsSame<T, InheritedTextValues>) {
- auto comparable = *mine;
- comparable.color = theirs->color;
- comparable.color_style_value = theirs->color_style_value;
- comparable.webkit_text_fill_color = theirs->webkit_text_fill_color;
- comparable.webkit_text_fill_color_is_current_color = theirs->webkit_text_fill_color_is_current_color;
- return !(comparable == *theirs);
- } else if constexpr (IsSame<T, BorderValues>) {
- auto comparable = *mine;
- comparable.border_left.color = theirs->border_left.color;
- comparable.border_top.color = theirs->border_top.color;
- comparable.border_right.color = theirs->border_right.color;
- comparable.border_bottom.color = theirs->border_bottom.color;
- comparable.border_left_color_style_value = theirs->border_left_color_style_value;
- comparable.border_top_color_style_value = theirs->border_top_color_style_value;
- comparable.border_right_color_style_value = theirs->border_right_color_style_value;
- comparable.border_bottom_color_style_value = theirs->border_bottom_color_style_value;
- return !(comparable == *theirs);
- } else if constexpr (IsSame<T, EffectsValues>) {
- auto comparable = *mine;
- comparable.opacity = theirs->opacity;
- comparable.opacity_style_value = theirs->opacity_style_value;
- return !(comparable == *theirs);
- } else if constexpr (IsSame<T, MiscResetValues>) {
- auto comparable = *mine;
- comparable.outline_color = theirs->outline_color;
- comparable.column_rule_color = theirs->column_rule_color;
- auto only_paint_hints = [](auto const& hints) {
- for (auto property : ReadonlySpan<u16> { hints.pointer, hints.length }) {
- if (property != to_underlying(PropertyID::Color) && property != to_underlying(PropertyID::Opacity))
- return false;
- }
- return true;
- };
- if (only_paint_hints(mine->implicit_will_change) && only_paint_hints(theirs->implicit_will_change))
- comparable.implicit_will_change = theirs->implicit_will_change;
- return !(comparable == *theirs);
- } else if constexpr (IsSame<T, InheritedUIValues>) {
- auto comparable = *mine;
- comparable.caret_color = theirs->caret_color;
- comparable.accent_color = theirs->accent_color;
- comparable.scrollbar_color = theirs->scrollbar_color;
- return !(comparable == *theirs);
- }
- return !(*mine == *theirs);
- };
-#define LIBWEB_COMPARE_STYLE_GROUP_PAYLOAD(name, path, sharing_name, affects_layout) \
- if constexpr (affects_layout) { \
- if (differs.template operator()<name>()) \
- return true; \
- }
- LIBWEB_ENUMERATE_COMPUTED_VALUE_STYLE_GROUPS(LIBWEB_COMPARE_STYLE_GROUP_PAYLOAD)
-#undef LIBWEB_COMPARE_STYLE_GROUP_PAYLOAD
- return false;
-}
-
void const* ComputedValues::style_group_payload(StyleGroupIndex group) const
{
switch (group) {
-#define LIBWEB_STYLE_GROUP_PAYLOAD_CASE(name, path, sharing_name, affects_layout) \
- case StyleGroupIndex::name: \
+#define LIBWEB_STYLE_GROUP_PAYLOAD_CASE(name, path, sharing_name) \
+ case StyleGroupIndex::name: \
return &*path;
LIBWEB_ENUMERATE_COMPUTED_VALUE_STYLE_GROUPS(LIBWEB_STYLE_GROUP_PAYLOAD_CASE)
#undef LIBWEB_STYLE_GROUP_PAYLOAD_CASE
diff --git a/Libraries/LibWeb/CSS/ComputedValues.h b/Libraries/LibWeb/CSS/ComputedValues.h
index 8d8f0c1d4ff..d005d3dc812 100644
--- a/Libraries/LibWeb/CSS/ComputedValues.h
+++ b/Libraries/LibWeb/CSS/ComputedValues.h
@@ -288,38 +288,31 @@ inline Gfx::InterpolationColorSpace to_interpolation_color_space(ColorInterpolat
}
// Every ComputedValues style value group, in vtable registration order:
-// G(enumerator, member path, sharing-info name, affects layout).
-// A group may be flagged as not affecting layout only when every one of its fields is
-// read exclusively at paint or display-list build time: background qualifies (paint;
-// resource-observer registration runs in apply_style regardless of layout), mask
-// qualifies (paint and hit-testing), and text_reset qualifies (text-decoration resolves
-// at display-list build; white-space-trim is unread by layout today, so implementing it
-// must revisit the flag). effects stays layout-affecting because filter/backdrop-filter
-// establish fixed-positioning containing blocks and re-parent abspos descendants.
-#define LIBWEB_ENUMERATE_COMPUTED_VALUE_STYLE_GROUPS(G) \
- G(InheritedTableValues, m_inherited.table, "inheritedTable", true) \
- G(InheritedListValues, m_inherited.list, "inheritedList", true) \
- G(InheritedUIValues, m_inherited.ui, "inheritedUI", true) \
- G(InheritedSVGValues, m_inherited.svg, "inheritedSVG", true) \
- G(InheritedTextValues, m_inherited.text, "inheritedText", true) \
- G(InheritedBoxValues, m_inherited.box, "inheritedBox", true) \
- G(FontValues, m_inherited.font, "font", true) \
- G(AnimationValues, m_noninherited.animation, "animation", true) \
- G(SVGResetValues, m_noninherited.svg_reset, "svgReset", true) \
- G(GridValues, m_noninherited.grid, "grid", true) \
- G(AnchorValues, m_noninherited.anchor, "anchor", true) \
- G(EffectsValues, m_noninherited.effects, "effects", true) \
- G(MaskValues, m_noninherited.mask_data, "mask", false) \
- G(TextResetValues, m_noninherited.text_reset, "textReset", false) \
- G(ContentValues, m_noninherited.content_data, "content", true) \
- G(TransformValues, m_noninherited.transform, "transform", true) \
- G(BackgroundValues, m_noninherited.background, "background", false) \
- G(BorderValues, m_noninherited.border, "border", true) \
- G(AlignmentValues, m_noninherited.alignment, "alignment", true) \
- G(MiscResetValues, m_noninherited.misc, "miscReset", true) \
- G(SizingValues, m_noninherited.sizing, "sizing", true) \
- G(SurroundValues, m_noninherited.surround, "surround", true) \
- G(BoxValues, m_noninherited.box, "box", true)
+// G(enumerator, member path, sharing-info name).
+#define LIBWEB_ENUMERATE_COMPUTED_VALUE_STYLE_GROUPS(G) \
+ G(InheritedTableValues, m_inherited.table, "inheritedTable") \
+ G(InheritedListValues, m_inherited.list, "inheritedList") \
+ G(InheritedUIValues, m_inherited.ui, "inheritedUI") \
+ G(InheritedSVGValues, m_inherited.svg, "inheritedSVG") \
+ G(InheritedTextValues, m_inherited.text, "inheritedText") \
+ G(InheritedBoxValues, m_inherited.box, "inheritedBox") \
+ G(FontValues, m_inherited.font, "font") \
+ G(AnimationValues, m_noninherited.animation, "animation") \
+ G(SVGResetValues, m_noninherited.svg_reset, "svgReset") \
+ G(GridValues, m_noninherited.grid, "grid") \
+ G(AnchorValues, m_noninherited.anchor, "anchor") \
+ G(EffectsValues, m_noninherited.effects, "effects") \
+ G(MaskValues, m_noninherited.mask_data, "mask") \
+ G(TextResetValues, m_noninherited.text_reset, "textReset") \
+ G(ContentValues, m_noninherited.content_data, "content") \
+ G(TransformValues, m_noninherited.transform, "transform") \
+ G(BackgroundValues, m_noninherited.background, "background") \
+ G(BorderValues, m_noninherited.border, "border") \
+ G(AlignmentValues, m_noninherited.alignment, "alignment") \
+ G(MiscResetValues, m_noninherited.misc, "miscReset") \
+ G(SizingValues, m_noninherited.sizing, "sizing") \
+ G(SurroundValues, m_noninherited.surround, "surround") \
+ G(BoxValues, m_noninherited.box, "box")
class StyleEngine;
@@ -435,10 +428,6 @@ public:
};
static Statistics const& statistics() { return s_statistics; }
- // The same question answered straight from two style records' group payload arrays, so a caller
- // that only wants the answer does not have to materialize a ComputedValues for either record.
- static bool layout_affecting_group_payloads_differ(void const* const* a, void const* const* b);
-
// Returns the Rust-owned payload for direct read-only layout access. The
// pointer is borrowed from this immutable ComputedValues instance.
void const* style_group_payload(StyleGroupIndex) const;
@@ -450,7 +439,7 @@ public:
template<typename Callback>
void for_each_style_group_sharing_state(ComputedValues const* parent, Callback callback) const
{
-#define LIBWEB_VISIT_STYLE_GROUP(name, path, sharing_name, affects_layout) \
+#define LIBWEB_VISIT_STYLE_GROUP(name, path, sharing_name) \
callback(sharing_name##sv, parent ? path.ptr_equals(parent->path) : false, path.is_default());
LIBWEB_ENUMERATE_COMPUTED_VALUE_STYLE_GROUPS(LIBWEB_VISIT_STYLE_GROUP)
#undef LIBWEB_VISIT_STYLE_GROUP
@@ -664,11 +653,6 @@ public:
static constexpr size_t style_group_index = to_underlying(StyleGroupIndex::InheritedListValues);
RefPtr<AbstractImageStyleValue const> list_style_image_value() const;
-
- bool operator==(InheritedListValues const& other) const
- {
- return ComputedValuesFFI::rust_style_group_payloads_equal(style_group_index, this, &other);
- }
};
struct InheritedUIValues : ComputedValuesFFI::InheritedUIValues {
@@ -692,11 +676,6 @@ public:
static_assert(alignof(Utf16FlyString) == alignof(size_t));
return { reinterpret_cast<Utf16FlyString const*>(color_schemes.pointer), color_schemes.length };
}
-
- bool operator==(InheritedUIValues const& other) const
- {
- return ComputedValuesFFI::rust_style_group_payloads_equal(style_group_index, this, &other);
- }
};
struct InheritedSVGValues : ComputedValuesFFI::InheritedSVGValues {
@@ -728,11 +707,6 @@ public:
static_cast<PaintOrder>(paint_order[2]),
};
}
-
- bool operator==(InheritedSVGValues const& other) const
- {
- return ComputedValuesFFI::rust_style_group_payloads_equal(style_group_index, this, &other);
- }
};
struct InheritedTextValues : ComputedValuesFFI::InheritedTextValues {
@@ -749,11 +723,6 @@ public:
return { reinterpret_cast<ShadowData const*>(text_shadow.pointer), text_shadow.length };
}
TextTransform text_transform_value() const { return static_cast<TextTransform>(text_transform); }
-
- bool operator==(InheritedTextValues const& other) const
- {
- return ComputedValuesFFI::rust_style_group_payloads_equal(style_group_index, this, &other);
- }
};
// The layout and lifecycle of this group are defined in Rust (computed_values.rs). The
@@ -785,11 +754,6 @@ public:
WEB_API Gfx::FontCascadeList const& font_list_value() const;
FontStyleKeyword font_style_keyword() const;
-
- bool operator==(FontValues const& other) const
- {
- return ComputedValuesFFI::rust_style_group_payloads_equal(style_group_index, this, &other);
- }
};
private:
@@ -823,30 +787,15 @@ public:
static constexpr size_t style_group_index = to_underlying(StyleGroupIndex::AnimationValues);
Vector<Utf16FlyString> animation_names_value() const;
-
- bool operator==(AnimationValues const& other) const
- {
- return ComputedValuesFFI::rust_style_group_payloads_equal(style_group_index, this, &other);
- }
};
// The layout and lifecycle of this group are defined in Rust (computed_values.rs).
struct SVGResetValues : ComputedValuesFFI::SVGResetValues {
static constexpr size_t style_group_index = to_underlying(StyleGroupIndex::SVGResetValues);
-
- bool operator==(SVGResetValues const& other) const
- {
- return ComputedValuesFFI::rust_style_group_payloads_equal(style_group_index, this, &other);
- }
};
struct GridValues : ComputedValuesFFI::GridValues {
static constexpr size_t style_group_index = to_underlying(StyleGroupIndex::GridValues);
-
- bool operator==(GridValues const& other) const
- {
- return ComputedValuesFFI::rust_style_group_payloads_equal(style_group_index, this, &other);
- }
};
struct AnchorValues : ComputedValuesFFI::AnchorValues {
@@ -854,11 +803,6 @@ public:
ReadonlySpan<Utf16FlyString> anchor_names_span() const { return fly_strings(anchor_names); }
- bool operator==(AnchorValues const& other) const
- {
- return ComputedValuesFFI::rust_style_group_payloads_equal(style_group_index, this, &other);
- }
-
private:
static ReadonlySpan<Utf16FlyString> fly_strings(ComputedValuesFFI::RetainedUtf16FlyStringList const& values)
{
@@ -881,11 +825,6 @@ public:
static_assert(alignof(ShadowData) == alignof(ComputedValuesFFI::ComputedShadow));
return { reinterpret_cast<ShadowData const*>(box_shadows.pointer), box_shadows.length };
}
-
- bool operator==(EffectsValues const& other) const
- {
- return ComputedValuesFFI::rust_style_group_payloads_equal(style_group_index, this, &other);
- }
};
struct MaskValues : ComputedValuesFFI::MaskValues {
@@ -898,11 +837,6 @@ public:
// https://drafts.fxtf.org/css-masking/#the-clip-path
// TODO: Support basic shapes and geometry boxes.
Optional<URL> clip_path_value() const;
-
- bool operator==(MaskValues const& other) const
- {
- return ComputedValuesFFI::rust_style_group_payloads_equal(style_group_index, this, &other);
- }
};
struct TextResetValues : ComputedValuesFFI::TextResetValues {
@@ -913,11 +847,6 @@ public:
static_assert(sizeof(TextDecorationLine) == sizeof(u8));
return { reinterpret_cast<TextDecorationLine const*>(text_decoration_lines.pointer), text_decoration_lines.length };
}
-
- bool operator==(TextResetValues const& other) const
- {
- return ComputedValuesFFI::rust_style_group_payloads_equal(style_group_index, this, &other);
- }
};
struct ContentValues : ComputedValuesFFI::ContentValues {
@@ -925,11 +854,6 @@ public:
NonnullRefPtr<StyleValue const> computed_content_value() const;
bool content_is_normal() const;
-
- bool operator==(ContentValues const& other) const
- {
- return ComputedValuesFFI::rust_style_group_payloads_equal(style_group_index, this, &other);
- }
};
struct TransformValues : public ComputedValuesFFI::TransformValues {
@@ -1000,10 +924,6 @@ public:
return {};
return CSSPixels::from_raw(perspective_px);
}
- bool operator==(TransformValues const& other) const
- {
- return ComputedValuesFFI::rust_style_group_payloads_equal(style_group_index, this, &other);
- }
private:
static RefPtr<TransformationStyleValue const> transformation_value(ComputedValuesFFI::ComputedStyleValueHandle const& handle)
@@ -1020,11 +940,6 @@ public:
Color background_color_value() const { return Color::from_bgra(background_color); }
Vector<RefPtr<AbstractImageStyleValue const>> background_images_value() const;
-
- bool operator==(BackgroundValues const& other) const
- {
- return ComputedValuesFFI::rust_style_group_payloads_equal(style_group_index, this, &other);
- }
};
struct BorderValues : ComputedValuesFFI::BorderValues {
@@ -1035,20 +950,10 @@ public:
BorderData const& border_right_value() const { return reinterpret_cast<BorderData const&>(border_right); }
BorderData const& border_bottom_value() const { return reinterpret_cast<BorderData const&>(border_bottom); }
RefPtr<AbstractImageStyleValue const> border_image_source_value() const;
-
- bool operator==(BorderValues const& other) const
- {
- return ComputedValuesFFI::rust_style_group_payloads_equal(style_group_index, this, &other);
- }
};
struct AlignmentValues : ComputedValuesFFI::AlignmentValues {
static constexpr size_t style_group_index = to_underlying(StyleGroupIndex::AlignmentValues);
-
- bool operator==(AlignmentValues const& other) const
- {
- return ComputedValuesFFI::rust_style_group_payloads_equal(style_group_index, this, &other);
- }
};
struct MiscResetValues : ComputedValuesFFI::MiscResetValues {
@@ -1056,29 +961,14 @@ public:
Optional<Utf16FlyString> view_transition_name_value() const;
ScrollSnapStrictness scroll_snap_strictness_value() const { return static_cast<ScrollSnapStrictness>(scroll_snap_strictness); }
-
- bool operator==(MiscResetValues const& other) const
- {
- return ComputedValuesFFI::rust_style_group_payloads_equal(style_group_index, this, &other);
- }
};
struct SizingValues : ComputedValuesFFI::SizingValues {
static constexpr size_t style_group_index = to_underlying(StyleGroupIndex::SizingValues);
-
- bool operator==(SizingValues const& other) const
- {
- return ComputedValuesFFI::rust_style_group_payloads_equal(style_group_index, this, &other);
- }
};
struct SurroundValues : ComputedValuesFFI::SurroundValues {
static constexpr size_t style_group_index = to_underlying(StyleGroupIndex::SurroundValues);
-
- bool operator==(SurroundValues const& other) const
- {
- return ComputedValuesFFI::rust_style_group_payloads_equal(style_group_index, this, &other);
- }
};
struct BoxValues : ComputedValuesFFI::BoxValues {
@@ -1088,11 +978,6 @@ public:
Float float_value() const { return static_cast<Float>(float_); }
Clear clear_value() const { return static_cast<Clear>(clear); }
Positioning position_value() const { return static_cast<Positioning>(position); }
-
- bool operator==(BoxValues const& other) const
- {
- return ComputedValuesFFI::rust_style_group_payloads_equal(style_group_index, this, &other);
- }
};
private:
diff --git a/Libraries/LibWeb/Layout/Node.cpp b/Libraries/LibWeb/Layout/Node.cpp
index 761e45d74f1..93fc5c4b88e 100644
--- a/Libraries/LibWeb/Layout/Node.cpp
+++ b/Libraries/LibWeb/Layout/Node.cpp
@@ -529,16 +529,12 @@ void NodeWithStyle::set_style_record_identity(CSS::InstalledStyle const& install
? held_before.view()
: document().style_computer().style_engine().style_record_view(held_read(), m_style_record_identity);
}
- // NB: SVG geometry can depend on currentColor strokes and animation composition. Keep its conservative reset.
- bool svg_style_may_affect_intrinsic_sizes = is_svg_box()
- && (old_record_view.animation_overlay_identity != 0
- || new_record_view.animation_overlay_identity != 0
- || (old_record_view.present
- && static_cast<CSS::ComputedValues::InheritedTextValues const*>(old_record_view.payloads[to_underlying(CSS::StyleGroupIndex::InheritedTextValues)])->color
- != static_cast<CSS::ComputedValues::InheritedTextValues const*>(new_record_view.payloads[to_underlying(CSS::StyleGroupIndex::InheritedTextValues)])->color));
+ // NB: SVG geometry can depend on animation composition. Keep its conservative reset.
+ bool svg_animation_may_affect_intrinsic_sizes = is_svg_box()
+ && (old_record_view.animation_overlay_identity != 0 || new_record_view.animation_overlay_identity != 0);
bool changes_layout_affecting_style = !old_record_view.present
- || svg_style_may_affect_intrinsic_sizes
- || CSS::ComputedValues::layout_affecting_group_payloads_differ(old_record_view.payloads, new_record_view.payloads);
+ || svg_animation_may_affect_intrinsic_sizes
+ || !CSS::ComputedValuesFFI::rust_style_payloads_layout_inputs_equal(old_record_view.payloads, new_record_view.payloads, is_svg_box());
release_pinned_style_record();
m_background_images.clear();
diff --git a/Libraries/LibWeb/Rust/src/css/computed_values.rs b/Libraries/LibWeb/Rust/src/css/computed_values.rs
index 51c1ea4679c..5cd03c02e45 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_values.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_values.rs
@@ -406,6 +406,29 @@ macro_rules! impl_computed_payload_clone_and_eq {
impl_content_hash_by_field!($type { $($field),+ });
};
+ // A style group whose payload holds values that layout does not read, each followed by `=> paint_only`, or values
+ // that layout reads only in part, each followed by `=>` and the function that compares that part.
+ ($type:ty { $($field:ident $(=> $layout_comparison:ident)?),+ $(,)? }) => {
+ impl_computed_payload_clone_and_eq!($type { $($field),+ });
+
+ impl StyleGroupLayoutInputs for $type {
+ fn layout_inputs_equal(&self, other: &Self) -> bool {
+ true $(&& layout_input_equal!(self.$field, other.$field $(, $layout_comparison)?))+
+ }
+ }
+ };
+}
+
+macro_rules! layout_input_equal {
+ ($first:expr, $second:expr) => {
+ $first == $second
+ };
+ ($first:expr, $second:expr, paint_only) => {
+ true
+ };
+ ($first:expr, $second:expr, $layout_inputs_equal:ident) => {
+ $layout_inputs_equal(&$first, &$second)
+ };
}
impl_computed_payload_clone_and_eq!(SizingValues {
@@ -637,11 +660,11 @@ impl_computed_payload_clone_and_eq!(InheritedTextValues {
tab_size_length,
tab_size_number,
text_indent,
- color,
- color_style_value,
+ color => paint_only,
+ color_style_value => paint_only,
font_smoothing,
- webkit_text_fill_color,
- webkit_text_fill_color_is_current_color,
+ webkit_text_fill_color => paint_only,
+ webkit_text_fill_color_is_current_color => paint_only,
text_shadow,
text_transform,
text_wrap_style,
@@ -708,14 +731,14 @@ impl_computed_payload_clone_and_eq!(BackgroundValues {
background_size,
});
impl_computed_payload_clone_and_eq!(BorderValues {
- border_left,
- border_top,
- border_right,
- border_bottom,
- border_left_color_style_value,
- border_top_color_style_value,
- border_right_color_style_value,
- border_bottom_color_style_value,
+ border_left => border_side_layout_inputs_equal,
+ border_top => border_side_layout_inputs_equal,
+ border_right => border_side_layout_inputs_equal,
+ border_bottom => border_side_layout_inputs_equal,
+ border_left_color_style_value => paint_only,
+ border_top_color_style_value => paint_only,
+ border_right_color_style_value => paint_only,
+ border_bottom_color_style_value => paint_only,
border_left_computed_width,
border_top_computed_width,
border_right_computed_width,
@@ -776,9 +799,9 @@ impl_computed_payload_clone_and_eq!(MiscResetValues {
outline_style,
object_fit,
column_height,
- outline_color,
+ outline_color => paint_only,
outline_width,
- column_rule_color,
+ column_rule_color => paint_only,
column_rule_width,
outline_offset,
user_select,
@@ -799,7 +822,7 @@ impl_computed_payload_clone_and_eq!(MiscResetValues {
shape_margin,
shape_outside,
will_change,
- implicit_will_change,
+ implicit_will_change => implicit_will_change_layout_inputs_equal,
});
impl_computed_payload_clone_and_eq!(FontValues {
font_size,
@@ -874,7 +897,7 @@ impl_computed_payload_clone_and_eq!(ComputedFilter {
operations
});
impl_computed_payload_clone_and_eq!(EffectsValues {
- opacity,
+ opacity => paint_only,
filter,
backdrop_filter,
mix_blend_mode,
@@ -882,7 +905,7 @@ impl_computed_payload_clone_and_eq!(EffectsValues {
box_shadows,
clip_is_rect,
clip_edges,
- opacity_style_value,
+ opacity_style_value => paint_only,
filter_style_value,
backdrop_filter_style_value,
mix_blend_mode_style_value,
@@ -906,13 +929,13 @@ impl_computed_payload_clone_and_eq!(AnchorValues {
position_visibility_no_overflow,
});
impl_computed_payload_clone_and_eq!(InheritedUIValues {
- caret_color,
- accent_color,
+ caret_color => paint_only,
+ accent_color => paint_only,
cursor,
pointer_events,
forced_color_adjust,
interpolate_size,
- scrollbar_color,
+ scrollbar_color => paint_only,
color_scheme,
color_schemes,
color_scheme_only,
@@ -1388,11 +1411,118 @@ unsafe fn payload_content_hash(group: StyleGroup, payload: *const c_void, hasher
with_style_group_type!(group, Payload => unsafe { payload_ref::<Payload>(payload) }.write_content_hash(hasher));
}
-pub(crate) fn style_group_affects_layout(group_index: usize) -> bool {
- !matches!(
- style_group(group_index),
- StyleGroup::Mask | StyleGroup::TextReset | StyleGroup::Background
- )
+/// The values of a style group's payload that layout reads. A box whose style changes only the other values keeps the
+/// layout it cached, as only painting reads them.
+trait StyleGroupLayoutInputs: PartialEq {
+ fn layout_inputs_equal(&self, other: &Self) -> bool {
+ self == other
+ }
+}
+
+fn border_side_layout_inputs_equal(
+ first: &crate::css::computed_value_types::ComputedBorderSide,
+ second: &crate::css::computed_value_types::ComputedBorderSide,
+) -> bool {
+ let crate::css::computed_value_types::ComputedBorderSide {
+ color: _,
+ line_style,
+ width,
+ } = first;
+ *line_style == second.line_style && *width == second.width
+}
+
+fn implicit_will_change_layout_inputs_equal(
+ first: &RetainedWillChangePropertyList,
+ second: &RetainedWillChangePropertyList,
+) -> bool {
+ use crate::css::property_metadata::property_id::{COLOR, OPACITY};
+ let names_only_paint_properties = |hints: &RetainedWillChangePropertyList| {
+ hints
+ .as_slice()
+ .iter()
+ .all(|&property| matches!(property, COLOR | OPACITY))
+ };
+ (names_only_paint_properties(first) && names_only_paint_properties(second)) || first == second
+}
+
+macro_rules! impl_style_group_read_whole_by_layout {
+ ($($type:ty),+ $(,)?) => {
+ $(impl StyleGroupLayoutInputs for $type {})+
+ };
+}
+
+impl_style_group_read_whole_by_layout!(
+ InheritedTableValues,
+ InheritedListValues,
+ InheritedSVGValues,
+ InheritedBoxValues,
+ FontValues,
+ AnimationValues,
+ SVGResetValues,
+ GridValues,
+ AnchorValues,
+ ContentValues,
+ TransformValues,
+ AlignmentValues,
+ SizingValues,
+ SurroundValues,
+ BoxValues,
+);
+
+macro_rules! impl_style_group_unread_by_layout {
+ ($($type:ty),+ $(,)?) => {
+ $(impl StyleGroupLayoutInputs for $type {
+ fn layout_inputs_equal(&self, _: &Self) -> bool {
+ true
+ }
+ })+
+ };
+}
+
+// Only painting reads these groups. Resource observers for background images register in apply_style, whatever layout
+// does. Hit testing also reads the mask. Text decorations resolve when the display list is built. Layout does not read
+// white-space-trim yet, so an implementation of it must remove TextResetValues from this list. EffectsValues is not
+// in this list, because filter and backdrop-filter make a containing block for fixed-position descendants.
+impl_style_group_unread_by_layout!(MaskValues, TextResetValues, BackgroundValues);
+
+/// Both payloads must be live payloads of the group's type.
+unsafe fn payload_layout_inputs_equal(group: StyleGroup, a: *const c_void, b: *const c_void) -> bool {
+ with_style_group_type!(group, Payload => {
+ // SAFETY: The caller guarantees live payloads of the group.
+ let (a, b) = unsafe { (payload_ref::<Payload>(a), payload_ref::<Payload>(b)) };
+ a.layout_inputs_equal(b)
+ })
+}
+
+/// Whether two records' group payloads hold the same values that layout reads (see [`StyleGroupLayoutInputs`]), so a
+/// box whose style moves from one record to the other keeps the layout it cached. The geometry of an SVG box can also
+/// depend on `color`, through `currentColor`.
+///
+/// # Safety
+/// Every payload must be a live payload of the group at its index.
+pub(crate) unsafe fn style_payloads_layout_inputs_equal(
+ a: &[*const c_void],
+ b: &[*const c_void],
+ is_svg_box: bool,
+) -> bool {
+ debug_assert_eq!(a.len(), group_index::COUNT);
+ debug_assert_eq!(b.len(), group_index::COUNT);
+ if is_svg_box {
+ // SAFETY: The caller guarantees live payloads of each group.
+ let (first, second) = unsafe {
+ (
+ payload_ref::<InheritedTextValues>(a[group_index::INHERITED_TEXT]),
+ payload_ref::<InheritedTextValues>(b[group_index::INHERITED_TEXT]),
+ )
+ };
+ if first.color != second.color {
+ return false;
+ }
+ }
+ (0..group_index::COUNT).all(|group| {
+ // SAFETY: The caller guarantees live payloads of each group.
+ a[group] == b[group] || unsafe { payload_layout_inputs_equal(style_group(group), a[group], b[group]) }
+ })
}
/// The style groups in `groups` whose payloads differ between two records, `[base, composed]` each: explicit
@@ -1942,20 +2072,24 @@ pub(crate) fn release_group_payload(group_index: usize, payload: *const c_void)
}
}
-/// Compares two payloads of the same style group for value equality, letting
-/// C++ group structs that inherit a Rust-native payload layout reuse the Rust
-/// field-wise equality instead of hand-writing a second one.
+/// See [`style_payloads_layout_inputs_equal`].
///
/// # Safety
-/// `a` and `b` must be valid payloads of the given group type.
+/// `a` and `b` must each point to a record's array of live group payloads.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn rust_style_group_payloads_equal(
- group_index: usize,
- a: *const c_void,
- b: *const c_void,
+pub unsafe extern "C" fn rust_style_payloads_layout_inputs_equal(
+ a: *const *const c_void,
+ b: *const *const c_void,
+ is_svg_box: bool,
) -> bool {
- // SAFETY: The caller guarantees live payloads of the group.
- unsafe { style_group_payloads_equal(group_index, a, b) }
+ // SAFETY: The caller guarantees two arrays of live group payloads.
+ unsafe {
+ style_payloads_layout_inputs_equal(
+ ak::slice_from_raw_parts(a, group_index::COUNT),
+ ak::slice_from_raw_parts(b, group_index::COUNT),
+ is_svg_box,
+ )
+ }
}
/// Clamps a border box size by the min and max sizes of a sizing group that resolve against the given basis, as
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
index a45525665d5..60905e14823 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
@@ -777,10 +777,6 @@ pub(crate) type ShellStyleChangedHost = (
unsafe extern "C" fn(*mut c_void, *mut c_void, u64, *const c_void, bool, bool),
);
-fn style_payloads_equal_in_layout_affecting_groups(a: StylePayloadsRef, b: StylePayloadsRef) -> bool {
- style_payloads_equal_in_groups(a, b, crate::css::computed_values::style_group_affects_layout)
-}
-
/// Whether `a` and `b` hold equal payloads in the inherited groups, which are all an anonymous box inherits.
fn style_payloads_equal_in_inherited_groups(a: StylePayloadsRef, b: StylePayloadsRef) -> bool {
style_payloads_equal_in_groups(a, b, |group_index| {
@@ -2378,6 +2374,29 @@ impl LayoutNodeArena {
previous != style_record
}
+ /// Whether the box `slot` keeps the layout it cached when it shows `new` in place of `previous` (see
+ /// [`crate::css::computed_values::style_payloads_layout_inputs_equal`]).
+ fn style_move_keeps_layout_inputs(
+ &self,
+ slot: NodeSlotId,
+ previous: StylePayloadsRef,
+ new: StylePayloadsRef,
+ ) -> bool {
+ if previous == new {
+ return true;
+ }
+ if previous.is_null() || new.is_null() {
+ return false;
+ }
+ // SAFETY: A row's non-null style addresses the payload array of the record pinned for it.
+ let (previous, new) = unsafe { (previous.deref(), new.deref()) };
+ let is_svg_box = super::node_facts::kind_is_svg_box(self.data(slot).kind.get());
+ // SAFETY: Published style-group payloads stay live while their records do.
+ unsafe {
+ crate::css::computed_values::style_payloads_layout_inputs_equal(&previous.groups, &new.groups, is_svg_box)
+ }
+ }
+
/// Applies `rows`, the rows of a style transaction a frame applies ahead of the host, to the boxes of their elements,
/// as the host's install of each row does: the row's record, the styles the box's anonymous descendants inherit, and
/// the relayout the move asks for. Answers the rows it applied, by element, or why it applied none: a row's box is
@@ -2426,7 +2445,7 @@ impl LayoutNodeArena {
self.publish_new_size_container_geometry(slot);
self.enroll_node_for_svg_paint_resources_sync(slot);
self.set_node_flag(slot, NodeFlag::HasAnimatedOpacityOrTransform, false);
- if !style_payloads_equal_in_layout_affecting_groups(previous_payloads, payloads) {
+ if !self.style_move_keeps_layout_inputs(slot, previous_payloads, payloads) {
self.bump_fragment_cache_epoch_of_self_and_ancestors(slot);
self.reset_cached_intrinsic_sizes_of_self_and_ancestors(slot);
}
@@ -2551,7 +2570,7 @@ impl LayoutNodeArena {
}
self.publish_new_size_container_geometry(slot);
self.enroll_node_for_svg_paint_resources_sync(slot);
- if !style_payloads_equal_in_layout_affecting_groups(previous_payloads, payloads) {
+ if !self.style_move_keeps_layout_inputs(slot, previous_payloads, payloads) {
self.bump_fragment_cache_epoch_of_self_and_ancestors(slot);
self.reset_cached_intrinsic_sizes_of_self_and_ancestors(slot);
}
@@ -2810,7 +2829,7 @@ impl LayoutNodeArena {
self.invalidate_overflow_after_style_change(row);
self.enroll_text_children_for_content_sync(row);
self.enroll_node_for_svg_paint_resources_sync(row);
- if style_payloads_equal_in_layout_affecting_groups(previous_payloads, style.payloads) {
+ if self.style_move_keeps_layout_inputs(row, previous_payloads, style.payloads) {
return;
}
self.bump_fragment_cache_epoch_of_self_and_ancestors(row);
@@ -3721,7 +3740,7 @@ impl LayoutNodeArena {
) {
let previous_payloads = self.data(slot).style.get();
let changes_layout_affecting_style =
- !style_payloads_equal_in_layout_affecting_groups(previous_payloads, derived.payloads);
+ !self.style_move_keeps_layout_inputs(slot, previous_payloads, derived.payloads);
self.replace_arena_pinned_style_record(slot, derived);
if changes_layout_affecting_style {
self.bump_fragment_cache_epoch_of_self_and_ancestors(slot);
diff --git a/Tests/LibWeb/Text/expected/css/style-engine/paint-changes-preserve-anonymous-box-intrinsic-sizes.txt b/Tests/LibWeb/Text/expected/css/style-engine/paint-changes-preserve-anonymous-box-intrinsic-sizes.txt
new file mode 100644
index 00000000000..a41e0cf2604
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/style-engine/paint-changes-preserve-anonymous-box-intrinsic-sizes.txt
@@ -0,0 +1,8 @@
+unchanged children reuse measurements: true
+unchanged children reuse measurements container width: 501
+inherited color preserves measurements: true
+inherited color preserves measurements container width: 500
+item color: rgb(255, 0, 0)
+inherited font size invalidates measurements: true
+inherited font size invalidates measurements container width: 501
+incremental layout stays consistent: true
diff --git a/Tests/LibWeb/Text/input/css/style-engine/paint-changes-preserve-anonymous-box-intrinsic-sizes.html b/Tests/LibWeb/Text/input/css/style-engine/paint-changes-preserve-anonymous-box-intrinsic-sizes.html
new file mode 100644
index 00000000000..29ca67cce8e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/style-engine/paint-changes-preserve-anonymous-box-intrinsic-sizes.html
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<script src="../../include.js"></script>
+<script src="../../layout-test.js"></script>
+<style>
+ #flex { display: flex; width: 500px; font-size: 16px; }
+ #flex > div { flex: 1 1 auto; }
+</style>
+<main id="flex"></main>
+<script>
+ test(() => {
+ const flex = document.getElementById("flex");
+ // Each item holds inline content beside a block, so the inline content sits in an anonymous block wrapper
+ // that inherits the item's style.
+ for (let index = 0; index < 20; ++index) {
+ const child = document.createElement("div");
+ child.innerHTML = `item ${index}<div>block ${index}</div>`;
+ flex.append(child);
+ }
+ flex.offsetWidth;
+ let width = 500;
+ function resizeAfter(label, change, shouldMeasure) {
+ const work = layoutWork(() => {
+ change();
+ internals.updateStyle();
+ width = width === 500 ? 501 : 500;
+ flex.style.width = `${width}px`;
+ });
+ const actualWidth = flex.offsetWidth;
+ const measurements = work.count({ event: "measure", cache: "miss" });
+ const reused = work.count({ event: "measure", cache: "hit" }) > 0;
+ println(`${label}: ${shouldMeasure ? measurements > 0 : measurements === 0 && reused}`);
+ println(`${label} container width: ${actualWidth}`);
+ }
+ resizeAfter("unchanged children reuse measurements", () => {}, false);
+ resizeAfter("inherited color preserves measurements", () => { flex.style.color = "red"; }, false);
+ println(`item color: ${getComputedStyle(flex.firstElementChild).color}`);
+ resizeAfter("inherited font size invalidates measurements", () => { flex.style.fontSize = "24px"; }, true);
+ println(`incremental layout stays consistent: ${internals.compareLayoutTreeWithFullRebuild().matches}`);
+ });
+</script>