KeyframeAnimation.cpp [plain text]
#include "config.h"
#include "KeyframeAnimation.h"
#include "AnimationControllerPrivate.h"
#include "CSSPropertyAnimation.h"
#include "CSSPropertyNames.h"
#include "CompositeAnimation.h"
#include "EventNames.h"
#include "RenderBoxModelObject.h"
#include "RenderStyle.h"
#include "StyleResolver.h"
namespace WebCore {
KeyframeAnimation::KeyframeAnimation(const Animation& animation, RenderElement* renderer, int index, CompositeAnimation* compositeAnimation, RenderStyle* unanimatedStyle)
: AnimationBase(animation, renderer, compositeAnimation)
, m_keyframes(animation.name())
, m_index(index)
, m_startEventDispatched(false)
, m_unanimatedStyle(unanimatedStyle)
{
if (m_object && m_object->element())
m_object->document().ensureStyleResolver().keyframeStylesForAnimation(m_object->element(), unanimatedStyle, m_keyframes);
validateTransformFunctionList();
#if ENABLE(CSS_FILTERS)
checkForMatchingFilterFunctionLists();
#endif
}
KeyframeAnimation::~KeyframeAnimation()
{
if (!postActive())
endAnimation();
}
void KeyframeAnimation::fetchIntervalEndpointsForProperty(CSSPropertyID property, const RenderStyle*& fromStyle, const RenderStyle*& toStyle, double& prog) const
{
double elapsedTime = getElapsedTime();
if (m_animation->duration() && m_animation->iterationCount() != Animation::IterationCountInfinite)
elapsedTime = std::min(elapsedTime, m_animation->duration() * m_animation->iterationCount());
const double fractionalTime = this->fractionalTime(1, elapsedTime, 0);
size_t numKeyframes = m_keyframes.size();
if (!numKeyframes)
return;
ASSERT(!m_keyframes[0].key());
ASSERT(m_keyframes[m_keyframes.size() - 1].key() == 1);
int prevIndex = -1;
int nextIndex = -1;
for (size_t i = 0; i < numKeyframes; ++i) {
const KeyframeValue& currKeyFrame = m_keyframes[i];
if (!currKeyFrame.containsProperty(property))
continue;
if (fractionalTime < currKeyFrame.key()) {
nextIndex = i;
break;
}
prevIndex = i;
}
double scale = 1;
double offset = 0;
if (prevIndex == -1)
prevIndex = 0;
if (nextIndex == -1)
nextIndex = m_keyframes.size() - 1;
const KeyframeValue& prevKeyframe = m_keyframes[prevIndex];
const KeyframeValue& nextKeyframe = m_keyframes[nextIndex];
fromStyle = prevKeyframe.style();
toStyle = nextKeyframe.style();
offset = prevKeyframe.key();
scale = 1.0 / (nextKeyframe.key() - prevKeyframe.key());
prog = progress(scale, offset, prevKeyframe.timingFunction(name()));
}
void KeyframeAnimation::animate(CompositeAnimation* compositeAnimation, RenderElement*, const RenderStyle*, RenderStyle* targetStyle, RefPtr<RenderStyle>& animatedStyle)
{
fireAnimationEventsIfNeeded();
if (isNew() && m_animation->playState() == AnimPlayStatePlaying && !compositeAnimation->isSuspended())
updateStateMachine(AnimationStateInput::StartAnimation, -1);
if (postActive()) {
if (!animatedStyle)
animatedStyle = const_cast<RenderStyle*>(targetStyle);
return;
}
if (waitingToStart() && m_animation->delay() > 0 && !m_animation->fillsBackwards())
return;
if (!m_keyframes.size()) {
updateStateMachine(AnimationStateInput::EndAnimation, -1);
return;
}
if (!animatedStyle)
animatedStyle = RenderStyle::clone(targetStyle);
HashSet<CSSPropertyID>::const_iterator endProperties = m_keyframes.endProperties();
for (HashSet<CSSPropertyID>::const_iterator it = m_keyframes.beginProperties(); it != endProperties; ++it) {
const RenderStyle* fromStyle = 0;
const RenderStyle* toStyle = 0;
double progress = 0.0;
fetchIntervalEndpointsForProperty(*it, fromStyle, toStyle, progress);
bool needsAnim = CSSPropertyAnimation::blendProperties(this, *it, animatedStyle.get(), fromStyle, toStyle, progress);
if (!needsAnim)
animatedStyle->setIsRunningAcceleratedAnimation();
}
}
void KeyframeAnimation::getAnimatedStyle(RefPtr<RenderStyle>& animatedStyle)
{
if (waitingToStart() && m_animation->delay() > 0 && !m_animation->fillsBackwards())
return;
if (!m_keyframes.size())
return;
if (!animatedStyle)
animatedStyle = RenderStyle::clone(&m_object->style());
HashSet<CSSPropertyID>::const_iterator endProperties = m_keyframes.endProperties();
for (HashSet<CSSPropertyID>::const_iterator it = m_keyframes.beginProperties(); it != endProperties; ++it) {
const RenderStyle* fromStyle = 0;
const RenderStyle* toStyle = 0;
double progress = 0.0;
fetchIntervalEndpointsForProperty(*it, fromStyle, toStyle, progress);
CSSPropertyAnimation::blendProperties(this, *it, animatedStyle.get(), fromStyle, toStyle, progress);
}
}
bool KeyframeAnimation::hasAnimationForProperty(CSSPropertyID property) const
{
return m_keyframes.containsProperty(property);
}
bool KeyframeAnimation::startAnimation(double timeOffset)
{
if (m_object && m_object->isComposited()) {
return toRenderBoxModelObject(m_object)->startAnimation(timeOffset, m_animation.get(), m_keyframes);
}
return false;
}
void KeyframeAnimation::pauseAnimation(double timeOffset)
{
if (!m_object)
return;
if (m_object->isComposited())
toRenderBoxModelObject(m_object)->animationPaused(timeOffset, m_keyframes.animationName());
if (!paused())
setNeedsStyleRecalc(m_object->element());
}
void KeyframeAnimation::endAnimation()
{
if (!m_object)
return;
if (m_object->isComposited())
toRenderBoxModelObject(m_object)->animationFinished(m_keyframes.animationName());
if (!paused())
setNeedsStyleRecalc(m_object->element());
}
bool KeyframeAnimation::shouldSendEventForListener(Document::ListenerType listenerType) const
{
return m_object->document().hasListenerType(listenerType);
}
void KeyframeAnimation::onAnimationStart(double elapsedTime)
{
sendAnimationEvent(eventNames().webkitAnimationStartEvent, elapsedTime);
}
void KeyframeAnimation::onAnimationIteration(double elapsedTime)
{
sendAnimationEvent(eventNames().webkitAnimationIterationEvent, elapsedTime);
}
void KeyframeAnimation::onAnimationEnd(double elapsedTime)
{
sendAnimationEvent(eventNames().webkitAnimationEndEvent, elapsedTime);
if (!m_animation->fillsForwards())
endAnimation();
}
bool KeyframeAnimation::sendAnimationEvent(const AtomicString& eventType, double elapsedTime)
{
Document::ListenerType listenerType;
if (eventType == eventNames().webkitAnimationIterationEvent)
listenerType = Document::ANIMATIONITERATION_LISTENER;
else if (eventType == eventNames().webkitAnimationEndEvent)
listenerType = Document::ANIMATIONEND_LISTENER;
else {
ASSERT(eventType == eventNames().webkitAnimationStartEvent);
if (m_startEventDispatched)
return false;
m_startEventDispatched = true;
listenerType = Document::ANIMATIONSTART_LISTENER;
}
if (shouldSendEventForListener(listenerType)) {
RefPtr<Element> element = m_object->element();
ASSERT(!element || !element->document().inPageCache());
if (!element)
return false;
m_compositeAnimation->animationController()->addEventToDispatch(element, eventType, m_keyframes.animationName(), elapsedTime);
if (eventType == eventNames().webkitAnimationEndEvent && element->renderer())
setNeedsStyleRecalc(element.get());
return true; }
return false; }
void KeyframeAnimation::overrideAnimations()
{
HashSet<CSSPropertyID>::const_iterator end = m_keyframes.endProperties();
for (HashSet<CSSPropertyID>::const_iterator it = m_keyframes.beginProperties(); it != end; ++it)
compositeAnimation()->overrideImplicitAnimations(*it);
}
void KeyframeAnimation::resumeOverriddenAnimations()
{
HashSet<CSSPropertyID>::const_iterator end = m_keyframes.endProperties();
for (HashSet<CSSPropertyID>::const_iterator it = m_keyframes.beginProperties(); it != end; ++it)
compositeAnimation()->resumeOverriddenImplicitAnimations(*it);
}
bool KeyframeAnimation::affectsProperty(CSSPropertyID property) const
{
return m_keyframes.containsProperty(property);
}
void KeyframeAnimation::validateTransformFunctionList()
{
m_transformFunctionListValid = false;
if (m_keyframes.size() < 2 || !m_keyframes.containsProperty(CSSPropertyWebkitTransform))
return;
size_t numKeyframes = m_keyframes.size();
size_t firstNonEmptyTransformKeyframeIndex = numKeyframes;
for (size_t i = 0; i < numKeyframes; ++i) {
const KeyframeValue& currentKeyframe = m_keyframes[i];
if (currentKeyframe.style()->transform().operations().size()) {
firstNonEmptyTransformKeyframeIndex = i;
break;
}
}
if (firstNonEmptyTransformKeyframeIndex == numKeyframes)
return;
const TransformOperations* firstVal = &m_keyframes[firstNonEmptyTransformKeyframeIndex].style()->transform();
for (size_t i = firstNonEmptyTransformKeyframeIndex + 1; i < numKeyframes; ++i) {
const KeyframeValue& currentKeyframe = m_keyframes[i];
const TransformOperations* val = ¤tKeyframe.style()->transform();
if (val->operations().isEmpty())
continue;
if (!firstVal->operationsMatch(*val))
return;
}
m_transformFunctionListValid = true;
}
#if ENABLE(CSS_FILTERS)
void KeyframeAnimation::checkForMatchingFilterFunctionLists()
{
m_filterFunctionListsMatch = false;
if (m_keyframes.size() < 2 || !m_keyframes.containsProperty(CSSPropertyWebkitFilter))
return;
size_t numKeyframes = m_keyframes.size();
size_t firstNonEmptyFilterKeyframeIndex = numKeyframes;
for (size_t i = 0; i < numKeyframes; ++i) {
const KeyframeValue& currentKeyframe = m_keyframes[i];
if (currentKeyframe.style()->filter().operations().size()) {
firstNonEmptyFilterKeyframeIndex = i;
break;
}
}
if (firstNonEmptyFilterKeyframeIndex == numKeyframes)
return;
const FilterOperations* firstVal = &m_keyframes[firstNonEmptyFilterKeyframeIndex].style()->filter();
for (size_t i = firstNonEmptyFilterKeyframeIndex + 1; i < numKeyframes; ++i) {
const KeyframeValue& currentKeyframe = m_keyframes[i];
const FilterOperations* val = ¤tKeyframe.style()->filter();
if (val->operations().isEmpty())
continue;
if (!firstVal->operationsMatch(*val))
return;
}
m_filterFunctionListsMatch = true;
}
#endif
double KeyframeAnimation::timeToNextService()
{
double t = AnimationBase::timeToNextService();
if (t != 0 || preActive())
return t;
HashSet<CSSPropertyID>::const_iterator endProperties = m_keyframes.endProperties();
bool acceleratedPropertiesOnly = true;
for (HashSet<CSSPropertyID>::const_iterator it = m_keyframes.beginProperties(); it != endProperties; ++it) {
if (!CSSPropertyAnimation::animationOfPropertyIsAccelerated(*it) || !isAccelerated()) {
acceleratedPropertiesOnly = false;
break;
}
}
if (acceleratedPropertiesOnly) {
bool isLooping;
getTimeToNextEvent(t, isLooping);
}
return t;
}
}