dom/smil/nsSMILAnimationController.h

branch
TOR_BUG_9701
changeset 8
97036ab72558
equal deleted inserted replaced
-1:000000000000 0:308c899aac65
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/. */
5
6 #ifndef NS_SMILANIMATIONCONTROLLER_H_
7 #define NS_SMILANIMATIONCONTROLLER_H_
8
9 #include "mozilla/Attributes.h"
10 #include "nsAutoPtr.h"
11 #include "nsCOMPtr.h"
12 #include "nsTArray.h"
13 #include "nsITimer.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"
20
21 struct nsSMILTargetIdentifier;
22 class nsIDocument;
23
24 namespace mozilla {
25 namespace dom {
26 class SVGAnimationElement;
27 }
28 }
29
30 //----------------------------------------------------------------------
31 // nsSMILAnimationController
32 //
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.
37 //
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.
42 //
43 class nsSMILAnimationController : public nsSMILTimeContainer,
44 public nsARefreshObserver
45 {
46 public:
47 nsSMILAnimationController(nsIDocument* aDoc);
48 ~nsSMILAnimationController();
49
50 // Clears mDocument pointer. (Called by our nsIDocument when it's going away)
51 void Disconnect();
52
53 // nsSMILContainer
54 virtual void Pause(uint32_t aType) MOZ_OVERRIDE;
55 virtual void Resume(uint32_t aType) MOZ_OVERRIDE;
56 virtual nsSMILTime GetParentTime() const MOZ_OVERRIDE;
57
58 // nsARefreshObserver
59 NS_IMETHOD_(MozExternalRefCountType) AddRef() MOZ_OVERRIDE;
60 NS_IMETHOD_(MozExternalRefCountType) Release() MOZ_OVERRIDE;
61
62 virtual void WillRefresh(mozilla::TimeStamp aTime) MOZ_OVERRIDE;
63
64 // Methods for registering and enumerating animation elements
65 void RegisterAnimationElement(mozilla::dom::SVGAnimationElement* aAnimationElement);
66 void UnregisterAnimationElement(mozilla::dom::SVGAnimationElement* aAnimationElement);
67
68 // Methods for resampling all animations
69 // (A resample performs the same operations as a sample but doesn't advance
70 // the current time and doesn't check if the container is paused)
71 // This will flush pending style changes for the document.
72 void Resample() { DoSample(false); }
73
74 void SetResampleNeeded()
75 {
76 if (!mRunningSample) {
77 if (!mResampleNeeded) {
78 FlagDocumentNeedsFlush();
79 }
80 mResampleNeeded = true;
81 }
82 }
83
84 // This will flush pending style changes for the document.
85 void FlushResampleRequests()
86 {
87 if (!mResampleNeeded)
88 return;
89
90 Resample();
91 }
92
93 // Methods for handling page transitions
94 void OnPageShow();
95 void OnPageHide();
96
97 // Methods for supporting cycle-collection
98 void Traverse(nsCycleCollectionTraversalCallback* aCallback);
99 void Unlink();
100
101 // Methods for relaying the availability of the refresh driver
102 void NotifyRefreshDriverCreated(nsRefreshDriver* aRefreshDriver);
103 void NotifyRefreshDriverDestroying(nsRefreshDriver* aRefreshDriver);
104
105 // Helper to check if we have any animation elements at all
106 bool HasRegisteredAnimations()
107 { return mAnimationElementTable.Count() != 0; }
108
109 protected:
110 // Typedefs
111 typedef nsPtrHashKey<nsSMILTimeContainer> TimeContainerPtrKey;
112 typedef nsTHashtable<TimeContainerPtrKey> TimeContainerHashtable;
113 typedef nsPtrHashKey<mozilla::dom::SVGAnimationElement> AnimationElementPtrKey;
114 typedef nsTHashtable<AnimationElementPtrKey> AnimationElementHashtable;
115
116 struct SampleTimeContainerParams
117 {
118 TimeContainerHashtable* mActiveContainers;
119 bool mSkipUnchangedContainers;
120 };
121
122 struct SampleAnimationParams
123 {
124 TimeContainerHashtable* mActiveContainers;
125 nsSMILCompositorTable* mCompositorTable;
126 };
127
128 struct GetMilestoneElementsParams
129 {
130 nsTArray<nsRefPtr<mozilla::dom::SVGAnimationElement> > mElements;
131 nsSMILMilestone mMilestone;
132 };
133
134 // Cycle-collection implementation helpers
135 static PLDHashOperator CompositorTableEntryTraverse(
136 nsSMILCompositor* aCompositor, void* aArg);
137
138 // Returns mDocument's refresh driver, if it's got one.
139 nsRefreshDriver* GetRefreshDriver();
140
141 // Methods for controlling whether we're sampling
142 void StartSampling(nsRefreshDriver* aRefreshDriver);
143 void StopSampling(nsRefreshDriver* aRefreshDriver);
144
145 // Wrapper for StartSampling that defers if no animations are registered.
146 void MaybeStartSampling(nsRefreshDriver* aRefreshDriver);
147
148 // Sample-related callbacks and implementation helpers
149 virtual void DoSample() MOZ_OVERRIDE;
150 void DoSample(bool aSkipUnchangedContainers);
151
152 void RewindElements();
153 static PLDHashOperator RewindNeeded(
154 TimeContainerPtrKey* aKey, void* aData);
155 static PLDHashOperator RewindAnimation(
156 AnimationElementPtrKey* aKey, void* aData);
157 static PLDHashOperator ClearRewindNeeded(
158 TimeContainerPtrKey* aKey, void* aData);
159
160 void DoMilestoneSamples();
161 static PLDHashOperator GetNextMilestone(
162 TimeContainerPtrKey* aKey, void* aData);
163 static PLDHashOperator GetMilestoneElements(
164 TimeContainerPtrKey* aKey, void* aData);
165
166 static PLDHashOperator SampleTimeContainer(
167 TimeContainerPtrKey* aKey, void* aData);
168 static PLDHashOperator SampleAnimation(
169 AnimationElementPtrKey* aKey, void* aData);
170 static void SampleTimedElement(mozilla::dom::SVGAnimationElement* aElement,
171 TimeContainerHashtable* aActiveContainers);
172 static void AddAnimationToCompositorTable(
173 mozilla::dom::SVGAnimationElement* aElement, nsSMILCompositorTable* aCompositorTable);
174 static bool GetTargetIdentifierForAnimation(
175 mozilla::dom::SVGAnimationElement* aAnimElem, nsSMILTargetIdentifier& aResult);
176
177 // Methods for adding/removing time containers
178 virtual nsresult AddChild(nsSMILTimeContainer& aChild) MOZ_OVERRIDE;
179 virtual void RemoveChild(nsSMILTimeContainer& aChild) MOZ_OVERRIDE;
180
181 void FlagDocumentNeedsFlush();
182
183 // Members
184 nsAutoRefCnt mRefCnt;
185 NS_DECL_OWNINGTHREAD
186
187 AnimationElementHashtable mAnimationElementTable;
188 TimeContainerHashtable mChildContainerTable;
189 mozilla::TimeStamp mCurrentSampleTime;
190 mozilla::TimeStamp mStartTime;
191
192 // Average time between samples from the refresh driver. This is used to
193 // detect large unexpected gaps between samples such as can occur when the
194 // computer sleeps. The nature of the SMIL model means that catching up these
195 // large gaps can be expensive as, for example, many events may need to be
196 // dispatched for the intervening time when no samples were received.
197 //
198 // In such cases, we ignore the intervening gap and continue sampling from
199 // when we were expecting the next sample to arrive.
200 //
201 // Note that we only do this for SMIL and not CSS transitions (which doesn't
202 // have so much work to do to catch up) nor scripted animations (which expect
203 // animation time to follow real time).
204 //
205 // This behaviour does not affect pausing (since we're not *expecting* any
206 // samples then) nor seeking (where the SMIL model behaves somewhat
207 // differently such as not dispatching events).
208 nsSMILTime mAvgTimeBetweenSamples;
209
210 bool mResampleNeeded;
211 // If we're told to start sampling but there are no animation elements we just
212 // record the time, set the following flag, and then wait until we have an
213 // animation element. Then we'll reset this flag and actually start sampling.
214 bool mDeferredStartSampling;
215 bool mRunningSample;
216
217 // Are we registered with our document's refresh driver?
218 bool mRegisteredWithRefreshDriver;
219
220 // Store raw ptr to mDocument. It owns the controller, so controller
221 // shouldn't outlive it
222 nsIDocument* mDocument;
223
224 // Contains compositors used in our last sample. We keep this around
225 // so we can detect when an element/attribute used to be animated,
226 // but isn't anymore for some reason. (e.g. if its <animate> element is
227 // removed or retargeted)
228 nsAutoPtr<nsSMILCompositorTable> mLastCompositorTable;
229 };
230
231 #endif // NS_SMILANIMATIONCONTROLLER_H_

mercurial