Refactor "needs layout" check in LayoutImage

Move the conditions used to check if layout is needed when the intrinsic
size changes out into a new function, and call that from
LayoutImage::InvalidatePaintAndMarkForLayoutIfNeeded.
Replace open-coded HasRelativeLogicalWidth() with a call to that method.

Bug: 870599
Change-Id: Iae480326083eb493d58233378d95af8c1dd62f0a
Reviewed-on: https://chromium-review.googlesource.com/c/1373839
Commit-Queue: Fredrik Söderquist <fs@opera.com>
Reviewed-by: Morten Stenshorne <mstensho@chromium.org>
Cr-Commit-Position: refs/heads/master@{#616624}
diff --git a/third_party/blink/renderer/core/layout/layout_image.cc b/third_party/blink/renderer/core/layout/layout_image.cc
index 44a4204..769df9a 100644
--- a/third_party/blink/renderer/core/layout/layout_image.cc
+++ b/third_party/blink/renderer/core/layout/layout_image.cc
@@ -210,6 +210,21 @@
   SetIntrinsicSize(new_size);
 }
 
+bool LayoutImage::NeedsLayoutOnIntrinsicSizeChange() const {
+  // If the actual area occupied by the image has changed and it is not
+  // constrained by style then a layout is required.
+  bool image_size_is_constrained = StyleRef().LogicalWidth().IsSpecified() &&
+                                   StyleRef().LogicalHeight().IsSpecified();
+  if (!image_size_is_constrained)
+    return true;
+
+  // FIXME: We only need to recompute the containing block's preferred size if
+  // the containing block's size depends on the image's size (i.e., the
+  // container uses shrink-to-fit sizing). There's no easy way to detect that
+  // shrink-to-fit is needed, always force a layout.
+  return HasRelativeLogicalWidth();
+}
+
 void LayoutImage::InvalidatePaintAndMarkForLayoutIfNeeded(
     CanDeferInvalidation defer) {
   LayoutSize old_intrinsic_size = IntrinsicSize();
@@ -225,30 +240,14 @@
   if (!ContainingBlock())
     return;
 
-  bool image_source_has_changed_size = old_intrinsic_size != new_intrinsic_size;
-  if (image_source_has_changed_size)
+  if (old_intrinsic_size != new_intrinsic_size) {
     SetPreferredLogicalWidthsDirty();
 
-  // If the actual area occupied by the image has changed and it is not
-  // constrained by style then a layout is required.
-  bool image_size_is_constrained = StyleRef().LogicalWidth().IsSpecified() &&
-                                   StyleRef().LogicalHeight().IsSpecified();
-
-  // FIXME: We only need to recompute the containing block's preferred size if
-  // the containing block's size depends on the image's size (i.e., the
-  // container uses shrink-to-fit sizing). There's no easy way to detect that
-  // shrink-to-fit is needed, always force a layout.
-  bool containing_block_needs_to_recompute_preferred_size =
-      StyleRef().LogicalWidth().IsPercentOrCalc() ||
-      StyleRef().LogicalMaxWidth().IsPercentOrCalc() ||
-      StyleRef().LogicalMinWidth().IsPercentOrCalc();
-
-  if (image_source_has_changed_size &&
-      (!image_size_is_constrained ||
-       containing_block_needs_to_recompute_preferred_size)) {
-    SetNeedsLayoutAndFullPaintInvalidation(
-        layout_invalidation_reason::kSizeChanged);
-    return;
+    if (NeedsLayoutOnIntrinsicSizeChange()) {
+      SetNeedsLayoutAndFullPaintInvalidation(
+          layout_invalidation_reason::kSizeChanged);
+      return;
+    }
   }
 
   SetShouldDoFullPaintInvalidationWithoutGeometryChange(
diff --git a/third_party/blink/renderer/core/layout/layout_image.h b/third_party/blink/renderer/core/layout/layout_image.h
index 7db612e..a1827913 100644
--- a/third_party/blink/renderer/core/layout/layout_image.h
+++ b/third_party/blink/renderer/core/layout/layout_image.h
@@ -136,6 +136,7 @@
 
   void InvalidatePaintAndMarkForLayoutIfNeeded(CanDeferInvalidation);
   void UpdateIntrinsicSizeIfNeeded(const LayoutSize&);
+  bool NeedsLayoutOnIntrinsicSizeChange() const;
   // Override intrinsic sizing info by HTMLImageElement "intrinsicsize"
   // attribute if enabled and exists.
   bool OverrideIntrinsicSizingInfo(IntrinsicSizingInfo&) const;