e3a9cca631c199403f9780e7de9ad7aa224545a5
[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/LayoutRectRecorder.h"
49 #include "core/rendering/RenderLayer.h"
50 #include "core/rendering/RenderView.h"
51 #include "core/rendering/compositing/CompositedLayerMapping.h"
52
53 namespace WebCore {
54
55 RenderLayerRepainter::RenderLayerRepainter(RenderLayerModelObject* renderer)
56     : m_renderer(renderer)
57     , m_repaintStatus(NeedsNormalRepaint)
58 {
59 }
60
61 void RenderLayerRepainter::repaintAfterLayout(RenderGeometryMap* geometryMap, bool shouldCheckForRepaint)
62 {
63     if (RuntimeEnabledFeatures::repaintAfterLayoutEnabled())
64         return;
65
66     // FIXME: really, we're in the repaint phase here, and the following queries are legal.
67     // Until those states are fully fledged, I'll just disable the ASSERTS.
68     DisableCompositingQueryAsserts disabler;
69     if (m_renderer->layer()->hasVisibleContent()) {
70         RenderView* view = m_renderer->view();
71         ASSERT(view);
72         // FIXME: LayoutState does not work with RenderLayers as there is not a 1-to-1
73         // mapping between them and the RenderObjects. It would be neat to enable
74         // LayoutState outside the layout() phase and use it here.
75         ASSERT(!view->layoutStateEnabled());
76
77         RenderLayerModelObject* repaintContainer = m_renderer->containerForRepaint();
78         LayoutRect oldRepaintRect = m_repaintRect;
79         computeRepaintRects(repaintContainer, geometryMap);
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));
86                     if (m_repaintRect != oldRepaintRect)
87                         m_renderer->repaintUsingContainer(repaintContainer, pixelSnappedIntRect(m_repaintRect));
88                 } else {
89                     m_renderer->repaintAfterLayoutIfNeeded(repaintContainer, m_renderer->selfNeedsLayout(), oldRepaintRect, &m_repaintRect);
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, const RenderGeometryMap* geometryMap)
109 {
110     if (RuntimeEnabledFeatures::repaintAfterLayoutEnabled())
111         return;
112
113     m_repaintRect = m_renderer->clippedOverflowRectForRepaint(repaintContainer);
114 }
115
116 void RenderLayerRepainter::computeRepaintRectsIncludingDescendants()
117 {
118     if (RuntimeEnabledFeatures::repaintAfterLayoutEnabled()) {
119         // FIXME: We want RenderLayerRepainter to go away when
120         // repaint-after-layout is on by default so we need to figure out how to
121         // handle this update.
122         //
123         // This is a little silly as we create and immediately destroy the RAII
124         // object but it makes sure we correctly set all of the repaint flags.
125         LayoutRectRecorder recorder(*m_renderer);
126
127     } else {
128         // FIXME: computeRepaintRects() has to walk up the parent chain for every layer to compute the rects.
129         // We should make this more efficient.
130         // FIXME: it's wrong to call this when layout is not up-to-date, which we do.
131         computeRepaintRects(m_renderer->containerForRepaint());
132     }
133
134     for (RenderLayer* layer = m_renderer->layer()->firstChild(); layer; layer = layer->nextSibling())
135         layer->repainter().computeRepaintRectsIncludingDescendants();
136 }
137
138 inline bool RenderLayerRepainter::shouldRepaintLayer() const
139 {
140     if (RuntimeEnabledFeatures::repaintAfterLayoutEnabled())
141         return false;
142
143     if (m_repaintStatus != NeedsFullRepaintForPositionedMovementLayout)
144         return true;
145
146     // Composited layers that were moved during a positioned movement only
147     // layout, don't need to be repainted. They just need to be recomposited.
148     return m_renderer->compositingState() != PaintsIntoOwnBacking;
149 }
150
151 // Since we're only painting non-composited layers, we know that they all share the same repaintContainer.
152 void RenderLayerRepainter::repaintIncludingNonCompositingDescendants(RenderLayerModelObject* repaintContainer)
153 {
154     m_renderer->repaintUsingContainer(repaintContainer, pixelSnappedIntRect(m_renderer->clippedOverflowRectForRepaint(repaintContainer)));
155
156     for (RenderLayer* curr = m_renderer->layer()->firstChild(); curr; curr = curr->nextSibling()) {
157         if (!curr->hasCompositedLayerMapping())
158             curr->repainter().repaintIncludingNonCompositingDescendants(repaintContainer);
159     }
160 }
161
162 LayoutRect RenderLayerRepainter::repaintRectIncludingNonCompositingDescendants() const
163 {
164     LayoutRect repaintRect;
165     if (RuntimeEnabledFeatures::repaintAfterLayoutEnabled())
166         repaintRect = m_renderer->newRepaintRect();
167     else
168         repaintRect = m_repaintRect;
169
170     for (RenderLayer* child = m_renderer->layer()->firstChild(); child; child = child->nextSibling()) {
171         // Don't include repaint rects for composited child layers; they will paint themselves and have a different origin.
172         if (child->hasCompositedLayerMapping())
173             continue;
174
175         repaintRect.unite(child->repainter().repaintRectIncludingNonCompositingDescendants());
176     }
177     return repaintRect;
178 }
179
180 void RenderLayerRepainter::setBackingNeedsRepaint()
181 {
182     ASSERT(m_renderer->compositingState() != NotComposited);
183
184     if (m_renderer->compositingState() == PaintsIntoGroupedBacking) {
185         // FIXME: should probably setNeedsDisplayInRect for this layer's bounds only.
186         m_renderer->groupedMapping()->squashingLayer()->setNeedsDisplay();
187     } else {
188         m_renderer->compositedLayerMapping()->setContentsNeedDisplay();
189     }
190 }
191
192 void RenderLayerRepainter::setBackingNeedsRepaintInRect(const LayoutRect& r)
193 {
194     // https://bugs.webkit.org/show_bug.cgi?id=61159 describes an unreproducible crash here,
195     // so assert but check that the layer is composited.
196     ASSERT(m_renderer->compositingState() != NotComposited);
197     if (m_renderer->compositingState() == NotComposited) {
198         // If we're trying to repaint the placeholder document layer, propagate the
199         // repaint to the native view system.
200         LayoutRect absRect(r);
201         LayoutPoint delta;
202         m_renderer->layer()->convertToLayerCoords(m_renderer->layer()->root(), delta);
203         absRect.moveBy(delta);
204
205         RenderView* view = m_renderer->view();
206         if (view)
207             view->repaintViewRectangle(absRect);
208     } else {
209         if (m_renderer->compositingState() == PaintsIntoGroupedBacking) {
210             // FIXME: LayoutRect rounding to IntRect is probably not a good idea.
211             IntRect offsetRect = pixelSnappedIntRect(r);
212             if (m_renderer->hasTransform())
213                 offsetRect = m_renderer->layer()->transform()->mapRect(pixelSnappedIntRect(r));
214
215             offsetRect.move(-m_renderer->layer()->offsetFromSquashingLayerOrigin());
216             m_renderer->groupedMapping()->squashingLayer()->setNeedsDisplayInRect(offsetRect);
217         } else {
218             IntRect repaintRect = pixelSnappedIntRect(r.location() +  m_renderer->compositedLayerMapping()->subpixelAccumulation(), r.size());
219             m_renderer->compositedLayerMapping()->setContentsNeedDisplayInRect(repaintRect);
220         }
221     }
222 }
223
224 void RenderLayerRepainter::repaintIncludingDescendants()
225 {
226     m_renderer->repaint();
227     for (RenderLayer* curr = m_renderer->layer()->firstChild(); curr; curr = curr->nextSibling())
228         curr->repainter().repaintIncludingDescendants();
229 }
230
231 void RenderLayerRepainter::setFilterBackendNeedsRepaintingInRect(const LayoutRect& rect)
232 {
233     if (rect.isEmpty())
234         return;
235
236     LayoutRect rectForRepaint = rect;
237     m_renderer->style()->filterOutsets().expandRect(rectForRepaint);
238
239     RenderLayerFilterInfo* filterInfo = m_renderer->layer()->filterInfo();
240     ASSERT(filterInfo);
241     filterInfo->expandDirtySourceRect(rectForRepaint);
242
243     ASSERT(filterInfo->renderer());
244     if (filterInfo->renderer()->hasCustomShaderFilter()) {
245         // If we have at least one custom shader, we need to update the whole bounding box of the layer, because the
246         // shader can address any ouput pixel.
247         // Note: This is only for output rect, so there's no need to expand the dirty source rect.
248         rectForRepaint.unite(m_renderer->layer()->calculateLayerBounds(m_renderer->layer()));
249     }
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 (parentLayer->hasCompositedLayerMapping()) {
257         parentLayer->repainter().setBackingNeedsRepaintInRect(parentLayerRect);
258         return;
259     }
260
261     if (parentLayer->paintsWithFilters()) {
262         parentLayer->repainter().setFilterBackendNeedsRepaintingInRect(parentLayerRect);
263         return;
264     }
265
266     if (parentLayer->isRootLayer()) {
267         RenderView* view = toRenderView(parentLayer->renderer());
268         view->repaintViewRectangle(parentLayerRect);
269         return;
270     }
271
272     ASSERT_NOT_REACHED();
273 }
274
275 RenderLayer* RenderLayerRepainter::enclosingFilterRepaintLayer() const
276 {
277     for (const RenderLayer* curr = m_renderer->layer(); curr; curr = curr->parent()) {
278         if ((curr != m_renderer->layer() && curr->requiresFullLayerImageForFilters()) || curr->compositingState() == PaintsIntoOwnBacking || curr->isRootLayer())
279             return const_cast<RenderLayer*>(curr);
280     }
281     return 0;
282 }
283
284 } // Namespace WebCore