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 #ifndef NS_SMILANIMATIONCONTROLLER_H_
7 #define NS_SMILANIMATIONCONTROLLER_H_
9 #include "mozilla/Attributes.h"
10 #include "nsAutoPtr.h"
14 #include "nsTHashtable.h"
15 #include "nsHashKeys.h"
16 #include "nsSMILTimeContainer.h"
17 #include "nsSMILCompositorTable.h"
18 #include "nsSMILMilestone.h"
19 #include "nsRefreshDriver.h"
21 struct nsSMILTargetIdentifier
;
26 class SVGAnimationElement
;
30 //----------------------------------------------------------------------
31 // nsSMILAnimationController
33 // The animation controller maintains the animation timer and determines the
34 // sample times and sample rate for all SMIL animations in a document. There is
35 // at most one animation controller per nsDocument so that frame-rate tuning can
36 // be performed at a document-level.
38 // The animation controller can contain many child time containers (timed
39 // document root objects) which may correspond to SVG document fragments within
40 // a compound document. These time containers can be paused individually or
41 // here, at the document level.
43 class nsSMILAnimationController
: public nsSMILTimeContainer
,
44 public nsARefreshObserver
47 explicit nsSMILAnimationController(nsIDocument
* aDoc
);
49 // Clears mDocument pointer. (Called by our nsIDocument when it's going away)
53 virtual void Pause(uint32_t aType
) MOZ_OVERRIDE
;
54 virtual void Resume(uint32_t aType
) MOZ_OVERRIDE
;
55 virtual nsSMILTime
GetParentTime() const MOZ_OVERRIDE
;
58 NS_IMETHOD_(MozExternalRefCountType
) AddRef() MOZ_OVERRIDE
;
59 NS_IMETHOD_(MozExternalRefCountType
) Release() MOZ_OVERRIDE
;
61 virtual void WillRefresh(mozilla::TimeStamp aTime
) MOZ_OVERRIDE
;
63 // Methods for registering and enumerating animation elements
64 void RegisterAnimationElement(mozilla::dom::SVGAnimationElement
* aAnimationElement
);
65 void UnregisterAnimationElement(mozilla::dom::SVGAnimationElement
* aAnimationElement
);
67 // Methods for resampling all animations
68 // (A resample performs the same operations as a sample but doesn't advance
69 // the current time and doesn't check if the container is paused)
70 // This will flush pending style changes for the document.
71 void Resample() { DoSample(false); }
73 void SetResampleNeeded()
75 if (!mRunningSample
) {
76 if (!mResampleNeeded
) {
77 FlagDocumentNeedsFlush();
79 mResampleNeeded
= true;
83 // This will flush pending style changes for the document.
84 void FlushResampleRequests()
92 // Methods for handling page transitions
96 // Methods for supporting cycle-collection
97 void Traverse(nsCycleCollectionTraversalCallback
* aCallback
);
100 // Methods for relaying the availability of the refresh driver
101 void NotifyRefreshDriverCreated(nsRefreshDriver
* aRefreshDriver
);
102 void NotifyRefreshDriverDestroying(nsRefreshDriver
* aRefreshDriver
);
104 // Helper to check if we have any animation elements at all
105 bool HasRegisteredAnimations()
106 { return mAnimationElementTable
.Count() != 0; }
109 ~nsSMILAnimationController();
112 typedef nsPtrHashKey
<nsSMILTimeContainer
> TimeContainerPtrKey
;
113 typedef nsTHashtable
<TimeContainerPtrKey
> TimeContainerHashtable
;
114 typedef nsPtrHashKey
<mozilla::dom::SVGAnimationElement
> AnimationElementPtrKey
;
115 typedef nsTHashtable
<AnimationElementPtrKey
> AnimationElementHashtable
;
117 struct SampleTimeContainerParams
119 TimeContainerHashtable
* mActiveContainers
;
120 bool mSkipUnchangedContainers
;
123 struct SampleAnimationParams
125 TimeContainerHashtable
* mActiveContainers
;
126 nsSMILCompositorTable
* mCompositorTable
;
129 struct GetMilestoneElementsParams
131 nsTArray
<nsRefPtr
<mozilla::dom::SVGAnimationElement
> > mElements
;
132 nsSMILMilestone mMilestone
;
135 // Cycle-collection implementation helpers
136 static PLDHashOperator
CompositorTableEntryTraverse(
137 nsSMILCompositor
* aCompositor
, void* aArg
);
139 // Returns mDocument's refresh driver, if it's got one.
140 nsRefreshDriver
* GetRefreshDriver();
142 // Methods for controlling whether we're sampling
143 void StartSampling(nsRefreshDriver
* aRefreshDriver
);
144 void StopSampling(nsRefreshDriver
* aRefreshDriver
);
146 // Wrapper for StartSampling that defers if no animations are registered.
147 void MaybeStartSampling(nsRefreshDriver
* aRefreshDriver
);
149 // Sample-related callbacks and implementation helpers
150 virtual void DoSample() MOZ_OVERRIDE
;
151 void DoSample(bool aSkipUnchangedContainers
);
153 void RewindElements();
154 static PLDHashOperator
RewindNeeded(
155 TimeContainerPtrKey
* aKey
, void* aData
);
156 static PLDHashOperator
RewindAnimation(
157 AnimationElementPtrKey
* aKey
, void* aData
);
158 static PLDHashOperator
ClearRewindNeeded(
159 TimeContainerPtrKey
* aKey
, void* aData
);
161 void DoMilestoneSamples();
162 static PLDHashOperator
GetNextMilestone(
163 TimeContainerPtrKey
* aKey
, void* aData
);
164 static PLDHashOperator
GetMilestoneElements(
165 TimeContainerPtrKey
* aKey
, void* aData
);
167 static PLDHashOperator
SampleTimeContainer(
168 TimeContainerPtrKey
* aKey
, void* aData
);
169 static PLDHashOperator
SampleAnimation(
170 AnimationElementPtrKey
* aKey
, void* aData
);
171 static void SampleTimedElement(mozilla::dom::SVGAnimationElement
* aElement
,
172 TimeContainerHashtable
* aActiveContainers
);
173 static void AddAnimationToCompositorTable(
174 mozilla::dom::SVGAnimationElement
* aElement
, nsSMILCompositorTable
* aCompositorTable
);
175 static bool GetTargetIdentifierForAnimation(
176 mozilla::dom::SVGAnimationElement
* aAnimElem
, nsSMILTargetIdentifier
& aResult
);
178 // Methods for adding/removing time containers
179 virtual nsresult
AddChild(nsSMILTimeContainer
& aChild
) MOZ_OVERRIDE
;
180 virtual void RemoveChild(nsSMILTimeContainer
& aChild
) MOZ_OVERRIDE
;
182 void FlagDocumentNeedsFlush();
185 nsAutoRefCnt mRefCnt
;
188 AnimationElementHashtable mAnimationElementTable
;
189 TimeContainerHashtable mChildContainerTable
;
190 mozilla::TimeStamp mCurrentSampleTime
;
191 mozilla::TimeStamp mStartTime
;
193 // Average time between samples from the refresh driver. This is used to
194 // detect large unexpected gaps between samples such as can occur when the
195 // computer sleeps. The nature of the SMIL model means that catching up these
196 // large gaps can be expensive as, for example, many events may need to be
197 // dispatched for the intervening time when no samples were received.
199 // In such cases, we ignore the intervening gap and continue sampling from
200 // when we were expecting the next sample to arrive.
202 // Note that we only do this for SMIL and not CSS transitions (which doesn't
203 // have so much work to do to catch up) nor scripted animations (which expect
204 // animation time to follow real time).
206 // This behaviour does not affect pausing (since we're not *expecting* any
207 // samples then) nor seeking (where the SMIL model behaves somewhat
208 // differently such as not dispatching events).
209 nsSMILTime mAvgTimeBetweenSamples
;
211 bool mResampleNeeded
;
212 // If we're told to start sampling but there are no animation elements we just
213 // record the time, set the following flag, and then wait until we have an
214 // animation element. Then we'll reset this flag and actually start sampling.
215 bool mDeferredStartSampling
;
218 // Are we registered with our document's refresh driver?
219 bool mRegisteredWithRefreshDriver
;
221 // Store raw ptr to mDocument. It owns the controller, so controller
222 // shouldn't outlive it
223 nsIDocument
* mDocument
;
225 // Contains compositors used in our last sample. We keep this around
226 // so we can detect when an element/attribute used to be animated,
227 // but isn't anymore for some reason. (e.g. if its <animate> element is
228 // removed or retargeted)
229 nsAutoPtr
<nsSMILCompositorTable
> mLastCompositorTable
;
232 #endif // NS_SMILANIMATIONCONTROLLER_H_