1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the Mozilla SMIL module.
17 * The Initial Developer of the Original Code is Brian Birtles.
18 * Portions created by the Initial Developer are Copyright (C) 2009
19 * the Initial Developer. All Rights Reserved.
22 * Brian Birtles <birtles@gmail.com>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsSMILTimeContainer.h"
39 #include "nsSMILTimeValue.h"
40 #include "nsSMILTimedElement.h"
42 nsSMILTimeContainer::nsSMILTimeContainer()
48 mNeedsPauseSample(PR_FALSE
),
49 mNeedsRewind(PR_FALSE
),
51 mPauseState(PAUSE_BEGIN
)
55 nsSMILTimeContainer::~nsSMILTimeContainer()
58 mParent
->RemoveChild(*this);
63 nsSMILTimeContainer::ContainerToParentTime(nsSMILTime aContainerTime
) const
65 // If we're paused, then future times are indefinite
66 if (IsPaused() && aContainerTime
> mCurrentTime
)
67 return nsSMILTimeValue::Indefinite();
69 return nsSMILTimeValue(aContainerTime
+ mParentOffset
);
73 nsSMILTimeContainer::ParentToContainerTime(nsSMILTime aParentTime
) const
75 // If we're paused, then any time after when we paused is indefinite
76 if (IsPaused() && aParentTime
> mPauseStart
)
77 return nsSMILTimeValue::Indefinite();
79 return nsSMILTimeValue(aParentTime
- mParentOffset
);
83 nsSMILTimeContainer::Begin()
87 mNeedsPauseSample
= PR_TRUE
;
90 // This is a little bit complicated here. Ideally we'd just like to call
91 // Sample() and force an initial sample but this turns out to be a bad idea
92 // because this may mean that NeedsSample() no longer reports true and so when
93 // we come to the first real sample our parent will skip us over altogether.
94 // So we force the time to be updated and adopt the policy to never call
95 // Sample() ourselves but to always leave that to our parent or client.
101 nsSMILTimeContainer::Pause(PRUint32 aType
)
103 PRBool didStartPause
= PR_FALSE
;
105 if (!mPauseState
&& aType
) {
106 mPauseStart
= GetParentTime();
107 mNeedsPauseSample
= PR_TRUE
;
108 didStartPause
= PR_TRUE
;
111 mPauseState
|= aType
;
119 nsSMILTimeContainer::Resume(PRUint32 aType
)
124 mPauseState
&= ~aType
;
127 nsSMILTime extraOffset
= GetParentTime() - mPauseStart
;
128 mParentOffset
+= extraOffset
;
134 nsSMILTimeContainer::GetCurrentTime() const
136 // The following behaviour is consistent with:
137 // http://www.w3.org/2003/01/REC-SVG11-20030114-errata
138 // #getCurrentTime_setCurrentTime_undefined_before_document_timeline_begin
139 // which says that if GetCurrentTime is called before the document timeline
140 // has begun we should just return 0.
141 if (IsPausedByType(PAUSE_BEGIN
))
148 nsSMILTimeContainer::SetCurrentTime(nsSMILTime aSeekTo
)
150 // SVG 1.1 doesn't specify what to do for negative times so we adopt SVGT1.2's
151 // behaviour of clamping negative times to 0.
152 aSeekTo
= PR_MAX(0, aSeekTo
);
154 // The following behaviour is consistent with:
155 // http://www.w3.org/2003/01/REC-SVG11-20030114-errata
156 // #getCurrentTime_setCurrentTime_undefined_before_document_timeline_begin
157 // which says that if SetCurrentTime is called before the document timeline
158 // has begun we should still adjust the offset.
159 nsSMILTime parentTime
= GetParentTime();
160 mParentOffset
= parentTime
- aSeekTo
;
161 mIsSeeking
= PR_TRUE
;
164 mNeedsPauseSample
= PR_TRUE
;
165 mPauseStart
= parentTime
;
168 if (aSeekTo
< mCurrentTime
) {
170 mNeedsRewind
= PR_TRUE
;
174 // Force an update to the current time in case we get a call to GetCurrentTime
175 // before another call to Sample().
182 nsSMILTimeContainer::GetParentTime() const
185 return mParent
->GetCurrentTime();
191 nsSMILTimeContainer::SyncPauseTime()
194 nsSMILTime parentTime
= GetParentTime();
195 nsSMILTime extraOffset
= parentTime
- mPauseStart
;
196 mParentOffset
+= extraOffset
;
197 mPauseStart
= parentTime
;
202 nsSMILTimeContainer::Sample()
210 mNeedsPauseSample
= PR_FALSE
;
214 nsSMILTimeContainer::SetParent(nsSMILTimeContainer
* aParent
)
217 mParent
->RemoveChild(*this);
218 // When we're not attached to a parent time container, GetParentTime() will
219 // return 0. We need to adjust our pause state information to be relative to
220 // this new time base.
221 // Note that since "current time = parent time - parent offset" setting the
222 // parent offset and pause start as follows preserves our current time even
223 // while parent time = 0.
224 mParentOffset
= -mCurrentTime
;
232 rv
= mParent
->AddChild(*this);
239 nsSMILTimeContainer::AddMilestone(const nsSMILMilestone
& aMilestone
,
240 nsISMILAnimationElement
& aElement
)
242 // We record the milestone time and store it along with the element but this
243 // time may change (e.g. if attributes are changed on the timed element in
244 // between samples). If this happens, then we may do an unecessary sample
245 // but that's pretty cheap.
246 return mMilestoneEntries
.Push(MilestoneEntry(aMilestone
, aElement
));
250 nsSMILTimeContainer::ClearMilestones()
252 mMilestoneEntries
.Clear();
256 nsSMILTimeContainer::GetNextMilestoneInParentTime(
257 nsSMILMilestone
& aNextMilestone
) const
259 if (mMilestoneEntries
.IsEmpty())
262 nsSMILTimeValue parentTime
=
263 ContainerToParentTime(mMilestoneEntries
.Top().mMilestone
.mTime
);
264 if (!parentTime
.IsResolved())
267 aNextMilestone
= nsSMILMilestone(parentTime
.GetMillis(),
268 mMilestoneEntries
.Top().mMilestone
.mIsEnd
);
274 nsSMILTimeContainer::PopMilestoneElementsAtMilestone(
275 const nsSMILMilestone
& aMilestone
,
276 AnimElemArray
& aMatchedElements
)
278 if (mMilestoneEntries
.IsEmpty())
281 nsSMILTimeValue containerTime
= ParentToContainerTime(aMilestone
.mTime
);
282 if (!containerTime
.IsResolved())
285 nsSMILMilestone
containerMilestone(containerTime
.GetMillis(),
288 NS_ABORT_IF_FALSE(mMilestoneEntries
.Top().mMilestone
>= containerMilestone
,
289 "Trying to pop off earliest times but we have earlier ones that were "
292 PRBool gotOne
= PR_FALSE
;
293 while (!mMilestoneEntries
.IsEmpty() &&
294 mMilestoneEntries
.Top().mMilestone
== containerMilestone
)
296 aMatchedElements
.AppendElement(mMilestoneEntries
.Pop().mTimebase
);
304 nsSMILTimeContainer::Traverse(nsCycleCollectionTraversalCallback
* aCallback
)
306 const MilestoneEntry
* p
= mMilestoneEntries
.Elements();
307 while (p
< mMilestoneEntries
.Elements() + mMilestoneEntries
.Length()) {
308 NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(*aCallback
, "mTimebase");
309 aCallback
->NoteXPCOMChild(p
->mTimebase
.get());
315 nsSMILTimeContainer::Unlink()
317 mMilestoneEntries
.Clear();
321 nsSMILTimeContainer::UpdateCurrentTime()
323 nsSMILTime now
= IsPaused() ? mPauseStart
: GetParentTime();
324 mCurrentTime
= now
- mParentOffset
;
325 NS_ABORT_IF_FALSE(mCurrentTime
>= 0, "Container has negative time");
329 nsSMILTimeContainer::NotifyTimeChange()
331 // Called when the container time is changed with respect to the document
332 // time. When this happens time dependencies in other time containers need to
333 // re-resolve their times because begin and end times are stored in container
336 // To get the list of timed elements with dependencies we simply re-use the
337 // milestone elements. This is because any timed element with dependents and
338 // with significant transitions yet to fire should have their next milestone
339 // registered. Other timed elements don't matter.
340 const MilestoneEntry
* p
= mMilestoneEntries
.Elements();
342 PRUint32 queueLength
= mMilestoneEntries
.Length();
344 while (p
< mMilestoneEntries
.Elements() + mMilestoneEntries
.Length()) {
345 nsISMILAnimationElement
* elem
= p
->mTimebase
.get();
346 elem
->TimedElement().HandleContainerTimeChange();
347 NS_ABORT_IF_FALSE(queueLength
== mMilestoneEntries
.Length(),
348 "Call to HandleContainerTimeChange resulted in a change to the "
349 "queue of milestones");