KeyframeAnimation.cpp [plain text]
#include "config.h"
#include "KeyframeAnimation.h"
#include "AnimationControllerPrivate.h"
#include "CSSPropertyNames.h"
#include "CSSStyleSelector.h"
#include "CompositeAnimation.h"
#include "EventNames.h"
#include "RenderLayer.h"
#include "RenderLayerBacking.h"
#include "RenderStyle.h"
#include <wtf/UnusedParam.h>
using namespace std;
namespace WebCore {
KeyframeAnimation::KeyframeAnimation(const Animation* animation, RenderObject* renderer, int index, CompositeAnimation* compAnim, RenderStyle* unanimatedStyle)
: AnimationBase(animation, renderer, compAnim)
, m_keyframes(renderer, animation->name())
, m_index(index)
, m_startEventDispatched(false)
, m_unanimatedStyle(unanimatedStyle)
{
if (m_object && m_object->node() && m_object->node()->isElementNode())
m_object->document()->styleSelector()->keyframeStylesForAnimation(static_cast<Element*>(m_object->node()), unanimatedStyle, m_keyframes);
validateTransformFunctionList();
}
KeyframeAnimation::~KeyframeAnimation()
{
if (!postActive())
endAnimation();
}
void KeyframeAnimation::fetchIntervalEndpointsForProperty(int property, const RenderStyle*& fromStyle, const RenderStyle*& toStyle, double& prog) const
{
double elapsedTime = getElapsedTime();
if (m_animation->duration() && m_animation->iterationCount() != Animation::IterationCountInfinite)
elapsedTime = min(elapsedTime, m_animation->duration() * m_animation->iterationCount());
double fractionalTime = m_animation->duration() ? (elapsedTime / m_animation->duration()) : 1;
if (fractionalTime < 0)
fractionalTime = 0;
int iteration = static_cast<int>(fractionalTime);
if (m_animation->iterationCount() != Animation::IterationCountInfinite)
iteration = min(iteration, m_animation->iterationCount() - 1);
fractionalTime -= iteration;
bool reversing = (m_animation->direction() == Animation::AnimationDirectionAlternate) && (iteration & 1);
if (reversing)
fractionalTime = 1 - fractionalTime;
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());
const TimingFunction* timingFunction = 0;
if (fromStyle->animations() && fromStyle->animations()->size() > 0) {
timingFunction = fromStyle->animations()->animation(0)->timingFunction().get();
}
prog = progress(scale, offset, timingFunction);
}
void KeyframeAnimation::animate(CompositeAnimation*, RenderObject*, const RenderStyle*, RenderStyle* targetStyle, RefPtr<RenderStyle>& animatedStyle)
{
fireAnimationEventsIfNeeded();
if (isNew() && m_animation->playState() == AnimPlayStatePlaying)
updateStateMachine(AnimationStateInputStartAnimation, -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(AnimationStateInputEndAnimation, -1);
return;
}
if (!animatedStyle)
animatedStyle = RenderStyle::clone(targetStyle);
HashSet<int>::const_iterator endProperties = m_keyframes.endProperties();
for (HashSet<int>::const_iterator it = m_keyframes.beginProperties(); it != endProperties; ++it) {
int property = *it;
const RenderStyle* fromStyle = 0;
const RenderStyle* toStyle = 0;
double progress = 0.0;
fetchIntervalEndpointsForProperty(property, fromStyle, toStyle, progress);
bool needsAnim = blendProperties(this, property, animatedStyle.get(), fromStyle, toStyle, progress);
if (needsAnim)
setAnimating();
else {
#if USE(ACCELERATED_COMPOSITING)
animatedStyle->setIsRunningAcceleratedAnimation();
#endif
}
}
}
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<int>::const_iterator endProperties = m_keyframes.endProperties();
for (HashSet<int>::const_iterator it = m_keyframes.beginProperties(); it != endProperties; ++it) {
int property = *it;
const RenderStyle* fromStyle = 0;
const RenderStyle* toStyle = 0;
double progress = 0.0;
fetchIntervalEndpointsForProperty(property, fromStyle, toStyle, progress);
blendProperties(this, property, animatedStyle.get(), fromStyle, toStyle, progress);
}
}
bool KeyframeAnimation::hasAnimationForProperty(int property) const
{
HashSet<int>::const_iterator end = m_keyframes.endProperties();
for (HashSet<int>::const_iterator it = m_keyframes.beginProperties(); it != end; ++it) {
if (*it == property)
return true;
}
return false;
}
bool KeyframeAnimation::startAnimation(double timeOffset)
{
#if USE(ACCELERATED_COMPOSITING)
if (m_object && m_object->hasLayer()) {
RenderLayer* layer = toRenderBoxModelObject(m_object)->layer();
if (layer->isComposited())
return layer->backing()->startAnimation(timeOffset, m_animation.get(), m_keyframes);
}
#else
UNUSED_PARAM(timeOffset);
#endif
return false;
}
void KeyframeAnimation::pauseAnimation(double timeOffset)
{
if (!m_object)
return;
#if USE(ACCELERATED_COMPOSITING)
if (m_object->hasLayer()) {
RenderLayer* layer = toRenderBoxModelObject(m_object)->layer();
if (layer->isComposited())
layer->backing()->animationPaused(timeOffset, m_keyframes.animationName());
}
#else
UNUSED_PARAM(timeOffset);
#endif
if (!paused())
setNeedsStyleRecalc(m_object->node());
}
void KeyframeAnimation::endAnimation()
{
if (!m_object)
return;
#if USE(ACCELERATED_COMPOSITING)
if (m_object->hasLayer()) {
RenderLayer* layer = toRenderBoxModelObject(m_object)->layer();
if (layer->isComposited())
layer->backing()->animationFinished(m_keyframes.animationName());
}
#endif
if (!paused())
setNeedsStyleRecalc(m_object->node());
}
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;
if (m_object->node() && m_object->node()->isElementNode())
element = static_cast<Element*>(m_object->node());
ASSERT(!element || (element->document() && !element->document()->inPageCache()));
if (!element)
return false;
m_compAnim->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<int>::const_iterator end = m_keyframes.endProperties();
for (HashSet<int>::const_iterator it = m_keyframes.beginProperties(); it != end; ++it)
compositeAnimation()->overrideImplicitAnimations(*it);
}
void KeyframeAnimation::resumeOverriddenAnimations()
{
HashSet<int>::const_iterator end = m_keyframes.endProperties();
for (HashSet<int>::const_iterator it = m_keyframes.beginProperties(); it != end; ++it)
compositeAnimation()->resumeOverriddenImplicitAnimations(*it);
}
bool KeyframeAnimation::affectsProperty(int property) const
{
HashSet<int>::const_iterator end = m_keyframes.endProperties();
for (HashSet<int>::const_iterator it = m_keyframes.beginProperties(); it != end; ++it) {
if (*it == property)
return true;
}
return false;
}
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->operations().size() != val->operations().size())
return;
for (size_t j = 0; j < firstVal->operations().size(); ++j) {
if (!firstVal->operations()[j]->isSameType(*val->operations()[j]))
return;
}
}
m_transformFunctionListValid = true;
}
double KeyframeAnimation::timeToNextService()
{
double t = AnimationBase::timeToNextService();
#if USE(ACCELERATED_COMPOSITING)
if (t != 0 || preActive())
return t;
HashSet<int>::const_iterator endProperties = m_keyframes.endProperties();
bool acceleratedPropertiesOnly = true;
for (HashSet<int>::const_iterator it = m_keyframes.beginProperties(); it != endProperties; ++it) {
if (!animationOfPropertyIsAccelerated(*it) || !isAccelerated()) {
acceleratedPropertiesOnly = false;
break;
}
}
if (acceleratedPropertiesOnly) {
bool isLooping;
getTimeToNextEvent(t, isLooping);
}
#endif
return t;
}
}