CompositeAnimation.cpp [plain text]
#include "config.h"
#include "CompositeAnimation.h"
#include "AnimationControllerPrivate.h"
#include "CSSPropertyAnimation.h"
#include "CSSPropertyNames.h"
#include "ImplicitAnimation.h"
#include "KeyframeAnimation.h"
#include "Logging.h"
#include "RenderObject.h"
#include "RenderStyle.h"
#include <wtf/text/CString.h>
namespace WebCore {
CompositeAnimation::CompositeAnimation(AnimationControllerPrivate* animationController)
: m_animationController(animationController)
{
m_suspended = animationController->isSuspended() && !animationController->allowsNewAnimationsWhileSuspended();
}
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->value.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->value.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->value->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 = !m_suspended && (anim->duration() || anim->delay() > 0);
Animation::AnimationMode mode = anim->animationMode();
if (mode == Animation::AnimateNone)
continue;
CSSPropertyID prop = anim->property();
bool all = mode == Animation::AnimateAll;
for (int propertyIndex = 0; propertyIndex < CSSPropertyAnimation::getNumProperties(); ++propertyIndex) {
if (all) {
bool isShorthand;
prop = CSSPropertyAnimation::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);
bool equal = true;
if (implAnim) {
if (!implAnim->postActive())
implAnim->setActive(true);
if (!implAnim->isTargetPropertyEqual(prop, targetStyle)) {
#if USE(ACCELERATED_COMPOSITING)
if (CSSPropertyAnimation::animationOfPropertyIsAccelerated(prop) && implAnim->isAccelerated()) {
if (!modifiedCurrentStyle)
modifiedCurrentStyle = RenderStyle::clone(currentStyle);
implAnim->blendPropertyValueInStyle(prop, modifiedCurrentStyle.get());
}
#endif
LOG(Animations, "Removing existing ImplicitAnimation %p for property %s", implAnim, getPropertyName(prop));
animationController()->animationWillBeRemoved(implAnim);
m_transitions.remove(prop);
equal = false;
}
} else {
equal = !isActiveTransition || CSSPropertyAnimation::propertiesEqual(prop, fromStyle, targetStyle);
}
if (!equal && isActiveTransition) {
RefPtr<ImplicitAnimation> animation = ImplicitAnimation::create(const_cast<Animation*>(anim), prop, renderer, this, modifiedCurrentStyle ? modifiedCurrentStyle.get() : fromStyle);
LOG(Animations, "Created ImplicitAnimation %p for property %s duration %.2f delay %.2f", animation.get(), getPropertyName(prop), anim->duration(), anim->delay());
m_transitions.set(prop, animation.release());
}
if (!all)
break;
}
}
}
Vector<int> toBeRemoved;
end = m_transitions.end();
for (CSSPropertyTransitionsMap::const_iterator it = m_transitions.begin(); it != end; ++it) {
ImplicitAnimation* anim = it->value.get();
if (!anim->active()) {
animationController()->animationWillBeRemoved(anim);
toBeRemoved.append(anim->animatingProperty());
LOG(Animations, "Removing ImplicitAnimation %p for property %s", anim, getPropertyName(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->value->postActive())
it->value->setIndex(-1);
}
} else {
for (AnimationNameMap::const_iterator it = m_keyframeAnimations.begin(); it != kfend; ++it)
it->value->setIndex(-1);
m_keyframeAnimationOrderMap.clear();
DEFINE_STATIC_LOCAL(const AtomicString, none, ("none", AtomicString::ConstructFromLiteral));
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);
LOG(Animations, "Creating KeyframeAnimation %p with keyframes %s, duration %.2f, delay %.2f, iterations %.2f", keyframeAnim.get(), anim->name().utf8().data(), anim->duration(), anim->delay(), anim->iterationCount());
if (m_suspended) {
keyframeAnim->updatePlayState(AnimPlayStatePaused);
LOG(Animations, " (created in suspended/paused state)");
}
#if !LOG_DISABLED
HashSet<CSSPropertyID>::const_iterator endProperties = keyframeAnim->keyframes().endProperties();
for (HashSet<CSSPropertyID>::const_iterator it = keyframeAnim->keyframes().beginProperties(); it != endProperties; ++it)
LOG(Animations, " property %s", getPropertyName(*it));
#endif
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->value.get();
if (keyframeAnim->index() < 0) {
animsToBeRemoved.append(keyframeAnim->name().impl());
animationController()->animationWillBeRemoved(keyframeAnim);
keyframeAnim->clear();
LOG(Animations, "Removing KeyframeAnimation %p", keyframeAnim);
}
}
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->value.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->value.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;
}
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->value.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->value.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(CSSPropertyID 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->value;
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->value.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->value.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->value.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->value.get();
if (anim && anim->hasStyle())
anim->updatePlayState(AnimPlayStatePlaying);
}
}
}
void CompositeAnimation::overrideImplicitAnimations(CSSPropertyID 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->value.get();
if (anim && anim->animatingProperty() == property)
anim->setOverridden(true);
}
}
}
void CompositeAnimation::resumeOverriddenImplicitAnimations(CSSPropertyID 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->value.get();
if (anim && anim->animatingProperty() == property)
anim->setOverridden(false);
}
}
}
bool CompositeAnimation::isAnimatingProperty(CSSPropertyID 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->value.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->value.get();
if (anim && anim->isAnimatingProperty(property, acceleratedOnly, isRunningNow))
return true;
}
}
return false;
}
bool CompositeAnimation::pauseAnimationAtTime(const AtomicString& name, double t)
{
m_keyframeAnimations.checkConsistency();
RefPtr<KeyframeAnimation> keyframeAnim = m_keyframeAnimations.get(name.impl());
if (!keyframeAnim || !keyframeAnim->running())
return false;
double count = keyframeAnim->m_animation->iterationCount();
if ((t >= 0.0) && ((count == Animation::IterationCountInfinite) || (t <= count * keyframeAnim->duration()))) {
keyframeAnim->freezeAtTime(t);
return true;
}
return false;
}
bool CompositeAnimation::pauseTransitionAtTime(CSSPropertyID property, double t)
{
if ((property < firstCSSProperty) || (property >= firstCSSProperty + numCSSProperties))
return false;
ImplicitAnimation* implAnim = m_transitions.get(property);
if (!implAnim) {
HashSet<CSSPropertyID> shorthandProperties = CSSPropertyAnimation::animatableShorthandsAffectingProperty(property);
bool anyPaused = false;
HashSet<CSSPropertyID>::const_iterator end = shorthandProperties.end();
for (HashSet<CSSPropertyID>::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->value.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->value.get();
if (anim->running())
++count;
}
}
return count;
}
}