2 * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
4 * Portions are Copyright (C) 1998 Netscape Communications Corporation.
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>
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.
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.
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
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.
45 #include "core/rendering/RenderLayerClipper.h"
47 #include "core/rendering/RenderLayer.h"
48 #include "core/rendering/RenderView.h"
52 static void adjustClipRectsForChildren(const RenderObject& renderer, ClipRects& clipRects)
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());
68 static void applyClipRects(const ClipRectsContext& context, RenderObject& renderer, LayoutPoint offset, ClipRects& clipRects)
70 ASSERT(renderer.hasOverflowClip() || renderer.hasClip());
72 RenderView* view = renderer.view();
74 if (clipRects.fixed() && context.rootLayer->renderer() == view)
75 offset -= view->frameView()->scrollOffsetForFixedPosition();
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()));
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()));
93 RenderLayerClipper::RenderLayerClipper(RenderLayerModelObject& renderer)
94 : m_renderer(renderer)
98 ClipRects* RenderLayerClipper::clipRectsIfCached(const ClipRectsContext& context) const
100 ASSERT(context.usesCache());
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)
109 ASSERT(entry.scrollbarRelevancy == context.scrollbarRelevancy);
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;
116 calculateClipRects(tempContext, clipRects);
117 ASSERT(clipRects == *entry.clipRects);
120 return entry.clipRects.get();
123 ClipRects* RenderLayerClipper::storeClipRectsInCache(const ClipRectsContext& context, ClipRects* parentClipRects, const ClipRects& clipRects) const
125 ClipRectsCache::Entry& entry = cache().get(context.cacheSlot);
126 entry.root = context.rootLayer;
128 entry.scrollbarRelevancy = context.scrollbarRelevancy;
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;
139 entry.clipRects = ClipRects::create(clipRects);
140 return entry.clipRects.get();
143 ClipRects* RenderLayerClipper::getClipRects(const ClipRectsContext& context) const
145 if (ClipRects* result = clipRectsIfCached(context))
148 // Note that it's important that we call getClipRects on our parent
149 // before we call calculateClipRects so that calculateClipRects will hit
151 ClipRects* parentClipRects = 0;
152 if (context.rootLayer != m_renderer.layer() && m_renderer.layer()->parent())
153 parentClipRects = m_renderer.layer()->parent()->clipper().getClipRects(context);
156 calculateClipRects(context, clipRects);
157 return storeClipRectsInCache(context, parentClipRects, clipRects);
160 void RenderLayerClipper::clearClipRectsIncludingDescendants()
162 // FIXME: it's not clear how this layer not having clip rects guarantees that no descendants have any.
168 for (RenderLayer* layer = m_renderer.layer()->firstChild(); layer; layer = layer->nextSibling())
169 layer->clipper().clearClipRectsIncludingDescendants();
172 void RenderLayerClipper::clearClipRectsIncludingDescendants(ClipRectsCacheSlot cacheSlot)
174 // FIXME: it's not clear how this layer not having clip rects guarantees that no descendants have any.
178 m_cache->clear(cacheSlot);
180 for (RenderLayer* layer = m_renderer.layer()->firstChild(); layer; layer = layer->nextSibling())
181 layer->clipper().clearClipRectsIncludingDescendants(cacheSlot);
184 LayoutRect RenderLayerClipper::childrenClipRect() const
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();
197 LayoutRect RenderLayerClipper::localClipRect() const
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);
206 LayoutRect clipRect = backgroundRect.rect();
207 if (clipRect == PaintInfo::infiniteRect())
210 LayoutPoint clippingRootOffset;
211 m_renderer.layer()->convertToLayerCoords(clippingRootLayer, clippingRootOffset);
212 clipRect.moveBy(-clippingRootOffset);
217 void RenderLayerClipper::calculateRects(const ClipRectsContext& context, const LayoutRect& paintDirtyRect, LayoutRect& layerBounds,
218 ClipRect& backgroundRect, ClipRect& foregroundRect, ClipRect& outlineRect, const LayoutPoint* offsetFromRoot) const
220 bool isClippingRoot = m_renderer.layer() == context.rootLayer;
222 if (!isClippingRoot && m_renderer.layer()->parent()) {
223 backgroundRect = backgroundClipRect(context);
224 backgroundRect.move(roundedIntSize(context.subPixelAccumulation));
225 backgroundRect.intersect(paintDirtyRect);
227 backgroundRect = paintDirtyRect;
230 foregroundRect = backgroundRect;
231 outlineRect = backgroundRect;
235 offset = *offsetFromRoot;
237 m_renderer.layer()->convertToLayerCoords(context.rootLayer, offset);
238 layerBounds = LayoutRect(offset, m_renderer.layer()->size());
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);
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);
263 LayoutRect bounds = toRenderBox(m_renderer).borderBoxRect();
264 bounds.moveBy(offset);
265 if (!isClippingRoot || context.respectOverflowClip == RespectOverflowClip)
266 backgroundRect.intersect(bounds);
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);
280 void RenderLayerClipper::calculateClipRects(const ClipRectsContext& context, ClipRects& clipRects) const
282 if (!m_renderer.layer()->parent()) {
283 // The root layer's clip rect is always infinite.
284 clipRects.reset(PaintInfo::infiniteRect());
288 bool isClippingRoot = m_renderer.layer() == context.rootLayer;
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;
294 // Ensure that our parent's clip has been calculated so that we can examine the values.
296 // FIXME: Why don't we just call getClipRects here?
297 if (context.usesCache() && parentLayer->clipper().cachedClipRects(context)) {
298 clipRects = *parentLayer->clipper().cachedClipRects(context);
300 parentLayer->clipper().calculateClipRects(context, clipRects);
303 clipRects.reset(PaintInfo::infiniteRect());
306 adjustClipRectsForChildren(m_renderer, clipRects);
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);
317 static ClipRect backgroundClipRectForPosition(const ClipRects& parentRects, EPosition position)
319 if (position == FixedPosition)
320 return parentRects.fixedClipRect();
322 if (position == AbsolutePosition)
323 return parentRects.posClipRect();
325 return parentRects.overflowClipRect();
328 ClipRect RenderLayerClipper::backgroundClipRect(const ClipRectsContext& context) const
330 ASSERT(m_renderer.layer()->parent());
331 ASSERT(m_renderer.view());
333 ClipRects parentClipRects;
334 if (m_renderer.layer() == context.rootLayer)
335 parentClipRects.reset(PaintInfo::infiniteRect());
337 m_renderer.layer()->parent()->clipper().getOrCalculateClipRects(context, parentClipRects);
339 ClipRect result = backgroundClipRectForPosition(parentClipRects, m_renderer.style()->position());
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());
348 void RenderLayerClipper::getOrCalculateClipRects(const ClipRectsContext& context, ClipRects& clipRects) const
350 if (context.usesCache())
351 clipRects = *getClipRects(context);
353 calculateClipRects(context, clipRects);
356 RenderLayer* RenderLayerClipper::clippingRootForPainting() const
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);
367 if (current->isRootLayer())
368 return const_cast<RenderLayer*>(current);
370 current = current->compositingContainer();
372 if (current->transform() || current->isPaintInvalidationContainer())
373 return const_cast<RenderLayer*>(current);
376 ASSERT_NOT_REACHED();