From 3bd3b82a2f6e61bb9f5bcc095ef96b39d8e19b80 Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Fri, 9 Dec 2011 12:39:53 +1000 Subject: Fix possible jump in animation timer. If both a stop and start happen within an event loop, ensure they are processed in order. Based on a patch from Charles Yin. Task-number: QTBUG-22865 Change-Id: I6131bd43a6ba5ad4fa37c863a9f4598bf2ac0e01 Reviewed-by: Leonardo Sobral Cunha Reviewed-by: Martin Jones --- src/corelib/animation/qabstractanimation.cpp | 64 +++++++++++++++++----------- 1 file changed, 40 insertions(+), 24 deletions(-) (limited to 'src/corelib/animation/qabstractanimation.cpp') diff --git a/src/corelib/animation/qabstractanimation.cpp b/src/corelib/animation/qabstractanimation.cpp index b529359f71..d852fee7f0 100644 --- a/src/corelib/animation/qabstractanimation.cpp +++ b/src/corelib/animation/qabstractanimation.cpp @@ -168,6 +168,7 @@ Q_GLOBAL_STATIC(QThreadStorage, unifiedTimer) QUnifiedTimer::QUnifiedTimer() : QObject(), defaultDriver(this), lastTick(0), timingInterval(DEFAULT_TIMER_INTERVAL), currentAnimationIdx(0), insideTick(false), consistentTiming(false), slowMode(false), + startAnimationPending(false), stopTimerPending(false), slowdownFactor(5.0f), isPauseTimerActive(false), runningLeafAnimations(0), profilerCallback(0) { time.invalidate(); @@ -284,30 +285,41 @@ void QUnifiedTimer::setTimingInterval(int interval) } } +void QUnifiedTimer::startAnimations() +{ + startAnimationPending = false; + //we transfer the waiting animations into the "really running" state + animations += animationsToStart; + animationsToStart.clear(); + if (!animations.isEmpty()) { + restartAnimationTimer(); + if (!time.isValid()) { + lastTick = 0; + time.start(); + } + } +} + +void QUnifiedTimer::stopTimer() +{ + stopTimerPending = false; + if (animations.isEmpty()) { + animationTimer.stop(); + isPauseTimerActive = false; + // invalidate the start reference time + time.invalidate(); + } +} void QUnifiedTimer::timerEvent(QTimerEvent *event) { //in the case of consistent timing we make sure the orders in which events come is always the same - //for that purpose we do as if the startstoptimer would always fire before the animation timer - if ((consistentTiming && startStopAnimationTimer.isActive()) || - event->timerId() == startStopAnimationTimer.timerId()) { - startStopAnimationTimer.stop(); - - //we transfer the waiting animations into the "really running" state - animations += animationsToStart; - animationsToStart.clear(); - if (animations.isEmpty()) { - animationTimer.stop(); - isPauseTimerActive = false; - // invalidate the start reference time - time.invalidate(); - } else { - restartAnimationTimer(); - if (!time.isValid()) { - lastTick = 0; - time.start(); - } - } + //for that purpose we do as if the startstoptimer would always fire before the animation timer + if (consistentTiming) { + if (stopTimerPending) + stopTimer(); + if (startAnimationPending) + startAnimations(); } if (event->timerId() == animationTimer.timerId()) { @@ -325,8 +337,10 @@ void QUnifiedTimer::registerAnimation(QAbstractAnimation *animation, bool isTopL Q_ASSERT(!QAbstractAnimationPrivate::get(animation)->hasRegisteredTimer); QAbstractAnimationPrivate::get(animation)->hasRegisteredTimer = true; inst->animationsToStart << animation; - if (!inst->startStopAnimationTimer.isActive()) - inst->startStopAnimationTimer.start(STARTSTOP_TIMER_DELAY, inst); + if (!inst->startAnimationPending) { + inst->startAnimationPending = true; + QMetaObject::invokeMethod(inst, "startAnimations", Qt::QueuedConnection); + } } } @@ -349,8 +363,10 @@ void QUnifiedTimer::unregisterAnimation(QAbstractAnimation *animation) if (idx <= inst->currentAnimationIdx) --inst->currentAnimationIdx; - if (inst->animations.isEmpty() && !inst->startStopAnimationTimer.isActive()) - inst->startStopAnimationTimer.start(STARTSTOP_TIMER_DELAY, inst); + if (inst->animations.isEmpty() && !inst->stopTimerPending) { + inst->stopTimerPending = true; + QMetaObject::invokeMethod(inst, "stopTimer", Qt::QueuedConnection); + } } else { inst->animationsToStart.removeOne(animation); } -- cgit v1.2.3