Synchronize MathML WPT tests against upstream
[WebKit-https.git] / Source / WebCore / rendering / RenderLayer.cpp
1 /*
2  * Copyright (C) 2006-2017 Apple Inc. All rights reserved.
3  *
4  * Portions are Copyright (C) 1998 Netscape Communications Corporation.
5  *
6  * Other contributors:
7  *   Robert O'Callahan <roc+@cs.cmu.edu>
8  *   David Baron <dbaron@fas.harvard.edu>
9  *   Christian Biesinger <cbiesinger@web.de>
10  *   Randall Jesup <rjesup@wgate.com>
11  *   Roland Mainz <roland.mainz@informatik.med.uni-giessen.de>
12  *   Josh Soref <timeless@mac.com>
13  *   Boris Zbarsky <bzbarsky@mit.edu>
14  *
15  * This library is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU Lesser General Public
17  * License as published by the Free Software Foundation; either
18  * version 2.1 of the License, or (at your option) any later version.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
28  *
29  * Alternatively, the contents of this file may be used under the terms
30  * of either the Mozilla Public License Version 1.1, found at
31  * http://www.mozilla.org/MPL/ (the "MPL") or the GNU General Public
32  * License Version 2.0, found at http://www.fsf.org/copyleft/gpl.html
33  * (the "GPL"), in which case the provisions of the MPL or the GPL are
34  * applicable instead of those above.  If you wish to allow use of your
35  * version of this file only under the terms of one of those two
36  * licenses (the MPL or the GPL) and not to allow others to use your
37  * version of this file under the LGPL, indicate your decision by
38  * deletingthe provisions above and replace them with the notice and
39  * other provisions required by the MPL or the GPL, as the case may be.
40  * If you do not delete the provisions above, a recipient may use your
41  * version of this file under any of the LGPL, the MPL or the GPL.
42  */
43
44 #include "config.h"
45 #include "RenderLayer.h"
46
47 #include "BoxShape.h"
48 #include "CSSAnimationController.h"
49 #include "CSSFilter.h"
50 #include "CSSPropertyNames.h"
51 #include "Chrome.h"
52 #include "DebugPageOverlays.h"
53 #include "DeprecatedGlobalSettings.h"
54 #include "Document.h"
55 #include "DocumentEventQueue.h"
56 #include "DocumentMarkerController.h"
57 #include "DocumentTimeline.h"
58 #include "Editor.h"
59 #include "Element.h"
60 #include "EventHandler.h"
61 #include "FEColorMatrix.h"
62 #include "FEMerge.h"
63 #include "FloatConversion.h"
64 #include "FloatPoint3D.h"
65 #include "FloatRect.h"
66 #include "FloatRoundedRect.h"
67 #include "FocusController.h"
68 #include "Frame.h"
69 #include "FrameLoader.h"
70 #include "FrameLoaderClient.h"
71 #include "FrameSelection.h"
72 #include "FrameTree.h"
73 #include "FrameView.h"
74 #include "Gradient.h"
75 #include "GraphicsContext.h"
76 #include "HTMLFormControlElement.h"
77 #include "HTMLFrameElement.h"
78 #include "HTMLFrameOwnerElement.h"
79 #include "HTMLIFrameElement.h"
80 #include "HTMLNames.h"
81 #include "HTMLParserIdioms.h"
82 #include "HitTestRequest.h"
83 #include "HitTestResult.h"
84 #include "HitTestingTransformState.h"
85 #include "Logging.h"
86 #include "OverflowEvent.h"
87 #include "OverlapTestRequestClient.h"
88 #include "Page.h"
89 #include "PlatformMouseEvent.h"
90 #include "RenderFlexibleBox.h"
91 #include "RenderFragmentContainer.h"
92 #include "RenderFragmentedFlow.h"
93 #include "RenderGeometryMap.h"
94 #include "RenderImage.h"
95 #include "RenderInline.h"
96 #include "RenderIterator.h"
97 #include "RenderLayerBacking.h"
98 #include "RenderLayerCompositor.h"
99 #include "RenderLayerFilters.h"
100 #include "RenderMarquee.h"
101 #include "RenderMultiColumnFlow.h"
102 #include "RenderReplica.h"
103 #include "RenderSVGResourceClipper.h"
104 #include "RenderSVGRoot.h"
105 #include "RenderScrollbar.h"
106 #include "RenderScrollbarPart.h"
107 #include "RenderTableCell.h"
108 #include "RenderTableRow.h"
109 #include "RenderText.h"
110 #include "RenderTheme.h"
111 #include "RenderTreeAsText.h"
112 #include "RenderView.h"
113 #include "RuntimeEnabledFeatures.h"
114 #include "SVGNames.h"
115 #include "ScaleTransformOperation.h"
116 #include "ScriptDisallowedScope.h"
117 #include "ScrollAnimator.h"
118 #include "Scrollbar.h"
119 #include "ScrollbarTheme.h"
120 #include "ScrollingCoordinator.h"
121 #include "Settings.h"
122 #include "ShadowRoot.h"
123 #include "SourceGraphic.h"
124 #include "StyleProperties.h"
125 #include "StyleResolver.h"
126 #include "TransformationMatrix.h"
127 #include "TranslateTransformOperation.h"
128 #include "WheelEventTestTrigger.h"
129 #include <stdio.h>
130 #include <wtf/MonotonicTime.h>
131 #include <wtf/StdLibExtras.h>
132 #include <wtf/text/CString.h>
133 #include <wtf/text/TextStream.h>
134
135 #if ENABLE(CSS_SCROLL_SNAP)
136 #include "AxisScrollSnapOffsets.h"
137 #endif
138
139 #define MIN_INTERSECT_FOR_REVEAL 32
140
141 namespace WebCore {
142
143 using namespace HTMLNames;
144
145 class ClipRects : public RefCounted<ClipRects> {
146     WTF_MAKE_FAST_ALLOCATED;
147 public:
148     static Ref<ClipRects> create()
149     {
150         return adoptRef(*new ClipRects);
151     }
152
153     static Ref<ClipRects> create(const ClipRects& other)
154     {
155         return adoptRef(*new ClipRects(other));
156     }
157
158     void reset()
159     {
160         m_overflowClipRect.reset();
161         m_fixedClipRect.reset();
162         m_posClipRect.reset();
163         m_fixed = false;
164     }
165
166     const ClipRect& overflowClipRect() const { return m_overflowClipRect; }
167     void setOverflowClipRect(const ClipRect& clipRect) { m_overflowClipRect = clipRect; }
168
169     const ClipRect& fixedClipRect() const { return m_fixedClipRect; }
170     void setFixedClipRect(const ClipRect& clipRect) { m_fixedClipRect = clipRect; }
171
172     const ClipRect& posClipRect() const { return m_posClipRect; }
173     void setPosClipRect(const ClipRect& clipRect) { m_posClipRect = clipRect; }
174
175     bool fixed() const { return m_fixed; }
176     void setFixed(bool fixed) { m_fixed = fixed; }
177
178     bool operator==(const ClipRects& other) const
179     {
180         return m_overflowClipRect == other.overflowClipRect()
181             && m_fixedClipRect == other.fixedClipRect()
182             && m_posClipRect == other.posClipRect()
183             && m_fixed == other.fixed();
184     }
185
186     ClipRects& operator=(const ClipRects& other)
187     {
188         m_overflowClipRect = other.overflowClipRect();
189         m_fixedClipRect = other.fixedClipRect();
190         m_posClipRect = other.posClipRect();
191         m_fixed = other.fixed();
192         return *this;
193     }
194
195 private:
196     ClipRects() = default;
197
198     ClipRects(const LayoutRect& clipRect)
199         : m_overflowClipRect(clipRect)
200         , m_fixedClipRect(clipRect)
201         , m_posClipRect(clipRect)
202     {
203     }
204
205     ClipRects(const ClipRects& other)
206         : RefCounted()
207         , m_fixed(other.fixed())
208         , m_overflowClipRect(other.overflowClipRect())
209         , m_fixedClipRect(other.fixedClipRect())
210         , m_posClipRect(other.posClipRect())
211     {
212     }
213
214     bool m_fixed { false };
215     ClipRect m_overflowClipRect;
216     ClipRect m_fixedClipRect;
217     ClipRect m_posClipRect;
218 };
219
220 class ClipRectsCache {
221     WTF_MAKE_FAST_ALLOCATED;
222 public:
223     ClipRectsCache()
224     {
225 #ifndef NDEBUG
226         for (int i = 0; i < NumCachedClipRectsTypes; ++i) {
227             m_clipRectsRoot[i] = 0;
228             m_scrollbarRelevancy[i] = IgnoreOverlayScrollbarSize;
229         }
230 #endif
231     }
232
233     ClipRects* getClipRects(ClipRectsType clipRectsType, ShouldRespectOverflowClip respectOverflow) const
234     {
235         return m_clipRects[getIndex(clipRectsType, respectOverflow)].get();
236     }
237
238     void setClipRects(ClipRectsType clipRectsType, ShouldRespectOverflowClip respectOverflow, RefPtr<ClipRects>&& clipRects)
239     {
240         m_clipRects[getIndex(clipRectsType, respectOverflow)] = WTFMove(clipRects);
241     }
242
243 #ifndef NDEBUG
244     const RenderLayer* m_clipRectsRoot[NumCachedClipRectsTypes];
245     OverlayScrollbarSizeRelevancy m_scrollbarRelevancy[NumCachedClipRectsTypes];
246 #endif
247
248 private:
249     unsigned getIndex(ClipRectsType clipRectsType, ShouldRespectOverflowClip respectOverflow) const
250     {
251         unsigned index = static_cast<unsigned>(clipRectsType);
252         if (respectOverflow == RespectOverflowClip)
253             index += static_cast<unsigned>(NumCachedClipRectsTypes);
254         ASSERT_WITH_SECURITY_IMPLICATION(index < NumCachedClipRectsTypes * 2);
255         return index;
256     }
257
258     RefPtr<ClipRects> m_clipRects[NumCachedClipRectsTypes * 2];
259 };
260
261 void makeMatrixRenderable(TransformationMatrix& matrix, bool has3DRendering)
262 {
263 #if !ENABLE(3D_TRANSFORMS)
264     UNUSED_PARAM(has3DRendering);
265     matrix.makeAffine();
266 #else
267     if (!has3DRendering)
268         matrix.makeAffine();
269 #endif
270 }
271
272 #if !LOG_DISABLED
273 static TextStream& operator<<(TextStream& ts, const ClipRects& clipRects)
274 {
275     TextStream::GroupScope scope(ts);
276     ts << indent << "ClipRects\n";
277     ts << indent << "  overflow  : " << clipRects.overflowClipRect() << "\n";
278     ts << indent << "  fixed     : " << clipRects.fixedClipRect() << "\n";
279     ts << indent << "  positioned: " << clipRects.posClipRect() << "\n";
280
281     return ts;
282 }
283
284 #endif
285
286 RenderLayer::RenderLayer(RenderLayerModelObject& rendererLayerModelObject)
287     : m_isRenderViewLayer(rendererLayerModelObject.isRenderView())
288     , m_forcedStackingContext(rendererLayerModelObject.isMedia())
289     , m_isOpportunisticStackingContext(false)
290     , m_zOrderListsDirty(false)
291     , m_normalFlowListDirty(true)
292     , m_hadNegativeZOrderList(false)
293     , m_inResizeMode(false)
294     , m_scrollDimensionsDirty(true)
295     , m_hasSelfPaintingLayerDescendant(false)
296     , m_hasSelfPaintingLayerDescendantDirty(false)
297     , m_usedTransparency(false)
298     , m_paintingInsideReflection(false)
299     , m_inOverflowRelayout(false)
300     , m_repaintStatus(NeedsNormalRepaint)
301     , m_visibleContentStatusDirty(true)
302     , m_hasVisibleContent(false)
303     , m_visibleDescendantStatusDirty(false)
304     , m_hasVisibleDescendant(false)
305     , m_registeredScrollableArea(false)
306     , m_isFixedIntersectingViewport(false)
307     , m_behavesAsFixed(false)
308     , m_3DTransformedDescendantStatusDirty(true)
309     , m_has3DTransformedDescendant(false)
310     , m_hasCompositingDescendant(false)
311     , m_hasCompositedScrollingAncestor(false)
312     , m_hasCompositedScrollableOverflow(false)
313     , m_hasTransformedAncestor(false)
314     , m_has3DTransformedAncestor(false)
315     , m_indirectCompositingReason(static_cast<unsigned>(IndirectCompositingReason::None))
316     , m_viewportConstrainedNotCompositedReason(NoNotCompositedReason)
317 #if PLATFORM(IOS_FAMILY)
318 #if ENABLE(IOS_TOUCH_EVENTS)
319     , m_registeredAsTouchEventListenerForScrolling(false)
320 #endif
321     , m_adjustForIOSCaretWhenScrolling(false)
322 #endif
323     , m_requiresScrollPositionReconciliation(false)
324     , m_containsDirtyOverlayScrollbars(false)
325     , m_updatingMarqueePosition(false)
326 #if !ASSERT_DISABLED
327     , m_layerListMutationAllowed(true)
328 #endif
329 #if ENABLE(CSS_COMPOSITING)
330     , m_blendMode(static_cast<unsigned>(BlendMode::Normal))
331     , m_hasNotIsolatedCompositedBlendingDescendants(false)
332     , m_hasNotIsolatedBlendingDescendants(false)
333     , m_hasNotIsolatedBlendingDescendantsStatusDirty(false)
334 #endif
335     , m_renderer(rendererLayerModelObject)
336 {
337     setIsNormalFlowOnly(shouldBeNormalFlowOnly());
338     setIsCSSStackingContext(shouldBeCSSStackingContext());
339
340     m_isSelfPaintingLayer = shouldBeSelfPaintingLayer();
341
342     if (!renderer().firstChild()) {
343         m_visibleContentStatusDirty = false;
344         m_hasVisibleContent = renderer().style().visibility() == Visibility::Visible;
345     }
346
347     if (Element* element = renderer().element()) {
348         // We save and restore only the scrollOffset as the other scroll values are recalculated.
349         m_scrollPosition = element->savedLayerScrollPosition();
350         if (!m_scrollPosition.isZero())
351             scrollAnimator().setCurrentPosition(m_scrollPosition);
352         element->setSavedLayerScrollPosition(IntPoint());
353     }
354 }
355
356 RenderLayer::~RenderLayer()
357 {
358     if (inResizeMode())
359         renderer().frame().eventHandler().resizeLayerDestroyed();
360
361     ASSERT(m_registeredScrollableArea == renderer().view().frameView().containsScrollableArea(this));
362
363     if (m_registeredScrollableArea)
364         renderer().view().frameView().removeScrollableArea(this);
365
366 #if ENABLE(IOS_TOUCH_EVENTS)
367     unregisterAsTouchEventListenerForScrolling();
368 #endif
369     if (Element* element = renderer().element())
370         element->setSavedLayerScrollPosition(m_scrollPosition);
371
372     destroyScrollbar(HorizontalScrollbar);
373     destroyScrollbar(VerticalScrollbar);
374
375     if (auto* scrollingCoordinator = renderer().page().scrollingCoordinator())
376         scrollingCoordinator->willDestroyScrollableArea(*this);
377
378     if (m_reflection)
379         removeReflection();
380
381     clearScrollCorner();
382     clearResizer();
383
384     clearLayerFilters();
385
386     if (paintsIntoProvidedBacking()) {
387         auto* backingProviderLayer = this->backingProviderLayer();
388         if (backingProviderLayer->backing())
389             backingProviderLayer->backing()->removeBackingSharingLayer(*this);
390     }
391
392     // Child layers will be deleted by their corresponding render objects, so
393     // we don't need to delete them ourselves.
394
395     clearBacking(true);
396
397     // Layer and all its children should be removed from the tree before destruction.
398     RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(renderer().renderTreeBeingDestroyed() || !parent());
399     RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(renderer().renderTreeBeingDestroyed() || !firstChild());
400 }
401
402 void RenderLayer::addChild(RenderLayer& child, RenderLayer* beforeChild)
403 {
404     RenderLayer* prevSibling = beforeChild ? beforeChild->previousSibling() : lastChild();
405     if (prevSibling) {
406         child.setPreviousSibling(prevSibling);
407         prevSibling->setNextSibling(&child);
408         ASSERT(prevSibling != &child);
409     } else
410         setFirstChild(&child);
411
412     if (beforeChild) {
413         beforeChild->setPreviousSibling(&child);
414         child.setNextSibling(beforeChild);
415         ASSERT(beforeChild != &child);
416     } else
417         setLastChild(&child);
418
419     child.setParent(this);
420
421     dirtyPaintOrderListsOnChildChange(child);
422
423     child.updateDescendantDependentFlags();
424     if (child.m_hasVisibleContent || child.m_hasVisibleDescendant)
425         setAncestorChainHasVisibleDescendant();
426
427     if (child.isSelfPaintingLayer() || child.hasSelfPaintingLayerDescendant())
428         setAncestorChainHasSelfPaintingLayerDescendant();
429
430     if (compositor().hasContentCompositingLayers())
431         setDescendantsNeedCompositingRequirementsTraversal();
432
433     if (child.hasDescendantNeedingCompositingRequirementsTraversal() || child.needsCompositingRequirementsTraversal())
434         child.setAncestorsHaveCompositingDirtyFlag(Compositing::HasDescendantNeedingRequirementsTraversal);
435
436     if (child.hasDescendantNeedingUpdateBackingOrHierarchyTraversal() || child.needsUpdateBackingOrHierarchyTraversal())
437         child.setAncestorsHaveCompositingDirtyFlag(Compositing::HasDescendantNeedingBackingOrHierarchyTraversal);
438
439 #if ENABLE(CSS_COMPOSITING)
440     if (child.hasBlendMode() || (child.hasNotIsolatedBlendingDescendants() && !child.isolatesBlending()))
441         updateAncestorChainHasBlendingDescendants(); // Why not just dirty?
442 #endif
443
444     compositor().layerWasAdded(*this, child);
445 }
446
447 void RenderLayer::removeChild(RenderLayer& oldChild)
448 {
449     if (!renderer().renderTreeBeingDestroyed())
450         compositor().layerWillBeRemoved(*this, oldChild);
451
452     // remove the child
453     if (oldChild.previousSibling())
454         oldChild.previousSibling()->setNextSibling(oldChild.nextSibling());
455     if (oldChild.nextSibling())
456         oldChild.nextSibling()->setPreviousSibling(oldChild.previousSibling());
457
458     if (m_first == &oldChild)
459         m_first = oldChild.nextSibling();
460     if (m_last == &oldChild)
461         m_last = oldChild.previousSibling();
462
463     dirtyPaintOrderListsOnChildChange(oldChild);
464
465     oldChild.setPreviousSibling(nullptr);
466     oldChild.setNextSibling(nullptr);
467     oldChild.setParent(nullptr);
468     
469     oldChild.updateDescendantDependentFlags();
470     if (oldChild.m_hasVisibleContent || oldChild.m_hasVisibleDescendant)
471         dirtyAncestorChainVisibleDescendantStatus();
472
473     if (oldChild.isSelfPaintingLayer() || oldChild.hasSelfPaintingLayerDescendant())
474         dirtyAncestorChainHasSelfPaintingLayerDescendantStatus();
475
476     if (compositor().hasContentCompositingLayers())
477         setDescendantsNeedCompositingRequirementsTraversal();
478
479 #if ENABLE(CSS_COMPOSITING)
480     if (oldChild.hasBlendMode() || (oldChild.hasNotIsolatedBlendingDescendants() && !oldChild.isolatesBlending()))
481         dirtyAncestorChainHasBlendingDescendants();
482 #endif
483 }
484
485 void RenderLayer::dirtyPaintOrderListsOnChildChange(RenderLayer& child)
486 {
487     if (child.isNormalFlowOnly())
488         dirtyNormalFlowList();
489
490     if (!child.isNormalFlowOnly() || child.firstChild()) {
491         // Dirty the z-order list in which we are contained. The stackingContext() can be null in the
492         // case where we're building up generated content layers. This is ok, since the lists will start
493         // off dirty in that case anyway.
494         child.dirtyStackingContextZOrderLists();
495     }
496 }
497
498 void RenderLayer::insertOnlyThisLayer()
499 {
500     if (!m_parent && renderer().parent()) {
501         // We need to connect ourselves when our renderer() has a parent.
502         // Find our enclosingLayer and add ourselves.
503         RenderLayer* parentLayer = renderer().parent()->enclosingLayer();
504         ASSERT(parentLayer);
505         RenderLayer* beforeChild = parentLayer->reflectionLayer() != this ? renderer().parent()->findNextLayer(parentLayer, &renderer()) : nullptr;
506         parentLayer->addChild(*this, beforeChild);
507     }
508
509     // Remove all descendant layers from the hierarchy and add them to the new position.
510     for (auto& child : childrenOfType<RenderElement>(renderer()))
511         child.moveLayers(m_parent, this);
512
513     // Clear out all the clip rects.
514     clearClipRectsIncludingDescendants();
515 }
516
517 void RenderLayer::removeOnlyThisLayer()
518 {
519     if (!m_parent)
520         return;
521
522     // Mark that we are about to lose our layer. This makes render tree
523     // walks ignore this layer while we're removing it.
524     renderer().setHasLayer(false);
525
526     compositor().layerWillBeRemoved(*m_parent, *this);
527
528     // Dirty the clip rects.
529     clearClipRectsIncludingDescendants();
530
531     RenderLayer* nextSib = nextSibling();
532
533     // Remove the child reflection layer before moving other child layers.
534     // The reflection layer should not be moved to the parent.
535     if (reflection())
536         removeChild(*reflectionLayer());
537
538     // Now walk our kids and reattach them to our parent.
539     RenderLayer* current = m_first;
540     while (current) {
541         RenderLayer* next = current->nextSibling();
542         removeChild(*current);
543         m_parent->addChild(*current, nextSib);
544         current->setRepaintStatus(NeedsFullRepaint);
545         current = next;
546     }
547
548     // Remove us from the parent.
549     m_parent->removeChild(*this);
550     renderer().destroyLayer();
551 }
552
553 static bool canCreateStackingContext(const RenderLayer& layer)
554 {
555     auto& renderer = layer.renderer();
556     return renderer.hasTransformRelatedProperty()
557         || renderer.hasClipPath()
558         || renderer.hasFilter()
559         || renderer.hasMask()
560         || renderer.hasBackdropFilter()
561 #if ENABLE(CSS_COMPOSITING)
562         || renderer.hasBlendMode()
563 #endif
564         || renderer.isTransparent()
565         || renderer.isPositioned() // Note that this only creates stacking context in conjunction with explicit z-index.
566         || renderer.hasReflection()
567         || renderer.style().hasIsolation()
568         || !renderer.style().hasAutoZIndex()
569         || (renderer.style().willChange() && renderer.style().willChange()->canCreateStackingContext());
570 }
571
572 bool RenderLayer::shouldBeNormalFlowOnly() const
573 {
574     if (canCreateStackingContext(*this))
575         return false;
576
577     return renderer().hasOverflowClip()
578         || renderer().isCanvas()
579         || renderer().isVideo()
580         || renderer().isEmbeddedObject()
581         || renderer().isRenderIFrame()
582         || (renderer().isRenderImage() && downcast<RenderImage>(renderer()).isEditableImage())
583         || (renderer().style().specifiesColumns() && !isRenderViewLayer())
584         || renderer().isInFlowRenderFragmentedFlow();
585 }
586
587 bool RenderLayer::shouldBeCSSStackingContext() const
588 {
589     return !renderer().style().hasAutoZIndex() || isRenderViewLayer();
590 }
591
592 bool RenderLayer::setIsNormalFlowOnly(bool isNormalFlowOnly)
593 {
594     if (isNormalFlowOnly == m_isNormalFlowOnly)
595         return false;
596     
597     m_isNormalFlowOnly = isNormalFlowOnly;
598
599     if (auto* p = parent())
600         p->dirtyNormalFlowList();
601     dirtyStackingContextZOrderLists();
602     return true;
603 }
604
605 void RenderLayer::isStackingContextChanged()
606 {
607     dirtyStackingContextZOrderLists();
608     if (isStackingContext())
609         dirtyZOrderLists();
610     else
611         clearZOrderLists();
612 }
613
614 bool RenderLayer::setIsOpportunisticStackingContext(bool isStacking)
615 {
616     bool wasStacking = isStackingContext();
617     m_isOpportunisticStackingContext = isStacking;
618     if (wasStacking == isStackingContext())
619         return false;
620
621     isStackingContextChanged();
622     return true;
623 }
624
625 bool RenderLayer::setIsCSSStackingContext(bool isCSSStackingContext)
626 {
627     bool wasStacking = isStackingContext();
628     m_isCSSStackingContext = isCSSStackingContext;
629     if (wasStacking == isStackingContext())
630         return false;
631
632     isStackingContextChanged();
633     return true;
634 }
635
636 void RenderLayer::setParent(RenderLayer* parent)
637 {
638     if (parent == m_parent)
639         return;
640
641     if (m_parent && !renderer().renderTreeBeingDestroyed())
642         compositor().layerWillBeRemoved(*m_parent, *this);
643
644     m_parent = parent;
645
646     if (m_parent && !renderer().renderTreeBeingDestroyed())
647         compositor().layerWasAdded(*m_parent, *this);
648 }
649
650 RenderLayer* RenderLayer::stackingContext() const
651 {
652     auto* layer = parent();
653     while (layer && !layer->isStackingContext())
654         layer = layer->parent();
655
656     ASSERT(!layer || layer->isStackingContext());
657     return layer;
658 }
659
660 void RenderLayer::dirtyZOrderLists()
661 {
662     ASSERT(layerListMutationAllowed());
663     ASSERT(isStackingContext());
664
665     if (m_posZOrderList)
666         m_posZOrderList->clear();
667     if (m_negZOrderList)
668         m_negZOrderList->clear();
669     m_zOrderListsDirty = true;
670
671     // FIXME: Ideally, we'd only dirty if the lists changed.
672     if (hasCompositingDescendant())
673         setNeedsCompositingPaintOrderChildrenUpdate();
674 }
675
676 void RenderLayer::dirtyStackingContextZOrderLists()
677 {
678     if (auto* sc = stackingContext())
679         sc->dirtyZOrderLists();
680 }
681
682 void RenderLayer::dirtyNormalFlowList()
683 {
684     ASSERT(layerListMutationAllowed());
685
686     if (m_normalFlowList)
687         m_normalFlowList->clear();
688     m_normalFlowListDirty = true;
689
690     if (hasCompositingDescendant())
691         setNeedsCompositingPaintOrderChildrenUpdate();
692 }
693
694 void RenderLayer::updateNormalFlowList()
695 {
696     if (!m_normalFlowListDirty)
697         return;
698
699     ASSERT(layerListMutationAllowed());
700
701     for (RenderLayer* child = firstChild(); child; child = child->nextSibling()) {
702         // Ignore non-overflow layers and reflections.
703         if (child->isNormalFlowOnly() && !isReflectionLayer(*child)) {
704             if (!m_normalFlowList)
705                 m_normalFlowList = makeUnique<Vector<RenderLayer*>>();
706             m_normalFlowList->append(child);
707         }
708     }
709     
710     m_normalFlowListDirty = false;
711 }
712
713 void RenderLayer::rebuildZOrderLists()
714 {
715     ASSERT(layerListMutationAllowed());
716     ASSERT(isDirtyStackingContext());
717     rebuildZOrderLists(m_posZOrderList, m_negZOrderList);
718     m_zOrderListsDirty = false;
719     
720     bool hasNegativeZOrderList = m_negZOrderList && m_negZOrderList->size();
721     // Having negative z-order lists affect whether a compositing layer needs a foreground layer.
722     // Ideally we'd only trigger this when having z-order children changes, but we blow away the old z-order
723     // lists on dirtying so we don't know the old state.
724     if (hasNegativeZOrderList != m_hadNegativeZOrderList) {
725         m_hadNegativeZOrderList = hasNegativeZOrderList;
726         if (isComposited())
727             setNeedsCompositingConfigurationUpdate();
728     }
729 }
730
731 void RenderLayer::rebuildZOrderLists(std::unique_ptr<Vector<RenderLayer*>>& posZOrderList, std::unique_ptr<Vector<RenderLayer*>>& negZOrderList)
732 {
733     bool includeHiddenLayers = compositor().usesCompositing();
734     for (RenderLayer* child = firstChild(); child; child = child->nextSibling()) {
735         if (!isReflectionLayer(*child))
736             child->collectLayers(includeHiddenLayers, posZOrderList, negZOrderList);
737     }
738
739     auto compareZIndex = [] (const RenderLayer* first, const RenderLayer* second) -> bool {
740         return first->zIndex() < second->zIndex();
741     };
742
743     // Sort the two lists.
744     if (posZOrderList)
745         std::stable_sort(posZOrderList->begin(), posZOrderList->end(), compareZIndex);
746
747     if (negZOrderList)
748         std::stable_sort(negZOrderList->begin(), negZOrderList->end(), compareZIndex);
749 }
750
751 void RenderLayer::collectLayers(bool includeHiddenLayers, std::unique_ptr<Vector<RenderLayer*>>& positiveZOrderList, std::unique_ptr<Vector<RenderLayer*>>& negativeZOrderList)
752 {
753     updateDescendantDependentFlags();
754
755     bool isStacking = isStackingContext();
756     // Overflow layers are just painted by their enclosing layers, so they don't get put in zorder lists.
757     bool includeHiddenLayer = includeHiddenLayers || (m_hasVisibleContent || (m_hasVisibleDescendant && isStacking));
758     if (includeHiddenLayer && !isNormalFlowOnly()) {
759         auto& layerList = (zIndex() >= 0) ? positiveZOrderList : negativeZOrderList;
760         if (!layerList)
761             layerList = makeUnique<Vector<RenderLayer*>>();
762         layerList->append(this);
763     }
764
765     // Recur into our children to collect more layers, but only if we don't establish
766     // a stacking context/container.
767     if ((includeHiddenLayers || m_hasVisibleDescendant) && !isStacking) {
768         for (RenderLayer* child = firstChild(); child; child = child->nextSibling()) {
769             // Ignore reflections.
770             if (!isReflectionLayer(*child))
771                 child->collectLayers(includeHiddenLayers, positiveZOrderList, negativeZOrderList);
772         }
773     }
774 }
775
776 void RenderLayer::setAncestorsHaveCompositingDirtyFlag(Compositing flag)
777 {
778     for (auto* layer = paintOrderParent(); layer; layer = layer->paintOrderParent()) {
779         if (layer->m_compositingDirtyBits.contains(flag))
780             break;
781         layer->m_compositingDirtyBits.add(flag);
782     }
783 }
784
785 void RenderLayer::updateLayerListsIfNeeded()
786 {
787     updateZOrderLists();
788     updateNormalFlowList();
789
790     if (RenderLayer* reflectionLayer = this->reflectionLayer()) {
791         reflectionLayer->updateZOrderLists();
792         reflectionLayer->updateNormalFlowList();
793     }
794 }
795
796 String RenderLayer::name() const
797 {
798     StringBuilder name;
799
800     if (Element* element = renderer().element()) {
801         name.append(" <");
802         name.append(element->tagName().convertToLowercaseWithoutLocale());
803         name.append('>');
804
805         if (element->hasID()) {
806             name.appendLiteral(" id=\'");
807             name.append(element->getIdAttribute());
808             name.append('\'');
809         }
810
811         if (element->hasClass()) {
812             name.appendLiteral(" class=\'");
813             size_t classNamesToDump = element->classNames().size();
814             const size_t maxNumClassNames = 7;
815             bool addEllipsis = false;
816             if (classNamesToDump > maxNumClassNames) {
817                 classNamesToDump = maxNumClassNames;
818                 addEllipsis = true;
819             }
820             
821             for (size_t i = 0; i < classNamesToDump; ++i) {
822                 if (i > 0)
823                     name.append(' ');
824                 name.append(element->classNames()[i]);
825             }
826             if (addEllipsis)
827                 name.append("...");
828             name.append('\'');
829         }
830     } else
831         name.append(renderer().renderName());
832
833     if (isReflection())
834         name.appendLiteral(" (reflection)");
835
836     return name.toString();
837 }
838
839 RenderLayerCompositor& RenderLayer::compositor() const
840 {
841     return renderer().view().compositor();
842 }
843
844 void RenderLayer::contentChanged(ContentChangeType changeType)
845 {
846     if (changeType == CanvasChanged || changeType == VideoChanged || changeType == FullScreenChanged || (isComposited() && changeType == ImageChanged)) {
847         setNeedsPostLayoutCompositingUpdate();
848         setNeedsCompositingConfigurationUpdate();
849     }
850
851     if (auto* backing = this->backing())
852         backing->contentChanged(changeType);
853 }
854
855 bool RenderLayer::canRender3DTransforms() const
856 {
857     return compositor().canRender3DTransforms();
858 }
859
860 bool RenderLayer::paintsWithFilters() const
861 {
862     if (!renderer().hasFilter())
863         return false;
864         
865     if (!isComposited())
866         return true;
867
868     return !m_backing->canCompositeFilters();
869 }
870
871 bool RenderLayer::requiresFullLayerImageForFilters() const 
872 {
873     if (!paintsWithFilters())
874         return false;
875
876     return m_filters && m_filters->hasFilterThatMovesPixels();
877 }
878
879 OptionSet<RenderLayer::UpdateLayerPositionsFlag> RenderLayer::flagsForUpdateLayerPositions(RenderLayer& startingLayer)
880 {
881     OptionSet<UpdateLayerPositionsFlag> flags = { CheckForRepaint };
882
883     if (auto* parent = startingLayer.parent()) {
884         if (parent->hasTransformedAncestor() || parent->transform())
885             flags.add(SeenTransformedLayer);
886
887         if (parent->has3DTransformedAncestor() || (parent->transform() && !parent->transform()->isAffine()))
888             flags.add(Seen3DTransformedLayer);
889
890         if (parent->behavesAsFixed() || (parent->renderer().isFixedPositioned() && !parent->hasTransformedAncestor()))
891             flags.add(SeenFixedLayer);
892
893         if (parent->hasCompositedScrollingAncestor() || parent->hasCompositedScrollableOverflow())
894             flags.add(SeenCompositedScrollingLayer);
895     }
896
897     return flags;
898 }
899
900 void RenderLayer::updateLayerPositionsAfterStyleChange()
901 {
902     updateLayerPositions(nullptr, flagsForUpdateLayerPositions(*this));
903 }
904
905 void RenderLayer::updateLayerPositionsAfterLayout(bool isRelayoutingSubtree, bool didFullRepaint)
906 {
907     auto updateLayerPositionFlags = [&](bool isRelayoutingSubtree, bool didFullRepaint) {
908         auto flags = flagsForUpdateLayerPositions(*this);
909         if (didFullRepaint) {
910             flags.remove(RenderLayer::CheckForRepaint);
911             flags.add(RenderLayer::NeedsFullRepaintInBacking);
912         }
913         if (isRelayoutingSubtree && enclosingPaginationLayer(RenderLayer::IncludeCompositedPaginatedLayers))
914             flags.add(RenderLayer::UpdatePagination);
915         return flags;
916     };
917
918     LOG(Compositing, "RenderLayer %p updateLayerPositionsAfterLayout", this);
919     RenderGeometryMap geometryMap(UseTransforms);
920     if (!isRenderViewLayer())
921         geometryMap.pushMappingsToAncestor(parent(), nullptr);
922
923     updateLayerPositions(&geometryMap, updateLayerPositionFlags(isRelayoutingSubtree, didFullRepaint));
924 }
925
926 void RenderLayer::updateLayerPositions(RenderGeometryMap* geometryMap, OptionSet<UpdateLayerPositionsFlag> flags)
927 {
928     updateLayerPosition(&flags);
929     applyPostLayoutScrollPositionIfNeeded();
930
931     if (geometryMap)
932         geometryMap->pushMappingsToAncestor(this, parent());
933
934     // Clear our cached clip rect information.
935     clearClipRects();
936     
937     if (hasOverflowControls()) {
938         LayoutSize offsetFromRoot;
939         if (geometryMap)
940             offsetFromRoot = LayoutSize(toFloatSize(geometryMap->absolutePoint(FloatPoint())));
941         else {
942             // FIXME: It looks suspicious to call convertToLayerCoords here
943             // as canUseOffsetFromAncestor may be true for an ancestor layer.
944             offsetFromRoot = offsetFromAncestor(root());
945         }
946         positionOverflowControls(roundedIntSize(offsetFromRoot));
947     }
948
949     updateDescendantDependentFlags();
950
951     if (flags & UpdatePagination)
952         updatePagination();
953     else
954         m_enclosingPaginationLayer = nullptr;
955     
956     if (m_hasVisibleContent) {
957         // FIXME: Paint offset cache does not work with RenderLayers as there is not a 1-to-1
958         // mapping between them and the RenderObjects. It would be neat to enable
959         // LayoutState outside the layout() phase and use it here.
960         ASSERT(!renderer().view().frameView().layoutContext().isPaintOffsetCacheEnabled());
961
962         RenderLayerModelObject* repaintContainer = renderer().containerForRepaint();
963         
964         auto hadRepaintLayoutRects = renderer().hasRepaintLayoutRects();
965         RepaintLayoutRects oldRects = hadRepaintLayoutRects ? renderer().repaintLayoutRects() : RepaintLayoutRects();
966         computeRepaintRects(repaintContainer, geometryMap);
967         
968         auto hasRepaintLayoutRects = renderer().hasRepaintLayoutRects();
969         RepaintLayoutRects newRects = hasRepaintLayoutRects ? renderer().repaintLayoutRects() : RepaintLayoutRects();
970         // FIXME: Should ASSERT that value calculated for m_outlineBox using the cached offset is the same
971         // as the value not using the cached offset, but we can't due to https://bugs.webkit.org/show_bug.cgi?id=37048
972         if ((flags & CheckForRepaint) && hasRepaintLayoutRects) {
973             if (!renderer().view().printing()) {
974                 if (m_repaintStatus & NeedsFullRepaint) {
975                     if (hadRepaintLayoutRects)
976                         renderer().repaintUsingContainer(repaintContainer, oldRects.m_repaintRect);
977                     if (!hadRepaintLayoutRects || newRects.m_repaintRect != oldRects.m_repaintRect)
978                         renderer().repaintUsingContainer(repaintContainer, newRects.m_repaintRect);
979                 } else if (shouldRepaintAfterLayout()) {
980                     // FIXME: We will convert this to just take the old and new RepaintLayoutRects once
981                     // we change other callers to use RepaintLayoutRects.
982                     renderer().repaintAfterLayoutIfNeeded(repaintContainer, oldRects.m_repaintRect, oldRects.m_outlineBox, &newRects.m_repaintRect, &newRects.m_outlineBox);
983                 }
984             }
985         }
986     } else
987         clearRepaintRects();
988
989     m_repaintStatus = NeedsNormalRepaint;
990     m_hasTransformedAncestor = flags.contains(SeenTransformedLayer);
991     m_has3DTransformedAncestor = flags.contains(Seen3DTransformedLayer);
992     m_behavesAsFixed = flags.contains(SeenFixedLayer);
993     setHasCompositedScrollingAncestor(flags.contains(SeenCompositedScrollingLayer));
994
995     // Update the reflection's position and size.
996     if (m_reflection)
997         m_reflection->layout();
998
999     if (renderer().isInFlowRenderFragmentedFlow()) {
1000         updatePagination();
1001         flags.add(UpdatePagination);
1002     }
1003
1004     if (transform()) {
1005         flags.add(SeenTransformedLayer);
1006         if (!transform()->isAffine())
1007             flags.add(Seen3DTransformedLayer);
1008     }
1009
1010     // Fixed inside transform behaves like absolute (per spec).
1011     if (renderer().isFixedPositioned() && !m_hasTransformedAncestor) {
1012         m_behavesAsFixed = true;
1013         flags.add(SeenFixedLayer);
1014     }
1015
1016     if (hasCompositedScrollableOverflow())
1017         flags.add(SeenCompositedScrollingLayer);
1018
1019     for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
1020         child->updateLayerPositions(geometryMap, flags);
1021
1022     // With all our children positioned, now update our marquee if we need to.
1023     if (m_marquee) {
1024         // FIXME: would like to use SetForScope<> but it doesn't work with bitfields.
1025         bool oldUpdatingMarqueePosition = m_updatingMarqueePosition;
1026         m_updatingMarqueePosition = true;
1027         m_marquee->updateMarqueePosition();
1028         m_updatingMarqueePosition = oldUpdatingMarqueePosition;
1029     }
1030
1031     if (renderer().isFixedPositioned() && renderer().settings().acceleratedCompositingForFixedPositionEnabled()) {
1032         bool intersectsViewport = compositor().fixedLayerIntersectsViewport(*this);
1033         if (intersectsViewport != m_isFixedIntersectingViewport) {
1034             m_isFixedIntersectingViewport = intersectsViewport;
1035             setNeedsPostLayoutCompositingUpdate();
1036         }
1037     }
1038
1039     if (isComposited())
1040         backing()->updateAfterLayout(flags.contains(ContainingClippingLayerChangedSize), flags.contains(NeedsFullRepaintInBacking));
1041
1042     if (geometryMap)
1043         geometryMap->popMappingsToAncestor(parent());
1044
1045     renderer().document().markers().invalidateRectsForAllMarkers();
1046 }
1047
1048 LayoutRect RenderLayer::repaintRectIncludingNonCompositingDescendants() const
1049 {
1050     LayoutRect repaintRect = renderer().repaintLayoutRects().m_repaintRect;
1051     for (RenderLayer* child = firstChild(); child; child = child->nextSibling()) {
1052         // Don't include repaint rects for composited child layers; they will paint themselves and have a different origin.
1053         if (child->isComposited())
1054             continue;
1055
1056         repaintRect.uniteIfNonZero(child->repaintRectIncludingNonCompositingDescendants());
1057     }
1058     return repaintRect;
1059 }
1060
1061 void RenderLayer::setAncestorChainHasSelfPaintingLayerDescendant()
1062 {
1063     for (RenderLayer* layer = this; layer; layer = layer->parent()) {
1064         if (!layer->m_hasSelfPaintingLayerDescendantDirty && layer->hasSelfPaintingLayerDescendant())
1065             break;
1066
1067         layer->m_hasSelfPaintingLayerDescendantDirty = false;
1068         layer->m_hasSelfPaintingLayerDescendant = true;
1069     }
1070 }
1071
1072 void RenderLayer::dirtyAncestorChainHasSelfPaintingLayerDescendantStatus()
1073 {
1074     for (RenderLayer* layer = this; layer; layer = layer->parent()) {
1075         layer->m_hasSelfPaintingLayerDescendantDirty = true;
1076         // If we have reached a self-painting layer, we know our parent should have a self-painting descendant
1077         // in this case, there is no need to dirty our ancestors further.
1078         if (layer->isSelfPaintingLayer()) {
1079             ASSERT(!parent() || parent()->m_hasSelfPaintingLayerDescendantDirty || parent()->hasSelfPaintingLayerDescendant());
1080             break;
1081         }
1082     }
1083 }
1084
1085 void RenderLayer::computeRepaintRects(const RenderLayerModelObject* repaintContainer, const RenderGeometryMap* geometryMap)
1086 {
1087     ASSERT(!m_visibleContentStatusDirty);
1088     renderer().computeRepaintLayoutRects(repaintContainer, geometryMap);
1089 }
1090
1091 void RenderLayer::computeRepaintRectsIncludingDescendants()
1092 {
1093     // FIXME: computeRepaintRects() has to walk up the parent chain for every layer to compute the rects.
1094     // We should make this more efficient.
1095     // FIXME: it's wrong to call this when layout is not up-to-date, which we do.
1096     computeRepaintRects(renderer().containerForRepaint());
1097
1098     for (RenderLayer* layer = firstChild(); layer; layer = layer->nextSibling())
1099         layer->computeRepaintRectsIncludingDescendants();
1100 }
1101
1102 void RenderLayer::clearRepaintRects()
1103 {
1104     ASSERT(!m_visibleContentStatusDirty);
1105
1106     renderer().clearRepaintLayoutRects();
1107 }
1108
1109 void RenderLayer::updateLayerPositionsAfterDocumentScroll()
1110 {
1111     ASSERT(this == renderer().view().layer());
1112
1113     LOG(Scrolling, "RenderLayer::updateLayerPositionsAfterDocumentScroll");
1114
1115     RenderGeometryMap geometryMap(UseTransforms);
1116     updateLayerPositionsAfterScroll(&geometryMap);
1117 }
1118
1119 void RenderLayer::updateLayerPositionsAfterOverflowScroll()
1120 {
1121     RenderGeometryMap geometryMap(UseTransforms);
1122     if (this != renderer().view().layer())
1123         geometryMap.pushMappingsToAncestor(parent(), nullptr);
1124
1125     // FIXME: why is it OK to not check the ancestors of this layer in order to
1126     // initialize the HasSeenViewportConstrainedAncestor and HasSeenAncestorWithOverflowClip flags?
1127     updateLayerPositionsAfterScroll(&geometryMap, IsOverflowScroll);
1128 }
1129
1130 void RenderLayer::updateLayerPositionsAfterScroll(RenderGeometryMap* geometryMap, OptionSet<UpdateLayerPositionsAfterScrollFlag> flags)
1131 {
1132     // FIXME: This shouldn't be needed, but there are some corner cases where
1133     // these flags are still dirty. Update so that the check below is valid.
1134     updateDescendantDependentFlags();
1135
1136     // If we have no visible content and no visible descendants, there is no point recomputing
1137     // our rectangles as they will be empty. If our visibility changes, we are expected to
1138     // recompute all our positions anyway.
1139     if (!m_hasVisibleDescendant && !m_hasVisibleContent)
1140         return;
1141
1142     bool positionChanged = updateLayerPosition();
1143     if (positionChanged)
1144         flags.add(HasChangedAncestor);
1145
1146     if (flags.containsAny({ HasChangedAncestor, HasSeenViewportConstrainedAncestor, IsOverflowScroll }))
1147         clearClipRects();
1148
1149     if (renderer().style().hasViewportConstrainedPosition())
1150         flags.add(HasSeenViewportConstrainedAncestor);
1151
1152     if (renderer().hasOverflowClip())
1153         flags.add(HasSeenAncestorWithOverflowClip);
1154     
1155     bool shouldComputeRepaintRects = (flags.contains(HasSeenViewportConstrainedAncestor) || flags.containsAll({ IsOverflowScroll, HasSeenAncestorWithOverflowClip })) && isSelfPaintingLayer();
1156     bool isVisuallyEmpty = !isVisuallyNonEmpty();
1157     bool shouldPushAndPopMappings = geometryMap && ((shouldComputeRepaintRects && !isVisuallyEmpty) || firstChild());
1158     if (shouldPushAndPopMappings)
1159         geometryMap->pushMappingsToAncestor(this, parent());
1160
1161     if (shouldComputeRepaintRects) {
1162         // When scrolling, we don't compute repaint rects for visually non-empty layers.
1163         if (isVisuallyEmpty)
1164             clearRepaintRects();
1165         else // FIXME: We could track the repaint container as we walk down the tree.
1166             computeRepaintRects(renderer().containerForRepaint(), geometryMap);
1167     } else if (!renderer().view().frameView().platformWidget()) {
1168         // When ScrollView's m_paintsEntireContents flag flips due to layer backing changes, the repaint area transitions from
1169         // visual to layout overflow. When this happens the cached repaint rects become invalid and they need to be recomputed (see webkit.org/b/188121).
1170         // Check that our cached rects are correct.
1171         ASSERT(!renderer().hasRepaintLayoutRects() || renderer().repaintLayoutRects().m_repaintRect == renderer().clippedOverflowRectForRepaint(renderer().containerForRepaint()));
1172         ASSERT(!renderer().hasRepaintLayoutRects() || renderer().repaintLayoutRects().m_outlineBox == renderer().outlineBoundsForRepaint(renderer().containerForRepaint()));
1173     }
1174     
1175     for (RenderLayer* child = firstChild(); child; child = child->nextSibling())
1176         child->updateLayerPositionsAfterScroll(geometryMap, flags);
1177
1178     // We don't update our reflection as scrolling is a translation which does not change the size()
1179     // of an object, thus RenderReplica will still repaint itself properly as the layer position was
1180     // updated above.
1181
1182     if (m_marquee) {
1183         bool oldUpdatingMarqueePosition = m_updatingMarqueePosition;
1184         m_updatingMarqueePosition = true;
1185         m_marquee->updateMarqueePosition();
1186         m_updatingMarqueePosition = oldUpdatingMarqueePosition;
1187     }
1188
1189     if (shouldPushAndPopMappings)
1190         geometryMap->popMappingsToAncestor(parent());
1191
1192     renderer().document().markers().invalidateRectsForAllMarkers();
1193 }
1194
1195 #if ENABLE(CSS_COMPOSITING)
1196
1197 void RenderLayer::updateBlendMode()
1198 {
1199     bool hadBlendMode = static_cast<BlendMode>(m_blendMode) != BlendMode::Normal;
1200     if (parent() && hadBlendMode != hasBlendMode()) {
1201         if (hasBlendMode())
1202             parent()->updateAncestorChainHasBlendingDescendants();
1203         else
1204             parent()->dirtyAncestorChainHasBlendingDescendants();
1205     }
1206
1207     BlendMode newBlendMode = renderer().style().blendMode();
1208     if (newBlendMode != static_cast<BlendMode>(m_blendMode))
1209         m_blendMode = static_cast<unsigned>(newBlendMode);
1210 }
1211
1212 void RenderLayer::willRemoveChildWithBlendMode()
1213 {
1214     parent()->dirtyAncestorChainHasBlendingDescendants();
1215 }
1216
1217 void RenderLayer::updateAncestorChainHasBlendingDescendants()
1218 {
1219     for (auto* layer = this; layer; layer = layer->parent()) {
1220         if (!layer->hasNotIsolatedBlendingDescendantsStatusDirty() && layer->hasNotIsolatedBlendingDescendants())
1221             break;
1222         layer->m_hasNotIsolatedBlendingDescendants = true;
1223         layer->m_hasNotIsolatedBlendingDescendantsStatusDirty = false;
1224
1225         layer->updateSelfPaintingLayer();
1226
1227         if (layer->isCSSStackingContext())
1228             break;
1229     }
1230 }
1231
1232 void RenderLayer::dirtyAncestorChainHasBlendingDescendants()
1233 {
1234     for (auto* layer = this; layer; layer = layer->parent()) {
1235         if (layer->hasNotIsolatedBlendingDescendantsStatusDirty())
1236             break;
1237         
1238         layer->m_hasNotIsolatedBlendingDescendantsStatusDirty = true;
1239
1240         if (layer->isCSSStackingContext())
1241             break;
1242     }
1243 }
1244 #endif
1245
1246 void RenderLayer::updateTransform()
1247 {
1248     bool hasTransform = renderer().hasTransform();
1249     bool had3DTransform = has3DTransform();
1250
1251     bool hadTransform = !!m_transform;
1252     if (hasTransform != hadTransform) {
1253         if (hasTransform)
1254             m_transform = makeUnique<TransformationMatrix>();
1255         else
1256             m_transform = nullptr;
1257         
1258         // Layers with transforms act as clip rects roots, so clear the cached clip rects here.
1259         clearClipRectsIncludingDescendants();
1260     }
1261     
1262     if (hasTransform) {
1263         RenderBox* box = renderBox();
1264         ASSERT(box);
1265         m_transform->makeIdentity();
1266         box->style().applyTransform(*m_transform, snapRectToDevicePixels(box->borderBoxRect(), box->document().deviceScaleFactor()), RenderStyle::IncludeTransformOrigin);
1267         makeMatrixRenderable(*m_transform, canRender3DTransforms());
1268     }
1269
1270     if (had3DTransform != has3DTransform()) {
1271         dirty3DTransformedDescendantStatus();
1272         // Having a 3D transform affects whether enclosing perspective and preserve-3d layers composite, so trigger an update.
1273         setNeedsPostLayoutCompositingUpdateOnAncestors();
1274     }
1275 }
1276
1277 TransformationMatrix RenderLayer::currentTransform(RenderStyle::ApplyTransformOrigin applyOrigin) const
1278 {
1279     if (!m_transform)
1280         return TransformationMatrix();
1281     
1282     RenderBox* box = renderBox();
1283
1284     if (RuntimeEnabledFeatures::sharedFeatures().webAnimationsCSSIntegrationEnabled()) {
1285         if (auto* timeline = renderer().documentTimeline()) {
1286             if (timeline->isRunningAcceleratedAnimationOnRenderer(renderer(), CSSPropertyTransform)) {
1287                 TransformationMatrix currTransform;
1288                 FloatRect pixelSnappedBorderRect = snapRectToDevicePixels(box->borderBoxRect(), box->document().deviceScaleFactor());
1289                 std::unique_ptr<RenderStyle> style = timeline->animatedStyleForRenderer(renderer());
1290                 style->applyTransform(currTransform, pixelSnappedBorderRect, applyOrigin);
1291                 makeMatrixRenderable(currTransform, canRender3DTransforms());
1292                 return currTransform;
1293             }
1294         }
1295     } else {
1296         if (renderer().animation().isRunningAcceleratedAnimationOnRenderer(renderer(), CSSPropertyTransform)) {
1297             TransformationMatrix currTransform;
1298             FloatRect pixelSnappedBorderRect = snapRectToDevicePixels(box->borderBoxRect(), box->document().deviceScaleFactor());
1299             std::unique_ptr<RenderStyle> style = renderer().animation().animatedStyleForRenderer(renderer());
1300             style->applyTransform(currTransform, pixelSnappedBorderRect, applyOrigin);
1301             makeMatrixRenderable(currTransform, canRender3DTransforms());
1302             return currTransform;
1303         }
1304     }
1305
1306
1307     // m_transform includes transform-origin, so we need to recompute the transform here.
1308     if (applyOrigin == RenderStyle::ExcludeTransformOrigin) {
1309         TransformationMatrix currTransform;
1310         FloatRect pixelSnappedBorderRect = snapRectToDevicePixels(box->borderBoxRect(), box->document().deviceScaleFactor());
1311         box->style().applyTransform(currTransform, pixelSnappedBorderRect, RenderStyle::ExcludeTransformOrigin);
1312         makeMatrixRenderable(currTransform, canRender3DTransforms());
1313         return currTransform;
1314     }
1315
1316     return *m_transform;
1317 }
1318
1319 TransformationMatrix RenderLayer::renderableTransform(OptionSet<PaintBehavior> paintBehavior) const
1320 {
1321     if (!m_transform)
1322         return TransformationMatrix();
1323     
1324     if (paintBehavior & PaintBehavior::FlattenCompositingLayers) {
1325         TransformationMatrix matrix = *m_transform;
1326         makeMatrixRenderable(matrix, false /* flatten 3d */);
1327         return matrix;
1328     }
1329
1330     return *m_transform;
1331 }
1332
1333 RenderLayer* RenderLayer::enclosingOverflowClipLayer(IncludeSelfOrNot includeSelf) const
1334 {
1335     const RenderLayer* layer = (includeSelf == IncludeSelf) ? this : parent();
1336     while (layer) {
1337         if (layer->renderer().hasOverflowClip())
1338             return const_cast<RenderLayer*>(layer);
1339
1340         layer = layer->parent();
1341     }
1342     return nullptr;
1343 }
1344
1345 // FIXME: This is terrible. Bring back a cached bit for this someday. This crawl is going to slow down all
1346 // painting of content inside paginated layers.
1347 bool RenderLayer::hasCompositedLayerInEnclosingPaginationChain() const
1348 {
1349     // No enclosing layer means no compositing in the chain.
1350     if (!m_enclosingPaginationLayer)
1351         return false;
1352     
1353     // If the enclosing layer is composited, we don't have to check anything in between us and that
1354     // layer.
1355     if (m_enclosingPaginationLayer->isComposited())
1356         return true;
1357
1358     // If we are the enclosing pagination layer, then we can't be composited or we'd have passed the
1359     // previous check.
1360     if (m_enclosingPaginationLayer == this)
1361         return false;
1362
1363     // The enclosing paginated layer is our ancestor and is not composited, so we have to check
1364     // intermediate layers between us and the enclosing pagination layer. Start with our own layer.
1365     if (isComposited())
1366         return true;
1367     
1368     // For normal flow layers, we can recur up the layer tree.
1369     if (isNormalFlowOnly())
1370         return parent()->hasCompositedLayerInEnclosingPaginationChain();
1371     
1372     // Otherwise we have to go up the containing block chain. Find the first enclosing
1373     // containing block layer ancestor, and check that.
1374     for (const auto* containingBlock = renderer().containingBlock(); containingBlock && !is<RenderView>(*containingBlock); containingBlock = containingBlock->containingBlock()) {
1375         if (containingBlock->hasLayer())
1376             return containingBlock->layer()->hasCompositedLayerInEnclosingPaginationChain();
1377     }
1378     return false;
1379 }
1380
1381 void RenderLayer::updatePagination()
1382 {
1383     m_enclosingPaginationLayer = nullptr;
1384     
1385     if (!parent())
1386         return;
1387     
1388     // Each layer that is inside a multicolumn flow thread has to be checked individually and
1389     // genuinely know if it is going to have to split itself up when painting only its contents (and not any other descendant
1390     // layers). We track an enclosingPaginationLayer instead of using a simple bit, since we want to be able to get back
1391     // to that layer easily.
1392     if (renderer().isInFlowRenderFragmentedFlow()) {
1393         m_enclosingPaginationLayer = makeWeakPtr(*this);
1394         return;
1395     }
1396
1397     if (isNormalFlowOnly()) {
1398         // Content inside a transform is not considered to be paginated, since we simply
1399         // paint the transform multiple times in each column, so we don't have to use
1400         // fragments for the transformed content.
1401         if (parent()->hasTransform())
1402             m_enclosingPaginationLayer = nullptr;
1403         else
1404             m_enclosingPaginationLayer = makeWeakPtr(parent()->enclosingPaginationLayer(IncludeCompositedPaginatedLayers));
1405         return;
1406     }
1407
1408     // For the new columns code, we want to walk up our containing block chain looking for an enclosing layer. Once
1409     // we find one, then we just check its pagination status.
1410     for (const auto* containingBlock = renderer().containingBlock(); containingBlock && !is<RenderView>(*containingBlock); containingBlock = containingBlock->containingBlock()) {
1411         if (containingBlock->hasLayer()) {
1412             // Content inside a transform is not considered to be paginated, since we simply
1413             // paint the transform multiple times in each column, so we don't have to use
1414             // fragments for the transformed content.
1415             if (containingBlock->layer()->hasTransform())
1416                 m_enclosingPaginationLayer = nullptr;
1417             else
1418                 m_enclosingPaginationLayer = makeWeakPtr(containingBlock->layer()->enclosingPaginationLayer(IncludeCompositedPaginatedLayers));
1419             return;
1420         }
1421     }
1422 }
1423
1424 void RenderLayer::setHasVisibleContent()
1425
1426     if (m_hasVisibleContent && !m_visibleContentStatusDirty) {
1427         ASSERT(!parent() || parent()->hasVisibleDescendant());
1428         return;
1429     }
1430
1431     m_visibleContentStatusDirty = false; 
1432     m_hasVisibleContent = true;
1433     computeRepaintRects(renderer().containerForRepaint());
1434     if (!isNormalFlowOnly()) {
1435         // We don't collect invisible layers in z-order lists if we are not in compositing mode.
1436         // As we became visible, we need to dirty our stacking containers ancestors to be properly
1437         // collected. FIXME: When compositing, we could skip this dirtying phase.
1438         for (RenderLayer* sc = stackingContext(); sc; sc = sc->stackingContext()) {
1439             sc->dirtyZOrderLists();
1440             if (sc->hasVisibleContent())
1441                 break;
1442         }
1443     }
1444
1445     if (parent())
1446         parent()->setAncestorChainHasVisibleDescendant();
1447 }
1448
1449 void RenderLayer::dirtyVisibleContentStatus() 
1450
1451     m_visibleContentStatusDirty = true; 
1452     if (parent())
1453         parent()->dirtyAncestorChainVisibleDescendantStatus();
1454 }
1455
1456 void RenderLayer::dirtyAncestorChainVisibleDescendantStatus()
1457 {
1458     for (RenderLayer* layer = this; layer; layer = layer->parent()) {
1459         if (layer->m_visibleDescendantStatusDirty)
1460             break;
1461
1462         layer->m_visibleDescendantStatusDirty = true;
1463     }
1464 }
1465
1466 void RenderLayer::setAncestorChainHasVisibleDescendant()
1467 {
1468     for (RenderLayer* layer = this; layer; layer = layer->parent()) {
1469         if (!layer->m_visibleDescendantStatusDirty && layer->hasVisibleDescendant())
1470             break;
1471
1472         layer->m_hasVisibleDescendant = true;
1473         layer->m_visibleDescendantStatusDirty = false;
1474     }
1475 }
1476
1477 void RenderLayer::updateDescendantDependentFlags()
1478 {
1479     if (m_visibleDescendantStatusDirty || m_hasSelfPaintingLayerDescendantDirty || hasNotIsolatedBlendingDescendantsStatusDirty()) {
1480         bool hasVisibleDescendant = false;
1481         bool hasSelfPaintingLayerDescendant = false;
1482 #if ENABLE(CSS_COMPOSITING)
1483         bool hasNotIsolatedBlendingDescendants = false;
1484 #endif
1485
1486         for (RenderLayer* child = firstChild(); child; child = child->nextSibling()) {
1487             child->updateDescendantDependentFlags();
1488
1489             hasVisibleDescendant |= child->m_hasVisibleContent || child->m_hasVisibleDescendant;
1490             hasSelfPaintingLayerDescendant |= child->isSelfPaintingLayer() || child->hasSelfPaintingLayerDescendant();
1491 #if ENABLE(CSS_COMPOSITING)
1492             hasNotIsolatedBlendingDescendants |= child->hasBlendMode() || (child->hasNotIsolatedBlendingDescendants() && !child->isolatesBlending());
1493 #endif
1494
1495             bool allFlagsSet = hasVisibleDescendant && hasSelfPaintingLayerDescendant;
1496 #if ENABLE(CSS_COMPOSITING)
1497             allFlagsSet &= hasNotIsolatedBlendingDescendants;
1498 #endif
1499             if (allFlagsSet)
1500                 break;
1501         }
1502
1503         m_hasVisibleDescendant = hasVisibleDescendant;
1504         m_visibleDescendantStatusDirty = false;
1505         m_hasSelfPaintingLayerDescendant = hasSelfPaintingLayerDescendant;
1506         m_hasSelfPaintingLayerDescendantDirty = false;
1507
1508 #if ENABLE(CSS_COMPOSITING)
1509         m_hasNotIsolatedBlendingDescendants = hasNotIsolatedBlendingDescendants;
1510         if (m_hasNotIsolatedBlendingDescendantsStatusDirty) {
1511             m_hasNotIsolatedBlendingDescendantsStatusDirty = false;
1512             updateSelfPaintingLayer();
1513         }
1514 #endif
1515     }
1516
1517     if (m_visibleContentStatusDirty) {
1518         if (renderer().style().visibility() == Visibility::Visible)
1519             m_hasVisibleContent = true;
1520         else {
1521             // layer may be hidden but still have some visible content, check for this
1522             m_hasVisibleContent = false;
1523             RenderObject* r = renderer().firstChild();
1524             while (r) {
1525                 if (r->style().visibility() == Visibility::Visible && !r->hasLayer()) {
1526                     m_hasVisibleContent = true;
1527                     break;
1528                 }
1529                 RenderObject* child = nullptr;
1530                 if (!r->hasLayer() && (child = r->firstChildSlow()))
1531                     r = child;
1532                 else if (r->nextSibling())
1533                     r = r->nextSibling();
1534                 else {
1535                     do {
1536                         r = r->parent();
1537                         if (r == &renderer())
1538                             r = nullptr;
1539                     } while (r && !r->nextSibling());
1540                     if (r)
1541                         r = r->nextSibling();
1542                 }
1543             }
1544         }    
1545         m_visibleContentStatusDirty = false; 
1546     }
1547 }
1548
1549 void RenderLayer::dirty3DTransformedDescendantStatus()
1550 {
1551     RenderLayer* curr = stackingContext();
1552     if (curr)
1553         curr->m_3DTransformedDescendantStatusDirty = true;
1554         
1555     // This propagates up through preserve-3d hierarchies to the enclosing flattening layer.
1556     // Note that preserves3D() creates stacking context, so we can just run up the stacking containers.
1557     while (curr && curr->preserves3D()) {
1558         curr->m_3DTransformedDescendantStatusDirty = true;
1559         curr = curr->stackingContext();
1560     }
1561 }
1562
1563 // Return true if this layer or any preserve-3d descendants have 3d.
1564 bool RenderLayer::update3DTransformedDescendantStatus()
1565 {
1566     if (m_3DTransformedDescendantStatusDirty) {
1567         m_has3DTransformedDescendant = false;
1568
1569         updateZOrderLists();
1570
1571         // Transformed or preserve-3d descendants can only be in the z-order lists, not
1572         // in the normal flow list, so we only need to check those.
1573         for (auto* layer : positiveZOrderLayers())
1574             m_has3DTransformedDescendant |= layer->update3DTransformedDescendantStatus();
1575
1576         // Now check our negative z-index children.
1577         for (auto* layer : negativeZOrderLayers())
1578             m_has3DTransformedDescendant |= layer->update3DTransformedDescendantStatus();
1579         
1580         m_3DTransformedDescendantStatusDirty = false;
1581     }
1582     
1583     // If we live in a 3d hierarchy, then the layer at the root of that hierarchy needs
1584     // the m_has3DTransformedDescendant set.
1585     if (preserves3D())
1586         return has3DTransform() || m_has3DTransformedDescendant;
1587
1588     return has3DTransform();
1589 }
1590
1591 bool RenderLayer::updateLayerPosition(OptionSet<UpdateLayerPositionsFlag>* flags)
1592 {
1593     LayoutPoint localPoint;
1594     LayoutSize inlineBoundingBoxOffset; // We don't put this into the RenderLayer x/y for inlines, so we need to subtract it out when done.
1595     if (renderer().isInline() && is<RenderInline>(renderer())) {
1596         auto& inlineFlow = downcast<RenderInline>(renderer());
1597         IntRect lineBox = inlineFlow.linesBoundingBox();
1598         setSize(lineBox.size());
1599         inlineBoundingBoxOffset = toLayoutSize(lineBox.location());
1600         localPoint += inlineBoundingBoxOffset;
1601     } else if (RenderBox* box = renderBox()) {
1602         // FIXME: Is snapping the size really needed here for the RenderBox case?
1603         auto newSize = snappedIntRect(box->frameRect()).size();
1604         if (newSize != size()) {
1605             if (is<RenderWidget>(*box) && downcast<RenderWidget>(*box).requiresAcceleratedCompositing()) {
1606                 // Trigger RenderLayerCompositor::requiresCompositingForFrame() which depends on the contentBoxRect size.
1607                 setNeedsPostLayoutCompositingUpdate();
1608             }
1609
1610             if (flags && renderer().hasOverflowClip())
1611                 flags->add(ContainingClippingLayerChangedSize);
1612
1613             setSize(newSize);
1614         }
1615         
1616         box->applyTopLeftLocationOffset(localPoint);
1617     }
1618
1619     if (!renderer().isOutOfFlowPositioned()) {
1620         auto* ancestor = renderer().parent();
1621         // We must adjust our position by walking up the render tree looking for the
1622         // nearest enclosing object with a layer.
1623         while (ancestor && !ancestor->hasLayer()) {
1624             if (is<RenderBox>(*ancestor) && !is<RenderTableRow>(*ancestor)) {
1625                 // Rows and cells share the same coordinate space (that of the section).
1626                 // Omit them when computing our xpos/ypos.
1627                 localPoint += downcast<RenderBox>(*ancestor).topLeftLocationOffset();
1628             }
1629             ancestor = ancestor->parent();
1630         }
1631         if (is<RenderTableRow>(ancestor)) {
1632             // Put ourselves into the row coordinate space.
1633             localPoint -= downcast<RenderTableRow>(*ancestor).topLeftLocationOffset();
1634         }
1635     }
1636     
1637     // Subtract our parent's scroll offset.
1638     RenderLayer* positionedParent;
1639     if (renderer().isOutOfFlowPositioned() && (positionedParent = enclosingAncestorForPosition(renderer().style().position()))) {
1640         // For positioned layers, we subtract out the enclosing positioned layer's scroll offset.
1641         if (positionedParent->renderer().hasOverflowClip())
1642             localPoint -= toLayoutSize(positionedParent->scrollPosition());
1643         
1644         if (renderer().isOutOfFlowPositioned() && positionedParent->renderer().isInFlowPositioned() && is<RenderInline>(positionedParent->renderer())) {
1645             LayoutSize offset = downcast<RenderInline>(positionedParent->renderer()).offsetForInFlowPositionedInline(&downcast<RenderBox>(renderer()));
1646             localPoint += offset;
1647         }
1648     } else if (parent()) {
1649         if (parent()->renderer().hasOverflowClip())
1650             localPoint -= toLayoutSize(parent()->scrollPosition());
1651     }
1652     
1653     bool positionOrOffsetChanged = false;
1654     if (renderer().isInFlowPositioned()) {
1655         LayoutSize newOffset = downcast<RenderBoxModelObject>(renderer()).offsetForInFlowPosition();
1656         positionOrOffsetChanged = newOffset != m_offsetForInFlowPosition;
1657         m_offsetForInFlowPosition = newOffset;
1658         localPoint.move(m_offsetForInFlowPosition);
1659     } else {
1660         m_offsetForInFlowPosition = LayoutSize();
1661     }
1662
1663     // FIXME: We'd really like to just get rid of the concept of a layer rectangle and rely on the renderers.
1664     localPoint -= inlineBoundingBoxOffset;
1665     
1666     positionOrOffsetChanged |= location() != localPoint;
1667     setLocation(localPoint);
1668     
1669     if (positionOrOffsetChanged && compositor().hasContentCompositingLayers()) {
1670         if (isComposited())
1671             setNeedsCompositingGeometryUpdate();
1672         // This layer's position can affect the location of a composited descendant (which may be a sibling in z-order),
1673         // so trigger a descendant walk from the paint-order parent.
1674         if (auto* paintParent = paintOrderParent())
1675             paintParent->setDescendantsNeedUpdateBackingAndHierarchyTraversal();
1676     }
1677
1678     return positionOrOffsetChanged;
1679 }
1680
1681 TransformationMatrix RenderLayer::perspectiveTransform() const
1682 {
1683     RenderBox* box = renderBox();
1684     if (!box)
1685         return TransformationMatrix();
1686     
1687     if (!box->hasTransformRelatedProperty())
1688         return TransformationMatrix();
1689
1690     const RenderStyle& style = box->style();
1691     if (!style.hasPerspective())
1692         return TransformationMatrix();
1693
1694     // Maybe fetch the perspective from the backing?
1695     const FloatRect borderBox = snapRectToDevicePixels(box->borderBoxRect(), box->document().deviceScaleFactor());
1696     float perspectiveOriginX = floatValueForLength(style.perspectiveOriginX(), borderBox.width());
1697     float perspectiveOriginY = floatValueForLength(style.perspectiveOriginY(), borderBox.height());
1698
1699     // A perspective origin of 0,0 makes the vanishing point in the center of the element.
1700     // We want it to be in the top-left, so subtract half the height and width.
1701     perspectiveOriginX -= borderBox.width() / 2.0f;
1702     perspectiveOriginY -= borderBox.height() / 2.0f;
1703     
1704     TransformationMatrix t;
1705     t.translate(perspectiveOriginX, perspectiveOriginY);
1706     t.applyPerspective(style.perspective());
1707     t.translate(-perspectiveOriginX, -perspectiveOriginY);
1708     
1709     return t;
1710 }
1711
1712 FloatPoint RenderLayer::perspectiveOrigin() const
1713 {
1714     if (!renderer().hasTransformRelatedProperty())
1715         return FloatPoint();
1716
1717     const LayoutRect borderBox = downcast<RenderBox>(renderer()).borderBoxRect();
1718     const RenderStyle& style = renderer().style();
1719
1720     return FloatPoint(floatValueForLength(style.perspectiveOriginX(), borderBox.width()),
1721                       floatValueForLength(style.perspectiveOriginY(), borderBox.height()));
1722 }
1723
1724 static inline bool isContainerForPositioned(RenderLayer& layer, PositionType position)
1725 {
1726     switch (position) {
1727     case PositionType::Fixed:
1728         return layer.renderer().canContainFixedPositionObjects();
1729
1730     case PositionType::Absolute:
1731         return layer.renderer().canContainAbsolutelyPositionedObjects();
1732     
1733     default:
1734         ASSERT_NOT_REACHED();
1735         return false;
1736     }
1737 }
1738
1739 bool RenderLayer::ancestorLayerIsInContainingBlockChain(const RenderLayer& ancestor, const RenderLayer* checkLimit) const
1740 {
1741     if (&ancestor == this)
1742         return true;
1743
1744     for (const auto* currentBlock = renderer().containingBlock(); currentBlock && !is<RenderView>(*currentBlock); currentBlock = currentBlock->containingBlock()) {
1745         auto* currLayer = currentBlock->layer();
1746         if (currLayer == &ancestor)
1747             return true;
1748         
1749         if (currLayer && currLayer == checkLimit)
1750             return false;
1751     }
1752     
1753     return false;
1754 }
1755
1756 RenderLayer* RenderLayer::enclosingAncestorForPosition(PositionType position) const
1757 {
1758     RenderLayer* curr = parent();
1759     while (curr && !isContainerForPositioned(*curr, position))
1760         curr = curr->parent();
1761
1762     return curr;
1763 }
1764
1765 static RenderLayer* enclosingFrameRenderLayer(const RenderLayer& layer)
1766 {
1767     auto* ownerElement = layer.renderer().document().ownerElement();
1768     if (!ownerElement)
1769         return nullptr;
1770
1771     auto* ownerRenderer = ownerElement->renderer();
1772     if (!ownerRenderer)
1773         return nullptr;
1774
1775     return ownerRenderer->enclosingLayer();
1776 }
1777
1778 static RenderLayer* parentLayerCrossFrame(const RenderLayer& layer)
1779 {
1780     if (auto* parent = layer.parent())
1781         return parent;
1782
1783     return enclosingFrameRenderLayer(layer);
1784 }
1785
1786 RenderLayer* RenderLayer::enclosingScrollableLayer() const
1787 {
1788     for (RenderLayer* nextLayer = parentLayerCrossFrame(*this); nextLayer; nextLayer = parentLayerCrossFrame(*nextLayer)) {
1789         if (is<RenderBox>(nextLayer->renderer()) && downcast<RenderBox>(nextLayer->renderer()).canBeScrolledAndHasScrollableArea())
1790             return nextLayer;
1791     }
1792
1793     return nullptr;
1794 }
1795
1796 IntRect RenderLayer::scrollableAreaBoundingBox(bool* isInsideFixed) const
1797 {
1798     return renderer().absoluteBoundingBoxRect(/* useTransforms */ true, isInsideFixed);
1799 }
1800
1801 bool RenderLayer::isRubberBandInProgress() const
1802 {
1803 #if ENABLE(RUBBER_BANDING)
1804     if (!scrollsOverflow())
1805         return false;
1806
1807     if (ScrollAnimator* scrollAnimator = existingScrollAnimator())
1808         return scrollAnimator->isRubberBandInProgress();
1809 #endif
1810
1811     return false;
1812 }
1813
1814 bool RenderLayer::forceUpdateScrollbarsOnMainThreadForPerformanceTesting() const
1815 {
1816     return renderer().settings().forceUpdateScrollbarsOnMainThreadForPerformanceTesting();
1817 }
1818
1819 RenderLayer* RenderLayer::enclosingTransformedAncestor() const
1820 {
1821     RenderLayer* curr = parent();
1822     while (curr && !curr->isRenderViewLayer() && !curr->transform())
1823         curr = curr->parent();
1824
1825     return curr;
1826 }
1827
1828 inline bool RenderLayer::shouldRepaintAfterLayout() const
1829 {
1830     if (m_repaintStatus == NeedsNormalRepaint)
1831         return true;
1832
1833     // Composited layers that were moved during a positioned movement only
1834     // layout, don't need to be repainted. They just need to be recomposited.
1835     ASSERT(m_repaintStatus == NeedsFullRepaintForPositionedMovementLayout);
1836     return !isComposited() || backing()->paintsIntoCompositedAncestor();
1837 }
1838
1839 void RenderLayer::setBackingProviderLayer(RenderLayer* backingProvider)
1840 {
1841     if (backingProvider == m_backingProviderLayer)
1842         return;
1843
1844     if (!renderer().renderTreeBeingDestroyed())
1845         clearClipRectsIncludingDescendants();
1846
1847     m_backingProviderLayer = makeWeakPtr(backingProvider);
1848 }
1849
1850 void RenderLayer::disconnectFromBackingProviderLayer()
1851 {
1852     if (!m_backingProviderLayer)
1853         return;
1854     
1855     ASSERT(m_backingProviderLayer->isComposited());
1856     if (m_backingProviderLayer->isComposited())
1857         m_backingProviderLayer->backing()->removeBackingSharingLayer(*this);
1858 }
1859
1860 bool compositedWithOwnBackingStore(const RenderLayer& layer)
1861 {
1862     return layer.isComposited() && !layer.backing()->paintsIntoCompositedAncestor();
1863 }
1864
1865 RenderLayer* RenderLayer::enclosingCompositingLayer(IncludeSelfOrNot includeSelf) const
1866 {
1867     if (includeSelf == IncludeSelf && isComposited())
1868         return const_cast<RenderLayer*>(this);
1869
1870     for (const RenderLayer* curr = paintOrderParent(); curr; curr = curr->paintOrderParent()) {
1871         if (curr->isComposited())
1872             return const_cast<RenderLayer*>(curr);
1873     }
1874          
1875     return nullptr;
1876 }
1877
1878 RenderLayer* RenderLayer::enclosingCompositingLayerForRepaint(IncludeSelfOrNot includeSelf) const
1879 {
1880     auto repaintTargetForLayer = [](const RenderLayer& layer) -> RenderLayer* {
1881         if (compositedWithOwnBackingStore(layer))
1882             return const_cast<RenderLayer*>(&layer);
1883         
1884         if (layer.paintsIntoProvidedBacking())
1885             return layer.backingProviderLayer();
1886         
1887         return nullptr;
1888     };
1889
1890     RenderLayer* repaintTarget = nullptr;
1891     if (includeSelf == IncludeSelf && (repaintTarget = repaintTargetForLayer(*this)))
1892         return repaintTarget;
1893
1894     for (const RenderLayer* curr = paintOrderParent(); curr; curr = curr->paintOrderParent()) {
1895         if ((repaintTarget = repaintTargetForLayer(*curr)))
1896             return repaintTarget;
1897     }
1898          
1899     return nullptr;
1900 }
1901
1902 RenderLayer* RenderLayer::enclosingFilterLayer(IncludeSelfOrNot includeSelf) const
1903 {
1904     const RenderLayer* curr = (includeSelf == IncludeSelf) ? this : parent();
1905     for (; curr; curr = curr->parent()) {
1906         if (curr->requiresFullLayerImageForFilters())
1907             return const_cast<RenderLayer*>(curr);
1908     }
1909     
1910     return nullptr;
1911 }
1912
1913 RenderLayer* RenderLayer::enclosingFilterRepaintLayer() const
1914 {
1915     for (const RenderLayer* curr = this; curr; curr = curr->parent()) {
1916         if ((curr != this && curr->requiresFullLayerImageForFilters()) || compositedWithOwnBackingStore(*curr) || curr->isRenderViewLayer())
1917             return const_cast<RenderLayer*>(curr);
1918     }
1919     return nullptr;
1920 }
1921
1922 // FIXME: This neeeds a better name.
1923 void RenderLayer::setFilterBackendNeedsRepaintingInRect(const LayoutRect& rect)
1924 {
1925     ASSERT(requiresFullLayerImageForFilters());
1926     ASSERT(m_filters);
1927
1928     if (rect.isEmpty())
1929         return;
1930     
1931     LayoutRect rectForRepaint = rect;
1932     renderer().style().filterOutsets().expandRect(rectForRepaint);
1933
1934     m_filters->expandDirtySourceRect(rectForRepaint);
1935     
1936     RenderLayer* parentLayer = enclosingFilterRepaintLayer();
1937     ASSERT(parentLayer);
1938     FloatQuad repaintQuad(rectForRepaint);
1939     LayoutRect parentLayerRect = renderer().localToContainerQuad(repaintQuad, &parentLayer->renderer()).enclosingBoundingBox();
1940
1941     if (parentLayer->isComposited()) {
1942         if (!parentLayer->backing()->paintsIntoWindow()) {
1943             parentLayer->setBackingNeedsRepaintInRect(parentLayerRect);
1944             return;
1945         }
1946         // If the painting goes to window, redirect the painting to the parent RenderView.
1947         parentLayer = renderer().view().layer();
1948         parentLayerRect = renderer().localToContainerQuad(repaintQuad, &parentLayer->renderer()).enclosingBoundingBox();
1949     }
1950
1951     if (parentLayer->paintsWithFilters()) {
1952         parentLayer->setFilterBackendNeedsRepaintingInRect(parentLayerRect);
1953         return;        
1954     }
1955     
1956     if (parentLayer->isRenderViewLayer()) {
1957         downcast<RenderView>(parentLayer->renderer()).repaintViewRectangle(parentLayerRect);
1958         return;
1959     }
1960     
1961     ASSERT_NOT_REACHED();
1962 }
1963
1964 bool RenderLayer::hasAncestorWithFilterOutsets() const
1965 {
1966     for (const RenderLayer* curr = this; curr; curr = curr->parent()) {
1967         if (curr->renderer().style().hasFilterOutsets())
1968             return true;
1969     }
1970     return false;
1971 }
1972
1973 RenderLayer* RenderLayer::clippingRootForPainting() const
1974 {
1975     if (isComposited())
1976         return const_cast<RenderLayer*>(this);
1977
1978     if (paintsIntoProvidedBacking())
1979         return backingProviderLayer();
1980
1981     const RenderLayer* current = this;
1982     while (current) {
1983         if (current->isRenderViewLayer())
1984             return const_cast<RenderLayer*>(current);
1985
1986         current = current->paintOrderParent();
1987         ASSERT(current);
1988         if (current->transform() || compositedWithOwnBackingStore(*current))
1989             return const_cast<RenderLayer*>(current);
1990
1991         if (current->paintsIntoProvidedBacking())
1992             return current->backingProviderLayer();
1993     }
1994
1995     ASSERT_NOT_REACHED();
1996     return nullptr;
1997 }
1998
1999 LayoutPoint RenderLayer::absoluteToContents(const LayoutPoint& absolutePoint) const
2000 {
2001     // We don't use convertToLayerCoords because it doesn't know about transforms
2002     return LayoutPoint(renderer().absoluteToLocal(absolutePoint, UseTransforms));
2003 }
2004
2005 bool RenderLayer::cannotBlitToWindow() const
2006 {
2007     if (isTransparent() || hasReflection() || hasTransform())
2008         return true;
2009     if (!parent())
2010         return false;
2011     return parent()->cannotBlitToWindow();
2012 }
2013
2014 RenderLayer* RenderLayer::transparentPaintingAncestor()
2015 {
2016     if (isComposited())
2017         return nullptr;
2018
2019     for (RenderLayer* curr = parent(); curr; curr = curr->parent()) {
2020         if (curr->isComposited())
2021             return nullptr;
2022         if (curr->isTransparent())
2023             return curr;
2024     }
2025     return nullptr;
2026 }
2027
2028 enum TransparencyClipBoxBehavior {
2029     PaintingTransparencyClipBox,
2030     HitTestingTransparencyClipBox
2031 };
2032
2033 enum TransparencyClipBoxMode {
2034     DescendantsOfTransparencyClipBox,
2035     RootOfTransparencyClipBox
2036 };
2037
2038 static LayoutRect transparencyClipBox(const RenderLayer&, const RenderLayer* rootLayer, TransparencyClipBoxBehavior, TransparencyClipBoxMode, OptionSet<PaintBehavior> = { });
2039
2040 static void expandClipRectForDescendantsAndReflection(LayoutRect& clipRect, const RenderLayer& layer, const RenderLayer* rootLayer,
2041     TransparencyClipBoxBehavior transparencyBehavior, OptionSet<PaintBehavior> paintBehavior)
2042 {
2043     // If we have a mask, then the clip is limited to the border box area (and there is
2044     // no need to examine child layers).
2045     if (!layer.renderer().hasMask()) {
2046         // Note: we don't have to walk z-order lists since transparent elements always establish
2047         // a stacking container. This means we can just walk the layer tree directly.
2048         for (RenderLayer* curr = layer.firstChild(); curr; curr = curr->nextSibling()) {
2049             if (!layer.isReflectionLayer(*curr))
2050                 clipRect.unite(transparencyClipBox(*curr, rootLayer, transparencyBehavior, DescendantsOfTransparencyClipBox, paintBehavior));
2051         }
2052     }
2053
2054     // If we have a reflection, then we need to account for that when we push the clip.  Reflect our entire
2055     // current transparencyClipBox to catch all child layers.
2056     // FIXME: Accelerated compositing will eventually want to do something smart here to avoid incorporating this
2057     // size into the parent layer.
2058     if (layer.renderer().hasReflection()) {
2059         LayoutSize delta = layer.offsetFromAncestor(rootLayer);
2060         clipRect.move(-delta);
2061         clipRect.unite(layer.renderBox()->reflectedRect(clipRect));
2062         clipRect.move(delta);
2063     }
2064 }
2065
2066 static LayoutRect transparencyClipBox(const RenderLayer& layer, const RenderLayer* rootLayer, TransparencyClipBoxBehavior transparencyBehavior,
2067     TransparencyClipBoxMode transparencyMode, OptionSet<PaintBehavior> paintBehavior)
2068 {
2069     // FIXME: Although this function completely ignores CSS-imposed clipping, we did already intersect with the
2070     // paintDirtyRect, and that should cut down on the amount we have to paint.  Still it
2071     // would be better to respect clips.
2072     
2073     if (rootLayer != &layer && ((transparencyBehavior == PaintingTransparencyClipBox && layer.paintsWithTransform(paintBehavior))
2074         || (transparencyBehavior == HitTestingTransparencyClipBox && layer.hasTransform()))) {
2075         // The best we can do here is to use enclosed bounding boxes to establish a "fuzzy" enough clip to encompass
2076         // the transformed layer and all of its children.
2077         RenderLayer::PaginationInclusionMode mode = transparencyBehavior == HitTestingTransparencyClipBox ? RenderLayer::IncludeCompositedPaginatedLayers : RenderLayer::ExcludeCompositedPaginatedLayers;
2078         const RenderLayer* paginationLayer = transparencyMode == DescendantsOfTransparencyClipBox ? layer.enclosingPaginationLayer(mode) : nullptr;
2079         const RenderLayer* rootLayerForTransform = paginationLayer ? paginationLayer : rootLayer;
2080         LayoutSize delta = layer.offsetFromAncestor(rootLayerForTransform);
2081
2082         TransformationMatrix transform;
2083         transform.translate(delta.width(), delta.height());
2084         transform.multiply(*layer.transform());
2085
2086         // We don't use fragment boxes when collecting a transformed layer's bounding box, since it always
2087         // paints unfragmented.
2088         LayoutRect clipRect = layer.boundingBox(&layer);
2089         expandClipRectForDescendantsAndReflection(clipRect, layer, &layer, transparencyBehavior, paintBehavior);
2090         layer.renderer().style().filterOutsets().expandRect(clipRect);
2091         LayoutRect result = transform.mapRect(clipRect);
2092         if (!paginationLayer)
2093             return result;
2094         
2095         // We have to break up the transformed extent across our columns.
2096         // Split our box up into the actual fragment boxes that render in the columns/pages and unite those together to
2097         // get our true bounding box.
2098         auto& enclosingFragmentedFlow = downcast<RenderFragmentedFlow>(paginationLayer->renderer());
2099         result = enclosingFragmentedFlow.fragmentsBoundingBox(result);
2100         result.move(paginationLayer->offsetFromAncestor(rootLayer));
2101         return result;
2102     }
2103     
2104     LayoutRect clipRect = layer.boundingBox(rootLayer, layer.offsetFromAncestor(rootLayer), transparencyBehavior == HitTestingTransparencyClipBox ? RenderLayer::UseFragmentBoxesIncludingCompositing : RenderLayer::UseFragmentBoxesExcludingCompositing);
2105     expandClipRectForDescendantsAndReflection(clipRect, layer, rootLayer, transparencyBehavior, paintBehavior);
2106     layer.renderer().style().filterOutsets().expandRect(clipRect);
2107
2108     return clipRect;
2109 }
2110
2111 static LayoutRect paintingExtent(const RenderLayer& currentLayer, const RenderLayer* rootLayer, const LayoutRect& paintDirtyRect, OptionSet<PaintBehavior> paintBehavior)
2112 {
2113     return intersection(transparencyClipBox(currentLayer, rootLayer, PaintingTransparencyClipBox, RootOfTransparencyClipBox, paintBehavior), paintDirtyRect);
2114 }
2115
2116 void RenderLayer::beginTransparencyLayers(GraphicsContext& context, const LayerPaintingInfo& paintingInfo, const LayoutRect& dirtyRect)
2117 {
2118     if (context.paintingDisabled() || (paintsWithTransparency(paintingInfo.paintBehavior) && m_usedTransparency))
2119         return;
2120
2121     RenderLayer* ancestor = transparentPaintingAncestor();
2122     if (ancestor)
2123         ancestor->beginTransparencyLayers(context, paintingInfo, dirtyRect);
2124     
2125     if (paintsWithTransparency(paintingInfo.paintBehavior)) {
2126         ASSERT(isCSSStackingContext());
2127         m_usedTransparency = true;
2128         context.save();
2129         LayoutRect adjustedClipRect = paintingExtent(*this, paintingInfo.rootLayer, dirtyRect, paintingInfo.paintBehavior);
2130         adjustedClipRect.move(paintingInfo.subpixelOffset);
2131         FloatRect pixelSnappedClipRect = snapRectToDevicePixels(adjustedClipRect, renderer().document().deviceScaleFactor());
2132         context.clip(pixelSnappedClipRect);
2133
2134 #if ENABLE(CSS_COMPOSITING)
2135         bool usesCompositeOperation = hasBlendMode() && !(renderer().isSVGRoot() && parent() && parent()->isRenderViewLayer());
2136         if (usesCompositeOperation)
2137             context.setCompositeOperation(context.compositeOperation(), blendMode());
2138 #endif
2139
2140         context.beginTransparencyLayer(renderer().opacity());
2141
2142 #if ENABLE(CSS_COMPOSITING)
2143         if (usesCompositeOperation)
2144             context.setCompositeOperation(context.compositeOperation(), BlendMode::Normal);
2145 #endif
2146
2147 #ifdef REVEAL_TRANSPARENCY_LAYERS
2148         context.setFillColor(Color(0.0f, 0.0f, 0.5f, 0.2f));
2149         context.fillRect(pixelSnappedClipRect);
2150 #endif
2151     }
2152 }
2153
2154 #if PLATFORM(IOS_FAMILY)
2155 void RenderLayer::willBeDestroyed()
2156 {
2157     if (RenderLayerBacking* layerBacking = backing())
2158         layerBacking->layerWillBeDestroyed();
2159 }
2160 #endif
2161
2162 bool RenderLayer::isDescendantOf(const RenderLayer& layer) const
2163 {
2164     for (auto* ancestor = this; ancestor; ancestor = ancestor->parent()) {
2165         if (&layer == ancestor)
2166             return true;
2167     }
2168     return false;
2169 }
2170
2171 void RenderLayer::convertToPixelSnappedLayerCoords(const RenderLayer* ancestorLayer, IntPoint& roundedLocation, ColumnOffsetAdjustment adjustForColumns) const
2172 {
2173     LayoutPoint location = convertToLayerCoords(ancestorLayer, roundedLocation, adjustForColumns);
2174     roundedLocation = roundedIntPoint(location);
2175 }
2176
2177 // Returns the layer reached on the walk up towards the ancestor.
2178 static inline const RenderLayer* accumulateOffsetTowardsAncestor(const RenderLayer* layer, const RenderLayer* ancestorLayer, LayoutPoint& location, RenderLayer::ColumnOffsetAdjustment adjustForColumns)
2179 {
2180     ASSERT(ancestorLayer != layer);
2181
2182     const RenderLayerModelObject& renderer = layer->renderer();
2183     auto position = renderer.style().position();
2184
2185     // FIXME: Special casing RenderFragmentedFlow so much for fixed positioning here is not great.
2186     RenderFragmentedFlow* fixedFragmentedFlowContainer = position == PositionType::Fixed ? renderer.enclosingFragmentedFlow() : nullptr;
2187     if (fixedFragmentedFlowContainer && !fixedFragmentedFlowContainer->isOutOfFlowPositioned())
2188         fixedFragmentedFlowContainer = nullptr;
2189
2190     // FIXME: Positioning of out-of-flow(fixed, absolute) elements collected in a RenderFragmentedFlow
2191     // may need to be revisited in a future patch.
2192     // If the fixed renderer is inside a RenderFragmentedFlow, we should not compute location using localToAbsolute,
2193     // since localToAbsolute maps the coordinates from named flow to regions coordinates and regions can be
2194     // positioned in a completely different place in the viewport (RenderView).
2195     if (position == PositionType::Fixed && !fixedFragmentedFlowContainer && (!ancestorLayer || ancestorLayer == renderer.view().layer())) {
2196         // If the fixed layer's container is the root, just add in the offset of the view. We can obtain this by calling
2197         // localToAbsolute() on the RenderView.
2198         FloatPoint absPos = renderer.localToAbsolute(FloatPoint(), IsFixed);
2199         location += LayoutSize(absPos.x(), absPos.y());
2200         return ancestorLayer;
2201     }
2202
2203     // For the fixed positioned elements inside a render flow thread, we should also skip the code path below
2204     // Otherwise, for the case of ancestorLayer == rootLayer and fixed positioned element child of a transformed
2205     // element in render flow thread, we will hit the fixed positioned container before hitting the ancestor layer.
2206     if (position == PositionType::Fixed && !fixedFragmentedFlowContainer) {
2207         // For a fixed layers, we need to walk up to the root to see if there's a fixed position container
2208         // (e.g. a transformed layer). It's an error to call offsetFromAncestor() across a layer with a transform,
2209         // so we should always find the ancestor at or before we find the fixed position container.
2210         RenderLayer* fixedPositionContainerLayer = nullptr;
2211         bool foundAncestor = false;
2212         for (RenderLayer* currLayer = layer->parent(); currLayer; currLayer = currLayer->parent()) {
2213             if (currLayer == ancestorLayer)
2214                 foundAncestor = true;
2215
2216             if (isContainerForPositioned(*currLayer, PositionType::Fixed)) {
2217                 fixedPositionContainerLayer = currLayer;
2218                 ASSERT_UNUSED(foundAncestor, foundAncestor);
2219                 break;
2220             }
2221         }
2222         
2223         ASSERT(fixedPositionContainerLayer); // We should have hit the RenderView's layer at least.
2224
2225         if (fixedPositionContainerLayer != ancestorLayer) {
2226             LayoutSize fixedContainerCoords = layer->offsetFromAncestor(fixedPositionContainerLayer);
2227             LayoutSize ancestorCoords = ancestorLayer->offsetFromAncestor(fixedPositionContainerLayer);
2228             location += (fixedContainerCoords - ancestorCoords);
2229             return ancestorLayer;
2230         }
2231     }
2232
2233     if (position == PositionType::Fixed && fixedFragmentedFlowContainer) {
2234         ASSERT(ancestorLayer);
2235         if (ancestorLayer->isOutOfFlowRenderFragmentedFlow()) {
2236             location += toLayoutSize(layer->location());
2237             return ancestorLayer;
2238         }
2239
2240         if (ancestorLayer == renderer.view().layer()) {
2241             // Add location in flow thread coordinates.
2242             location += toLayoutSize(layer->location());
2243
2244             // Add flow thread offset in view coordinates since the view may be scrolled.
2245             FloatPoint absPos = renderer.view().localToAbsolute(FloatPoint(), IsFixed);
2246             location += LayoutSize(absPos.x(), absPos.y());
2247             return ancestorLayer;
2248         }
2249     }
2250
2251     RenderLayer* parentLayer;
2252     if (position == PositionType::Absolute || position == PositionType::Fixed) {
2253         // Do what enclosingAncestorForPosition() does, but check for ancestorLayer along the way.
2254         parentLayer = layer->parent();
2255         bool foundAncestorFirst = false;
2256         while (parentLayer) {
2257             // RenderFragmentedFlow is a positioned container, child of RenderView, positioned at (0,0).
2258             // This implies that, for out-of-flow positioned elements inside a RenderFragmentedFlow,
2259             // we are bailing out before reaching root layer.
2260             if (isContainerForPositioned(*parentLayer, position))
2261                 break;
2262
2263             if (parentLayer == ancestorLayer) {
2264                 foundAncestorFirst = true;
2265                 break;
2266             }
2267
2268             parentLayer = parentLayer->parent();
2269         }
2270
2271         // We should not reach RenderView layer past the RenderFragmentedFlow layer for any
2272         // children of the RenderFragmentedFlow.
2273         if (renderer.enclosingFragmentedFlow() && !layer->isOutOfFlowRenderFragmentedFlow())
2274             ASSERT(parentLayer != renderer.view().layer());
2275
2276         if (foundAncestorFirst) {
2277             // Found ancestorLayer before the abs. positioned container, so compute offset of both relative
2278             // to enclosingAncestorForPosition and subtract.
2279             RenderLayer* positionedAncestor = parentLayer->enclosingAncestorForPosition(position);
2280             LayoutSize thisCoords = layer->offsetFromAncestor(positionedAncestor);
2281             LayoutSize ancestorCoords = ancestorLayer->offsetFromAncestor(positionedAncestor);
2282             location += (thisCoords - ancestorCoords);
2283             return ancestorLayer;
2284         }
2285     } else
2286         parentLayer = layer->parent();
2287     
2288     if (!parentLayer)
2289         return nullptr;
2290
2291     location += toLayoutSize(layer->location());
2292
2293     if (adjustForColumns == RenderLayer::AdjustForColumns) {
2294         if (RenderLayer* parentLayer = layer->parent()) {
2295             if (is<RenderMultiColumnFlow>(parentLayer->renderer())) {
2296                 RenderFragmentContainer* fragment = downcast<RenderMultiColumnFlow>(parentLayer->renderer()).physicalTranslationFromFlowToFragment(location);
2297                 if (fragment)
2298                     location.moveBy(fragment->topLeftLocation() + -parentLayer->renderBox()->topLeftLocation());
2299             }
2300         }
2301     }
2302
2303     return parentLayer;
2304 }
2305
2306 LayoutPoint RenderLayer::convertToLayerCoords(const RenderLayer* ancestorLayer, const LayoutPoint& location, ColumnOffsetAdjustment adjustForColumns) const
2307 {
2308     if (ancestorLayer == this)
2309         return location;
2310
2311     const RenderLayer* currLayer = this;
2312     LayoutPoint locationInLayerCoords = location;
2313     while (currLayer && currLayer != ancestorLayer)
2314         currLayer = accumulateOffsetTowardsAncestor(currLayer, ancestorLayer, locationInLayerCoords, adjustForColumns);
2315     return locationInLayerCoords;
2316 }
2317
2318 LayoutSize RenderLayer::offsetFromAncestor(const RenderLayer* ancestorLayer, ColumnOffsetAdjustment adjustForColumns) const
2319 {
2320     return toLayoutSize(convertToLayerCoords(ancestorLayer, LayoutPoint(), adjustForColumns));
2321 }
2322
2323 bool RenderLayer::canUseCompositedScrolling() const
2324 {
2325     bool isVisible = renderer().style().visibility() == Visibility::Visible;
2326     if (renderer().settings().asyncOverflowScrollingEnabled())
2327         return isVisible && scrollsOverflow();
2328
2329 #if PLATFORM(IOS_FAMILY) && ENABLE(OVERFLOW_SCROLLING_TOUCH)
2330     return isVisible && scrollsOverflow() && (renderer().style().useTouchOverflowScrolling() || renderer().settings().alwaysUseAcceleratedOverflowScroll());
2331 #else
2332     return false;
2333 #endif
2334 }
2335
2336 #if ENABLE(IOS_TOUCH_EVENTS)
2337 bool RenderLayer::handleTouchEvent(const PlatformTouchEvent& touchEvent)
2338 {
2339     // If we have accelerated scrolling, let the scrolling be handled outside of WebKit.
2340     if (hasCompositedScrollableOverflow())
2341         return false;
2342
2343     return ScrollableArea::handleTouchEvent(touchEvent);
2344 }
2345
2346 void RenderLayer::registerAsTouchEventListenerForScrolling()
2347 {
2348     if (!renderer().element() || m_registeredAsTouchEventListenerForScrolling)
2349         return;
2350     
2351     renderer().document().addTouchEventHandler(*renderer().element());
2352     m_registeredAsTouchEventListenerForScrolling = true;
2353 }
2354
2355 void RenderLayer::unregisterAsTouchEventListenerForScrolling()
2356 {
2357     if (!renderer().element() || !m_registeredAsTouchEventListenerForScrolling)
2358         return;
2359
2360     renderer().document().removeTouchEventHandler(*renderer().element());
2361     m_registeredAsTouchEventListenerForScrolling = false;
2362 }
2363 #endif // ENABLE(IOS_TOUCH_EVENTS)
2364
2365 // FIXME: this is only valid after we've made layers.
2366 bool RenderLayer::usesCompositedScrolling() const
2367 {
2368     return isComposited() && backing()->hasScrollingLayer();
2369 }
2370
2371 // FIXME: this is only valid after we've made layers.
2372 bool RenderLayer::usesAsyncScrolling() const
2373 {
2374     return compositor().useCoordinatedScrollingForLayer(*this);
2375 }
2376
2377 static inline int adjustedScrollDelta(int beginningDelta)
2378 {
2379     // This implemention matches Firefox's.
2380     // http://mxr.mozilla.org/firefox/source/toolkit/content/widgets/browser.xml#856.
2381     const int speedReducer = 12;
2382
2383     int adjustedDelta = beginningDelta / speedReducer;
2384     if (adjustedDelta > 1)
2385         adjustedDelta = static_cast<int>(adjustedDelta * sqrt(static_cast<double>(adjustedDelta))) - 1;
2386     else if (adjustedDelta < -1)
2387         adjustedDelta = static_cast<int>(adjustedDelta * sqrt(static_cast<double>(-adjustedDelta))) + 1;
2388
2389     return adjustedDelta;
2390 }
2391
2392 static inline IntSize adjustedScrollDelta(const IntSize& delta)
2393 {
2394     return IntSize(adjustedScrollDelta(delta.width()), adjustedScrollDelta(delta.height()));
2395 }
2396
2397 void RenderLayer::panScrollFromPoint(const IntPoint& sourcePoint)
2398 {
2399     IntPoint lastKnownMousePosition = renderer().frame().eventHandler().lastKnownMousePosition();
2400     
2401     // 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
2402     static IntPoint previousMousePosition;
2403     if (lastKnownMousePosition.x() < 0 || lastKnownMousePosition.y() < 0)
2404         lastKnownMousePosition = previousMousePosition;
2405     else
2406         previousMousePosition = lastKnownMousePosition;
2407
2408     IntSize delta = lastKnownMousePosition - sourcePoint;
2409
2410     if (abs(delta.width()) <= ScrollView::noPanScrollRadius) // at the center we let the space for the icon
2411         delta.setWidth(0);
2412     if (abs(delta.height()) <= ScrollView::noPanScrollRadius)
2413         delta.setHeight(0);
2414
2415     scrollByRecursively(adjustedScrollDelta(delta));
2416 }
2417
2418 // FIXME: unify with the scrollRectToVisible() code below.
2419 void RenderLayer::scrollByRecursively(const IntSize& delta, ScrollableArea** scrolledArea)
2420 {
2421     if (delta.isZero())
2422         return;
2423
2424     bool restrictedByLineClamp = false;
2425     if (renderer().parent())
2426         restrictedByLineClamp = !renderer().parent()->style().lineClamp().isNone();
2427
2428     if (renderer().hasOverflowClip() && !restrictedByLineClamp) {
2429         ScrollOffset newScrollOffset = scrollOffset() + delta;
2430         scrollToOffset(newScrollOffset);
2431         if (scrolledArea)
2432             *scrolledArea = this;
2433
2434         // If this layer can't do the scroll we ask the next layer up that can scroll to try
2435         IntSize remainingScrollOffset = newScrollOffset - scrollOffset();
2436         if (!remainingScrollOffset.isZero() && renderer().parent()) {
2437             if (RenderLayer* scrollableLayer = enclosingScrollableLayer())
2438                 scrollableLayer->scrollByRecursively(remainingScrollOffset, scrolledArea);
2439
2440             renderer().frame().eventHandler().updateAutoscrollRenderer();
2441         }
2442     } else {
2443         // If we are here, we were called on a renderer that can be programmatically scrolled, but doesn't
2444         // have an overflow clip. Which means that it is a document node that can be scrolled.
2445         renderer().view().frameView().scrollBy(delta);
2446         if (scrolledArea)
2447             *scrolledArea = &renderer().view().frameView();
2448
2449         // FIXME: If we didn't scroll the whole way, do we want to try looking at the frames ownerElement? 
2450         // https://bugs.webkit.org/show_bug.cgi?id=28237
2451     }
2452 }
2453
2454 void RenderLayer::setPostLayoutScrollPosition(Optional<ScrollPosition> position)
2455 {
2456     m_postLayoutScrollPosition = position;
2457 }
2458
2459 void RenderLayer::applyPostLayoutScrollPositionIfNeeded()
2460 {
2461     if (!m_postLayoutScrollPosition)
2462         return;
2463
2464     scrollToOffset(scrollOffsetFromPosition(m_postLayoutScrollPosition.value()));
2465     m_postLayoutScrollPosition = WTF::nullopt;
2466 }
2467
2468 void RenderLayer::scrollToXPosition(int x, ScrollType scrollType, ScrollClamping clamping)
2469 {
2470     ScrollPosition position(x, m_scrollPosition.y());
2471     scrollToOffset(scrollOffsetFromPosition(position), scrollType, clamping);
2472 }
2473
2474 void RenderLayer::scrollToYPosition(int y, ScrollType scrollType, ScrollClamping clamping)
2475 {
2476     ScrollPosition position(m_scrollPosition.x(), y);
2477     scrollToOffset(scrollOffsetFromPosition(position), scrollType, clamping);
2478 }
2479
2480 ScrollOffset RenderLayer::clampScrollOffset(const ScrollOffset& scrollOffset) const
2481 {
2482     return scrollOffset.constrainedBetween(IntPoint(), maximumScrollOffset());
2483 }
2484
2485 void RenderLayer::scrollToOffset(const ScrollOffset& scrollOffset, ScrollType scrollType, ScrollClamping clamping)
2486 {
2487     ScrollOffset clampedScrollOffset = clamping == ScrollClamping::Clamped ? clampScrollOffset(scrollOffset) : scrollOffset;
2488     if (clampedScrollOffset == this->scrollOffset())
2489         return;
2490
2491     auto previousScrollType = currentScrollType();
2492     setCurrentScrollType(scrollType);
2493
2494     bool handled = false;
2495 #if ENABLE(ASYNC_SCROLLING)
2496     if (ScrollingCoordinator* scrollingCoordinator = page().scrollingCoordinator())
2497         handled = scrollingCoordinator->requestScrollPositionUpdate(*this, scrollPositionFromOffset(clampedScrollOffset));
2498 #endif
2499
2500     if (!handled)
2501         scrollToOffsetWithoutAnimation(clampedScrollOffset, clamping);
2502
2503     setCurrentScrollType(previousScrollType);
2504 }
2505
2506 void RenderLayer::scrollTo(const ScrollPosition& position)
2507 {
2508     RenderBox* box = renderBox();
2509     if (!box)
2510         return;
2511
2512     LOG_WITH_STREAM(Scrolling, stream << "RenderLayer::scrollTo " << position << " from " << m_scrollPosition << " (is user scroll " << (currentScrollType() == ScrollType::User) << ")");
2513
2514     ScrollPosition newPosition = position;
2515     if (!box->isHTMLMarquee()) {
2516         // Ensure that the dimensions will be computed if they need to be (for overflow:hidden blocks).
2517         if (m_scrollDimensionsDirty)
2518             computeScrollDimensions();
2519 #if PLATFORM(IOS_FAMILY)
2520         if (adjustForIOSCaretWhenScrolling()) {
2521             // FIXME: It's not clear what this code is trying to do. Behavior seems reasonable with it removed.
2522             int maxOffset = scrollWidth() - roundToInt(box->clientWidth());
2523             ScrollOffset newOffset = scrollOffsetFromPosition(newPosition);
2524             int scrollXOffset = newOffset.x();
2525             if (scrollXOffset > maxOffset - caretWidth) {
2526                 scrollXOffset += caretWidth;
2527                 if (scrollXOffset <= caretWidth)
2528                     scrollXOffset = 0;
2529             } else if (scrollXOffset < m_scrollPosition.x() - caretWidth)
2530                 scrollXOffset -= caretWidth;
2531
2532             newOffset.setX(scrollXOffset);
2533             newPosition = scrollPositionFromOffset(newOffset);
2534         }
2535 #endif
2536     }
2537     
2538     if (m_scrollPosition == newPosition) {
2539         // FIXME: Nothing guarantees we get a scrollTo() with an unchanged position at the end of a user gesture.
2540         // The ScrollingCoordinator probably needs to message the main thread when a gesture ends.
2541         if (requiresScrollPositionReconciliation()) {
2542             setNeedsCompositingGeometryUpdate();
2543             updateCompositingLayersAfterScroll();
2544         }
2545         return;
2546     }
2547
2548     m_scrollPosition = newPosition;
2549
2550     RenderView& view = renderer().view();
2551
2552     // Update the positions of our child layers (if needed as only fixed layers should be impacted by a scroll).
2553     // We don't update compositing layers, because we need to do a deep update from the compositing ancestor.
2554     if (!view.frameView().layoutContext().isInRenderTreeLayout()) {
2555         // If we're in the middle of layout, we'll just update layers once layout has finished.
2556         updateLayerPositionsAfterOverflowScroll();
2557
2558         view.frameView().scheduleUpdateWidgetPositions();
2559
2560         if (!m_updatingMarqueePosition) {
2561             // Avoid updating compositing layers if, higher on the stack, we're already updating layer
2562             // positions. Updating layer positions requires a full walk of up-to-date RenderLayers, and
2563             // in this case we're still updating their positions; we'll update compositing layers later
2564             // when that completes.
2565             if (usesCompositedScrolling()) {
2566                 setNeedsCompositingGeometryUpdate();
2567
2568                 // Scroll position can affect the location of a composited descendant (which may be a sibling in z-order),
2569                 // so trigger a descendant walk from the paint-order parent.
2570                 if (auto* paintParent = paintOrderParent())
2571                     paintParent->setDescendantsNeedUpdateBackingAndHierarchyTraversal();
2572             }
2573
2574             updateCompositingLayersAfterScroll();
2575         }
2576
2577         // Update regions, scrolling may change the clip of a particular region.
2578         renderer().document().invalidateRenderingDependentRegions();
2579         DebugPageOverlays::didLayout(renderer().frame());
2580     }
2581
2582     Frame& frame = renderer().frame();
2583     RenderLayerModelObject* repaintContainer = renderer().containerForRepaint();
2584     // The caret rect needs to be invalidated after scrolling
2585     frame.selection().setCaretRectNeedsUpdate();
2586     
2587     LayoutRect rectForRepaint = renderer().hasRepaintLayoutRects() ? renderer().repaintLayoutRects().m_repaintRect : renderer().clippedOverflowRectForRepaint(repaintContainer);
2588
2589     FloatQuad quadForFakeMouseMoveEvent = FloatQuad(rectForRepaint);
2590     if (repaintContainer)
2591         quadForFakeMouseMoveEvent = repaintContainer->localToAbsoluteQuad(quadForFakeMouseMoveEvent);
2592     frame.eventHandler().dispatchFakeMouseMoveEventSoonInQuad(quadForFakeMouseMoveEvent);
2593
2594     bool requiresRepaint = true;
2595     if (usesCompositedScrolling()) {
2596         setNeedsCompositingGeometryUpdate();
2597         setDescendantsNeedUpdateBackingAndHierarchyTraversal();
2598         requiresRepaint = false;
2599     }
2600
2601     // Just schedule a full repaint of our object.
2602     if (requiresRepaint)
2603         renderer().repaintUsingContainer(repaintContainer, rectForRepaint);
2604
2605     // Schedule the scroll and scroll-related DOM events.
2606     if (Element* element = renderer().element())
2607         element->document().eventQueue().enqueueOrDispatchScrollEvent(*element);
2608
2609     if (scrollsOverflow())
2610         view.frameView().didChangeScrollOffset();
2611
2612     view.frameView().viewportContentsChanged();
2613     frame.editor().renderLayerDidScroll(*this);
2614 }
2615
2616 static inline bool frameElementAndViewPermitScroll(HTMLFrameElementBase* frameElementBase, FrameView& frameView)
2617 {
2618     // If scrollbars aren't explicitly forbidden, permit scrolling.
2619     if (frameElementBase && frameElementBase->scrollingMode() != ScrollbarAlwaysOff)
2620         return true;
2621
2622     // If scrollbars are forbidden, user initiated scrolls should obviously be ignored.
2623     if (frameView.wasScrolledByUser())
2624         return false;
2625
2626     // Forbid autoscrolls when scrollbars are off, but permits other programmatic scrolls,
2627     // like navigation to an anchor.
2628     return !frameView.frame().eventHandler().autoscrollInProgress();
2629 }
2630
2631 bool RenderLayer::allowsCurrentScroll() const
2632 {
2633     if (!renderer().hasOverflowClip())
2634         return false;
2635
2636     // Don't scroll to reveal an overflow layer that is restricted by the -webkit-line-clamp property.
2637     // FIXME: Is this still needed? It used to be relevant for Safari RSS.
2638     if (renderer().parent() && !renderer().parent()->style().lineClamp().isNone())
2639         return false;
2640
2641     RenderBox* box = renderBox();
2642     ASSERT(box); // Only boxes can have overflowClip set.
2643
2644     if (renderer().frame().eventHandler().autoscrollInProgress()) {
2645         // The "programmatically" here is misleading; this asks whether the box has scrollable overflow,
2646         // or is a special case like a form control.
2647         return box->canBeProgramaticallyScrolled();
2648     }
2649
2650     // Programmatic scrolls can scroll overflow:hidden.
2651     return box->hasHorizontalOverflow() || box->hasVerticalOverflow();
2652 }
2653
2654 void RenderLayer::scrollRectToVisible(const LayoutRect& absoluteRect, bool insideFixed, const ScrollRectToVisibleOptions& options)
2655 {
2656     LOG_WITH_STREAM(Scrolling, stream << "Layer " << this << " scrollRectToVisible " << absoluteRect);
2657
2658     RenderLayer* parentLayer = nullptr;
2659     LayoutRect newRect = absoluteRect;
2660
2661     // We may end up propagating a scroll event. It is important that we suspend events until 
2662     // the end of the function since they could delete the layer or the layer's renderer().
2663     FrameView& frameView = renderer().view().frameView();
2664
2665     if (renderer().parent())
2666         parentLayer = renderer().parent()->enclosingLayer();
2667
2668     if (allowsCurrentScroll()) {
2669         // Don't scroll to reveal an overflow layer that is restricted by the -webkit-line-clamp property.
2670         // This will prevent us from revealing text hidden by the slider in Safari RSS.
2671         RenderBox* box = renderBox();
2672         ASSERT(box);
2673         LayoutRect localExposeRect(box->absoluteToLocalQuad(FloatQuad(FloatRect(absoluteRect))).boundingBox());
2674         if (shouldPlaceBlockDirectionScrollbarOnLeft()) {
2675             // For direction: rtl; writing-mode: horizontal-tb box, the scroll bar is on the left side. The visible rect
2676             // starts from the right side of scroll bar. So the x of localExposeRect should start from the same position too.
2677             localExposeRect.moveBy(LayoutPoint(-verticalScrollbarWidth(), 0));
2678         }
2679         LayoutRect layerBounds(0_lu, 0_lu, box->clientWidth(), box->clientHeight());
2680         LayoutRect revealRect = getRectToExpose(layerBounds, localExposeRect, insideFixed, options.alignX, options.alignY);
2681
2682         ScrollOffset clampedScrollOffset = clampScrollOffset(scrollOffset() + toIntSize(roundedIntRect(revealRect).location()));
2683         if (clampedScrollOffset != scrollOffset()) {
2684             ScrollOffset oldScrollOffset = scrollOffset();
2685             scrollToOffset(clampedScrollOffset);
2686             IntSize scrollOffsetDifference = scrollOffset() - oldScrollOffset;
2687             localExposeRect.move(-scrollOffsetDifference);
2688             newRect = LayoutRect(box->localToAbsoluteQuad(FloatQuad(FloatRect(localExposeRect)), UseTransforms).boundingBox());
2689         }
2690     } else if (!parentLayer && renderer().isRenderView()) {
2691         HTMLFrameOwnerElement* ownerElement = renderer().document().ownerElement();
2692
2693         if (ownerElement && ownerElement->renderer()) {
2694             HTMLFrameElementBase* frameElementBase = nullptr;
2695
2696             if (is<HTMLFrameElementBase>(*ownerElement))
2697                 frameElementBase = downcast<HTMLFrameElementBase>(ownerElement);
2698
2699             if (frameElementAndViewPermitScroll(frameElementBase, frameView)) {
2700                 // If this assertion fires we need to protect the ownerElement from being destroyed.
2701                 ScriptDisallowedScope::InMainThread scriptDisallowedScope;
2702
2703                 LayoutRect viewRect = frameView.visibleContentRect(LegacyIOSDocumentVisibleRect);
2704                 LayoutRect exposeRect = getRectToExpose(viewRect, absoluteRect, insideFixed, options.alignX, options.alignY);
2705
2706                 IntPoint scrollOffset(roundedIntPoint(exposeRect.location()));
2707                 // Adjust offsets if they're outside of the allowable range.
2708                 scrollOffset = scrollOffset.constrainedBetween(IntPoint(), IntPoint(frameView.contentsSize()));
2709                 frameView.setScrollPosition(scrollOffset);
2710
2711                 if (options.shouldAllowCrossOriginScrolling == ShouldAllowCrossOriginScrolling::Yes || frameView.safeToPropagateScrollToParent()) {
2712                     parentLayer = ownerElement->renderer()->enclosingLayer();
2713                     // Convert the rect into the coordinate space of the parent frame's document.
2714                     newRect = frameView.contentsToContainingViewContents(enclosingIntRect(newRect));
2715                     insideFixed = false; // FIXME: ideally need to determine if this <iframe> is inside position:fixed.
2716                 } else
2717                     parentLayer = nullptr;
2718             }
2719         } else {
2720             if (options.revealMode == SelectionRevealMode::RevealUpToMainFrame && frameView.frame().isMainFrame())
2721                 return;
2722
2723             auto minScrollPosition = frameView.minimumScrollPosition();
2724             auto maxScrollPosition = frameView.maximumScrollPosition();
2725
2726 #if !PLATFORM(IOS_FAMILY)
2727             LayoutRect viewRect = frameView.visibleContentRect();
2728 #else
2729             // FIXME: ContentInsets should be taken care of in UI process side. webkit.org/b/199682
2730             // To do that, getRectToExpose needs to return the additional scrolling to do beyond content rect.
2731             LayoutRect viewRect = frameView.viewRectExpandedByContentInsets();
2732
2733             // FIXME: webkit.org/b/199683 FrameView::visibleContentRect is wrong when content insets are present
2734             maxScrollPosition = frameView.scrollPositionFromOffset(ScrollPosition(frameView.totalContentsSize() - flooredIntSize(viewRect.size())));
2735
2736             auto contentInsets = page().contentInsets();
2737             minScrollPosition.move(-contentInsets.left(), -contentInsets.top());
2738             maxScrollPosition.move(contentInsets.right(), contentInsets.bottom());
2739 #endif
2740             // Move the target rect into "scrollView contents" coordinates.
2741             LayoutRect targetRect = absoluteRect;
2742             targetRect.move(0, frameView.headerHeight());
2743
2744             LayoutRect revealRect = getRectToExpose(viewRect, targetRect, insideFixed, options.alignX, options.alignY);
2745             // Avoid scrolling to the rounded value of revealRect.location() if we don't actually need to scroll
2746             if (revealRect != viewRect) {
2747                 ScrollOffset clampedScrollPosition = roundedIntPoint(revealRect.location()).constrainedBetween(minScrollPosition, maxScrollPosition);
2748                 frameView.setScrollPosition(clampedScrollPosition);
2749             }
2750
2751             // This is the outermost view of a web page, so after scrolling this view we
2752             // scroll its container by calling Page::scrollRectIntoView.
2753             // This only has an effect on the Mac platform in applications
2754             // that put web views into scrolling containers, such as Mac OS X Mail.
2755             // The canAutoscroll function in EventHandler also knows about this.
2756             page().chrome().scrollRectIntoView(snappedIntRect(absoluteRect));
2757         }
2758     }
2759     
2760     if (parentLayer)
2761         parentLayer->scrollRectToVisible(newRect, insideFixed, options);
2762 }
2763
2764 void RenderLayer::updateCompositingLayersAfterScroll()
2765 {
2766     if (compositor().hasContentCompositingLayers()) {
2767         // Our stacking container is guaranteed to contain all of our descendants that may need
2768         // repositioning, so update compositing layers from there.
2769         if (RenderLayer* compositingAncestor = stackingContext()->enclosingCompositingLayer()) {
2770             if (usesCompositedScrolling())
2771                 compositor().updateCompositingLayers(CompositingUpdateType::OnCompositedScroll, compositingAncestor);
2772             else {
2773                 // FIXME: would be nice to only dirty layers whose positions were affected by scrolling.
2774                 compositingAncestor->setDescendantsNeedUpdateBackingAndHierarchyTraversal();
2775                 compositor().updateCompositingLayers(CompositingUpdateType::OnScroll, compositingAncestor);
2776             }
2777         }
2778     }
2779 }
2780
2781 LayoutRect RenderLayer::getRectToExpose(const LayoutRect& visibleRect, const LayoutRect& exposeRect, bool insideFixed, const ScrollAlignment& alignX, const ScrollAlignment& alignY) const
2782 {
2783     FrameView& frameView = renderer().view().frameView();
2784     if (renderer().isRenderView() && insideFixed) {
2785         // If the element is inside position:fixed and we're not scaled, no amount of scrolling is going to move things around.
2786         if (frameView.frameScaleFactor() == 1)
2787             return visibleRect;
2788
2789         if (renderer().settings().visualViewportEnabled()) {
2790             // exposeRect is in absolute coords, affected by page scale. Unscale it.
2791             LayoutRect unscaledExposeRect = exposeRect;
2792             unscaledExposeRect.scale(1 / frameView.frameScaleFactor());
2793             unscaledExposeRect.move(0, -frameView.headerHeight());
2794
2795             // These are both in unscaled coordinates.
2796             LayoutRect layoutViewport = frameView.layoutViewportRect();
2797             LayoutRect visualViewport = frameView.visualViewportRect();
2798
2799             // The rect to expose may be partially offscreen, which we can't do anything about with position:fixed.
2800             unscaledExposeRect.intersect(layoutViewport);
2801             // Make sure it's not larger than the visual viewport; if so, we'll just move to the top left.
2802             unscaledExposeRect.setSize(unscaledExposeRect.size().shrunkTo(visualViewport.size()));
2803
2804             // Compute how much we have to move the visualViewport to reveal the part of the layoutViewport that contains exposeRect.
2805             LayoutRect requiredVisualViewport = getRectToExpose(visualViewport, unscaledExposeRect, false, alignX, alignY);
2806             // Scale it back up.
2807             requiredVisualViewport.scale(frameView.frameScaleFactor());
2808             requiredVisualViewport.move(0, frameView.headerHeight());
2809             return requiredVisualViewport;
2810         }
2811     }
2812
2813     // Determine the appropriate X behavior.
2814     ScrollAlignment::Behavior scrollX;
2815     LayoutRect exposeRectX(exposeRect.x(), visibleRect.y(), exposeRect.width(), visibleRect.height());
2816     LayoutUnit intersectWidth = intersection(visibleRect, exposeRectX).width();
2817     if (intersectWidth == exposeRect.width() || intersectWidth >= MIN_INTERSECT_FOR_REVEAL)
2818         // If the rectangle is fully visible, use the specified visible behavior.
2819         // If the rectangle is partially visible, but over a certain threshold,
2820         // then treat it as fully visible to avoid unnecessary horizontal scrolling
2821         scrollX = ScrollAlignment::getVisibleBehavior(alignX);
2822     else if (intersectWidth == visibleRect.width()) {
2823         // If the rect is bigger than the visible area, don't bother trying to center. Other alignments will work.
2824         scrollX = ScrollAlignment::getVisibleBehavior(alignX);
2825         if (scrollX == ScrollAlignment::Behavior::AlignCenter)
2826             scrollX = ScrollAlignment::Behavior::NoScroll;
2827     } else if (intersectWidth > 0)
2828         // If the rectangle is partially visible, but not above the minimum threshold, use the specified partial behavior
2829         scrollX = ScrollAlignment::getPartialBehavior(alignX);
2830     else
2831         scrollX = ScrollAlignment::getHiddenBehavior(alignX);
2832     // If we're trying to align to the closest edge, and the exposeRect is further right
2833     // than the visibleRect, and not bigger than the visible area, then align with the right.
2834     if (scrollX == ScrollAlignment::Behavior::AlignToClosestEdge && exposeRect.maxX() > visibleRect.maxX() && exposeRect.width() < visibleRect.width())
2835         scrollX = ScrollAlignment::Behavior::AlignRight;
2836
2837     // Given the X behavior, compute the X coordinate.
2838     LayoutUnit x;
2839     if (scrollX == ScrollAlignment::Behavior::NoScroll)
2840         x = visibleRect.x();
2841     else if (scrollX == ScrollAlignment::Behavior::AlignRight)
2842         x = exposeRect.maxX() - visibleRect.width();
2843     else if (scrollX == ScrollAlignment::Behavior::AlignCenter)
2844         x = exposeRect.x() + (exposeRect.width() - visibleRect.width()) / 2;
2845     else
2846         x = exposeRect.x();
2847
2848     // Determine the appropriate Y behavior.
2849     ScrollAlignment::Behavior scrollY;
2850     LayoutRect exposeRectY(visibleRect.x(), exposeRect.y(), visibleRect.width(), exposeRect.height());
2851     LayoutUnit intersectHeight = intersection(visibleRect, exposeRectY).height();
2852     if (intersectHeight == exposeRect.height())
2853         // If the rectangle is fully visible, use the specified visible behavior.
2854         scrollY = ScrollAlignment::getVisibleBehavior(alignY);
2855     else if (intersectHeight == visibleRect.height()) {
2856         // If the rect is bigger than the visible area, don't bother trying to center. Other alignments will work.
2857         scrollY = ScrollAlignment::getVisibleBehavior(alignY);
2858         if (scrollY == ScrollAlignment::Behavior::AlignCenter)
2859             scrollY = ScrollAlignment::Behavior::NoScroll;
2860     } else if (intersectHeight > 0)
2861         // If the rectangle is partially visible, use the specified partial behavior
2862         scrollY = ScrollAlignment::getPartialBehavior(alignY);
2863     else
2864         scrollY = ScrollAlignment::getHiddenBehavior(alignY);
2865     // If we're trying to align to the closest edge, and the exposeRect is further down
2866     // than the visibleRect, and not bigger than the visible area, then align with the bottom.
2867     if (scrollY == ScrollAlignment::Behavior::AlignToClosestEdge && exposeRect.maxY() > visibleRect.maxY() && exposeRect.height() < visibleRect.height())
2868         scrollY = ScrollAlignment::Behavior::AlignBottom;
2869
2870     // Given the Y behavior, compute the Y coordinate.
2871     LayoutUnit y;
2872     if (scrollY == ScrollAlignment::Behavior::NoScroll)
2873         y = visibleRect.y();
2874     else if (scrollY == ScrollAlignment::Behavior::AlignBottom)
2875         y = exposeRect.maxY() - visibleRect.height();
2876     else if (scrollY == ScrollAlignment::Behavior::AlignCenter)
2877         y = exposeRect.y() + (exposeRect.height() - visibleRect.height()) / 2;
2878     else
2879         y = exposeRect.y();
2880
2881     return LayoutRect(LayoutPoint(x, y), visibleRect.size());
2882 }
2883
2884 void RenderLayer::autoscroll(const IntPoint& positionInWindow)
2885 {
2886     IntPoint currentDocumentPosition = renderer().view().frameView().windowToContents(positionInWindow);
2887     scrollRectToVisible(LayoutRect(currentDocumentPosition, LayoutSize(1, 1)), false, { SelectionRevealMode::Reveal, ScrollAlignment::alignToEdgeIfNeeded, ScrollAlignment::alignToEdgeIfNeeded, ShouldAllowCrossOriginScrolling::Yes });
2888 }
2889
2890 bool RenderLayer::canResize() const
2891 {
2892     // We need a special case for <iframe> because they never have
2893     // hasOverflowClip(). However, they do "implicitly" clip their contents, so
2894     // we want to allow resizing them also.
2895     return (renderer().hasOverflowClip() || renderer().isRenderIFrame()) && renderer().style().resize() != Resize::None;
2896 }
2897
2898 void RenderLayer::resize(const PlatformMouseEvent& evt, const LayoutSize& oldOffset)
2899 {
2900     // FIXME: This should be possible on generated content but is not right now.
2901     if (!inResizeMode() || !canResize() || !renderer().element())
2902         return;
2903
2904     // FIXME: The only case where renderer->element()->renderer() != renderer is with continuations. Do they matter here?
2905     // If they do it would still be better to deal with them explicitly.
2906     Element* element = renderer().element();
2907     auto* renderer = downcast<RenderBox>(element->renderer());
2908
2909     Document& document = element->document();
2910     if (!document.frame()->eventHandler().mousePressed())
2911         return;
2912
2913     float zoomFactor = renderer->style().effectiveZoom();
2914
2915     LayoutSize newOffset = offsetFromResizeCorner(document.view()->windowToContents(evt.position()));
2916     newOffset.setWidth(newOffset.width() / zoomFactor);
2917     newOffset.setHeight(newOffset.height() / zoomFactor);
2918     
2919     LayoutSize currentSize = LayoutSize(renderer->width() / zoomFactor, renderer->height() / zoomFactor);
2920     LayoutSize minimumSize = element->minimumSizeForResizing().shrunkTo(currentSize);
2921     element->setMinimumSizeForResizing(minimumSize);
2922     
2923     LayoutSize adjustedOldOffset = LayoutSize(oldOffset.width() / zoomFactor, oldOffset.height() / zoomFactor);
2924     if (shouldPlaceBlockDirectionScrollbarOnLeft()) {
2925         newOffset.setWidth(-newOffset.width());
2926         adjustedOldOffset.setWidth(-adjustedOldOffset.width());
2927     }
2928     
2929     LayoutSize difference = (currentSize + newOffset - adjustedOldOffset).expandedTo(minimumSize) - currentSize;
2930
2931     StyledElement* styledElement = downcast<StyledElement>(element);
2932     bool isBoxSizingBorder = renderer->style().boxSizing() == BoxSizing::BorderBox;
2933
2934     Resize resize = renderer->style().resize();
2935     if (resize != Resize::Vertical && difference.width()) {
2936         if (is<HTMLFormControlElement>(*element)) {
2937             // Make implicit margins from the theme explicit (see <http://bugs.webkit.org/show_bug.cgi?id=9547>).
2938             styledElement->setInlineStyleProperty(CSSPropertyMarginLeft, renderer->marginLeft() / zoomFactor, CSSPrimitiveValue::CSS_PX);
2939             styledElement->setInlineStyleProperty(CSSPropertyMarginRight, renderer->marginRight() / zoomFactor, CSSPrimitiveValue::CSS_PX);
2940         }
2941         LayoutUnit baseWidth = renderer->width() - (isBoxSizingBorder ? 0_lu : renderer->horizontalBorderAndPaddingExtent());
2942         baseWidth = baseWidth / zoomFactor;
2943         styledElement->setInlineStyleProperty(CSSPropertyWidth, roundToInt(baseWidth + difference.width()), CSSPrimitiveValue::CSS_PX);
2944     }
2945
2946     if (resize != Resize::Horizontal && difference.height()) {
2947         if (is<HTMLFormControlElement>(*element)) {
2948             // Make implicit margins from the theme explicit (see <http://bugs.webkit.org/show_bug.cgi?id=9547>).
2949             styledElement->setInlineStyleProperty(CSSPropertyMarginTop, renderer->marginTop() / zoomFactor, CSSPrimitiveValue::CSS_PX);
2950             styledElement->setInlineStyleProperty(CSSPropertyMarginBottom, renderer->marginBottom() / zoomFactor, CSSPrimitiveValue::CSS_PX);
2951         }
2952         LayoutUnit baseHeight = renderer->height() - (isBoxSizingBorder ? 0_lu : renderer->verticalBorderAndPaddingExtent());
2953         baseHeight = baseHeight / zoomFactor;
2954         styledElement->setInlineStyleProperty(CSSPropertyHeight, roundToInt(baseHeight + difference.height()), CSSPrimitiveValue::CSS_PX);
2955     }
2956
2957     document.updateLayout();
2958
2959     // FIXME (Radar 4118564): We should also autoscroll the window as necessary to keep the point under the cursor in view.
2960 }
2961
2962 void RenderLayer::setScrollOffset(const ScrollOffset& offset)
2963 {
2964     scrollTo(scrollPositionFromOffset(offset));
2965 }
2966
2967 ScrollingNodeID RenderLayer::scrollingNodeID() const
2968 {
2969     if (!isComposited())
2970         return 0;
2971
2972     return backing()->scrollingNodeIDForRole(ScrollCoordinationRole::Scrolling);
2973 }
2974
2975 IntRect RenderLayer::visibleContentRectInternal(VisibleContentRectIncludesScrollbars scrollbarInclusion, VisibleContentRectBehavior) const
2976 {
2977     IntSize scrollbarSpace;
2978     if (showsOverflowControls() && scrollbarInclusion == IncludeScrollbars)
2979         scrollbarSpace = scrollbarIntrusion();
2980     
2981     auto visibleSize = this->visibleSize();
2982     return { scrollPosition(), { std::max(0, visibleSize.width() - scrollbarSpace.width()), std::max(0, visibleSize.height() - scrollbarSpace.height()) } };
2983 }
2984
2985 IntSize RenderLayer::overhangAmount() const
2986 {
2987 #if ENABLE(RUBBER_BANDING)
2988     if (!renderer().settings().rubberBandingForSubScrollableRegionsEnabled())
2989         return IntSize();
2990
2991     IntSize stretch;
2992
2993     // FIXME: use maximumScrollOffset(), or just move this to ScrollableArea.
2994     ScrollOffset scrollOffset = scrollOffsetFromPosition(scrollPosition());
2995     auto reachableSize = reachableTotalContentsSize();
2996     if (scrollOffset.y() < 0)
2997         stretch.setHeight(scrollOffset.y());
2998     else if (reachableSize.height() && scrollOffset.y() > reachableSize.height() - visibleHeight())
2999         stretch.setHeight(scrollOffset.y() - (reachableSize.height() - visibleHeight()));
3000
3001     if (scrollOffset.x() < 0)
3002         stretch.setWidth(scrollOffset.x());
3003     else if (reachableSize.width() && scrollOffset.x() > reachableSize.width() - visibleWidth())
3004         stretch.setWidth(scrollOffset.x() - (reachableSize.width() - visibleWidth()));
3005
3006     return stretch;
3007 #else
3008     return IntSize();
3009 #endif
3010 }
3011
3012 bool RenderLayer::isActive() const
3013 {
3014     return page().focusController().isActive();
3015 }
3016
3017 static int cornerStart(const RenderLayer& layer, int minX, int maxX, int thickness)
3018 {
3019     if (layer.shouldPlaceBlockDirectionScrollbarOnLeft())
3020         return minX + layer.renderer().style().borderLeftWidth();
3021     return maxX - thickness - layer.renderer().style().borderRightWidth();
3022 }
3023
3024 static LayoutRect cornerRect(const RenderLayer& layer, const LayoutRect& bounds)
3025 {
3026     int horizontalThickness;
3027     int verticalThickness;
3028     if (!layer.verticalScrollbar() && !layer.horizontalScrollbar()) {
3029         // FIXME: This isn't right.  We need to know the thickness of custom scrollbars
3030         // even when they don't exist in order to set the resizer square size properly.
3031         horizontalThickness = ScrollbarTheme::theme().scrollbarThickness();
3032         verticalThickness = horizontalThickness;
3033     } else if (layer.verticalScrollbar() && !layer.horizontalScrollbar()) {
3034         horizontalThickness = layer.verticalScrollbar()->width();
3035         verticalThickness = horizontalThickness;
3036     } else if (layer.horizontalScrollbar() && !layer.verticalScrollbar()) {
3037         verticalThickness = layer.horizontalScrollbar()->height();
3038         horizontalThickness = verticalThickness;
3039     } else {
3040         horizontalThickness = layer.verticalScrollbar()->width();
3041         verticalThickness = layer.horizontalScrollbar()->height();
3042     }
3043     return LayoutRect(cornerStart(layer, bounds.x(), bounds.maxX(), horizontalThickness),
3044         bounds.maxY() - verticalThickness - layer.renderer().style().borderBottomWidth(),
3045         horizontalThickness, verticalThickness);
3046 }
3047
3048 IntRect RenderLayer::scrollCornerRect() const
3049 {
3050     // We have a scrollbar corner when a non overlay scrollbar is visible and not filling the entire length of the box.
3051     // This happens when:
3052     // (a) A resizer is present and at least one non overlay scrollbar is present
3053     // (b) Both non overlay scrollbars are present.
3054     // Overlay scrollbars always fill the entire length of the box so we never have scroll corner in that case.
3055     bool hasHorizontalBar = m_hBar && !m_hBar->isOverlayScrollbar();
3056     bool hasVerticalBar = m_vBar && !m_vBar->isOverlayScrollbar();
3057     bool hasResizer = renderer().style().resize() != Resize::None;
3058     if ((hasHorizontalBar && hasVerticalBar) || (hasResizer && (hasHorizontalBar || hasVerticalBar)))
3059         return snappedIntRect(cornerRect(*this, renderBox()->borderBoxRect()));
3060     return IntRect();
3061 }
3062
3063 static LayoutRect resizerCornerRect(const RenderLayer& layer, const LayoutRect& bounds)
3064 {
3065     ASSERT(layer.renderer().isBox());
3066     if (layer.renderer().style().resize() == Resize::None)
3067         return LayoutRect();
3068     return cornerRect(layer, bounds);
3069 }
3070
3071 LayoutRect RenderLayer::scrollCornerAndResizerRect() const
3072 {
3073     RenderBox* box = renderBox();
3074     if (!box)
3075         return LayoutRect();
3076     LayoutRect scrollCornerAndResizer = scrollCornerRect();
3077     if (scrollCornerAndResizer.isEmpty())
3078         scrollCornerAndResizer = resizerCornerRect(*this, box->borderBoxRect());
3079     return scrollCornerAndResizer;
3080 }
3081
3082 bool RenderLayer::isScrollCornerVisible() const
3083 {
3084     ASSERT(renderer().isBox());
3085     return !scrollCornerRect().isEmpty();
3086 }
3087
3088 IntRect RenderLayer::convertFromScrollbarToContainingView(const Scrollbar& scrollbar, const IntRect& scrollbarRect) const
3089 {
3090     IntRect rect = scrollbarRect;
3091     rect.move(scrollbarOffset(scrollbar));
3092
3093     return renderer().view().frameView().convertFromRendererToContainingView(&renderer(), rect);
3094 }
3095
3096 IntRect RenderLayer::convertFromContainingViewToScrollbar(const Scrollbar& scrollbar, const IntRect& parentRect) const
3097 {
3098     IntRect rect = renderer().view().frameView().convertFromContainingViewToRenderer(&renderer(), parentRect);
3099     rect.move(-scrollbarOffset(scrollbar));
3100     return rect;
3101 }
3102
3103 IntPoint RenderLayer::convertFromScrollbarToContainingView(const Scrollbar& scrollbar, const IntPoint& scrollbarPoint) const
3104 {
3105     IntPoint point = scrollbarPoint;
3106     point.move(scrollbarOffset(scrollbar));
3107     return renderer().view().frameView().convertFromRendererToContainingView(&renderer(), point);
3108 }
3109
3110 IntPoint RenderLayer::convertFromContainingViewToScrollbar(const Scrollbar& scrollbar, const IntPoint& parentPoint) const
3111 {
3112     IntPoint point = renderer().view().frameView().convertFromContainingViewToRenderer(&renderer(), parentPoint);
3113     point.move(-scrollbarOffset(scrollbar));
3114     return point;
3115 }
3116
3117 IntSize RenderLayer::visibleSize() const
3118 {
3119     RenderBox* box = renderBox();
3120     if (!box)
3121         return IntSize();
3122
3123     return IntSize(roundToInt(box->clientWidth()), roundToInt(box->clientHeight()));
3124 }
3125
3126 IntSize RenderLayer::contentsSize() const
3127 {
3128     return IntSize(scrollWidth(), scrollHeight());
3129 }
3130
3131 IntSize RenderLayer::reachableTotalContentsSize() const
3132 {
3133     IntSize contentsSize = this->contentsSize();
3134
3135     if (!hasScrollableHorizontalOverflow())
3136         contentsSize.setWidth(std::min(contentsSize.width(), visibleSize().width()));
3137
3138     if (!hasScrollableVerticalOverflow())
3139         contentsSize.setHeight(std::min(contentsSize.height(), visibleSize().height()));
3140
3141     return contentsSize;
3142 }
3143
3144 void RenderLayer::availableContentSizeChanged(AvailableSizeChangeReason reason)
3145 {
3146     ScrollableArea::availableContentSizeChanged(reason);
3147
3148     if (reason == AvailableSizeChangeReason::ScrollbarsChanged) {
3149         if (is<RenderBlock>(renderer()))
3150             downcast<RenderBlock>(renderer()).setShouldForceRelayoutChildren(true);
3151         renderer().setNeedsLayout();
3152     }
3153 }
3154
3155 bool RenderLayer::shouldSuspendScrollAnimations() const
3156 {
3157     return renderer().view().frameView().shouldSuspendScrollAnimations();
3158 }
3159
3160 #if PLATFORM(IOS_FAMILY)
3161 void RenderLayer::didStartScroll()
3162 {
3163     page().chrome().client().didStartOverflowScroll();
3164 }
3165
3166 void RenderLayer::didEndScroll()
3167 {
3168     page().chrome().client().didEndOverflowScroll();
3169 }
3170     
3171 void RenderLayer::didUpdateScroll()
3172 {
3173     // Send this notification when we scroll, since this is how we keep selection updated.
3174     page().chrome().client().didLayout(ChromeClient::Scroll);
3175 }
3176 #endif
3177
3178 IntPoint RenderLayer::lastKnownMousePosition() const
3179 {
3180     return renderer().frame().eventHandler().lastKnownMousePosition();
3181 }
3182
3183 bool RenderLayer::isHandlingWheelEvent() const
3184 {
3185     return renderer().frame().eventHandler().isHandlingWheelEvent();
3186 }
3187
3188 IntRect RenderLayer::rectForHorizontalScrollbar(const IntRect& borderBoxRect) const
3189 {
3190     if (!m_hBar)
3191         return IntRect();
3192
3193     const RenderBox* box = renderBox();
3194     const IntRect& scrollCorner = scrollCornerRect();
3195
3196     return IntRect(horizontalScrollbarStart(borderBoxRect.x()),
3197         borderBoxRect.maxY() - box->borderBottom() - m_hBar->height(),
3198         borderBoxRect.width() - (box->borderLeft() + box->borderRight()) - scrollCorner.width(),
3199         m_hBar->height());
3200 }
3201
3202 IntRect RenderLayer::rectForVerticalScrollbar(const IntRect& borderBoxRect) const
3203 {
3204     if (!m_vBar)
3205         return IntRect();
3206
3207     const RenderBox* box = renderBox();
3208     const IntRect& scrollCorner = scrollCornerRect();
3209
3210     return IntRect(verticalScrollbarStart(borderBoxRect.x(), borderBoxRect.maxX()),
3211         borderBoxRect.y() + box->borderTop(),
3212         m_vBar->width(),
3213         borderBoxRect.height() - (box->borderTop() + box->borderBottom()) - scrollCorner.height());
3214 }
3215
3216 LayoutUnit RenderLayer::verticalScrollbarStart(int minX, int maxX) const
3217 {
3218     const RenderBox* box = renderBox();
3219     if (shouldPlaceBlockDirectionScrollbarOnLeft())
3220         return minX + box->borderLeft();
3221     return maxX - box->borderRight() - m_vBar->width();
3222 }
3223
3224 LayoutUnit RenderLayer::horizontalScrollbarStart(int minX) const
3225 {
3226     const RenderBox* box = renderBox();
3227     int x = minX + box->borderLeft();
3228     if (shouldPlaceBlockDirectionScrollbarOnLeft())
3229         x += m_vBar ? m_vBar->width() : roundToInt(resizerCornerRect(*this, box->borderBoxRect()).width());
3230     return x;
3231 }
3232
3233 IntSize RenderLayer::scrollbarOffset(const Scrollbar& scrollbar) const
3234 {
3235     RenderBox* box = renderBox();
3236
3237     if (&scrollbar == m_vBar.get())
3238         return IntSize(verticalScrollbarStart(0, box->width()), box->borderTop());
3239
3240     if (&scrollbar == m_hBar.get())
3241         return IntSize(horizontalScrollbarStart(0), box->height() - box->borderBottom() - scrollbar.height());
3242     
3243     ASSERT_NOT_REACHED();
3244     return IntSize();
3245 }
3246
3247 void RenderLayer::invalidateScrollbarRect(Scrollbar& scrollbar, const IntRect& rect)
3248 {
3249     if (!showsOverflowControls())
3250         return;
3251
3252     if (&scrollbar == m_vBar.get()) {
3253         if (GraphicsLayer* layer = layerForVerticalScrollbar()) {
3254             layer->setNeedsDisplayInRect(rect);
3255             return;
3256         }
3257     } else {
3258         if (GraphicsLayer* layer = layerForHorizontalScrollbar()) {
3259             layer->setNeedsDisplayInRect(rect);
3260             return;
3261         }
3262     }
3263
3264     IntRect scrollRect = rect;
3265     RenderBox* box = renderBox();
3266     ASSERT(box);
3267     // If we are not yet inserted into the tree, there is no need to repaint.
3268     if (!box->parent())
3269         return;
3270
3271     if (&scrollbar == m_vBar.get())
3272         scrollRect.move(verticalScrollbarStart(0, box->width()), box->borderTop());
3273     else
3274         scrollRect.move(horizontalScrollbarStart(0), box->height() - box->borderBottom() - scrollbar.height());
3275     LayoutRect repaintRect = scrollRect;
3276     renderBox()->flipForWritingMode(repaintRect);
3277     renderer().repaintRectangle(repaintRect);
3278 }
3279
3280 void RenderLayer::invalidateScrollCornerRect(const IntRect& rect)
3281 {
3282     if (!showsOverflowControls())
3283         return;
3284
3285     if (GraphicsLayer* layer = layerForScrollCorner()) {
3286         layer->setNeedsDisplayInRect(rect);
3287         return;
3288     }
3289
3290     if (m_scrollCorner)
3291         m_scrollCorner->repaintRectangle(rect);
3292     if (m_resizer)
3293         m_resizer->repaintRectangle(rect);
3294 }
3295
3296 static bool scrollbarHiddenByStyle(Scrollbar* scrollbar)
3297 {
3298     if (!scrollbar || !scrollbar->isCustomScrollbar())
3299         return false;
3300
3301     std::unique_ptr<RenderStyle> scrollbarStyle = static_cast<RenderScrollbar*>(scrollbar)->getScrollbarPseudoStyle(ScrollbarBGPart, PseudoId::Scrollbar);
3302
3303     return scrollbarStyle && scrollbarStyle->display() == DisplayType::None;
3304 }
3305
3306 bool RenderLayer::horizontalScrollbarHiddenByStyle() const
3307 {
3308     return scrollbarHiddenByStyle(horizontalScrollbar());
3309 }
3310
3311 bool RenderLayer::verticalScrollbarHiddenByStyle() const
3312 {
3313     return scrollbarHiddenByStyle(verticalScrollbar());
3314 }
3315
3316 static inline RenderElement* rendererForScrollbar(RenderLayerModelObject& renderer)
3317 {
3318     if (Element* element = renderer.element()) {
3319         if (ShadowRoot* shadowRoot = element->containingShadowRoot()) {
3320             if (shadowRoot->mode() == ShadowRootMode::UserAgent)
3321                 return shadowRoot->host()->renderer();
3322         }
3323     }
3324
3325     return &renderer;
3326 }
3327
3328 Ref<Scrollbar> RenderLayer::createScrollbar(ScrollbarOrientation orientation)
3329 {
3330     RefPtr<Scrollbar> widget;
3331     ASSERT(rendererForScrollbar(renderer()));
3332     auto& actualRenderer = *rendererForScrollbar(renderer());
3333     bool hasCustomScrollbarStyle = is<RenderBox>(actualRenderer) && downcast<RenderBox>(actualRenderer).style().hasPseudoStyle(PseudoId::Scrollbar);
3334     if (hasCustomScrollbarStyle)
3335         widget = RenderScrollbar::createCustomScrollbar(*this, orientation, downcast<RenderBox>(actualRenderer).element());
3336     else {
3337         widget = Scrollbar::createNativeScrollbar(*this, orientation, RegularScrollbar);
3338         didAddScrollbar(widget.get(), orientation);
3339         if (page().expectsWheelEventTriggers())
3340             scrollAnimator().setWheelEventTestTrigger(page().testTrigger());
3341     }
3342     renderer().view().frameView().addChild(*widget);
3343     return widget.releaseNonNull();
3344 }
3345
3346 void RenderLayer::destroyScrollbar(ScrollbarOrientation orientation)
3347 {
3348     RefPtr<Scrollbar>& scrollbar = orientation == HorizontalScrollbar ? m_hBar : m_vBar;
3349     if (!scrollbar)
3350         return;
3351
3352     if (!scrollbar->isCustomScrollbar())
3353         willRemoveScrollbar(scrollbar.get(), orientation);
3354
3355     scrollbar->removeFromParent();
3356     scrollbar = nullptr;
3357 }
3358
3359 bool RenderLayer::scrollsOverflow() const
3360 {
3361     if (!is<RenderBox>(renderer()))
3362         return false;
3363
3364     return downcast<RenderBox>(renderer()).scrollsOverflow();
3365 }
3366
3367 void RenderLayer::setHasHorizontalScrollbar(bool hasScrollbar)
3368 {
3369     if (hasScrollbar == hasHorizontalScrollbar())
3370         return;
3371
3372     if (hasScrollbar) {
3373         m_hBar = createScrollbar(HorizontalScrollbar);
3374 #if ENABLE(RUBBER_BANDING)
3375         ScrollElasticity elasticity = scrollsOverflow() && renderer().settings().rubberBandingForSubScrollableRegionsEnabled() ? ScrollElasticityAutomatic : ScrollElasticityNone;
3376         ScrollableArea::setHorizontalScrollElasticity(elasticity);
3377 #endif
3378     } else {
3379         destroyScrollbar(HorizontalScrollbar);
3380 #if ENABLE(RUBBER_BANDING)
3381         ScrollableArea::setHorizontalScrollElasticity(ScrollElasticityNone);
3382 #endif
3383     }
3384
3385     // Destroying or creating one bar can cause our scrollbar corner to come and go.  We need to update the opposite scrollbar's style.
3386     if (m_hBar)
3387         m_hBar->styleChanged();
3388     if (m_vBar)
3389         m_vBar->styleChanged();
3390 }
3391
3392 void RenderLayer::setHasVerticalScrollbar(bool hasScrollbar)
3393 {
3394     if (hasScrollbar == hasVerticalScrollbar())
3395         return;
3396
3397     if (hasScrollbar) {
3398         m_vBar = createScrollbar(VerticalScrollbar);
3399 #if ENABLE(RUBBER_BANDING)
3400         ScrollElasticity elasticity = scrollsOverflow() && renderer().settings().rubberBandingForSubScrollableRegionsEnabled() ? ScrollElasticityAutomatic : ScrollElasticityNone;
3401         ScrollableArea::setVerticalScrollElasticity(elasticity);
3402 #endif
3403     } else {
3404         destroyScrollbar(VerticalScrollbar);
3405 #if ENABLE(RUBBER_BANDING)
3406         ScrollableArea::setVerticalScrollElasticity(ScrollElasticityNone);
3407 #endif
3408     }
3409
3410      // Destroying or creating one bar can cause our scrollbar corner to come and go.  We need to update the opposite scrollbar's style.
3411     if (m_hBar)
3412         m_hBar->styleChanged();
3413     if (m_vBar)
3414         m_vBar->styleChanged();
3415 }
3416
3417 ScrollableArea* RenderLayer::enclosingScrollableArea() const
3418 {
3419     if (RenderLayer* scrollableLayer = enclosingScrollableLayer())
3420         return scrollableLayer;
3421
3422     // FIXME: We should return the frame view here (or possibly an ancestor frame view,
3423     // if the frame view isn't scrollable.
3424     return nullptr;
3425 }
3426
3427 bool RenderLayer::isScrollableOrRubberbandable()
3428 {
3429     return renderer().isScrollableOrRubberbandableBox();
3430 }
3431
3432 bool RenderLayer::hasScrollableOrRubberbandableAncestor()
3433 {
3434     for (RenderLayer* nextLayer = parentLayerCrossFrame(*this); nextLayer; nextLayer = parentLayerCrossFrame(*nextLayer)) {
3435         if (nextLayer->isScrollableOrRubberbandable())
3436             return true;
3437     }
3438
3439     return false;
3440 }
3441
3442 bool RenderLayer::useDarkAppearance() const
3443 {
3444     return renderer().useDarkAppearance();
3445 }
3446
3447 #if ENABLE(CSS_SCROLL_SNAP)
3448 void RenderLayer::updateSnapOffsets()
3449 {
3450     // FIXME: Extend support beyond HTMLElements.
3451     if (!is<HTMLElement>(enclosingElement()) || !enclosingElement()->renderBox())
3452         return;
3453
3454     RenderBox* box = enclosingElement()->renderBox();
3455     updateSnapOffsetsForScrollableArea(*this, *downcast<HTMLElement>(enclosingElement()), *box, box->style());
3456 }
3457
3458 bool RenderLayer::isScrollSnapInProgress() const
3459 {
3460     if (!scrollsOverflow())
3461         return false;
3462     
3463     if (ScrollAnimator* scrollAnimator = existingScrollAnimator())
3464         return scrollAnimator->isScrollSnapInProgress();
3465     
3466     return false;
3467 }
3468 #endif
3469
3470 bool RenderLayer::usesMockScrollAnimator() const
3471 {
3472     return DeprecatedGlobalSettings::usesMockScrollAnimator();
3473 }
3474
3475 void RenderLayer::logMockScrollAnimatorMessage(const String& message) const
3476 {
3477     renderer().document().addConsoleMessage(MessageSource::Other, MessageLevel::Debug, "RenderLayer: " + message);
3478 }
3479
3480 int RenderLayer::verticalScrollbarWidth(OverlayScrollbarSizeRelevancy relevancy) const
3481 {
3482     if (!m_vBar
3483         || !showsOverflowControls()
3484         || (m_vBar->isOverlayScrollbar() && (relevancy == IgnoreOverlayScrollbarSize || !m_vBar->shouldParticipateInHitTesting())))
3485         return 0;
3486
3487     return m_vBar->width();
3488 }
3489
3490 int RenderLayer::horizontalScrollbarHeight(OverlayScrollbarSizeRelevancy relevancy) const
3491 {
3492     if (!m_hBar
3493         || !showsOverflowControls()
3494         || (m_hBar->isOverlayScrollbar() && (relevancy == IgnoreOverlayScrollbarSize || !m_hBar->shouldParticipateInHitTesting())))
3495         return 0;
3496
3497     return m_hBar->height();
3498 }
3499
3500 IntSize RenderLayer::offsetFromResizeCorner(const IntPoint& absolutePoint) const
3501 {
3502     // Currently the resize corner is either the bottom right corner or the bottom left corner.
3503     // FIXME: This assumes the location is 0, 0. Is this guaranteed to always be the case?
3504     IntSize elementSize = size();
3505     if (shouldPlaceBlockDirectionScrollbarOnLeft())
3506         elementSize.setWidth(0);
3507     IntPoint resizerPoint = IntPoint(elementSize);
3508     IntPoint localPoint = roundedIntPoint(absoluteToContents(absolutePoint));
3509     return localPoint - resizerPoint;
3510 }
3511
3512 bool RenderLayer::hasOverflowControls() const
3513 {
3514     return m_hBar || m_vBar || m_scrollCorner || renderer().style().resize() != Resize::None;
3515 }
3516
3517 void RenderLayer::positionOverflowControls(const IntSize& offsetFromRoot)
3518 {
3519     if (!m_hBar && !m_vBar && !canResize())
3520         return;
3521     
3522     RenderBox* box = renderBox();
3523     if (!box)
3524         return;
3525
3526     const IntRect borderBox = snappedIntRect(box->borderBoxRect());
3527     const IntRect& scrollCorner = scrollCornerRect();
3528     IntRect absBounds(borderBox.location() + offsetFromRoot, borderBox.size());
3529     if (m_vBar) {
3530         IntRect vBarRect = rectForVerticalScrollbar(borderBox);
3531         vBarRect.move(offsetFromRoot);
3532         m_vBar->setFrameRect(vBarRect);
3533     }
3534     
3535     if (m_hBar) {
3536         IntRect hBarRect = rectForHorizontalScrollbar(borderBox);
3537         hBarRect.move(offsetFromRoot);
3538         m_hBar->setFrameRect(hBarRect);
3539     }
3540     
3541     if (m_scrollCorner)
3542         m_scrollCorner->setFrameRect(scrollCorner);
3543     if (m_resizer)
3544         m_resizer->setFrameRect(resizerCornerRect(*this, borderBox));
3545
3546     if (isComposited())
3547         backing()->positionOverflowControlsLayers();
3548 }
3549
3550 int RenderLayer::scrollWidth() const
3551 {
3552     ASSERT(renderBox());
3553     if (m_scrollDimensionsDirty)
3554         const_cast<RenderLayer*>(this)->computeScrollDimensions();
3555     // FIXME: This should use snappedIntSize() instead with absolute coordinates.
3556     return m_scrollSize.width();
3557 }
3558
3559 int RenderLayer::scrollHeight() const
3560 {
3561     ASSERT(renderBox());
3562     if (m_scrollDimensionsDirty)
3563         const_cast<RenderLayer*>(this)->computeScrollDimensions();
3564     // FIXME: This should use snappedIntSize() instead with absolute coordinates.
3565     return m_scrollSize.height();
3566 }
3567
3568 LayoutUnit RenderLayer::overflowTop() const
3569 {
3570     RenderBox* box = renderBox();
3571     LayoutRect overflowRect(box->layoutOverflowRect());
3572     box->flipForWritingMode(overflowRect);
3573     return overflowRect.y();
3574 }
3575
3576 LayoutUnit RenderLayer::overflowBottom() const
3577 {
3578     RenderBox* box = renderBox();
3579     LayoutRect overflowRect(box->layoutOverflowRect());
3580     box->flipForWritingMode(overflowRect);
3581     return overflowRect.maxY();
3582 }
3583
3584 LayoutUnit RenderLayer::overflowLeft() const
3585 {
3586     RenderBox* box = renderBox();
3587     LayoutRect overflowRect(box->layoutOverflowRect());
3588     box->flipForWritingMode(overflowRect);
3589     return overflowRect.x();
3590 }
3591
3592 LayoutUnit RenderLayer::overflowRight() const
3593 {
3594     RenderBox* box = renderBox();
3595     LayoutRect overflowRect(box->layoutOverflowRect());
3596     box->flipForWritingMode(overflowRect);
3597     return overflowRect.maxX();
3598 }
3599
3600 void RenderLayer::computeScrollDimensions()
3601 {
3602     RenderBox* box = renderBox();
3603     ASSERT(box);
3604
3605     m_scrollDimensionsDirty = false;
3606
3607     m_scrollSize.setWidth(roundToInt(overflowRight() - overflowLeft()));
3608     m_scrollSize.setHeight(roundToInt(overflowBottom() - overflowTop()));
3609
3610     int scrollableLeftOverflow = roundToInt(overflowLeft() - box->borderLeft());
3611     if (shouldPlaceBlockDirectionScrollbarOnLeft())
3612         scrollableLeftOverflow -= verticalScrollbarWidth();
3613     int scrollableTopOverflow = roundToInt(overflowTop() - box->borderTop());
3614     setScrollOrigin(IntPoint(-scrollableLeftOverflow, -scrollableTopOverflow));
3615     
3616     m_hasCompositedScrollableOverflow = canUseCompositedScrolling() && (hasScrollableHorizontalOverflow() || hasScrollableVerticalOverflow());
3617 }
3618
3619 bool RenderLayer::hasScrollableHorizontalOverflow() const
3620 {
3621     return hasHorizontalOverflow() && renderBox()->scrollsOverflowX();
3622 }
3623
3624 bool RenderLayer::hasScrollableVerticalOverflow() const
3625 {
3626     return hasVerticalOverflow() && renderBox()->scrollsOverflowY();
3627 }
3628
3629 bool RenderLayer::hasHorizontalOverflow() const
3630 {
3631     ASSERT(!m_scrollDimensionsDirty);
3632
3633     return scrollWidth() > roundToInt(renderBox()->clientWidth());
3634 }
3635
3636 bool RenderLayer::hasVerticalOverflow() const
3637 {
3638     ASSERT(!m_scrollDimensionsDirty);
3639
3640     return scrollHeight() > roundToInt(renderBox()->clientHeight());
3641 }
3642
3643 static bool styleRequiresScrollbar(const RenderStyle& style, ScrollbarOrientation axis)
3644 {
3645     Overflow overflow = axis == ScrollbarOrientation::HorizontalScrollbar ? style.overflowX() : style.overflowY();
3646     bool overflowScrollActsLikeAuto = overflow == Overflow::Scroll && !style.hasPseudoStyle(PseudoId::Scrollbar) && ScrollbarTheme::theme().usesOverlayScrollbars();
3647     return overflow == Overflow::Scroll && !overflowScrollActsLikeAuto;
3648 }
3649
3650 static bool styleDefinesAutomaticScrollbar(const RenderStyle& style, ScrollbarOrientation axis)
3651 {
3652     Overflow overflow = axis == ScrollbarOrientation::HorizontalScrollbar ? style.overflowX() : style.overflowY();
3653     bool overflowScrollActsLikeAuto = overflow == Overflow::Scroll && !style.hasPseudoStyle(PseudoId::Scrollbar) && ScrollbarTheme::theme().usesOverlayScrollbars();
3654     return overflow == Overflow::Auto || overflowScrollActsLikeAuto;
3655 }
3656
3657 void RenderLayer::updateScrollbarsAfterLayout()
3658 {
3659     RenderBox* box = renderBox();
3660     ASSERT(box);
3661
3662     // List box parts handle the scrollbars by themselves so we have nothing to do.
3663     if (box->style().appearance() == ListboxPart)
3664         return;
3665
3666     bool hasHorizontalOverflow = this->hasHorizontalOverflow();
3667     bool hasVerticalOverflow = this->hasVerticalOverflow();
3668
3669     // If overflow requires a scrollbar, then we just need to enable or disable.
3670     if (m_hBar && styleRequiresScrollbar(renderer().style(), HorizontalScrollbar))
3671         m_hBar->setEnabled(hasHorizontalOverflow);
3672     if (m_vBar && styleRequiresScrollbar(renderer().style(), VerticalScrollbar))
3673         m_vBar->setEnabled(hasVerticalOverflow);
3674
3675     // Scrollbars with auto behavior may need to lay out again if scrollbars got added or removed.
3676     bool autoHorizontalScrollBarChanged = box->hasHorizontalScrollbarWithAutoBehavior() && (hasHorizontalScrollbar() != hasHorizontalOverflow);
3677     bool autoVerticalScrollBarChanged = box->hasVerticalScrollbarWithAutoBehavior() && (hasVerticalScrollbar() != hasVerticalOverflow);
3678
3679     if (autoHorizontalScrollBarChanged || autoVerticalScrollBarChanged) {
3680         if (box->hasHorizontalScrollbarWithAutoBehavior())
3681             setHasHorizontalScrollbar(hasHorizontalOverflow);
3682         if (box->hasVerticalScrollbarWithAutoBehavior())
3683             setHasVerticalScrollbar(hasVerticalOverflow);
3684
3685         updateSelfPaintingLayer();
3686
3687         renderer().repaint();
3688
3689         if (renderer().style().overflowX() == Overflow::Auto || renderer().style().overflowY() == Overflow::Auto) {
3690             if (!m_inOverflowRelayout) {
3691                 m_inOverflowRelayout = true;
3692                 renderer().setNeedsLayout(MarkOnlyThis);
3693                 if (is<RenderBlock>(renderer())) {
3694                     RenderBlock& block = downcast<RenderBlock>(renderer());
3695                     block.scrollbarsChanged(autoHorizontalScrollBarChanged, autoVerticalScrollBarChanged);
3696                     block.layoutBlock(true);
3697                 } else
3698                     renderer().layout();
3699                 m_inOverflowRelayout = false;
3700             }
3701         }
3702         
3703         RenderObject* parent = renderer().parent();
3704         if (parent && parent->isFlexibleBox() && renderer().isBox())
3705             downcast<RenderFlexibleBox>(parent)->clearCachedMainSizeForChild(*renderBox());
3706     }
3707
3708     // Set up the range (and page step/line step).
3709     if (m_hBar) {
3710         int clientWidth = roundToInt(box->clientWidth());
3711         int pageStep = Scrollbar::pageStep(clientWidth);
3712         m_hBar->setSteps(Scrollbar::pixelsPerLineStep(), pageStep);
3713         m_hBar->setProportion(clientWidth, m_scrollSize.width());
3714     }
3715     if (m_vBar) {
3716         int clientHeight = roundToInt(box->clientHeight());
3717         int pageStep = Scrollbar::pageStep(clientHeight);
3718         m_vBar->setSteps(Scrollbar::pixelsPerLineStep(), pageStep);
3719         m_vBar->setProportion(clientHeight, m_scrollSize.height());
3720     }
3721
3722     updateScrollableAreaSet(hasScrollableHorizontalOverflow() || hasScrollableVerticalOverflow());
3723 }
3724
3725 // This is called from layout code (before updateLayerPositions).
3726 void RenderLayer::updateScrollInfoAfterLayout()
3727 {
3728     RenderBox* box = renderBox();
3729     if (!box)
3730         return;
3731
3732     m_scrollDimensionsDirty = true;
3733     ScrollOffset originalScrollOffset = scrollOffset();
3734
3735     computeScrollDimensions();
3736
3737 #if ENABLE(CSS_SCROLL_SNAP)
3738     // FIXME: Ensure that offsets are also updated in case of programmatic style changes.
3739     // https://bugs.webkit.org/show_bug.cgi?id=135964
3740     updateSnapOffsets();
3741 #endif
3742
3743     if (!box->isHTMLMarquee() && !isRubberBandInProgress()) {
3744         // Layout may cause us to be at an invalid scroll position. In this case we need
3745         // to pull our scroll offsets back to the max (or push them up to the min).
3746         ScrollOffset clampedScrollOffset = clampScrollOffset(scrollOffset());
3747 #if PLATFORM(IOS_FAMILY)
3748         // FIXME: This looks wrong. The caret adjust mode should only be enabled on editing related entry points.
3749         // This code was added to fix an issue where the text insertion point would always be drawn on the right edge
3750         // of a text field whose content overflowed its bounds. See <rdar://problem/15579797> for more details.
3751         setAdjustForIOSCaretWhenScrolling(true);
3752 #endif
3753         if (clampedScrollOffset != scrollOffset())
3754             scrollToOffset(clampedScrollOffset);
3755
3756 #if PLATFORM(IOS_FAMILY)
3757         setAdjustForIOSCaretWhenScrolling(false);
3758 #endif
3759     }
3760
3761     updateScrollbarsAfterLayout();
3762
3763     if (originalScrollOffset != scrollOffset())
3764         scrollToOffsetWithoutAnimation(IntPoint(scrollOffset()));
3765
3766     if (isComposited()) {
3767         setNeedsCompositingGeometryUpdate();
3768         setNeedsCompositingConfigurationUpdate();
3769     }
3770
3771     if (canUseCompositedScrolling())
3772         setNeedsPostLayoutCompositingUpdate();
3773
3774     updateScrollSnapState();
3775 }
3776
3777 bool RenderLayer::overflowControlsIntersectRect(const IntRect& localRect) const
3778 {
3779     const IntRect borderBox = snappedIntRect(renderBox()->borderBoxRect());
3780
3781     if (rectForHorizontalScrollbar(borderBox).intersects(localRect))
3782         return true;
3783
3784     if (rectForVerticalScrollbar(borderBox).intersects(localRect))
3785         return true;
3786
3787     if (scrollCornerRect().intersects(localRect))
3788         return true;
3789     
3790     if (resizerCornerRect(*this, borderBox).intersects(localRect))
3791         return true;
3792
3793     return false;
3794 }
3795
3796 bool RenderLayer::showsOverflowControls() const
3797 {
3798 #if PLATFORM(IOS_FAMILY)
3799     // On iOS, the scrollbars are made in the UI process.
3800     return !canUseCompositedScrolling();
3801 #endif
3802
3803     return true;
3804 }
3805
3806 void RenderLayer::paintOverflowControls(GraphicsContext& context, const IntPoint& paintOffset, const IntRect& damageRect, bool paintingOverlayControls)
3807 {
3808     // Don't do anything if we have no overflow.
3809     if (!renderer().hasOverflowClip())
3810         return;
3811
3812     if (!showsOverflowControls())
3813         return;
3814
3815     // Overlay scrollbars paint in a second pass through the layer tree so that they will paint
3816     // on top of everything else. If this is the normal painting pass, paintingOverlayControls
3817     // will be false, and we should just tell the root layer that there are overlay scrollbars
3818     // that need to be painted. That will cause the second pass through the layer tree to run,
3819     // and we'll paint the scrollbars then. In the meantime, cache tx and ty so that the 
3820     // second pass doesn't need to re-enter the RenderTree to get it right.
3821     if (hasOverlayScrollbars() && !paintingOverlayControls) {
3822         m_cachedOverlayScrollbarOffset = paintOffset;
3823
3824         // It's not necessary to do the second pass if the scrollbars paint into layers.
3825         if ((m_hBar && layerForHorizontalScrollbar()) || (m_vBar && layerForVerticalScrollbar()))
3826             return;
3827         IntRect localDamgeRect = damageRect;
3828         localDamgeRect.moveBy(-paintOffset);
3829         if (!overflowControlsIntersectRect(localDamgeRect))
3830             return;
3831
3832         RenderLayer* paintingRoot = enclosingCompositingLayer();
3833         if (!paintingRoot)
3834             paintingRoot = renderer().view().layer();
3835
3836         paintingRoot->setContainsDirtyOverlayScrollbars(true);
3837         return;
3838     }
3839
3840     // This check is required to avoid painting custom CSS scrollbars twice.
3841     if (paintingOverlayControls && !hasOverlayScrollbars())
3842         return;
3843
3844     IntPoint adjustedPaintOffset = paintOffset;
3845     if (paintingOverlayControls)
3846         adjustedPaintOffset = m_cachedOverlayScrollbarOffset;
3847
3848     // Move the scrollbar widgets if necessary.  We normally move and resize widgets during layout, but sometimes
3849     // widgets can move without layout occurring (most notably when you scroll a document that
3850     // contains fixed positioned elements).
3851     positionOverflowControls(toIntSize(adjustedPaintOffset));
3852
3853     // Now that we're sure the scrollbars are in the right place, paint them.
3854     if (m_hBar && !layerForHorizontalScrollbar())
3855         m_hBar->paint(context, damageRect);
3856     if (m_vBar && !layerForVerticalScrollbar())
3857         m_vBar->paint(context, damageRect);
3858
3859     if (layerForScrollCorner())
3860         return;
3861
3862     // We fill our scroll corner with white if we have a scrollbar that doesn't run all the way up to the
3863     // edge of the box.
3864     paintScrollCorner(context, adjustedPaintOffset, damageRect);
3865     
3866     // Paint our resizer last, since it sits on top of the scroll corner.
3867     paintResizer(context, adjustedPaintOffset, damageRect);
3868 }
3869
3870 void RenderLayer::paintScrollCorner(GraphicsContext& context, const IntPoint& paintOffset, const IntRect& damageRect)
3871 {
3872     IntRect absRect = scrollCornerRect();
3873     absRect.moveBy(paintOffset);
3874     if (!absRect.intersects(damageRect))
3875         return;
3876
3877     if (context.invalidatingControlTints()) {
3878         updateScrollCornerStyle();
3879         return;
3880     }
3881
3882     if (m_scrollCorner) {
3883         m_scrollCorner->paintIntoRect(context, paintOffset, absRect);
3884         return;
3885     }
3886
3887     // We don't want to paint a corner if we have overlay scrollbars, since we need
3888     // to see what is behind it.
3889     if (!hasOverlayScrollbars())
3890         ScrollbarTheme::theme().paintScrollCorner(context, absRect);
3891 }
3892
3893 void RenderLayer::drawPlatformResizerImage(GraphicsContext& context, const LayoutRect& resizerCornerRect)
3894 {
3895     RefPtr<Image> resizeCornerImage;
3896     FloatSize cornerResizerSize;
3897     if (renderer().document().deviceScaleFactor() >= 2) {
3898         static NeverDestroyed<Image*> resizeCornerImageHiRes(&Image::loadPlatformResource("textAreaResizeCorner@2x").leakRef());
3899         resizeCornerImage = resizeCornerImageHiRes;
3900         cornerResizerSize = resizeCornerImage->size();
3901         cornerResizerSize.scale(0.5f);
3902     } else {
3903         static NeverDestroyed<Image*> resizeCornerImageLoRes(&Image::loadPlatformResource("textAreaResizeCorner").leakRef());
3904         resizeCornerImage = resizeCornerImageLoRes;
3905         cornerResizerSize = resizeCornerImage->size();
3906     }
3907
3908     if (shouldPlaceBlockDirectionScrollbarOnLeft()) {
3909         context.save();
3910         context.translate(resizerCornerRect.x() + cornerResizerSize.width(), resizerCornerRect.y() + resizerCornerRect.height() - cornerResizerSize.height());
3911         context.scale(FloatSize(-1.0, 1.0));
3912         if (resizeCornerImage)
3913             context.drawImage(*resizeCornerImage, FloatRect(FloatPoint(), cornerResizerSize));
3914         context.restore();
3915         return;
3916     }
3917     
3918     if (!resizeCornerImage)
3919         return;
3920     FloatRect imageRect = snapRectToDevicePixels(LayoutRect(resizerCornerRect.maxXMaxYCorner() - cornerResizerSize, cornerResizerSize), renderer().document().deviceScaleFactor());
3921     context.drawImage(*resizeCornerImage, imageRect);
3922 }
3923
3924 void RenderLayer::paintResizer(GraphicsContext& context, const LayoutPoint& paintOffset, const LayoutRect& damageRect)
3925 {
3926     if (renderer().style().resize() == Resize::None)
3927         return;
3928
3929     RenderBox* box = renderBox();
3930     ASSERT(box);
3931
3932     LayoutRect absRect = resizerCornerRect(*this, box->borderBoxRect());
3933     absRect.moveBy(paintOffset);
3934     if (!absRect.intersects(damageRect))
3935         return;
3936
3937     if (context.invalidatingControlTints()) {
3938         updateResizerStyle();
3939         return;
3940     }
3941     
3942     if (m_resizer) {
3943         m_resizer->paintIntoRect(context, paintOffset, absRect);
3944         return;
3945     }
3946
3947     drawPlatformResizerImage(context, absRect);
3948
3949     // Draw a frame around the resizer (1px grey line) if there are any scrollbars present.
3950     // Clipping will exclude the right and bottom edges of this frame.
3951     if (!hasOverlayScrollbars() && (m_vBar || m_hBar)) {
3952         GraphicsContextStateSaver stateSaver(context);
3953         context.clip(absRect);
3954         LayoutRect largerCorner = absRect;
3955         largerCorner.setSize(LayoutSize(largerCorner.width() + 1_lu, largerCorner.height() + 1_lu));
3956         context.setStrokeColor(Color(makeRGB(217, 217, 217)));
3957         context.setStrokeThickness(1.0f);
3958         context.setFillColor(Color::transparent);
3959         context.drawRect(snappedIntRect(largerCorner));
3960     }
3961 }
3962
3963 bool RenderLayer::isPointInResizeControl(const IntPoint& absolutePoint) const
3964 {
3965     if (!canResize())
3966         return false;
3967     
3968     RenderBox* box = renderBox();
3969     ASSERT(box);
3970
3971     IntPoint localPoint = roundedIntPoint(absoluteToContents(absolutePoint));
3972
3973     IntRect localBounds(IntPoint(), snappedIntRect(box->frameRect()).size());
3974     return resizerCornerRect(*this, localBounds).contains(localPoint);
3975 }
3976
3977 bool RenderLayer::hitTestOverflowControls(HitTestResult& result, const IntPoint& localPoint)
3978 {
3979     if (!m_hBar && !m_vBar && !canResize())
3980         return false;
3981
3982     RenderBox* box = renderBox();
3983     ASSERT(box);
3984     
3985     IntRect resizeControlRect;
3986     if (renderer().style().resize() != Resize::None) {
3987         resizeControlRect = snappedIntRect(resizerCornerRect(*this, box->borderBoxRect()));
3988         if (resizeControlRect.contains(localPoint))
3989             return true;
3990     }
3991
3992     int resizeControlSize = std::max(resizeControlRect.height(), 0);
3993
3994     // FIXME: We should hit test the m_scrollCorner and pass it back through the result.
3995
3996     if (m_vBar && m_vBar->shouldParticipateInHitTesting()) {
3997         LayoutRect vBarRect(verticalScrollbarStart(0, box->width()),
3998                             box->borderTop(),
3999                             m_vBar->width(),
4000                             box->height() - (box->borderTop() + box->borderBottom()) - (m_hBar ? m_hBar->height() : resizeControlSize));
4001         if (vBarRect.contains(localPoint)) {
4002             result.setScrollbar(m_vBar.get());
4003             return true;
4004         }
4005     }
4006
4007     resizeControlSize = std::max(resizeControlRect.width(), 0);
4008     if (m_hBar && m_hBar->shouldParticipateInHitTesting()) {
4009         LayoutRect hBarRect(horizontalScrollbarStart(0),
4010                             box->height() - box->borderBottom() - m_hBar->height(),
4011                             box->width() - (box->borderLeft() + box->borderRight()) - (m_vBar ? m_vBar->width() : resizeControlSize),
4012                             m_hBar->height());
4013         if (hBarRect.contains(localPoint)) {
4014             result.setScrollbar(m_hBar.get());
4015             return true;
4016         }
4017     }
4018
4019     return false;
4020 }
4021
4022 bool RenderLayer::scroll(ScrollDirection direction, ScrollGranularity granularity, float multiplier)
4023 {
4024     return ScrollableArea::scroll(direction, granularity, multiplier);
4025 }
4026
4027 void RenderLayer::paint(GraphicsContext& context, const LayoutRect& damageRect, const LayoutSize& subpixelOffset, OptionSet<PaintBehavior> paintBehavior, RenderObject* subtreePaintRoot, OptionSet<PaintLayerFlag> paintFlags, SecurityOriginPaintPolicy paintPolicy)
4028 {
4029     OverlapTestRequestMap overlapTestRequests;
4030
4031     LayerPaintingInfo paintingInfo(this, enclosingIntRect(damageRect), paintBehavior, subpixelOffset, subtreePaintRoot, &overlapTestRequests, paintPolicy == SecurityOriginPaintPolicy::AccessibleOriginOnly);
4032     paintLayer(context, paintingInfo, paintFlags);
4033
4034     for (auto& widget : overlapTestRequests.keys())
4035         widget->setOverlapTestResult(false);
4036 }
4037
4038 void RenderLayer::paintOverlayScrollbars(GraphicsContext& context, const LayoutRect& damageRect, OptionSet<PaintBehavior> paintBehavior, RenderObject* subtreePaintRoot)
4039 {
4040     if (!m_containsDirtyOverlayScrollbars)
4041         return;
4042
4043     LayerPaintingInfo paintingInfo(this, enclosingIntRect(damageRect), paintBehavior, LayoutSize(), subtreePaintRoot);
4044     paintLayer(context, paintingInfo, PaintLayerPaintingOverlayScrollbars);
4045
4046     m_containsDirtyOverlayScrollbars = false;
4047 }
4048
4049 void RenderLayer::clipToRect(GraphicsContext& context, const LayerPaintingInfo& paintingInfo, const ClipRect& clipRect, BorderRadiusClippingRule rule)
4050 {
4051     float deviceScaleFactor = renderer().document().deviceScaleFactor();
4052     bool needsClipping = !clipRect.isInfinite() && clipRect.rect() != paintingInfo.paintDirtyRect;
4053     if (needsClipping || clipRect.affectedByRadius())
4054         context.save();
4055
4056     if (needsClipping) {
4057         LayoutRect adjustedClipRect = clipRect.rect();
4058         adjustedClipRect.move(paintingInfo.subpixelOffset);
4059         auto snappedClipRect = snapRectToDevicePixels(adjustedClipRect, deviceScaleFactor);
4060         context.clip(snappedClipRect);
4061
4062         if (paintingInfo.eventRegionContext)
4063             paintingInfo.eventRegionContext->pushClip(enclosingIntRect(snappedClipRect));
4064     }
4065
4066     if (clipRect.affectedByRadius()) {
4067         // If the clip rect has been tainted by a border radius, then we have to walk up our layer chain applying the clips from
4068         // any layers with overflow. The condition for being able to apply these clips is that the overflow object be in our
4069         // containing block chain so we check that also.
4070         for (RenderLayer* layer = rule == IncludeSelfForBorderRadius ? this : parent(); layer; layer = layer->parent()) {
4071             if (layer->renderer().hasOverflowClip() && layer->renderer().style().hasBorderRadius() && ancestorLayerIsInContainingBlockChain(*layer)) {
4072                 LayoutRect adjustedClipRect = LayoutRect(toLayoutPoint(layer->offsetFromAncestor(paintingInfo.rootLayer, AdjustForColumns)), layer->size());
4073                 adjustedClipRect.move(paintingInfo.subpixelOffset);
4074                 FloatRoundedRect roundedRect = layer->renderer().style().getRoundedInnerBorderFor(adjustedClipRect).pixelSnappedRoundedRectForPainting(deviceScaleFactor);
4075                 if (roundedRect.intersectionIsRectangular(paintingInfo.paintDirtyRect))
4076                     context.clip(snapRectToDevicePixels(intersection(paintingInfo.paintDirtyRect, adjustedClipRect), deviceScaleFactor));
4077                 else
4078                     context.clipRoundedRect(roundedRect);
4079             }
4080             
4081             if (layer == paintingInfo.rootLayer)
4082                 break;
4083         }
4084     }
4085 }
4086
4087 void RenderLayer::restoreClip(GraphicsContext& context, const LayerPaintingInfo& paintingInfo, const ClipRect& clipRect)
4088 {
4089     if ((!clipRect.isInfinite() && clipRect.rect() != paintingInfo.paintDirtyRect) || clipRect.affectedByRadius()) {
4090         context.restore();
4091
4092         if (paintingInfo.eventRegionContext)
4093             paintingInfo.eventRegionContext->popClip();
4094     }
4095 }
4096
4097 static void performOverlapTests(OverlapTestRequestMap& overlapTestRequests, const RenderLayer* rootLayer, const RenderLayer* layer)
4098 {
4099     if (overlapTestRequests.isEmpty())
4100         return;
4101
4102     Vector<OverlapTestRequestClient*> overlappedRequestClients;
4103     LayoutRect boundingBox = layer->boundingBox(rootLayer, layer->offsetFromAncestor(rootLayer));
4104     for (auto& request : overlapTestRequests) {
4105         if (!boundingBox.intersects(request.value))
4106             continue;
4107
4108         request.key->setOverlapTestResult(true);
4109         overlappedRequestClients.append(request.key);
4110     }
4111     for (auto* client : overlappedRequestClients)
4112         overlapTestRequests.remove(client);
4113 }
4114
4115 static inline bool shouldDoSoftwarePaint(const RenderLayer* layer, bool paintingReflection)
4116 {
4117     return paintingReflection && !layer->has3DTransform();
4118 }
4119     
4120 static inline bool shouldSuppressPaintingLayer(RenderLayer* layer)
4121 {
4122     if (layer->renderer().style().isNotFinal() && !layer->isRenderViewLayer() && !layer->renderer().isDocumentElementRenderer())
4123         return true;
4124
4125     // Avoid painting all layers if the document is in a state where visual updates aren't allowed.
4126     // A full repaint will occur in Document::setVisualUpdatesAllowed(bool) if painting is suppressed here.
4127     if (!layer->renderer().document().visualUpdatesAllowed())
4128         return true;
4129
4130     return false;
4131 }
4132
4133 static inline bool paintForFixedRootBackground(const RenderLayer* layer, OptionSet<RenderLayer::PaintLayerFlag> paintFlags)
4134 {
4135     return layer->renderer().isDocumentElementRenderer() && (paintFlags & RenderLayer::PaintLayerPaintingRootBackgroundOnly);
4136 }
4137
4138 void RenderLayer::paintLayer(GraphicsContext& context, const LayerPaintingInfo& paintingInfo, OptionSet<PaintLayerFlag> paintFlags)
4139 {
4140     auto shouldContinuePaint = [&] () {
4141         return backing()->paintsIntoWindow()
4142             || backing()->paintsIntoCompositedAncestor()
4143             || shouldDoSoftwarePaint(this, paintFlags.contains(PaintLayerPaintingReflection))
4144             || paintForFixedRootBackground(this, paintFlags);
4145     };
4146
4147     auto paintsIntoDifferentCompositedDestination = [&]() {
4148         if (paintsIntoProvidedBacking())
4149             return true;
4150     
4151         if (isComposited() && !shouldContinuePaint())
4152             return true;
4153
4154         return false;
4155     };
4156     
4157     if (paintsIntoDifferentCompositedDestination()) {
4158         if (!context.performingPaintInvalidation() && !(paintingInfo.paintBehavior & PaintBehavior::FlattenCompositingLayers))
4159             return;
4160
4161         paintFlags.add(PaintLayerTemporaryClipRects);
4162     }
4163
4164     if (viewportConstrainedNotCompositedReason() == NotCompositedForBoundsOutOfView) {
4165         // Don't paint out-of-view viewport constrained layers (when doing prepainting) because they will never be visible
4166         // unless their position or viewport size is changed.
4167         ASSERT(renderer().isFixedPositioned());
4168         return;
4169     }
4170
4171     paintLayerWithEffects(context, paintingInfo, paintFlags);
4172 }
4173
4174 void RenderLayer::paintLayerWithEffects(GraphicsContext& context, const LayerPaintingInfo& paintingInfo, OptionSet<PaintLayerFlag> paintFlags)
4175 {
4176     // Non self-painting leaf layers don't need to be painted as their renderer() should properly paint itself.
4177     if (!isSelfPaintingLayer() && !hasSelfPaintingLayerDescendant())
4178         return;
4179
4180     if (shouldSuppressPaintingLayer(this))
4181         return;
4182
4183     // If this layer is totally invisible then there is nothing to paint.
4184     if (!renderer().opacity())
4185         return;
4186
4187     if (paintsWithTransparency(paintingInfo.paintBehavior))
4188         paintFlags.add(PaintLayerHaveTransparency);
4189
4190     // PaintLayerAppliedTransform is used in RenderReplica, to avoid applying the transform twice.
4191     if (paintsWithTransform(paintingInfo.paintBehavior) && !(paintFlags & PaintLayerAppliedTransform)) {
4192         TransformationMatrix layerTransform = renderableTransform(paintingInfo.paintBehavior);
4193         // If the transform can't be inverted, then don't paint anything.
4194         if (!layerTransform.isInvertible())
4195             return;
4196
4197         // If we have a transparency layer enclosing us and we are the root of a transform, then we need to establish the transparency
4198         // layer from the parent now, assuming there is a parent
4199         if (paintFlags & PaintLayerHaveTransparency) {
4200             if (parent())
4201                 parent()->beginTransparencyLayers(context, paintingInfo, paintingInfo.paintDirtyRect);
4202             else
4203                 beginTransparencyLayers(context, paintingInfo, paintingInfo.paintDirtyRect);
4204         }
4205
4206         if (enclosingPaginationLayer(ExcludeCompositedPaginatedLayers)) {
4207             paintTransformedLayerIntoFragments(context, paintingInfo, paintFlags);
4208             return;
4209         }
4210
4211         // Make sure the parent's clip rects have been calculated.
4212         ClipRect clipRect = paintingInfo.paintDirtyRect;
4213         if (parent()) {
4214             ClipRectsContext clipRectsContext(paintingInfo.rootLayer, (paintFlags & PaintLayerTemporaryClipRects) ? TemporaryClipRects : PaintingClipRects,
4215                 IgnoreOverlayScrollbarSize, (paintFlags & PaintLayerPaintingOverflowContents) ? IgnoreOverflowClip : RespectOverflowClip);
4216             clipRect = backgroundClipRect(clipRectsContext);
4217             clipRect.intersect(paintingInfo.paintDirtyRect);
4218         
4219             // Push the parent coordinate space's clip.
4220             parent()->clipToRect(context, paintingInfo, clipRect);
4221         }
4222
4223         paintLayerByApplyingTransform(context, paintingInfo, paintFlags);
4224
4225         // Restore the clip.
4226         if (parent())
4227             parent()->restoreClip(context, paintingInfo, clipRect);
4228
4229         return;
4230     }
4231     
4232     paintLayerContentsAndReflection(context, paintingInfo, paintFlags);
4233 }
4234
4235 void RenderLayer::paintLayerContentsAndReflection(GraphicsContext& context, const LayerPaintingInfo& paintingInfo, OptionSet<PaintLayerFlag> paintFlags)
4236 {
4237     ASSERT(isSelfPaintingLayer() || hasSelfPaintingLayerDescendant());
4238
4239     auto localPaintFlags = paintFlags - PaintLayerAppliedTransform;
4240
4241     // Paint the reflection first if we have one.
4242     if (m_reflection && !m_paintingInsideReflection) {
4243         // Mark that we are now inside replica painting.
4244         m_paintingInsideReflection = true;
4245         reflectionLayer()->paintLayer(context, paintingInfo, localPaintFlags | PaintLayerPaintingReflection);
4246         m_paintingInsideReflection = false;
4247     }
4248
4249     localPaintFlags.add(paintLayerPaintingCompositingAllPhasesFlags());
4250     paintLayerContents(context, paintingInfo, localPaintFlags);
4251 }
4252
4253 bool RenderLayer::setupFontSubpixelQuantization(GraphicsContext& context, bool& didQuantizeFonts)
4254 {
4255     if (context.paintingDisabled())
4256         return false;
4257
4258     bool scrollingOnMainThread = true;
4259 #if ENABLE(ASYNC_SCROLLING)
4260     if (ScrollingCoordinator* scrollingCoordinator = page().scrollingCoordinator())
4261         scrollingOnMainThread = scrollingCoordinator->shouldUpdateScrollLayerPositionSynchronously(renderer().view().frameView());
4262 #endif
4263
4264     // FIXME: We shouldn't have to disable subpixel quantization for overflow clips or subframes once we scroll those
4265     // things on the scrolling thread.
4266     bool contentsScrollByPainting = (renderer().hasOverflowClip() && !usesCompositedScrolling()) || (renderer().frame().ownerElement());
4267     bool isZooming = !page().chrome().client().hasStablePageScaleFactor();
4268     if (scrollingOnMainThread || contentsScrollByPainting || isZooming) {
4269         didQuantizeFonts = context.shouldSubpixelQuantizeFonts();
4270         context.setShouldSubpixelQuantizeFonts(false);
4271         return true;
4272     }
4273     return false;
4274 }
4275
4276 static inline LayoutRect computeReferenceBox(const RenderObject& renderer, const CSSBoxType& boxType, const LayoutSize& offsetFromRoot, const LayoutRect& rootRelativeBounds)
4277 {
4278     // FIXME: Support different reference boxes for inline content.
4279     // https://bugs.webkit.org/show_bug.cgi?id=129047
4280     if (!renderer.isBox())
4281         return rootRelativeBounds;
4282
4283     LayoutRect referenceBox;
4284     const auto& box = downcast<RenderBox>(renderer);
4285     switch (boxType) {
4286     case CSSBoxType::ContentBox:
4287     case CSSBoxType::FillBox:
4288         referenceBox = box.contentBoxRect();
4289         referenceBox.move(offsetFromRoot);
4290         break;
4291     case CSSBoxType::PaddingBox:
4292         referenceBox = box.paddingBoxRect();
4293         referenceBox.move(offsetFromRoot);
4294         break;
4295     case CSSBoxType::MarginBox:
4296         referenceBox = box.marginBoxRect();
4297         referenceBox.move(offsetFromRoot);
4298         break;
4299     // stroke-box, view-box compute to border-box for HTML elements.
4300     case CSSBoxType::StrokeBox:
4301     case CSSBoxType::ViewBox:
4302     case CSSBoxType::BorderBox:
4303     case CSSBoxType::BoxMissing:
4304         referenceBox = box.borderBoxRect();
4305         referenceBox.move(offsetFromRoot);
4306         break;
4307     }
4308
4309     return referenceBox;
4310 }
4311
4312 Path RenderLayer::computeClipPath(const LayoutSize& offsetFromRoot, LayoutRect& rootRelativeBounds, WindRule& windRule) const
4313 {
4314     const RenderStyle& style = renderer().style();
4315     float deviceSaleFactor = renderer().document().deviceScaleFactor();
4316
4317     if (is<ShapeClipPathOperation>(*style.clipPath())) {
4318         auto& clipPath = downcast<ShapeClipPathOperation>(*style.clipPath());
4319         FloatRect referenceBox = snapRectToDevicePixels(computeReferenceBox(renderer(), clipPath.referenceBox(), offsetFromRoot, rootRelativeBounds), deviceSaleFactor);
4320
4321         windRule = clipPath.windRule();
4322         return clipPath.pathForReferenceRect(referenceBox);
4323     }
4324     
4325     if (is<BoxClipPathOperation>(*style.clipPath()) && is<RenderBox>(renderer())) {
4326
4327         auto& clipPath = downcast<BoxClipPathOperation>(*style.clipPath());
4328
4329         FloatRoundedRect shapeRect = computeRoundedRectForBoxShape(clipPath.referenceBox(), downcast<RenderBox>(renderer())).pixelSnappedRoundedRectForPainting(deviceSaleFactor);
4330         shapeRect.move(offsetFromRoot);
4331
4332         windRule = WindRule::NonZero;
4333         return clipPath.pathForReferenceRect(shapeRect);
4334     }
4335     
4336     return Path();
4337 }
4338
4339 bool RenderLayer::setupClipPath(GraphicsContext& context, const LayerPaintingInfo& paintingInfo, const LayoutSize& offsetFromRoot, LayoutRect& rootRelativeBounds, bool& rootRelativeBoundsComputed)
4340 {
4341     if (!renderer().hasClipPath() || context.paintingDisabled())
4342         return false;
4343
4344     if (!rootRelativeBoundsComputed) {
4345         rootRelativeBounds = calculateLayerBounds(paintingInfo.rootLayer, offsetFromRoot, { });
4346         rootRelativeBoundsComputed = true;
4347     }
4348
4349     // SVG elements get clipped in SVG code.
4350     if (is<RenderSVGRoot>(renderer()))
4351         return false;
4352
4353     auto& style = renderer().style();
4354     LayoutSize paintingOffsetFromRoot = LayoutSize(snapSizeToDevicePixel(offsetFromRoot + paintingInfo.subpixelOffset, LayoutPoint(), renderer().document().deviceScaleFactor()));
4355     ASSERT(style.clipPath());
4356     if (is<ShapeClipPathOperation>(*style.clipPath()) || (is<BoxClipPathOperation>(*style.clipPath()) && is<RenderBox>(renderer()))) {
4357         WindRule windRule;
4358         Path path = computeClipPath(paintingOffsetFromRoot, rootRelativeBounds, windRule);
4359         context.save();
4360         context.clipPath(path, windRule);
4361         return true;
4362     }
4363
4364     if (style.clipPath()->type() == ClipPathOperation::Reference) {
4365         ReferenceClipPathOperation* referenceClipPathOperation = static_cast<ReferenceClipPathOperation*>(style.clipPath());
4366         Element* element = renderer().document().getElementById(referenceClipPathOperation->fragment());
4367         if (element && element->renderer() && is<RenderSVGResourceClipper>(element->renderer())) {
4368             context.save();
4369             float deviceSaleFactor = renderer().document().deviceScaleFactor();
4370             FloatRect referenceBox = snapRectToDevicePixels(computeReferenceBox(renderer(), CSSBoxType::ContentBox, paintingOffsetFromRoot, rootRelativeBounds), deviceSaleFactor);
4371             FloatPoint offset {referenceBox.location()};
4372             context.translate(offset);
4373             FloatRect svgReferenceBox {FloatPoint(), referenceBox.size()};
4374             downcast<RenderSVGResourceClipper>(*element->renderer()).applyClippingToContext(renderer(), svgReferenceBox, paintingInfo.paintDirtyRect, context);
4375             context.translate(FloatPoint(-offset.x(), -offset.y()));
4376             return true;
4377         }
4378     }
4379
4380     return false;
4381 }
4382
4383 RenderLayerFilters* RenderLayer::filtersForPainting(GraphicsContext& context, OptionSet<PaintLayerFlag> paintFlags) const
4384 {
4385     if (context.paintingDisabled())
4386         return nullptr;
4387
4388     if (paintFlags & PaintLayerPaintingOverlayScrollbars)
4389         return nullptr;
4390
4391     if (!paintsWithFilters())
4392         return nullptr;
4393
4394     if (m_filters && m_filters->filter())
4395         return m_filters.get();
4396
4397     return nullptr;
4398 }
4399
4400 GraphicsContext* RenderLayer::setupFilters(GraphicsContext& destinationContext, LayerPaintingInfo& paintingInfo, OptionSet<PaintLayerFlag> paintFlags, const LayoutSize& offsetFromRoot, LayoutRect& rootRelativeBounds, bool& rootRelativeBoundsComputed)
4401 {
4402     auto* paintingFilters = filtersForPainting(destinationContext, paintFlags);
4403     if (!paintingFilters)
4404         return nullptr;
4405
4406     LayoutRect filterRepaintRect = paintingFilters->dirtySourceRect();
4407     filterRepaintRect.move(offsetFromRoot);
4408
4409     if (!rootRelativeBoundsComputed) {
4410         rootRelativeBounds = calculateLayerBounds(paintingInfo.rootLayer, offsetFromRoot, { });
4411         rootRelativeBoundsComputed = true;
4412     }
4413
4414     GraphicsContext* filterContext = paintingFilters->beginFilterEffect(destinationContext, enclosingIntRect(rootRelativeBounds), enclosingIntRect(paintingInfo.paintDirtyRect), enclosingIntRect(filterRepaintRect));
4415     if (!filterContext)
4416         return nullptr;
4417
4418     paintingInfo.paintDirtyRect = paintingFilters->repaintRect();
4419
4420     // If the filter needs the full source image, we need to avoid using the clip rectangles.
4421     // Otherwise, if for example this layer has overflow:hidden, a drop shadow will not compute correctly.
4422     // Note that we will still apply the clipping on the final rendering of the filter.
4423     paintingInfo.clipToDirtyRect = !paintingFilters->hasFilterThatMovesPixels();
4424
4425     paintingInfo.requireSecurityOriginAccessForWidgets = paintingFilters->hasFilterThatShouldBeRestrictedBySecurityOrigin();
4426
4427     return filterContext;
4428 }
4429
4430 void RenderLayer::applyFilters(GraphicsContext& originalContext, const LayerPaintingInfo& paintingInfo, const LayerFragments& layerFragments)
4431 {
4432     // FIXME: Handle more than one fragment.
4433     ClipRect backgroundRect = layerFragments.isEmpty() ? ClipRect() : layerFragments[0].backgroundRect;
4434     clipToRect(originalContext, paintingInfo, backgroundRect);
4435     m_filters->applyFilterEffect(originalContext);
4436     restoreClip(originalContext, paintingInfo, backgroundRect);
4437 }
4438
4439 void RenderLayer::paintLayerContents(GraphicsContext& context, const LayerPaintingInfo& paintingInfo, OptionSet<PaintLayerFlag> paintFlags)
4440 {
4441     ASSERT(isSelfPaintingLayer() || hasSelfPaintingLayerDescendant());
4442
4443     auto localPaintFlags = paintFlags - PaintLayerAppliedTransform;
4444     bool haveTransparency = localPaintFlags.contains(PaintLayerHaveTransparency);
4445     bool isSelfPaintingLayer = this->isSelfPaintingLayer();
4446     bool isPaintingOverlayScrollbars = paintFlags.contains(PaintLayerPaintingOverlayScrollbars);
4447     bool isPaintingScrollingContent = paintFlags.contains(PaintLayerPaintingCompositingScrollingPhase);
4448     bool isPaintingCompositedForeground = paintFlags.contains(PaintLayerPaintingCompositingForegroundPhase);
4449     bool isPaintingCompositedBackground = paintFlags.contains(PaintLayerPaintingCompositingBackgroundPhase);
4450     bool isPaintingOverflowContents = paintFlags.contains(PaintLayerPaintingOverflowContents);
4451     bool isCollectingEventRegion = paintFlags.contains(PaintLayerCollectingEventRegion);
4452     // Outline always needs to be painted even if we have no visible content. Also,
4453     // the outline is painted in the background phase during composited scrolling.
4454     // If it were painted in the foreground phase, it would move with the scrolled
4455     // content. When not composited scrolling, the outline is painted in the
4456     // foreground phase. Since scrolled contents are moved by repainting in this
4457     // case, the outline won't get 'dragged along'.
4458     bool shouldPaintOutline = isSelfPaintingLayer && !isPaintingOverlayScrollbars && !isCollectingEventRegion
4459         && (renderer().view().printing() || renderer().view().hasRenderersWithOutline())
4460         && ((isPaintingScrollingContent && isPaintingCompositedBackground)
4461         || (!isPaintingScrollingContent && isPaintingCompositedForeground));
4462     bool shouldPaintContent = m_hasVisibleContent && isSelfPaintingLayer && !isPaintingOverlayScrollbars && !isCollectingEventRegion;
4463
4464     if (localPaintFlags & PaintLayerPaintingRootBackgroundOnly && !renderer().isRenderView() && !renderer().isDocumentElementRenderer())
4465         return;
4466
4467     updateLayerListsIfNeeded();
4468
4469     LayoutSize offsetFromRoot = offsetFromAncestor(paintingInfo.rootLayer);
4470     LayoutRect rootRelativeBounds;
4471     bool rootRelativeBoundsComputed = false;
4472
4473     // FIXME: We shouldn't have to disable subpixel quantization for overflow clips or subframes once we scroll those
4474     // things on the scrolling thread.
4475     bool didQuantizeFonts = true;
4476     bool needToAdjustSubpixelQuantization = setupFontSubpixelQuantization(context, didQuantizeFonts);
4477
4478     // Apply clip-path to context.
4479     LayoutSize columnAwareOffsetFromRoot = offsetFromRoot;
4480     if (renderer().enclosingFragmentedFlow() && (renderer().hasClipPath() || filtersForPainting(context, paintFlags)))
4481         columnAwareOffsetFromRoot = toLayoutSize(convertToLayerCoords(paintingInfo.rootLayer, LayoutPoint(), AdjustForColumns));
4482
4483     bool hasClipPath = false;
4484     if (shouldApplyClipPath(paintingInfo.paintBehavior, localPaintFlags))
4485         hasClipPath = setupClipPath(context, paintingInfo, columnAwareOffsetFromRoot, rootRelativeBounds, rootRelativeBoundsComputed);
4486
4487     bool selectionAndBackgroundsOnly = paintingInfo.paintBehavior.contains(PaintBehavior::SelectionAndBackgroundsOnly);
4488     bool selectionOnly = paintingInfo.paintBehavior.contains(PaintBehavior::SelectionOnly);
4489
4490     SinglePaintFrequencyTracking singlePaintFrequencyTracking(m_paintFrequencyTracker, shouldPaintContent);
4491
4492     LayerFragments layerFragments;
4493     RenderObject* subtreePaintRootForRenderer = nullptr;
4494
4495     { // Scope for filter-related state changes.
4496         LayerPaintingInfo localPaintingInfo(paintingInfo);
4497         GraphicsContext* filterContext = setupFilters(context, localPaintingInfo, paintFlags, columnAwareOffsetFromRoot, rootRelativeBounds, rootRelativeBoundsComputed);
4498         if (filterContext && haveTransparency) {
4499             // If we have a filter and transparency, we have to eagerly start a transparency layer here, rather than risk a child layer lazily starts one with the wrong context.
4500             beginTransparencyLayers(context, localPaintingInfo, paintingInfo.paintDirtyRect);
4501         }
4502         GraphicsContext& currentContext = filterContext ? *filterContext : context;
4503
4504         // If this layer's renderer is a child of the subtreePaintRoot, we render unconditionally, which
4505         // is done by passing a nil subtreePaintRoot down to our renderer (as if no subtreePaintRoot was ever set).
4506         // Otherwise, our renderer tree may or may not contain the subtreePaintRoot root, so we pass that root along
4507         // so it will be tested against as we descend through the renderers.
4508         if (localPaintingInfo.subtreePaintRoot && !renderer().isDescendantOf(localPaintingInfo.subtreePaintRoot))
4509             subtreePaintRootForRenderer = localPaintingInfo.subtreePaintRoot;
4510
4511         if (localPaintingInfo.overlapTestRequests && isSelfPaintingLayer)
4512             performOverlapTests(*localPaintingInfo.overlapTestRequests, localPaintingInfo.rootLayer, this);
4513
4514         OptionSet<PaintBehavior> paintBehavior = PaintBehavior::Normal;
4515         if (localPaintFlags & PaintLayerPaintingSkipRootBackground)
4516             paintBehavior.add(PaintBehavior::SkipRootBackground);
4517         else if (localPaintFlags & PaintLayerPaintingRootBackgroundOnly)
4518             paintBehavior.add(PaintBehavior::RootBackgroundOnly);
4519
4520         if (paintingInfo.paintBehavior & PaintBehavior::FlattenCompositingLayers)
4521             paintBehavior.add(PaintBehavior::FlattenCompositingLayers);
4522         
4523         if (paintingInfo.paintBehavior & PaintBehavior::Snapshotting)
4524             paintBehavior.add(PaintBehavior::Snapshotting);
4525         
4526         if ((paintingInfo.paintBehavior & PaintBehavior::TileFirstPaint) && isRenderViewLayer())
4527             paintBehavior.add(PaintBehavior::TileFirstPaint);
4528
4529         if (paintingInfo.paintBehavior & PaintBehavior::ExcludeSelection)
4530             paintBehavior.add(PaintBehavior::ExcludeSelection);
4531
4532         LayoutRect paintDirtyRect = localPaintingInfo.paintDirtyRect;
4533         if (shouldPaintContent || shouldPaintOutline || isPaintingOverlayScrollbars || isCollectingEventRegion) {
4534             // Collect the fragments. This will compute the clip rectangles and paint offsets for each layer fragment, as well as whether or not the content of each
4535             // fragment should paint. If the parent's filter dictates full repaint to ensure proper filter effect,
4536             // use the overflow clip as dirty rect, instead of no clipping. It maintains proper clipping for overflow::scroll.
4537             if (!localPaintingInfo.clipToDirtyRect && renderer().hasOverflowClip()) {
4538                 // We can turn clipping back by requesting full repaint for the overflow area.
4539                 localPaintingInfo.clipToDirtyRect = true;
4540                 paintDirtyRect = clipRectRelativeToAncestor(localPaintingInfo.rootLayer, offsetFromRoot, LayoutRect::infiniteRect());
4541             }
4542             collectFragments(layerFragments, localPaintingInfo.rootLayer, paintDirtyRect, ExcludeCompositedPaginatedLayers,
4543                 (localPaintFlags & PaintLayerTemporaryClipRects) ? TemporaryClipRects : PaintingClipRects, IgnoreOverlayScrollbarSize,
4544                 (isPaintingOverflowContents) ? IgnoreOverflowClip : RespectOverflowClip, offsetFromRoot);
4545             updatePaintingInfoForFragments(layerFragments, localPaintingInfo, localPaintFlags, shouldPaintContent, offsetFromRoot);
4546         }
4547         
4548         if (isPaintingCompositedBackground) {
4549             // Paint only the backgrounds for all of the fragments of the layer.
4550             if (shouldPaintContent && !selectionOnly) {
4551                 paintBackgroundForFragments(layerFragments, currentContext, context, paintingInfo.paintDirtyRect, haveTransparency,
4552                     localPaintingInfo, paintBehavior, subtreePaintRootForRenderer);
4553             }
4554         }
4555
4556         // Now walk the sorted list of children with negative z-indices.
4557         if ((isPaintingScrollingContent && isPaintingOverflowContents) || (!isPaintingScrollingContent && isPaintingCompositedBackground))
4558             paintList(negativeZOrderLayers(), currentContext, localPaintingInfo, localPaintFlags);
4559         
4560         if (isPaintingCompositedForeground) {
4561             if (shouldPaintContent) {
4562                 paintForegroundForFragments(layerFragments, currentContext, context, paintingInfo.paintDirtyRect, haveTransparency,
4563                     localPaintingInfo, paintBehavior, subtreePaintRootForRenderer);
4564             }
4565         }
4566
4567         if (isCollectingEventRegion)
4568             collectEventRegionForFragments(layerFragments, currentContext, localPaintingInfo);
4569
4570         if (shouldPaintOutline)
4571             paintOutlineForFragments(layerFragments, currentContext, localPaintingInfo, paintBehavior, subtreePaintRootForRenderer);
4572