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