diff --git a/src/core/animation.cpp b/src/core/animation.cpp index e02dc76be..037d8faa5 100644 --- a/src/core/animation.cpp +++ b/src/core/animation.cpp @@ -852,23 +852,33 @@ namespace umbriel { } bool AnimatedValue::finishSpringTail(double pixelsPerUnit) { - if (!m_animating || !m_physics || !std::isfinite(pixelsPerUnit) || pixelsPerUnit <= 0.0) { + return m_physics && finishQuantizedSpringTail(pixelsPerUnit); + } + + bool AnimatedValue::finishQuantizedSpringTail(double stepsPerUnit) { + if (!m_animating || m_curve.easing != Easing::Spring || !std::isfinite(stepsPerUnit) || stepsPerUnit <= 0.0) { return false; } - const double targetPixels = m_target * pixelsPerUnit; - if (!std::isfinite(targetPixels)) { + const double targetSteps = m_target * stepsPerUnit; + if (!std::isfinite(targetSteps)) { return false; } - const double roundedTarget = std::round(targetPixels); - const double roundingMargin = 0.5 - std::abs(targetPixels - roundedTarget); - const double remaining = springDisplacementBound(m_current, m_target, m_velocity, m_curve.spring) * pixelsPerUnit; - if (roundingMargin <= 0.0 || remaining >= roundingMargin) { + const double roundedTarget = std::round(targetSteps); + const double roundingMargin = 0.5 - std::abs(targetSteps - roundedTarget); + const double remaining = springDisplacementBound(m_current, m_target, m_velocity, m_curve.spring) * stepsPerUnit; + if (!std::isfinite(remaining) || roundingMargin <= 0.0 || remaining >= roundingMargin) { return false; } snap(m_target); return true; } + void AnimatedValue::seedStartMsec(double startMsec) { + if (m_animating && m_startMsec == 0 && std::isfinite(startMsec) && startMsec > 0.0) { + m_startMsec = startMsec; + } + } + void AnimatedValue::translate(double delta) { m_from += delta; m_target += delta; diff --git a/src/core/animation.h b/src/core/animation.h index c2b3cf1c2..4aaf9e2fe 100644 --- a/src/core/animation.h +++ b/src/core/animation.h @@ -186,9 +186,16 @@ namespace umbriel { // Physics spring: continues from the current value with `initialVelocity` in units per second and settles when // the solver reaches the target. Retargeting runs the same solver, but from rest over a precomputed duration. void settleSpring(double to, const SpringConfig& spring, double initialVelocity = 0.0); + static constexpr double kAlphaQuantizationSteps = 255.0; // Stop a physics spring once its remaining energy cannot leave the projected target's pixel rounding cell. Returns // true only when the spring was finished. The solver remains untouched while any visible motion is still possible. [[nodiscard]] bool finishSpringTail(double pixelsPerUnit); + // Stop a spring (physics or duration-based) once its remaining displacement bound is below half a quantized step + // (e.g. stepsPerUnit = 255.0 for 8-bit alpha). Returns true only when the spring was finished. + bool finishQuantizedSpringTail(double stepsPerUnit); + // Seed the animation start clock (e.g. from the latest vblank) so the first tick advances immediately instead of + // stalling for one frame at elapsed = 0. No-op if not animating or already started. + void seedStartMsec(double startMsec); // Shift from, current and target by the same amount, keeping any motion intact. For a coordinate space that is // renumbered under a running animation. void translate(double delta); diff --git a/src/layer/layer_surface.cpp b/src/layer/layer_surface.cpp index ec87743f2..b8a925210 100644 --- a/src/layer/layer_surface.cpp +++ b/src/layer/layer_surface.cpp @@ -148,6 +148,9 @@ namespace umbriel { bool LayerSurface::tickAnimations(double nowMsec) { const bool ticked = m_fade.tick(nowMsec); + if (ticked && effectRegistry().animationPreset(AnimationEvent::Layers) == nullptr) { + m_fade.finishQuantizedSpringTail(AnimatedValue::kAlphaQuantizationSteps); + } if (m_scene != nullptr) { bindAnimationEffect(&m_scene->tree->node, AnimationEvent::Layers, m_fade); bindAnimationEffect(&m_popupTree->node, AnimationEvent::Layers, m_fade); diff --git a/src/output/output.cpp b/src/output/output.cpp index 6b9187f11..60f7cac6f 100644 --- a/src/output/output.cpp +++ b/src/output/output.cpp @@ -2121,10 +2121,7 @@ namespace umbriel { } // Sample animations on this output's vblank cadence rather than when the frame event got dispatched, so event-loop // latency and other outputs' rendering do not make motion steps uneven. - const uint64_t nowNs = static_cast(now.tv_sec) * 1'000'000'000 + static_cast(now.tv_nsec); - const uint64_t vblankNs = latestVblankNs(nowNs, m_lastPresentNs, m_refreshNs); - const uint64_t sampleNs = vblankNs != 0 ? vblankNs : nowNs; - m_server->tickAnimations(m_server->animationFrameClockMsec(static_cast(sampleNs) / 1e6)); + m_server->tickAnimations(latestVblankMsec()); if (stampEffectTime && m_outputEffectsTimed) { applyOutputEffects(); } @@ -2537,6 +2534,15 @@ namespace umbriel { } } + double Output::latestVblankMsec() const { + timespec now{}; + clock_gettime(CLOCK_MONOTONIC, &now); + const uint64_t nowNs = static_cast(now.tv_sec) * 1'000'000'000 + static_cast(now.tv_nsec); + const uint64_t vblankNs = latestVblankNs(nowNs, m_lastPresentNs, m_refreshNs); + const uint64_t sampleNs = vblankNs != 0 ? vblankNs : nowNs; + return m_server->animationFrameClockMsec(static_cast(sampleNs) / 1e6); + } + void Output::handleDestroy() { if (m_output->data == this) { m_output->data = nullptr; diff --git a/src/output/output.h b/src/output/output.h index 410d97c35..3509a9ebe 100644 --- a/src/output/output.h +++ b/src/output/output.h @@ -104,6 +104,7 @@ namespace umbriel { // Seconds persistent effects on this output draw at. Advances on effect frames, follows the clock while nothing // here needs them, and holds the frozen instant while the clock is frozen. float effectSeconds() const { return m_effectSeconds; } + double latestVblankMsec() const; // Effect instances on this output that need frames of their own. unsigned effectEligible() const; void onGammaChanged(wlr_gamma_control_v1* control); diff --git a/src/view/view_animation.cpp b/src/view/view_animation.cpp index 5d7d52449..454eb31f5 100644 --- a/src/view/view_animation.cpp +++ b/src/view/view_animation.cpp @@ -1103,10 +1103,16 @@ namespace umbriel { openingFinished = m_mapped && !m_inScratchpad && !m_fade.animating() && m_fade.target() > m_fade.from(); } if (m_focusDim.tick(nowMsec)) { + if (effectRegistry().animationPreset(AnimationEvent::DimUnfocused) == nullptr) { + m_focusDim.finishQuantizedSpringTail(AnimatedValue::kAlphaQuantizationSteps); + } setFadeAlpha(m_fadeAlpha); active = active || m_focusDim.animating(); } const bool opacityChanged = m_ruleOpacity.tick(nowMsec); + if (opacityChanged) { + m_ruleOpacity.finishQuantizedSpringTail(AnimatedValue::kAlphaQuantizationSteps); + } const bool blurChanged = m_ruleBlur.tick(nowMsec); if (opacityChanged || blurChanged) { setFadeAlpha(m_fadeAlpha); @@ -1118,6 +1124,7 @@ namespace umbriel { active = active || m_ruleOpacity.animating() || m_ruleBlur.animating(); } if (m_modalShadeAlpha.tick(nowMsec)) { + m_modalShadeAlpha.finishQuantizedSpringTail(AnimatedValue::kAlphaQuantizationSteps); syncModalShade(); active = active || m_modalShadeAlpha.animating(); } diff --git a/src/view/view_chrome.cpp b/src/view/view_chrome.cpp index bc1180d68..07a9b2f96 100644 --- a/src/view/view_chrome.cpp +++ b/src/view/view_chrome.cpp @@ -185,6 +185,9 @@ namespace umbriel { const double target = focused || !m_mapped || !animation.enabled || !dim.enabled ? 1.0 : 1.0 - dim.dim; if (m_mapped && focusChanged && animation.enabled && dim.enabled) { m_focusDim.retarget(target, dim.durationMs, dim.curve); + if (const Output* out = currentOutput()) { + m_focusDim.seedStartMsec(out->latestVblankMsec()); + } scheduleFrame(); } else { m_focusDim.snap(target); diff --git a/tests/unit/animation.cpp b/tests/unit/animation.cpp index ef480056f..7b749c989 100644 --- a/tests/unit/animation.cpp +++ b/tests/unit/animation.cpp @@ -474,4 +474,54 @@ UMBRIEL_TEST(animatedColorRefreshesShaderIdentityOnlyWhenRetargeted) { checkShaderSeed(color.shaderSeed()); } +UMBRIEL_TEST(quantizedSpringTailCutsOverdampedAlphaTail) { + umbriel::AnimatedValue dim{1.0}; + dim.retargetSpring(0.8, 3.0, 1800.0); + const uint64_t rawDurationMs = dim.durationMs(); + CHECK(rawDurationMs > 1000); + + CHECK(dim.tick(1000.0)); + CHECK(!dim.finishQuantizedSpringTail(255.0)); + CHECK(dim.animating()); + + double settledAt = 0.0; + for (int frame = 1; frame <= 120 && dim.animating(); ++frame) { + const double now = 1000.0 + static_cast(frame) * (1000.0 / 60.0); + CHECK(dim.tick(now)); + if (dim.finishQuantizedSpringTail(255.0)) { + settledAt = now - 1000.0; + } + } + CHECK(!dim.animating()); + CHECK_EQ(dim.current(), 0.8); + CHECK(settledAt > 0.0); + CHECK(settledAt < static_cast(rawDurationMs) * 0.75); + + // Half-integer 8-bit target (0.5 * 255 = 127.5) sits on a rounding boundary and must not snap early. + umbriel::AnimatedValue halfDim{0.0}; + halfDim.retargetSpring(0.5, 3.0, 1800.0); + CHECK(halfDim.tick(1000.0)); + for (int frame = 1; frame <= 120 && halfDim.animating(); ++frame) { + CHECK(halfDim.tick(1000.0 + static_cast(frame) * (1000.0 / 60.0))); + CHECK(!halfDim.finishQuantizedSpringTail(255.0)); + } +} + +UMBRIEL_TEST(seedStartMsecAdvancesOnFirstTick) { + umbriel::AnimatedValue dim{1.0}; + dim.retargetSpring(0.8, 3.0, 1800.0); + dim.seedStartMsec(1000.0); + + // First tick at the next vblank advances immediately instead of stalling at elapsed = 0. + CHECK(dim.tick(1016.0)); + CHECK(dim.current() < 1.0); + CHECK(dim.current() > 0.8); + + // Subsequent seedStartMsec after start is a no-op. + const double afterFirst = dim.current(); + dim.seedStartMsec(2000.0); + CHECK(dim.tick(1032.0)); + CHECK(dim.current() < afterFirst); +} + int main() { return RUN_TESTS(); }