AnimationController.cpp [plain text]
#include "config.h"
#include "AnimationController.h"
#include "AnimationBase.h"
#include "AnimationControllerPrivate.h"
#include "CSSParser.h"
#include "CSSPropertyAnimation.h"
#include "CompositeAnimation.h"
#include "EventNames.h"
#include "Frame.h"
#include "FrameView.h"
#include "Logging.h"
#include "PseudoElement.h"
#include "RenderView.h"
#include "TransitionEvent.h"
#include "WebKitAnimationEvent.h"
#include "WebKitTransitionEvent.h"
#include <wtf/CurrentTime.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_animationsWaitingForStyle()
, m_animationsWaitingForStartTimeResponse()
, m_waitingForAsyncStartNotification(false)
, m_isSuspended(false)
{
}
AnimationControllerPrivate::~AnimationControllerPrivate()
{
}
CompositeAnimation* AnimationControllerPrivate::ensureCompositeAnimation(RenderObject* renderer)
{
RenderObjectAnimationMap::AddResult result = m_compositeAnimations.add(renderer, 0);
if (result.isNewEntry)
result.iterator->value = CompositeAnimation::create(this);
return result.iterator->value.get();
}
bool AnimationControllerPrivate::clear(RenderObject* renderer)
{
RefPtr<CompositeAnimation> animation = m_compositeAnimations.take(renderer);
if (!animation)
return false;
animation->clearRenderer();
return animation->isSuspended();
}
double AnimationControllerPrivate::updateAnimations(SetChanged callSetChanged)
{
double timeToNextService = -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->value.get();
if (!compAnim->isSuspended() && compAnim->hasAnimations()) {
double t = compAnim->timeToNextService();
if (t != -1 && (t < timeToNextService || timeToNextService == -1))
timeToNextService = t;
if (!timeToNextService) {
if (callSetChanged == CallSetChanged) {
Node* node = it->key->node();
ASSERT(!node || (node->document() && !node->document()->inPageCache()));
node->setNeedsStyleRecalc(SyntheticStyleChange);
calledSetChanged = true;
}
else
break;
}
}
}
if (calledSetChanged)
m_frame->document()->updateStyleIfNeeded();
return timeToNextService;
}
void AnimationControllerPrivate::updateAnimationTimerForRenderer(RenderObject* renderer)
{
double timeToNextService = 0;
const CompositeAnimation* compositeAnimation = m_compositeAnimations.get(renderer);
if (!compositeAnimation->isSuspended() && compositeAnimation->hasAnimations())
timeToNextService = compositeAnimation->timeToNextService();
if (m_animationTimer.isActive() && (m_animationTimer.repeatInterval() || m_animationTimer.nextFireInterval() <= timeToNextService))
return;
m_animationTimer.startOneShot(timeToNextService);
}
void AnimationControllerPrivate::updateAnimationTimer(SetChanged callSetChanged)
{
double timeToNextService = updateAnimations(callSetChanged);
LOG(Animations, "updateAnimationTimer: timeToNextService is %.2f", timeToNextService);
if (!timeToNextService) {
if (!m_animationTimer.isActive() || m_animationTimer.repeatInterval() == 0)
m_animationTimer.startRepeating(cAnimationTimerDelay);
return;
}
if (timeToNextService < 0) {
if (m_animationTimer.isActive())
m_animationTimer.stop();
return;
}
m_animationTimer.startOneShot(timeToNextService);
}
void AnimationControllerPrivate::updateStyleIfNeededDispatcherFired(Timer<AnimationControllerPrivate>*)
{
fireEventsAndUpdateStyle();
}
void AnimationControllerPrivate::fireEventsAndUpdateStyle()
{
RefPtr<Frame> protector = m_frame;
bool updateStyle = !m_eventsToDispatch.isEmpty() || !m_nodeChangesToDispatch.isEmpty();
Vector<EventToDispatch> eventsToDispatch = m_eventsToDispatch;
m_eventsToDispatch.clear();
Vector<EventToDispatch>::const_iterator eventsToDispatchEnd = eventsToDispatch.end();
for (Vector<EventToDispatch>::const_iterator it = eventsToDispatch.begin(); it != eventsToDispatchEnd; ++it) {
Element* element = it->element.get();
if (it->eventType == eventNames().transitionendEvent)
element->dispatchEvent(TransitionEvent::create(it->eventType, it->name, it->elapsedTime, PseudoElement::pseudoElementNameForEvents(element->pseudoId())));
else
element->dispatchEvent(WebKitAnimationEvent::create(it->eventType, it->name, it->elapsedTime));
}
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();
}
#if ENABLE(REQUEST_ANIMATION_FRAME)
void AnimationControllerPrivate::animationFrameCallbackFired()
{
double timeToNextService = updateAnimations(CallSetChanged);
if (timeToNextService >= 0)
m_frame->document()->view()->scheduleAnimation();
}
#endif
void AnimationControllerPrivate::animationTimerFired(Timer<AnimationControllerPrivate>*)
{
setBeginAnimationUpdateTime(cBeginAnimationUpdateTimeNotSet);
updateAnimationTimer(CallSetChanged);
fireEventsAndUpdateStyle();
}
bool AnimationControllerPrivate::isRunningAnimationOnRenderer(RenderObject* renderer, CSSPropertyID property, bool isRunningNow) const
{
const CompositeAnimation* animation = m_compositeAnimations.get(renderer);
if (!animation)
return false;
return animation->isAnimatingProperty(property, false, isRunningNow);
}
bool AnimationControllerPrivate::isRunningAcceleratedAnimationOnRenderer(RenderObject* renderer, CSSPropertyID property, bool isRunningNow) const
{
const CompositeAnimation* animation = m_compositeAnimations.get(renderer);
if (!animation)
return false;
return animation->isAnimatingProperty(property, true, isRunningNow);
}
void AnimationControllerPrivate::suspendAnimations()
{
if (isSuspended())
return;
suspendAnimationsForDocument(m_frame->document());
for (Frame* child = m_frame->tree()->firstChild(); child; child = child->tree()->nextSibling())
child->animation()->suspendAnimations();
m_isSuspended = true;
}
void AnimationControllerPrivate::resumeAnimations()
{
if (!isSuspended())
return;
resumeAnimationsForDocument(m_frame->document());
for (Frame* child = m_frame->tree()->firstChild(); child; child = child->tree()->nextSibling())
child->animation()->resumeAnimations();
m_isSuspended = false;
}
void AnimationControllerPrivate::suspendAnimationsForDocument(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->key;
if (renderer->document() == document) {
CompositeAnimation* compAnim = it->value.get();
compAnim->suspendAnimations();
}
}
updateAnimationTimer();
}
void AnimationControllerPrivate::resumeAnimationsForDocument(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->key;
if (renderer->document() == document) {
CompositeAnimation* compAnim = it->value.get();
compAnim->resumeAnimations();
}
}
updateAnimationTimer();
}
void AnimationControllerPrivate::startAnimationsIfNotSuspended(Document* document)
{
if (!isSuspended())
resumeAnimationsForDocument(document);
}
bool AnimationControllerPrivate::pauseAnimationAtTime(RenderObject* renderer, const AtomicString& name, double t)
{
if (!renderer)
return false;
CompositeAnimation* compositeAnimation = ensureCompositeAnimation(renderer);
if (compositeAnimation->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;
CompositeAnimation* compositeAnimation = ensureCompositeAnimation(renderer);
if (compositeAnimation->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_waitingForAsyncStartNotification)
startTimeResponse(beginAnimationUpdateTime());
}
void AnimationControllerPrivate::receivedStartTimeResponse(double time)
{
m_waitingForAsyncStartNotification = false;
startTimeResponse(time);
}
PassRefPtr<RenderStyle> AnimationControllerPrivate::getAnimatedStyleForRenderer(RenderObject* renderer)
{
if (!renderer)
return 0;
const CompositeAnimation* rendererAnimations = m_compositeAnimations.get(renderer);
if (!rendererAnimations)
return renderer->style();
RefPtr<RenderStyle> animatingStyle = rendererAnimations->getAnimatedStyle();
if (!animatingStyle)
animatingStyle = renderer->style();
return animatingStyle.release();
}
unsigned AnimationControllerPrivate::numberOfActiveAnimations(Document* document) const
{
unsigned count = 0;
RenderObjectAnimationMap::const_iterator animationsEnd = m_compositeAnimations.end();
for (RenderObjectAnimationMap::const_iterator it = m_compositeAnimations.begin(); it != animationsEnd; ++it) {
RenderObject* renderer = it->key;
CompositeAnimation* compAnim = it->value.get();
if (renderer->document() == document)
count += compAnim->numberOfActiveAnimations();
}
return count;
}
void AnimationControllerPrivate::addToAnimationsWaitingForStyle(AnimationBase* animation)
{
m_animationsWaitingForStartTimeResponse.remove(animation);
m_animationsWaitingForStyle.add(animation);
}
void AnimationControllerPrivate::removeFromAnimationsWaitingForStyle(AnimationBase* animationToRemove)
{
m_animationsWaitingForStyle.remove(animationToRemove);
}
void AnimationControllerPrivate::styleAvailable()
{
WaitingAnimationsSet::const_iterator it = m_animationsWaitingForStyle.begin();
WaitingAnimationsSet::const_iterator end = m_animationsWaitingForStyle.end();
for (; it != end; ++it)
(*it)->styleAvailable();
m_animationsWaitingForStyle.clear();
}
void AnimationControllerPrivate::addToAnimationsWaitingForStartTimeResponse(AnimationBase* animation, bool willGetResponse)
{
if (willGetResponse)
m_waitingForAsyncStartNotification = true;
m_animationsWaitingForStartTimeResponse.add(animation);
}
void AnimationControllerPrivate::removeFromAnimationsWaitingForStartTimeResponse(AnimationBase* animationToRemove)
{
m_animationsWaitingForStartTimeResponse.remove(animationToRemove);
if (m_animationsWaitingForStartTimeResponse.isEmpty())
m_waitingForAsyncStartNotification = false;
}
void AnimationControllerPrivate::startTimeResponse(double time)
{
WaitingAnimationsSet::const_iterator it = m_animationsWaitingForStartTimeResponse.begin();
WaitingAnimationsSet::const_iterator end = m_animationsWaitingForStartTimeResponse.end();
for (; it != end; ++it)
(*it)->onAnimationStartResponse(time);
m_animationsWaitingForStartTimeResponse.clear();
m_waitingForAsyncStartNotification = false;
}
void AnimationControllerPrivate::animationWillBeRemoved(AnimationBase* animation)
{
removeFromAnimationsWaitingForStyle(animation);
removeFromAnimationsWaitingForStartTimeResponse(animation);
}
AnimationController::AnimationController(Frame* frame)
: m_data(adoptPtr(new AnimationControllerPrivate(frame)))
, m_beginAnimationUpdateCount(0)
{
}
AnimationController::~AnimationController()
{
}
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()));
if (node)
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());
CompositeAnimation* rendererAnimations = m_data->ensureCompositeAnimation(renderer);
RefPtr<RenderStyle> blendedStyle = rendererAnimations->animate(renderer, oldStyle, newStyle);
if (renderer->parent() || newStyle->animations() || (oldStyle && oldStyle->animations())) {
m_data->updateAnimationTimerForRenderer(renderer);
#if ENABLE(REQUEST_ANIMATION_FRAME)
if (FrameView* view = renderer->document()->view())
view->scheduleAnimation();
#endif
}
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 AtomicString& name, double t)
{
return m_data->pauseAnimationAtTime(renderer, name, t);
}
unsigned AnimationController::numberOfActiveAnimations(Document* document) const
{
return m_data->numberOfActiveAnimations(document);
}
bool AnimationController::pauseTransitionAtTime(RenderObject* renderer, const String& property, double t)
{
return m_data->pauseTransitionAtTime(renderer, property, t);
}
bool AnimationController::isRunningAnimationOnRenderer(RenderObject* renderer, CSSPropertyID property, bool isRunningNow) const
{
return m_data->isRunningAnimationOnRenderer(renderer, property, isRunningNow);
}
bool AnimationController::isRunningAcceleratedAnimationOnRenderer(RenderObject* renderer, CSSPropertyID property, bool isRunningNow) const
{
return m_data->isRunningAcceleratedAnimationOnRenderer(renderer, property, isRunningNow);
}
bool AnimationController::isSuspended() const
{
return m_data->isSuspended();
}
void AnimationController::suspendAnimations()
{
LOG(Animations, "controller is suspending animations");
m_data->suspendAnimations();
}
void AnimationController::resumeAnimations()
{
LOG(Animations, "controller is resuming animations");
m_data->resumeAnimations();
}
#if ENABLE(REQUEST_ANIMATION_FRAME)
void AnimationController::serviceAnimations()
{
m_data->animationFrameCallbackFired();
}
#endif
void AnimationController::suspendAnimationsForDocument(Document* document)
{
LOG(Animations, "suspending animations for document %p", document);
m_data->suspendAnimationsForDocument(document);
}
void AnimationController::resumeAnimationsForDocument(Document* document)
{
LOG(Animations, "resuming animations for document %p", document);
m_data->resumeAnimationsForDocument(document);
}
void AnimationController::startAnimationsIfNotSuspended(Document* document)
{
LOG(Animations, "animations may start for document %p", document);
m_data->startAnimationsIfNotSuspended(document);
}
void AnimationController::beginAnimationUpdate()
{
if (!m_beginAnimationUpdateCount)
m_data->setBeginAnimationUpdateTime(cBeginAnimationUpdateTimeNotSet);
++m_beginAnimationUpdateCount;
}
void AnimationController::endAnimationUpdate()
{
ASSERT(m_beginAnimationUpdateCount > 0);
--m_beginAnimationUpdateCount;
if (!m_beginAnimationUpdateCount)
m_data->endAnimationUpdate();
}
bool AnimationController::supportsAcceleratedAnimationOfProperty(CSSPropertyID property)
{
#if USE(ACCELERATED_COMPOSITING)
return CSSPropertyAnimation::animationOfPropertyIsAccelerated(property);
#else
UNUSED_PARAM(property);
return false;
#endif
}
}