Skip to content

Truly independent web browser

LibWeb: Run animations with implicit keyframes on the compositor

Andreas Kling40f412d3d0d4
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 74c36d01cc3..cf1e9b0520a 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -9257,6 +9257,7 @@ void Document::update_compositor_animations(Layout::BegunRead const& read)
struct CompetingPropertyEffects {
GC::Ptr<Animations::KeyframeEffect> winner;
bool all_effects_use_replace { true };
+ u32 effect_count { 0 };
};
struct CompetingEffects {
CompetingPropertyEffects opacity;
@@ -9596,6 +9597,7 @@ void Document::update_compositor_animations(Layout::BegunRead const& read)
continue;
auto& effects = competing_effects.ensure(*target);
auto add_competing_effect = [&](CompetingPropertyEffects& property_effects) {
+ ++property_effects.effect_count;
bool effect_produces_output = effect.is_in_effect();
if (effect_produces_output && effect.composite() != Bindings::CompositeOperation::Replace)
property_effects.all_effects_use_replace = false;
@@ -9627,17 +9629,33 @@ void Document::update_compositor_animations(Layout::BegunRead const& read)
Bindings::CompositeOperationOrAuto::Replace);
});
};
+ // A keyframe that leaves a property out takes the underlying value, which is the output of the effects beneath the
+ // winner where there are any. The compositor only knows the element's own style.
+ auto winner_takes_the_underlying_value = [](Animations::KeyframeEffect& effect, auto is_property) {
+ auto const* key_frame_set = effect.key_frame_set();
+ if (!key_frame_set)
+ return false;
+ return any_of(key_frame_set->keyframes_by_key, [&](auto const& entry) {
+ return any_of(entry.properties, [&](auto const& property) {
+ return is_property(property.key.id()) && property.value.template has<Animations::KeyframeEffect::KeyFrameSet::UseInitial>();
+ });
+ });
+ };
for (auto& [target, effects] : competing_effects) {
(void)target;
- auto validate_winner = [&](CompetingPropertyEffects& property_effects) {
+ auto validate_winner = [&](CompetingPropertyEffects& property_effects, auto is_property) {
if (!property_effects.all_effects_use_replace
- || (property_effects.winner && !winner_uses_replace_keyframes(*property_effects.winner)))
+ || (property_effects.winner && !winner_uses_replace_keyframes(*property_effects.winner))
+ || (property_effects.winner && property_effects.effect_count > 1 && winner_takes_the_underlying_value(*property_effects.winner, is_property)))
property_effects.winner = nullptr;
};
- validate_winner(effects.opacity);
- validate_winner(effects.background_color);
- validate_winner(effects.filter);
- validate_winner(effects.transform);
+ auto is = [](CSS::PropertyID property_id) {
+ return [property_id](CSS::PropertyID id) { return id == property_id; };
+ };
+ validate_winner(effects.opacity, is(CSS::PropertyID::Opacity));
+ validate_winner(effects.background_color, is(CSS::PropertyID::BackgroundColor));
+ validate_winner(effects.filter, is(CSS::PropertyID::Filter));
+ validate_winner(effects.transform, is_transform_family_property);
}
// An opacity of 1 does not otherwise create an effects frame. Give an otherwise publishable opacity
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index 7b32f7d5f4b..f9074120296 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -1951,6 +1951,9 @@ void Element::swap_in_style_record(Layout::BegunRead const& read, Optional<CSS::
{
if (pseudo_element.has_value()) {
VERIFY(is_synthetic_pseudo_element(*pseudo_element));
+ // NB: The compositor animations of a pseudo-element keep the values they took from its style for as long as
+ // the generation of its element stays the same.
+ ++m_animation_style_generation;
if (record)
ensure_synthetic_pseudo_element(*pseudo_element).swap_in_style_record(read, record);
else if (auto existing_pseudo_element = get_synthetic_pseudo_element(*pseudo_element); existing_pseudo_element.has_value())
@@ -5446,6 +5449,8 @@ void Element::set_computed_style(Layout::BegunRead const& read, Optional<CSS::Ps
note_subtree_style_moved();
if (pseudo_element_type.has_value()) {
VERIFY(is_synthetic_pseudo_element(*pseudo_element_type));
+ // NB: As for a pseudo-element's style record, see swap_in_style_record().
+ ++m_animation_style_generation;
if (style)
ensure_synthetic_pseudo_element(*pseudo_element_type).set_computed_style(read, move(style));
else if (auto existing_pseudo_element = get_synthetic_pseudo_element(*pseudo_element_type); existing_pseudo_element.has_value())
diff --git a/Libraries/LibWeb/Rust/src/painting/visual_animation_builder.rs b/Libraries/LibWeb/Rust/src/painting/visual_animation_builder.rs
index 31241104f6e..8e9c794eb88 100644
--- a/Libraries/LibWeb/Rust/src/painting/visual_animation_builder.rs
+++ b/Libraries/LibWeb/Rust/src/painting/visual_animation_builder.rs
@@ -653,10 +653,12 @@ fn lower_keyframe(
return lower_transform_keyframe(request, host, index, keyframe, identity_shapes);
}
};
- if property.state_in(keyframe) != FfiCompositorKeyframeValueState::Present {
+ let state = property.state_in(keyframe);
+ if state == FfiCompositorKeyframeValueState::Absent {
return Some(None);
}
- let resolved = host.resolved_keyframe_value(index, property, false)?;
+ let uses_underlying_style = state == FfiCompositorKeyframeValueState::UsesUnderlyingStyle;
+ let resolved = host.resolved_keyframe_value(index, property, uses_underlying_style)?;
let value = match kind {
FfiVisualAnimationTargetKind::Opacity => {
VisualAnimationValue::Opacity(opacity_from_style_value(resolved.data())?)
@@ -1244,6 +1246,30 @@ mod tests {
assert!(!state.build(&request, &Host::new(&ffi_host), |_| vec![3]).built);
}
+ #[test]
+ fn opacity_keyframes_that_take_the_underlying_style_build_from_it() {
+ let host = TestHost::default()
+ .with_underlying_value(0, AnimatedProperty::Opacity, opacity_value(0.25))
+ .with_value(1, AnimatedProperty::Opacity, opacity_value(0.5))
+ .with_underlying_value(2, AnimatedProperty::Opacity, opacity_value(0.25));
+ let ffi_host = host.ffi();
+ let underlying = FfiCompositorKeyframeValueState::UsesUnderlyingStyle;
+ let keyframes = [
+ keyframe(0.0, AnimatedProperty::Opacity, underlying),
+ present(0.5, AnimatedProperty::Opacity),
+ keyframe(1.0, AnimatedProperty::Opacity, underlying),
+ ];
+ let ffi_request = build_request(FfiVisualAnimationTargetKind::Opacity, &keyframes, 0);
+ let request = Request::with_keyframes(&ffi_request, &keyframes);
+ let mut state = CompositorAnimationEffectState::default();
+
+ assert!(state.build(&request, &Host::new(&ffi_host), |_| vec![1]).built);
+ let animation = pending_animation(&state, FfiVisualAnimationTargetKind::Opacity);
+ assert_eq!(animation.keyframes[0].value, VisualAnimationValue::Opacity(0.25));
+ assert_eq!(animation.keyframes[1].value, VisualAnimationValue::Opacity(0.5));
+ assert_eq!(animation.keyframes[2].value, VisualAnimationValue::Opacity(0.25));
+ }
+
#[test]
fn transform_precision_inputs_do_not_replace_compositor_interpolation_angles() {
let mut value = transformation(transform_function::ROTATE, vec![deg(1e30 * 360.0)]);
diff --git a/Tests/LibWeb/Text/expected/css/compositor-animation-implicit-keyframes-over-other-effects.txt b/Tests/LibWeb/Text/expected/css/compositor-animation-implicit-keyframes-over-other-effects.txt
new file mode 100644
index 00000000000..6cf63375813
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/compositor-animation-implicit-keyframes-over-other-effects.txt
@@ -0,0 +1,2 @@
+over another animation: 0
+alone: 1
diff --git a/Tests/LibWeb/Text/expected/css/compositor-animation-implicit-keyframes.txt b/Tests/LibWeb/Text/expected/css/compositor-animation-implicit-keyframes.txt
index 96c98d718e7..91a603dceec 100644
--- a/Tests/LibWeb/Text/expected/css/compositor-animation-implicit-keyframes.txt
+++ b/Tests/LibWeb/Text/expected/css/compositor-animation-implicit-keyframes.txt
@@ -1,3 +1,3 @@
-opacity-pulse: 0
-color-pulse: 0
-filter-pulse: 0
+opacity-pulse: 1
+color-pulse: 1
+filter-pulse: 1
diff --git a/Tests/LibWeb/Text/expected/css/compositor-animation-missing-endpoint.txt b/Tests/LibWeb/Text/expected/css/compositor-animation-missing-endpoint.txt
index 24ff0c16504..34018755e2c 100644
--- a/Tests/LibWeb/Text/expected/css/compositor-animation-missing-endpoint.txt
+++ b/Tests/LibWeb/Text/expected/css/compositor-animation-missing-endpoint.txt
@@ -1,3 +1,3 @@
-animations stay on the main thread: true
+missing endpoints take the underlying style on the compositor: true
complete keyframes use the compositor: true
-removing the endpoint returns to the main thread: true
+removing the endpoint stays on the compositor: true
diff --git a/Tests/LibWeb/Text/expected/css/compositor-animation-pseudo-element-underlying-style.txt b/Tests/LibWeb/Text/expected/css/compositor-animation-pseudo-element-underlying-style.txt
new file mode 100644
index 00000000000..c349a3d346e
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/compositor-animation-pseudo-element-underlying-style.txt
@@ -0,0 +1,2 @@
+compositor animations: 1
+keyframes resolved again: true
diff --git a/Tests/LibWeb/Text/input/HTML/frame-in-flight/hover-lane-leaves-transitions-beside-an-animation-to-the-host.html b/Tests/LibWeb/Text/input/HTML/frame-in-flight/hover-lane-leaves-transitions-beside-an-animation-to-the-host.html
index 258aeaa6d2f..6a016d9257c 100644
--- a/Tests/LibWeb/Text/input/HTML/frame-in-flight/hover-lane-leaves-transitions-beside-an-animation-to-the-host.html
+++ b/Tests/LibWeb/Text/input/HTML/frame-in-flight/hover-lane-leaves-transitions-beside-an-animation-to-the-host.html
@@ -3,8 +3,8 @@
<script src="frame-in-flight.js"></script>
<style>
body { margin: 0; }
- #box { width: 100px; height: 60px; animation: fade 100s linear infinite; transition: outline-color 100s linear; outline: 4px solid rgb(0, 0, 255); }
- @keyframes fade { to { opacity: 0.5; } }
+ #box { width: 100px; height: 60px; animation: round 100s linear infinite; transition: outline-color 100s linear; outline: 4px solid rgb(0, 0, 255); }
+ @keyframes round { to { border-top-left-radius: 10px; } }
#box:hover { height: 80px; outline-color: rgb(0, 128, 0); }
</style>
<div id="box"></div>
diff --git a/Tests/LibWeb/Text/input/css/compositor-animation-implicit-keyframes-over-other-effects.html b/Tests/LibWeb/Text/input/css/compositor-animation-implicit-keyframes-over-other-effects.html
new file mode 100644
index 00000000000..5729d931aa3
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/compositor-animation-implicit-keyframes-over-other-effects.html
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ @keyframes pulse { 50% { opacity: 0.5; } }
+ @keyframes fade { from { opacity: 0.2; } to { opacity: 0.4; } }
+ div { width: 10px; height: 10px; background-color: blue; }
+</style>
+<div id="target"></div>
+<script>
+ asyncTest(async done => {
+ // The 0% and 100% keyframes of the pulse take the output of the fade beneath it, which only the main thread has.
+ target.style.animation = "fade 100s linear infinite, pulse 100s linear infinite";
+ await Promise.all(target.getAnimations().map(animation => animation.ready));
+ internals.updateCompositorAnimations();
+ println(`over another animation: ${internals.getStyleInvalidationCounters().compositorVisualAnimations}`);
+
+ target.style.animation = "pulse 100s linear infinite";
+ await Promise.all(target.getAnimations().map(animation => animation.ready));
+ internals.updateCompositorAnimations();
+ println(`alone: ${internals.getStyleInvalidationCounters().compositorVisualAnimations}`);
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/css/compositor-animation-missing-endpoint.html b/Tests/LibWeb/Text/input/css/compositor-animation-missing-endpoint.html
index 34d4cd9cb8a..ab5e17c9ffc 100644
--- a/Tests/LibWeb/Text/input/css/compositor-animation-missing-endpoint.html
+++ b/Tests/LibWeb/Text/input/css/compositor-animation-missing-endpoint.html
@@ -26,21 +26,21 @@
asyncTest(async done => {
await Promise.all(document.getAnimations().map(animation => animation.ready));
internals.updateCompositorAnimations();
- println(`animations stay on the main thread: ${internals.getStyleInvalidationCounters().compositorVisualAnimations === 0}`);
+ println(`missing endpoints take the underlying style on the compositor: ${internals.getStyleInvalidationCounters().compositorVisualAnimations === 2}`);
const backgroundAnimation = document.getElementById("target").getAnimations()[0];
backgroundAnimation.effect.setKeyframes([
{ backgroundColor: "rgb(35, 35, 35)", offset: 0 },
{ backgroundColor: "rgb(65, 65, 65)", offset: 1 },
]);
internals.updateCompositorAnimations();
- println(`complete keyframes use the compositor: ${internals.getStyleInvalidationCounters().compositorVisualAnimations === 1}`);
+ println(`complete keyframes use the compositor: ${internals.getStyleInvalidationCounters().compositorVisualAnimations === 2}`);
backgroundAnimation.effect.setKeyframes([
{ backgroundColor: "rgb(35, 35, 35)", offset: 0 },
{ backgroundColor: "rgb(65, 65, 65)", offset: 0.7 },
]);
internals.updateCompositorAnimations();
- println(`removing the endpoint returns to the main thread: ${internals.getStyleInvalidationCounters().compositorVisualAnimations === 0}`);
+ println(`removing the endpoint stays on the compositor: ${internals.getStyleInvalidationCounters().compositorVisualAnimations === 2}`);
for (const animation of document.getAnimations())
animation.cancel();
done();
diff --git a/Tests/LibWeb/Text/input/css/compositor-animation-pseudo-element-underlying-style.html b/Tests/LibWeb/Text/input/css/compositor-animation-pseudo-element-underlying-style.html
new file mode 100644
index 00000000000..aedc6452870
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/compositor-animation-pseudo-element-underlying-style.html
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ @keyframes pulse { 50% { opacity: 0.5; } }
+ #target::before { content: ""; display: block; width: 10px; height: 10px; background-color: blue; animation: pulse 100s linear infinite; }
+ #target.faded::before { opacity: 0.2; }
+</style>
+<div id="target"></div>
+<script>
+ asyncTest(async done => {
+ await Promise.all(document.getAnimations().map(animation => animation.ready));
+ internals.updateCompositorAnimations();
+ println(`compositor animations: ${internals.getStyleInvalidationCounters().compositorVisualAnimations}`);
+
+ // A change to the style of the pseudo-element resolves its keyframes again, as they take its opacity.
+ const resolutions = internals.getStyleInvalidationCounters().compositorKeyframeValueResolutions;
+ target.classList.add("faded");
+ internals.updateStyle();
+ internals.updateCompositorAnimations();
+ println(`keyframes resolved again: ${internals.getStyleInvalidationCounters().compositorKeyframeValueResolutions > resolutions}`);
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/css/offscreen-paint-only-animation.html b/Tests/LibWeb/Text/input/css/offscreen-paint-only-animation.html
index d91be8b1243..67e7a964753 100644
--- a/Tests/LibWeb/Text/input/css/offscreen-paint-only-animation.html
+++ b/Tests/LibWeb/Text/input/css/offscreen-paint-only-animation.html
@@ -6,7 +6,7 @@
to { background-position: calc(100px - var(--offset)) 50%, 0 50%; }
}
body { margin: 0; }
- .target { position: absolute; top: 200vh; width: 100px; height: 100px; }
+ .target { position: absolute; top: 200vh; width: 100px; height: 100px; border-top: 1px solid; }
</style>
<script>
asyncTest(async done => {
@@ -28,8 +28,8 @@
target.className = "target";
document.body.append(target);
const animation = new Animation(new KeyframeEffect(target, [
- { backgroundColor: "rgb(0, 0, 0)", offset: 0 },
- { backgroundColor: "rgb(200, 0, 0)", offset: 0.8 },
+ { borderTopColor: "rgb(0, 0, 0)", offset: 0 },
+ { borderTopColor: "rgb(200, 0, 0)", offset: 0.8 },
], { duration: 1000, iterations: Infinity }), timeline);
animation.play();
animation.startTime = 0;
@@ -39,12 +39,12 @@
println(`offscreen background stops the frame pump: ${counters.animationFramePumpRequests === 0}`);
timeline.setTimeForObservation(400);
- println(`computed style samples the current color: ${getComputedStyle(target).backgroundColor === "rgb(100, 0, 0)"}`);
+ println(`computed style samples the current color: ${getComputedStyle(target).borderTopColor === "rgb(100, 0, 0)"}`);
timeline.setTime(600);
timeline.setTimeForObservation(600);
target.scrollIntoView();
await settle();
- println(`scrolling into view samples the current color: ${getComputedStyle(target).backgroundColor === "rgb(150, 0, 0)"}`);
+ println(`scrolling into view samples the current color: ${getComputedStyle(target).borderTopColor === "rgb(150, 0, 0)"}`);
counters = await countersOverFrames();
println(`visible background resumes the frame pump: ${counters.animationFramePumpRequests > 0}`);
@@ -69,7 +69,7 @@
target.style.cssText = "";
const child = document.createElement("div");
- child.style.cssText = "position: fixed; top: 0; width: 20px; height: 20px; background-color: inherit";
+ child.style.cssText = "position: fixed; top: 0; width: 20px; height: 20px; border-top: 1px solid; border-top-color: inherit";
target.append(child);
await settle();
counters = await countersOverFrames();