Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/core/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions src/core/animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/layer/layer_surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 10 additions & 4 deletions src/output/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64_t>(now.tv_sec) * 1'000'000'000 + static_cast<uint64_t>(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<double>(sampleNs) / 1e6));
m_server->tickAnimations(latestVblankMsec());
if (stampEffectTime && m_outputEffectsTimed) {
applyOutputEffects();
}
Expand Down Expand Up @@ -2537,6 +2534,15 @@ namespace umbriel {
}
}

double Output::latestVblankMsec() const {
timespec now{};
clock_gettime(CLOCK_MONOTONIC, &now);
const uint64_t nowNs = static_cast<uint64_t>(now.tv_sec) * 1'000'000'000 + static_cast<uint64_t>(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<double>(sampleNs) / 1e6);
}

void Output::handleDestroy() {
if (m_output->data == this) {
m_output->data = nullptr;
Expand Down
1 change: 1 addition & 0 deletions src/output/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions src/view/view_animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}
Expand Down
3 changes: 3 additions & 0 deletions src/view/view_chrome.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
50 changes: 50 additions & 0 deletions tests/unit/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>(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<double>(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<double>(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(); }