CompositeAnimation.cpp [plain text]
#include "config.h"
#include "CompositeAnimation.h"
#include "AnimationControllerPrivate.h"
#include "CSSPropertyLonghand.h"
#include "CSSPropertyNames.h"
#include "ImplicitAnimation.h"
#include "KeyframeAnimation.h"
#include "RenderObject.h"
#include "RenderStyle.h"
#include "WebKitAnimation.h"
#include "WebKitAnimationList.h"
namespace WebCore {
CompositeAnimation::~CompositeAnimation()
{
clearRenderer();
m_transitions.clear();
m_keyframeAnimations.clear();
}
void CompositeAnimation::clearRenderer()
{
if (!m_transitions.isEmpty()) {
CSSPropertyTransitionsMap::const_iterator transitionsEnd = m_transitions.end();
for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != transitionsEnd; ++it) {
ImplicitAnimation* transition = it->second.get();
animationController()->animationWillBeRemoved(transition);
transition->clear();
}
}
if (!m_keyframeAnimations.isEmpty()) {
m_keyframeAnimations.checkConsistency();
AnimationNameMap::const_iterator animationsEnd = m_keyframeAnimations.end();
for (AnimationNameMap::const_iterator it = m_keyframeAnimations.begin(); it != animationsEnd; ++it) {
KeyframeAnimation* anim = it->second.get();
animationController()->animationWillBeRemoved(anim);
anim->clear();
}
}
}
void CompositeAnimation::updateTransitions(RenderObject* renderer, RenderStyle* currentStyle, RenderStyle* targetStyle)
{
if (!currentStyle || (!targetStyle->transitions() && m_transitions.isEmpty()))
return;
CSSPropertyTransitionsMap::const_iterator end = m_transitions.end();
for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != end; ++it)
it->second->setActive(false);
RefPtr<RenderStyle> modifiedCurrentStyle;
if (targetStyle->transitions()) {
for (size_t i = 0; i < targetStyle->transitions()->size(); ++i) {
const Animation* anim = targetStyle->transitions()->animation(i);
bool isActiveTransition = anim->duration() || anim->delay() > 0;
int prop = anim->property();
if (prop == cAnimateNone)
continue;
bool all = prop == cAnimateAll;
for (int propertyIndex = 0; propertyIndex < AnimationBase::getNumProperties(); ++propertyIndex) {
if (all) {
bool isShorthand;
prop = AnimationBase::getPropertyAtIndex(propertyIndex, isShorthand);
if (isShorthand)
continue;
}
ASSERT(prop >= firstCSSProperty && prop < (firstCSSProperty + numCSSProperties));
RefPtr<KeyframeAnimation> keyframeAnim = getAnimationForProperty(prop);
RenderStyle* fromStyle = keyframeAnim ? keyframeAnim->unanimatedStyle() : currentStyle;
ImplicitAnimation* implAnim = m_transitions.get(prop).get();
bool equal = true;
if (implAnim) {
if (!implAnim->postActive())
implAnim->setActive(true);
if (!implAnim->isTargetPropertyEqual(prop, targetStyle)) {
#if USE(ACCELERATED_COMPOSITING)
if (AnimationBase::animationOfPropertyIsAccelerated(prop) && implAnim->isAccelerated()) {
if (!modifiedCurrentStyle)
modifiedCurrentStyle = RenderStyle::clone(currentStyle);
implAnim->blendPropertyValueInStyle(prop, modifiedCurrentStyle.get());
}
#endif
animationController()->animationWillBeRemoved(implAnim);
m_transitions.remove(prop);
equal = false;
}
} else {
equal = !isActiveTransition || AnimationBase::propertiesEqual(prop, fromStyle, targetStyle);
}
if (!equal && isActiveTransition) {
m_transitions.set(prop, ImplicitAnimation::create(const_cast<Animation*>(anim), prop, renderer, this, modifiedCurrentStyle ? modifiedCurrentStyle.get() : fromStyle));
}
if (!all)
break;
}
}
}
Vector<int> toBeRemoved;
end = m_transitions.end();
for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != end; ++it) {
ImplicitAnimation* anim = it->second.get();
if (!anim->active()) {
animationController()->animationWillBeRemoved(anim);
toBeRemoved.append(anim->animatingProperty());
}
}
for (size_t j = 0; j < toBeRemoved.size(); ++j)
m_transitions.remove(toBeRemoved[j]);
}
void CompositeAnimation::updateKeyframeAnimations(RenderObject* renderer, RenderStyle* currentStyle, RenderStyle* targetStyle)
{
if (m_keyframeAnimations.isEmpty() && !targetStyle->hasAnimations())
return;
m_keyframeAnimations.checkConsistency();
AnimationNameMap::const_iterator kfend = m_keyframeAnimations.end();
if (currentStyle && currentStyle->hasAnimations() && targetStyle->hasAnimations() && *(currentStyle->animations()) == *(targetStyle->animations())) {
for (AnimationNameMap::const_iterator it = m_keyframeAnimations.begin(); it != kfend; ++it) {
if (it->second->postActive())
it->second->setIndex(-1);
}
} else {
for (AnimationNameMap::const_iterator it = m_keyframeAnimations.begin(); it != kfend; ++it)
it->second->setIndex(-1);
m_keyframeAnimationOrderMap.clear();
DEFINE_STATIC_LOCAL(const AtomicString, none, ("none"));
if (targetStyle->animations()) {
int numAnims = targetStyle->animations()->size();
for (int i = 0; i < numAnims; ++i) {
const Animation* anim = targetStyle->animations()->animation(i);
AtomicString animationName(anim->name());
if (!anim->isValidAnimation())
continue;
RefPtr<KeyframeAnimation> keyframeAnim = m_keyframeAnimations.get(animationName.impl());
if (keyframeAnim) {
if (keyframeAnim->postActive())
continue;
keyframeAnim->updatePlayState(anim->playState());
keyframeAnim->setAnimation(anim);
keyframeAnim->setIndex(i);
} else if ((anim->duration() || anim->delay()) && anim->iterationCount() && animationName != none) {
keyframeAnim = KeyframeAnimation::create(const_cast<Animation*>(anim), renderer, i, this, targetStyle);
m_keyframeAnimations.set(keyframeAnim->name().impl(), keyframeAnim);
}
if (keyframeAnim)
m_keyframeAnimationOrderMap.append(keyframeAnim->name().impl());
}
}
}
Vector<AtomicStringImpl*> animsToBeRemoved;
kfend = m_keyframeAnimations.end();
for (AnimationNameMap::const_iterator it = m_keyframeAnimations.begin(); it != kfend; ++it) {
KeyframeAnimation* keyframeAnim = it->second.get();
if (keyframeAnim->index() < 0) {
animsToBeRemoved.append(keyframeAnim->name().impl());
animationController()->animationWillBeRemoved(keyframeAnim);
keyframeAnim->clear();
}
}
for (size_t j = 0; j < animsToBeRemoved.size(); ++j)
m_keyframeAnimations.remove(animsToBeRemoved[j]);
}
PassRefPtr<RenderStyle> CompositeAnimation::animate(RenderObject* renderer, RenderStyle* currentStyle, RenderStyle* targetStyle)
{
RefPtr<RenderStyle> resultStyle;
updateTransitions(renderer, currentStyle, targetStyle);
updateKeyframeAnimations(renderer, currentStyle, targetStyle);
m_keyframeAnimations.checkConsistency();
if (currentStyle) {
if (!m_transitions.isEmpty()) {
CSSPropertyTransitionsMap::const_iterator end = m_transitions.end();
for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != end; ++it) {
if (ImplicitAnimation* anim = it->second.get())
anim->animate(this, renderer, currentStyle, targetStyle, resultStyle);
}
}
}
for (Vector<AtomicStringImpl*>::const_iterator it = m_keyframeAnimationOrderMap.begin(); it != m_keyframeAnimationOrderMap.end(); ++it) {
RefPtr<KeyframeAnimation> keyframeAnim = m_keyframeAnimations.get(*it);
if (keyframeAnim)
keyframeAnim->animate(this, renderer, currentStyle, targetStyle, resultStyle);
}
return resultStyle ? resultStyle.release() : targetStyle;
}
PassRefPtr<RenderStyle> CompositeAnimation::getAnimatedStyle() const
{
RefPtr<RenderStyle> resultStyle;
CSSPropertyTransitionsMap::const_iterator end = m_transitions.end();
for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != end; ++it) {
if (ImplicitAnimation* implicitAnimation = it->second.get())
implicitAnimation->getAnimatedStyle(resultStyle);
}
m_keyframeAnimations.checkConsistency();
for (Vector<AtomicStringImpl*>::const_iterator it = m_keyframeAnimationOrderMap.begin(); it != m_keyframeAnimationOrderMap.end(); ++it) {
RefPtr<KeyframeAnimation> keyframeAnimation = m_keyframeAnimations.get(*it);
if (keyframeAnimation)
keyframeAnimation->getAnimatedStyle(resultStyle);
}
return resultStyle;
}
void CompositeAnimation::setAnimating(bool animating)
{
if (!m_transitions.isEmpty()) {
CSSPropertyTransitionsMap::const_iterator transitionsEnd = m_transitions.end();
for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != transitionsEnd; ++it) {
ImplicitAnimation* transition = it->second.get();
transition->setAnimating(animating);
}
}
if (!m_keyframeAnimations.isEmpty()) {
m_keyframeAnimations.checkConsistency();
AnimationNameMap::const_iterator animationsEnd = m_keyframeAnimations.end();
for (AnimationNameMap::const_iterator it = m_keyframeAnimations.begin(); it != animationsEnd; ++it) {
KeyframeAnimation* anim = it->second.get();
anim->setAnimating(animating);
}
}
}
double CompositeAnimation::timeToNextService() const
{
double minT = -1;
if (!m_transitions.isEmpty()) {
CSSPropertyTransitionsMap::const_iterator transitionsEnd = m_transitions.end();
for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != transitionsEnd; ++it) {
ImplicitAnimation* transition = it->second.get();
double t = transition ? transition->timeToNextService() : -1;
if (t < minT || minT == -1)
minT = t;
if (minT == 0)
return 0;
}
}
if (!m_keyframeAnimations.isEmpty()) {
m_keyframeAnimations.checkConsistency();
AnimationNameMap::const_iterator animationsEnd = m_keyframeAnimations.end();
for (AnimationNameMap::const_iterator it = m_keyframeAnimations.begin(); it != animationsEnd; ++it) {
KeyframeAnimation* animation = it->second.get();
double t = animation ? animation->timeToNextService() : -1;
if (t < minT || minT == -1)
minT = t;
if (minT == 0)
return 0;
}
}
return minT;
}
PassRefPtr<KeyframeAnimation> CompositeAnimation::getAnimationForProperty(int property) const
{
RefPtr<KeyframeAnimation> retval;
if (!m_keyframeAnimations.isEmpty()) {
m_keyframeAnimations.checkConsistency();
AnimationNameMap::const_iterator animationsEnd = m_keyframeAnimations.end();
for (AnimationNameMap::const_iterator it = m_keyframeAnimations.begin(); it != animationsEnd; ++it) {
RefPtr<KeyframeAnimation> anim = it->second;
if (anim->hasAnimationForProperty(property))
retval = anim;
}
}
return retval;
}
void CompositeAnimation::suspendAnimations()
{
if (m_suspended)
return;
m_suspended = true;
if (!m_keyframeAnimations.isEmpty()) {
m_keyframeAnimations.checkConsistency();
AnimationNameMap::const_iterator animationsEnd = m_keyframeAnimations.end();
for (AnimationNameMap::const_iterator it = m_keyframeAnimations.begin(); it != animationsEnd; ++it) {
if (KeyframeAnimation* anim = it->second.get())
anim->updatePlayState(AnimPlayStatePaused);
}
}
if (!m_transitions.isEmpty()) {
CSSPropertyTransitionsMap::const_iterator transitionsEnd = m_transitions.end();
for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != transitionsEnd; ++it) {
ImplicitAnimation* anim = it->second.get();
if (anim && anim->hasStyle())
anim->updatePlayState(AnimPlayStatePaused);
}
}
}
void CompositeAnimation::resumeAnimations()
{
if (!m_suspended)
return;
m_suspended = false;
if (!m_keyframeAnimations.isEmpty()) {
m_keyframeAnimations.checkConsistency();
AnimationNameMap::const_iterator animationsEnd = m_keyframeAnimations.end();
for (AnimationNameMap::const_iterator it = m_keyframeAnimations.begin(); it != animationsEnd; ++it) {
KeyframeAnimation* anim = it->second.get();
if (anim && anim->playStatePlaying())
anim->updatePlayState(AnimPlayStatePlaying);
}
}
if (!m_transitions.isEmpty()) {
CSSPropertyTransitionsMap::const_iterator transitionsEnd = m_transitions.end();
for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != transitionsEnd; ++it) {
ImplicitAnimation* anim = it->second.get();
if (anim && anim->hasStyle())
anim->updatePlayState(AnimPlayStatePlaying);
}
}
}
void CompositeAnimation::overrideImplicitAnimations(int property)
{
CSSPropertyTransitionsMap::const_iterator end = m_transitions.end();
if (!m_transitions.isEmpty()) {
for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != end; ++it) {
ImplicitAnimation* anim = it->second.get();
if (anim && anim->animatingProperty() == property)
anim->setOverridden(true);
}
}
}
void CompositeAnimation::resumeOverriddenImplicitAnimations(int property)
{
if (!m_transitions.isEmpty()) {
CSSPropertyTransitionsMap::const_iterator end = m_transitions.end();
for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != end; ++it) {
ImplicitAnimation* anim = it->second.get();
if (anim && anim->animatingProperty() == property)
anim->setOverridden(false);
}
}
}
bool CompositeAnimation::isAnimatingProperty(int property, bool acceleratedOnly, bool isRunningNow) const
{
if (!m_keyframeAnimations.isEmpty()) {
m_keyframeAnimations.checkConsistency();
AnimationNameMap::const_iterator animationsEnd = m_keyframeAnimations.end();
for (AnimationNameMap::const_iterator it = m_keyframeAnimations.begin(); it != animationsEnd; ++it) {
KeyframeAnimation* anim = it->second.get();
if (anim && anim->isAnimatingProperty(property, acceleratedOnly, isRunningNow))
return true;
}
}
if (!m_transitions.isEmpty()) {
CSSPropertyTransitionsMap::const_iterator transitionsEnd = m_transitions.end();
for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != transitionsEnd; ++it) {
ImplicitAnimation* anim = it->second.get();
if (anim && anim->isAnimatingProperty(property, acceleratedOnly, isRunningNow))
return true;
}
}
return false;
}
bool CompositeAnimation::pauseAnimationAtTime(const AtomicString& name, double t)
{
if (!name)
return false;
m_keyframeAnimations.checkConsistency();
RefPtr<KeyframeAnimation> keyframeAnim = m_keyframeAnimations.get(name.impl());
if (!keyframeAnim || !keyframeAnim->running())
return false;
int count = keyframeAnim->m_animation->iterationCount();
if ((t >= 0.0) && (!count || (t <= count * keyframeAnim->duration()))) {
keyframeAnim->freezeAtTime(t);
return true;
}
return false;
}
bool CompositeAnimation::pauseTransitionAtTime(int property, double t)
{
if ((property < firstCSSProperty) || (property >= firstCSSProperty + numCSSProperties))
return false;
ImplicitAnimation* implAnim = m_transitions.get(property).get();
if (!implAnim) {
HashSet<int> shorthandProperties = AnimationBase::animatableShorthandsAffectingProperty(property);
bool anyPaused = false;
HashSet<int>::const_iterator end = shorthandProperties.end();
for (HashSet<int>::const_iterator it = shorthandProperties.begin(); it != end; ++it) {
if (pauseTransitionAtTime(*it, t))
anyPaused = true;
}
return anyPaused;
}
if (!implAnim->running())
return false;
if ((t >= 0.0) && (t <= implAnim->duration())) {
implAnim->freezeAtTime(t);
return true;
}
return false;
}
unsigned CompositeAnimation::numberOfActiveAnimations() const
{
unsigned count = 0;
if (!m_keyframeAnimations.isEmpty()) {
m_keyframeAnimations.checkConsistency();
AnimationNameMap::const_iterator animationsEnd = m_keyframeAnimations.end();
for (AnimationNameMap::const_iterator it = m_keyframeAnimations.begin(); it != animationsEnd; ++it) {
KeyframeAnimation* anim = it->second.get();
if (anim->running())
++count;
}
}
if (!m_transitions.isEmpty()) {
CSSPropertyTransitionsMap::const_iterator transitionsEnd = m_transitions.end();
for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != transitionsEnd; ++it) {
ImplicitAnimation* anim = it->second.get();
if (anim->running())
++count;
}
}
return count;
}
PassRefPtr<WebKitAnimationList> CompositeAnimation::animations() const
{
RefPtr<WebKitAnimationList> animations = WebKitAnimationList::create();
if (!m_keyframeAnimations.isEmpty()) {
m_keyframeAnimations.checkConsistency();
for (Vector<AtomicStringImpl*>::const_iterator it = m_keyframeAnimationOrderMap.begin(); it != m_keyframeAnimationOrderMap.end(); ++it) {
RefPtr<KeyframeAnimation> keyframeAnimation = m_keyframeAnimations.get(*it);
if (keyframeAnimation) {
RefPtr<WebKitAnimation> anim = WebKitAnimation::create(keyframeAnimation);
animations->append(anim);
}
}
}
return animations;
}
}