Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / RenderLayerClipper.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/RenderLayerClipper.h"
46
47 #include "core/rendering/RenderLayer.h"
48 #include "core/rendering/RenderView.h"
49
50 namespace WebCore {
51
52 void RenderLayerClipper::updateClipRects(const ClipRectsContext& clipRectsContext)
53 {
54     ClipRectsType clipRectsType = clipRectsContext.clipRectsType;
55     ASSERT(clipRectsType < NumCachedClipRectsTypes);
56     if (m_clipRectsCache && m_clipRectsCache->getClipRects(clipRectsType, clipRectsContext.respectOverflowClip)) {
57         // FIXME: these asserts trigger for squashing. Need to update this code to support squashing as appropriate.
58         ASSERT(clipRectsContext.rootLayer == m_clipRectsCache->m_clipRectsRoot[clipRectsType]);
59         ASSERT(m_clipRectsCache->m_scrollbarRelevancy[clipRectsType] == clipRectsContext.overlayScrollbarSizeRelevancy);
60
61 #ifdef CHECK_CACHED_CLIP_RECTS
62         // This code is useful to check cached clip rects, but is too expensive to leave enabled in debug builds by default.
63         ClipRectsContext tempContext(clipRectsContext);
64         tempContext.clipRectsType = TemporaryClipRects;
65         ClipRects clipRects;
66         calculateClipRects(tempContext, clipRects);
67         ASSERT(clipRects == *m_clipRectsCache->getClipRects(clipRectsType, clipRectsContext.respectOverflowClip).get());
68 #endif
69         return; // We have the correct cached value.
70     }
71
72     // For transformed layers, the root layer was shifted to be us, so there is no need to
73     // examine the parent. We want to cache clip rects with us as the root.
74     RenderLayer* parentLayer = clipRectsContext.rootLayer != m_renderer->layer() ? m_renderer->layer()->parent() : 0;
75     if (parentLayer)
76         parentLayer->clipper().updateClipRects(clipRectsContext);
77
78     ClipRects clipRects;
79     calculateClipRects(clipRectsContext, clipRects);
80
81     if (!m_clipRectsCache)
82         m_clipRectsCache = adoptPtr(new ClipRectsCache);
83
84     if (parentLayer && parentLayer->clipper().clipRects(clipRectsContext) && clipRects == *parentLayer->clipper().clipRects(clipRectsContext))
85         m_clipRectsCache->setClipRects(clipRectsType, clipRectsContext.respectOverflowClip, parentLayer->clipper().clipRects(clipRectsContext));
86     else
87         m_clipRectsCache->setClipRects(clipRectsType, clipRectsContext.respectOverflowClip, ClipRects::create(clipRects));
88
89 #ifndef NDEBUG
90     m_clipRectsCache->m_clipRectsRoot[clipRectsType] = clipRectsContext.rootLayer;
91     m_clipRectsCache->m_scrollbarRelevancy[clipRectsType] = clipRectsContext.overlayScrollbarSizeRelevancy;
92 #endif
93 }
94
95 void RenderLayerClipper::clearClipRectsIncludingDescendants(ClipRectsType typeToClear)
96 {
97     // FIXME: it's not clear how this layer not having clip rects guarantees that no descendants have any.
98     if (!m_clipRectsCache)
99         return;
100
101     clearClipRects(typeToClear);
102
103     for (RenderLayer* layer = m_renderer->layer()->firstChild(); layer; layer = layer->nextSibling())
104         layer->clipper().clearClipRectsIncludingDescendants(typeToClear);
105 }
106
107 void RenderLayerClipper::clearClipRects(ClipRectsType typeToClear)
108 {
109     if (typeToClear == AllClipRectTypes) {
110         m_clipRectsCache = nullptr;
111     } else {
112         ASSERT(typeToClear < NumCachedClipRectsTypes);
113         RefPtr<ClipRects> dummy;
114         m_clipRectsCache->setClipRects(typeToClear, RespectOverflowClip, dummy);
115         m_clipRectsCache->setClipRects(typeToClear, IgnoreOverflowClip, dummy);
116     }
117 }
118
119 LayoutRect RenderLayerClipper::childrenClipRect() const
120 {
121     // FIXME: border-radius not accounted for.
122     // FIXME: Regions not accounted for.
123     RenderView* renderView = m_renderer->view();
124     RenderLayer* clippingRootLayer = clippingRootForPainting();
125     LayoutRect layerBounds;
126     ClipRect backgroundRect, foregroundRect, outlineRect;
127     ClipRectsContext clipRectsContext(clippingRootLayer, TemporaryClipRects);
128     // Need to use temporary clip rects, because the value of 'dontClipToOverflow' may be different from the painting path (<rdar://problem/11844909>).
129     calculateRects(clipRectsContext, renderView->unscaledDocumentRect(), layerBounds, backgroundRect, foregroundRect, outlineRect);
130     return clippingRootLayer->renderer()->localToAbsoluteQuad(FloatQuad(foregroundRect.rect())).enclosingBoundingBox();
131 }
132
133 LayoutRect RenderLayerClipper::selfClipRect() const
134 {
135     // FIXME: border-radius not accounted for.
136     RenderView* renderView = m_renderer->view();
137     RenderLayer* clippingRootLayer = clippingRootForPainting();
138     LayoutRect layerBounds;
139     ClipRect backgroundRect, foregroundRect, outlineRect;
140     ClipRectsContext clipRectsContext(clippingRootLayer, PaintingClipRects);
141     calculateRects(clipRectsContext, renderView->documentRect(), layerBounds, backgroundRect, foregroundRect, outlineRect);
142     return clippingRootLayer->renderer()->localToAbsoluteQuad(FloatQuad(backgroundRect.rect())).enclosingBoundingBox();
143 }
144
145 LayoutRect RenderLayerClipper::localClipRect() const
146 {
147     // FIXME: border-radius not accounted for.
148     RenderLayer* clippingRootLayer = clippingRootForPainting();
149     LayoutRect layerBounds;
150     ClipRect backgroundRect, foregroundRect, outlineRect;
151     ClipRectsContext clipRectsContext(clippingRootLayer, PaintingClipRects);
152     calculateRects(clipRectsContext, PaintInfo::infiniteRect(), layerBounds, backgroundRect, foregroundRect, outlineRect);
153
154     LayoutRect clipRect = backgroundRect.rect();
155     if (clipRect == PaintInfo::infiniteRect())
156         return clipRect;
157
158     LayoutPoint clippingRootOffset;
159     m_renderer->layer()->convertToLayerCoords(clippingRootLayer, clippingRootOffset);
160     clipRect.moveBy(-clippingRootOffset);
161
162     return clipRect;
163 }
164
165 void RenderLayerClipper::calculateRects(const ClipRectsContext& clipRectsContext, const LayoutRect& paintDirtyRect, LayoutRect& layerBounds,
166     ClipRect& backgroundRect, ClipRect& foregroundRect, ClipRect& outlineRect, const LayoutPoint* offsetFromRoot) const
167 {
168     if (clipRectsContext.rootLayer != m_renderer->layer() && m_renderer->layer()->parent()) {
169         backgroundRect = backgroundClipRect(clipRectsContext);
170         backgroundRect.move(roundedIntSize(clipRectsContext.subPixelAccumulation));
171         backgroundRect.intersect(paintDirtyRect);
172     } else {
173         backgroundRect = paintDirtyRect;
174     }
175
176     foregroundRect = backgroundRect;
177     outlineRect = backgroundRect;
178
179     LayoutPoint offset;
180     if (offsetFromRoot)
181         offset = *offsetFromRoot;
182     else
183         m_renderer->layer()->convertToLayerCoords(clipRectsContext.rootLayer, offset);
184     layerBounds = LayoutRect(offset, m_renderer->layer()->size());
185
186     // Update the clip rects that will be passed to child layers.
187     if (m_renderer->hasOverflowClip()) {
188         // This layer establishes a clip of some kind.
189         if (m_renderer->layer() != clipRectsContext.rootLayer || clipRectsContext.respectOverflowClip == RespectOverflowClip) {
190             foregroundRect.intersect(toRenderBox(m_renderer)->overflowClipRect(offset, clipRectsContext.overlayScrollbarSizeRelevancy));
191             if (m_renderer->style()->hasBorderRadius())
192                 foregroundRect.setHasRadius(true);
193         }
194
195         // If we establish an overflow clip at all, then go ahead and make sure our background
196         // rect is intersected with our layer's bounds including our visual overflow,
197         // since any visual overflow like box-shadow or border-outset is not clipped by overflow:auto/hidden.
198         if (toRenderBox(m_renderer)->hasVisualOverflow()) {
199             // FIXME: Perhaps we should be propagating the borderbox as the clip rect for children, even though
200             //        we may need to inflate our clip specifically for shadows or outsets.
201             // FIXME: Does not do the right thing with CSS regions yet, since we don't yet factor in the
202             // individual region boxes as overflow.
203             LayoutRect layerBoundsWithVisualOverflow = toRenderBox(m_renderer)->visualOverflowRect();
204             toRenderBox(m_renderer)->flipForWritingMode(layerBoundsWithVisualOverflow); // Layers are in physical coordinates, so the overflow has to be flipped.
205             layerBoundsWithVisualOverflow.moveBy(offset);
206             if (m_renderer->layer() != clipRectsContext.rootLayer || clipRectsContext.respectOverflowClip == RespectOverflowClip)
207                 backgroundRect.intersect(layerBoundsWithVisualOverflow);
208         } else {
209             LayoutRect bounds = toRenderBox(m_renderer)->borderBoxRect();
210             bounds.moveBy(offset);
211             if (m_renderer->layer() != clipRectsContext.rootLayer || clipRectsContext.respectOverflowClip == RespectOverflowClip)
212                 backgroundRect.intersect(bounds);
213         }
214     }
215
216     // CSS clip (different than clipping due to overflow) can clip to any box, even if it falls outside of the border box.
217     if (m_renderer->hasClip()) {
218         // Clip applies to *us* as well, so go ahead and update the damageRect.
219         LayoutRect newPosClip = toRenderBox(m_renderer)->clipRect(offset);
220         backgroundRect.intersect(newPosClip);
221         foregroundRect.intersect(newPosClip);
222         outlineRect.intersect(newPosClip);
223     }
224 }
225
226 void RenderLayerClipper::calculateClipRects(const ClipRectsContext& clipRectsContext, ClipRects& clipRects) const
227 {
228     if (!m_renderer->layer()->parent()) {
229         // The root layer's clip rect is always infinite.
230         clipRects.reset(PaintInfo::infiniteRect());
231         return;
232     }
233
234     ClipRectsType clipRectsType = clipRectsContext.clipRectsType;
235     bool useCached = clipRectsType != TemporaryClipRects;
236
237     // For transformed layers, the root layer was shifted to be us, so there is no need to
238     // examine the parent. We want to cache clip rects with us as the root.
239     RenderLayer* parentLayer = clipRectsContext.rootLayer != m_renderer->layer() ? m_renderer->layer()->parent() : 0;
240
241     // Ensure that our parent's clip has been calculated so that we can examine the values.
242     if (parentLayer) {
243         if (useCached && parentLayer->clipper().clipRects(clipRectsContext)) {
244             clipRects = *parentLayer->clipper().clipRects(clipRectsContext);
245         } else {
246             ClipRectsContext parentContext(clipRectsContext);
247             parentContext.overlayScrollbarSizeRelevancy = IgnoreOverlayScrollbarSize; // FIXME: why?
248             parentLayer->clipper().calculateClipRects(parentContext, clipRects);
249         }
250     } else {
251         clipRects.reset(PaintInfo::infiniteRect());
252     }
253
254     // A fixed object is essentially the root of its containing block hierarchy, so when
255     // we encounter such an object, we reset our clip rects to the fixedClipRect.
256     if (m_renderer->style()->position() == FixedPosition) {
257         clipRects.setPosClipRect(clipRects.fixedClipRect());
258         clipRects.setOverflowClipRect(clipRects.fixedClipRect());
259         clipRects.setFixed(true);
260     } else if (m_renderer->style()->hasInFlowPosition()) {
261         clipRects.setPosClipRect(clipRects.overflowClipRect());
262     } else if (m_renderer->style()->position() == AbsolutePosition) {
263         clipRects.setOverflowClipRect(clipRects.posClipRect());
264     }
265
266     // Update the clip rects that will be passed to child layers.
267     if ((m_renderer->hasOverflowClip() && (clipRectsContext.respectOverflowClip == RespectOverflowClip || m_renderer->layer() != clipRectsContext.rootLayer)) || m_renderer->hasClip()) {
268         // This layer establishes a clip of some kind.
269
270         // This offset cannot use convertToLayerCoords, because sometimes our rootLayer may be across
271         // some transformed layer boundary, for example, in the RenderLayerCompositor overlapMap, where
272         // clipRects are needed in view space.
273         LayoutPoint offset;
274         offset = roundedLayoutPoint(m_renderer->localToContainerPoint(FloatPoint(), clipRectsContext.rootLayer->renderer()));
275         RenderView* view = m_renderer->view();
276         ASSERT(view);
277         if (view && clipRects.fixed() && clipRectsContext.rootLayer->renderer() == view) {
278             offset -= view->frameView()->scrollOffsetForFixedPosition();
279         }
280
281         if (m_renderer->hasOverflowClip()) {
282             ClipRect newOverflowClip = toRenderBox(m_renderer)->overflowClipRect(offset, clipRectsContext.overlayScrollbarSizeRelevancy);
283             if (m_renderer->style()->hasBorderRadius())
284                 newOverflowClip.setHasRadius(true);
285             clipRects.setOverflowClipRect(intersection(newOverflowClip, clipRects.overflowClipRect()));
286             if (m_renderer->isPositioned())
287                 clipRects.setPosClipRect(intersection(newOverflowClip, clipRects.posClipRect()));
288         }
289         if (m_renderer->hasClip()) {
290             LayoutRect newPosClip = toRenderBox(m_renderer)->clipRect(offset);
291             clipRects.setPosClipRect(intersection(newPosClip, clipRects.posClipRect()));
292             clipRects.setOverflowClipRect(intersection(newPosClip, clipRects.overflowClipRect()));
293             clipRects.setFixedClipRect(intersection(newPosClip, clipRects.fixedClipRect()));
294         }
295     }
296 }
297
298 static inline ClipRect backgroundClipRectForPosition(const ClipRects& parentRects, EPosition position)
299 {
300     if (position == FixedPosition)
301         return parentRects.fixedClipRect();
302
303     if (position == AbsolutePosition)
304         return parentRects.posClipRect();
305
306     return parentRects.overflowClipRect();
307 }
308
309 ClipRect RenderLayerClipper::backgroundClipRect(const ClipRectsContext& clipRectsContext) const
310 {
311     ASSERT(m_renderer->layer()->parent());
312
313     ClipRects parentRects;
314
315     // If we cross into a different pagination context, then we can't rely on the cache.
316     // Just switch over to using TemporaryClipRects.
317     if (clipRectsContext.clipRectsType != TemporaryClipRects && m_renderer->layer()->parent()->enclosingPaginationLayer() != m_renderer->layer()->enclosingPaginationLayer()) {
318         ClipRectsContext tempContext(clipRectsContext);
319         tempContext.clipRectsType = TemporaryClipRects;
320         parentClipRects(tempContext, parentRects);
321     } else {
322         parentClipRects(clipRectsContext, parentRects);
323     }
324
325     ClipRect backgroundClipRect = backgroundClipRectForPosition(parentRects, m_renderer->style()->position());
326     RenderView* view = m_renderer->view();
327     ASSERT(view);
328
329     // Note: infinite clipRects should not be scrolled here, otherwise they will accidentally no longer be considered infinite.
330     if (parentRects.fixed() && clipRectsContext.rootLayer->renderer() == view && backgroundClipRect != PaintInfo::infiniteRect())
331         backgroundClipRect.move(view->frameView()->scrollOffsetForFixedPosition());
332
333     return backgroundClipRect;
334 }
335
336 void RenderLayerClipper::parentClipRects(const ClipRectsContext& clipRectsContext, ClipRects& clipRects) const
337 {
338     ASSERT(m_renderer->layer()->parent());
339
340     RenderLayerClipper& parentClipper = m_renderer->layer()->parent()->clipper();
341     if (clipRectsContext.clipRectsType == TemporaryClipRects) {
342         parentClipper.calculateClipRects(clipRectsContext, clipRects);
343         return;
344     }
345
346     parentClipper.updateClipRects(clipRectsContext);
347     clipRects = *parentClipper.clipRects(clipRectsContext);
348 }
349
350 RenderLayer* RenderLayerClipper::clippingRootForPainting() const
351 {
352     if (m_renderer->hasCompositedLayerMapping())
353         return const_cast<RenderLayer*>(m_renderer->layer());
354
355     const RenderLayer* current = m_renderer->layer();
356     while (current) {
357         if (current->isRootLayer())
358             return const_cast<RenderLayer*>(current);
359
360         current = current->compositingContainer();
361         ASSERT(current);
362         if (current->transform()
363             || (current->compositingState() == PaintsIntoOwnBacking)
364         )
365             return const_cast<RenderLayer*>(current);
366     }
367
368     ASSERT_NOT_REACHED();
369     return 0;
370 }
371
372 } // namespace WebCore