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