| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "config.h" | 5 #include "config.h" |
| 6 #include "core/paint/LayoutObjectDrawingRecorder.h" | 6 #include "core/paint/LayoutObjectDrawingRecorder.h" |
| 7 | 7 |
| 8 #include "core/layout/LayoutTestHelper.h" | 8 #include "core/layout/LayoutTestHelper.h" |
| 9 #include "core/layout/LayoutView.h" | 9 #include "core/layout/LayoutView.h" |
| 10 #include "core/paint/DisplayItemListPaintTest.h" | 10 #include "core/paint/PaintControllerPaintTest.h" |
| 11 #include "core/paint/PaintLayer.h" | 11 #include "core/paint/PaintLayer.h" |
| 12 #include "platform/graphics/GraphicsContext.h" | 12 #include "platform/graphics/GraphicsContext.h" |
| 13 #include "platform/graphics/GraphicsLayer.h" | 13 #include "platform/graphics/GraphicsLayer.h" |
| 14 #include "platform/graphics/paint/DisplayItemList.h" | |
| 15 #include "platform/graphics/paint/DrawingDisplayItem.h" | 14 #include "platform/graphics/paint/DrawingDisplayItem.h" |
| 15 #include "platform/graphics/paint/PaintController.h" |
| 16 #include <gtest/gtest.h> | 16 #include <gtest/gtest.h> |
| 17 | 17 |
| 18 namespace blink { | 18 namespace blink { |
| 19 | 19 |
| 20 using LayoutObjectDrawingRecorderTest = DisplayItemListPaintTest; | 20 using LayoutObjectDrawingRecorderTest = PaintControllerPaintTest; |
| 21 using LayoutObjectDrawingRecorderTestForSlimmingPaintV2 = DisplayItemListPaintTe
stForSlimmingPaintV2; | 21 using LayoutObjectDrawingRecorderTestForSlimmingPaintV2 = PaintControllerPaintTe
stForSlimmingPaintV2; |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 void drawNothing(GraphicsContext& context, const LayoutView& layoutView, PaintPh
ase phase, const LayoutRect& bound) | 25 void drawNothing(GraphicsContext& context, const LayoutView& layoutView, PaintPh
ase phase, const LayoutRect& bound) |
| 26 { | 26 { |
| 27 if (LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(context, layoutV
iew, phase, LayoutPoint())) | 27 if (LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(context, layoutV
iew, phase, LayoutPoint())) |
| 28 return; | 28 return; |
| 29 | 29 |
| 30 LayoutObjectDrawingRecorder drawingRecorder(context, layoutView, phase, boun
d, LayoutPoint()); | 30 LayoutObjectDrawingRecorder drawingRecorder(context, layoutView, phase, boun
d, LayoutPoint()); |
| 31 } | 31 } |
| 32 | 32 |
| 33 void drawRect(GraphicsContext& context, LayoutView& layoutView, PaintPhase phase
, const LayoutRect& bound) | 33 void drawRect(GraphicsContext& context, LayoutView& layoutView, PaintPhase phase
, const LayoutRect& bound) |
| 34 { | 34 { |
| 35 if (LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(context, layoutV
iew, phase, LayoutPoint())) | 35 if (LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(context, layoutV
iew, phase, LayoutPoint())) |
| 36 return; | 36 return; |
| 37 LayoutObjectDrawingRecorder drawingRecorder(context, layoutView, phase, boun
d, LayoutPoint()); | 37 LayoutObjectDrawingRecorder drawingRecorder(context, layoutView, phase, boun
d, LayoutPoint()); |
| 38 IntRect rect(0, 0, 10, 10); | 38 IntRect rect(0, 0, 10, 10); |
| 39 context.drawRect(rect); | 39 context.drawRect(rect); |
| 40 } | 40 } |
| 41 | 41 |
| 42 TEST_F(LayoutObjectDrawingRecorderTest, Nothing) | 42 TEST_F(LayoutObjectDrawingRecorderTest, Nothing) |
| 43 { | 43 { |
| 44 GraphicsContext context(&rootDisplayItemList()); | 44 GraphicsContext context(&rootPaintController()); |
| 45 LayoutRect bound = layoutView().viewRect(); | 45 LayoutRect bound = layoutView().viewRect(); |
| 46 EXPECT_EQ((size_t)0, rootDisplayItemList().displayItems().size()); | 46 EXPECT_EQ((size_t)0, rootPaintController().displayItems().size()); |
| 47 | 47 |
| 48 drawNothing(context, layoutView(), PaintPhaseForeground, bound); | 48 drawNothing(context, layoutView(), PaintPhaseForeground, bound); |
| 49 rootDisplayItemList().commitNewDisplayItems(); | 49 rootPaintController().commitNewDisplayItems(); |
| 50 EXPECT_DISPLAY_LIST(rootDisplayItemList().displayItems(), 1, | 50 EXPECT_DISPLAY_LIST(rootPaintController().displayItems(), 1, |
| 51 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseForeground))); | 51 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseForeground))); |
| 52 EXPECT_FALSE(static_cast<const DrawingDisplayItem&>(rootDisplayItemList().di
splayItems()[0]).picture()); | 52 EXPECT_FALSE(static_cast<const DrawingDisplayItem&>(rootPaintController().di
splayItems()[0]).picture()); |
| 53 } | 53 } |
| 54 | 54 |
| 55 TEST_F(LayoutObjectDrawingRecorderTest, Rect) | 55 TEST_F(LayoutObjectDrawingRecorderTest, Rect) |
| 56 { | 56 { |
| 57 GraphicsContext context(&rootDisplayItemList()); | 57 GraphicsContext context(&rootPaintController()); |
| 58 LayoutRect bound = layoutView().viewRect(); | 58 LayoutRect bound = layoutView().viewRect(); |
| 59 drawRect(context, layoutView(), PaintPhaseForeground, bound); | 59 drawRect(context, layoutView(), PaintPhaseForeground, bound); |
| 60 rootDisplayItemList().commitNewDisplayItems(); | 60 rootPaintController().commitNewDisplayItems(); |
| 61 EXPECT_DISPLAY_LIST(rootDisplayItemList().displayItems(), 1, | 61 EXPECT_DISPLAY_LIST(rootPaintController().displayItems(), 1, |
| 62 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseForeground))); | 62 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseForeground))); |
| 63 } | 63 } |
| 64 | 64 |
| 65 TEST_F(LayoutObjectDrawingRecorderTest, Cached) | 65 TEST_F(LayoutObjectDrawingRecorderTest, Cached) |
| 66 { | 66 { |
| 67 GraphicsContext context(&rootDisplayItemList()); | 67 GraphicsContext context(&rootPaintController()); |
| 68 LayoutRect bound = layoutView().viewRect(); | 68 LayoutRect bound = layoutView().viewRect(); |
| 69 drawNothing(context, layoutView(), PaintPhaseBlockBackground, bound); | 69 drawNothing(context, layoutView(), PaintPhaseBlockBackground, bound); |
| 70 drawRect(context, layoutView(), PaintPhaseForeground, bound); | 70 drawRect(context, layoutView(), PaintPhaseForeground, bound); |
| 71 rootDisplayItemList().commitNewDisplayItems(); | 71 rootPaintController().commitNewDisplayItems(); |
| 72 | 72 |
| 73 EXPECT_DISPLAY_LIST(rootDisplayItemList().displayItems(), 2, | 73 EXPECT_DISPLAY_LIST(rootPaintController().displayItems(), 2, |
| 74 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseBlockBackground)), | 74 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseBlockBackground)), |
| 75 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseForeground))); | 75 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseForeground))); |
| 76 | 76 |
| 77 drawNothing(context, layoutView(), PaintPhaseBlockBackground, bound); | 77 drawNothing(context, layoutView(), PaintPhaseBlockBackground, bound); |
| 78 drawRect(context, layoutView(), PaintPhaseForeground, bound); | 78 drawRect(context, layoutView(), PaintPhaseForeground, bound); |
| 79 | 79 |
| 80 EXPECT_DISPLAY_LIST(rootDisplayItemList().newDisplayItems(), 2, | 80 EXPECT_DISPLAY_LIST(rootPaintController().newDisplayItems(), 2, |
| 81 TestDisplayItem(layoutView(), DisplayItem::drawingTypeToCachedDrawingTyp
e(DisplayItem::paintPhaseToDrawingType(PaintPhaseBlockBackground))), | 81 TestDisplayItem(layoutView(), DisplayItem::drawingTypeToCachedDrawingTyp
e(DisplayItem::paintPhaseToDrawingType(PaintPhaseBlockBackground))), |
| 82 TestDisplayItem(layoutView(), DisplayItem::drawingTypeToCachedDrawingTyp
e(DisplayItem::paintPhaseToDrawingType(PaintPhaseForeground)))); | 82 TestDisplayItem(layoutView(), DisplayItem::drawingTypeToCachedDrawingTyp
e(DisplayItem::paintPhaseToDrawingType(PaintPhaseForeground)))); |
| 83 | 83 |
| 84 rootDisplayItemList().commitNewDisplayItems(); | 84 rootPaintController().commitNewDisplayItems(); |
| 85 | 85 |
| 86 EXPECT_DISPLAY_LIST(rootDisplayItemList().displayItems(), 2, | 86 EXPECT_DISPLAY_LIST(rootPaintController().displayItems(), 2, |
| 87 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseBlockBackground)), | 87 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseBlockBackground)), |
| 88 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseForeground))); | 88 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseForeground))); |
| 89 } | 89 } |
| 90 | 90 |
| 91 template <typename T> | 91 template <typename T> |
| 92 FloatRect drawAndGetCullRect(DisplayItemList& list, const LayoutObject& layoutOb
ject, const T& bounds) | 92 FloatRect drawAndGetCullRect(PaintController& controller, const LayoutObject& la
youtObject, const T& bounds) |
| 93 { | 93 { |
| 94 list.invalidateAll(); | 94 controller.invalidateAll(); |
| 95 { | 95 { |
| 96 // Draw some things which will produce a non-null picture. | 96 // Draw some things which will produce a non-null picture. |
| 97 GraphicsContext context(&list); | 97 GraphicsContext context(&controller); |
| 98 LayoutObjectDrawingRecorder recorder( | 98 LayoutObjectDrawingRecorder recorder( |
| 99 context, layoutObject, DisplayItem::BoxDecorationBackground, bounds,
LayoutPoint()); | 99 context, layoutObject, DisplayItem::BoxDecorationBackground, bounds,
LayoutPoint()); |
| 100 context.drawRect(enclosedIntRect(FloatRect(bounds))); | 100 context.drawRect(enclosedIntRect(FloatRect(bounds))); |
| 101 } | 101 } |
| 102 list.commitNewDisplayItems(); | 102 controller.commitNewDisplayItems(); |
| 103 const auto& drawing = static_cast<const DrawingDisplayItem&>(list.displayIte
ms()[0]); | 103 const auto& drawing = static_cast<const DrawingDisplayItem&>(controller.disp
layItems()[0]); |
| 104 return drawing.picture()->cullRect(); | 104 return drawing.picture()->cullRect(); |
| 105 } | 105 } |
| 106 | 106 |
| 107 TEST_F(LayoutObjectDrawingRecorderTest, CullRectMatchesProvidedClip) | 107 TEST_F(LayoutObjectDrawingRecorderTest, CullRectMatchesProvidedClip) |
| 108 { | 108 { |
| 109 // It's safe for the picture's cull rect to be expanded (though doing so | 109 // It's safe for the picture's cull rect to be expanded (though doing so |
| 110 // excessively may harm performance), but it cannot be contracted. | 110 // excessively may harm performance), but it cannot be contracted. |
| 111 // For now, this test expects the two rects to match completely. | 111 // For now, this test expects the two rects to match completely. |
| 112 // | 112 // |
| 113 // This rect is chosen so that in the x direction, pixel snapping rounds in | 113 // This rect is chosen so that in the x direction, pixel snapping rounds in |
| 114 // the opposite direction to enclosing, and in the y direction, the edges | 114 // the opposite direction to enclosing, and in the y direction, the edges |
| 115 // are exactly on a half-pixel boundary. The numbers chosen map nicely to | 115 // are exactly on a half-pixel boundary. The numbers chosen map nicely to |
| 116 // both float and LayoutUnit, to make equality checking reliable. | 116 // both float and LayoutUnit, to make equality checking reliable. |
| 117 FloatRect rect(20.75, -5.5, 5.375, 10); | 117 FloatRect rect(20.75, -5.5, 5.375, 10); |
| 118 EXPECT_EQ(rect, drawAndGetCullRect(rootDisplayItemList(), layoutView(), rect
)); | 118 EXPECT_EQ(rect, drawAndGetCullRect(rootPaintController(), layoutView(), rect
)); |
| 119 EXPECT_EQ(rect, drawAndGetCullRect(rootDisplayItemList(), layoutView(), Layo
utRect(rect))); | 119 EXPECT_EQ(rect, drawAndGetCullRect(rootPaintController(), layoutView(), Layo
utRect(rect))); |
| 120 } | 120 } |
| 121 | 121 |
| 122 TEST_F(LayoutObjectDrawingRecorderTest, PaintOffsetCache) | 122 TEST_F(LayoutObjectDrawingRecorderTest, PaintOffsetCache) |
| 123 { | 123 { |
| 124 RuntimeEnabledFeatures::setSlimmingPaintOffsetCachingEnabled(true); | 124 RuntimeEnabledFeatures::setSlimmingPaintOffsetCachingEnabled(true); |
| 125 | 125 |
| 126 GraphicsContext context(&rootDisplayItemList()); | 126 GraphicsContext context(&rootPaintController()); |
| 127 LayoutRect bounds = layoutView().viewRect(); | 127 LayoutRect bounds = layoutView().viewRect(); |
| 128 LayoutPoint paintOffset(1, 2); | 128 LayoutPoint paintOffset(1, 2); |
| 129 | 129 |
| 130 rootDisplayItemList().invalidateAll(); | 130 rootPaintController().invalidateAll(); |
| 131 EXPECT_FALSE(LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(context
, layoutView(), PaintPhaseForeground, paintOffset)); | 131 EXPECT_FALSE(LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(context
, layoutView(), PaintPhaseForeground, paintOffset)); |
| 132 { | 132 { |
| 133 LayoutObjectDrawingRecorder drawingRecorder(context, layoutView(), Paint
PhaseForeground, bounds, paintOffset); | 133 LayoutObjectDrawingRecorder drawingRecorder(context, layoutView(), Paint
PhaseForeground, bounds, paintOffset); |
| 134 IntRect rect(0, 0, 10, 10); | 134 IntRect rect(0, 0, 10, 10); |
| 135 context.drawRect(rect); | 135 context.drawRect(rect); |
| 136 } | 136 } |
| 137 | 137 |
| 138 rootDisplayItemList().commitNewDisplayItems(); | 138 rootPaintController().commitNewDisplayItems(); |
| 139 EXPECT_DISPLAY_LIST(rootDisplayItemList().displayItems(), 1, | 139 EXPECT_DISPLAY_LIST(rootPaintController().displayItems(), 1, |
| 140 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseForeground))); | 140 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseForeground))); |
| 141 | 141 |
| 142 // Ensure we cannot use the cache with a new paint offset. | 142 // Ensure we cannot use the cache with a new paint offset. |
| 143 LayoutPoint newPaintOffset(2, 3); | 143 LayoutPoint newPaintOffset(2, 3); |
| 144 EXPECT_FALSE(LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(context
, layoutView(), PaintPhaseForeground, newPaintOffset)); | 144 EXPECT_FALSE(LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(context
, layoutView(), PaintPhaseForeground, newPaintOffset)); |
| 145 | 145 |
| 146 // Test that a new paint offset is recorded. | 146 // Test that a new paint offset is recorded. |
| 147 { | 147 { |
| 148 LayoutObjectDrawingRecorder drawingRecorder(context, layoutView(), Paint
PhaseForeground, bounds, newPaintOffset); | 148 LayoutObjectDrawingRecorder drawingRecorder(context, layoutView(), Paint
PhaseForeground, bounds, newPaintOffset); |
| 149 IntRect rect(0, 0, 10, 10); | 149 IntRect rect(0, 0, 10, 10); |
| 150 context.drawRect(rect); | 150 context.drawRect(rect); |
| 151 } | 151 } |
| 152 | 152 |
| 153 rootDisplayItemList().commitNewDisplayItems(); | 153 rootPaintController().commitNewDisplayItems(); |
| 154 EXPECT_DISPLAY_LIST(rootDisplayItemList().displayItems(), 1, | 154 EXPECT_DISPLAY_LIST(rootPaintController().displayItems(), 1, |
| 155 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseForeground))); | 155 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseForeground))); |
| 156 | 156 |
| 157 // Ensure the old paint offset cannot be used. | 157 // Ensure the old paint offset cannot be used. |
| 158 EXPECT_FALSE(LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(context
, layoutView(), PaintPhaseForeground, paintOffset)); | 158 EXPECT_FALSE(LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(context
, layoutView(), PaintPhaseForeground, paintOffset)); |
| 159 | 159 |
| 160 // Ensure the new paint offset can be used. | 160 // Ensure the new paint offset can be used. |
| 161 EXPECT_TRUE(LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(context,
layoutView(), PaintPhaseForeground, newPaintOffset)); | 161 EXPECT_TRUE(LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(context,
layoutView(), PaintPhaseForeground, newPaintOffset)); |
| 162 rootDisplayItemList().commitNewDisplayItems(); | 162 rootPaintController().commitNewDisplayItems(); |
| 163 EXPECT_DISPLAY_LIST(rootDisplayItemList().displayItems(), 1, | 163 EXPECT_DISPLAY_LIST(rootPaintController().displayItems(), 1, |
| 164 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseForeground))); | 164 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseForeground))); |
| 165 } | 165 } |
| 166 | 166 |
| 167 } // namespace | 167 } // namespace |
| 168 } // namespace blink | 168 } // namespace blink |
| OLD | NEW |