Skip to content

Truly independent web browser

LibWeb: Skip per-frame sampling of animations nothing can show

Andreas Kling8a1eda843c44
diff --git a/Libraries/LibWeb/Animations/KeyframeEffect.cpp b/Libraries/LibWeb/Animations/KeyframeEffect.cpp
index 9736b16177d..c570a537830 100644
--- a/Libraries/LibWeb/Animations/KeyframeEffect.cpp
+++ b/Libraries/LibWeb/Animations/KeyframeEffect.cpp
@@ -33,6 +33,7 @@
#include <LibWeb/Layout/Node.h>
#include <LibWeb/SVG/SMILAnimation.h>
#include <LibWeb/SVG/SVGAnimationElement.h>
+#include <LibWeb/SVG/SVGElement.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
namespace Web::Animations {
@@ -1094,6 +1095,30 @@ void KeyframeEffect::visit_edges(GC::Cell::Visitor& visitor)
visitor.visit(m_keyframe_objects_cache);
}
+// Whether nothing shows the target's style, as a display: none ancestor leaves the target without a box.
+// NB: SVG resources keep their style under display: none, where boxes elsewhere in the document can reference them.
+static bool target_is_unseen_in_display_none_subtree(DOM::Element const& target)
+{
+ if (target.has_layout_box() || !target.has_inclusive_ancestor_with_display_none_ignoring_animations())
+ return false;
+ bool has_referenceable_svg_element = false;
+ target.for_each_in_inclusive_subtree_of_type<SVG::SVGElement>([&](SVG::SVGElement const& element) {
+ if (!element.is_referenceable_in_display_none_subtree())
+ return TraversalDecision::Continue;
+ has_referenceable_svg_element = true;
+ return TraversalDecision::Break;
+ });
+ return !has_referenceable_svg_element;
+}
+
+// NB: A custom property can feed the display of the target through var().
+bool KeyframeEffect::may_animate_display() const
+{
+ return any_of(target_properties(), [](auto const& property) {
+ return property.id() == CSS::PropertyID::Display || property.is_custom_property();
+ });
+}
+
bool KeyframeEffect::can_skip_per_frame_style_update() const
{
if (m_is_compositor_driven || m_is_compositor_replaced)
@@ -1155,6 +1180,8 @@ bool KeyframeEffect::can_skip_per_frame_style_update() const
if (!target->is_connected())
return cache_result(true);
+ if (target->document().layout_is_up_to_date() && !may_animate_display() && target_is_unseen_in_display_none_subtree(*target))
+ return cache_result(true);
bool has_animated_property = false;
auto const* key_frame_set = m_key_frame_set.ptr();
if (!key_frame_set)
@@ -1225,6 +1252,11 @@ bool KeyframeEffect::can_skip_per_frame_animation_tick() const
if (!m_is_compositor_driven && !m_is_compositor_replaced && !can_skip_per_frame_style_update())
return false;
+ // NB: Playing backward, an effect with an infinite iteration count finishes at time zero, and with a zero iteration
+ // duration it finishes at its end time. No timer wakes the main thread for either.
+ if (auto animation = associated_animation(); animation && isinf(iteration_count()) && (animation->playback_rate() < 0 || !isinf(end_time().value)))
+ return false;
+
// A compositor-handled one-iteration effect has a document timer that wakes the main thread at its active end.
if ((m_is_compositor_driven || m_is_compositor_replaced) && (!isinf(iteration_count()) || m_is_observation_relevant_compositor_animation))
return true;
diff --git a/Libraries/LibWeb/Animations/KeyframeEffect.h b/Libraries/LibWeb/Animations/KeyframeEffect.h
index 67a875bfe4b..cc1fd138c95 100644
--- a/Libraries/LibWeb/Animations/KeyframeEffect.h
+++ b/Libraries/LibWeb/Animations/KeyframeEffect.h
@@ -212,6 +212,7 @@ public:
void update_computed_properties_for_style(AnimationUpdateContext&, DOM::AbstractElement);
private:
+ bool may_animate_display() const;
bool css_animation_events_are_heard(bool only_iteration_events) const;
friend class Animation;
diff --git a/Tests/LibWeb/Text/expected/css/display-none-subtree-animation-throttling.txt b/Tests/LibWeb/Text/expected/css/display-none-subtree-animation-throttling.txt
new file mode 100644
index 00000000000..094e8d4f2a1
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/display-none-subtree-animation-throttling.txt
@@ -0,0 +1,10 @@
+animation with no box builds overlays: false
+animation with no box requests frames: false
+computed style samples animation: 50px
+referenceable geometry builds overlays: true
+SVG resource builds overlays: true
+custom property animation requests frames: true
+reverse playback finishes
+zero duration infinite iterations finishes
+revealed animation builds overlays: true
+revealed computed style: advanced
diff --git a/Tests/LibWeb/Text/input/css/display-none-subtree-animation-throttling.html b/Tests/LibWeb/Text/input/css/display-none-subtree-animation-throttling.html
new file mode 100644
index 00000000000..7501a851a6e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/display-none-subtree-animation-throttling.html
@@ -0,0 +1,68 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<div id="custom-display" style="--display: none; display: var(--display)"></div>
+<div id="hidden" style="display: none">
+ <svg id="plain"><circle></circle></svg>
+ <svg><linearGradient><stop id="stop"></stop></linearGradient></svg>
+ <svg><circle id="named-circle"></circle></svg>
+</div>
+<script>
+ function nextFrame() {
+ return new Promise(resolve => requestAnimationFrame(resolve));
+ }
+
+ async function animationOverlayBuildsOverFrames(frameCount) {
+ for (let i = 0; i < 2; ++i)
+ await nextFrame();
+ internals.resetStyleInvalidationCounters();
+ for (let i = 0; i < frameCount; ++i)
+ await nextFrame();
+ return internals.getStyleInvalidationCounters().animatedStyleOverlayBuilds;
+ }
+
+ function animate(element, keyframes) {
+ return element.animate(keyframes, { duration: 100000, iterations: Infinity });
+ }
+
+ asyncTest(async done => {
+ const plainCircle = document.querySelector("#plain circle");
+ const dashes = { strokeDashoffset: ["0px", "100px"] };
+ const animation = animate(plainCircle, dashes);
+ println(`animation with no box builds overlays: ${(await animationOverlayBuildsOverFrames(2)) > 0}`);
+ println(`animation with no box requests frames: ${internals.getStyleInvalidationCounters().animationFramePumpRequests > 0}`);
+
+ animation.currentTime = 50000;
+ println(`computed style samples animation: ${getComputedStyle(plainCircle).strokeDashoffset}`);
+
+ document.getElementById("named-circle").id = "referenced-circle";
+ const referenceable = animate(document.getElementById("referenced-circle"), dashes);
+ println(`referenceable geometry builds overlays: ${(await animationOverlayBuildsOverFrames(2)) > 0}`);
+ referenceable.cancel();
+
+ const resource = animate(document.getElementById("stop"), { stopOpacity: [0, 1] });
+ println(`SVG resource builds overlays: ${(await animationOverlayBuildsOverFrames(2)) > 0}`);
+ resource.cancel();
+
+ const customDisplay = animate(document.getElementById("custom-display"), { "--display": ["none", "block"] });
+ await animationOverlayBuildsOverFrames(2);
+ println(`custom property animation requests frames: ${internals.getStyleInvalidationCounters().animationFramePumpRequests > 0}`);
+ customDisplay.cancel();
+
+ const reversed = animate(plainCircle, dashes);
+ reversed.currentTime = 5;
+ reversed.playbackRate = -1;
+ await reversed.finished;
+ println("reverse playback finishes");
+ reversed.cancel();
+
+ const finiteEnd = plainCircle.animate(dashes, { duration: 0, iterations: Infinity, endDelay: 5 });
+ await finiteEnd.finished;
+ println("zero duration infinite iterations finishes");
+ finiteEnd.cancel();
+
+ document.getElementById("hidden").style.display = "block";
+ println(`revealed animation builds overlays: ${(await animationOverlayBuildsOverFrames(2)) > 0}`);
+ println(`revealed computed style: ${getComputedStyle(plainCircle).strokeDashoffset === "50px" ? "stale" : "advanced"}`);
+ done();
+ });
+</script>