[Merge 106] Force update of all affected PaintLayers after OverriddenCullRectScope

The cause of the bug is that
- DraggedNodeImageBuilder can create OverriddenCullRectScope with
  a stacking context that doesn't contain all descendants;
- After OverriddenCullRectScope, we only mark the stacking context
  needing cull rect update, while the non-contained descendant will
  miss the update and keep using the cull rect for the drag image.

Now force cull rect update of all affected PaintLayers after
OverriddenCullRectScope.

(cherry picked from commit c11aca756d4bd2124d051900884063752b882c8c)

Bug: 1370030
Change-Id: I19e0d62eb2f2556ed50e77caf9316736ce89cfe9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3933198
Reviewed-by: Philip Rogers <[email protected]>
Commit-Queue: Xianzhu Wang <[email protected]>
Cr-Original-Commit-Position: refs/heads/main@{#1054477}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3975573
Cr-Commit-Position: refs/branch-heads/5249@{#869}
Cr-Branched-From: 4f7bea5de862aaa52e6bde5920755a9ef9db120b-refs/heads/main@{#1036826}
diff --git a/third_party/blink/renderer/core/paint/cull_rect_updater.cc b/third_party/blink/renderer/core/paint/cull_rect_updater.cc
index 661ac3aa..ae73546 100644
--- a/third_party/blink/renderer/core/paint/cull_rect_updater.cc
+++ b/third_party/blink/renderer/core/paint/cull_rect_updater.cc
@@ -21,6 +21,8 @@
 
 namespace {
 
+bool g_is_overriding_cull_rects = false;
+
 void SetLayerNeedsRepaintOnCullRectChange(PaintLayer& layer) {
   if (layer.PreviousPaintResult() == kMayBeClippedByCullRect ||
       RuntimeEnabledFeatures::PaintUnderInvalidationCheckingEnabled()) {
@@ -241,7 +243,10 @@
     UpdateForDescendants(context, layer);
   }
 
-  layer.ClearNeedsCullRectUpdate();
+  if (g_is_overriding_cull_rects)
+    layer.SetNeedsCullRectUpdate();
+  else
+    layer.ClearNeedsCullRectUpdate();
 }
 
 // "Children" in |force_update_children| means children in the containing block
@@ -494,8 +499,7 @@
 }
 
 OverriddenCullRectScope::OverriddenCullRectScope(PaintLayer& starting_layer,
-                                                 const CullRect& cull_rect)
-    : starting_layer_(starting_layer) {
+                                                 const CullRect& cull_rect) {
   if (starting_layer.GetLayoutObject().GetFrame()->IsLocalRoot() &&
       !starting_layer.NeedsCullRectUpdate() &&
       !starting_layer.DescendantNeedsCullRectUpdate() &&
@@ -505,14 +509,15 @@
     return;
   }
 
-  updated_ = true;
+  DCHECK(!g_is_overriding_cull_rects);
+  g_is_overriding_cull_rects = true;
+
   starting_layer.SetNeedsCullRectUpdate();
   CullRectUpdater(starting_layer).UpdateInternal(cull_rect);
 }
 
 OverriddenCullRectScope::~OverriddenCullRectScope() {
-  if (updated_)
-    starting_layer_.SetNeedsCullRectUpdate();
+  g_is_overriding_cull_rects = false;
 }
 
 }  // namespace blink
diff --git a/third_party/blink/renderer/core/paint/cull_rect_updater.h b/third_party/blink/renderer/core/paint/cull_rect_updater.h
index 7a9bce4..e725e655 100644
--- a/third_party/blink/renderer/core/paint/cull_rect_updater.h
+++ b/third_party/blink/renderer/core/paint/cull_rect_updater.h
@@ -84,16 +84,12 @@
 // cull rect when leaving this scope.
 // TODO(crbug.com/1215251): Avoid repaint after the scope if the scope is used
 // to paint into a separate PaintController.
-class OverriddenCullRectScope {
+class CORE_EXPORT OverriddenCullRectScope {
   STACK_ALLOCATED();
 
  public:
   OverriddenCullRectScope(PaintLayer&, const CullRect&);
   ~OverriddenCullRectScope();
-
- private:
-  PaintLayer& starting_layer_;
-  bool updated_ = false;
 };
 
 }  // namespace blink
diff --git a/third_party/blink/renderer/core/paint/paint_layer_painter_test.cc b/third_party/blink/renderer/core/paint/paint_layer_painter_test.cc
index d9064a1..6f3eca7b 100644
--- a/third_party/blink/renderer/core/paint/paint_layer_painter_test.cc
+++ b/third_party/blink/renderer/core/paint/paint_layer_painter_test.cc
@@ -6,6 +6,7 @@
 
 #include "testing/gmock/include/gmock/gmock.h"
 #include "third_party/blink/renderer/core/layout/layout_box_model_object.h"
+#include "third_party/blink/renderer/core/paint/cull_rect_updater.h"
 #include "third_party/blink/renderer/core/paint/paint_controller_paint_test.h"
 #include "third_party/blink/renderer/platform/graphics/graphics_context.h"
 #include "third_party/blink/renderer/platform/testing/find_cc_layer.h"
@@ -749,6 +750,38 @@
   EXPECT_TRUE(html_layer.NeedsPaintPhaseDescendantOutlines());
 }
 
+TEST_P(PaintLayerPainterTest, PaintWithOverriddenCullRect) {
+  SetBodyInnerHTML(R"HTML(
+    <div id="stacking" style="opacity: 0.5; height: 200px;">
+      <div id="absolute" style="position: absolute; height: 200px"></div>
+    </div>
+  )HTML");
+
+  auto& stacking = *GetPaintLayerByElementId("stacking");
+  auto& absolute = *GetPaintLayerByElementId("absolute");
+  EXPECT_EQ(gfx::Rect(0, 0, 800, 600), GetCullRect(stacking).Rect());
+  EXPECT_EQ(gfx::Rect(0, 0, 800, 600), GetCullRect(absolute).Rect());
+  EXPECT_EQ(kFullyPainted, stacking.PreviousPaintResult());
+  EXPECT_EQ(kFullyPainted, absolute.PreviousPaintResult());
+  {
+    OverriddenCullRectScope scope(stacking,
+                                  CullRect(gfx::Rect(0, 0, 100, 100)));
+    EXPECT_EQ(gfx::Rect(0, 0, 100, 100), GetCullRect(stacking).Rect());
+    EXPECT_EQ(gfx::Rect(0, 0, 100, 100), GetCullRect(absolute).Rect());
+    PaintController controller(PaintController::kTransient);
+    GraphicsContext context(controller);
+    PaintLayerPainter(stacking).Paint(context);
+  }
+  UpdateAllLifecyclePhasesForTest();
+  // Should restore the original status after OverridingCullRectScope.
+  EXPECT_EQ(gfx::Rect(0, 0, 800, 600), GetCullRect(stacking).Rect());
+  EXPECT_EQ(gfx::Rect(0, 0, 800, 600), GetCullRect(absolute).Rect());
+  EXPECT_EQ(kFullyPainted, stacking.PreviousPaintResult());
+  EXPECT_EQ(kFullyPainted, absolute.PreviousPaintResult());
+  EXPECT_FALSE(stacking.SelfOrDescendantNeedsRepaint());
+  EXPECT_FALSE(absolute.SelfOrDescendantNeedsRepaint());
+}
+
 class PaintLayerPainterPaintedOutputInvisibleTest
     : public PaintLayerPainterTest {
  protected: