CompositeAnimation.cpp [plain text]
#include "config.h"
#include "CompositeAnimation.h"
#include "CSSAnimationControllerPrivate.h"
#include "CSSPropertyAnimation.h"
#include "CSSPropertyNames.h"
#include "ImplicitAnimation.h"
#include "KeyframeAnimation.h"
#include "Logging.h"
#include "RenderElement.h"
#include "RenderStyle.h"
#include <wtf/NeverDestroyed.h>
#include <wtf/text/CString.h>
namespace WebCore {
CompositeAnimation::CompositeAnimation(CSSAnimationControllerPrivate& animationController)
: m_animationController(animationController)
{
m_suspended = m_animationController.isSuspended() && !m_animationController.allowsNewAnimationsWhileSuspended();
}
CompositeAnimation::~CompositeAnimation()
{
clearElement();
m_transitions.clear();
m_keyframeAnimations.clear();
}
void CompositeAnimation::clearElement()
{
if (!m_transitions.isEmpty()) {
for (auto& transition : m_transitions.values()) {
animationController().animationWillBeRemoved(transition.get());
transition->clear();
}
}
if (!m_keyframeAnimations.isEmpty()) {
m_keyframeAnimations.checkConsistency();
for (auto& animation : m_keyframeAnimations.values()) {
animationController().animationWillBeRemoved(animation.get());
animation->clear();
}
}
}
void CompositeAnimation::updateTransitions(Element& element, const RenderStyle* currentStyle, const RenderStyle& targetStyle)
{
if (!currentStyle || (!targetStyle.transitions() && m_transitions.isEmpty()))
return;
for (auto& transition : m_transitions.values())
transition->setActive(false);
std::unique_ptr<RenderStyle> modifiedCurrentStyle;
if (targetStyle.transitions()) {
for (size_t i = 0; i < targetStyle.transitions()->size(); ++i) {
auto& animation = targetStyle.transitions()->animation(i);
bool isActiveTransition = animation.duration() || animation.delay() > 0;
Animation::AnimationMode mode = animation.animationMode();
if (mode == Animation::AnimateNone || mode == Animation::AnimateUnknownProperty)
continue;
CSSPropertyID prop = animation.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));
auto* keyframeAnimation = animationForProperty(prop);
auto* fromStyle = keyframeAnimation ? &keyframeAnimation->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 (CSSPropertyAnimation::animationOfPropertyIsAccelerated(prop) && implAnim->isAccelerated()) {
if (!modifiedCurrentStyle)
modifiedCurrentStyle = RenderStyle::clonePtr(*currentStyle);
implAnim->blendPropertyValueInStyle(prop, modifiedCurrentStyle.get());
}
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) {
auto implicitAnimation = ImplicitAnimation::create(animation, prop, element, *this, modifiedCurrentStyle ? *modifiedCurrentStyle : *fromStyle);
if (m_suspended && implicitAnimation->hasStyle())
implicitAnimation->updatePlayState(AnimPlayStatePaused);
LOG(Animations, "Created ImplicitAnimation %p on element %p for property %s duration %.2f delay %.2f", implicitAnimation.ptr(), &element, getPropertyName(prop), animation.duration(), animation.delay());
m_transitions.set(prop, WTFMove(implicitAnimation));
}
if (!all)
break;
}
}
}
Vector<int> toBeRemoved;
for (auto& transition : m_transitions.values()) {
if (!transition->active()) {
animationController().animationWillBeRemoved(transition.get());
toBeRemoved.append(transition->animatingProperty());
LOG(Animations, "Removing ImplicitAnimation %p from element %p for property %s", transition.get(), &element, getPropertyName(transition->animatingProperty()));
}
}
for (auto propertyToRemove : toBeRemoved)
m_transitions.remove(propertyToRemove);
}
void CompositeAnimation::updateKeyframeAnimations(Element& element, const RenderStyle* currentStyle, const RenderStyle& targetStyle)
{
if (m_keyframeAnimations.isEmpty() && !targetStyle.hasAnimations())
return;
m_keyframeAnimations.checkConsistency();
if (currentStyle && currentStyle->hasAnimations() && targetStyle.hasAnimations() && *(currentStyle->animations()) == *(targetStyle.animations()))
return;
#if ENABLE(CSS_ANIMATIONS_LEVEL_2)
m_hasScrollTriggeredAnimation = false;
#endif
AnimationNameMap newAnimations;
m_keyframeAnimationOrderMap.clear();
static NeverDestroyed<const AtomicString> none("none", AtomicString::ConstructFromLiteral);
if (targetStyle.animations()) {
int numAnims = targetStyle.animations()->size();
for (int i = 0; i < numAnims; ++i) {
auto& animation = targetStyle.animations()->animation(i);
AtomicString animationName(animation.name());
if (!animation.isValidAnimation())
continue;
RefPtr<KeyframeAnimation> keyframeAnim = m_keyframeAnimations.get(animationName.impl());
if (keyframeAnim) {
newAnimations.add(keyframeAnim->name().impl(), keyframeAnim);
if (keyframeAnim->postActive())
continue;
#if ENABLE(CSS_ANIMATIONS_LEVEL_2)
if (animation.trigger()->isScrollAnimationTrigger())
m_hasScrollTriggeredAnimation = true;
#endif
keyframeAnim->updatePlayState(animation.playState());
keyframeAnim->setAnimation(animation);
} else if ((animation.duration() || animation.delay()) && animation.iterationCount() && animationName != none) {
keyframeAnim = KeyframeAnimation::create(animation, element, *this, targetStyle);
LOG(Animations, "Creating KeyframeAnimation %p on element %p with keyframes %s, duration %.2f, delay %.2f, iterations %.2f", keyframeAnim.get(), &element, animation.name().utf8().data(), animation.duration(), animation.delay(), animation.iterationCount());
if (m_suspended) {
keyframeAnim->updatePlayState(AnimPlayStatePaused);
LOG(Animations, " (created in suspended/paused state)");
}
#if !LOG_DISABLED
for (auto propertyID : keyframeAnim->keyframes().properties())
LOG(Animations, " property %s", getPropertyName(propertyID));
#endif
#if ENABLE(CSS_ANIMATIONS_LEVEL_2)
if (animation.trigger()->isScrollAnimationTrigger())
m_hasScrollTriggeredAnimation = true;
#endif
newAnimations.set(keyframeAnim->name().impl(), keyframeAnim);
}
if (keyframeAnim)
m_keyframeAnimationOrderMap.append(keyframeAnim->name().impl());
}
}
for (auto& animation : m_keyframeAnimations.values()) {
if (!newAnimations.contains(animation->name().impl())) {
animationController().animationWillBeRemoved(animation.get());
animation->clear();
LOG(Animations, "Removing KeyframeAnimation %p from element %p", animation.get(), &element);
}
}
std::swap(newAnimations, m_keyframeAnimations);
}
AnimationUpdate CompositeAnimation::animate(Element& element, const RenderStyle* currentStyle, const RenderStyle& targetStyle)
{
updateTransitions(element, currentStyle, targetStyle);
updateKeyframeAnimations(element, currentStyle, targetStyle);
m_keyframeAnimations.checkConsistency();
bool animationStateChanged = false;
bool forceStackingContext = false;
std::unique_ptr<RenderStyle> animatedStyle;
if (currentStyle) {
bool checkForStackingContext = false;
for (auto& transition : m_transitions.values()) {
bool didBlendStyle = false;
if (transition->animate(*this, targetStyle, animatedStyle, didBlendStyle))
animationStateChanged = true;
if (didBlendStyle)
checkForStackingContext |= WillChangeData::propertyCreatesStackingContext(transition->animatingProperty());
}
if (animatedStyle && checkForStackingContext) {
if (animatedStyle->opacity() < 1.0f
|| animatedStyle->hasTransformRelatedProperty()
|| animatedStyle->hasMask()
|| animatedStyle->clipPath()
|| animatedStyle->boxReflect()
|| animatedStyle->hasFilter()
#if ENABLE(FILTERS_LEVEL_2)
|| animatedStyle->hasBackdropFilter()
#endif
)
forceStackingContext = true;
}
}
for (auto& name : m_keyframeAnimationOrderMap) {
RefPtr<KeyframeAnimation> keyframeAnim = m_keyframeAnimations.get(name);
if (keyframeAnim) {
bool didBlendStyle = false;
if (keyframeAnim->animate(*this, targetStyle, animatedStyle, didBlendStyle))
animationStateChanged = true;
forceStackingContext |= didBlendStyle && keyframeAnim->triggersStackingContext();
m_hasAnimationThatDependsOnLayout |= keyframeAnim->dependsOnLayout();
}
}
if (forceStackingContext && animatedStyle) {
if (animatedStyle->hasAutoZIndex())
animatedStyle->setZIndex(0);
}
return { WTFMove(animatedStyle), animationStateChanged };
}
std::unique_ptr<RenderStyle> CompositeAnimation::getAnimatedStyle() const
{
std::unique_ptr<RenderStyle> resultStyle;
for (auto& transition : m_transitions.values())
transition->getAnimatedStyle(resultStyle);
m_keyframeAnimations.checkConsistency();
for (auto& name : m_keyframeAnimationOrderMap) {
RefPtr<KeyframeAnimation> keyframeAnimation = m_keyframeAnimations.get(name);
if (keyframeAnimation)
keyframeAnimation->getAnimatedStyle(resultStyle);
}
return resultStyle;
}
std::optional<Seconds> CompositeAnimation::timeToNextService() const
{
std::optional<Seconds> minT;
if (!m_transitions.isEmpty()) {
for (auto& transition : m_transitions.values()) {
std::optional<Seconds> t = transition->timeToNextService();
if (!t)
continue;
if (!minT || t.value() < minT.value())
minT = t.value();
if (minT.value() == 0_s)
return 0_s;
}
}
if (!m_keyframeAnimations.isEmpty()) {
m_keyframeAnimations.checkConsistency();
for (auto& animation : m_keyframeAnimations.values()) {
std::optional<Seconds> t = animation->timeToNextService();
if (!t)
continue;
if (!minT || t.value() < minT.value())
minT = t.value();
if (minT.value() == 0_s)
return 0_s;
}
}
return minT;
}
KeyframeAnimation* CompositeAnimation::animationForProperty(CSSPropertyID property) const
{
KeyframeAnimation* result = nullptr;
if (!m_keyframeAnimations.isEmpty()) {
m_keyframeAnimations.checkConsistency();
for (auto& animation : m_keyframeAnimations.values()) {
if (animation->hasAnimationForProperty(property))
result = animation.get();
}
}
return result;
}
bool CompositeAnimation::computeExtentOfTransformAnimation(LayoutRect& bounds) const
{
bool seenTransformAnimation = false;
for (auto& animation : m_keyframeAnimations.values()) {
if (!animation->hasAnimationForProperty(CSSPropertyTransform))
continue;
if (seenTransformAnimation)
return false;
seenTransformAnimation = true;
if (!animation->computeExtentOfTransformAnimation(bounds))
return false;
}
for (auto& transition : m_transitions.values()) {
if (transition->animatingProperty() != CSSPropertyTransform || !transition->hasStyle())
continue;
if (seenTransformAnimation)
return false;
if (!transition->computeExtentOfTransformAnimation(bounds))
return false;
}
return true;
}
void CompositeAnimation::suspendAnimations()
{
if (m_suspended)
return;
m_suspended = true;
if (!m_keyframeAnimations.isEmpty()) {
m_keyframeAnimations.checkConsistency();
for (auto& animation : m_keyframeAnimations.values())
animation->updatePlayState(AnimPlayStatePaused);
}
if (!m_transitions.isEmpty()) {
for (auto& transition : m_transitions.values()) {
if (transition->hasStyle())
transition->updatePlayState(AnimPlayStatePaused);
}
}
}
void CompositeAnimation::resumeAnimations()
{
if (!m_suspended)
return;
m_suspended = false;
if (!m_keyframeAnimations.isEmpty()) {
m_keyframeAnimations.checkConsistency();
for (auto& animation : m_keyframeAnimations.values()) {
if (animation->playStatePlaying())
animation->updatePlayState(AnimPlayStatePlaying);
}
}
if (!m_transitions.isEmpty()) {
for (auto& transition : m_transitions.values()) {
if (transition->hasStyle())
transition->updatePlayState(AnimPlayStatePlaying);
}
}
}
void CompositeAnimation::overrideImplicitAnimations(CSSPropertyID property)
{
if (!m_transitions.isEmpty()) {
for (auto& transition : m_transitions.values()) {
if (transition->animatingProperty() == property)
transition->setOverridden(true);
}
}
}
void CompositeAnimation::resumeOverriddenImplicitAnimations(CSSPropertyID property)
{
if (!m_transitions.isEmpty()) {
for (auto& transition : m_transitions.values()) {
if (transition->animatingProperty() == property)
transition->setOverridden(false);
}
}
}
bool CompositeAnimation::isAnimatingProperty(CSSPropertyID property, bool acceleratedOnly, AnimationBase::RunningState runningState) const
{
if (!m_keyframeAnimations.isEmpty()) {
m_keyframeAnimations.checkConsistency();
for (auto& animation : m_keyframeAnimations.values()) {
if (animation->isAnimatingProperty(property, acceleratedOnly, runningState))
return true;
}
}
if (!m_transitions.isEmpty()) {
for (auto& transition : m_transitions.values()) {
if (transition->isAnimatingProperty(property, acceleratedOnly, runningState))
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;
keyframeAnim->freezeAtTime(t);
return true;
}
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;
for (auto propertyID : shorthandProperties) {
if (pauseTransitionAtTime(propertyID, 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;
m_keyframeAnimations.checkConsistency();
for (auto& animation : m_keyframeAnimations.values()) {
if (animation->running())
++count;
}
for (auto& transition : m_transitions.values()) {
if (transition->running())
++count;
}
return count;
}
}