Bug 641388: When a refresh driver is created, don't start sampling unless we have animations registered. r=bz
authorDaniel Holbert <dholbert@cs.stanford.edu>
Fri, 01 Apr 2011 13:33:20 -0700
changeset 64632 b71e50bf9afc36bd0369935555dc97b8fe612a96
parent 64631 cd525fb68574a48f62e61779fbbd7b629d7946a9
child 64633 df19f4679db15dde177c87be50e0dc9a57d5a96a
push idunknown
push userunknown
push dateunknown
reviewersbz
bugs641388
milestone2.2a1pre
Bug 641388: When a refresh driver is created, don't start sampling unless we have animations registered. r=bz
content/smil/nsSMILAnimationController.cpp
content/smil/nsSMILAnimationController.h
--- a/content/smil/nsSMILAnimationController.cpp
+++ b/content/smil/nsSMILAnimationController.cpp
@@ -146,21 +146,17 @@ nsSMILAnimationController::Resume(PRUint
   // Update mCurrentSampleTime so that calls to GetParentTime--used for
   // calculating parent offsets--are accurate
   mCurrentSampleTime = mozilla::TimeStamp::Now();
 
   nsSMILTimeContainer::Resume(aType);
 
   if (wasPaused && !mPauseState && mChildContainerTable.Count()) {
     Sample(); // Run the first sample manually
-    if (mAnimationElementTable.Count()) {
-      StartSampling(GetRefreshDriverForDoc(mDocument));
-    } else {
-      mDeferredStartSampling = PR_TRUE;
-    }
+    MaybeStartSampling(GetRefreshDriverForDoc(mDocument));
   }
 }
 
 nsSMILTime
 nsSMILAnimationController::GetParentTime() const
 {
   return (nsSMILTime)(mCurrentSampleTime - mStartTime).ToMilliseconds();
 }
@@ -294,18 +290,18 @@ nsSMILAnimationController::Unlink()
 
 //----------------------------------------------------------------------
 // Refresh driver lifecycle related methods
 
 void
 nsSMILAnimationController::NotifyRefreshDriverCreated(
     nsRefreshDriver* aRefreshDriver)
 {
-  if (!mPauseState && !mDeferredStartSampling) {
-    StartSampling(aRefreshDriver);
+  if (!mPauseState) {
+    MaybeStartSampling(aRefreshDriver);
   }
 }
 
 void
 nsSMILAnimationController::NotifyRefreshDriverDestroying(
     nsRefreshDriver* aRefreshDriver)
 {
   if (!mPauseState && !mDeferredStartSampling) {
@@ -341,16 +337,32 @@ nsSMILAnimationController::StopSampling(
     // (and RefreshDriver), which would make GetRefreshDriverForDoc return null.
     NS_ABORT_IF_FALSE(!GetRefreshDriverForDoc(mDocument) ||
                       aRefreshDriver == GetRefreshDriverForDoc(mDocument),
                       "Stopping sampling with wrong refresh driver");
     aRefreshDriver->RemoveRefreshObserver(this, Flush_Style);
   }
 }
 
+void
+nsSMILAnimationController::MaybeStartSampling(nsRefreshDriver* aRefreshDriver)
+{
+  if (mDeferredStartSampling) {
+    // We've received earlier 'MaybeStartSampling' calls, and we're
+    // deferring until we get a registered animation.
+    return;
+  }
+
+  if (mAnimationElementTable.Count()) {
+    StartSampling(aRefreshDriver);
+  } else {
+    mDeferredStartSampling = PR_TRUE;
+  }
+}
+
 //----------------------------------------------------------------------
 // Sample-related methods and callbacks
 
 PR_CALLBACK PLDHashOperator
 TransferCachedBaseValue(nsSMILCompositor* aCompositor,
                         void* aData)
 {
   nsSMILCompositorTable* lastCompositorTable =
@@ -815,25 +827,21 @@ nsSMILAnimationController::GetTargetIden
 
 //----------------------------------------------------------------------
 // Add/remove child time containers
 
 nsresult
 nsSMILAnimationController::AddChild(nsSMILTimeContainer& aChild)
 {
   TimeContainerPtrKey* key = mChildContainerTable.PutEntry(&aChild);
-  NS_ENSURE_TRUE(key,NS_ERROR_OUT_OF_MEMORY);
+  NS_ENSURE_TRUE(key, NS_ERROR_OUT_OF_MEMORY);
 
   if (!mPauseState && mChildContainerTable.Count() == 1) {
     Sample(); // Run the first sample manually
-    if (mAnimationElementTable.Count()) {
-      StartSampling(GetRefreshDriverForDoc(mDocument));
-    } else {
-      mDeferredStartSampling = PR_TRUE;
-    }
+    MaybeStartSampling(GetRefreshDriverForDoc(mDocument));
   }
 
   return NS_OK;
 }
 
 void
 nsSMILAnimationController::RemoveChild(nsSMILTimeContainer& aChild)
 {
--- a/content/smil/nsSMILAnimationController.h
+++ b/content/smil/nsSMILAnimationController.h
@@ -157,16 +157,19 @@ protected:
   // Cycle-collection implementation helpers
   PR_STATIC_CALLBACK(PLDHashOperator) CompositorTableEntryTraverse(
       nsSMILCompositor* aCompositor, void* aArg);
 
   // Methods for controlling whether we're sampling
   void StartSampling(nsRefreshDriver* aRefreshDriver);
   void StopSampling(nsRefreshDriver* aRefreshDriver);
 
+  // Wrapper for StartSampling that defers if no animations are registered.
+  void MaybeStartSampling(nsRefreshDriver* aRefreshDriver);
+
   // Sample-related callbacks and implementation helpers
   virtual void DoSample();
   void DoSample(PRBool aSkipUnchangedContainers);
 
   void RewindElements();
   PR_STATIC_CALLBACK(PLDHashOperator) RewindNeeded(
       TimeContainerPtrKey* aKey, void* aData);
   PR_STATIC_CALLBACK(PLDHashOperator) RewindAnimation(