Bug 1448665: Simplify the unanimated style setup in nsComputedDOMStyle. r?xidorn draft
authorEmilio Cobos Álvarez <emilio@crisal.io>
Sun, 25 Mar 2018 16:35:53 +0200
changeset 772254 82a24c5e22163446d1cc50736e8581bb333027ba
parent 772253 51b1078b75c404011e9a7e6c074a94e9d05619f7
push id103884
push userbmo:emilio@crisal.io
push dateSun, 25 Mar 2018 14:38:11 +0000
reviewersxidorn
bugs1448665
milestone61.0a1
Bug 1448665: Simplify the unanimated style setup in nsComputedDOMStyle. r?xidorn We never create nsComputedDOMStyle objects with an unanimated computed style, so this can be much simpler. MozReview-Commit-ID: 2NyBoErxRtV
dom/animation/KeyframeEffectReadOnly.cpp
dom/base/nsDOMWindowUtils.cpp
layout/style/ServoStyleSet.cpp
layout/style/ServoStyleSet.h
layout/style/nsComputedDOMStyle.cpp
layout/style/nsComputedDOMStyle.h
--- a/dom/animation/KeyframeEffectReadOnly.cpp
+++ b/dom/animation/KeyframeEffectReadOnly.cpp
@@ -406,17 +406,16 @@ KeyframeEffectReadOnly::EnsureBaseStyle(
 
   if (!aBaseComputedStyle) {
     Element* animatingElement =
       EffectCompositor::GetElementToRestyle(mTarget->mElement,
                                             mTarget->mPseudoType);
     aBaseComputedStyle =
       aPresContext->StyleSet()->AsServo()->GetBaseContextForElement(
           animatingElement,
-          aPresContext,
           aComputedStyle);
   }
   RefPtr<RawServoAnimationValue> baseValue =
     Servo_ComputedValues_ExtractAnimationValue(aBaseComputedStyle,
                                                aProperty.mProperty).Consume();
   mBaseStyleValuesForServo.Put(aProperty.mProperty, baseValue);
 }
 
--- a/dom/base/nsDOMWindowUtils.cpp
+++ b/dom/base/nsDOMWindowUtils.cpp
@@ -2819,25 +2819,24 @@ nsDOMWindowUtils::GetUnanimatedComputedS
   }
 
   nsIPresShell* shell = GetPresShell();
   if (!shell) {
     return NS_ERROR_FAILURE;
   }
 
   RefPtr<nsAtom> pseudo = nsCSSPseudoElements::GetPseudoAtom(aPseudoElement);
-  RefPtr<ComputedStyle> styleContext =
+  RefPtr<ComputedStyle> style =
     nsComputedDOMStyle::GetUnanimatedComputedStyleNoFlush(element, pseudo);
-  if (!styleContext) {
+  if (!style) {
     return NS_ERROR_FAILURE;
   }
 
   RefPtr<RawServoAnimationValue> value =
-    Servo_ComputedValues_ExtractAnimationValue(styleContext->AsServo(),
-                                               propertyID).Consume();
+    Servo_ComputedValues_ExtractAnimationValue(style, propertyID).Consume();
   if (!value) {
     return NS_ERROR_FAILURE;
   }
   Servo_AnimationValue_Serialize(value, propertyID, &aResult);
   return NS_OK;
 }
 
 nsresult
--- a/layout/style/ServoStyleSet.cpp
+++ b/layout/style/ServoStyleSet.cpp
@@ -1236,17 +1236,16 @@ ServoStyleSet::GetAnimationValues(
                            aComputedStyle,
                            mRawSet.get(),
                            &aAnimationValues);
 }
 
 already_AddRefed<ComputedStyle>
 ServoStyleSet::GetBaseContextForElement(
   Element* aElement,
-  nsPresContext* aPresContext,
   const ComputedStyle* aStyle)
 {
   return Servo_StyleSet_GetBaseComputedValuesForElement(mRawSet.get(),
                                                         aElement,
                                                         aStyle,
                                                         &Snapshots()).Consume();
 }
 
--- a/layout/style/ServoStyleSet.h
+++ b/layout/style/ServoStyleSet.h
@@ -367,17 +367,16 @@ public:
 
   nsCSSCounterStyleRule* CounterStyleRuleForName(nsAtom* aName);
 
   // Get all the currently-active font feature values set.
   already_AddRefed<gfxFontFeatureValueSet> BuildFontFeatureValueSet();
 
   already_AddRefed<ComputedStyle>
   GetBaseContextForElement(dom::Element* aElement,
-                           nsPresContext* aPresContext,
                            const ComputedStyle* aStyle);
 
   // Get a ComputedStyle that represents |aStyle|, but as though it additionally
   // matched the rules of the newly added |aAnimaitonaValue|.
   //
   // We use this function to temporarily generate a ComputedStyle for
   // calculating the cumulative change hints.
   //
