Bug 1568536 - Simplify some style system APIs. r=heycam
authorEmilio Cobos Álvarez <emilio@crisal.io>
Fri, 16 Aug 2019 05:59:03 +0000
changeset 488510 778ea34e599419af2a4b3dbb24c3d00bfee22acf
parent 488509 b05d7e06eeb73aeab7b3763c314282d78bc4d36f
child 488511 400b06aac33e27a53b1ff0da585cd57035661d4c
push id36446
push usermalexandru@mozilla.com
push dateFri, 16 Aug 2019 21:53:14 +0000
treeherdermozilla-central@1d4db40e38dd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersheycam
bugs1568536
milestone70.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1568536 - Simplify some style system APIs. r=heycam Return a raw pointer instead of a strong reference to a ComputedStyle, and handle the case of the style not being present by returning null rather than requiring an extra function to check it and crashing if the precondition is not met. Also, name them so that it's clear they just return outdated styles and don't make any extra effort. This is just cleanup that makes the next patch easier / more obvious. Differential Revision: https://phabricator.services.mozilla.com/D40080
dom/base/Element.cpp
layout/base/nsCSSFrameConstructor.cpp
servo/ports/geckolib/glue.rs
--- a/dom/base/Element.cpp
+++ b/dom/base/Element.cpp
@@ -4117,29 +4117,26 @@ void Element::AddSizeOfExcludingThis(nsW
     // better distinguish in DMD's output the memory measured within Servo
     // code.
     *aNodeSize += Servo_Element_SizeOfExcludingThisAndCVs(
         ServoElementMallocSizeOf, ServoElementMallocEnclosingSizeOf,
         &aSizes.mState.mSeenPtrs, this);
 
     // Now measure just the ComputedValues (and style structs) under
     // mServoData. This counts towards the relevant fields in |aSizes|.
-    RefPtr<ComputedStyle> sc;
-    if (Servo_Element_HasPrimaryComputedValues(this)) {
-      sc = Servo_Element_GetPrimaryComputedValues(this).Consume();
-      if (!aSizes.mState.HaveSeenPtr(sc.get())) {
-        sc->AddSizeOfIncludingThis(aSizes, &aSizes.mLayoutComputedValuesDom);
+    if (auto* style = Servo_Element_GetMaybeOutOfDateStyle(this)) {
+      if (!aSizes.mState.HaveSeenPtr(style)) {
+        style->AddSizeOfIncludingThis(aSizes, &aSizes.mLayoutComputedValuesDom);
       }
 
       for (size_t i = 0; i < PseudoStyle::kEagerPseudoCount; i++) {
-        if (Servo_Element_HasPseudoComputedValues(this, i)) {
-          sc = Servo_Element_GetPseudoComputedValues(this, i).Consume();
-          if (!aSizes.mState.HaveSeenPtr(sc.get())) {
-            sc->AddSizeOfIncludingThis(aSizes,
-                                       &aSizes.mLayoutComputedValuesDom);
+        if (auto* style = Servo_Element_GetMaybeOutOfDatePseudoStyle(this, i)) {
+          if (!aSizes.mState.HaveSeenPtr(style)) {
+            style->AddSizeOfIncludingThis(aSizes,
+                                          &aSizes.mLayoutComputedValuesDom);
           }
         }
       }
     }
   }
 }
 
 #ifdef DEBUG
--- a/layout/base/nsCSSFrameConstructor.cpp
+++ b/layout/base/nsCSSFrameConstructor.cpp
@@ -4718,22 +4718,25 @@ already_AddRefed<ComputedStyle> nsCSSFra
   MOZ_ASSERT(parent, "Text out of the flattened tree?");
 
   // FIXME(emilio): We can't use ResolveServoStyle properly because this text
   // node can come from non-lazy frame construction, in which case the style we
   // inherit from can indeed be out-of-date. After an eventual XBL removal, this
   // can go. Note that this is not a correctness issue, since we'll restyle
   // later in any case.
   //
-  // Also, this probably doesn't need to be a strong ref...
-  //
   // Do NOT add new callers to this function in this file, ever, or I'll find
   // out.
-  RefPtr<ComputedStyle> parentStyle =
-      Servo_Element_GetPrimaryComputedValues(parent).Consume();
+  //
+  // FIXME(emilio): The const_cast is unfortunate, but it's not worse than what
+  // we did before.
+  auto* parentStyle =
+      const_cast<ComputedStyle*>(Servo_Element_GetMaybeOutOfDateStyle(parent));
+  MOZ_ASSERT(parentStyle,
+             "How are we inserting text frames in an unstyled element?");
   return mPresShell->StyleSet()->ResolveStyleForText(aContent, parentStyle);
 }
 
 // MathML Mod - RBS
 void nsCSSFrameConstructor::FlushAccumulatedBlock(
     nsFrameConstructorState& aState, nsIContent* aContent,
     nsContainerFrame* aParentFrame, nsFrameList& aBlockList,
     nsFrameList& aNewList) {
--- a/servo/ports/geckolib/glue.rs
+++ b/servo/ports/geckolib/glue.rs
@@ -1225,61 +1225,41 @@ pub extern "C" fn Servo_Element_SizeOfEx
         );
         (*data).size_of_excluding_cvs(&mut ops)
     } else {
         0
     }
 }
 
 #[no_mangle]
