Bug 1334036 - Part 4: Move mAnimationGeneration to RestyleManager. draft
authorBoris Chiou <boris.chiou@gmail.com>
Fri, 17 Feb 2017 13:47:04 +0800
changeset 486518 75d99eda33f8a4eb96e0595db782758022edad7e
parent 486517 7001a3b36b2b9e433f3d37ff8bc3981863016e41
child 486519 c51c01e68783ed86a8bcc9b376b4ae6ee9f851f5
child 486721 9595e520632264b2ee5899d1f67dfbf01293c2f2
push id46008
push userbmo:boris.chiou@gmail.com
push dateSat, 18 Feb 2017 05:04:48 +0000
bugs1334036
milestone54.0a1
Bug 1334036 - Part 4: Move mAnimationGeneration to RestyleManager. MozReview-Commit-ID: EW6OLRSSTAu
dom/animation/EffectCompositor.cpp
dom/animation/EffectSet.cpp
layout/base/GeckoRestyleManager.cpp
layout/base/GeckoRestyleManager.h
layout/base/RestyleManager.cpp
layout/base/RestyleManager.h
layout/style/AnimationCollection.cpp
layout/style/nsTransitionManager.cpp
--- a/dom/animation/EffectCompositor.cpp
+++ b/dom/animation/EffectCompositor.cpp
@@ -264,25 +264,18 @@ EffectCompositor::RequestRestyle(dom::El
     bool hasPendingRestyle = elementsToRestyle.Get(key);
     if (!hasPendingRestyle) {
       PostRestyleForAnimation(aElement, aPseudoType, aCascadeLevel);
     }
     elementsToRestyle.Put(key, true);
   }
 
   if (aRestyleType == RestyleType::Layer) {
-    // FIXME: Bug 1334036: we call RequestRestyle for both stylo and gecko,
-    // so we should fix this after supporting animations on the compositor.
-    if (mPresContext->RestyleManager()->IsServo()) {
-      NS_WARNING("stylo: RequestRestyle to layer, but Servo-backed style "
-                 "system haven't supported compositor-driven animations yet");
-      return;
-    }
     // Prompt layers to re-sync their animations.
-    mPresContext->RestyleManager()->AsGecko()->IncrementAnimationGeneration();
+    mPresContext->RestyleManager()->IncrementAnimationGeneration();
     EffectSet* effectSet =
       EffectSet::GetEffectSet(aElement, aPseudoType);
     if (effectSet) {
       effectSet->UpdateAnimationGeneration(mPresContext);
     }
   }
 }
 