--- a/layout/style/nsComputedDOMStyle.cpp
+++ b/layout/style/nsComputedDOMStyle.cpp
@@ -66,22 +66,20 @@ using namespace mozilla::dom;
 /*
  * This is the implementation of the readonly CSSStyleDeclaration that is
  * returned by the getComputedStyle() function.
  */
 
 already_AddRefed<nsComputedDOMStyle>
 NS_NewComputedDOMStyle(dom::Element* aElement, const nsAString& aPseudoElt,
                        nsIPresShell* aPresShell,
-                       nsComputedDOMStyle::StyleType aStyleType,
-                       nsComputedDOMStyle::AnimationFlag aFlag)
-{
-  RefPtr<nsComputedDOMStyle> computedStyle;
-  computedStyle = new nsComputedDOMStyle(aElement, aPseudoElt,
-                                         aPresShell, aStyleType, aFlag);
+                       nsComputedDOMStyle::StyleType aStyleType)
+{
+  RefPtr<nsComputedDOMStyle> computedStyle =
+    new nsComputedDOMStyle(aElement, aPseudoElt, aPresShell, aStyleType);
   return computedStyle.forget();
 }
 
 static nsDOMCSSValueList*
 GetROCSSValueList(bool aCommaDelimited)
 {
   return new nsDOMCSSValueList(aCommaDelimited, true);
 }
@@ -312,27 +310,25 @@ ComputedStyleMap::Update()
     }
   }
   mExposedPropertyCount = index;
 }
 
 nsComputedDOMStyle::nsComputedDOMStyle(dom::Element* aElement,
                                        const nsAString& aPseudoElt,
                                        nsIPresShell* aPresShell,
-                                       StyleType aStyleType,
-                                       AnimationFlag aFlag)
+                                       StyleType aStyleType)
   : mDocumentWeak(nullptr)
   , mOuterFrame(nullptr)
   , mInnerFrame(nullptr)
   , mPresShell(nullptr)
   , mStyleType(aStyleType)
   , mComputedStyleGeneration(0)
   , mExposeVisitedStyle(false)
   , mResolvedComputedStyle(false)
-  , mAnimationFlag(aFlag)
 {
   MOZ_ASSERT(aElement && aPresShell);
   MOZ_ASSERT(aPresShell->GetPresContext());
 
   mDocumentWeak = do_GetWeakReference(aPresShell->GetDocument());
   mContent = aElement;
   mPseudo = nsCSSPseudoElements::GetPseudoAtom(aPseudoElt);
 }
