Upstream version 5.34.104.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/CompositedLayerMapping.h"
48 #include "core/rendering/FilterEffectRenderer.h"
49 #include "core/rendering/RenderLayer.h"
50 #include "core/rendering/RenderView.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(RenderGeometryMap* geometryMap, bool shouldCheckForRepaint)
61 {
62     // FIXME: really, we're in the repaint phase here, and the following queries are legal.
63     // Until those states are fully fledged, I'll just disable the ASSERTS.
64     DisableCompositingQueryAsserts disabler;
65     if (m_renderer->layer()->hasVisibleContent()) {
66         RenderView* view = m_renderer->view();
67         ASSERT(view);
68         // FIXME: LayoutState does not work with RenderLayers as there is not a 1-to-1
69         // mapping between them and the RenderObjects. It would be neat to enable
70         // LayoutState outside the layout() phase and use it here.
71         ASSERT(!view->layoutStateEnabled());
72
73         RenderLayerModelObject* repaintContainer = m_renderer->containerForRepaint();
74         LayoutRect oldRepaintRect = m_repaintRect;
75         LayoutRect oldOutlineBox = m_outlineBox;
76         computeRepaintRects(repaintContainer, geometryMap);
77
78         // FIXME: Should ASSERT that value calculated for m_outlineBox using the cached offset is the same
79         // as the value not using the cached offset, but we can't due to https://bugs.webkit.org/show_bug.cgi?id=37048
80         if (shouldCheckForRepaint) {
81             if (view && !view->document().printing()) {
82                 if (m_repaintStatus & NeedsFullRepaint) {
83                     m_renderer->repaintUsingContainer(repaintContainer, pixelSnappedIntRect(oldRepaintRect));
84                     if (m_repaintRect != oldRepaintRect)
85                         m_renderer->repaintUsingContainer(repaintContainer, pixelSnappedIntRect(m_repaintRect));
86                 } else if (shouldRepaintAfterLayout()) {
87                     m_renderer->repaintAfterLayoutIfNeeded(repaintContainer, m_renderer->selfNeedsLayout(), oldRepaintRect, oldOutlineBox, &m_repaintRect, &m_outlineBox);
88                 }
89             }
90         }
91     } else {
92         clearRepaintRects();
93     }
94
95     m_repaintStatus = NeedsNormalRepaint;
96
97 }
98
99 void RenderLayerRepainter::clearRepaintRects()
100 {
101     ASSERT(!m_renderer->layer()->hasVisibleContent());
102
103     m_repaintRect = IntRect();
104     m_outlineBox = IntRect();
105 }
106
107 void RenderLayerRepainter::computeRepaintRects(const RenderLayerModelObject* repaintContainer, const RenderGeometryMap* geometryMap)
108 {
109     m_repaintRect = m_renderer->clippedOverflowRectForRepaint(repaintContainer);
110     m_outlineBox = m_renderer->outlineBoundsForRepaint(repaintContainer, geometryMap);
111 }
112
113 void RenderLayerRepainter::computeRepaintRectsIncludingDescendants()
114 {
115     // FIXME: computeRepaintRects() has to walk up the parent chain for every layer to compute the rects.
116     // We should make this more efficient.
117     // FIXME: it's wrong to call this when layout is not up-to-date, which we do.
118     computeRepaintRects(m_renderer->containerForRepaint());
119
120     for (RenderLayer* layer = m_renderer->layer()->firstChild(); layer; layer = layer->nextSibling())
121         layer->repainter().computeRepaintRectsIncludingDescendants();
122 }
123
124 inline bool RenderLayerRepainter::shouldRepaintAfterLayout() const
125 {
126     if (m_repaintStatus == NeedsNormalRepaint)
127         return true;
128
129     // Composited layers that were moved during a positioned movement only
130     // layout, don't need to be repainted. They just need to be recomposited.
131     ASSERT(m_repaintStatus == NeedsFullRepaintForPositionedMovementLayout);
132     return m_renderer->compositingState() != PaintsIntoOwnBacking;
133 }
134
135 // Since we're only painting non-composited layers, we know that they all share the same repaintContainer.
136 void RenderLayerRepainter::repaintIncludingNonCompositingDescendants(RenderLayerModelObject* repaintContainer)
137 {
138     m_renderer->repaintUsingContainer(repaintContainer, pixelSnappedIntRect(m_renderer->clippedOverflowRectForRepaint(repaintContainer)));
139
140     for (RenderLayer* curr = m_renderer->layer()->firstChild(); curr; curr = curr->nextSibling()) {
141         if (!curr->hasCompositedLayerMapping())
142             curr->repainter().repaintIncludingNonCompositingDescendants(repaintContainer);
143     }
144 }
145
146 LayoutRect RenderLayerRepainter::repaintRectIncludingNonCompositingDescendants() const
147 {
148     LayoutRect repaintRect = m_repaintRect;
149     for (RenderLayer* child = m_renderer->layer()->firstChild(); child; child = child->nextSibling()) {
150         // Don't include repaint rects for composited child layers; they will paint themselves and have a different origin.
151         if (child->hasCompositedLayerMapping())
152             continue;
153
154         repaintRect.unite(child->repainter().repaintRectIncludingNonCompositingDescendants());
155     }
156     return repaintRect;
157 }
158
159 void RenderLayerRepainter::setBackingNeedsRepaint()
160 {
161     ASSERT(m_renderer->compositingState() != NotComposited);
162
163     if (m_renderer->compositingState() == PaintsIntoGroupedBacking) {
164         // FIXME: should probably setNeedsDisplayInRect for this layer's bounds only.
165         m_renderer->groupedMapping()->squashingLayer()->setNeedsDisplay();
166     } else {
167         m_renderer->compositedLayerMapping()->setContentsNeedDisplay();
168     }
169 }
170
171 void RenderLayerRepainter::setBackingNeedsRepaintInRect(const LayoutRect& r)
172 {
173     // https://bugs.webkit.org/show_bug.cgi?id=61159 describes an unreproducible crash here,
174     // so assert but check that the layer is composited.
175     ASSERT(m_renderer->compositingState() != NotComposited);
176     if (m_renderer->compositingState() == NotComposited) {
177         // If we're trying to repaint the placeholder document layer, propagate the
178         // repaint to the native view system.
179         LayoutRect absRect(r);
180         LayoutPoint delta;
181         m_renderer->layer()->convertToLayerCoords(m_renderer->layer()->root(), delta);
182         absRect.moveBy(delta);
183
184         RenderView* view = m_renderer->view();
185         if (view)
186             view->repaintViewRectangle(absRect);
187     } else {
188         if (m_renderer->compositingState() == PaintsIntoGroupedBacking) {
189             // FIXME: LayoutRect rounding to IntRect is probably not a good idea.
190             IntRect offsetRect = pixelSnappedIntRect(r);
191             if (m_renderer->hasTransform())
192                 offsetRect = m_renderer->layer()->transform()->mapRect(pixelSnappedIntRect(r));
193
194             offsetRect.move(-m_renderer->layer()->offsetFromSquashingLayerOrigin());
195             m_renderer->groupedMapping()->squashingLayer()->setNeedsDisplayInRect(offsetRect);
196         } else {
197             IntRect repaintRect = pixelSnappedIntRect(r.location() +  m_renderer->compositedLayerMapping()->subpixelAccumulation(), r.size());
198             m_renderer->compositedLayerMapping()->setContentsNeedDisplayInRect(repaintRect);
199         }
200     }
201 }
202
203 void RenderLayerRepainter::repaintIncludingDescendants()
204 {
205     m_renderer->repaint();
206     for (RenderLayer* curr = m_renderer->layer()->firstChild(); curr; curr = curr->nextSibling())
207         curr->repainter().repaintIncludingDescendants();
208 }
209
210 void RenderLayerRepainter::setFilterBackendNeedsRepaintingInRect(const LayoutRect& rect)
211 {
212     if (rect.isEmpty())
213         return;
214
215     LayoutRect rectForRepaint = rect;
216     m_renderer->style()->filterOutsets().expandRect(rectForRepaint);
217
218     RenderLayerFilterInfo* filterInfo = m_renderer->layer()->filterInfo();
219     ASSERT(filterInfo);
220     filterInfo->expandDirtySourceRect(rectForRepaint);
221
222     ASSERT(filterInfo->renderer());
223     if (filterInfo->renderer()->hasCustomShaderFilter()) {
224         // If we have at least one custom shader, we need to update the whole bounding box of the layer, because the
225         // shader can address any ouput pixel.
226         // Note: This is only for output rect, so there's no need to expand the dirty source rect.
227         rectForRepaint.unite(m_renderer->layer()->calculateLayerBounds(m_renderer->layer()));
228     }
229
230     RenderLayer* parentLayer = enclosingFilterRepaintLayer();
231     ASSERT(parentLayer);
232     FloatQuad repaintQuad(rectForRepaint);
233     LayoutRect parentLayerRect = m_renderer->localToContainerQuad(repaintQuad, parentLayer->renderer()).enclosingBoundingBox();
234
235     if (parentLayer->hasCompositedLayerMapping()) {
236         parentLayer->repainter().setBackingNeedsRepaintInRect(parentLayerRect);
237         return;
238     }
239
240     if (parentLayer->paintsWithFilters()) {
241         parentLayer->repainter().setFilterBackendNeedsRepaintingInRect(parentLayerRect);
242         return;
243     }
244
245     if (parentLayer->isRootLayer()) {
246         RenderView* view = toRenderView(parentLayer->renderer());
247         view->repaintViewRectangle(parentLayerRect);
248         return;
249     }
250
251     ASSERT_NOT_REACHED();
252 }
253
254 RenderLayer* RenderLayerRepainter::enclosingFilterRepaintLayer() const
255 {
256     for (const RenderLayer* curr = m_renderer->layer(); curr; curr = curr->parent()) {
257         if ((curr != m_renderer->layer() && curr->requiresFullLayerImageForFilters()) || curr->compositingState() == PaintsIntoOwnBacking || curr->isRootLayer())
258             return const_cast<RenderLayer*>(curr);
259     }
260     return 0;
261 }
262
263 } // Namespace WebCore