Upstream version 10.38.220.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / compositing / RenderLayerCompositor.cpp
1 /*
2  * Copyright (C) 2009, 2010 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 COMPUTER, 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 "core/rendering/compositing/RenderLayerCompositor.h"
29
30 #include "core/animation/DocumentAnimations.h"
31 #include "core/dom/FullscreenElementStack.h"
32 #include "core/frame/FrameView.h"
33 #include "core/frame/LocalFrame.h"
34 #include "core/frame/Settings.h"
35 #include "core/html/HTMLIFrameElement.h"
36 #include "core/inspector/InspectorInstrumentation.h"
37 #include "core/inspector/InspectorNodeIds.h"
38 #include "core/page/Chrome.h"
39 #include "core/page/ChromeClient.h"
40 #include "core/page/Page.h"
41 #include "core/page/scrolling/ScrollingCoordinator.h"
42 #include "core/rendering/RenderEmbeddedObject.h"
43 #include "core/rendering/RenderLayerStackingNode.h"
44 #include "core/rendering/RenderLayerStackingNodeIterator.h"
45 #include "core/rendering/RenderPart.h"
46 #include "core/rendering/RenderVideo.h"
47 #include "core/rendering/RenderView.h"
48 #include "core/rendering/compositing/CompositedLayerMapping.h"
49 #include "core/rendering/compositing/CompositingInputsUpdater.h"
50 #include "core/rendering/compositing/CompositingLayerAssigner.h"
51 #include "core/rendering/compositing/CompositingRequirementsUpdater.h"
52 #include "core/rendering/compositing/GraphicsLayerTreeBuilder.h"
53 #include "core/rendering/compositing/GraphicsLayerUpdater.h"
54 #include "platform/OverscrollTheme.h"
55 #include "platform/RuntimeEnabledFeatures.h"
56 #include "platform/ScriptForbiddenScope.h"
57 #include "platform/TraceEvent.h"
58 #include "platform/graphics/GraphicsLayer.h"
59 #include "public/platform/Platform.h"
60
61 namespace blink {
62
63 RenderLayerCompositor::RenderLayerCompositor(RenderView& renderView)
64     : m_renderView(renderView)
65     , m_compositingReasonFinder(renderView)
66     , m_pendingUpdateType(CompositingUpdateNone)
67     , m_hasAcceleratedCompositing(true)
68     , m_compositing(false)
69     , m_rootShouldAlwaysCompositeDirty(true)
70     , m_needsUpdateFixedBackground(false)
71     , m_isTrackingPaintInvalidations(false)
72     , m_rootLayerAttachment(RootLayerUnattached)
73     , m_inOverlayFullscreenVideo(false)
74 {
75     updateAcceleratedCompositingSettings();
76 }
77
78 RenderLayerCompositor::~RenderLayerCompositor()
79 {
80     ASSERT(m_rootLayerAttachment == RootLayerUnattached);
81 }
82
83 bool RenderLayerCompositor::inCompositingMode() const
84 {
85     // FIXME: This should assert that lificycle is >= CompositingClean since
86     // the last step of updateIfNeeded can set this bit to false.
87     ASSERT(!m_rootShouldAlwaysCompositeDirty);
88     return m_compositing;
89 }
90
91 bool RenderLayerCompositor::staleInCompositingMode() const
92 {
93     return m_compositing;
94 }
95
96 void RenderLayerCompositor::setCompositingModeEnabled(bool enable)
97 {
98     if (enable == m_compositing)
99         return;
100
101     m_compositing = enable;
102
103     // RenderPart::requiresAcceleratedCompositing is used to determine self-paintingness
104     // and bases it's return value for frames on the m_compositing bit here.
105     if (HTMLFrameOwnerElement* ownerElement = m_renderView.document().ownerElement()) {
106         if (RenderPart* renderer = ownerElement->renderPart())
107             renderer->layer()->updateSelfPaintingLayer();
108     }
109
110     if (m_compositing)
111         ensureRootLayer();
112     else
113         destroyRootLayer();
114
115     // Compositing also affects the answer to RenderIFrame::requiresAcceleratedCompositing(), so
116     // we need to schedule a style recalc in our parent document.
117     if (HTMLFrameOwnerElement* ownerElement = m_renderView.document().ownerElement())
118         ownerElement->setNeedsCompositingUpdate();
119 }
120
121 void RenderLayerCompositor::enableCompositingModeIfNeeded()
122 {
123     if (!m_rootShouldAlwaysCompositeDirty)
124         return;
125
126     m_rootShouldAlwaysCompositeDirty = false;
127     if (m_compositing)
128         return;
129
130     if (rootShouldAlwaysComposite()) {
131         // FIXME: Is this needed? It was added in https://bugs.webkit.org/show_bug.cgi?id=26651.
132         // No tests fail if it's deleted.
133         setNeedsCompositingUpdate(CompositingUpdateRebuildTree);
134         setCompositingModeEnabled(true);
135     }
136 }
137
138 bool RenderLayerCompositor::rootShouldAlwaysComposite() const
139 {
140     if (!m_hasAcceleratedCompositing)
141         return false;
142     return m_renderView.frame()->isLocalRoot() || m_compositingReasonFinder.requiresCompositingForScrollableFrame();
143 }
144
145 void RenderLayerCompositor::updateAcceleratedCompositingSettings()
146 {
147     m_compositingReasonFinder.updateTriggers();
148     m_hasAcceleratedCompositing = m_renderView.document().settings()->acceleratedCompositingEnabled();
149     m_rootShouldAlwaysCompositeDirty = true;
150 }
151
152 bool RenderLayerCompositor::layerSquashingEnabled() const
153 {
154     if (!RuntimeEnabledFeatures::layerSquashingEnabled())
155         return false;
156     if (Settings* settings = m_renderView.document().settings())
157         return settings->layerSquashingEnabled();
158     return true;
159 }
160
161 bool RenderLayerCompositor::acceleratedCompositingForOverflowScrollEnabled() const
162 {
163     return m_compositingReasonFinder.hasOverflowScrollTrigger();
164 }
165
166 static RenderVideo* findFullscreenVideoRenderer(Document& document)
167 {
168     // Recursively find the document that is in fullscreen.
169     Element* fullscreenElement = FullscreenElementStack::fullscreenElementFrom(document);
170     Document* contentDocument = &document;
171     while (fullscreenElement && fullscreenElement->isFrameOwnerElement()) {
172         contentDocument = toHTMLFrameOwnerElement(fullscreenElement)->contentDocument();
173         if (!contentDocument)
174             return 0;
175         fullscreenElement = FullscreenElementStack::fullscreenElementFrom(*contentDocument);
176     }
177     // Get the current fullscreen element from the document.
178     fullscreenElement = FullscreenElementStack::currentFullScreenElementFrom(*contentDocument);
179     if (!isHTMLVideoElement(fullscreenElement))
180         return 0;
181     RenderObject* renderer = fullscreenElement->renderer();
182     if (!renderer)
183         return 0;
184     return toRenderVideo(renderer);
185 }
186
187 void RenderLayerCompositor::updateIfNeededRecursive()
188 {
189     for (Frame* child = m_renderView.frameView()->frame().tree().firstChild(); child; child = child->tree().nextSibling()) {
190         if (child->isLocalFrame())
191             toLocalFrame(child)->contentRenderer()->compositor()->updateIfNeededRecursive();
192     }
193
194     TRACE_EVENT0("blink", "RenderLayerCompositor::updateIfNeededRecursive");
195
196     ASSERT(!m_renderView.needsLayout());
197
198     ScriptForbiddenScope forbidScript;
199
200     // FIXME: enableCompositingModeIfNeeded can trigger a CompositingUpdateRebuildTree,
201     // which asserts that it's not InCompositingUpdate.
202     enableCompositingModeIfNeeded();
203
204     rootRenderLayer()->updateDescendantDependentFlagsForEntireSubtree();
205
206     lifecycle().advanceTo(DocumentLifecycle::InCompositingUpdate);
207     updateIfNeeded();
208     lifecycle().advanceTo(DocumentLifecycle::CompositingClean);
209
210     DocumentAnimations::startPendingAnimations(m_renderView.document());
211
212 #if ENABLE(ASSERT)
213     ASSERT(lifecycle().state() == DocumentLifecycle::CompositingClean);
214     assertNoUnresolvedDirtyBits();
215     for (Frame* child = m_renderView.frameView()->frame().tree().firstChild(); child; child = child->tree().nextSibling()) {
216         if (child->isLocalFrame())
217             toLocalFrame(child)->contentRenderer()->compositor()->assertNoUnresolvedDirtyBits();
218     }
219 #endif
220 }
221
222 void RenderLayerCompositor::setNeedsCompositingUpdate(CompositingUpdateType updateType)
223 {
224     ASSERT(updateType != CompositingUpdateNone);
225     m_pendingUpdateType = std::max(m_pendingUpdateType, updateType);
226     page()->animator().scheduleVisualUpdate();
227     lifecycle().ensureStateAtMost(DocumentLifecycle::LayoutClean);
228 }
229
230 void RenderLayerCompositor::didLayout()
231 {
232     // FIXME: Technically we only need to do this when the FrameView's
233     // isScrollable method would return a different value.
234     m_rootShouldAlwaysCompositeDirty = true;
235     enableCompositingModeIfNeeded();
236
237     // FIXME: Rather than marking the entire RenderView as dirty, we should
238     // track which RenderLayers moved during layout and only dirty those
239     // specific RenderLayers.
240     rootRenderLayer()->setNeedsCompositingInputsUpdate();
241 }
242
243 #if ENABLE(ASSERT)
244
245 void RenderLayerCompositor::assertNoUnresolvedDirtyBits()
246 {
247     ASSERT(m_pendingUpdateType == CompositingUpdateNone);
248     ASSERT(!m_rootShouldAlwaysCompositeDirty);
249 }
250
251 #endif
252
253 void RenderLayerCompositor::applyOverlayFullscreenVideoAdjustment()
254 {
255     m_inOverlayFullscreenVideo = false;
256     if (!m_rootContentLayer)
257         return;
258
259     bool isLocalRoot = m_renderView.frame()->isLocalRoot();
260     RenderVideo* video = findFullscreenVideoRenderer(m_renderView.document());
261     if (!video || !video->layer()->hasCompositedLayerMapping()) {
262         if (isLocalRoot) {
263             GraphicsLayer* backgroundLayer = fixedRootBackgroundLayer();
264             if (backgroundLayer && !backgroundLayer->parent())
265                 rootFixedBackgroundsChanged();
266         }
267         return;
268     }
269
270     GraphicsLayer* videoLayer = video->layer()->compositedLayerMapping()->mainGraphicsLayer();
271
272     // The fullscreen video has layer position equal to its enclosing frame's scroll position because fullscreen container is fixed-positioned.
273     // We should reset layer position here since we are going to reattach the layer at the very top level.
274     videoLayer->setPosition(IntPoint());
275
276     // Only steal fullscreen video layer and clear all other layers if we are the main frame.
277     if (!isLocalRoot)
278         return;
279
280     m_rootContentLayer->removeAllChildren();
281     m_overflowControlsHostLayer->addChild(videoLayer);
282     if (GraphicsLayer* backgroundLayer = fixedRootBackgroundLayer())
283         backgroundLayer->removeFromParent();
284     m_inOverlayFullscreenVideo = true;
285 }
286
287 void RenderLayerCompositor::updateWithoutAcceleratedCompositing(CompositingUpdateType updateType)
288 {
289     ASSERT(!hasAcceleratedCompositing());
290
291     if (updateType >= CompositingUpdateAfterCompositingInputChange)
292         CompositingInputsUpdater(rootRenderLayer()).update();
293
294 #if ENABLE(ASSERT)
295     CompositingInputsUpdater::assertNeedsCompositingInputsUpdateBitsCleared(rootRenderLayer());
296 #endif
297 }
298
299 void RenderLayerCompositor::updateIfNeeded()
300 {
301     CompositingUpdateType updateType = m_pendingUpdateType;
302     m_pendingUpdateType = CompositingUpdateNone;
303
304     if (!hasAcceleratedCompositing()) {
305         updateWithoutAcceleratedCompositing(updateType);
306         return;
307     }
308
309     if (updateType == CompositingUpdateNone)
310         return;
311
312     RenderLayer* updateRoot = rootRenderLayer();
313
314     Vector<RenderLayer*> layersNeedingPaintInvalidation;
315
316     if (updateType >= CompositingUpdateAfterCompositingInputChange) {
317         CompositingInputsUpdater(updateRoot).update();
318
319 #if ENABLE(ASSERT)
320         // FIXME: Move this check to the end of the compositing update.
321         CompositingInputsUpdater::assertNeedsCompositingInputsUpdateBitsCleared(updateRoot);
322 #endif
323
324         CompositingRequirementsUpdater(m_renderView, m_compositingReasonFinder).update(updateRoot);
325
326         CompositingLayerAssigner layerAssigner(this);
327         layerAssigner.assign(updateRoot, layersNeedingPaintInvalidation);
328
329         bool layersChanged = layerAssigner.layersChanged();
330
331         {
332             TRACE_EVENT0("blink", "RenderLayerCompositor::updateAfterCompositingChange");
333             if (const FrameView::ScrollableAreaSet* scrollableAreas = m_renderView.frameView()->scrollableAreas()) {
334                 for (FrameView::ScrollableAreaSet::iterator it = scrollableAreas->begin(); it != scrollableAreas->end(); ++it)
335                     layersChanged |= (*it)->updateAfterCompositingChange();
336             }
337         }
338
339         if (layersChanged)
340             updateType = std::max(updateType, CompositingUpdateRebuildTree);
341     }
342
343     if (updateType != CompositingUpdateNone) {
344         GraphicsLayerUpdater updater;
345         updater.update(*updateRoot, layersNeedingPaintInvalidation);
346
347         if (updater.needsRebuildTree())
348             updateType = std::max(updateType, CompositingUpdateRebuildTree);
349
350 #if ENABLE(ASSERT)
351         // FIXME: Move this check to the end of the compositing update.
352         GraphicsLayerUpdater::assertNeedsToUpdateGraphicsLayerBitsCleared(*updateRoot);
353 #endif
354     }
355
356     if (updateType >= CompositingUpdateRebuildTree) {
357         GraphicsLayerTreeBuilder::AncestorInfo ancestorInfo;
358         GraphicsLayerVector childList;
359         ancestorInfo.childLayersOfEnclosingCompositedLayer = &childList;
360         {
361             TRACE_EVENT0("blink", "GraphicsLayerTreeBuilder::rebuild");
362             GraphicsLayerTreeBuilder().rebuild(*updateRoot, ancestorInfo);
363         }
364
365         if (childList.isEmpty())
366             destroyRootLayer();
367         else
368             m_rootContentLayer->setChildren(childList);
369
370         if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled())
371             applyOverlayFullscreenVideoAdjustment();
372     }
373
374     if (m_needsUpdateFixedBackground) {
375         rootFixedBackgroundsChanged();
376         m_needsUpdateFixedBackground = false;
377     }
378
379     for (unsigned i = 0; i < layersNeedingPaintInvalidation.size(); i++) {
380         RenderLayer* layer = layersNeedingPaintInvalidation[i];
381         layer->paintInvalidator().computePaintInvalidationRectsIncludingNonCompositingDescendants();
382
383         paintInvalidationOnCompositingChange(layer);
384     }
385
386     // Inform the inspector that the layer tree has changed.
387     if (m_renderView.frame()->isMainFrame())
388         InspectorInstrumentation::layerTreeDidChange(m_renderView.frame());
389 }
390
391 bool RenderLayerCompositor::allocateOrClearCompositedLayerMapping(RenderLayer* layer, const CompositingStateTransitionType compositedLayerUpdate)
392 {
393     bool compositedLayerMappingChanged = false;
394
395     // FIXME: It would be nice to directly use the layer's compositing reason,
396     // but allocateOrClearCompositedLayerMapping also gets called without having updated compositing
397     // requirements fully.
398     switch (compositedLayerUpdate) {
399     case AllocateOwnCompositedLayerMapping:
400         ASSERT(!layer->hasCompositedLayerMapping());
401         setCompositingModeEnabled(true);
402
403         // If we need to issue paint invalidations, do so before allocating the compositedLayerMapping and clearing out the groupedMapping.
404         paintInvalidationOnCompositingChange(layer);
405
406         // If this layer was previously squashed, we need to remove its reference to a groupedMapping right away, so
407         // that computing paint invalidation rects will know the layer's correct compositingState.
408         // FIXME: do we need to also remove the layer from it's location in the squashing list of its groupedMapping?
409         // Need to create a test where a squashed layer pops into compositing. And also to cover all other
410         // sorts of compositingState transitions.
411         layer->setLostGroupedMapping(false);
412         layer->setGroupedMapping(0);
413
414         layer->ensureCompositedLayerMapping();
415         compositedLayerMappingChanged = true;
416
417         // At this time, the ScrollingCooridnator only supports the top-level frame.
418         if (layer->isRootLayer() && m_renderView.frame()->isLocalRoot()) {
419             if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator())
420                 scrollingCoordinator->frameViewRootLayerDidChange(m_renderView.frameView());
421         }
422         break;
423     case RemoveOwnCompositedLayerMapping:
424     // PutInSquashingLayer means you might have to remove the composited layer mapping first.
425     case PutInSquashingLayer:
426         if (layer->hasCompositedLayerMapping()) {
427             // If we're removing the compositedLayerMapping from a reflection, clear the source GraphicsLayer's pointer to
428             // its replica GraphicsLayer. In practice this should never happen because reflectee and reflection
429             // are both either composited, or not composited.
430             if (layer->isReflection()) {
431                 RenderLayer* sourceLayer = toRenderLayerModelObject(layer->renderer()->parent())->layer();
432                 if (sourceLayer->hasCompositedLayerMapping()) {
433                     ASSERT(sourceLayer->compositedLayerMapping()->mainGraphicsLayer()->replicaLayer() == layer->compositedLayerMapping()->mainGraphicsLayer());
434                     sourceLayer->compositedLayerMapping()->mainGraphicsLayer()->setReplicatedByLayer(0);
435                 }
436             }
437
438             layer->clearCompositedLayerMapping();
439             compositedLayerMappingChanged = true;
440         }
441
442         break;
443     case RemoveFromSquashingLayer:
444     case NoCompositingStateChange:
445         // Do nothing.
446         break;
447     }
448
449     if (layer->hasCompositedLayerMapping() && layer->compositedLayerMapping()->updateRequiresOwnBackingStoreForIntrinsicReasons()) {
450         compositedLayerMappingChanged = true;
451         layer->compositedLayerMapping()->setNeedsGraphicsLayerUpdate(GraphicsLayerUpdateSubtree);
452     }
453
454     if (compositedLayerMappingChanged && layer->renderer()->isRenderPart()) {
455         RenderLayerCompositor* innerCompositor = frameContentsCompositor(toRenderPart(layer->renderer()));
456         if (innerCompositor && innerCompositor->staleInCompositingMode())
457             innerCompositor->updateRootLayerAttachment();
458     }
459
460     if (compositedLayerMappingChanged)
461         layer->clipper().clearClipRectsIncludingDescendants(PaintingClipRects);
462
463     // If a fixed position layer gained/lost a compositedLayerMapping or the reason not compositing it changed,
464     // the scrolling coordinator needs to recalculate whether it can do fast scrolling.
465     if (compositedLayerMappingChanged) {
466         if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator())
467             scrollingCoordinator->frameViewFixedObjectsDidChange(m_renderView.frameView());
468     }
469
470     return compositedLayerMappingChanged;
471 }
472
473 void RenderLayerCompositor::paintInvalidationOnCompositingChange(RenderLayer* layer)
474 {
475     // If the renderer is not attached yet, no need to issue paint invalidations.
476     if (layer->renderer() != &m_renderView && !layer->renderer()->parent())
477         return;
478
479     layer->paintInvalidator().paintInvalidationIncludingNonCompositingDescendants();
480 }
481
482 void RenderLayerCompositor::frameViewDidChangeLocation(const IntPoint& contentsOffset)
483 {
484     if (m_overflowControlsHostLayer)
485         m_overflowControlsHostLayer->setPosition(contentsOffset);
486 }
487
488 void RenderLayerCompositor::frameViewDidChangeSize()
489 {
490     if (m_containerLayer) {
491         FrameView* frameView = m_renderView.frameView();
492         m_containerLayer->setSize(frameView->unscaledVisibleContentSize());
493         m_overflowControlsHostLayer->setSize(frameView->unscaledVisibleContentSize(IncludeScrollbars));
494
495         frameViewDidScroll();
496         updateOverflowControlsLayers();
497     }
498 }
499
500 enum AcceleratedFixedRootBackgroundHistogramBuckets {
501     ScrolledMainFrameBucket = 0,
502     ScrolledMainFrameWithAcceleratedFixedRootBackground = 1,
503     ScrolledMainFrameWithUnacceleratedFixedRootBackground = 2,
504     AcceleratedFixedRootBackgroundHistogramMax = 3
505 };
506
507 void RenderLayerCompositor::frameViewDidScroll()
508 {
509     FrameView* frameView = m_renderView.frameView();
510     IntPoint scrollPosition = frameView->scrollPosition();
511
512     if (!m_scrollLayer)
513         return;
514
515     bool scrollingCoordinatorHandlesOffset = false;
516     if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator()) {
517         if (Settings* settings = m_renderView.document().settings()) {
518             if (m_renderView.frame()->isLocalRoot() || settings->compositedScrollingForFramesEnabled())
519                 scrollingCoordinatorHandlesOffset = scrollingCoordinator->scrollableAreaScrollLayerDidChange(frameView);
520         }
521     }
522
523     // Scroll position = scroll minimum + scroll offset. Adjust the layer's
524     // position to handle whatever the scroll coordinator isn't handling.
525     // The minimum scroll position is non-zero for RTL pages with overflow.
526     if (scrollingCoordinatorHandlesOffset)
527         m_scrollLayer->setPosition(-frameView->minimumScrollPosition());
528     else
529         m_scrollLayer->setPosition(-scrollPosition);
530
531
532     Platform::current()->histogramEnumeration("Renderer.AcceleratedFixedRootBackground",
533         ScrolledMainFrameBucket,
534         AcceleratedFixedRootBackgroundHistogramMax);
535 }
536
537 void RenderLayerCompositor::frameViewScrollbarsExistenceDidChange()
538 {
539     if (m_containerLayer)
540         updateOverflowControlsLayers();
541 }
542
543 void RenderLayerCompositor::rootFixedBackgroundsChanged()
544 {
545     if (!supportsFixedRootBackgroundCompositing())
546         return;
547
548     // To avoid having to make the fixed root background layer fixed positioned to
549     // stay put, we position it in the layer tree as follows:
550     //
551     // + Overflow controls host
552     //   + LocalFrame clip
553     //     + (Fixed root background) <-- Here.
554     //     + LocalFrame scroll
555     //       + Root content layer
556     //   + Scrollbars
557     //
558     // That is, it needs to be the first child of the frame clip, the sibling of
559     // the frame scroll layer. The compositor does not own the background layer, it
560     // just positions it (like the foreground layer).
561     if (GraphicsLayer* backgroundLayer = fixedRootBackgroundLayer())
562         m_containerLayer->addChildBelow(backgroundLayer, m_scrollLayer.get());
563 }
564
565 bool RenderLayerCompositor::scrollingLayerDidChange(RenderLayer* layer)
566 {
567     if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator())
568         return scrollingCoordinator->scrollableAreaScrollLayerDidChange(layer->scrollableArea());
569     return false;
570 }
571
572 String RenderLayerCompositor::layerTreeAsText(LayerTreeFlags flags)
573 {
574     ASSERT(lifecycle().state() >= DocumentLifecycle::PaintInvalidationClean);
575
576     if (!m_rootContentLayer)
577         return String();
578
579     // We skip dumping the scroll and clip layers to keep layerTreeAsText output
580     // similar between platforms (unless we explicitly request dumping from the
581     // root.
582     GraphicsLayer* rootLayer = m_rootContentLayer.get();
583     if (flags & LayerTreeIncludesRootLayer)
584         rootLayer = rootGraphicsLayer();
585
586     String layerTreeText = rootLayer->layerTreeAsText(flags);
587
588     // The true root layer is not included in the dump, so if we want to report
589     // its paint invalidation rects, they must be included here.
590     if (flags & LayerTreeIncludesPaintInvalidationRects)
591         return m_renderView.frameView()->trackedPaintInvalidationRectsAsText() + layerTreeText;
592
593     return layerTreeText;
594 }
595
596 RenderLayerCompositor* RenderLayerCompositor::frameContentsCompositor(RenderPart* renderer)
597 {
598     if (!renderer->node()->isFrameOwnerElement())
599         return 0;
600
601     HTMLFrameOwnerElement* element = toHTMLFrameOwnerElement(renderer->node());
602     if (Document* contentDocument = element->contentDocument()) {
603         if (RenderView* view = contentDocument->renderView())
604             return view->compositor();
605     }
606     return 0;
607 }
608
609 // FIXME: What does this function do? It needs a clearer name.
610 bool RenderLayerCompositor::parentFrameContentLayers(RenderPart* renderer)
611 {
612     RenderLayerCompositor* innerCompositor = frameContentsCompositor(renderer);
613     if (!innerCompositor || !innerCompositor->staleInCompositingMode() || innerCompositor->rootLayerAttachment() != RootLayerAttachedViaEnclosingFrame)
614         return false;
615
616     RenderLayer* layer = renderer->layer();
617     if (!layer->hasCompositedLayerMapping())
618         return false;
619
620     CompositedLayerMapping* compositedLayerMapping = layer->compositedLayerMapping();
621     GraphicsLayer* hostingLayer = compositedLayerMapping->parentForSublayers();
622     GraphicsLayer* rootLayer = innerCompositor->rootGraphicsLayer();
623     if (hostingLayer->children().size() != 1 || hostingLayer->children()[0] != rootLayer) {
624         hostingLayer->removeAllChildren();
625         hostingLayer->addChild(rootLayer);
626     }
627     return true;
628 }
629
630 static void fullyInvalidatePaintRecursive(RenderLayer* layer)
631 {
632     if (layer->compositingState() == PaintsIntoOwnBacking) {
633         layer->compositedLayerMapping()->setContentsNeedDisplay();
634         layer->compositedLayerMapping()->setSquashingContentsNeedDisplay();
635     }
636
637     for (RenderLayer* child = layer->firstChild(); child; child = child->nextSibling())
638         fullyInvalidatePaintRecursive(child);
639 }
640
641 void RenderLayerCompositor::fullyInvalidatePaint()
642 {
643     // We're walking all compositing layers and invalidating them, so there's
644     // no need to have up-to-date compositing state.
645     DisableCompositingQueryAsserts disabler;
646     fullyInvalidatePaintRecursive(rootRenderLayer());
647 }
648
649 RenderLayer* RenderLayerCompositor::rootRenderLayer() const
650 {
651     return m_renderView.layer();
652 }
653
654 GraphicsLayer* RenderLayerCompositor::rootGraphicsLayer() const
655 {
656     if (m_overflowControlsHostLayer)
657         return m_overflowControlsHostLayer.get();
658     return m_rootContentLayer.get();
659 }
660
661 GraphicsLayer* RenderLayerCompositor::scrollLayer() const
662 {
663     return m_scrollLayer.get();
664 }
665
666 GraphicsLayer* RenderLayerCompositor::containerLayer() const
667 {
668     return m_containerLayer.get();
669 }
670
671 GraphicsLayer* RenderLayerCompositor::ensureRootTransformLayer()
672 {
673     ASSERT(rootGraphicsLayer());
674
675     if (!m_rootTransformLayer.get()) {
676         m_rootTransformLayer = GraphicsLayer::create(graphicsLayerFactory(), this);
677         m_overflowControlsHostLayer->addChild(m_rootTransformLayer.get());
678         m_rootTransformLayer->addChild(m_containerLayer.get());
679         updateOverflowControlsLayers();
680     }
681
682     return m_rootTransformLayer.get();
683 }
684
685 void RenderLayerCompositor::setIsInWindow(bool isInWindow)
686 {
687     if (!staleInCompositingMode())
688         return;
689
690     if (isInWindow) {
691         if (m_rootLayerAttachment != RootLayerUnattached)
692             return;
693
694         RootLayerAttachment attachment = m_renderView.frame()->isLocalRoot() ? RootLayerAttachedViaChromeClient : RootLayerAttachedViaEnclosingFrame;
695         attachRootLayer(attachment);
696     } else {
697         if (m_rootLayerAttachment == RootLayerUnattached)
698             return;
699
700         detachRootLayer();
701     }
702 }
703
704 void RenderLayerCompositor::updateRootLayerPosition()
705 {
706     if (m_rootContentLayer) {
707         const IntRect& documentRect = m_renderView.documentRect();
708         m_rootContentLayer->setSize(documentRect.size());
709         m_rootContentLayer->setPosition(documentRect.location());
710 #if USE(RUBBER_BANDING)
711         if (m_layerForOverhangShadow)
712             OverscrollTheme::theme()->updateOverhangShadowLayer(m_layerForOverhangShadow.get(), m_rootContentLayer.get());
713 #endif
714     }
715     if (m_containerLayer) {
716         FrameView* frameView = m_renderView.frameView();
717         m_containerLayer->setSize(frameView->unscaledVisibleContentSize());
718         m_overflowControlsHostLayer->setSize(frameView->unscaledVisibleContentSize(IncludeScrollbars));
719     }
720 }
721
722 void RenderLayerCompositor::updatePotentialCompositingReasonsFromStyle(RenderLayer* layer)
723 {
724     layer->setPotentialCompositingReasonsFromStyle(m_compositingReasonFinder.potentialCompositingReasonsFromStyle(layer->renderer()));
725 }
726
727 void RenderLayerCompositor::updateDirectCompositingReasons(RenderLayer* layer)
728 {
729     layer->setCompositingReasons(m_compositingReasonFinder.directReasons(layer), CompositingReasonComboAllDirectReasons);
730 }
731
732 void RenderLayerCompositor::setOverlayLayer(GraphicsLayer* layer)
733 {
734     ASSERT(rootGraphicsLayer());
735
736     if (layer->parent() != m_overflowControlsHostLayer.get())
737         m_overflowControlsHostLayer->addChild(layer);
738 }
739
740 bool RenderLayerCompositor::canBeComposited(const RenderLayer* layer) const
741 {
742     // FIXME: We disable accelerated compositing for elements in a RenderFlowThread as it doesn't work properly.
743     // See http://webkit.org/b/84900 to re-enable it.
744     return m_hasAcceleratedCompositing && layer->isSelfPaintingLayer() && !layer->subtreeIsInvisible() && layer->renderer()->flowThreadState() == RenderObject::NotInsideFlowThread;
745 }
746
747 // Return true if the given layer is a stacking context and has compositing child
748 // layers that it needs to clip. In this case we insert a clipping GraphicsLayer
749 // into the hierarchy between this layer and its children in the z-order hierarchy.
750 bool RenderLayerCompositor::clipsCompositingDescendants(const RenderLayer* layer) const
751 {
752     return layer->hasCompositingDescendant() && layer->renderer()->hasClipOrOverflowClip();
753 }
754
755 // If an element has negative z-index children, those children render in front of the
756 // layer background, so we need an extra 'contents' layer for the foreground of the layer
757 // object.
758 bool RenderLayerCompositor::needsContentsCompositingLayer(const RenderLayer* layer) const
759 {
760     return layer->stackingNode()->hasNegativeZOrderList();
761 }
762
763 static void paintScrollbar(Scrollbar* scrollbar, GraphicsContext& context, const IntRect& clip)
764 {
765     if (!scrollbar)
766         return;
767
768     context.save();
769     const IntRect& scrollbarRect = scrollbar->frameRect();
770     context.translate(-scrollbarRect.x(), -scrollbarRect.y());
771     IntRect transformedClip = clip;
772     transformedClip.moveBy(scrollbarRect.location());
773     scrollbar->paint(&context, transformedClip);
774     context.restore();
775 }
776
777 void RenderLayerCompositor::paintContents(const GraphicsLayer* graphicsLayer, GraphicsContext& context, GraphicsLayerPaintingPhase, const IntRect& clip)
778 {
779     if (graphicsLayer == layerForHorizontalScrollbar())
780         paintScrollbar(m_renderView.frameView()->horizontalScrollbar(), context, clip);
781     else if (graphicsLayer == layerForVerticalScrollbar())
782         paintScrollbar(m_renderView.frameView()->verticalScrollbar(), context, clip);
783     else if (graphicsLayer == layerForScrollCorner()) {
784         const IntRect& scrollCorner = m_renderView.frameView()->scrollCornerRect();
785         context.save();
786         context.translate(-scrollCorner.x(), -scrollCorner.y());
787         IntRect transformedClip = clip;
788         transformedClip.moveBy(scrollCorner.location());
789         m_renderView.frameView()->paintScrollCorner(&context, transformedClip);
790         context.restore();
791     }
792 }
793
794 bool RenderLayerCompositor::supportsFixedRootBackgroundCompositing() const
795 {
796     if (Settings* settings = m_renderView.document().settings()) {
797         if (settings->acceleratedCompositingForFixedRootBackgroundEnabled())
798             return true;
799     }
800     return false;
801 }
802
803 bool RenderLayerCompositor::needsFixedRootBackgroundLayer(const RenderLayer* layer) const
804 {
805     if (layer != m_renderView.layer())
806         return false;
807
808     return supportsFixedRootBackgroundCompositing() && m_renderView.rootBackgroundIsEntirelyFixed();
809 }
810
811 GraphicsLayer* RenderLayerCompositor::fixedRootBackgroundLayer() const
812 {
813     // Get the fixed root background from the RenderView layer's compositedLayerMapping.
814     RenderLayer* viewLayer = m_renderView.layer();
815     if (!viewLayer)
816         return 0;
817
818     if (viewLayer->compositingState() == PaintsIntoOwnBacking && viewLayer->compositedLayerMapping()->backgroundLayerPaintsFixedRootBackground())
819         return viewLayer->compositedLayerMapping()->backgroundLayer();
820
821     return 0;
822 }
823
824 static void resetTrackedPaintInvalidationRectsRecursive(GraphicsLayer* graphicsLayer)
825 {
826     if (!graphicsLayer)
827         return;
828
829     graphicsLayer->resetTrackedPaintInvalidations();
830
831     for (size_t i = 0; i < graphicsLayer->children().size(); ++i)
832         resetTrackedPaintInvalidationRectsRecursive(graphicsLayer->children()[i]);
833
834     if (GraphicsLayer* replicaLayer = graphicsLayer->replicaLayer())
835         resetTrackedPaintInvalidationRectsRecursive(replicaLayer);
836
837     if (GraphicsLayer* maskLayer = graphicsLayer->maskLayer())
838         resetTrackedPaintInvalidationRectsRecursive(maskLayer);
839
840     if (GraphicsLayer* clippingMaskLayer = graphicsLayer->contentsClippingMaskLayer())
841         resetTrackedPaintInvalidationRectsRecursive(clippingMaskLayer);
842 }
843
844 void RenderLayerCompositor::resetTrackedPaintInvalidationRects()
845 {
846     if (GraphicsLayer* rootLayer = rootGraphicsLayer())
847         resetTrackedPaintInvalidationRectsRecursive(rootLayer);
848 }
849
850 void RenderLayerCompositor::setTracksPaintInvalidations(bool tracksPaintInvalidations)
851 {
852     ASSERT(lifecycle().state() == DocumentLifecycle::PaintInvalidationClean);
853     m_isTrackingPaintInvalidations = tracksPaintInvalidations;
854 }
855
856 bool RenderLayerCompositor::isTrackingPaintInvalidations() const
857 {
858     return m_isTrackingPaintInvalidations;
859 }
860
861 static bool shouldCompositeOverflowControls(FrameView* view)
862 {
863     if (Page* page = view->frame().page()) {
864         if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
865             if (scrollingCoordinator->coordinatesScrollingForFrameView(view))
866                 return true;
867     }
868
869     return true;
870 }
871
872 bool RenderLayerCompositor::requiresHorizontalScrollbarLayer() const
873 {
874     FrameView* view = m_renderView.frameView();
875     return shouldCompositeOverflowControls(view) && view->horizontalScrollbar();
876 }
877
878 bool RenderLayerCompositor::requiresVerticalScrollbarLayer() const
879 {
880     FrameView* view = m_renderView.frameView();
881     return shouldCompositeOverflowControls(view) && view->verticalScrollbar();
882 }
883
884 bool RenderLayerCompositor::requiresScrollCornerLayer() const
885 {
886     FrameView* view = m_renderView.frameView();
887     return shouldCompositeOverflowControls(view) && view->isScrollCornerVisible();
888 }
889
890 void RenderLayerCompositor::updateOverflowControlsLayers()
891 {
892 #if USE(RUBBER_BANDING)
893     if (m_renderView.frame()->isLocalRoot()) {
894         if (!m_layerForOverhangShadow) {
895             m_layerForOverhangShadow = GraphicsLayer::create(graphicsLayerFactory(), this);
896             OverscrollTheme::theme()->setUpOverhangShadowLayer(m_layerForOverhangShadow.get());
897             OverscrollTheme::theme()->updateOverhangShadowLayer(m_layerForOverhangShadow.get(), m_rootContentLayer.get());
898             m_scrollLayer->addChild(m_layerForOverhangShadow.get());
899         }
900     } else {
901         ASSERT(!m_layerForOverhangShadow);
902     }
903 #endif
904     GraphicsLayer* controlsParent = m_rootTransformLayer.get() ? m_rootTransformLayer.get() : m_overflowControlsHostLayer.get();
905
906     if (requiresHorizontalScrollbarLayer()) {
907         if (!m_layerForHorizontalScrollbar) {
908             m_layerForHorizontalScrollbar = GraphicsLayer::create(graphicsLayerFactory(), this);
909         }
910
911         if (m_layerForHorizontalScrollbar->parent() != controlsParent) {
912             controlsParent->addChild(m_layerForHorizontalScrollbar.get());
913
914             if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator())
915                 scrollingCoordinator->scrollableAreaScrollbarLayerDidChange(m_renderView.frameView(), HorizontalScrollbar);
916         }
917     } else if (m_layerForHorizontalScrollbar) {
918         m_layerForHorizontalScrollbar->removeFromParent();
919         m_layerForHorizontalScrollbar = nullptr;
920
921         if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator())
922             scrollingCoordinator->scrollableAreaScrollbarLayerDidChange(m_renderView.frameView(), HorizontalScrollbar);
923     }
924
925     if (requiresVerticalScrollbarLayer()) {
926         if (!m_layerForVerticalScrollbar) {
927             m_layerForVerticalScrollbar = GraphicsLayer::create(graphicsLayerFactory(), this);
928         }
929
930         if (m_layerForVerticalScrollbar->parent() != controlsParent) {
931             controlsParent->addChild(m_layerForVerticalScrollbar.get());
932
933             if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator())
934                 scrollingCoordinator->scrollableAreaScrollbarLayerDidChange(m_renderView.frameView(), VerticalScrollbar);
935         }
936     } else if (m_layerForVerticalScrollbar) {
937         m_layerForVerticalScrollbar->removeFromParent();
938         m_layerForVerticalScrollbar = nullptr;
939
940         if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator())
941             scrollingCoordinator->scrollableAreaScrollbarLayerDidChange(m_renderView.frameView(), VerticalScrollbar);
942     }
943
944     if (requiresScrollCornerLayer()) {
945         if (!m_layerForScrollCorner) {
946             m_layerForScrollCorner = GraphicsLayer::create(graphicsLayerFactory(), this);
947             controlsParent->addChild(m_layerForScrollCorner.get());
948         }
949     } else if (m_layerForScrollCorner) {
950         m_layerForScrollCorner->removeFromParent();
951         m_layerForScrollCorner = nullptr;
952     }
953
954     m_renderView.frameView()->positionScrollbarLayers();
955 }
956
957 void RenderLayerCompositor::ensureRootLayer()
958 {
959     RootLayerAttachment expectedAttachment = m_renderView.frame()->isLocalRoot() ? RootLayerAttachedViaChromeClient : RootLayerAttachedViaEnclosingFrame;
960     if (expectedAttachment == m_rootLayerAttachment)
961          return;
962
963     if (!m_rootContentLayer) {
964         m_rootContentLayer = GraphicsLayer::create(graphicsLayerFactory(), this);
965         IntRect overflowRect = m_renderView.pixelSnappedLayoutOverflowRect();
966         m_rootContentLayer->setSize(FloatSize(overflowRect.maxX(), overflowRect.maxY()));
967         m_rootContentLayer->setPosition(FloatPoint());
968         m_rootContentLayer->setOwnerNodeId(InspectorNodeIds::idForNode(m_renderView.generatingNode()));
969
970         // Need to clip to prevent transformed content showing outside this frame
971         m_rootContentLayer->setMasksToBounds(true);
972     }
973
974     if (!m_overflowControlsHostLayer) {
975         ASSERT(!m_scrollLayer);
976         ASSERT(!m_containerLayer);
977
978         // Create a layer to host the clipping layer and the overflow controls layers.
979         m_overflowControlsHostLayer = GraphicsLayer::create(graphicsLayerFactory(), this);
980
981         // Create a clipping layer if this is an iframe or settings require to clip.
982         m_containerLayer = GraphicsLayer::create(graphicsLayerFactory(), this);
983         bool containerMasksToBounds = !m_renderView.frame()->isLocalRoot();
984         if (Settings* settings = m_renderView.document().settings()) {
985             if (settings->mainFrameClipsContent())
986                 containerMasksToBounds = true;
987         }
988         m_containerLayer->setMasksToBounds(containerMasksToBounds);
989         m_overflowControlsHostLayer->setMasksToBounds(containerMasksToBounds);
990
991         m_scrollLayer = GraphicsLayer::create(graphicsLayerFactory(), this);
992         if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator())
993             scrollingCoordinator->setLayerIsContainerForFixedPositionLayers(m_scrollLayer.get(), true);
994
995         // Hook them up
996         m_overflowControlsHostLayer->addChild(m_containerLayer.get());
997         m_containerLayer->addChild(m_scrollLayer.get());
998         m_scrollLayer->addChild(m_rootContentLayer.get());
999
1000         frameViewDidChangeSize();
1001     }
1002
1003     // Check to see if we have to change the attachment
1004     if (m_rootLayerAttachment != RootLayerUnattached)
1005         detachRootLayer();
1006
1007     attachRootLayer(expectedAttachment);
1008 }
1009
1010 void RenderLayerCompositor::destroyRootLayer()
1011 {
1012     if (!m_rootContentLayer)
1013         return;
1014
1015     detachRootLayer();
1016
1017 #if USE(RUBBER_BANDING)
1018     if (m_layerForOverhangShadow) {
1019         m_layerForOverhangShadow->removeFromParent();
1020         m_layerForOverhangShadow = nullptr;
1021     }
1022 #endif
1023
1024     if (m_layerForHorizontalScrollbar) {
1025         m_layerForHorizontalScrollbar->removeFromParent();
1026         m_layerForHorizontalScrollbar = nullptr;
1027         if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator())
1028             scrollingCoordinator->scrollableAreaScrollbarLayerDidChange(m_renderView.frameView(), HorizontalScrollbar);
1029         if (Scrollbar* horizontalScrollbar = m_renderView.frameView()->verticalScrollbar())
1030             m_renderView.frameView()->invalidateScrollbar(horizontalScrollbar, IntRect(IntPoint(0, 0), horizontalScrollbar->frameRect().size()));
1031     }
1032
1033     if (m_layerForVerticalScrollbar) {
1034         m_layerForVerticalScrollbar->removeFromParent();
1035         m_layerForVerticalScrollbar = nullptr;
1036         if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator())
1037             scrollingCoordinator->scrollableAreaScrollbarLayerDidChange(m_renderView.frameView(), VerticalScrollbar);
1038         if (Scrollbar* verticalScrollbar = m_renderView.frameView()->verticalScrollbar())
1039             m_renderView.frameView()->invalidateScrollbar(verticalScrollbar, IntRect(IntPoint(0, 0), verticalScrollbar->frameRect().size()));
1040     }
1041
1042     if (m_layerForScrollCorner) {
1043         m_layerForScrollCorner = nullptr;
1044         m_renderView.frameView()->invalidateScrollCorner(m_renderView.frameView()->scrollCornerRect());
1045     }
1046
1047     if (m_overflowControlsHostLayer) {
1048         m_overflowControlsHostLayer = nullptr;
1049         m_containerLayer = nullptr;
1050         m_scrollLayer = nullptr;
1051     }
1052     ASSERT(!m_scrollLayer);
1053     m_rootContentLayer = nullptr;
1054     m_rootTransformLayer = nullptr;
1055 }
1056
1057 void RenderLayerCompositor::attachRootLayer(RootLayerAttachment attachment)
1058 {
1059     if (!m_rootContentLayer)
1060         return;
1061
1062     switch (attachment) {
1063         case RootLayerUnattached:
1064             ASSERT_NOT_REACHED();
1065             break;
1066         case RootLayerAttachedViaChromeClient: {
1067             LocalFrame& frame = m_renderView.frameView()->frame();
1068             Page* page = frame.page();
1069             if (!page)
1070                 return;
1071             page->chrome().client().attachRootGraphicsLayer(rootGraphicsLayer());
1072             break;
1073         }
1074         case RootLayerAttachedViaEnclosingFrame: {
1075             HTMLFrameOwnerElement* ownerElement = m_renderView.document().ownerElement();
1076             ASSERT(ownerElement);
1077             // The layer will get hooked up via CompositedLayerMapping::updateGraphicsLayerConfiguration()
1078             // for the frame's renderer in the parent document.
1079             ownerElement->setNeedsCompositingUpdate();
1080             break;
1081         }
1082     }
1083
1084     m_rootLayerAttachment = attachment;
1085 }
1086
1087 void RenderLayerCompositor::detachRootLayer()
1088 {
1089     if (!m_rootContentLayer || m_rootLayerAttachment == RootLayerUnattached)
1090         return;
1091
1092     switch (m_rootLayerAttachment) {
1093     case RootLayerAttachedViaEnclosingFrame: {
1094         // The layer will get unhooked up via CompositedLayerMapping::updateGraphicsLayerConfiguration()
1095         // for the frame's renderer in the parent document.
1096         if (m_overflowControlsHostLayer)
1097             m_overflowControlsHostLayer->removeFromParent();
1098         else
1099             m_rootContentLayer->removeFromParent();
1100
1101         if (HTMLFrameOwnerElement* ownerElement = m_renderView.document().ownerElement())
1102             ownerElement->setNeedsCompositingUpdate();
1103         break;
1104     }
1105     case RootLayerAttachedViaChromeClient: {
1106         LocalFrame& frame = m_renderView.frameView()->frame();
1107         Page* page = frame.page();
1108         if (!page)
1109             return;
1110         page->chrome().client().attachRootGraphicsLayer(0);
1111     }
1112     break;
1113     case RootLayerUnattached:
1114         break;
1115     }
1116
1117     m_rootLayerAttachment = RootLayerUnattached;
1118 }
1119
1120 void RenderLayerCompositor::updateRootLayerAttachment()
1121 {
1122     ensureRootLayer();
1123 }
1124
1125 ScrollingCoordinator* RenderLayerCompositor::scrollingCoordinator() const
1126 {
1127     if (Page* page = this->page())
1128         return page->scrollingCoordinator();
1129
1130     return 0;
1131 }
1132
1133 GraphicsLayerFactory* RenderLayerCompositor::graphicsLayerFactory() const
1134 {
1135     if (Page* page = this->page())
1136         return page->chrome().client().graphicsLayerFactory();
1137     return 0;
1138 }
1139
1140 Page* RenderLayerCompositor::page() const
1141 {
1142     return m_renderView.frameView()->frame().page();
1143 }
1144
1145 DocumentLifecycle& RenderLayerCompositor::lifecycle() const
1146 {
1147     return m_renderView.document().lifecycle();
1148 }
1149
1150 String RenderLayerCompositor::debugName(const GraphicsLayer* graphicsLayer)
1151 {
1152     String name;
1153     if (graphicsLayer == m_rootContentLayer.get()) {
1154         name = "Content Root Layer";
1155     } else if (graphicsLayer == m_rootTransformLayer.get()) {
1156         name = "Root Transform Layer";
1157 #if USE(RUBBER_BANDING)
1158     } else if (graphicsLayer == m_layerForOverhangShadow.get()) {
1159         name = "Overhang Areas Shadow";
1160 #endif
1161     } else if (graphicsLayer == m_overflowControlsHostLayer.get()) {
1162         name = "Overflow Controls Host Layer";
1163     } else if (graphicsLayer == m_layerForHorizontalScrollbar.get()) {
1164         name = "Horizontal Scrollbar Layer";
1165     } else if (graphicsLayer == m_layerForVerticalScrollbar.get()) {
1166         name = "Vertical Scrollbar Layer";
1167     } else if (graphicsLayer == m_layerForScrollCorner.get()) {
1168         name = "Scroll Corner Layer";
1169     } else if (graphicsLayer == m_containerLayer.get()) {
1170         name = "LocalFrame Clipping Layer";
1171     } else if (graphicsLayer == m_scrollLayer.get()) {
1172         name = "LocalFrame Scrolling Layer";
1173     } else {
1174         ASSERT_NOT_REACHED();
1175     }
1176
1177     return name;
1178 }
1179
1180 } // namespace blink