1 /* 2 * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved. 3 * 4 * Portions are Copyright (C) 1998 Netscape Communications Corporation. 5 * 6 * Other contributors: 7 * Robert O'Callahan <roc+@cs.cmu.edu> 8 * David Baron <dbaron (at) fas.harvard.edu> 9 * Christian Biesinger <cbiesinger (at) web.de> 10 * Randall Jesup <rjesup (at) wgate.com> 11 * Roland Mainz <roland.mainz (at) informatik.med.uni-giessen.de> 12 * Josh Soref <timeless (at) mac.com> 13 * Boris Zbarsky <bzbarsky (at) mit.edu> 14 * 15 * This library is free software; you can redistribute it and/or 16 * modify it under the terms of the GNU Lesser General Public 17 * License as published by the Free Software Foundation; either 18 * version 2.1 of the License, or (at your option) any later version. 19 * 20 * This library is distributed in the hope that it will be useful, 21 * but WITHOUT ANY WARRANTY; without even the implied warranty of 22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 23 * Lesser General Public License for more details. 24 * 25 * You should have received a copy of the GNU Lesser General Public 26 * License along with this library; if not, write to the Free Software 27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 28 * 29 * Alternatively, the contents of this file may be used under the terms 30 * of either the Mozilla Public License Version 1.1, found at 31 * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public 32 * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html 33 * (the "GPL"), in which case the provisions of the MPL or the GPL are 34 * applicable instead of those above. If you wish to allow use of your 35 * version of this file only under the terms of one of those two 36 * licenses (the MPL or the GPL) and not to allow others to use your 37 * version of this file under the LGPL, indicate your decision by 38 * deletingthe provisions above and replace them with the notice and 39 * other provisions required by the MPL or the GPL, as the case may be. 40 * If you do not delete the provisions above, a recipient may use your 41 * version of this file under any of the LGPL, the MPL or the GPL. 42 */ 43 44 #include "config.h" 45 #include "core/rendering/RenderLayerRepainter.h" 46 47 #include "core/rendering/FilterEffectRenderer.h" 48 #include "core/rendering/RenderLayer.h" 49 #include "core/rendering/RenderView.h" 50 #include "core/rendering/compositing/CompositedLayerMapping.h" 51 52 namespace WebCore { 53 54 RenderLayerRepainter::RenderLayerRepainter(RenderLayerModelObject& renderer) 55 : m_renderer(renderer) 56 , m_repaintStatus(NeedsNormalRepaint) 57 { 58 } 59 60 void RenderLayerRepainter::repaintAfterLayout(bool shouldCheckForRepaint) 61 { 62 if (RuntimeEnabledFeatures::repaintAfterLayoutEnabled()) 63 return; 64 65 // FIXME: really, we're in the repaint phase here, and the following queries are legal. 66 // Until those states are fully fledged, I'll just disable the ASSERTS. 67 DisableCompositingQueryAsserts disabler; 68 if (m_renderer.layer()->hasVisibleContent()) { 69 RenderView* view = m_renderer.view(); 70 ASSERT(view); 71 // FIXME: LayoutState does not work with RenderLayers as there is not a 1-to-1 72 // mapping between them and the RenderObjects. It would be neat to enable 73 // LayoutState outside the layout() phase and use it here. 74 ASSERT(!view->layoutStateCachedOffsetsEnabled()); 75 76 const RenderLayerModelObject* repaintContainer = m_renderer.containerForPaintInvalidation(); 77 LayoutRect oldRepaintRect = m_repaintRect; 78 LayoutPoint oldOffset = m_offset; 79 computeRepaintRects(); 80 shouldCheckForRepaint &= shouldRepaintLayer(); 81 82 if (shouldCheckForRepaint) { 83 if (view && !view->document().printing()) { 84 if (m_repaintStatus & NeedsFullRepaint) { 85 m_renderer.invalidatePaintUsingContainer(repaintContainer, pixelSnappedIntRect(oldRepaintRect), InvalidationLayer); 86 if (m_repaintRect != oldRepaintRect) 87 m_renderer.invalidatePaintUsingContainer(repaintContainer, pixelSnappedIntRect(m_repaintRect), InvalidationLayer); 88 } else { 89 m_renderer.invalidatePaintAfterLayoutIfNeeded(repaintContainer, m_renderer.selfNeedsLayout(), oldRepaintRect, oldOffset, &m_repaintRect, &m_offset); 90 } 91 } 92 } 93 } else { 94 clearRepaintRects(); 95 } 96 97 m_repaintStatus = NeedsNormalRepaint; 98 99 } 100 101 void RenderLayerRepainter::clearRepaintRects() 102 { 103 ASSERT(!m_renderer.layer()->hasVisibleContent()); 104 105 m_repaintRect = IntRect(); 106 } 107 108 void RenderLayerRepainter::computeRepaintRects() 109 { 110 const RenderLayerModelObject* repaintContainer = m_renderer.containerForPaintInvalidation(); 111 LayoutRect repaintRect = m_renderer.boundsRectForPaintInvalidation(repaintContainer); 112 if (RuntimeEnabledFeatures::repaintAfterLayoutEnabled()) { 113 // FIXME: We want RenderLayerRepainter to go away when 114 // repaint-after-layout is on by default so we need to figure out how to 115 // handle this update. 116 m_renderer.setPreviousPaintInvalidationRect(repaintRect); 117 } else { 118 m_repaintRect = repaintRect; 119 m_offset = RenderLayer::positionFromPaintInvalidationContainer(&m_renderer, repaintContainer); 120 } 121 } 122 123 void RenderLayerRepainter::computeRepaintRectsIncludingNonCompositingDescendants() 124 { 125 // FIXME: computeRepaintRects() has to walk up the parent chain for every layer to compute the rects. 126 // We should make this more efficient. 127 // FIXME: it's wrong to call this when layout is not up-to-date, which we do. 128 computeRepaintRects(); 129 130 for (RenderLayer* layer = m_renderer.layer()->firstChild(); layer; layer = layer->nextSibling()) { 131 if (layer->compositingState() != PaintsIntoOwnBacking && layer->compositingState() != PaintsIntoGroupedBacking) 132 layer->repainter().computeRepaintRectsIncludingNonCompositingDescendants(); 133 } 134 } 135 136 inline bool RenderLayerRepainter::shouldRepaintLayer() const 137 { 138 if (RuntimeEnabledFeatures::repaintAfterLayoutEnabled()) 139 return false; 140 141 if (m_repaintStatus != NeedsFullRepaintForPositionedMovementLayout) 142 return true; 143 144 // Composited layers that were moved during a positioned movement only 145 // layout, don't need to be repainted. They just need to be recomposited. 146 return m_renderer.compositingState() != PaintsIntoOwnBacking; 147 } 148 149 // Since we're only painting non-composited layers, we know that they all share the same repaintContainer. 150 void RenderLayerRepainter::repaintIncludingNonCompositingDescendants() 151 { 152 repaintIncludingNonCompositingDescendantsInternal(m_renderer.containerForPaintInvalidation()); 153 } 154 155 void RenderLayerRepainter::repaintIncludingNonCompositingDescendantsInternal(const RenderLayerModelObject* repaintContainer) 156 { 157 m_renderer.invalidatePaintUsingContainer(repaintContainer, pixelSnappedIntRect(m_renderer.boundsRectForPaintInvalidation(repaintContainer)), InvalidationLayer); 158 159 // FIXME: Repaints can be issued during style recalc at present, via RenderLayerModelObject::styleWillChange. This happens in scenarios when 160 // repaint is needed but not layout. 161 DisableCompositingQueryAsserts disabler; 162 163 for (RenderLayer* curr = m_renderer.layer()->firstChild(); curr; curr = curr->nextSibling()) { 164 if (curr->compositingState() != PaintsIntoOwnBacking && curr->compositingState() != PaintsIntoGroupedBacking) 165 curr->repainter().repaintIncludingNonCompositingDescendantsInternal(repaintContainer); 166 } 167 } 168 169 LayoutRect RenderLayerRepainter::repaintRectIncludingNonCompositingDescendants() const 170 { 171 LayoutRect repaintRect; 172 if (RuntimeEnabledFeatures::repaintAfterLayoutEnabled()) 173 repaintRect = m_renderer.previousPaintInvalidationRect(); 174 else 175 repaintRect = m_repaintRect; 176 177 for (RenderLayer* child = m_renderer.layer()->firstChild(); child; child = child->nextSibling()) { 178 // Don't include repaint rects for composited child layers; they will paint themselves and have a different origin. 179 if (child->compositingState() == PaintsIntoOwnBacking || child->compositingState() == PaintsIntoGroupedBacking) 180 continue; 181 182 repaintRect.unite(child->repainter().repaintRectIncludingNonCompositingDescendants()); 183 } 184 return repaintRect; 185 } 186 187 void RenderLayerRepainter::setBackingNeedsRepaintInRect(const LayoutRect& r) 188 { 189 // https://bugs.webkit.org/show_bug.cgi?id=61159 describes an unreproducible crash here, 190 // so assert but check that the layer is composited. 191 ASSERT(m_renderer.compositingState() != NotComposited); 192 if (m_renderer.compositingState() == NotComposited) { 193 // If we're trying to repaint the placeholder document layer, propagate the 194 // repaint to the native view system. 195 LayoutRect absRect(r); 196 LayoutPoint delta; 197 m_renderer.layer()->convertToLayerCoords(m_renderer.layer()->root(), delta); 198 absRect.moveBy(delta); 199 200 if (absRect.isEmpty()) 201 return; 202 203 RenderView* view = m_renderer.view(); 204 if (view) 205 view->repaintViewRectangle(absRect); 206 return; 207 } 208 IntRect repaintRect = pixelSnappedIntRect(r); 209 // FIXME: generalize accessors to backing GraphicsLayers so that this code is squashing-agnostic. 210 if (m_renderer.groupedMapping()) { 211 if (GraphicsLayer* squashingLayer = m_renderer.groupedMapping()->squashingLayer()) 212 squashingLayer->setNeedsDisplayInRect(repaintRect); 213 } else { 214 m_renderer.compositedLayerMapping()->setContentsNeedDisplayInRect(repaintRect); 215 } 216 } 217 218 void RenderLayerRepainter::setFilterBackendNeedsRepaintingInRect(const LayoutRect& rect) 219 { 220 if (rect.isEmpty()) 221 return; 222 LayoutRect rectForRepaint = rect; 223 m_renderer.style()->filterOutsets().expandRect(rectForRepaint); 224 225 RenderLayerFilterInfo* filterInfo = m_renderer.layer()->filterInfo(); 226 ASSERT(filterInfo); 227 filterInfo->expandDirtySourceRect(rectForRepaint); 228 229 RenderLayer* parentLayer = enclosingFilterRepaintLayer(); 230 ASSERT(parentLayer); 231 FloatQuad repaintQuad(rectForRepaint); 232 LayoutRect parentLayerRect = m_renderer.localToContainerQuad(repaintQuad, parentLayer->renderer()).enclosingBoundingBox(); 233 234 if (parentLayerRect.isEmpty()) 235 return; 236 237 if (parentLayer->hasCompositedLayerMapping()) { 238 parentLayer->repainter().setBackingNeedsRepaintInRect(parentLayerRect); 239 return; 240 } 241 242 if (parentLayer->paintsWithFilters()) { 243 parentLayer->repainter().setFilterBackendNeedsRepaintingInRect(parentLayerRect); 244 return; 245 } 246 247 if (parentLayer->isRootLayer()) { 248 RenderView* view = toRenderView(parentLayer->renderer()); 249 view->repaintViewRectangle(parentLayerRect); 250 return; 251 } 252 253 ASSERT_NOT_REACHED(); 254 } 255 256 RenderLayer* RenderLayerRepainter::enclosingFilterRepaintLayer() const 257 { 258 for (const RenderLayer* curr = m_renderer.layer(); curr; curr = curr->parent()) { 259 if ((curr != m_renderer.layer() && curr->requiresFullLayerImageForFilters()) || curr->compositingState() == PaintsIntoOwnBacking || curr->isRootLayer()) 260 return const_cast<RenderLayer*>(curr); 261 } 262 return 0; 263 } 264 265 } // Namespace WebCore 266