Skip to content

Truly independent web browser

LibWeb: Self-check a style update that flew once nothing is left to do

Andreas Klinga32002f90dc5
diff --git a/Libraries/LibWeb/CSS/StyleEngineBridge.h b/Libraries/LibWeb/CSS/StyleEngineBridge.h
index e5ea4ae4b75..d220ba491e1 100644
--- a/Libraries/LibWeb/CSS/StyleEngineBridge.h
+++ b/Libraries/LibWeb/CSS/StyleEngineBridge.h
@@ -96,6 +96,7 @@ void style_engine_set_tree_scope_root(DocumentHost const*, TreeScopeID tree_scop
void style_engine_set_sheet_conditions_hold(DocumentHost const*, SheetID sheet, bool conditions_hold);
u32 style_engine_connected_element_count(DocumentHost const*, BegunRead const*);
bool style_engine_self_check_is_enabled(DocumentHost const*);
+bool style_engine_flown_update_awaits_self_check(DocumentHost const*);
bool style_engine_custom_property_environment_is_engine_made(DocumentHost const*, BegunRead const*, u64 identity);
bool style_engine_pending_transaction_may_affect_layout_geometry(DocumentHost const*, BegunRead const*);
void style_engine_defer_pending_transaction_for_geometry_read(DocumentHost const*, BegunRead const*);
diff --git a/Libraries/LibWeb/CSS/UpdateStyle.cpp b/Libraries/LibWeb/CSS/UpdateStyle.cpp
index e9744775a81..7f6092d6580 100644
--- a/Libraries/LibWeb/CSS/UpdateStyle.cpp
+++ b/Libraries/LibWeb/CSS/UpdateStyle.cpp
@@ -567,8 +567,21 @@ static void update_style(Layout::BegunRead const& read, DOM::Document& document,
return begin_style_update_inputs(read, document, container_layout);
}
}();
- if (!inputs.has_value())
+ auto const self_check = [&] {
+ auto* root = document.document_element();
+ StyleEngineFFI::style_engine_self_check(document.style_computer().style_engine().host(), &read, root ? root->style_node_id().value() : 0);
+ };
+ // NB: An update behind one that flew and left nothing to do is the one that checks it.
+ auto const self_check_a_flown_update = [&] {
+ if constexpr (!drains_flown_transaction) {
+ if (StyleEngineFFI::style_engine_flown_update_awaits_self_check(document.style_computer().style_engine().host()))
+ self_check();
+ }
+ };
+ if (!inputs.has_value()) {
+ self_check_a_flown_update();
return;
+ }
if (has_no_style_work) {
dbgln("Style update found work its document said it had none of");
VERIFY_NOT_REACHED();
@@ -603,8 +616,10 @@ static void update_style(Layout::BegunRead const& read, DOM::Document& document,
if (element.installed_style().view().animated_overlay || element.custom_property_data_is_animation_overlay({}))
sample_animations_for_installed_record(DOM::AbstractElement { element });
});
- if (!applied.had_reactions)
+ if (!applied.had_reactions) {
+ self_check_a_flown_update();
return;
+ }
document.set_has_completed_style_update();
apply_document_style_invalidation_after_style_change(document, application.invalidation);
@@ -618,10 +633,8 @@ static void update_style(Layout::BegunRead const& read, DOM::Document& document,
// update checks. So does the one after an update that stopped at its limit of passes with what they fed back
// still pending. An update that stopped with nothing left has settled.
auto const stopped_with_work_left = applied.pass_guard_hits != 0 && document.style_computer().style_engine().has_pending_transaction(read);
- if (!drains_flown_transaction && !stopped_with_work_left && StyleEngineFFI::style_engine_self_check_is_enabled(document.style_computer().style_engine().host())) {
- auto* root = document.document_element();
- StyleEngineFFI::style_engine_self_check(document.style_computer().style_engine().host(), &read, root ? root->style_node_id().value() : 0);
- }
+ if (!drains_flown_transaction && !stopped_with_work_left && StyleEngineFFI::style_engine_self_check_is_enabled(document.style_computer().style_engine().host()))
+ self_check();
}
// Records what update_style() records before it runs the document's style update, and lets the update fly
diff --git a/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs b/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
index a16f0f3fc80..f325e1634eb 100644
--- a/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
@@ -880,6 +880,8 @@ pub(crate) struct EngineMemo {
takes_transaction_to_apply: std::cell::Cell<bool>,
/// What `LIBWEB_STYLE_SELFCHECK` asked for as the host started.
pub(super) style_self_check: super::pipeline::SelfCheckSettings,
+ /// Whether the self-check of a style update that flew is yet to run: it is the next update's to run.
+ pub(super) flown_update_awaits_self_check: std::cell::Cell<bool>,
}
impl EngineMemo {
@@ -1009,6 +1011,7 @@ impl Default for EngineMemo {
applying_reactions: Default::default(),
takes_transaction_to_apply: Default::default(),
style_self_check: super::pipeline::SelfCheckSettings::from_environment(),
+ flown_update_awaits_self_check: Default::default(),
}
}
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/self_check.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/self_check.rs
index da9a49157e7..dc1d80782dd 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/self_check.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/self_check.rs
@@ -385,6 +385,9 @@ pub(super) fn differences_by_element(
(Some(_), None) if hidden_in_cold(node) => continue,
(Some(_), None) => failures.push((node, "live holds a record, cold computed none".to_owned())),
(None, Some(_)) => failures.push((node, "live holds no record, cold computed one".to_owned())),
+ // NB: No pass samples the animations of an element that is not displayed. A cold pass reads what a read
+ // below it last sampled, which the record the passes keep does not, and a read samples them again.
+ (Some(live), Some(_)) if !live.is_displayed() && state.element_samples_custom_properties(node) => continue,
(Some(live), Some(cold)) => {
let differences = record_differences(live, cold);
if !differences.is_empty() {
@@ -437,11 +440,11 @@ pub(super) fn installed_differences(
let mut failures = Vec::new();
for node in state.projection.style_preorder(root) {
// NB: The engine keeps the records of the elements below one that stopped being displayed,
- // which no pass keeps current and the host installs none of.
- let hidden = std::iter::successors(state.projection.inheritance_parent(node), |&ancestor| {
- state.projection.inheritance_parent(ancestor)
- })
- .any(|ancestor| live.get(ancestor).is_none_or(|record| !record.is_displayed()));
+ // which no pass keeps current and the host installs none of. What the host holds for an
+ // element that is itself not displayed is what a read below it last sampled, and a read
+ // of it demands its style again.
+ let hidden = std::iter::successors(Some(node), |&ancestor| state.projection.inheritance_parent(ancestor))
+ .any(|ancestor| live.get(ancestor).is_none_or(|record| !record.is_displayed()));
if hidden {
continue;
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/reaction_application.rs b/Libraries/LibWeb/Rust/src/css/style/reaction_application.rs
index bd9f6f4a4a1..f38fe6bbb7b 100644
--- a/Libraries/LibWeb/Rust/src/css/style/reaction_application.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/reaction_application.rs
@@ -258,11 +258,18 @@ pub extern "C" fn style_engine_self_check_is_enabled(host: &DocumentHost) -> boo
host.engine_memo().style_self_check.is_enabled()
}
+/// Whether a style update that flew is yet to be checked, which an update finding nothing to do has to do.
+#[unsafe(no_mangle)]
+pub extern "C" fn style_engine_flown_update_awaits_self_check(host: &DocumentHost) -> bool {
+ host.engine_memo().flown_update_awaits_self_check.get()
+}
+
/// Checks what the engine holds for the host's document under `root`, its document element's style node or zero,
/// against a cold pass, as `LIBWEB_STYLE_SELFCHECK` asks, once a style update has settled.
#[unsafe(no_mangle)]
pub extern "C" fn style_engine_self_check(host: &DocumentHost, read: &BegunRead, root: u32) {
let settings = host.engine_memo().style_self_check;
+ host.engine_memo().flown_update_awaits_self_check.set(false);
if let Some(root) = StyleNodeID::from_raw(root)
&& settings.is_enabled()
{
diff --git a/Libraries/LibWeb/Rust/src/css/style/style_job.rs b/Libraries/LibWeb/Rust/src/css/style/style_job.rs
index d9a23f02fb1..9d5a15ca132 100644
--- a/Libraries/LibWeb/Rust/src/css/style/style_job.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/style_job.rs
@@ -526,6 +526,9 @@ pub extern "C" fn style_engine_drain_flown_style_update(host: &DocumentHost, rea
#[unsafe(no_mangle)]
pub extern "C" fn style_engine_end_flown_style_drain(host: &DocumentHost) {
host.end_style_drain();
+ let memo = host.engine_memo();
+ memo.flown_update_awaits_self_check
+ .set(memo.style_self_check.is_enabled());
}
/// Whether the render state installed the record `style_record` of the element `style_node` names in its box ahead of
diff --git a/Tests/LibWeb/Text/expected/css/selector-match-below-shown-root-past-font-waits.txt b/Tests/LibWeb/Text/expected/css/style-engine/selector-match-below-shown-root-past-font-waits.txt
similarity index 100%
rename from Tests/LibWeb/Text/expected/css/selector-match-below-shown-root-past-font-waits.txt
rename to Tests/LibWeb/Text/expected/css/style-engine/selector-match-below-shown-root-past-font-waits.txt
diff --git a/Tests/LibWeb/Text/expected/css/style-engine/undisplayed-element-read-after-its-animation-is-cancelled.txt b/Tests/LibWeb/Text/expected/css/style-engine/undisplayed-element-read-after-its-animation-is-cancelled.txt
new file mode 100644
index 00000000000..548e957850a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/style-engine/undisplayed-element-read-after-its-animation-is-cancelled.txt
@@ -0,0 +1,4 @@
+at 30%, below: rgb(30, 30, 30)
+cancelled, itself: rgb(0, 0, 0)
+at 30%, below: rgb(30, 30, 30)
+cancelled and shown, itself: rgb(0, 0, 0)
diff --git a/Tests/LibWeb/Text/input/css/selector-match-below-shown-root-past-font-waits.html b/Tests/LibWeb/Text/input/css/style-engine/selector-match-below-shown-root-past-font-waits.html
similarity index 96%
rename from Tests/LibWeb/Text/input/css/selector-match-below-shown-root-past-font-waits.html
rename to Tests/LibWeb/Text/input/css/style-engine/selector-match-below-shown-root-past-font-waits.html
index 449d760e7fc..28bc5dbd620 100644
--- a/Tests/LibWeb/Text/input/css/selector-match-below-shown-root-past-font-waits.html
+++ b/Tests/LibWeb/Text/input/css/style-engine/selector-match-below-shown-root-past-font-waits.html
@@ -7,7 +7,7 @@
color: rgb(255, 0, 0);
}
</style>
-<script src="../include.js"></script>
+<script src="../../include.js"></script>
<div><div id="sibling"></div><div id="ancestor" class="ancestor"></div></div>
<script>
// An element below one shown again is matched anew, however many rounds the styles above it wait on fonts.
diff --git a/Tests/LibWeb/Text/input/css/style-engine/undisplayed-element-read-after-its-animation-is-cancelled.html b/Tests/LibWeb/Text/input/css/style-engine/undisplayed-element-read-after-its-animation-is-cancelled.html
new file mode 100644
index 00000000000..ad922fabd81
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/style-engine/undisplayed-element-read-after-its-animation-is-cancelled.html
@@ -0,0 +1,50 @@
+<!DOCTYPE html>
+<style>
+ @property --tone {
+ syntax: "<color>";
+ inherits: true;
+ initial-value: rgb(0, 0, 0);
+ }
+ .toned {
+ color: var(--tone);
+ }
+ .none {
+ display: none;
+ }
+</style>
+<div id="read-itself" class="none toned"><div class="none"><div id="reader-1"></div></div></div>
+<div id="shown" class="none toned"><div class="none"><div id="reader-2"></div></div></div>
+<script src="../../include.js"></script>
+<script>
+ function colorOf(element) {
+ return getComputedStyle(element).color;
+ }
+
+ // A read below an element that is not displayed gives the element the color its animation samples.
+ function cancelAfterReadingBelow(animated, reader) {
+ const animation = animated.animate([{ "--tone": "rgb(0, 0, 0)" }, { "--tone": "rgb(100, 100, 100)" }], {
+ duration: 100000,
+ fill: "both",
+ });
+ animation.pause();
+ animation.currentTime = 30000;
+ println(`at 30%, below: ${colorOf(reader)}`);
+ animation.cancel();
+ }
+
+ asyncTest(async done => {
+ const nextFrame = () => new Promise(resolve => requestAnimationFrame(resolve));
+
+ cancelAfterReadingBelow(document.getElementById("read-itself"), document.getElementById("reader-1"));
+ await nextFrame();
+ await nextFrame();
+ println(`cancelled, itself: ${colorOf(document.getElementById("read-itself"))}`);
+
+ cancelAfterReadingBelow(document.getElementById("shown"), document.getElementById("reader-2"));
+ await nextFrame();
+ await nextFrame();
+ document.getElementById("shown").classList.remove("none");
+ println(`cancelled and shown, itself: ${colorOf(document.getElementById("shown"))}`);
+ done();
+ });
+</script>