Merge mozilla-central into tracemonkey.
[mozilla-central.git] / content / smil / nsSMILTimeContainer.cpp
blob47a43fb8da16ffd4ddbf7b530dd5379e070272bb
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
13 * License.
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.
21 * Contributor(s):
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()
44 mParent(nsnull),
45 mCurrentTime(0L),
46 mParentOffset(0L),
47 mPauseStart(0L),
48 mNeedsPauseSample(PR_FALSE),
49 mNeedsRewind(PR_FALSE),
50 mIsSeeking(PR_FALSE),
51 mPauseState(PAUSE_BEGIN)
55 nsSMILTimeContainer::~nsSMILTimeContainer()
57 if (mParent) {
58 mParent->RemoveChild(*this);
62 nsSMILTimeValue
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);
72 nsSMILTimeValue
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);
82 void
83 nsSMILTimeContainer::Begin()
85 Resume(PAUSE_BEGIN);
86 if (mPauseState) {
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.
97 UpdateCurrentTime();
100 void
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;
113 if (didStartPause) {
114 NotifyTimeChange();
118 void
119 nsSMILTimeContainer::Resume(PRUint32 aType)
121 if (!mPauseState)
122 return;
124 mPauseState &= ~aType;
126 if (!mPauseState) {
127 nsSMILTime extraOffset = GetParentTime() - mPauseStart;
128 mParentOffset += extraOffset;
129 NotifyTimeChange();
133 nsSMILTime
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))
142 return 0L;
144 return mCurrentTime;
147 void
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;
163 if (IsPaused()) {
164 mNeedsPauseSample = PR_TRUE;
165 mPauseStart = parentTime;
168 if (aSeekTo < mCurrentTime) {
169 // Backwards seek
170 mNeedsRewind = PR_TRUE;
171 ClearMilestones();
174 // Force an update to the current time in case we get a call to GetCurrentTime
175 // before another call to Sample().
176 UpdateCurrentTime();
178 NotifyTimeChange();
181 nsSMILTime
182 nsSMILTimeContainer::GetParentTime() const
184 if (mParent)
185 return mParent->GetCurrentTime();
187 return 0L;
190 void
191 nsSMILTimeContainer::SyncPauseTime()
193 if (IsPaused()) {
194 nsSMILTime parentTime = GetParentTime();
195 nsSMILTime extraOffset = parentTime - mPauseStart;
196 mParentOffset += extraOffset;
197 mPauseStart = parentTime;
201 void
202 nsSMILTimeContainer::Sample()
204 if (!NeedsSample())
205 return;
207 UpdateCurrentTime();
208 DoSample();
210 mNeedsPauseSample = PR_FALSE;
213 nsresult
214 nsSMILTimeContainer::SetParent(nsSMILTimeContainer* aParent)
216 if (mParent) {
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;
225 mPauseStart = 0L;
228 mParent = aParent;
230 nsresult rv = NS_OK;
231 if (mParent) {
232 rv = mParent->AddChild(*this);
235 return rv;
238 PRBool
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));
249 void
250 nsSMILTimeContainer::ClearMilestones()
252 mMilestoneEntries.Clear();
255 PRBool
256 nsSMILTimeContainer::GetNextMilestoneInParentTime(
257 nsSMILMilestone& aNextMilestone) const
259 if (mMilestoneEntries.IsEmpty())
260 return PR_FALSE;
262 nsSMILTimeValue parentTime =
263 ContainerToParentTime(mMilestoneEntries.Top().mMilestone.mTime);
264 if (!parentTime.IsResolved())
265 return PR_FALSE;
267 aNextMilestone = nsSMILMilestone(parentTime.GetMillis(),
268 mMilestoneEntries.Top().mMilestone.mIsEnd);
270 return PR_TRUE;
273 PRBool
274 nsSMILTimeContainer::PopMilestoneElementsAtMilestone(
275 const nsSMILMilestone& aMilestone,
276 AnimElemArray& aMatchedElements)
278 if (mMilestoneEntries.IsEmpty())
279 return PR_FALSE;
281 nsSMILTimeValue containerTime = ParentToContainerTime(aMilestone.mTime);
282 if (!containerTime.IsResolved())
283 return PR_FALSE;
285 nsSMILMilestone containerMilestone(containerTime.GetMillis(),
286 aMilestone.mIsEnd);
288 NS_ABORT_IF_FALSE(mMilestoneEntries.Top().mMilestone >= containerMilestone,
289 "Trying to pop off earliest times but we have earlier ones that were "
290 "overlooked");
292 PRBool gotOne = PR_FALSE;
293 while (!mMilestoneEntries.IsEmpty() &&
294 mMilestoneEntries.Top().mMilestone == containerMilestone)
296 aMatchedElements.AppendElement(mMilestoneEntries.Pop().mTimebase);
297 gotOne = PR_TRUE;
300 return gotOne;
303 void
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());
310 ++p;
314 void
315 nsSMILTimeContainer::Unlink()
317 mMilestoneEntries.Clear();
320 void
321 nsSMILTimeContainer::UpdateCurrentTime()
323 nsSMILTime now = IsPaused() ? mPauseStart : GetParentTime();
324 mCurrentTime = now - mParentOffset;
325 NS_ABORT_IF_FALSE(mCurrentTime >= 0, "Container has negative time");
328 void
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
334 // time.
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();
341 #if DEBUG
342 PRUint32 queueLength = mMilestoneEntries.Length();
343 #endif
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");
350 ++p;