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 <wtf/UnusedParam.h>
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_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::getKeyframeAnimationInterval(const RenderStyle*& fromStyle, const RenderStyle*& toStyle, double& prog) const
{
double elapsedTime = getElapsedTime();
double t = m_animation->duration() ? (elapsedTime / m_animation->duration()) : 1;
int i = static_cast<int>(t);
t -= i;
if (m_animation->direction() && (i & 1))
t = 1 - t;
double scale = 1;
double offset = 0;
Vector<KeyframeValue>::const_iterator endKeyframes = m_keyframes.endKeyframes();
for (Vector<KeyframeValue>::const_iterator it = m_keyframes.beginKeyframes(); it != endKeyframes; ++it) {
if (t < it->key()) {
if (!fromStyle)
break;
scale = 1.0 / (it->key() - offset);
toStyle = it->style();
break;
}
offset = it->key();
fromStyle = it->style();
}
if (!fromStyle || !toStyle)
return;
const TimingFunction* timingFunction = 0;
if (fromStyle->animations() && fromStyle->animations()->size() > 0) {
timingFunction = &(fromStyle->animations()->animation(0)->timingFunction());
}
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)
return;
const RenderStyle* fromStyle = 0;
const RenderStyle* toStyle = 0;
double progress;
getKeyframeAnimationInterval(fromStyle, toStyle, progress);
if (!fromStyle || !toStyle) {
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) {
bool needsAnim = blendProperties(this, *it, animatedStyle.get(), fromStyle, toStyle, progress);
if (needsAnim)
setAnimating();
else {
#if USE(ACCELERATED_COMPOSITING)
animatedStyle->setIsRunningAcceleratedAnimation();
#endif
}
}
}
void KeyframeAnimation::getAnimatedStyle(RefPtr<RenderStyle>& animatedStyle)
{
const RenderStyle* fromStyle = 0;
const RenderStyle* toStyle = 0;
double progress;
getKeyframeAnimationInterval(fromStyle, toStyle, progress);
if (!fromStyle || !toStyle)
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)
blendProperties(this, *it, 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(beginTime);
#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());
}
#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);
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);
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;
Vector<KeyframeValue>::const_iterator end = m_keyframes.endKeyframes();
size_t firstIndex = 0;
Vector<KeyframeValue>::const_iterator firstIt = end;
for (Vector<KeyframeValue>::const_iterator it = m_keyframes.beginKeyframes(); it != end; ++it, ++firstIndex) {
if (it->style()->transform().operations().size() > 0) {
firstIt = it;
break;
}
}
if (firstIt == end)
return;
const TransformOperations* firstVal = &firstIt->style()->transform();
for (Vector<KeyframeValue>::const_iterator it = firstIt+1; it != end; ++it) {
const TransformOperations* val = &it->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) || isFallbackAnimating()) {
acceleratedPropertiesOnly = false;
break;
}
}
if (acceleratedPropertiesOnly) {
bool isLooping;
getTimeToNextEvent(t, isLooping);
}
#endif
return t;
}
}