ImplicitAnimation.cpp [plain text]
#include "config.h"
#include "ImplicitAnimation.h"
#include "CSSAnimationControllerPrivate.h"
#include "CSSPropertyAnimation.h"
#include "CompositeAnimation.h"
#include "EventNames.h"
#include "GeometryUtilities.h"
#include "KeyframeAnimation.h"
#include "RenderBox.h"
#include "StylePendingResources.h"
namespace WebCore {
ImplicitAnimation::ImplicitAnimation(const Animation& transition, CSSPropertyID animatingProperty, Element& element, CompositeAnimation& compositeAnimation, const RenderStyle& fromStyle)
: AnimationBase(transition, element, compositeAnimation)
, m_fromStyle(RenderStyle::clonePtr(fromStyle))
, m_transitionProperty(transition.property())
, m_animatingProperty(animatingProperty)
{
ASSERT(animatingProperty != CSSPropertyInvalid);
}
ImplicitAnimation::~ImplicitAnimation()
{
if (!postActive())
endAnimation();
}
bool ImplicitAnimation::shouldSendEventForListener(Document::ListenerType inListenerType) const
{
return element()->document().hasListenerType(inListenerType);
}
bool ImplicitAnimation::animate(CompositeAnimation& compositeAnimation, const RenderStyle& targetStyle, std::unique_ptr<RenderStyle>& animatedStyle, bool& didBlendStyle)
{
if (postActive())
return false;
AnimationState oldState = state();
if (isNew())
reset(targetStyle, compositeAnimation);
if (!animatedStyle)
animatedStyle = RenderStyle::clonePtr(targetStyle);
CSSPropertyAnimation::blendProperties(this, m_animatingProperty, animatedStyle.get(), m_fromStyle.get(), m_toStyle.get(), progress());
fireAnimationEventsIfNeeded();
didBlendStyle = true;
return state() != oldState;
}
void ImplicitAnimation::getAnimatedStyle(std::unique_ptr<RenderStyle>& animatedStyle)
{
if (!animatedStyle)
animatedStyle = RenderStyle::clonePtr(*m_toStyle);
CSSPropertyAnimation::blendProperties(this, m_animatingProperty, animatedStyle.get(), m_fromStyle.get(), m_toStyle.get(), progress());
}
bool ImplicitAnimation::computeExtentOfTransformAnimation(LayoutRect& bounds) const
{
ASSERT(hasStyle());
if (!is<RenderBox>(renderer()))
return true;
ASSERT(m_animatingProperty == CSSPropertyTransform);
RenderBox& box = downcast<RenderBox>(*renderer());
FloatRect rendererBox = snapRectToDevicePixels(box.borderBoxRect(), box.document().deviceScaleFactor());
LayoutRect startBounds = bounds;
LayoutRect endBounds = bounds;
if (transformFunctionListsMatch()) {
if (!computeTransformedExtentViaTransformList(rendererBox, *m_fromStyle, startBounds))
return false;
if (!computeTransformedExtentViaTransformList(rendererBox, *m_toStyle, endBounds))
return false;
} else {
if (!computeTransformedExtentViaMatrix(rendererBox, *m_fromStyle, startBounds))
return false;
if (!computeTransformedExtentViaMatrix(rendererBox, *m_toStyle, endBounds))
return false;
}
bounds = unionRect(startBounds, endBounds);
return true;
}
bool ImplicitAnimation::startAnimation(double timeOffset)
{
if (auto* renderer = compositedRenderer())
return renderer->startTransition(timeOffset, m_animatingProperty, m_fromStyle.get(), m_toStyle.get());
return false;
}
void ImplicitAnimation::pauseAnimation(double timeOffset)
{
if (auto* renderer = compositedRenderer())
renderer->transitionPaused(timeOffset, m_animatingProperty);
if (!paused())
setNeedsStyleRecalc(element());
}
void ImplicitAnimation::endAnimation()
{
if (auto* renderer = compositedRenderer())
renderer->transitionFinished(m_animatingProperty);
}
void ImplicitAnimation::onAnimationEnd(double elapsedTime)
{
if (auto* animation = m_compositeAnimation->animationForProperty(m_animatingProperty))
animation->setUnanimatedStyle(RenderStyle::clonePtr(*m_toStyle));
sendTransitionEvent(eventNames().transitionendEvent, elapsedTime);
endAnimation();
}
bool ImplicitAnimation::sendTransitionEvent(const AtomicString& eventType, double elapsedTime)
{
if (eventType == eventNames().transitionendEvent) {
Document::ListenerType listenerType = Document::TRANSITIONEND_LISTENER;
if (shouldSendEventForListener(listenerType)) {
String propertyName = getPropertyNameString(m_animatingProperty);
auto element = makeRefPtr(this->element());
ASSERT(!element || element->document().pageCacheState() == Document::NotInPageCache);
if (!element)
return false;
m_compositeAnimation->animationController().addEventToDispatch(*element, eventType, propertyName, elapsedTime);
if (eventType == eventNames().transitionendEvent && element->renderer())
setNeedsStyleRecalc(element.get());
return true; }
}
return false; }
void ImplicitAnimation::reset(const RenderStyle& to, CompositeAnimation& compositeAnimation)
{
ASSERT(m_fromStyle);
m_toStyle = RenderStyle::clonePtr(to);
if (element())
Style::loadPendingResources(*m_toStyle, element()->document(), element());
if (m_fromStyle && m_toStyle && !compositeAnimation.isSuspended())
updateStateMachine(AnimationStateInput::RestartAnimation, -1);
validateTransformFunctionList();
checkForMatchingFilterFunctionLists();
#if ENABLE(FILTERS_LEVEL_2)
checkForMatchingBackdropFilterFunctionLists();
#endif
}
void ImplicitAnimation::setOverridden(bool b)
{
if (b == m_overridden)
return;
m_overridden = b;
updateStateMachine(m_overridden ? AnimationStateInput::PauseOverride : AnimationStateInput::ResumeOverride, -1);
}
bool ImplicitAnimation::affectsProperty(CSSPropertyID property) const
{
return (m_animatingProperty == property);
}
bool ImplicitAnimation::isTargetPropertyEqual(CSSPropertyID prop, const RenderStyle* targetStyle)
{
if (!m_toStyle)
return false;
return CSSPropertyAnimation::propertiesEqual(prop, m_toStyle.get(), targetStyle);
}
void ImplicitAnimation::blendPropertyValueInStyle(CSSPropertyID prop, RenderStyle* currentStyle)
{
if (!m_toStyle)
return;
CSSPropertyAnimation::blendProperties(this, prop, currentStyle, m_fromStyle.get(), m_toStyle.get(), progress());
}
void ImplicitAnimation::validateTransformFunctionList()
{
m_transformFunctionListsMatch = false;
if (!m_fromStyle || !m_toStyle)
return;
const TransformOperations* val = &m_fromStyle->transform();
const TransformOperations* toVal = &m_toStyle->transform();
if (val->operations().isEmpty())
val = toVal;
if (val->operations().isEmpty())
return;
if (val != toVal && !toVal->operations().isEmpty() && !val->operationsMatch(*toVal))
return;
m_transformFunctionListsMatch = true;
}
static bool filterOperationsMatch(const FilterOperations* fromOperations, const FilterOperations& toOperations)
{
if (fromOperations->operations().isEmpty())
fromOperations = &toOperations;
if (fromOperations->operations().isEmpty())
return false;
if (fromOperations != &toOperations && !toOperations.operations().isEmpty() && !fromOperations->operationsMatch(toOperations))
return false;
return true;
}
void ImplicitAnimation::checkForMatchingFilterFunctionLists()
{
m_filterFunctionListsMatch = false;
if (!m_fromStyle || !m_toStyle)
return;
m_filterFunctionListsMatch = filterOperationsMatch(&m_fromStyle->filter(), m_toStyle->filter());
}
#if ENABLE(FILTERS_LEVEL_2)
void ImplicitAnimation::checkForMatchingBackdropFilterFunctionLists()
{
m_backdropFilterFunctionListsMatch = false;
if (!m_fromStyle || !m_toStyle)
return;
m_backdropFilterFunctionListsMatch = filterOperationsMatch(&m_fromStyle->backdropFilter(), m_toStyle->backdropFilter());
}
#endif
std::optional<Seconds> ImplicitAnimation::timeToNextService()
{
std::optional<Seconds> t = AnimationBase::timeToNextService();
if (!t || t.value() != 0_s || preActive())
return t;
if (CSSPropertyAnimation::animationOfPropertyIsAccelerated(m_animatingProperty) && isAccelerated()) {
bool isLooping;
getTimeToNextEvent(t.value(), isLooping);
}
return t;
}
}