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