--- a/dom/animation/EffectSet.cpp
+++ b/dom/animation/EffectSet.cpp
@@ -106,21 +106,18 @@ EffectSet::DestroyEffectSet(dom::Element
   effectSet = nullptr;
 
   aElement->DeleteProperty(propName);
 }
 
 void
 EffectSet::UpdateAnimationGeneration(nsPresContext* aPresContext)
 {
-  MOZ_ASSERT(aPresContext->RestyleManager()->IsGecko(),
-             "stylo: Servo-backed style system should not be using "
-             "EffectSet");
   mAnimationGeneration =
-    aPresContext->RestyleManager()->AsGecko()->GetAnimationGeneration();
+    aPresContext->RestyleManager()->GetAnimationGeneration();
 }
 
 /* static */ nsIAtom**
 EffectSet::GetEffectSetPropertyAtoms()
 {
   static nsIAtom* effectSetPropertyAtoms[] =
     {
       nsGkAtoms::animationEffectsProperty,
--- a/layout/base/GeckoRestyleManager.cpp
+++ b/layout/base/GeckoRestyleManager.cpp
@@ -6,17 +6,16 @@
 /**
  * Code responsible for managing style changes: tracking what style
  * changes need to happen, scheduling them, and doing them.
  */
 
 #include "mozilla/GeckoRestyleManager.h"
 
 #include <algorithm> // For std::max
-#include "mozilla/EffectSet.h"
 #include "mozilla/EventStates.h"
 #include "mozilla/ViewportFrame.h"
 #include "mozilla/css/StyleRule.h" // For nsCSSSelector
 #include "nsLayoutUtils.h"
 #include "AnimationCommon.h" // For GetLayerAnimationInfo
 #include "FrameLayerBuilder.h"
 #include "GeckoProfiler.h"
 #include "LayerAnimationInfo.h" // For LayerAnimationInfo::sRecords
@@ -86,17 +85,16 @@ ElementTagToString(dom::Element* aElemen
 GeckoRestyleManager::GeckoRestyleManager(nsPresContext* aPresContext)
   : RestyleManager(StyleBackendType::Gecko, aPresContext)
   , mDoRebuildAllStyleData(false)
   , mInRebuildAllStyleData(false)
   , mSkipAnimationRules(false)
   , mHavePendingNonAnimationRestyles(false)
   , mRebuildAllExtraHint(nsChangeHint(0))
   , mRebuildAllRestyleHint(nsRestyleHint(0))
-  , mAnimationGeneration(0)
   , mReframingStyleContexts(nullptr)
   , mPendingRestyles(ELEMENT_HAS_PENDING_RESTYLE |
                      ELEMENT_IS_POTENTIAL_RESTYLE_ROOT |
                      ELEMENT_IS_CONDITIONAL_RESTYLE_ANCESTOR)
   , mIsProcessingRestyles(false)
 #ifdef RESTYLE_LOGGING
   , mLoggingDepth(0)
 #endif
@@ -329,23 +327,16 @@ GeckoRestyleManager::AttributeChanged(El
                                            aAttribute,
                                            aModType,
                                            true,
                                            aOldValue,
                                            rsdata);
   PostRestyleEvent(aElement, rshint, hint, &rsdata);
 }
 
-/* static */ uint64_t
-GeckoRestyleManager::GetAnimationGenerationForFrame(nsIFrame* aFrame)
-{
-  EffectSet* effectSet = EffectSet::GetEffectSet(aFrame);
-  return effectSet ? effectSet->GetAnimationGeneration() : 0;
-}
-
 void
 GeckoRestyleManager::RebuildAllStyleData(nsChangeHint aExtraHint,
                                          nsRestyleHint aRestyleHint)
 {
   NS_ASSERTION(!(aExtraHint & nsChangeHint_ReconstructFrame),
                "Should not reconstruct the root of the frame tree.  "
                "Use ReconstructDocElementHierarchy instead.");
   MOZ_ASSERT(!(aRestyleHint & ~(eRestyle_Subtree | eRestyle_ForceDescendants)),
@@ -1197,17 +1188,17 @@ ElementRestyler::ElementRestyler(nsPresC
 #endif
 {
 }
 
 void
 ElementRestyler::AddLayerChangesForAnimation()
 {
   uint64_t frameGeneration =
-    GeckoRestyleManager::GetAnimationGenerationForFrame(mFrame);
+    RestyleManager::GetAnimationGenerationForFrame(mFrame);
 
   nsChangeHint hint = nsChangeHint(0);
   for (const LayerAnimationInfo::Record& layerInfo :
          LayerAnimationInfo::sRecords) {
     Layer* layer =
       FrameLayerBuilder::GetDedicatedLayer(mFrame, layerInfo.mLayerType);
     if (layer && frameGeneration != layer->GetAnimationGeneration()) {
       // If we have a transform layer but don't have any transform style, we
--- a/layout/base/GeckoRestyleManager.h
+++ b/layout/base/GeckoRestyleManager.h
@@ -66,29 +66,17 @@ public:
   // Forwarded nsIMutationObserver method, to handle restyling (and
   // passing the notification to the frame).
   void AttributeChanged(Element* aElement,
                         int32_t  aNameSpaceID,
                         nsIAtom* aAttribute,
                         int32_t  aModType,
                         const nsAttrValue* aOldValue);
 
-  // Get a counter that increments on every style change, that we use to
-  // track whether off-main-thread animations are up-to-date.
-  uint64_t GetAnimationGeneration() const { return mAnimationGeneration; }
-
-  static uint64_t GetAnimationGenerationForFrame(nsIFrame* aFrame);
-
-  // Update the animation generation count to mark that animation state
-  // has changed.
-  //
-  // This is normally performed automatically by ProcessPendingRestyles
-  // but it is also called when we have out-of-band changes to animations
-  // such as changes made through the Web Animations API.
-  void IncrementAnimationGeneration() {
+  void IncrementAnimationGeneration() override {
     // We update the animation generation at start of each call to
     // ProcessPendingRestyles so we should ignore any subsequent (redundant)
     // calls that occur while we are still processing restyles.
     if (!mIsProcessingRestyles) {
       ++mAnimationGeneration;
     }
   }
 
@@ -395,20 +383,16 @@ private:
   bool mInRebuildAllStyleData : 1;
   // Whether rule matching should skip styles associated with animation
   bool mSkipAnimationRules : 1;
   bool mHavePendingNonAnimationRestyles : 1;
 
   nsChangeHint mRebuildAllExtraHint;
   nsRestyleHint mRebuildAllRestyleHint;
 
-  // The total number of animation flushes by this frame constructor.
-  // Used to keep the layer and animation manager in sync.
-  uint64_t mAnimationGeneration;
-
   ReframingStyleContexts* mReframingStyleContexts;
 
   RestyleTracker mPendingRestyles;
 
   // Are we currently in the middle of a call to ProcessRestyles?
   // This flag is used both as a debugging aid to assert that we are not
   // performing nested calls to ProcessPendingRestyles, as well as to ignore
   // redundant calls to IncrementAnimationGeneration.
--- a/layout/base/RestyleManager.cpp
+++ b/layout/base/RestyleManager.cpp
@@ -1,16 +1,18 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/RestyleManager.h"
 #include "mozilla/RestyleManagerInlines.h"
+
+#include "mozilla/EffectSet.h"
 #include "mozilla/StyleSetHandleInlines.h"
 #include "nsIFrame.h"
 #include "nsIPresShellInlines.h"
 
 namespace mozilla {
 
 RestyleManager::RestyleManager(StyleBackendType aType,
                                nsPresContext* aPresContext)
@@ -314,16 +316,23 @@ RestyleManager::ContentRemoved(nsINode* 
       }
       if (content == aFollowingSibling) {
         reachedFollowingSibling = true;
       }
     }
   }
 }
 
+/* static */ uint64_t
+RestyleManager::GetAnimationGenerationForFrame(nsIFrame* aFrame)
+{
+  EffectSet* effectSet = EffectSet::GetEffectSet(aFrame);
+  return effectSet ? effectSet->GetAnimationGeneration() : 0;
+}
+
 /**
  * Calculates the change hint and the restyle hint for a given content state
  * change.
  *
  * This is called from both Restyle managers.
  */
 void
 RestyleManager::ContentStateChangedInternal(Element* aElement,
--- a/layout/base/RestyleManager.h
+++ b/layout/base/RestyleManager.h
@@ -183,16 +183,38 @@ public:
                                   const nsAttrValue* aNewValue);
   inline void AttributeChanged(dom::Element* aElement,
                                int32_t aNameSpaceID,
                                nsIAtom* aAttribute,
                                int32_t aModType,
                                const nsAttrValue* aOldValue);
   inline nsresult ReparentStyleContext(nsIFrame* aFrame);
 
+  // Get a counter that increments on every style change, that we use to
+  // track whether off-main-thread animations are up-to-date.
+  uint64_t GetAnimationGeneration() const { return mAnimationGeneration; }
+
+  static uint64_t GetAnimationGenerationForFrame(nsIFrame* aFrame);
+
+  // Update the animation generation count to mark that animation state
+  // has changed.
+  //
+  // This is normally performed automatically by ProcessPendingRestyles
+  // but it is also called when we have out-of-band changes to animations
+  // such as changes made through the Web Animations API.
+  virtual void IncrementAnimationGeneration()
+  {
+    // We update the animation generation at start of each call to
+    // ProcessPendingRestyles so we should ignore any subsequent (redundant)
+    // calls that occur while we are still processing restyles.
+    if (!IsInStyleRefresh()) {
+      ++mAnimationGeneration;
+    }
+  }
+
 protected:
   RestyleManager(StyleBackendType aType, nsPresContext* aPresContext);
 
   virtual ~RestyleManager()
   {
     MOZ_ASSERT(!mAnimationsWithDestroyedFrame,
                "leaving dangling pointers from AnimationsWithDestroyedFrame");
   }
@@ -268,15 +290,19 @@ protected:
    */
   static nsIFrame*
   GetNextContinuationWithSameStyle(nsIFrame* aFrame,
                                    nsStyleContext* aOldStyleContext,
                                    bool* aHaveMoreContinuations = nullptr);
 
   AnimationsWithDestroyedFrame* mAnimationsWithDestroyedFrame = nullptr;
 
+  // The total number of animation flushes by this frame constructor.
+  // Used to keep the layer and animation manager in sync.
+  uint64_t mAnimationGeneration = 0;
+
   friend class mozilla::GeckoRestyleManager;
   friend class mozilla::ServoRestyleManager;
 };
 
 } // namespace mozilla
 
 #endif
--- a/layout/style/AnimationCollection.cpp
+++ b/layout/style/AnimationCollection.cpp
@@ -133,22 +133,17 @@ AnimationCollection<AnimationType>::Pseu
   }
 }
 
 template <class AnimationType>
 void
 AnimationCollection<AnimationType>::UpdateCheckGeneration(
   nsPresContext* aPresContext)
 {
-  if (aPresContext->RestyleManager()->IsServo()) {
-    // stylo: ServoRestyleManager does not support animations yet.
-    return;
-  }
-  mCheckGeneration =
-    aPresContext->RestyleManager()->AsGecko()->GetAnimationGeneration();
+  mCheckGeneration = aPresContext->RestyleManager()->GetAnimationGeneration();
 }
 
 template<class AnimationType>
 /*static*/ nsIAtom*
 AnimationCollection<AnimationType>::GetPropertyAtomForPseudoType(
   CSSPseudoElementType aPseudoType)
 {
   nsIAtom* propName = nullptr;
--- a/layout/style/nsTransitionManager.cpp
+++ b/layout/style/nsTransitionManager.cpp
@@ -501,17 +501,17 @@ nsTransitionManager::StyleContextChanged
     return;
   }
 
   MOZ_ASSERT(mPresContext->RestyleManager()->IsGecko(),
              "ServoRestyleManager should not use nsTransitionManager "
              "for transitions");
   if (collection &&
       collection->mCheckGeneration ==
-        mPresContext->RestyleManager()->AsGecko()->GetAnimationGeneration()) {
+        mPresContext->RestyleManager()->GetAnimationGeneration()) {
     // When we start a new transition, we immediately post a restyle.
     // If the animation generation on the collection is current, that
     // means *this* is that restyle, since we bump the animation
     // generation on the restyle manager whenever there's a real style
     // change (i.e., one where mInAnimationOnlyStyleUpdate isn't true,
     // which causes us to return above).  Thus we shouldn't do anything.
     return;
   }
@@ -919,17 +919,17 @@ nsTransitionManager::ConsiderInitiatingT
              "for transitions");
 
   RefPtr<CSSTransition> animation =
     new CSSTransition(mPresContext->Document()->GetScopeObject());
   animation->SetOwningElement(
     OwningElementRef(*aElement, aNewStyleContext->GetPseudoType()));
   animation->SetTimelineNoUpdate(timeline);
   animation->SetCreationSequence(
-    mPresContext->RestyleManager()->AsGecko()->GetAnimationGeneration());
+    mPresContext->RestyleManager()->GetAnimationGeneration());
   animation->SetEffectFromStyle(pt);
   animation->PlayFromStyle();
 
   if (!aElementTransitions) {
     bool createdCollection = false;
     aElementTransitions =
       CSSTransitionCollection::GetOrCreateAnimationCollection(
         aElement, aNewStyleContext->GetPseudoType(), &createdCollection);