2 * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
4 * Portions are Copyright (C) 1998 Netscape Communications Corporation.
7 * Robert O'Callahan <roc+@cs.cmu.edu>
8 * David Baron <dbaron@fas.harvard.edu>
9 * Christian Biesinger <cbiesinger@web.de>
10 * Randall Jesup <rjesup@wgate.com>
11 * Roland Mainz <roland.mainz@informatik.med.uni-giessen.de>
12 * Josh Soref <timeless@mac.com>
13 * Boris Zbarsky <bzbarsky@mit.edu>
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.
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.
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
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.
45 #include "core/rendering/RenderLayerRepainter.h"
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"
54 RenderLayerRepainter::RenderLayerRepainter(RenderLayerModelObject& renderer)
55 : m_renderer(renderer)
56 , m_repaintStatus(NeedsNormalRepaint)
60 void RenderLayerRepainter::repaintAfterLayout(bool shouldCheckForRepaint)
62 if (RuntimeEnabledFeatures::repaintAfterLayoutEnabled())
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();
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->layoutStateEnabled());
76 const RenderLayerModelObject* repaintContainer = m_renderer.containerForRepaint();
77 LayoutRect oldRepaintRect = m_repaintRect;
78 LayoutPoint oldOffset = m_offset;
79 computeRepaintRects(repaintContainer);
80 shouldCheckForRepaint &= shouldRepaintLayer();
82 if (shouldCheckForRepaint) {
83 if (view && !view->document().printing()) {
84 if (m_repaintStatus & NeedsFullRepaint) {
85 m_renderer.repaintUsingContainer(repaintContainer, pixelSnappedIntRect(oldRepaintRect), InvalidationLayer);
86 if (m_repaintRect != oldRepaintRect)
87 m_renderer.repaintUsingContainer(repaintContainer, pixelSnappedIntRect(m_repaintRect), InvalidationLayer);
89 m_renderer.repaintAfterLayoutIfNeeded(repaintContainer, m_renderer.selfNeedsLayout(), oldRepaintRect, oldOffset, &m_repaintRect, &m_offset);
97 m_repaintStatus = NeedsNormalRepaint;
101 void RenderLayerRepainter::clearRepaintRects()
103 ASSERT(!m_renderer.layer()->hasVisibleContent());
105 m_repaintRect = IntRect();
108 void RenderLayerRepainter::computeRepaintRects(const RenderLayerModelObject* repaintContainer)
110 if (RuntimeEnabledFeatures::repaintAfterLayoutEnabled()) {
111 // FIXME: We want RenderLayerRepainter to go away when
112 // repaint-after-layout is on by default so we need to figure out how to
113 // handle this update.
114 m_renderer.setPreviousRepaintRect(m_renderer.clippedOverflowRectForRepaint(m_renderer.containerForRepaint()));
116 m_repaintRect = m_renderer.clippedOverflowRectForRepaint(repaintContainer);
117 m_offset = m_renderer.positionFromRepaintContainer(repaintContainer);
121 void RenderLayerRepainter::computeRepaintRectsIncludingDescendants()
123 // FIXME: computeRepaintRects() has to walk up the parent chain for every layer to compute the rects.
124 // We should make this more efficient.
125 // FIXME: it's wrong to call this when layout is not up-to-date, which we do.
126 computeRepaintRects(m_renderer.containerForRepaint());
128 for (RenderLayer* layer = m_renderer.layer()->firstChild(); layer; layer = layer->nextSibling())
129 layer->repainter().computeRepaintRectsIncludingDescendants();
132 inline bool RenderLayerRepainter::shouldRepaintLayer() const
134 if (RuntimeEnabledFeatures::repaintAfterLayoutEnabled())
137 if (m_repaintStatus != NeedsFullRepaintForPositionedMovementLayout)
140 // Composited layers that were moved during a positioned movement only
141 // layout, don't need to be repainted. They just need to be recomposited.
142 return m_renderer.compositingState() != PaintsIntoOwnBacking;
145 // Since we're only painting non-composited layers, we know that they all share the same repaintContainer.
146 void RenderLayerRepainter::repaintIncludingNonCompositingDescendants(const RenderLayerModelObject* repaintContainer)
148 m_renderer.repaintUsingContainer(repaintContainer, pixelSnappedIntRect(m_renderer.clippedOverflowRectForRepaint(repaintContainer)), InvalidationLayer);
150 for (RenderLayer* curr = m_renderer.layer()->firstChild(); curr; curr = curr->nextSibling()) {
151 if (!curr->hasCompositedLayerMapping())
152 curr->repainter().repaintIncludingNonCompositingDescendants(repaintContainer);
156 LayoutRect RenderLayerRepainter::repaintRectIncludingNonCompositingDescendants() const
158 LayoutRect repaintRect;
159 if (RuntimeEnabledFeatures::repaintAfterLayoutEnabled())
160 repaintRect = m_renderer.previousRepaintRect();
162 repaintRect = m_repaintRect;
164 for (RenderLayer* child = m_renderer.layer()->firstChild(); child; child = child->nextSibling()) {
165 // Don't include repaint rects for composited child layers; they will paint themselves and have a different origin.
166 if (child->hasCompositedLayerMapping())
169 repaintRect.unite(child->repainter().repaintRectIncludingNonCompositingDescendants());
174 void RenderLayerRepainter::setBackingNeedsRepaint()
176 // There is only one call site, and that call site ensures that the compositing state is PaintsIntoOwnBacking.
177 ASSERT(m_renderer.compositingState() == PaintsIntoOwnBacking);
179 m_renderer.compositedLayerMapping()->setContentsNeedDisplay();
182 void RenderLayerRepainter::setBackingNeedsRepaintInRect(const LayoutRect& r)
184 // https://bugs.webkit.org/show_bug.cgi?id=61159 describes an unreproducible crash here,
185 // so assert but check that the layer is composited.
186 ASSERT(m_renderer.compositingState() != NotComposited);
187 if (m_renderer.compositingState() == NotComposited) {
188 // If we're trying to repaint the placeholder document layer, propagate the
189 // repaint to the native view system.
190 LayoutRect absRect(r);
192 m_renderer.layer()->convertToLayerCoords(m_renderer.layer()->root(), delta);
193 absRect.moveBy(delta);
195 if (absRect.isEmpty())
198 RenderView* view = m_renderer.view();
200 view->repaintViewRectangle(absRect);
203 if (m_renderer.compositingState() == PaintsIntoGroupedBacking) {
204 LayoutRect updatedRect(r);
206 ASSERT(m_renderer.layer());
207 ASSERT(m_renderer.layer()->enclosingTransformedAncestor());
208 ASSERT(m_renderer.layer()->enclosingTransformedAncestor()->renderer());
210 // FIXME: this defensive code should not have to exist. None of these pointers should ever be 0. See crbug.com/370410.
211 RenderLayerModelObject* transformedAncestor = 0;
212 if (RenderLayer* ancestor = m_renderer.layer()->enclosingTransformedAncestor())
213 transformedAncestor = ancestor->renderer();
214 if (!transformedAncestor)
217 // If the transformedAncestor is actually the RenderView, we might get
218 // confused and think that we can use LayoutState. Ideally, we'd made
219 // LayoutState work for all composited layers as well, but until then
220 // we need to disable LayoutState for squashed layers.
221 LayoutStateDisabler layoutStateDisabler(*transformedAncestor);
223 // This code adjusts the repaint rectangle to be in the space of the transformed ancestor of the grouped (i.e. squashed)
224 // layer. This is because all layers that squash together need to repaint w.r.t. a single container that is
225 // an ancestor of all of them, in order to properly take into account any local transforms etc.
226 // FIXME: remove this special-case code that works around the repainting code structure.
227 m_renderer.computeRectForRepaint(transformedAncestor, updatedRect);
228 updatedRect.moveBy(-m_renderer.layer()->groupedMapping()->squashingOffsetFromTransformedAncestor());
230 IntRect repaintRect = pixelSnappedIntRect(updatedRect);
231 if (GraphicsLayer* squashingLayer = m_renderer.groupedMapping()->squashingLayer())
232 squashingLayer->setNeedsDisplayInRect(repaintRect);
234 IntRect repaintRect = pixelSnappedIntRect(r);
235 m_renderer.compositedLayerMapping()->setContentsNeedDisplayInRect(repaintRect);
239 void RenderLayerRepainter::setFilterBackendNeedsRepaintingInRect(const LayoutRect& rect)
244 LayoutRect rectForRepaint = rect;
245 m_renderer.style()->filterOutsets().expandRect(rectForRepaint);
247 RenderLayerFilterInfo* filterInfo = m_renderer.layer()->filterInfo();
249 filterInfo->expandDirtySourceRect(rectForRepaint);
251 RenderLayer* parentLayer = enclosingFilterRepaintLayer();
253 FloatQuad repaintQuad(rectForRepaint);
254 LayoutRect parentLayerRect = m_renderer.localToContainerQuad(repaintQuad, parentLayer->renderer()).enclosingBoundingBox();
256 if (parentLayerRect.isEmpty())
259 if (parentLayer->hasCompositedLayerMapping()) {
260 parentLayer->repainter().setBackingNeedsRepaintInRect(parentLayerRect);
264 if (parentLayer->paintsWithFilters()) {
265 parentLayer->repainter().setFilterBackendNeedsRepaintingInRect(parentLayerRect);
269 if (parentLayer->isRootLayer()) {
270 RenderView* view = toRenderView(parentLayer->renderer());
271 view->repaintViewRectangle(parentLayerRect);
275 ASSERT_NOT_REACHED();
278 RenderLayer* RenderLayerRepainter::enclosingFilterRepaintLayer() const
280 for (const RenderLayer* curr = m_renderer.layer(); curr; curr = curr->parent()) {
281 if ((curr != m_renderer.layer() && curr->requiresFullLayerImageForFilters()) || curr->compositingState() == PaintsIntoOwnBacking || curr->isRootLayer())
282 return const_cast<RenderLayer*>(curr);
287 } // Namespace WebCore