Unify hasTouchScrollableOverflow/needsCompositedScrolling concepts
[WebKit-https.git] / Source / WebCore / rendering / RenderLayerBacking.cpp
1 /*
2  * Copyright (C) 2009, 2010, 2011 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27
28 #include "RenderLayerBacking.h"
29
30 #include "CSSAnimationController.h"
31 #include "CanvasRenderingContext.h"
32 #include "CSSPropertyNames.h"
33 #include "CachedImage.h"
34 #include "Chrome.h"
35 #include "FilterEffectRenderer.h"
36 #include "FrameView.h"
37 #include "GraphicsContext.h"
38 #include "GraphicsLayer.h"
39 #include "HTMLBodyElement.h"
40 #include "HTMLCanvasElement.h"
41 #include "HTMLIFrameElement.h"
42 #include "HTMLMediaElement.h"
43 #include "HTMLNames.h"
44 #include "HTMLPlugInElement.h"
45 #include "InspectorInstrumentation.h"
46 #include "KeyframeList.h"
47 #include "Logging.h"
48 #include "MainFrame.h"
49 #include "Page.h"
50 #include "PluginViewBase.h"
51 #include "ProgressTracker.h"
52 #include "RenderFlowThread.h"
53 #include "RenderHTMLCanvas.h"
54 #include "RenderIFrame.h"
55 #include "RenderImage.h"
56 #include "RenderLayerCompositor.h"
57 #include "RenderEmbeddedObject.h"
58 #include "RenderMedia.h"
59 #include "RenderNamedFlowFragment.h"
60 #include "RenderRegion.h"
61 #include "RenderVideo.h"
62 #include "RenderView.h"
63 #include "ScrollingCoordinator.h"
64 #include "Settings.h"
65 #include "StyleResolver.h"
66 #include "TiledBacking.h"
67
68 #if PLATFORM(IOS)
69 #include "RuntimeApplicationChecks.h"
70 #endif
71
72 namespace WebCore {
73
74 using namespace HTMLNames;
75
76 CanvasCompositingStrategy canvasCompositingStrategy(const RenderObject& renderer)
77 {
78     ASSERT(renderer.isCanvas());
79     
80     const HTMLCanvasElement* canvas = downcast<HTMLCanvasElement>(renderer.node());
81     CanvasRenderingContext* context = canvas->renderingContext();
82     if (!context || !context->isAccelerated())
83         return UnacceleratedCanvas;
84     
85     if (context->isGPUBased())
86         return CanvasAsLayerContents;
87
88 #if ENABLE(ACCELERATED_2D_CANVAS)
89     return CanvasAsLayerContents;
90 #else
91     return CanvasPaintedToLayer; // On Mac and iOS we paint accelerated canvases into their layers.
92 #endif
93 }
94
95 // This acts as a cache of what we know about what is painting into this RenderLayerBacking.
96 class PaintedContentsInfo {
97 public:
98     enum class ContentsTypeDetermination {
99         Unknown,
100         SimpleContainer,
101         DirectlyCompositedImage,
102         Painted
103     };
104
105     PaintedContentsInfo(RenderLayerBacking& inBacking)
106         : m_backing(inBacking)
107     {
108     }
109     
110     void setWantsSubpixelAntialiasedTextState(bool wantsSubpixelAntialiasedTextState)
111     {
112         m_subpixelAntialiasedText = wantsSubpixelAntialiasedTextState ? RequestState::Unknown : RequestState::DontCare;
113     }
114
115     RequestState paintsBoxDecorationsDetermination();
116     bool paintsBoxDecorations()
117     {
118         RequestState state = paintsBoxDecorationsDetermination();
119         return state == RequestState::True || state == RequestState::Undetermined;
120     }
121
122     RequestState paintsContentDetermination();
123     bool paintsContent()
124     {
125         RequestState state = paintsContentDetermination();
126         return state == RequestState::True || state == RequestState::Undetermined;
127     }
128
129     RequestState paintsSubpixelAntialiasedTextDetermination();
130     bool paintsSubpixelAntialiasedText()
131     {
132         RequestState state = paintsSubpixelAntialiasedTextDetermination();
133         return state == RequestState::True || state == RequestState::Undetermined;
134     }
135
136     ContentsTypeDetermination contentsTypeDetermination();
137     bool isSimpleContainer()
138     {
139         return contentsTypeDetermination() == ContentsTypeDetermination::SimpleContainer;
140     }
141
142     bool isDirectlyCompositedImage()
143     {
144         return contentsTypeDetermination() == ContentsTypeDetermination::DirectlyCompositedImage;
145     }
146
147     RenderLayerBacking& m_backing;
148     RequestState m_boxDecorations { RequestState::Unknown };
149     RequestState m_content { RequestState::Unknown };
150     RequestState m_subpixelAntialiasedText { RequestState::DontCare };
151
152     ContentsTypeDetermination m_contentsType { ContentsTypeDetermination::Unknown };
153 };
154
155 RequestState PaintedContentsInfo::paintsBoxDecorationsDetermination()
156 {
157     if (m_boxDecorations != RequestState::Unknown)
158         return m_boxDecorations;
159
160     m_boxDecorations = m_backing.paintsBoxDecorations() ? RequestState::True : RequestState::False;
161     return m_boxDecorations;
162 }
163
164 RequestState PaintedContentsInfo::paintsContentDetermination()
165 {
166     if (m_content != RequestState::Unknown && m_subpixelAntialiasedText != RequestState::Unknown)
167         return m_content;
168
169     RenderLayer::PaintedContentRequest contentRequest;
170     if (m_subpixelAntialiasedText == RequestState::Unknown)
171         contentRequest.hasSubpixelAntialiasedText = RequestState::Unknown;
172
173     m_content = m_backing.paintsContent(contentRequest) ? RequestState::True : RequestState::False;
174
175     if (m_subpixelAntialiasedText == RequestState::Unknown)
176         m_subpixelAntialiasedText = contentRequest.hasSubpixelAntialiasedText;
177
178     return m_content;
179 }
180
181 RequestState PaintedContentsInfo::paintsSubpixelAntialiasedTextDetermination()
182 {
183     if (m_subpixelAntialiasedText != RequestState::Unknown)
184         return m_subpixelAntialiasedText;
185
186     paintsContentDetermination();
187
188     return m_subpixelAntialiasedText;
189 }
190
191 PaintedContentsInfo::ContentsTypeDetermination PaintedContentsInfo::contentsTypeDetermination()
192 {
193     if (m_contentsType != ContentsTypeDetermination::Unknown)
194         return m_contentsType;
195
196     if (m_backing.isSimpleContainerCompositingLayer(*this))
197         m_contentsType = ContentsTypeDetermination::SimpleContainer;
198     else if (m_backing.isDirectlyCompositedImage())
199         m_contentsType = ContentsTypeDetermination::DirectlyCompositedImage;
200     else
201         m_contentsType = ContentsTypeDetermination::Painted;
202
203     return m_contentsType;
204 }
205
206
207 RenderLayerBacking::RenderLayerBacking(RenderLayer& layer)
208     : m_owningLayer(layer)
209 {
210     if (layer.isRootLayer()) {
211         m_isMainFrameRenderViewLayer = renderer().frame().isMainFrame();
212         m_isMainFrameLayerWithTiledBacking = renderer().page().chrome().client().shouldUseTiledBackingForFrameView(renderer().view().frameView());
213     }
214     
215     createPrimaryGraphicsLayer();
216
217     if (TiledBacking* tiledBacking = this->tiledBacking()) {
218         tiledBacking->setIsInWindow(renderer().page().isInWindow());
219
220         if (m_isMainFrameLayerWithTiledBacking) {
221             tiledBacking->setScrollingPerformanceLoggingEnabled(renderer().settings().scrollingPerformanceLoggingEnabled());
222             adjustTiledBackingCoverage();
223         }
224     }
225 }
226
227 RenderLayerBacking::~RenderLayerBacking()
228 {
229     updateAncestorClippingLayer(false);
230     updateChildClippingStrategy(false);
231     updateDescendantClippingLayer(false);
232     updateOverflowControlsLayers(false, false, false);
233     updateForegroundLayer(false);
234     updateBackgroundLayer(false);
235     updateMaskingLayer(false, false);
236     updateScrollingLayers(false);
237     detachFromScrollingCoordinator(Scrolling | ViewportConstrained);
238     destroyGraphicsLayers();
239 }
240
241 void RenderLayerBacking::willDestroyLayer(const GraphicsLayer* layer)
242 {
243     if (layer && layer->type() == GraphicsLayer::Type::Normal && layer->tiledBacking())
244         compositor().layerTiledBackingUsageChanged(layer, false);
245 }
246
247 std::unique_ptr<GraphicsLayer> RenderLayerBacking::createGraphicsLayer(const String& name, GraphicsLayer::Type layerType)
248 {
249     auto* graphicsLayerFactory = renderer().page().chrome().client().graphicsLayerFactory();
250
251     std::unique_ptr<GraphicsLayer> graphicsLayer = GraphicsLayer::create(graphicsLayerFactory, *this, layerType);
252
253     graphicsLayer->setName(name);
254
255 #if PLATFORM(COCOA) && USE(CA)
256     graphicsLayer->setAcceleratesDrawing(compositor().acceleratedDrawingEnabled());
257     graphicsLayer->setUsesDisplayListDrawing(compositor().displayListDrawingEnabled());
258 #endif
259     
260     return graphicsLayer;
261 }
262
263 void RenderLayerBacking::setUsesDisplayListDrawing(bool usesDisplayListDrawing)
264 {
265     // Note that this only affects the primary layer.
266     if (usesDisplayListDrawing == m_graphicsLayer->usesDisplayListDrawing())
267         return;
268
269     m_graphicsLayer->setUsesDisplayListDrawing(usesDisplayListDrawing);
270     if (m_graphicsLayer->drawsContent())
271         m_graphicsLayer->setNeedsDisplay();
272 }
273
274 String RenderLayerBacking::displayListAsText(DisplayList::AsTextFlags flags) const
275 {
276     return m_graphicsLayer->displayListAsText(flags);
277 }
278
279 void RenderLayerBacking::setIsTrackingDisplayListReplay(bool isTrackingReplay)
280 {
281     m_graphicsLayer->setIsTrackingDisplayListReplay(isTrackingReplay);
282 }
283
284 String RenderLayerBacking::replayDisplayListAsText(DisplayList::AsTextFlags flags) const
285 {
286     return m_graphicsLayer->replayDisplayListAsText(flags);
287 }
288
289 void RenderLayerBacking::tiledBackingUsageChanged(const GraphicsLayer* layer, bool usingTiledBacking)
290 {
291     compositor().layerTiledBackingUsageChanged(layer, usingTiledBacking);
292 }
293
294 TiledBacking* RenderLayerBacking::tiledBacking() const
295 {
296     return m_graphicsLayer->tiledBacking();
297 }
298
299 static TiledBacking::TileCoverage computePageTiledBackingCoverage(RenderLayerBacking* backing)
300 {
301     // FIXME: When we use TiledBacking for overflow, this should look at RenderView scrollability.
302     FrameView& frameView = backing->owningLayer().renderer().view().frameView();
303
304     // If the page is non-visible, don't incur the cost of keeping extra tiles for scrolling.
305     if (!backing->owningLayer().page().isVisible())
306         return TiledBacking::CoverageForVisibleArea;
307
308     TiledBacking::TileCoverage tileCoverage = TiledBacking::CoverageForVisibleArea;
309     bool useMinimalTilesDuringLiveResize = frameView.inLiveResize();
310     if (frameView.speculativeTilingEnabled() && !useMinimalTilesDuringLiveResize) {
311         bool clipsToExposedRect = static_cast<bool>(frameView.viewExposedRect());
312         if (frameView.horizontalScrollbarMode() != ScrollbarAlwaysOff || clipsToExposedRect)
313             tileCoverage |= TiledBacking::CoverageForHorizontalScrolling;
314
315         if (frameView.verticalScrollbarMode() != ScrollbarAlwaysOff || clipsToExposedRect)
316             tileCoverage |= TiledBacking::CoverageForVerticalScrolling;
317     }
318     return tileCoverage;
319 }
320
321 void RenderLayerBacking::adjustTiledBackingCoverage()
322 {
323     if (!m_isMainFrameLayerWithTiledBacking)
324         return;
325
326     TiledBacking::TileCoverage tileCoverage = computePageTiledBackingCoverage(this);
327     tiledBacking()->setTileCoverage(tileCoverage);
328 }
329
330 void RenderLayerBacking::setTiledBackingHasMargins(bool hasExtendedBackgroundOnLeftAndRight, bool hasExtendedBackgroundOnTopAndBottom)
331 {
332     if (!m_isMainFrameLayerWithTiledBacking)
333         return;
334
335     tiledBacking()->setHasMargins(hasExtendedBackgroundOnTopAndBottom, hasExtendedBackgroundOnTopAndBottom, hasExtendedBackgroundOnLeftAndRight, hasExtendedBackgroundOnLeftAndRight);
336 }
337
338 void RenderLayerBacking::updateDebugIndicators(bool showBorder, bool showRepaintCounter)
339 {
340     m_graphicsLayer->setShowDebugBorder(showBorder);
341     m_graphicsLayer->setShowRepaintCounter(showRepaintCounter);
342     
343     if (m_ancestorClippingLayer)
344         m_ancestorClippingLayer->setShowDebugBorder(showBorder);
345
346     if (m_foregroundLayer) {
347         m_foregroundLayer->setShowDebugBorder(showBorder);
348         m_foregroundLayer->setShowRepaintCounter(showRepaintCounter);
349     }
350     
351     if (m_contentsContainmentLayer)
352         m_contentsContainmentLayer->setShowDebugBorder(showBorder);
353     
354     if (m_backgroundLayer) {
355         m_backgroundLayer->setShowDebugBorder(showBorder);
356         m_backgroundLayer->setShowRepaintCounter(showRepaintCounter);
357     }
358
359     if (m_maskLayer) {
360         m_maskLayer->setShowDebugBorder(showBorder);
361         m_maskLayer->setShowRepaintCounter(showRepaintCounter);
362     }
363
364     if (m_layerForHorizontalScrollbar)
365         m_layerForHorizontalScrollbar->setShowDebugBorder(showBorder);
366
367     if (m_layerForVerticalScrollbar)
368         m_layerForVerticalScrollbar->setShowDebugBorder(showBorder);
369
370     if (m_layerForScrollCorner)
371         m_layerForScrollCorner->setShowDebugBorder(showBorder);
372
373     if (m_scrollingLayer)
374         m_scrollingLayer->setShowDebugBorder(showBorder);
375
376     if (m_scrollingContentsLayer) {
377         m_scrollingContentsLayer->setShowDebugBorder(showBorder);
378         m_scrollingContentsLayer->setShowRepaintCounter(showRepaintCounter);
379     }
380 }
381
382 void RenderLayerBacking::createPrimaryGraphicsLayer()
383 {
384     String layerName = m_owningLayer.name();
385     const unsigned maxLayerNameLength = 100;
386     if (layerName.length() > maxLayerNameLength) {
387         layerName.truncate(maxLayerNameLength);
388         layerName.append("...");
389     }
390     m_graphicsLayer = createGraphicsLayer(layerName, m_isMainFrameLayerWithTiledBacking ? GraphicsLayer::Type::PageTiledBacking : GraphicsLayer::Type::Normal);
391
392     if (m_isMainFrameLayerWithTiledBacking) {
393         m_childContainmentLayer = createGraphicsLayer("Page TiledBacking containment");
394         m_graphicsLayer->addChild(m_childContainmentLayer.get());
395     }
396
397 #if !PLATFORM(IOS)
398     if (m_isMainFrameRenderViewLayer) {
399         // Page scale is applied above the RenderView on iOS.
400         m_graphicsLayer->setContentsOpaque(!compositor().viewHasTransparentBackground());
401         m_graphicsLayer->setAppliesPageScale();
402     }
403 #endif
404
405 #if PLATFORM(COCOA) && USE(CA)
406     if (!compositor().acceleratedDrawingEnabled() && renderer().isCanvas()) {
407         const HTMLCanvasElement* canvas = downcast<HTMLCanvasElement>(renderer().element());
408         if (canvas->shouldAccelerate(canvas->size()))
409             m_graphicsLayer->setAcceleratesDrawing(true);
410     }
411 #endif    
412     
413     updateOpacity(renderer().style());
414     updateTransform(renderer().style());
415     updateFilters(renderer().style());
416 #if ENABLE(FILTERS_LEVEL_2)
417     updateBackdropFilters(renderer().style());
418 #endif
419 #if ENABLE(CSS_COMPOSITING)
420     updateBlendMode(renderer().style());
421 #endif
422     updateCustomAppearance(renderer().style());
423 }
424
425 #if PLATFORM(IOS)
426 void RenderLayerBacking::layerWillBeDestroyed()
427 {
428     RenderObject& renderer = this->renderer();
429     if (is<RenderEmbeddedObject>(renderer) && downcast<RenderEmbeddedObject>(renderer).allowsAcceleratedCompositing()) {
430         PluginViewBase* pluginViewBase = downcast<PluginViewBase>(downcast<RenderWidget>(renderer).widget());
431         if (pluginViewBase && m_graphicsLayer->contentsLayerForMedia())
432             pluginViewBase->detachPluginLayer();
433     }
434 }
435
436 bool RenderLayerBacking::needsIOSDumpRenderTreeMainFrameRenderViewLayerIsAlwaysOpaqueHack(const GraphicsLayer& layer) const
437 {
438     if (m_isMainFrameRenderViewLayer && IOSApplication::isDumpRenderTree()) {
439         // In iOS WebKit1 the main frame's RenderView layer is always transparent. We lie that it is opaque so that
440         // internals.layerTreeAsText() tests succeed.
441         ASSERT_UNUSED(layer, !layer.contentsOpaque());
442         return true;
443     }
444     return false;
445 }
446 #endif
447
448 void RenderLayerBacking::destroyGraphicsLayers()
449 {
450     if (m_graphicsLayer) {
451         willDestroyLayer(m_graphicsLayer.get());
452         m_graphicsLayer->removeFromParent();
453     }
454
455     m_ancestorClippingLayer = nullptr;
456     m_contentsContainmentLayer = nullptr;
457     m_graphicsLayer = nullptr;
458     m_foregroundLayer = nullptr;
459     m_backgroundLayer = nullptr;
460     m_childContainmentLayer = nullptr;
461     m_maskLayer = nullptr;
462     m_childClippingMaskLayer = nullptr;
463
464     m_scrollingLayer = nullptr;
465     m_scrollingContentsLayer = nullptr;
466 }
467
468 void RenderLayerBacking::updateOpacity(const RenderStyle& style)
469 {
470     m_graphicsLayer->setOpacity(compositingOpacity(style.opacity()));
471 }
472
473 void RenderLayerBacking::updateTransform(const RenderStyle& style)
474 {
475     // FIXME: This could use m_owningLayer.transform(), but that currently has transform-origin
476     // baked into it, and we don't want that.
477     TransformationMatrix t;
478     if (m_owningLayer.hasTransform()) {
479         auto& renderBox = downcast<RenderBox>(renderer());
480         style.applyTransform(t, snapRectToDevicePixels(renderBox.borderBoxRect(), deviceScaleFactor()), RenderStyle::ExcludeTransformOrigin);
481         makeMatrixRenderable(t, compositor().canRender3DTransforms());
482     }
483     
484     if (m_contentsContainmentLayer) {
485         m_contentsContainmentLayer->setTransform(t);
486         m_graphicsLayer->setTransform(TransformationMatrix());
487     } else
488         m_graphicsLayer->setTransform(t);
489 }
490
491 void RenderLayerBacking::updateFilters(const RenderStyle& style)
492 {
493     m_canCompositeFilters = m_graphicsLayer->setFilters(style.filter());
494 }
495
496 #if ENABLE(FILTERS_LEVEL_2)
497 void RenderLayerBacking::updateBackdropFilters(const RenderStyle& style)
498 {
499     m_canCompositeBackdropFilters = m_graphicsLayer->setBackdropFilters(style.backdropFilter());
500 }
501
502 void RenderLayerBacking::updateBackdropFiltersGeometry()
503 {
504     if (!m_canCompositeBackdropFilters)
505         return;
506
507     if (!is<RenderBox>(renderer()))
508         return;
509
510     RenderBox& renderer = downcast<RenderBox>(this->renderer());
511     LayoutRect boxRect = renderer.borderBoxRect();
512     if (renderer.hasClip())
513         boxRect.intersect(renderer.clipRect(LayoutPoint(), nullptr));
514     boxRect.move(contentOffsetInCompostingLayer());
515
516     FloatRoundedRect backdropFiltersRect;
517     if (renderer.style().hasBorderRadius() && !renderer.hasClip())
518         backdropFiltersRect = renderer.style().getRoundedInnerBorderFor(boxRect).pixelSnappedRoundedRectForPainting(deviceScaleFactor());
519     else
520         backdropFiltersRect = FloatRoundedRect(snapRectToDevicePixels(boxRect, deviceScaleFactor()));
521
522     m_graphicsLayer->setBackdropFiltersRect(backdropFiltersRect);
523 }
524 #endif
525
526 #if ENABLE(CSS_COMPOSITING)
527 void RenderLayerBacking::updateBlendMode(const RenderStyle& style)
528 {
529     // FIXME: where is the blend mode updated when m_ancestorClippingLayers come and go?
530     if (m_ancestorClippingLayer) {
531         m_ancestorClippingLayer->setBlendMode(style.blendMode());
532         m_graphicsLayer->setBlendMode(BlendModeNormal);
533     } else
534         m_graphicsLayer->setBlendMode(style.blendMode());
535 }
536 #endif
537
538 void RenderLayerBacking::updateCustomAppearance(const RenderStyle& style)
539 {
540     ControlPart appearance = style.appearance();
541     if (appearance == MediaControlsLightBarBackgroundPart)
542         m_graphicsLayer->setCustomAppearance(GraphicsLayer::LightBackdropAppearance);
543     else if (appearance == MediaControlsDarkBarBackgroundPart)
544         m_graphicsLayer->setCustomAppearance(GraphicsLayer::DarkBackdropAppearance);
545     else
546         m_graphicsLayer->setCustomAppearance(GraphicsLayer::NoCustomAppearance);
547 }
548
549 static bool layerOrAncestorIsTransformedOrUsingCompositedScrolling(RenderLayer& layer)
550 {
551     for (RenderLayer* curr = &layer; curr; curr = curr->parent()) {
552         if (curr->hasTransform() || curr->usesAcceleratedScrolling())
553             return true;
554     }
555
556     return false;
557 }
558
559 bool RenderLayerBacking::shouldClipCompositedBounds() const
560 {
561 #if !PLATFORM(IOS)
562     // Scrollbar layers use this layer for relative positioning, so don't clip.
563     if (layerForHorizontalScrollbar() || layerForVerticalScrollbar())
564         return false;
565 #endif
566
567     if (m_isMainFrameLayerWithTiledBacking)
568         return false;
569
570     if (layerOrAncestorIsTransformedOrUsingCompositedScrolling(m_owningLayer))
571         return false;
572
573     if (m_owningLayer.isFlowThreadCollectingGraphicsLayersUnderRegions())
574         return false;
575
576     return true;
577 }
578
579 static bool hasNonZeroTransformOrigin(const RenderObject& renderer)
580 {
581     const RenderStyle& style = renderer.style();
582     return (style.transformOriginX().type() == Fixed && style.transformOriginX().value())
583         || (style.transformOriginY().type() == Fixed && style.transformOriginY().value());
584 }
585
586 void RenderLayerBacking::updateCompositedBounds()
587 {
588     LayoutRect layerBounds = m_owningLayer.calculateLayerBounds(&m_owningLayer, LayoutSize(), RenderLayer::DefaultCalculateLayerBoundsFlags | RenderLayer::ExcludeHiddenDescendants | RenderLayer::DontConstrainForMask);
589     // Clip to the size of the document or enclosing overflow-scroll layer.
590     // If this or an ancestor is transformed, we can't currently compute the correct rect to intersect with.
591     // We'd need RenderObject::convertContainerToLocalQuad(), which doesn't yet exist.
592     if (shouldClipCompositedBounds()) {
593         RenderView& view = renderer().view();
594         RenderLayer* rootLayer = view.layer();
595
596         LayoutRect clippingBounds;
597         if (renderer().style().position() == FixedPosition && renderer().container() == &view)
598             clippingBounds = view.frameView().rectForFixedPositionLayout();
599         else
600             clippingBounds = view.unscaledDocumentRect();
601
602         if (&m_owningLayer != rootLayer)
603             clippingBounds.intersect(m_owningLayer.backgroundClipRect(RenderLayer::ClipRectsContext(rootLayer, AbsoluteClipRects)).rect()); // FIXME: Incorrect for CSS regions.
604
605         LayoutPoint delta = m_owningLayer.convertToLayerCoords(rootLayer, LayoutPoint(), RenderLayer::AdjustForColumns);
606         clippingBounds.move(-delta.x(), -delta.y());
607
608         layerBounds.intersect(clippingBounds);
609     }
610     
611     // If the element has a transform-origin that has fixed lengths, and the renderer has zero size,
612     // then we need to ensure that the compositing layer has non-zero size so that we can apply
613     // the transform-origin via the GraphicsLayer anchorPoint (which is expressed as a fractional value).
614     if (layerBounds.isEmpty() && (hasNonZeroTransformOrigin(renderer()) || renderer().style().hasPerspective())) {
615         layerBounds.setWidth(1);
616         layerBounds.setHeight(1);
617         m_artificiallyInflatedBounds = true;
618     } else
619         m_artificiallyInflatedBounds = false;
620
621     setCompositedBounds(layerBounds);
622 }
623
624 void RenderLayerBacking::updateAfterWidgetResize()
625 {
626     if (!is<RenderWidget>(renderer()))
627         return;
628     if (RenderLayerCompositor* innerCompositor = RenderLayerCompositor::frameContentsCompositor(&downcast<RenderWidget>(renderer()))) {
629         innerCompositor->frameViewDidChangeSize();
630         innerCompositor->frameViewDidChangeLocation(flooredIntPoint(contentsBox().location()));
631     }
632 }
633
634 void RenderLayerBacking::updateAfterLayout(UpdateAfterLayoutFlags flags)
635 {
636     if (!compositor().compositingLayersNeedRebuild()) {
637         // Calling updateGeometry() here gives incorrect results, because the
638         // position of this layer's GraphicsLayer depends on the position of our compositing
639         // ancestor's GraphicsLayer. That cannot be determined until all the descendant 
640         // RenderLayers of that ancestor have been processed via updateLayerPositions().
641         //
642         // The solution is to update compositing children of this layer here,
643         // via updateCompositingChildrenGeometry().
644         updateCompositedBounds();
645         compositor().updateCompositingDescendantGeometry(m_owningLayer, m_owningLayer, flags & CompositingChildrenOnly);
646         
647         if (flags & IsUpdateRoot) {
648             updateGeometry();
649             compositor().updateRootLayerPosition();
650             RenderLayer* stackingContainer = m_owningLayer.enclosingStackingContainer();
651             if (!compositor().compositingLayersNeedRebuild() && stackingContainer && (stackingContainer != &m_owningLayer))
652                 compositor().updateCompositingDescendantGeometry(*stackingContainer, *stackingContainer, flags & CompositingChildrenOnly);
653         }
654     }
655     
656     if (flags & NeedsFullRepaint && canIssueSetNeedsDisplay())
657         setContentsNeedDisplay();
658 }
659
660 bool RenderLayerBacking::updateConfiguration()
661 {
662     m_owningLayer.updateDescendantDependentFlags();
663     m_owningLayer.updateZOrderLists();
664
665     bool layerConfigChanged = false;
666     setBackgroundLayerPaintsFixedRootBackground(compositor().needsFixedRootBackgroundLayer(m_owningLayer));
667     
668     // The background layer is currently only used for fixed root backgrounds.
669     if (updateBackgroundLayer(m_backgroundLayerPaintsFixedRootBackground))
670         layerConfigChanged = true;
671
672     if (updateForegroundLayer(compositor().needsContentsCompositingLayer(m_owningLayer)))
673         layerConfigChanged = true;
674     
675     bool needsDescendantsClippingLayer = compositor().clipsCompositingDescendants(m_owningLayer);
676
677     if (!renderer().view().needsLayout()) {
678         bool usesCompositedScrolling = m_owningLayer.usesAcceleratedScrolling();
679
680         // Our scrolling layer will clip.
681         if (usesCompositedScrolling)
682             needsDescendantsClippingLayer = false;
683
684         if (updateScrollingLayers(usesCompositedScrolling))
685             layerConfigChanged = true;
686
687         if (updateDescendantClippingLayer(needsDescendantsClippingLayer))
688             layerConfigChanged = true;
689     }
690
691     if (updateAncestorClippingLayer(compositor().clippedByAncestor(m_owningLayer)))
692         layerConfigChanged = true;
693
694     if (updateOverflowControlsLayers(requiresHorizontalScrollbarLayer(), requiresVerticalScrollbarLayer(), requiresScrollCornerLayer()))
695         layerConfigChanged = true;
696
697     if (layerConfigChanged)
698         updateInternalHierarchy();
699
700     if (GraphicsLayer* flatteningLayer = tileCacheFlatteningLayer()) {
701         if (layerConfigChanged || flatteningLayer->parent() != m_graphicsLayer.get())
702             m_graphicsLayer->addChild(flatteningLayer);
703     }
704
705     updateMaskingLayer(renderer().hasMask(), renderer().hasClipPath());
706
707     updateChildClippingStrategy(needsDescendantsClippingLayer);
708
709     if (m_owningLayer.hasReflection()) {
710         if (m_owningLayer.reflectionLayer()->backing()) {
711             GraphicsLayer* reflectionLayer = m_owningLayer.reflectionLayer()->backing()->graphicsLayer();
712             m_graphicsLayer->setReplicatedByLayer(reflectionLayer);
713         }
714     } else
715         m_graphicsLayer->setReplicatedByLayer(nullptr);
716
717     PaintedContentsInfo contentsInfo(*this);
718
719     if (!m_owningLayer.isRootLayer()) {
720         bool didUpdateContentsRect = false;
721         updateDirectlyCompositedBoxDecorations(contentsInfo, didUpdateContentsRect);
722     } else
723         updateRootLayerConfiguration();
724     
725     if (contentsInfo.isDirectlyCompositedImage())
726         updateImageContents(contentsInfo);
727
728     if (is<RenderEmbeddedObject>(renderer()) && downcast<RenderEmbeddedObject>(renderer()).allowsAcceleratedCompositing()) {
729         PluginViewBase* pluginViewBase = downcast<PluginViewBase>(downcast<RenderWidget>(renderer()).widget());
730 #if PLATFORM(IOS)
731         if (pluginViewBase && !m_graphicsLayer->contentsLayerForMedia()) {
732             pluginViewBase->detachPluginLayer();
733             pluginViewBase->attachPluginLayer();
734         }
735 #else
736         if (!pluginViewBase->shouldNotAddLayer())
737             m_graphicsLayer->setContentsToPlatformLayer(pluginViewBase->platformLayer(), GraphicsLayer::ContentsLayerForPlugin);
738 #endif
739     }
740 #if ENABLE(VIDEO)
741     else if (is<RenderVideo>(renderer()) && downcast<RenderVideo>(renderer()).shouldDisplayVideo()) {
742         HTMLMediaElement* mediaElement = downcast<HTMLMediaElement>(renderer().element());
743         m_graphicsLayer->setContentsToPlatformLayer(mediaElement->platformLayer(), GraphicsLayer::ContentsLayerForMedia);
744     }
745 #endif
746 #if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
747     else if (renderer().isCanvas() && canvasCompositingStrategy(renderer()) == CanvasAsLayerContents) {
748         const HTMLCanvasElement* canvas = downcast<HTMLCanvasElement>(renderer().element());
749         if (CanvasRenderingContext* context = canvas->renderingContext())
750             m_graphicsLayer->setContentsToPlatformLayer(context->platformLayer(), GraphicsLayer::ContentsLayerForCanvas);
751         layerConfigChanged = true;
752     }
753 #endif
754     if (is<RenderWidget>(renderer()))
755         layerConfigChanged = RenderLayerCompositor::parentFrameContentLayers(&downcast<RenderWidget>(renderer()));
756
757     return layerConfigChanged;
758 }
759
760 static LayoutRect clipBox(RenderBox& renderer)
761 {
762     LayoutRect result = LayoutRect::infiniteRect();
763     if (renderer.hasOverflowClip())
764         result = renderer.overflowClipRect(LayoutPoint(), 0); // FIXME: Incorrect for CSS regions.
765
766     if (renderer.hasClip())
767         result.intersect(renderer.clipRect(LayoutPoint(), 0)); // FIXME: Incorrect for CSS regions.
768
769     return result;
770 }
771
772 static bool subpixelOffsetFromRendererChanged(const LayoutSize& oldSubpixelOffsetFromRenderer, const LayoutSize& newSubpixelOffsetFromRenderer, float deviceScaleFactor)
773 {
774     FloatSize previous = snapSizeToDevicePixel(oldSubpixelOffsetFromRenderer, LayoutPoint(), deviceScaleFactor);
775     FloatSize current = snapSizeToDevicePixel(newSubpixelOffsetFromRenderer, LayoutPoint(), deviceScaleFactor);
776     return previous != current;
777 }
778     
779 static FloatSize subpixelForLayerPainting(const LayoutPoint& point, float pixelSnappingFactor)
780 {
781     LayoutUnit x = point.x();
782     LayoutUnit y = point.y();
783     x = x >= 0 ? floorToDevicePixel(x, pixelSnappingFactor) : ceilToDevicePixel(x, pixelSnappingFactor);
784     y = y >= 0 ? floorToDevicePixel(y, pixelSnappingFactor) : ceilToDevicePixel(y, pixelSnappingFactor);
785     return point - LayoutPoint(x, y);
786 }
787
788 struct OffsetFromRenderer {
789     // 1.2px - > { m_devicePixelOffset = 1px m_subpixelOffset = 0.2px }
790     LayoutSize m_devicePixelOffset;
791     LayoutSize m_subpixelOffset;
792 };
793
794 static OffsetFromRenderer computeOffsetFromRenderer(const LayoutSize& offset, float deviceScaleFactor)
795 {
796     OffsetFromRenderer offsetFromRenderer;
797     offsetFromRenderer.m_subpixelOffset = LayoutSize(subpixelForLayerPainting(toLayoutPoint(offset), deviceScaleFactor));
798     offsetFromRenderer.m_devicePixelOffset = offset - offsetFromRenderer.m_subpixelOffset;
799     return offsetFromRenderer;
800 }
801     
802 struct SnappedRectInfo {
803     LayoutRect m_snappedRect;
804     LayoutSize m_snapDelta;
805 };
806     
807 static SnappedRectInfo snappedGraphicsLayer(const LayoutSize& offset, const LayoutSize& size, float deviceScaleFactor)
808 {
809     SnappedRectInfo snappedGraphicsLayer;
810     LayoutRect graphicsLayerRect = LayoutRect(toLayoutPoint(offset), size);
811     snappedGraphicsLayer.m_snappedRect = LayoutRect(snapRectToDevicePixels(graphicsLayerRect, deviceScaleFactor));
812     snappedGraphicsLayer.m_snapDelta = snappedGraphicsLayer.m_snappedRect.location() - toLayoutPoint(offset);
813     return snappedGraphicsLayer;
814 }
815
816 static LayoutSize computeOffsetFromAncestorGraphicsLayer(RenderLayer* compositedAncestor, const LayoutPoint& location, float deviceScaleFactor)
817 {
818     if (!compositedAncestor)
819         return toLayoutSize(location);
820
821     // FIXME: This is a workaround until after webkit.org/162634 gets fixed. ancestorSubpixelOffsetFromRenderer
822     // could be stale when a dynamic composited state change triggers a pre-order updateGeometry() traversal.
823     LayoutSize ancestorSubpixelOffsetFromRenderer = compositedAncestor->backing()->subpixelOffsetFromRenderer();
824     LayoutRect ancestorCompositedBounds = compositedAncestor->backing()->compositedBounds();
825     LayoutSize floored = toLayoutSize(LayoutPoint(floorPointToDevicePixels(ancestorCompositedBounds.location() - ancestorSubpixelOffsetFromRenderer, deviceScaleFactor)));
826     LayoutSize ancestorRendererOffsetFromAncestorGraphicsLayer = -(floored + ancestorSubpixelOffsetFromRenderer);
827     return ancestorRendererOffsetFromAncestorGraphicsLayer + toLayoutSize(location);
828 }
829
830 class ComputedOffsets {
831 public:
832     ComputedOffsets(const RenderLayer& renderLayer, const LayoutRect& localRect, const LayoutRect& parentGraphicsLayerRect, const LayoutRect& primaryGraphicsLayerRect)
833         : m_renderLayer(renderLayer)
834         , m_location(localRect.location())
835         , m_parentGraphicsLayerOffset(toLayoutSize(parentGraphicsLayerRect.location()))
836         , m_primaryGraphicsLayerOffset(toLayoutSize(primaryGraphicsLayerRect.location()))
837         , m_deviceScaleFactor(renderLayer.renderer().document().deviceScaleFactor())
838     {
839     }
840
841     LayoutSize fromParentGraphicsLayer()
842     {
843         if (!m_fromParentGraphicsLayer)
844             m_fromParentGraphicsLayer = fromAncestorGraphicsLayer() - m_parentGraphicsLayerOffset;
845         return m_fromParentGraphicsLayer.value();
846     }
847     
848     LayoutSize fromPrimaryGraphicsLayer()
849     {
850         if (!m_fromPrimaryGraphicsLayer)
851             m_fromPrimaryGraphicsLayer = fromAncestorGraphicsLayer() - m_parentGraphicsLayerOffset - m_primaryGraphicsLayerOffset;
852         return m_fromPrimaryGraphicsLayer.value();
853     }
854     
855 private:
856     LayoutSize fromAncestorGraphicsLayer()
857     {
858         if (!m_fromAncestorGraphicsLayer) {
859             RenderLayer* compositedAncestor = m_renderLayer.ancestorCompositingLayer();
860             LayoutPoint localPointInAncestorRenderLayerCoords = m_renderLayer.convertToLayerCoords(compositedAncestor, m_location, RenderLayer::AdjustForColumns);
861             m_fromAncestorGraphicsLayer = computeOffsetFromAncestorGraphicsLayer(compositedAncestor, localPointInAncestorRenderLayerCoords, m_deviceScaleFactor);
862         }
863         return m_fromAncestorGraphicsLayer.value();
864     }
865
866     std::optional<LayoutSize> m_fromAncestorGraphicsLayer;
867     std::optional<LayoutSize> m_fromParentGraphicsLayer;
868     std::optional<LayoutSize> m_fromPrimaryGraphicsLayer;
869     
870     const RenderLayer& m_renderLayer;
871     // Location is relative to the renderer.
872     const LayoutPoint m_location;
873     const LayoutSize m_parentGraphicsLayerOffset;
874     const LayoutSize m_primaryGraphicsLayerOffset;
875     float m_deviceScaleFactor;
876 };
877
878 LayoutRect RenderLayerBacking::computePrimaryGraphicsLayerRect(const LayoutRect& parentGraphicsLayerRect) const
879 {
880     ComputedOffsets compositedBoundsOffset(m_owningLayer, compositedBounds(), parentGraphicsLayerRect, LayoutRect());
881     return LayoutRect(encloseRectToDevicePixels(LayoutRect(toLayoutPoint(compositedBoundsOffset.fromParentGraphicsLayer()), compositedBounds().size()),
882         deviceScaleFactor()));
883 }
884
885 LayoutRect RenderLayerBacking::computeParentGraphicsLayerRect(RenderLayer* compositedAncestor, LayoutSize& ancestorClippingLayerOffset) const
886 {
887     if (!compositedAncestor || !compositedAncestor->backing())
888         return renderer().view().documentRect();
889
890     auto* ancestorBackingLayer = compositedAncestor->backing();
891     LayoutRect parentGraphicsLayerRect;
892     if (m_owningLayer.isInsideFlowThread()) {
893         // FIXME: flows/columns need work.
894         LayoutRect ancestorCompositedBounds = ancestorBackingLayer->compositedBounds();
895         ancestorCompositedBounds.setLocation(LayoutPoint());
896         adjustAncestorCompositingBoundsForFlowThread(ancestorCompositedBounds, compositedAncestor);
897         parentGraphicsLayerRect = ancestorCompositedBounds;
898     }
899
900     if (ancestorBackingLayer->hasClippingLayer()) {
901         // If the compositing ancestor has a layer to clip children, we parent in that, and therefore position relative to it.
902         LayoutRect clippingBox = clipBox(downcast<RenderBox>(compositedAncestor->renderer()));
903         LayoutSize clippingBoxOffset = computeOffsetFromAncestorGraphicsLayer(compositedAncestor, clippingBox.location(), deviceScaleFactor());
904         parentGraphicsLayerRect = snappedGraphicsLayer(clippingBoxOffset, clippingBox.size(), deviceScaleFactor()).m_snappedRect;
905     }
906
907 #if PLATFORM(IOS)
908     if (compositedAncestor->hasTouchScrollableOverflow()) {
909         LayoutRect ancestorCompositedBounds = ancestorBackingLayer->compositedBounds();
910         auto& renderBox = downcast<RenderBox>(compositedAncestor->renderer());
911         LayoutRect paddingBox(renderBox.borderLeft(), renderBox.borderTop(), renderBox.width() - renderBox.borderLeft() - renderBox.borderRight(), renderBox.height() - renderBox.borderTop() - renderBox.borderBottom());
912         ScrollOffset scrollOffset = compositedAncestor->scrollOffset();
913         parentGraphicsLayerRect = LayoutRect((paddingBox.location() - toLayoutSize(ancestorCompositedBounds.location()) - toLayoutSize(scrollOffset)), paddingBox.size());
914     }
915 #else
916     if (compositedAncestor->needsCompositedScrolling()) {
917         auto& renderBox = downcast<RenderBox>(compositedAncestor->renderer());
918         LayoutPoint scrollOrigin(renderBox.borderLeft(), renderBox.borderTop());
919         parentGraphicsLayerRect = LayoutRect(scrollOrigin - toLayoutSize(compositedAncestor->scrollOffset()), renderBox.borderBoxRect().size());
920     }
921 #endif
922
923     if (m_ancestorClippingLayer) {
924         // Call calculateRects to get the backgroundRect which is what is used to clip the contents of this
925         // layer. Note that we call it with temporaryClipRects = true because normally when computing clip rects
926         // for a compositing layer, rootLayer is the layer itself.
927         ShouldRespectOverflowClip shouldRespectOverflowClip = compositedAncestor->isolatesCompositedBlending() ? RespectOverflowClip : IgnoreOverflowClip;
928         RenderLayer::ClipRectsContext clipRectsContext(compositedAncestor, TemporaryClipRects, IgnoreOverlayScrollbarSize, shouldRespectOverflowClip);
929         LayoutRect parentClipRect = m_owningLayer.backgroundClipRect(clipRectsContext).rect(); // FIXME: Incorrect for CSS regions.
930         ASSERT(!parentClipRect.isInfinite());
931         LayoutSize clippingOffset = computeOffsetFromAncestorGraphicsLayer(compositedAncestor, parentClipRect.location(), deviceScaleFactor());
932         LayoutRect snappedClippingLayerRect = snappedGraphicsLayer(clippingOffset, parentClipRect.size(), deviceScaleFactor()).m_snappedRect;
933         // The primary layer is then parented in, and positioned relative to this clipping layer.
934         ancestorClippingLayerOffset = snappedClippingLayerRect.location() - parentGraphicsLayerRect.location();
935         parentGraphicsLayerRect = snappedClippingLayerRect;
936     }
937     return parentGraphicsLayerRect;
938 }
939
940 void RenderLayerBacking::updateGeometry()
941 {
942     // If we haven't built z-order lists yet, wait until later.
943     if (m_owningLayer.isStackingContainer() && m_owningLayer.m_zOrderListsDirty)
944         return;
945
946     const RenderStyle& style = renderer().style();
947     // Set transform property, if it is not animating. We have to do this here because the transform
948     // is affected by the layer dimensions.
949     if (!renderer().animation().isRunningAcceleratedAnimationOnRenderer(renderer(), CSSPropertyTransform, AnimationBase::Running | AnimationBase::Paused))
950         updateTransform(style);
951
952     // Set opacity, if it is not animating.
953     if (!renderer().animation().isRunningAcceleratedAnimationOnRenderer(renderer(), CSSPropertyOpacity, AnimationBase::Running | AnimationBase::Paused))
954         updateOpacity(style);
955
956     updateFilters(style);
957 #if ENABLE(FILTERS_LEVEL_2)
958     updateBackdropFilters(style);
959 #endif
960 #if ENABLE(CSS_COMPOSITING)
961     updateBlendMode(style);
962 #endif
963     m_owningLayer.updateDescendantDependentFlags();
964
965     // FIXME: reflections should force transform-style to be flat in the style: https://bugs.webkit.org/show_bug.cgi?id=106959
966     bool preserves3D = style.transformStyle3D() == TransformStyle3DPreserve3D && !renderer().hasReflection();
967     m_graphicsLayer->setPreserves3D(preserves3D);
968     m_graphicsLayer->setBackfaceVisibility(style.backfaceVisibility() == BackfaceVisibilityVisible);
969
970     RenderLayer* compositedAncestor = m_owningLayer.ancestorCompositingLayer();
971     LayoutSize ancestorClippingLayerOffset;
972     LayoutRect parentGraphicsLayerRect = computeParentGraphicsLayerRect(compositedAncestor, ancestorClippingLayerOffset);
973     LayoutRect primaryGraphicsLayerRect = computePrimaryGraphicsLayerRect(parentGraphicsLayerRect);
974
975     ComputedOffsets compositedBoundsOffset(m_owningLayer, compositedBounds(), parentGraphicsLayerRect, primaryGraphicsLayerRect);
976     m_compositedBoundsOffsetFromGraphicsLayer = compositedBoundsOffset.fromPrimaryGraphicsLayer();
977     m_graphicsLayer->setPosition(primaryGraphicsLayerRect.location());
978     m_graphicsLayer->setSize(primaryGraphicsLayerRect.size());
979
980     ComputedOffsets rendererOffset(m_owningLayer, LayoutRect(), parentGraphicsLayerRect, primaryGraphicsLayerRect);
981     if (m_ancestorClippingLayer) {
982         // Clipping layer is parented in the ancestor layer.
983         m_ancestorClippingLayer->setPosition(toLayoutPoint(ancestorClippingLayerOffset));
984         m_ancestorClippingLayer->setSize(parentGraphicsLayerRect.size());
985         m_ancestorClippingLayer->setOffsetFromRenderer(-rendererOffset.fromParentGraphicsLayer());
986     }
987
988     if (m_contentsContainmentLayer) {
989         m_contentsContainmentLayer->setPreserves3D(preserves3D);
990         m_contentsContainmentLayer->setPosition(primaryGraphicsLayerRect.location());
991         m_graphicsLayer->setPosition(FloatPoint());
992         // Use the same size as m_graphicsLayer so transforms behave correctly.
993         m_contentsContainmentLayer->setSize(primaryGraphicsLayerRect.size());
994     }
995
996     // Compute renderer offset from primary graphics layer. Note that primaryGraphicsLayerRect is in parentGraphicsLayer's coordidate system which is not necessarily
997     // the same as the ancestor graphics layer.
998     OffsetFromRenderer primaryGraphicsLayerOffsetFromRenderer;
999     LayoutSize oldSubpixelOffsetFromRenderer = m_subpixelOffsetFromRenderer;
1000     primaryGraphicsLayerOffsetFromRenderer = computeOffsetFromRenderer(-rendererOffset.fromPrimaryGraphicsLayer(), deviceScaleFactor());
1001     m_subpixelOffsetFromRenderer = primaryGraphicsLayerOffsetFromRenderer.m_subpixelOffset;
1002
1003     if (primaryGraphicsLayerOffsetFromRenderer.m_devicePixelOffset != m_graphicsLayer->offsetFromRenderer()) {
1004         m_graphicsLayer->setOffsetFromRenderer(primaryGraphicsLayerOffsetFromRenderer.m_devicePixelOffset);
1005         positionOverflowControlsLayers();
1006     }
1007
1008     if (!m_isMainFrameRenderViewLayer) {
1009         // For non-root layers, background is always painted by the primary graphics layer.
1010         ASSERT(!m_backgroundLayer);
1011         // Subpixel offset from graphics layer or size changed.
1012         bool hadSubpixelRounding = !m_subpixelOffsetFromRenderer.isZero() || compositedBounds().size() != primaryGraphicsLayerRect.size();
1013         m_graphicsLayer->setContentsOpaque(!hadSubpixelRounding && m_owningLayer.backgroundIsKnownToBeOpaqueInRect(compositedBounds()));
1014     }
1015
1016     // If we have a layer that clips children, position it.
1017     LayoutRect clippingBox;
1018     if (GraphicsLayer* clipLayer = clippingLayer()) {
1019         clippingBox = clipBox(downcast<RenderBox>(renderer()));
1020         // Clipping layer is parented in the primary graphics layer.
1021         LayoutSize clipBoxOffsetFromGraphicsLayer = toLayoutSize(clippingBox.location()) + rendererOffset.fromPrimaryGraphicsLayer();
1022         SnappedRectInfo snappedClippingGraphicsLayer = snappedGraphicsLayer(clipBoxOffsetFromGraphicsLayer, clippingBox.size(), deviceScaleFactor());
1023         clipLayer->setPosition(snappedClippingGraphicsLayer.m_snappedRect.location());
1024         clipLayer->setSize(snappedClippingGraphicsLayer.m_snappedRect.size());
1025         clipLayer->setOffsetFromRenderer(toLayoutSize(clippingBox.location() - snappedClippingGraphicsLayer.m_snapDelta));
1026
1027         if (m_childClippingMaskLayer && !m_scrollingLayer) {
1028             m_childClippingMaskLayer->setSize(clipLayer->size());
1029             m_childClippingMaskLayer->setPosition(FloatPoint());
1030             m_childClippingMaskLayer->setOffsetFromRenderer(clipLayer->offsetFromRenderer());
1031         }
1032     }
1033     
1034     if (m_maskLayer)
1035         updateMaskingLayerGeometry();
1036     
1037     if (m_owningLayer.renderer().hasTransformRelatedProperty()) {
1038         // Update properties that depend on layer dimensions.
1039         FloatPoint3D transformOrigin = computeTransformOriginForPainting(downcast<RenderBox>(renderer()).borderBoxRect());
1040         FloatPoint layerOffset = roundPointToDevicePixels(toLayoutPoint(rendererOffset.fromParentGraphicsLayer()), deviceScaleFactor());
1041         // Compute the anchor point, which is in the center of the renderer box unless transform-origin is set.
1042         FloatPoint3D anchor(
1043             primaryGraphicsLayerRect.width() ? ((layerOffset.x() - primaryGraphicsLayerRect.x()) + transformOrigin.x()) / primaryGraphicsLayerRect.width() : 0.5,
1044             primaryGraphicsLayerRect.height() ? ((layerOffset.y() - primaryGraphicsLayerRect.y())+ transformOrigin.y()) / primaryGraphicsLayerRect.height() : 0.5,
1045             transformOrigin.z());
1046
1047         if (m_contentsContainmentLayer)
1048             m_contentsContainmentLayer->setAnchorPoint(anchor);
1049         else
1050             m_graphicsLayer->setAnchorPoint(anchor);
1051
1052         GraphicsLayer* clipLayer = clippingLayer();
1053         if (style.hasPerspective()) {
1054             TransformationMatrix t = owningLayer().perspectiveTransform();
1055             
1056             if (clipLayer) {
1057                 clipLayer->setChildrenTransform(t);
1058                 m_graphicsLayer->setChildrenTransform(TransformationMatrix());
1059             }
1060             else
1061                 m_graphicsLayer->setChildrenTransform(t);
1062         } else {
1063             if (clipLayer)
1064                 clipLayer->setChildrenTransform(TransformationMatrix());
1065             else
1066                 m_graphicsLayer->setChildrenTransform(TransformationMatrix());
1067         }
1068     } else {
1069         m_graphicsLayer->setAnchorPoint(FloatPoint3D(0.5, 0.5, 0));
1070         if (m_contentsContainmentLayer)
1071             m_contentsContainmentLayer->setAnchorPoint(FloatPoint3D(0.5, 0.5, 0));
1072     }
1073
1074     if (m_foregroundLayer) {
1075         FloatPoint foregroundPosition;
1076         FloatSize foregroundSize = primaryGraphicsLayerRect.size();
1077         FloatSize foregroundOffset = m_graphicsLayer->offsetFromRenderer();
1078         if (hasClippingLayer()) {
1079             // If we have a clipping layer (which clips descendants), then the foreground layer is a child of it,
1080             // so that it gets correctly sorted with children. In that case, position relative to the clipping layer.
1081             foregroundSize = FloatSize(clippingBox.size());
1082             foregroundOffset = toFloatSize(clippingBox.location());
1083         }
1084
1085         m_foregroundLayer->setPosition(foregroundPosition);
1086         m_foregroundLayer->setSize(foregroundSize);
1087         m_foregroundLayer->setOffsetFromRenderer(foregroundOffset);
1088     }
1089
1090     if (m_backgroundLayer) {
1091         FloatPoint backgroundPosition;
1092         FloatSize backgroundSize = primaryGraphicsLayerRect.size();
1093         if (backgroundLayerPaintsFixedRootBackground()) {
1094             const FrameView& frameView = renderer().view().frameView();
1095             backgroundPosition = frameView.scrollPositionForFixedPosition();
1096             backgroundSize = frameView.layoutSize();
1097         }
1098         m_backgroundLayer->setPosition(backgroundPosition);
1099         m_backgroundLayer->setSize(backgroundSize);
1100         m_backgroundLayer->setOffsetFromRenderer(m_graphicsLayer->offsetFromRenderer());
1101     }
1102
1103     if (m_owningLayer.reflectionLayer() && m_owningLayer.reflectionLayer()->isComposited()) {
1104         RenderLayerBacking* reflectionBacking = m_owningLayer.reflectionLayer()->backing();
1105         reflectionBacking->updateGeometry();
1106         
1107         // The reflection layer has the bounds of m_owningLayer.reflectionLayer(),
1108         // but the reflected layer is the bounds of this layer, so we need to position it appropriately.
1109         FloatRect layerBounds = this->compositedBounds();
1110         FloatRect reflectionLayerBounds = reflectionBacking->compositedBounds();
1111         reflectionBacking->graphicsLayer()->setReplicatedLayerPosition(FloatPoint(layerBounds.location() - reflectionLayerBounds.location()));
1112     }
1113
1114     if (m_scrollingLayer) {
1115         ASSERT(m_scrollingContentsLayer);
1116         auto& renderBox = downcast<RenderBox>(renderer());
1117         LayoutRect paddingBox(renderBox.borderLeft(), renderBox.borderTop(), renderBox.width() - renderBox.borderLeft() - renderBox.borderRight(), renderBox.height() - renderBox.borderTop() - renderBox.borderBottom());
1118         ScrollOffset scrollOffset = m_owningLayer.scrollOffset();
1119
1120         // FIXME: need to do some pixel snapping here.
1121         m_scrollingLayer->setPosition(FloatPoint(paddingBox.location() - compositedBounds().location()));
1122
1123         m_scrollingLayer->setSize(roundedIntSize(LayoutSize(renderBox.clientWidth(), renderBox.clientHeight())));
1124 #if PLATFORM(IOS)
1125         FloatSize oldScrollingLayerOffset = m_scrollingLayer->offsetFromRenderer();
1126         m_scrollingLayer->setOffsetFromRenderer(FloatPoint() - paddingBox.location());
1127         bool paddingBoxOffsetChanged = oldScrollingLayerOffset != m_scrollingLayer->offsetFromRenderer();
1128
1129         if (m_owningLayer.isInUserScroll()) {
1130             // If scrolling is happening externally, we don't want to touch the layer bounds origin here because that will cause jitter.
1131             m_scrollingLayer->syncBoundsOrigin(scrollOffset);
1132             m_owningLayer.setRequiresScrollBoundsOriginUpdate(true);
1133         } else {
1134             // Note that we implement the contents offset via the bounds origin on this layer, rather than a position on the sublayer.
1135             m_scrollingLayer->setBoundsOrigin(scrollOffset);
1136             m_owningLayer.setRequiresScrollBoundsOriginUpdate(false);
1137         }
1138         
1139         IntSize scrollSize(m_owningLayer.scrollWidth(), m_owningLayer.scrollHeight());
1140
1141         m_scrollingContentsLayer->setPosition(FloatPoint());
1142         
1143         if (scrollSize != m_scrollingContentsLayer->size() || paddingBoxOffsetChanged)
1144             m_scrollingContentsLayer->setNeedsDisplay();
1145
1146         m_scrollingContentsLayer->setSize(scrollSize);
1147         // Scrolling the content layer does not need to trigger a repaint. The offset will be compensated away during painting.
1148         // FIXME: The paint offset and the scroll offset should really be separate concepts.
1149         LayoutSize scrollingContentsOffset = toLayoutSize(paddingBox.location() - toLayoutSize(scrollOffset));
1150         m_scrollingContentsLayer->setOffsetFromRenderer(scrollingContentsOffset, GraphicsLayer::DontSetNeedsDisplay);
1151 #else
1152         m_scrollingContentsLayer->setPosition(-scrollOffset);
1153
1154         FloatSize oldScrollingLayerOffset = m_scrollingLayer->offsetFromRenderer();
1155         m_scrollingLayer->setOffsetFromRenderer(-toFloatSize(paddingBox.location()));
1156
1157         if (m_childClippingMaskLayer) {
1158             m_childClippingMaskLayer->setPosition(m_scrollingLayer->position());
1159             m_childClippingMaskLayer->setSize(m_scrollingLayer->size());
1160             m_childClippingMaskLayer->setOffsetFromRenderer(toFloatSize(paddingBox.location()));
1161         }
1162
1163         bool paddingBoxOffsetChanged = oldScrollingLayerOffset != m_scrollingLayer->offsetFromRenderer();
1164
1165         IntSize scrollSize(m_owningLayer.scrollWidth(), m_owningLayer.scrollHeight());
1166         if (scrollSize != m_scrollingContentsLayer->size() || paddingBoxOffsetChanged)
1167             m_scrollingContentsLayer->setNeedsDisplay();
1168
1169         LayoutSize scrollingContentsOffset = toLayoutSize(paddingBox.location() - toLayoutSize(scrollOffset));
1170         if (scrollingContentsOffset != m_scrollingContentsLayer->offsetFromRenderer() || scrollSize != m_scrollingContentsLayer->size())
1171             compositor().scrollingLayerDidChange(m_owningLayer);
1172
1173         m_scrollingContentsLayer->setSize(scrollSize);
1174         // FIXME: The paint offset and the scroll offset should really be separate concepts.
1175         m_scrollingContentsLayer->setOffsetFromRenderer(scrollingContentsOffset, GraphicsLayer::DontSetNeedsDisplay);
1176 #endif
1177
1178         if (m_foregroundLayer) {
1179             m_foregroundLayer->setSize(m_scrollingContentsLayer->size());
1180             m_foregroundLayer->setOffsetFromRenderer(m_scrollingContentsLayer->offsetFromRenderer());
1181         }
1182     }
1183
1184     // If this layer was created just for clipping or to apply perspective, it doesn't need its own backing store.
1185     LayoutRect ancestorCompositedBounds = compositedAncestor ? compositedAncestor->backing()->compositedBounds() : LayoutRect();
1186     setRequiresOwnBackingStore(compositor().requiresOwnBackingStore(m_owningLayer, compositedAncestor,
1187         LayoutRect(toLayoutPoint(compositedBoundsOffset.fromParentGraphicsLayer()), compositedBounds().size()), ancestorCompositedBounds));
1188 #if ENABLE(FILTERS_LEVEL_2)
1189     updateBackdropFiltersGeometry();
1190 #endif
1191     updateAfterWidgetResize();
1192
1193     if (subpixelOffsetFromRendererChanged(oldSubpixelOffsetFromRenderer, m_subpixelOffsetFromRenderer, deviceScaleFactor()) && canIssueSetNeedsDisplay())
1194         setContentsNeedDisplay();
1195
1196     compositor().updateScrollCoordinatedStatus(m_owningLayer);
1197 }
1198
1199 void RenderLayerBacking::updateAfterDescendants()
1200 {
1201     // FIXME: this potentially duplicates work we did in updateConfiguration().
1202     PaintedContentsInfo contentsInfo(*this);
1203     contentsInfo.setWantsSubpixelAntialiasedTextState(GraphicsLayer::supportsSubpixelAntialiasedLayerText());
1204
1205     if (!m_owningLayer.isRootLayer()) {
1206         bool didUpdateContentsRect = false;
1207         updateDirectlyCompositedBoxDecorations(contentsInfo, didUpdateContentsRect);
1208         if (!didUpdateContentsRect && m_graphicsLayer->usesContentsLayer())
1209             resetContentsRect();
1210     }
1211
1212     updateDrawsContent(contentsInfo);
1213
1214     m_graphicsLayer->setContentsVisible(m_owningLayer.hasVisibleContent() || hasVisibleNonCompositedDescendants());
1215     if (m_scrollingLayer) {
1216         m_scrollingLayer->setContentsVisible(renderer().style().visibility() == VISIBLE);
1217         m_scrollingLayer->setUserInteractionEnabled(renderer().style().pointerEvents() != PE_NONE);
1218     }
1219 }
1220
1221 // FIXME: Avoid repaints when clip path changes.
1222 void RenderLayerBacking::updateMaskingLayerGeometry()
1223 {
1224     m_maskLayer->setSize(m_graphicsLayer->size());
1225     m_maskLayer->setPosition(FloatPoint());
1226     m_maskLayer->setOffsetFromRenderer(m_graphicsLayer->offsetFromRenderer());
1227     
1228     if (!m_maskLayer->drawsContent()) {
1229         if (renderer().hasClipPath()) {
1230             ASSERT(renderer().style().clipPath()->type() != ClipPathOperation::Reference);
1231
1232             WindRule windRule;
1233             // FIXME: Use correct reference box for inlines: https://bugs.webkit.org/show_bug.cgi?id=129047
1234             LayoutRect boundingBox = m_owningLayer.boundingBox(&m_owningLayer);
1235             LayoutRect referenceBoxForClippedInline = LayoutRect(snapRectToDevicePixels(boundingBox, deviceScaleFactor()));
1236             LayoutSize offset = LayoutSize(snapSizeToDevicePixel(-m_subpixelOffsetFromRenderer, LayoutPoint(), deviceScaleFactor()));
1237             Path clipPath = m_owningLayer.computeClipPath(offset, referenceBoxForClippedInline, windRule);
1238
1239             FloatSize pathOffset = m_maskLayer->offsetFromRenderer();
1240             if (!pathOffset.isZero())
1241                 clipPath.translate(-pathOffset);
1242             
1243             m_maskLayer->setShapeLayerPath(clipPath);
1244             m_maskLayer->setShapeLayerWindRule(windRule);
1245         }
1246     }
1247 }
1248
1249 void RenderLayerBacking::adjustAncestorCompositingBoundsForFlowThread(LayoutRect& ancestorCompositingBounds, const RenderLayer* compositingAncestor) const
1250 {
1251     RenderLayer* flowThreadLayer = m_owningLayer.isInsideOutOfFlowThread() ? m_owningLayer.stackingContainer() : nullptr;
1252     if (flowThreadLayer && flowThreadLayer->isRenderFlowThread()) {
1253         if (m_owningLayer.isFlowThreadCollectingGraphicsLayersUnderRegions()) {
1254             // The RenderNamedFlowThread is not composited, as we need it to paint the 
1255             // background layer of the regions. We need to compensate for that by manually
1256             // subtracting the position of the flow-thread.
1257             IntPoint flowPosition;
1258             flowThreadLayer->convertToPixelSnappedLayerCoords(compositingAncestor, flowPosition);
1259             ancestorCompositingBounds.moveBy(flowPosition);
1260         }
1261
1262         // Move the ancestor position at the top of the region where the composited layer is going to display.
1263         RenderFlowThread& flowThread = downcast<RenderFlowThread>(flowThreadLayer->renderer());
1264         RenderNamedFlowFragment* parentRegion = flowThread.cachedRegionForCompositedLayer(m_owningLayer);
1265         if (!parentRegion)
1266             return;
1267
1268         IntPoint flowDelta;
1269         m_owningLayer.convertToPixelSnappedLayerCoords(flowThreadLayer, flowDelta);
1270         parentRegion->adjustRegionBoundsFromFlowThreadPortionRect(ancestorCompositingBounds);
1271         RenderBoxModelObject& layerOwner = downcast<RenderBoxModelObject>(parentRegion->layerOwner());
1272         RenderLayerBacking* layerOwnerBacking = layerOwner.layer()->backing();
1273         if (!layerOwnerBacking)
1274             return;
1275
1276         // Make sure that the region propagates its borders, paddings, outlines or box-shadows to layers inside it.
1277         // Note that the composited bounds of the RenderRegion are already calculated because
1278         // RenderLayerCompositor::rebuildCompositingLayerTree will only iterate on the content of the region after the
1279         // region itself is computed.
1280         ancestorCompositingBounds.moveBy(roundedIntPoint(layerOwnerBacking->compositedBounds().location()));
1281         ancestorCompositingBounds.move(-layerOwner.borderAndPaddingStart(), -layerOwner.borderAndPaddingBefore());
1282
1283         // If there's a clipping GraphicsLayer on the hierarchy (region graphics layer -> clipping graphics layer ->
1284         // composited content graphics layer), substract the offset of the clipping layer, since it's its parent
1285         // that positions us (the graphics layer of the region).
1286         if (layerOwnerBacking->clippingLayer())
1287             ancestorCompositingBounds.moveBy(roundedIntPoint(layerOwnerBacking->clippingLayer()->position()));
1288     }
1289 }
1290
1291 void RenderLayerBacking::updateDirectlyCompositedBoxDecorations(PaintedContentsInfo& contentsInfo, bool& didUpdateContentsRect)
1292 {
1293     if (!m_owningLayer.hasVisibleContent())
1294         return;
1295
1296     // The order of operations here matters, since the last valid type of contents needs
1297     // to also update the contentsRect.
1298     updateDirectlyCompositedBackgroundColor(contentsInfo, didUpdateContentsRect);
1299     updateDirectlyCompositedBackgroundImage(contentsInfo, didUpdateContentsRect);
1300 }
1301
1302 void RenderLayerBacking::updateInternalHierarchy()
1303 {
1304     // m_foregroundLayer has to be inserted in the correct order with child layers,
1305     // so it's not inserted here.
1306     if (m_ancestorClippingLayer)
1307         m_ancestorClippingLayer->removeAllChildren();
1308     
1309     if (m_contentsContainmentLayer) {
1310         m_contentsContainmentLayer->removeAllChildren();
1311         if (m_ancestorClippingLayer)
1312             m_ancestorClippingLayer->addChild(m_contentsContainmentLayer.get());
1313     }
1314     
1315     if (m_backgroundLayer)
1316         m_contentsContainmentLayer->addChild(m_backgroundLayer.get());
1317
1318     if (m_contentsContainmentLayer)
1319         m_contentsContainmentLayer->addChild(m_graphicsLayer.get());
1320     else if (m_ancestorClippingLayer)
1321         m_ancestorClippingLayer->addChild(m_graphicsLayer.get());
1322
1323     if (m_childContainmentLayer) {
1324         m_childContainmentLayer->removeFromParent();
1325         m_graphicsLayer->addChild(m_childContainmentLayer.get());
1326     }
1327
1328     if (m_scrollingLayer) {
1329         GraphicsLayer* superlayer = m_childContainmentLayer ? m_childContainmentLayer.get() : m_graphicsLayer.get();
1330         m_scrollingLayer->removeFromParent();
1331         superlayer->addChild(m_scrollingLayer.get());
1332     }
1333
1334     // The clip for child layers does not include space for overflow controls, so they exist as
1335     // siblings of the clipping layer if we have one. Normal children of this layer are set as
1336     // children of the clipping layer.
1337     if (m_layerForHorizontalScrollbar) {
1338         m_layerForHorizontalScrollbar->removeFromParent();
1339         m_graphicsLayer->addChild(m_layerForHorizontalScrollbar.get());
1340     }
1341     if (m_layerForVerticalScrollbar) {
1342         m_layerForVerticalScrollbar->removeFromParent();
1343         m_graphicsLayer->addChild(m_layerForVerticalScrollbar.get());
1344     }
1345     if (m_layerForScrollCorner) {
1346         m_layerForScrollCorner->removeFromParent();
1347         m_graphicsLayer->addChild(m_layerForScrollCorner.get());
1348     }
1349 }
1350
1351 void RenderLayerBacking::resetContentsRect()
1352 {
1353     m_graphicsLayer->setContentsRect(snapRectToDevicePixels(contentsBox(), deviceScaleFactor()));
1354     
1355     if (is<RenderBox>(renderer())) {
1356         LayoutRect boxRect(LayoutPoint(), downcast<RenderBox>(renderer()).size());
1357         boxRect.move(contentOffsetInCompostingLayer());
1358         FloatRoundedRect contentsClippingRect = renderer().style().getRoundedInnerBorderFor(boxRect).pixelSnappedRoundedRectForPainting(deviceScaleFactor());
1359         m_graphicsLayer->setContentsClippingRect(contentsClippingRect);
1360     }
1361
1362     m_graphicsLayer->setContentsTileSize(IntSize());
1363     m_graphicsLayer->setContentsTilePhase(IntSize());
1364 }
1365
1366 void RenderLayerBacking::updateDrawsContent()
1367 {
1368     PaintedContentsInfo contentsInfo(*this);
1369     contentsInfo.setWantsSubpixelAntialiasedTextState(GraphicsLayer::supportsSubpixelAntialiasedLayerText());
1370
1371     updateDrawsContent(contentsInfo);
1372 }
1373
1374 void RenderLayerBacking::updateDrawsContent(PaintedContentsInfo& contentsInfo)
1375 {
1376     if (m_scrollingLayer) {
1377         // We don't have to consider overflow controls, because we know that the scrollbars are drawn elsewhere.
1378         // m_graphicsLayer only needs backing store if the non-scrolling parts (background, outlines, borders, shadows etc) need to paint.
1379         // m_scrollingLayer never has backing store.
1380         // m_scrollingContentsLayer only needs backing store if the scrolled contents need to paint.
1381         bool hasNonScrollingPaintedContent = m_owningLayer.hasVisibleContent() && m_owningLayer.hasVisibleBoxDecorationsOrBackground();
1382         m_graphicsLayer->setDrawsContent(hasNonScrollingPaintedContent);
1383
1384         bool hasScrollingPaintedContent = m_owningLayer.hasVisibleContent() && (renderer().hasBackground() || contentsInfo.paintsContent());
1385         m_scrollingContentsLayer->setDrawsContent(hasScrollingPaintedContent);
1386         return;
1387     }
1388
1389     bool hasPaintedContent = containsPaintedContent(contentsInfo);
1390
1391     m_paintsSubpixelAntialiasedText = renderer().settings().subpixelAntialiasedLayerTextEnabled() && contentsInfo.paintsSubpixelAntialiasedText();
1392
1393     // FIXME: we could refine this to only allocate backing for one of these layers if possible.
1394     m_graphicsLayer->setDrawsContent(hasPaintedContent);
1395     if (m_foregroundLayer) {
1396         m_foregroundLayer->setDrawsContent(hasPaintedContent);
1397         m_foregroundLayer->setSupportsSubpixelAntialiasedText(m_paintsSubpixelAntialiasedText);
1398         // The text content is painted into the foreground layer.
1399         // FIXME: this ignores SVG background images which may contain text.
1400         m_graphicsLayer->setSupportsSubpixelAntialiasedText(false);
1401     } else
1402         m_graphicsLayer->setSupportsSubpixelAntialiasedText(m_paintsSubpixelAntialiasedText);
1403
1404     if (m_backgroundLayer)
1405         m_backgroundLayer->setDrawsContent(hasPaintedContent);
1406 }
1407
1408 // Return true if the layer changed.
1409 bool RenderLayerBacking::updateAncestorClippingLayer(bool needsAncestorClip)
1410 {
1411     bool layersChanged = false;
1412
1413     if (needsAncestorClip) {
1414         if (!m_ancestorClippingLayer) {
1415             m_ancestorClippingLayer = createGraphicsLayer("ancestor clipping");
1416             m_ancestorClippingLayer->setMasksToBounds(true);
1417             layersChanged = true;
1418         }
1419     } else if (hasAncestorClippingLayer()) {
1420         willDestroyLayer(m_ancestorClippingLayer.get());
1421         m_ancestorClippingLayer->removeFromParent();
1422         m_ancestorClippingLayer = nullptr;
1423         layersChanged = true;
1424     }
1425     
1426     return layersChanged;
1427 }
1428
1429 // Return true if the layer changed.
1430 bool RenderLayerBacking::updateDescendantClippingLayer(bool needsDescendantClip)
1431 {
1432     bool layersChanged = false;
1433
1434     if (needsDescendantClip) {
1435         if (!m_childContainmentLayer && !m_isMainFrameLayerWithTiledBacking) {
1436             m_childContainmentLayer = createGraphicsLayer("child clipping");
1437             m_childContainmentLayer->setMasksToBounds(true);
1438             layersChanged = true;
1439         }
1440     } else if (hasClippingLayer()) {
1441         willDestroyLayer(m_childContainmentLayer.get());
1442         m_childContainmentLayer->removeFromParent();
1443         m_childContainmentLayer = nullptr;
1444         layersChanged = true;
1445     }
1446     
1447     return layersChanged;
1448 }
1449
1450 void RenderLayerBacking::setBackgroundLayerPaintsFixedRootBackground(bool backgroundLayerPaintsFixedRootBackground)
1451 {
1452     m_backgroundLayerPaintsFixedRootBackground = backgroundLayerPaintsFixedRootBackground;
1453 }
1454
1455 bool RenderLayerBacking::requiresHorizontalScrollbarLayer() const
1456 {
1457     if (!m_owningLayer.hasOverlayScrollbars() && !m_owningLayer.needsCompositedScrolling())
1458         return false;
1459     return m_owningLayer.horizontalScrollbar();
1460 }
1461
1462 bool RenderLayerBacking::requiresVerticalScrollbarLayer() const
1463 {
1464     if (!m_owningLayer.hasOverlayScrollbars() && !m_owningLayer.needsCompositedScrolling())
1465         return false;
1466     return m_owningLayer.verticalScrollbar();
1467 }
1468
1469 bool RenderLayerBacking::requiresScrollCornerLayer() const
1470 {
1471     if (!m_owningLayer.hasOverlayScrollbars() && !m_owningLayer.needsCompositedScrolling())
1472         return false;
1473     return !m_owningLayer.scrollCornerAndResizerRect().isEmpty();
1474 }
1475
1476 bool RenderLayerBacking::updateOverflowControlsLayers(bool needsHorizontalScrollbarLayer, bool needsVerticalScrollbarLayer, bool needsScrollCornerLayer)
1477 {
1478     bool horizontalScrollbarLayerChanged = false;
1479     if (needsHorizontalScrollbarLayer) {
1480         if (!m_layerForHorizontalScrollbar) {
1481             m_layerForHorizontalScrollbar = createGraphicsLayer("horizontal scrollbar");
1482             horizontalScrollbarLayerChanged = true;
1483         }
1484     } else if (m_layerForHorizontalScrollbar) {
1485         willDestroyLayer(m_layerForHorizontalScrollbar.get());
1486         m_layerForHorizontalScrollbar = nullptr;
1487         horizontalScrollbarLayerChanged = true;
1488     }
1489
1490     bool verticalScrollbarLayerChanged = false;
1491     if (needsVerticalScrollbarLayer) {
1492         if (!m_layerForVerticalScrollbar) {
1493             m_layerForVerticalScrollbar = createGraphicsLayer("vertical scrollbar");
1494             verticalScrollbarLayerChanged = true;
1495         }
1496     } else if (m_layerForVerticalScrollbar) {
1497         willDestroyLayer(m_layerForVerticalScrollbar.get());
1498         m_layerForVerticalScrollbar = nullptr;
1499         verticalScrollbarLayerChanged = true;
1500     }
1501
1502     bool scrollCornerLayerChanged = false;
1503     if (needsScrollCornerLayer) {
1504         if (!m_layerForScrollCorner) {
1505             m_layerForScrollCorner = createGraphicsLayer("scroll corner");
1506             scrollCornerLayerChanged = true;
1507         }
1508     } else if (m_layerForScrollCorner) {
1509         willDestroyLayer(m_layerForScrollCorner.get());
1510         m_layerForScrollCorner = nullptr;
1511         scrollCornerLayerChanged = true;
1512     }
1513
1514     if (auto* scrollingCoordinator = m_owningLayer.page().scrollingCoordinator()) {
1515         if (horizontalScrollbarLayerChanged)
1516             scrollingCoordinator->scrollableAreaScrollbarLayerDidChange(m_owningLayer, HorizontalScrollbar);
1517         if (verticalScrollbarLayerChanged)
1518             scrollingCoordinator->scrollableAreaScrollbarLayerDidChange(m_owningLayer, VerticalScrollbar);
1519     }
1520
1521     return horizontalScrollbarLayerChanged || verticalScrollbarLayerChanged || scrollCornerLayerChanged;
1522 }
1523
1524 void RenderLayerBacking::positionOverflowControlsLayers()
1525 {
1526     if (!m_owningLayer.hasScrollbars())
1527         return;
1528
1529     const IntRect borderBox = snappedIntRect(renderBox()->borderBoxRect());
1530
1531     FloatSize offsetFromRenderer = m_graphicsLayer->offsetFromRenderer();
1532     if (GraphicsLayer* layer = layerForHorizontalScrollbar()) {
1533         IntRect hBarRect = m_owningLayer.rectForHorizontalScrollbar(borderBox);
1534         layer->setPosition(hBarRect.location() - offsetFromRenderer);
1535         layer->setSize(hBarRect.size());
1536         if (layer->usesContentsLayer()) {
1537             IntRect barRect = IntRect(IntPoint(), hBarRect.size());
1538             layer->setContentsRect(barRect);
1539             layer->setContentsClippingRect(FloatRoundedRect(barRect));
1540         }
1541         layer->setDrawsContent(m_owningLayer.horizontalScrollbar() && !layer->usesContentsLayer());
1542     }
1543     
1544     if (GraphicsLayer* layer = layerForVerticalScrollbar()) {
1545         IntRect vBarRect = m_owningLayer.rectForVerticalScrollbar(borderBox);
1546         layer->setPosition(vBarRect.location() - offsetFromRenderer);
1547         layer->setSize(vBarRect.size());
1548         if (layer->usesContentsLayer()) {
1549             IntRect barRect = IntRect(IntPoint(), vBarRect.size());
1550             layer->setContentsRect(barRect);
1551             layer->setContentsClippingRect(FloatRoundedRect(barRect));
1552         }
1553         layer->setDrawsContent(m_owningLayer.verticalScrollbar() && !layer->usesContentsLayer());
1554     }
1555
1556     if (GraphicsLayer* layer = layerForScrollCorner()) {
1557         const LayoutRect& scrollCornerAndResizer = m_owningLayer.scrollCornerAndResizerRect();
1558         layer->setPosition(scrollCornerAndResizer.location() - offsetFromRenderer);
1559         layer->setSize(scrollCornerAndResizer.size());
1560         layer->setDrawsContent(!scrollCornerAndResizer.isEmpty());
1561     }
1562 }
1563
1564 bool RenderLayerBacking::hasUnpositionedOverflowControlsLayers() const
1565 {
1566     if (GraphicsLayer* layer = layerForHorizontalScrollbar())
1567         if (!layer->drawsContent())
1568             return true;
1569
1570     if (GraphicsLayer* layer = layerForVerticalScrollbar())
1571         if (!layer->drawsContent())
1572             return true;
1573
1574     if (GraphicsLayer* layer = layerForScrollCorner())
1575         if (!layer->drawsContent())
1576             return true;
1577
1578     return false;
1579 }
1580
1581 bool RenderLayerBacking::updateForegroundLayer(bool needsForegroundLayer)
1582 {
1583     bool layerChanged = false;
1584     if (needsForegroundLayer) {
1585         if (!m_foregroundLayer) {
1586             String layerName = m_owningLayer.name() + " (foreground)";
1587             m_foregroundLayer = createGraphicsLayer(layerName);
1588             m_foregroundLayer->setDrawsContent(true);
1589             m_foregroundLayer->setPaintingPhase(GraphicsLayerPaintForeground);
1590             layerChanged = true;
1591         }
1592     } else if (m_foregroundLayer) {
1593         willDestroyLayer(m_foregroundLayer.get());
1594         m_foregroundLayer->removeFromParent();
1595         m_foregroundLayer = nullptr;
1596         layerChanged = true;
1597     }
1598
1599     if (layerChanged) {
1600         m_graphicsLayer->setNeedsDisplay();
1601         m_graphicsLayer->setPaintingPhase(paintingPhaseForPrimaryLayer());
1602     }
1603
1604     return layerChanged;
1605 }
1606
1607 bool RenderLayerBacking::updateBackgroundLayer(bool needsBackgroundLayer)
1608 {
1609     bool layerChanged = false;
1610     if (needsBackgroundLayer) {
1611         if (!m_backgroundLayer) {
1612             String layerName = m_owningLayer.name() + " (background)";
1613             m_backgroundLayer = createGraphicsLayer(layerName);
1614             m_backgroundLayer->setDrawsContent(true);
1615             m_backgroundLayer->setAnchorPoint(FloatPoint3D());
1616             m_backgroundLayer->setPaintingPhase(GraphicsLayerPaintBackground);
1617             layerChanged = true;
1618         }
1619         
1620         if (!m_contentsContainmentLayer) {
1621             String layerName = m_owningLayer.name() + " (contents containment)";
1622             m_contentsContainmentLayer = createGraphicsLayer(layerName);
1623             m_contentsContainmentLayer->setAppliesPageScale(true);
1624             m_graphicsLayer->setAppliesPageScale(false);
1625             layerChanged = true;
1626         }
1627     } else {
1628         if (m_backgroundLayer) {
1629             willDestroyLayer(m_backgroundLayer.get());
1630             m_backgroundLayer->removeFromParent();
1631             m_backgroundLayer = nullptr;
1632             layerChanged = true;
1633         }
1634         if (m_contentsContainmentLayer) {
1635             willDestroyLayer(m_contentsContainmentLayer.get());
1636             m_contentsContainmentLayer->removeFromParent();
1637             m_contentsContainmentLayer = nullptr;
1638             layerChanged = true;
1639             m_graphicsLayer->setAppliesPageScale(true);
1640         }
1641     }
1642     
1643     if (layerChanged) {
1644         m_graphicsLayer->setNeedsDisplay();
1645         // This assumes that the background layer is only used for fixed backgrounds, which is currently a correct assumption.
1646         compositor().fixedRootBackgroundLayerChanged();
1647     }
1648     
1649     return layerChanged;
1650 }
1651
1652 // Masking layer is used for masks or clip-path.
1653 void RenderLayerBacking::updateMaskingLayer(bool hasMask, bool hasClipPath)
1654 {
1655     bool layerChanged = false;
1656     if (hasMask || hasClipPath) {
1657         GraphicsLayerPaintingPhase maskPhases = 0;
1658         if (hasMask)
1659             maskPhases = GraphicsLayerPaintMask;
1660         
1661         if (hasClipPath) {
1662             // If we have a mask, we need to paint the combined clip-path and mask into the mask layer.
1663             if (hasMask || renderer().style().clipPath()->type() == ClipPathOperation::Reference || !GraphicsLayer::supportsLayerType(GraphicsLayer::Type::Shape))
1664                 maskPhases |= GraphicsLayerPaintClipPath;
1665         }
1666
1667         bool paintsContent = maskPhases;
1668         GraphicsLayer::Type requiredLayerType = paintsContent ? GraphicsLayer::Type::Normal : GraphicsLayer::Type::Shape;
1669         if (m_maskLayer && m_maskLayer->type() != requiredLayerType) {
1670             m_graphicsLayer->setMaskLayer(nullptr);
1671             willDestroyLayer(m_maskLayer.get());
1672             m_maskLayer = nullptr;
1673         }
1674
1675         if (!m_maskLayer) {
1676             m_maskLayer = createGraphicsLayer("mask", requiredLayerType);
1677             m_maskLayer->setDrawsContent(paintsContent);
1678             m_maskLayer->setPaintingPhase(maskPhases);
1679             layerChanged = true;
1680             m_graphicsLayer->setMaskLayer(m_maskLayer.get());
1681         }
1682     } else if (m_maskLayer) {
1683         m_graphicsLayer->setMaskLayer(nullptr);
1684         willDestroyLayer(m_maskLayer.get());
1685         m_maskLayer = nullptr;
1686         layerChanged = true;
1687     }
1688
1689     if (layerChanged)
1690         m_graphicsLayer->setPaintingPhase(paintingPhaseForPrimaryLayer());
1691 }
1692
1693 void RenderLayerBacking::updateChildClippingStrategy(bool needsDescendantsClippingLayer)
1694 {
1695     if (hasClippingLayer() && needsDescendantsClippingLayer) {
1696         if (is<RenderBox>(renderer()) && (renderer().style().clipPath() || renderer().style().hasBorderRadius())) {
1697             // FIXME: we shouldn't get geometry here as layout may not have been udpated.
1698             LayoutRect boxRect(LayoutPoint(), downcast<RenderBox>(renderer()).size());
1699             boxRect.move(contentOffsetInCompostingLayer());
1700             FloatRoundedRect contentsClippingRect = renderer().style().getRoundedInnerBorderFor(boxRect).pixelSnappedRoundedRectForPainting(deviceScaleFactor());
1701             if (clippingLayer()->setMasksToBoundsRect(contentsClippingRect)) {
1702                 if (m_childClippingMaskLayer) 
1703                     m_childClippingMaskLayer = nullptr;
1704                 return;
1705             }
1706
1707             if (!m_childClippingMaskLayer) {
1708                 m_childClippingMaskLayer = createGraphicsLayer("child clipping mask");
1709                 m_childClippingMaskLayer->setDrawsContent(true);
1710                 m_childClippingMaskLayer->setPaintingPhase(GraphicsLayerPaintChildClippingMask);
1711                 clippingLayer()->setMaskLayer(m_childClippingMaskLayer.get());
1712             }
1713         }
1714     } else {
1715         if (m_childClippingMaskLayer) {
1716             if (hasClippingLayer())
1717                 clippingLayer()->setMaskLayer(nullptr);
1718             m_childClippingMaskLayer = nullptr;
1719         } else 
1720             if (hasClippingLayer())
1721                 clippingLayer()->setMasksToBoundsRect(FloatRoundedRect(FloatRect(FloatPoint(), clippingLayer()->size())));
1722     }
1723 }
1724
1725 bool RenderLayerBacking::updateScrollingLayers(bool needsScrollingLayers)
1726 {
1727     if (needsScrollingLayers == !!m_scrollingLayer)
1728         return false;
1729
1730     if (!m_scrollingLayer) {
1731         // Outer layer which corresponds with the scroll view.
1732         m_scrollingLayer = createGraphicsLayer("scrolling container", GraphicsLayer::Type::Scrolling);
1733         m_scrollingLayer->setDrawsContent(false);
1734         m_scrollingLayer->setMasksToBounds(true);
1735
1736         // Inner layer which renders the content that scrolls.
1737         m_scrollingContentsLayer = createGraphicsLayer("scrolled Contents");
1738         m_scrollingContentsLayer->setDrawsContent(true);
1739
1740         GraphicsLayerPaintingPhase paintPhase = GraphicsLayerPaintOverflowContents | GraphicsLayerPaintCompositedScroll;
1741         if (!m_foregroundLayer)
1742             paintPhase |= GraphicsLayerPaintForeground;
1743         m_scrollingContentsLayer->setPaintingPhase(paintPhase);
1744         m_scrollingLayer->addChild(m_scrollingContentsLayer.get());
1745     } else {
1746         compositor().willRemoveScrollingLayerWithBacking(m_owningLayer, *this);
1747
1748         willDestroyLayer(m_scrollingLayer.get());
1749         willDestroyLayer(m_scrollingContentsLayer.get());
1750         m_scrollingLayer = nullptr;
1751         m_scrollingContentsLayer = nullptr;
1752     }
1753
1754     m_graphicsLayer->setPaintingPhase(paintingPhaseForPrimaryLayer());
1755     m_graphicsLayer->setNeedsDisplay(); // Because painting phases changed.
1756
1757     if (m_scrollingLayer)
1758         compositor().didAddScrollingLayer(m_owningLayer);
1759     
1760     return true;
1761 }
1762
1763 void RenderLayerBacking::detachFromScrollingCoordinator(LayerScrollCoordinationRoles roles)
1764 {
1765     if (!m_scrollingNodeID && !m_viewportConstrainedNodeID)
1766         return;
1767
1768     auto* scrollingCoordinator = m_owningLayer.page().scrollingCoordinator();
1769     if (!scrollingCoordinator)
1770         return;
1771
1772     if ((roles & Scrolling) && m_scrollingNodeID) {
1773         LOG(Compositing, "Detaching Scrolling node %" PRIu64, m_scrollingNodeID);
1774         scrollingCoordinator->detachFromStateTree(m_scrollingNodeID);
1775         m_scrollingNodeID = 0;
1776     }
1777     
1778     if ((roles & ViewportConstrained) && m_viewportConstrainedNodeID) {
1779         LOG(Compositing, "Detaching ViewportConstrained node %" PRIu64, m_viewportConstrainedNodeID);
1780         scrollingCoordinator->detachFromStateTree(m_viewportConstrainedNodeID);
1781         m_viewportConstrainedNodeID = 0;
1782     }
1783 }
1784
1785 void RenderLayerBacking::setIsScrollCoordinatedWithViewportConstrainedRole(bool viewportCoordinated)
1786 {
1787     m_graphicsLayer->setIsViewportConstrained(viewportCoordinated);
1788 }
1789
1790 GraphicsLayerPaintingPhase RenderLayerBacking::paintingPhaseForPrimaryLayer() const
1791 {
1792     unsigned phase = 0;
1793     if (!m_backgroundLayer)
1794         phase |= GraphicsLayerPaintBackground;
1795     if (!m_foregroundLayer)
1796         phase |= GraphicsLayerPaintForeground;
1797
1798     if (m_scrollingContentsLayer) {
1799         phase &= ~GraphicsLayerPaintForeground;
1800         phase |= GraphicsLayerPaintCompositedScroll;
1801     }
1802
1803     return static_cast<GraphicsLayerPaintingPhase>(phase);
1804 }
1805
1806 float RenderLayerBacking::compositingOpacity(float rendererOpacity) const
1807 {
1808     float finalOpacity = rendererOpacity;
1809     
1810     for (RenderLayer* curr = m_owningLayer.parent(); curr; curr = curr->parent()) {
1811         // We only care about parents that are stacking contexts.
1812         // Recall that opacity creates stacking context.
1813         if (!curr->isStackingContainer())
1814             continue;
1815         
1816         // If we found a compositing layer, we want to compute opacity
1817         // relative to it. So we can break here.
1818         if (curr->isComposited())
1819             break;
1820         
1821         finalOpacity *= curr->renderer().opacity();
1822     }
1823
1824     return finalOpacity;
1825 }
1826
1827 // FIXME: Code is duplicated in RenderLayer. Also, we should probably not consider filters a box decoration here.
1828 static inline bool hasVisibleBoxDecorations(const RenderStyle& style)
1829 {
1830     return style.hasVisibleBorder() || style.hasBorderRadius() || style.hasOutline() || style.hasAppearance() || style.boxShadow() || style.hasFilter();
1831 }
1832
1833 static bool canDirectlyCompositeBackgroundBackgroundImage(const RenderStyle& style)
1834 {
1835     if (!GraphicsLayer::supportsContentsTiling())
1836         return false;
1837
1838     auto& fillLayer = style.backgroundLayers();
1839     if (fillLayer.next())
1840         return false;
1841
1842     if (!fillLayer.imagesAreLoaded())
1843         return false;
1844
1845     if (fillLayer.attachment() != ScrollBackgroundAttachment)
1846         return false;
1847
1848     // FIXME: Allow color+image compositing when it makes sense.
1849     // For now bailing out.
1850     if (style.visitedDependentColor(CSSPropertyBackgroundColor).isVisible())
1851         return false;
1852
1853     // FIXME: support gradients with isGeneratedImage.
1854     auto* styleImage = fillLayer.image();
1855     if (!styleImage->isCachedImage())
1856         return false;
1857
1858     auto* image = styleImage->cachedImage()->image();
1859     if (!image->isBitmapImage())
1860         return false;
1861
1862     return true;
1863 }
1864
1865 static bool hasPaintedBoxDecorationsOrBackgroundImage(const RenderStyle& style)
1866 {
1867     if (hasVisibleBoxDecorations(style))
1868         return true;
1869
1870     if (!style.hasBackgroundImage())
1871         return false;
1872
1873     return !canDirectlyCompositeBackgroundBackgroundImage(style);
1874 }
1875
1876 static inline bool hasPerspectiveOrPreserves3D(const RenderStyle& style)
1877 {
1878     return style.hasPerspective() || style.preserves3D();
1879 }
1880
1881 Color RenderLayerBacking::rendererBackgroundColor() const
1882 {
1883     RenderElement* backgroundRenderer = nullptr;
1884     if (renderer().isDocumentElementRenderer())
1885         backgroundRenderer = renderer().view().rendererForRootBackground();
1886     
1887     if (!backgroundRenderer)
1888         backgroundRenderer = &renderer();
1889
1890     return backgroundRenderer->style().visitedDependentColor(CSSPropertyBackgroundColor);
1891 }
1892
1893 void RenderLayerBacking::updateDirectlyCompositedBackgroundColor(PaintedContentsInfo& contentsInfo, bool& didUpdateContentsRect)
1894 {
1895     if (!contentsInfo.isSimpleContainer() || (is<RenderBox>(renderer()) && !downcast<RenderBox>(renderer()).paintsOwnBackground())) {
1896         m_graphicsLayer->setContentsToSolidColor(Color());
1897         return;
1898     }
1899
1900     Color backgroundColor = rendererBackgroundColor();
1901
1902     // An unset (invalid) color will remove the solid color.
1903     m_graphicsLayer->setContentsToSolidColor(backgroundColor);
1904     FloatRect contentsRect = backgroundBoxForSimpleContainerPainting();
1905     m_graphicsLayer->setContentsRect(contentsRect);
1906     m_graphicsLayer->setContentsClippingRect(FloatRoundedRect(contentsRect));
1907     didUpdateContentsRect = true;
1908 }
1909
1910 void RenderLayerBacking::updateDirectlyCompositedBackgroundImage(PaintedContentsInfo& contentsInfo, bool& didUpdateContentsRect)
1911 {
1912     if (!GraphicsLayer::supportsContentsTiling())
1913         return;
1914
1915     if (contentsInfo.isDirectlyCompositedImage())
1916         return;
1917
1918     auto& style = renderer().style();
1919     if (!contentsInfo.isSimpleContainer() || !style.hasBackgroundImage()) {
1920         m_graphicsLayer->setContentsToImage(0);
1921         return;
1922     }
1923
1924     auto destRect = backgroundBoxForSimpleContainerPainting();
1925     FloatSize phase;
1926     FloatSize tileSize;
1927     // FIXME: Absolute paint location is required here.
1928     downcast<RenderBox>(renderer()).getGeometryForBackgroundImage(&renderer(), LayoutPoint(), destRect, phase, tileSize);
1929
1930     m_graphicsLayer->setContentsTileSize(tileSize);
1931     m_graphicsLayer->setContentsTilePhase(phase);
1932     m_graphicsLayer->setContentsRect(destRect);
1933     m_graphicsLayer->setContentsClippingRect(FloatRoundedRect(destRect));
1934     m_graphicsLayer->setContentsToImage(style.backgroundLayers().image()->cachedImage()->image());
1935
1936     didUpdateContentsRect = true;
1937 }
1938
1939 void RenderLayerBacking::updateRootLayerConfiguration()
1940 {
1941     if (!m_isMainFrameLayerWithTiledBacking)
1942         return;
1943
1944     Color backgroundColor;
1945     bool viewIsTransparent = compositor().viewHasTransparentBackground(&backgroundColor);
1946
1947     if (m_backgroundLayerPaintsFixedRootBackground && m_backgroundLayer) {
1948         m_backgroundLayer->setBackgroundColor(backgroundColor);
1949         m_backgroundLayer->setContentsOpaque(!viewIsTransparent);
1950
1951         m_graphicsLayer->setBackgroundColor(Color());
1952         m_graphicsLayer->setContentsOpaque(false);
1953     } else {
1954         m_graphicsLayer->setBackgroundColor(backgroundColor);
1955         m_graphicsLayer->setContentsOpaque(!viewIsTransparent);
1956     }
1957 }
1958
1959 static bool supportsDirectlyCompositedBoxDecorations(const RenderLayerModelObject& renderer)
1960 {
1961     if (!GraphicsLayer::supportsBackgroundColorContent())
1962         return false;
1963
1964     const RenderStyle& style = renderer.style();
1965     if (renderer.hasClip())
1966         return false;
1967
1968     if (hasPaintedBoxDecorationsOrBackgroundImage(style))
1969         return false;
1970
1971     // FIXME: We can't create a directly composited background if this
1972     // layer will have children that intersect with the background layer.
1973     // A better solution might be to introduce a flattening layer if
1974     // we do direct box decoration composition.
1975     // https://bugs.webkit.org/show_bug.cgi?id=119461
1976     if (hasPerspectiveOrPreserves3D(style))
1977         return false;
1978
1979     // FIXME: we should be able to allow backgroundComposite; However since this is not a common use case it has been deferred for now.
1980     if (style.backgroundComposite() != CompositeSourceOver)
1981         return false;
1982
1983     return true;
1984 }
1985
1986 bool RenderLayerBacking::paintsBoxDecorations() const
1987 {
1988     if (!m_owningLayer.hasVisibleBoxDecorations())
1989         return false;
1990
1991     return !supportsDirectlyCompositedBoxDecorations(renderer());
1992 }
1993
1994 bool RenderLayerBacking::paintsContent(RenderLayer::PaintedContentRequest& request) const
1995 {
1996     bool paintsContent = false;
1997
1998     if (m_owningLayer.hasVisibleContent() && m_owningLayer.hasNonEmptyChildRenderers(request))
1999         paintsContent = true;
2000
2001     if (request.isSatisfied())
2002         return paintsContent;
2003
2004     if (isPaintDestinationForDescendantLayers(request))
2005         paintsContent = true;
2006
2007     if (request.isSatisfied())
2008         return paintsContent;
2009
2010     if (request.hasPaintedContent == RequestState::Unknown)
2011         request.hasPaintedContent = RequestState::False;
2012
2013     if (request.hasSubpixelAntialiasedText == RequestState::Unknown)
2014         request.hasSubpixelAntialiasedText = RequestState::False;
2015
2016     return paintsContent;
2017 }
2018
2019 static bool isRestartedPlugin(RenderObject& renderer)
2020 {
2021     if (!is<RenderEmbeddedObject>(renderer))
2022         return false;
2023
2024     HTMLFrameOwnerElement& element = downcast<RenderEmbeddedObject>(renderer).frameOwnerElement();
2025     if (!is<HTMLPlugInElement>(element))
2026         return false;
2027
2028     return downcast<HTMLPlugInElement>(element).isRestartedPlugin();
2029 }
2030
2031 static bool isCompositedPlugin(RenderObject& renderer)
2032 {
2033     return is<RenderEmbeddedObject>(renderer) && downcast<RenderEmbeddedObject>(renderer).allowsAcceleratedCompositing();
2034 }
2035
2036 // A "simple container layer" is a RenderLayer which has no visible content to render.
2037 // It may have no children, or all its children may be themselves composited.
2038 // This is a useful optimization, because it allows us to avoid allocating backing store.
2039 bool RenderLayerBacking::isSimpleContainerCompositingLayer(PaintedContentsInfo& contentsInfo) const
2040 {
2041     if (renderer().isRenderReplaced() && (!isCompositedPlugin(renderer()) || isRestartedPlugin(renderer())))
2042         return false;
2043
2044     if (renderer().isTextControl())
2045         return false;
2046
2047     if (contentsInfo.paintsBoxDecorations() || contentsInfo.paintsContent())
2048         return false;
2049
2050     if (renderer().style().backgroundClip() == TextFillBox)
2051         return false;
2052     
2053     if (renderer().isRenderNamedFlowFragmentContainer())
2054         return false;
2055
2056     if (renderer().isDocumentElementRenderer() && m_owningLayer.isolatesCompositedBlending())
2057         return false;
2058
2059     if (renderer().isRenderView()) {
2060         // Look to see if the root object has a non-simple background
2061         auto* rootObject = renderer().document().documentElement() ? renderer().document().documentElement()->renderer() : nullptr;
2062         if (!rootObject)
2063             return false;
2064         
2065         // Reject anything that has a border, a border-radius or outline,
2066         // or is not a simple background (no background, or solid color).
2067         if (hasPaintedBoxDecorationsOrBackgroundImage(rootObject->style()))
2068             return false;
2069         
2070         // Now look at the body's renderer.
2071         auto* body = renderer().document().body();
2072         if (!body)
2073             return false;
2074         auto* bodyRenderer = body->renderer();
2075         if (!bodyRenderer)
2076             return false;
2077         
2078         if (hasPaintedBoxDecorationsOrBackgroundImage(bodyRenderer->style()))
2079             return false;
2080     }
2081
2082     return true;
2083 }
2084
2085 // Returning true stops the traversal.
2086 enum class LayerTraversal { Continue, Stop };
2087
2088 static LayerTraversal traverseVisibleNonCompositedDescendantLayers(RenderLayer& parent, std::function<LayerTraversal (const RenderLayer&)> layerFunc)
2089 {
2090     // FIXME: We shouldn't be called with a stale z-order lists. See bug 85512.
2091     parent.updateLayerListsIfNeeded();
2092
2093 #if !ASSERT_DISABLED
2094     LayerListMutationDetector mutationChecker(&parent);
2095 #endif
2096
2097     if (auto* normalFlowList = parent.normalFlowList()) {
2098         for (auto* childLayer : *normalFlowList) {
2099             if (compositedWithOwnBackingStore(*childLayer))
2100                 continue;
2101
2102             if (layerFunc(*childLayer) == LayerTraversal::Stop)
2103                 return LayerTraversal::Stop;
2104             
2105             if (traverseVisibleNonCompositedDescendantLayers(*childLayer, layerFunc) == LayerTraversal::Stop)
2106                 return LayerTraversal::Stop;
2107         }
2108     }
2109
2110     if (parent.isStackingContainer()) {
2111         if (!parent.hasVisibleDescendant())
2112             return LayerTraversal::Continue;
2113
2114         // Use the m_hasCompositingDescendant bit to optimize?
2115         if (auto* negZOrderList = parent.negZOrderList()) {
2116             for (auto* childLayer : *negZOrderList) {
2117                 if (compositedWithOwnBackingStore(*childLayer))
2118                     continue;
2119
2120                 if (layerFunc(*childLayer) == LayerTraversal::Stop)
2121                     return LayerTraversal::Stop;
2122
2123                 if (traverseVisibleNonCompositedDescendantLayers(*childLayer, layerFunc) == LayerTraversal::Stop)
2124                     return LayerTraversal::Stop;
2125             }
2126         }
2127
2128         if (auto* posZOrderList = parent.posZOrderList()) {
2129             for (auto* childLayer : *posZOrderList) {
2130                 if (compositedWithOwnBackingStore(*childLayer))
2131                     continue;
2132
2133                 if (layerFunc(*childLayer) == LayerTraversal::Stop)
2134                     return LayerTraversal::Stop;
2135
2136                 if (traverseVisibleNonCompositedDescendantLayers(*childLayer, layerFunc) == LayerTraversal::Stop)
2137                     return LayerTraversal::Stop;
2138             }
2139         }
2140     }
2141
2142     return LayerTraversal::Continue;
2143 }
2144
2145 // Conservative test for having no rendered children.
2146 bool RenderLayerBacking::isPaintDestinationForDescendantLayers(RenderLayer::PaintedContentRequest& request) const
2147 {
2148     bool hasPaintingDescendant = false;
2149     traverseVisibleNonCompositedDescendantLayers(m_owningLayer, [&hasPaintingDescendant, &request](const RenderLayer& layer) {
2150         hasPaintingDescendant |= layer.isVisuallyNonEmpty(&request);
2151         return (hasPaintingDescendant && request.isSatisfied()) ? LayerTraversal::Stop : LayerTraversal::Continue;
2152     });
2153
2154     return hasPaintingDescendant;
2155 }
2156
2157 bool RenderLayerBacking::hasVisibleNonCompositedDescendants() const
2158 {
2159     bool hasVisibleDescendant = false;
2160     traverseVisibleNonCompositedDescendantLayers(m_owningLayer, [&hasVisibleDescendant](const RenderLayer& layer) {
2161         hasVisibleDescendant |= layer.hasVisibleContent();
2162         return hasVisibleDescendant ? LayerTraversal::Stop : LayerTraversal::Continue;
2163     });
2164
2165     return hasVisibleDescendant;
2166 }
2167
2168 bool RenderLayerBacking::containsPaintedContent(PaintedContentsInfo& contentsInfo) const
2169 {
2170     if (contentsInfo.isSimpleContainer() || paintsIntoWindow() || paintsIntoCompositedAncestor() || m_artificiallyInflatedBounds || m_owningLayer.isReflection())
2171         return false;
2172
2173     if (contentsInfo.isDirectlyCompositedImage())
2174         return false;
2175
2176     // FIXME: we could optimize cases where the image, video or canvas is known to fill the border box entirely,
2177     // and set background color on the layer in that case, instead of allocating backing store and painting.
2178 #if ENABLE(VIDEO)
2179     if (is<RenderVideo>(renderer()) && downcast<RenderVideo>(renderer()).shouldDisplayVideo())
2180         return m_owningLayer.hasVisibleBoxDecorationsOrBackground() || (!(downcast<RenderVideo>(renderer()).supportsAcceleratedRendering()) && m_requiresOwnBackingStore);
2181 #endif
2182
2183 #if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
2184     if (is<RenderHTMLCanvas>(renderer()) && canvasCompositingStrategy(renderer()) == CanvasAsLayerContents)
2185         return m_owningLayer.hasVisibleBoxDecorationsOrBackground();
2186 #endif
2187
2188     return true;
2189 }
2190
2191 // An image can be directly compositing if it's the sole content of the layer, and has no box decorations
2192 // that require painting. Direct compositing saves backing store.
2193 bool RenderLayerBacking::isDirectlyCompositedImage() const
2194 {
2195     if (!is<RenderImage>(renderer()) || m_owningLayer.hasVisibleBoxDecorationsOrBackground() || m_owningLayer.paintsWithFilters() || renderer().hasClip())
2196         return false;
2197
2198 #if ENABLE(VIDEO)
2199     if (is<RenderMedia>(renderer()))
2200         return false;
2201 #endif
2202
2203     auto& imageRenderer = downcast<RenderImage>(renderer());
2204     if (CachedImage* cachedImage = imageRenderer.cachedImage()) {
2205         if (!cachedImage->hasImage())
2206             return false;
2207
2208         Image* image = cachedImage->imageForRenderer(&imageRenderer);
2209         if (!image->isBitmapImage())
2210             return false;
2211
2212         if (image->orientationForCurrentFrame() != DefaultImageOrientation)
2213             return false;
2214
2215         return m_graphicsLayer->shouldDirectlyCompositeImage(image);
2216     }
2217
2218     return false;
2219 }
2220
2221 void RenderLayerBacking::contentChanged(ContentChangeType changeType)
2222 {
2223     PaintedContentsInfo contentsInfo(*this);
2224     if ((changeType == ImageChanged) && contentsInfo.isDirectlyCompositedImage()) {
2225         updateImageContents(contentsInfo);
2226         return;
2227     }
2228
2229     if ((changeType == BackgroundImageChanged) && canDirectlyCompositeBackgroundBackgroundImage(renderer().style()))
2230         updateGeometry();
2231
2232     if ((changeType == MaskImageChanged) && m_maskLayer) {
2233         // The composited layer bounds relies on box->maskClipRect(), which changes
2234         // when the mask image becomes available.
2235         updateAfterLayout(CompositingChildrenOnly | IsUpdateRoot);
2236     }
2237
2238 #if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
2239     if ((changeType == CanvasChanged || changeType == CanvasPixelsChanged) && renderer().isCanvas() && canvasCompositingStrategy(renderer()) == CanvasAsLayerContents) {
2240         m_graphicsLayer->setContentsNeedsDisplay();
2241         return;
2242     }
2243 #endif
2244 }
2245
2246 void RenderLayerBacking::updateImageContents(PaintedContentsInfo& contentsInfo)
2247 {
2248     auto& imageRenderer = downcast<RenderImage>(renderer());
2249
2250     CachedImage* cachedImage = imageRenderer.cachedImage();
2251     if (!cachedImage)
2252         return;
2253
2254     Image* image = cachedImage->imageForRenderer(&imageRenderer);
2255     if (!image)
2256         return;
2257
2258     // We have to wait until the image is fully loaded before setting it on the layer.
2259     if (!cachedImage->isLoaded())
2260         return;
2261
2262     // This is a no-op if the layer doesn't have an inner layer for the image.
2263     m_graphicsLayer->setContentsRect(snapRectToDevicePixels(contentsBox(), deviceScaleFactor()));
2264
2265     LayoutRect boxRect(LayoutPoint(), imageRenderer.size());
2266     boxRect.move(contentOffsetInCompostingLayer());
2267     FloatRoundedRect contentsClippingRect = renderer().style().getRoundedInnerBorderFor(boxRect).pixelSnappedRoundedRectForPainting(deviceScaleFactor());
2268     m_graphicsLayer->setContentsClippingRect(contentsClippingRect);
2269
2270     m_graphicsLayer->setContentsToImage(image);
2271     
2272     updateDrawsContent(contentsInfo);
2273     
2274     // Image animation is "lazy", in that it automatically stops unless someone is drawing
2275     // the image. So we have to kick the animation each time; this has the downside that the
2276     // image will keep animating, even if its layer is not visible.
2277     image->startAnimation();
2278 }
2279
2280 FloatPoint3D RenderLayerBacking::computeTransformOriginForPainting(const LayoutRect& borderBox) const
2281 {
2282     const RenderStyle& style = renderer().style();
2283     float deviceScaleFactor = this->deviceScaleFactor();
2284
2285     FloatPoint3D origin;
2286     origin.setX(roundToDevicePixel(floatValueForLength(style.transformOriginX(), borderBox.width()), deviceScaleFactor));
2287     origin.setY(roundToDevicePixel(floatValueForLength(style.transformOriginY(), borderBox.height()), deviceScaleFactor));
2288     origin.setZ(style.transformOriginZ());
2289
2290     return origin;
2291 }
2292
2293 // Return the offset from the top-left of this compositing layer at which the renderer's contents are painted.
2294 LayoutSize RenderLayerBacking::contentOffsetInCompostingLayer() const
2295 {
2296     return LayoutSize(-m_compositedBounds.x() + m_compositedBoundsOffsetFromGraphicsLayer.width(), -m_compositedBounds.y() + m_compositedBoundsOffsetFromGraphicsLayer.height());
2297 }
2298
2299 LayoutRect RenderLayerBacking::contentsBox() const
2300 {
2301     if (!is<RenderBox>(renderer()))
2302         return LayoutRect();
2303
2304     auto& renderBox = downcast<RenderBox>(renderer());
2305     LayoutRect contentsRect;
2306 #if ENABLE(VIDEO)
2307     if (is<RenderVideo>(renderBox))
2308         contentsRect = downcast<RenderVideo>(renderBox).videoBox();
2309     else
2310 #endif
2311     if (is<RenderReplaced>(renderBox)) {
2312         RenderReplaced& renderReplaced = downcast<RenderReplaced>(renderBox);
2313         contentsRect = renderReplaced.replacedContentRect(renderBox.intrinsicSize());
2314     } else
2315         contentsRect = renderBox.contentBoxRect();
2316
2317     contentsRect.move(contentOffsetInCompostingLayer());
2318     return contentsRect;
2319 }
2320
2321 static LayoutRect backgroundRectForBox(const RenderBox& box)
2322 {
2323     switch (box.style().backgroundClip()) {
2324     case BorderFillBox:
2325         return box.borderBoxRect();
2326     case PaddingFillBox:
2327         return box.paddingBoxRect();
2328     case ContentFillBox:
2329         return box.contentBoxRect();
2330     default:
2331         break;
2332     }
2333
2334     ASSERT_NOT_REACHED();
2335     return LayoutRect();
2336 }
2337
2338 FloatRect RenderLayerBacking::backgroundBoxForSimpleContainerPainting() const
2339 {
2340     if (!is<RenderBox>(renderer()))
2341         return FloatRect();
2342
2343     LayoutRect backgroundBox = backgroundRectForBox(downcast<RenderBox>(renderer()));
2344     backgroundBox.move(contentOffsetInCompostingLayer());
2345     return snapRectToDevicePixels(backgroundBox, deviceScaleFactor());
2346 }
2347
2348 GraphicsLayer* RenderLayerBacking::parentForSublayers() const
2349 {
2350     if (m_scrollingContentsLayer)
2351         return m_scrollingContentsLayer.get();
2352
2353     return m_childContainmentLayer ? m_childContainmentLayer.get() : m_graphicsLayer.get();
2354 }
2355
2356 GraphicsLayer* RenderLayerBacking::childForSuperlayers() const
2357 {
2358     if (m_ancestorClippingLayer)
2359         return m_ancestorClippingLayer.get();
2360
2361     if (m_contentsContainmentLayer)
2362         return m_contentsContainmentLayer.get();
2363     
2364     return m_graphicsLayer.get();
2365 }
2366
2367 bool RenderLayerBacking::paintsIntoWindow() const
2368 {
2369 #if USE(COORDINATED_GRAPHICS_THREADED)
2370         return false;
2371 #endif
2372
2373     if (m_isMainFrameLayerWithTiledBacking)
2374         return false;
2375
2376     if (m_owningLayer.isRootLayer()) {
2377 #if PLATFORM(IOS) || USE(COORDINATED_GRAPHICS)
2378         if (compositor().inForcedCompositingMode())
2379             return false;
2380 #endif
2381
2382         return compositor().rootLayerAttachment() != RenderLayerCompositor::RootLayerAttachedViaEnclosingFrame;
2383     }
2384     
2385     return false;
2386 }
2387
2388 void RenderLayerBacking::setRequiresOwnBackingStore(bool requiresOwnBacking)
2389 {
2390     if (requiresOwnBacking == m_requiresOwnBackingStore)
2391         return;
2392     
2393     m_requiresOwnBackingStore = requiresOwnBacking;
2394
2395     // This affects the answer to paintsIntoCompositedAncestor(), which in turn affects
2396     // cached clip rects, so when it changes we have to clear clip rects on descendants.
2397     m_owningLayer.clearClipRectsIncludingDescendants(PaintingClipRects);
2398     m_owningLayer.computeRepaintRectsIncludingDescendants();
2399     
2400     compositor().repaintInCompositedAncestor(m_owningLayer, compositedBounds());
2401 }
2402
2403 void RenderLayerBacking::setContentsNeedDisplay(GraphicsLayer::ShouldClipToLayer shouldClip)
2404 {
2405     ASSERT(!paintsIntoCompositedAncestor());
2406
2407     FrameView& frameView = owningLayer().renderer().view().frameView();
2408     if (m_isMainFrameRenderViewLayer && frameView.isTrackingRepaints())
2409         frameView.addTrackedRepaintRect(owningLayer().absoluteBoundingBoxForPainting());
2410     
2411     if (m_graphicsLayer && m_graphicsLayer->drawsContent()) {
2412         // By default, setNeedsDisplay will clip to the size of the GraphicsLayer, which does not include margin tiles.
2413         // So if the TiledBacking has a margin that needs to be invalidated, we need to send in a rect to setNeedsDisplayInRect
2414         // that is large enough to include the margin. TiledBacking::bounds() includes the margin.
2415         TiledBacking* tiledBacking = this->tiledBacking();
2416         FloatRect rectToRepaint = tiledBacking ? tiledBacking->bounds() : FloatRect(FloatPoint(0, 0), m_graphicsLayer->size());
2417         m_graphicsLayer->setNeedsDisplayInRect(rectToRepaint, shouldClip);
2418     }
2419     
2420     if (m_foregroundLayer && m_foregroundLayer->drawsContent())
2421         m_foregroundLayer->setNeedsDisplay();
2422
2423     if (m_backgroundLayer && m_backgroundLayer->drawsContent())
2424         m_backgroundLayer->setNeedsDisplay();
2425
2426     if (m_maskLayer && m_maskLayer->drawsContent())
2427         m_maskLayer->setNeedsDisplay();
2428
2429     if (m_childClippingMaskLayer && m_childClippingMaskLayer->drawsContent())
2430         m_childClippingMaskLayer->setNeedsDisplay();
2431
2432     if (m_scrollingContentsLayer && m_scrollingContentsLayer->drawsContent())
2433         m_scrollingContentsLayer->setNeedsDisplay();
2434 }
2435
2436 // r is in the coordinate space of the layer's render object
2437 void RenderLayerBacking::setContentsNeedDisplayInRect(const LayoutRect& r, GraphicsLayer::ShouldClipToLayer shouldClip)
2438 {
2439     ASSERT(!paintsIntoCompositedAncestor());
2440
2441     FloatRect pixelSnappedRectForPainting = snapRectToDevicePixels(r, deviceScaleFactor());
2442     FrameView& frameView = owningLayer().renderer().view().frameView();
2443     if (m_isMainFrameRenderViewLayer && frameView.isTrackingRepaints())
2444         frameView.addTrackedRepaintRect(pixelSnappedRectForPainting);
2445
2446     if (m_graphicsLayer && m_graphicsLayer->drawsContent()) {
2447         FloatRect layerDirtyRect = pixelSnappedRectForPainting;
2448         layerDirtyRect.move(-m_graphicsLayer->offsetFromRenderer() - m_subpixelOffsetFromRenderer);
2449         m_graphicsLayer->setNeedsDisplayInRect(layerDirtyRect, shouldClip);
2450     }
2451
2452     if (m_foregroundLayer && m_foregroundLayer->drawsContent()) {
2453         FloatRect layerDirtyRect = pixelSnappedRectForPainting;
2454         layerDirtyRect.move(-m_foregroundLayer->offsetFromRenderer() - m_subpixelOffsetFromRenderer);
2455         m_foregroundLayer->setNeedsDisplayInRect(layerDirtyRect, shouldClip);
2456     }
2457
2458     // FIXME: need to split out repaints for the background.
2459     if (m_backgroundLayer && m_backgroundLayer->drawsContent()) {
2460         FloatRect layerDirtyRect = pixelSnappedRectForPainting;
2461         layerDirtyRect.move(-m_backgroundLayer->offsetFromRenderer() - m_subpixelOffsetFromRenderer);
2462         m_backgroundLayer->setNeedsDisplayInRect(layerDirtyRect, shouldClip);
2463     }
2464
2465     if (m_maskLayer && m_maskLayer->drawsContent()) {
2466         FloatRect layerDirtyRect = pixelSnappedRectForPainting;
2467         layerDirtyRect.move(-m_maskLayer->offsetFromRenderer() - m_subpixelOffsetFromRenderer);
2468         m_maskLayer->setNeedsDisplayInRect(layerDirtyRect, shouldClip);
2469     }
2470
2471     if (m_childClippingMaskLayer && m_childClippingMaskLayer->drawsContent()) {
2472         FloatRect layerDirtyRect = r;
2473         layerDirtyRect.move(-m_childClippingMaskLayer->offsetFromRenderer());
2474         m_childClippingMaskLayer->setNeedsDisplayInRect(layerDirtyRect);
2475     }
2476
2477     if (m_scrollingContentsLayer && m_scrollingContentsLayer->drawsContent()) {
2478         FloatRect layerDirtyRect = pixelSnappedRectForPainting;
2479         layerDirtyRect.move(-m_scrollingContentsLayer->offsetFromRenderer() - m_subpixelOffsetFromRenderer);
2480 #if PLATFORM(IOS)
2481         // Account for the fact that RenderLayerBacking::updateGeometry() bakes scrollOffset into offsetFromRenderer on iOS,
2482         // but the repaint rect is computed without taking the scroll position into account (see shouldApplyClipAndScrollPositionForRepaint()).
2483         layerDirtyRect.moveBy(-m_owningLayer.scrollPosition());
2484 #endif
2485         m_scrollingContentsLayer->setNeedsDisplayInRect(layerDirtyRect, shouldClip);
2486     }
2487 }
2488
2489 void RenderLayerBacking::paintIntoLayer(const GraphicsLayer* graphicsLayer, GraphicsContext& context,
2490     const IntRect& paintDirtyRect, // In the coords of rootLayer.
2491     PaintBehavior paintBehavior, GraphicsLayerPaintingPhase paintingPhase)
2492 {
2493     if ((paintsIntoWindow() || paintsIntoCompositedAncestor()) && paintingPhase != GraphicsLayerPaintChildClippingMask) {
2494 #if !PLATFORM(IOS) && !OS(WINDOWS)
2495         // FIXME: Looks like the CALayer tree is out of sync with the GraphicsLayer heirarchy
2496         // when pages are restored from the PageCache.
2497         // <rdar://problem/8712587> ASSERT: When Going Back to Page with Plugins in PageCache
2498         ASSERT_NOT_REACHED();
2499 #endif
2500         return;
2501     }
2502
2503     RenderLayer::PaintLayerFlags paintFlags = 0;
2504     if (paintingPhase & GraphicsLayerPaintBackground)
2505         paintFlags |= RenderLayer::PaintLayerPaintingCompositingBackgroundPhase;
2506     if (paintingPhase & GraphicsLayerPaintForeground)
2507         paintFlags |= RenderLayer::PaintLayerPaintingCompositingForegroundPhase;
2508     if (paintingPhase & GraphicsLayerPaintMask)
2509         paintFlags |= RenderLayer::PaintLayerPaintingCompositingMaskPhase;
2510     if (paintingPhase & GraphicsLayerPaintClipPath)
2511         paintFlags |= RenderLayer::PaintLayerPaintingCompositingClipPathPhase;
2512     if (paintingPhase & GraphicsLayerPaintChildClippingMask)
2513         paintFlags |= RenderLayer::PaintLayerPaintingChildClippingMaskPhase;
2514     if (paintingPhase & GraphicsLayerPaintOverflowContents)
2515         paintFlags |= RenderLayer::PaintLayerPaintingOverflowContents;
2516     if (paintingPhase & GraphicsLayerPaintCompositedScroll)
2517         paintFlags |= RenderLayer::PaintLayerPaintingCompositingScrollingPhase;
2518
2519     if (graphicsLayer == m_backgroundLayer.get())
2520         paintFlags |= (RenderLayer::PaintLayerPaintingRootBackgroundOnly | RenderLayer::PaintLayerPaintingCompositingForegroundPhase); // Need PaintLayerPaintingCompositingForegroundPhase to walk child layers.
2521     else if (compositor().fixedRootBackgroundLayer())
2522         paintFlags |= RenderLayer::PaintLayerPaintingSkipRootBackground;
2523
2524 #ifndef NDEBUG
2525     RenderElement::SetLayoutNeededForbiddenScope forbidSetNeedsLayout(&renderer());
2526 #endif
2527
2528     FrameView::PaintingState paintingState;
2529     if (m_owningLayer.isRootLayer())
2530         renderer().view().frameView().willPaintContents(context, paintDirtyRect, paintingState);
2531
2532     // FIXME: GraphicsLayers need a way to split for RenderRegions.
2533     RenderLayer::LayerPaintingInfo paintingInfo(&m_owningLayer, paintDirtyRect, paintBehavior, -m_subpixelOffsetFromRenderer);
2534     m_owningLayer.paintLayerContents(context, paintingInfo, paintFlags);
2535
2536     if (m_owningLayer.containsDirtyOverlayScrollbars())
2537         m_owningLayer.paintLayerContents(context, paintingInfo, paintFlags | RenderLayer::PaintLayerPaintingOverlayScrollbars);
2538
2539     if (m_owningLayer.isRootLayer())
2540         renderer().view().frameView().didPaintContents(context, paintDirtyRect, paintingState);
2541
2542     compositor().didPaintBacking(this);
2543
2544     ASSERT(!m_owningLayer.m_usedTransparency);
2545 }
2546
2547 // Up-call from compositing layer drawing callback.
2548 void RenderLayerBacking::paintContents(const GraphicsLayer* graphicsLayer, GraphicsContext& context, GraphicsLayerPaintingPhase paintingPhase, const FloatRect& clip)
2549 {
2550 #ifndef NDEBUG
2551     renderer().page().setIsPainting(true);
2552 #endif
2553
2554     // The dirtyRect is in the coords of the painting root.
2555     FloatRect adjustedClipRect = clip;
2556     adjustedClipRect.move(m_subpixelOffsetFromRenderer);
2557     IntRect dirtyRect = enclosingIntRect(adjustedClipRect);
2558
2559     if (graphicsLayer == m_graphicsLayer.get()
2560         || graphicsLayer == m_foregroundLayer.get()
2561         || graphicsLayer == m_backgroundLayer.get()
2562         || graphicsLayer == m_maskLayer.get()
2563         || graphicsLayer == m_childClippingMaskLayer.get()
2564         || graphicsLayer == m_scrollingContentsLayer.get()) {
2565         InspectorInstrumentation::willPaint(renderer());
2566
2567         if (!(paintingPhase & GraphicsLayerPaintOverflowContents))
2568             dirtyRect.intersect(enclosingIntRect(compositedBoundsIncludingMargin()));
2569
2570         // We have to use the same root as for hit testing, because both methods can compute and cache clipRects.
2571         paintIntoLayer(graphicsLayer, context, dirtyRect, PaintBehaviorNormal, paintingPhase);
2572
2573         InspectorInstrumentation::didPaint(renderer(), dirtyRect);
2574     } else if (graphicsLayer == layerForHorizontalScrollbar()) {
2575         paintScrollbar(m_owningLayer.horizontalScrollbar(), context, dirtyRect);
2576     } else if (graphicsLayer == layerForVerticalScrollbar()) {
2577         paintScrollbar(m_owningLayer.verticalScrollbar(), context, dirtyRect);
2578     } else if (graphicsLayer == layerForScrollCorner()) {
2579         const LayoutRect& scrollCornerAndResizer = m_owningLayer.scrollCornerAndResizerRect();
2580         context.save();
2581         context.translate(-scrollCornerAndResizer.x(), -scrollCornerAndResizer.y());
2582         LayoutRect transformedClip = LayoutRect(clip);
2583         transformedClip.moveBy(scrollCornerAndResizer.location());
2584         m_owningLayer.paintScrollCorner(context, IntPoint(), snappedIntRect(transformedClip));
2585         m_owningLayer.paintResizer(context, IntPoint(), transformedClip);
2586         context.restore();
2587     }
2588 #ifndef NDEBUG
2589     renderer().page().setIsPainting(false);
2590 #endif
2591 }
2592
2593 float RenderLayerBacking::pageScaleFactor() const
2594 {
2595     return compositor().pageScaleFactor();
2596 }
2597
2598 float RenderLayerBacking::zoomedOutPageScaleFactor() const
2599 {
2600     return compositor().zoomedOutPageScaleFactor();
2601 }
2602
2603 float RenderLayerBacking::deviceScaleFactor() const
2604 {
2605     return compositor().deviceScaleFactor();
2606 }
2607
2608 float RenderLayerBacking::contentsScaleMultiplierForNewTiles(const GraphicsLayer* layer) const
2609 {
2610     return compositor().contentsScaleMultiplierForNewTiles(layer);
2611 }
2612
2613 bool RenderLayerBacking::paintsOpaquelyAtNonIntegralScales(const GraphicsLayer*) const
2614 {
2615     return m_isMainFrameRenderViewLayer;
2616 }
2617
2618 void RenderLayerBacking::didCommitChangesForLayer(const GraphicsLayer* layer) const
2619 {
2620     compositor().didFlushChangesForLayer(m_owningLayer, layer);
2621 }
2622
2623 bool RenderLayerBacking::getCurrentTransform(const GraphicsLayer* graphicsLayer, TransformationMatrix& transform) const
2624 {
2625     GraphicsLayer* transformedLayer = m_contentsContainmentLayer.get() ? m_contentsContainmentLayer.get() : m_graphicsLayer.get();
2626     if (graphicsLayer != transformedLayer)
2627         return false;
2628
2629     if (m_owningLayer.hasTransform()) {
2630         transform = m_owningLayer.currentTransform(RenderStyle::ExcludeTransformOrigin);
2631         return true;
2632     }
2633     return false;
2634 }
2635
2636 bool RenderLayerBacking::isTrackingRepaints() const
2637 {
2638     return static_cast<GraphicsLayerClient&>(compositor()).isTrackingRepaints();
2639 }
2640
2641 bool RenderLayerBacking::shouldSkipLayerInDump(const GraphicsLayer* layer, LayerTreeAsTextBehavior behavior) const
2642 {
2643     if (behavior & LayerTreeAsTextDebug)
2644         return false;
2645
2646     // Skip the root tile cache's flattening layer.
2647     return m_isMainFrameRenderViewLayer && layer && layer == m_childContainmentLayer.get();
2648 }
2649
2650 bool RenderLayerBacking::shouldDumpPropertyForLayer(const GraphicsLayer* layer, const char* propertyName) const
2651 {
2652     // For backwards compatibility with WebKit1 and other platforms,
2653     // skip some properties on the root tile cache.
2654     if (m_isMainFrameRenderViewLayer && layer == m_graphicsLayer.get()) {
2655         if (!strcmp(propertyName, "drawsContent"))
2656             return false;
2657
2658         // Background color could be of interest to tests or other dumpers if it's non-white.
2659         if (!strcmp(propertyName, "backgroundColor") && layer->backgroundColor() == Color::white)
2660             return false;
2661
2662         // The root tile cache's repaints will show up at the top with FrameView's,
2663         // so don't dump them twice.
2664         if (!strcmp(propertyName, "repaintRects"))
2665             return false;
2666     }
2667
2668     return true;
2669 }
2670
2671 bool RenderLayerBacking::shouldAggressivelyRetainTiles(const GraphicsLayer*) const
2672 {
2673     // Only the main frame TileController has enough information about in-window state to
2674     // correctly implement aggressive tile retention.
2675     if (!m_isMainFrameRenderViewLayer)
2676         return false;
2677
2678     return renderer().settings().aggressiveTileRetentionEnabled();
2679 }
2680
2681 bool RenderLayerBacking::shouldTemporarilyRetainTileCohorts(const GraphicsLayer*) const
2682 {
2683     return renderer().settings().temporaryTileCohortRetentionEnabled();
2684 }
2685
2686 bool RenderLayerBacking::useGiantTiles() const
2687 {
2688     return renderer().settings().useGiantTiles();
2689 }
2690
2691 #ifndef NDEBUG
2692 void RenderLayerBacking::verifyNotPainting()
2693 {
2694     ASSERT(!renderer().page().isPainting());
2695 }
2696 #endif
2697
2698 bool RenderLayerBacking::startAnimation(double timeOffset, const Animation* anim, const KeyframeList& keyframes)
2699 {
2700     bool hasOpacity = keyframes.containsProperty(CSSPropertyOpacity);
2701     bool hasTransform = renderer().isBox() && keyframes.containsProperty(CSSPropertyTransform);
2702     bool hasFilter = keyframes.containsProperty(CSSPropertyFilter);
2703
2704     bool hasBackdropFilter = false;
2705 #if ENABLE(FILTERS_LEVEL_2)
2706     hasBackdropFilter = keyframes.containsProperty(CSSPropertyWebkitBackdropFilter);
2707 #endif
2708
2709     if (!hasOpacity && !hasTransform && !hasFilter && !hasBackdropFilter)
2710         return false;
2711
2712     KeyframeValueList transformVector(AnimatedPropertyTransform);
2713     KeyframeValueList opacityVector(AnimatedPropertyOpacity);
2714     KeyframeValueList filterVector(AnimatedPropertyFilter);
2715 #if ENABLE(FILTERS_LEVEL_2)
2716     KeyframeValueList backdropFilterVector(AnimatedPropertyWebkitBackdropFilter);
2717 #endif
2718
2719     size_t numKeyframes = keyframes.size();
2720     for (size_t i = 0; i < numKeyframes; ++i) {
2721         const KeyframeValue& currentKeyframe = keyframes[i];
2722         const RenderStyle* keyframeStyle = currentKeyframe.style();
2723         double key = currentKeyframe.key();
2724
2725         if (!keyframeStyle)
2726             continue;
2727             
2728         TimingFunction* tf = currentKeyframe.timingFunction(keyframes.animationName());
2729         
2730         bool isFirstOrLastKeyframe = key == 0 || key == 1;
2731         if ((hasTransform && isFirstOrLastKeyframe) || currentKeyframe.containsProperty(CSSPropertyTransform))
2732             transformVector.insert(std::make_unique<TransformAnimationValue>(key, keyframeStyle->transform(), tf));
2733
2734         if ((hasOpacity && isFirstOrLastKeyframe) || currentKeyframe.containsProperty(CSSPropertyOpacity))
2735             opacityVector.insert(std::make_unique<FloatAnimationValue>(key, keyframeStyle->opacity(), tf));
2736
2737         if ((hasFilter && isFirstOrLastKeyframe) || currentKeyframe.containsProperty(CSSPropertyFilter))
2738             filterVector.insert(std::make_unique<FilterAnimationValue>(key, keyframeStyle->filter(), tf));
2739
2740 #if ENABLE(FILTERS_LEVEL_2)
2741         if ((hasBackdropFilter && isFirstOrLastKeyframe) || currentKeyframe.containsProperty(CSSPropertyWebkitBackdropFilter))
2742             backdropFilterVector.insert(std::make_unique<FilterAnimationValue>(key, keyframeStyle->backdropFilter(), tf));
2743 #endif
2744     }
2745
2746     if (!renderer().settings().acceleratedCompositedAnimationsEnabled())
2747         return false;
2748
2749     bool didAnimate = false;
2750
2751     if (hasTransform && m_graphicsLayer->addAnimation(transformVector, snappedIntRect(renderBox()->borderBoxRect()).size(), anim, keyframes.animationName(), timeOffset))
2752         didAnimate = true;
2753
2754     if (hasOpacity && m_graphicsLayer->addAnimation(opacityVector, IntSize(), anim, keyframes.animationName(), timeOffset))
2755         didAnimate = true;
2756
2757     if (hasFilter && m_graphicsLayer->addAnimation(filterVector, IntSize(), anim, keyframes.animationName(), timeOffset))
2758         didAnimate = true;
2759
2760 #if ENABLE(FILTERS_LEVEL_2)
2761     if (hasBackdropFilter && m_graphicsLayer->addAnimation(backdropFilterVector, IntSize(), anim, keyframes.animationName(), timeOffset))
2762         didAnimate = true;
2763 #endif
2764
2765     return didAnimate;
2766 }
2767
2768 void RenderLayerBacking::animationPaused(double timeOffset, const String& animationName)
2769 {
2770     m_graphicsLayer->pauseAnimation(animationName, timeOffset);
2771 }
2772
2773 void RenderLayerBacking::animationFinished(const String& animationName)
2774 {
2775     m_graphicsLayer->removeAnimation(animationName);
2776 }
2777
2778 bool RenderLayerBacking::startTransition(double timeOffset, CSSPropertyID property, const RenderStyle* fromStyle, const RenderStyle* toStyle)
2779 {
2780     bool didAnimate = false;
2781
2782     ASSERT(property != CSSPropertyInvalid);
2783
2784     if (property == CSSPropertyOpacity) {
2785         const Animation* opacityAnim = toStyle->transitionForProperty(CSSPropertyOpacity);
2786         if (opacityAnim && !opacityAnim->isEmptyOrZeroDuration()) {
2787             KeyframeValueList opacityVector(AnimatedPropertyOpacity);
2788             opacityVector.insert(std::make_unique<FloatAnimationValue>(0, compositingOpacity(fromStyle->opacity())));
2789             opacityVector.insert(std::make_unique<FloatAnimationValue>(1, compositingOpacity(toStyle->opacity())));
2790             // The boxSize param is only used for transform animations (which can only run on RenderBoxes), so we pass an empty size here.
2791             if (m_graphicsLayer->addAnimation(opacityVector, FloatSize(), opacityAnim, GraphicsLayer::animationNameForTransition(AnimatedPropertyOpacity), timeOffset)) {
2792                 // To ensure that the correct opacity is visible when the animation ends, also set the final opacity.
2793                 updateOpacity(*toStyle);
2794                 didAnimate = true;
2795             }
2796         }
2797     }
2798
2799     if (property == CSSPropertyTransform && m_owningLayer.hasTransform()) {
2800         const Animation* transformAnim = toStyle->transitionForProperty(CSSPropertyTransform);
2801         if (transformAnim && !transformAnim->isEmptyOrZeroDuration()) {
2802             KeyframeValueList transformVector(AnimatedPropertyTransform);
2803             transformVector.insert(std::make_unique<TransformAnimationValue>(0, fromStyle->transform()));
2804             transformVector.insert(std::make_unique<TransformAnimationValue>(1, toStyle->transform()));
2805             if (m_graphicsLayer->addAnimation(transformVector, snappedIntRect(renderBox()->borderBoxRect()).size(), transformAnim, GraphicsLayer::animationNameForTransition(AnimatedPropertyTransform), timeOffset)) {
2806                 // To ensure that the correct transform is visible when the animation ends, also set the final transform.
2807                 updateTransform(*toStyle);
2808                 didAnimate = true;
2809             }
2810         }
2811     }
2812
2813     if (property == CSSPropertyFilter && m_owningLayer.hasFilter()) {
2814         const Animation* filterAnim = toStyle->transitionForProperty(CSSPropertyFilter);
2815         if (filterAnim && !filterAnim->isEmptyOrZeroDuration()) {
2816             KeyframeValueList filterVector(AnimatedPropertyFilter);
2817             filterVector.insert(std::make_unique<FilterAnimationValue>(0, fromStyle->filter()));
2818             filterVector.insert(std::make_unique<FilterAnimationValue>(1, toStyle->filter()));
2819             if (m_graphicsLayer->addAnimation(filterVector, FloatSize(), filterAnim, GraphicsLayer::animationNameForTransition(AnimatedPropertyFilter), timeOffset)) {
2820                 // To ensure that the correct filter is visible when the animation ends, also set the final filter.
2821                 updateFilters(*toStyle);
2822                 didAnimate = true;
2823             }
2824         }
2825     }
2826
2827 #if ENABLE(FILTERS_LEVEL_2)
2828     if (property == CSSPropertyWebkitBackdropFilter && m_owningLayer.hasBackdropFilter()) {
2829         const Animation* backdropFilterAnim = toStyle->transitionForProperty(CSSPropertyWebkitBackdropFilter);
2830         if (backdropFilterAnim && !backdropFilterAnim->isEmptyOrZeroDuration()) {
2831             KeyframeValueList backdropFilterVector(AnimatedPropertyWebkitBackdropFilter);
2832             backdropFilterVector.insert(std::make_unique<FilterAnimationValue>(0, fromStyle->backdropFilter()));
2833             backdropFilterVector.insert(std::make_unique<FilterAnimationValue>(1, toStyle->backdropFilter()));
2834             if (m_graphicsLayer->addAnimation(backdropFilterVector, FloatSize(), backdropFilterAnim, GraphicsLayer::animationNameForTransition(AnimatedPropertyWebkitBackdropFilter), timeOffset)) {
2835                 // To ensure that the correct backdrop filter is visible when the animation ends, also set the final backdrop filter.
2836                 updateBackdropFilters(*toStyle);
2837                 didAnimate = true;
2838             }
2839         }
2840     }
2841 #endif
2842
2843     return didAnimate;
2844 }
2845
2846 void RenderLayerBacking::transitionPaused(double timeOffset, CSSPropertyID property)
2847 {
2848     AnimatedPropertyID animatedProperty = cssToGraphicsLayerProperty(property);
2849     if (animatedProperty != AnimatedPropertyInvalid)
2850         m_graphicsLayer->pauseAnimation(GraphicsLayer::animationNameForTransition(animatedProperty), timeOffset);
2851 }
2852
2853 void RenderLayerBacking::transitionFinished(CSSPropertyID property)
2854 {
2855     AnimatedPropertyID animatedProperty = cssToGraphicsLayerProperty(property);
2856     if (animatedProperty != AnimatedPropertyInvalid)
2857         m_graphicsLayer->removeAnimation(GraphicsLayer::animationNameForTransition(animatedProperty));
2858 }
2859
2860 void RenderLayerBacking::notifyAnimationStarted(const GraphicsLayer*, const String&, double time)
2861 {
2862     renderer().animation().notifyAnimationStarted(renderer(), time);
2863 }
2864
2865 void RenderLayerBacking::notifyFlushRequired(const GraphicsLayer* layer)
2866 {
2867     if (renderer().renderTreeBeingDestroyed())
2868         return;
2869     compositor().scheduleLayerFlush(layer->canThrottleLayerFlush());
2870 }
2871
2872 void RenderLayerBacking::notifyFlushBeforeDisplayRefresh(const GraphicsLayer* layer)
2873 {
2874     compositor().notifyFlushBeforeDisplayRefresh(layer);
2875 }
2876
2877 // This is used for the 'freeze' API, for testing only.
2878 void RenderLayerBacking::suspendAnimations(double time)
2879 {
2880     m_graphicsLayer->suspendAnimations(time);
2881 }
2882
2883 void RenderLayerBacking::resumeAnimations()
2884 {
2885     m_graphicsLayer->resumeAnimations();
2886 }
2887
2888 LayoutRect RenderLayerBacking::compositedBounds() const
2889 {
2890     return m_compositedBounds;
2891 }
2892
2893 void RenderLayerBacking::setCompositedBounds(const LayoutRect& bounds)
2894 {
2895     m_compositedBounds = bounds;
2896 }
2897
2898 LayoutRect RenderLayerBacking::compositedBoundsIncludingMargin() const
2899 {
2900     TiledBacking* tiledBacking = this->tiledBacking();
2901     if (!tiledBacking || !tiledBacking->hasMargins())
2902         return compositedBounds();
2903
2904     LayoutRect boundsIncludingMargin = compositedBounds();
2905     LayoutUnit leftMarginWidth = tiledBacking->leftMarginWidth();
2906     LayoutUnit topMarginHeight = tiledBacking->topMarginHeight();
2907
2908     boundsIncludingMargin.moveBy(LayoutPoint(-leftMarginWidth, -topMarginHeight));
2909     boundsIncludingMargin.expand(leftMarginWidth + tiledBacking->rightMarginWidth(), topMarginHeight + tiledBacking->bottomMarginHeight());
2910
2911     return boundsIncludingMargin;
2912 }
2913
2914 CSSPropertyID RenderLayerBacking::graphicsLayerToCSSProperty(AnimatedPropertyID property)
2915 {
2916     CSSPropertyID cssProperty = CSSPropertyInvalid;
2917     switch (property) {
2918     case AnimatedPropertyTransform:
2919         cssProperty = CSSPropertyTransform;
2920         break;
2921     case AnimatedPropertyOpacity:
2922         cssProperty = CSSPropertyOpacity;
2923         break;
2924     case AnimatedPropertyBackgroundColor:
2925         cssProperty = CSSPropertyBackgroundColor;
2926         break;
2927     case AnimatedPropertyFilter:
2928         cssProperty = CSSPropertyFilter;
2929         break;
2930 #if ENABLE(FILTERS_LEVEL_2)
2931     case AnimatedPropertyWebkitBackdropFilter:
2932         cssProperty = CSSPropertyWebkitBackdropFilter;
2933         break;
2934 #endif
2935     case AnimatedPropertyInvalid:
2936         ASSERT_NOT_REACHED();
2937     }
2938     return cssProperty;
2939 }
2940
2941 AnimatedPropertyID RenderLayerBacking::cssToGraphicsLayerProperty(CSSPropertyID cssProperty)
2942 {
2943     switch (cssProperty) {
2944     case CSSPropertyTransform:
2945         return AnimatedPropertyTransform;
2946     case CSSPropertyOpacity:
2947         return AnimatedPropertyOpacity;
2948     case CSSPropertyBackgroundColor:
2949         return AnimatedPropertyBackgroundColor;
2950     case CSSPropertyFilter:
2951         return AnimatedPropertyFilter;
2952 #if ENABLE(FILTERS_LEVEL_2)
2953     case CSSPropertyWebkitBackdropFilter:
2954         return AnimatedPropertyWebkitBackdropFilter;
2955 #endif
2956     default:
2957         // It's fine if we see other css properties here; they are just not accelerated.
2958         break;
2959     }
2960     return AnimatedPropertyInvalid;
2961 }
2962
2963 CompositingLayerType RenderLayerBacking::compositingLayerType() const
2964 {
2965     if (m_graphicsLayer->usesContentsLayer())
2966         return MediaCompositingLayer;
2967
2968     if (m_graphicsLayer->drawsContent())
2969         return m_graphicsLayer->tiledBacking() ? TiledCompositingLayer : NormalCompositingLayer;
2970     
2971     return ContainerCompositingLayer;
2972 }
2973
2974 double RenderLayerBacking::backingStoreMemoryEstimate() const
2975 {
2976     double backingMemory;
2977     
2978     // m_ancestorClippingLayer, m_contentsContainmentLayer and m_childContainmentLayer are just used for masking or containment, so have no backing.
2979     backingMemory = m_graphicsLayer->backingStoreMemoryEstimate();
2980     if (m_foregroundLayer)
2981         backingMemory += m_foregroundLayer->backingStoreMemoryEstimate();
2982     if (m_backgroundLayer)
2983         backingMemory += m_backgroundLayer->backingStoreMemoryEstimate();
2984     if (m_maskLayer)
2985         backingMemory += m_maskLayer->backingStoreMemoryEstimate();
2986     if (m_childClippingMaskLayer)
2987         backingMemory += m_childClippingMaskLayer->backingStoreMemoryEstimate();
2988
2989     if (m_scrollingContentsLayer)
2990         backingMemory += m_scrollingContentsLayer->backingStoreMemoryEstimate();
2991
2992     if (m_layerForHorizontalScrollbar)
2993         backingMemory += m_layerForHorizontalScrollbar->backingStoreMemoryEstimate();
2994
2995     if (m_layerForVerticalScrollbar)
2996         backingMemory += m_layerForVerticalScrollbar->backingStoreMemoryEstimate();
2997
2998     if (m_layerForScrollCorner)
2999         backingMemory += m_layerForScrollCorner->backingStoreMemoryEstimate();
3000     
3001     return backingMemory;
3002 }
3003
3004 } // namespace WebCore