Bug 1340322 - Part 3: Make BuildAnimations static function. r?birtles draft
authorHiroyuki Ikezoe <hikezoe@mozilla.com>
Mon, 06 Mar 2017 09:48:36 +0900
changeset 493774 6ca57fde71c0e97f1590550d68077751df129426
parent 493773 846dbf9f284cd19f1e43119a0b8dd5825c391801
child 493775 fa3987d6026dcaf86002cc121c659ac812ce6e66
push id47837
push userhikezoe@mozilla.com
push dateMon, 06 Mar 2017 01:11:14 +0000
reviewersbirtles
bugs1340322
milestone54.0a1
Bug 1340322 - Part 3: Make BuildAnimations static function. r?birtles This function will be also a template function. MozReview-Commit-ID: 4SbzEw8YzIZ
layout/style/nsAnimationManager.cpp
layout/style/nsAnimationManager.h
--- a/layout/style/nsAnimationManager.cpp
+++ b/layout/style/nsAnimationManager.cpp
@@ -402,82 +402,16 @@ UpdateOldAnimationPropertiesWithNew(
   // animation to become irrelevant so only add a changed record if
   // the animation is still relevant.
   if (animationChanged && aOld.IsRelevant()) {
     nsNodeUtils::AnimationChanged(&aOld);
   }
 }
 
 void
