AnimationController.cpp [plain text]
#include "config.h"
#include "AnimationController.h"
#include "AnimationBase.h"
#include "AnimationControllerPrivate.h"
#include "CSSParser.h"
#include "CompositeAnimation.h"
#include "EventNames.h"
#include "Frame.h"
#include "RenderView.h"
#include "WebKitAnimationEvent.h"
#include "WebKitTransitionEvent.h"
#include <wtf/CurrentTime.h>
#include <wtf/UnusedParam.h>
namespace WebCore {
static const double cAnimationTimerDelay = 0.025;
static const double cBeginAnimationUpdateTimeNotSet = -1;
AnimationControllerPrivate::AnimationControllerPrivate(Frame* frame)
: m_animationTimer(this, &AnimationControllerPrivate::animationTimerFired)
, m_updateStyleIfNeededDispatcher(this, &AnimationControllerPrivate::updateStyleIfNeededDispatcherFired)
, m_frame(frame)
, m_beginAnimationUpdateTime(cBeginAnimationUpdateTimeNotSet)
, m_styleAvailableWaiters(0)
, m_lastStyleAvailableWaiter(0)
, m_responseWaiters(0)
, m_lastResponseWaiter(0)
, m_waitingForResponse(false)
{
}
AnimationControllerPrivate::~AnimationControllerPrivate()
{
}
PassRefPtr<CompositeAnimation> AnimationControllerPrivate::accessCompositeAnimation(RenderObject* renderer)
{
RefPtr<CompositeAnimation> animation = m_compositeAnimations.get(renderer);
if (!animation) {
animation = CompositeAnimation::create(this);
m_compositeAnimations.set(renderer, animation);
}
return animation;
}
bool AnimationControllerPrivate::clear(RenderObject* renderer)
{
PassRefPtr<CompositeAnimation> animation = m_compositeAnimations.take(renderer);
if (!animation)
return false;
animation->clearRenderer();
return animation->isSuspended();
}
void AnimationControllerPrivate::updateAnimationTimer(bool callSetChanged)
{
double needsService = -1;
bool calledSetChanged = false;
RenderObjectAnimationMap::const_iterator animationsEnd = m_compositeAnimations.end();
for (RenderObjectAnimationMap::const_iterator it = m_compositeAnimations.begin(); it != animationsEnd; ++it) {
CompositeAnimation* compAnim = it->second.get();
if (!compAnim->isSuspended() && compAnim->hasAnimations()) {
double t = compAnim->timeToNextService();
if (t != -1 && (t < needsService || needsService == -1))
needsService = t;
if (needsService == 0) {
if (callSetChanged) {
Node* node = it->first->node();
ASSERT(!node || (node->document() && !node->document()->inPageCache()));
node->setNeedsStyleRecalc(SyntheticStyleChange);
calledSetChanged = true;
}
else
break;
}
}
}
if (calledSetChanged)
m_frame->document()->updateStyleIfNeeded();
if (needsService == 0) {
if (!m_animationTimer.isActive() || m_animationTimer.repeatInterval() == 0)
m_animationTimer.startRepeating(cAnimationTimerDelay);
return;
}
if (needsService < 0) {
if (m_animationTimer.isActive())
m_animationTimer.stop();
return;
}
if (m_animationTimer.isActive())
m_animationTimer.stop();
m_animationTimer.startOneShot(needsService);
}
void AnimationControllerPrivate::updateStyleIfNeededDispatcherFired(Timer<AnimationControllerPrivate>*)
{
fireEventsAndUpdateStyle();
}
void AnimationControllerPrivate::fireEventsAndUpdateStyle()
{
RefPtr<Frame> protector = m_frame;
bool updateStyle = !m_eventsToDispatch.isEmpty() || !m_nodeChangesToDispatch.isEmpty();
Vector<EventToDispatch>::const_iterator eventsToDispatchEnd = m_eventsToDispatch.end();
for (Vector<EventToDispatch>::const_iterator it = m_eventsToDispatch.begin(); it != eventsToDispatchEnd; ++it) {
if (it->eventType == eventNames().webkitTransitionEndEvent)
it->element->dispatchEvent(WebKitTransitionEvent::create(it->eventType, it->name, it->elapsedTime));
else
it->element->dispatchEvent(WebKitAnimationEvent::create(it->eventType, it->name, it->elapsedTime));
}
m_eventsToDispatch.clear();
Vector<RefPtr<Node> >::const_iterator nodeChangesToDispatchEnd = m_nodeChangesToDispatch.end();
for (Vector<RefPtr<Node> >::const_iterator it = m_nodeChangesToDispatch.begin(); it != nodeChangesToDispatchEnd; ++it)
(*it)->setNeedsStyleRecalc(SyntheticStyleChange);
m_nodeChangesToDispatch.clear();
if (updateStyle && m_frame)
m_frame->document()->updateStyleIfNeeded();
}
void AnimationControllerPrivate::startUpdateStyleIfNeededDispatcher()
{
if (!m_updateStyleIfNeededDispatcher.isActive())
m_updateStyleIfNeededDispatcher.startOneShot(0);
}
void AnimationControllerPrivate::addEventToDispatch(PassRefPtr<Element> element, const AtomicString& eventType, const String& name, double elapsedTime)
{
m_eventsToDispatch.grow(m_eventsToDispatch.size()+1);
EventToDispatch& event = m_eventsToDispatch[m_eventsToDispatch.size()-1];
event.element = element;
event.eventType = eventType;
event.name = name;
event.elapsedTime = elapsedTime;
startUpdateStyleIfNeededDispatcher();
}
void AnimationControllerPrivate::addNodeChangeToDispatch(PassRefPtr<Node> node)
{
ASSERT(!node || (node->document() && !node->document()->inPageCache()));
if (!node)
return;
m_nodeChangesToDispatch.append(node);
startUpdateStyleIfNeededDispatcher();
}
void AnimationControllerPrivate::animationTimerFired(Timer<AnimationControllerPrivate>*)
{
setBeginAnimationUpdateTime(cBeginAnimationUpdateTimeNotSet);
updateAnimationTimer(true);
fireEventsAndUpdateStyle();
}
bool AnimationControllerPrivate::isAnimatingPropertyOnRenderer(RenderObject* renderer, CSSPropertyID property, bool isRunningNow) const
{
RefPtr<CompositeAnimation> animation = m_compositeAnimations.get(renderer);
if (!animation)
return false;
return animation->isAnimatingProperty(property, isRunningNow);
}
void AnimationControllerPrivate::suspendAnimations(Document* document)
{
setBeginAnimationUpdateTime(cBeginAnimationUpdateTimeNotSet);
RenderObjectAnimationMap::const_iterator animationsEnd = m_compositeAnimations.end();
for (RenderObjectAnimationMap::const_iterator it = m_compositeAnimations.begin(); it != animationsEnd; ++it) {
RenderObject* renderer = it->first;
if (renderer->document() == document) {
CompositeAnimation* compAnim = it->second.get();
compAnim->suspendAnimations();
}
}
updateAnimationTimer();
}
void AnimationControllerPrivate::resumeAnimations(Document* document)
{
setBeginAnimationUpdateTime(cBeginAnimationUpdateTimeNotSet);
RenderObjectAnimationMap::const_iterator animationsEnd = m_compositeAnimations.end();
for (RenderObjectAnimationMap::const_iterator it = m_compositeAnimations.begin(); it != animationsEnd; ++it) {
RenderObject* renderer = it->first;
if (renderer->document() == document) {
CompositeAnimation* compAnim = it->second.get();
compAnim->resumeAnimations();
}
}
updateAnimationTimer();
}
bool AnimationControllerPrivate::pauseAnimationAtTime(RenderObject* renderer, const String& name, double t)
{
if (!renderer)
return false;
RefPtr<CompositeAnimation> compAnim = accessCompositeAnimation(renderer);
if (!compAnim)
return false;
if (compAnim->pauseAnimationAtTime(name, t)) {
renderer->node()->setNeedsStyleRecalc(SyntheticStyleChange);
startUpdateStyleIfNeededDispatcher();
return true;
}
return false;
}
bool AnimationControllerPrivate::pauseTransitionAtTime(RenderObject* renderer, const String& property, double t)
{
if (!renderer)
return false;
RefPtr<CompositeAnimation> compAnim = accessCompositeAnimation(renderer);
if (!compAnim)
return false;
if (compAnim->pauseTransitionAtTime(cssPropertyID(property), t)) {
renderer->node()->setNeedsStyleRecalc(SyntheticStyleChange);
startUpdateStyleIfNeededDispatcher();
return true;
}
return false;
}
double AnimationControllerPrivate::beginAnimationUpdateTime()
{
if (m_beginAnimationUpdateTime == cBeginAnimationUpdateTimeNotSet)
m_beginAnimationUpdateTime = currentTime();
return m_beginAnimationUpdateTime;
}
void AnimationControllerPrivate::endAnimationUpdate()
{
styleAvailable();
if (!m_waitingForResponse)
startTimeResponse(beginAnimationUpdateTime());
}
void AnimationControllerPrivate::receivedStartTimeResponse(double time)
{
m_waitingForResponse = false;
startTimeResponse(time);
}
PassRefPtr<RenderStyle> AnimationControllerPrivate::getAnimatedStyleForRenderer(RenderObject* renderer)
{
if (!renderer)
return 0;
RefPtr<CompositeAnimation> rendererAnimations = m_compositeAnimations.get(renderer);
if (!rendererAnimations)
return renderer->style();
setBeginAnimationUpdateTime(cBeginAnimationUpdateTimeNotSet);
RefPtr<RenderStyle> animatingStyle = rendererAnimations->getAnimatedStyle();
if (!animatingStyle)
animatingStyle = renderer->style();
return animatingStyle.release();
}
unsigned AnimationControllerPrivate::numberOfActiveAnimations() const
{
unsigned count = 0;
RenderObjectAnimationMap::const_iterator animationsEnd = m_compositeAnimations.end();
for (RenderObjectAnimationMap::const_iterator it = m_compositeAnimations.begin(); it != animationsEnd; ++it) {
CompositeAnimation* compAnim = it->second.get();
count += compAnim->numberOfActiveAnimations();
}
return count;
}
void AnimationControllerPrivate::addToStyleAvailableWaitList(AnimationBase* animation)
{
ASSERT(!animation->next());
if (m_styleAvailableWaiters)
m_lastStyleAvailableWaiter->setNext(animation);
else
m_styleAvailableWaiters = animation;
m_lastStyleAvailableWaiter = animation;
animation->setNext(0);
}
void AnimationControllerPrivate::removeFromStyleAvailableWaitList(AnimationBase* animationToRemove)
{
AnimationBase* prevAnimation = 0;
for (AnimationBase* animation = m_styleAvailableWaiters; animation; animation = animation->next()) {
if (animation == animationToRemove) {
if (prevAnimation)
prevAnimation->setNext(animation->next());
else
m_styleAvailableWaiters = animation->next();
if (m_lastStyleAvailableWaiter == animation)
m_lastStyleAvailableWaiter = prevAnimation;
animationToRemove->setNext(0);
}
}
}
void AnimationControllerPrivate::styleAvailable()
{
for (AnimationBase* animation = m_styleAvailableWaiters; animation; ) {
AnimationBase* nextAnimation = animation->next();
animation->setNext(0);
animation->styleAvailable();
animation = nextAnimation;
}
m_styleAvailableWaiters = 0;
m_lastStyleAvailableWaiter = 0;
}
void AnimationControllerPrivate::addToStartTimeResponseWaitList(AnimationBase* animation, bool willGetResponse)
{
ASSERT(!animation->next());
if (willGetResponse)
m_waitingForResponse = true;
if (m_responseWaiters)
m_lastResponseWaiter->setNext(animation);
else
m_responseWaiters = animation;
m_lastResponseWaiter = animation;
animation->setNext(0);
}
void AnimationControllerPrivate::removeFromStartTimeResponseWaitList(AnimationBase* animationToRemove)
{
AnimationBase* prevAnimation = 0;
for (AnimationBase* animation = m_responseWaiters; animation; animation = animation->next()) {
if (animation == animationToRemove) {
if (prevAnimation)
prevAnimation->setNext(animation->next());
else
m_responseWaiters = animation->next();
if (m_lastResponseWaiter == animation)
m_lastResponseWaiter = prevAnimation;
animationToRemove->setNext(0);
}
prevAnimation = animation;
}
}
void AnimationControllerPrivate::startTimeResponse(double time)
{
for (AnimationBase* animation = m_responseWaiters; animation; ) {
AnimationBase* nextAnimation = animation->next();
animation->setNext(0);
animation->onAnimationStartResponse(time);
animation = nextAnimation;
}
m_responseWaiters = 0;
m_lastResponseWaiter = 0;
}
AnimationController::AnimationController(Frame* frame)
: m_data(new AnimationControllerPrivate(frame))
{
}
AnimationController::~AnimationController()
{
delete m_data;
}
void AnimationController::cancelAnimations(RenderObject* renderer)
{
if (!m_data->hasAnimations())
return;
if (m_data->clear(renderer)) {
Node* node = renderer->node();
ASSERT(!node || (node->document() && !node->document()->inPageCache()));
node->setNeedsStyleRecalc(SyntheticStyleChange);
}
}
PassRefPtr<RenderStyle> AnimationController::updateAnimations(RenderObject* renderer, RenderStyle* newStyle)
{
if (!renderer->document() || renderer->document()->inPageCache())
return newStyle;
RenderStyle* oldStyle = renderer->style();
if ((!oldStyle || (!oldStyle->animations() && !oldStyle->transitions())) && (!newStyle->animations() && !newStyle->transitions()))
return newStyle;
if (renderer->view()->printing())
return newStyle;
ASSERT(renderer->node());
RefPtr<CompositeAnimation> rendererAnimations = m_data->accessCompositeAnimation(renderer);
RefPtr<RenderStyle> blendedStyle = rendererAnimations->animate(renderer, oldStyle, newStyle);
m_data->updateAnimationTimer();
if (blendedStyle != newStyle) {
if (blendedStyle->hasAutoZIndex() && (blendedStyle->opacity() < 1.0f || blendedStyle->hasTransform()))
blendedStyle->setZIndex(0);
}
return blendedStyle.release();
}
PassRefPtr<RenderStyle> AnimationController::getAnimatedStyleForRenderer(RenderObject* renderer)
{
return m_data->getAnimatedStyleForRenderer(renderer);
}
void AnimationController::notifyAnimationStarted(RenderObject*, double startTime)
{
m_data->receivedStartTimeResponse(startTime);
}
bool AnimationController::pauseAnimationAtTime(RenderObject* renderer, const String& name, double t)
{
return m_data->pauseAnimationAtTime(renderer, name, t);
}
unsigned AnimationController::numberOfActiveAnimations() const
{
return m_data->numberOfActiveAnimations();
}
bool AnimationController::pauseTransitionAtTime(RenderObject* renderer, const String& property, double t)
{
return m_data->pauseTransitionAtTime(renderer, property, t);
}
bool AnimationController::isAnimatingPropertyOnRenderer(RenderObject* renderer, CSSPropertyID property, bool isRunningNow) const
{
return m_data->isAnimatingPropertyOnRenderer(renderer, property, isRunningNow);
}
void AnimationController::suspendAnimations(Document* document)
{
m_data->suspendAnimations(document);
}
void AnimationController::resumeAnimations(Document* document)
{
m_data->resumeAnimations(document);
}
void AnimationController::beginAnimationUpdate()
{
m_data->setBeginAnimationUpdateTime(cBeginAnimationUpdateTimeNotSet);
}
void AnimationController::endAnimationUpdate()
{
m_data->endAnimationUpdate();
}
bool AnimationController::supportsAcceleratedAnimationOfProperty(CSSPropertyID property)
{
#if USE(ACCELERATED_COMPOSITING)
return AnimationBase::animationOfPropertyIsAccelerated(property);
#else
UNUSED_PARAM(property);
return false;
#endif
}
}