OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/BoxPaintInvalidator.h" | 5 #include "core/paint/BoxPaintInvalidator.h" |
6 | 6 |
7 #include "core/frame/Settings.h" | 7 #include "core/frame/Settings.h" |
8 #include "core/layout/LayoutView.h" | 8 #include "core/layout/LayoutView.h" |
9 #include "core/paint/ObjectPaintInvalidator.h" | 9 #include "core/paint/ObjectPaintInvalidator.h" |
10 #include "core/paint/PaintInvalidator.h" | 10 #include "core/paint/PaintInvalidator.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 } | 58 } |
59 if (delta < 0) { | 59 if (delta < 0) { |
60 return LayoutRect(location.x(), | 60 return LayoutRect(location.x(), |
61 location.y() + newSize.height() - extraHeight, | 61 location.y() + newSize.height() - extraHeight, |
62 oldSize.width(), -delta + extraHeight); | 62 oldSize.width(), -delta + extraHeight); |
63 } | 63 } |
64 return LayoutRect(); | 64 return LayoutRect(); |
65 } | 65 } |
66 | 66 |
67 bool BoxPaintInvalidator::incrementallyInvalidatePaint() { | 67 bool BoxPaintInvalidator::incrementallyInvalidatePaint() { |
68 const LayoutRect& oldRect = m_context.oldBounds.rect; | 68 LayoutRect rightDelta; |
69 const LayoutRect& newRect = m_context.newBounds.rect; | 69 LayoutRect bottomDelta; |
70 DCHECK(oldRect.location() == newRect.location()); | 70 if (m_box.isLayoutView()) { |
71 LayoutRect rightDelta = | 71 // This corresponds to the special case in computePaintInvalidationReason() |
72 computeRightDelta(newRect.location(), oldRect.size(), newRect.size(), 0); | 72 // for LayoutView in non-rootLayerScrolling mode. In rootLayerScrolling |
73 LayoutRect bottomDelta = | 73 // mode, we'll do full paint invalidation (see crbug.com/660156). |
74 computeBottomDelta(newRect.location(), oldRect.size(), newRect.size(), 0); | 74 DCHECK(!RuntimeEnabledFeatures::rootLayerScrollingEnabled()); |
75 | 75 DCHECK(m_context.oldBounds.location() == m_context.newBounds.location()); |
76 if (m_box.styleRef().hasBorder() || m_box.styleRef().hasBackground()) { | 76 rightDelta = computeRightDelta(m_context.newBounds.location(), |
77 LayoutSize oldBorderBoxSize = computePreviousBorderBoxSize(oldRect.size()); | 77 m_context.oldBounds.size(), |
| 78 m_context.newBounds.size(), 0); |
| 79 bottomDelta = computeBottomDelta(m_context.newBounds.location(), |
| 80 m_context.oldBounds.size(), |
| 81 m_context.newBounds.size(), 0); |
| 82 } else { |
| 83 LayoutSize oldBorderBoxSize = |
| 84 computePreviousBorderBoxSize(m_context.oldBounds.size()); |
78 LayoutSize newBorderBoxSize = m_box.size(); | 85 LayoutSize newBorderBoxSize = m_box.size(); |
79 DCHECK(m_context.oldLocation == m_context.newLocation); | 86 DCHECK(m_context.oldLocation == m_context.newLocation); |
80 rightDelta.unite(computeRightDelta(m_context.newLocation, oldBorderBoxSize, | 87 rightDelta = computeRightDelta(m_context.newLocation, oldBorderBoxSize, |
81 newBorderBoxSize, m_box.borderRight())); | 88 newBorderBoxSize, m_box.borderRight()); |
82 bottomDelta.unite(computeBottomDelta(m_context.newLocation, | 89 bottomDelta = computeBottomDelta(m_context.newLocation, oldBorderBoxSize, |
83 oldBorderBoxSize, newBorderBoxSize, | 90 newBorderBoxSize, m_box.borderBottom()); |
84 m_box.borderBottom())); | |
85 } | 91 } |
86 | 92 |
87 if (rightDelta.isEmpty() && bottomDelta.isEmpty()) | 93 if (rightDelta.isEmpty() && bottomDelta.isEmpty()) |
88 return false; | 94 return false; |
89 | 95 |
90 invalidatePaintRectClippedByOldAndNewBounds(rightDelta); | 96 ObjectPaintInvalidator objectPaintInvalidator(m_box); |
91 invalidatePaintRectClippedByOldAndNewBounds(bottomDelta); | 97 objectPaintInvalidator.invalidatePaintUsingContainer( |
| 98 *m_context.paintInvalidationContainer, rightDelta, |
| 99 PaintInvalidationIncremental); |
| 100 objectPaintInvalidator.invalidatePaintUsingContainer( |
| 101 *m_context.paintInvalidationContainer, bottomDelta, |
| 102 PaintInvalidationIncremental); |
92 return true; | 103 return true; |
93 } | 104 } |
94 | 105 |
95 void BoxPaintInvalidator::invalidatePaintRectClippedByOldAndNewBounds( | |
96 const LayoutRect& rect) { | |
97 if (rect.isEmpty()) | |
98 return; | |
99 | |
100 ObjectPaintInvalidator objectPaintInvalidator(m_box); | |
101 LayoutRect rectClippedByOldBounds = | |
102 intersection(rect, m_context.oldBounds.rect); | |
103 LayoutRect rectClippedByNewBounds = | |
104 intersection(rect, m_context.newBounds.rect); | |
105 // Invalidate only once if the clipped rects equal. | |
106 if (rectClippedByOldBounds == rectClippedByNewBounds) { | |
107 objectPaintInvalidator.invalidatePaintUsingContainer( | |
108 *m_context.paintInvalidationContainer, rectClippedByOldBounds, | |
109 PaintInvalidationIncremental); | |
110 return; | |
111 } | |
112 // Invalidate the bigger one if one contains another. Otherwise invalidate | |
113 // both. | |
114 if (!rectClippedByNewBounds.contains(rectClippedByOldBounds)) | |
115 objectPaintInvalidator.invalidatePaintUsingContainer( | |
116 *m_context.paintInvalidationContainer, rectClippedByOldBounds, | |
117 PaintInvalidationIncremental); | |
118 if (!rectClippedByOldBounds.contains(rectClippedByNewBounds)) | |
119 objectPaintInvalidator.invalidatePaintUsingContainer( | |
120 *m_context.paintInvalidationContainer, rectClippedByNewBounds, | |
121 PaintInvalidationIncremental); | |
122 } | |
123 | |
124 PaintInvalidationReason BoxPaintInvalidator::computePaintInvalidationReason() { | 106 PaintInvalidationReason BoxPaintInvalidator::computePaintInvalidationReason() { |
125 PaintInvalidationReason reason = | 107 PaintInvalidationReason reason = |
126 ObjectPaintInvalidatorWithContext(m_box, m_context) | 108 ObjectPaintInvalidatorWithContext(m_box, m_context) |
127 .computePaintInvalidationReason(); | 109 .computePaintInvalidationReason(); |
128 | 110 |
129 if (isImmediateFullPaintInvalidationReason(reason) || | 111 if (isImmediateFullPaintInvalidationReason(reason) || |
130 reason == PaintInvalidationNone) | 112 reason == PaintInvalidationNone) |
131 return reason; | 113 return reason; |
132 | 114 |
133 if (m_box.mayNeedPaintInvalidationAnimatedBackgroundImage() && | 115 if (m_box.mayNeedPaintInvalidationAnimatedBackgroundImage() && |
134 !m_box.backgroundIsKnownToBeObscured()) | 116 !m_box.backgroundIsKnownToBeObscured()) |
135 reason = PaintInvalidationDelayedFull; | 117 reason = PaintInvalidationDelayedFull; |
136 | 118 |
137 // If the current paint invalidation reason is PaintInvalidationDelayedFull, | 119 // If the current paint invalidation reason is PaintInvalidationDelayedFull, |
138 // then this paint invalidation can delayed if the LayoutBox in question is | 120 // then this paint invalidation can delayed if the LayoutBox in question is |
139 // not on-screen. The logic to decide whether this is appropriate exists at | 121 // not on-screen. The logic to decide whether this is appropriate exists at |
140 // the site of the original paint invalidation that chose | 122 // the site of the original paint invalidation that chose |
141 // PaintInvalidationDelayedFull. | 123 // PaintInvalidationDelayedFull. |
142 if (reason == PaintInvalidationDelayedFull) { | 124 if (reason == PaintInvalidationDelayedFull) { |
143 // Do regular full paint invalidation if the object is onscreen. | 125 // Do regular full paint invalidation if the object is onscreen. |
144 return m_box.intersectsVisibleViewport() ? PaintInvalidationFull | 126 return m_box.intersectsVisibleViewport() ? PaintInvalidationFull |
145 : PaintInvalidationDelayedFull; | 127 : PaintInvalidationDelayedFull; |
146 } | 128 } |
147 | 129 |
| 130 DCHECK(reason == PaintInvalidationIncremental); |
| 131 |
148 if (m_box.isLayoutView()) { | 132 if (m_box.isLayoutView()) { |
149 const LayoutView& layoutView = toLayoutView(m_box); | 133 const LayoutView& layoutView = toLayoutView(m_box); |
150 // In normal compositing mode, root background doesn't need to be | 134 // In normal compositing mode, root background doesn't need to be |
151 // invalidated for box changes, because the background always covers the | 135 // invalidated for box changes, because the background always covers the |
152 // whole document rect and clipping is done by | 136 // whole document rect and clipping is done by |
153 // compositor()->m_containerLayer. Also the scrollbars are always | 137 // compositor()->m_containerLayer. Also the scrollbars are always |
154 // composited. There are no other box decoration on the LayoutView thus we | 138 // composited. There are no other box decoration on the LayoutView thus we |
155 // can safely exit here. | 139 // can safely exit here. |
156 if (layoutView.usesCompositing() && | 140 if (layoutView.usesCompositing() && |
157 !RuntimeEnabledFeatures::rootLayerScrollingEnabled()) | 141 !RuntimeEnabledFeatures::rootLayerScrollingEnabled()) |
158 return reason; | 142 return reason; |
159 } | 143 } |
160 | 144 |
161 if (reason == PaintInvalidationIncremental && | |
162 m_context.oldBounds.rect != m_context.newBounds.rect) { | |
163 if (m_context.newBounds.coversExtraPixels || | |
164 m_context.oldBounds.coversExtraPixels) { | |
165 // Incremental invalidation is not applicable because the difference | |
166 // between oldBounds and newBounds may not cover all changed pixels along | |
167 // the edges. | |
168 return PaintInvalidationBoundsChange; | |
169 } | |
170 | |
171 // If the transform is not identity or translation, incremental invalidation | |
172 // is not applicable because the difference between oldBounds and newBounds | |
173 // doesn't cover all area needing invalidation. | |
174 // TODO(crbug.com/426111): Should also consider ancestor transforms | |
175 // since paintInvalidationContainer. Combine this logic into the above | |
176 // boundsCoversExtraPixels logic. | |
177 if (m_context.paintInvalidationContainer != m_box && m_box.hasLayer() && | |
178 m_box.layer()->transform() && | |
179 !m_box.layer()->transform()->isIdentityOrTranslation()) | |
180 return PaintInvalidationBoundsChange; | |
181 } | |
182 | |
183 const ComputedStyle& style = m_box.styleRef(); | 145 const ComputedStyle& style = m_box.styleRef(); |
184 if (style.backgroundLayers().thisOrNextLayersUseContentBox() || | 146 if (style.backgroundLayers().thisOrNextLayersUseContentBox() || |
185 style.maskLayers().thisOrNextLayersUseContentBox() || | 147 style.maskLayers().thisOrNextLayersUseContentBox() || |
186 style.boxSizing() == BoxSizingBorderBox) { | 148 style.boxSizing() == BoxSizingBorderBox) { |
187 if (previousBoxSizesMap().get(&m_box).contentBoxRect != | 149 if (previousBoxSizesMap().get(&m_box).contentBoxRect != |
188 m_box.contentBoxRect()) | 150 m_box.contentBoxRect()) |
189 return PaintInvalidationContentBoxChange; | 151 return PaintInvalidationContentBoxChange; |
190 } | 152 } |
191 | 153 |
192 if (!style.hasBackground() && !style.hasBoxDecorations()) { | |
193 if (reason == PaintInvalidationIncremental && | |
194 m_context.oldBounds.rect != m_context.newBounds.rect && | |
195 m_box.hasNonCompositedScrollbars()) | |
196 return PaintInvalidationBorderBoxChange; | |
197 return reason; | |
198 } | |
199 | |
200 if (style.backgroundLayers().thisOrNextLayersHaveLocalAttachment()) { | 154 if (style.backgroundLayers().thisOrNextLayersHaveLocalAttachment()) { |
201 if (previousBoxSizesMap().get(&m_box).layoutOverflowRect != | 155 if (previousBoxSizesMap().get(&m_box).layoutOverflowRect != |
202 m_box.layoutOverflowRect()) | 156 m_box.layoutOverflowRect()) |
203 return PaintInvalidationLayoutOverflowBoxChange; | 157 return PaintInvalidationLayoutOverflowBoxChange; |
204 } | 158 } |
205 | 159 |
206 LayoutSize oldBorderBoxSize = | 160 LayoutSize oldBorderBoxSize = |
207 computePreviousBorderBoxSize(m_context.oldBounds.rect.size()); | 161 computePreviousBorderBoxSize(m_context.oldBounds.size()); |
208 LayoutSize newBorderBoxSize = m_box.size(); | 162 LayoutSize newBorderBoxSize = m_box.size(); |
| 163 bool borderBoxChanged = oldBorderBoxSize != newBorderBoxSize; |
209 | 164 |
210 if (oldBorderBoxSize == newBorderBoxSize) | 165 if (!borderBoxChanged && m_context.oldBounds == m_context.newBounds) |
211 return reason; | 166 return PaintInvalidationNone; |
212 | 167 |
213 // See another hasNonCompositedScrollbars() callsite above. | 168 // If either border box changed or bounds changed, and old or new border box |
| 169 // doesn't equal old or new bounds, incremental invalidation is not |
| 170 // applicable. This captures the following cases: |
| 171 // - pixel snapping of paint invalidation bounds, |
| 172 // - scale, rotate, skew etc. transforms, |
| 173 // - visual overflows. |
| 174 if (m_context.oldBounds != |
| 175 LayoutRect(m_context.oldLocation, oldBorderBoxSize) || |
| 176 m_context.newBounds != |
| 177 LayoutRect(m_context.newLocation, newBorderBoxSize)) { |
| 178 return borderBoxChanged ? PaintInvalidationBorderBoxChange |
| 179 : PaintInvalidationBoundsChange; |
| 180 } |
| 181 |
| 182 DCHECK(borderBoxChanged); |
| 183 |
214 if (m_box.hasNonCompositedScrollbars()) | 184 if (m_box.hasNonCompositedScrollbars()) |
215 return PaintInvalidationBorderBoxChange; | 185 return PaintInvalidationBorderBoxChange; |
216 | 186 |
217 if (style.hasVisualOverflowingEffect() || style.hasAppearance() || | 187 if (style.hasVisualOverflowingEffect() || style.hasAppearance() || |
218 style.hasFilterInducingProperty() || style.resize() != RESIZE_NONE) | 188 style.hasFilterInducingProperty() || style.resize() != RESIZE_NONE || |
| 189 style.hasMask()) |
219 return PaintInvalidationBorderBoxChange; | 190 return PaintInvalidationBorderBoxChange; |
220 | 191 |
221 if (style.hasBorderRadius()) | 192 if (style.hasBorderRadius()) |
222 return PaintInvalidationBorderBoxChange; | 193 return PaintInvalidationBorderBoxChange; |
223 | 194 |
224 if (oldBorderBoxSize.width() != newBorderBoxSize.width() && | 195 if (oldBorderBoxSize.width() != newBorderBoxSize.width() && |
225 m_box.mustInvalidateBackgroundOrBorderPaintOnWidthChange()) | 196 m_box.mustInvalidateBackgroundOrBorderPaintOnWidthChange()) |
226 return PaintInvalidationBorderBoxChange; | 197 return PaintInvalidationBorderBoxChange; |
227 if (oldBorderBoxSize.height() != newBorderBoxSize.height() && | 198 if (oldBorderBoxSize.height() != newBorderBoxSize.height() && |
228 m_box.mustInvalidateBackgroundOrBorderPaintOnHeightChange()) | 199 m_box.mustInvalidateBackgroundOrBorderPaintOnHeightChange()) |
229 return PaintInvalidationBorderBoxChange; | 200 return PaintInvalidationBorderBoxChange; |
230 | 201 |
231 return reason; | 202 return PaintInvalidationIncremental; |
232 } | 203 } |
233 | 204 |
234 PaintInvalidationReason BoxPaintInvalidator::invalidatePaintIfNeeded() { | 205 PaintInvalidationReason BoxPaintInvalidator::invalidatePaintIfNeeded() { |
235 PaintInvalidationReason reason = computePaintInvalidationReason(); | 206 PaintInvalidationReason reason = computePaintInvalidationReason(); |
236 if (reason == PaintInvalidationIncremental) { | 207 if (reason == PaintInvalidationIncremental) { |
237 if (incrementallyInvalidatePaint()) { | 208 if (incrementallyInvalidatePaint()) { |
238 m_context.paintingLayer->setNeedsRepaint(); | 209 m_context.paintingLayer->setNeedsRepaint(); |
239 m_box.invalidateDisplayItemClients(reason); | 210 m_box.invalidateDisplayItemClients(reason); |
240 } else { | 211 } else { |
241 reason = PaintInvalidationNone; | 212 reason = PaintInvalidationNone; |
(...skipping 13 matching lines...) Expand all Loading... |
255 if (PaintLayerScrollableArea* area = m_box.getScrollableArea()) | 226 if (PaintLayerScrollableArea* area = m_box.getScrollableArea()) |
256 area->invalidatePaintOfScrollControlsIfNeeded(m_context); | 227 area->invalidatePaintOfScrollControlsIfNeeded(m_context); |
257 | 228 |
258 // This is for the next invalidatePaintIfNeeded so must be at the end. | 229 // This is for the next invalidatePaintIfNeeded so must be at the end. |
259 savePreviousBoxSizesIfNeeded(); | 230 savePreviousBoxSizesIfNeeded(); |
260 | 231 |
261 return reason; | 232 return reason; |
262 } | 233 } |
263 | 234 |
264 bool BoxPaintInvalidator::needsToSavePreviousBoxSizes() { | 235 bool BoxPaintInvalidator::needsToSavePreviousBoxSizes() { |
265 LayoutSize paintInvalidationSize = m_context.newBounds.rect.size(); | 236 LayoutSize paintInvalidationSize = m_context.newBounds.size(); |
266 // Don't save old box sizes if the paint rect is empty because we'll | 237 // Don't save old box sizes if the paint rect is empty because we'll |
267 // full invalidate once the paint rect becomes non-empty. | 238 // full invalidate once the paint rect becomes non-empty. |
268 if (paintInvalidationSize.isEmpty()) | 239 if (paintInvalidationSize.isEmpty()) |
269 return false; | 240 return false; |
270 | 241 |
| 242 if (m_box.paintedOutputOfObjectHasNoEffectRegardlessOfSize()) |
| 243 return false; |
| 244 |
271 const ComputedStyle& style = m_box.styleRef(); | 245 const ComputedStyle& style = m_box.styleRef(); |
272 | 246 |
273 // If we use border-box sizing we need to track changes in the size of the | 247 // If we use border-box sizing we need to track changes in the size of the |
274 // content box. | 248 // content box. |
275 if (style.boxSizing() == BoxSizingBorderBox) | 249 if (style.boxSizing() == BoxSizingBorderBox) |
276 return true; | 250 return true; |
277 | 251 |
278 // We need the old box sizes only when the box has background, decorations, or | |
279 // masks. | |
280 // Main LayoutView paints base background, thus interested in box size. | |
281 if (!m_box.isLayoutView() && !style.hasBackground() && | |
282 !style.hasBoxDecorations() && !style.hasMask()) | |
283 return false; | |
284 | |
285 // No need to save old border box size if we can use size of the old paint | 252 // No need to save old border box size if we can use size of the old paint |
286 // rect as the old border box size in the next invalidation. | 253 // rect as the old border box size in the next invalidation. |
287 if (paintInvalidationSize != m_box.size()) | 254 if (paintInvalidationSize != m_box.size()) |
288 return true; | 255 return true; |
289 | 256 |
290 // Background and mask layers can depend on other boxes than border box. See | 257 // Background and mask layers can depend on other boxes than border box. See |
291 // crbug.com/490533 | 258 // crbug.com/490533 |
292 if (style.backgroundLayers().thisOrNextLayersUseContentBox() || | 259 if (style.backgroundLayers().thisOrNextLayersUseContentBox() || |
293 style.backgroundLayers().thisOrNextLayersHaveLocalAttachment() || | 260 style.backgroundLayers().thisOrNextLayersHaveLocalAttachment() || |
294 style.maskLayers().thisOrNextLayersUseContentBox()) | 261 style.maskLayers().thisOrNextLayersUseContentBox()) |
295 return true; | 262 return true; |
296 | 263 |
297 return false; | 264 return false; |
298 } | 265 } |
299 | 266 |
300 void BoxPaintInvalidator::savePreviousBoxSizesIfNeeded() { | 267 void BoxPaintInvalidator::savePreviousBoxSizesIfNeeded() { |
301 if (!needsToSavePreviousBoxSizes()) { | 268 if (!needsToSavePreviousBoxSizes()) { |
302 previousBoxSizesMap().remove(&m_box); | 269 previousBoxSizesMap().remove(&m_box); |
303 return; | 270 return; |
304 } | 271 } |
305 | 272 |
306 PreviousBoxSizes sizes = {m_box.size(), m_box.contentBoxRect(), | 273 PreviousBoxSizes sizes = {m_box.size(), m_box.contentBoxRect(), |
307 m_box.layoutOverflowRect()}; | 274 m_box.layoutOverflowRect()}; |
308 previousBoxSizesMap().set(&m_box, sizes); | 275 previousBoxSizesMap().set(&m_box, sizes); |
309 } | 276 } |
310 | 277 |
311 LayoutSize BoxPaintInvalidator::computePreviousBorderBoxSize( | 278 LayoutSize BoxPaintInvalidator::computePreviousBorderBoxSize( |
312 const LayoutSize& previousBoundsSize) { | 279 const LayoutSize& previousBoundsSize) { |
313 // PreviousBorderBoxSize is only valid when there is background or box | |
314 // decorations. | |
315 DCHECK(m_box.styleRef().hasBackground() || | |
316 m_box.styleRef().hasBoxDecorations()); | |
317 | |
318 auto it = previousBoxSizesMap().find(&m_box); | 280 auto it = previousBoxSizesMap().find(&m_box); |
319 if (it != previousBoxSizesMap().end()) | 281 if (it != previousBoxSizesMap().end()) |
320 return it->value.borderBoxSize; | 282 return it->value.borderBoxSize; |
321 | 283 |
322 // We didn't save the old border box size because it was the same as the size | 284 // We didn't save the old border box size because it was the same as the size |
323 // of oldBounds. | 285 // of oldBounds. |
324 return previousBoundsSize; | 286 return previousBoundsSize; |
325 } | 287 } |
326 | 288 |
327 } // namespace blink | 289 } // namespace blink |
OLD | NEW |