1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_PAINT_PAINT_CONTROLLER_PAINT_TEST_H_
6 #define THIRD_PARTY_BLINK_RENDERER_CORE_PAINT_PAINT_CONTROLLER_PAINT_TEST_H_
7 
8 #include "third_party/blink/renderer/core/editing/frame_selection.h"
9 #include "third_party/blink/renderer/core/frame/local_frame_view.h"
10 #include "third_party/blink/renderer/core/layout/layout_view.h"
11 #include "third_party/blink/renderer/core/paint/paint_layer.h"
12 #include "third_party/blink/renderer/core/paint/paint_layer_scrollable_area.h"
13 #include "third_party/blink/renderer/core/testing/core_unit_test_helper.h"
14 #include "third_party/blink/renderer/platform/graphics/graphics_context.h"
15 #include "third_party/blink/renderer/platform/graphics/graphics_layer.h"
16 #include "third_party/blink/renderer/platform/graphics/paint/cull_rect.h"
17 #include "third_party/blink/renderer/platform/graphics/paint/paint_controller_test.h"
18 #include "third_party/blink/renderer/platform/testing/paint_test_configurations.h"
19 
20 namespace blink {
21 
22 class PaintControllerPaintTestBase : public RenderingTest {
23  public:
24   PaintControllerPaintTestBase(LocalFrameClient* local_frame_client = nullptr)
RenderingTest(local_frame_client)25       : RenderingTest(local_frame_client) {}
26 
27  protected:
GetLayoutView()28   LayoutView& GetLayoutView() const { return *GetDocument().GetLayoutView(); }
RootPaintController()29   PaintController& RootPaintController() const {
30     if (RuntimeEnabledFeatures::CompositeAfterPaintEnabled())
31       return *GetDocument().View()->GetPaintController();
32     return GetLayoutView()
33         .Layer()
34         ->GraphicsLayerBacking()
35         ->GetPaintController();
36   }
37 
SetUp()38   void SetUp() override {
39     EnableCompositing();
40     RenderingTest::SetUp();
41   }
42 
43   bool PaintWithoutCommit(
44       const base::Optional<IntRect>& interest_rect = base::nullopt) {
45     GetDocument().View()->Lifecycle().AdvanceTo(DocumentLifecycle::kInPaint);
46     if (RuntimeEnabledFeatures::CompositeAfterPaintEnabled()) {
47       if (GetLayoutView().Layer()->SelfOrDescendantNeedsRepaint()) {
48         GraphicsContext graphics_context(RootPaintController());
49         GetDocument().View()->Paint(
50             graphics_context, kGlobalPaintNormalPhase,
51             interest_rect ? CullRect(*interest_rect) : CullRect::Infinite());
52         return true;
53       }
54       GetDocument().View()->Lifecycle().AdvanceTo(
55           DocumentLifecycle::kPaintClean);
56       return false;
57     }
58     // Only root graphics layer is supported.
59     if (!GetLayoutView()
60              .Layer()
61              ->GraphicsLayerBacking()
62              ->PaintWithoutCommitForTesting(interest_rect)) {
63       GetDocument().View()->Lifecycle().AdvanceTo(
64           DocumentLifecycle::kPaintClean);
65       return false;
66     }
67     return true;
68   }
69 
ViewScrollingBackgroundClient()70   const DisplayItemClient& ViewScrollingBackgroundClient() {
71     return GetLayoutView()
72         .GetScrollableArea()
73         ->GetScrollingBackgroundDisplayItemClient();
74   }
75 
CommitAndFinishCycle()76   void CommitAndFinishCycle() {
77     // Only root graphics layer is supported.
78     RootPaintController().CommitNewDisplayItems();
79     RootPaintController().FinishCycle();
80     GetDocument().View()->Lifecycle().AdvanceTo(DocumentLifecycle::kPaintClean);
81   }
82 
83   void Paint(const base::Optional<IntRect>& interest_rect = base::nullopt) {
84     // Only root graphics layer is supported.
85     if (PaintWithoutCommit(interest_rect))
86       CommitAndFinishCycle();
87   }
88 
DisplayItemListContains(const DisplayItemList & display_item_list,DisplayItemClient & client,DisplayItem::Type type)89   bool DisplayItemListContains(const DisplayItemList& display_item_list,
90                                DisplayItemClient& client,
91                                DisplayItem::Type type) const {
92     for (auto& item : display_item_list) {
93       if (item.Client() == client && item.GetType() == type)
94         return true;
95     }
96     return false;
97   }
98 
NumCachedNewItems()99   int NumCachedNewItems() const {
100     return RootPaintController().num_cached_new_items_;
101   }
102 
CaretDisplayItemClientForTesting()103   const DisplayItemClient& CaretDisplayItemClientForTesting() const {
104     return GetDocument()
105         .GetFrame()
106         ->Selection()
107         .CaretDisplayItemClientForTesting();
108   }
109 
InvalidateAll(PaintController & paint_controller)110   void InvalidateAll(PaintController& paint_controller) {
111     paint_controller.InvalidateAllForTesting();
112     if (RuntimeEnabledFeatures::CompositeAfterPaintEnabled()) {
113       DCHECK_EQ(&paint_controller, GetDocument().View()->GetPaintController());
114       GetLayoutView().Layer()->SetNeedsRepaint();
115     }
116   }
117 
ClientCacheIsValid(const DisplayItemClient & client)118   bool ClientCacheIsValid(const DisplayItemClient& client) {
119     return RootPaintController().ClientCacheIsValid(client);
120   }
121 
122   using SubsequenceMarkers = PaintController::SubsequenceMarkers;
GetSubsequenceMarkers(const DisplayItemClient & client)123   SubsequenceMarkers* GetSubsequenceMarkers(const DisplayItemClient& client) {
124     return RootPaintController().GetSubsequenceMarkers(client);
125   }
126 };
127 
128 class PaintControllerPaintTest : public PaintTestConfigurations,
129                                  public PaintControllerPaintTestBase {
130  public:
131   PaintControllerPaintTest(LocalFrameClient* local_frame_client = nullptr)
PaintControllerPaintTestBase(local_frame_client)132       : PaintControllerPaintTestBase(local_frame_client) {}
133 };
134 
135 // Shorter names for frequently used display item types in core/ tests.
136 const DisplayItem::Type kNonScrollingBackgroundChunkType =
137     DisplayItem::PaintPhaseToDrawingType(PaintPhase::kSelfBlockBackgroundOnly);
138 const DisplayItem::Type kScrollingBackgroundChunkType =
139     DisplayItem::PaintPhaseToClipType(PaintPhase::kSelfBlockBackgroundOnly);
140 const DisplayItem::Type kClippedContentsBackgroundChunkType =
141     DisplayItem::PaintPhaseToClipType(
142         PaintPhase::kDescendantBlockBackgroundsOnly);
143 
144 }  // namespace blink
145 
146 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_PAINT_PAINT_CONTROLLER_PAINT_TEST_H_
147