1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsSMILAnimationController.h"
7 #include "nsSMILCompositor.h"
8 #include "nsSMILCSSProperty.h"
9 #include "nsCSSProps.h"
11 #include "mozilla/dom/Element.h"
12 #include "nsIDocument.h"
13 #include "mozilla/dom/SVGAnimationElement.h"
14 #include "nsSMILTimedElement.h"
16 #include "mozilla/AutoRestore.h"
18 using namespace mozilla
;
19 using namespace mozilla::dom
;
21 //----------------------------------------------------------------------
22 // nsSMILAnimationController implementation
24 //----------------------------------------------------------------------
25 // ctors, dtors, factory methods
27 nsSMILAnimationController::nsSMILAnimationController(nsIDocument
* aDoc
)
28 : mAvgTimeBetweenSamples(0),
29 mResampleNeeded(false),
30 mDeferredStartSampling(false),
31 mRunningSample(false),
32 mRegisteredWithRefreshDriver(false),
35 NS_ABORT_IF_FALSE(aDoc
, "need a non-null document");
37 nsRefreshDriver
* refreshDriver
= GetRefreshDriver();
39 mStartTime
= refreshDriver
->MostRecentRefresh();
41 mStartTime
= mozilla::TimeStamp::Now();
43 mCurrentSampleTime
= mStartTime
;
48 nsSMILAnimationController::~nsSMILAnimationController()
50 NS_ASSERTION(mAnimationElementTable
.Count() == 0,
51 "Animation controller shouldn't be tracking any animation"
52 " elements when it dies");
53 NS_ASSERTION(!mRegisteredWithRefreshDriver
,
54 "Leaving stale entry in refresh driver's observer list");
58 nsSMILAnimationController::Disconnect()
60 NS_ABORT_IF_FALSE(mDocument
, "disconnecting when we weren't connected...?");
61 NS_ABORT_IF_FALSE(mRefCnt
.get() == 1,
62 "Expecting to disconnect when doc is sole remaining owner");
63 NS_ASSERTION(mPauseState
& nsSMILTimeContainer::PAUSE_PAGEHIDE
,
64 "Expecting to be paused for pagehide before disconnect");
66 StopSampling(GetRefreshDriver());
68 mDocument
= nullptr; // (raw pointer)
71 //----------------------------------------------------------------------
72 // nsSMILTimeContainer methods:
75 nsSMILAnimationController::Pause(uint32_t aType
)
77 nsSMILTimeContainer::Pause(aType
);
80 mDeferredStartSampling
= false;
81 StopSampling(GetRefreshDriver());
86 nsSMILAnimationController::Resume(uint32_t aType
)
88 bool wasPaused
= (mPauseState
!= 0);
89 // Update mCurrentSampleTime so that calls to GetParentTime--used for
90 // calculating parent offsets--are accurate
91 mCurrentSampleTime
= mozilla::TimeStamp::Now();
93 nsSMILTimeContainer::Resume(aType
);
95 if (wasPaused
&& !mPauseState
&& mChildContainerTable
.Count()) {
96 MaybeStartSampling(GetRefreshDriver());
97 Sample(); // Run the first sample manually
102 nsSMILAnimationController::GetParentTime() const
104 return (nsSMILTime
)(mCurrentSampleTime
- mStartTime
).ToMilliseconds();
107 //----------------------------------------------------------------------
108 // nsARefreshObserver methods:
109 NS_IMPL_ADDREF(nsSMILAnimationController
)
110 NS_IMPL_RELEASE(nsSMILAnimationController
)
112 // nsRefreshDriver Callback function
114 nsSMILAnimationController::WillRefresh(mozilla::TimeStamp aTime
)
116 // Although we never expect aTime to go backwards, when we initialise the
117 // animation controller, if we can't get hold of a refresh driver we
118 // initialise mCurrentSampleTime to Now(). It may be possible that after
119 // doing so we get sampled by a refresh driver whose most recent refresh time
120 // predates when we were initialised, so to be safe we make sure to take the
121 // most recent time here.
122 aTime
= std::max(mCurrentSampleTime
, aTime
);
124 // Sleep detection: If the time between samples is a whole lot greater than we
125 // were expecting then we assume the computer went to sleep or someone's
126 // messing with the clock. In that case, fiddle our parent offset and use our
127 // average time between samples to calculate the new sample time. This
128 // prevents us from hanging while trying to catch up on all the missed time.
130 // Smoothing of coefficient for the average function. 0.2 should let us track
131 // the sample rate reasonably tightly without being overly affected by
132 // occasional delays.
133 static const double SAMPLE_DUR_WEIGHTING
= 0.2;
134 // If the elapsed time exceeds our expectation by this number of times we'll
135 // initiate special behaviour to basically ignore the intervening time.
136 static const double SAMPLE_DEV_THRESHOLD
= 200.0;
138 nsSMILTime elapsedTime
=
139 (nsSMILTime
)(aTime
- mCurrentSampleTime
).ToMilliseconds();
140 if (mAvgTimeBetweenSamples
== 0) {
142 mAvgTimeBetweenSamples
= elapsedTime
;
144 if (elapsedTime
> SAMPLE_DEV_THRESHOLD
* mAvgTimeBetweenSamples
) {
145 // Unexpectedly long delay between samples.
146 NS_WARNING("Detected really long delay between samples, continuing from "
148 mParentOffset
+= elapsedTime
- mAvgTimeBetweenSamples
;
150 // Update the moving average. Due to truncation here the average will
151 // normally be a little less than it should be but that's probably ok.
152 mAvgTimeBetweenSamples
=
153 (nsSMILTime
)(elapsedTime
* SAMPLE_DUR_WEIGHTING
+
154 mAvgTimeBetweenSamples
* (1.0 - SAMPLE_DUR_WEIGHTING
));
156 mCurrentSampleTime
= aTime
;
161 //----------------------------------------------------------------------
162 // Animation element registration methods:
165 nsSMILAnimationController::RegisterAnimationElement(
166 SVGAnimationElement
* aAnimationElement
)
168 mAnimationElementTable
.PutEntry(aAnimationElement
);
169 if (mDeferredStartSampling
) {
170 mDeferredStartSampling
= false;
171 if (mChildContainerTable
.Count()) {
172 // mAnimationElementTable was empty, but now we've added its 1st element
173 NS_ABORT_IF_FALSE(mAnimationElementTable
.Count() == 1,
174 "we shouldn't have deferred sampling if we already had "
175 "animations registered");
176 StartSampling(GetRefreshDriver());
177 Sample(); // Run the first sample manually
178 } // else, don't sample until a time container is registered (via AddChild)
183 nsSMILAnimationController::UnregisterAnimationElement(
184 SVGAnimationElement
* aAnimationElement
)
186 mAnimationElementTable
.RemoveEntry(aAnimationElement
);
189 //----------------------------------------------------------------------
193 nsSMILAnimationController::OnPageShow()
195 Resume(nsSMILTimeContainer::PAUSE_PAGEHIDE
);
199 nsSMILAnimationController::OnPageHide()
201 Pause(nsSMILTimeContainer::PAUSE_PAGEHIDE
);
204 //----------------------------------------------------------------------
205 // Cycle-collection support
208 nsSMILAnimationController::Traverse(
209 nsCycleCollectionTraversalCallback
* aCallback
)
211 // Traverse last compositor table
212 if (mLastCompositorTable
) {
213 mLastCompositorTable
->EnumerateEntries(CompositorTableEntryTraverse
,
218 /*static*/ PLDHashOperator
219 nsSMILAnimationController::CompositorTableEntryTraverse(
220 nsSMILCompositor
* aCompositor
,
223 nsCycleCollectionTraversalCallback
* cb
=
224 static_cast<nsCycleCollectionTraversalCallback
*>(aArg
);
225 aCompositor
->Traverse(cb
);
226 return PL_DHASH_NEXT
;
230 nsSMILAnimationController::Unlink()
232 mLastCompositorTable
= nullptr;
235 //----------------------------------------------------------------------
236 // Refresh driver lifecycle related methods
239 nsSMILAnimationController::NotifyRefreshDriverCreated(
240 nsRefreshDriver
* aRefreshDriver
)
243 MaybeStartSampling(aRefreshDriver
);
248 nsSMILAnimationController::NotifyRefreshDriverDestroying(
249 nsRefreshDriver
* aRefreshDriver
)
251 if (!mPauseState
&& !mDeferredStartSampling
) {
252 StopSampling(aRefreshDriver
);
256 //----------------------------------------------------------------------
257 // Timer-related implementation helpers
260 nsSMILAnimationController::StartSampling(nsRefreshDriver
* aRefreshDriver
)
262 NS_ASSERTION(mPauseState
== 0, "Starting sampling but controller is paused");
263 NS_ASSERTION(!mDeferredStartSampling
,
264 "Started sampling but the deferred start flag is still set");
265 if (aRefreshDriver
) {
266 MOZ_ASSERT(!mRegisteredWithRefreshDriver
,
267 "Redundantly registering with refresh driver");
268 NS_ABORT_IF_FALSE(!GetRefreshDriver() ||
269 aRefreshDriver
== GetRefreshDriver(),
270 "Starting sampling with wrong refresh driver");
271 // We're effectively resuming from a pause so update our current sample time
272 // or else it will confuse our "average time between samples" calculations.
273 mCurrentSampleTime
= mozilla::TimeStamp::Now();
274 aRefreshDriver
->AddRefreshObserver(this, Flush_Style
);
275 mRegisteredWithRefreshDriver
= true;
280 nsSMILAnimationController::StopSampling(nsRefreshDriver
* aRefreshDriver
)
282 if (aRefreshDriver
&& mRegisteredWithRefreshDriver
) {
283 // NOTE: The document might already have been detached from its PresContext
284 // (and RefreshDriver), which would make GetRefreshDriver() return null.
285 NS_ABORT_IF_FALSE(!GetRefreshDriver() ||
286 aRefreshDriver
== GetRefreshDriver(),
287 "Stopping sampling with wrong refresh driver");
288 aRefreshDriver
->RemoveRefreshObserver(this, Flush_Style
);
289 mRegisteredWithRefreshDriver
= false;
294 nsSMILAnimationController::MaybeStartSampling(nsRefreshDriver
* aRefreshDriver
)
296 if (mDeferredStartSampling
) {
297 // We've received earlier 'MaybeStartSampling' calls, and we're
298 // deferring until we get a registered animation.
302 if (mAnimationElementTable
.Count()) {
303 StartSampling(aRefreshDriver
);
305 mDeferredStartSampling
= true;
309 //----------------------------------------------------------------------
310 // Sample-related methods and callbacks
313 TransferCachedBaseValue(nsSMILCompositor
* aCompositor
,
316 nsSMILCompositorTable
* lastCompositorTable
=
317 static_cast<nsSMILCompositorTable
*>(aData
);
318 nsSMILCompositor
* lastCompositor
=
319 lastCompositorTable
->GetEntry(aCompositor
->GetKey());
321 if (lastCompositor
) {
322 aCompositor
->StealCachedBaseValue(lastCompositor
);
325 return PL_DHASH_NEXT
;
329 RemoveCompositorFromTable(nsSMILCompositor
* aCompositor
,
332 nsSMILCompositorTable
* lastCompositorTable
=
333 static_cast<nsSMILCompositorTable
*>(aData
);
334 lastCompositorTable
->RemoveEntry(aCompositor
->GetKey());
335 return PL_DHASH_NEXT
;
339 DoClearAnimationEffects(nsSMILCompositor
* aCompositor
,
342 aCompositor
->ClearAnimationEffects();
343 return PL_DHASH_NEXT
;
347 DoComposeAttribute(nsSMILCompositor
* aCompositor
,
350 aCompositor
->ComposeAttribute();
351 return PL_DHASH_NEXT
;
355 nsSMILAnimationController::DoSample()
357 DoSample(true); // Skip unchanged time containers
361 nsSMILAnimationController::DoSample(bool aSkipUnchangedContainers
)
364 NS_ERROR("Shouldn't be sampling after document has disconnected");
367 if (mRunningSample
) {
368 NS_ERROR("Shouldn't be recursively sampling");
372 mResampleNeeded
= false;
373 // Set running sample flag -- do this before flushing styles so that when we
374 // flush styles we don't end up requesting extra samples
375 AutoRestore
<bool> autoRestoreRunningSample(mRunningSample
);
376 mRunningSample
= true;
378 // STEP 1: Bring model up to date
379 // (i) Rewind elements where necessary
380 // (ii) Run milestone samples
382 DoMilestoneSamples();
384 // STEP 2: Sample the child time containers
386 // When we sample the child time containers they will simply record the sample
387 // time in document time.
388 TimeContainerHashtable
activeContainers(mChildContainerTable
.Count());
389 SampleTimeContainerParams tcParams
= { &activeContainers
,
390 aSkipUnchangedContainers
};
391 mChildContainerTable
.EnumerateEntries(SampleTimeContainer
, &tcParams
);
393 // STEP 3: (i) Sample the timed elements AND
394 // (ii) Create a table of compositors
396 // (i) Here we sample the timed elements (fetched from the
397 // SVGAnimationElements) which determine from the active time if the
398 // element is active and what its simple time etc. is. This information is
399 // then passed to its time client (nsSMILAnimationFunction).
401 // (ii) During the same loop we also build up a table that contains one
402 // compositor for each animated attribute and which maps animated elements to
403 // the corresponding compositor for their target attribute.
405 // Note that this compositor table needs to be allocated on the heap so we can
406 // store it until the next sample. This lets us find out which elements were
407 // animated in sample 'n-1' but not in sample 'n' (and hence need to have
408 // their animation effects removed in sample 'n').
410 // Parts (i) and (ii) are not functionally related but we combine them here to
411 // save iterating over the animation elements twice.
413 // Create the compositor table
414 nsAutoPtr
<nsSMILCompositorTable
>
415 currentCompositorTable(new nsSMILCompositorTable(0));
417 SampleAnimationParams saParams
= { &activeContainers
,
418 currentCompositorTable
};
419 mAnimationElementTable
.EnumerateEntries(SampleAnimation
,
421 activeContainers
.Clear();
423 // STEP 4: Compare previous sample's compositors against this sample's.
424 // (Transfer cached base values across, & remove animation effects from
425 // no-longer-animated targets.)
426 if (mLastCompositorTable
) {
427 // * Transfer over cached base values, from last sample's compositors
428 currentCompositorTable
->EnumerateEntries(TransferCachedBaseValue
,
429 mLastCompositorTable
);
431 // * For each compositor in current sample's hash table, remove entry from
432 // prev sample's hash table -- we don't need to clear animation
433 // effects of those compositors, since they're still being animated.
434 currentCompositorTable
->EnumerateEntries(RemoveCompositorFromTable
,
435 mLastCompositorTable
);
437 // * For each entry that remains in prev sample's hash table (i.e. for
438 // every target that's no longer animated), clear animation effects.
439 mLastCompositorTable
->EnumerateEntries(DoClearAnimationEffects
, nullptr);
442 // return early if there are no active animations to avoid a style flush
443 if (currentCompositorTable
->Count() == 0) {
444 mLastCompositorTable
= nullptr;
448 nsCOMPtr
<nsIDocument
> kungFuDeathGrip(mDocument
); // keeps 'this' alive too
449 mDocument
->FlushPendingNotifications(Flush_Style
);
452 // WARNING: the above flush may have destroyed the pres shell and/or
453 // WARNING: frames and other layout related objects.
456 // STEP 5: Compose currently-animated attributes.
457 // XXXdholbert: This step traverses our animation targets in an effectively
458 // random order. For animation from/to 'inherit' values to work correctly
459 // when the inherited value is *also* being animated, we really should be
460 // traversing our animated nodes in an ancestors-first order (bug 501183)
461 currentCompositorTable
->EnumerateEntries(DoComposeAttribute
, nullptr);
463 // Update last compositor table
464 mLastCompositorTable
= currentCompositorTable
.forget();
466 NS_ASSERTION(!mResampleNeeded
, "Resample dirty flag set during sample!");
470 nsSMILAnimationController::RewindElements()
472 bool rewindNeeded
= false;
473 mChildContainerTable
.EnumerateEntries(RewindNeeded
, &rewindNeeded
);
477 mAnimationElementTable
.EnumerateEntries(RewindAnimation
, nullptr);
478 mChildContainerTable
.EnumerateEntries(ClearRewindNeeded
, nullptr);
481 /*static*/ PLDHashOperator
482 nsSMILAnimationController::RewindNeeded(TimeContainerPtrKey
* aKey
,
485 NS_ABORT_IF_FALSE(aData
,
486 "Null data pointer during time container enumeration");
487 bool* rewindNeeded
= static_cast<bool*>(aData
);
489 nsSMILTimeContainer
* container
= aKey
->GetKey();
490 if (container
->NeedsRewind()) {
491 *rewindNeeded
= true;
492 return PL_DHASH_STOP
;
495 return PL_DHASH_NEXT
;
498 /*static*/ PLDHashOperator
499 nsSMILAnimationController::RewindAnimation(AnimationElementPtrKey
* aKey
,
502 SVGAnimationElement
* animElem
= aKey
->GetKey();
503 nsSMILTimeContainer
* timeContainer
= animElem
->GetTimeContainer();
504 if (timeContainer
&& timeContainer
->NeedsRewind()) {
505 animElem
->TimedElement().Rewind();
508 return PL_DHASH_NEXT
;
511 /*static*/ PLDHashOperator
512 nsSMILAnimationController::ClearRewindNeeded(TimeContainerPtrKey
* aKey
,
515 aKey
->GetKey()->ClearNeedsRewind();
516 return PL_DHASH_NEXT
;
520 nsSMILAnimationController::DoMilestoneSamples()
522 // We need to sample the timing model but because SMIL operates independently
523 // of the frame-rate, we can get one sample at t=0s and the next at t=10min.
525 // In between those two sample times a whole string of significant events
526 // might be expected to take place: events firing, new interdependencies
527 // between animations resolved and dissolved, etc.
529 // Furthermore, at any given time, we want to sample all the intervals that
530 // end at that time BEFORE any that begin. This behaviour is implied by SMIL's
531 // endpoint-exclusive timing model.
533 // So we have the animations (specifically the timed elements) register the
534 // next significant moment (called a milestone) in their lifetime and then we
535 // step through the model at each of these moments and sample those animations
536 // registered for those times. This way events can fire in the correct order,
537 // dependencies can be resolved etc.
539 nsSMILTime sampleTime
= INT64_MIN
;
542 // We want to find any milestones AT OR BEFORE the current sample time so we
543 // initialise the next milestone to the moment after (1ms after, to be
544 // precise) the current sample time and see if there are any milestones
545 // before that. Any other milestones will be dealt with in a subsequent
547 nsSMILMilestone
nextMilestone(GetCurrentTime() + 1, true);
548 mChildContainerTable
.EnumerateEntries(GetNextMilestone
, &nextMilestone
);
550 if (nextMilestone
.mTime
> GetCurrentTime()) {
554 GetMilestoneElementsParams params
;
555 params
.mMilestone
= nextMilestone
;
556 mChildContainerTable
.EnumerateEntries(GetMilestoneElements
, ¶ms
);
557 uint32_t length
= params
.mElements
.Length();
559 // During the course of a sampling we don't want to actually go backwards.
560 // Due to negative offsets, early ends and the like, a timed element might
561 // register a milestone that is actually in the past. That's fine, but it's
562 // still only going to get *sampled* with whatever time we're up to and no
565 // Because we're only performing this clamping at the last moment, the
566 // animations will still all get sampled in the correct order and
567 // dependencies will be appropriately resolved.
568 sampleTime
= std::max(nextMilestone
.mTime
, sampleTime
);
570 for (uint32_t i
= 0; i
< length
; ++i
) {
571 SVGAnimationElement
* elem
= params
.mElements
[i
].get();
572 NS_ABORT_IF_FALSE(elem
, "nullptr animation element in list");
573 nsSMILTimeContainer
* container
= elem
->GetTimeContainer();
575 // The container may be nullptr if the element has been detached from its
576 // parent since registering a milestone.
579 nsSMILTimeValue containerTimeValue
=
580 container
->ParentToContainerTime(sampleTime
);
581 if (!containerTimeValue
.IsDefinite())
584 // Clamp the converted container time to non-negative values.
585 nsSMILTime containerTime
= std::max
<nsSMILTime
>(0, containerTimeValue
.GetMillis());
587 if (nextMilestone
.mIsEnd
) {
588 elem
->TimedElement().SampleEndAt(containerTime
);
590 elem
->TimedElement().SampleAt(containerTime
);
596 /*static*/ PLDHashOperator
597 nsSMILAnimationController::GetNextMilestone(TimeContainerPtrKey
* aKey
,
600 NS_ABORT_IF_FALSE(aKey
, "Null hash key for time container hash table");
601 NS_ABORT_IF_FALSE(aKey
->GetKey(), "Null time container key in hash table");
602 NS_ABORT_IF_FALSE(aData
,
603 "Null data pointer during time container enumeration");
605 nsSMILMilestone
* nextMilestone
= static_cast<nsSMILMilestone
*>(aData
);
607 nsSMILTimeContainer
* container
= aKey
->GetKey();
608 if (container
->IsPausedByType(nsSMILTimeContainer::PAUSE_BEGIN
))
609 return PL_DHASH_NEXT
;
611 nsSMILMilestone thisMilestone
;
612 bool didGetMilestone
=
613 container
->GetNextMilestoneInParentTime(thisMilestone
);
614 if (didGetMilestone
&& thisMilestone
< *nextMilestone
) {
615 *nextMilestone
= thisMilestone
;
618 return PL_DHASH_NEXT
;
621 /*static*/ PLDHashOperator
622 nsSMILAnimationController::GetMilestoneElements(TimeContainerPtrKey
* aKey
,
625 NS_ABORT_IF_FALSE(aKey
, "Null hash key for time container hash table");
626 NS_ABORT_IF_FALSE(aKey
->GetKey(), "Null time container key in hash table");
627 NS_ABORT_IF_FALSE(aData
,
628 "Null data pointer during time container enumeration");
630 GetMilestoneElementsParams
* params
=
631 static_cast<GetMilestoneElementsParams
*>(aData
);
633 nsSMILTimeContainer
* container
= aKey
->GetKey();
634 if (container
->IsPausedByType(nsSMILTimeContainer::PAUSE_BEGIN
))
635 return PL_DHASH_NEXT
;
637 container
->PopMilestoneElementsAtMilestone(params
->mMilestone
,
640 return PL_DHASH_NEXT
;
643 /*static*/ PLDHashOperator
644 nsSMILAnimationController::SampleTimeContainer(TimeContainerPtrKey
* aKey
,
647 NS_ENSURE_TRUE(aKey
, PL_DHASH_NEXT
);
648 NS_ENSURE_TRUE(aKey
->GetKey(), PL_DHASH_NEXT
);
649 NS_ENSURE_TRUE(aData
, PL_DHASH_NEXT
);
651 SampleTimeContainerParams
* params
=
652 static_cast<SampleTimeContainerParams
*>(aData
);
654 nsSMILTimeContainer
* container
= aKey
->GetKey();
655 if (!container
->IsPausedByType(nsSMILTimeContainer::PAUSE_BEGIN
) &&
656 (container
->NeedsSample() || !params
->mSkipUnchangedContainers
)) {
657 container
->ClearMilestones();
659 container
->MarkSeekFinished();
660 params
->mActiveContainers
->PutEntry(container
);
663 return PL_DHASH_NEXT
;
666 /*static*/ PLDHashOperator
667 nsSMILAnimationController::SampleAnimation(AnimationElementPtrKey
* aKey
,
670 NS_ENSURE_TRUE(aKey
, PL_DHASH_NEXT
);
671 NS_ENSURE_TRUE(aKey
->GetKey(), PL_DHASH_NEXT
);
672 NS_ENSURE_TRUE(aData
, PL_DHASH_NEXT
);
674 SVGAnimationElement
* animElem
= aKey
->GetKey();
675 SampleAnimationParams
* params
= static_cast<SampleAnimationParams
*>(aData
);
677 SampleTimedElement(animElem
, params
->mActiveContainers
);
678 AddAnimationToCompositorTable(animElem
, params
->mCompositorTable
);
680 return PL_DHASH_NEXT
;
684 nsSMILAnimationController::SampleTimedElement(
685 SVGAnimationElement
* aElement
, TimeContainerHashtable
* aActiveContainers
)
687 nsSMILTimeContainer
* timeContainer
= aElement
->GetTimeContainer();
691 // We'd like to call timeContainer->NeedsSample() here and skip all timed
692 // elements that belong to paused time containers that don't need a sample,
693 // but that doesn't work because we've already called Sample() on all the time
694 // containers so the paused ones don't need a sample any more and they'll
697 // Instead we build up a hashmap of active time containers during the previous
698 // step (SampleTimeContainer) and then test here if the container for this
699 // timed element is in the list.
700 if (!aActiveContainers
->GetEntry(timeContainer
))
703 nsSMILTime containerTime
= timeContainer
->GetCurrentTime();
705 NS_ABORT_IF_FALSE(!timeContainer
->IsSeeking(),
706 "Doing a regular sample but the time container is still seeking");
707 aElement
->TimedElement().SampleAt(containerTime
);
711 nsSMILAnimationController::AddAnimationToCompositorTable(
712 SVGAnimationElement
* aElement
, nsSMILCompositorTable
* aCompositorTable
)
714 // Add a compositor to the hash table if there's not already one there
715 nsSMILTargetIdentifier key
;
716 if (!GetTargetIdentifierForAnimation(aElement
, key
))
717 // Something's wrong/missing about animation's target; skip this animation
720 nsSMILAnimationFunction
& func
= aElement
->AnimationFunction();
722 // Only add active animation functions. If there are no active animations
723 // targeting an attribute, no compositor will be created and any previously
724 // applied animations will be cleared.
725 if (func
.IsActiveOrFrozen()) {
726 // Look up the compositor for our target, & add our animation function
727 // to its list of animation functions.
728 nsSMILCompositor
* result
= aCompositorTable
->PutEntry(key
);
729 result
->AddAnimationFunction(&func
);
731 } else if (func
.HasChanged()) {
732 // Look up the compositor for our target, and force it to skip the
733 // "nothing's changed so don't bother compositing" optimization for this
734 // sample. |func| is inactive, but it's probably *newly* inactive (since
735 // it's got HasChanged() == true), so we need to make sure to recompose
737 nsSMILCompositor
* result
= aCompositorTable
->PutEntry(key
);
738 result
->ToggleForceCompositing();
740 // We've now made sure that |func|'s inactivity will be reflected as of
741 // this sample. We need to clear its HasChanged() flag so that it won't
742 // trigger this same clause in future samples (until it changes again).
743 func
.ClearHasChanged();
748 IsTransformAttribute(int32_t aNamespaceID
, nsIAtom
*aAttributeName
)
750 return aNamespaceID
== kNameSpaceID_None
&&
751 (aAttributeName
== nsGkAtoms::transform
||
752 aAttributeName
== nsGkAtoms::patternTransform
||
753 aAttributeName
== nsGkAtoms::gradientTransform
);
756 // Helper function that, given a SVGAnimationElement, looks up its target
757 // element & target attribute and populates a nsSMILTargetIdentifier
760 nsSMILAnimationController::GetTargetIdentifierForAnimation(
761 SVGAnimationElement
* aAnimElem
, nsSMILTargetIdentifier
& aResult
)
763 // Look up target (animated) element
764 Element
* targetElem
= aAnimElem
->GetTargetElementContent();
766 // Animation has no target elem -- skip it.
769 // Look up target (animated) attribute
770 // SMILANIM section 3.1, attributeName may
771 // have an XMLNS prefix to indicate the XML namespace.
772 nsCOMPtr
<nsIAtom
> attributeName
;
773 int32_t attributeNamespaceID
;
774 if (!aAnimElem
->GetTargetAttributeName(&attributeNamespaceID
,
775 getter_AddRefs(attributeName
)))
776 // Animation has no target attr -- skip it.
779 // animateTransform can only animate transforms, conversely transforms
780 // can only be animated by animateTransform
781 if (IsTransformAttribute(attributeNamespaceID
, attributeName
) !=
782 (aAnimElem
->Tag() == nsGkAtoms::animateTransform
))
785 // Look up target (animated) attribute-type
786 nsSMILTargetAttrType attributeType
= aAnimElem
->GetTargetAttributeType();
788 // Check if an 'auto' attributeType refers to a CSS property or XML attribute.
789 // Note that SMIL requires we search for CSS properties first. So if they
790 // overlap, 'auto' = 'CSS'. (SMILANIM 3.1)
792 if (attributeType
== eSMILTargetAttrType_auto
) {
793 if (attributeNamespaceID
== kNameSpaceID_None
) {
794 // width/height are special as they may be attributes or for
795 // outer-<svg> elements, mapped into style.
796 if (attributeName
== nsGkAtoms::width
||
797 attributeName
== nsGkAtoms::height
) {
798 isCSS
= targetElem
->GetNameSpaceID() != kNameSpaceID_SVG
;
801 nsCSSProps::LookupProperty(nsDependentAtomString(attributeName
),
802 nsCSSProps::eEnabledForAllContent
);
803 isCSS
= nsSMILCSSProperty::IsPropertyAnimatable(prop
);
807 isCSS
= (attributeType
== eSMILTargetAttrType_CSS
);
811 aResult
.mElement
= targetElem
;
812 aResult
.mAttributeName
= attributeName
;
813 aResult
.mAttributeNamespaceID
= attributeNamespaceID
;
814 aResult
.mIsCSS
= isCSS
;
819 //----------------------------------------------------------------------
820 // Add/remove child time containers
823 nsSMILAnimationController::AddChild(nsSMILTimeContainer
& aChild
)
825 TimeContainerPtrKey
* key
= mChildContainerTable
.PutEntry(&aChild
);
826 NS_ENSURE_TRUE(key
, NS_ERROR_OUT_OF_MEMORY
);
828 if (!mPauseState
&& mChildContainerTable
.Count() == 1) {
829 MaybeStartSampling(GetRefreshDriver());
830 Sample(); // Run the first sample manually
837 nsSMILAnimationController::RemoveChild(nsSMILTimeContainer
& aChild
)
839 mChildContainerTable
.RemoveEntry(&aChild
);
841 if (!mPauseState
&& mChildContainerTable
.Count() == 0) {
842 StopSampling(GetRefreshDriver());
848 nsSMILAnimationController::GetRefreshDriver()
851 NS_ERROR("Requesting refresh driver after document has disconnected!");
855 nsIPresShell
* shell
= mDocument
->GetShell();
860 nsPresContext
* context
= shell
->GetPresContext();
861 return context
? context
->RefreshDriver() : nullptr;
865 nsSMILAnimationController::FlagDocumentNeedsFlush()
867 mDocument
->SetNeedStyleFlush();