@@ -484,22 +480,32 @@ MustReresolveStyle(const mozilla::Comput
 {
   MOZ_ASSERT(aStyle);
 
   // TODO(emilio): We may want to avoid re-resolving pseudo-element styles
   // more often.
   return aStyle->HasPseudoElementData() && !aStyle->GetPseudo();
 }
 
+static inline CSSPseudoElementType
+GetPseudoType(nsAtom* aPseudo)
+{
+  if (!aPseudo) {
+    return CSSPseudoElementType::NotPseudo;
+  }
+  // FIXME(emilio, bug 1433439): The eIgnoreEnabledState thing is dubious.
+  return nsCSSPseudoElements::GetPseudoType(
+    aPseudo, CSSEnabledState::eIgnoreEnabledState);
+}
+
 already_AddRefed<ComputedStyle>
 nsComputedDOMStyle::DoGetComputedStyleNoFlush(Element* aElement,
                                               nsAtom* aPseudo,
                                               nsIPresShell* aPresShell,
-                                              StyleType aStyleType,
-                                              AnimationFlag aAnimationFlag)
+                                              StyleType aStyleType)
 {
   MOZ_ASSERT(aElement, "NULL element");
   // If the content has a pres shell, we must use it.  Otherwise we'd
   // potentially mix rule trees by using the wrong pres shell's style
   // set.  Using the pres shell from the content also means that any
   // content that's actually *in* a document will get the style from the
   // correct document.
   nsIPresShell* presShell = nsContentUtils::GetPresShellForContent(aElement);
@@ -507,32 +513,19 @@ nsComputedDOMStyle::DoGetComputedStyleNo
   if (!presShell) {
     inDocWithShell = false;
     presShell = aPresShell;
     if (!presShell) {
       return nullptr;
     }
   }
 
-  // We do this check to avoid having to add too much special casing of
-  // Servo functions we call to explicitly ignore any element data in
-  // the tree. (See comment in ServoStyleSet::ResolveStyleLazily.)
-  MOZ_RELEASE_ASSERT((aStyleType == eAll && aAnimationFlag == eWithAnimation) ||
-                     !aElement->OwnerDoc()->GetBFCacheEntry(),
-                     "nsComputedDOMStyle doesn't support getting styles without "
-                     "document rules or without animation for documents in the "
-                     "bfcache");
-
-  auto pseudoType = CSSPseudoElementType::NotPseudo;
-  if (aPseudo) {
-    pseudoType = nsCSSPseudoElements::
-      GetPseudoType(aPseudo, CSSEnabledState::eIgnoreEnabledState);
-    if (pseudoType >= CSSPseudoElementType::Count) {
-      return nullptr;
-    }
+  auto pseudoType = GetPseudoType(aPseudo);
+  if (aPseudo && pseudoType >= CSSPseudoElementType::Count) {
+    return nullptr;
   }
 
   // XXX the !aElement->IsHTMLElement(nsGkAtoms::area)
   // check is needed due to bug 135040 (to avoid using
   // mPrimaryFrame). Remove it once that's fixed.
   if (inDocWithShell &&
       aStyleType == eAll &&
       !aElement->IsHTMLElement(nsGkAtoms::area)) {
@@ -544,64 +537,55 @@ nsComputedDOMStyle::DoGetComputedStyleNo
     } else if (!aPseudo) {
       frame = nsLayoutUtils::GetStyleFrame(aElement);
     }
     if (frame) {
       ComputedStyle* result = frame->Style();
       // Don't use the style if it was influenced by pseudo-elements, since then
       // it's not the primary style for this element / pseudo.
       if (!MustReresolveStyle(result)) {
-        // The existing style may have animation styles so check if we need to
-        // remove them.
-        if (aAnimationFlag == eWithoutAnimation) {
-          nsPresContext* presContext = frame->PresContext();
-          Element* elementOrPseudoElement =
-            EffectCompositor::GetElementToRestyle(aElement, pseudoType);
-          if (!elementOrPseudoElement) {
-            return nullptr;
-          }
-          return presContext->StyleSet()->AsServo()->
-            GetBaseContextForElement(elementOrPseudoElement,
-                                     presContext,
-                                     result);
-        }
-
         RefPtr<ComputedStyle> ret = result;
         return ret.forget();
       }
     }
   }
 
   // No frame has been created, or we have a pseudo, or we're looking
   // for the default style, so resolve the style ourselves.
-
-  nsPresContext* presContext = presShell->GetPresContext();
-  if (!presContext) {
-    return nullptr;
-  }
-
   ServoStyleSet* styleSet = presShell->StyleSet()->AsServo();
 
   StyleRuleInclusion rules = aStyleType == eDefaultOnly
                              ? StyleRuleInclusion::DefaultOnly
                              : StyleRuleInclusion::All;
   RefPtr<ComputedStyle> result =
      styleSet->ResolveStyleLazily(aElement, pseudoType, rules);
-  if (aAnimationFlag == eWithAnimation) {
-    return result.forget();
-  }
+  return result.forget();
+}
+
+already_AddRefed<ComputedStyle>
+nsComputedDOMStyle::GetUnanimatedComputedStyleNoFlush(Element* aElement,
+                                                      nsAtom* aPseudo)
+{
+  RefPtr<ComputedStyle> style = GetComputedStyleNoFlush(aElement, aPseudo);
+  if (!style) {
+    return nullptr;
+  }
+
+  auto pseudoType = GetPseudoType(aPseudo);
+  nsIPresShell* shell = aElement->OwnerDoc()->GetShell();
+  MOZ_ASSERT(shell, "How in the world did we get a style a few lines above?");
 
   Element* elementOrPseudoElement =
     EffectCompositor::GetElementToRestyle(aElement, pseudoType);
   if (!elementOrPseudoElement) {
     return nullptr;
   }
-  return styleSet->GetBaseContextForElement(elementOrPseudoElement,
-                                            presContext,
-                                            result);
+
+  return shell->StyleSet()->AsServo()->GetBaseContextForElement(
+    elementOrPseudoElement, style);
 }
 
 nsMargin
 nsComputedDOMStyle::GetAdjustedValuesForBoxSizing()
 {
   // We want the width/height of whatever parts 'width' or 'height' controls,
   // which can be different depending on the value of the 'box-sizing' property.
   const nsStylePosition* stylePos = StylePosition();
@@ -947,18 +931,17 @@ nsComputedDOMStyle::UpdateCurrentStyleSo
 
   if (!mComputedStyle || MustReresolveStyle(mComputedStyle)) {
     // Need to resolve a style.
     RefPtr<ComputedStyle> resolvedComputedStyle =
       DoGetComputedStyleNoFlush(
           mContent->AsElement(),
           mPseudo,
           presShellForContent ? presShellForContent.get() : mPresShell,
-          mStyleType,
-          eWithAnimation);
+          mStyleType);
     if (!resolvedComputedStyle) {
       ClearComputedStyle();
       return;
     }
 
     // No need to re-get the generation, even though GetComputedStyle
     // will flush, since we flushed style at the top of this function.
     // We don't need to check this if we only flushed the parent.
--- a/layout/style/nsComputedDOMStyle.h
+++ b/layout/style/nsComputedDOMStyle.h
@@ -81,59 +81,44 @@ public:
 
   void IndexedGetter(uint32_t aIndex, bool& aFound, nsAString& aPropName) final;
 
   enum StyleType {
     eDefaultOnly, // Only includes UA and user sheets
     eAll // Includes all stylesheets
   };
 
-  enum AnimationFlag {
-    eWithAnimation,
-    eWithoutAnimation,
-  };
-
   nsComputedDOMStyle(mozilla::dom::Element* aElement,
                      const nsAString& aPseudoElt,
                      nsIPresShell* aPresShell,
-                     StyleType aStyleType,
-                     AnimationFlag aFlag = eWithAnimation);
+                     StyleType aStyleType);
 
   virtual nsINode *GetParentObject() override
   {
     return mContent;
   }
 
   static already_AddRefed<mozilla::ComputedStyle>
   GetComputedStyle(mozilla::dom::Element* aElement, nsAtom* aPseudo,
                    StyleType aStyleType = eAll);
 
   static already_AddRefed<mozilla::ComputedStyle>
   GetComputedStyleNoFlush(mozilla::dom::Element* aElement,
                           nsAtom* aPseudo,
                           StyleType aStyleType = eAll)
   {
     return DoGetComputedStyleNoFlush(aElement,
-                                    aPseudo,
-                                    aElement->OwnerDoc()->GetShell(),
-                                    aStyleType,
-                                    eWithAnimation);
+                                     aPseudo,
+                                     aElement->OwnerDoc()->GetShell(),
+                                     aStyleType);
   }
 
   static already_AddRefed<mozilla::ComputedStyle>
   GetUnanimatedComputedStyleNoFlush(mozilla::dom::Element* aElement,
-                                    nsAtom* aPseudo,
-                                    StyleType aStyleType = eAll)
-  {
-    return DoGetComputedStyleNoFlush(aElement,
-                                     aPseudo,
-                                     aElement->OwnerDoc()->GetShell(),
-                                     aStyleType,
-                                     eWithoutAnimation);
-  }
+                                    nsAtom* aPseudo);
 
   // Helper for nsDOMWindowUtils::GetVisitedDependentComputedStyle
   void SetExposeVisitedStyle(bool aExpose) {
     NS_ASSERTION(aExpose != mExposeVisitedStyle, "should always be changing");
     mExposeVisitedStyle = aExpose;
   }
 
 