-nsAnimationManager::UpdateAnimations(nsStyleContext* aStyleContext,
-                                     mozilla::dom::Element* aElement)
-{
-  MOZ_ASSERT(mPresContext->IsDynamic(),
-             "Should not update animations for print or print preview");
-  MOZ_ASSERT(aElement->IsInComposedDoc(),
-             "Should not update animations that are not attached to the "
-             "document tree");
-
-  NonOwningAnimationTarget target(aElement, aStyleContext->GetPseudoType());
-
-  // Everything that causes our animation data to change triggers a
-  // style change, which in turn triggers a non-animation restyle.
-  // Likewise, when we initially construct frames, we're not in a
-  // style change, but also not in an animation restyle.
-
-  const nsStyleDisplay* disp = aStyleContext->StyleDisplay();
-  CSSAnimationCollection* collection =
-    CSSAnimationCollection::GetAnimationCollection(target.mElement,
-                                                   target.mPseudoType);
-  if (!collection &&
-      disp->mAnimationNameCount == 1 &&
-      disp->mAnimations[0].GetName().IsEmpty()) {
-    return;
-  }
-
-  nsAutoAnimationMutationBatch mb(aElement->OwnerDoc());
-
-  // Build the updated animations list, extracting matching animations from
-  // the existing collection as we go.
-  OwningCSSAnimationPtrArray newAnimations;
-  if (!aStyleContext->IsInDisplayNoneSubtree()) {
-    BuildAnimations(aStyleContext, target, collection, newAnimations);
-  }
-
-  if (newAnimations.IsEmpty()) {
-    if (collection) {
-      collection->Destroy();
-    }
-    return;
-  }
-
-  if (!collection) {
-    bool createdCollection = false;
-    collection =
-      CSSAnimationCollection::GetOrCreateAnimationCollection(
-        target.mElement, target.mPseudoType, &createdCollection);
-    if (!collection) {
-      MOZ_ASSERT(!createdCollection, "outparam should agree with return value");
-      NS_WARNING("allocating collection failed");
-      return;
-    }
-
-    if (createdCollection) {
-      AddElementCollection(collection);
-    }
-  }
-  collection->mAnimations.SwapElements(newAnimations);
-
-  // Cancel removed animations
-  for (size_t newAnimIdx = newAnimations.Length(); newAnimIdx-- != 0; ) {
-    newAnimations[newAnimIdx]->CancelFromStyle();
-  }
-}
-
-void
 nsAnimationManager::StopAnimationsForElement(
   mozilla::dom::Element* aElement,
   mozilla::CSSPseudoElementType aPseudoType)
 {
   MOZ_ASSERT(aElement);
   CSSAnimationCollection* collection =
     CSSAnimationCollection::GetAnimationCollection(aElement, aPseudoType);
   if (!collection) {
@@ -1068,23 +1002,23 @@ CSSAnimationBuilder::GetComputedValue(ns
   // If we hit this assertion, it probably means we are fetching a value from
   // the computed style that we don't know how to represent as
   // a StyleAnimationValue.
   MOZ_ASSERT(result.GetUnit() != eCSSUnit_Null, "Got null computed value");
 
   return result;
 }
 
-void
-nsAnimationManager::BuildAnimations(nsStyleContext* aStyleContext,
-                                    const NonOwningAnimationTarget& aTarget,
-                                    CSSAnimationCollection* aCollection,
-                                    OwningCSSAnimationPtrArray& aAnimations)
+static nsAnimationManager::OwningCSSAnimationPtrArray
+BuildAnimations(nsPresContext* aPresContext,
+                nsStyleContext* aStyleContext,
+                const NonOwningAnimationTarget& aTarget,
+                nsAnimationManager::CSSAnimationCollection* aCollection)
 {
-  MOZ_ASSERT(aAnimations.IsEmpty(), "expect empty array");
+  nsAnimationManager::OwningCSSAnimationPtrArray result;
 
   const nsStyleDisplay *disp = aStyleContext->StyleDisplay();
 
   CSSAnimationBuilder builder(aStyleContext, aTarget);
 
   for (size_t animIdx = disp->mAnimationNameCount; animIdx-- != 0;) {
     const StyleAnimation& src = disp->mAnimations[animIdx];
 
@@ -1092,23 +1026,93 @@ nsAnimationManager::BuildAnimations(nsSt
     // not generate animation events. This includes when the animation-name is
     // "none" which is represented by an empty name in the StyleAnimation.
     // Since such animations neither affect style nor dispatch events, we do
     // not generate a corresponding CSSAnimation for them.
     if (src.GetName().IsEmpty()) {
       continue;
     }
 
-    RefPtr<CSSAnimation> dest = BuildAnimation(mPresContext,
+    RefPtr<CSSAnimation> dest = BuildAnimation(aPresContext,
                                                aStyleContext,
                                                aTarget,
                                                src,
                                                builder,
                                                aCollection);
     if (!dest) {
       continue;
     }
 
     dest->SetAnimationIndex(static_cast<uint64_t>(animIdx));
-    aAnimations.AppendElement(dest);
+    result.AppendElement(dest);
+  }
+  return result;
+}
+
+void
+nsAnimationManager::UpdateAnimations(nsStyleContext* aStyleContext,
+                                     mozilla::dom::Element* aElement)
+{
+  MOZ_ASSERT(mPresContext->IsDynamic(),
+             "Should not update animations for print or print preview");
+  MOZ_ASSERT(aElement->IsInComposedDoc(),
+             "Should not update animations that are not attached to the "
+             "document tree");
+
+  NonOwningAnimationTarget target(aElement, aStyleContext->GetPseudoType());
+
+  // Everything that causes our animation data to change triggers a
+  // style change, which in turn triggers a non-animation restyle.
+  // Likewise, when we initially construct frames, we're not in a
+  // style change, but also not in an animation restyle.
+
+  const nsStyleDisplay* disp = aStyleContext->StyleDisplay();
+  CSSAnimationCollection* collection =
+    CSSAnimationCollection::GetAnimationCollection(target.mElement,
+                                                   target.mPseudoType);
+  if (!collection &&
+      disp->mAnimationNameCount == 1 &&
+      disp->mAnimations[0].GetName().IsEmpty()) {
+    return;
+  }
+
+  nsAutoAnimationMutationBatch mb(aElement->OwnerDoc());
+
+  // Build the updated animations list, extracting matching animations from
+  // the existing collection as we go.
+  OwningCSSAnimationPtrArray newAnimations;
+  if (!aStyleContext->IsInDisplayNoneSubtree()) {
+    newAnimations = BuildAnimations(mPresContext,
+                                    aStyleContext,
+                                    target,
+                                    collection);
+  }
+
+  if (newAnimations.IsEmpty()) {
+    if (collection) {
+      collection->Destroy();
+    }
+    return;
+  }
+
+  if (!collection) {
+    bool createdCollection = false;
+    collection =
+      CSSAnimationCollection::GetOrCreateAnimationCollection(
+        target.mElement, target.mPseudoType, &createdCollection);
+    if (!collection) {
+      MOZ_ASSERT(!createdCollection, "outparam should agree with return value");
+      NS_WARNING("allocating collection failed");
+      return;
+    }
+
+    if (createdCollection) {
+      AddElementCollection(collection);
+    }
+  }
+  collection->mAnimations.SwapElements(newAnimations);
+
+  // Cancel removed animations
+  for (size_t newAnimIdx = newAnimations.Length(); newAnimIdx-- != 0; ) {
+    newAnimations[newAnimIdx]->CancelFromStyle();
   }
 }
 
--- a/layout/style/nsAnimationManager.h
+++ b/layout/style/nsAnimationManager.h
@@ -350,18 +350,12 @@ public:
   // ::before and ::after.
   void StopAnimationsForElement(mozilla::dom::Element* aElement,
                                 mozilla::CSSPseudoElementType aPseudoType);
 
 protected:
   ~nsAnimationManager() override = default;
 
 private:
-
-  void BuildAnimations(nsStyleContext* aStyleContext,
-                       const mozilla::NonOwningAnimationTarget& aTarget,
-                       CSSAnimationCollection* aCollection,
-                       OwningCSSAnimationPtrArray& aAnimations);
-
   mozilla::DelayedEventDispatcher<mozilla::AnimationEventInfo> mEventDispatcher;
 };
 
 #endif /* !defined(nsAnimationManager_h_) */