| 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 "core/paint/LayoutObjectDrawingRecorder.h" | 5 #include "core/paint/LayoutObjectDrawingRecorder.h" |
| 6 | 6 |
| 7 #include "core/layout/LayoutTestHelper.h" | 7 #include "core/layout/LayoutTestHelper.h" |
| 8 #include "core/layout/LayoutView.h" | 8 #include "core/layout/LayoutView.h" |
| 9 #include "core/paint/PaintControllerPaintTest.h" | 9 #include "core/paint/PaintControllerPaintTest.h" |
| 10 #include "core/paint/PaintInfo.h" |
| 10 #include "core/paint/PaintLayer.h" | 11 #include "core/paint/PaintLayer.h" |
| 11 #include "platform/graphics/GraphicsContext.h" | 12 #include "platform/graphics/GraphicsContext.h" |
| 12 #include "platform/graphics/GraphicsLayer.h" | 13 #include "platform/graphics/GraphicsLayer.h" |
| 13 #include "platform/graphics/paint/DrawingDisplayItem.h" | 14 #include "platform/graphics/paint/DrawingDisplayItem.h" |
| 14 #include "platform/graphics/paint/PaintController.h" | 15 #include "platform/graphics/paint/PaintController.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 namespace blink { | 18 namespace blink { |
| 18 | 19 |
| 19 using LayoutObjectDrawingRecorderTest = PaintControllerPaintTest; | 20 using LayoutObjectDrawingRecorderTest = PaintControllerPaintTest; |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 void drawNothing(GraphicsContext& context, const LayoutView& layoutView, PaintPh
ase phase, const LayoutRect& bound) | 24 void drawNothing(GraphicsContext& context, const LayoutView& layoutView, PaintPh
ase phase, const LayoutRect& bound) |
| 24 { | 25 { |
| 25 if (LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(context, layoutV
iew, phase, LayoutPoint())) | 26 if (LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(context, layoutV
iew, phase)) |
| 26 return; | 27 return; |
| 27 | 28 |
| 28 LayoutObjectDrawingRecorder drawingRecorder(context, layoutView, phase, boun
d, LayoutPoint()); | 29 LayoutObjectDrawingRecorder drawingRecorder(context, layoutView, phase, boun
d); |
| 29 } | 30 } |
| 30 | 31 |
| 31 void drawRect(GraphicsContext& context, LayoutView& layoutView, PaintPhase phase
, const LayoutRect& bound) | 32 void drawRect(GraphicsContext& context, LayoutView& layoutView, PaintPhase phase
, const LayoutRect& bound) |
| 32 { | 33 { |
| 33 if (LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(context, layoutV
iew, phase, LayoutPoint())) | 34 if (LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(context, layoutV
iew, phase)) |
| 34 return; | 35 return; |
| 35 LayoutObjectDrawingRecorder drawingRecorder(context, layoutView, phase, boun
d, LayoutPoint()); | 36 LayoutObjectDrawingRecorder drawingRecorder(context, layoutView, phase, boun
d); |
| 36 IntRect rect(0, 0, 10, 10); | 37 IntRect rect(0, 0, 10, 10); |
| 37 context.drawRect(rect); | 38 context.drawRect(rect); |
| 38 } | 39 } |
| 39 | 40 |
| 40 TEST_F(LayoutObjectDrawingRecorderTest, Nothing) | 41 TEST_F(LayoutObjectDrawingRecorderTest, Nothing) |
| 41 { | 42 { |
| 42 rootPaintController().invalidateAll(); | 43 rootPaintController().invalidateAll(); |
| 43 GraphicsContext context(rootPaintController()); | 44 GraphicsContext context(rootPaintController()); |
| 44 LayoutRect bound = layoutView().viewRect(); | 45 LayoutRect bound = layoutView().viewRect(); |
| 45 drawNothing(context, layoutView(), PaintPhaseForeground, bound); | 46 drawNothing(context, layoutView(), PaintPhaseForeground, bound); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseForeground))); | 88 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseForeground))); |
| 88 } | 89 } |
| 89 | 90 |
| 90 template <typename T> | 91 template <typename T> |
| 91 FloatRect drawAndGetCullRect(PaintController& controller, const LayoutObject& la
youtObject, const T& bounds) | 92 FloatRect drawAndGetCullRect(PaintController& controller, const LayoutObject& la
youtObject, const T& bounds) |
| 92 { | 93 { |
| 93 controller.invalidateAll(); | 94 controller.invalidateAll(); |
| 94 { | 95 { |
| 95 // Draw some things which will produce a non-null picture. | 96 // Draw some things which will produce a non-null picture. |
| 96 GraphicsContext context(controller); | 97 GraphicsContext context(controller); |
| 97 LayoutObjectDrawingRecorder recorder( | 98 LayoutObjectDrawingRecorder recorder(context, layoutObject, DisplayItem:
:BoxDecorationBackground, bounds); |
| 98 context, layoutObject, DisplayItem::BoxDecorationBackground, bounds,
LayoutPoint()); | |
| 99 context.drawRect(enclosedIntRect(FloatRect(bounds))); | 99 context.drawRect(enclosedIntRect(FloatRect(bounds))); |
| 100 } | 100 } |
| 101 controller.commitNewDisplayItems(); | 101 controller.commitNewDisplayItems(); |
| 102 const auto& drawing = static_cast<const DrawingDisplayItem&>(controller.disp
layItemList()[0]); | 102 const auto& drawing = static_cast<const DrawingDisplayItem&>(controller.disp
layItemList()[0]); |
| 103 return drawing.picture()->cullRect(); | 103 return drawing.picture()->cullRect(); |
| 104 } | 104 } |
| 105 | 105 |
| 106 TEST_F(LayoutObjectDrawingRecorderTest, CullRectMatchesProvidedClip) | 106 TEST_F(LayoutObjectDrawingRecorderTest, CullRectMatchesProvidedClip) |
| 107 { | 107 { |
| 108 // It's safe for the picture's cull rect to be expanded (though doing so | 108 // It's safe for the picture's cull rect to be expanded (though doing so |
| 109 // excessively may harm performance), but it cannot be contracted. | 109 // excessively may harm performance), but it cannot be contracted. |
| 110 // For now, this test expects the two rects to match completely. | 110 // For now, this test expects the two rects to match completely. |
| 111 // | 111 // |
| 112 // This rect is chosen so that in the x direction, pixel snapping rounds in | 112 // This rect is chosen so that in the x direction, pixel snapping rounds in |
| 113 // the opposite direction to enclosing, and in the y direction, the edges | 113 // the opposite direction to enclosing, and in the y direction, the edges |
| 114 // are exactly on a half-pixel boundary. The numbers chosen map nicely to | 114 // are exactly on a half-pixel boundary. The numbers chosen map nicely to |
| 115 // both float and LayoutUnit, to make equality checking reliable. | 115 // both float and LayoutUnit, to make equality checking reliable. |
| 116 FloatRect rect(20.75, -5.5, 5.375, 10); | 116 FloatRect rect(20.75, -5.5, 5.375, 10); |
| 117 EXPECT_EQ(rect, drawAndGetCullRect(rootPaintController(), layoutView(), rect
)); | 117 EXPECT_EQ(rect, drawAndGetCullRect(rootPaintController(), layoutView(), rect
)); |
| 118 EXPECT_EQ(rect, drawAndGetCullRect(rootPaintController(), layoutView(), Layo
utRect(rect))); | 118 EXPECT_EQ(rect, drawAndGetCullRect(rootPaintController(), layoutView(), Layo
utRect(rect))); |
| 119 } | 119 } |
| 120 | 120 |
| 121 TEST_F(LayoutObjectDrawingRecorderTest, PaintOffsetCache) | 121 TEST_F(LayoutObjectDrawingRecorderTest, PaintOffsetCache) |
| 122 { | 122 { |
| 123 RuntimeEnabledFeatures::setSlimmingPaintOffsetCachingEnabled(true); | 123 layoutView().document().lifecycle().advanceTo(DocumentLifecycle::InCompositi
ngUpdate); |
| 124 layoutView().document().lifecycle().advanceTo(DocumentLifecycle::Compositing
Clean); |
| 125 |
| 126 RuntimeEnabledFeatures::setSlimmingPaintSynchronizedPaintingEnabled(true); |
| 127 layoutView().document().lifecycle().advanceTo(DocumentLifecycle::InPaint); |
| 124 | 128 |
| 125 GraphicsContext context(rootPaintController()); | 129 GraphicsContext context(rootPaintController()); |
| 126 LayoutRect bounds = layoutView().viewRect(); | 130 LayoutRect bounds = layoutView().viewRect(); |
| 127 LayoutPoint paintOffset(1, 2); | 131 LayoutPoint paintOffset(1, 2); |
| 132 layoutView().setPreviousPaintOffset(paintOffset); |
| 133 PaintInvalidationState paintInvalidationState(layoutView(), LayoutRect(), 0)
; |
| 134 PaintInfo paintInfo(&context, IntRect(0, 0, 100, 100), PaintPhaseForeground,
0, 0); |
| 135 paintInfo.paintInvalidationState = &paintInvalidationState; |
| 128 | 136 |
| 129 rootPaintController().invalidateAll(); | 137 rootPaintController().invalidateAll(); |
| 130 EXPECT_FALSE(LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(context
, layoutView(), PaintPhaseForeground, paintOffset)); | 138 EXPECT_FALSE(LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(context
, layoutView(), PaintPhaseForeground)); |
| 131 { | 139 { |
| 132 LayoutObjectDrawingRecorder drawingRecorder(context, layoutView(), Paint
PhaseForeground, bounds, paintOffset); | 140 LayoutObjectDrawingRecorder drawingRecorder(context, layoutView(), Paint
PhaseForeground, bounds); |
| 133 IntRect rect(0, 0, 10, 10); | 141 IntRect rect(0, 0, 10, 10); |
| 134 context.drawRect(rect); | 142 context.drawRect(rect); |
| 135 } | 143 } |
| 136 | 144 |
| 137 rootPaintController().commitNewDisplayItems(); | 145 GraphicsLayer* graphicsLayer = layoutView().layer()->graphicsLayerBacking(); |
| 146 rootPaintController().commitNewDisplayItems(graphicsLayer); |
| 138 EXPECT_DISPLAY_LIST(rootPaintController().displayItemList(), 1, | 147 EXPECT_DISPLAY_LIST(rootPaintController().displayItemList(), 1, |
| 139 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseForeground))); | 148 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseForeground))); |
| 140 | 149 |
| 141 // Ensure we cannot use the cache with a new paint offset. | 150 // Ensure we cannot use the cache with a new paint offset. |
| 142 LayoutPoint newPaintOffset(2, 3); | 151 LayoutPoint newPaintOffset(2, 3); |
| 143 EXPECT_FALSE(LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(context
, layoutView(), PaintPhaseForeground, newPaintOffset)); | 152 layoutView().mutableForPainting().invalidatePaintIfNeeded(paintInfo, newPain
tOffset); |
| 153 EXPECT_FALSE(LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(context
, layoutView(), PaintPhaseForeground)); |
| 144 | 154 |
| 145 // Test that a new paint offset is recorded. | 155 // Test that a new paint offset is recorded. |
| 146 { | 156 { |
| 147 LayoutObjectDrawingRecorder drawingRecorder(context, layoutView(), Paint
PhaseForeground, bounds, newPaintOffset); | 157 LayoutObjectDrawingRecorder drawingRecorder(context, layoutView(), Paint
PhaseForeground, bounds); |
| 148 IntRect rect(0, 0, 10, 10); | 158 IntRect rect(0, 0, 10, 10); |
| 149 context.drawRect(rect); | 159 context.drawRect(rect); |
| 150 } | 160 } |
| 151 | 161 |
| 152 rootPaintController().commitNewDisplayItems(); | 162 rootPaintController().commitNewDisplayItems(graphicsLayer); |
| 153 EXPECT_DISPLAY_LIST(rootPaintController().displayItemList(), 1, | 163 EXPECT_DISPLAY_LIST(rootPaintController().displayItemList(), 1, |
| 154 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseForeground))); | 164 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseForeground))); |
| 155 | 165 |
| 156 // Ensure the old paint offset cannot be used. | |
| 157 EXPECT_FALSE(LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(context
, layoutView(), PaintPhaseForeground, paintOffset)); | |
| 158 | |
| 159 // Ensure the new paint offset can be used. | 166 // Ensure the new paint offset can be used. |
| 160 EXPECT_TRUE(LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(context,
layoutView(), PaintPhaseForeground, newPaintOffset)); | 167 layoutView().mutableForPainting().invalidatePaintIfNeeded(paintInfo, newPain
tOffset); |
| 161 rootPaintController().commitNewDisplayItems(); | 168 EXPECT_TRUE(LayoutObjectDrawingRecorder::useCachedDrawingIfPossible(context,
layoutView(), PaintPhaseForeground)); |
| 169 rootPaintController().commitNewDisplayItems(graphicsLayer); |
| 162 EXPECT_DISPLAY_LIST(rootPaintController().displayItemList(), 1, | 170 EXPECT_DISPLAY_LIST(rootPaintController().displayItemList(), 1, |
| 163 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseForeground))); | 171 TestDisplayItem(layoutView(), DisplayItem::paintPhaseToDrawingType(Paint
PhaseForeground))); |
| 164 } | 172 } |
| 165 | 173 |
| 166 } // namespace | 174 } // namespace |
| 167 } // namespace blink | 175 } // namespace blink |
| OLD | NEW |