Upstream version 7.35.139.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/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         LayoutRect oldOutlineBox = m_outlineBox;
80         computeRepaintRects(repaintContainer, geometryMap);
81         shouldCheckForRepaint &= shouldRepaintLayer();
82
83         // FIXME: Should ASSERT that value calculated for m_outlineBox using the cached offset is the same
84         // as the value not using the cached offset, but we can't due to https://bugs.webkit.org/show_bug.cgi?id=37048
85         if (shouldCheckForRepaint) {
86             if (view && !view->document().printing()) {
87                 if (m_repaintStatus & NeedsFullRepaint) {
88                     m_renderer->repaintUsingContainer(repaintContainer, pixelSnappedIntRect(oldRepaintRect));
89                     if (m_repaintRect != oldRepaintRect)
90                         m_renderer->repaintUsingContainer(repaintContainer, pixelSnappedIntRect(m_repaintRect));
91                 } else {
92                     m_renderer->repaintAfterLayoutIfNeeded(repaintContainer, m_renderer->selfNeedsLayout(), oldRepaintRect, oldOutlineBox, &m_repaintRect, &m_outlineBox);
93                 }
94             }
95         }
96     } else {
97         clearRepaintRects();
98     }
99
100     m_repaintStatus = NeedsNormalRepaint;
101
102 }
103
104 void RenderLayerRepainter::clearRepaintRects()
105 {
106     ASSERT(!m_renderer->layer()->hasVisibleContent());
107
108     m_repaintRect = IntRect();
109     m_outlineBox = IntRect();
110 }
111
112 void RenderLayerRepainter::computeRepaintRects(const RenderLayerModelObject* repaintContainer, const RenderGeometryMap* geometryMap)
113 {
114     if (RuntimeEnabledFeatures::repaintAfterLayoutEnabled())
115         return;
116
117     m_repaintRect = m_renderer->clippedOverflowRectForRepaint(repaintContainer);
118     m_outlineBox = m_renderer->outlineBoundsForRepaint(repaintContainer, geometryMap);
119 }
120
121 void RenderLayerRepainter::computeRepaintRectsIncludingDescendants()
122 {
123     if (RuntimeEnabledFeatures::repaintAfterLayoutEnabled()) {
124         // FIXME: We want RenderLayerRepainter to go away when
125         // repaint-after-layout is on by default so we need to figure out how to
126         // handle this update.
127         //
128         // This is a little silly as we create and immediately destroy the RAII
129         // object but it makes sure we correctly set all of the repaint flags.
130         LayoutRectRecorder recorder(*m_renderer);
131
132     } else {
133         // FIXME: computeRepaintRects() has to walk up the parent chain for every layer to compute the rects.
134         // We should make this more efficient.
135         // FIXME: it's wrong to call this when layout is not up-to-date, which we do.
136         computeRepaintRects(m_renderer->containerForRepaint());
137     }
138
139     for (RenderLayer* layer = m_renderer->layer()->firstChild(); layer; layer = layer->nextSibling())
140         layer->repainter().computeRepaintRectsIncludingDescendants();
141 }
142
143 inline bool RenderLayerRepainter::shouldRepaintLayer() const
144 {
145     if (RuntimeEnabledFeatures::repaintAfterLayoutEnabled())
146         return false;
147
148     if (m_repaintStatus != NeedsFullRepaintForPositionedMovementLayout)
149         return true;
150
151     // Composited layers that were moved during a positioned movement only
152     // layout, don't need to be repainted. They just need to be recomposited.
153     return m_renderer->compositingState() != PaintsIntoOwnBacking;
154 }
155
156 // Since we're only painting non-composited layers, we know that they all share the same repaintContainer.
157 void RenderLayerRepainter::repaintIncludingNonCompositingDescendants(RenderLayerModelObject* repaintContainer)
158 {
159     m_renderer->repaintUsingContainer(repaintContainer, pixelSnappedIntRect(m_renderer->clippedOverflowRectForRepaint(repaintContainer)));
160
161     for (RenderLayer* curr = m_renderer->layer()->firstChild(); curr; curr = curr->nextSibling()) {
162         if (!curr->hasCompositedLayerMapping())
163             curr->repainter().repaintIncludingNonCompositingDescendants(repaintContainer);
164     }
165 }
166
167 LayoutRect RenderLayerRepainter::repaintRectIncludingNonCompositingDescendants() const
168 {
169     LayoutRect repaintRect;
170     if (RuntimeEnabledFeatures::repaintAfterLayoutEnabled())
171         repaintRect = m_renderer->newRepaintRect();
172     else
173         repaintRect = m_repaintRect;
174
175     for (RenderLayer* child = m_renderer->layer()->firstChild(); child; child = child->nextSibling()) {
176         // Don't include repaint rects for composited child layers; they will paint themselves and have a different origin.
177         if (child->hasCompositedLayerMapping())
178             continue;
179
180         repaintRect.unite(child->repainter().repaintRectIncludingNonCompositingDescendants());
181     }
182     return repaintRect;
183 }
184
185 void RenderLayerRepainter::setBackingNeedsRepaint()
186 {
187     ASSERT(m_renderer->compositingState() != NotComposited);
188
189     if (m_renderer->compositingState() == PaintsIntoGroupedBacking) {
190         // FIXME: should probably setNeedsDisplayInRect for this layer's bounds only.
191         m_renderer->groupedMapping()->squashingLayer()->setNeedsDisplay();
192     } else {
193         m_renderer->compositedLayerMapping()->setContentsNeedDisplay();
194     }
195 }
196
197 void RenderLayerRepainter::setBackingNeedsRepaintInRect(const LayoutRect& r)
198 {
199     // https://bugs.webkit.org/show_bug.cgi?id=61159 describes an unreproducible crash here,
200     // so assert but check that the layer is composited.
201     ASSERT(m_renderer->compositingState() != NotComposited);
202     if (m_renderer->compositingState() == NotComposited) {
203         // If we're trying to repaint the placeholder document layer, propagate the
204         // repaint to the native view system.
205         LayoutRect absRect(r);
206         LayoutPoint delta;
207         m_renderer->layer()->convertToLayerCoords(m_renderer->layer()->root(), delta);
208         absRect.moveBy(delta);
209
210         RenderView* view = m_renderer->view();
211         if (view)
212             view->repaintViewRectangle(absRect);
213     } else {
214         if (m_renderer->compositingState() == PaintsIntoGroupedBacking) {
215             // FIXME: LayoutRect rounding to IntRect is probably not a good idea.
216             IntRect offsetRect = pixelSnappedIntRect(r);
217             if (m_renderer->hasTransform())
218                 offsetRect = m_renderer->layer()->transform()->mapRect(pixelSnappedIntRect(r));
219
220             offsetRect.move(-m_renderer->layer()->offsetFromSquashingLayerOrigin());
221             m_renderer->groupedMapping()->squashingLayer()->setNeedsDisplayInRect(offsetRect);
222         } else {
223             IntRect repaintRect = pixelSnappedIntRect(r.location() +  m_renderer->compositedLayerMapping()->subpixelAccumulation(), r.size());
224             m_renderer->compositedLayerMapping()->setContentsNeedDisplayInRect(repaintRect);
225         }
226     }
227 }
228
229 void RenderLayerRepainter::repaintIncludingDescendants()
230 {
231     m_renderer->repaint();
232     for (RenderLayer* curr = m_renderer->layer()->firstChild(); curr; curr = curr->nextSibling())
233         curr->repainter().repaintIncludingDescendants();
234 }
235
236 void RenderLayerRepainter::setFilterBackendNeedsRepaintingInRect(const LayoutRect& rect)
237 {
238     if (rect.isEmpty())
239         return;
240
241     LayoutRect rectForRepaint = rect;
242     m_renderer->style()->filterOutsets().expandRect(rectForRepaint);
243
244     RenderLayerFilterInfo* filterInfo = m_renderer->layer()->filterInfo();
245     ASSERT(filterInfo);
246     filterInfo->expandDirtySourceRect(rectForRepaint);
247
248     ASSERT(filterInfo->renderer());
249     if (filterInfo->renderer()->hasCustomShaderFilter()) {
250         // If we have at least one custom shader, we need to update the whole bounding box of the layer, because the
251         // shader can address any ouput pixel.
252         // Note: This is only for output rect, so there's no need to expand the dirty source rect.
253         rectForRepaint.unite(m_renderer->layer()->calculateLayerBounds(m_renderer->layer()));
254     }
255
256     RenderLayer* parentLayer = enclosingFilterRepaintLayer();
257     ASSERT(parentLayer);
258     FloatQuad repaintQuad(rectForRepaint);
259     LayoutRect parentLayerRect = m_renderer->localToContainerQuad(repaintQuad, parentLayer->renderer()).enclosingBoundingBox();
260
261     if (parentLayer->hasCompositedLayerMapping()) {
262         parentLayer->repainter().setBackingNeedsRepaintInRect(parentLayerRect);
263         return;
264     }
265
266     if (parentLayer->paintsWithFilters()) {
267         parentLayer->repainter().setFilterBackendNeedsRepaintingInRect(parentLayerRect);
268         return;
269     }
270
271     if (parentLayer->isRootLayer()) {
272         RenderView* view = toRenderView(parentLayer->renderer());
273         view->repaintViewRectangle(parentLayerRect);
274         return;
275     }
276
277     ASSERT_NOT_REACHED();
278 }
279
280 RenderLayer* RenderLayerRepainter::enclosingFilterRepaintLayer() const
281 {
282     for (const RenderLayer* curr = m_renderer->layer(); curr; curr = curr->parent()) {
283         if ((curr != m_renderer->layer() && curr->requiresFullLayerImageForFilters()) || curr->compositingState() == PaintsIntoOwnBacking || curr->isRootLayer())
284             return const_cast<RenderLayer*>(curr);
285     }
286     return 0;
287 }
288
289 } // Namespace WebCore