Skip to content

Truly independent web browser

LibWeb: Index ancestor transform animations by their layout nodes

sideshowbarker28ba84f07c0a
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index f6c478e82a4..0ec4182f4a2 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -9316,7 +9316,7 @@ void Document::update_compositor_animations(Layout::BegunRead const& read)
CompositorAnimationKeyframesByEffect keyframes_by_effect;
GC::RootHashMap<GC::Ptr<Animations::KeyframeEffect>, bool> only_translates_horizontally_cache;
GC::RootHashMap<GC::Ptr<Animations::KeyframeEffect>, bool> animated_transform_preserves_axes_cache;
- GC::RootHashMap<Element const*, GC::RootVector<GC::Ptr<Animations::KeyframeEffect>>> in_effect_transform_effects_by_target;
+ HashMap<Layout::Node const*, GC::RootVector<GC::Ptr<Animations::KeyframeEffect>>> in_effect_transform_effects_by_layout_node;
GC::RootHashMap<GC::Ptr<Element>, CSSPixelRect> observation_target_rect_cache;
GC::RootHashTable<GC::Ref<Element>> elements_with_intersection_observation_descendants;
GC::RootHashTable<GC::Ref<Element>> elements_with_visibility_observation_descendants;
@@ -9383,12 +9383,10 @@ void Document::update_compositor_animations(Layout::BegunRead const& read)
for (auto const* ancestor = layout_node->parent(); ancestor; ancestor = ancestor->parent()) {
if (ancestor->has_css_transform() && !transform_preserves_horizontal_axis(*ancestor))
return true;
- if (auto* ancestor_element = as_if<Element>(ancestor->dom_node())) {
- if (auto effects = in_effect_transform_effects_by_target.find(ancestor_element); effects != in_effect_transform_effects_by_target.end()) {
- for (auto effect : effects->value) {
- if (!animated_transform_preserves_axes(*effect, *ancestor))
- return true;
- }
+ if (auto effects = in_effect_transform_effects_by_layout_node.find(ancestor); effects != in_effect_transform_effects_by_layout_node.end()) {
+ for (auto effect : effects->value) {
+ if (!animated_transform_preserves_axes(*effect, *ancestor))
+ return true;
}
}
if (ancestor->dom_node() == &observation_root)
@@ -9518,9 +9516,9 @@ void Document::update_compositor_animations(Layout::BegunRead const& read)
auto visible_bounds = viewport_bounds;
for (auto const* ancestor = layout_node; ancestor; ancestor = ancestor->parent()) {
bool can_move_or_expand_paint = ancestor->has_css_transform() || ancestor->perspective().has_value()
- || ancestor->is_sticky_position() || ancestor->filter().has_filters();
+ || ancestor->is_sticky_position() || ancestor->filter().has_filters()
+ || in_effect_transform_effects_by_layout_node.contains(ancestor);
if (auto const* element = as_if<Element>(ancestor->dom_node())) {
- can_move_or_expand_paint |= in_effect_transform_effects_by_target.contains(element);
if (auto effects = competing_effects.get(*element); effects.has_value() && effects->filter.winner)
can_move_or_expand_paint = true;
}
@@ -9653,7 +9651,8 @@ void Document::update_compositor_animations(Layout::BegunRead const& read)
add_competing_effect(effects.filter);
if (any_of(effect.target_properties(), [](auto const& property) { return is_transform_family_property(property.id()); })) {
add_competing_effect(effects.transform);
- in_effect_transform_effects_by_target.ensure(&target->element()).append(effect);
+ if (auto const* layout_node = target->unsafe_layout_node(read))
+ in_effect_transform_effects_by_layout_node.ensure(layout_node).append(effect);
}
}
diff --git a/Tests/LibWeb/Text/expected/css/compositor-animation-animated-observer-ancestor.txt b/Tests/LibWeb/Text/expected/css/compositor-animation-animated-observer-ancestor.txt
new file mode 100644
index 00000000000..a7da208c4e2
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/compositor-animation-animated-observer-ancestor.txt
@@ -0,0 +1,3 @@
+target starts outside root: true
+only ancestor animation uses compositor: true
+target enters root: true
diff --git a/Tests/LibWeb/Text/expected/css/removed-pseudo-transform-animation-with-observer.txt b/Tests/LibWeb/Text/expected/css/removed-pseudo-transform-animation-with-observer.txt
new file mode 100644
index 00000000000..c8f3d7201ff
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/removed-pseudo-transform-animation-with-observer.txt
@@ -0,0 +1 @@
+Completed transition: matrix(1, 0, 0, 1, 0, 0)
diff --git a/Tests/LibWeb/Text/input/css/compositor-animation-animated-observer-ancestor.html b/Tests/LibWeb/Text/input/css/compositor-animation-animated-observer-ancestor.html
new file mode 100644
index 00000000000..3c0b5ba77e6
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/compositor-animation-animated-observer-ancestor.html
@@ -0,0 +1,59 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ @keyframes move-horizontally {
+ from, 25% { transform: translateX(0); }
+ 26%, to { transform: translateX(30px); }
+ }
+
+ @keyframes turn {
+ from, 90% { transform: rotate(0deg); }
+ to { transform: rotate(90deg); }
+ }
+
+ #root {
+ position: relative;
+ width: 20px;
+ height: 20px;
+ overflow: hidden;
+ }
+
+ #rotated-parent {
+ position: absolute;
+ top: -30px;
+ animation: turn 100s linear forwards;
+ transform-origin: 0 0;
+ }
+
+ #target {
+ width: 20px;
+ height: 20px;
+ animation: move-horizontally 400ms linear infinite;
+ }
+</style>
+<div id="root"><div id="rotated-parent"><div id="target"></div></div></div>
+<script>
+ asyncTest(async done => {
+ const ancestorAnimation = document.querySelector("#rotated-parent").getAnimations()[0];
+ ancestorAnimation.currentTime = 0;
+ const animation = target.getAnimations()[0];
+ animation.currentTime = 0;
+
+ let resolveObservation;
+ let nextObservation = new Promise(resolve => resolveObservation = resolve);
+ const observer = new IntersectionObserver(entries => resolveObservation(entries.at(-1)), { root });
+ observer.observe(target);
+
+ println(`target starts outside root: ${!(await nextObservation).isIntersecting}`);
+ println(`only ancestor animation uses compositor: ${internals.getStyleInvalidationCounters().compositorVisualAnimations === 1}`);
+
+ nextObservation = new Promise(resolve => resolveObservation = resolve);
+ ancestorAnimation.currentTime = 100000;
+ animation.currentTime = 200;
+ println(`target enters root: ${(await nextObservation).isIntersecting}`);
+ observer.disconnect();
+ ancestorAnimation.cancel();
+ animation.cancel();
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/css/removed-pseudo-transform-animation-with-observer.html b/Tests/LibWeb/Text/input/css/removed-pseudo-transform-animation-with-observer.html
new file mode 100644
index 00000000000..503c9d2e169
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/removed-pseudo-transform-animation-with-observer.html
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ #host { position: relative; height: 100px; }
+ #host.loading::after {
+ content: "";
+ position: absolute;
+ width: 100px;
+ height: 3px;
+ background: red;
+ animation: loading 0.5s infinite;
+ }
+ @keyframes loading {
+ from { transform: scaleX(0); }
+ to { transform: scaleX(1); }
+ }
+ #child { height: 50px; background: lightblue; transform: translateX(200px); }
+ #host.loaded #child { transform: translateX(0); transition: transform 0.1s; }
+</style>
+<div id="host" class="loading"><div id="child"></div></div>
+<script>
+ asyncTest(async done => {
+ const observer = new IntersectionObserver(() => {});
+ observer.observe(child);
+ for (let i = 0; i < 4; ++i)
+ await new Promise(requestAnimationFrame);
+ const finished = new Promise(resolve => child.addEventListener("transitionend", resolve, { once: true }));
+ host.classList.replace("loading", "loaded");
+ await finished;
+ println(`Completed transition: ${getComputedStyle(child).transform}`);
+ observer.disconnect();
+ done();
+ });
+</script>