Improve performance in MarkContainerChainForOverflowRecalcIfNeeded()

The methods to check and set the visual overflow recalc flags
were both doing similar checks, so we were duplicating those calls.
In this change we somehow inline the code of that methods
so we avoid repeating those operations.

This is intended to improve performance in
perf_tests/shadow_dom/v1-small-deep-distribution.html.

BUG=1051342,941180

Change-Id: Ica18dd6d07b9f8bca9c35c9db438fbc09bb4a53c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2062430
Commit-Queue: Manuel Rego <rego@igalia.com>
Reviewed-by: Chris Harrelson <chrishtr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#742546}
diff --git a/third_party/blink/renderer/core/layout/layout_object.cc b/third_party/blink/renderer/core/layout/layout_object.cc
index 8152c30f..09b953c3 100644
--- a/third_party/blink/renderer/core/layout/layout_object.cc
+++ b/third_party/blink/renderer/core/layout/layout_object.cc
@@ -2011,18 +2011,26 @@
                  ? object->Parent()
                  : object->Container();
     if (object) {
-      bool no_changes = true;
-      if (mark_container_chain_layout_overflow_recalc &&
-          !object->SelfNeedsLayoutOverflowRecalc()) {
-        object->SetChildNeedsLayoutOverflowRecalc();
-        no_changes = false;
+      bool already_needs_layout_overflow_recalc = false;
+      if (mark_container_chain_layout_overflow_recalc) {
+        already_needs_layout_overflow_recalc =
+            object->SelfNeedsLayoutOverflowRecalc();
+        if (!already_needs_layout_overflow_recalc)
+          object->SetChildNeedsLayoutOverflowRecalc();
       }
-      if (!object->SelfPaintingLayerNeedsVisualOverflowRecalc()) {
-        object->MarkSelfPaintingLayerForVisualOverflowRecalc();
-        no_changes = false;
+
+      if (object->HasLayer()) {
+        auto* box_model_object = ToLayoutBoxModelObject(object);
+        if (box_model_object->HasSelfPaintingLayer()) {
+          auto* layer = box_model_object->Layer();
+          if (layer->NeedsVisualOverflowRecalc()) {
+            if (already_needs_layout_overflow_recalc)
+              return;
+          } else {
+            layer->SetNeedsVisualOverflowRecalc();
+          }
+        }
       }
-      if (no_changes)
-        return;
     }
 
   } while (object);