Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / RenderLayerRepainter.cpp
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@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>
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->layoutStateEnabled());
75
76         const RenderLayerModelObject* repaintContainer = m_renderer.containerForRepaint();
77         LayoutRect oldRepaintRect = m_repaintRect;
78         LayoutPoint oldOffset = m_offset;
79         computeRepaintRects(repaintContainer);
80         shouldCheckForRepaint &= shouldRepaintLayer();
81
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);
88                 } else {
89                     m_renderer.repaintAfterLayoutIfNeeded(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(const RenderLayerModelObject* repaintContainer)
109 {
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()));
115     } else {
116         m_repaintRect = m_renderer.clippedOverflowRectForRepaint(repaintContainer);
117         m_offset = m_renderer.positionFromRepaintContainer(repaintContainer);
118     }
119 }
120
121 void RenderLayerRepainter::computeRepaintRectsIncludingDescendants()
122 {
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());
127
128     for (RenderLayer* layer = m_renderer.layer()->firstChild(); layer; layer = layer->nextSibling())
129         layer->repainter().computeRepaintRectsIncludingDescendants();
130 }
131
132 inline bool RenderLayerRepainter::shouldRepaintLayer() const
133 {
134     if (RuntimeEnabledFeatures::repaintAfterLayoutEnabled())
135         return false;
136
137     if (m_repaintStatus != NeedsFullRepaintForPositionedMovementLayout)
138         return true;
139
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;
143 }
144
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)
147 {
148     m_renderer.repaintUsingContainer(repaintContainer, pixelSnappedIntRect(m_renderer.clippedOverflowRectForRepaint(repaintContainer)), InvalidationLayer);
149
150     for (RenderLayer* curr = m_renderer.layer()->firstChild(); curr; curr = curr->nextSibling()) {
151         if (!curr->hasCompositedLayerMapping())
152             curr->repainter().repaintIncludingNonCompositingDescendants(repaintContainer);
153     }
154 }
155
156 LayoutRect RenderLayerRepainter::repaintRectIncludingNonCompositingDescendants() const
157 {
158     LayoutRect repaintRect;
159     if (RuntimeEnabledFeatures::repaintAfterLayoutEnabled())
160         repaintRect = m_renderer.previousRepaintRect();
161     else
162         repaintRect = m_repaintRect;
163
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())
167             continue;
168
169         repaintRect.unite(child->repainter().repaintRectIncludingNonCompositingDescendants());
170     }
171     return repaintRect;
172 }
173
174 void RenderLayerRepainter::setBackingNeedsRepaint()
175 {
176     // There is only one call site, and that call site ensures that the compositing state is PaintsIntoOwnBacking.
177     ASSERT(m_renderer.compositingState() == PaintsIntoOwnBacking);
178
179     m_renderer.compositedLayerMapping()->setContentsNeedDisplay();
180 }
181
182 void RenderLayerRepainter::setBackingNeedsRepaintInRect(const LayoutRect& r)
183 {
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);
191         LayoutPoint delta;
192         m_renderer.layer()->convertToLayerCoords(m_renderer.layer()->root(), delta);
193         absRect.moveBy(delta);
194
195         if (absRect.isEmpty())
196             return;
197
198         RenderView* view = m_renderer.view();
199         if (view)
200             view->repaintViewRectangle(absRect);
201         return;
202     }
203     if (m_renderer.compositingState() == PaintsIntoGroupedBacking) {
204         LayoutRect updatedRect(r);
205
206         ASSERT(m_renderer.layer());
207         ASSERT(m_renderer.layer()->enclosingTransformedAncestor());
208         ASSERT(m_renderer.layer()->enclosingTransformedAncestor()->renderer());
209
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)
215             return;
216
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);
222
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());
229
230         IntRect repaintRect = pixelSnappedIntRect(updatedRect);
231         if (GraphicsLayer* squashingLayer = m_renderer.groupedMapping()->squashingLayer())
232             squashingLayer->setNeedsDisplayInRect(repaintRect);
233     } else {
234         IntRect repaintRect = pixelSnappedIntRect(r);
235         m_renderer.compositedLayerMapping()->setContentsNeedDisplayInRect(repaintRect);
236     }
237 }
238
239 void RenderLayerRepainter::setFilterBackendNeedsRepaintingInRect(const LayoutRect& rect)
240 {
241     if (rect.isEmpty())
242         return;
243
244     LayoutRect rectForRepaint = rect;
245     m_renderer.style()->filterOutsets().expandRect(rectForRepaint);
246
247     RenderLayerFilterInfo* filterInfo = m_renderer.layer()->filterInfo();
248     ASSERT(filterInfo);
249     filterInfo->expandDirtySourceRect(rectForRepaint);
250
251     RenderLayer* parentLayer = enclosingFilterRepaintLayer();
252     ASSERT(parentLayer);
253     FloatQuad repaintQuad(rectForRepaint);
254     LayoutRect parentLayerRect = m_renderer.localToContainerQuad(repaintQuad, parentLayer->renderer()).enclosingBoundingBox();
255
256     if (parentLayerRect.isEmpty())
257         return;
258
259     if (parentLayer->hasCompositedLayerMapping()) {
260         parentLayer->repainter().setBackingNeedsRepaintInRect(parentLayerRect);
261         return;
262     }
263
264     if (parentLayer->paintsWithFilters()) {
265         parentLayer->repainter().setFilterBackendNeedsRepaintingInRect(parentLayerRect);
266         return;
267     }
268
269     if (parentLayer->isRootLayer()) {
270         RenderView* view = toRenderView(parentLayer->renderer());
271         view->repaintViewRectangle(parentLayerRect);
272         return;
273     }
274
275     ASSERT_NOT_REACHED();
276 }
277
278 RenderLayer* RenderLayerRepainter::enclosingFilterRepaintLayer() const
279 {
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);
283     }
284     return 0;
285 }
286
287 } // Namespace WebCore