@@ -182,20 +167,19 @@ private:
   // Helper functions called by UpdateCurrentStyleSources.
   void ClearComputedStyle();
   void SetResolvedComputedStyle(RefPtr<mozilla::ComputedStyle>&& aContext,
                                 uint64_t aGeneration);
   void SetFrameComputedStyle(mozilla::ComputedStyle* aStyle, uint64_t aGeneration);
 
   static already_AddRefed<mozilla::ComputedStyle>
   DoGetComputedStyleNoFlush(mozilla::dom::Element* aElement,
-                           nsAtom* aPseudo,
-                           nsIPresShell* aPresShell,
-                           StyleType aStyleType,
-                           AnimationFlag aAnimationFlag);
+                            nsAtom* aPseudo,
+                            nsIPresShell* aPresShell,
+                            StyleType aStyleType);
 
 #define STYLE_STRUCT(name_, checkdata_cb_)                              \
   const nsStyle##name_ * Style##name_() {                               \
     return mComputedStyle->Style##name_();                               \
   }
 #include "nsStyleStructList.h"
 #undef STYLE_STRUCT
 
@@ -796,28 +780,21 @@ private:
   bool mExposeVisitedStyle;
 
   /**
    * Whether we resolved a style context last time we called
    * UpdateCurrentStyleSources.  Initially false.
    */
   bool mResolvedComputedStyle;
 
-  /**
-   * Whether we include animation rules in the computed style.
-   */
-  AnimationFlag mAnimationFlag;
-
 #ifdef DEBUG
   bool mFlushedPendingReflows;
 #endif
 };
 
 already_AddRefed<nsComputedDOMStyle>
 NS_NewComputedDOMStyle(mozilla::dom::Element* aElement,
                        const nsAString& aPseudoElt,
                        nsIPresShell* aPresShell,
                        nsComputedDOMStyle::StyleType aStyleType =
-                         nsComputedDOMStyle::eAll,
-                       nsComputedDOMStyle::AnimationFlag aFlag =
-                         nsComputedDOMStyle::eWithAnimation);
+                         nsComputedDOMStyle::eAll);
 
 #endif /* nsComputedDOMStyle_h__ */