Truly independent web browser
LibWeb: Narrow two style engine calls from the host
Andreas Kling02a0a54d72ab
diff --git a/Libraries/LibWeb/CSS/CSS.cpp b/Libraries/LibWeb/CSS/CSS.cppindex 2daa081d09c..9c5515cdfb5 100644--- a/Libraries/LibWeb/CSS/CSS.cpp+++ b/Libraries/LibWeb/CSS/CSS.cpp@@ -9,6 +9,8 @@#include <LibWeb/CSS/HighlightRegistry.h>#include <LibWeb/CSS/Parser/Parser.h>#include <LibWeb/CSS/Parser/SyntaxParsing.h>+#include <LibWeb/CSS/StyleComputer.h>+#include <LibWeb/CSS/StyleEngineBridge.h>#include <LibWeb/DOM/Document.h>// What the Rust implementation of the CSS namespace (Libraries/LibWeb/Bindings/Rust/src/css/css.rs) asks of the@@ -64,15 +66,18 @@ StyleValueData const* web_css_namespace_parse_with_a_syntax(void* document, Flatvoid web_css_namespace_register(void* raw_document, FlatPtr name, void const* syntax, bool inherits, StyleValueData const* initial_value){auto& document = document_from_abi(raw_document);- auto property_name = Utf16FlyString { Utf16String::adopt_raw(name) };CustomPropertyRegistration registered_property {- .property_name = property_name,+ .property_name = Utf16FlyString { Utf16String::adopt_raw(name) },.syntax = Parser::RustSyntaxHandle { syntax },.inherit = inherits,.initial_value = initial_value ? RefPtr<StyleValue const> { StyleValue::adopt_rust_style_value_data(initial_value) } : nullptr,};document.registered_property_set().set(registered_property.property_name, move(registered_property));- document.did_change_custom_property_registrations(property_name);+ document.did_change_custom_property_registrations();++ // NB: An @property rule reaches the style engine as a rule of its own. CSS.registerProperty() has no rule, so tell+ // the engine directly.+ StyleEngineFFI::style_engine_record_custom_property_registration_change(document.style_computer().style_engine().host());}}diff --git a/Libraries/LibWeb/CSS/StyleComputer.cpp b/Libraries/LibWeb/CSS/StyleComputer.cppindex ad29578d91a..90f658ecfe7 100644--- a/Libraries/LibWeb/CSS/StyleComputer.cpp+++ b/Libraries/LibWeb/CSS/StyleComputer.cpp@@ -708,14 +708,14 @@ NonnullOwnPtr<StyleComputer::AnimationSample> StyleComputer::begin_animation_sam.length_contexts = [](void* context, u8 container_relative_length_unit_mask, ComputedValuesFFI::FfiAnimationLengthContexts* contexts) {auto& sample = *static_cast<SampleContext*>(context);// Each context is the sampled element's own, built afresh: a batch builds several elements' contexts in one call.- auto length_context_for = [&](PropertyID property_id) {+ auto length_context_for = [&](LengthResolutionPhase phase) {return to_ffi_length_resolution_context_with_container_bases(- sample.style_computer->make_computation_context_for_property(property_id, sample.computed_properties, sample.abstract_element).length_resolution_context,+ sample.style_computer->make_length_resolution_context(phase, sample.computed_properties, sample.abstract_element),container_relative_length_unit_mask);};- contexts->font = length_context_for(PropertyID::FontFamily);- contexts->line_height = length_context_for(PropertyID::LineHeight);- contexts->remaining = length_context_for(PropertyID::Color); },+ contexts->font = length_context_for(LengthResolutionPhase::Font);+ contexts->line_height = length_context_for(LengthResolutionPhase::LineHeight);+ contexts->remaining = length_context_for(LengthResolutionPhase::Remaining); },};return sample;}@@ -1504,8 +1504,7 @@ CSSPixels StyleComputer::default_user_font_size()return 16;}-// https://w3c.github.io/csswg-drafts/css-fonts/#absolute-size-mapping-ComputationContext StyleComputer::make_computation_context_for_property(PropertyID property_id, ComputedStyleWorkingSet const& style, Optional<DOM::AbstractElement> abstract_element) const+Length::ResolutionContext StyleComputer::make_length_resolution_context(LengthResolutionPhase phase, ComputedStyleWorkingSet const& style, Optional<DOM::AbstractElement> abstract_element) const{auto subject_inline_axis_is_horizontal = [&]() {auto writing_mode = [&](DOM::AbstractElement const& candidate) -> Optional<WritingMode> {@@ -1526,95 +1525,45 @@ ComputationContext StyleComputer::make_computation_context_for_property(Propertybool const is_document_element = abstract_element.has_value()&& !abstract_element->pseudo_element().has_value()&& abstract_element->element().is_document_element();-- switch (property_id) {- // FIXME: While `color-scheme` doesn't actually require a computation context (since it only takes keyword values),- // callers request one uniformly. Since `color-scheme` must be computed before creating a generic computation- // context, use the font context instead.- case PropertyID::ColorScheme:- case PropertyID::FontFamily:- case PropertyID::FontFeatureSettings:- case PropertyID::FontKerning:- case PropertyID::FontOpticalSizing:- case PropertyID::FontSize:- case PropertyID::FontSizeAdjust:- case PropertyID::FontStyle:- case PropertyID::FontVariantAlternates:- case PropertyID::FontVariantCaps:- case PropertyID::FontVariantEastAsian:- case PropertyID::FontVariantEmoji:- case PropertyID::FontVariantLigatures:- case PropertyID::FontVariantNumeric:- case PropertyID::FontVariantPosition:- case PropertyID::FontVariationSettings:- case PropertyID::FontWeight:- case PropertyID::FontWidth:- case PropertyID::MathDepth:- case PropertyID::TextRendering: {- auto inheritance_parent = abstract_element.map([](auto& element) { return element.element_to_inherit_style_from(); }).value_or(OptionalNone {});+ bool const subject_is_pseudo_element = abstract_element.has_value() && abstract_element->pseudo_element().has_value();+ auto const* subject_element = abstract_element.has_value() ? &abstract_element->element() : nullptr;+ // NB: The remaining properties read nothing from the parent, so they skip looking for it.+ auto inheritance_parent = phase == LengthResolutionPhase::Remaining+ ? Optional<DOM::AbstractElement> {}+ : abstract_element.map([](auto& element) { return element.element_to_inherit_style_from(); }).value_or(OptionalNone {});++ if (phase == LengthResolutionPhase::Font) {auto length_resolution_context = inheritance_parent.has_value() && inheritance_parent->has_style() && inheritance_parent->element().navigable()? Length::ResolutionContext::for_element(inheritance_parent.value()): Length::ResolutionContext::for_document(m_document);length_resolution_context.subject_inline_axis_is_horizontal = subject_inline_axis_is_horizontal;- length_resolution_context.subject_element = abstract_element.has_value() ? &abstract_element->element() : nullptr;- length_resolution_context.subject_is_pseudo_element = abstract_element.has_value() && abstract_element->pseudo_element().has_value();-- return {- .length_resolution_context = length_resolution_context,- .abstract_element = abstract_element- };- }- case PropertyID::LineHeight: {- auto inheritance_parent = abstract_element.map([](auto& element) { return element.element_to_inherit_style_from(); }).value_or(OptionalNone {});-- auto line_height_font_metrics = Length::FontMetrics {- style.font_size(),- style.first_available_computed_font(document().font_computer())->pixel_metrics(),- inheritance_parent.has_value() && inheritance_parent->has_style() ? inheritance_parent->computed_style()->line_height() : normal_line_height(document().font_computer().initial_font().pixel_metrics())- };-- return {- .length_resolution_context = {- .viewport_rect = m_viewport_rect,- .font_metrics = line_height_font_metrics,- .root_font_metrics = is_document_element- ? line_height_font_metrics- : m_root_element_font_metrics,- .font_metrics_depend_on_viewport_metrics = style.font_metrics_depend_on_viewport_metrics(),- .root_font_metrics_depend_on_viewport_metrics = is_document_element- ? style.font_metrics_depend_on_viewport_metrics()- : m_root_element_font_metrics_depend_on_viewport_metrics,- .subject_inline_axis_is_horizontal = subject_inline_axis_is_horizontal,- .subject_is_pseudo_element = abstract_element.has_value() && abstract_element->pseudo_element().has_value(),- .subject_element = abstract_element.has_value() ? &abstract_element->element() : nullptr,- },- .abstract_element = abstract_element- };- }- default: {- auto font_metrics = Length::FontMetrics {- style.font_size(),- style.first_available_computed_font(document().font_computer())->pixel_metrics(),- style.line_height(document().font_computer())- };- return {- .length_resolution_context = {- .viewport_rect = m_viewport_rect,- .font_metrics = font_metrics,- .root_font_metrics = is_document_element ? font_metrics : m_root_element_font_metrics,- .font_metrics_depend_on_viewport_metrics = style.font_metrics_depend_on_viewport_metrics(),- .root_font_metrics_depend_on_viewport_metrics = is_document_element ? style.font_metrics_depend_on_viewport_metrics() : m_root_element_font_metrics_depend_on_viewport_metrics,- .subject_inline_axis_is_horizontal = subject_inline_axis_is_horizontal,- .subject_is_pseudo_element = abstract_element.has_value() && abstract_element->pseudo_element().has_value(),- .subject_element = abstract_element.has_value() ? &abstract_element->element() : nullptr,- },- .abstract_element = abstract_element,- .color_scheme = style.color_scheme(document().page().preferred_color_scheme(), document().supported_color_schemes())- };- }+ length_resolution_context.subject_element = subject_element;+ length_resolution_context.subject_is_pseudo_element = subject_is_pseudo_element;+ return length_resolution_context;}- VERIFY_NOT_REACHED();+ auto line_height = [&]() -> CSSPixels {+ if (phase == LengthResolutionPhase::Remaining)+ return style.line_height(document().font_computer());+ if (inheritance_parent.has_value() && inheritance_parent->has_style())+ return inheritance_parent->computed_style()->line_height();+ return normal_line_height(document().font_computer().initial_font().pixel_metrics());+ }();+ auto font_metrics = Length::FontMetrics {+ style.font_size(),+ style.first_available_computed_font(document().font_computer())->pixel_metrics(),+ line_height+ };+ return {+ .viewport_rect = m_viewport_rect,+ .font_metrics = font_metrics,+ .root_font_metrics = is_document_element ? font_metrics : m_root_element_font_metrics,+ .font_metrics_depend_on_viewport_metrics = style.font_metrics_depend_on_viewport_metrics(),+ .root_font_metrics_depend_on_viewport_metrics = is_document_element ? style.font_metrics_depend_on_viewport_metrics() : m_root_element_font_metrics_depend_on_viewport_metrics,+ .subject_inline_axis_is_horizontal = subject_inline_axis_is_horizontal,+ .subject_is_pseudo_element = subject_is_pseudo_element,+ .subject_element = subject_element,+ };}static ComputedValuesFFI::FfiInputLineHeightMetrics input_line_height_metrics(ComputedStyleWorkingSet const& style, DOM::AbstractElement abstract_element, bool should_measure)diff --git a/Libraries/LibWeb/CSS/StyleComputer.h b/Libraries/LibWeb/CSS/StyleComputer.hindex c171974914e..40e71e3df44 100644--- a/Libraries/LibWeb/CSS/StyleComputer.h+++ b/Libraries/LibWeb/CSS/StyleComputer.h@@ -307,7 +307,13 @@ private:// Whether the engine keeps a before-change style the current stabilization epoch recorded.mutable bool m_transition_baselines_recorded { false };- ComputationContext make_computation_context_for_property(PropertyID, ComputedStyleWorkingSet const&, Optional<DOM::AbstractElement>) const;+ // What a sampled element's lengths resolve against, by when in the cascade they compute.+ enum class LengthResolutionPhase : u8 {+ Font,+ LineHeight,+ Remaining,+ };+ Length::ResolutionContext make_length_resolution_context(LengthResolutionPhase, ComputedStyleWorkingSet const&, Optional<DOM::AbstractElement>) const;CSSPixelRect m_viewport_rect;mutable StyleEngine m_style_engine;diff --git a/Libraries/LibWeb/CSS/StyleEngineBridge.h b/Libraries/LibWeb/CSS/StyleEngineBridge.hindex 0476dea1b93..1e55fcf1223 100644--- a/Libraries/LibWeb/CSS/StyleEngineBridge.h+++ b/Libraries/LibWeb/CSS/StyleEngineBridge.h@@ -57,7 +57,7 @@ void style_engine_set_element_table_spans(DocumentHost const*, StyleNodeID node,void style_engine_set_element_id_name(DocumentHost const*, StyleNodeID node, StyleAtomID name);void style_engine_set_shadow_root(DocumentHost const*, StyleNodeID shadow_host, StyleNodeID shadow_root);void style_engine_record_environment_change(DocumentHost const*, u8 changes);-void style_engine_record_custom_property_registration_change(DocumentHost const*, StyleAtomID name);+void style_engine_record_custom_property_registration_change(DocumentHost const*);void style_engine_flush(DocumentHost const*);void style_engine_finish_sheet_rules_replacement(DocumentHost const*, SheetID sheet, u32 declaration_block);void style_engine_attach_sheet(DocumentHost const*, SheetID sheet, TreeScopeID tree_scope, SheetID before_sheet);diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cppindex 808d5279c00..9ecf5bfed1e 100644--- a/Libraries/LibWeb/DOM/Document.cpp+++ b/Libraries/LibWeb/DOM/Document.cpp@@ -2800,14 +2800,14 @@ void Document::record_style_environment_change(StyleEnvironmentChange changes)void Document::invalidate_style_for_viewport_change(CSSPixelSize previous_viewport_size){- Vector<Utf16FlyString> registrations_reading_viewport;+ bool any_registration_read_viewport = false;auto invalidate_registered_initial_values = [&](auto& registrations) {for (auto& [name, registration] : registrations) {if (!registration.computed_initial_value_depends_on_viewport_metrics)continue;registration.computed_initial_value = nullptr;registration.computed_initial_value_depends_on_viewport_metrics = false;- registrations_reading_viewport.append(name);+ any_registration_read_viewport = true;}};invalidate_registered_initial_values(m_registered_property_set);@@ -2816,7 +2816,7 @@ void Document::invalidate_style_for_viewport_change(CSSPixelSize previous_viewpoLayout::ForcedReadScope read { *this };auto& style_engine = style_computer().style_engine();- if (!registrations_reading_viewport.is_empty()) {+ if (any_registration_read_viewport) {// The custom-property registry holds the initial values just dropped, so the next style// update has to publish the recomputed ones before it reads them. A registered initial value// moving is a change to its registration, which restyles the elements declaring, holding or@@ -2824,8 +2824,7 @@ void Document::invalidate_style_for_viewport_change(CSSPixelSize previous_viewpom_rust_custom_property_registry_synced = false;++m_custom_property_registration_generation;m_custom_property_environments_need_republishing = true;- for (auto const& name : registrations_reading_viewport)- CSS::StyleEngineFFI::style_engine_record_custom_property_registration_change(style_engine.host(), style_engine.intern_atom(name));+ CSS::StyleEngineFFI::style_engine_record_custom_property_registration_change(style_engine.host());}// The viewport is one of the document's published inputs, and the style engine drives a record@@ -12886,7 +12885,7 @@ void Document::for_each_registered_custom_property(Function<void(CSS::CustomProp}}-void Document::did_change_custom_property_registrations(Optional<Utf16FlyString> registered_property_set_change)+void Document::did_change_custom_property_registrations(){++m_custom_property_registration_generation;sync_custom_property_registrations_to_rust();@@ -12895,13 +12894,6 @@ void Document::did_change_custom_property_registrations(Optional<Utf16FlyString>// That waits for the next style update, which reaches the engine under a read of its own. A change beside// a frame in flight leaves the flight alone.m_custom_property_environments_need_republishing = true;-- // An @property rule's program input already reaches the elements declaring or referencing its- // name. CSS.registerProperty() has no rule, so publish the equivalent named input explicitly.- if (registered_property_set_change.has_value()) {- auto& style_engine = style_computer().style_engine();- CSS::StyleEngineFFI::style_engine_record_custom_property_registration_change(style_engine.host(), style_engine.intern_atom(*registered_property_set_change));- }}void Document::republish_custom_property_environments_for_style_update(Layout::BegunRead const& read)diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.hindex ea0afee1711..7d666306f10 100644--- a/Libraries/LibWeb/DOM/Document.h+++ b/Libraries/LibWeb/DOM/Document.h@@ -1860,7 +1860,7 @@ public:void for_each_registered_custom_property(Function<void(CSS::CustomPropertyRegistration const&)> const&) const;size_t custom_property_registration_generation() const { return m_custom_property_registration_generation; }void const* rust_custom_property_registry() const { return m_rust_custom_property_registry; }- void did_change_custom_property_registrations(Optional<Utf16FlyString> registered_property_set_change = {});+ void did_change_custom_property_registrations();CSS::StyleScope const& style_scope() const { return m_style_scope; }CSS::StyleScope& style_scope() { return m_style_scope; }diff --git a/Libraries/LibWeb/Rust/src/css/style/boundary.rs b/Libraries/LibWeb/Rust/src/css/style/boundary.rsindex 2fccdc60b8b..4232b1151ac 100644--- a/Libraries/LibWeb/Rust/src/css/style/boundary.rs+++ b/Libraries/LibWeb/Rust/src/css/style/boundary.rs@@ -354,7 +354,7 @@ style_boundary! {});set_element_id_name => SetElementIdName { node: StyleNodeID, name: StyleAtomID };record_environment_change => RecordEnvironmentChange { changes: u8 };- record_custom_property_registration_change => RecordCustomPropertyRegistrationChange { name: StyleAtomID };+ record_custom_property_registration_change => RecordCustomPropertyRegistrationChange => engine.record_custom_property_registration_change();flush => Flush => engine.flush_without_document_root();finish_sheet_rules_replacement => FinishSheetRulesReplacement { sheet: SheetID, declaration_block: u32 };attach_sheet => AttachSheet { sheet: SheetID, tree_scope: TreeScopeID, before_sheet: Option<SheetID> } =>diff --git a/Libraries/LibWeb/Rust/src/css/style/engine_sample.rs b/Libraries/LibWeb/Rust/src/css/style/engine_sample.rsindex eb0e01bab9b..98add2323bb 100644--- a/Libraries/LibWeb/Rust/src/css/style/engine_sample.rs+++ b/Libraries/LibWeb/Rust/src/css/style/engine_sample.rs@@ -84,9 +84,8 @@ impl RetainedState {/// values in over `record`, the record the host holds for it: the font context, which/// reads the record its inheritance parent holds; the line-height context, the element's own/// font with the line height it inherits; and the one everything else resolves against, the- /// element's own font. A mirror of the host's `make_computation_context_for_property(FontFamily /- /// LineHeight / Color)` over the working set it reconstructs from that record, without the- /// container bases only the host resolves.+ /// element's own font. A mirror of the host's `make_length_resolution_context` over the working+ /// set it reconstructs from that record, without the container bases only the host resolves.pub(crate) fn animation_sample_length_contexts(&self,node: StyleNodeID,diff --git a/Libraries/LibWeb/Rust/src/css/style/inputs.rs b/Libraries/LibWeb/Rust/src/css/style/inputs.rsindex aefa7b2f35d..b1e35367461 100644--- a/Libraries/LibWeb/Rust/src/css/style/inputs.rs+++ b/Libraries/LibWeb/Rust/src/css/style/inputs.rs@@ -2403,7 +2403,7 @@ impl StyleEngine {/// Record a registration made through `CSS.registerProperty()`. Stylesheet registrations are/// already represented by their `@property` rule's program input.- pub fn record_custom_property_registration_change(&mut self, _name: StyleAtomID) {+ pub fn record_custom_property_registration_change(&mut self) {self.retained.counters.bump(Counter::RawMutationRecords);self.retained.counters.bump(Counter::ProgramDeltas);// A registration made through `CSS.registerProperty()` moves how the elements declaring