Use updated interest rect when layer needs repainting.

This ensures that repaints don't use a larger rect than necessary, while also ensuring that a new interest rect that is contained within the previous interest rect doesn't cause a repaint on its own.

Currently, the previous interest rect is used if it didn't change too much even when the layer needs repainting, as introduced in https://codereview.chromium.org/1441973003. The original rationale behind this behavior was to ensure consistency between blink and cc. With cc refactorings, this should not be an issue any longer.

BUG=646410
CQ_INCLUDE_TRYBOTS=master.tryserver.chromium.linux:linux_layout_tests_slimming_paint_v2

Review-Url: https://codereview.chromium.org/2332273003
Cr-Commit-Position: refs/heads/master@{#418291}
diff --git a/third_party/WebKit/Source/core/layout/compositing/CompositedLayerMapping.cpp b/third_party/WebKit/Source/core/layout/compositing/CompositedLayerMapping.cpp
index 631edc6..2195dd2 100644
--- a/third_party/WebKit/Source/core/layout/compositing/CompositedLayerMapping.cpp
+++ b/third_party/WebKit/Source/core/layout/compositing/CompositedLayerMapping.cpp
@@ -2406,7 +2406,7 @@
         return wholeLayerRect;
 
     IntRect newInterestRect = recomputeInterestRect(graphicsLayer);
-    if (interestRectChangedEnoughToRepaint(previousInterestRect, newInterestRect, expandedIntSize(graphicsLayer->size())))
+    if (needsRepaint(*graphicsLayer) || interestRectChangedEnoughToRepaint(previousInterestRect, newInterestRect, expandedIntSize(graphicsLayer->size())))
         return newInterestRect;
     return previousInterestRect;
 }
diff --git a/third_party/WebKit/Source/core/layout/compositing/CompositedLayerMappingTest.cpp b/third_party/WebKit/Source/core/layout/compositing/CompositedLayerMappingTest.cpp
index 91e14d1e..5697e82 100644
--- a/third_party/WebKit/Source/core/layout/compositing/CompositedLayerMappingTest.cpp
+++ b/third_party/WebKit/Source/core/layout/compositing/CompositedLayerMappingTest.cpp
@@ -421,6 +421,26 @@
     EXPECT_RECT_EQ(IntRect(0, 0, 800, 6600), previousInterestRect(rootScrollingLayer));
 }
 
+TEST_F(CompositedLayerMappingTest, InterestRectChangeOnShrunkenViewport)
+{
+    setBodyInnerHTML(
+        "<style>"
+        "  ::-webkit-scrollbar { width: 0; height: 0; }"
+        "  body { margin: 0; }"
+        "</style>"
+        "<div id='div' style='width: 100px; height: 10000px'>Text</div>");
+
+    document().view()->updateAllLifecyclePhases();
+    GraphicsLayer* rootScrollingLayer = document().layoutViewItem().layer()->graphicsLayerBackingForScrolling();
+    EXPECT_RECT_EQ(IntRect(0, 0, 800, 4600), previousInterestRect(rootScrollingLayer));
+
+    document().view()->setFrameRect(IntRect(0, 0, 800, 60));
+    document().view()->updateAllLifecyclePhases();
+    // Repaint required, so interest rect should be updated to shrunken size.
+    EXPECT_RECT_EQ(IntRect(0, 0, 800, 4060), recomputeInterestRect(rootScrollingLayer));
+    EXPECT_RECT_EQ(IntRect(0, 0, 800, 4060), previousInterestRect(rootScrollingLayer));
+}
+
 TEST_F(CompositedLayerMappingTest, InterestRectChangeOnScroll)
 {
     document().frame()->settings()->setPreferCompositingToLCDTextEnabled(true);
@@ -469,7 +489,7 @@
     EXPECT_RECT_EQ(IntRect(0, 0, 400, 6600), previousInterestRect(scrollingLayer));
 }
 
-TEST_F(CompositedLayerMappingTest, InterestRectShouldNotChangeOnPaintInvalidation)
+TEST_F(CompositedLayerMappingTest, InterestRectShouldChangeOnPaintInvalidation)
 {
     document().frame()->settings()->setPreferCompositingToLCDTextEnabled(true);
 
@@ -494,11 +514,11 @@
     EXPECT_RECT_EQ(IntRect(0, 5400, 400, 4600), recomputeInterestRect(scrollingLayer));
     EXPECT_RECT_EQ(IntRect(0, 1400, 400, 8600), previousInterestRect(scrollingLayer));
 
-    // Paint invalidation and repaint should not change previous paint interest rect.
+    // Paint invalidation and repaint should change previous paint interest rect.
     document().getElementById("content")->setTextContent("Change");
     document().view()->updateAllLifecyclePhases();
     EXPECT_RECT_EQ(IntRect(0, 5400, 400, 4600), recomputeInterestRect(scrollingLayer));
-    EXPECT_RECT_EQ(IntRect(0, 1400, 400, 8600), previousInterestRect(scrollingLayer));
+    EXPECT_RECT_EQ(IntRect(0, 5400, 400, 4600), previousInterestRect(scrollingLayer));
 }
 
 TEST_F(CompositedLayerMappingTest, InterestRectOfSquashingLayerWithNegativeOverflow)