Truly independent web browser
LibWeb: Start pending CSS transitions at their style change event time
Aliaksandr Kalenik2b719df93115
diff --git a/Libraries/LibWeb/Animations/Animation.cpp b/Libraries/LibWeb/Animations/Animation.cppindex a625e01346b..9d70eed35d2 100644--- a/Libraries/LibWeb/Animations/Animation.cpp+++ b/Libraries/LibWeb/Animations/Animation.cpp@@ -18,6 +18,7 @@#include <LibWeb/Bindings/WrapperWorld.h>#include <LibWeb/CSS/CSSAnimation.h>#include <LibWeb/CSS/CSSNumericValue.h>+#include <LibWeb/CSS/CSSTransition.h>#include <LibWeb/CSS/Length.h>#include <LibWeb/CSS/StyleValues/ComputationContext.h>#include <LibWeb/DOM/Document.h>@@ -1601,6 +1602,14 @@ void Animation::run_pending_play_task()// FIXME: We can get a more accurate time here if we record the actual instant we became ready rather than waiting// to try and run this taskauto ready_time = m_timeline->current_time().value();+ // AD-HOC: A new CSS transition of properties the compositor may run stays pending past the rendering update of its+ // style change event, which already rendered it, on the compositor or here. It became ready at the time of+ // its style change event, from which the compositor runs it, so that a style change a frame later starts+ // the next transition from the value on the screen rather than from this transition's start value.+ if (auto const* keyframe_effect = as_if<KeyframeEffect>(m_effect.ptr());+ is_css_transition() && keyframe_effect && keyframe_effect->may_run_on_the_compositor()+ && as<CSS::CSSTransition>(*this).previous_phase() == CSS::CSSTransition::Phase::Idle)+ ready_time = { TimeValue::Type::Milliseconds, as<CSS::CSSTransition>(*this).style_change_event_time() };// 3. Perform the steps corresponding to the first matching condition below, if any:diff --git a/Libraries/LibWeb/CSS/CSSTransition.cpp b/Libraries/LibWeb/CSS/CSSTransition.cppindex 9c2106bd458..28093aed635 100644--- a/Libraries/LibWeb/CSS/CSSTransition.cpp+++ b/Libraries/LibWeb/CSS/CSSTransition.cpp@@ -284,6 +284,11 @@ void CSSTransition::visit_edges(Cell::Visitor& visitor)visitor.visit(m_keyframe_effect);}+double CSSTransition::style_change_event_time() const+{+ return m_start_time - m_keyframe_effect->start_delay().value;+}+double CSSTransition::timing_function_output_at_time(double t) const{// AD-HOC: If the transition has an empty duration then we get NaN here,diff --git a/Libraries/LibWeb/CSS/CSSTransition.h b/Libraries/LibWeb/CSS/CSSTransition.hindex 87561adf58d..198edf9ef05 100644--- a/Libraries/LibWeb/CSS/CSSTransition.h+++ b/Libraries/LibWeb/CSS/CSSTransition.h@@ -44,6 +44,7 @@ public:virtual Animations::AnimationClass animation_class() const override;virtual int class_specific_composite_order(GC::Ref<Animations::Animation> other) const override;+ double style_change_event_time() const;double transition_start_time() const { return m_start_time; }double transition_end_time() const { return m_end_time; }NonnullRefPtr<StyleValue const> transition_end_value() const { return m_end_value; }diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cppindex b03abc42a3b..5a3d00f1ce4 100644--- a/Libraries/LibWeb/DOM/Document.cpp+++ b/Libraries/LibWeb/DOM/Document.cpp@@ -7606,10 +7606,9 @@ static Painting::CompositorAnimationEffectState::BuildOutcome build_compositor_a// OPTIMIZATION: A new CSS transition is play-pending until the next animation update. Start its compositor// descriptor at the style change event time without resolving the Animation's pending task.- auto const& transition = as<CSS::CSSTransition>(*animation);auto style_change_event_time = Animations::TimeValue {Animations::TimeValue::Type::Milliseconds,- transition.transition_start_time() - effect.start_delay().value,+ as<CSS::CSSTransition>(*animation).style_change_event_time(),};if (!timeline->can_convert_a_timeline_time_to_an_origin_relative_time())return {};diff --git a/Tests/LibWeb/Text/expected/css/transition-retargeted-next-frame-starts-from-current-value.txt b/Tests/LibWeb/Text/expected/css/transition-retargeted-next-frame-starts-from-current-value.txtnew file mode 100644index 00000000000..6a366b86d5f--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/transition-retargeted-next-frame-starts-from-current-value.txt@@ -0,0 +1,2 @@+transition retargeted a frame after it started starts from where it moved to: true+transition retargeted again a frame later starts from where that one moved to: truediff --git a/Tests/LibWeb/Text/input/css/transition-retargeted-next-frame-starts-from-current-value.html b/Tests/LibWeb/Text/input/css/transition-retargeted-next-frame-starts-from-current-value.htmlnew file mode 100644index 00000000000..69bf51f68da--- /dev/null+++ b/Tests/LibWeb/Text/input/css/transition-retargeted-next-frame-starts-from-current-value.html@@ -0,0 +1,52 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>+ #target {+ width: 20px;+ height: 20px;+ transition: transform 1s linear;+ }+</style>+<div id="target"></div>+<script>+ function nextTask() {+ const { promise, resolve } = Promise.withResolvers();+ const channel = new MessageChannel();+ channel.port1.onmessage = resolve;+ channel.port2.postMessage(null);+ return promise;+ }++ async function renderFrameAfter(milliseconds) {+ await new Promise(resolve => setTimeout(resolve, milliseconds));+ internals.injectRenderingOpportunity(performance.now());+ await nextTask();+ }++ // A transition that the last rendering update started has not run yet, so it shows its start value.+ function startOffsetOfTransitionTheLastFrameStarted() {+ return new DOMMatrix(getComputedStyle(target).transform).m41;+ }++ asyncTest(async done => {+ internals.setManualRenderingOpportunities(true);+ await nextTask();+ await renderFrameAfter(0);++ target.style.transform = "translateX(1000px)";+ await renderFrameAfter(0);++ target.style.transform = "translateX(2000px)";+ await renderFrameAfter(50);+ const firstRetargetStartOffset = startOffsetOfTransitionTheLastFrameStarted();+ println(`transition retargeted a frame after it started starts from where it moved to: ${firstRetargetStartOffset > 0}`);++ target.style.transform = "translateX(3000px)";+ await renderFrameAfter(50);+ const secondRetargetStartOffset = startOffsetOfTransitionTheLastFrameStarted();+ println(`transition retargeted again a frame later starts from where that one moved to: ${secondRetargetStartOffset > firstRetargetStartOffset}`);++ internals.setManualRenderingOpportunities(false);+ done();+ });+</script>