Bug 1889091 - Part 6: Remove "scratch" register parameter from emitPushArguments...
[gecko.git] / dom / smil / SMILAnimationController.h
blob04f2e34cf3b1a838f8bf6ef9a12b911e90fd5647
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"
15 #include "nsCOMPtr.h"
16 #include "nsTArray.h"
17 #include "nsTHashtable.h"
18 #include "nsHashKeys.h"
19 #include "nsRefreshObservers.h"
21 class nsRefreshDriver;
23 namespace mozilla {
24 struct SMILTargetIdentifier;
25 namespace dom {
26 class Element;
27 class SVGAnimationElement;
28 } // namespace dom
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 {
45 public:
46 explicit SMILAnimationController(mozilla::dom::Document* aDoc);
48 // Clears mDocument pointer. (Called by our mozilla::dom::Document when it's
49 // going away)
50 void Disconnect();
52 // SMILContainer
53 void Pause(uint32_t aType) override;
54 void Resume(uint32_t aType) override;
55 SMILTime GetParentTime() const override;
57 // nsARefreshObserver
58 NS_IMETHOD_(MozExternalRefCountType) AddRef() override;
59 NS_IMETHOD_(MozExternalRefCountType) Release() override;
61 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;
86 Resample();
89 // Methods for handling page transitions
90 void OnPageShow();
91 void OnPageHide();
93 // Methods for supporting cycle-collection
94 void Traverse(nsCycleCollectionTraversalCallback* aCallback);
95 void Unlink();
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;
110 bool PreTraverse();
111 bool PreTraverseInSubtree(mozilla::dom::Element* aRoot);
113 protected:
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 UpdateSampling();
127 bool ShouldSample() const;
129 void StopSampling(nsRefreshDriver* aRefreshDriver);
131 // Wrapper for StartSampling that defers if no animations are registered.
132 void MaybeStartSampling(nsRefreshDriver* aRefreshDriver);
134 // Sample-related callbacks and implementation helpers
135 void DoSample() override;
136 void DoSample(bool aSkipUnchangedContainers);
138 void RewindElements();
140 void DoMilestoneSamples();
142 static void SampleTimedElement(mozilla::dom::SVGAnimationElement* aElement,
143 TimeContainerHashtable* aActiveContainers);
145 static void AddAnimationToCompositorTable(
146 mozilla::dom::SVGAnimationElement* aElement,
147 SMILCompositorTable* aCompositorTable, bool& aStyleFlushNeeded);
149 static bool GetTargetIdentifierForAnimation(
150 mozilla::dom::SVGAnimationElement* aAnimElem,
151 SMILTargetIdentifier& aResult);
153 // Methods for adding/removing time containers
154 nsresult AddChild(SMILTimeContainer& aChild) override;
155 void RemoveChild(SMILTimeContainer& aChild) override;
157 void FlagDocumentNeedsFlush();
159 // Members
160 nsAutoRefCnt mRefCnt;
161 NS_DECL_OWNINGTHREAD
163 AnimationElementHashtable mAnimationElementTable;
164 TimeContainerHashtable mChildContainerTable;
165 mozilla::TimeStamp mCurrentSampleTime;
166 mozilla::TimeStamp mStartTime;
168 // Average time between samples from the refresh driver. This is used to
169 // detect large unexpected gaps between samples such as can occur when the
170 // computer sleeps. The nature of the SMIL model means that catching up these
171 // large gaps can be expensive as, for example, many events may need to be
172 // dispatched for the intervening time when no samples were received.
174 // In such cases, we ignore the intervening gap and continue sampling from
175 // when we were expecting the next sample to arrive.
177 // Note that we only do this for SMIL and not CSS transitions (which doesn't
178 // have so much work to do to catch up) nor scripted animations (which expect
179 // animation time to follow real time).
181 // This behaviour does not affect pausing (since we're not *expecting* any
182 // samples then) nor seeking (where the SMIL model behaves somewhat
183 // differently such as not dispatching events).
184 SMILTime mAvgTimeBetweenSamples = 0;
186 bool mResampleNeeded = false;
187 bool mRunningSample = false;
189 // Are we registered with our document's refresh driver?
190 bool mRegisteredWithRefreshDriver = false;
192 // Have we updated animated values without adding them to the restyle tracker?
193 bool mMightHavePendingStyleUpdates = false;
195 // Store raw ptr to mDocument. It owns the controller, so controller
196 // shouldn't outlive it
197 mozilla::dom::Document* mDocument;
199 // Contains compositors used in our last sample. We keep this around
200 // so we can detect when an element/attribute used to be animated,
201 // but isn't anymore for some reason. (e.g. if its <animate> element is
202 // removed or retargeted)
203 UniquePtr<SMILCompositorTable> mLastCompositorTable;
206 } // namespace mozilla
208 #endif // DOM_SMIL_SMILANIMATIONCONTROLLER_H_