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