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 void RenderLayerClipper::updateClipRects(const ClipRectsContext& clipRectsContext)
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);
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;
66 calculateClipRects(tempContext, clipRects);
67 ASSERT(clipRects == *m_clipRectsCache->getClipRects(clipRectsType, clipRectsContext.respectOverflowClip).get());
69 return; // We have the correct cached value.
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;
76 parentLayer->clipper().updateClipRects(clipRectsContext);
79 calculateClipRects(clipRectsContext, clipRects);
81 if (!m_clipRectsCache)
82 m_clipRectsCache = adoptPtr(new ClipRectsCache);
84 if (parentLayer && parentLayer->clipper().clipRects(clipRectsContext) && clipRects == *parentLayer->clipper().clipRects(clipRectsContext))
85 m_clipRectsCache->setClipRects(clipRectsType, clipRectsContext.respectOverflowClip, parentLayer->clipper().clipRects(clipRectsContext));
87 m_clipRectsCache->setClipRects(clipRectsType, clipRectsContext.respectOverflowClip, ClipRects::create(clipRects));
90 m_clipRectsCache->m_clipRectsRoot[clipRectsType] = clipRectsContext.rootLayer;
91 m_clipRectsCache->m_scrollbarRelevancy[clipRectsType] = clipRectsContext.overlayScrollbarSizeRelevancy;
95 void RenderLayerClipper::clearClipRectsIncludingDescendants(ClipRectsType typeToClear)
97 // FIXME: it's not clear how this layer not having clip rects guarantees that no descendants have any.
98 if (!m_clipRectsCache)
101 clearClipRects(typeToClear);
103 for (RenderLayer* layer = m_renderer->layer()->firstChild(); layer; layer = layer->nextSibling())
104 layer->clipper().clearClipRectsIncludingDescendants(typeToClear);
107 void RenderLayerClipper::clearClipRects(ClipRectsType typeToClear)
109 if (typeToClear == AllClipRectTypes) {
110 m_clipRectsCache = nullptr;
112 ASSERT(typeToClear < NumCachedClipRectsTypes);
113 RefPtr<ClipRects> dummy;
114 m_clipRectsCache->setClipRects(typeToClear, RespectOverflowClip, dummy);
115 m_clipRectsCache->setClipRects(typeToClear, IgnoreOverflowClip, dummy);
119 LayoutRect RenderLayerClipper::childrenClipRect() const
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();
133 LayoutRect RenderLayerClipper::selfClipRect() const
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();
145 LayoutRect RenderLayerClipper::localClipRect() const
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);
154 LayoutRect clipRect = backgroundRect.rect();
155 if (clipRect == PaintInfo::infiniteRect())
158 LayoutPoint clippingRootOffset;
159 m_renderer->layer()->convertToLayerCoords(clippingRootLayer, clippingRootOffset);
160 clipRect.moveBy(-clippingRootOffset);
165 void RenderLayerClipper::calculateRects(const ClipRectsContext& clipRectsContext, const LayoutRect& paintDirtyRect, LayoutRect& layerBounds,
166 ClipRect& backgroundRect, ClipRect& foregroundRect, ClipRect& outlineRect, const LayoutPoint* offsetFromRoot) const
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);
173 backgroundRect = paintDirtyRect;
176 foregroundRect = backgroundRect;
177 outlineRect = backgroundRect;
181 offset = *offsetFromRoot;
183 m_renderer->layer()->convertToLayerCoords(clipRectsContext.rootLayer, offset);
184 layerBounds = LayoutRect(offset, m_renderer->layer()->size());
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);
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);
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);
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);
226 void RenderLayerClipper::calculateClipRects(const ClipRectsContext& clipRectsContext, ClipRects& clipRects) const
228 if (!m_renderer->layer()->parent()) {
229 // The root layer's clip rect is always infinite.
230 clipRects.reset(PaintInfo::infiniteRect());
234 ClipRectsType clipRectsType = clipRectsContext.clipRectsType;
235 bool useCached = clipRectsType != TemporaryClipRects;
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;
241 // Ensure that our parent's clip has been calculated so that we can examine the values.
243 if (useCached && parentLayer->clipper().clipRects(clipRectsContext)) {
244 clipRects = *parentLayer->clipper().clipRects(clipRectsContext);
246 ClipRectsContext parentContext(clipRectsContext);
247 parentContext.overlayScrollbarSizeRelevancy = IgnoreOverlayScrollbarSize; // FIXME: why?
248 parentLayer->clipper().calculateClipRects(parentContext, clipRects);
251 clipRects.reset(PaintInfo::infiniteRect());
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());
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.
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.
274 offset = roundedLayoutPoint(m_renderer->localToContainerPoint(FloatPoint(), clipRectsContext.rootLayer->renderer()));
275 RenderView* view = m_renderer->view();
277 if (view && clipRects.fixed() && clipRectsContext.rootLayer->renderer() == view) {
278 offset -= view->frameView()->scrollOffsetForFixedPosition();
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()));
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()));
298 static inline ClipRect backgroundClipRectForPosition(const ClipRects& parentRects, EPosition position)
300 if (position == FixedPosition)
301 return parentRects.fixedClipRect();
303 if (position == AbsolutePosition)
304 return parentRects.posClipRect();
306 return parentRects.overflowClipRect();
309 ClipRect RenderLayerClipper::backgroundClipRect(const ClipRectsContext& clipRectsContext) const
311 ASSERT(m_renderer->layer()->parent());
313 ClipRects parentRects;
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);
322 parentClipRects(clipRectsContext, parentRects);
325 ClipRect backgroundClipRect = backgroundClipRectForPosition(parentRects, m_renderer->style()->position());
326 RenderView* view = m_renderer->view();
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());
333 return backgroundClipRect;
336 void RenderLayerClipper::parentClipRects(const ClipRectsContext& clipRectsContext, ClipRects& clipRects) const
338 ASSERT(m_renderer->layer()->parent());
340 RenderLayerClipper& parentClipper = m_renderer->layer()->parent()->clipper();
341 if (clipRectsContext.clipRectsType == TemporaryClipRects) {
342 parentClipper.calculateClipRects(clipRectsContext, clipRects);
346 parentClipper.updateClipRects(clipRectsContext);
347 clipRects = *parentClipper.clipRects(clipRectsContext);
350 RenderLayer* RenderLayerClipper::clippingRootForPainting() const
352 if (m_renderer->hasCompositedLayerMapping())
353 return const_cast<RenderLayer*>(m_renderer->layer());
355 const RenderLayer* current = m_renderer->layer();
357 if (current->isRootLayer())
358 return const_cast<RenderLayer*>(current);
360 current = current->compositingContainer();
362 if (current->transform()
363 || (current->compositingState() == PaintsIntoOwnBacking)
365 return const_cast<RenderLayer*>(current);
368 ASSERT_NOT_REACHED();
372 } // namespace WebCore