2 * Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 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 "RenderLayer.h"
47 #include "AnimationController.h"
49 #include "ColumnInfo.h"
50 #include "CSSPropertyNames.h"
53 #include "DocumentEventQueue.h"
55 #include "EventHandler.h"
56 #include "FloatConversion.h"
57 #include "FloatPoint3D.h"
58 #include "FloatRect.h"
59 #include "FloatRoundedRect.h"
60 #include "FlowThreadController.h"
61 #include "FocusController.h"
63 #include "FrameLoader.h"
64 #include "FrameLoaderClient.h"
65 #include "FrameSelection.h"
66 #include "FrameTree.h"
67 #include "FrameView.h"
69 #include "GraphicsContext.h"
70 #include "HTMLFrameElement.h"
71 #include "HTMLFrameOwnerElement.h"
72 #include "HTMLNames.h"
73 #include "HistogramSupport.h"
74 #include "HitTestingTransformState.h"
75 #include "HitTestRequest.h"
76 #include "HitTestResult.h"
77 #include "InspectorInstrumentation.h"
78 #include "OverflowEvent.h"
79 #include "OverlapTestRequestClient.h"
81 #include "PlatformMouseEvent.h"
82 #include "RenderFlowThread.h"
83 #include "RenderGeometryMap.h"
84 #include "RenderInline.h"
85 #include "RenderIterator.h"
86 #include "RenderLayerBacking.h"
87 #include "RenderLayerCompositor.h"
88 #include "RenderMarquee.h"
89 #include "RenderMultiColumnFlowThread.h"
90 #include "RenderNamedFlowFragment.h"
91 #include "RenderNamedFlowThread.h"
92 #include "RenderRegion.h"
93 #include "RenderReplica.h"
94 #include "RenderSVGResourceClipper.h"
95 #include "RenderScrollbar.h"
96 #include "RenderScrollbarPart.h"
97 #include "RenderTableCell.h"
98 #include "RenderTableRow.h"
99 #include "RenderTheme.h"
100 #include "RenderTreeAsText.h"
101 #include "RenderView.h"
102 #include "SVGNames.h"
103 #include "ScaleTransformOperation.h"
104 #include "ScrollAnimator.h"
105 #include "Scrollbar.h"
106 #include "ScrollbarTheme.h"
107 #include "ScrollingCoordinator.h"
108 #include "Settings.h"
109 #include "ShadowRoot.h"
110 #include "SourceGraphic.h"
111 #include "StyleProperties.h"
112 #include "StyleResolver.h"
113 #include "TextStream.h"
114 #include "TransformationMatrix.h"
115 #include "TranslateTransformOperation.h"
117 #include <wtf/StdLibExtras.h>
118 #include <wtf/text/CString.h>
120 #if ENABLE(CSS_FILTERS)
121 #include "FEColorMatrix.h"
123 #include "FilterEffectRenderer.h"
124 #include "RenderLayerFilterInfo.h"
127 #define MIN_INTERSECT_FOR_REVEAL 32
131 using namespace HTMLNames;
133 bool ClipRect::intersects(const HitTestLocation& hitTestLocation) const
135 return hitTestLocation.intersects(m_rect);
138 void makeMatrixRenderable(TransformationMatrix& matrix, bool has3DRendering)
140 #if !ENABLE(3D_RENDERING)
141 UNUSED_PARAM(has3DRendering);
149 RenderLayer::RenderLayer(RenderLayerModelObject& rendererLayerModelObject)
150 : m_inResizeMode(false)
151 , m_scrollDimensionsDirty(true)
152 , m_normalFlowListDirty(true)
153 , m_hasSelfPaintingLayerDescendant(false)
154 , m_hasSelfPaintingLayerDescendantDirty(false)
155 , m_hasOutOfFlowPositionedDescendant(false)
156 , m_hasOutOfFlowPositionedDescendantDirty(true)
157 , m_needsCompositedScrolling(false)
158 , m_descendantsAreContiguousInStackingOrder(false)
159 , m_isRootLayer(rendererLayerModelObject.isRenderView())
160 , m_usedTransparency(false)
161 , m_paintingInsideReflection(false)
162 , m_inOverflowRelayout(false)
163 , m_repaintStatus(NeedsNormalRepaint)
164 , m_visibleContentStatusDirty(true)
165 , m_hasVisibleContent(false)
166 , m_visibleDescendantStatusDirty(false)
167 , m_hasVisibleDescendant(false)
168 , m_isPaginated(false)
169 , m_3DTransformedDescendantStatusDirty(true)
170 , m_has3DTransformedDescendant(false)
171 , m_hasCompositingDescendant(false)
172 , m_hasTransformedAncestor(false)
173 , m_has3DTransformedAncestor(false)
174 , m_indirectCompositingReason(NoIndirectCompositingReason)
175 , m_viewportConstrainedNotCompositedReason(NoNotCompositedReason)
177 , m_adjustForIOSCaretWhenScrolling(false)
178 , m_registeredAsTouchEventListenerForScrolling(false)
179 , m_inUserScroll(false)
180 , m_requiresScrollBoundsOriginUpdate(false)
182 , m_containsDirtyOverlayScrollbars(false)
183 , m_updatingMarqueePosition(false)
185 , m_layerListMutationAllowed(true)
187 #if ENABLE(CSS_FILTERS)
188 , m_hasFilterInfo(false)
190 #if ENABLE(CSS_COMPOSITING)
191 , m_blendMode(BlendModeNormal)
192 , m_hasUnisolatedCompositedBlendingDescendants(false)
193 , m_hasUnisolatedBlendingDescendants(false)
194 , m_hasUnisolatedBlendingDescendantsStatusDirty(false)
196 , m_renderer(rendererLayerModelObject)
202 , m_staticInlinePosition(0)
203 , m_staticBlockPosition(0)
204 , m_enclosingPaginationLayer(0)
206 m_isNormalFlowOnly = shouldBeNormalFlowOnly();
207 m_isSelfPaintingLayer = shouldBeSelfPaintingLayer();
209 // Non-stacking containers should have empty z-order lists. As this is already the case,
210 // there is no need to dirty / recompute these lists.
211 m_zOrderListsDirty = isStackingContainer();
213 ScrollableArea::setConstrainsScrollingToContentEdge(false);
215 if (!renderer().firstChild()) {
216 m_visibleContentStatusDirty = false;
217 m_hasVisibleContent = renderer().style().visibility() == VISIBLE;
220 if (Element* element = renderer().element()) {
221 // We save and restore only the scrollOffset as the other scroll values are recalculated.
222 m_scrollOffset = element->savedLayerScrollOffset();
223 if (!m_scrollOffset.isZero())
224 scrollAnimator()->setCurrentPosition(FloatPoint(m_scrollOffset.width(), m_scrollOffset.height()));
225 element->setSavedLayerScrollOffset(IntSize());
229 RenderLayer::~RenderLayer()
231 if (inResizeMode() && !renderer().documentBeingDestroyed())
232 renderer().frame().eventHandler().resizeLayerDestroyed();
234 renderer().view().frameView().removeScrollableArea(this);
236 if (!renderer().documentBeingDestroyed()) {
238 unregisterAsTouchEventListenerForScrolling();
240 if (Element* element = renderer().element())
241 element->setSavedLayerScrollOffset(m_scrollOffset);
244 destroyScrollbar(HorizontalScrollbar);
245 destroyScrollbar(VerticalScrollbar);
247 if (renderer().frame().page()) {
248 if (ScrollingCoordinator* scrollingCoordinator = renderer().frame().page()->scrollingCoordinator())
249 scrollingCoordinator->willDestroyScrollableArea(this);
255 #if ENABLE(CSS_FILTERS)
256 FilterInfo::remove(*this);
259 // Child layers will be deleted by their corresponding render objects, so
260 // we don't need to delete them ourselves.
265 String RenderLayer::name() const
268 name.append(renderer().renderName());
270 if (Element* element = renderer().element()) {
272 name.append(element->tagName());
274 if (element->hasID()) {
275 name.appendLiteral(" id=\'");
276 name.append(element->getIdAttribute());
280 if (element->hasClass()) {
281 name.appendLiteral(" class=\'");
282 for (size_t i = 0; i < element->classNames().size(); ++i) {
285 name.append(element->classNames()[i]);
292 name.appendLiteral(" (reflection)");
294 return name.toString();
297 RenderLayerCompositor& RenderLayer::compositor() const
299 return renderer().view().compositor();
302 void RenderLayer::contentChanged(ContentChangeType changeType)
304 // This can get called when video becomes accelerated, so the layers may change.
305 if ((changeType == CanvasChanged || changeType == VideoChanged || changeType == FullScreenChanged) && compositor().updateLayerCompositingState(*this))
306 compositor().setCompositingLayersNeedRebuild();
309 m_backing->contentChanged(changeType);
312 bool RenderLayer::canRender3DTransforms() const
314 return compositor().canRender3DTransforms();
317 #if ENABLE(CSS_FILTERS)
319 bool RenderLayer::paintsWithFilters() const
321 // FIXME: Eventually there will be cases where we paint with filters even without accelerated compositing,
322 // and this whole function won't be inside the #if below.
324 if (!renderer().hasFilter())
330 if (!m_backing || !m_backing->canCompositeFilters())
336 bool RenderLayer::requiresFullLayerImageForFilters() const
338 if (!paintsWithFilters())
340 FilterEffectRenderer* renderer = filterRenderer();
341 return renderer && renderer->hasFilterThatMovesPixels();
344 FilterEffectRenderer* RenderLayer::filterRenderer() const
346 FilterInfo* filterInfo = FilterInfo::getIfExists(*this);
347 return filterInfo ? filterInfo->renderer() : 0;
352 LayoutPoint RenderLayer::computeOffsetFromRoot(bool& hasLayerOffset) const
354 hasLayerOffset = true;
357 return LayoutPoint();
359 // This is similar to root() but we check if an ancestor layer would
360 // prevent the optimization from working.
361 const RenderLayer* rootLayer = 0;
362 for (const RenderLayer* parentLayer = parent(); parentLayer; rootLayer = parentLayer, parentLayer = parentLayer->parent()) {
363 hasLayerOffset = parentLayer->canUseConvertToLayerCoords();
365 return LayoutPoint();
367 ASSERT(rootLayer == root());
370 parent()->convertToLayerCoords(rootLayer, offset);
374 void RenderLayer::updateLayerPositionsAfterLayout(const RenderLayer* rootLayer, UpdateLayerPositionsFlags flags)
376 RenderGeometryMap geometryMap(UseTransforms);
377 if (this != rootLayer)
378 geometryMap.pushMappingsToAncestor(parent(), 0);
379 updateLayerPositions(&geometryMap, flags);
382 void RenderLayer::updateLayerPositions(RenderGeometryMap* geometryMap, UpdateLayerPositionsFlags flags)
384 updateLayerPosition(); // For relpositioned layers or non-positioned layers,
385 // we need to keep in sync, since we may have shifted relative
386 // to our parent layer.
388 geometryMap->pushMappingsToAncestor(this, parent());
390 // Clear our cached clip rect information.
393 if (hasOverflowControls()) {
394 LayoutPoint offsetFromRoot;
396 offsetFromRoot = LayoutPoint(geometryMap->absolutePoint(FloatPoint()));
398 // FIXME: It looks suspicious to call convertToLayerCoords here
399 // as canUseConvertToLayerCoords may be true for an ancestor layer.
400 convertToLayerCoords(root(), offsetFromRoot);
402 positionOverflowControls(toIntSize(roundedIntPoint(offsetFromRoot)));
405 updateDescendantDependentFlags();
407 if (flags & UpdatePagination)
410 m_isPaginated = false;
411 m_enclosingPaginationLayer = 0;
414 if (m_hasVisibleContent) {
415 // FIXME: LayoutState does not work with RenderLayers as there is not a 1-to-1
416 // mapping between them and the RenderObjects. It would be neat to enable
417 // LayoutState outside the layout() phase and use it here.
418 ASSERT(!renderer().view().layoutStateEnabled());
420 RenderLayerModelObject* repaintContainer = renderer().containerForRepaint();
421 LayoutRect oldRepaintRect = m_repaintRect;
422 LayoutRect oldOutlineBox = m_outlineBox;
423 computeRepaintRects(repaintContainer, geometryMap);
425 // FIXME: Should ASSERT that value calculated for m_outlineBox using the cached offset is the same
426 // as the value not using the cached offset, but we can't due to https://bugs.webkit.org/show_bug.cgi?id=37048
427 if (flags & CheckForRepaint) {
428 if (!renderer().view().printing()) {
429 bool didRepaint = false;
430 if (m_repaintStatus & NeedsFullRepaint) {
431 renderer().repaintUsingContainer(repaintContainer, oldRepaintRect);
432 if (m_repaintRect != oldRepaintRect) {
433 renderer().repaintUsingContainer(repaintContainer, m_repaintRect);
436 } else if (shouldRepaintAfterLayout()) {
437 renderer().repaintAfterLayoutIfNeeded(repaintContainer, oldRepaintRect, oldOutlineBox, &m_repaintRect, &m_outlineBox);
441 if (didRepaint && renderer().isRenderNamedFlowFragmentContainer()) {
442 // If we just repainted a region, we must also repaint the flow thread since it is the one
443 // doing the actual painting of the flowed content.
444 RenderNamedFlowFragment* region = toRenderBlockFlow(&renderer())->renderNamedFlowFragment();
445 if (region->isValid())
446 region->flowThread()->layer()->repaintIncludingDescendants();
453 m_repaintStatus = NeedsNormalRepaint;
454 m_hasTransformedAncestor = flags & SeenTransformedLayer;
455 m_has3DTransformedAncestor = flags & Seen3DTransformedLayer;
457 // Go ahead and update the reflection's position and size.
459 m_reflection->layout();
461 // Clear the IsCompositingUpdateRoot flag once we've found the first compositing layer in this update.
462 bool isUpdateRoot = (flags & IsCompositingUpdateRoot);
464 flags &= ~IsCompositingUpdateRoot;
466 if (useRegionBasedColumns() && renderer().isInFlowRenderFlowThread()) {
468 flags |= UpdatePagination;
471 if (renderer().hasColumns())
472 flags |= UpdatePagination;
475 flags |= SeenTransformedLayer;
476 if (!transform()->isAffine())
477 flags |= Seen3DTransformedLayer;
480 for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
481 child->updateLayerPositions(geometryMap, flags);
483 if ((flags & UpdateCompositingLayers) && isComposited()) {
484 RenderLayerBacking::UpdateAfterLayoutFlags updateFlags = RenderLayerBacking::CompositingChildrenOnly;
485 if (flags & NeedsFullRepaintInBacking)
486 updateFlags |= RenderLayerBacking::NeedsFullRepaint;
488 updateFlags |= RenderLayerBacking::IsUpdateRoot;
489 backing()->updateAfterLayout(updateFlags);
492 // With all our children positioned, now update our marquee if we need to.
494 // FIXME: would like to use TemporaryChange<> but it doesn't work with bitfields.
495 bool oldUpdatingMarqueePosition = m_updatingMarqueePosition;
496 m_updatingMarqueePosition = true;
497 m_marquee->updateMarqueePosition();
498 m_updatingMarqueePosition = oldUpdatingMarqueePosition;
502 geometryMap->popMappingsToAncestor(parent());
505 LayoutRect RenderLayer::repaintRectIncludingNonCompositingDescendants() const
507 LayoutRect repaintRect = m_repaintRect;
508 for (RenderLayer* child = firstChild(); child; child = child->nextSibling()) {
509 // Don't include repaint rects for composited child layers; they will paint themselves and have a different origin.
510 if (child->isComposited())
513 repaintRect.unite(child->repaintRectIncludingNonCompositingDescendants());
518 void RenderLayer::setAncestorChainHasSelfPaintingLayerDescendant()
520 for (RenderLayer* layer = this; layer; layer = layer->parent()) {
521 if (!layer->m_hasSelfPaintingLayerDescendantDirty && layer->hasSelfPaintingLayerDescendant())
524 layer->m_hasSelfPaintingLayerDescendantDirty = false;
525 layer->m_hasSelfPaintingLayerDescendant = true;
529 void RenderLayer::dirtyAncestorChainHasSelfPaintingLayerDescendantStatus()
531 for (RenderLayer* layer = this; layer; layer = layer->parent()) {
532 layer->m_hasSelfPaintingLayerDescendantDirty = true;
533 // If we have reached a self-painting layer, we know our parent should have a self-painting descendant
534 // in this case, there is no need to dirty our ancestors further.
535 if (layer->isSelfPaintingLayer()) {
536 ASSERT(!parent() || parent()->m_hasSelfPaintingLayerDescendantDirty || parent()->hasSelfPaintingLayerDescendant());
542 bool RenderLayer::acceleratedCompositingForOverflowScrollEnabled() const
544 return renderer().frame().settings().acceleratedCompositingForOverflowScrollEnabled();
547 // If we are a stacking container, then this function will determine if our
548 // descendants for a contiguous block in stacking order. This is required in
549 // order for an element to be safely promoted to a stacking container. It is safe
550 // to become a stacking container if this change would not alter the stacking
551 // order of layers on the page. That can only happen if a non-descendant appear
552 // between us and our descendants in stacking order. Here's an example:
562 // I've labeled our normal flow descendants A, B, C, and D, our stacking
563 // container descendants with their z indices, and us with 'this' (we're a
564 // stacking container and our zIndex doesn't matter here). These nodes appear in
565 // three lists: posZOrder, negZOrder, and normal flow (keep in mind that normal
566 // flow layers don't overlap). So if we arrange these lists in order we get our
569 // [-8], [A-D], [0, 2, 5, 7]--> pos z-order.
571 // Neg z-order. <-+ +--> Normal flow descendants.
573 // We can then assign new, 'stacking' order indices to these elements as follows:
575 // [-8], [A-D], [0, 2, 5, 7]
576 // 'Stacking' indices: -1 0 1 2 3 4
578 // Note that the normal flow descendants can share an index because they don't
579 // stack/overlap. Now our problem becomes very simple: a layer can safely become
580 // a stacking container if the stacking-order indices of it and its descendants
581 // appear in a contiguous block in the list of stacking indices. This problem
582 // can be solved very efficiently by calculating the min/max stacking indices in
583 // the subtree, and the number stacking container descendants. Once we have this
584 // information, we know that the subtree's indices form a contiguous block if:
586 // maxStackIndex - minStackIndex == numSCDescendants
588 // So for node A in the example above we would have:
590 // minStackIndex = -1
591 // numSCDecendants = 2
594 // maxStackIndex - minStackIndex == numSCDescendants
595 // ===> 1 - (-1) == 2
598 // Since this is true, A can safely become a stacking container.
599 // Now, for node C we have:
602 // minStackIndex = 0 <-- because C has stacking index 0.
603 // numSCDecendants = 2
606 // maxStackIndex - minStackIndex == numSCDescendants
610 // Since this is false, C cannot be safely promoted to a stacking container. This
611 // happened because of the elements with z-index 5 and 0. Now if 5 had been a
612 // child of C rather than D, and A had no child with Z index 0, we would have had:
615 // minStackIndex = 0 <-- because C has stacking index 0.
616 // numSCDecendants = 3
619 // maxStackIndex - minStackIndex == numSCDescendants
623 // And we would conclude that C could be promoted.
624 void RenderLayer::updateDescendantsAreContiguousInStackingOrder()
626 if (!isStackingContext() || !acceleratedCompositingForOverflowScrollEnabled())
629 ASSERT(!m_normalFlowListDirty);
630 ASSERT(!m_zOrderListsDirty);
632 std::unique_ptr<Vector<RenderLayer*>> posZOrderList;
633 std::unique_ptr<Vector<RenderLayer*>> negZOrderList;
634 rebuildZOrderLists(StopAtStackingContexts, posZOrderList, negZOrderList);
636 // Create a reverse lookup.
637 HashMap<const RenderLayer*, int> lookup;
640 int stackingOrderIndex = -1;
641 size_t listSize = negZOrderList->size();
642 for (size_t i = 0; i < listSize; ++i) {
643 RenderLayer* currentLayer = negZOrderList->at(listSize - i - 1);
644 if (!currentLayer->isStackingContext())
646 lookup.set(currentLayer, stackingOrderIndex--);
651 size_t listSize = posZOrderList->size();
652 int stackingOrderIndex = 1;
653 for (size_t i = 0; i < listSize; ++i) {
654 RenderLayer* currentLayer = posZOrderList->at(i);
655 if (!currentLayer->isStackingContext())
657 lookup.set(currentLayer, stackingOrderIndex++);
664 bool firstIteration = true;
665 updateDescendantsAreContiguousInStackingOrderRecursive(lookup, minIndex, maxIndex, count, firstIteration);
668 void RenderLayer::updateDescendantsAreContiguousInStackingOrderRecursive(const HashMap<const RenderLayer*, int>& lookup, int& minIndex, int& maxIndex, int& count, bool firstIteration)
670 if (isStackingContext() && !firstIteration) {
671 if (lookup.contains(this)) {
672 minIndex = std::min(minIndex, lookup.get(this));
673 maxIndex = std::max(maxIndex, lookup.get(this));
679 for (RenderLayer* child = firstChild(); child; child = child->nextSibling()) {
680 int childMinIndex = 0;
681 int childMaxIndex = 0;
683 child->updateDescendantsAreContiguousInStackingOrderRecursive(lookup, childMinIndex, childMaxIndex, childCount, false);
686 minIndex = std::min(minIndex, childMinIndex);
687 maxIndex = std::max(maxIndex, childMaxIndex);
691 if (!isStackingContext()) {
692 bool newValue = maxIndex - minIndex == count;
693 bool didUpdate = newValue != m_descendantsAreContiguousInStackingOrder;
694 m_descendantsAreContiguousInStackingOrder = newValue;
696 updateNeedsCompositedScrolling();
700 void RenderLayer::computeRepaintRects(const RenderLayerModelObject* repaintContainer, const RenderGeometryMap* geometryMap)
702 ASSERT(!m_visibleContentStatusDirty);
704 m_repaintRect = renderer().clippedOverflowRectForRepaint(repaintContainer);
705 m_outlineBox = renderer().outlineBoundsForRepaint(repaintContainer, geometryMap);
709 void RenderLayer::computeRepaintRectsIncludingDescendants()
711 // FIXME: computeRepaintRects() has to walk up the parent chain for every layer to compute the rects.
712 // We should make this more efficient.
713 // FIXME: it's wrong to call this when layout is not up-to-date, which we do.
714 computeRepaintRects(renderer().containerForRepaint());
716 for (RenderLayer* layer = firstChild(); layer; layer = layer->nextSibling())
717 layer->computeRepaintRectsIncludingDescendants();
720 void RenderLayer::clearRepaintRects()
722 ASSERT(!m_hasVisibleContent);
723 ASSERT(!m_visibleContentStatusDirty);
725 m_repaintRect = LayoutRect();
726 m_outlineBox = LayoutRect();
729 void RenderLayer::updateLayerPositionsAfterDocumentScroll()
731 ASSERT(this == renderer().view().layer());
733 RenderGeometryMap geometryMap(UseTransforms);
734 updateLayerPositionsAfterScroll(&geometryMap);
737 void RenderLayer::updateLayerPositionsAfterOverflowScroll()
739 RenderGeometryMap geometryMap(UseTransforms);
740 if (this != renderer().view().layer())
741 geometryMap.pushMappingsToAncestor(parent(), 0);
743 // FIXME: why is it OK to not check the ancestors of this layer in order to
744 // initialize the HasSeenViewportConstrainedAncestor and HasSeenAncestorWithOverflowClip flags?
745 updateLayerPositionsAfterScroll(&geometryMap, IsOverflowScroll);
748 void RenderLayer::updateLayerPositionsAfterScroll(RenderGeometryMap* geometryMap, UpdateLayerPositionsAfterScrollFlags flags)
750 // FIXME: This shouldn't be needed, but there are some corner cases where
751 // these flags are still dirty. Update so that the check below is valid.
752 updateDescendantDependentFlags();
754 // If we have no visible content and no visible descendants, there is no point recomputing
755 // our rectangles as they will be empty. If our visibility changes, we are expected to
756 // recompute all our positions anyway.
757 if (!m_hasVisibleDescendant && !m_hasVisibleContent)
760 bool positionChanged = updateLayerPosition();
762 flags |= HasChangedAncestor;
765 geometryMap->pushMappingsToAncestor(this, parent());
767 if (flags & HasChangedAncestor || flags & HasSeenViewportConstrainedAncestor || flags & IsOverflowScroll)
770 if (renderer().style().hasViewportConstrainedPosition())
771 flags |= HasSeenViewportConstrainedAncestor;
773 if (renderer().hasOverflowClip())
774 flags |= HasSeenAncestorWithOverflowClip;
776 if (flags & HasSeenViewportConstrainedAncestor
777 || (flags & IsOverflowScroll && flags & HasSeenAncestorWithOverflowClip)) {
778 // FIXME: We could track the repaint container as we walk down the tree.
779 computeRepaintRects(renderer().containerForRepaint(), geometryMap);
781 // Check that our cached rects are correct.
782 ASSERT(m_repaintRect == renderer().clippedOverflowRectForRepaint(renderer().containerForRepaint()));
783 ASSERT(m_outlineBox == renderer().outlineBoundsForRepaint(renderer().containerForRepaint(), geometryMap));
786 for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
787 child->updateLayerPositionsAfterScroll(geometryMap, flags);
789 // We don't update our reflection as scrolling is a translation which does not change the size()
790 // of an object, thus RenderReplica will still repaint itself properly as the layer position was
794 bool oldUpdatingMarqueePosition = m_updatingMarqueePosition;
795 m_updatingMarqueePosition = true;
796 m_marquee->updateMarqueePosition();
797 m_updatingMarqueePosition = oldUpdatingMarqueePosition;
801 geometryMap->popMappingsToAncestor(parent());
804 void RenderLayer::positionNewlyCreatedOverflowControls()
806 if (!backing()->hasUnpositionedOverflowControlsLayers())
809 RenderGeometryMap geometryMap(UseTransforms);
810 if (this != renderer().view().layer() && parent())
811 geometryMap.pushMappingsToAncestor(parent(), 0);
813 LayoutPoint offsetFromRoot = LayoutPoint(geometryMap.absolutePoint(FloatPoint()));
814 positionOverflowControls(toIntSize(roundedIntPoint(offsetFromRoot)));
817 #if ENABLE(CSS_COMPOSITING)
819 void RenderLayer::updateBlendMode()
821 bool hadBlendMode = m_blendMode != BlendModeNormal;
822 if (parent() && hadBlendMode != hasBlendMode()) {
824 parent()->updateAncestorChainHasBlendingDescendants();
826 parent()->dirtyAncestorChainHasBlendingDescendants();
829 BlendMode newBlendMode = renderer().style().blendMode();
830 if (newBlendMode != m_blendMode)
831 m_blendMode = newBlendMode;
834 void RenderLayer::updateAncestorChainHasBlendingDescendants()
836 for (auto layer = this; layer; layer = layer->parent()) {
837 if (!layer->hasUnisolatedBlendingDescendantsStatusDirty() && layer->hasUnisolatedBlendingDescendants())
839 layer->m_hasUnisolatedBlendingDescendants = true;
840 layer->m_hasUnisolatedBlendingDescendantsStatusDirty = false;
842 if (layer->isStackingContext())
847 void RenderLayer::dirtyAncestorChainHasBlendingDescendants()
849 for (auto layer = this; layer; layer = layer->parent()) {
850 if (layer->hasUnisolatedBlendingDescendantsStatusDirty())
853 layer->m_hasUnisolatedBlendingDescendantsStatusDirty = true;
855 if (layer->isStackingContext())
861 void RenderLayer::updateTransform()
863 // hasTransform() on the renderer is also true when there is transform-style: preserve-3d or perspective set,
864 // so check style too.
865 bool hasTransform = renderer().hasTransform() && renderer().style().hasTransform();
866 bool had3DTransform = has3DTransform();
868 bool hadTransform = !!m_transform;
869 if (hasTransform != hadTransform) {
871 m_transform = std::make_unique<TransformationMatrix>();
873 m_transform = nullptr;
875 // Layers with transforms act as clip rects roots, so clear the cached clip rects here.
876 clearClipRectsIncludingDescendants();
880 RenderBox* box = renderBox();
882 m_transform->makeIdentity();
883 box->style().applyTransform(*m_transform, pixelSnappedForPainting(box->borderBoxRect(), box->document().deviceScaleFactor()), RenderStyle::IncludeTransformOrigin);
884 makeMatrixRenderable(*m_transform, canRender3DTransforms());
887 if (had3DTransform != has3DTransform())
888 dirty3DTransformedDescendantStatus();
891 TransformationMatrix RenderLayer::currentTransform(RenderStyle::ApplyTransformOrigin applyOrigin) const
894 return TransformationMatrix();
896 RenderBox* box = renderBox();
898 FloatRect pixelSnappedBorderRect = pixelSnappedForPainting(box->borderBoxRect(), box->document().deviceScaleFactor());
899 if (renderer().style().isRunningAcceleratedAnimation()) {
900 TransformationMatrix currTransform;
901 RefPtr<RenderStyle> style = renderer().animation().getAnimatedStyleForRenderer(&renderer());
902 style->applyTransform(currTransform, pixelSnappedBorderRect, applyOrigin);
903 makeMatrixRenderable(currTransform, canRender3DTransforms());
904 return currTransform;
907 // m_transform includes transform-origin, so we need to recompute the transform here.
908 if (applyOrigin == RenderStyle::ExcludeTransformOrigin) {
909 TransformationMatrix currTransform;
910 box->style().applyTransform(currTransform, pixelSnappedBorderRect, RenderStyle::ExcludeTransformOrigin);
911 makeMatrixRenderable(currTransform, canRender3DTransforms());
912 return currTransform;
918 TransformationMatrix RenderLayer::renderableTransform(PaintBehavior paintBehavior) const
921 return TransformationMatrix();
923 if (paintBehavior & PaintBehaviorFlattenCompositingLayers) {
924 TransformationMatrix matrix = *m_transform;
925 makeMatrixRenderable(matrix, false /* flatten 3d */);
932 RenderLayer* RenderLayer::enclosingOverflowClipLayer(IncludeSelfOrNot includeSelf) const
934 const RenderLayer* layer = (includeSelf == IncludeSelf) ? this : parent();
936 if (layer->renderer().hasOverflowClip())
937 return const_cast<RenderLayer*>(layer);
939 layer = layer->parent();
944 static bool checkContainingBlockChainForPagination(RenderLayerModelObject* renderer, RenderBox* ancestorColumnsRenderer)
946 RenderLayerModelObject* prevBlock = renderer;
947 RenderBlock* containingBlock;
948 for (containingBlock = renderer->containingBlock();
949 containingBlock && containingBlock != &renderer->view() && containingBlock != ancestorColumnsRenderer;
950 containingBlock = containingBlock->containingBlock()) {
951 prevBlock = containingBlock;
954 // If the columns block wasn't in our containing block chain, then we aren't paginated by it.
955 if (containingBlock != ancestorColumnsRenderer)
958 // If the previous block is absolutely positioned, then we can't be paginated by the columns block.
959 if (prevBlock->isOutOfFlowPositioned())
962 // Otherwise we are paginated by the columns block.
966 bool RenderLayer::useRegionBasedColumns() const
968 const Settings& settings = renderer().frame().settings();
969 return settings.regionBasedColumnsEnabled();
972 void RenderLayer::updatePagination()
974 m_isPaginated = false;
975 m_enclosingPaginationLayer = 0;
977 if (isComposited() || !parent())
978 return; // FIXME: We will have to deal with paginated compositing layers someday.
979 // FIXME: For now the RenderView can't be paginated. Eventually printing will move to a model where it is though.
981 // The main difference between the paginated booleans for the old column code and the new column code
982 // is that each paginated layer has to paint on its own with the new code. There is no
983 // recurring into child layers. This means that the m_isPaginated bits for the new column code can't just be set on
984 // "roots" that get split and paint all their descendants. Instead each layer has to be checked individually and
985 // genuinely know if it is going to have to split itself up when painting only its contents (and not any other descendant
986 // layers). We track an enclosingPaginationLayer instead of using a simple bit, since we want to be able to get back
987 // to that layer easily.
988 bool regionBasedColumnsUsed = useRegionBasedColumns();
989 if (regionBasedColumnsUsed && renderer().isInFlowRenderFlowThread()) {
990 m_enclosingPaginationLayer = this;
994 if (isNormalFlowOnly()) {
995 if (regionBasedColumnsUsed) {
996 // Content inside a transform is not considered to be paginated, since we simply
997 // paint the transform multiple times in each column, so we don't have to use
998 // fragments for the transformed content.
999 m_enclosingPaginationLayer = parent()->enclosingPaginationLayer();
1000 if (m_enclosingPaginationLayer && m_enclosingPaginationLayer->hasTransform())
1001 m_enclosingPaginationLayer = 0;
1003 m_isPaginated = parent()->renderer().hasColumns();
1007 // For the new columns code, we want to walk up our containing block chain looking for an enclosing layer. Once
1008 // we find one, then we just check its pagination status.
1009 if (regionBasedColumnsUsed) {
1010 RenderView* renderView = &renderer().view();
1011 RenderBlock* containingBlock;
1012 for (containingBlock = renderer().containingBlock();
1013 containingBlock && containingBlock != renderView;
1014 containingBlock = containingBlock->containingBlock()) {
1015 if (containingBlock->hasLayer()) {
1016 // Content inside a transform is not considered to be paginated, since we simply
1017 // paint the transform multiple times in each column, so we don't have to use
1018 // fragments for the transformed content.
1019 m_enclosingPaginationLayer = containingBlock->layer()->enclosingPaginationLayer();
1020 if (m_enclosingPaginationLayer && m_enclosingPaginationLayer->hasTransform())
1021 m_enclosingPaginationLayer = 0;
1028 // If we're not normal flow, then we need to look for a multi-column object between us and our stacking container.
1029 RenderLayer* ancestorStackingContainer = stackingContainer();
1030 for (RenderLayer* curr = parent(); curr; curr = curr->parent()) {
1031 if (curr->renderer().hasColumns()) {
1032 m_isPaginated = checkContainingBlockChainForPagination(&renderer(), curr->renderBox());
1035 if (curr == ancestorStackingContainer)
1040 bool RenderLayer::canBeStackingContainer() const
1042 if (isStackingContext() || !stackingContainer())
1045 return m_descendantsAreContiguousInStackingOrder;
1048 void RenderLayer::setHasVisibleContent()
1050 if (m_hasVisibleContent && !m_visibleContentStatusDirty) {
1051 ASSERT(!parent() || parent()->hasVisibleDescendant());
1055 m_visibleContentStatusDirty = false;
1056 m_hasVisibleContent = true;
1057 computeRepaintRects(renderer().containerForRepaint());
1058 if (!isNormalFlowOnly()) {
1059 // We don't collect invisible layers in z-order lists if we are not in compositing mode.
1060 // As we became visible, we need to dirty our stacking containers ancestors to be properly
1061 // collected. FIXME: When compositing, we could skip this dirtying phase.
1062 for (RenderLayer* sc = stackingContainer(); sc; sc = sc->stackingContainer()) {
1063 sc->dirtyZOrderLists();
1064 if (sc->hasVisibleContent())
1070 parent()->setAncestorChainHasVisibleDescendant();
1073 void RenderLayer::dirtyVisibleContentStatus()
1075 m_visibleContentStatusDirty = true;
1077 parent()->dirtyAncestorChainVisibleDescendantStatus();
1080 void RenderLayer::dirtyAncestorChainVisibleDescendantStatus()
1082 for (RenderLayer* layer = this; layer; layer = layer->parent()) {
1083 if (layer->m_visibleDescendantStatusDirty)
1086 layer->m_visibleDescendantStatusDirty = true;
1090 void RenderLayer::setAncestorChainHasVisibleDescendant()
1092 for (RenderLayer* layer = this; layer; layer = layer->parent()) {
1093 if (!layer->m_visibleDescendantStatusDirty && layer->hasVisibleDescendant())
1096 layer->m_hasVisibleDescendant = true;
1097 layer->m_visibleDescendantStatusDirty = false;
1101 void RenderLayer::updateDescendantDependentFlags(HashSet<const RenderObject*>* outOfFlowDescendantContainingBlocks)
1103 if (m_visibleDescendantStatusDirty || m_hasSelfPaintingLayerDescendantDirty || m_hasOutOfFlowPositionedDescendantDirty || hasUnisolatedBlendingDescendantsStatusDirty()) {
1104 m_hasVisibleDescendant = false;
1105 m_hasSelfPaintingLayerDescendant = false;
1106 m_hasOutOfFlowPositionedDescendant = false;
1107 #if ENABLE(CSS_COMPOSITING)
1108 m_hasUnisolatedBlendingDescendants = false;
1111 HashSet<const RenderObject*> childOutOfFlowDescendantContainingBlocks;
1112 for (RenderLayer* child = firstChild(); child; child = child->nextSibling()) {
1113 childOutOfFlowDescendantContainingBlocks.clear();
1114 child->updateDescendantDependentFlags(&childOutOfFlowDescendantContainingBlocks);
1116 bool childIsOutOfFlowPositioned = child->renderer().isOutOfFlowPositioned();
1117 if (childIsOutOfFlowPositioned)
1118 childOutOfFlowDescendantContainingBlocks.add(child->renderer().containingBlock());
1120 if (outOfFlowDescendantContainingBlocks) {
1121 HashSet<const RenderObject*>::const_iterator it = childOutOfFlowDescendantContainingBlocks.begin();
1122 for (; it != childOutOfFlowDescendantContainingBlocks.end(); ++it)
1123 outOfFlowDescendantContainingBlocks->add(*it);
1126 bool hasVisibleDescendant = child->m_hasVisibleContent || child->m_hasVisibleDescendant;
1127 bool hasSelfPaintingLayerDescendant = child->isSelfPaintingLayer() || child->hasSelfPaintingLayerDescendant();
1128 bool hasOutOfFlowPositionedDescendant = !childOutOfFlowDescendantContainingBlocks.isEmpty();
1129 #if ENABLE(CSS_COMPOSITING)
1130 bool hasUnisolatedBlendingDescendants = child->hasBlendMode() || (child->hasUnisolatedBlendingDescendants() && !child->isolatesBlending());
1132 m_hasUnisolatedBlendingDescendants |= hasUnisolatedBlendingDescendants;
1134 m_hasVisibleDescendant |= hasVisibleDescendant;
1135 m_hasSelfPaintingLayerDescendant |= hasSelfPaintingLayerDescendant;
1136 m_hasOutOfFlowPositionedDescendant |= hasOutOfFlowPositionedDescendant;
1138 bool allFlagsSet = m_hasVisibleDescendant && m_hasSelfPaintingLayerDescendant && m_hasOutOfFlowPositionedDescendant;
1139 #if ENABLE(CSS_COMPOSITING)
1140 allFlagsSet &= m_hasUnisolatedBlendingDescendants;
1146 if (outOfFlowDescendantContainingBlocks)
1147 outOfFlowDescendantContainingBlocks->remove(&renderer());
1149 m_visibleDescendantStatusDirty = false;
1150 m_hasSelfPaintingLayerDescendantDirty = false;
1152 if (m_hasOutOfFlowPositionedDescendantDirty)
1153 updateNeedsCompositedScrolling();
1155 m_hasOutOfFlowPositionedDescendantDirty = false;
1156 #if ENABLE(CSS_COMPOSITING)
1157 m_hasUnisolatedBlendingDescendantsStatusDirty = false;
1161 if (m_visibleContentStatusDirty) {
1162 if (renderer().style().visibility() == VISIBLE)
1163 m_hasVisibleContent = true;
1165 // layer may be hidden but still have some visible content, check for this
1166 m_hasVisibleContent = false;
1167 RenderObject* r = renderer().firstChild();
1169 if (r->style().visibility() == VISIBLE && !r->hasLayer()) {
1170 m_hasVisibleContent = true;
1173 RenderObject* child = nullptr;
1174 if (!r->hasLayer() && (child = r->firstChildSlow()))
1176 else if (r->nextSibling())
1177 r = r->nextSibling();
1181 if (r == &renderer())
1183 } while (r && !r->nextSibling());
1185 r = r->nextSibling();
1189 m_visibleContentStatusDirty = false;
1193 // Return true if the new clipping behaviour requires layer update.
1194 bool RenderLayer::checkIfDescendantClippingContextNeedsUpdate(bool isClipping)
1196 for (RenderLayer* child = firstChild(); child; child = child->nextSibling()) {
1197 RenderLayerBacking* backing = child->backing();
1198 // Layer subtree needs update when new clipping is added or existing clipping is removed.
1199 if (backing && (isClipping || backing->hasAncestorClippingLayer()))
1202 if (child->checkIfDescendantClippingContextNeedsUpdate(isClipping))
1208 void RenderLayer::dirty3DTransformedDescendantStatus()
1210 RenderLayer* curr = stackingContainer();
1212 curr->m_3DTransformedDescendantStatusDirty = true;
1214 // This propagates up through preserve-3d hierarchies to the enclosing flattening layer.
1215 // Note that preserves3D() creates stacking context, so we can just run up the stacking containers.
1216 while (curr && curr->preserves3D()) {
1217 curr->m_3DTransformedDescendantStatusDirty = true;
1218 curr = curr->stackingContainer();
1222 // Return true if this layer or any preserve-3d descendants have 3d.
1223 bool RenderLayer::update3DTransformedDescendantStatus()
1225 if (m_3DTransformedDescendantStatusDirty) {
1226 m_has3DTransformedDescendant = false;
1228 updateZOrderLists();
1230 // Transformed or preserve-3d descendants can only be in the z-order lists, not
1231 // in the normal flow list, so we only need to check those.
1232 if (Vector<RenderLayer*>* positiveZOrderList = posZOrderList()) {
1233 for (unsigned i = 0; i < positiveZOrderList->size(); ++i)
1234 m_has3DTransformedDescendant |= positiveZOrderList->at(i)->update3DTransformedDescendantStatus();
1237 // Now check our negative z-index children.
1238 if (Vector<RenderLayer*>* negativeZOrderList = negZOrderList()) {
1239 for (unsigned i = 0; i < negativeZOrderList->size(); ++i)
1240 m_has3DTransformedDescendant |= negativeZOrderList->at(i)->update3DTransformedDescendantStatus();
1243 m_3DTransformedDescendantStatusDirty = false;
1246 // If we live in a 3d hierarchy, then the layer at the root of that hierarchy needs
1247 // the m_has3DTransformedDescendant set.
1249 return has3DTransform() || m_has3DTransformedDescendant;
1251 return has3DTransform();
1254 bool RenderLayer::updateLayerPosition()
1256 LayoutPoint localPoint;
1257 LayoutSize inlineBoundingBoxOffset; // We don't put this into the RenderLayer x/y for inlines, so we need to subtract it out when done.
1258 if (renderer().isInline() && renderer().isRenderInline()) {
1259 RenderInline& inlineFlow = toRenderInline(renderer());
1260 IntRect lineBox = inlineFlow.linesBoundingBox();
1261 setSize(lineBox.size());
1262 inlineBoundingBoxOffset = toLayoutSize(lineBox.location());
1263 localPoint += inlineBoundingBoxOffset;
1264 } else if (RenderBox* box = renderBox()) {
1265 // FIXME: Is snapping the size really needed here for the RenderBox case?
1266 setSize(pixelSnappedIntSize(box->size(), box->location()));
1267 localPoint += box->topLeftLocationOffset();
1270 if (!renderer().isOutOfFlowPositioned() && renderer().parent()) {
1271 // We must adjust our position by walking up the render tree looking for the
1272 // nearest enclosing object with a layer.
1273 RenderElement* curr = renderer().parent();
1274 while (curr && !curr->hasLayer()) {
1275 if (curr->isBox() && !curr->isTableRow()) {
1276 // Rows and cells share the same coordinate space (that of the section).
1277 // Omit them when computing our xpos/ypos.
1278 localPoint += toRenderBox(curr)->topLeftLocationOffset();
1280 curr = curr->parent();
1282 if (curr->isBox() && curr->isTableRow()) {
1283 // Put ourselves into the row coordinate space.
1284 localPoint -= toRenderBox(curr)->topLeftLocationOffset();
1288 // Subtract our parent's scroll offset.
1289 if (renderer().isOutOfFlowPositioned() && enclosingPositionedAncestor()) {
1290 RenderLayer* positionedParent = enclosingPositionedAncestor();
1292 // For positioned layers, we subtract out the enclosing positioned layer's scroll offset.
1293 if (positionedParent->renderer().hasOverflowClip()) {
1294 LayoutSize offset = positionedParent->scrolledContentOffset();
1295 localPoint -= offset;
1298 if (renderer().isOutOfFlowPositioned() && positionedParent->renderer().isInFlowPositioned() && positionedParent->renderer().isRenderInline()) {
1299 LayoutSize offset = toRenderInline(positionedParent->renderer()).offsetForInFlowPositionedInline(&toRenderBox(renderer()));
1300 localPoint += offset;
1302 } else if (parent()) {
1303 if (parent()->renderer().hasOverflowClip()) {
1304 IntSize scrollOffset = parent()->scrolledContentOffset();
1305 localPoint -= scrollOffset;
1309 bool positionOrOffsetChanged = false;
1310 if (renderer().isInFlowPositioned()) {
1311 LayoutSize newOffset = toRenderBoxModelObject(renderer()).offsetForInFlowPosition();
1312 positionOrOffsetChanged = newOffset != m_offsetForInFlowPosition;
1313 m_offsetForInFlowPosition = newOffset;
1314 localPoint.move(m_offsetForInFlowPosition);
1316 m_offsetForInFlowPosition = LayoutSize();
1319 // FIXME: We'd really like to just get rid of the concept of a layer rectangle and rely on the renderers.
1320 localPoint -= inlineBoundingBoxOffset;
1322 positionOrOffsetChanged |= location() != localPoint;
1323 setLocation(localPoint);
1324 return positionOrOffsetChanged;
1327 TransformationMatrix RenderLayer::perspectiveTransform() const
1329 if (!renderer().hasTransform())
1330 return TransformationMatrix();
1332 const RenderStyle& style = renderer().style();
1333 if (!style.hasPerspective())
1334 return TransformationMatrix();
1336 // Maybe fetch the perspective from the backing?
1337 const IntRect borderBox = toRenderBox(renderer()).pixelSnappedBorderBoxRect();
1338 const float boxWidth = borderBox.width();
1339 const float boxHeight = borderBox.height();
1341 float perspectiveOriginX = floatValueForLength(style.perspectiveOriginX(), boxWidth);
1342 float perspectiveOriginY = floatValueForLength(style.perspectiveOriginY(), boxHeight);
1344 // A perspective origin of 0,0 makes the vanishing point in the center of the element.
1345 // We want it to be in the top-left, so subtract half the height and width.
1346 perspectiveOriginX -= boxWidth / 2.0f;
1347 perspectiveOriginY -= boxHeight / 2.0f;
1349 TransformationMatrix t;
1350 t.translate(perspectiveOriginX, perspectiveOriginY);
1351 t.applyPerspective(style.perspective());
1352 t.translate(-perspectiveOriginX, -perspectiveOriginY);
1357 FloatPoint RenderLayer::perspectiveOrigin() const
1359 if (!renderer().hasTransform())
1360 return FloatPoint();
1362 const LayoutRect borderBox = toRenderBox(renderer()).borderBoxRect();
1363 const RenderStyle& style = renderer().style();
1365 return FloatPoint(floatValueForLength(style.perspectiveOriginX(), borderBox.width()),
1366 floatValueForLength(style.perspectiveOriginY(), borderBox.height()));
1369 RenderLayer* RenderLayer::stackingContainer() const
1371 RenderLayer* layer = parent();
1372 while (layer && !layer->isStackingContainer())
1373 layer = layer->parent();
1375 ASSERT(!layer || layer->isStackingContainer());
1379 static inline bool isPositionedContainer(RenderLayer* layer)
1381 return layer->isRootLayer() || layer->renderer().isPositioned() || layer->hasTransform();
1384 static inline bool isFixedPositionedContainer(RenderLayer* layer)
1386 return layer->isRootLayer() || layer->hasTransform();
1389 RenderLayer* RenderLayer::enclosingPositionedAncestor() const
1391 RenderLayer* curr = parent();
1392 while (curr && !isPositionedContainer(curr))
1393 curr = curr->parent();
1398 static RenderLayer* parentLayerCrossFrame(const RenderLayer* layer)
1401 if (layer->parent())
1402 return layer->parent();
1404 HTMLFrameOwnerElement* ownerElement = layer->renderer().document().ownerElement();
1408 RenderElement* ownerRenderer = ownerElement->renderer();
1412 return ownerRenderer->enclosingLayer();
1415 RenderLayer* RenderLayer::enclosingScrollableLayer() const
1417 for (RenderLayer* nextLayer = parentLayerCrossFrame(this); nextLayer; nextLayer = parentLayerCrossFrame(nextLayer)) {
1418 if (nextLayer->renderer().isBox() && toRenderBox(nextLayer->renderer()).canBeScrolledAndHasScrollableArea())
1425 IntRect RenderLayer::scrollableAreaBoundingBox() const
1427 return renderer().absoluteBoundingBoxRect();
1430 RenderLayer* RenderLayer::enclosingTransformedAncestor() const
1432 RenderLayer* curr = parent();
1433 while (curr && !curr->isRootLayer() && !curr->transform())
1434 curr = curr->parent();
1439 static inline const RenderLayer* compositingContainer(const RenderLayer* layer)
1441 return layer->isNormalFlowOnly() ? layer->parent() : layer->stackingContainer();
1444 inline bool RenderLayer::shouldRepaintAfterLayout() const
1446 if (m_repaintStatus == NeedsNormalRepaint)
1449 // Composited layers that were moved during a positioned movement only
1450 // layout, don't need to be repainted. They just need to be recomposited.
1451 ASSERT(m_repaintStatus == NeedsFullRepaintForPositionedMovementLayout);
1452 return !isComposited() || backing()->paintsIntoCompositedAncestor();
1455 static bool compositedWithOwnBackingStore(const RenderLayer* layer)
1457 return layer->isComposited() && !layer->backing()->paintsIntoCompositedAncestor();
1460 RenderLayer* RenderLayer::enclosingCompositingLayer(IncludeSelfOrNot includeSelf) const
1462 if (includeSelf == IncludeSelf && isComposited())
1463 return const_cast<RenderLayer*>(this);
1465 for (const RenderLayer* curr = compositingContainer(this); curr; curr = compositingContainer(curr)) {
1466 if (curr->isComposited())
1467 return const_cast<RenderLayer*>(curr);
1473 RenderLayer* RenderLayer::enclosingCompositingLayerForRepaint(IncludeSelfOrNot includeSelf) const
1475 if (includeSelf == IncludeSelf && isComposited() && !backing()->paintsIntoCompositedAncestor())
1476 return const_cast<RenderLayer*>(this);
1478 for (const RenderLayer* curr = compositingContainer(this); curr; curr = compositingContainer(curr)) {
1479 if (compositedWithOwnBackingStore(curr))
1480 return const_cast<RenderLayer*>(curr);
1486 #if ENABLE(CSS_FILTERS)
1488 RenderLayer* RenderLayer::enclosingFilterLayer(IncludeSelfOrNot includeSelf) const
1490 const RenderLayer* curr = (includeSelf == IncludeSelf) ? this : parent();
1491 for (; curr; curr = curr->parent()) {
1492 if (curr->requiresFullLayerImageForFilters())
1493 return const_cast<RenderLayer*>(curr);
1499 RenderLayer* RenderLayer::enclosingFilterRepaintLayer() const
1501 for (const RenderLayer* curr = this; curr; curr = curr->parent()) {
1502 if ((curr != this && curr->requiresFullLayerImageForFilters()) || compositedWithOwnBackingStore(curr) || curr->isRootLayer())
1503 return const_cast<RenderLayer*>(curr);
1508 void RenderLayer::setFilterBackendNeedsRepaintingInRect(const LayoutRect& rect)
1513 LayoutRect rectForRepaint = rect;
1514 renderer().style().filterOutsets().expandRect(rectForRepaint);
1516 FilterInfo& filterInfo = FilterInfo::get(*this);
1517 filterInfo.expandDirtySourceRect(rectForRepaint);
1519 RenderLayer* parentLayer = enclosingFilterRepaintLayer();
1520 ASSERT(parentLayer);
1521 FloatQuad repaintQuad(rectForRepaint);
1522 LayoutRect parentLayerRect = renderer().localToContainerQuad(repaintQuad, &parentLayer->renderer()).enclosingBoundingBox();
1524 if (parentLayer->isComposited()) {
1525 if (!parentLayer->backing()->paintsIntoWindow()) {
1526 parentLayer->setBackingNeedsRepaintInRect(parentLayerRect);
1529 // If the painting goes to window, redirect the painting to the parent RenderView.
1530 parentLayer = renderer().view().layer();
1531 parentLayerRect = renderer().localToContainerQuad(repaintQuad, &parentLayer->renderer()).enclosingBoundingBox();
1534 if (parentLayer->paintsWithFilters()) {
1535 parentLayer->setFilterBackendNeedsRepaintingInRect(parentLayerRect);
1539 if (parentLayer->isRootLayer()) {
1540 toRenderView(parentLayer->renderer()).repaintViewRectangle(parentLayerRect);
1544 ASSERT_NOT_REACHED();
1547 bool RenderLayer::hasAncestorWithFilterOutsets() const
1549 for (const RenderLayer* curr = this; curr; curr = curr->parent()) {
1550 if (curr->renderer().style().hasFilterOutsets())
1558 RenderLayer* RenderLayer::clippingRootForPainting() const
1561 return const_cast<RenderLayer*>(this);
1563 const RenderLayer* current = this;
1565 if (current->isRootLayer())
1566 return const_cast<RenderLayer*>(current);
1568 current = compositingContainer(current);
1570 if (current->transform() || compositedWithOwnBackingStore(current))
1571 return const_cast<RenderLayer*>(current);
1574 ASSERT_NOT_REACHED();
1578 LayoutPoint RenderLayer::absoluteToContents(const LayoutPoint& absolutePoint) const
1580 // We don't use convertToLayerCoords because it doesn't know about transforms
1581 return roundedLayoutPoint(renderer().absoluteToLocal(absolutePoint, UseTransforms));
1584 bool RenderLayer::cannotBlitToWindow() const
1586 if (isTransparent() || hasReflection() || hasTransform())
1590 return parent()->cannotBlitToWindow();
1593 bool RenderLayer::isTransparent() const
1595 if (renderer().element() && renderer().element()->isSVGElement())
1597 return renderer().isTransparent() || renderer().hasMask();
1600 RenderLayer* RenderLayer::transparentPaintingAncestor()
1605 for (RenderLayer* curr = parent(); curr; curr = curr->parent()) {
1606 if (curr->isComposited())
1608 if (curr->isTransparent())
1614 enum TransparencyClipBoxBehavior {
1615 PaintingTransparencyClipBox,
1616 HitTestingTransparencyClipBox
1619 enum TransparencyClipBoxMode {
1620 DescendantsOfTransparencyClipBox,
1621 RootOfTransparencyClipBox
1624 static LayoutRect transparencyClipBox(const RenderLayer*, const RenderLayer* rootLayer, TransparencyClipBoxBehavior, TransparencyClipBoxMode, PaintBehavior = 0);
1626 static void expandClipRectForRegionAndReflection(LayoutRect& clipRect, const RenderLayer* layer, const RenderLayer* rootLayer,
1627 TransparencyClipBoxBehavior transparencyBehavior, PaintBehavior paintBehavior)
1629 // If this is a region, then the painting is actually done by its flow thread's layer.
1630 if (layer->renderer().isRenderNamedFlowFragmentContainer()) {
1631 RenderBlockFlow* regionContainer = toRenderBlockFlow(&layer->renderer());
1632 RenderNamedFlowFragment* region = regionContainer->renderNamedFlowFragment();
1633 RenderLayer* flowThreadLayer = region->flowThread()->layer();
1634 if (!layer->reflection() || layer->reflectionLayer() != flowThreadLayer) {
1635 LayoutRect flowThreadClipRect = transparencyClipBox(flowThreadLayer, rootLayer, transparencyBehavior, DescendantsOfTransparencyClipBox, paintBehavior);
1637 LayoutPoint offsetFromRoot;
1638 layer->convertToLayerCoords(flowThreadLayer, offsetFromRoot);
1640 LayoutSize moveOffset = (offsetFromRoot + regionContainer->contentBoxRect().location()) - region->flowThreadPortionRect().location();
1641 flowThreadClipRect.move(moveOffset);
1643 clipRect.unite(flowThreadClipRect);
1648 static void expandClipRectForDescendantsAndReflection(LayoutRect& clipRect, const RenderLayer* layer, const RenderLayer* rootLayer,
1649 TransparencyClipBoxBehavior transparencyBehavior, PaintBehavior paintBehavior)
1651 // If we have a mask, then the clip is limited to the border box area (and there is
1652 // no need to examine child layers).
1653 if (!layer->renderer().hasMask()) {
1654 // Note: we don't have to walk z-order lists since transparent elements always establish
1655 // a stacking container. This means we can just walk the layer tree directly.
1656 for (RenderLayer* curr = layer->firstChild(); curr; curr = curr->nextSibling()) {
1657 if (!layer->reflection() || layer->reflectionLayer() != curr)
1658 clipRect.unite(transparencyClipBox(curr, rootLayer, transparencyBehavior, DescendantsOfTransparencyClipBox, paintBehavior));
1662 expandClipRectForRegionAndReflection(clipRect, layer, rootLayer, transparencyBehavior, paintBehavior);
1664 // If we have a reflection, then we need to account for that when we push the clip. Reflect our entire
1665 // current transparencyClipBox to catch all child layers.
1666 // FIXME: Accelerated compositing will eventually want to do something smart here to avoid incorporating this
1667 // size into the parent layer.
1668 if (layer->renderer().hasReflection()) {
1670 layer->convertToLayerCoords(rootLayer, delta);
1671 clipRect.move(-delta.x(), -delta.y());
1672 clipRect.unite(layer->renderBox()->reflectedRect(clipRect));
1673 clipRect.moveBy(delta);
1677 static LayoutRect transparencyClipBox(const RenderLayer* layer, const RenderLayer* rootLayer, TransparencyClipBoxBehavior transparencyBehavior,
1678 TransparencyClipBoxMode transparencyMode, PaintBehavior paintBehavior)
1680 // FIXME: Although this function completely ignores CSS-imposed clipping, we did already intersect with the
1681 // paintDirtyRect, and that should cut down on the amount we have to paint. Still it
1682 // would be better to respect clips.
1684 if (rootLayer != layer && ((transparencyBehavior == PaintingTransparencyClipBox && layer->paintsWithTransform(paintBehavior))
1685 || (transparencyBehavior == HitTestingTransparencyClipBox && layer->hasTransform()))) {
1686 // The best we can do here is to use enclosed bounding boxes to establish a "fuzzy" enough clip to encompass
1687 // the transformed layer and all of its children.
1688 const RenderLayer* paginationLayer = transparencyMode == DescendantsOfTransparencyClipBox ? layer->enclosingPaginationLayer() : 0;
1689 const RenderLayer* rootLayerForTransform = paginationLayer ? paginationLayer : rootLayer;
1691 layer->convertToLayerCoords(rootLayerForTransform, delta);
1693 TransformationMatrix transform;
1694 transform.translate(delta.x(), delta.y());
1695 transform = transform * *layer->transform();
1697 // We don't use fragment boxes when collecting a transformed layer's bounding box, since it always
1698 // paints unfragmented.
1699 LayoutRect clipRect = layer->boundingBox(layer);
1700 expandClipRectForDescendantsAndReflection(clipRect, layer, layer, transparencyBehavior, paintBehavior);
1701 #if ENABLE(CSS_FILTERS)
1702 layer->renderer().style().filterOutsets().expandRect(clipRect);
1704 LayoutRect result = transform.mapRect(clipRect);
1705 if (!paginationLayer)
1708 // We have to break up the transformed extent across our columns.
1709 // Split our box up into the actual fragment boxes that render in the columns/pages and unite those together to
1710 // get our true bounding box.
1711 RenderFlowThread& enclosingFlowThread = toRenderFlowThread(paginationLayer->renderer());
1712 result = enclosingFlowThread.fragmentsBoundingBox(result);
1714 LayoutPoint rootLayerDelta;
1715 paginationLayer->convertToLayerCoords(rootLayer, rootLayerDelta);
1716 result.moveBy(rootLayerDelta);
1720 LayoutRect clipRect = layer->boundingBox(rootLayer, RenderLayer::UseFragmentBoxes);
1721 expandClipRectForDescendantsAndReflection(clipRect, layer, rootLayer, transparencyBehavior, paintBehavior);
1722 #if ENABLE(CSS_FILTERS)
1723 layer->renderer().style().filterOutsets().expandRect(clipRect);
1728 LayoutRect RenderLayer::paintingExtent(const RenderLayer* rootLayer, const LayoutRect& paintDirtyRect, PaintBehavior paintBehavior)
1730 return intersection(transparencyClipBox(this, rootLayer, PaintingTransparencyClipBox, RootOfTransparencyClipBox, paintBehavior), paintDirtyRect);
1733 void RenderLayer::beginTransparencyLayers(GraphicsContext* context, const RenderLayer* rootLayer, const LayoutRect& paintDirtyRect, PaintBehavior paintBehavior)
1735 if (context->paintingDisabled() || (paintsWithTransparency(paintBehavior) && m_usedTransparency))
1738 RenderLayer* ancestor = transparentPaintingAncestor();
1740 ancestor->beginTransparencyLayers(context, rootLayer, paintDirtyRect, paintBehavior);
1742 if (paintsWithTransparency(paintBehavior)) {
1743 m_usedTransparency = true;
1745 LayoutRect clipRect = paintingExtent(rootLayer, paintDirtyRect, paintBehavior);
1746 context->clip(clipRect);
1748 #if ENABLE(CSS_COMPOSITING)
1750 context->setCompositeOperation(context->compositeOperation(), m_blendMode);
1753 context->beginTransparencyLayer(renderer().opacity());
1755 #if ENABLE(CSS_COMPOSITING)
1757 context->setCompositeOperation(context->compositeOperation(), BlendModeNormal);
1760 #ifdef REVEAL_TRANSPARENCY_LAYERS
1761 context->setFillColor(Color(0.0f, 0.0f, 0.5f, 0.2f), ColorSpaceDeviceRGB);
1762 context->fillRect(clipRect);
1768 void RenderLayer::willBeDestroyed()
1770 if (RenderLayerBacking* layerBacking = backing())
1771 layerBacking->layerWillBeDestroyed();
1775 void RenderLayer::addChild(RenderLayer* child, RenderLayer* beforeChild)
1777 RenderLayer* prevSibling = beforeChild ? beforeChild->previousSibling() : lastChild();
1779 child->setPreviousSibling(prevSibling);
1780 prevSibling->setNextSibling(child);
1781 ASSERT(prevSibling != child);
1783 setFirstChild(child);
1786 beforeChild->setPreviousSibling(child);
1787 child->setNextSibling(beforeChild);
1788 ASSERT(beforeChild != child);
1790 setLastChild(child);
1792 child->setParent(this);
1794 if (child->isNormalFlowOnly())
1795 dirtyNormalFlowList();
1797 if (!child->isNormalFlowOnly() || child->firstChild()) {
1798 // Dirty the z-order list in which we are contained. The stackingContainer() can be null in the
1799 // case where we're building up generated content layers. This is ok, since the lists will start
1800 // off dirty in that case anyway.
1801 child->dirtyStackingContainerZOrderLists();
1804 child->updateDescendantDependentFlags();
1805 if (child->m_hasVisibleContent || child->m_hasVisibleDescendant)
1806 setAncestorChainHasVisibleDescendant();
1808 if (child->isSelfPaintingLayer() || child->hasSelfPaintingLayerDescendant())
1809 setAncestorChainHasSelfPaintingLayerDescendant();
1811 if (child->renderer().isOutOfFlowPositioned() || child->hasOutOfFlowPositionedDescendant())
1812 setAncestorChainHasOutOfFlowPositionedDescendant(child->renderer().containingBlock());
1814 #if ENABLE(CSS_COMPOSITING)
1815 if (child->hasBlendMode() || (child->hasUnisolatedBlendingDescendants() && !child->isolatesBlending()))
1816 updateAncestorChainHasBlendingDescendants();
1819 compositor().layerWasAdded(*this, *child);
1822 RenderLayer* RenderLayer::removeChild(RenderLayer* oldChild)
1824 if (!renderer().documentBeingDestroyed())
1825 compositor().layerWillBeRemoved(*this, *oldChild);
1828 if (oldChild->previousSibling())
1829 oldChild->previousSibling()->setNextSibling(oldChild->nextSibling());
1830 if (oldChild->nextSibling())
1831 oldChild->nextSibling()->setPreviousSibling(oldChild->previousSibling());
1833 if (m_first == oldChild)
1834 m_first = oldChild->nextSibling();
1835 if (m_last == oldChild)
1836 m_last = oldChild->previousSibling();
1838 if (oldChild->isNormalFlowOnly())
1839 dirtyNormalFlowList();
1840 if (!oldChild->isNormalFlowOnly() || oldChild->firstChild()) {
1841 // Dirty the z-order list in which we are contained. When called via the
1842 // reattachment process in removeOnlyThisLayer, the layer may already be disconnected
1843 // from the main layer tree, so we need to null-check the |stackingContainer| value.
1844 oldChild->dirtyStackingContainerZOrderLists();
1847 if (oldChild->renderer().isOutOfFlowPositioned() || oldChild->hasOutOfFlowPositionedDescendant())
1848 dirtyAncestorChainHasOutOfFlowPositionedDescendantStatus();
1850 oldChild->setPreviousSibling(0);
1851 oldChild->setNextSibling(0);
1852 oldChild->setParent(0);
1854 oldChild->updateDescendantDependentFlags();
1855 if (oldChild->m_hasVisibleContent || oldChild->m_hasVisibleDescendant)
1856 dirtyAncestorChainVisibleDescendantStatus();
1858 if (oldChild->isSelfPaintingLayer() || oldChild->hasSelfPaintingLayerDescendant())
1859 dirtyAncestorChainHasSelfPaintingLayerDescendantStatus();
1861 #if ENABLE(CSS_COMPOSITING)
1862 if (oldChild->hasBlendMode() || (oldChild->hasUnisolatedBlendingDescendants() && !oldChild->isolatesBlending()))
1863 dirtyAncestorChainHasBlendingDescendants();
1869 void RenderLayer::removeOnlyThisLayer()
1874 // Mark that we are about to lose our layer. This makes render tree
1875 // walks ignore this layer while we're removing it.
1876 renderer().setHasLayer(false);
1878 compositor().layerWillBeRemoved(*m_parent, *this);
1880 // Dirty the clip rects.
1881 clearClipRectsIncludingDescendants();
1883 RenderLayer* nextSib = nextSibling();
1885 // Remove the child reflection layer before moving other child layers.
1886 // The reflection layer should not be moved to the parent.
1888 removeChild(reflectionLayer());
1890 // Now walk our kids and reattach them to our parent.
1891 RenderLayer* current = m_first;
1893 RenderLayer* next = current->nextSibling();
1894 removeChild(current);
1895 m_parent->addChild(current, nextSib);
1896 current->setRepaintStatus(NeedsFullRepaint);
1897 // updateLayerPositions depends on hasLayer() already being false for proper layout.
1898 ASSERT(!renderer().hasLayer());
1899 current->updateLayerPositions(0); // FIXME: use geometry map.
1903 // Remove us from the parent.
1904 m_parent->removeChild(this);
1905 renderer().destroyLayer();
1908 void RenderLayer::insertOnlyThisLayer()
1910 if (!m_parent && renderer().parent()) {
1911 // We need to connect ourselves when our renderer() has a parent.
1912 // Find our enclosingLayer and add ourselves.
1913 RenderLayer* parentLayer = renderer().parent()->enclosingLayer();
1914 ASSERT(parentLayer);
1915 RenderLayer* beforeChild = parentLayer->reflectionLayer() != this ? renderer().parent()->findNextLayer(parentLayer, &renderer()) : 0;
1916 parentLayer->addChild(this, beforeChild);
1919 // Remove all descendant layers from the hierarchy and add them to the new position.
1920 for (auto& child : childrenOfType<RenderElement>(renderer()))
1921 child.moveLayers(m_parent, this);
1923 // Clear out all the clip rects.
1924 clearClipRectsIncludingDescendants();
1927 void RenderLayer::convertToPixelSnappedLayerCoords(const RenderLayer* ancestorLayer, IntPoint& roundedLocation, ColumnOffsetAdjustment adjustForColumns) const
1929 LayoutPoint location = roundedLocation;
1930 convertToLayerCoords(ancestorLayer, location, adjustForColumns);
1931 roundedLocation = roundedIntPoint(location);
1934 void RenderLayer::convertToPixelSnappedLayerCoords(const RenderLayer* ancestorLayer, IntRect& roundedRect, ColumnOffsetAdjustment adjustForColumns) const
1936 LayoutRect rect = roundedRect;
1937 convertToLayerCoords(ancestorLayer, rect, adjustForColumns);
1938 roundedRect = pixelSnappedIntRect(rect);
1941 // Returns the layer reached on the walk up towards the ancestor.
1942 static inline const RenderLayer* accumulateOffsetTowardsAncestor(const RenderLayer* layer, const RenderLayer* ancestorLayer, LayoutPoint& location, RenderLayer::ColumnOffsetAdjustment adjustForColumns)
1944 ASSERT(ancestorLayer != layer);
1946 const RenderLayerModelObject& renderer = layer->renderer();
1947 EPosition position = renderer.style().position();
1949 // FIXME: Special casing RenderFlowThread so much for fixed positioning here is not great.
1950 RenderFlowThread* fixedFlowThreadContainer = position == FixedPosition ? renderer.flowThreadContainingBlock() : 0;
1951 if (fixedFlowThreadContainer && !fixedFlowThreadContainer->isOutOfFlowPositioned())
1952 fixedFlowThreadContainer = 0;
1954 // FIXME: Positioning of out-of-flow(fixed, absolute) elements collected in a RenderFlowThread
1955 // may need to be revisited in a future patch.
1956 // If the fixed renderer is inside a RenderFlowThread, we should not compute location using localToAbsolute,
1957 // since localToAbsolute maps the coordinates from named flow to regions coordinates and regions can be
1958 // positioned in a completely different place in the viewport (RenderView).
1959 if (position == FixedPosition && !fixedFlowThreadContainer && (!ancestorLayer || ancestorLayer == renderer.view().layer())) {
1960 // If the fixed layer's container is the root, just add in the offset of the view. We can obtain this by calling
1961 // localToAbsolute() on the RenderView.
1962 FloatPoint absPos = renderer.localToAbsolute(FloatPoint(), IsFixed);
1963 location += LayoutSize(absPos.x(), absPos.y());
1964 return ancestorLayer;
1967 // For the fixed positioned elements inside a render flow thread, we should also skip the code path below
1968 // Otherwise, for the case of ancestorLayer == rootLayer and fixed positioned element child of a transformed
1969 // element in render flow thread, we will hit the fixed positioned container before hitting the ancestor layer.
1970 if (position == FixedPosition && !fixedFlowThreadContainer) {
1971 // For a fixed layers, we need to walk up to the root to see if there's a fixed position container
1972 // (e.g. a transformed layer). It's an error to call convertToLayerCoords() across a layer with a transform,
1973 // so we should always find the ancestor at or before we find the fixed position container.
1974 RenderLayer* fixedPositionContainerLayer = 0;
1975 bool foundAncestor = false;
1976 for (RenderLayer* currLayer = layer->parent(); currLayer; currLayer = currLayer->parent()) {
1977 if (currLayer == ancestorLayer)
1978 foundAncestor = true;
1980 if (isFixedPositionedContainer(currLayer)) {
1981 fixedPositionContainerLayer = currLayer;
1982 ASSERT_UNUSED(foundAncestor, foundAncestor);
1987 ASSERT(fixedPositionContainerLayer); // We should have hit the RenderView's layer at least.
1989 if (fixedPositionContainerLayer != ancestorLayer) {
1990 LayoutPoint fixedContainerCoords;
1991 layer->convertToLayerCoords(fixedPositionContainerLayer, fixedContainerCoords);
1993 LayoutPoint ancestorCoords;
1994 ancestorLayer->convertToLayerCoords(fixedPositionContainerLayer, ancestorCoords);
1996 location += (fixedContainerCoords - ancestorCoords);
1997 return ancestorLayer;
2001 if (position == FixedPosition && fixedFlowThreadContainer) {
2002 ASSERT(ancestorLayer);
2003 if (ancestorLayer->isOutOfFlowRenderFlowThread()) {
2004 location += toLayoutSize(layer->location());
2005 return ancestorLayer;
2008 if (ancestorLayer == renderer.view().layer()) {
2009 // Add location in flow thread coordinates.
2010 location += toLayoutSize(layer->location());
2012 // Add flow thread offset in view coordinates since the view may be scrolled.
2013 FloatPoint absPos = renderer.view().localToAbsolute(FloatPoint(), IsFixed);
2014 location += LayoutSize(absPos.x(), absPos.y());
2015 return ancestorLayer;
2019 RenderLayer* parentLayer;
2020 if (position == AbsolutePosition || position == FixedPosition) {
2021 // Do what enclosingPositionedAncestor() does, but check for ancestorLayer along the way.
2022 parentLayer = layer->parent();
2023 bool foundAncestorFirst = false;
2024 while (parentLayer) {
2025 // RenderFlowThread is a positioned container, child of RenderView, positioned at (0,0).
2026 // This implies that, for out-of-flow positioned elements inside a RenderFlowThread,
2027 // we are bailing out before reaching root layer.
2028 if (isPositionedContainer(parentLayer))
2031 if (parentLayer == ancestorLayer) {
2032 foundAncestorFirst = true;
2036 parentLayer = parentLayer->parent();
2039 // We should not reach RenderView layer past the RenderFlowThread layer for any
2040 // children of the RenderFlowThread.
2041 if (renderer.flowThreadContainingBlock() && !layer->isOutOfFlowRenderFlowThread())
2042 ASSERT(parentLayer != renderer.view().layer());
2044 if (foundAncestorFirst) {
2045 // Found ancestorLayer before the abs. positioned container, so compute offset of both relative
2046 // to enclosingPositionedAncestor and subtract.
2047 RenderLayer* positionedAncestor = parentLayer->enclosingPositionedAncestor();
2049 LayoutPoint thisCoords;
2050 layer->convertToLayerCoords(positionedAncestor, thisCoords);
2052 LayoutPoint ancestorCoords;
2053 ancestorLayer->convertToLayerCoords(positionedAncestor, ancestorCoords);
2055 location += (thisCoords - ancestorCoords);
2056 return ancestorLayer;
2059 parentLayer = layer->parent();
2064 location += toLayoutSize(layer->location());
2066 if (adjustForColumns == RenderLayer::AdjustForColumns) {
2067 if (RenderLayer* parentLayer = layer->parent()) {
2068 LayoutSize layerColumnOffset;
2069 parentLayer->renderer().adjustForColumns(layerColumnOffset, location);
2070 location += layerColumnOffset;
2072 if (parentLayer->renderer().isRenderMultiColumnFlowThread()) {
2073 RenderRegion* region = toRenderMultiColumnFlowThread(parentLayer->renderer()).physicalTranslationFromFlowToRegion(location);
2075 location.moveBy(region->topLeftLocation() + -parentLayer->renderBox()->topLeftLocation());
2083 void RenderLayer::convertToLayerCoords(const RenderLayer* ancestorLayer, LayoutPoint& location, ColumnOffsetAdjustment adjustForColumns) const
2085 if (ancestorLayer == this)
2088 const RenderLayer* currLayer = this;
2089 while (currLayer && currLayer != ancestorLayer)
2090 currLayer = accumulateOffsetTowardsAncestor(currLayer, ancestorLayer, location, adjustForColumns);
2093 void RenderLayer::convertToLayerCoords(const RenderLayer* ancestorLayer, LayoutRect& rect, ColumnOffsetAdjustment adjustForColumns) const
2096 convertToLayerCoords(ancestorLayer, delta, adjustForColumns);
2097 rect.move(-delta.x(), -delta.y());
2101 bool RenderLayer::hasAcceleratedTouchScrolling() const
2103 #if ENABLE(ACCELERATED_OVERFLOW_SCROLLING)
2104 if (!scrollsOverflow())
2107 Settings* settings = renderer().document().settings();
2108 // FIXME: settings should not be null at this point. If you find a reliable way to hit this assertion, please file a bug.
2109 // See <rdar://problem/10266101>.
2112 return renderer().style().useTouchOverflowScrolling() || (settings && settings->alwaysUseAcceleratedOverflowScroll());
2118 #if PLATFORM(IOS) && ENABLE(TOUCH_EVENTS)
2119 bool RenderLayer::handleTouchEvent(const PlatformTouchEvent& touchEvent)
2121 // If we have accelerated scrolling, let the scrolling be handled outside of WebKit.
2122 if (hasAcceleratedTouchScrolling())
2125 return ScrollableArea::handleTouchEvent(touchEvent);
2129 void RenderLayer::registerAsTouchEventListenerForScrolling()
2131 if (!renderer().element() || m_registeredAsTouchEventListenerForScrolling)
2134 renderer().document().addTouchEventListener(renderer().element());
2135 m_registeredAsTouchEventListenerForScrolling = true;
2138 void RenderLayer::unregisterAsTouchEventListenerForScrolling()
2140 if (!renderer().element() || !m_registeredAsTouchEventListenerForScrolling)
2143 renderer().document().removeTouchEventListener(renderer().element());
2144 m_registeredAsTouchEventListenerForScrolling = false;
2146 #endif // PLATFORM(IOS)
2148 bool RenderLayer::usesCompositedScrolling() const
2150 return isComposited() && backing()->scrollingLayer();
2153 bool RenderLayer::needsCompositedScrolling() const
2155 return m_needsCompositedScrolling;
2158 void RenderLayer::updateNeedsCompositedScrolling()
2160 bool oldNeedsCompositedScrolling = m_needsCompositedScrolling;
2162 if (!renderer().view().frameView().containsScrollableArea(this))
2163 m_needsCompositedScrolling = false;
2165 bool forceUseCompositedScrolling = acceleratedCompositingForOverflowScrollEnabled()
2166 && canBeStackingContainer()
2167 && !hasOutOfFlowPositionedDescendant();
2169 #if !PLATFORM(IOS) && ENABLE(ACCELERATED_OVERFLOW_SCROLLING)
2170 m_needsCompositedScrolling = forceUseCompositedScrolling || renderer().style().useTouchOverflowScrolling();
2172 // On iOS we don't want to opt into accelerated composited scrolling, which creates scroll bar
2173 // layers in WebCore, because we use UIKit to composite our scroll bars.
2174 m_needsCompositedScrolling = forceUseCompositedScrolling;
2176 // We gather a boolean value for use with Google UMA histograms to
2177 // quantify the actual effects of a set of patches attempting to
2178 // relax composited scrolling requirements, thereby increasing the
2179 // number of composited overflow divs.
2180 if (acceleratedCompositingForOverflowScrollEnabled())
2181 HistogramSupport::histogramEnumeration("Renderer.NeedsCompositedScrolling", m_needsCompositedScrolling, 2);
2184 if (oldNeedsCompositedScrolling != m_needsCompositedScrolling) {
2185 updateSelfPaintingLayer();
2186 if (isStackingContainer())
2191 dirtyStackingContainerZOrderLists();
2193 compositor().setShouldReevaluateCompositingAfterLayout();
2194 compositor().setCompositingLayersNeedRebuild();
2198 static inline int adjustedScrollDelta(int beginningDelta) {
2199 // This implemention matches Firefox's.
2200 // http://mxr.mozilla.org/firefox/source/toolkit/content/widgets/browser.xml#856.
2201 const int speedReducer = 12;
2203 int adjustedDelta = beginningDelta / speedReducer;
2204 if (adjustedDelta > 1)
2205 adjustedDelta = static_cast<int>(adjustedDelta * sqrt(static_cast<double>(adjustedDelta))) - 1;
2206 else if (adjustedDelta < -1)
2207 adjustedDelta = static_cast<int>(adjustedDelta * sqrt(static_cast<double>(-adjustedDelta))) + 1;
2209 return adjustedDelta;
2212 static inline IntSize adjustedScrollDelta(const IntSize& delta)
2214 return IntSize(adjustedScrollDelta(delta.width()), adjustedScrollDelta(delta.height()));
2217 void RenderLayer::panScrollFromPoint(const IntPoint& sourcePoint)
2219 IntPoint lastKnownMousePosition = renderer().frame().eventHandler().lastKnownMousePosition();
2221 // We need to check if the last known mouse position is out of the window. When the mouse is out of the window, the position is incoherent
2222 static IntPoint previousMousePosition;
2223 if (lastKnownMousePosition.x() < 0 || lastKnownMousePosition.y() < 0)
2224 lastKnownMousePosition = previousMousePosition;
2226 previousMousePosition = lastKnownMousePosition;
2228 IntSize delta = lastKnownMousePosition - sourcePoint;
2230 if (abs(delta.width()) <= ScrollView::noPanScrollRadius) // at the center we let the space for the icon
2232 if (abs(delta.height()) <= ScrollView::noPanScrollRadius)
2235 scrollByRecursively(adjustedScrollDelta(delta), ScrollOffsetClamped);
2238 void RenderLayer::scrollByRecursively(const IntSize& delta, ScrollOffsetClamping clamp, ScrollableArea** scrolledArea)
2243 bool restrictedByLineClamp = false;
2244 if (renderer().parent())
2245 restrictedByLineClamp = !renderer().parent()->style().lineClamp().isNone();
2247 if (renderer().hasOverflowClip() && !restrictedByLineClamp) {
2248 IntSize newScrollOffset = scrollOffset() + delta;
2249 scrollToOffset(newScrollOffset, clamp);
2251 *scrolledArea = this;
2253 // If this layer can't do the scroll we ask the next layer up that can scroll to try
2254 IntSize remainingScrollOffset = newScrollOffset - scrollOffset();
2255 if (!remainingScrollOffset.isZero() && renderer().parent()) {
2256 if (RenderLayer* scrollableLayer = enclosingScrollableLayer())
2257 scrollableLayer->scrollByRecursively(remainingScrollOffset, clamp, scrolledArea);
2259 renderer().frame().eventHandler().updateAutoscrollRenderer();
2262 // If we are here, we were called on a renderer that can be programmatically scrolled, but doesn't
2263 // have an overflow clip. Which means that it is a document node that can be scrolled.
2264 renderer().view().frameView().scrollBy(delta);
2266 *scrolledArea = &renderer().view().frameView();
2268 // FIXME: If we didn't scroll the whole way, do we want to try looking at the frames ownerElement?
2269 // https://bugs.webkit.org/show_bug.cgi?id=28237
2273 IntSize RenderLayer::clampScrollOffset(const IntSize& scrollOffset) const
2275 RenderBox* box = renderBox();
2278 int maxX = scrollWidth() - box->pixelSnappedClientWidth();
2279 int maxY = scrollHeight() - box->pixelSnappedClientHeight();
2281 int x = std::max(std::min(scrollOffset.width(), maxX), 0);
2282 int y = std::max(std::min(scrollOffset.height(), maxY), 0);
2283 return IntSize(x, y);
2286 void RenderLayer::scrollToOffset(const IntSize& scrollOffset, ScrollOffsetClamping clamp)
2288 IntSize newScrollOffset = clamp == ScrollOffsetClamped ? clampScrollOffset(scrollOffset) : scrollOffset;
2289 if (newScrollOffset != this->scrollOffset())
2290 scrollToOffsetWithoutAnimation(IntPoint(newScrollOffset));
2293 void RenderLayer::scrollTo(int x, int y)
2295 RenderBox* box = renderBox();
2299 if (box->style().overflowX() != OMARQUEE) {
2300 // Ensure that the dimensions will be computed if they need to be (for overflow:hidden blocks).
2301 if (m_scrollDimensionsDirty)
2302 computeScrollDimensions();
2304 if (adjustForIOSCaretWhenScrolling()) {
2305 int maxX = scrollWidth() - box->clientWidth();
2306 if (x > maxX - caretWidth) {
2308 if (x <= caretWidth)
2310 } else if (x < m_scrollOffset.width() - caretWidth)
2316 // FIXME: Eventually, we will want to perform a blit. For now never
2317 // blit, since the check for blitting is going to be very
2318 // complicated (since it will involve testing whether our layer
2319 // is either occluded by another layer or clipped by an enclosing
2320 // layer or contains fixed backgrounds, etc.).
2321 IntSize newScrollOffset = IntSize(x - scrollOrigin().x(), y - scrollOrigin().y());
2322 if (m_scrollOffset == newScrollOffset) {
2324 if (m_requiresScrollBoundsOriginUpdate)
2325 updateCompositingLayersAfterScroll();
2330 IntPoint oldPosition = IntPoint(m_scrollOffset);
2331 m_scrollOffset = newScrollOffset;
2333 InspectorInstrumentation::willScrollLayer(&renderer().frame());
2335 RenderView& view = renderer().view();
2337 // Update the positions of our child layers (if needed as only fixed layers should be impacted by a scroll).
2338 // We don't update compositing layers, because we need to do a deep update from the compositing ancestor.
2339 bool inLayout = view.frameView().isInLayout();
2341 // If we're in the middle of layout, we'll just update layers once layout has finished.
2342 updateLayerPositionsAfterOverflowScroll();
2343 // Update regions, scrolling may change the clip of a particular region.
2344 #if ENABLE(DASHBOARD_SUPPORT)
2345 view.frameView().updateAnnotatedRegions();
2347 view.frameView().updateWidgetPositions();
2349 if (!m_updatingMarqueePosition) {
2350 // Avoid updating compositing layers if, higher on the stack, we're already updating layer
2351 // positions. Updating layer positions requires a full walk of up-to-date RenderLayers, and
2352 // in this case we're still updating their positions; we'll update compositing layers later
2353 // when that completes.
2354 updateCompositingLayersAfterScroll();
2357 #if PLATFORM(IOS) && ENABLE(TOUCH_EVENTS)
2358 renderer().document().dirtyTouchEventRects();
2362 Frame& frame = renderer().frame();
2363 RenderLayerModelObject* repaintContainer = renderer().containerForRepaint();
2364 // The caret rect needs to be invalidated after scrolling
2365 frame.selection().setCaretRectNeedsUpdate();
2367 FloatQuad quadForFakeMouseMoveEvent = FloatQuad(m_repaintRect);
2368 if (repaintContainer)
2369 quadForFakeMouseMoveEvent = repaintContainer->localToAbsoluteQuad(quadForFakeMouseMoveEvent);
2370 frame.eventHandler().dispatchFakeMouseMoveEventSoonInQuad(quadForFakeMouseMoveEvent);
2372 bool requiresRepaint = true;
2373 if (compositor().inCompositingMode() && usesCompositedScrolling())
2374 requiresRepaint = false;
2376 // Just schedule a full repaint of our object.
2377 if (requiresRepaint)
2378 renderer().repaintUsingContainer(repaintContainer, m_repaintRect);
2380 // Schedule the scroll and scroll-related DOM events.
2381 if (Element* element = renderer().element()) {
2382 element->document().eventQueue().enqueueOrDispatchScrollEvent(*element);
2383 element->document().sendWillRevealEdgeEventsIfNeeded(oldPosition, IntPoint(newScrollOffset), visibleContentRect(), contentsSize(), element);
2386 InspectorInstrumentation::didScrollLayer(&frame);
2387 if (scrollsOverflow())
2388 frame.loader().client().didChangeScrollOffset();
2391 static inline bool frameElementAndViewPermitScroll(HTMLFrameElementBase* frameElementBase, FrameView* frameView)
2393 // If scrollbars aren't explicitly forbidden, permit scrolling.
2394 if (frameElementBase && frameElementBase->scrollingMode() != ScrollbarAlwaysOff)
2397 // If scrollbars are forbidden, user initiated scrolls should obviously be ignored.
2398 if (frameView->wasScrolledByUser())
2401 // Forbid autoscrolls when scrollbars are off, but permits other programmatic scrolls,
2402 // like navigation to an anchor.
2403 return !frameView->frame().eventHandler().autoscrollInProgress();
2406 void RenderLayer::scrollRectToVisible(const LayoutRect& rect, const ScrollAlignment& alignX, const ScrollAlignment& alignY)
2408 RenderLayer* parentLayer = 0;
2409 LayoutRect newRect = rect;
2411 // We may end up propagating a scroll event. It is important that we suspend events until
2412 // the end of the function since they could delete the layer or the layer's renderer().
2413 FrameView& frameView = renderer().view().frameView();
2415 bool restrictedByLineClamp = false;
2416 if (renderer().parent()) {
2417 parentLayer = renderer().parent()->enclosingLayer();
2418 restrictedByLineClamp = !renderer().parent()->style().lineClamp().isNone();
2421 if (renderer().hasOverflowClip() && !restrictedByLineClamp) {
2422 // Don't scroll to reveal an overflow layer that is restricted by the -webkit-line-clamp property.
2423 // This will prevent us from revealing text hidden by the slider in Safari RSS.
2424 RenderBox* box = renderBox();
2426 LayoutRect localExposeRect(box->absoluteToLocalQuad(FloatQuad(FloatRect(rect)), UseTransforms).boundingBox());
2427 LayoutRect layerBounds(0, 0, box->clientWidth(), box->clientHeight());
2428 LayoutRect r = getRectToExpose(layerBounds, layerBounds, localExposeRect, alignX, alignY);
2430 IntSize clampedScrollOffset = clampScrollOffset(scrollOffset() + toIntSize(roundedIntRect(r).location()));
2431 if (clampedScrollOffset != scrollOffset()) {
2432 IntSize oldScrollOffset = scrollOffset();
2433 scrollToOffset(clampedScrollOffset);
2434 IntSize scrollOffsetDifference = scrollOffset() - oldScrollOffset;
2435 localExposeRect.move(-scrollOffsetDifference);
2436 newRect = LayoutRect(box->localToAbsoluteQuad(FloatQuad(FloatRect(localExposeRect)), UseTransforms).boundingBox());
2438 } else if (!parentLayer && renderer().isBox() && renderBox()->canBeProgramaticallyScrolled()) {
2439 Element* ownerElement = renderer().document().ownerElement();
2441 if (ownerElement && ownerElement->renderer()) {
2442 HTMLFrameElementBase* frameElementBase = 0;
2444 if (ownerElement->hasTagName(frameTag) || ownerElement->hasTagName(iframeTag))
2445 frameElementBase = toHTMLFrameElementBase(ownerElement);
2447 if (frameElementAndViewPermitScroll(frameElementBase, &frameView)) {
2448 LayoutRect viewRect = frameView.visibleContentRect(LegacyIOSDocumentVisibleRect);
2449 LayoutRect exposeRect = getRectToExpose(viewRect, viewRect, rect, alignX, alignY);
2451 int xOffset = roundToInt(exposeRect.x());
2452 int yOffset = roundToInt(exposeRect.y());
2453 // Adjust offsets if they're outside of the allowable range.
2454 xOffset = std::max(0, std::min(frameView.contentsWidth(), xOffset));
2455 yOffset = std::max(0, std::min(frameView.contentsHeight(), yOffset));
2457 frameView.setScrollPosition(IntPoint(xOffset, yOffset));
2458 if (frameView.safeToPropagateScrollToParent()) {
2459 parentLayer = ownerElement->renderer()->enclosingLayer();
2460 // FIXME: This doesn't correctly convert the rect to
2461 // absolute coordinates in the parent.
2462 newRect.setX(rect.x() - frameView.scrollX() + frameView.x());
2463 newRect.setY(rect.y() - frameView.scrollY() + frameView.y());
2469 LayoutRect viewRect = frameView.visibleContentRect();
2470 LayoutRect visibleRectRelativeToDocument = viewRect;
2471 IntSize scrollOffsetRelativeToDocument = frameView.scrollOffsetRelativeToDocument();
2472 visibleRectRelativeToDocument.setLocation(IntPoint(scrollOffsetRelativeToDocument.width(), scrollOffsetRelativeToDocument.height()));
2474 LayoutRect viewRect = frameView.unobscuredContentRect();
2475 LayoutRect visibleRectRelativeToDocument = viewRect;
2478 LayoutRect r = getRectToExpose(viewRect, visibleRectRelativeToDocument, rect, alignX, alignY);
2480 frameView.setScrollPosition(roundedIntPoint(r.location()));
2482 // This is the outermost view of a web page, so after scrolling this view we
2483 // scroll its container by calling Page::scrollRectIntoView.
2484 // This only has an effect on the Mac platform in applications
2485 // that put web views into scrolling containers, such as Mac OS X Mail.
2486 // The canAutoscroll function in EventHandler also knows about this.
2487 if (Page* page = frameView.frame().page())
2488 page->chrome().scrollRectIntoView(pixelSnappedIntRect(rect));
2492 if (renderer().frame().eventHandler().autoscrollInProgress())
2493 parentLayer = enclosingScrollableLayer();
2496 parentLayer->scrollRectToVisible(newRect, alignX, alignY);
2499 void RenderLayer::updateCompositingLayersAfterScroll()
2501 if (compositor().inCompositingMode()) {
2502 // Our stacking container is guaranteed to contain all of our descendants that may need
2503 // repositioning, so update compositing layers from there.
2504 if (RenderLayer* compositingAncestor = stackingContainer()->enclosingCompositingLayer()) {
2505 if (usesCompositedScrolling() && !hasOutOfFlowPositionedDescendant())
2506 compositor().updateCompositingLayers(CompositingUpdateOnCompositedScroll, compositingAncestor);
2508 compositor().updateCompositingLayers(CompositingUpdateOnScroll, compositingAncestor);
2513 LayoutRect RenderLayer::getRectToExpose(const LayoutRect &visibleRect, const LayoutRect &visibleRectRelativeToDocument, const LayoutRect &exposeRect, const ScrollAlignment& alignX, const ScrollAlignment& alignY)
2515 // Determine the appropriate X behavior.
2516 ScrollBehavior scrollX;
2517 LayoutRect exposeRectX(exposeRect.x(), visibleRect.y(), exposeRect.width(), visibleRect.height());
2518 LayoutUnit intersectWidth = intersection(visibleRect, exposeRectX).width();
2519 if (intersectWidth == exposeRect.width() || intersectWidth >= MIN_INTERSECT_FOR_REVEAL)
2520 // If the rectangle is fully visible, use the specified visible behavior.
2521 // If the rectangle is partially visible, but over a certain threshold,
2522 // then treat it as fully visible to avoid unnecessary horizontal scrolling
2523 scrollX = ScrollAlignment::getVisibleBehavior(alignX);
2524 else if (intersectWidth == visibleRect.width()) {
2525 // If the rect is bigger than the visible area, don't bother trying to center. Other alignments will work.
2526 scrollX = ScrollAlignment::getVisibleBehavior(alignX);
2527 if (scrollX == alignCenter)
2529 } else if (intersectWidth > 0)
2530 // If the rectangle is partially visible, but not above the minimum threshold, use the specified partial behavior
2531 scrollX = ScrollAlignment::getPartialBehavior(alignX);
2533 scrollX = ScrollAlignment::getHiddenBehavior(alignX);
2534 // If we're trying to align to the closest edge, and the exposeRect is further right
2535 // than the visibleRect, and not bigger than the visible area, then align with the right.
2536 if (scrollX == alignToClosestEdge && exposeRect.maxX() > visibleRect.maxX() && exposeRect.width() < visibleRect.width())
2537 scrollX = alignRight;
2539 // Given the X behavior, compute the X coordinate.
2541 if (scrollX == noScroll)
2542 x = visibleRect.x();
2543 else if (scrollX == alignRight)
2544 x = exposeRect.maxX() - visibleRect.width();
2545 else if (scrollX == alignCenter)
2546 x = exposeRect.x() + (exposeRect.width() - visibleRect.width()) / 2;
2550 // Determine the appropriate Y behavior.
2551 ScrollBehavior scrollY;
2552 LayoutRect exposeRectY(visibleRect.x(), exposeRect.y(), visibleRect.width(), exposeRect.height());
2553 LayoutUnit intersectHeight = intersection(visibleRectRelativeToDocument, exposeRectY).height();
2554 if (intersectHeight == exposeRect.height())
2555 // If the rectangle is fully visible, use the specified visible behavior.
2556 scrollY = ScrollAlignment::getVisibleBehavior(alignY);
2557 else if (intersectHeight == visibleRect.height()) {
2558 // If the rect is bigger than the visible area, don't bother trying to center. Other alignments will work.
2559 scrollY = ScrollAlignment::getVisibleBehavior(alignY);
2560 if (scrollY == alignCenter)
2562 } else if (intersectHeight > 0)
2563 // If the rectangle is partially visible, use the specified partial behavior
2564 scrollY = ScrollAlignment::getPartialBehavior(alignY);
2566 scrollY = ScrollAlignment::getHiddenBehavior(alignY);
2567 // If we're trying to align to the closest edge, and the exposeRect is further down
2568 // than the visibleRect, and not bigger than the visible area, then align with the bottom.
2569 if (scrollY == alignToClosestEdge && exposeRect.maxY() > visibleRect.maxY() && exposeRect.height() < visibleRect.height())
2570 scrollY = alignBottom;
2572 // Given the Y behavior, compute the Y coordinate.
2574 if (scrollY == noScroll)
2575 y = visibleRect.y();
2576 else if (scrollY == alignBottom)
2577 y = exposeRect.maxY() - visibleRect.height();
2578 else if (scrollY == alignCenter)
2579 y = exposeRect.y() + (exposeRect.height() - visibleRect.height()) / 2;
2583 return LayoutRect(LayoutPoint(x, y), visibleRect.size());
2586 void RenderLayer::autoscroll(const IntPoint& position)
2588 IntPoint currentDocumentPosition = renderer().view().frameView().windowToContents(position);
2589 scrollRectToVisible(LayoutRect(currentDocumentPosition, LayoutSize(1, 1)), ScrollAlignment::alignToEdgeIfNeeded, ScrollAlignment::alignToEdgeIfNeeded);
2592 bool RenderLayer::canResize() const
2594 // We need a special case for <iframe> because they never have
2595 // hasOverflowClip(). However, they do "implicitly" clip their contents, so
2596 // we want to allow resizing them also.
2597 return (renderer().hasOverflowClip() || renderer().isRenderIFrame()) && renderer().style().resize() != RESIZE_NONE;
2600 void RenderLayer::resize(const PlatformMouseEvent& evt, const LayoutSize& oldOffset)
2602 // FIXME: This should be possible on generated content but is not right now.
2603 if (!inResizeMode() || !canResize() || !renderer().element())
2606 // FIXME: The only case where renderer->element()->renderer() != renderer is with continuations. Do they matter here?
2607 // If they do it would still be better to deal with them explicitly.
2608 Element* element = renderer().element();
2609 RenderBox* renderer = toRenderBox(element->renderer());
2611 Document& document = element->document();
2612 if (!document.frame()->eventHandler().mousePressed())
2615 float zoomFactor = renderer->style().effectiveZoom();
2617 LayoutSize newOffset = offsetFromResizeCorner(document.view()->windowToContents(evt.position()));
2618 newOffset.setWidth(newOffset.width() / zoomFactor);
2619 newOffset.setHeight(newOffset.height() / zoomFactor);
2621 LayoutSize currentSize = LayoutSize(renderer->width() / zoomFactor, renderer->height() / zoomFactor);
2622 LayoutSize minimumSize = element->minimumSizeForResizing().shrunkTo(currentSize);
2623 element->setMinimumSizeForResizing(minimumSize);
2625 LayoutSize adjustedOldOffset = LayoutSize(oldOffset.width() / zoomFactor, oldOffset.height() / zoomFactor);
2626 if (renderer->style().shouldPlaceBlockDirectionScrollbarOnLogicalLeft()) {
2627 newOffset.setWidth(-newOffset.width());
2628 adjustedOldOffset.setWidth(-adjustedOldOffset.width());
2631 LayoutSize difference = (currentSize + newOffset - adjustedOldOffset).expandedTo(minimumSize) - currentSize;
2633 StyledElement* styledElement = toStyledElement(element);
2634 bool isBoxSizingBorder = renderer->style().boxSizing() == BORDER_BOX;
2636 EResize resize = renderer->style().resize();
2637 if (resize != RESIZE_VERTICAL && difference.width()) {
2638 if (element->isFormControlElement()) {
2639 // Make implicit margins from the theme explicit (see <http://bugs.webkit.org/show_bug.cgi?id=9547>).
2640 styledElement->setInlineStyleProperty(CSSPropertyMarginLeft, renderer->marginLeft() / zoomFactor, CSSPrimitiveValue::CSS_PX);
2641 styledElement->setInlineStyleProperty(CSSPropertyMarginRight, renderer->marginRight() / zoomFactor, CSSPrimitiveValue::CSS_PX);
2643 LayoutUnit baseWidth = renderer->width() - (isBoxSizingBorder ? LayoutUnit() : renderer->horizontalBorderAndPaddingExtent());
2644 baseWidth = baseWidth / zoomFactor;
2645 styledElement->setInlineStyleProperty(CSSPropertyWidth, roundToInt(baseWidth + difference.width()), CSSPrimitiveValue::CSS_PX);
2648 if (resize != RESIZE_HORIZONTAL && difference.height()) {
2649 if (element->isFormControlElement()) {
2650 // Make implicit margins from the theme explicit (see <http://bugs.webkit.org/show_bug.cgi?id=9547>).
2651 styledElement->setInlineStyleProperty(CSSPropertyMarginTop, renderer->marginTop() / zoomFactor, CSSPrimitiveValue::CSS_PX);
2652 styledElement->setInlineStyleProperty(CSSPropertyMarginBottom, renderer->marginBottom() / zoomFactor, CSSPrimitiveValue::CSS_PX);
2654 LayoutUnit baseHeight = renderer->height() - (isBoxSizingBorder ? LayoutUnit() : renderer->verticalBorderAndPaddingExtent());
2655 baseHeight = baseHeight / zoomFactor;
2656 styledElement->setInlineStyleProperty(CSSPropertyHeight, roundToInt(baseHeight + difference.height()), CSSPrimitiveValue::CSS_PX);
2659 document.updateLayout();
2661 // FIXME (Radar 4118564): We should also autoscroll the window as necessary to keep the point under the cursor in view.
2664 int RenderLayer::scrollSize(ScrollbarOrientation orientation) const
2666 Scrollbar* scrollbar = ((orientation == HorizontalScrollbar) ? m_hBar : m_vBar).get();
2667 return scrollbar ? (scrollbar->totalSize() - scrollbar->visibleSize()) : 0;
2670 void RenderLayer::setScrollOffset(const IntPoint& offset)
2672 scrollTo(offset.x(), offset.y());
2675 int RenderLayer::scrollPosition(Scrollbar* scrollbar) const
2677 if (scrollbar->orientation() == HorizontalScrollbar)
2678 return scrollXOffset();
2679 if (scrollbar->orientation() == VerticalScrollbar)
2680 return scrollYOffset();
2684 IntPoint RenderLayer::scrollPosition() const
2686 return IntPoint(m_scrollOffset);
2689 IntPoint RenderLayer::minimumScrollPosition() const
2691 return -scrollOrigin();
2694 IntPoint RenderLayer::maximumScrollPosition() const
2696 // FIXME: m_scrollSize may not be up-to-date if m_scrollDimensionsDirty is true.
2697 return -scrollOrigin() + roundedIntSize(m_scrollSize) - visibleContentRectIncludingScrollbars(ContentsVisibleRect).size();
2700 IntRect RenderLayer::visibleContentRectInternal(VisibleContentRectIncludesScrollbars scrollbarInclusion, VisibleContentRectBehavior) const
2702 int verticalScrollbarWidth = 0;
2703 int horizontalScrollbarHeight = 0;
2704 if (showsOverflowControls() && scrollbarInclusion == IncludeScrollbars) {
2705 verticalScrollbarWidth = (verticalScrollbar() && !verticalScrollbar()->isOverlayScrollbar()) ? verticalScrollbar()->width() : 0;
2706 horizontalScrollbarHeight = (horizontalScrollbar() && !horizontalScrollbar()->isOverlayScrollbar()) ? horizontalScrollbar()->height() : 0;
2709 return IntRect(IntPoint(scrollXOffset(), scrollYOffset()),
2710 IntSize(std::max(0, m_layerSize.width() - verticalScrollbarWidth),
2711 std::max(0, m_layerSize.height() - horizontalScrollbarHeight)));
2714 IntSize RenderLayer::overhangAmount() const
2719 bool RenderLayer::isActive() const
2721 Page* page = renderer().frame().page();
2722 return page && page->focusController().isActive();
2725 static int cornerStart(const RenderLayer* layer, int minX, int maxX, int thickness)
2727 if (layer->renderer().style().shouldPlaceBlockDirectionScrollbarOnLogicalLeft())
2728 return minX + layer->renderer().style().borderLeftWidth();
2729 return maxX - thickness - layer->renderer().style().borderRightWidth();
2732 static LayoutRect cornerRect(const RenderLayer* layer, const LayoutRect& bounds)
2734 int horizontalThickness;
2735 int verticalThickness;
2736 if (!layer->verticalScrollbar() && !layer->horizontalScrollbar()) {
2737 // FIXME: This isn't right. We need to know the thickness of custom scrollbars
2738 // even when they don't exist in order to set the resizer square size properly.
2739 horizontalThickness = ScrollbarTheme::theme()->scrollbarThickness();
2740 verticalThickness = horizontalThickness;
2741 } else if (layer->verticalScrollbar() && !layer->horizontalScrollbar()) {
2742 horizontalThickness = layer->verticalScrollbar()->width();
2743 verticalThickness = horizontalThickness;
2744 } else if (layer->horizontalScrollbar() && !layer->verticalScrollbar()) {
2745 verticalThickness = layer->horizontalScrollbar()->height();
2746 horizontalThickness = verticalThickness;
2748 horizontalThickness = layer->verticalScrollbar()->width();
2749 verticalThickness = layer->horizontalScrollbar()->height();
2751 return LayoutRect(cornerStart(layer, bounds.x(), bounds.maxX(), horizontalThickness),
2752 bounds.maxY() - verticalThickness - layer->renderer().style().borderBottomWidth(),
2753 horizontalThickness, verticalThickness);
2756 IntRect RenderLayer::scrollCornerRect() const
2758 // We have a scrollbar corner when a scrollbar is visible and not filling the entire length of the box.
2759 // This happens when:
2760 // (a) A resizer is present and at least one scrollbar is present
2761 // (b) Both scrollbars are present.
2762 bool hasHorizontalBar = horizontalScrollbar();
2763 bool hasVerticalBar = verticalScrollbar();
2764 bool hasResizer = renderer().style().resize() != RESIZE_NONE;
2765 if ((hasHorizontalBar && hasVerticalBar) || (hasResizer && (hasHorizontalBar || hasVerticalBar)))
2766 return pixelSnappedIntRect(cornerRect(this, renderBox()->borderBoxRect()));
2770 static LayoutRect resizerCornerRect(const RenderLayer* layer, const LayoutRect& bounds)
2772 ASSERT(layer->renderer().isBox());
2773 if (layer->renderer().style().resize() == RESIZE_NONE)
2774 return LayoutRect();
2775 return cornerRect(layer, bounds);
2778 LayoutRect RenderLayer::scrollCornerAndResizerRect() const
2780 RenderBox* box = renderBox();
2782 return LayoutRect();
2783 LayoutRect scrollCornerAndResizer = scrollCornerRect();
2784 if (scrollCornerAndResizer.isEmpty())
2785 scrollCornerAndResizer = resizerCornerRect(this, box->borderBoxRect());
2786 return scrollCornerAndResizer;
2789 bool RenderLayer::isScrollCornerVisible() const
2791 ASSERT(renderer().isBox());
2792 return !scrollCornerRect().isEmpty();
2795 IntRect RenderLayer::convertFromScrollbarToContainingView(const Scrollbar* scrollbar, const IntRect& scrollbarRect) const
2797 IntRect rect = scrollbarRect;
2798 rect.move(scrollbarOffset(scrollbar));
2800 return renderer().view().frameView().convertFromRenderer(&renderer(), rect);
2803 IntRect RenderLayer::convertFromContainingViewToScrollbar(const Scrollbar* scrollbar, const IntRect& parentRect) const
2805 IntRect rect = renderer().view().frameView().convertToRenderer(&renderer(), parentRect);
2806 rect.move(-scrollbarOffset(scrollbar));
2810 IntPoint RenderLayer::convertFromScrollbarToContainingView(const Scrollbar* scrollbar, const IntPoint& scrollbarPoint) const
2812 IntPoint point = scrollbarPoint;
2813 point.move(scrollbarOffset(scrollbar));
2814 return renderer().view().frameView().convertFromRenderer(&renderer(), point);
2817 IntPoint RenderLayer::convertFromContainingViewToScrollbar(const Scrollbar* scrollbar, const IntPoint& parentPoint) const
2819 IntPoint point = renderer().view().frameView().convertToRenderer(&renderer(), parentPoint);
2820 point.move(-scrollbarOffset(scrollbar));
2824 IntSize RenderLayer::contentsSize() const
2826 return IntSize(scrollWidth(), scrollHeight());
2829 bool RenderLayer::shouldSuspendScrollAnimations() const
2831 return renderer().view().frameView().shouldSuspendScrollAnimations();
2835 void RenderLayer::didStartScroll()
2837 if (Page* page = renderer().frame().page())
2838 page->chrome().client().didStartOverflowScroll();
2841 void RenderLayer::didEndScroll()
2843 if (Page* page = renderer().frame().page())
2844 page->chrome().client().didEndOverflowScroll();
2847 void RenderLayer::didUpdateScroll()
2849 // Send this notification when we scroll, since this is how we keep selection updated.
2850 if (Page* page = renderer().frame().page())
2851 page->chrome().client().didLayout(ChromeClient::Scroll);
2855 IntPoint RenderLayer::lastKnownMousePosition() const
2857 return renderer().frame().eventHandler().lastKnownMousePosition();
2860 bool RenderLayer::isHandlingWheelEvent() const
2862 return renderer().frame().eventHandler().isHandlingWheelEvent();
2865 IntRect RenderLayer::rectForHorizontalScrollbar(const IntRect& borderBoxRect) const
2870 const RenderBox* box = renderBox();
2871 const IntRect& scrollCorner = scrollCornerRect();
2873 return IntRect(horizontalScrollbarStart(borderBoxRect.x()),
2874 borderBoxRect.maxY() - box->borderBottom() - m_hBar->height(),
2875 borderBoxRect.width() - (box->borderLeft() + box->borderRight()) - scrollCorner.width(),
2879 IntRect RenderLayer::rectForVerticalScrollbar(const IntRect& borderBoxRect) const
2884 const RenderBox* box = renderBox();
2885 const IntRect& scrollCorner = scrollCornerRect();
2887 return IntRect(verticalScrollbarStart(borderBoxRect.x(), borderBoxRect.maxX()),
2888 borderBoxRect.y() + box->borderTop(),
2890 borderBoxRect.height() - (box->borderTop() + box->borderBottom()) - scrollCorner.height());
2893 LayoutUnit RenderLayer::verticalScrollbarStart(int minX, int maxX) const
2895 const RenderBox* box = renderBox();
2896 if (renderer().style().shouldPlaceBlockDirectionScrollbarOnLogicalLeft())
2897 return minX + box->borderLeft();
2898 return maxX - box->borderRight() - m_vBar->width();
2901 LayoutUnit RenderLayer::horizontalScrollbarStart(int minX) const
2903 const RenderBox* box = renderBox();
2904 int x = minX + box->borderLeft();
2905 if (renderer().style().shouldPlaceBlockDirectionScrollbarOnLogicalLeft())
2906 x += m_vBar ? m_vBar->width() : roundToInt(resizerCornerRect(this, box->borderBoxRect()).width());
2910 IntSize RenderLayer::scrollbarOffset(const Scrollbar* scrollbar) const
2912 RenderBox* box = renderBox();
2914 if (scrollbar == m_vBar.get())
2915 return IntSize(verticalScrollbarStart(0, box->width()), box->borderTop());
2917 if (scrollbar == m_hBar.get())
2918 return IntSize(horizontalScrollbarStart(0), box->height() - box->borderBottom() - scrollbar->height());
2920 ASSERT_NOT_REACHED();
2924 void RenderLayer::invalidateScrollbarRect(Scrollbar* scrollbar, const IntRect& rect)
2926 if (!showsOverflowControls())
2929 if (scrollbar == m_vBar.get()) {
2930 if (GraphicsLayer* layer = layerForVerticalScrollbar()) {
2931 layer->setNeedsDisplayInRect(rect);
2935 if (GraphicsLayer* layer = layerForHorizontalScrollbar()) {
2936 layer->setNeedsDisplayInRect(rect);
2941 IntRect scrollRect = rect;
2942 RenderBox* box = renderBox();
2944 // If we are not yet inserted into the tree, there is no need to repaint.
2948 if (scrollbar == m_vBar.get())
2949 scrollRect.move(verticalScrollbarStart(0, box->width()), box->borderTop());
2951 scrollRect.move(horizontalScrollbarStart(0), box->height() - box->borderBottom() - scrollbar->height());
2952 LayoutRect repaintRect = scrollRect;
2953 renderBox()->flipForWritingMode(repaintRect);
2954 renderer().repaintRectangle(repaintRect);
2957 void RenderLayer::invalidateScrollCornerRect(const IntRect& rect)
2959 if (!showsOverflowControls())
2962 if (GraphicsLayer* layer = layerForScrollCorner()) {
2963 layer->setNeedsDisplayInRect(rect);
2968 m_scrollCorner->repaintRectangle(rect);
2970 m_resizer->repaintRectangle(rect);
2973 static inline RenderElement* rendererForScrollbar(RenderLayerModelObject& renderer)
2975 if (Element* element = renderer.element()) {
2976 if (ShadowRoot* shadowRoot = element->containingShadowRoot()) {
2977 if (shadowRoot->type() == ShadowRoot::UserAgentShadowRoot)
2978 return shadowRoot->hostElement()->renderer();
2985 PassRefPtr<Scrollbar> RenderLayer::createScrollbar(ScrollbarOrientation orientation)
2987 RefPtr<Scrollbar> widget;
2988 RenderElement* actualRenderer = rendererForScrollbar(renderer());
2989 bool hasCustomScrollbarStyle = actualRenderer->isBox() && actualRenderer->style().hasPseudoStyle(SCROLLBAR);
2990 if (hasCustomScrollbarStyle)
2991 widget = RenderScrollbar::createCustomScrollbar(this, orientation, actualRenderer->element());
2993 widget = Scrollbar::createNativeScrollbar(this, orientation, RegularScrollbar);
2994 didAddScrollbar(widget.get(), orientation);
2996 renderer().view().frameView().addChild(widget.get());
2997 return widget.release();
3000 void RenderLayer::destroyScrollbar(ScrollbarOrientation orientation)
3002 RefPtr<Scrollbar>& scrollbar = orientation == HorizontalScrollbar ? m_hBar : m_vBar;
3006 if (!scrollbar->isCustomScrollbar())
3007 willRemoveScrollbar(scrollbar.get(), orientation);
3009 scrollbar->removeFromParent();
3010 scrollbar->disconnectFromScrollableArea();
3014 bool RenderLayer::scrollsOverflow() const
3016 if (!renderer().isBox())
3019 return toRenderBox(renderer()).scrollsOverflow();
3022 void RenderLayer::setHasHorizontalScrollbar(bool hasScrollbar)
3024 if (hasScrollbar == hasHorizontalScrollbar())
3028 m_hBar = createScrollbar(HorizontalScrollbar);
3030 destroyScrollbar(HorizontalScrollbar);
3032 // Destroying or creating one bar can cause our scrollbar corner to come and go. We need to update the opposite scrollbar's style.
3034 m_hBar->styleChanged();
3036 m_vBar->styleChanged();
3038 // Force an update since we know the scrollbars have changed things.
3039 #if ENABLE(DASHBOARD_SUPPORT)
3040 if (renderer().document().hasAnnotatedRegions())
3041 renderer().document().setAnnotatedRegionsDirty(true);
3045 void RenderLayer::setHasVerticalScrollbar(bool hasScrollbar)
3047 if (hasScrollbar == hasVerticalScrollbar())
3051 m_vBar = createScrollbar(VerticalScrollbar);
3053 destroyScrollbar(VerticalScrollbar);
3055 // Destroying or creating one bar can cause our scrollbar corner to come and go. We need to update the opposite scrollbar's style.
3057 m_hBar->styleChanged();
3059 m_vBar->styleChanged();
3061 // Force an update since we know the scrollbars have changed things.
3062 #if ENABLE(DASHBOARD_SUPPORT)
3063 if (renderer().document().hasAnnotatedRegions())
3064 renderer().document().setAnnotatedRegionsDirty(true);
3068 ScrollableArea* RenderLayer::enclosingScrollableArea() const
3070 if (RenderLayer* scrollableLayer = enclosingScrollableLayer())
3071 return scrollableLayer;
3073 // FIXME: We should return the frame view here (or possibly an ancestor frame view,
3074 // if the frame view isn't scrollable.
3078 int RenderLayer::verticalScrollbarWidth(OverlayScrollbarSizeRelevancy relevancy) const
3081 || !showsOverflowControls()
3082 || (m_vBar->isOverlayScrollbar() && (relevancy == IgnoreOverlayScrollbarSize || !m_vBar->shouldParticipateInHitTesting())))
3085 return m_vBar->width();
3088 int RenderLayer::horizontalScrollbarHeight(OverlayScrollbarSizeRelevancy relevancy) const
3091 || !showsOverflowControls()
3092 || (m_hBar->isOverlayScrollbar() && (relevancy == IgnoreOverlayScrollbarSize || !m_hBar->shouldParticipateInHitTesting())))
3095 return m_hBar->height();
3098 IntSize RenderLayer::offsetFromResizeCorner(const IntPoint& absolutePoint) const
3100 // Currently the resize corner is either the bottom right corner or the bottom left corner.
3101 // FIXME: This assumes the location is 0, 0. Is this guaranteed to always be the case?
3102 IntSize elementSize = size();
3103 if (renderer().style().shouldPlaceBlockDirectionScrollbarOnLogicalLeft())
3104 elementSize.setWidth(0);
3105 IntPoint resizerPoint = IntPoint(elementSize);
3106 IntPoint localPoint = roundedIntPoint(absoluteToContents(absolutePoint));
3107 return localPoint - resizerPoint;
3110 bool RenderLayer::hasOverflowControls() const
3112 return m_hBar || m_vBar || m_scrollCorner || renderer().style().resize() != RESIZE_NONE;
3115 void RenderLayer::positionOverflowControls(const IntSize& offsetFromRoot)
3117 if (!m_hBar && !m_vBar && !canResize())
3120 RenderBox* box = renderBox();
3124 const IntRect borderBox = box->pixelSnappedBorderBoxRect();
3125 const IntRect& scrollCorner = scrollCornerRect();
3126 IntRect absBounds(borderBox.location() + offsetFromRoot, borderBox.size());
3128 IntRect vBarRect = rectForVerticalScrollbar(borderBox);
3129 vBarRect.move(offsetFromRoot);
3130 m_vBar->setFrameRect(vBarRect);
3134 IntRect hBarRect = rectForHorizontalScrollbar(borderBox);
3135 hBarRect.move(offsetFromRoot);
3136 m_hBar->setFrameRect(hBarRect);
3140 m_scrollCorner->setFrameRect(scrollCorner);
3142 m_resizer->setFrameRect(resizerCornerRect(this, borderBox));
3145 backing()->positionOverflowControlsLayers();
3148 int RenderLayer::scrollWidth() const
3150 ASSERT(renderBox());
3151 if (m_scrollDimensionsDirty)
3152 const_cast<RenderLayer*>(this)->computeScrollDimensions();
3153 return snapSizeToPixel(m_scrollSize.width(), renderBox()->clientLeft() + renderBox()->x());
3156 int RenderLayer::scrollHeight() const
3158 ASSERT(renderBox());
3159 if (m_scrollDimensionsDirty)
3160 const_cast<RenderLayer*>(this)->computeScrollDimensions();
3161 return snapSizeToPixel(m_scrollSize.height(), renderBox()->clientTop() + renderBox()->y());
3164 LayoutUnit RenderLayer::overflowTop() const
3166 RenderBox* box = renderBox();
3167 LayoutRect overflowRect(box->layoutOverflowRect());
3168 box->flipForWritingMode(overflowRect);
3169 return overflowRect.y();
3172 LayoutUnit RenderLayer::overflowBottom() const
3174 RenderBox* box = renderBox();
3175 LayoutRect overflowRect(box->layoutOverflowRect());
3176 box->flipForWritingMode(overflowRect);
3177 return overflowRect.maxY();
3180 LayoutUnit RenderLayer::overflowLeft() const
3182 RenderBox* box = renderBox();
3183 LayoutRect overflowRect(box->layoutOverflowRect());
3184 box->flipForWritingMode(overflowRect);
3185 return overflowRect.x();
3188 LayoutUnit RenderLayer::overflowRight() const
3190 RenderBox* box = renderBox();
3191 LayoutRect overflowRect(box->layoutOverflowRect());
3192 box->flipForWritingMode(overflowRect);
3193 return overflowRect.maxX();
3196 void RenderLayer::computeScrollDimensions()
3198 RenderBox* box = renderBox();
3201 m_scrollDimensionsDirty = false;
3203 m_scrollSize.setWidth(overflowRight() - overflowLeft());
3204 m_scrollSize.setHeight(overflowBottom() - overflowTop());
3206 int scrollableLeftOverflow = overflowLeft() - box->borderLeft();
3207 int scrollableTopOverflow = overflowTop() - box->borderTop();
3208 setScrollOrigin(IntPoint(-scrollableLeftOverflow, -scrollableTopOverflow));
3211 bool RenderLayer::hasScrollableHorizontalOverflow() const
3213 return hasHorizontalOverflow() && renderBox()->scrollsOverflowX();
3216 bool RenderLayer::hasScrollableVerticalOverflow() const
3218 return hasVerticalOverflow() && renderBox()->scrollsOverflowY();
3221 bool RenderLayer::hasHorizontalOverflow() const
3223 ASSERT(!m_scrollDimensionsDirty);
3225 return scrollWidth() > renderBox()->pixelSnappedClientWidth();
3228 bool RenderLayer::hasVerticalOverflow() const
3230 ASSERT(!m_scrollDimensionsDirty);
3232 return scrollHeight() > renderBox()->pixelSnappedClientHeight();
3235 void RenderLayer::updateScrollbarsAfterLayout()
3237 RenderBox* box = renderBox();
3240 // List box parts handle the scrollbars by themselves so we have nothing to do.
3241 if (box->style().appearance() == ListboxPart)
3244 bool hasHorizontalOverflow = this->hasHorizontalOverflow();
3245 bool hasVerticalOverflow = this->hasVerticalOverflow();
3247 // overflow:scroll should just enable/disable.
3248 if (renderer().style().overflowX() == OSCROLL)
3249 m_hBar->setEnabled(hasHorizontalOverflow);
3250 if (renderer().style().overflowY() == OSCROLL)
3251 m_vBar->setEnabled(hasVerticalOverflow);
3253 // overflow:auto may need to lay out again if scrollbars got added/removed.
3254 bool autoHorizontalScrollBarChanged = box->hasAutoHorizontalScrollbar() && (hasHorizontalScrollbar() != hasHorizontalOverflow);
3255 bool autoVerticalScrollBarChanged = box->hasAutoVerticalScrollbar() && (hasVerticalScrollbar() != hasVerticalOverflow);
3257 if (autoHorizontalScrollBarChanged || autoVerticalScrollBarChanged) {
3258 if (box->hasAutoHorizontalScrollbar())
3259 setHasHorizontalScrollbar(hasHorizontalOverflow);
3260 if (box->hasAutoVerticalScrollbar())
3261 setHasVerticalScrollbar(hasVerticalOverflow);
3263 updateSelfPaintingLayer();
3265 // Force an update since we know the scrollbars have changed things.
3266 #if ENABLE(DASHBOARD_SUPPORT)
3267 if (renderer().document().hasAnnotatedRegions())
3268 renderer().document().setAnnotatedRegionsDirty(true);
3271 renderer().repaint();
3273 if (renderer().style().overflowX() == OAUTO || renderer().style().overflowY() == OAUTO) {
3274 if (!m_inOverflowRelayout) {
3275 // Our proprietary overflow: overlay value doesn't trigger a layout.
3276 m_inOverflowRelayout = true;
3277 renderer().setNeedsLayout(MarkOnlyThis);
3278 if (renderer().isRenderBlock()) {
3279 RenderBlock& block = toRenderBlock(renderer());
3280 block.scrollbarsChanged(autoHorizontalScrollBarChanged, autoVerticalScrollBarChanged);
3281 block.layoutBlock(true);
3283 renderer().layout();
3284 m_inOverflowRelayout = false;
3289 // Set up the range (and page step/line step).
3291 int clientWidth = box->pixelSnappedClientWidth();
3292 int pageStep = std::max(std::max<int>(clientWidth * Scrollbar::minFractionToStepWhenPaging(), clientWidth - Scrollbar::maxOverlapBetweenPages()), 1);
3293 m_hBar->setSteps(Scrollbar::pixelsPerLineStep(), pageStep);
3294 m_hBar->setProportion(clientWidth, m_scrollSize.width());
3297 int clientHeight = box->pixelSnappedClientHeight();
3298 int pageStep = std::max(std::max<int>(clientHeight * Scrollbar::minFractionToStepWhenPaging(), clientHeight - Scrollbar::maxOverlapBetweenPages()), 1);
3299 m_vBar->setSteps(Scrollbar::pixelsPerLineStep(), pageStep);
3300 m_vBar->setProportion(clientHeight, m_scrollSize.height());
3303 updateScrollableAreaSet(hasScrollableHorizontalOverflow() || hasScrollableVerticalOverflow());
3306 void RenderLayer::updateScrollInfoAfterLayout()
3308 RenderBox* box = renderBox();
3312 m_scrollDimensionsDirty = true;
3313 IntSize originalScrollOffset = scrollOffset();
3315 computeScrollDimensions();
3317 if (box->style().overflowX() != OMARQUEE) {
3318 // Layout may cause us to be at an invalid scroll position. In this case we need
3319 // to pull our scroll offsets back to the max (or push them up to the min).
3320 IntSize clampedScrollOffset = clampScrollOffset(scrollOffset());
3322 // FIXME: This looks wrong. The caret adjust mode should only be enabled on editing related entry points.
3323 // This code was added to fix an issue where the text insertion point would always be drawn on the right edge
3324 // of a text field whose content overflowed its bounds. See <rdar://problem/15579797> for more details.
3325 setAdjustForIOSCaretWhenScrolling(true);
3327 if (clampedScrollOffset != scrollOffset())
3328 scrollToOffset(clampedScrollOffset);
3331 setAdjustForIOSCaretWhenScrolling(false);
3335 updateScrollbarsAfterLayout();
3337 if (originalScrollOffset != scrollOffset())
3338 scrollToOffsetWithoutAnimation(IntPoint(scrollOffset()));
3340 // Composited scrolling may need to be enabled or disabled if the amount of overflow changed.
3341 if (compositor().updateLayerCompositingState(*this))
3342 compositor().setCompositingLayersNeedRebuild();
3345 bool RenderLayer::overflowControlsIntersectRect(const IntRect& localRect) const
3347 const IntRect borderBox = renderBox()->pixelSnappedBorderBoxRect();
3349 if (rectForHorizontalScrollbar(borderBox).intersects(localRect))
3352 if (rectForVerticalScrollbar(borderBox).intersects(localRect))
3355 if (scrollCornerRect().intersects(localRect))
3358 if (resizerCornerRect(this, borderBox).intersects(localRect))
3364 bool RenderLayer::showsOverflowControls() const
3367 // Don't render (custom) scrollbars if we have accelerated scrolling.
3368 if (hasAcceleratedTouchScrolling())
3375 void RenderLayer::paintOverflowControls(GraphicsContext* context, const IntPoint& paintOffset, const IntRect& damageRect, bool paintingOverlayControls)
3377 // Don't do anything if we have no overflow.
3378 if (!renderer().hasOverflowClip())
3381 if (!showsOverflowControls())
3384 // Overlay scrollbars paint in a second pass through the layer tree so that they will paint
3385 // on top of everything else. If this is the normal painting pass, paintingOverlayControls
3386 // will be false, and we should just tell the root layer that there are overlay scrollbars
3387 // that need to be painted. That will cause the second pass through the layer tree to run,
3388 // and we'll paint the scrollbars then. In the meantime, cache tx and ty so that the
3389 // second pass doesn't need to re-enter the RenderTree to get it right.
3390 if (hasOverlayScrollbars() && !paintingOverlayControls) {
3391 m_cachedOverlayScrollbarOffset = paintOffset;
3393 // It's not necessary to do the second pass if the scrollbars paint into layers.
3394 if ((m_hBar && layerForHorizontalScrollbar()) || (m_vBar && layerForVerticalScrollbar()))
3396 IntRect localDamgeRect = damageRect;
3397 localDamgeRect.moveBy(-paintOffset);
3398 if (!overflowControlsIntersectRect(localDamgeRect))
3401 RenderLayer* paintingRoot = enclosingCompositingLayer();
3403 paintingRoot = renderer().view().layer();
3405 paintingRoot->setContainsDirtyOverlayScrollbars(true);