DocumentTimeline.cpp [plain text]
#include "config.h"
#include "DocumentTimeline.h"
#include "AnimationPlaybackEvent.h"
#include "CSSAnimation.h"
#include "CSSPropertyAnimation.h"
#include "CSSTransition.h"
#include "DOMWindow.h"
#include "DeclarativeAnimation.h"
#include "Document.h"
#include "DocumentAnimationScheduler.h"
#include "GraphicsLayer.h"
#include "KeyframeEffect.h"
#include "Microtasks.h"
#include "Node.h"
#include "Page.h"
#include "PseudoElement.h"
#include "RenderElement.h"
#include "RenderLayer.h"
#include "RenderLayerBacking.h"
static const Seconds defaultAnimationInterval { 15_ms };
static const Seconds throttledAnimationInterval { 30_ms };
namespace WebCore {
Ref<DocumentTimeline> DocumentTimeline::create(Document& document)
{
return adoptRef(*new DocumentTimeline(document, 0_s));
}
Ref<DocumentTimeline> DocumentTimeline::create(Document& document, DocumentTimelineOptions&& options)
{
return adoptRef(*new DocumentTimeline(document, Seconds::fromMilliseconds(options.originTime)));
}
DocumentTimeline::DocumentTimeline(Document& document, Seconds originTime)
: AnimationTimeline(DocumentTimelineClass)
, m_document(&document)
, m_originTime(originTime)
, m_tickScheduleTimer(*this, &DocumentTimeline::scheduleAnimationResolutionIfNeeded)
#if !USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
, m_animationResolutionTimer(*this, &DocumentTimeline::animationResolutionTimerFired)
#endif
{
if (m_document && m_document->page() && !m_document->page()->isVisible())
suspendAnimations();
}
DocumentTimeline::~DocumentTimeline() = default;
void DocumentTimeline::detachFromDocument()
{
m_currentTimeClearingTaskQueue.close();
m_elementsWithRunningAcceleratedAnimations.clear();
auto& animationsToRemove = m_animations;
while (!animationsToRemove.isEmpty())
animationsToRemove.first()->remove();
unscheduleAnimationResolution();
m_document = nullptr;
}
static inline bool compareDeclarativeAnimationOwningElementPositionsInDocumentTreeOrder(Element* lhsOwningElement, Element* rhsOwningElement)
{
if (is<PseudoElement>(lhsOwningElement) && is<PseudoElement>(rhsOwningElement)) {
auto* lhsPseudoElement = downcast<PseudoElement>(lhsOwningElement);
auto* rhsPseudoElement = downcast<PseudoElement>(rhsOwningElement);
if (lhsPseudoElement->hostElement() == rhsPseudoElement->hostElement())
return lhsPseudoElement->isBeforePseudoElement();
}
if (is<PseudoElement>(lhsOwningElement)) {
auto* lhsHostElement = downcast<PseudoElement>(lhsOwningElement)->hostElement();
if (rhsOwningElement == lhsHostElement)
return false;
lhsOwningElement = lhsHostElement;
}
if (is<PseudoElement>(rhsOwningElement)) {
auto* rhsHostElement = downcast<PseudoElement>(rhsOwningElement)->hostElement();
if (lhsOwningElement == rhsHostElement)
return true;
rhsOwningElement = rhsHostElement;
}
return lhsOwningElement->compareDocumentPosition(*rhsOwningElement) & Node::DOCUMENT_POSITION_FOLLOWING;
}
Vector<RefPtr<WebAnimation>> DocumentTimeline::getAnimations() const
{
ASSERT(m_document);
Vector<RefPtr<WebAnimation>> cssTransitions;
Vector<RefPtr<WebAnimation>> cssAnimations;
Vector<RefPtr<WebAnimation>> webAnimations;
for (const auto& animation : m_allAnimations) {
if (!animation->isRelevant() || animation->timeline() != this || !is<KeyframeEffect>(animation->effect()))
continue;
auto* target = downcast<KeyframeEffect>(animation->effect())->target();
if (!target || !target->isDescendantOf(*m_document))
continue;
if (is<CSSTransition>(animation) && downcast<CSSTransition>(animation)->owningElement())
cssTransitions.append(animation);
else if (is<CSSAnimation>(animation) && downcast<CSSAnimation>(animation)->owningElement())
cssAnimations.append(animation);
else
webAnimations.append(animation);
}
std::sort(cssTransitions.begin(), cssTransitions.end(), [](auto& lhs, auto& rhs) {
auto* lhsTransition = downcast<CSSTransition>(lhs.get());
auto* rhsTransition = downcast<CSSTransition>(rhs.get());
auto* lhsOwningElement = lhsTransition->owningElement();
auto* rhsOwningElement = rhsTransition->owningElement();
if (lhsOwningElement != rhsOwningElement)
return compareDeclarativeAnimationOwningElementPositionsInDocumentTreeOrder(lhsOwningElement, rhsOwningElement);
if (lhsTransition->generationTime() != rhsTransition->generationTime())
return lhsTransition->generationTime() < rhsTransition->generationTime();
return lhsTransition->transitionProperty().utf8() < rhsTransition->transitionProperty().utf8();
});
std::sort(cssAnimations.begin(), cssAnimations.end(), [](auto& lhs, auto& rhs) {
auto* lhsOwningElement = downcast<CSSAnimation>(lhs.get())->owningElement();
auto* rhsOwningElement = downcast<CSSAnimation>(rhs.get())->owningElement();
if (lhsOwningElement != rhsOwningElement)
return compareDeclarativeAnimationOwningElementPositionsInDocumentTreeOrder(lhsOwningElement, rhsOwningElement);
return false;
});
Vector<RefPtr<WebAnimation>> animations;
animations.appendRange(cssTransitions.begin(), cssTransitions.end());
animations.appendRange(cssAnimations.begin(), cssAnimations.end());
animations.appendRange(webAnimations.begin(), webAnimations.end());
return animations;
}
void DocumentTimeline::updateThrottlingState()
{
scheduleAnimationResolutionIfNeeded();
}
Seconds DocumentTimeline::animationInterval() const
{
if (!m_document || !m_document->page())
return Seconds::infinity();
return m_document->page()->isLowPowerModeEnabled() ? throttledAnimationInterval : defaultAnimationInterval;
}
void DocumentTimeline::suspendAnimations()
{
if (animationsAreSuspended())
return;
if (!m_cachedCurrentTime)
m_cachedCurrentTime = liveCurrentTime();
for (const auto& animation : m_animations)
animation->setSuspended(true);
m_isSuspended = true;
applyPendingAcceleratedAnimations();
unscheduleAnimationResolution();
}
void DocumentTimeline::resumeAnimations()
{
if (!animationsAreSuspended())
return;
m_cachedCurrentTime = WTF::nullopt;
m_isSuspended = false;
for (const auto& animation : m_animations)
animation->setSuspended(false);
scheduleAnimationResolutionIfNeeded();
}
bool DocumentTimeline::animationsAreSuspended()
{
return m_isSuspended;
}
unsigned DocumentTimeline::numberOfActiveAnimationsForTesting() const
{
unsigned count = 0;
for (const auto& animation : m_animations) {
if (!animation->isSuspended())
++count;
}
return count;
}
Seconds DocumentTimeline::liveCurrentTime() const
{
#if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
return m_document->animationScheduler().lastTimestamp();
#else
return Seconds(m_document->domWindow()->nowTimestamp());
#endif
}
Optional<Seconds> DocumentTimeline::currentTime()
{
if (!m_document || !m_document->domWindow())
return AnimationTimeline::currentTime();
if (auto* mainDocumentTimeline = m_document->existingTimeline()) {
if (mainDocumentTimeline != this) {
if (auto mainDocumentTimelineCurrentTime = mainDocumentTimeline->currentTime())
return mainDocumentTimelineCurrentTime.value() - m_originTime;
return WTF::nullopt;
}
}
auto currentTime = liveCurrentTime();
#if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
if (m_document->animationScheduler().isFiring())
cacheCurrentTime(currentTime);
#endif
if (!m_cachedCurrentTime) {
#if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
auto lastAnimationSchedulerTimestamp = currentTime;
auto delta = Seconds(m_document->domWindow()->nowTimestamp()) - lastAnimationSchedulerTimestamp;
int frames = std::floor(delta.seconds() / animationInterval().seconds());
cacheCurrentTime(lastAnimationSchedulerTimestamp + Seconds(frames * animationInterval().seconds()));
#else
cacheCurrentTime(currentTime);
#endif
}
return m_cachedCurrentTime.value() - m_originTime;
}
void DocumentTimeline::cacheCurrentTime(Seconds newCurrentTime)
{
m_cachedCurrentTime = newCurrentTime;
m_waitingOnVMIdle = true;
if (!m_currentTimeClearingTaskQueue.hasPendingTasks())
m_currentTimeClearingTaskQueue.enqueueTask(std::bind(&DocumentTimeline::maybeClearCachedCurrentTime, this));
m_document->vm().whenIdle([this, protectedThis = makeRefPtr(this)]() {
m_waitingOnVMIdle = false;
maybeClearCachedCurrentTime();
});
}
void DocumentTimeline::maybeClearCachedCurrentTime()
{
if (!m_isSuspended && !m_waitingOnVMIdle && !m_currentTimeClearingTaskQueue.hasPendingTasks())
m_cachedCurrentTime = WTF::nullopt;
}
void DocumentTimeline::scheduleAnimationResolutionIfNeeded()
{
if (!m_isUpdatingAnimations && !m_isSuspended && !m_animations.isEmpty())
scheduleAnimationResolution();
}
void DocumentTimeline::animationTimingDidChange(WebAnimation& animation)
{
AnimationTimeline::animationTimingDidChange(animation);
scheduleAnimationResolutionIfNeeded();
}
void DocumentTimeline::removeAnimation(WebAnimation& animation)
{
AnimationTimeline::removeAnimation(animation);
if (m_animations.isEmpty())
unscheduleAnimationResolution();
}
void DocumentTimeline::scheduleAnimationResolution()
{
#if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
m_document->animationScheduler().scheduleWebAnimationsResolution();
#else
m_animationResolutionTimer.startOneShot(animationInterval());
#endif
}
void DocumentTimeline::unscheduleAnimationResolution()
{
m_tickScheduleTimer.stop();
#if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
m_document->animationScheduler().unscheduleWebAnimationsResolution();
#else
m_animationResolutionTimer.stop();
#endif
}
#if USE(REQUEST_ANIMATION_FRAME_DISPLAY_MONITOR)
void DocumentTimeline::documentAnimationSchedulerDidFire()
#else
void DocumentTimeline::animationResolutionTimerFired()
#endif
{
updateAnimationsAndSendEvents();
applyPendingAcceleratedAnimations();
scheduleNextTick();
}
void DocumentTimeline::updateAnimationsAndSendEvents()
{
m_numberOfAnimationTimelineInvalidationsForTesting++;
m_isUpdatingAnimations = true;
Vector<RefPtr<WebAnimation>> animationsToRemove;
Vector<RefPtr<CSSTransition>> completedTransitions;
for (auto& animation : m_animations) {
if (animation->timeline() != this) {
ASSERT(!animation->timeline());
animationsToRemove.append(animation);
continue;
}
animation->tick();
if (!animation->isRelevant() && !animation->needsTick())
animationsToRemove.append(animation);
if (!animation->needsTick() && is<CSSTransition>(animation) && animation->playState() == WebAnimation::PlayState::Finished) {
auto* transition = downcast<CSSTransition>(animation.get());
if (transition->owningElement())
completedTransitions.append(transition);
}
}
MicrotaskQueue::mainThreadQueue().performMicrotaskCheckpoint();
auto pendingAnimationEvents = WTFMove(m_pendingAnimationEvents);
std::stable_sort(pendingAnimationEvents.begin(), pendingAnimationEvents.end(), [] (const Ref<AnimationPlaybackEvent>& lhs, const Ref<AnimationPlaybackEvent>& rhs) {
if (lhs->timelineTime() && !rhs->timelineTime())
return false;
if (!lhs->timelineTime() && rhs->timelineTime())
return true;
if (!lhs->timelineTime() && !rhs->timelineTime())
return true;
return lhs->timelineTime().value() < rhs->timelineTime().value();
});
for (auto& pendingEvent : pendingAnimationEvents)
pendingEvent->target()->dispatchEvent(pendingEvent);
for (auto& animation : animationsToRemove)
removeAnimation(*animation);
for (auto& completedTransition : completedTransitions)
transitionDidComplete(completedTransition);
m_isUpdatingAnimations = false;
}
void DocumentTimeline::transitionDidComplete(RefPtr<CSSTransition> transition)
{
ASSERT(transition);
removeAnimation(*transition);
if (is<KeyframeEffect>(transition->effect())) {
if (auto* target = downcast<KeyframeEffect>(transition->effect())->target()) {
m_elementToCompletedCSSTransitionByCSSPropertyID.ensure(target, [] {
return HashMap<CSSPropertyID, RefPtr<CSSTransition>> { };
}).iterator->value.set(transition->property(), transition);
}
}
}
void DocumentTimeline::scheduleNextTick()
{
if (m_animations.isEmpty())
return;
for (const auto& animation : m_animations) {
if (!animation->isRunningAccelerated()) {
scheduleAnimationResolutionIfNeeded();
return;
}
}
Seconds scheduleDelay = Seconds::infinity();
for (const auto& animation : m_animations) {
auto animationTimeToNextRequiredTick = animation->timeToNextTick();
if (animationTimeToNextRequiredTick < animationInterval()) {
scheduleAnimationResolutionIfNeeded();
return;
}
scheduleDelay = std::min(scheduleDelay, animationTimeToNextRequiredTick);
}
if (scheduleDelay < Seconds::infinity())
m_tickScheduleTimer.startOneShot(scheduleDelay);
}
bool DocumentTimeline::computeExtentOfAnimation(RenderElement& renderer, LayoutRect& bounds) const
{
if (!renderer.element())
return true;
KeyframeEffect* matchingEffect = nullptr;
for (const auto& animation : animationsForElement(*renderer.element())) {
auto* effect = animation->effect();
if (is<KeyframeEffect>(effect)) {
auto* keyframeEffect = downcast<KeyframeEffect>(effect);
if (keyframeEffect->animatedProperties().contains(CSSPropertyTransform))
matchingEffect = downcast<KeyframeEffect>(effect);
}
}
if (matchingEffect)
return matchingEffect->computeExtentOfTransformAnimation(bounds);
return true;
}
bool DocumentTimeline::isRunningAnimationOnRenderer(RenderElement& renderer, CSSPropertyID property) const
{
if (!renderer.element())
return false;
for (const auto& animation : animationsForElement(*renderer.element())) {
auto playState = animation->playState();
if (playState != WebAnimation::PlayState::Running && playState != WebAnimation::PlayState::Paused)
continue;
auto* effect = animation->effect();
if (is<KeyframeEffect>(effect) && downcast<KeyframeEffect>(effect)->animatedProperties().contains(property))
return true;
}
return false;
}
bool DocumentTimeline::isRunningAcceleratedAnimationOnRenderer(RenderElement& renderer, CSSPropertyID property) const
{
if (!renderer.element())
return false;
for (const auto& animation : animationsForElement(*renderer.element())) {
auto playState = animation->playState();
if (playState != WebAnimation::PlayState::Running && playState != WebAnimation::PlayState::Paused)
continue;
auto* effect = animation->effect();
if (is<KeyframeEffect>(effect)) {
auto* keyframeEffect = downcast<KeyframeEffect>(effect);
if (keyframeEffect->isRunningAccelerated() && keyframeEffect->animatedProperties().contains(property))
return true;
}
}
return false;
}
std::unique_ptr<RenderStyle> DocumentTimeline::animatedStyleForRenderer(RenderElement& renderer)
{
std::unique_ptr<RenderStyle> result;
if (auto* element = renderer.element()) {
for (const auto& animation : animationsForElement(*element)) {
if (is<KeyframeEffect>(animation->effect()))
downcast<KeyframeEffect>(animation->effect())->getAnimatedStyle(result);
}
}
if (!result)
result = RenderStyle::clonePtr(renderer.style());
return result;
}
void DocumentTimeline::animationWasAddedToElement(WebAnimation& animation, Element& element)
{
AnimationTimeline::animationWasAddedToElement(animation, element);
updateListOfElementsWithRunningAcceleratedAnimationsForElement(element);
}
void DocumentTimeline::animationWasRemovedFromElement(WebAnimation& animation, Element& element)
{
AnimationTimeline::animationWasRemovedFromElement(animation, element);
updateListOfElementsWithRunningAcceleratedAnimationsForElement(element);
}
void DocumentTimeline::animationAcceleratedRunningStateDidChange(WebAnimation& animation)
{
m_acceleratedAnimationsPendingRunningStateChange.add(&animation);
if (is<KeyframeEffect>(animation.effect())) {
if (auto* target = downcast<KeyframeEffect>(animation.effect())->target())
updateListOfElementsWithRunningAcceleratedAnimationsForElement(*target);
}
}
void DocumentTimeline::updateListOfElementsWithRunningAcceleratedAnimationsForElement(Element& element)
{
auto animations = animationsForElement(element);
bool runningAnimationsForElementAreAllAccelerated = !animations.isEmpty();
for (const auto& animation : animations) {
if (!animation->isRunningAccelerated()) {
runningAnimationsForElementAreAllAccelerated = false;
break;
}
}
if (runningAnimationsForElementAreAllAccelerated)
m_elementsWithRunningAcceleratedAnimations.add(&element);
else
m_elementsWithRunningAcceleratedAnimations.remove(&element);
}
void DocumentTimeline::applyPendingAcceleratedAnimations()
{
auto acceleratedAnimationsPendingRunningStateChange = m_acceleratedAnimationsPendingRunningStateChange;
m_acceleratedAnimationsPendingRunningStateChange.clear();
bool hasForcedLayout = false;
for (auto& animation : acceleratedAnimationsPendingRunningStateChange) {
if (!hasForcedLayout) {
auto* effect = animation->effect();
if (is<KeyframeEffect>(effect))
hasForcedLayout |= downcast<KeyframeEffect>(effect)->forceLayoutIfNeeded();
}
animation->applyPendingAcceleratedActions();
}
}
bool DocumentTimeline::resolveAnimationsForElement(Element& element, RenderStyle& targetStyle)
{
bool hasNonAcceleratedAnimations = false;
bool hasPendingAcceleratedAnimations = true;
for (const auto& animation : animationsForElement(element)) {
animation->resolve(targetStyle);
if (!hasNonAcceleratedAnimations) {
if (auto* effect = animation->effect()) {
if (is<KeyframeEffect>(effect)) {
auto* keyframeEffect = downcast<KeyframeEffect>(effect);
for (auto cssPropertyId : keyframeEffect->animatedProperties()) {
if (!CSSPropertyAnimation::animationOfPropertyIsAccelerated(cssPropertyId)) {
hasNonAcceleratedAnimations = true;
continue;
}
if (!hasPendingAcceleratedAnimations)
hasPendingAcceleratedAnimations = keyframeEffect->hasPendingAcceleratedAction();
}
}
}
}
}
return !hasNonAcceleratedAnimations && hasPendingAcceleratedAnimations;
}
bool DocumentTimeline::runningAnimationsForElementAreAllAccelerated(Element& element) const
{
return m_elementsWithRunningAcceleratedAnimations.contains(&element);
}
void DocumentTimeline::enqueueAnimationPlaybackEvent(AnimationPlaybackEvent& event)
{
m_pendingAnimationEvents.append(event);
}
Vector<std::pair<String, double>> DocumentTimeline::acceleratedAnimationsForElement(Element& element) const
{
auto* renderer = element.renderer();
if (renderer && renderer->isComposited()) {
auto* compositedRenderer = downcast<RenderBoxModelObject>(renderer);
if (auto* graphicsLayer = compositedRenderer->layer()->backing()->graphicsLayer())
return graphicsLayer->acceleratedAnimationsForTesting();
}
return { };
}
unsigned DocumentTimeline::numberOfAnimationTimelineInvalidationsForTesting() const
{
return m_numberOfAnimationTimelineInvalidationsForTesting;
}
}