AnimationTimeline.cpp [plain text]
#include "config.h"
#include "AnimationTimeline.h"
#include "Animation.h"
#include "AnimationEffectReadOnly.h"
#include "AnimationList.h"
#include "CSSAnimation.h"
#include "CSSPropertyAnimation.h"
#include "CSSTransition.h"
#include "DocumentTimeline.h"
#include "Element.h"
#include "KeyframeEffectReadOnly.h"
#include "RenderStyle.h"
#include "RenderView.h"
#include "StylePropertyShorthand.h"
#include "WebAnimationUtilities.h"
#include <wtf/text/TextStream.h>
#include <wtf/text/WTFString.h>
namespace WebCore {
AnimationTimeline::AnimationTimeline(ClassType classType)
: m_classType(classType)
{
}
AnimationTimeline::~AnimationTimeline()
{
}
void AnimationTimeline::addAnimation(Ref<WebAnimation>&& animation)
{
m_animationsWithoutTarget.add(animation.ptr());
m_animations.add(WTFMove(animation));
timingModelDidChange();
}
void AnimationTimeline::removeAnimation(Ref<WebAnimation>&& animation)
{
m_animationsWithoutTarget.remove(animation.ptr());
m_animations.remove(WTFMove(animation));
timingModelDidChange();
}
std::optional<double> AnimationTimeline::bindingsCurrentTime()
{
auto time = currentTime();
if (!time)
return std::nullopt;
return secondsToWebAnimationsAPITime(*time);
}
void AnimationTimeline::setCurrentTime(Seconds currentTime)
{
m_currentTime = currentTime;
timingModelDidChange();
}
HashMap<Element*, ListHashSet<RefPtr<WebAnimation>>>& AnimationTimeline::relevantMapForAnimation(WebAnimation& animation)
{
if (animation.isCSSAnimation())
return m_elementToCSSAnimationsMap;
if (animation.isCSSTransition())
return m_elementToCSSTransitionsMap;
return m_elementToAnimationsMap;
}
void AnimationTimeline::animationWasAddedToElement(WebAnimation& animation, Element& element)
{
m_animationsWithoutTarget.remove(&animation);
relevantMapForAnimation(animation).ensure(&element, [] {
return ListHashSet<RefPtr<WebAnimation>> { };
}).iterator->value.add(&animation);
}
static inline bool removeCSSTransitionFromMap(CSSTransition& transition, Element& element, HashMap<Element*, HashMap<CSSPropertyID, RefPtr<CSSTransition>>>& map)
{
auto iterator = map.find(&element);
if (iterator == map.end())
return false;
auto& cssTransitionsByProperty = iterator->value;
cssTransitionsByProperty.remove(transition.property());
if (cssTransitionsByProperty.isEmpty())
map.remove(&element);
return true;
}
void AnimationTimeline::animationWasRemovedFromElement(WebAnimation& animation, Element& element)
{
m_animationsWithoutTarget.add(&animation);
auto& map = relevantMapForAnimation(animation);
auto iterator = map.find(&element);
if (iterator == map.end())
return;
auto& animations = iterator->value;
animations.remove(&animation);
if (!animations.size())
map.remove(iterator);
if (is<CSSAnimation>(animation)) {
auto iterator = m_elementToCSSAnimationByName.find(&element);
if (iterator != m_elementToCSSAnimationByName.end()) {
auto& cssAnimationsByName = iterator->value;
auto& name = downcast<CSSAnimation>(animation).animationName();
cssAnimationsByName.remove(name);
if (cssAnimationsByName.isEmpty())
m_elementToCSSAnimationByName.remove(&element);
}
} else if (is<CSSTransition>(animation)) {
auto& transition = downcast<CSSTransition>(animation);
if (!removeCSSTransitionFromMap(transition, element, m_elementToRunningCSSTransitionByCSSPropertyID))
removeCSSTransitionFromMap(transition, element, m_elementToCompletedCSSTransitionByCSSPropertyID);
}
}
Vector<RefPtr<WebAnimation>> AnimationTimeline::animationsForElement(Element& element, Ordering ordering) const
{
Vector<RefPtr<WebAnimation>> animations;
if (m_elementToCSSTransitionsMap.contains(&element)) {
const auto& cssTransitions = m_elementToCSSTransitionsMap.get(&element);
if (ordering == Ordering::Sorted) {
Vector<RefPtr<WebAnimation>> sortedCSSTransitions;
sortedCSSTransitions.appendRange(cssTransitions.begin(), cssTransitions.end());
std::sort(sortedCSSTransitions.begin(), sortedCSSTransitions.end(), [](auto& lhs, auto& rhs) {
auto* lhsTransition = downcast<CSSTransition>(lhs.get());
auto* rhsTransition = downcast<CSSTransition>(rhs.get());
if (lhsTransition->generationTime() != rhsTransition->generationTime())
return lhsTransition->generationTime() < rhsTransition->generationTime();
return lhsTransition->transitionProperty().utf8() < rhsTransition->transitionProperty().utf8();
});
animations.appendVector(sortedCSSTransitions);
} else
animations.appendRange(cssTransitions.begin(), cssTransitions.end());
}
if (m_elementToCSSAnimationsMap.contains(&element)) {
const auto& cssAnimations = m_elementToCSSAnimationsMap.get(&element);
animations.appendRange(cssAnimations.begin(), cssAnimations.end());
}
if (m_elementToAnimationsMap.contains(&element)) {
const auto& webAnimations = m_elementToAnimationsMap.get(&element);
animations.appendRange(webAnimations.begin(), webAnimations.end());
}
return animations;
}
void AnimationTimeline::removeAnimationsForElement(Element& element)
{
for (auto& animation : animationsForElement(element))
animation->remove();
}
void AnimationTimeline::cancelDeclarativeAnimationsForElement(Element& element)
{
for (auto& cssTransition : m_elementToCSSTransitionsMap.get(&element))
cssTransition->cancel();
for (auto& cssAnimation : m_elementToCSSAnimationsMap.get(&element))
cssAnimation->cancel();
}
void AnimationTimeline::updateCSSAnimationsForElement(Element& element, const RenderStyle* currentStyle, const RenderStyle& afterChangeStyle)
{
if (currentStyle && currentStyle->hasAnimations() && currentStyle->display() != DisplayType::None && afterChangeStyle.display() == DisplayType::None) {
if (m_elementToCSSAnimationByName.contains(&element)) {
for (const auto& cssAnimationsByNameMapItem : m_elementToCSSAnimationByName.take(&element))
cancelOrRemoveDeclarativeAnimation(cssAnimationsByNameMapItem.value);
}
return;
}
if (currentStyle && currentStyle->hasAnimations() && afterChangeStyle.hasAnimations() && *(currentStyle->animations()) == *(afterChangeStyle.animations()))
return;
static NeverDestroyed<const String> animationNameNone(MAKE_STATIC_STRING_IMPL("none"));
HashSet<String> namesOfPreviousAnimations;
if (currentStyle && currentStyle->hasAnimations()) {
auto* previousAnimations = currentStyle->animations();
for (size_t i = 0; i < previousAnimations->size(); ++i) {
auto& previousAnimation = previousAnimations->animation(i);
if (previousAnimation.isValidAnimation() && previousAnimation.name() != animationNameNone)
namesOfPreviousAnimations.add(previousAnimation.name());
}
}
auto& cssAnimationsByName = m_elementToCSSAnimationByName.ensure(&element, [] {
return HashMap<String, RefPtr<CSSAnimation>> { };
}).iterator->value;
if (auto* currentAnimations = afterChangeStyle.animations()) {
for (size_t i = 0; i < currentAnimations->size(); ++i) {
auto& currentAnimation = currentAnimations->animation(i);
auto& name = currentAnimation.name();
if (namesOfPreviousAnimations.contains(name)) {
cssAnimationsByName.get(name)->setBackingAnimation(currentAnimation);
} else if (currentAnimation.isValidAnimation() && name != animationNameNone) {
cssAnimationsByName.set(name, CSSAnimation::create(element, currentAnimation, currentStyle, afterChangeStyle));
}
namesOfPreviousAnimations.remove(name);
}
}
for (const auto& nameOfAnimationToRemove : namesOfPreviousAnimations)
cancelOrRemoveDeclarativeAnimation(cssAnimationsByName.take(nameOfAnimationToRemove));
if (cssAnimationsByName.isEmpty())
m_elementToCSSAnimationByName.remove(&element);
}
RefPtr<WebAnimation> AnimationTimeline::cssAnimationForElementAndProperty(Element& element, CSSPropertyID property)
{
RefPtr<WebAnimation> matchingAnimation;
for (const auto& animation : m_elementToCSSAnimationsMap.get(&element)) {
auto* effect = animation->effect();
if (is<KeyframeEffectReadOnly>(effect) && downcast<KeyframeEffectReadOnly>(effect)->animatedProperties().contains(property))
matchingAnimation = animation;
}
return matchingAnimation;
}
static bool propertyInStyleMatchesValueForTransitionInMap(CSSPropertyID property, const RenderStyle& style, HashMap<CSSPropertyID, RefPtr<CSSTransition>>& transitions)
{
if (auto* transition = transitions.get(property)) {
if (CSSPropertyAnimation::propertiesEqual(property, &style, &transition->targetStyle()))
return true;
}
return false;
}
static double transitionCombinedDuration(const Animation* transition)
{
return std::max(0.0, transition->duration()) + transition->delay();
}
static bool transitionMatchesProperty(const Animation& transition, CSSPropertyID property)
{
auto mode = transition.animationMode();
if (mode == Animation::AnimateNone || mode == Animation::AnimateUnknownProperty)
return false;
if (mode == Animation::AnimateSingleProperty) {
auto transitionProperty = transition.property();
if (transitionProperty != property) {
auto shorthand = shorthandForProperty(transitionProperty);
for (size_t i = 0; i < shorthand.length(); ++i) {
if (shorthand.properties()[i] == property)
return true;
}
return false;
}
}
return true;
}
void AnimationTimeline::updateCSSTransitionsForElement(Element& element, const RenderStyle& currentStyle, const RenderStyle& afterChangeStyle)
{
if (currentStyle.hasTransitions() && currentStyle.display() != DisplayType::None && afterChangeStyle.display() == DisplayType::None) {
if (m_elementToRunningCSSTransitionByCSSPropertyID.contains(&element)) {
for (const auto& cssTransitionsByCSSPropertyIDMapItem : m_elementToRunningCSSTransitionByCSSPropertyID.take(&element))
cancelOrRemoveDeclarativeAnimation(cssTransitionsByCSSPropertyIDMapItem.value);
}
return;
}
auto& runningTransitionsByProperty = m_elementToRunningCSSTransitionByCSSPropertyID.ensure(&element, [] {
return HashMap<CSSPropertyID, RefPtr<CSSTransition>> { };
}).iterator->value;
auto& completedTransitionsByProperty = m_elementToCompletedCSSTransitionByCSSPropertyID.ensure(&element, [] {
return HashMap<CSSPropertyID, RefPtr<CSSTransition>> { };
}).iterator->value;
auto generationTime = MonotonicTime::now();
auto numberOfProperties = CSSPropertyAnimation::getNumProperties();
for (int propertyIndex = 0; propertyIndex < numberOfProperties; ++propertyIndex) {
std::optional<bool> isShorthand;
auto property = CSSPropertyAnimation::getPropertyAtIndex(propertyIndex, isShorthand);
if (isShorthand && *isShorthand)
continue;
const Animation* matchingBackingAnimation = nullptr;
if (auto* transitions = afterChangeStyle.transitions()) {
for (size_t i = 0; i < transitions->size(); ++i) {
auto& backingAnimation = transitions->animation(i);
if (transitionMatchesProperty(backingAnimation, property))
matchingBackingAnimation = &backingAnimation;
}
}
auto existingAnimation = cssAnimationForElementAndProperty(element, property);
const auto& beforeChangeStyle = existingAnimation ? downcast<CSSAnimation>(existingAnimation.get())->unanimatedStyle() : currentStyle;
if (!runningTransitionsByProperty.contains(property)
&& !CSSPropertyAnimation::propertiesEqual(property, &beforeChangeStyle, &afterChangeStyle)
&& CSSPropertyAnimation::canPropertyBeInterpolated(property, &beforeChangeStyle, &afterChangeStyle)
&& !propertyInStyleMatchesValueForTransitionInMap(property, afterChangeStyle, completedTransitionsByProperty)
&& matchingBackingAnimation && transitionCombinedDuration(matchingBackingAnimation) > 0) {
completedTransitionsByProperty.remove(property);
auto delay = Seconds(matchingBackingAnimation->delay());
auto duration = Seconds(matchingBackingAnimation->duration());
auto& reversingAdjustedStartStyle = beforeChangeStyle;
auto reversingShorteningFactor = 1;
runningTransitionsByProperty.set(property, CSSTransition::create(element, property, generationTime, *matchingBackingAnimation, &beforeChangeStyle, afterChangeStyle, delay, duration, reversingAdjustedStartStyle, reversingShorteningFactor));
} else if (completedTransitionsByProperty.contains(property) && !propertyInStyleMatchesValueForTransitionInMap(property, afterChangeStyle, completedTransitionsByProperty)) {
completedTransitionsByProperty.remove(property);
}
bool hasRunningTransition = runningTransitionsByProperty.contains(property);
if ((hasRunningTransition || completedTransitionsByProperty.contains(property)) && !matchingBackingAnimation) {
if (hasRunningTransition)
runningTransitionsByProperty.take(property)->cancel();
else
completedTransitionsByProperty.remove(property);
}
if (matchingBackingAnimation && runningTransitionsByProperty.contains(property) && !propertyInStyleMatchesValueForTransitionInMap(property, afterChangeStyle, runningTransitionsByProperty)) {
auto previouslyRunningTransition = runningTransitionsByProperty.take(property);
auto& previouslyRunningTransitionCurrentStyle = previouslyRunningTransition->currentStyle();
if (CSSPropertyAnimation::propertiesEqual(property, &previouslyRunningTransitionCurrentStyle, &afterChangeStyle) || !CSSPropertyAnimation::canPropertyBeInterpolated(property, ¤tStyle, &afterChangeStyle)) {
previouslyRunningTransition->cancel();
} else if (transitionCombinedDuration(matchingBackingAnimation) <= 0.0 || !CSSPropertyAnimation::canPropertyBeInterpolated(property, &previouslyRunningTransitionCurrentStyle, &afterChangeStyle)) {
previouslyRunningTransition->cancel();
} else if (CSSPropertyAnimation::propertiesEqual(property, &previouslyRunningTransition->reversingAdjustedStartStyle(), &afterChangeStyle)) {
previouslyRunningTransition->cancel();
auto& reversingAdjustedStartStyle = previouslyRunningTransition->targetStyle();
double transformedProgress = 1;
if (auto* effect = previouslyRunningTransition->effect())
transformedProgress = effect->iterationProgress().value_or(transformedProgress);
auto reversingShorteningFactor = std::max(std::min(((transformedProgress * previouslyRunningTransition->reversingShorteningFactor()) + (1 - previouslyRunningTransition->reversingShorteningFactor())), 1.0), 0.0);
auto delay = matchingBackingAnimation->delay() < 0 ? Seconds(matchingBackingAnimation->delay()) * reversingShorteningFactor : Seconds(matchingBackingAnimation->delay());
auto duration = Seconds(matchingBackingAnimation->duration()) * reversingShorteningFactor;
runningTransitionsByProperty.set(property, CSSTransition::create(element, property, generationTime, *matchingBackingAnimation, &previouslyRunningTransitionCurrentStyle, afterChangeStyle, delay, duration, reversingAdjustedStartStyle, reversingShorteningFactor));
} else {
previouslyRunningTransition->cancel();
auto delay = Seconds(matchingBackingAnimation->delay());
auto duration = Seconds(matchingBackingAnimation->duration());
auto& reversingAdjustedStartStyle = currentStyle;
auto reversingShorteningFactor = 1;
runningTransitionsByProperty.set(property, CSSTransition::create(element, property, generationTime, *matchingBackingAnimation, &previouslyRunningTransitionCurrentStyle, afterChangeStyle, delay, duration, reversingAdjustedStartStyle, reversingShorteningFactor));
}
}
}
}
void AnimationTimeline::cancelOrRemoveDeclarativeAnimation(RefPtr<DeclarativeAnimation> animation)
{
auto phase = animation->effect()->phase();
if (phase != AnimationEffectReadOnly::Phase::Idle && phase != AnimationEffectReadOnly::Phase::After)
animation->cancel();
else
removeAnimation(animation.releaseNonNull());
}
String AnimationTimeline::description()
{
TextStream stream;
int count = 1;
stream << (m_classType == DocumentTimelineClass ? "DocumentTimeline" : "AnimationTimeline") << " with " << m_animations.size() << " animations:";
stream << "\n";
for (const auto& animation : m_animations) {
writeIndent(stream, 1);
stream << count << ". " << animation->description();
stream << "\n";
count++;
}
return stream.release();
}
}