1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef DOM_SMIL_SMILANIMATIONCONTROLLER_H_
8 #define DOM_SMIL_SMILANIMATIONCONTROLLER_H_
10 #include "mozilla/Attributes.h"
11 #include "mozilla/SMILCompositorTable.h"
12 #include "mozilla/SMILMilestone.h"
13 #include "mozilla/SMILTimeContainer.h"
14 #include "mozilla/UniquePtr.h"
17 #include "nsTHashtable.h"
18 #include "nsHashKeys.h"
19 #include "nsRefreshObservers.h"
21 class nsRefreshDriver
;
24 struct SMILTargetIdentifier
;
27 class SVGAnimationElement
;
30 //----------------------------------------------------------------------
31 // SMILAnimationController
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 document 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 SMILAnimationController final
: public SMILTimeContainer
,
44 public nsARefreshObserver
{
46 explicit SMILAnimationController(mozilla::dom::Document
* aDoc
);
48 // Clears mDocument pointer. (Called by our mozilla::dom::Document when it's
53 virtual void Pause(uint32_t aType
) override
;
54 virtual void Resume(uint32_t aType
) override
;
55 virtual SMILTime
GetParentTime() const override
;
58 NS_IMETHOD_(MozExternalRefCountType
) AddRef() override
;
59 NS_IMETHOD_(MozExternalRefCountType
) Release() override
;
61 virtual void WillRefresh(mozilla::TimeStamp aTime
) override
;
63 // Methods for registering and enumerating animation elements
64 void RegisterAnimationElement(
65 mozilla::dom::SVGAnimationElement
* aAnimationElement
);
66 void UnregisterAnimationElement(
67 mozilla::dom::SVGAnimationElement
* aAnimationElement
);
69 // Methods for resampling all animations
70 // (A resample performs the same operations as a sample but doesn't advance
71 // the current time and doesn't check if the container is paused)
72 // This will flush pending style changes for the document.
73 void Resample() { DoSample(false); }
75 void SetResampleNeeded() {
76 if (!mRunningSample
&& !mResampleNeeded
) {
77 FlagDocumentNeedsFlush();
78 mResampleNeeded
= true;
82 // This will flush pending style changes for the document.
83 void FlushResampleRequests() {
84 if (!mResampleNeeded
) return;
89 // Methods for handling page transitions
93 // Methods for supporting cycle-collection
94 void Traverse(nsCycleCollectionTraversalCallback
* aCallback
);
97 // Methods for relaying the availability of the refresh driver
98 void NotifyRefreshDriverCreated(nsRefreshDriver
* aRefreshDriver
);
99 void NotifyRefreshDriverDestroying(nsRefreshDriver
* aRefreshDriver
);
101 // Helper to check if we have any animation elements at all
102 bool HasRegisteredAnimations() const {
103 return mAnimationElementTable
.Count() != 0;
106 bool MightHavePendingStyleUpdates() const {
107 return mMightHavePendingStyleUpdates
;
111 bool PreTraverseInSubtree(mozilla::dom::Element
* aRoot
);
114 ~SMILAnimationController();
116 // alias declarations
117 using TimeContainerPtrKey
= nsPtrHashKey
<SMILTimeContainer
>;
118 using TimeContainerHashtable
= nsTHashtable
<TimeContainerPtrKey
>;
119 using AnimationElementPtrKey
= nsPtrHashKey
<dom::SVGAnimationElement
>;
120 using AnimationElementHashtable
= nsTHashtable
<AnimationElementPtrKey
>;
122 // Returns mDocument's refresh driver, if it's got one.
123 nsRefreshDriver
* GetRefreshDriver();
125 // Methods for controlling whether we're sampling
126 void StartSampling(nsRefreshDriver
* aRefreshDriver
);
127 void StopSampling(nsRefreshDriver
* aRefreshDriver
);
129 // Wrapper for StartSampling that defers if no animations are registered.
130 void MaybeStartSampling(nsRefreshDriver
* aRefreshDriver
);
132 // Sample-related callbacks and implementation helpers
133 virtual void DoSample() override
;
134 void DoSample(bool aSkipUnchangedContainers
);
136 void RewindElements();
138 void DoMilestoneSamples();
140 static void SampleTimedElement(mozilla::dom::SVGAnimationElement
* aElement
,
141 TimeContainerHashtable
* aActiveContainers
);
143 static void AddAnimationToCompositorTable(
144 mozilla::dom::SVGAnimationElement
* aElement
,
145 SMILCompositorTable
* aCompositorTable
, bool& aStyleFlushNeeded
);
147 static bool GetTargetIdentifierForAnimation(
148 mozilla::dom::SVGAnimationElement
* aAnimElem
,
149 SMILTargetIdentifier
& aResult
);
151 // Methods for adding/removing time containers
152 virtual nsresult
AddChild(SMILTimeContainer
& aChild
) override
;
153 virtual void RemoveChild(SMILTimeContainer
& aChild
) override
;
155 void FlagDocumentNeedsFlush();
158 nsAutoRefCnt mRefCnt
;
161 AnimationElementHashtable mAnimationElementTable
;
162 TimeContainerHashtable mChildContainerTable
;
163 mozilla::TimeStamp mCurrentSampleTime
;
164 mozilla::TimeStamp mStartTime
;
166 // Average time between samples from the refresh driver. This is used to
167 // detect large unexpected gaps between samples such as can occur when the
168 // computer sleeps. The nature of the SMIL model means that catching up these
169 // large gaps can be expensive as, for example, many events may need to be
170 // dispatched for the intervening time when no samples were received.
172 // In such cases, we ignore the intervening gap and continue sampling from
173 // when we were expecting the next sample to arrive.
175 // Note that we only do this for SMIL and not CSS transitions (which doesn't
176 // have so much work to do to catch up) nor scripted animations (which expect
177 // animation time to follow real time).
179 // This behaviour does not affect pausing (since we're not *expecting* any
180 // samples then) nor seeking (where the SMIL model behaves somewhat
181 // differently such as not dispatching events).
182 SMILTime mAvgTimeBetweenSamples
;
184 bool mResampleNeeded
;
185 // If we're told to start sampling but there are no animation elements we just
186 // record the time, set the following flag, and then wait until we have an
187 // animation element. Then we'll reset this flag and actually start sampling.
188 bool mDeferredStartSampling
;
191 // Are we registered with our document's refresh driver?
192 bool mRegisteredWithRefreshDriver
;
194 // Have we updated animated values without adding them to the restyle tracker?
195 bool mMightHavePendingStyleUpdates
;
197 // Store raw ptr to mDocument. It owns the controller, so controller
198 // shouldn't outlive it
199 mozilla::dom::Document
* mDocument
;
201 // Contains compositors used in our last sample. We keep this around
202 // so we can detect when an element/attribute used to be animated,
203 // but isn't anymore for some reason. (e.g. if its <animate> element is
204 // removed or retargeted)
205 UniquePtr
<SMILCompositorTable
> mLastCompositorTable
;
208 } // namespace mozilla
210 #endif // DOM_SMIL_SMILANIMATIONCONTROLLER_H_