b0397270606616547168bac76472ebf8735595da
[WebKit-https.git] / Source / WebCore / rendering / 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 #if USE(ACCELERATED_COMPOSITING)
29 #include "RenderLayerCompositor.h"
30
31 #include "AnimationController.h"
32 #include "CanvasRenderingContext.h"
33 #include "CSSPropertyNames.h"
34 #include "Chrome.h"
35 #include "ChromeClient.h"
36 #include "Frame.h"
37 #include "FrameView.h"
38 #include "GraphicsLayer.h"
39 #include "HTMLCanvasElement.h"
40 #include "HTMLIFrameElement.h"
41 #include "HTMLNames.h"
42 #include "HitTestResult.h"
43 #include "InspectorInstrumentation.h"
44 #include "Logging.h"
45 #include "NodeList.h"
46 #include "Page.h"
47 #include "RenderApplet.h"
48 #include "RenderEmbeddedObject.h"
49 #include "RenderFullScreen.h"
50 #include "RenderGeometryMap.h"
51 #include "RenderIFrame.h"
52 #include "RenderLayerBacking.h"
53 #include "RenderReplica.h"
54 #include "RenderVideo.h"
55 #include "RenderView.h"
56 #include "ScrollbarTheme.h"
57 #include "ScrollingConstraints.h"
58 #include "ScrollingCoordinator.h"
59 #include "Settings.h"
60 #include "TiledBacking.h"
61 #include "TransformState.h"
62 #include "WebCoreMemoryInstrumentation.h"
63 #include <wtf/MemoryInstrumentationHashMap.h>
64
65 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
66 #include "HTMLMediaElement.h"
67 #endif
68
69 #if !LOG_DISABLED
70 #include <wtf/CurrentTime.h>
71 #endif
72
73 #ifndef NDEBUG
74 #include "RenderTreeAsText.h"
75 #endif
76
77 #if ENABLE(3D_RENDERING)
78 // This symbol is used to determine from a script whether 3D rendering is enabled (via 'nm').
79 bool WebCoreHas3DRendering = true;
80 #endif
81
82 namespace WebCore {
83
84 using namespace HTMLNames;
85
86 class RenderLayerCompositor::OverlapMap {
87     WTF_MAKE_NONCOPYABLE(OverlapMap);
88 public:
89     OverlapMap()
90         : m_geometryMap(UseTransforms)
91     {
92         // Begin assuming the root layer will be composited so that there is
93         // something on the stack. The root layer should also never get an
94         // popCompositingContainer call.
95         pushCompositingContainer();
96     }
97
98     void add(const RenderLayer* layer, const IntRect& bounds)
99     {
100         // Layers do not contribute to overlap immediately--instead, they will
101         // contribute to overlap as soon as their composited ancestor has been
102         // recursively processed and popped off the stack.
103         ASSERT(m_overlapStack.size() >= 2);
104         m_overlapStack[m_overlapStack.size() - 2].append(bounds);
105         m_layers.add(layer);
106     }
107
108     bool contains(const RenderLayer* layer)
109     {
110         return m_layers.contains(layer);
111     }
112
113     bool overlapsLayers(const IntRect& bounds) const
114     {
115         const RectList& layerRects = m_overlapStack.last();
116         for (unsigned i = 0; i < layerRects.size(); i++) {
117             if (layerRects[i].intersects(bounds))
118                 return true;
119         }
120         return false;
121     }
122
123     bool isEmpty()
124     {
125         return m_layers.isEmpty();
126     }
127
128     void pushCompositingContainer()
129     {
130         m_overlapStack.append(RectList());
131     }
132
133     void popCompositingContainer()
134     {
135         m_overlapStack[m_overlapStack.size() - 2].append(m_overlapStack.last());
136         m_overlapStack.removeLast();
137     }
138
139     RenderGeometryMap& geometryMap() { return m_geometryMap; }
140
141 private:
142     typedef Vector<IntRect> RectList;
143     Vector<RectList> m_overlapStack;
144     HashSet<const RenderLayer*> m_layers;
145     RenderGeometryMap m_geometryMap;
146 };
147
148 struct CompositingState {
149     CompositingState(RenderLayer* compAncestor, bool testOverlap)
150         : m_compositingAncestor(compAncestor)
151         , m_subtreeIsCompositing(false)
152         , m_testingOverlap(testOverlap)
153 #ifndef NDEBUG
154         , m_depth(0)
155 #endif
156     {
157     }
158     
159     CompositingState(const CompositingState& other)
160         : m_compositingAncestor(other.m_compositingAncestor)
161         , m_subtreeIsCompositing(other.m_subtreeIsCompositing)
162         , m_testingOverlap(other.m_testingOverlap)
163 #ifndef NDEBUG
164         , m_depth(other.m_depth + 1)
165 #endif
166     {
167     }
168     
169     RenderLayer* m_compositingAncestor;
170     bool m_subtreeIsCompositing;
171     bool m_testingOverlap;
172 #ifndef NDEBUG
173     int m_depth;
174 #endif
175 };
176
177
178 static inline bool compositingLogEnabled()
179 {
180 #if !LOG_DISABLED
181     return LogCompositing.state == WTFLogChannelOn;
182 #else
183     return false;
184 #endif
185 }
186
187 RenderLayerCompositor::RenderLayerCompositor(RenderView* renderView)
188     : m_renderView(renderView)
189     , m_updateCompositingLayersTimer(this, &RenderLayerCompositor::updateCompositingLayersTimerFired)
190     , m_hasAcceleratedCompositing(true)
191     , m_compositingTriggers(static_cast<ChromeClient::CompositingTriggerFlags>(ChromeClient::AllTriggers))
192     , m_compositedLayerCount(0)
193     , m_showDebugBorders(false)
194     , m_showRepaintCounter(false)
195     , m_acceleratedDrawingEnabled(false)
196     , m_compositingConsultsOverlap(true)
197     , m_reevaluateCompositingAfterLayout(false)
198     , m_compositing(false)
199     , m_compositingLayersNeedRebuild(false)
200     , m_flushingLayers(false)
201     , m_shouldFlushOnReattach(false)
202     , m_forceCompositingMode(false)
203     , m_isTrackingRepaints(false)
204     , m_rootLayerAttachment(RootLayerUnattached)
205 #if !LOG_DISABLED
206     , m_rootLayerUpdateCount(0)
207     , m_obligateCompositedLayerCount(0)
208     , m_secondaryCompositedLayerCount(0)
209     , m_obligatoryBackingStoreBytes(0)
210     , m_secondaryBackingStoreBytes(0)
211 #endif
212 {
213 }
214
215 RenderLayerCompositor::~RenderLayerCompositor()
216 {
217     ASSERT(m_rootLayerAttachment == RootLayerUnattached);
218 }
219
220 void RenderLayerCompositor::enableCompositingMode(bool enable /* = true */)
221 {
222     if (enable != m_compositing) {
223         m_compositing = enable;
224         
225         if (m_compositing) {
226             ensureRootLayer();
227             notifyIFramesOfCompositingChange();
228         } else
229             destroyRootLayer();
230     }
231 }
232
233 void RenderLayerCompositor::cacheAcceleratedCompositingFlags()
234 {
235     bool hasAcceleratedCompositing = false;
236     bool showDebugBorders = false;
237     bool showRepaintCounter = false;
238     bool forceCompositingMode = false;
239     bool acceleratedDrawingEnabled = false;
240
241     if (Settings* settings = m_renderView->document()->settings()) {
242         hasAcceleratedCompositing = settings->acceleratedCompositingEnabled();
243
244         // We allow the chrome to override the settings, in case the page is rendered
245         // on a chrome that doesn't allow accelerated compositing.
246         if (hasAcceleratedCompositing) {
247             if (Page* page = this->page()) {
248                 ChromeClient* chromeClient = page->chrome()->client();
249                 m_compositingTriggers = chromeClient->allowedCompositingTriggers();
250                 hasAcceleratedCompositing = m_compositingTriggers;
251             }
252         }
253
254         showDebugBorders = settings->showDebugBorders();
255         showRepaintCounter = settings->showRepaintCounter();
256         forceCompositingMode = settings->forceCompositingMode() && hasAcceleratedCompositing;
257
258         if (forceCompositingMode && m_renderView->document()->ownerElement())
259             forceCompositingMode = requiresCompositingForScrollableFrame();
260
261         acceleratedDrawingEnabled = settings->acceleratedDrawingEnabled();
262     }
263
264     if (hasAcceleratedCompositing != m_hasAcceleratedCompositing || showDebugBorders != m_showDebugBorders || showRepaintCounter != m_showRepaintCounter || forceCompositingMode != m_forceCompositingMode)
265         setCompositingLayersNeedRebuild();
266
267     bool debugBordersChanged = m_showDebugBorders != showDebugBorders;
268     m_hasAcceleratedCompositing = hasAcceleratedCompositing;
269     m_showDebugBorders = showDebugBorders;
270     m_showRepaintCounter = showRepaintCounter;
271     m_forceCompositingMode = forceCompositingMode;
272     m_acceleratedDrawingEnabled = acceleratedDrawingEnabled;
273     
274     if (debugBordersChanged) {
275         if (m_layerForHorizontalScrollbar)
276             m_layerForHorizontalScrollbar->setShowDebugBorder(m_showDebugBorders);
277
278         if (m_layerForVerticalScrollbar)
279             m_layerForVerticalScrollbar->setShowDebugBorder(m_showDebugBorders);
280
281         if (m_layerForScrollCorner)
282             m_layerForScrollCorner->setShowDebugBorder(m_showDebugBorders);
283     }
284 }
285
286 bool RenderLayerCompositor::canRender3DTransforms() const
287 {
288     return hasAcceleratedCompositing() && (m_compositingTriggers & ChromeClient::ThreeDTransformTrigger);
289 }
290
291 void RenderLayerCompositor::setCompositingLayersNeedRebuild(bool needRebuild)
292 {
293     if (inCompositingMode())
294         m_compositingLayersNeedRebuild = needRebuild;
295 }
296
297 void RenderLayerCompositor::scheduleLayerFlush()
298 {
299     if (Page* page = this->page())
300         page->chrome()->client()->scheduleCompositingLayerFlush();
301 }
302
303 void RenderLayerCompositor::flushPendingLayerChanges(bool isFlushRoot)
304 {
305     // FrameView::flushCompositingStateIncludingSubframes() flushes each subframe,
306     // but GraphicsLayer::flushCompositingState() will cross frame boundaries
307     // if the GraphicsLayers are connected (the RootLayerAttachedViaEnclosingFrame case).
308     // As long as we're not the root of the flush, we can bail.
309     if (!isFlushRoot && rootLayerAttachment() == RootLayerAttachedViaEnclosingFrame)
310         return;
311     
312     if (rootLayerAttachment() == RootLayerUnattached) {
313         m_shouldFlushOnReattach = true;
314         return;
315     }
316
317     AnimationUpdateBlock animationUpdateBlock(m_renderView->frameView()->frame()->animation());
318
319     ASSERT(!m_flushingLayers);
320     m_flushingLayers = true;
321
322     if (GraphicsLayer* rootLayer = rootGraphicsLayer()) {
323         FrameView* frameView = m_renderView ? m_renderView->frameView() : 0;
324         if (frameView) {
325             // Having a m_clipLayer indicates that we're doing scrolling via GraphicsLayers.
326             IntRect visibleRect = m_clipLayer ? IntRect(IntPoint(), frameView->contentsSize()) : frameView->visibleContentRect();
327             rootLayer->flushCompositingState(visibleRect);
328         }
329     }
330     
331     ASSERT(m_flushingLayers);
332     m_flushingLayers = false;
333
334     if (!m_viewportConstrainedLayersNeedingUpdate.isEmpty()) {
335         HashSet<RenderLayer*>::const_iterator end = m_viewportConstrainedLayersNeedingUpdate.end();
336         for (HashSet<RenderLayer*>::const_iterator it = m_viewportConstrainedLayersNeedingUpdate.begin(); it != end; ++it)
337             registerOrUpdateViewportConstrainedLayer(*it);
338         
339         m_viewportConstrainedLayersNeedingUpdate.clear();
340     }
341 }
342
343 void RenderLayerCompositor::didFlushChangesForLayer(RenderLayer* layer)
344 {
345     if (m_viewportConstrainedLayers.contains(layer))
346         m_viewportConstrainedLayersNeedingUpdate.add(layer);
347 }
348
349 void RenderLayerCompositor::notifyFlushBeforeDisplayRefresh(const GraphicsLayer*)
350 {
351     if (!m_layerUpdater) {
352         PlatformDisplayID displayID = 0;
353         if (Page* page = this->page())
354             displayID = page->displayID();
355
356         m_layerUpdater = adoptPtr(new GraphicsLayerUpdater(this, displayID));
357     }
358     
359     m_layerUpdater->scheduleUpdate();
360 }
361
362 void RenderLayerCompositor::flushLayers(GraphicsLayerUpdater*)
363 {
364     flushPendingLayerChanges(true); // FIXME: deal with iframes
365 }
366
367 RenderLayerCompositor* RenderLayerCompositor::enclosingCompositorFlushingLayers() const
368 {
369     if (!m_renderView->frameView())
370         return 0;
371
372     for (Frame* frame = m_renderView->frameView()->frame(); frame; frame = frame->tree()->parent()) {
373         RenderLayerCompositor* compositor = frame->contentRenderer() ? frame->contentRenderer()->compositor() : 0;
374         if (compositor->isFlushingLayers())
375             return compositor;
376     }
377     
378     return 0;
379 }
380
381 void RenderLayerCompositor::scheduleCompositingLayerUpdate()
382 {
383     if (!m_updateCompositingLayersTimer.isActive())
384         m_updateCompositingLayersTimer.startOneShot(0);
385 }
386
387 void RenderLayerCompositor::updateCompositingLayersTimerFired(Timer<RenderLayerCompositor>*)
388 {
389     updateCompositingLayers(CompositingUpdateAfterLayout);
390 }
391
392 bool RenderLayerCompositor::hasAnyAdditionalCompositedLayers(const RenderLayer* rootLayer) const
393 {
394     return m_compositedLayerCount > (rootLayer->isComposited() ? 1 : 0);
395 }
396
397 void RenderLayerCompositor::updateCompositingLayers(CompositingUpdateType updateType, RenderLayer* updateRoot)
398 {
399     m_updateCompositingLayersTimer.stop();
400     
401     // Compositing layers will be updated in Document::implicitClose() if suppressed here.
402     if (!m_renderView->document()->visualUpdatesAllowed())
403         return;
404
405     // Avoid updating the layers with old values. Compositing layers will be updated after the layout is finished.
406     if (m_renderView->needsLayout())
407         return;
408
409     if (m_forceCompositingMode && !m_compositing)
410         enableCompositingMode(true);
411
412     if (!m_reevaluateCompositingAfterLayout && !m_compositing)
413         return;
414
415     AnimationUpdateBlock animationUpdateBlock(m_renderView->frameView()->frame()->animation());
416
417     bool checkForHierarchyUpdate = m_reevaluateCompositingAfterLayout;
418     bool needGeometryUpdate = false;
419
420     switch (updateType) {
421     case CompositingUpdateAfterStyleChange:
422     case CompositingUpdateAfterLayout:
423     case CompositingUpdateOnHitTest:
424         checkForHierarchyUpdate = true;
425         break;
426     case CompositingUpdateOnScroll:
427         if (m_compositingConsultsOverlap)
428             checkForHierarchyUpdate = true; // Overlap can change with scrolling, so need to check for hierarchy updates.
429
430         needGeometryUpdate = true;
431         break;
432     }
433
434     if (!checkForHierarchyUpdate && !needGeometryUpdate)
435         return;
436
437     bool needHierarchyUpdate = m_compositingLayersNeedRebuild;
438     bool isFullUpdate = !updateRoot;
439     if (!updateRoot || m_compositingConsultsOverlap) {
440         // Only clear the flag if we're updating the entire hierarchy.
441         m_compositingLayersNeedRebuild = false;
442         updateRoot = rootRenderLayer();
443     }
444
445     if (isFullUpdate && updateType == CompositingUpdateAfterLayout)
446         m_reevaluateCompositingAfterLayout = false;
447
448 #if !LOG_DISABLED
449     double startTime = 0;
450     if (compositingLogEnabled()) {
451         ++m_rootLayerUpdateCount;
452         startTime = currentTime();
453     }
454 #endif
455
456     if (checkForHierarchyUpdate) {
457         // Go through the layers in presentation order, so that we can compute which RenderLayers need compositing layers.
458         // FIXME: we could maybe do this and the hierarchy udpate in one pass, but the parenting logic would be more complex.
459         CompositingState compState(updateRoot, m_compositingConsultsOverlap);
460         bool layersChanged = false;
461         bool saw3DTransform = false;
462         if (m_compositingConsultsOverlap) {
463             OverlapMap overlapTestRequestMap;
464             computeCompositingRequirements(0, updateRoot, &overlapTestRequestMap, compState, layersChanged, saw3DTransform);
465         } else
466             computeCompositingRequirements(0, updateRoot, 0, compState, layersChanged, saw3DTransform);
467         
468         needHierarchyUpdate |= layersChanged;
469     }
470
471 #if !LOG_DISABLED
472     if (compositingLogEnabled() && isFullUpdate && (needHierarchyUpdate || needGeometryUpdate)) {
473         m_obligateCompositedLayerCount = 0;
474         m_secondaryCompositedLayerCount = 0;
475         m_obligatoryBackingStoreBytes = 0;
476         m_secondaryBackingStoreBytes = 0;
477
478         Frame* frame = m_renderView->frameView()->frame();
479         bool isMainFrame = !m_renderView->document()->ownerElement();
480         LOG(Compositing, "\nUpdate %d of %s. Overlap testing is %s\n", m_rootLayerUpdateCount, isMainFrame ? "main frame" : frame->tree()->uniqueName().string().utf8().data(),
481             m_compositingConsultsOverlap ? "on" : "off");
482     }
483 #endif
484
485     if (needHierarchyUpdate) {
486         // Update the hierarchy of the compositing layers.
487         Vector<GraphicsLayer*> childList;
488         rebuildCompositingLayerTree(updateRoot, childList, 0);
489
490         // Host the document layer in the RenderView's root layer.
491         if (isFullUpdate) {
492             // Even when childList is empty, don't drop out of compositing mode if there are
493             // composited layers that we didn't hit in our traversal (e.g. because of visibility:hidden).
494             if (childList.isEmpty() && !hasAnyAdditionalCompositedLayers(updateRoot))
495                 destroyRootLayer();
496             else
497                 m_rootContentLayer->setChildren(childList);
498         }
499     } else if (needGeometryUpdate) {
500         // We just need to do a geometry update. This is only used for position:fixed scrolling;
501         // most of the time, geometry is updated via RenderLayer::styleChanged().
502         updateLayerTreeGeometry(updateRoot, 0);
503     }
504     
505 #if !LOG_DISABLED
506     if (compositingLogEnabled() && isFullUpdate && (needHierarchyUpdate || needGeometryUpdate)) {
507         double endTime = currentTime();
508         LOG(Compositing, "Total layers   primary   secondary   obligatory backing (KB)   secondary backing(KB)   total backing (KB)  update time (ms)\n");
509
510         LOG(Compositing, "%8d %11d %9d %20.2f %22.2f %22.2f %18.2f\n",
511             m_obligateCompositedLayerCount + m_secondaryCompositedLayerCount, m_obligateCompositedLayerCount,
512             m_secondaryCompositedLayerCount, m_obligatoryBackingStoreBytes / 1024, m_secondaryBackingStoreBytes / 1024, (m_obligatoryBackingStoreBytes + m_secondaryBackingStoreBytes) / 1024, 1000.0 * (endTime - startTime));
513     }
514 #endif
515     ASSERT(updateRoot || !m_compositingLayersNeedRebuild);
516
517     if (!hasAcceleratedCompositing())
518         enableCompositingMode(false);
519
520     // Inform the inspector that the layer tree has changed.
521     InspectorInstrumentation::layerTreeDidChange(page());
522 }
523
524 void RenderLayerCompositor::layerBecameNonComposited(const RenderLayer* renderLayer)
525 {
526     // Inform the inspector that the given RenderLayer was destroyed.
527     InspectorInstrumentation::renderLayerDestroyed(page(), renderLayer);
528
529     ASSERT(m_compositedLayerCount > 0);
530     --m_compositedLayerCount;
531 }
532
533 #if !LOG_DISABLED
534 void RenderLayerCompositor::logLayerInfo(const RenderLayer* layer, int depth)
535 {
536     if (!compositingLogEnabled())
537         return;
538         
539     RenderLayerBacking* backing = layer->backing();
540     if (requiresCompositingLayer(layer) || layer->isRootLayer()) {
541         ++m_obligateCompositedLayerCount;
542         m_obligatoryBackingStoreBytes += backing->backingStoreMemoryEstimate();
543     } else {
544         ++m_secondaryCompositedLayerCount;
545         m_secondaryBackingStoreBytes += backing->backingStoreMemoryEstimate();
546     }
547
548     LOG(Compositing, "%*p %dx%d %.2fKB (%s) %s\n", 12 + depth * 2, layer, backing->compositedBounds().width(), backing->compositedBounds().height(),
549         backing->backingStoreMemoryEstimate() / 1024,
550         reasonForCompositing(layer), layer->name().utf8().data());
551 }
552 #endif
553
554 bool RenderLayerCompositor::updateBacking(RenderLayer* layer, CompositingChangeRepaint shouldRepaint)
555 {
556     bool layerChanged = false;
557     RenderLayer::ViewportConstrainedNotCompositedReason viewportConstrainedNotCompositedReason = RenderLayer::NoNotCompositedReason;
558
559     if (needsToBeComposited(layer, &viewportConstrainedNotCompositedReason)) {
560         enableCompositingMode();
561         
562         if (!layer->backing()) {
563             // If we need to repaint, do so before making backing
564             if (shouldRepaint == CompositingChangeRepaintNow)
565                 repaintOnCompositingChange(layer);
566
567             layer->ensureBacking();
568
569             // At this time, the ScrollingCooridnator only supports the top-level frame.
570             if (layer->isRootLayer() && !m_renderView->document()->ownerElement()) {
571                 layer->backing()->attachToScrollingCoordinatorWithParent(0);
572                 if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator())
573                     scrollingCoordinator->frameViewRootLayerDidChange(m_renderView->frameView());
574             }
575
576             // This layer and all of its descendants have cached repaints rects that are relative to
577             // the repaint container, so change when compositing changes; we need to update them here.
578             if (layer->parent())
579                 layer->computeRepaintRectsIncludingDescendants();
580
581             layerChanged = true;
582         }
583
584         // Need to add for every compositing layer, because a composited layer may change from being non-fixed to fixed.
585         updateViewportConstraintStatus(layer);
586     } else {
587         if (layer->backing()) {
588             // If we're removing backing on a reflection, clear the source GraphicsLayer's pointer to
589             // its replica GraphicsLayer. In practice this should never happen because reflectee and reflection 
590             // are both either composited, or not composited.
591             if (layer->isReflection()) {
592                 RenderLayer* sourceLayer = toRenderLayerModelObject(layer->renderer()->parent())->layer();
593                 if (RenderLayerBacking* backing = sourceLayer->backing()) {
594                     ASSERT(backing->graphicsLayer()->replicaLayer() == layer->backing()->graphicsLayer());
595                     backing->graphicsLayer()->setReplicatedByLayer(0);
596                 }
597             }
598
599             removeViewportConstrainedLayer(layer);
600             
601             layer->clearBacking();
602             layerChanged = true;
603
604             // This layer and all of its descendants have cached repaints rects that are relative to
605             // the repaint container, so change when compositing changes; we need to update them here.
606             layer->computeRepaintRectsIncludingDescendants();
607
608             // If we need to repaint, do so now that we've removed the backing
609             if (shouldRepaint == CompositingChangeRepaintNow)
610                 repaintOnCompositingChange(layer);
611         }
612     }
613     
614 #if ENABLE(VIDEO)
615     if (layerChanged && layer->renderer()->isVideo()) {
616         // If it's a video, give the media player a chance to hook up to the layer.
617         RenderVideo* video = toRenderVideo(layer->renderer());
618         video->acceleratedRenderingStateChanged();
619     }
620 #endif
621
622     if (layerChanged && layer->renderer()->isRenderPart()) {
623         RenderLayerCompositor* innerCompositor = frameContentsCompositor(toRenderPart(layer->renderer()));
624         if (innerCompositor && innerCompositor->inCompositingMode())
625             innerCompositor->updateRootLayerAttachment();
626     }
627     
628     if (layerChanged)
629         layer->clearClipRectsIncludingDescendants(PaintingClipRects);
630
631     // If a fixed position layer gained/lost a backing or the reason not compositing it changed,
632     // the scrolling coordinator needs to recalculate whether it can do fast scrolling.
633     if (layer->renderer()->style()->position() == FixedPosition) {
634         if (layer->viewportConstrainedNotCompositedReason() != viewportConstrainedNotCompositedReason) {
635             layer->setViewportConstrainedNotCompositedReason(viewportConstrainedNotCompositedReason);
636             layerChanged = true;
637         }
638         if (layerChanged) {
639             if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator())
640                 scrollingCoordinator->frameViewFixedObjectsDidChange(m_renderView->frameView());
641         }
642     }
643     
644     if (layer->backing())
645         layer->backing()->updateDebugIndicators(m_showDebugBorders, m_showRepaintCounter);
646
647     return layerChanged;
648 }
649
650 bool RenderLayerCompositor::updateLayerCompositingState(RenderLayer* layer, CompositingChangeRepaint shouldRepaint)
651 {
652     bool layerChanged = updateBacking(layer, shouldRepaint);
653
654     // See if we need content or clipping layers. Methods called here should assume
655     // that the compositing state of descendant layers has not been updated yet.
656     if (layer->backing() && layer->backing()->updateGraphicsLayerConfiguration())
657         layerChanged = true;
658
659     return layerChanged;
660 }
661
662 void RenderLayerCompositor::repaintOnCompositingChange(RenderLayer* layer)
663 {
664     // If the renderer is not attached yet, no need to repaint.
665     if (layer->renderer() != m_renderView && !layer->renderer()->parent())
666         return;
667
668     RenderLayerModelObject* repaintContainer = layer->renderer()->containerForRepaint();
669     if (!repaintContainer)
670         repaintContainer = m_renderView;
671
672     layer->repaintIncludingNonCompositingDescendants(repaintContainer);
673     if (repaintContainer == m_renderView) {
674         // The contents of this layer may be moving between the window
675         // and a GraphicsLayer, so we need to make sure the window system
676         // synchronizes those changes on the screen.
677         m_renderView->frameView()->setNeedsOneShotDrawingSynchronization();
678     }
679 }
680
681 // This method assumes that layout is up-to-date, unlike repaintOnCompositingChange().
682 void RenderLayerCompositor::repaintInCompositedAncestor(RenderLayer* layer, const LayoutRect& rect)
683 {
684     RenderLayer* compositedAncestor = layer->enclosingCompositingLayerForRepaint(false /*exclude self*/);
685     if (compositedAncestor) {
686         ASSERT(compositedAncestor->backing());
687
688         LayoutPoint offset;
689         layer->convertToLayerCoords(compositedAncestor, offset);
690
691         LayoutRect repaintRect = rect;
692         repaintRect.moveBy(offset);
693
694         compositedAncestor->setBackingNeedsRepaintInRect(repaintRect);
695     }
696
697     // The contents of this layer may be moving from a GraphicsLayer to the window,
698     // so we need to make sure the window system synchronizes those changes on the screen.
699     if (compositedAncestor == m_renderView->layer())
700         m_renderView->frameView()->setNeedsOneShotDrawingSynchronization();
701 }
702
703 // The bounds of the GraphicsLayer created for a compositing layer is the union of the bounds of all the descendant
704 // RenderLayers that are rendered by the composited RenderLayer.
705 IntRect RenderLayerCompositor::calculateCompositedBounds(const RenderLayer* layer, const RenderLayer* ancestorLayer) const
706 {
707     if (!canBeComposited(layer))
708         return IntRect();
709     return layer->calculateLayerBounds(ancestorLayer, 0, RenderLayer::DefaultCalculateLayerBoundsFlags | RenderLayer::ExcludeHiddenDescendants | RenderLayer::DontConstrainForMask);
710 }
711
712 void RenderLayerCompositor::layerWasAdded(RenderLayer* /*parent*/, RenderLayer* /*child*/)
713 {
714     setCompositingLayersNeedRebuild();
715 }
716
717 void RenderLayerCompositor::layerWillBeRemoved(RenderLayer* parent, RenderLayer* child)
718 {
719     if (!child->isComposited() || parent->renderer()->documentBeingDestroyed())
720         return;
721
722     removeViewportConstrainedLayer(child);
723     repaintInCompositedAncestor(child, child->backing()->compositedBounds());
724
725     setCompositingParent(child, 0);
726     setCompositingLayersNeedRebuild();
727 }
728
729 RenderLayer* RenderLayerCompositor::enclosingNonStackingClippingLayer(const RenderLayer* layer) const
730 {
731     for (RenderLayer* curr = layer->parent(); curr != 0; curr = curr->parent()) {
732         if (curr->isStackingContext())
733             return 0;
734
735         if (curr->renderer()->hasClipOrOverflowClip())
736             return curr;
737     }
738     return 0;
739 }
740
741 void RenderLayerCompositor::addToOverlapMap(OverlapMap& overlapMap, RenderLayer* layer, IntRect& layerBounds, bool& boundsComputed)
742 {
743     if (layer->isRootLayer())
744         return;
745
746     if (!boundsComputed) {
747         // FIXME: If this layer's overlap bounds include its children, we don't need to add its
748         // children's bounds to the overlap map.
749         layerBounds = enclosingIntRect(overlapMap.geometryMap().absoluteRect(layer->overlapBounds()));
750         // Empty rects never intersect, but we need them to for the purposes of overlap testing.
751         if (layerBounds.isEmpty())
752             layerBounds.setSize(IntSize(1, 1));
753         boundsComputed = true;
754     }
755
756     IntRect clipRect = pixelSnappedIntRect(layer->backgroundClipRect(RenderLayer::ClipRectsContext(rootRenderLayer(), 0, AbsoluteClipRects)).rect()); // FIXME: Incorrect for CSS regions.
757     clipRect.scale(pageScaleFactor());
758     clipRect.intersect(layerBounds);
759     overlapMap.add(layer, clipRect);
760 }
761
762 void RenderLayerCompositor::addToOverlapMapRecursive(OverlapMap& overlapMap, RenderLayer* layer, RenderLayer* ancestorLayer)
763 {
764     if (!canBeComposited(layer) || overlapMap.contains(layer))
765         return;
766
767     // A null ancestorLayer is an indication that 'layer' has already been pushed.
768     if (ancestorLayer)
769         overlapMap.geometryMap().pushMappingsToAncestor(layer, ancestorLayer);
770     
771     IntRect bounds;
772     bool haveComputedBounds = false;
773     addToOverlapMap(overlapMap, layer, bounds, haveComputedBounds);
774
775 #if !ASSERT_DISABLED
776     LayerListMutationDetector mutationChecker(layer);
777 #endif
778
779     if (layer->isStackingContext()) {
780         if (Vector<RenderLayer*>* negZOrderList = layer->negZOrderList()) {
781             size_t listSize = negZOrderList->size();
782             for (size_t i = 0; i < listSize; ++i) {
783                 RenderLayer* curLayer = negZOrderList->at(i);
784                 addToOverlapMapRecursive(overlapMap, curLayer, layer);
785             }
786         }
787     }
788
789     if (Vector<RenderLayer*>* normalFlowList = layer->normalFlowList()) {
790         size_t listSize = normalFlowList->size();
791         for (size_t i = 0; i < listSize; ++i) {
792             RenderLayer* curLayer = normalFlowList->at(i);
793             addToOverlapMapRecursive(overlapMap, curLayer, layer);
794         }
795     }
796
797     if (layer->isStackingContext()) {
798         if (Vector<RenderLayer*>* posZOrderList = layer->posZOrderList()) {
799             size_t listSize = posZOrderList->size();
800             for (size_t i = 0; i < listSize; ++i) {
801                 RenderLayer* curLayer = posZOrderList->at(i);
802                 addToOverlapMapRecursive(overlapMap, curLayer, layer);
803             }
804         }
805     }
806     
807     if (ancestorLayer)
808         overlapMap.geometryMap().popMappingsToAncestor(ancestorLayer);
809 }
810
811 //  Recurse through the layers in z-index and overflow order (which is equivalent to painting order)
812 //  For the z-order children of a compositing layer:
813 //      If a child layers has a compositing layer, then all subsequent layers must
814 //      be compositing in order to render above that layer.
815 //
816 //      If a child in the negative z-order list is compositing, then the layer itself
817 //      must be compositing so that its contents render over that child.
818 //      This implies that its positive z-index children must also be compositing.
819 //
820 void RenderLayerCompositor::computeCompositingRequirements(RenderLayer* ancestorLayer, RenderLayer* layer, OverlapMap* overlapMap, CompositingState& compositingState, bool& layersChanged, bool& descendantHas3DTransform)
821 {
822     layer->updateLayerListsIfNeeded();
823     
824     if (overlapMap)
825         overlapMap->geometryMap().pushMappingsToAncestor(layer, ancestorLayer);
826     
827     // Clear the flag
828     layer->setHasCompositingDescendant(false);
829     
830     RenderLayer::IndirectCompositingReason compositingReason = compositingState.m_subtreeIsCompositing ? RenderLayer::IndirectCompositingForStacking : RenderLayer::NoIndirectCompositingReason;
831
832     bool haveComputedBounds = false;
833     IntRect absBounds;
834     if (overlapMap && !overlapMap->isEmpty() && compositingState.m_testingOverlap) {
835         // If we're testing for overlap, we only need to composite if we overlap something that is already composited.
836         absBounds = enclosingIntRect(overlapMap->geometryMap().absoluteRect(layer->overlapBounds()));
837
838         // Empty rects never intersect, but we need them to for the purposes of overlap testing.
839         if (absBounds.isEmpty())
840             absBounds.setSize(IntSize(1, 1));
841         haveComputedBounds = true;
842         compositingReason = overlapMap->overlapsLayers(absBounds) ? RenderLayer::IndirectCompositingForOverlap : RenderLayer::NoIndirectCompositingReason;
843     }
844
845 #if ENABLE(VIDEO)
846     // Video is special. It's the only RenderLayer type that can both have
847     // RenderLayer children and whose children can't use its backing to render
848     // into. These children (the controls) always need to be promoted into their
849     // own layers to draw on top of the accelerated video.
850     if (compositingState.m_compositingAncestor && compositingState.m_compositingAncestor->renderer()->isVideo())
851         compositingReason = RenderLayer::IndirectCompositingForOverlap;
852 #endif
853
854     layer->setIndirectCompositingReason(compositingReason);
855
856     // The children of this layer don't need to composite, unless there is
857     // a compositing layer among them, so start by inheriting the compositing
858     // ancestor with m_subtreeIsCompositing set to false.
859     CompositingState childState(compositingState);
860     childState.m_subtreeIsCompositing = false;
861
862     bool willBeComposited = needsToBeComposited(layer);
863     if (willBeComposited) {
864         // Tell the parent it has compositing descendants.
865         compositingState.m_subtreeIsCompositing = true;
866         // This layer now acts as the ancestor for kids.
867         childState.m_compositingAncestor = layer;
868
869         if (overlapMap)
870             overlapMap->pushCompositingContainer();
871     }
872
873 #if !ASSERT_DISABLED
874     LayerListMutationDetector mutationChecker(layer);
875 #endif
876
877     bool anyDescendantHas3DTransform = false;
878
879     if (layer->isStackingContext()) {
880         if (Vector<RenderLayer*>* negZOrderList = layer->negZOrderList()) {
881             size_t listSize = negZOrderList->size();
882             for (size_t i = 0; i < listSize; ++i) {
883                 RenderLayer* curLayer = negZOrderList->at(i);
884                 computeCompositingRequirements(layer, curLayer, overlapMap, childState, layersChanged, anyDescendantHas3DTransform);
885
886                 // If we have to make a layer for this child, make one now so we can have a contents layer
887                 // (since we need to ensure that the -ve z-order child renders underneath our contents).
888                 if (!willBeComposited && childState.m_subtreeIsCompositing) {
889                     // make layer compositing
890                     layer->setIndirectCompositingReason(RenderLayer::IndirectCompositingForBackgroundLayer);
891                     childState.m_compositingAncestor = layer;
892                     if (overlapMap)
893                         overlapMap->pushCompositingContainer();
894                     willBeComposited = true;
895                 }
896             }
897         }
898     }
899     
900     if (Vector<RenderLayer*>* normalFlowList = layer->normalFlowList()) {
901         size_t listSize = normalFlowList->size();
902         for (size_t i = 0; i < listSize; ++i) {
903             RenderLayer* curLayer = normalFlowList->at(i);
904             computeCompositingRequirements(layer, curLayer, overlapMap, childState, layersChanged, anyDescendantHas3DTransform);
905         }
906     }
907
908     if (layer->isStackingContext()) {
909         if (Vector<RenderLayer*>* posZOrderList = layer->posZOrderList()) {
910             size_t listSize = posZOrderList->size();
911             for (size_t i = 0; i < listSize; ++i) {
912                 RenderLayer* curLayer = posZOrderList->at(i);
913                 computeCompositingRequirements(layer, curLayer, overlapMap, childState, layersChanged, anyDescendantHas3DTransform);
914             }
915         }
916     }
917     
918     // If we just entered compositing mode, the root will have become composited (as long as accelerated compositing is enabled).
919     if (layer->isRootLayer()) {
920         if (inCompositingMode() && m_hasAcceleratedCompositing)
921             willBeComposited = true;
922     }
923     
924     ASSERT(willBeComposited == needsToBeComposited(layer));
925
926     // All layers (even ones that aren't being composited) need to get added to
927     // the overlap map. Layers that do not composite will draw into their
928     // compositing ancestor's backing, and so are still considered for overlap.
929     if (overlapMap && childState.m_compositingAncestor && !childState.m_compositingAncestor->isRootLayer())
930         addToOverlapMap(*overlapMap, layer, absBounds, haveComputedBounds);
931
932     // Now check for reasons to become composited that depend on the state of descendant layers.
933     RenderLayer::IndirectCompositingReason indirectCompositingReason;
934     if (!willBeComposited && canBeComposited(layer)
935         && requiresCompositingForIndirectReason(layer->renderer(), childState.m_subtreeIsCompositing, anyDescendantHas3DTransform, indirectCompositingReason)) {
936         layer->setIndirectCompositingReason(indirectCompositingReason);
937         childState.m_compositingAncestor = layer;
938         if (overlapMap) {
939             overlapMap->pushCompositingContainer();
940             addToOverlapMapRecursive(*overlapMap, layer);
941         }
942         willBeComposited = true;
943     }
944     
945     ASSERT(willBeComposited == needsToBeComposited(layer));
946     if (layer->reflectionLayer()) {
947         // FIXME: Shouldn't we call computeCompositingRequirements to handle a reflection overlapping with another renderer?
948         layer->reflectionLayer()->setIndirectCompositingReason(willBeComposited ? RenderLayer::IndirectCompositingForStacking : RenderLayer::NoIndirectCompositingReason);
949     }
950
951     // Subsequent layers in the parent stacking context also need to composite.
952     if (childState.m_subtreeIsCompositing)
953         compositingState.m_subtreeIsCompositing = true;
954
955     // Turn overlap testing off for later layers if it's already off, or if we have a 3D transform or an animating transform.
956     if (!childState.m_testingOverlap || layer->has3DTransform() || isRunningAcceleratedTransformAnimation(layer->renderer()))
957         compositingState.m_testingOverlap = false;
958
959     // Set the flag to say that this SC has compositing children.
960     layer->setHasCompositingDescendant(childState.m_subtreeIsCompositing);
961
962     // setHasCompositingDescendant() may have changed the answer to needsToBeComposited() when clipping,
963     // so test that again.
964     if (canBeComposited(layer) && clipsCompositingDescendants(layer)) {
965         if (!willBeComposited) {
966             childState.m_compositingAncestor = layer;
967             if (overlapMap) {
968                 overlapMap->pushCompositingContainer();
969                 addToOverlapMapRecursive(*overlapMap, layer);
970             }
971             willBeComposited = true;
972          }
973
974         // We're done processing an element that clips. The container can keep testing overlap.
975         compositingState.m_testingOverlap = true;
976     }
977
978     if (overlapMap && childState.m_compositingAncestor == layer && !layer->isRootLayer())
979         overlapMap->popCompositingContainer();
980
981     // If we're back at the root, and no other layers need to be composited, and the root layer itself doesn't need
982     // to be composited, then we can drop out of compositing mode altogether. However, don't drop out of compositing mode
983     // if there are composited layers that we didn't hit in our traversal (e.g. because of visibility:hidden).
984     if (layer->isRootLayer() && !childState.m_subtreeIsCompositing && !requiresCompositingLayer(layer) && !m_forceCompositingMode && !hasAnyAdditionalCompositedLayers(layer)) {
985         enableCompositingMode(false);
986         willBeComposited = false;
987     }
988     
989     // If the layer is going into compositing mode, repaint its old location.
990     ASSERT(willBeComposited == needsToBeComposited(layer));
991     if (!layer->isComposited() && willBeComposited)
992         repaintOnCompositingChange(layer);
993
994     // Update backing now, so that we can use isComposited() reliably during tree traversal in rebuildCompositingLayerTree().
995     if (updateBacking(layer, CompositingChangeRepaintNow))
996         layersChanged = true;
997
998     if (layer->reflectionLayer() && updateLayerCompositingState(layer->reflectionLayer(), CompositingChangeRepaintNow))
999         layersChanged = true;
1000
1001     descendantHas3DTransform |= anyDescendantHas3DTransform || layer->has3DTransform();
1002
1003     if (overlapMap)
1004         overlapMap->geometryMap().popMappingsToAncestor(ancestorLayer);
1005 }
1006
1007 void RenderLayerCompositor::setCompositingParent(RenderLayer* childLayer, RenderLayer* parentLayer)
1008 {
1009     ASSERT(!parentLayer || childLayer->ancestorCompositingLayer() == parentLayer);
1010     ASSERT(childLayer->isComposited());
1011
1012     // It's possible to be called with a parent that isn't yet composited when we're doing
1013     // partial updates as required by painting or hit testing. Just bail in that case;
1014     // we'll do a full layer update soon.
1015     if (!parentLayer || !parentLayer->isComposited())
1016         return;
1017
1018     if (parentLayer) {
1019         GraphicsLayer* hostingLayer = parentLayer->backing()->parentForSublayers();
1020         GraphicsLayer* hostedLayer = childLayer->backing()->childForSuperlayers();
1021         
1022         hostingLayer->addChild(hostedLayer);
1023     } else
1024         childLayer->backing()->childForSuperlayers()->removeFromParent();
1025 }
1026
1027 void RenderLayerCompositor::removeCompositedChildren(RenderLayer* layer)
1028 {
1029     ASSERT(layer->isComposited());
1030
1031     GraphicsLayer* hostingLayer = layer->backing()->parentForSublayers();
1032     hostingLayer->removeAllChildren();
1033 }
1034
1035 #if ENABLE(VIDEO)
1036 bool RenderLayerCompositor::canAccelerateVideoRendering(RenderVideo* o) const
1037 {
1038     if (!m_hasAcceleratedCompositing)
1039         return false;
1040
1041     return o->supportsAcceleratedRendering();
1042 }
1043 #endif
1044
1045 void RenderLayerCompositor::rebuildCompositingLayerTree(RenderLayer* layer, Vector<GraphicsLayer*>& childLayersOfEnclosingLayer, int depth)
1046 {
1047     // Make the layer compositing if necessary, and set up clipping and content layers.
1048     // Note that we can only do work here that is independent of whether the descendant layers
1049     // have been processed. computeCompositingRequirements() will already have done the repaint if necessary.
1050     
1051     RenderLayerBacking* layerBacking = layer->backing();
1052     if (layerBacking) {
1053         // The compositing state of all our children has been updated already, so now
1054         // we can compute and cache the composited bounds for this layer.
1055         layerBacking->updateCompositedBounds();
1056
1057         if (RenderLayer* reflection = layer->reflectionLayer()) {
1058             if (reflection->backing())
1059                 reflection->backing()->updateCompositedBounds();
1060         }
1061
1062         layerBacking->updateGraphicsLayerConfiguration();
1063         layerBacking->updateGraphicsLayerGeometry();
1064
1065         if (!layer->parent())
1066             updateRootLayerPosition();
1067
1068 #if !LOG_DISABLED
1069         logLayerInfo(layer, depth);
1070 #else
1071         UNUSED_PARAM(depth);
1072 #endif
1073         if (layerBacking->hasUnpositionedOverflowControlsLayers())
1074             layer->positionNewlyCreatedOverflowControls();
1075     }
1076
1077     // If this layer has backing, then we are collecting its children, otherwise appending
1078     // to the compositing child list of an enclosing layer.
1079     Vector<GraphicsLayer*> layerChildren;
1080     Vector<GraphicsLayer*>& childList = layerBacking ? layerChildren : childLayersOfEnclosingLayer;
1081
1082 #if !ASSERT_DISABLED
1083     LayerListMutationDetector mutationChecker(layer);
1084 #endif
1085
1086     if (layer->isStackingContext()) {
1087         if (Vector<RenderLayer*>* negZOrderList = layer->negZOrderList()) {
1088             size_t listSize = negZOrderList->size();
1089             for (size_t i = 0; i < listSize; ++i) {
1090                 RenderLayer* curLayer = negZOrderList->at(i);
1091                 rebuildCompositingLayerTree(curLayer, childList, depth + 1);
1092             }
1093         }
1094
1095         // If a negative z-order child is compositing, we get a foreground layer which needs to get parented.
1096         if (layerBacking && layerBacking->foregroundLayer())
1097             childList.append(layerBacking->foregroundLayer());
1098     }
1099
1100     if (Vector<RenderLayer*>* normalFlowList = layer->normalFlowList()) {
1101         size_t listSize = normalFlowList->size();
1102         for (size_t i = 0; i < listSize; ++i) {
1103             RenderLayer* curLayer = normalFlowList->at(i);
1104             rebuildCompositingLayerTree(curLayer, childList, depth + 1);
1105         }
1106     }
1107     
1108     if (layer->isStackingContext()) {
1109         if (Vector<RenderLayer*>* posZOrderList = layer->posZOrderList()) {
1110             size_t listSize = posZOrderList->size();
1111             for (size_t i = 0; i < listSize; ++i) {
1112                 RenderLayer* curLayer = posZOrderList->at(i);
1113                 rebuildCompositingLayerTree(curLayer, childList, depth + 1);
1114             }
1115         }
1116     }
1117     
1118     if (layerBacking) {
1119         bool parented = false;
1120         if (layer->renderer()->isRenderPart())
1121             parented = parentFrameContentLayers(toRenderPart(layer->renderer()));
1122
1123         if (!parented)
1124             layerBacking->parentForSublayers()->setChildren(layerChildren);
1125
1126         // If the layer has a clipping layer the overflow controls layers will be siblings of the clipping layer.
1127         // Otherwise, the overflow control layers are normal children.
1128         if (!layerBacking->hasClippingLayer() && !layerBacking->hasScrollingLayer()) {
1129             if (GraphicsLayer* overflowControlLayer = layerBacking->layerForHorizontalScrollbar()) {
1130                 overflowControlLayer->removeFromParent();
1131                 layerBacking->parentForSublayers()->addChild(overflowControlLayer);
1132             }
1133
1134             if (GraphicsLayer* overflowControlLayer = layerBacking->layerForVerticalScrollbar()) {
1135                 overflowControlLayer->removeFromParent();
1136                 layerBacking->parentForSublayers()->addChild(overflowControlLayer);
1137             }
1138
1139             if (GraphicsLayer* overflowControlLayer = layerBacking->layerForScrollCorner()) {
1140                 overflowControlLayer->removeFromParent();
1141                 layerBacking->parentForSublayers()->addChild(overflowControlLayer);
1142             }
1143         }
1144
1145         childLayersOfEnclosingLayer.append(layerBacking->childForSuperlayers());
1146     }
1147 }
1148
1149 void RenderLayerCompositor::frameViewDidChangeLocation(const IntPoint& contentsOffset)
1150 {
1151     if (m_overflowControlsHostLayer)
1152         m_overflowControlsHostLayer->setPosition(contentsOffset);
1153 }
1154
1155 void RenderLayerCompositor::frameViewDidChangeSize()
1156 {
1157     if (m_clipLayer) {
1158         FrameView* frameView = m_renderView->frameView();
1159         m_clipLayer->setSize(frameView->visibleContentRect(false /* exclude scrollbars */).size());
1160
1161         frameViewDidScroll();
1162         updateOverflowControlsLayers();
1163
1164 #if ENABLE(RUBBER_BANDING)
1165         if (m_layerForOverhangAreas)
1166             m_layerForOverhangAreas->setSize(frameView->frameRect().size());
1167 #endif
1168     }
1169 }
1170
1171 void RenderLayerCompositor::frameViewDidScroll()
1172 {
1173     FrameView* frameView = m_renderView->frameView();
1174     IntPoint scrollPosition = frameView->scrollPosition();
1175
1176     if (!m_scrollLayer)
1177         return;
1178
1179     // If there's a scrolling coordinator that manages scrolling for this frame view,
1180     // it will also manage updating the scroll layer position.
1181     if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator()) {
1182         if (scrollingCoordinator->coordinatesScrollingForFrameView(frameView))
1183             return;
1184         if (Settings* settings = m_renderView->document()->settings())
1185             if (settings->compositedScrollingForFramesEnabled())
1186                 scrollingCoordinator->scrollableAreaScrollLayerDidChange(frameView, m_scrollLayer.get());
1187     }
1188
1189     m_scrollLayer->setPosition(FloatPoint(-scrollPosition.x(), -scrollPosition.y()));
1190 }
1191
1192 void RenderLayerCompositor::frameViewDidLayout()
1193 {
1194     RenderLayerBacking* renderViewBacking = m_renderView->layer()->backing();
1195     if (renderViewBacking)
1196         renderViewBacking->adjustTileCacheCoverage();
1197 }
1198
1199 void RenderLayerCompositor::scrollingLayerDidChange(RenderLayer* layer)
1200 {
1201     RenderLayerBacking* backing = layer->backing();
1202     if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator())
1203         scrollingCoordinator->scrollableAreaScrollLayerDidChange(layer, backing ? backing->scrollingContentsLayer() : 0);
1204 }
1205
1206 String RenderLayerCompositor::layerTreeAsText(LayerTreeFlags flags)
1207 {
1208     updateCompositingLayers(CompositingUpdateAfterLayout);
1209
1210     if (!m_rootContentLayer)
1211         return String();
1212
1213     flushPendingLayerChanges(true);
1214
1215     LayerTreeAsTextBehavior layerTreeBehavior = LayerTreeAsTextBehaviorNormal;
1216     if (flags & LayerTreeFlagsIncludeDebugInfo)
1217         layerTreeBehavior |= LayerTreeAsTextDebug;
1218     if (flags & LayerTreeFlagsIncludeVisibleRects)
1219         layerTreeBehavior |= LayerTreeAsTextIncludeVisibleRects;
1220     if (flags & LayerTreeFlagsIncludeTileCaches)
1221         layerTreeBehavior |= LayerTreeAsTextIncludeTileCaches;
1222     if (flags & LayerTreeFlagsIncludeRepaintRects)
1223         layerTreeBehavior |= LayerTreeAsTextIncludeRepaintRects;
1224
1225     // We skip dumping the scroll and clip layers to keep layerTreeAsText output
1226     // similar between platforms.
1227     String layerTreeText = m_rootContentLayer->layerTreeAsText(layerTreeBehavior);
1228
1229     // The true root layer is not included in the dump, so if we want to report
1230     // its repaint rects, they must be included here.
1231     if (flags & LayerTreeFlagsIncludeRepaintRects) {
1232         String layerTreeTextWithRootRepaintRects = m_renderView->frameView()->trackedRepaintRectsAsText();
1233         layerTreeTextWithRootRepaintRects.append(layerTreeText);
1234         return layerTreeTextWithRootRepaintRects;
1235     }
1236
1237     return layerTreeText;
1238 }
1239
1240 RenderLayerCompositor* RenderLayerCompositor::frameContentsCompositor(RenderPart* renderer)
1241 {
1242     if (!renderer->node()->isFrameOwnerElement())
1243         return 0;
1244         
1245     HTMLFrameOwnerElement* element = static_cast<HTMLFrameOwnerElement*>(renderer->node());
1246     if (Document* contentDocument = element->contentDocument()) {
1247         if (RenderView* view = contentDocument->renderView())
1248             return view->compositor();
1249     }
1250     return 0;
1251 }
1252
1253 bool RenderLayerCompositor::parentFrameContentLayers(RenderPart* renderer)
1254 {
1255     RenderLayerCompositor* innerCompositor = frameContentsCompositor(renderer);
1256     if (!innerCompositor || !innerCompositor->inCompositingMode() || innerCompositor->rootLayerAttachment() != RootLayerAttachedViaEnclosingFrame)
1257         return false;
1258     
1259     RenderLayer* layer = renderer->layer();
1260     if (!layer->isComposited())
1261         return false;
1262
1263     RenderLayerBacking* backing = layer->backing();
1264     GraphicsLayer* hostingLayer = backing->parentForSublayers();
1265     GraphicsLayer* rootLayer = innerCompositor->rootGraphicsLayer();
1266     if (hostingLayer->children().size() != 1 || hostingLayer->children()[0] != rootLayer) {
1267         hostingLayer->removeAllChildren();
1268         hostingLayer->addChild(rootLayer);
1269     }
1270     return true;
1271 }
1272
1273 // This just updates layer geometry without changing the hierarchy.
1274 void RenderLayerCompositor::updateLayerTreeGeometry(RenderLayer* layer, int depth)
1275 {
1276     if (RenderLayerBacking* layerBacking = layer->backing()) {
1277         // The compositing state of all our children has been updated already, so now
1278         // we can compute and cache the composited bounds for this layer.
1279         layerBacking->updateCompositedBounds();
1280
1281         if (RenderLayer* reflection = layer->reflectionLayer()) {
1282             if (reflection->backing())
1283                 reflection->backing()->updateCompositedBounds();
1284         }
1285
1286         layerBacking->updateGraphicsLayerConfiguration();
1287         layerBacking->updateGraphicsLayerGeometry();
1288
1289         if (!layer->parent())
1290             updateRootLayerPosition();
1291
1292 #if !LOG_DISABLED
1293         logLayerInfo(layer, depth);
1294 #else
1295         UNUSED_PARAM(depth);
1296 #endif
1297     }
1298
1299 #if !ASSERT_DISABLED
1300     LayerListMutationDetector mutationChecker(layer);
1301 #endif
1302
1303     if (layer->isStackingContext()) {
1304         if (Vector<RenderLayer*>* negZOrderList = layer->negZOrderList()) {
1305             size_t listSize = negZOrderList->size();
1306             for (size_t i = 0; i < listSize; ++i)
1307                 updateLayerTreeGeometry(negZOrderList->at(i), depth + 1);
1308         }
1309     }
1310
1311     if (Vector<RenderLayer*>* normalFlowList = layer->normalFlowList()) {
1312         size_t listSize = normalFlowList->size();
1313         for (size_t i = 0; i < listSize; ++i)
1314             updateLayerTreeGeometry(normalFlowList->at(i), depth + 1);
1315     }
1316     
1317     if (layer->isStackingContext()) {
1318         if (Vector<RenderLayer*>* posZOrderList = layer->posZOrderList()) {
1319             size_t listSize = posZOrderList->size();
1320             for (size_t i = 0; i < listSize; ++i)
1321                 updateLayerTreeGeometry(posZOrderList->at(i), depth + 1);
1322         }
1323     }
1324 }
1325
1326 // Recurs down the RenderLayer tree until its finds the compositing descendants of compositingAncestor and updates their geometry.
1327 void RenderLayerCompositor::updateCompositingDescendantGeometry(RenderLayer* compositingAncestor, RenderLayer* layer, bool compositedChildrenOnly)
1328 {
1329     if (layer != compositingAncestor) {
1330         if (RenderLayerBacking* layerBacking = layer->backing()) {
1331             layerBacking->updateCompositedBounds();
1332
1333             if (RenderLayer* reflection = layer->reflectionLayer()) {
1334                 if (reflection->backing())
1335                     reflection->backing()->updateCompositedBounds();
1336             }
1337
1338             layerBacking->updateGraphicsLayerGeometry();
1339             if (compositedChildrenOnly)
1340                 return;
1341         }
1342     }
1343
1344     if (layer->reflectionLayer())
1345         updateCompositingDescendantGeometry(compositingAncestor, layer->reflectionLayer(), compositedChildrenOnly);
1346
1347     if (!layer->hasCompositingDescendant())
1348         return;
1349
1350 #if !ASSERT_DISABLED
1351     LayerListMutationDetector mutationChecker(layer);
1352 #endif
1353     
1354     if (layer->isStackingContext()) {
1355         if (Vector<RenderLayer*>* negZOrderList = layer->negZOrderList()) {
1356             size_t listSize = negZOrderList->size();
1357             for (size_t i = 0; i < listSize; ++i)
1358                 updateCompositingDescendantGeometry(compositingAncestor, negZOrderList->at(i), compositedChildrenOnly);
1359         }
1360     }
1361
1362     if (Vector<RenderLayer*>* normalFlowList = layer->normalFlowList()) {
1363         size_t listSize = normalFlowList->size();
1364         for (size_t i = 0; i < listSize; ++i)
1365             updateCompositingDescendantGeometry(compositingAncestor, normalFlowList->at(i), compositedChildrenOnly);
1366     }
1367     
1368     if (layer->isStackingContext()) {
1369         if (Vector<RenderLayer*>* posZOrderList = layer->posZOrderList()) {
1370             size_t listSize = posZOrderList->size();
1371             for (size_t i = 0; i < listSize; ++i)
1372                 updateCompositingDescendantGeometry(compositingAncestor, posZOrderList->at(i), compositedChildrenOnly);
1373         }
1374     }
1375 }
1376
1377
1378 void RenderLayerCompositor::repaintCompositedLayers(const IntRect* absRect)
1379 {
1380     recursiveRepaintLayer(rootRenderLayer(), absRect);
1381 }
1382
1383 void RenderLayerCompositor::recursiveRepaintLayer(RenderLayer* layer, const IntRect* rect)
1384 {
1385     // FIXME: This method does not work correctly with transforms.
1386     if (layer->isComposited() && !layer->backing()->paintsIntoCompositedAncestor()) {
1387         if (rect)
1388             layer->setBackingNeedsRepaintInRect(*rect);
1389         else
1390             layer->setBackingNeedsRepaint();
1391     }
1392
1393 #if !ASSERT_DISABLED
1394     LayerListMutationDetector mutationChecker(layer);
1395 #endif
1396
1397     if (layer->hasCompositingDescendant()) {
1398         if (Vector<RenderLayer*>* negZOrderList = layer->negZOrderList()) {
1399             size_t listSize = negZOrderList->size();
1400             for (size_t i = 0; i < listSize; ++i) {
1401                 RenderLayer* curLayer = negZOrderList->at(i);
1402                 if (rect) {
1403                     IntRect childRect(*rect);
1404                     curLayer->convertToPixelSnappedLayerCoords(layer, childRect);
1405                     recursiveRepaintLayer(curLayer, &childRect);
1406                 } else
1407                     recursiveRepaintLayer(curLayer);
1408             }
1409         }
1410
1411         if (Vector<RenderLayer*>* posZOrderList = layer->posZOrderList()) {
1412             size_t listSize = posZOrderList->size();
1413             for (size_t i = 0; i < listSize; ++i) {
1414                 RenderLayer* curLayer = posZOrderList->at(i);
1415                 if (rect) {
1416                     IntRect childRect(*rect);
1417                     curLayer->convertToPixelSnappedLayerCoords(layer, childRect);
1418                     recursiveRepaintLayer(curLayer, &childRect);
1419                 } else
1420                     recursiveRepaintLayer(curLayer);
1421             }
1422         }
1423     }
1424     if (Vector<RenderLayer*>* normalFlowList = layer->normalFlowList()) {
1425         size_t listSize = normalFlowList->size();
1426         for (size_t i = 0; i < listSize; ++i) {
1427             RenderLayer* curLayer = normalFlowList->at(i);
1428             if (rect) {
1429                 IntRect childRect(*rect);
1430                 curLayer->convertToPixelSnappedLayerCoords(layer, childRect);
1431                 recursiveRepaintLayer(curLayer, &childRect);
1432             } else
1433                 recursiveRepaintLayer(curLayer);
1434         }
1435     }
1436 }
1437
1438 RenderLayer* RenderLayerCompositor::rootRenderLayer() const
1439 {
1440     return m_renderView->layer();
1441 }
1442
1443 GraphicsLayer* RenderLayerCompositor::rootGraphicsLayer() const
1444 {
1445     if (m_overflowControlsHostLayer)
1446         return m_overflowControlsHostLayer.get();
1447     return m_rootContentLayer.get();
1448 }
1449
1450 GraphicsLayer* RenderLayerCompositor::scrollLayer() const
1451 {
1452     return m_scrollLayer.get();
1453 }
1454
1455 TiledBacking* RenderLayerCompositor::pageTiledBacking() const
1456 {
1457     RenderLayerBacking* renderViewBacking = m_renderView->layer()->backing();
1458     return renderViewBacking ? renderViewBacking->tiledBacking() : 0;
1459 }
1460
1461 void RenderLayerCompositor::didMoveOnscreen()
1462 {
1463     if (TiledBacking* tiledBacking = pageTiledBacking())
1464         tiledBacking->setIsInWindow(true);
1465
1466     if (!inCompositingMode() || m_rootLayerAttachment != RootLayerUnattached)
1467         return;
1468
1469     RootLayerAttachment attachment = shouldPropagateCompositingToEnclosingFrame() ? RootLayerAttachedViaEnclosingFrame : RootLayerAttachedViaChromeClient;
1470     attachRootLayer(attachment);
1471 }
1472
1473 void RenderLayerCompositor::willMoveOffscreen()
1474 {
1475     if (TiledBacking* tiledBacking = pageTiledBacking())
1476         tiledBacking->setIsInWindow(false);
1477
1478     if (!inCompositingMode() || m_rootLayerAttachment == RootLayerUnattached)
1479         return;
1480
1481     detachRootLayer();
1482 }
1483
1484 void RenderLayerCompositor::clearBackingForLayerIncludingDescendants(RenderLayer* layer)
1485 {
1486     if (!layer)
1487         return;
1488
1489     if (layer->isComposited()) {
1490         removeViewportConstrainedLayer(layer);
1491         layer->clearBacking();
1492     }
1493     
1494     for (RenderLayer* currLayer = layer->firstChild(); currLayer; currLayer = currLayer->nextSibling())
1495         clearBackingForLayerIncludingDescendants(currLayer);
1496 }
1497
1498 void RenderLayerCompositor::clearBackingForAllLayers()
1499 {
1500     clearBackingForLayerIncludingDescendants(m_renderView->layer());
1501 }
1502
1503 void RenderLayerCompositor::updateRootLayerPosition()
1504 {
1505     if (m_rootContentLayer) {
1506         const IntRect& documentRect = m_renderView->documentRect();
1507         m_rootContentLayer->setSize(documentRect.size());
1508         m_rootContentLayer->setPosition(documentRect.location());
1509     }
1510     if (m_clipLayer) {
1511         FrameView* frameView = m_renderView->frameView();
1512         m_clipLayer->setSize(frameView->visibleContentRect(false /* exclude scrollbars */).size());
1513     }
1514
1515 #if ENABLE(RUBBER_BANDING)
1516     if (m_contentShadowLayer) {
1517         m_contentShadowLayer->setPosition(m_rootContentLayer->position());
1518
1519         FloatSize rootContentLayerSize = m_rootContentLayer->size();
1520         if (m_contentShadowLayer->size() != rootContentLayerSize) {
1521             m_contentShadowLayer->setSize(rootContentLayerSize);
1522             ScrollbarTheme::theme()->setUpContentShadowLayer(m_contentShadowLayer.get());
1523         }
1524     }
1525 #endif
1526 }
1527
1528 bool RenderLayerCompositor::has3DContent() const
1529 {
1530     return layerHas3DContent(rootRenderLayer());
1531 }
1532
1533 bool RenderLayerCompositor::allowsIndependentlyCompositedFrames(const FrameView* view)
1534 {
1535 #if PLATFORM(MAC)
1536     // frames are only independently composited in Mac pre-WebKit2.
1537     return view->platformWidget();
1538 #else
1539     UNUSED_PARAM(view);
1540 #endif
1541     return false;
1542 }
1543
1544 bool RenderLayerCompositor::shouldPropagateCompositingToEnclosingFrame() const
1545 {
1546     // Parent document content needs to be able to render on top of a composited frame, so correct behavior
1547     // is to have the parent document become composited too. However, this can cause problems on platforms that
1548     // use native views for frames (like Mac), so disable that behavior on those platforms for now.
1549     HTMLFrameOwnerElement* ownerElement = m_renderView->document()->ownerElement();
1550     RenderObject* renderer = ownerElement ? ownerElement->renderer() : 0;
1551
1552     // If we are the top-level frame, don't propagate.
1553     if (!ownerElement)
1554         return false;
1555
1556     if (!allowsIndependentlyCompositedFrames(m_renderView->frameView()))
1557         return true;
1558
1559     if (!renderer || !renderer->isRenderPart())
1560         return false;
1561
1562     // On Mac, only propagate compositing if the frame is overlapped in the parent
1563     // document, or the parent is already compositing, or the main frame is scaled.
1564     Page* page = this->page();
1565     if (page && page->pageScaleFactor() != 1)
1566         return true;
1567     
1568     RenderPart* frameRenderer = toRenderPart(renderer);
1569     if (frameRenderer->widget()) {
1570         ASSERT(frameRenderer->widget()->isFrameView());
1571         FrameView* view = static_cast<FrameView*>(frameRenderer->widget());
1572         if (view->isOverlappedIncludingAncestors() || view->hasCompositingAncestor())
1573             return true;
1574     }
1575
1576     return false;
1577 }
1578
1579 bool RenderLayerCompositor::needsToBeComposited(const RenderLayer* layer, RenderLayer::ViewportConstrainedNotCompositedReason* viewportConstrainedNotCompositedReason) const
1580 {
1581     if (!canBeComposited(layer))
1582         return false;
1583
1584     return requiresCompositingLayer(layer, viewportConstrainedNotCompositedReason) || layer->mustCompositeForIndirectReasons() || (inCompositingMode() && layer->isRootLayer());
1585 }
1586
1587 // Note: this specifies whether the RL needs a compositing layer for intrinsic reasons.
1588 // Use needsToBeComposited() to determine if a RL actually needs a compositing layer.
1589 // static
1590 bool RenderLayerCompositor::requiresCompositingLayer(const RenderLayer* layer, RenderLayer::ViewportConstrainedNotCompositedReason* viewportConstrainedNotCompositedReason) const
1591 {
1592     RenderObject* renderer = layer->renderer();
1593     // The compositing state of a reflection should match that of its reflected layer.
1594     if (layer->isReflection()) {
1595         renderer = renderer->parent(); // The RenderReplica's parent is the object being reflected.
1596         layer = toRenderLayerModelObject(renderer)->layer();
1597     }
1598     // The root layer always has a compositing layer, but it may not have backing.
1599     return requiresCompositingForTransform(renderer)
1600         || requiresCompositingForVideo(renderer)
1601         || requiresCompositingForCanvas(renderer)
1602         || requiresCompositingForPlugin(renderer)
1603         || requiresCompositingForFrame(renderer)
1604         || (canRender3DTransforms() && renderer->style()->backfaceVisibility() == BackfaceVisibilityHidden)
1605         || clipsCompositingDescendants(layer)
1606         || requiresCompositingForAnimation(renderer)
1607         || requiresCompositingForFilters(renderer)
1608         || requiresCompositingForPosition(renderer, layer, viewportConstrainedNotCompositedReason)
1609         || requiresCompositingForOverflowScrolling(layer)
1610         || requiresCompositingForBlending(renderer);
1611 }
1612
1613 bool RenderLayerCompositor::canBeComposited(const RenderLayer* layer) const
1614 {
1615     // FIXME: We disable accelerated compositing for elements in a RenderFlowThread as it doesn't work properly.
1616     // See http://webkit.org/b/84900 to re-enable it.
1617     return m_hasAcceleratedCompositing && layer->isSelfPaintingLayer() && !layer->renderer()->inRenderFlowThread();
1618 }
1619
1620 bool RenderLayerCompositor::requiresOwnBackingStore(const RenderLayer* layer, const RenderLayer* compositingAncestorLayer) const
1621 {
1622     RenderObject* renderer = layer->renderer();
1623     if (compositingAncestorLayer
1624         && !(compositingAncestorLayer->backing()->graphicsLayer()->drawsContent()
1625             || compositingAncestorLayer->backing()->paintsIntoWindow()
1626             || compositingAncestorLayer->backing()->paintsIntoCompositedAncestor()))
1627         return true;
1628
1629     if (layer->isRootLayer()
1630         || layer->transform() // note: excludes perspective and transformStyle3D.
1631         || requiresCompositingForVideo(renderer)
1632         || requiresCompositingForCanvas(renderer)
1633         || requiresCompositingForPlugin(renderer)
1634         || requiresCompositingForFrame(renderer)
1635         || (canRender3DTransforms() && renderer->style()->backfaceVisibility() == BackfaceVisibilityHidden)
1636         || requiresCompositingForAnimation(renderer)
1637         || requiresCompositingForFilters(renderer)
1638         || requiresCompositingForBlending(renderer)
1639         || requiresCompositingForPosition(renderer, layer)
1640         || requiresCompositingForOverflowScrolling(layer)
1641         || renderer->isTransparent()
1642         || renderer->hasMask()
1643         || renderer->hasReflection()
1644         || renderer->hasFilter())
1645         return true;
1646         
1647     
1648     if (layer->mustCompositeForIndirectReasons()) {
1649         RenderLayer::IndirectCompositingReason reason = layer->indirectCompositingReason();
1650         return reason == RenderLayer::IndirectCompositingForOverlap
1651             || reason == RenderLayer::IndirectCompositingForStacking
1652             || reason == RenderLayer::IndirectCompositingForBackgroundLayer
1653             || reason == RenderLayer::IndirectCompositingForGraphicalEffect
1654             || reason == RenderLayer::IndirectCompositingForPreserve3D; // preserve-3d has to create backing store to ensure that 3d-transformed elements intersect.
1655     }
1656     return false;
1657 }
1658
1659 #if !LOG_DISABLED
1660 const char* RenderLayerCompositor::reasonForCompositing(const RenderLayer* layer)
1661 {
1662     RenderObject* renderer = layer->renderer();
1663     if (layer->isReflection()) {
1664         renderer = renderer->parent();
1665         layer = toRenderLayerModelObject(renderer)->layer();
1666     }
1667
1668     if (requiresCompositingForTransform(renderer))
1669         return "3D transform";
1670
1671     if (requiresCompositingForVideo(renderer))
1672         return "video";
1673
1674     if (requiresCompositingForCanvas(renderer))
1675         return "canvas";
1676
1677     if (requiresCompositingForPlugin(renderer))
1678         return "plugin";
1679
1680     if (requiresCompositingForFrame(renderer))
1681         return "iframe";
1682     
1683     if ((canRender3DTransforms() && renderer->style()->backfaceVisibility() == BackfaceVisibilityHidden))
1684         return "backface-visibility: hidden";
1685
1686     if (clipsCompositingDescendants(layer))
1687         return "clips compositing descendants";
1688
1689     if (requiresCompositingForAnimation(renderer))
1690         return "animation";
1691
1692     if (requiresCompositingForFilters(renderer))
1693         return "filters";
1694
1695     if (requiresCompositingForPosition(renderer, layer))
1696         return renderer->style()->position() == FixedPosition ? "position: fixed" : "position: sticky";
1697
1698     if (requiresCompositingForOverflowScrolling(layer))
1699         return "-webkit-overflow-scrolling: touch";
1700
1701     if (layer->indirectCompositingReason() == RenderLayer::IndirectCompositingForStacking)
1702         return "stacking";
1703
1704     if (layer->indirectCompositingReason() == RenderLayer::IndirectCompositingForOverlap)
1705         return "overlap";
1706
1707     if (layer->indirectCompositingReason() == RenderLayer::IndirectCompositingForBackgroundLayer)
1708         return "negative z-index children";
1709
1710     if (layer->indirectCompositingReason() == RenderLayer::IndirectCompositingForGraphicalEffect) {
1711         if (layer->transform())
1712             return "transform with composited descendants";
1713
1714         if (renderer->isTransparent())
1715             return "opacity with composited descendants";
1716
1717         if (renderer->hasMask())
1718             return "mask with composited descendants";
1719
1720         if (renderer->hasReflection())
1721             return "reflection with composited descendants";
1722
1723         if (renderer->hasFilter())
1724             return "filter with composited descendants";
1725             
1726         if (renderer->hasBlendMode())
1727             return "blending with composited descendants";
1728     }
1729
1730     if (layer->indirectCompositingReason() == RenderLayer::IndirectCompositingForPerspective)
1731         return "perspective";
1732
1733     if (layer->indirectCompositingReason() == RenderLayer::IndirectCompositingForPreserve3D)
1734         return "preserve-3d";
1735
1736     if (inCompositingMode() && layer->isRootLayer())
1737         return "root";
1738
1739     return "";
1740 }
1741 #endif
1742
1743 // Return true if the given layer has some ancestor in the RenderLayer hierarchy that clips,
1744 // up to the enclosing compositing ancestor. This is required because compositing layers are parented
1745 // according to the z-order hierarchy, yet clipping goes down the renderer hierarchy.
1746 // Thus, a RenderLayer can be clipped by a RenderLayer that is an ancestor in the renderer hierarchy,
1747 // but a sibling in the z-order hierarchy.
1748 bool RenderLayerCompositor::clippedByAncestor(RenderLayer* layer) const
1749 {
1750     if (!layer->isComposited() || !layer->parent())
1751         return false;
1752
1753     RenderLayer* compositingAncestor = layer->ancestorCompositingLayer();
1754     if (!compositingAncestor)
1755         return false;
1756
1757     // If the compositingAncestor clips, that will be taken care of by clipsCompositingDescendants(),
1758     // so we only care about clipping between its first child that is our ancestor (the computeClipRoot),
1759     // and layer.
1760     RenderLayer* computeClipRoot = 0;
1761     RenderLayer* curr = layer;
1762     while (curr) {
1763         RenderLayer* next = curr->parent();
1764         if (next == compositingAncestor) {
1765             computeClipRoot = curr;
1766             break;
1767         }
1768         curr = next;
1769     }
1770     
1771     if (!computeClipRoot || computeClipRoot == layer)
1772         return false;
1773
1774     return layer->backgroundClipRect(RenderLayer::ClipRectsContext(computeClipRoot, 0, TemporaryClipRects)).rect() != PaintInfo::infiniteRect(); // FIXME: Incorrect for CSS regions.
1775 }
1776
1777 // Return true if the given layer is a stacking context and has compositing child
1778 // layers that it needs to clip. In this case we insert a clipping GraphicsLayer
1779 // into the hierarchy between this layer and its children in the z-order hierarchy.
1780 bool RenderLayerCompositor::clipsCompositingDescendants(const RenderLayer* layer) const
1781 {
1782     return layer->hasCompositingDescendant() && layer->renderer()->hasClipOrOverflowClip();
1783 }
1784
1785 // Return true if there is an ancestor layer that is fixed positioned to the view.
1786 // Note that if the ancestor has a stacking context and is fixed position then this method
1787 // will return false.
1788 bool RenderLayerCompositor::fixedPositionedByAncestor(const RenderLayer* layer) const
1789 {
1790     if (!layer->isComposited() || !layer->parent())
1791         return false;
1792
1793     const RenderLayer* compositingAncestor = layer->ancestorCompositingLayer();
1794     if (!compositingAncestor)
1795         return false;
1796
1797     const RenderLayer* curr = layer;
1798     while (curr) {
1799         const RenderLayer* next = curr->parent();
1800         if (next == compositingAncestor)
1801             return false;
1802
1803         if (next && next->renderer()->style()->position() == FixedPosition)
1804             return true;
1805         curr = next;
1806     }
1807     return false;
1808 }
1809
1810 bool RenderLayerCompositor::requiresCompositingForScrollableFrame() const
1811 {
1812     // Need this done first to determine overflow.
1813     ASSERT(!m_renderView->needsLayout());
1814     HTMLFrameOwnerElement* ownerElement = m_renderView->document()->ownerElement();
1815     if (!ownerElement)
1816         return false;
1817
1818     if (!(m_compositingTriggers & ChromeClient::ScrollableInnerFrameTrigger))
1819         return false;
1820
1821     FrameView* frameView = m_renderView->frameView();
1822     return frameView->isScrollable();
1823 }
1824
1825 bool RenderLayerCompositor::requiresCompositingForTransform(RenderObject* renderer) const
1826 {
1827     if (!(m_compositingTriggers & ChromeClient::ThreeDTransformTrigger))
1828         return false;
1829
1830     RenderStyle* style = renderer->style();
1831     // Note that we ask the renderer if it has a transform, because the style may have transforms,
1832     // but the renderer may be an inline that doesn't suppport them.
1833     return renderer->hasTransform() && style->transform().has3DOperation();
1834 }
1835
1836 bool RenderLayerCompositor::requiresCompositingForVideo(RenderObject* renderer) const
1837 {
1838     if (!(m_compositingTriggers & ChromeClient::VideoTrigger))
1839         return false;
1840 #if ENABLE(VIDEO)
1841     if (renderer->isVideo()) {
1842         RenderVideo* video = toRenderVideo(renderer);
1843         return video->shouldDisplayVideo() && canAccelerateVideoRendering(video);
1844     }
1845 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
1846     else if (renderer->isRenderPart()) {
1847         if (!m_hasAcceleratedCompositing)
1848             return false;
1849
1850         Node* node = renderer->node();
1851         if (!node || (!node->hasTagName(HTMLNames::videoTag) && !node->hasTagName(HTMLNames::audioTag)))
1852             return false;
1853
1854         HTMLMediaElement* mediaElement = static_cast<HTMLMediaElement*>(node);
1855         return mediaElement->player() ? mediaElement->player()->supportsAcceleratedRendering() : false;
1856     }
1857 #endif // ENABLE(PLUGIN_PROXY_FOR_VIDEO)
1858 #else
1859     UNUSED_PARAM(renderer);
1860 #endif
1861     return false;
1862 }
1863
1864 bool RenderLayerCompositor::requiresCompositingForCanvas(RenderObject* renderer) const
1865 {
1866     if (!(m_compositingTriggers & ChromeClient::CanvasTrigger))
1867         return false;
1868
1869     if (renderer->isCanvas()) {
1870         HTMLCanvasElement* canvas = static_cast<HTMLCanvasElement*>(renderer->node());
1871         return canvas->renderingContext() && canvas->renderingContext()->isAccelerated();
1872     }
1873     return false;
1874 }
1875
1876 bool RenderLayerCompositor::requiresCompositingForPlugin(RenderObject* renderer) const
1877 {
1878     if (!(m_compositingTriggers & ChromeClient::PluginTrigger))
1879         return false;
1880
1881     bool composite = renderer->isEmbeddedObject() && toRenderEmbeddedObject(renderer)->allowsAcceleratedCompositing();
1882     if (!composite)
1883         return false;
1884
1885     m_reevaluateCompositingAfterLayout = true;
1886     
1887     RenderWidget* pluginRenderer = toRenderWidget(renderer);
1888     // If we can't reliably know the size of the plugin yet, don't change compositing state.
1889     if (pluginRenderer->needsLayout())
1890         return pluginRenderer->hasLayer() && pluginRenderer->layer()->isComposited();
1891
1892     // Don't go into compositing mode if height or width are zero, or size is 1x1.
1893     IntRect contentBox = pixelSnappedIntRect(pluginRenderer->contentBoxRect());
1894     return contentBox.height() * contentBox.width() > 1;
1895 }
1896
1897 bool RenderLayerCompositor::requiresCompositingForFrame(RenderObject* renderer) const
1898 {
1899     if (!renderer->isRenderPart())
1900         return false;
1901     
1902     RenderPart* frameRenderer = toRenderPart(renderer);
1903
1904     if (!frameRenderer->requiresAcceleratedCompositing())
1905         return false;
1906
1907     m_reevaluateCompositingAfterLayout = true;
1908
1909     RenderLayerCompositor* innerCompositor = frameContentsCompositor(frameRenderer);
1910     if (!innerCompositor || !innerCompositor->shouldPropagateCompositingToEnclosingFrame())
1911         return false;
1912
1913     // If we can't reliably know the size of the iframe yet, don't change compositing state.
1914     if (renderer->needsLayout())
1915         return frameRenderer->hasLayer() && frameRenderer->layer()->isComposited();
1916     
1917     // Don't go into compositing mode if height or width are zero.
1918     IntRect contentBox = pixelSnappedIntRect(frameRenderer->contentBoxRect());
1919     return contentBox.height() * contentBox.width() > 0;
1920 }
1921
1922 bool RenderLayerCompositor::requiresCompositingForAnimation(RenderObject* renderer) const
1923 {
1924     if (!(m_compositingTriggers & ChromeClient::AnimationTrigger))
1925         return false;
1926
1927     if (AnimationController* animController = renderer->animation()) {
1928         return (animController->isRunningAnimationOnRenderer(renderer, CSSPropertyOpacity) && inCompositingMode())
1929 #if ENABLE(CSS_FILTERS)
1930 #if !PLATFORM(MAC) || (!PLATFORM(IOS) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080)
1931             // <rdar://problem/10907251> - WebKit2 doesn't support CA animations of CI filters on Lion and below
1932             || animController->isRunningAnimationOnRenderer(renderer, CSSPropertyWebkitFilter)
1933 #endif // !PLATFORM(MAC) || (!PLATFORM(IOS) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1080)
1934 #endif // CSS_FILTERS
1935             || animController->isRunningAnimationOnRenderer(renderer, CSSPropertyWebkitTransform);
1936     }
1937     return false;
1938 }
1939
1940 bool RenderLayerCompositor::requiresCompositingForIndirectReason(RenderObject* renderer, bool hasCompositedDescendants, bool has3DTransformedDescendants, RenderLayer::IndirectCompositingReason& reason) const
1941 {
1942     RenderLayer* layer = toRenderBoxModelObject(renderer)->layer();
1943
1944     // When a layer has composited descendants, some effects, like 2d transforms, filters, masks etc must be implemented
1945     // via compositing so that they also apply to those composited descdendants.
1946     if (hasCompositedDescendants && (layer->transform() || renderer->createsGroup() || renderer->hasReflection())) {
1947         reason = RenderLayer::IndirectCompositingForGraphicalEffect;
1948         return true;
1949     }
1950
1951     // A layer with preserve-3d or perspective only needs to be composited if there are descendant layers that
1952     // will be affected by the preserve-3d or perspective.
1953     if (has3DTransformedDescendants) {
1954         if (renderer->style()->transformStyle3D() == TransformStyle3DPreserve3D) {
1955             reason = RenderLayer::IndirectCompositingForPreserve3D;
1956             return true;
1957         }
1958     
1959         if (renderer->style()->hasPerspective()) {
1960             reason = RenderLayer::IndirectCompositingForPerspective;
1961             return true;
1962         }
1963     }
1964
1965     reason = RenderLayer::NoIndirectCompositingReason;
1966     return false;
1967 }
1968
1969 bool RenderLayerCompositor::requiresCompositingForFilters(RenderObject* renderer) const
1970 {
1971 #if ENABLE(CSS_FILTERS)
1972     if (!(m_compositingTriggers & ChromeClient::FilterTrigger))
1973         return false;
1974
1975     return renderer->hasFilter();
1976 #else
1977     UNUSED_PARAM(renderer);
1978     return false;
1979 #endif
1980 }
1981
1982 bool RenderLayerCompositor::requiresCompositingForBlending(RenderObject* renderer) const
1983 {
1984 #if ENABLE(CSS_COMPOSITING)
1985     return renderer->hasBlendMode();
1986 #else
1987     UNUSED_PARAM(renderer);
1988     return false;
1989 #endif
1990 }
1991
1992 bool RenderLayerCompositor::requiresCompositingForPosition(RenderObject* renderer, const RenderLayer* layer, RenderLayer::ViewportConstrainedNotCompositedReason* viewportConstrainedNotCompositedReason) const
1993 {
1994     // position:fixed elements that create their own stacking context (e.g. have an explicit z-index,
1995     // opacity, transform) can get their own composited layer. A stacking context is required otherwise
1996     // z-index and clipping will be broken.
1997     if (!renderer->isPositioned())
1998         return false;
1999     
2000     EPosition position = renderer->style()->position();
2001     bool isFixed = renderer->isOutOfFlowPositioned() && position == FixedPosition;
2002     if (isFixed && !layer->isStackingContext())
2003         return false;
2004     
2005     bool isSticky = renderer->isInFlowPositioned() && position == StickyPosition;
2006     if (!isFixed && !isSticky)
2007         return false;
2008
2009     // FIXME: acceleratedCompositingForFixedPositionEnabled should probably be renamed acceleratedCompositingForViewportConstrainedPositionEnabled().
2010     if (Settings* settings = m_renderView->document()->settings())
2011         if (!settings->acceleratedCompositingForFixedPositionEnabled())
2012             return false;
2013
2014     if (isSticky)
2015         return true;
2016
2017     RenderObject* container = renderer->container();
2018     // If the renderer is not hooked up yet then we have to wait until it is.
2019     if (!container) {
2020         m_reevaluateCompositingAfterLayout = true;
2021         return false;
2022     }
2023
2024     // Don't promote fixed position elements that are descendants of a non-view container, e.g. transformed elements.
2025     // They will stay fixed wrt the container rather than the enclosing frame.
2026     if (container != m_renderView) {
2027         if (viewportConstrainedNotCompositedReason)
2028             *viewportConstrainedNotCompositedReason = RenderLayer::NotCompositedForNonViewContainer;
2029         return false;
2030     }
2031
2032     // Fixed position elements that are invisible in the current view don't get their own layer.
2033     if (FrameView* frameView = m_renderView->frameView()) {
2034         IntRect viewBounds = frameView->visibleContentRect();
2035         LayoutRect layerBounds = layer->calculateLayerBounds(rootRenderLayer(), 0, RenderLayer::DefaultCalculateLayerBoundsFlags
2036             | RenderLayer::ExcludeHiddenDescendants | RenderLayer::DontConstrainForMask | RenderLayer::IncludeCompositedDescendants);
2037         layerBounds.scale(frameView->frame()->frameScaleFactor());
2038         if (!viewBounds.intersects(enclosingIntRect(layerBounds))) {
2039             if (viewportConstrainedNotCompositedReason)
2040                 *viewportConstrainedNotCompositedReason = RenderLayer::NotCompositedForBoundsOutOfView;
2041             return false;
2042         }
2043     }
2044
2045     return true;
2046 }
2047
2048 bool RenderLayerCompositor::requiresCompositingForOverflowScrolling(const RenderLayer* layer) const
2049 {
2050     return layer->needsCompositedScrolling();
2051 }
2052
2053 bool RenderLayerCompositor::isRunningAcceleratedTransformAnimation(RenderObject* renderer) const
2054 {
2055     if (!(m_compositingTriggers & ChromeClient::AnimationTrigger))
2056         return false;
2057
2058     if (AnimationController* animController = renderer->animation())
2059         return animController->isRunningAnimationOnRenderer(renderer, CSSPropertyWebkitTransform);
2060
2061     return false;
2062 }
2063
2064 // If an element has negative z-index children, those children render in front of the 
2065 // layer background, so we need an extra 'contents' layer for the foreground of the layer
2066 // object.
2067 bool RenderLayerCompositor::needsContentsCompositingLayer(const RenderLayer* layer) const
2068 {
2069     return layer->hasNegativeZOrderList();
2070 }
2071
2072 bool RenderLayerCompositor::requiresScrollLayer(RootLayerAttachment attachment) const
2073 {
2074     // This applies when the application UI handles scrolling, in which case RenderLayerCompositor doesn't need to manage it.
2075     if (m_renderView->frameView()->delegatesScrolling())
2076         return false;
2077
2078     // We need to handle our own scrolling if we're:
2079     return !m_renderView->frameView()->platformWidget() // viewless (i.e. non-Mac, or Mac in WebKit2)
2080         || attachment == RootLayerAttachedViaEnclosingFrame; // a composited frame on Mac
2081 }
2082
2083 static void paintScrollbar(Scrollbar* scrollbar, GraphicsContext& context, const IntRect& clip)
2084 {
2085     if (!scrollbar)
2086         return;
2087
2088     context.save();
2089     const IntRect& scrollbarRect = scrollbar->frameRect();
2090     context.translate(-scrollbarRect.x(), -scrollbarRect.y());
2091     IntRect transformedClip = clip;
2092     transformedClip.moveBy(scrollbarRect.location());
2093     scrollbar->paint(&context, transformedClip);
2094     context.restore();
2095 }
2096
2097 void RenderLayerCompositor::paintContents(const GraphicsLayer* graphicsLayer, GraphicsContext& context, GraphicsLayerPaintingPhase, const IntRect& clip)
2098 {
2099     if (graphicsLayer == layerForHorizontalScrollbar())
2100         paintScrollbar(m_renderView->frameView()->horizontalScrollbar(), context, clip);
2101     else if (graphicsLayer == layerForVerticalScrollbar())
2102         paintScrollbar(m_renderView->frameView()->verticalScrollbar(), context, clip);
2103     else if (graphicsLayer == layerForScrollCorner()) {
2104         const IntRect& scrollCorner = m_renderView->frameView()->scrollCornerRect();
2105         context.save();
2106         context.translate(-scrollCorner.x(), -scrollCorner.y());
2107         IntRect transformedClip = clip;
2108         transformedClip.moveBy(scrollCorner.location());
2109         m_renderView->frameView()->paintScrollCorner(&context, transformedClip);
2110         context.restore();
2111 #if PLATFORM(CHROMIUM) && ENABLE(RUBBER_BANDING)
2112     } else if (graphicsLayer == layerForOverhangAreas()) {
2113         ScrollView* view = m_renderView->frameView();
2114         view->calculateAndPaintOverhangAreas(&context, clip);
2115 #endif
2116     }
2117 }
2118
2119 static void resetTrackedRepaintRectsRecursive(GraphicsLayer* graphicsLayer)
2120 {
2121     if (!graphicsLayer)
2122         return;
2123
2124     graphicsLayer->resetTrackedRepaints();
2125
2126     for (size_t i = 0; i < graphicsLayer->children().size(); ++i)
2127         resetTrackedRepaintRectsRecursive(graphicsLayer->children()[i]);
2128
2129     if (GraphicsLayer* replicaLayer = graphicsLayer->replicaLayer())
2130         resetTrackedRepaintRectsRecursive(replicaLayer);
2131
2132     if (GraphicsLayer* maskLayer = graphicsLayer->maskLayer())
2133         resetTrackedRepaintRectsRecursive(maskLayer);
2134 }
2135
2136 void RenderLayerCompositor::resetTrackedRepaintRects()
2137 {
2138     if (GraphicsLayer* rootLayer = rootGraphicsLayer())
2139         resetTrackedRepaintRectsRecursive(rootLayer);
2140 }
2141
2142 void RenderLayerCompositor::setTracksRepaints(bool tracksRepaints)
2143 {
2144     m_isTrackingRepaints = tracksRepaints;
2145 }
2146
2147 bool RenderLayerCompositor::isTrackingRepaints() const
2148 {
2149     return m_isTrackingRepaints;
2150 }
2151
2152 float RenderLayerCompositor::deviceScaleFactor() const
2153 {
2154     Page* page = this->page();
2155     return page ? page->deviceScaleFactor() : 1;
2156 }
2157
2158 float RenderLayerCompositor::pageScaleFactor() const
2159 {
2160     Page* page = this->page();
2161     return page ? page->pageScaleFactor() : 1;
2162 }
2163
2164 void RenderLayerCompositor::didCommitChangesForLayer(const GraphicsLayer*) const
2165 {
2166     // Nothing to do here yet.
2167 }
2168
2169 bool RenderLayerCompositor::keepLayersPixelAligned() const
2170 {
2171     // When scaling, attempt to align compositing layers to pixel boundaries.
2172     return true;
2173 }
2174
2175 static bool shouldCompositeOverflowControls(FrameView* view)
2176 {
2177     if (view->platformWidget())
2178         return false;
2179
2180     if (Page* page = view->frame()->page()) {
2181         if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator())
2182             if (scrollingCoordinator->coordinatesScrollingForFrameView(view))
2183                 return true;
2184     }
2185
2186 #if !PLATFORM(CHROMIUM)
2187     if (!view->hasOverlayScrollbars())
2188         return false;
2189 #endif
2190     return true;
2191 }
2192
2193 bool RenderLayerCompositor::requiresHorizontalScrollbarLayer() const
2194 {
2195     FrameView* view = m_renderView->frameView();
2196     return shouldCompositeOverflowControls(view) && view->horizontalScrollbar();
2197 }
2198
2199 bool RenderLayerCompositor::requiresVerticalScrollbarLayer() const
2200 {
2201     FrameView* view = m_renderView->frameView();
2202     return shouldCompositeOverflowControls(view) && view->verticalScrollbar();
2203 }
2204
2205 bool RenderLayerCompositor::requiresScrollCornerLayer() const
2206 {
2207     FrameView* view = m_renderView->frameView();
2208     return shouldCompositeOverflowControls(view) && view->isScrollCornerVisible();
2209 }
2210
2211 #if ENABLE(RUBBER_BANDING)
2212 bool RenderLayerCompositor::requiresOverhangAreasLayer() const
2213 {
2214     // We don't want a layer if this is a subframe.
2215     if (m_renderView->document()->ownerElement())
2216         return false;
2217
2218     // We do want a layer if we have a scrolling coordinator.
2219     if (scrollingCoordinator())
2220         return true;
2221
2222     // Chromium always wants a layer.
2223 #if PLATFORM(CHROMIUM)
2224     return true;
2225 #endif
2226
2227     return false;
2228 }
2229
2230 bool RenderLayerCompositor::requiresContentShadowLayer() const
2231 {
2232     // We don't want a layer if this is a subframe.
2233     if (m_renderView->document()->ownerElement())
2234         return false;
2235
2236 #if PLATFORM(MAC)
2237     // On Mac, we want a content shadow layer if we have a scrolling coordinator.
2238     if (scrollingCoordinator())
2239         return true;
2240 #endif
2241
2242     return false;
2243 }
2244 #endif
2245
2246 void RenderLayerCompositor::updateOverflowControlsLayers()
2247 {
2248 #if ENABLE(RUBBER_BANDING)
2249     if (requiresOverhangAreasLayer()) {
2250         if (!m_layerForOverhangAreas) {
2251             m_layerForOverhangAreas = GraphicsLayer::create(graphicsLayerFactory(), this);
2252 #ifndef NDEBUG
2253             m_layerForOverhangAreas->setName("overhang areas");
2254 #endif
2255             m_layerForOverhangAreas->setDrawsContent(false);
2256             m_layerForOverhangAreas->setSize(m_renderView->frameView()->frameRect().size());
2257
2258             ScrollbarTheme::theme()->setUpOverhangAreasLayerContents(m_layerForOverhangAreas.get());
2259
2260             // We want the overhang areas layer to be positioned below the frame contents,
2261             // so insert it below the clip layer.
2262             m_overflowControlsHostLayer->addChildBelow(m_layerForOverhangAreas.get(), m_clipLayer.get());
2263         }
2264     } else if (m_layerForOverhangAreas) {
2265         m_layerForOverhangAreas->removeFromParent();
2266         m_layerForOverhangAreas = nullptr;
2267     }
2268
2269     if (requiresContentShadowLayer()) {
2270         if (!m_contentShadowLayer) {
2271             m_contentShadowLayer = GraphicsLayer::create(graphicsLayerFactory(), this);
2272 #ifndef NDEBUG
2273             m_contentShadowLayer->setName("content shadow");
2274 #endif
2275             m_contentShadowLayer->setSize(m_rootContentLayer->size());
2276             m_contentShadowLayer->setPosition(m_rootContentLayer->position());
2277             ScrollbarTheme::theme()->setUpContentShadowLayer(m_contentShadowLayer.get());
2278
2279             m_scrollLayer->addChildBelow(m_contentShadowLayer.get(), m_rootContentLayer.get());
2280         }
2281     } else if (m_contentShadowLayer) {
2282         m_contentShadowLayer->removeFromParent();
2283         m_contentShadowLayer = nullptr;
2284     }
2285 #endif
2286
2287     if (requiresHorizontalScrollbarLayer()) {
2288         if (!m_layerForHorizontalScrollbar) {
2289             m_layerForHorizontalScrollbar = GraphicsLayer::create(graphicsLayerFactory(), this);
2290             m_layerForHorizontalScrollbar->setShowDebugBorder(m_showDebugBorders);
2291 #ifndef NDEBUG
2292             m_layerForHorizontalScrollbar->setName("horizontal scrollbar");
2293 #endif
2294 #if PLATFORM(MAC) && USE(CA)
2295             m_layerForHorizontalScrollbar->setAcceleratesDrawing(acceleratedDrawingEnabled());
2296 #endif
2297             m_overflowControlsHostLayer->addChild(m_layerForHorizontalScrollbar.get());
2298
2299             if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator())
2300                 scrollingCoordinator->frameViewHorizontalScrollbarLayerDidChange(m_renderView->frameView(), m_layerForHorizontalScrollbar.get());
2301         }
2302     } else if (m_layerForHorizontalScrollbar) {
2303         m_layerForHorizontalScrollbar->removeFromParent();
2304         m_layerForHorizontalScrollbar = nullptr;
2305
2306         if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator())
2307             scrollingCoordinator->frameViewHorizontalScrollbarLayerDidChange(m_renderView->frameView(), 0);
2308     }
2309
2310     if (requiresVerticalScrollbarLayer()) {
2311         if (!m_layerForVerticalScrollbar) {
2312             m_layerForVerticalScrollbar = GraphicsLayer::create(graphicsLayerFactory(), this);
2313             m_layerForVerticalScrollbar->setShowDebugBorder(m_showDebugBorders);
2314 #ifndef NDEBUG
2315             m_layerForVerticalScrollbar->setName("vertical scrollbar");
2316 #endif
2317 #if PLATFORM(MAC) && USE(CA)
2318         m_layerForVerticalScrollbar->setAcceleratesDrawing(acceleratedDrawingEnabled());
2319 #endif
2320             m_overflowControlsHostLayer->addChild(m_layerForVerticalScrollbar.get());
2321
2322             if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator())
2323                 scrollingCoordinator->frameViewVerticalScrollbarLayerDidChange(m_renderView->frameView(), m_layerForVerticalScrollbar.get());
2324         }
2325     } else if (m_layerForVerticalScrollbar) {
2326         m_layerForVerticalScrollbar->removeFromParent();
2327         m_layerForVerticalScrollbar = nullptr;
2328
2329         if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator())
2330             scrollingCoordinator->frameViewVerticalScrollbarLayerDidChange(m_renderView->frameView(), 0);
2331     }
2332
2333     if (requiresScrollCornerLayer()) {
2334         if (!m_layerForScrollCorner) {
2335             m_layerForScrollCorner = GraphicsLayer::create(graphicsLayerFactory(), this);
2336             m_layerForScrollCorner->setShowDebugBorder(m_showDebugBorders);
2337 #ifndef NDEBUG
2338             m_layerForScrollCorner->setName("scroll corner");
2339 #endif
2340 #if PLATFORM(MAC) && USE(CA)
2341             m_layerForScrollCorner->setAcceleratesDrawing(acceleratedDrawingEnabled());
2342 #endif
2343             m_overflowControlsHostLayer->addChild(m_layerForScrollCorner.get());
2344         }
2345     } else if (m_layerForScrollCorner) {
2346         m_layerForScrollCorner->removeFromParent();
2347         m_layerForScrollCorner = nullptr;
2348     }
2349
2350     m_renderView->frameView()->positionScrollbarLayers();
2351 }
2352
2353 void RenderLayerCompositor::ensureRootLayer()
2354 {
2355     RootLayerAttachment expectedAttachment = shouldPropagateCompositingToEnclosingFrame() ? RootLayerAttachedViaEnclosingFrame : RootLayerAttachedViaChromeClient;
2356     if (expectedAttachment == m_rootLayerAttachment)
2357          return;
2358
2359     if (!m_rootContentLayer) {
2360         m_rootContentLayer = GraphicsLayer::create(graphicsLayerFactory(), this);
2361 #ifndef NDEBUG
2362         m_rootContentLayer->setName("content root");
2363 #endif
2364         IntRect overflowRect = m_renderView->pixelSnappedLayoutOverflowRect();
2365         m_rootContentLayer->setSize(FloatSize(overflowRect.maxX(), overflowRect.maxY()));
2366         m_rootContentLayer->setPosition(FloatPoint());
2367
2368         // Need to clip to prevent transformed content showing outside this frame
2369         m_rootContentLayer->setMasksToBounds(true);
2370     }
2371
2372     if (requiresScrollLayer(expectedAttachment)) {
2373         if (!m_overflowControlsHostLayer) {
2374             ASSERT(!m_scrollLayer);
2375             ASSERT(!m_clipLayer);
2376
2377             // Create a layer to host the clipping layer and the overflow controls layers.
2378             m_overflowControlsHostLayer = GraphicsLayer::create(graphicsLayerFactory(), this);
2379 #ifndef NDEBUG
2380             m_overflowControlsHostLayer->setName("overflow controls host");
2381 #endif
2382
2383             // Create a clipping layer if this is an iframe
2384             m_clipLayer = GraphicsLayer::create(graphicsLayerFactory(), this);
2385 #ifndef NDEBUG
2386             m_clipLayer->setName("frame clipping");
2387 #endif
2388             m_clipLayer->setMasksToBounds(true);
2389             
2390             m_scrollLayer = GraphicsLayer::create(graphicsLayerFactory(), this);
2391 #ifndef NDEBUG
2392             m_scrollLayer->setName("frame scrolling");
2393 #endif
2394             if (ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator())
2395                 scrollingCoordinator->setLayerIsContainerForFixedPositionLayers(m_scrollLayer.get(), true);
2396
2397             // Hook them up
2398             m_overflowControlsHostLayer->addChild(m_clipLayer.get());
2399             m_clipLayer->addChild(m_scrollLayer.get());
2400             m_scrollLayer->addChild(m_rootContentLayer.get());
2401
2402             frameViewDidChangeSize();
2403             frameViewDidScroll();
2404         }
2405     } else {
2406         if (m_overflowControlsHostLayer) {
2407             m_overflowControlsHostLayer = nullptr;
2408             m_clipLayer = nullptr;
2409             m_scrollLayer = nullptr;
2410         }
2411     }
2412
2413     // Check to see if we have to change the attachment
2414     if (m_rootLayerAttachment != RootLayerUnattached)
2415         detachRootLayer();
2416
2417     attachRootLayer(expectedAttachment);
2418 }
2419
2420 void RenderLayerCompositor::destroyRootLayer()
2421 {
2422     if (!m_rootContentLayer)
2423         return;
2424
2425     detachRootLayer();
2426
2427 #if ENABLE(RUBBER_BANDING)
2428     if (m_layerForOverhangAreas) {
2429         m_layerForOverhangAreas->removeFromParent();
2430         m_layerForOverhangAreas = nullptr;
2431     }
2432 #endif
2433
2434     if (m_layerForHorizontalScrollbar) {
2435         m_layerForHorizontalScrollbar->removeFromParent();
2436         m_layerForHorizontalScrollbar = nullptr;
2437         if (Scrollbar* horizontalScrollbar = m_renderView->frameView()->verticalScrollbar())
2438             m_renderView->frameView()->invalidateScrollbar(horizontalScrollbar, IntRect(IntPoint(0, 0), horizontalScrollbar->frameRect().size()));
2439     }
2440
2441     if (m_layerForVerticalScrollbar) {
2442         m_layerForVerticalScrollbar->removeFromParent();
2443         m_layerForVerticalScrollbar = nullptr;
2444         if (Scrollbar* verticalScrollbar = m_renderView->frameView()->verticalScrollbar())
2445             m_renderView->frameView()->invalidateScrollbar(verticalScrollbar, IntRect(IntPoint(0, 0), verticalScrollbar->frameRect().size()));
2446     }
2447
2448     if (m_layerForScrollCorner) {
2449         m_layerForScrollCorner = nullptr;
2450         m_renderView->frameView()->invalidateScrollCorner(m_renderView->frameView()->scrollCornerRect());
2451     }
2452
2453     if (m_overflowControlsHostLayer) {
2454         m_overflowControlsHostLayer = nullptr;
2455         m_clipLayer = nullptr;
2456         m_scrollLayer = nullptr;
2457     }
2458     ASSERT(!m_scrollLayer);
2459     m_rootContentLayer = nullptr;
2460
2461     m_layerUpdater = nullptr;
2462 }
2463
2464 void RenderLayerCompositor::attachRootLayer(RootLayerAttachment attachment)
2465 {
2466     if (!m_rootContentLayer)
2467         return;
2468
2469     switch (attachment) {
2470         case RootLayerUnattached:
2471             ASSERT_NOT_REACHED();
2472             break;
2473         case RootLayerAttachedViaChromeClient: {
2474             Frame* frame = m_renderView->frameView()->frame();
2475             Page* page = frame ? frame->page() : 0;
2476             if (!page)
2477                 return;
2478
2479             page->chrome()->client()->attachRootGraphicsLayer(frame, rootGraphicsLayer());
2480             break;
2481         }
2482         case RootLayerAttachedViaEnclosingFrame: {
2483             // The layer will get hooked up via RenderLayerBacking::updateGraphicsLayerConfiguration()
2484             // for the frame's renderer in the parent document.
2485             m_renderView->document()->ownerElement()->scheduleSetNeedsStyleRecalc(SyntheticStyleChange);
2486             break;
2487         }
2488     }
2489
2490     m_rootLayerAttachment = attachment;
2491     rootLayerAttachmentChanged();
2492     
2493     if (m_shouldFlushOnReattach) {
2494         flushPendingLayerChanges(true);
2495         m_shouldFlushOnReattach = false;
2496     }
2497 }
2498
2499 void RenderLayerCompositor::detachRootLayer()
2500 {
2501     if (!m_rootContentLayer || m_rootLayerAttachment == RootLayerUnattached)
2502         return;
2503
2504     switch (m_rootLayerAttachment) {
2505     case RootLayerAttachedViaEnclosingFrame: {
2506         // The layer will get unhooked up via RenderLayerBacking::updateGraphicsLayerConfiguration()
2507         // for the frame's renderer in the parent document.
2508         if (m_overflowControlsHostLayer)
2509             m_overflowControlsHostLayer->removeFromParent();
2510         else
2511             m_rootContentLayer->removeFromParent();
2512
2513         if (HTMLFrameOwnerElement* ownerElement = m_renderView->document()->ownerElement())
2514             ownerElement->scheduleSetNeedsStyleRecalc(SyntheticStyleChange);
2515         break;
2516     }
2517     case RootLayerAttachedViaChromeClient: {
2518         Frame* frame = m_renderView->frameView()->frame();
2519         Page* page = frame ? frame->page() : 0;
2520         if (!page)
2521             return;
2522
2523         page->chrome()->client()->attachRootGraphicsLayer(frame, 0);
2524     }
2525     break;
2526     case RootLayerUnattached:
2527         break;
2528     }
2529
2530     m_rootLayerAttachment = RootLayerUnattached;
2531     rootLayerAttachmentChanged();
2532 }
2533
2534 void RenderLayerCompositor::updateRootLayerAttachment()
2535 {
2536     ensureRootLayer();
2537 }
2538
2539 void RenderLayerCompositor::rootLayerAttachmentChanged()
2540 {
2541     // The attachment can affect whether the RenderView layer's paintsIntoWindow() behavior,
2542     // so call updateGraphicsLayerGeometry() to udpate that.
2543     RenderLayer* layer = m_renderView->layer();
2544     if (RenderLayerBacking* backing = layer ? layer->backing() : 0)
2545         backing->updateDrawsContent();
2546 }
2547
2548 // IFrames are special, because we hook compositing layers together across iframe boundaries
2549 // when both parent and iframe content are composited. So when this frame becomes composited, we have
2550 // to use a synthetic style change to get the iframes into RenderLayers in order to allow them to composite.
2551 void RenderLayerCompositor::notifyIFramesOfCompositingChange()
2552 {
2553     Frame* frame = m_renderView->frameView() ? m_renderView->frameView()->frame() : 0;
2554     if (!frame)
2555         return;
2556
2557     for (Frame* child = frame->tree()->firstChild(); child; child = child->tree()->traverseNext(frame)) {
2558         if (child->document() && child->document()->ownerElement())
2559             child->document()->ownerElement()->scheduleSetNeedsStyleRecalc(SyntheticStyleChange);
2560     }
2561     
2562     // Compositing also affects the answer to RenderIFrame::requiresAcceleratedCompositing(), so 
2563     // we need to schedule a style recalc in our parent document.
2564     if (HTMLFrameOwnerElement* ownerElement = m_renderView->document()->ownerElement())
2565         ownerElement->scheduleSetNeedsStyleRecalc(SyntheticStyleChange);
2566 }
2567
2568 bool RenderLayerCompositor::layerHas3DContent(const RenderLayer* layer) const
2569 {
2570     const RenderStyle* style = layer->renderer()->style();
2571
2572     if (style && 
2573         (style->transformStyle3D() == TransformStyle3DPreserve3D ||
2574          style->hasPerspective() ||
2575          style->transform().has3DOperation()))
2576         return true;
2577
2578     const_cast<RenderLayer*>(layer)->updateLayerListsIfNeeded();
2579
2580 #if !ASSERT_DISABLED
2581     LayerListMutationDetector mutationChecker(const_cast<RenderLayer*>(layer));
2582 #endif
2583
2584     if (layer->isStackingContext()) {
2585         if (Vector<RenderLayer*>* negZOrderList = layer->negZOrderList()) {
2586             size_t listSize = negZOrderList->size();
2587             for (size_t i = 0; i < listSize; ++i) {
2588                 RenderLayer* curLayer = negZOrderList->at(i);
2589                 if (layerHas3DContent(curLayer))
2590                     return true;
2591             }
2592         }
2593
2594         if (Vector<RenderLayer*>* posZOrderList = layer->posZOrderList()) {
2595             size_t listSize = posZOrderList->size();
2596             for (size_t i = 0; i < listSize; ++i) {
2597                 RenderLayer* curLayer = posZOrderList->at(i);
2598                 if (layerHas3DContent(curLayer))
2599                     return true;
2600             }
2601         }
2602     }
2603
2604     if (Vector<RenderLayer*>* normalFlowList = layer->normalFlowList()) {
2605         size_t listSize = normalFlowList->size();
2606         for (size_t i = 0; i < listSize; ++i) {
2607             RenderLayer* curLayer = normalFlowList->at(i);
2608             if (layerHas3DContent(curLayer))
2609                 return true;
2610         }
2611     }
2612     return false;
2613 }
2614
2615 void RenderLayerCompositor::deviceOrPageScaleFactorChanged()
2616 {
2617     // Start at the RenderView's layer, since that's where the scale is applied.
2618     RenderLayer* viewLayer = m_renderView->layer();
2619     if (!viewLayer->isComposited())
2620         return;
2621
2622     if (GraphicsLayer* rootLayer = viewLayer->backing()->graphicsLayer())
2623         rootLayer->noteDeviceOrPageScaleFactorChangedIncludingDescendants();
2624 }
2625
2626 static bool isRootmostFixedOrStickyLayer(RenderLayer* layer)
2627 {
2628     if (layer->renderer()->isStickyPositioned())
2629         return true;
2630
2631     if (layer->renderer()->style()->position() != FixedPosition)
2632         return false;
2633
2634     for (RenderLayer* stackingContext = layer->stackingContext(); stackingContext; stackingContext = stackingContext->stackingContext()) {
2635         if (stackingContext->isComposited() && stackingContext->renderer()->style()->position() == FixedPosition)
2636             return false;
2637     }
2638
2639     return true;
2640 }
2641
2642 void RenderLayerCompositor::updateViewportConstraintStatus(RenderLayer* layer)
2643 {
2644     if (isRootmostFixedOrStickyLayer(layer))
2645         addViewportConstrainedLayer(layer);
2646     else
2647         removeViewportConstrainedLayer(layer);
2648 }
2649
2650 void RenderLayerCompositor::addViewportConstrainedLayer(RenderLayer* layer)
2651 {
2652     m_viewportConstrainedLayers.add(layer);
2653     registerOrUpdateViewportConstrainedLayer(layer);
2654 }
2655
2656 void RenderLayerCompositor::removeViewportConstrainedLayer(RenderLayer* layer)
2657 {
2658     if (!m_viewportConstrainedLayers.contains(layer))
2659         return;
2660
2661     unregisterViewportConstrainedLayer(layer);
2662     m_viewportConstrainedLayers.remove(layer);
2663 }
2664
2665 FixedPositionViewportConstraints RenderLayerCompositor::computeFixedViewportConstraints(RenderLayer* layer) const
2666 {
2667     ASSERT(layer->isComposited());
2668
2669     FrameView* frameView = m_renderView->frameView();
2670
2671     LayoutRect viewportRect = frameView->visibleContentRect();
2672     viewportRect.setLocation(toPoint(frameView->scrollOffsetForFixedPosition()));
2673
2674     FixedPositionViewportConstraints constraints = FixedPositionViewportConstraints();
2675
2676     GraphicsLayer* graphicsLayer = layer->backing()->graphicsLayer();
2677
2678     constraints.setLayerPositionAtLastLayout(graphicsLayer->position());
2679     constraints.setViewportRectAtLastLayout(viewportRect);
2680         
2681     RenderStyle* style = layer->renderer()->style();
2682     if (!style->left().isAuto())
2683         constraints.addAnchorEdge(ViewportConstraints::AnchorEdgeLeft);
2684
2685     if (!style->right().isAuto())
2686         constraints.addAnchorEdge(ViewportConstraints::AnchorEdgeRight);
2687
2688     if (!style->top().isAuto())
2689         constraints.addAnchorEdge(ViewportConstraints::AnchorEdgeTop);
2690
2691     if (!style->bottom().isAuto())
2692         constraints.addAnchorEdge(ViewportConstraints::AnchorEdgeBottom);
2693
2694     // If left and right are auto, use left.
2695     if (style->left().isAuto() && style->right().isAuto())
2696         constraints.addAnchorEdge(ViewportConstraints::AnchorEdgeLeft);
2697
2698     // If top and bottom are auto, use top.
2699     if (style->top().isAuto() && style->bottom().isAuto())
2700         constraints.addAnchorEdge(ViewportConstraints::AnchorEdgeTop);
2701         
2702     return constraints;
2703 }
2704
2705 StickyPositionViewportConstraints RenderLayerCompositor::computeStickyViewportConstraints(RenderLayer* layer) const
2706 {
2707     ASSERT(layer->isComposited());
2708
2709     FrameView* frameView = m_renderView->frameView();
2710     LayoutRect viewportRect = frameView->visibleContentRect();
2711
2712     StickyPositionViewportConstraints constraints = StickyPositionViewportConstraints();
2713
2714     RenderBoxModelObject* renderer = toRenderBoxModelObject(layer->renderer());
2715
2716     renderer->computeStickyPositionConstraints(constraints, viewportRect);
2717
2718     GraphicsLayer* graphicsLayer = layer->backing()->graphicsLayer();
2719
2720     constraints.setLayerPositionAtLastLayout(graphicsLayer->position());
2721     constraints.setStickyOffsetAtLastLayout(renderer->stickyPositionOffset());
2722
2723     return constraints;
2724 }
2725
2726 static RenderLayerBacking* nearestScrollingCoordinatorAncestor(RenderLayer* layer)
2727 {
2728     RenderLayer* ancestor = layer->parent();
2729     while (ancestor) {
2730         if (RenderLayerBacking* backing = ancestor->backing()) {
2731             if (backing->scrollLayerID() && !ancestor->scrollsOverflow())
2732                 return backing;
2733         }
2734         ancestor = ancestor->parent();
2735     }
2736
2737     return 0;
2738 }
2739
2740 void RenderLayerCompositor::registerOrUpdateViewportConstrainedLayer(RenderLayer* layer)
2741 {
2742     // FIXME: We should support sticky position here! And we should eventuall support fixed/sticky elements
2743     // that are inside non-main frames once we get non-main frames scrolling with the ScrollingCoordinator.
2744     if (m_renderView->document()->ownerElement())
2745         return;
2746
2747     ScrollingCoordinator* scrollingCoordinator = this->scrollingCoordinator();
2748     if (!scrollingCoordinator)
2749         return;
2750
2751     // FIXME: rename to supportsViewportConstrainedPositionLayers()?
2752     if (!scrollingCoordinator->supportsFixedPositionLayers() || !layer->parent())
2753         return;
2754
2755     ASSERT(m_viewportConstrainedLayers.contains(layer));
2756     ASSERT(layer->isComposited());
2757
2758     RenderLayerBacking* backing = layer->backing();
2759     if (!backing)
2760         return;
2761
2762     ScrollingNodeID nodeID = backing->scrollLayerID();
2763     RenderLayerBacking* parent = nearestScrollingCoordinatorAncestor(layer);
2764     if (!parent)
2765         return;
2766
2767     // Always call this even if the backing is already attached because the parent may have changed.
2768     backing->attachToScrollingCoordinatorWithParent(parent);
2769     nodeID = backing->scrollLayerID();
2770
2771     if (layer->renderer()->isStickyPositioned())
2772         scrollingCoordinator->updateViewportConstrainedNode(nodeID, computeStickyViewportConstraints(layer), backing->graphicsLayer());
2773     else
2774         scrollingCoordinator->updateViewportConstrainedNode(nodeID, computeFixedViewportConstraints(layer), backing->graphicsLayer());
2775 }
2776
2777 void RenderLayerCompositor::unregisterViewportConstrainedLayer(RenderLayer* layer)
2778 {
2779     ASSERT(m_viewportConstrainedLayers.contains(layer));
2780
2781     if (RenderLayerBacking* backing = layer->backing())
2782         backing->detachFromScrollingCoordinator();
2783 }
2784
2785 void RenderLayerCompositor::windowScreenDidChange(PlatformDisplayID displayID)
2786 {
2787     if (m_layerUpdater)
2788         m_layerUpdater->screenDidChange(displayID);
2789 }
2790
2791 ScrollingCoordinator* RenderLayerCompositor::scrollingCoordinator() const
2792 {
2793     if (Page* page = this->page())
2794         return page->scrollingCoordinator();
2795
2796     return 0;
2797 }
2798
2799 GraphicsLayerFactory* RenderLayerCompositor::graphicsLayerFactory() const
2800 {
2801     if (Page* page = this->page())
2802         return page->chrome()->client()->graphicsLayerFactory();
2803
2804     return 0;
2805 }
2806
2807 Page* RenderLayerCompositor::page() const
2808 {
2809     if (Frame* frame = m_renderView->frameView()->frame())
2810         return frame->page();
2811     
2812     return 0;
2813 }
2814
2815 void RenderLayerCompositor::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
2816 {
2817     MemoryClassInfo info(memoryObjectInfo, this, PlatformMemoryTypes::Rendering);
2818     info.addWeakPointer(m_renderView);
2819     info.addMember(m_rootContentLayer);
2820     info.addMember(m_updateCompositingLayersTimer);
2821     info.addMember(m_clipLayer);
2822     info.addMember(m_scrollLayer);
2823     info.addMember(m_viewportConstrainedLayers);
2824     info.addMember(m_viewportConstrainedLayersNeedingUpdate);
2825     info.addMember(m_overflowControlsHostLayer);
2826     info.addMember(m_layerForHorizontalScrollbar);
2827     info.addMember(m_layerForVerticalScrollbar);
2828     info.addMember(m_layerForScrollCorner);
2829 #if ENABLE(RUBBER_BANDING)
2830     info.addMember(m_layerForOverhangAreas);
2831     info.addMember(m_contentShadowLayer);
2832 #endif
2833     info.addMember(m_layerUpdater);
2834 }
2835
2836 } // namespace WebCore
2837
2838 #endif // USE(ACCELERATED_COMPOSITING)