This source file includes following definitions.
- m_preventScheduledAnimationsChanges
- schedule
- unschedule
- hasAnimations
- hasPendingSynchronization
- notifyIntervalsChanged
- elapsed
- isPaused
- isStarted
- begin
- pause
- resume
- setElapsed
- isTimelineRunning
- scheduleAnimationFrame
- cancelAnimationFrame
- scheduleWakeUp
- wakeupTimerFired
- updateDocumentOrderIndexes
- document
- currentTime
- serviceOnNextFrame
- serviceAnimations
- updateAnimationsAndScheduleFrameIfNeeded
- updateAnimations
#include "config.h"
#include "core/svg/animation/SMILTimeContainer.h"
#include "core/animation/AnimationClock.h"
#include "core/animation/DocumentTimeline.h"
#include "core/dom/ElementTraversal.h"
#include "core/frame/FrameView.h"
#include "core/svg/SVGSVGElement.h"
#include "core/svg/animation/SVGSMILElement.h"
using namespace std;
namespace WebCore {
static const double initialFrameDelay = 0.025;
class DiscardScope {
public:
explicit DiscardScope(SVGSVGElement& timeContainerOwner) : m_discardScopeElement(&timeContainerOwner) { }
private:
RefPtr<SVGSVGElement> m_discardScopeElement;
};
SMILTimeContainer::SMILTimeContainer(SVGSVGElement& owner)
: m_beginTime(0)
, m_pauseTime(0)
, m_resumeTime(0)
, m_accumulatedActiveTime(0)
, m_presetStartTime(0)
, m_frameSchedulingState(Idle)
, m_documentOrderIndexesDirty(false)
, m_wakeupTimer(this, &SMILTimeContainer::wakeupTimerFired)
, m_ownerSVGElement(owner)
#ifndef NDEBUG
, m_preventScheduledAnimationsChanges(false)
#endif
{
}
SMILTimeContainer::~SMILTimeContainer()
{
cancelAnimationFrame();
ASSERT(!m_wakeupTimer.isActive());
#ifndef NDEBUG
ASSERT(!m_preventScheduledAnimationsChanges);
#endif
}
void SMILTimeContainer::schedule(SVGSMILElement* animation, SVGElement* target, const QualifiedName& attributeName)
{
ASSERT(animation->timeContainer() == this);
ASSERT(target);
ASSERT(animation->hasValidAttributeName());
#ifndef NDEBUG
ASSERT(!m_preventScheduledAnimationsChanges);
#endif
ElementAttributePair key(target, attributeName);
OwnPtr<AnimationsVector>& scheduled = m_scheduledAnimations.add(key, nullptr).storedValue->value;
if (!scheduled)
scheduled = adoptPtr(new AnimationsVector);
ASSERT(!scheduled->contains(animation));
scheduled->append(animation);
SMILTime nextFireTime = animation->nextProgressTime();
if (nextFireTime.isFinite())
notifyIntervalsChanged();
}
void SMILTimeContainer::unschedule(SVGSMILElement* animation, SVGElement* target, const QualifiedName& attributeName)
{
ASSERT(animation->timeContainer() == this);
#ifndef NDEBUG
ASSERT(!m_preventScheduledAnimationsChanges);
#endif
ElementAttributePair key(target, attributeName);
AnimationsVector* scheduled = m_scheduledAnimations.get(key);
ASSERT(scheduled);
size_t idx = scheduled->find(animation);
ASSERT(idx != kNotFound);
scheduled->remove(idx);
}
bool SMILTimeContainer::hasAnimations() const
{
return !m_scheduledAnimations.isEmpty();
}
bool SMILTimeContainer::hasPendingSynchronization() const
{
return m_frameSchedulingState == SynchronizeAnimations && m_wakeupTimer.isActive() && !m_wakeupTimer.nextFireInterval();
}
void SMILTimeContainer::notifyIntervalsChanged()
{
if (!isStarted())
return;
if (hasPendingSynchronization())
return;
cancelAnimationFrame();
scheduleWakeUp(0, SynchronizeAnimations);
}
SMILTime SMILTimeContainer::elapsed() const
{
if (!m_beginTime)
return 0;
if (isPaused())
return m_accumulatedActiveTime;
return currentTime() + m_accumulatedActiveTime - lastResumeTime();
}
bool SMILTimeContainer::isPaused() const
{
return m_pauseTime;
}
bool SMILTimeContainer::isStarted() const
{
return m_beginTime;
}
void SMILTimeContainer::begin()
{
ASSERT(!m_beginTime);
double now = currentTime();
m_beginTime = now - m_presetStartTime;
DiscardScope discardScope(m_ownerSVGElement);
SMILTime earliestFireTime = updateAnimations(SMILTime(m_presetStartTime), m_presetStartTime ? true : false);
m_presetStartTime = 0;
if (m_pauseTime) {
m_pauseTime = now;
ASSERT(m_frameSchedulingState == Idle || m_frameSchedulingState == SynchronizeAnimations);
} else if (!hasPendingSynchronization()) {
ASSERT(isTimelineRunning());
if (earliestFireTime.isFinite()) {
SMILTime delay = earliestFireTime - elapsed();
scheduleWakeUp(std::max(initialFrameDelay, delay.value()), SynchronizeAnimations);
}
}
}
void SMILTimeContainer::pause()
{
ASSERT(!isPaused());
m_pauseTime = currentTime();
if (m_beginTime) {
m_accumulatedActiveTime += m_pauseTime - lastResumeTime();
cancelAnimationFrame();
}
m_resumeTime = 0;
}
void SMILTimeContainer::resume()
{
ASSERT(isPaused());
m_resumeTime = currentTime();
m_pauseTime = 0;
scheduleWakeUp(0, SynchronizeAnimations);
}
void SMILTimeContainer::setElapsed(SMILTime time)
{
if (!m_beginTime) {
m_presetStartTime = time.value();
return;
}
cancelAnimationFrame();
double now = currentTime();
m_beginTime = now - time.value();
m_resumeTime = 0;
if (m_pauseTime) {
m_pauseTime = now;
m_accumulatedActiveTime = time.value();
} else {
m_accumulatedActiveTime = 0;
}
#ifndef NDEBUG
m_preventScheduledAnimationsChanges = true;
#endif
GroupedAnimationsMap::iterator end = m_scheduledAnimations.end();
for (GroupedAnimationsMap::iterator it = m_scheduledAnimations.begin(); it != end; ++it) {
AnimationsVector* scheduled = it->value.get();
unsigned size = scheduled->size();
for (unsigned n = 0; n < size; n++)
scheduled->at(n)->reset();
}
#ifndef NDEBUG
m_preventScheduledAnimationsChanges = false;
#endif
updateAnimationsAndScheduleFrameIfNeeded(time, true);
}
bool SMILTimeContainer::isTimelineRunning() const
{
return m_beginTime && !isPaused();
}
void SMILTimeContainer::scheduleAnimationFrame(SMILTime fireTime)
{
ASSERT(isTimelineRunning() && fireTime.isFinite());
ASSERT(!m_wakeupTimer.isActive());
SMILTime delay = fireTime - elapsed();
if (delay.value() < DocumentTimeline::s_minimumDelay) {
serviceOnNextFrame();
} else {
scheduleWakeUp(delay.value() - DocumentTimeline::s_minimumDelay, FutureAnimationFrame);
}
}
void SMILTimeContainer::cancelAnimationFrame()
{
m_frameSchedulingState = Idle;
m_wakeupTimer.stop();
}
void SMILTimeContainer::scheduleWakeUp(double delayTime, FrameSchedulingState frameSchedulingState)
{
ASSERT(frameSchedulingState == SynchronizeAnimations || frameSchedulingState == FutureAnimationFrame);
m_wakeupTimer.startOneShot(delayTime, FROM_HERE);
m_frameSchedulingState = frameSchedulingState;
}
void SMILTimeContainer::wakeupTimerFired(Timer<SMILTimeContainer>*)
{
ASSERT(m_frameSchedulingState == SynchronizeAnimations || m_frameSchedulingState == FutureAnimationFrame);
if (m_frameSchedulingState == FutureAnimationFrame) {
ASSERT(isTimelineRunning());
m_frameSchedulingState = Idle;
serviceOnNextFrame();
} else {
m_frameSchedulingState = Idle;
updateAnimationsAndScheduleFrameIfNeeded(elapsed());
}
}
void SMILTimeContainer::updateDocumentOrderIndexes()
{
unsigned timingElementCount = 0;
for (SVGSMILElement* element = Traversal<SVGSMILElement>::firstWithin(m_ownerSVGElement); element; element = Traversal<SVGSMILElement>::next(*element, &m_ownerSVGElement))
element->setDocumentOrderIndex(timingElementCount++);
m_documentOrderIndexesDirty = false;
}
struct PriorityCompare {
PriorityCompare(SMILTime elapsed) : m_elapsed(elapsed) {}
bool operator()(const RefPtr<SVGSMILElement>& a, const RefPtr<SVGSMILElement>& b)
{
SMILTime aBegin = a->intervalBegin();
SMILTime bBegin = b->intervalBegin();
aBegin = a->isFrozen() && m_elapsed < aBegin ? a->previousIntervalBegin() : aBegin;
bBegin = b->isFrozen() && m_elapsed < bBegin ? b->previousIntervalBegin() : bBegin;
if (aBegin == bBegin)
return a->documentOrderIndex() < b->documentOrderIndex();
return aBegin < bBegin;
}
SMILTime m_elapsed;
};
Document& SMILTimeContainer::document() const
{
return m_ownerSVGElement.document();
}
double SMILTimeContainer::currentTime() const
{
return document().animationClock().currentTime();
}
void SMILTimeContainer::serviceOnNextFrame()
{
if (document().view()) {
document().view()->scheduleAnimation();
m_frameSchedulingState = AnimationFrame;
}
}
void SMILTimeContainer::serviceAnimations(double monotonicAnimationStartTime)
{
if (m_frameSchedulingState != AnimationFrame)
return;
m_frameSchedulingState = Idle;
updateAnimationsAndScheduleFrameIfNeeded(elapsed());
}
void SMILTimeContainer::updateAnimationsAndScheduleFrameIfNeeded(SMILTime elapsed, bool seekToTime)
{
DiscardScope discardScope(m_ownerSVGElement);
SMILTime earliestFireTime = updateAnimations(elapsed, seekToTime);
if (hasPendingSynchronization())
return;
if (!isTimelineRunning())
return;
if (!earliestFireTime.isFinite())
return;
scheduleAnimationFrame(earliestFireTime);
}
SMILTime SMILTimeContainer::updateAnimations(SMILTime elapsed, bool seekToTime)
{
SMILTime earliestFireTime = SMILTime::unresolved();
#ifndef NDEBUG
m_preventScheduledAnimationsChanges = true;
#endif
if (m_documentOrderIndexesDirty)
updateDocumentOrderIndexes();
Vector<RefPtr<SVGSMILElement> > animationsToApply;
GroupedAnimationsMap::iterator end = m_scheduledAnimations.end();
for (GroupedAnimationsMap::iterator it = m_scheduledAnimations.begin(); it != end; ++it) {
AnimationsVector* scheduled = it->value.get();
std::sort(scheduled->begin(), scheduled->end(), PriorityCompare(elapsed));
SVGSMILElement* resultElement = 0;
unsigned size = scheduled->size();
for (unsigned n = 0; n < size; n++) {
SVGSMILElement* animation = scheduled->at(n);
ASSERT(animation->timeContainer() == this);
ASSERT(animation->targetElement());
ASSERT(animation->hasValidAttributeName());
if (!resultElement) {
if (!animation->hasValidAttributeType())
continue;
resultElement = animation;
}
if (!animation->progress(elapsed, resultElement, seekToTime) && resultElement == animation)
resultElement = 0;
SMILTime nextFireTime = animation->nextProgressTime();
if (nextFireTime.isFinite())
earliestFireTime = min(nextFireTime, earliestFireTime);
}
if (resultElement)
animationsToApply.append(resultElement);
}
std::sort(animationsToApply.begin(), animationsToApply.end(), PriorityCompare(elapsed));
unsigned animationsToApplySize = animationsToApply.size();
if (!animationsToApplySize) {
#ifndef NDEBUG
m_preventScheduledAnimationsChanges = false;
#endif
return earliestFireTime;
}
for (unsigned i = 0; i < animationsToApplySize; ++i)
animationsToApply[i]->applyResultsToTarget();
#ifndef NDEBUG
m_preventScheduledAnimationsChanges = false;
#endif
for (unsigned i = 0; i < animationsToApplySize; ++i) {
if (animationsToApply[i]->inDocument() && animationsToApply[i]->isSVGDiscardElement()) {
RefPtr<SVGSMILElement> animDiscard = animationsToApply[i];
RefPtr<SVGElement> targetElement = animDiscard->targetElement();
if (targetElement && targetElement->inDocument()) {
targetElement->remove(IGNORE_EXCEPTION);
ASSERT(!targetElement->inDocument());
}
if (animDiscard->inDocument()) {
animDiscard->remove(IGNORE_EXCEPTION);
ASSERT(!animDiscard->inDocument());
}
}
}
return earliestFireTime;
}
}