1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
#include "CSSTransition.h"
#include "mozilla/AnimationEventDispatcher.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/dom/CSSTransitionBinding.h"
#include "mozilla/dom/KeyframeEffect.h"
#include "mozilla/dom/KeyframeEffectBinding.h"
#include "nsPresContext.h"
namespace mozilla::dom {
JSObject* CSSTransition::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return dom::CSSTransition_Binding::Wrap(aCx, this, aGivenProto);
}
void CSSTransition::GetTransitionProperty(nsString& aRetVal) const {
MOZ_ASSERT(mTransitionProperty.IsValid(),
"Transition Property should be initialized");
mTransitionProperty.ToString(aRetVal);
}
AnimationPlayState CSSTransition::PlayStateFromJS() const {
FlushUnanimatedStyle();
return Animation::PlayStateFromJS();
}
bool CSSTransition::PendingFromJS() const {
// Transitions don't become pending again after they start running but, if
// while the transition is still pending, style is updated in such a way
// that the transition will be canceled, we need to report false here.
// Hence we need to flush, but only when we're pending.
if (Pending()) {
FlushUnanimatedStyle();
}
return Animation::PendingFromJS();
}
void CSSTransition::PlayFromJS(ErrorResult& aRv) {
FlushUnanimatedStyle();
Animation::PlayFromJS(aRv);
}
void CSSTransition::UpdateTiming(SeekFlag aSeekFlag,
SyncNotifyFlag aSyncNotifyFlag) {
if (mNeedsNewAnimationIndexWhenRun &&
PlayState() != AnimationPlayState::Idle) {
mAnimationIndex = sNextAnimationIndex++;
mNeedsNewAnimationIndexWhenRun = false;
}
Animation::UpdateTiming(aSeekFlag, aSyncNotifyFlag);
}
void CSSTransition::QueueEvents(const StickyTimeDuration& aActiveTime) {
if (!mOwningElement.ShouldFireEvents()) {
return;
}
nsPresContext* presContext = mOwningElement.GetPresContext();
if (!presContext) {
return;
}
static constexpr StickyTimeDuration zeroDuration = StickyTimeDuration();
TransitionPhase currentPhase;
StickyTimeDuration intervalStartTime;
StickyTimeDuration intervalEndTime;
if (!mEffect) {
currentPhase = GetAnimationPhaseWithoutEffect<TransitionPhase>(*this);
} else {
ComputedTiming computedTiming = mEffect->GetComputedTiming();
currentPhase = static_cast<TransitionPhase>(computedTiming.mPhase);
intervalStartTime = IntervalStartTime(computedTiming.mActiveDuration);
intervalEndTime = IntervalEndTime(computedTiming.mActiveDuration);
}
if (mPendingState != PendingState::NotPending &&
(mPreviousTransitionPhase == TransitionPhase::Idle ||
mPreviousTransitionPhase == TransitionPhase::Pending)) {
currentPhase = TransitionPhase::Pending;
}
if (currentPhase == mPreviousTransitionPhase) {
return;
}
// TimeStamps to use for ordering the events when they are dispatched. We
// use a TimeStamp so we can compare events produced by different elements,
// perhaps even with different timelines.
// The zero timestamp is for transitionrun events where we ignore the delay
// for the purpose of ordering events.
TimeStamp zeroTimeStamp = AnimationTimeToTimeStamp(zeroDuration);
TimeStamp startTimeStamp = ElapsedTimeToTimeStamp(intervalStartTime);
TimeStamp endTimeStamp = ElapsedTimeToTimeStamp(intervalEndTime);
AutoTArray<AnimationEventInfo, 3> events;
auto appendTransitionEvent = [&](EventMessage aMessage,
const StickyTimeDuration& aElapsedTime,
const TimeStamp& aScheduledEventTimeStamp) {
double elapsedTime = aElapsedTime.ToSeconds();
if (aMessage == eTransitionCancel) {
// 0 is an inappropriate value for this callsite. What we need to do is
// use a single random value for all increasing times reportable.
// That is to say, whenever elapsedTime goes negative (because an
// animation restarts, something rewinds the animation, or otherwise)
// a new random value for the mix-in must be generated.
elapsedTime = nsRFPService::ReduceTimePrecisionAsSecsRFPOnly(
elapsedTime, 0, mRTPCallerType);
}
events.AppendElement(AnimationEventInfo(
TransitionProperty(), mOwningElement.Target(), aMessage, elapsedTime,
mAnimationIndex, aScheduledEventTimeStamp, this));
};
// Handle cancel events first
if ((mPreviousTransitionPhase != TransitionPhase::Idle &&
mPreviousTransitionPhase != TransitionPhase::After) &&
currentPhase == TransitionPhase::Idle) {
appendTransitionEvent(eTransitionCancel, aActiveTime,
GetTimelineCurrentTimeAsTimeStamp());
}
// All other events
switch (mPreviousTransitionPhase) {
case TransitionPhase::Idle:
if (currentPhase == TransitionPhase::Pending ||
currentPhase == TransitionPhase::Before) {
// When we are replacing a transition and flushing the style in the
// meantime, after a timeout, we may tick this transition without a
// proper |mPendingReadyTime| because the refresh driver is not in
// refresh, i.e. mInRefresh is false. So in the current tick we queue
// this event but the transition would be triggered in the next tick.
//
// In general, we use Animation::EnsurePaintIsScheduled() to assign a
// valid time to |mPendingReadyTime| of this transition, and then we
// could trigger this transition if this value is set. When triggering,
// we set a proper |mStartTime|, which could be used to calculate the
// animation time, i.e. |zeroTimeStamp|.
//
// However, due to this race condition (i.e. the transition hasn't been
// triggered yet but we are enqueuing this event), it's posssible to
// have a null |zeroTimeStamp|, which breaks the sorting of transition
// events. So we use the current time as a fallback way to make sure we
// have a reasonable schedule time for sorting.
appendTransitionEvent(eTransitionRun, intervalStartTime,
zeroTimeStamp.IsNull()
? GetTimelineCurrentTimeAsTimeStamp()
: zeroTimeStamp);
} else if (currentPhase == TransitionPhase::Active) {
appendTransitionEvent(eTransitionRun, intervalStartTime, zeroTimeStamp);
appendTransitionEvent(eTransitionStart, intervalStartTime,
startTimeStamp);
} else if (currentPhase == TransitionPhase::After) {
appendTransitionEvent(eTransitionRun, intervalStartTime, zeroTimeStamp);
appendTransitionEvent(eTransitionStart, intervalStartTime,
startTimeStamp);
appendTransitionEvent(eTransitionEnd, intervalEndTime, endTimeStamp);
}
break;
case TransitionPhase::Pending:
case TransitionPhase::Before:
if (currentPhase == TransitionPhase::Active) {
appendTransitionEvent(eTransitionStart, intervalStartTime,
startTimeStamp);
} else if (currentPhase == TransitionPhase::After) {
appendTransitionEvent(eTransitionStart, intervalStartTime,
startTimeStamp);
appendTransitionEvent(eTransitionEnd, intervalEndTime, endTimeStamp);
}
break;
case TransitionPhase::Active:
if (currentPhase == TransitionPhase::After) {
appendTransitionEvent(eTransitionEnd, intervalEndTime, endTimeStamp);
} else if (currentPhase == TransitionPhase::Before) {
appendTransitionEvent(eTransitionEnd, intervalStartTime,
startTimeStamp);
}
break;
case TransitionPhase::After:
if (currentPhase == TransitionPhase::Active) {
appendTransitionEvent(eTransitionStart, intervalEndTime,
startTimeStamp);
} else if (currentPhase == TransitionPhase::Before) {
appendTransitionEvent(eTransitionStart, intervalEndTime,
startTimeStamp);
appendTransitionEvent(eTransitionEnd, intervalStartTime, endTimeStamp);
}
break;
}
mPreviousTransitionPhase = currentPhase;
if (!events.IsEmpty()) {
presContext->AnimationEventDispatcher()->QueueEvents(std::move(events));
}
}
void CSSTransition::Tick(TickState& aState) {
Animation::Tick(aState);
QueueEvents();
}
const AnimatedPropertyID& CSSTransition::TransitionProperty() const {
MOZ_ASSERT(mTransitionProperty.IsValid(),
"Transition property should be initialized");
return mTransitionProperty;
}
AnimationValue CSSTransition::ToValue() const {
MOZ_ASSERT(!mTransitionToValue.IsNull(),
"Transition ToValue should be initialized");
return mTransitionToValue;
}
int32_t CSSTransition::CompareCompositeOrder(
const Maybe<EventContext>& aContext, const CSSTransition& aOther,
const Maybe<EventContext>& aOtherContext,
nsContentUtils::NodeIndexCache& aCache) const {
MOZ_ASSERT((IsTiedToMarkup() || aContext) &&
(aOther.IsTiedToMarkup() || aOtherContext),
"Should only be called for CSS transitions that are sorted "
"as CSS transitions (i.e. tied to CSS markup) or with overridden "
"target and animation index");
// 0. Object-equality case
if (&aOther == this) {
return 0;
}
// 1. Sort by document order
const OwningElementRef& owningElement1 =
IsTiedToMarkup() ? mOwningElement : OwningElementRef(aContext->mTarget);
const OwningElementRef& owningElement2 =
aOther.IsTiedToMarkup() ? aOther.mOwningElement
: OwningElementRef(aOtherContext->mTarget);
if (!owningElement1.Equals(owningElement2)) {
return owningElement1.Compare(owningElement2, aCache);
}
// 2. (Same element and pseudo): Sort by transition generation
const uint64_t index1 = IsTiedToMarkup() ? mAnimationIndex : aContext->mIndex;
const uint64_t index2 =
aOther.IsTiedToMarkup() ? aOther.mAnimationIndex : aOtherContext->mIndex;
if (index1 != index2) {
return index1 < index2 ? -1 : 1;
}
// 3. (Same transition generation): Sort by transition property
if (mTransitionProperty == aOther.mTransitionProperty) {
return 0;
}
nsAutoString name, otherName;
GetTransitionProperty(name);
aOther.GetTransitionProperty(otherName);
return name < otherName ? -1 : 1;
}
/* static */
Nullable<TimeDuration> CSSTransition::GetCurrentTimeAt(
const AnimationTimeline& aTimeline, const TimeStamp& aBaseTime,
const TimeDuration& aStartTime, double aPlaybackRate) {
Nullable<TimeDuration> result;
Nullable<TimeDuration> timelineTime = aTimeline.ToTimelineTime(aBaseTime);
if (!timelineTime.IsNull()) {
result.SetValue(
(timelineTime.Value() - aStartTime).MultDouble(aPlaybackRate));
}
return result;
}
double CSSTransition::CurrentValuePortion() const {
if (!GetEffect()) {
return 0.0;
}
// Transitions use a fill mode of 'backwards' so GetComputedTiming will
// never return a null time progress due to being *before* the animation
// interval. However, it might be possible that we're behind on flushing
// causing us to get called *after* the animation interval. So, just in
// case, we override the fill mode to 'both' to ensure the progress
// is never null.
TimingParams timingToUse = GetEffect()->SpecifiedTiming();
timingToUse.SetFill(dom::FillMode::Both);
ComputedTiming computedTiming = GetEffect()->GetComputedTiming(&timingToUse);
if (computedTiming.mProgress.IsNull()) {
return 0.0;
}
// 'transition-timing-function' corresponds to the effect timing while
// the transition keyframes have a linear timing function so we can ignore
// them for the purposes of calculating the value portion.
return computedTiming.mProgress.Value();
}
bool CSSTransition::UpdateStartValueFromReplacedTransition() {
MOZ_ASSERT(mEffect && mEffect->AsKeyframeEffect() &&
mEffect->AsKeyframeEffect()->HasAnimationOfPropertySet(
nsCSSPropertyIDSet::CompositorAnimatables()),
"Should be called for compositor-runnable transitions");
if (!mReplacedTransition) {
return false;
}
// We don't set |mReplacedTransition| if the timeline of this transition is
// different from the document timeline. The timeline of Animation may be
// null via script, so if it's null, it must be different from the document
// timeline (because document timeline is readonly so we cannot change it by
// script). Therefore, we check this assertion if mReplacedTransition is
// valid.
MOZ_ASSERT(mTimeline,
"Should have a timeline if we are replacing transition start "
"values");
if (Maybe<double> valuePosition =
ComputeTransformedProgress(*mTimeline, *mReplacedTransition)) {
// FIXME: Bug 1634945. We may have to use the last value on the compositor
// to replace the start value.
const AnimationValue& replacedFrom = mReplacedTransition->mFromValue;
const AnimationValue& replacedTo = mReplacedTransition->mToValue;
AnimationValue startValue;
startValue.mServo =
Servo_AnimationValues_Interpolate(replacedFrom.mServo,
replacedTo.mServo, *valuePosition)
.Consume();
mEffect->AsKeyframeEffect()->ReplaceTransitionStartValue(
std::move(startValue));
}
mReplacedTransition.reset();
return true;
}
/* static*/
Maybe<double> CSSTransition::ComputeTransformedProgress(
const AnimationTimeline& aTimeline,
const ReplacedTransitionProperties& aProperties) {
ComputedTiming computedTiming = AnimationEffect::GetComputedTimingAt(
CSSTransition::GetCurrentTimeAt(aTimeline, TimeStamp::Now(),
aProperties.mStartTime,
aProperties.mPlaybackRate),
aProperties.mTiming, aProperties.mPlaybackRate,
Animation::ProgressTimelinePosition::NotBoundary);
if (computedTiming.mProgress.IsNull()) {
return Nothing();
}
return Some(StyleComputedTimingFunction::GetPortion(
aProperties.mTimingFunction, computedTiming.mProgress.Value(),
computedTiming.mBeforeFlag));
}
void CSSTransition::SetEffectFromStyle(KeyframeEffect* aEffect) {
MOZ_ASSERT(aEffect->IsValidTransition());
Animation::SetEffectNoUpdate(aEffect);
mTransitionProperty = aEffect->Properties()[0].mProperty;
mTransitionToValue = aEffect->Properties()[0].mSegments[0].mToValue;
}
} // namespace mozilla::dom
|