Skip to content

Truly independent web browser

LibWeb: Count animated custom property moves in the stabilization bound

Andreas Kling8856c840fc37
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index ee979cf53d0..4f8997e7ac9 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -2263,6 +2263,7 @@ void Document::begin_style_stabilization_epoch()
m_style_stabilization_pass_count = 0;
m_style_stabilization_has_style_reactions = false;
m_animated_custom_property_changes_in_stabilization_epoch.clear();
+ m_animated_custom_property_moves_in_stabilization_epoch = 0;
++m_style_invalidation_counters.style_stabilization_epochs;
++m_transition_generation;
}
@@ -2284,9 +2285,11 @@ void Document::record_style_stabilization_pass(Layout::BegunRead const& read)
++m_style_invalidation_counters.exact_stabilization_passes;
// Size-query and style-reaction dependencies are acyclic, so a coherent pass settles at
- // least one more connected element. Include inner StyleEngine transactions in the same exact
- // bound as layout feedback so neither feedback path can spin independently of the epoch.
- auto const exact_stabilization_round_limit = static_cast<u64>(CSS::StyleEngineFFI::style_engine_connected_element_count(style_computer().style_engine().host(), &read)) + 1;
+ // least one more connected element, or moves the animated custom properties of one that reads
+ // another of them, which an element does a limited number of times in an epoch. Include inner
+ // StyleEngine transactions in the same exact bound as layout feedback so neither feedback path
+ // can spin independently of the epoch.
+ auto const exact_stabilization_round_limit = static_cast<u64>(CSS::StyleEngineFFI::style_engine_connected_element_count(style_computer().style_engine().host(), &read)) + m_animated_custom_property_moves_in_stabilization_epoch + 1;
if (m_style_stabilization_pass_count > ordinary_stabilization_round_limit + exact_stabilization_round_limit) {
++m_style_invalidation_counters.style_stabilization_bound_failures;
VERIFY_NOT_REACHED();
@@ -2295,6 +2298,7 @@ void Document::record_style_stabilization_pass(Layout::BegunRead const& read)
u64 Document::note_animated_custom_properties_moved_in_stabilization_epoch(CSS::StyleNodeID style_node)
{
+ ++m_animated_custom_property_moves_in_stabilization_epoch;
return ++m_animated_custom_property_changes_in_stabilization_epoch.ensure(style_node);
}
@@ -2303,7 +2307,7 @@ bool Document::is_at_style_stabilization_bound(Layout::BegunRead const& read) co
// NB: A style update runs at most this many passes.
constexpr u64 passes_of_a_style_update = 9;
constexpr u64 ordinary_stabilization_round_limit = 8;
- return m_style_stabilization_pass_count + passes_of_a_style_update > ordinary_stabilization_round_limit + static_cast<u64>(CSS::StyleEngineFFI::style_engine_connected_element_count(style_computer().style_engine().host(), &read));
+ return m_style_stabilization_pass_count + passes_of_a_style_update > ordinary_stabilization_round_limit + static_cast<u64>(CSS::StyleEngineFFI::style_engine_connected_element_count(style_computer().style_engine().host(), &read)) + m_animated_custom_property_moves_in_stabilization_epoch;
}
void Document::end_style_stabilization_epoch()
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index 5cd440ca36c..f24718fe7fc 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -1536,6 +1536,7 @@ public:
[[nodiscard]] bool is_at_style_stabilization_bound(Layout::BegunRead const& read) const;
// How many times an element's animated custom properties moved in this style stabilization epoch, counting this one.
[[nodiscard]] u64 note_animated_custom_properties_moved_in_stabilization_epoch(CSS::StyleNodeID);
+ [[nodiscard]] u64 animated_custom_property_moves_in_stabilization_epoch() const { return m_animated_custom_property_moves_in_stabilization_epoch; }
void end_style_stabilization_epoch();
void note_style_stabilization_has_style_reactions()
{
@@ -2013,6 +2014,7 @@ private:
bool m_autofocus_processed_flag { false };
HashMap<CSS::StyleNodeID, u64> m_animated_custom_property_changes_in_stabilization_epoch;
+ u64 m_animated_custom_property_moves_in_stabilization_epoch { 0 };
bool m_created_for_appropriate_template_contents { false };
GC::Ptr<Document> m_associated_inert_template_document;
GC::Ptr<Document> m_appropriate_template_contents_owner_document;
diff --git a/Libraries/LibWeb/Layout/UpdateLayout.cpp b/Libraries/LibWeb/Layout/UpdateLayout.cpp
index e2bfb3a62c8..5ac76d7a5c5 100644
--- a/Libraries/LibWeb/Layout/UpdateLayout.cpp
+++ b/Libraries/LibWeb/Layout/UpdateLayout.cpp
@@ -35,6 +35,7 @@ Layout::RustFFI::FfiLayoutUpdateHostCallbacks Document::layout_update_host_callb
return {
.context = this,
.connected_element_count = [](void* context, Layout::BegunRead const* read) -> u32 { return CSS::StyleEngineFFI::style_engine_connected_element_count(static_cast<Document*>(context)->style_computer().style_engine().host(), read); },
+ .animated_custom_property_moves = [](void* context, Layout::BegunRead const*) -> u64 { return static_cast<Document*>(context)->animated_custom_property_moves_in_stabilization_epoch(); },
.update_style = [](void* context, Layout::BegunRead const*) { static_cast<Document*>(context)->update_style(); },
.process_pending_list_item_renumbers = [](void* context, Layout::BegunRead const*) { static_cast<Document*>(context)->process_pending_list_item_renumbers(); },
.process_pending_top_layer_layout_changes = [](void* context, Layout::BegunRead const* read) { static_cast<Document*>(context)->process_pending_top_layer_layout_changes(*read); },
diff --git a/Libraries/LibWeb/Rust/src/layout/update_layout.rs b/Libraries/LibWeb/Rust/src/layout/update_layout.rs
index db62dda35fb..2d2382e85ab 100644
--- a/Libraries/LibWeb/Rust/src/layout/update_layout.rs
+++ b/Libraries/LibWeb/Rust/src/layout/update_layout.rs
@@ -68,6 +68,7 @@ fn read_arena<A, R>(host: &DocumentHost, read: &BegunRead, args: A, answer: fn(&
pub struct FfiLayoutUpdateHostCallbacks {
context: *mut c_void,
connected_element_count: extern "C" fn(*mut c_void, &BegunRead) -> u32,
+ animated_custom_property_moves: extern "C" fn(*mut c_void, &BegunRead) -> u64,
update_style: extern "C" fn(*mut c_void, &BegunRead),
process_pending_list_item_renumbers: extern "C" fn(*mut c_void, &BegunRead),
process_pending_top_layer_layout_changes: extern "C" fn(*mut c_void, &BegunRead),
@@ -176,6 +177,10 @@ impl FfiLayoutUpdateHostCallbacks {
(self.connected_element_count)(self.context, read)
}
+ fn animated_custom_property_moves(&self, _: &MainThread, read: &BegunRead) -> u64 {
+ (self.animated_custom_property_moves)(self.context, read)
+ }
+
fn update_style(&self, _: &MainThread, read: &BegunRead) {
(self.update_style)(self.context, read);
}
@@ -1092,7 +1097,8 @@ fn update_layout(
// Size-query dependencies point from a descendant to an ancestor query container. They are
// therefore acyclic, and a coherent style/layout pass can settle at least one more level of
- // a nested dependency chain. One pass per connected element is a conservative exact bound.
+ // a nested dependency chain. One pass per connected element is a conservative exact bound, with
+ // one more per move of an animated custom property, as a chain of them moves one value per style pass.
// Recompute it after each pass because an initial style update can enroll the elements of a
// freshly parsed document after the layout update has already started. The bound is at least
// the ordinary limit, so the passes within it do not ask for the count.
@@ -1106,7 +1112,10 @@ fn update_layout(
let mut trace = StabilizationTrace::default();
while layout_pass <= ORDINARY_STABILIZATION_ROUND_LIMIT
|| layout_pass
- < ORDINARY_STABILIZATION_ROUND_LIMIT + u64::from(host.connected_element_count(main_thread, read)) + 1
+ < ORDINARY_STABILIZATION_ROUND_LIMIT
+ + u64::from(host.connected_element_count(main_thread, read))
+ + host.animated_custom_property_moves(main_thread, read)
+ + 1
{
layout_pass += 1;
diff --git a/Tests/LibWeb/Text/expected/css/custom-property-animations-chain-deeper-than-the-document.txt b/Tests/LibWeb/Text/expected/css/custom-property-animations-chain-deeper-than-the-document.txt
new file mode 100644
index 00000000000..e1ababd4a1e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/custom-property-animations-chain-deeper-than-the-document.txt
@@ -0,0 +1,2 @@
+first read of a pseudo-element: 80px
+the element: 80px
diff --git a/Tests/LibWeb/Text/expected/css/custom-property-animations-chain-on-one-element.txt b/Tests/LibWeb/Text/expected/css/custom-property-animations-chain-on-one-element.txt
new file mode 100644
index 00000000000..dca86fa39f7
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/custom-property-animations-chain-on-one-element.txt
@@ -0,0 +1,2 @@
+first read of a pseudo-element: 40px
+the element: 40px
diff --git a/Tests/LibWeb/Text/input/css/custom-property-animations-chain-deeper-than-the-document.html b/Tests/LibWeb/Text/input/css/custom-property-animations-chain-deeper-than-the-document.html
new file mode 100644
index 00000000000..9667b24327c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/custom-property-animations-chain-deeper-than-the-document.html
@@ -0,0 +1,32 @@
+<!DOCTYPE html>
+<body>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ // Two values of the chain on each element keep the style passes going for twice as long as the document has
+ // elements, so a read of the last element below it finds its input pending if it stops at the number of elements.
+ const elements = 40;
+ const style = document.createElement("style");
+ let parent = document.body;
+ let level = 0;
+ for (let index = 0; index < elements; ++index) {
+ const animations = [];
+ for (let value = 0; value < 2; ++value) {
+ ++level;
+ CSS.registerProperty({ name: `--level${level}`, syntax: "<length>", inherits: true, initialValue: "0px" });
+ const previous = level === 1 ? "0px" : `var(--level${level - 1})`;
+ const declaration = `--level${level}: calc(${previous} + 1px)`;
+ style.textContent += `@keyframes level${level} { from { ${declaration} } to { ${declaration} } }\n`;
+ animations.push(`level${level} 1000s linear both`);
+ }
+ const element = document.createElement("div");
+ element.style.animation = animations.join(", ");
+ parent.append(element);
+ parent = element;
+ }
+ document.head.append(style);
+ const name = `--level${level}`;
+ println(`first read of a pseudo-element: ${getComputedStyle(parent, "::first-line").getPropertyValue(name)}`);
+ println(`the element: ${getComputedStyle(parent).getPropertyValue(name)}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/css/custom-property-animations-chain-on-one-element.html b/Tests/LibWeb/Text/input/css/custom-property-animations-chain-on-one-element.html
new file mode 100644
index 00000000000..d3573b1cc04
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/custom-property-animations-chain-on-one-element.html
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<body>
+<script src="../include.js"></script>
+<div id="target"></div>
+<script>
+ test(() => {
+ // Each value of the chain moves in the style pass after the one it reads, however few elements the document has.
+ const depth = 40;
+ const style = document.createElement("style");
+ const animations = [];
+ for (let level = 1; level <= depth; ++level) {
+ CSS.registerProperty({ name: `--level${level}`, syntax: "<length>", inherits: true, initialValue: "0px" });
+ const previous = level === 1 ? "0px" : `var(--level${level - 1})`;
+ const value = `--level${level}: calc(${previous} + 1px)`;
+ style.textContent += `@keyframes level${level} { from { ${value} } to { ${value} } }\n`;
+ animations.push(`level${level} 1000s linear both`);
+ }
+ document.head.append(style);
+ target.style.animation = animations.join(", ");
+ const name = `--level${depth}`;
+ println(`first read of a pseudo-element: ${getComputedStyle(target, "::first-line").getPropertyValue(name)}`);
+ println(`the element: ${getComputedStyle(target).getPropertyValue(name)}`);
+ });
+</script>