-pub extern "C" fn Servo_Element_HasPrimaryComputedValues(element: &RawGeckoElement) -> bool {
-    let element = GeckoElement(element);
-    let data = element
-        .borrow_data()
-        .expect("Looking for CVs on unstyled element");
-    data.has_styles()
-}
-
-#[no_mangle]
-pub extern "C" fn Servo_Element_GetPrimaryComputedValues(
+pub extern "C" fn Servo_Element_GetMaybeOutOfDateStyle(
     element: &RawGeckoElement,
-) -> Strong<ComputedValues> {
+) -> *const ComputedValues {
     let element = GeckoElement(element);
-    let data = element
-        .borrow_data()
-        .expect("Getting CVs on unstyled element");
-    data.styles.primary().clone().into()
-}
-
-#[no_mangle]
-pub extern "C" fn Servo_Element_HasPseudoComputedValues(
+    let data = match element.borrow_data() {
+        Some(d) => d,
+        None => return ptr::null(),
+    };
+    &**data.styles.primary() as *const _
+}
+
+#[no_mangle]
+pub extern "C" fn Servo_Element_GetMaybeOutOfDatePseudoStyle(
     element: &RawGeckoElement,
     index: usize,
-) -> bool {
+) -> *const ComputedValues {
     let element = GeckoElement(element);
-    let data = element
-        .borrow_data()
-        .expect("Looking for CVs on unstyled element");
-    data.styles.pseudos.as_array()[index].is_some()
-}
-
-#[no_mangle]
-pub extern "C" fn Servo_Element_GetPseudoComputedValues(
-    element: &RawGeckoElement,
-    index: usize,
-) -> Strong<ComputedValues> {
-    let element = GeckoElement(element);
-    let data = element
-        .borrow_data()
-        .expect("Getting CVs that aren't present");
-    data.styles.pseudos.as_array()[index]
-        .as_ref()
-        .expect("Getting CVs that aren't present")
-        .clone()
-        .into()
+    let data = match element.borrow_data() {
+        Some(d) => d,
+        None => return ptr::null(),
+    };
+    match data.styles.pseudos.as_array()[index].as_ref() {
+        Some(style) => &**style as *const _,
+        None => ptr::null(),
+    }
 }
 
 #[no_mangle]
 pub extern "C" fn Servo_Element_IsDisplayNone(element: &RawGeckoElement) -> bool {
     let element = GeckoElement(element);
     let data = element
         .get_data()
         .expect("Invoking Servo_Element_IsDisplayNone on unstyled element");