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 "nsSMILTimeContainer.h"
7 #include "nsSMILTimeValue.h"
8 #include "nsSMILTimedElement.h"
11 nsSMILTimeContainer::nsSMILTimeContainer()
17 mNeedsPauseSample(false),
20 mPauseState(PAUSE_BEGIN
)
24 nsSMILTimeContainer::~nsSMILTimeContainer()
27 mParent
->RemoveChild(*this);
32 nsSMILTimeContainer::ContainerToParentTime(nsSMILTime aContainerTime
) const
34 // If we're paused, then future times are indefinite
35 if (IsPaused() && aContainerTime
> mCurrentTime
)
36 return nsSMILTimeValue::Indefinite();
38 return nsSMILTimeValue(aContainerTime
+ mParentOffset
);
42 nsSMILTimeContainer::ParentToContainerTime(nsSMILTime aParentTime
) const
44 // If we're paused, then any time after when we paused is indefinite
45 if (IsPaused() && aParentTime
> mPauseStart
)
46 return nsSMILTimeValue::Indefinite();
48 return nsSMILTimeValue(aParentTime
- mParentOffset
);
52 nsSMILTimeContainer::Begin()
56 mNeedsPauseSample
= true;
59 // This is a little bit complicated here. Ideally we'd just like to call
60 // Sample() and force an initial sample but this turns out to be a bad idea
61 // because this may mean that NeedsSample() no longer reports true and so when
62 // we come to the first real sample our parent will skip us over altogether.
63 // So we force the time to be updated and adopt the policy to never call
64 // Sample() ourselves but to always leave that to our parent or client.
70 nsSMILTimeContainer::Pause(uint32_t aType
)
72 bool didStartPause
= false;
74 if (!mPauseState
&& aType
) {
75 mPauseStart
= GetParentTime();
76 mNeedsPauseSample
= true;
88 nsSMILTimeContainer::Resume(uint32_t aType
)
93 mPauseState
&= ~aType
;
96 nsSMILTime extraOffset
= GetParentTime() - mPauseStart
;
97 mParentOffset
+= extraOffset
;
103 nsSMILTimeContainer::GetCurrentTime() const
105 // The following behaviour is consistent with:
106 // http://www.w3.org/2003/01/REC-SVG11-20030114-errata
107 // #getCurrentTime_setCurrentTime_undefined_before_document_timeline_begin
108 // which says that if GetCurrentTime is called before the document timeline
109 // has begun we should just return 0.
110 if (IsPausedByType(PAUSE_BEGIN
))
117 nsSMILTimeContainer::SetCurrentTime(nsSMILTime aSeekTo
)
119 // SVG 1.1 doesn't specify what to do for negative times so we adopt SVGT1.2's
120 // behaviour of clamping negative times to 0.
121 aSeekTo
= std::max
<nsSMILTime
>(0, aSeekTo
);
123 // The following behaviour is consistent with:
124 // http://www.w3.org/2003/01/REC-SVG11-20030114-errata
125 // #getCurrentTime_setCurrentTime_undefined_before_document_timeline_begin
126 // which says that if SetCurrentTime is called before the document timeline
127 // has begun we should still adjust the offset.
128 nsSMILTime parentTime
= GetParentTime();
129 mParentOffset
= parentTime
- aSeekTo
;
133 mNeedsPauseSample
= true;
134 mPauseStart
= parentTime
;
137 if (aSeekTo
< mCurrentTime
) {
143 // Force an update to the current time in case we get a call to GetCurrentTime
144 // before another call to Sample().
151 nsSMILTimeContainer::GetParentTime() const
154 return mParent
->GetCurrentTime();
160 nsSMILTimeContainer::SyncPauseTime()
163 nsSMILTime parentTime
= GetParentTime();
164 nsSMILTime extraOffset
= parentTime
- mPauseStart
;
165 mParentOffset
+= extraOffset
;
166 mPauseStart
= parentTime
;
171 nsSMILTimeContainer::Sample()
179 mNeedsPauseSample
= false;
183 nsSMILTimeContainer::SetParent(nsSMILTimeContainer
* aParent
)
186 mParent
->RemoveChild(*this);
187 // When we're not attached to a parent time container, GetParentTime() will
188 // return 0. We need to adjust our pause state information to be relative to
189 // this new time base.
190 // Note that since "current time = parent time - parent offset" setting the
191 // parent offset and pause start as follows preserves our current time even
192 // while parent time = 0.
193 mParentOffset
= -mCurrentTime
;
201 rv
= mParent
->AddChild(*this);
208 nsSMILTimeContainer::AddMilestone(const nsSMILMilestone
& aMilestone
,
209 mozilla::dom::SVGAnimationElement
& aElement
)
211 // We record the milestone time and store it along with the element but this
212 // time may change (e.g. if attributes are changed on the timed element in
213 // between samples). If this happens, then we may do an unecessary sample
214 // but that's pretty cheap.
215 return mMilestoneEntries
.Push(MilestoneEntry(aMilestone
, aElement
));
219 nsSMILTimeContainer::ClearMilestones()
221 mMilestoneEntries
.Clear();
225 nsSMILTimeContainer::GetNextMilestoneInParentTime(
226 nsSMILMilestone
& aNextMilestone
) const
228 if (mMilestoneEntries
.IsEmpty())
231 nsSMILTimeValue parentTime
=
232 ContainerToParentTime(mMilestoneEntries
.Top().mMilestone
.mTime
);
233 if (!parentTime
.IsDefinite())
236 aNextMilestone
= nsSMILMilestone(parentTime
.GetMillis(),
237 mMilestoneEntries
.Top().mMilestone
.mIsEnd
);
243 nsSMILTimeContainer::PopMilestoneElementsAtMilestone(
244 const nsSMILMilestone
& aMilestone
,
245 AnimElemArray
& aMatchedElements
)
247 if (mMilestoneEntries
.IsEmpty())
250 nsSMILTimeValue containerTime
= ParentToContainerTime(aMilestone
.mTime
);
251 if (!containerTime
.IsDefinite())
254 nsSMILMilestone
containerMilestone(containerTime
.GetMillis(),
257 NS_ABORT_IF_FALSE(mMilestoneEntries
.Top().mMilestone
>= containerMilestone
,
258 "Trying to pop off earliest times but we have earlier ones that were "
262 while (!mMilestoneEntries
.IsEmpty() &&
263 mMilestoneEntries
.Top().mMilestone
== containerMilestone
)
265 aMatchedElements
.AppendElement(mMilestoneEntries
.Pop().mTimebase
);
273 nsSMILTimeContainer::Traverse(nsCycleCollectionTraversalCallback
* aCallback
)
275 const MilestoneEntry
* p
= mMilestoneEntries
.Elements();
276 while (p
< mMilestoneEntries
.Elements() + mMilestoneEntries
.Length()) {
277 NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(*aCallback
, "mTimebase");
278 aCallback
->NoteXPCOMChild(static_cast<nsIContent
*>(p
->mTimebase
.get()));
284 nsSMILTimeContainer::Unlink()
286 mMilestoneEntries
.Clear();
290 nsSMILTimeContainer::UpdateCurrentTime()
292 nsSMILTime now
= IsPaused() ? mPauseStart
: GetParentTime();
293 mCurrentTime
= now
- mParentOffset
;
294 NS_ABORT_IF_FALSE(mCurrentTime
>= 0, "Container has negative time");
298 nsSMILTimeContainer::NotifyTimeChange()
300 // Called when the container time is changed with respect to the document
301 // time. When this happens time dependencies in other time containers need to
302 // re-resolve their times because begin and end times are stored in container
305 // To get the list of timed elements with dependencies we simply re-use the
306 // milestone elements. This is because any timed element with dependents and
307 // with significant transitions yet to fire should have their next milestone
308 // registered. Other timed elements don't matter.
309 const MilestoneEntry
* p
= mMilestoneEntries
.Elements();
311 uint32_t queueLength
= mMilestoneEntries
.Length();
313 while (p
< mMilestoneEntries
.Elements() + mMilestoneEntries
.Length()) {
314 mozilla::dom::SVGAnimationElement
* elem
= p
->mTimebase
.get();
315 elem
->TimedElement().HandleContainerTimeChange();
316 NS_ABORT_IF_FALSE(queueLength
== mMilestoneEntries
.Length(),
317 "Call to HandleContainerTimeChange resulted in a change to the "
318 "queue of milestones");