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