2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-12-18 02:42:41 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2015-04-21 04:22:09 +03:00
|
|
|
#include "PendingAnimationTracker.h"
|
2014-12-18 02:42:41 +03:00
|
|
|
|
2019-03-29 18:12:47 +03:00
|
|
|
#include "mozilla/PresShell.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "mozilla/dom/AnimationEffect.h"
|
2015-04-28 05:29:13 +03:00
|
|
|
#include "mozilla/dom/AnimationTimeline.h"
|
2018-04-05 20:42:42 +03:00
|
|
|
#include "mozilla/dom/Nullable.h"
|
2014-12-22 03:35:41 +03:00
|
|
|
#include "nsIFrame.h"
|
2018-11-01 03:15:45 +03:00
|
|
|
#include "nsTransitionManager.h" // For CSSTransition
|
2014-12-22 03:35:41 +03:00
|
|
|
|
2018-04-05 20:42:42 +03:00
|
|
|
using mozilla::dom::Nullable;
|
2014-12-18 02:42:41 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2015-04-21 04:22:09 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION(PendingAnimationTracker, mPlayPendingSet,
|
2015-03-27 09:56:45 +03:00
|
|
|
mPausePendingSet, mDocument)
|
2014-12-18 02:42:41 +03:00
|
|
|
|
2015-04-21 04:22:09 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(PendingAnimationTracker, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(PendingAnimationTracker, Release)
|
2014-12-18 02:42:41 +03:00
|
|
|
|
2020-11-23 19:07:43 +03:00
|
|
|
PendingAnimationTracker::PendingAnimationTracker(dom::Document* aDocument)
|
|
|
|
: mDocument(aDocument) {}
|
|
|
|
|
2015-04-21 04:22:09 +03:00
|
|
|
void PendingAnimationTracker::AddPending(dom::Animation& aAnimation,
|
|
|
|
AnimationSet& aSet) {
|
|
|
|
aSet.PutEntry(&aAnimation);
|
2014-12-22 03:35:41 +03:00
|
|
|
|
|
|
|
// Schedule a paint. Otherwise animations that don't trigger a paint by
|
|
|
|
// themselves (e.g. CSS animations with an empty keyframes rule) won't
|
|
|
|
// start until something else paints.
|
|
|
|
EnsurePaintIsScheduled();
|
2014-12-18 02:42:41 +03:00
|
|
|
}
|
|
|
|
|
2015-04-21 04:22:09 +03:00
|
|
|
void PendingAnimationTracker::RemovePending(dom::Animation& aAnimation,
|
|
|
|
AnimationSet& aSet) {
|
|
|
|
aSet.RemoveEntry(&aAnimation);
|
2014-12-18 02:42:41 +03:00
|
|
|
}
|
|
|
|
|
2015-04-21 04:22:09 +03:00
|
|
|
bool PendingAnimationTracker::IsWaiting(const dom::Animation& aAnimation,
|
|
|
|
const AnimationSet& aSet) const {
|
|
|
|
return aSet.Contains(const_cast<dom::Animation*>(&aAnimation));
|
2014-12-18 02:42:41 +03:00
|
|
|
}
|
|
|
|
|
2015-04-21 04:22:09 +03:00
|
|
|
void PendingAnimationTracker::TriggerPendingAnimationsOnNextTick(
|
|
|
|
const TimeStamp& aReadyTime) {
|
2015-07-21 04:47:13 +03:00
|
|
|
auto triggerAnimationsAtReadyTime = [aReadyTime](
|
|
|
|
AnimationSet& aAnimationSet) {
|
|
|
|
for (auto iter = aAnimationSet.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
dom::Animation* animation = iter.Get()->GetKey();
|
|
|
|
dom::AnimationTimeline* timeline = animation->GetTimeline();
|
|
|
|
|
|
|
|
// If the animation does not have a timeline, just drop it from the map.
|
|
|
|
// The animation will detect that it is not being tracked and will trigger
|
|
|
|
// itself on the next tick where it has a timeline.
|
|
|
|
if (!timeline) {
|
|
|
|
iter.Remove();
|
2016-01-15 03:27:00 +03:00
|
|
|
continue;
|
2015-07-21 04:47:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// When the timeline's refresh driver is under test control, its values
|
|
|
|
// have no correspondance to wallclock times so we shouldn't try to
|
|
|
|
// convert aReadyTime (which is a wallclock time) to a timeline value.
|
|
|
|
// Instead, the animation will be started/paused when the refresh driver
|
|
|
|
// is next advanced since this will trigger a call to
|
|
|
|
// TriggerPendingAnimationsNow.
|
|
|
|
if (!timeline->TracksWallclockTime()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Nullable<TimeDuration> readyTime = timeline->ToTimelineTime(aReadyTime);
|
|
|
|
animation->TriggerOnNextTick(readyTime);
|
|
|
|
|
|
|
|
iter.Remove();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
triggerAnimationsAtReadyTime(mPlayPendingSet);
|
|
|
|
triggerAnimationsAtReadyTime(mPausePendingSet);
|
2016-12-02 04:10:44 +03:00
|
|
|
|
|
|
|
mHasPlayPendingGeometricAnimations =
|
|
|
|
mPlayPendingSet.Count() ? CheckState::Indeterminate : CheckState::Absent;
|
2015-01-09 01:57:58 +03:00
|
|
|
}
|
|
|
|
|
2015-04-21 04:22:09 +03:00
|
|
|
void PendingAnimationTracker::TriggerPendingAnimationsNow() {
|
2015-07-21 04:47:13 +03:00
|
|
|
auto triggerAndClearAnimations = [](AnimationSet& aAnimationSet) {
|
|
|
|
for (auto iter = aAnimationSet.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
iter.Get()->GetKey()->TriggerNow();
|
|
|
|
}
|
|
|
|
aAnimationSet.Clear();
|
|
|
|
};
|
|
|
|
|
|
|
|
triggerAndClearAnimations(mPlayPendingSet);
|
|
|
|
triggerAndClearAnimations(mPausePendingSet);
|
2016-12-02 04:10:44 +03:00
|
|
|
|
|
|
|
mHasPlayPendingGeometricAnimations = CheckState::Absent;
|
|
|
|
}
|
|
|
|
|
2019-01-08 09:46:23 +03:00
|
|
|
static bool IsTransition(const dom::Animation& aAnimation) {
|
2018-11-01 03:15:45 +03:00
|
|
|
const dom::CSSTransition* transition = aAnimation.AsCSSTransition();
|
|
|
|
return transition && transition->IsTiedToMarkup();
|
|
|
|
}
|
|
|
|
|
2016-12-02 04:10:44 +03:00
|
|
|
void PendingAnimationTracker::MarkAnimationsThatMightNeedSynchronization() {
|
2018-11-01 03:15:45 +03:00
|
|
|
// We only set mHasPlayPendingGeometricAnimations to "present" in this method
|
|
|
|
// and nowhere else. After setting the state to "present", if there is any
|
|
|
|
// change to the set of play-pending animations we will reset
|
|
|
|
// mHasPlayPendingGeometricAnimations to either "indeterminate" or "absent".
|
|
|
|
//
|
|
|
|
// As a result, if mHasPlayPendingGeometricAnimations is "present", we can
|
|
|
|
// assume that this method has already been called for the current set of
|
|
|
|
// play-pending animations and it is not necessary to run this method again.
|
|
|
|
//
|
|
|
|
// If mHasPlayPendingGeometricAnimations is "absent", then we can also skip
|
|
|
|
// the body of this method since there are no notifications to be sent.
|
2016-12-02 04:10:44 +03:00
|
|
|
//
|
2018-11-01 03:15:45 +03:00
|
|
|
// Therefore, the only case we need to be concerned about is the
|
|
|
|
// "indeterminate" case. For all other cases we can return early.
|
2016-12-02 04:10:44 +03:00
|
|
|
//
|
|
|
|
// Note that *without* this optimization, starting animations would become
|
2018-11-01 03:15:45 +03:00
|
|
|
// O(n^2) in the case where each animation is on a different element and
|
2016-12-02 04:10:44 +03:00
|
|
|
// contains a compositor-animatable property since we would end up iterating
|
|
|
|
// over all animations in the play-pending set for each target element.
|
2018-11-01 03:15:45 +03:00
|
|
|
if (mHasPlayPendingGeometricAnimations != CheckState::Indeterminate) {
|
2016-12-02 04:10:44 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-01 03:15:45 +03:00
|
|
|
// We only synchronize CSS transitions with other CSS transitions (and we only
|
|
|
|
// synchronize non-transition animations with non-transition animations)
|
|
|
|
// since typically the author will not trigger both CSS animations and
|
|
|
|
// CSS transitions simultaneously and expect them to be synchronized.
|
|
|
|
//
|
|
|
|
// If we try to synchronize CSS transitions with non-transitions then for some
|
|
|
|
// content we will end up degrading performance by forcing animations to run
|
|
|
|
// on the main thread that really don't need to.
|
2016-12-02 04:10:44 +03:00
|
|
|
|
|
|
|
mHasPlayPendingGeometricAnimations = CheckState::Absent;
|
|
|
|
for (auto iter = mPlayPendingSet.ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
auto animation = iter.Get()->GetKey();
|
|
|
|
if (animation->GetEffect() && animation->GetEffect()->AffectsGeometry()) {
|
2018-11-01 03:15:45 +03:00
|
|
|
mHasPlayPendingGeometricAnimations &= ~CheckState::Absent;
|
|
|
|
mHasPlayPendingGeometricAnimations |= IsTransition(*animation)
|
|
|
|
? CheckState::TransitionsPresent
|
|
|
|
: CheckState::AnimationsPresent;
|
|
|
|
|
|
|
|
// If we have both transitions and animations we don't need to look any
|
|
|
|
// further.
|
|
|
|
if (mHasPlayPendingGeometricAnimations ==
|
|
|
|
(CheckState::TransitionsPresent | CheckState::AnimationsPresent)) {
|
|
|
|
break;
|
|
|
|
}
|
2016-12-02 04:10:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-01 03:15:45 +03:00
|
|
|
if (mHasPlayPendingGeometricAnimations == CheckState::Absent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto iter = mPlayPendingSet.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
auto animation = iter.Get()->GetKey();
|
|
|
|
bool isTransition = IsTransition(*animation);
|
|
|
|
if ((isTransition &&
|
|
|
|
mHasPlayPendingGeometricAnimations & CheckState::TransitionsPresent) ||
|
|
|
|
(!isTransition &&
|
|
|
|
mHasPlayPendingGeometricAnimations & CheckState::AnimationsPresent)) {
|
|
|
|
animation->NotifyGeometricAnimationsStartingThisFrame();
|
|
|
|
}
|
|
|
|
}
|
2014-12-22 03:35:41 +03:00
|
|
|
}
|
|
|
|
|
2015-04-21 04:22:09 +03:00
|
|
|
void PendingAnimationTracker::EnsurePaintIsScheduled() {
|
2014-12-22 03:35:41 +03:00
|
|
|
if (!mDocument) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-29 18:12:47 +03:00
|
|
|
PresShell* presShell = mDocument->GetPresShell();
|
2014-12-22 03:35:41 +03:00
|
|
|
if (!presShell) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIFrame* rootFrame = presShell->GetRootFrame();
|
|
|
|
if (!rootFrame) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-29 13:42:13 +03:00
|
|
|
rootFrame->SchedulePaintWithoutInvalidatingObservers();
|
2014-12-22 03:35:41 +03:00
|
|
|
}
|
|
|
|
|
2014-12-18 02:42:41 +03:00
|
|
|
} // namespace mozilla
|