448a8965e7a0528f4134bf743f4c69d8d1db7e3b
[WebKit-https.git] / Source / WebCore / rendering / RenderLayerBacking.cpp
1 /*
2  * Copyright (C) 2009, 2010, 2011 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE 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
30 #include "RenderLayerBacking.h"
31
32 #include "AnimationController.h"
33 #include "CanvasRenderingContext.h"
34 #include "CSSPropertyNames.h"
35 #include "CachedImage.h"
36 #include "Chrome.h"
37 #include "FontCache.h"
38 #include "FrameView.h"
39 #include "GraphicsContext.h"
40 #include "GraphicsLayer.h"
41 #include "HTMLCanvasElement.h"
42 #include "HTMLIFrameElement.h"
43 #include "HTMLMediaElement.h"
44 #include "HTMLNames.h"
45 #include "InspectorInstrumentation.h"
46 #include "KeyframeList.h"
47 #include "PluginViewBase.h"
48 #include "RenderApplet.h"
49 #include "RenderIFrame.h"
50 #include "RenderImage.h"
51 #include "RenderLayerCompositor.h"
52 #include "RenderEmbeddedObject.h"
53 #include "RenderVideo.h"
54 #include "RenderView.h"
55 #include "ScrollingCoordinator.h"
56 #include "Settings.h"
57 #include "StyleResolver.h"
58 #include "TiledBacking.h"
59 #include "WebCoreMemoryInstrumentation.h"
60 #include <wtf/CurrentTime.h>
61 #include <wtf/text/StringBuilder.h>
62
63 #if ENABLE(CSS_FILTERS)
64 #include "FilterEffectRenderer.h"
65 #if ENABLE(CSS_SHADERS)
66 #include "CustomFilterOperation.h"
67 #endif
68 #endif
69
70 #if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
71 #include "GraphicsContext3D.h"
72 #endif
73
74 using namespace std;
75
76 namespace WebCore {
77
78 using namespace HTMLNames;
79
80 static bool hasBoxDecorationsOrBackgroundImage(const RenderStyle*);
81 static IntRect clipBox(RenderBox* renderer);
82
83 static inline bool isAcceleratedCanvas(RenderObject* renderer)
84 {
85 #if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
86     if (renderer->isCanvas()) {
87         HTMLCanvasElement* canvas = static_cast<HTMLCanvasElement*>(renderer->node());
88         if (CanvasRenderingContext* context = canvas->renderingContext())
89             return context->isAccelerated();
90     }
91 #else
92     UNUSED_PARAM(renderer);
93 #endif
94     return false;
95 }
96
97 // Get the scrolling coordinator in a way that works inside RenderLayerBacking's destructor.
98 static ScrollingCoordinator* scrollingCoordinatorFromLayer(RenderLayer* layer)
99 {
100     Page* page = layer->renderer()->frame()->page();
101     if (!page)
102         return 0;
103
104     return page->scrollingCoordinator();
105 }
106
107 bool RenderLayerBacking::m_creatingPrimaryGraphicsLayer = false;
108
109 RenderLayerBacking::RenderLayerBacking(RenderLayer* layer)
110     : m_owningLayer(layer)
111     , m_scrollLayerID(0)
112     , m_artificiallyInflatedBounds(false)
113     , m_boundsConstrainedByClipping(false)
114     , m_isMainFrameRenderViewLayer(false)
115     , m_usingTiledCacheLayer(false)
116     , m_requiresOwnBackingStore(true)
117 #if ENABLE(CSS_FILTERS)
118     , m_canCompositeFilters(false)
119 #endif
120     , m_backgroundLayerPaintsFixedRootBackground(false)
121 {
122     if (layer->isRootLayer()) {
123         Frame* frame = toRenderView(renderer())->frameView()->frame();
124         Page* page = frame ? frame->page() : 0;
125         if (page && frame && page->mainFrame() == frame) {
126             m_isMainFrameRenderViewLayer = true;
127
128 #if PLATFORM(MAC)
129             // FIXME: It's a little weird that we base this decision on whether there's a scrolling coordinator or not.
130             if (page->scrollingCoordinator())
131                 m_usingTiledCacheLayer = true;
132 #endif
133         }
134     }
135     
136     createPrimaryGraphicsLayer();
137
138     if (m_usingTiledCacheLayer) {
139         TiledBacking* tiledBacking = this->tiledBacking();
140         if (Page* page = renderer()->frame()->page()) {
141             Frame* frame = renderer()->frame();
142             tiledBacking->setIsInWindow(page->isInWindow());
143
144             if (m_isMainFrameRenderViewLayer)
145                 tiledBacking->setUnparentsOffscreenTiles(true);
146
147             tiledBacking->setScrollingPerformanceLoggingEnabled(frame->settings() && frame->settings()->scrollingPerformanceLoggingEnabled());
148             adjustTileCacheCoverage();
149         }
150     }
151 }
152
153 RenderLayerBacking::~RenderLayerBacking()
154 {
155     updateClippingLayers(false, false);
156     updateOverflowControlsLayers(false, false, false);
157     updateForegroundLayer(false);
158     updateBackgroundLayer(false);
159     updateMaskLayer(false);
160     updateScrollingLayers(false);
161     detachFromScrollingCoordinator();
162     destroyGraphicsLayers();
163 }
164
165 PassOwnPtr<GraphicsLayer> RenderLayerBacking::createGraphicsLayer(const String& name)
166 {
167     GraphicsLayerFactory* graphicsLayerFactory = 0;
168     if (Page* page = renderer()->frame()->page())
169         graphicsLayerFactory = page->chrome()->client()->graphicsLayerFactory();
170
171     OwnPtr<GraphicsLayer> graphicsLayer = GraphicsLayer::create(graphicsLayerFactory, this);
172
173 #ifndef NDEBUG
174     graphicsLayer->setName(name);
175 #else
176     UNUSED_PARAM(name);
177 #endif
178     graphicsLayer->setMaintainsPixelAlignment(compositor()->keepLayersPixelAligned());
179
180 #if PLATFORM(MAC) && USE(CA)
181     graphicsLayer->setAcceleratesDrawing(compositor()->acceleratedDrawingEnabled());
182 #endif    
183     
184     return graphicsLayer.release();
185 }
186
187 bool RenderLayerBacking::shouldUseTileCache(const GraphicsLayer*) const
188 {
189     return m_usingTiledCacheLayer && m_creatingPrimaryGraphicsLayer;
190 }
191
192 TiledBacking* RenderLayerBacking::tiledBacking() const
193 {
194     return m_graphicsLayer->tiledBacking();
195 }
196
197 void RenderLayerBacking::adjustTileCacheCoverage()
198 {
199     if (!m_usingTiledCacheLayer)
200         return;
201
202     TiledBacking::TileCoverage tileCoverage = TiledBacking::CoverageForVisibleArea;
203
204     // FIXME: When we use TiledBacking for overflow, this should look at RenderView scrollability.
205     Frame* frame = renderer()->frame();
206     if (frame) {
207         FrameView* frameView = frame->view();
208         if (frameView->horizontalScrollbarMode() != ScrollbarAlwaysOff)
209             tileCoverage |= TiledBacking::CoverageForHorizontalScrolling;
210
211         if (frameView->verticalScrollbarMode() != ScrollbarAlwaysOff)
212             tileCoverage |= TiledBacking::CoverageForVerticalScrolling;
213
214         if (ScrollingCoordinator* scrollingCoordinator = scrollingCoordinatorFromLayer(m_owningLayer)) {
215             // Ask our TiledBacking for large tiles unless the only reason we're main-thread-scrolling
216             // is a page overlay (find-in-page, the Web Inspector highlight mechanism, etc.).
217             if (scrollingCoordinator->mainThreadScrollingReasons() & ~ScrollingCoordinator::ForcedOnMainThread)
218                 tileCoverage |= TiledBacking::CoverageForSlowScrolling;
219         }
220     }
221
222     tiledBacking()->setTileCoverage(tileCoverage);
223 }
224
225 void RenderLayerBacking::updateDebugIndicators(bool showBorder, bool showRepaintCounter)
226 {
227     m_graphicsLayer->setShowDebugBorder(showBorder);
228     m_graphicsLayer->setShowRepaintCounter(showRepaintCounter);
229     
230     if (m_ancestorClippingLayer)
231         m_ancestorClippingLayer->setShowDebugBorder(showBorder);
232
233     if (m_foregroundLayer) {
234         m_foregroundLayer->setShowDebugBorder(showBorder);
235         m_foregroundLayer->setShowRepaintCounter(showRepaintCounter);
236     }
237     
238     if (m_contentsContainmentLayer)
239         m_contentsContainmentLayer->setShowDebugBorder(showBorder);
240     
241     if (m_backgroundLayer) {
242         m_backgroundLayer->setShowDebugBorder(showBorder);
243         m_backgroundLayer->setShowRepaintCounter(showRepaintCounter);
244     }
245
246     if (m_maskLayer) {
247         m_maskLayer->setShowDebugBorder(showBorder);
248         m_maskLayer->setShowRepaintCounter(showRepaintCounter);
249     }
250
251     if (m_layerForHorizontalScrollbar)
252         m_layerForHorizontalScrollbar->setShowDebugBorder(showBorder);
253
254     if (m_layerForVerticalScrollbar)
255         m_layerForVerticalScrollbar->setShowDebugBorder(showBorder);
256
257     if (m_layerForScrollCorner)
258         m_layerForScrollCorner->setShowDebugBorder(showBorder);
259
260     if (m_scrollingLayer)
261         m_scrollingLayer->setShowDebugBorder(showBorder);
262
263     if (m_scrollingContentsLayer) {
264         m_scrollingContentsLayer->setShowDebugBorder(showBorder);
265         m_scrollingContentsLayer->setShowRepaintCounter(showRepaintCounter);
266     }
267 }
268
269 void RenderLayerBacking::createPrimaryGraphicsLayer()
270 {
271     String layerName;
272 #ifndef NDEBUG
273     layerName = m_owningLayer->name();
274 #endif
275     
276     // The call to createGraphicsLayer ends calling back into here as
277     // a GraphicsLayerClient to ask if it shouldUseTileCache(). We only want
278     // the tile cache on our main layer. This is pretty ugly, but saves us from
279     // exposing the API to all clients.
280
281     m_creatingPrimaryGraphicsLayer = true;
282     m_graphicsLayer = createGraphicsLayer(layerName);
283     m_creatingPrimaryGraphicsLayer = false;
284
285     if (m_usingTiledCacheLayer)
286         m_childContainmentLayer = createGraphicsLayer("TileCache Flattening Layer");
287
288     if (m_isMainFrameRenderViewLayer) {
289         m_graphicsLayer->setContentsOpaque(true);
290         m_graphicsLayer->setAppliesPageScale();
291     }
292
293 #if PLATFORM(MAC) && USE(CA)
294     if (!compositor()->acceleratedDrawingEnabled() && renderer()->isCanvas()) {
295         HTMLCanvasElement* canvas = static_cast<HTMLCanvasElement*>(renderer()->node());
296         if (canvas->shouldAccelerate(canvas->size()))
297             m_graphicsLayer->setAcceleratesDrawing(true);
298     }
299 #endif    
300     
301     updateOpacity(renderer()->style());
302     updateTransform(renderer()->style());
303 #if ENABLE(CSS_FILTERS)
304     updateFilters(renderer()->style());
305 #endif
306 #if ENABLE(CSS_COMPOSITING)
307     updateLayerBlendMode(renderer()->style());
308 #endif
309 }
310
311 void RenderLayerBacking::destroyGraphicsLayers()
312 {
313     if (m_graphicsLayer)
314         m_graphicsLayer->removeFromParent();
315
316     m_ancestorClippingLayer = nullptr;
317     m_contentsContainmentLayer = nullptr;
318     m_graphicsLayer = nullptr;
319     m_foregroundLayer = nullptr;
320     m_backgroundLayer = nullptr;
321     m_childContainmentLayer = nullptr;
322     m_maskLayer = nullptr;
323
324     m_scrollingLayer = nullptr;
325     m_scrollingContentsLayer = nullptr;
326 }
327
328 void RenderLayerBacking::updateOpacity(const RenderStyle* style)
329 {
330     m_graphicsLayer->setOpacity(compositingOpacity(style->opacity()));
331 }
332
333 void RenderLayerBacking::updateTransform(const RenderStyle* style)
334 {
335     // FIXME: This could use m_owningLayer->transform(), but that currently has transform-origin
336     // baked into it, and we don't want that.
337     TransformationMatrix t;
338     if (m_owningLayer->hasTransform()) {
339         style->applyTransform(t, toRenderBox(renderer())->pixelSnappedBorderBoxRect().size(), RenderStyle::ExcludeTransformOrigin);
340         makeMatrixRenderable(t, compositor()->canRender3DTransforms());
341     }
342     
343     if (m_contentsContainmentLayer) {
344         m_contentsContainmentLayer->setTransform(t);
345         m_graphicsLayer->setTransform(TransformationMatrix());
346     } else
347         m_graphicsLayer->setTransform(t);
348 }
349
350 #if ENABLE(CSS_FILTERS)
351 void RenderLayerBacking::updateFilters(const RenderStyle* style)
352 {
353     m_canCompositeFilters = m_graphicsLayer->setFilters(owningLayer()->computeFilterOperations(style));
354 }
355 #endif
356
357 #if ENABLE(CSS_COMPOSITING)
358 void RenderLayerBacking::updateLayerBlendMode(const RenderStyle*)
359 {
360 }
361 #endif
362
363 static bool hasNonZeroTransformOrigin(const RenderObject* renderer)
364 {
365     RenderStyle* style = renderer->style();
366     return (style->transformOriginX().type() == Fixed && style->transformOriginX().value())
367         || (style->transformOriginY().type() == Fixed && style->transformOriginY().value());
368 }
369
370 static bool layerOrAncestorIsTransformedOrUsingCompositedScrolling(RenderLayer* layer)
371 {
372     for (RenderLayer* curr = layer; curr; curr = curr->parent()) {
373         if (curr->hasTransform() || curr->needsCompositedScrolling())
374             return true;
375     }
376     
377     return false;
378 }
379
380 bool RenderLayerBacking::shouldClipCompositedBounds() const
381 {
382     // Scrollbar layers use this layer for relative positioning, so don't clip.
383     if (layerForHorizontalScrollbar() || layerForVerticalScrollbar())
384         return false;
385
386     if (m_usingTiledCacheLayer)
387         return false;
388
389     if (!compositor()->compositingConsultsOverlap())
390         return false;
391
392     if (layerOrAncestorIsTransformedOrUsingCompositedScrolling(m_owningLayer))
393         return false;
394
395     return true;
396 }
397
398 void RenderLayerBacking::updateCompositedBounds()
399 {
400     IntRect layerBounds = compositor()->calculateCompositedBounds(m_owningLayer, m_owningLayer);
401
402     // Clip to the size of the document or enclosing overflow-scroll layer.
403     // If this or an ancestor is transformed, we can't currently compute the correct rect to intersect with.
404     // We'd need RenderObject::convertContainerToLocalQuad(), which doesn't yet exist.
405     if (shouldClipCompositedBounds()) {
406         RenderView* view = m_owningLayer->renderer()->view();
407         RenderLayer* rootLayer = view->layer();
408
409         LayoutRect clippingBounds;
410         if (renderer()->style()->position() == FixedPosition && renderer()->container() == view)
411             clippingBounds = view->frameView()->viewportConstrainedVisibleContentRect();
412         else
413             clippingBounds = view->unscaledDocumentRect();
414
415         if (m_owningLayer != rootLayer)
416             clippingBounds.intersect(m_owningLayer->backgroundClipRect(RenderLayer::ClipRectsContext(rootLayer, 0, AbsoluteClipRects)).rect()); // FIXME: Incorrect for CSS regions.
417
418         LayoutPoint delta;
419         m_owningLayer->convertToLayerCoords(rootLayer, delta);
420         clippingBounds.move(-delta.x(), -delta.y());
421
422         layerBounds.intersect(pixelSnappedIntRect(clippingBounds));
423         m_boundsConstrainedByClipping = true;
424     } else
425         m_boundsConstrainedByClipping = false;
426     
427     // If the element has a transform-origin that has fixed lengths, and the renderer has zero size,
428     // then we need to ensure that the compositing layer has non-zero size so that we can apply
429     // the transform-origin via the GraphicsLayer anchorPoint (which is expressed as a fractional value).
430     if (layerBounds.isEmpty() && hasNonZeroTransformOrigin(renderer())) {
431         layerBounds.setWidth(1);
432         layerBounds.setHeight(1);
433         m_artificiallyInflatedBounds = true;
434     } else
435         m_artificiallyInflatedBounds = false;
436
437     setCompositedBounds(layerBounds);
438 }
439
440 void RenderLayerBacking::updateAfterWidgetResize()
441 {
442     if (renderer()->isRenderPart()) {
443         if (RenderLayerCompositor* innerCompositor = RenderLayerCompositor::frameContentsCompositor(toRenderPart(renderer()))) {
444             innerCompositor->frameViewDidChangeSize();
445             innerCompositor->frameViewDidChangeLocation(contentsBox().location());
446         }
447     }
448 }
449
450 void RenderLayerBacking::updateAfterLayout(UpdateAfterLayoutFlags flags)
451 {
452     RenderLayerCompositor* layerCompositor = compositor();
453     if (!layerCompositor->compositingLayersNeedRebuild()) {
454         // Calling updateGraphicsLayerGeometry() here gives incorrect results, because the
455         // position of this layer's GraphicsLayer depends on the position of our compositing
456         // ancestor's GraphicsLayer. That cannot be determined until all the descendant 
457         // RenderLayers of that ancestor have been processed via updateLayerPositions().
458         //
459         // The solution is to update compositing children of this layer here,
460         // via updateCompositingChildrenGeometry().
461         updateCompositedBounds();
462         layerCompositor->updateCompositingDescendantGeometry(m_owningLayer, m_owningLayer, flags & CompositingChildrenOnly);
463         
464         if (flags & IsUpdateRoot) {
465             updateGraphicsLayerGeometry();
466             layerCompositor->updateRootLayerPosition();
467         }
468     }
469     
470     if (flags & NeedsFullRepaint && !paintsIntoWindow() && !paintsIntoCompositedAncestor())
471         setContentsNeedDisplay();
472 }
473
474 bool RenderLayerBacking::updateGraphicsLayerConfiguration()
475 {
476     RenderLayerCompositor* compositor = this->compositor();
477     RenderObject* renderer = this->renderer();
478
479     m_owningLayer->updateDescendantDependentFlags();
480     m_owningLayer->updateZOrderLists();
481
482     bool layerConfigChanged = false;
483     setBackgroundLayerPaintsFixedRootBackground(compositor->needsFixedRootBackgroundLayer(m_owningLayer));
484     
485     // The background layer is currently only used for fixed root backgrounds.
486     if (updateBackgroundLayer(m_backgroundLayerPaintsFixedRootBackground))
487         layerConfigChanged = true;
488
489     if (updateForegroundLayer(compositor->needsContentsCompositingLayer(m_owningLayer)))
490         layerConfigChanged = true;
491     
492     bool needsDescendentsClippingLayer = compositor->clipsCompositingDescendants(m_owningLayer);
493
494     // Our scrolling layer will clip.
495     if (m_owningLayer->needsCompositedScrolling())
496         needsDescendentsClippingLayer = false;
497
498     if (updateClippingLayers(compositor->clippedByAncestor(m_owningLayer), needsDescendentsClippingLayer))
499         layerConfigChanged = true;
500
501     if (updateOverflowControlsLayers(requiresHorizontalScrollbarLayer(), requiresVerticalScrollbarLayer(), requiresScrollCornerLayer()))
502         layerConfigChanged = true;
503
504     if (updateScrollingLayers(m_owningLayer->needsCompositedScrolling()))
505         layerConfigChanged = true;
506
507     if (layerConfigChanged)
508         updateInternalHierarchy();
509
510     if (GraphicsLayer* flatteningLayer = tileCacheFlatteningLayer()) {
511         flatteningLayer->removeFromParent();
512         m_graphicsLayer->addChild(flatteningLayer);
513     }
514
515     if (updateMaskLayer(renderer->hasMask()))
516         m_graphicsLayer->setMaskLayer(m_maskLayer.get());
517
518     if (m_owningLayer->hasReflection()) {
519         if (m_owningLayer->reflectionLayer()->backing()) {
520             GraphicsLayer* reflectionLayer = m_owningLayer->reflectionLayer()->backing()->graphicsLayer();
521             m_graphicsLayer->setReplicatedByLayer(reflectionLayer);
522         }
523     } else
524         m_graphicsLayer->setReplicatedByLayer(0);
525
526     updateBackgroundColor(isSimpleContainerCompositingLayer());
527     updateRootLayerConfiguration();
528     
529     if (isDirectlyCompositedImage())
530         updateImageContents();
531
532     if (renderer->isEmbeddedObject() && toRenderEmbeddedObject(renderer)->allowsAcceleratedCompositing()) {
533         PluginViewBase* pluginViewBase = static_cast<PluginViewBase*>(toRenderWidget(renderer)->widget());
534         m_graphicsLayer->setContentsToMedia(pluginViewBase->platformLayer());
535     }
536 #if ENABLE(VIDEO)
537     else if (renderer->isVideo()) {
538         HTMLMediaElement* mediaElement = static_cast<HTMLMediaElement*>(renderer->node());
539         m_graphicsLayer->setContentsToMedia(mediaElement->platformLayer());
540     }
541 #endif
542 #if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
543     else if (isAcceleratedCanvas(renderer)) {
544         HTMLCanvasElement* canvas = static_cast<HTMLCanvasElement*>(renderer->node());
545         if (CanvasRenderingContext* context = canvas->renderingContext())
546             m_graphicsLayer->setContentsToCanvas(context->platformLayer());
547         layerConfigChanged = true;
548     }
549 #endif
550     if (renderer->isRenderPart())
551         layerConfigChanged = RenderLayerCompositor::parentFrameContentLayers(toRenderPart(renderer));
552
553     return layerConfigChanged;
554 }
555
556 static IntRect clipBox(RenderBox* renderer)
557 {
558     LayoutRect result = PaintInfo::infiniteRect();
559     if (renderer->hasOverflowClip())
560         result = renderer->overflowClipRect(LayoutPoint(), 0); // FIXME: Incorrect for CSS regions.
561
562     if (renderer->hasClip())
563         result.intersect(renderer->clipRect(LayoutPoint(), 0)); // FIXME: Incorrect for CSS regions.
564
565     return pixelSnappedIntRect(result);
566 }
567
568 void RenderLayerBacking::updateGraphicsLayerGeometry()
569 {
570     // If we haven't built z-order lists yet, wait until later.
571     if (m_owningLayer->isStackingContainer() && m_owningLayer->m_zOrderListsDirty)
572         return;
573
574     // Set transform property, if it is not animating. We have to do this here because the transform
575     // is affected by the layer dimensions.
576     if (!renderer()->animation()->isRunningAcceleratedAnimationOnRenderer(renderer(), CSSPropertyWebkitTransform))
577         updateTransform(renderer()->style());
578
579     // Set opacity, if it is not animating.
580     if (!renderer()->animation()->isRunningAcceleratedAnimationOnRenderer(renderer(), CSSPropertyOpacity))
581         updateOpacity(renderer()->style());
582         
583 #if ENABLE(CSS_FILTERS)
584     updateFilters(renderer()->style());
585 #endif
586
587 #if ENABLE(CSS_COMPOSITING)
588     updateLayerBlendMode(renderer()->style());
589 #endif
590
591     bool isSimpleContainer = isSimpleContainerCompositingLayer();
592     
593     m_owningLayer->updateDescendantDependentFlags();
594
595     // m_graphicsLayer is the corresponding GraphicsLayer for this RenderLayer and its non-compositing
596     // descendants. So, the visibility flag for m_graphicsLayer should be true if there are any
597     // non-compositing visible layers.
598     m_graphicsLayer->setContentsVisible(m_owningLayer->hasVisibleContent() || hasVisibleNonCompositingDescendantLayers());
599
600     RenderStyle* style = renderer()->style();
601     // FIXME: reflections should force transform-style to be flat in the style: https://bugs.webkit.org/show_bug.cgi?id=106959
602     bool preserves3D = style->transformStyle3D() == TransformStyle3DPreserve3D && !renderer()->hasReflection();
603     m_graphicsLayer->setPreserves3D(preserves3D);
604     m_graphicsLayer->setBackfaceVisibility(style->backfaceVisibility() == BackfaceVisibilityVisible);
605
606     RenderLayer* compAncestor = m_owningLayer->ancestorCompositingLayer();
607     
608     // We compute everything relative to the enclosing compositing layer.
609     IntRect ancestorCompositingBounds;
610     if (compAncestor) {
611         ASSERT(compAncestor->backing());
612         ancestorCompositingBounds = pixelSnappedIntRect(compAncestor->backing()->compositedBounds());
613     }
614
615     IntRect localCompositingBounds = pixelSnappedIntRect(compositedBounds());
616
617     IntRect relativeCompositingBounds(localCompositingBounds);
618     IntPoint delta;
619     m_owningLayer->convertToPixelSnappedLayerCoords(compAncestor, delta);
620     relativeCompositingBounds.moveBy(delta);
621
622     IntPoint graphicsLayerParentLocation;
623     if (compAncestor && compAncestor->backing()->hasClippingLayer()) {
624         // If the compositing ancestor has a layer to clip children, we parent in that, and therefore
625         // position relative to it.
626         IntRect clippingBox = clipBox(toRenderBox(compAncestor->renderer()));
627         graphicsLayerParentLocation = clippingBox.location();
628     } else if (compAncestor)
629         graphicsLayerParentLocation = ancestorCompositingBounds.location();
630     else
631         graphicsLayerParentLocation = renderer()->view()->documentRect().location();
632
633     if (compAncestor && compAncestor->needsCompositedScrolling()) {
634         RenderBox* renderBox = toRenderBox(compAncestor->renderer());
635         IntSize scrollOffset = compAncestor->scrolledContentOffset();
636         IntPoint scrollOrigin(renderBox->borderLeft(), renderBox->borderTop());
637         graphicsLayerParentLocation = scrollOrigin - scrollOffset;
638     }
639     
640     if (compAncestor && m_ancestorClippingLayer) {
641         // Call calculateRects to get the backgroundRect which is what is used to clip the contents of this
642         // layer. Note that we call it with temporaryClipRects = true because normally when computing clip rects
643         // for a compositing layer, rootLayer is the layer itself.
644         RenderLayer::ClipRectsContext clipRectsContext(compAncestor, 0, TemporaryClipRects, IgnoreOverlayScrollbarSize, IgnoreOverflowClip);
645         IntRect parentClipRect = pixelSnappedIntRect(m_owningLayer->backgroundClipRect(clipRectsContext).rect()); // FIXME: Incorrect for CSS regions.
646         ASSERT(parentClipRect != PaintInfo::infiniteRect());
647         m_ancestorClippingLayer->setPosition(FloatPoint(parentClipRect.location() - graphicsLayerParentLocation));
648         m_ancestorClippingLayer->setSize(parentClipRect.size());
649
650         // backgroundRect is relative to compAncestor, so subtract deltaX/deltaY to get back to local coords.
651         m_ancestorClippingLayer->setOffsetFromRenderer(parentClipRect.location() - delta);
652
653         // The primary layer is then parented in, and positioned relative to this clipping layer.
654         graphicsLayerParentLocation = parentClipRect.location();
655     }
656
657     FloatSize contentsSize = relativeCompositingBounds.size();
658     
659     if (m_contentsContainmentLayer) {
660         m_contentsContainmentLayer->setPreserves3D(preserves3D);
661         m_contentsContainmentLayer->setPosition(FloatPoint(relativeCompositingBounds.location() - graphicsLayerParentLocation));
662         // Use the same size as m_graphicsLayer so transforms behave correctly.
663         m_contentsContainmentLayer->setSize(contentsSize);
664         graphicsLayerParentLocation = relativeCompositingBounds.location();
665     }
666
667     m_graphicsLayer->setPosition(FloatPoint(relativeCompositingBounds.location() - graphicsLayerParentLocation));
668     m_graphicsLayer->setOffsetFromRenderer(toIntSize(localCompositingBounds.location()));
669     
670     FloatSize oldSize = m_graphicsLayer->size();
671     if (oldSize != contentsSize) {
672         m_graphicsLayer->setSize(contentsSize);
673         // Usually invalidation will happen via layout etc, but if we've affected the layer
674         // size by constraining relative to a clipping ancestor or the viewport, we
675         // have to invalidate to avoid showing stretched content.
676         if (m_boundsConstrainedByClipping)
677             m_graphicsLayer->setNeedsDisplay();
678     }
679     if (!m_isMainFrameRenderViewLayer)
680         m_graphicsLayer->setContentsOpaque(m_owningLayer->contentsOpaqueInRect(localCompositingBounds));
681
682     // If we have a layer that clips children, position it.
683     IntRect clippingBox;
684     if (GraphicsLayer* clipLayer = clippingLayer()) {
685         clippingBox = clipBox(toRenderBox(renderer()));
686         clipLayer->setPosition(FloatPoint(clippingBox.location() - localCompositingBounds.location()));
687         clipLayer->setSize(clippingBox.size());
688         clipLayer->setOffsetFromRenderer(toIntSize(clippingBox.location()));
689     }
690     
691     if (m_maskLayer) {
692         if (m_maskLayer->size() != m_graphicsLayer->size()) {
693             m_maskLayer->setSize(m_graphicsLayer->size());
694             m_maskLayer->setNeedsDisplay();
695         }
696         m_maskLayer->setPosition(FloatPoint());
697         m_maskLayer->setOffsetFromRenderer(m_graphicsLayer->offsetFromRenderer());
698     }
699     
700     if (m_owningLayer->hasTransform()) {
701         const IntRect borderBox = toRenderBox(renderer())->pixelSnappedBorderBoxRect();
702
703         // Get layout bounds in the coords of compAncestor to match relativeCompositingBounds.
704         IntRect layerBounds = IntRect(delta, borderBox.size());
705
706         // Update properties that depend on layer dimensions
707         FloatPoint3D transformOrigin = computeTransformOrigin(borderBox);
708         // Compute the anchor point, which is in the center of the renderer box unless transform-origin is set.
709         FloatPoint3D anchor(relativeCompositingBounds.width()  != 0.0f ? ((layerBounds.x() - relativeCompositingBounds.x()) + transformOrigin.x()) / relativeCompositingBounds.width()  : 0.5f,
710                             relativeCompositingBounds.height() != 0.0f ? ((layerBounds.y() - relativeCompositingBounds.y()) + transformOrigin.y()) / relativeCompositingBounds.height() : 0.5f,
711                             transformOrigin.z());
712         if (m_contentsContainmentLayer)
713             m_contentsContainmentLayer->setAnchorPoint(anchor);
714         else
715             m_graphicsLayer->setAnchorPoint(anchor);
716
717         RenderStyle* style = renderer()->style();
718         GraphicsLayer* clipLayer = clippingLayer();
719         if (style->hasPerspective()) {
720             TransformationMatrix t = owningLayer()->perspectiveTransform();
721             
722             if (clipLayer) {
723                 clipLayer->setChildrenTransform(t);
724                 m_graphicsLayer->setChildrenTransform(TransformationMatrix());
725             }
726             else
727                 m_graphicsLayer->setChildrenTransform(t);
728         } else {
729             if (clipLayer)
730                 clipLayer->setChildrenTransform(TransformationMatrix());
731             else
732                 m_graphicsLayer->setChildrenTransform(TransformationMatrix());
733         }
734     } else {
735         m_graphicsLayer->setAnchorPoint(FloatPoint3D(0.5f, 0.5f, 0));
736         if (m_contentsContainmentLayer)
737             m_contentsContainmentLayer->setAnchorPoint(FloatPoint3D(0.5f, 0.5f, 0));
738     }
739
740     if (m_foregroundLayer) {
741         FloatPoint foregroundPosition;
742         FloatSize foregroundSize = contentsSize;
743         IntSize foregroundOffset = m_graphicsLayer->offsetFromRenderer();
744         if (hasClippingLayer()) {
745             // If we have a clipping layer (which clips descendants), then the foreground layer is a child of it,
746             // so that it gets correctly sorted with children. In that case, position relative to the clipping layer.
747             foregroundSize = FloatSize(clippingBox.size());
748             foregroundOffset = toIntSize(clippingBox.location());
749         }
750
751         m_foregroundLayer->setPosition(foregroundPosition);
752         if (foregroundSize != m_foregroundLayer->size()) {
753             m_foregroundLayer->setSize(foregroundSize);
754             m_foregroundLayer->setNeedsDisplay();
755         }
756         m_foregroundLayer->setOffsetFromRenderer(foregroundOffset);
757     }
758
759     if (m_backgroundLayer) {
760         FloatPoint backgroundPosition;
761         FloatSize backgroundSize = contentsSize;
762         if (backgroundLayerPaintsFixedRootBackground()) {
763             FrameView* frameView = toRenderView(renderer())->frameView();
764             backgroundPosition = IntPoint(frameView->scrollOffsetForFixedPosition());
765             backgroundSize = frameView->visibleContentRect().size();
766         }
767         m_backgroundLayer->setPosition(backgroundPosition);
768         if (backgroundSize != m_backgroundLayer->size()) {
769             m_backgroundLayer->setSize(backgroundSize);
770             m_backgroundLayer->setNeedsDisplay();
771         }
772         m_backgroundLayer->setOffsetFromRenderer(m_graphicsLayer->offsetFromRenderer());
773     }
774
775     if (m_owningLayer->reflectionLayer() && m_owningLayer->reflectionLayer()->isComposited()) {
776         RenderLayerBacking* reflectionBacking = m_owningLayer->reflectionLayer()->backing();
777         reflectionBacking->updateGraphicsLayerGeometry();
778         
779         // The reflection layer has the bounds of m_owningLayer->reflectionLayer(),
780         // but the reflected layer is the bounds of this layer, so we need to position it appropriately.
781         FloatRect layerBounds = compositedBounds();
782         FloatRect reflectionLayerBounds = reflectionBacking->compositedBounds();
783         reflectionBacking->graphicsLayer()->setReplicatedLayerPosition(FloatPoint(layerBounds.location() - reflectionLayerBounds.location()));
784     }
785
786     if (m_scrollingLayer) {
787         ASSERT(m_scrollingContentsLayer);
788         RenderBox* renderBox = toRenderBox(renderer());
789         IntRect paddingBox(renderBox->borderLeft(), renderBox->borderTop(), renderBox->width() - renderBox->borderLeft() - renderBox->borderRight(), renderBox->height() - renderBox->borderTop() - renderBox->borderBottom());
790         IntSize scrollOffset = m_owningLayer->scrolledContentOffset();
791
792         m_scrollingLayer->setPosition(FloatPoint(paddingBox.location() - localCompositingBounds.location()));
793
794         m_scrollingLayer->setSize(paddingBox.size());
795         m_scrollingContentsLayer->setPosition(FloatPoint(-scrollOffset.width(), -scrollOffset.height()));
796
797         IntSize oldScrollingLayerOffset = m_scrollingLayer->offsetFromRenderer();
798         m_scrollingLayer->setOffsetFromRenderer(-toIntSize(paddingBox.location()));
799
800         bool paddingBoxOffsetChanged = oldScrollingLayerOffset != m_scrollingLayer->offsetFromRenderer();
801
802         IntSize scrollSize(m_owningLayer->scrollWidth(), m_owningLayer->scrollHeight());
803         if (scrollSize != m_scrollingContentsLayer->size() || paddingBoxOffsetChanged)
804             m_scrollingContentsLayer->setNeedsDisplay();
805
806         IntSize scrollingContentsOffset = toIntSize(paddingBox.location() - scrollOffset);
807         if (scrollingContentsOffset != m_scrollingContentsLayer->offsetFromRenderer() || scrollSize != m_scrollingContentsLayer->size())
808             compositor()->scrollingLayerDidChange(m_owningLayer);
809
810         m_scrollingContentsLayer->setSize(scrollSize);
811         // FIXME: The paint offset and the scroll offset should really be separate concepts.
812         m_scrollingContentsLayer->setOffsetFromRenderer(scrollingContentsOffset, GraphicsLayer::DontSetNeedsDisplay);
813     }
814
815     // If this layer was created just for clipping or to apply perspective, it doesn't need its own backing store.
816     setRequiresOwnBackingStore(compositor()->requiresOwnBackingStore(m_owningLayer, compAncestor));
817
818     updateContentsRect(isSimpleContainer);
819     updateBackgroundColor(isSimpleContainer);
820     updateDrawsContent(isSimpleContainer);
821     updateAfterWidgetResize();
822     registerScrollingLayers();
823 }
824
825 void RenderLayerBacking::registerScrollingLayers()
826 {
827     // Register fixed position layers and their containers with the scrolling coordinator.
828     ScrollingCoordinator* scrollingCoordinator = scrollingCoordinatorFromLayer(m_owningLayer);
829     if (!scrollingCoordinator)
830         return;
831
832     compositor()->updateViewportConstraintStatus(m_owningLayer);
833
834     // FIXME: it would be nice to avoid all this work if the platform doesn't implement setLayerIsFixedToContainerLayer().
835     if (renderer()->style()->position() == FixedPosition || compositor()->fixedPositionedByAncestor(m_owningLayer))
836         scrollingCoordinator->setLayerIsFixedToContainerLayer(childForSuperlayers(), true);
837     else {
838         if (m_ancestorClippingLayer)
839             scrollingCoordinator->setLayerIsFixedToContainerLayer(m_ancestorClippingLayer.get(), false);
840         scrollingCoordinator->setLayerIsFixedToContainerLayer(m_graphicsLayer.get(), false);
841     }
842     // Page scale is applied as a transform on the root render view layer. Because the scroll
843     // layer is further up in the hierarchy, we need to avoid marking the root render view
844     // layer as a container.
845     bool isContainer = m_owningLayer->hasTransform() && !m_owningLayer->isRootLayer();
846     scrollingCoordinator->setLayerIsContainerForFixedPositionLayers(childForSuperlayers(), isContainer);
847 }
848
849 void RenderLayerBacking::updateInternalHierarchy()
850 {
851     // m_foregroundLayer has to be inserted in the correct order with child layers,
852     // so it's not inserted here.
853     if (m_ancestorClippingLayer)
854         m_ancestorClippingLayer->removeAllChildren();
855     
856     if (m_contentsContainmentLayer) {
857         m_contentsContainmentLayer->removeAllChildren();
858         if (m_ancestorClippingLayer)
859             m_ancestorClippingLayer->addChild(m_contentsContainmentLayer.get());
860     }
861     
862     if (m_backgroundLayer)
863         m_contentsContainmentLayer->addChild(m_backgroundLayer.get());
864
865     m_graphicsLayer->removeFromParent();
866     if (m_contentsContainmentLayer)
867         m_contentsContainmentLayer->addChild(m_graphicsLayer.get());
868     else if (m_ancestorClippingLayer)
869         m_ancestorClippingLayer->addChild(m_graphicsLayer.get());
870
871     if (m_childContainmentLayer) {
872         m_childContainmentLayer->removeFromParent();
873         m_graphicsLayer->addChild(m_childContainmentLayer.get());
874     }
875
876     if (m_scrollingLayer) {
877         GraphicsLayer* superlayer = m_childContainmentLayer ? m_childContainmentLayer.get() : m_graphicsLayer.get();
878         m_scrollingLayer->removeFromParent();
879         superlayer->addChild(m_scrollingLayer.get());
880     }
881
882     // The clip for child layers does not include space for overflow controls, so they exist as
883     // siblings of the clipping layer if we have one. Normal children of this layer are set as
884     // children of the clipping layer.
885     if (m_layerForHorizontalScrollbar) {
886         m_layerForHorizontalScrollbar->removeFromParent();
887         m_graphicsLayer->addChild(m_layerForHorizontalScrollbar.get());
888     }
889     if (m_layerForVerticalScrollbar) {
890         m_layerForVerticalScrollbar->removeFromParent();
891         m_graphicsLayer->addChild(m_layerForVerticalScrollbar.get());
892     }
893     if (m_layerForScrollCorner) {
894         m_layerForScrollCorner->removeFromParent();
895         m_graphicsLayer->addChild(m_layerForScrollCorner.get());
896     }
897 }
898
899 void RenderLayerBacking::updateContentsRect(bool isSimpleContainer)
900 {
901     IntRect contentsRect;
902     if (isSimpleContainer && renderer()->hasBackground())
903         contentsRect = backgroundBox();
904     else
905         contentsRect = contentsBox();
906
907     m_graphicsLayer->setContentsRect(contentsRect);
908 }
909
910 void RenderLayerBacking::updateDrawsContent()
911 {
912     updateDrawsContent(isSimpleContainerCompositingLayer());
913 }
914
915 void RenderLayerBacking::updateDrawsContent(bool isSimpleContainer)
916 {
917     if (m_scrollingLayer) {
918         // We don't have to consider overflow controls, because we know that the scrollbars are drawn elsewhere.
919         // m_graphicsLayer only needs backing store if the non-scrolling parts (background, outlines, borders, shadows etc) need to paint.
920         // m_scrollingLayer never has backing store.
921         // m_scrollingContentsLayer only needs backing store if the scrolled contents need to paint.
922         bool hasNonScrollingPaintedContent = m_owningLayer->hasVisibleContent() && m_owningLayer->hasBoxDecorationsOrBackground();
923         m_graphicsLayer->setDrawsContent(hasNonScrollingPaintedContent);
924
925         bool hasScrollingPaintedContent = m_owningLayer->hasVisibleContent() && (renderer()->hasBackground() || paintsChildren());
926         m_scrollingContentsLayer->setDrawsContent(hasScrollingPaintedContent);
927         return;
928     }
929
930     bool hasPaintedContent = !isSimpleContainer && containsPaintedContent();
931
932     // FIXME: we could refine this to only allocate backing for one of these layers if possible.
933     m_graphicsLayer->setDrawsContent(hasPaintedContent);
934     if (m_foregroundLayer)
935         m_foregroundLayer->setDrawsContent(hasPaintedContent);
936
937     if (m_backgroundLayer)
938         m_backgroundLayer->setDrawsContent(hasPaintedContent);
939 }
940
941 // Return true if the layers changed.
942 bool RenderLayerBacking::updateClippingLayers(bool needsAncestorClip, bool needsDescendantClip)
943 {
944     bool layersChanged = false;
945
946     if (needsAncestorClip) {
947         if (!m_ancestorClippingLayer) {
948             m_ancestorClippingLayer = createGraphicsLayer("Ancestor clipping Layer");
949             m_ancestorClippingLayer->setMasksToBounds(true);
950             layersChanged = true;
951         }
952     } else if (m_ancestorClippingLayer) {
953         m_ancestorClippingLayer->removeFromParent();
954         m_ancestorClippingLayer = nullptr;
955         layersChanged = true;
956     }
957     
958     if (needsDescendantClip) {
959         if (!m_childContainmentLayer && !m_usingTiledCacheLayer) {
960             m_childContainmentLayer = createGraphicsLayer("Child clipping Layer");
961             m_childContainmentLayer->setMasksToBounds(true);
962             layersChanged = true;
963         }
964     } else if (hasClippingLayer()) {
965         m_childContainmentLayer->removeFromParent();
966         m_childContainmentLayer = nullptr;
967         layersChanged = true;
968     }
969     
970     return layersChanged;
971 }
972
973 void RenderLayerBacking::setBackgroundLayerPaintsFixedRootBackground(bool backgroundLayerPaintsFixedRootBackground)
974 {
975     m_backgroundLayerPaintsFixedRootBackground = backgroundLayerPaintsFixedRootBackground;
976 }
977
978 bool RenderLayerBacking::requiresHorizontalScrollbarLayer() const
979 {
980 #if !PLATFORM(CHROMIUM)
981     if (!m_owningLayer->hasOverlayScrollbars() && !m_owningLayer->needsCompositedScrolling())
982         return false;
983 #endif
984     return m_owningLayer->horizontalScrollbar();
985 }
986
987 bool RenderLayerBacking::requiresVerticalScrollbarLayer() const
988 {
989 #if !PLATFORM(CHROMIUM)
990     if (!m_owningLayer->hasOverlayScrollbars() && !m_owningLayer->needsCompositedScrolling())
991         return false;
992 #endif
993     return m_owningLayer->verticalScrollbar();
994 }
995
996 bool RenderLayerBacking::requiresScrollCornerLayer() const
997 {
998 #if !PLATFORM(CHROMIUM)
999     if (!m_owningLayer->hasOverlayScrollbars() && !m_owningLayer->needsCompositedScrolling())
1000         return false;
1001 #endif
1002     return !m_owningLayer->scrollCornerAndResizerRect().isEmpty();
1003 }
1004
1005 bool RenderLayerBacking::updateOverflowControlsLayers(bool needsHorizontalScrollbarLayer, bool needsVerticalScrollbarLayer, bool needsScrollCornerLayer)
1006 {
1007     bool horizontalScrollbarLayerChanged = false;
1008     if (needsHorizontalScrollbarLayer) {
1009         if (!m_layerForHorizontalScrollbar) {
1010             m_layerForHorizontalScrollbar = createGraphicsLayer("horizontal scrollbar");
1011             horizontalScrollbarLayerChanged = true;
1012         }
1013     } else if (m_layerForHorizontalScrollbar) {
1014         m_layerForHorizontalScrollbar.clear();
1015         horizontalScrollbarLayerChanged = true;
1016     }
1017
1018     bool verticalScrollbarLayerChanged = false;
1019     if (needsVerticalScrollbarLayer) {
1020         if (!m_layerForVerticalScrollbar) {
1021             m_layerForVerticalScrollbar = createGraphicsLayer("vertical scrollbar");
1022             verticalScrollbarLayerChanged = true;
1023         }
1024     } else if (m_layerForVerticalScrollbar) {
1025         m_layerForVerticalScrollbar.clear();
1026         verticalScrollbarLayerChanged = true;
1027     }
1028
1029     bool scrollCornerLayerChanged = false;
1030     if (needsScrollCornerLayer) {
1031         if (!m_layerForScrollCorner) {
1032             m_layerForScrollCorner = createGraphicsLayer("scroll corner");
1033             scrollCornerLayerChanged = true;
1034         }
1035     } else if (m_layerForScrollCorner) {
1036         m_layerForScrollCorner.clear();
1037         scrollCornerLayerChanged = true;
1038     }
1039
1040     if (ScrollingCoordinator* scrollingCoordinator = scrollingCoordinatorFromLayer(m_owningLayer)) {
1041         if (horizontalScrollbarLayerChanged)
1042             scrollingCoordinator->scrollableAreaScrollbarLayerDidChange(m_owningLayer, HorizontalScrollbar);
1043         if (verticalScrollbarLayerChanged)
1044             scrollingCoordinator->scrollableAreaScrollbarLayerDidChange(m_owningLayer, VerticalScrollbar);
1045     }
1046
1047     return horizontalScrollbarLayerChanged || verticalScrollbarLayerChanged || scrollCornerLayerChanged;
1048 }
1049
1050 void RenderLayerBacking::positionOverflowControlsLayers(const IntSize& offsetFromRoot)
1051 {
1052     IntSize offsetFromRenderer = m_graphicsLayer->offsetFromRenderer();
1053     if (GraphicsLayer* layer = layerForHorizontalScrollbar()) {
1054         Scrollbar* hBar = m_owningLayer->horizontalScrollbar();
1055         if (hBar) {
1056             layer->setPosition(hBar->frameRect().location() - offsetFromRoot - offsetFromRenderer);
1057             layer->setSize(hBar->frameRect().size());
1058             if (layer->hasContentsLayer())
1059                 layer->setContentsRect(IntRect(IntPoint(), hBar->frameRect().size()));
1060         }
1061         layer->setDrawsContent(hBar && !layer->hasContentsLayer());
1062     }
1063     
1064     if (GraphicsLayer* layer = layerForVerticalScrollbar()) {
1065         Scrollbar* vBar = m_owningLayer->verticalScrollbar();
1066         if (vBar) {
1067             layer->setPosition(vBar->frameRect().location() - offsetFromRoot - offsetFromRenderer);
1068             layer->setSize(vBar->frameRect().size());
1069             if (layer->hasContentsLayer())
1070                 layer->setContentsRect(IntRect(IntPoint(), vBar->frameRect().size()));
1071         }
1072         layer->setDrawsContent(vBar && !layer->hasContentsLayer());
1073     }
1074
1075     if (GraphicsLayer* layer = layerForScrollCorner()) {
1076         const LayoutRect& scrollCornerAndResizer = m_owningLayer->scrollCornerAndResizerRect();
1077         layer->setPosition(scrollCornerAndResizer.location() - offsetFromRenderer);
1078         layer->setSize(scrollCornerAndResizer.size());
1079         layer->setDrawsContent(!scrollCornerAndResizer.isEmpty());
1080     }
1081 }
1082
1083 bool RenderLayerBacking::hasUnpositionedOverflowControlsLayers() const
1084 {
1085     if (GraphicsLayer* layer = layerForHorizontalScrollbar())
1086         if (!layer->drawsContent())
1087             return true;
1088
1089     if (GraphicsLayer* layer = layerForVerticalScrollbar())
1090         if (!layer->drawsContent())
1091             return true;
1092
1093     if (GraphicsLayer* layer = layerForScrollCorner())
1094         if (!layer->drawsContent())
1095             return true;
1096
1097     return false;
1098 }
1099
1100 bool RenderLayerBacking::updateForegroundLayer(bool needsForegroundLayer)
1101 {
1102     bool layerChanged = false;
1103     if (needsForegroundLayer) {
1104         if (!m_foregroundLayer) {
1105             String layerName;
1106 #ifndef NDEBUG
1107             layerName = m_owningLayer->name() + " (foreground)";
1108 #endif
1109             m_foregroundLayer = createGraphicsLayer(layerName);
1110             m_foregroundLayer->setDrawsContent(true);
1111             m_foregroundLayer->setPaintingPhase(GraphicsLayerPaintForeground);
1112             layerChanged = true;
1113         }
1114     } else if (m_foregroundLayer) {
1115         m_foregroundLayer->removeFromParent();
1116         m_foregroundLayer = nullptr;
1117         layerChanged = true;
1118     }
1119
1120     if (layerChanged)
1121         m_graphicsLayer->setPaintingPhase(paintingPhaseForPrimaryLayer());
1122
1123     return layerChanged;
1124 }
1125
1126 bool RenderLayerBacking::updateBackgroundLayer(bool needsBackgroundLayer)
1127 {
1128     bool layerChanged = false;
1129     if (needsBackgroundLayer) {
1130         if (!m_backgroundLayer) {
1131             String layerName;
1132 #ifndef NDEBUG
1133             layerName = m_owningLayer->name() + " (background)";
1134 #endif
1135             m_backgroundLayer = createGraphicsLayer(layerName);
1136             m_backgroundLayer->setDrawsContent(true);
1137             m_backgroundLayer->setAnchorPoint(FloatPoint3D());
1138             m_backgroundLayer->setPaintingPhase(GraphicsLayerPaintBackground);
1139             layerChanged = true;
1140         }
1141         
1142         if (!m_contentsContainmentLayer) {
1143             String layerName;
1144 #ifndef NDEBUG
1145             layerName = m_owningLayer->name() + " (contents containment)";
1146 #endif
1147             m_contentsContainmentLayer = createGraphicsLayer(layerName);
1148             m_contentsContainmentLayer->setAppliesPageScale(true);
1149             m_graphicsLayer->setAppliesPageScale(false);
1150             layerChanged = true;
1151         }
1152     } else {
1153         if (m_backgroundLayer) {
1154             m_backgroundLayer->removeFromParent();
1155             m_backgroundLayer = nullptr;
1156             layerChanged = true;
1157         }
1158         if (m_contentsContainmentLayer) {
1159             m_contentsContainmentLayer->removeFromParent();
1160             m_contentsContainmentLayer = nullptr;
1161             layerChanged = true;
1162             m_graphicsLayer->setAppliesPageScale(true);
1163         }
1164     }
1165     
1166     if (layerChanged) {
1167         // This assumes that the background layer is only used for fixed backgrounds, which is currently a correct assumption.
1168         if (renderer()->view())
1169             compositor()->fixedRootBackgroundLayerChanged();
1170     }
1171     
1172     return layerChanged;
1173 }
1174
1175 bool RenderLayerBacking::updateMaskLayer(bool needsMaskLayer)
1176 {
1177     bool layerChanged = false;
1178     if (needsMaskLayer) {
1179         if (!m_maskLayer) {
1180             m_maskLayer = createGraphicsLayer("Mask");
1181             m_maskLayer->setDrawsContent(true);
1182             m_maskLayer->setPaintingPhase(GraphicsLayerPaintMask);
1183             layerChanged = true;
1184         }
1185     } else if (m_maskLayer) {
1186         m_maskLayer = nullptr;
1187         layerChanged = true;
1188     }
1189
1190     if (layerChanged)
1191         m_graphicsLayer->setPaintingPhase(paintingPhaseForPrimaryLayer());
1192
1193     return layerChanged;
1194 }
1195
1196 bool RenderLayerBacking::updateScrollingLayers(bool needsScrollingLayers)
1197 {
1198     ScrollingCoordinator* scrollingCoordinator = scrollingCoordinatorFromLayer(m_owningLayer);
1199
1200     bool layerChanged = false;
1201     if (needsScrollingLayers) {
1202         if (!m_scrollingLayer) {
1203             // Outer layer which corresponds with the scroll view.
1204             m_scrollingLayer = createGraphicsLayer("Scrolling container");
1205             m_scrollingLayer->setDrawsContent(false);
1206             m_scrollingLayer->setMasksToBounds(true);
1207
1208             // Inner layer which renders the content that scrolls.
1209             m_scrollingContentsLayer = createGraphicsLayer("Scrolled Contents");
1210             m_scrollingContentsLayer->setDrawsContent(true);
1211             m_scrollingContentsLayer->setPaintingPhase(GraphicsLayerPaintForeground | GraphicsLayerPaintOverflowContents);
1212             m_scrollingLayer->addChild(m_scrollingContentsLayer.get());
1213
1214             layerChanged = true;
1215             if (scrollingCoordinator)
1216                 scrollingCoordinator->scrollableAreaScrollLayerDidChange(m_owningLayer);
1217         }
1218     } else if (m_scrollingLayer) {
1219         m_scrollingLayer = nullptr;
1220         m_scrollingContentsLayer = nullptr;
1221         layerChanged = true;
1222         if (scrollingCoordinator)
1223             scrollingCoordinator->scrollableAreaScrollLayerDidChange(m_owningLayer);
1224     }
1225
1226     if (layerChanged) {
1227         updateInternalHierarchy();
1228         m_graphicsLayer->setPaintingPhase(paintingPhaseForPrimaryLayer());
1229         m_graphicsLayer->setNeedsDisplay();
1230         if (renderer()->view())
1231             compositor()->scrollingLayerDidChange(m_owningLayer);
1232     }
1233
1234     return layerChanged;
1235 }
1236
1237 void RenderLayerBacking::attachToScrollingCoordinatorWithParent(RenderLayerBacking* parent)
1238 {
1239     ScrollingCoordinator* scrollingCoordinator = scrollingCoordinatorFromLayer(m_owningLayer);
1240     if (!scrollingCoordinator)
1241         return;
1242
1243     // FIXME: When we support overflow areas, we will have to refine this for overflow areas that are also
1244     // positon:fixed.
1245     ScrollingNodeType nodeType;
1246     if (renderer()->style()->position() == FixedPosition)
1247         nodeType = FixedNode;
1248     else if (renderer()->style()->position() == StickyPosition)
1249         nodeType = StickyNode;
1250     else
1251         nodeType = ScrollingNode;
1252
1253     ScrollingNodeID parentID = parent ? parent->scrollLayerID() : 0;
1254     m_scrollLayerID = scrollingCoordinator->attachToStateTree(nodeType, m_scrollLayerID ? m_scrollLayerID : scrollingCoordinator->uniqueScrollLayerID(), parentID);
1255 }
1256
1257 void RenderLayerBacking::detachFromScrollingCoordinator()
1258 {
1259     // If m_scrollLayerID is 0, then this backing is not attached to the ScrollingCoordinator.
1260     if (!m_scrollLayerID)
1261         return;
1262
1263     ScrollingCoordinator* scrollingCoordinator = scrollingCoordinatorFromLayer(m_owningLayer);
1264     if (!scrollingCoordinator)
1265         return;
1266
1267     scrollingCoordinator->detachFromStateTree(m_scrollLayerID);
1268     m_scrollLayerID = 0;
1269 }
1270
1271 GraphicsLayerPaintingPhase RenderLayerBacking::paintingPhaseForPrimaryLayer() const
1272 {
1273     unsigned phase = 0;
1274     if (!m_backgroundLayer)
1275         phase |= GraphicsLayerPaintBackground;
1276     if (!m_foregroundLayer)
1277         phase |= GraphicsLayerPaintForeground;
1278     if (!m_maskLayer)
1279         phase |= GraphicsLayerPaintMask;
1280
1281     if (m_scrollingContentsLayer)
1282         phase &= ~GraphicsLayerPaintForeground;
1283
1284     return static_cast<GraphicsLayerPaintingPhase>(phase);
1285 }
1286
1287 float RenderLayerBacking::compositingOpacity(float rendererOpacity) const
1288 {
1289     float finalOpacity = rendererOpacity;
1290     
1291     for (RenderLayer* curr = m_owningLayer->parent(); curr; curr = curr->parent()) {
1292         // We only care about parents that are stacking contexts.
1293         // Recall that opacity creates stacking context.
1294         if (!curr->isStackingContainer())
1295             continue;
1296         
1297         // If we found a compositing layer, we want to compute opacity
1298         // relative to it. So we can break here.
1299         if (curr->isComposited())
1300             break;
1301         
1302         finalOpacity *= curr->renderer()->opacity();
1303     }
1304
1305     return finalOpacity;
1306 }
1307
1308 static bool hasBoxDecorations(const RenderStyle* style)
1309 {
1310     return style->hasBorder() || style->hasBorderRadius() || style->hasOutline() || style->hasAppearance() || style->boxShadow() || style->hasFilter();
1311 }
1312
1313 static bool hasBoxDecorationsOrBackgroundImage(const RenderStyle* style)
1314 {
1315     return hasBoxDecorations(style) || style->hasBackgroundImage();
1316 }
1317
1318 Color RenderLayerBacking::rendererBackgroundColor() const
1319 {
1320     RenderObject* backgroundRenderer = renderer();
1321     if (backgroundRenderer->isRoot())
1322         backgroundRenderer = backgroundRenderer->rendererForRootBackground();
1323
1324     return backgroundRenderer->style()->visitedDependentColor(CSSPropertyBackgroundColor);
1325 }
1326
1327 void RenderLayerBacking::updateBackgroundColor(bool isSimpleContainer)
1328 {
1329     Color backgroundColor;
1330     if (isSimpleContainer)
1331         backgroundColor = rendererBackgroundColor();
1332
1333     // An unset (invalid) color will remove the solid color.
1334     m_graphicsLayer->setContentsToSolidColor(backgroundColor);
1335 }
1336
1337 void RenderLayerBacking::updateRootLayerConfiguration()
1338 {
1339     if (!m_usingTiledCacheLayer)
1340         return;
1341
1342     Color backgroundColor;
1343     bool viewIsTransparent = compositor()->viewHasTransparentBackground(&backgroundColor);
1344
1345     if (m_backgroundLayerPaintsFixedRootBackground && m_backgroundLayer) {
1346         m_backgroundLayer->setBackgroundColor(backgroundColor);
1347         m_backgroundLayer->setContentsOpaque(!viewIsTransparent);
1348
1349         m_graphicsLayer->setBackgroundColor(Color());
1350         m_graphicsLayer->setContentsOpaque(false);
1351     } else {
1352         m_graphicsLayer->setBackgroundColor(backgroundColor);
1353         m_graphicsLayer->setContentsOpaque(!viewIsTransparent);
1354     }
1355 }
1356
1357 static bool supportsDirectBoxDecorationsComposition(const RenderObject* renderer)
1358 {
1359     if (!GraphicsLayer::supportsBackgroundColorContent())
1360         return false;
1361
1362     if (renderer->hasClip())
1363         return false;
1364
1365     if (hasBoxDecorationsOrBackgroundImage(renderer->style()))
1366         return false;
1367
1368     // FIXME: we should be able to allow backgroundComposite; However since this is not a common use case it has been deferred for now.
1369     if (renderer->style()->backgroundComposite() != CompositeSourceOver)
1370         return false;
1371
1372     if (renderer->style()->backgroundClip() == TextFillBox)
1373         return false;
1374
1375     return true;
1376 }
1377
1378 bool RenderLayerBacking::paintsBoxDecorations() const
1379 {
1380     if (!m_owningLayer->hasVisibleBoxDecorations())
1381         return false;
1382
1383     if (!supportsDirectBoxDecorationsComposition(renderer()))
1384         return true;
1385
1386     return false;
1387 }
1388
1389 bool RenderLayerBacking::paintsChildren() const
1390 {
1391     if (m_owningLayer->hasVisibleContent() && m_owningLayer->hasNonEmptyChildRenderers())
1392         return true;
1393         
1394     if (hasVisibleNonCompositingDescendantLayers())
1395         return true;
1396
1397     return false;
1398 }
1399
1400 static bool isCompositedPlugin(RenderObject* renderer)
1401 {
1402     return renderer->isEmbeddedObject() && toRenderEmbeddedObject(renderer)->allowsAcceleratedCompositing();
1403 }
1404
1405 // A "simple container layer" is a RenderLayer which has no visible content to render.
1406 // It may have no children, or all its children may be themselves composited.
1407 // This is a useful optimization, because it allows us to avoid allocating backing store.
1408 bool RenderLayerBacking::isSimpleContainerCompositingLayer() const
1409 {
1410     RenderObject* renderObject = renderer();
1411     if (renderObject->hasMask()) // masks require special treatment
1412         return false;
1413
1414     if (renderObject->isReplaced() && !isCompositedPlugin(renderObject))
1415         return false;
1416     
1417     if (paintsBoxDecorations() || paintsChildren())
1418         return false;
1419
1420     if (renderObject->isRenderRegion())
1421         return false;
1422
1423     if (renderObject->node() && renderObject->node()->isDocumentNode()) {
1424         // Look to see if the root object has a non-simple background
1425         RenderObject* rootObject = renderObject->document()->documentElement() ? renderObject->document()->documentElement()->renderer() : 0;
1426         if (!rootObject)
1427             return false;
1428         
1429         RenderStyle* style = rootObject->style();
1430         
1431         // Reject anything that has a border, a border-radius or outline,
1432         // or is not a simple background (no background, or solid color).
1433         if (hasBoxDecorationsOrBackgroundImage(style))
1434             return false;
1435         
1436         // Now look at the body's renderer.
1437         HTMLElement* body = renderObject->document()->body();
1438         RenderObject* bodyObject = (body && body->hasLocalName(bodyTag)) ? body->renderer() : 0;
1439         if (!bodyObject)
1440             return false;
1441         
1442         style = bodyObject->style();
1443         
1444         if (hasBoxDecorationsOrBackgroundImage(style))
1445             return false;
1446     }
1447
1448     return true;
1449 }
1450
1451 static bool hasVisibleNonCompositingDescendant(RenderLayer* parent)
1452 {
1453     // FIXME: We shouldn't be called with a stale z-order lists. See bug 85512.
1454     parent->updateLayerListsIfNeeded();
1455
1456 #if !ASSERT_DISABLED
1457     LayerListMutationDetector mutationChecker(parent);
1458 #endif
1459
1460     if (Vector<RenderLayer*>* normalFlowList = parent->normalFlowList()) {
1461         size_t listSize = normalFlowList->size();
1462         for (size_t i = 0; i < listSize; ++i) {
1463             RenderLayer* curLayer = normalFlowList->at(i);
1464             if (!curLayer->isComposited()
1465                 && (curLayer->hasVisibleContent() || hasVisibleNonCompositingDescendant(curLayer)))
1466                 return true;
1467         }
1468     }
1469
1470     if (parent->isStackingContainer()) {
1471         if (!parent->hasVisibleDescendant())
1472             return false;
1473
1474         // Use the m_hasCompositingDescendant bit to optimize?
1475         if (Vector<RenderLayer*>* negZOrderList = parent->negZOrderList()) {
1476             size_t listSize = negZOrderList->size();
1477             for (size_t i = 0; i < listSize; ++i) {
1478                 RenderLayer* curLayer = negZOrderList->at(i);
1479                 if (!curLayer->isComposited()
1480                     && (curLayer->hasVisibleContent() || hasVisibleNonCompositingDescendant(curLayer)))
1481                     return true;
1482             }
1483         }
1484
1485         if (Vector<RenderLayer*>* posZOrderList = parent->posZOrderList()) {
1486             size_t listSize = posZOrderList->size();
1487             for (size_t i = 0; i < listSize; ++i) {
1488                 RenderLayer* curLayer = posZOrderList->at(i);
1489                 if (!curLayer->isComposited()
1490                     && (curLayer->hasVisibleContent() || hasVisibleNonCompositingDescendant(curLayer)))
1491                     return true;
1492             }
1493         }
1494     }
1495
1496     return false;
1497 }
1498
1499 // Conservative test for having no rendered children.
1500 bool RenderLayerBacking::hasVisibleNonCompositingDescendantLayers() const
1501 {
1502     return hasVisibleNonCompositingDescendant(m_owningLayer);
1503 }
1504
1505 bool RenderLayerBacking::containsPaintedContent() const
1506 {
1507     if (isSimpleContainerCompositingLayer() || paintsIntoWindow() || paintsIntoCompositedAncestor() || m_artificiallyInflatedBounds || m_owningLayer->isReflection())
1508         return false;
1509
1510     if (isDirectlyCompositedImage())
1511         return false;
1512
1513     // FIXME: we could optimize cases where the image, video or canvas is known to fill the border box entirely,
1514     // and set background color on the layer in that case, instead of allocating backing store and painting.
1515 #if ENABLE(VIDEO)
1516     if (renderer()->isVideo() && toRenderVideo(renderer())->shouldDisplayVideo())
1517         return m_owningLayer->hasBoxDecorationsOrBackground();
1518 #endif
1519 #if PLATFORM(MAC) && USE(CA) && (PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070)
1520 #elif ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
1521     if (isAcceleratedCanvas(renderer()))
1522         return m_owningLayer->hasBoxDecorationsOrBackground();
1523 #endif
1524
1525     return true;
1526 }
1527
1528 // An image can be directly compositing if it's the sole content of the layer, and has no box decorations
1529 // that require painting. Direct compositing saves backing store.
1530 bool RenderLayerBacking::isDirectlyCompositedImage() const
1531 {
1532     RenderObject* renderObject = renderer();
1533     
1534     if (!renderObject->isImage() || m_owningLayer->hasBoxDecorationsOrBackground() || renderObject->hasClip())
1535         return false;
1536
1537     RenderImage* imageRenderer = toRenderImage(renderObject);
1538     if (CachedImage* cachedImage = imageRenderer->cachedImage()) {
1539         if (!cachedImage->hasImage())
1540             return false;
1541
1542         Image* image = cachedImage->imageForRenderer(imageRenderer);
1543         if (!image->isBitmapImage())
1544             return false;
1545
1546         return m_graphicsLayer->shouldDirectlyCompositeImage(image);
1547     }
1548
1549     return false;
1550 }
1551
1552 void RenderLayerBacking::contentChanged(ContentChangeType changeType)
1553 {
1554     if ((changeType == ImageChanged) && isDirectlyCompositedImage()) {
1555         updateImageContents();
1556         return;
1557     }
1558
1559     if ((changeType == MaskImageChanged) && m_maskLayer) {
1560         // The composited layer bounds relies on box->maskClipRect(), which changes
1561         // when the mask image becomes available.
1562         updateAfterLayout(CompositingChildrenOnly | IsUpdateRoot);
1563     }
1564
1565 #if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
1566     if ((changeType == CanvasChanged || changeType == CanvasPixelsChanged) && isAcceleratedCanvas(renderer())) {
1567         m_graphicsLayer->setContentsNeedsDisplay();
1568         return;
1569     }
1570 #endif
1571 }
1572
1573 void RenderLayerBacking::updateImageContents()
1574 {
1575     ASSERT(renderer()->isImage());
1576     RenderImage* imageRenderer = toRenderImage(renderer());
1577
1578     CachedImage* cachedImage = imageRenderer->cachedImage();
1579     if (!cachedImage)
1580         return;
1581
1582     Image* image = cachedImage->imageForRenderer(imageRenderer);
1583     if (!image)
1584         return;
1585
1586     // We have to wait until the image is fully loaded before setting it on the layer.
1587     if (!cachedImage->isLoaded())
1588         return;
1589
1590     // This is a no-op if the layer doesn't have an inner layer for the image.
1591     m_graphicsLayer->setContentsToImage(image);
1592     bool isSimpleContainer = false;
1593     updateDrawsContent(isSimpleContainer);
1594     
1595     // Image animation is "lazy", in that it automatically stops unless someone is drawing
1596     // the image. So we have to kick the animation each time; this has the downside that the
1597     // image will keep animating, even if its layer is not visible.
1598     image->startAnimation();
1599 }
1600
1601 FloatPoint3D RenderLayerBacking::computeTransformOrigin(const IntRect& borderBox) const
1602 {
1603     RenderStyle* style = renderer()->style();
1604
1605     FloatPoint3D origin;
1606     origin.setX(floatValueForLength(style->transformOriginX(), borderBox.width()));
1607     origin.setY(floatValueForLength(style->transformOriginY(), borderBox.height()));
1608     origin.setZ(style->transformOriginZ());
1609
1610     return origin;
1611 }
1612
1613 FloatPoint RenderLayerBacking::computePerspectiveOrigin(const IntRect& borderBox) const
1614 {
1615     RenderStyle* style = renderer()->style();
1616
1617     float boxWidth = borderBox.width();
1618     float boxHeight = borderBox.height();
1619
1620     FloatPoint origin;
1621     origin.setX(floatValueForLength(style->perspectiveOriginX(), boxWidth));
1622     origin.setY(floatValueForLength(style->perspectiveOriginY(), boxHeight));
1623
1624     return origin;
1625 }
1626
1627 // Return the offset from the top-left of this compositing layer at which the renderer's contents are painted.
1628 IntSize RenderLayerBacking::contentOffsetInCompostingLayer() const
1629 {
1630     return IntSize(-m_compositedBounds.x(), -m_compositedBounds.y());
1631 }
1632
1633 IntRect RenderLayerBacking::contentsBox() const
1634 {
1635     if (!renderer()->isBox())
1636         return IntRect();
1637
1638     IntRect contentsRect;
1639 #if ENABLE(VIDEO)
1640     if (renderer()->isVideo()) {
1641         RenderVideo* videoRenderer = toRenderVideo(renderer());
1642         contentsRect = videoRenderer->videoBox();
1643     } else
1644 #endif
1645         contentsRect = pixelSnappedIntRect(toRenderBox(renderer())->contentBoxRect());
1646
1647     contentsRect.move(contentOffsetInCompostingLayer());
1648     return contentsRect;
1649 }
1650
1651 static LayoutRect backgroundRectForBox(const RenderBox* box)
1652 {
1653     EFillBox clip = box->style()->backgroundClip();
1654     switch (clip) {
1655     case BorderFillBox:
1656         return box->borderBoxRect();
1657     case PaddingFillBox:
1658         return box->paddingBoxRect();
1659     case ContentFillBox:
1660         return box->contentBoxRect();
1661     case TextFillBox:
1662         break;
1663     }
1664
1665     ASSERT_NOT_REACHED();
1666     return LayoutRect();
1667 }
1668
1669 IntRect RenderLayerBacking::backgroundBox() const
1670 {
1671     if (!renderer()->isBox())
1672         return IntRect();
1673
1674     IntRect pixelSnappedBackgroundBox = pixelSnappedIntRect(backgroundRectForBox(toRenderBox(renderer())));
1675     pixelSnappedBackgroundBox.move(contentOffsetInCompostingLayer());
1676     return pixelSnappedBackgroundBox;
1677 }
1678
1679 GraphicsLayer* RenderLayerBacking::parentForSublayers() const
1680 {
1681     if (m_scrollingContentsLayer)
1682         return m_scrollingContentsLayer.get();
1683
1684     return m_childContainmentLayer ? m_childContainmentLayer.get() : m_graphicsLayer.get();
1685 }
1686
1687 GraphicsLayer* RenderLayerBacking::childForSuperlayers() const
1688 {
1689     if (m_ancestorClippingLayer)
1690         return m_ancestorClippingLayer.get();
1691
1692     if (m_contentsContainmentLayer)
1693         return m_contentsContainmentLayer.get();
1694     
1695     return m_graphicsLayer.get();
1696 }
1697
1698 bool RenderLayerBacking::paintsIntoWindow() const
1699 {
1700     if (m_usingTiledCacheLayer)
1701         return false;
1702
1703     if (m_owningLayer->isRootLayer()) {
1704 #if PLATFORM(BLACKBERRY)
1705         if (compositor()->inForcedCompositingMode())
1706             return false;
1707 #endif
1708
1709         return compositor()->rootLayerAttachment() != RenderLayerCompositor::RootLayerAttachedViaEnclosingFrame;
1710     }
1711     
1712     return false;
1713 }
1714
1715 void RenderLayerBacking::setRequiresOwnBackingStore(bool requiresOwnBacking)
1716 {
1717     if (requiresOwnBacking == m_requiresOwnBackingStore)
1718         return;
1719     
1720     m_requiresOwnBackingStore = requiresOwnBacking;
1721
1722     // This affects the answer to paintsIntoCompositedAncestor(), which in turn affects
1723     // cached clip rects, so when it changes we have to clear clip rects on descendants.
1724     m_owningLayer->clearClipRectsIncludingDescendants(PaintingClipRects);
1725     m_owningLayer->computeRepaintRectsIncludingDescendants();
1726     
1727     compositor()->repaintInCompositedAncestor(m_owningLayer, compositedBounds());
1728 }
1729
1730 #if ENABLE(CSS_COMPOSITING)
1731 void RenderLayerBacking::setBlendMode(BlendMode)
1732 {
1733 }
1734 #endif
1735
1736 void RenderLayerBacking::setContentsNeedDisplay()
1737 {
1738     ASSERT(!paintsIntoCompositedAncestor());
1739     
1740     if (m_graphicsLayer && m_graphicsLayer->drawsContent())
1741         m_graphicsLayer->setNeedsDisplay();
1742     
1743     if (m_foregroundLayer && m_foregroundLayer->drawsContent())
1744         m_foregroundLayer->setNeedsDisplay();
1745
1746     if (m_backgroundLayer && m_backgroundLayer->drawsContent())
1747         m_backgroundLayer->setNeedsDisplay();
1748
1749     if (m_maskLayer && m_maskLayer->drawsContent())
1750         m_maskLayer->setNeedsDisplay();
1751
1752     if (m_scrollingContentsLayer && m_scrollingContentsLayer->drawsContent())
1753         m_scrollingContentsLayer->setNeedsDisplay();
1754 }
1755
1756 // r is in the coordinate space of the layer's render object
1757 void RenderLayerBacking::setContentsNeedDisplayInRect(const IntRect& r)
1758 {
1759     ASSERT(!paintsIntoCompositedAncestor());
1760
1761     if (m_graphicsLayer && m_graphicsLayer->drawsContent()) {
1762         IntRect layerDirtyRect = r;
1763         layerDirtyRect.move(-m_graphicsLayer->offsetFromRenderer());
1764         m_graphicsLayer->setNeedsDisplayInRect(layerDirtyRect);
1765     }
1766
1767     if (m_foregroundLayer && m_foregroundLayer->drawsContent()) {
1768         IntRect layerDirtyRect = r;
1769         layerDirtyRect.move(-m_foregroundLayer->offsetFromRenderer());
1770         m_foregroundLayer->setNeedsDisplayInRect(layerDirtyRect);
1771     }
1772
1773     // FIXME: need to split out repaints for the background.
1774     if (m_backgroundLayer && m_backgroundLayer->drawsContent()) {
1775         IntRect layerDirtyRect = r;
1776         layerDirtyRect.move(-m_backgroundLayer->offsetFromRenderer());
1777         m_backgroundLayer->setNeedsDisplayInRect(layerDirtyRect);
1778     }
1779
1780     if (m_maskLayer && m_maskLayer->drawsContent()) {
1781         IntRect layerDirtyRect = r;
1782         layerDirtyRect.move(-m_maskLayer->offsetFromRenderer());
1783         m_maskLayer->setNeedsDisplayInRect(layerDirtyRect);
1784     }
1785
1786     if (m_scrollingContentsLayer && m_scrollingContentsLayer->drawsContent()) {
1787         IntRect layerDirtyRect = r;
1788         layerDirtyRect.move(-m_scrollingContentsLayer->offsetFromRenderer());
1789         m_scrollingContentsLayer->setNeedsDisplayInRect(layerDirtyRect);
1790     }
1791 }
1792
1793 void RenderLayerBacking::paintIntoLayer(const GraphicsLayer* graphicsLayer, GraphicsContext* context,
1794                     const IntRect& paintDirtyRect, // In the coords of rootLayer.
1795                     PaintBehavior paintBehavior, GraphicsLayerPaintingPhase paintingPhase)
1796 {
1797     if (paintsIntoWindow() || paintsIntoCompositedAncestor()) {
1798         ASSERT_NOT_REACHED();
1799         return;
1800     }
1801
1802     FontCachePurgePreventer fontCachePurgePreventer;
1803     
1804     RenderLayer::PaintLayerFlags paintFlags = 0;
1805     if (paintingPhase & GraphicsLayerPaintBackground)
1806         paintFlags |= RenderLayer::PaintLayerPaintingCompositingBackgroundPhase;
1807     if (paintingPhase & GraphicsLayerPaintForeground)
1808         paintFlags |= RenderLayer::PaintLayerPaintingCompositingForegroundPhase;
1809     if (paintingPhase & GraphicsLayerPaintMask)
1810         paintFlags |= RenderLayer::PaintLayerPaintingCompositingMaskPhase;
1811     if (paintingPhase & GraphicsLayerPaintOverflowContents)
1812         paintFlags |= RenderLayer::PaintLayerPaintingOverflowContents;
1813
1814     if (graphicsLayer == m_backgroundLayer)
1815         paintFlags |= (RenderLayer::PaintLayerPaintingRootBackgroundOnly | RenderLayer::PaintLayerPaintingCompositingForegroundPhase); // Need PaintLayerPaintingCompositingForegroundPhase to walk child layers.
1816     else if (m_backgroundLayer)
1817         paintFlags |= RenderLayer::PaintLayerPaintingSkipRootBackground;
1818     
1819     // FIXME: GraphicsLayers need a way to split for RenderRegions.
1820     RenderLayer::LayerPaintingInfo paintingInfo(m_owningLayer, paintDirtyRect, paintBehavior, LayoutSize());
1821     m_owningLayer->paintLayerContents(context, paintingInfo, paintFlags);
1822
1823     if (m_owningLayer->containsDirtyOverlayScrollbars())
1824         m_owningLayer->paintLayerContents(context, paintingInfo, paintFlags | RenderLayer::PaintLayerPaintingOverlayScrollbars);
1825
1826     ASSERT(!m_owningLayer->m_usedTransparency);
1827 }
1828
1829 static void paintScrollbar(Scrollbar* scrollbar, GraphicsContext& context, const IntRect& clip)
1830 {
1831     if (!scrollbar)
1832         return;
1833
1834     context.save();
1835     const IntRect& scrollbarRect = scrollbar->frameRect();
1836     context.translate(-scrollbarRect.x(), -scrollbarRect.y());
1837     IntRect transformedClip = clip;
1838     transformedClip.moveBy(scrollbarRect.location());
1839     scrollbar->paint(&context, transformedClip);
1840     context.restore();
1841 }
1842
1843 // Up-call from compositing layer drawing callback.
1844 void RenderLayerBacking::paintContents(const GraphicsLayer* graphicsLayer, GraphicsContext& context, GraphicsLayerPaintingPhase paintingPhase, const IntRect& clip)
1845 {
1846 #ifndef NDEBUG
1847     if (Page* page = renderer()->frame()->page())
1848         page->setIsPainting(true);
1849 #endif
1850
1851     if (graphicsLayer == m_graphicsLayer.get()
1852         || graphicsLayer == m_foregroundLayer.get()
1853         || graphicsLayer == m_backgroundLayer.get()
1854         || graphicsLayer == m_maskLayer.get()
1855         || graphicsLayer == m_scrollingContentsLayer.get()) {
1856         InspectorInstrumentationCookie cookie = InspectorInstrumentation::willPaint(m_owningLayer->renderer()->frame());
1857
1858         // The dirtyRect is in the coords of the painting root.
1859         IntRect dirtyRect = clip;
1860         if (!(paintingPhase & GraphicsLayerPaintOverflowContents))
1861             dirtyRect.intersect(compositedBounds());
1862
1863         // We have to use the same root as for hit testing, because both methods can compute and cache clipRects.
1864         paintIntoLayer(graphicsLayer, &context, dirtyRect, PaintBehaviorNormal, paintingPhase);
1865
1866         if (m_usingTiledCacheLayer)
1867             m_owningLayer->renderer()->frame()->view()->setLastPaintTime(currentTime());
1868
1869         InspectorInstrumentation::didPaint(cookie, &context, clip);
1870     } else if (graphicsLayer == layerForHorizontalScrollbar()) {
1871         paintScrollbar(m_owningLayer->horizontalScrollbar(), context, clip);
1872     } else if (graphicsLayer == layerForVerticalScrollbar()) {
1873         paintScrollbar(m_owningLayer->verticalScrollbar(), context, clip);
1874     } else if (graphicsLayer == layerForScrollCorner()) {
1875         const IntRect& scrollCornerAndResizer = m_owningLayer->scrollCornerAndResizerRect();
1876         context.save();
1877         context.translate(-scrollCornerAndResizer.x(), -scrollCornerAndResizer.y());
1878         IntRect transformedClip = clip;
1879         transformedClip.moveBy(scrollCornerAndResizer.location());
1880         m_owningLayer->paintScrollCorner(&context, IntPoint(), transformedClip);
1881         m_owningLayer->paintResizer(&context, IntPoint(), transformedClip);
1882         context.restore();
1883     }
1884 #ifndef NDEBUG
1885     if (Page* page = renderer()->frame()->page())
1886         page->setIsPainting(false);
1887 #endif
1888 }
1889
1890 float RenderLayerBacking::pageScaleFactor() const
1891 {
1892     return compositor()->pageScaleFactor();
1893 }
1894
1895 float RenderLayerBacking::deviceScaleFactor() const
1896 {
1897     return compositor()->deviceScaleFactor();
1898 }
1899
1900 void RenderLayerBacking::didCommitChangesForLayer(const GraphicsLayer* layer) const
1901 {
1902     compositor()->didFlushChangesForLayer(m_owningLayer, layer);
1903 }
1904
1905 bool RenderLayerBacking::getCurrentTransform(const GraphicsLayer* graphicsLayer, TransformationMatrix& transform) const
1906 {
1907     GraphicsLayer* transformedLayer = m_contentsContainmentLayer.get() ? m_contentsContainmentLayer.get() : m_graphicsLayer.get();
1908     if (graphicsLayer != transformedLayer)
1909         return false;
1910
1911     if (m_owningLayer->hasTransform()) {
1912         transform = m_owningLayer->currentTransform(RenderStyle::ExcludeTransformOrigin);
1913         return true;
1914     }
1915     return false;
1916 }
1917
1918 bool RenderLayerBacking::isTrackingRepaints() const
1919 {
1920     GraphicsLayerClient* client = compositor();
1921     return client ? client->isTrackingRepaints() : false;
1922 }
1923
1924 #ifndef NDEBUG
1925 void RenderLayerBacking::verifyNotPainting()
1926 {
1927     ASSERT(!renderer()->frame()->page() || !renderer()->frame()->page()->isPainting());
1928 }
1929 #endif
1930
1931 bool RenderLayerBacking::startAnimation(double timeOffset, const Animation* anim, const KeyframeList& keyframes)
1932 {
1933     bool hasOpacity = keyframes.containsProperty(CSSPropertyOpacity);
1934     bool hasTransform = renderer()->isBox() && keyframes.containsProperty(CSSPropertyWebkitTransform);
1935 #if ENABLE(CSS_FILTERS)
1936     bool hasFilter = keyframes.containsProperty(CSSPropertyWebkitFilter);
1937 #else
1938     bool hasFilter = false;
1939 #endif
1940
1941     if (!hasOpacity && !hasTransform && !hasFilter)
1942         return false;
1943     
1944     KeyframeValueList transformVector(AnimatedPropertyWebkitTransform);
1945     KeyframeValueList opacityVector(AnimatedPropertyOpacity);
1946 #if ENABLE(CSS_FILTERS)
1947     KeyframeValueList filterVector(AnimatedPropertyWebkitFilter);
1948 #endif
1949
1950     size_t numKeyframes = keyframes.size();
1951     for (size_t i = 0; i < numKeyframes; ++i) {
1952         const KeyframeValue& currentKeyframe = keyframes[i];
1953         const RenderStyle* keyframeStyle = currentKeyframe.style();
1954         float key = currentKeyframe.key();
1955
1956         if (!keyframeStyle)
1957             continue;
1958             
1959         // Get timing function.
1960         RefPtr<TimingFunction> tf = keyframeStyle->hasAnimations() ? (*keyframeStyle->animations()).animation(0)->timingFunction() : 0;
1961         
1962         bool isFirstOrLastKeyframe = key == 0 || key == 1;
1963         if ((hasTransform && isFirstOrLastKeyframe) || currentKeyframe.containsProperty(CSSPropertyWebkitTransform))
1964             transformVector.insert(new TransformAnimationValue(key, &(keyframeStyle->transform()), tf));
1965         
1966         if ((hasOpacity && isFirstOrLastKeyframe) || currentKeyframe.containsProperty(CSSPropertyOpacity))
1967             opacityVector.insert(new FloatAnimationValue(key, keyframeStyle->opacity(), tf));
1968
1969 #if ENABLE(CSS_FILTERS)
1970         if ((hasFilter && isFirstOrLastKeyframe) || currentKeyframe.containsProperty(CSSPropertyWebkitFilter))
1971             filterVector.insert(new FilterAnimationValue(key, &(keyframeStyle->filter()), tf));
1972 #endif
1973     }
1974
1975     bool didAnimateTransform = false;
1976     bool didAnimateOpacity = false;
1977 #if ENABLE(CSS_FILTERS)
1978     bool didAnimateFilter = false;
1979 #endif
1980     
1981     if (hasTransform && m_graphicsLayer->addAnimation(transformVector, toRenderBox(renderer())->pixelSnappedBorderBoxRect().size(), anim, keyframes.animationName(), timeOffset))
1982         didAnimateTransform = true;
1983
1984     if (hasOpacity && m_graphicsLayer->addAnimation(opacityVector, IntSize(), anim, keyframes.animationName(), timeOffset))
1985         didAnimateOpacity = true;
1986
1987 #if ENABLE(CSS_FILTERS)
1988     if (hasFilter && m_graphicsLayer->addAnimation(filterVector, IntSize(), anim, keyframes.animationName(), timeOffset))
1989         didAnimateFilter = true;
1990 #endif
1991
1992 #if ENABLE(CSS_FILTERS)
1993     return didAnimateTransform || didAnimateOpacity || didAnimateFilter;
1994 #else
1995     return didAnimateTransform || didAnimateOpacity;
1996 #endif
1997 }
1998
1999 void RenderLayerBacking::animationPaused(double timeOffset, const String& animationName)
2000 {
2001     m_graphicsLayer->pauseAnimation(animationName, timeOffset);
2002 }
2003
2004 void RenderLayerBacking::animationFinished(const String& animationName)
2005 {
2006     m_graphicsLayer->removeAnimation(animationName);
2007 }
2008
2009 bool RenderLayerBacking::startTransition(double timeOffset, CSSPropertyID property, const RenderStyle* fromStyle, const RenderStyle* toStyle)
2010 {
2011     bool didAnimateOpacity = false;
2012     bool didAnimateTransform = false;
2013 #if ENABLE(CSS_FILTERS)
2014     bool didAnimateFilter = false;
2015 #endif
2016
2017     ASSERT(property != CSSPropertyInvalid);
2018
2019     if (property == CSSPropertyOpacity) {
2020         const Animation* opacityAnim = toStyle->transitionForProperty(CSSPropertyOpacity);
2021         if (opacityAnim && !opacityAnim->isEmptyOrZeroDuration()) {
2022             KeyframeValueList opacityVector(AnimatedPropertyOpacity);
2023             opacityVector.insert(new FloatAnimationValue(0, compositingOpacity(fromStyle->opacity())));
2024             opacityVector.insert(new FloatAnimationValue(1, compositingOpacity(toStyle->opacity())));
2025             // The boxSize param is only used for transform animations (which can only run on RenderBoxes), so we pass an empty size here.
2026             if (m_graphicsLayer->addAnimation(opacityVector, IntSize(), opacityAnim, GraphicsLayer::animationNameForTransition(AnimatedPropertyOpacity), timeOffset)) {
2027                 // To ensure that the correct opacity is visible when the animation ends, also set the final opacity.
2028                 updateOpacity(toStyle);
2029                 didAnimateOpacity = true;
2030             }
2031         }
2032     }
2033
2034     if (property == CSSPropertyWebkitTransform && m_owningLayer->hasTransform()) {
2035         const Animation* transformAnim = toStyle->transitionForProperty(CSSPropertyWebkitTransform);
2036         if (transformAnim && !transformAnim->isEmptyOrZeroDuration()) {
2037             KeyframeValueList transformVector(AnimatedPropertyWebkitTransform);
2038             transformVector.insert(new TransformAnimationValue(0, &fromStyle->transform()));
2039             transformVector.insert(new TransformAnimationValue(1, &toStyle->transform()));
2040             if (m_graphicsLayer->addAnimation(transformVector, toRenderBox(renderer())->pixelSnappedBorderBoxRect().size(), transformAnim, GraphicsLayer::animationNameForTransition(AnimatedPropertyWebkitTransform), timeOffset)) {
2041                 // To ensure that the correct transform is visible when the animation ends, also set the final transform.
2042                 updateTransform(toStyle);
2043                 didAnimateTransform = true;
2044             }
2045         }
2046     }
2047
2048 #if ENABLE(CSS_FILTERS)
2049     if (property == CSSPropertyWebkitFilter && m_owningLayer->hasFilter()) {
2050         const Animation* filterAnim = toStyle->transitionForProperty(CSSPropertyWebkitFilter);
2051         if (filterAnim && !filterAnim->isEmptyOrZeroDuration()) {
2052             KeyframeValueList filterVector(AnimatedPropertyWebkitFilter);
2053             filterVector.insert(new FilterAnimationValue(0, &fromStyle->filter()));
2054             filterVector.insert(new FilterAnimationValue(1, &toStyle->filter()));
2055             if (m_graphicsLayer->addAnimation(filterVector, IntSize(), filterAnim, GraphicsLayer::animationNameForTransition(AnimatedPropertyWebkitFilter), timeOffset)) {
2056                 // To ensure that the correct filter is visible when the animation ends, also set the final filter.
2057                 updateFilters(toStyle);
2058                 didAnimateFilter = true;
2059             }
2060         }
2061     }
2062 #endif
2063
2064 #if ENABLE(CSS_FILTERS)
2065     return didAnimateOpacity || didAnimateTransform || didAnimateFilter;
2066 #else
2067     return didAnimateOpacity || didAnimateTransform;
2068 #endif
2069 }
2070
2071 void RenderLayerBacking::transitionPaused(double timeOffset, CSSPropertyID property)
2072 {
2073     AnimatedPropertyID animatedProperty = cssToGraphicsLayerProperty(property);
2074     if (animatedProperty != AnimatedPropertyInvalid)
2075         m_graphicsLayer->pauseAnimation(GraphicsLayer::animationNameForTransition(animatedProperty), timeOffset);
2076 }
2077
2078 void RenderLayerBacking::transitionFinished(CSSPropertyID property)
2079 {
2080     AnimatedPropertyID animatedProperty = cssToGraphicsLayerProperty(property);
2081     if (animatedProperty != AnimatedPropertyInvalid)
2082         m_graphicsLayer->removeAnimation(GraphicsLayer::animationNameForTransition(animatedProperty));
2083 }
2084
2085 void RenderLayerBacking::notifyAnimationStarted(const GraphicsLayer*, double time)
2086 {
2087     renderer()->animation()->notifyAnimationStarted(renderer(), time);
2088 }
2089
2090 void RenderLayerBacking::notifyFlushRequired(const GraphicsLayer*)
2091 {
2092     if (!renderer()->documentBeingDestroyed())
2093         compositor()->scheduleLayerFlush();
2094 }
2095
2096 void RenderLayerBacking::notifyFlushBeforeDisplayRefresh(const GraphicsLayer* layer)
2097 {
2098     compositor()->notifyFlushBeforeDisplayRefresh(layer);
2099 }
2100
2101 // This is used for the 'freeze' API, for testing only.
2102 void RenderLayerBacking::suspendAnimations(double time)
2103 {
2104     m_graphicsLayer->suspendAnimations(time);
2105 }
2106
2107 void RenderLayerBacking::resumeAnimations()
2108 {
2109     m_graphicsLayer->resumeAnimations();
2110 }
2111
2112 IntRect RenderLayerBacking::compositedBounds() const
2113 {
2114     return m_compositedBounds;
2115 }
2116
2117 void RenderLayerBacking::setCompositedBounds(const IntRect& bounds)
2118 {
2119     m_compositedBounds = bounds;
2120 }
2121
2122 CSSPropertyID RenderLayerBacking::graphicsLayerToCSSProperty(AnimatedPropertyID property)
2123 {
2124     CSSPropertyID cssProperty = CSSPropertyInvalid;
2125     switch (property) {
2126         case AnimatedPropertyWebkitTransform:
2127             cssProperty = CSSPropertyWebkitTransform;
2128             break;
2129         case AnimatedPropertyOpacity:
2130             cssProperty = CSSPropertyOpacity;
2131             break;
2132         case AnimatedPropertyBackgroundColor:
2133             cssProperty = CSSPropertyBackgroundColor;
2134             break;
2135         case AnimatedPropertyWebkitFilter:
2136 #if ENABLE(CSS_FILTERS)
2137             cssProperty = CSSPropertyWebkitFilter;
2138 #else
2139             ASSERT_NOT_REACHED();
2140 #endif
2141             break;
2142         case AnimatedPropertyInvalid:
2143             ASSERT_NOT_REACHED();
2144     }
2145     return cssProperty;
2146 }
2147
2148 AnimatedPropertyID RenderLayerBacking::cssToGraphicsLayerProperty(CSSPropertyID cssProperty)
2149 {
2150     switch (cssProperty) {
2151         case CSSPropertyWebkitTransform:
2152             return AnimatedPropertyWebkitTransform;
2153         case CSSPropertyOpacity:
2154             return AnimatedPropertyOpacity;
2155         case CSSPropertyBackgroundColor:
2156             return AnimatedPropertyBackgroundColor;
2157 #if ENABLE(CSS_FILTERS)
2158         case CSSPropertyWebkitFilter:
2159             return AnimatedPropertyWebkitFilter;
2160 #endif
2161         default:
2162             // It's fine if we see other css properties here; they are just not accelerated.
2163             break;
2164     }
2165     return AnimatedPropertyInvalid;
2166 }
2167
2168 CompositingLayerType RenderLayerBacking::compositingLayerType() const
2169 {
2170     if (m_graphicsLayer->hasContentsLayer())
2171         return MediaCompositingLayer;
2172
2173     if (m_graphicsLayer->drawsContent())
2174         return m_graphicsLayer->usingTiledLayer() ? TiledCompositingLayer : NormalCompositingLayer;
2175     
2176     return ContainerCompositingLayer;
2177 }
2178
2179 double RenderLayerBacking::backingStoreMemoryEstimate() const
2180 {
2181     double backingMemory;
2182     
2183     // m_ancestorClippingLayer, m_contentsContainmentLayer and m_childContainmentLayer are just used for masking or containment, so have no backing.
2184     backingMemory = m_graphicsLayer->backingStoreMemoryEstimate();
2185     if (m_foregroundLayer)
2186         backingMemory += m_foregroundLayer->backingStoreMemoryEstimate();
2187     if (m_backgroundLayer)
2188         backingMemory += m_backgroundLayer->backingStoreMemoryEstimate();
2189     if (m_maskLayer)
2190         backingMemory += m_maskLayer->backingStoreMemoryEstimate();
2191
2192     if (m_scrollingContentsLayer)
2193         backingMemory += m_scrollingContentsLayer->backingStoreMemoryEstimate();
2194
2195     if (m_layerForHorizontalScrollbar)
2196         backingMemory += m_layerForHorizontalScrollbar->backingStoreMemoryEstimate();
2197
2198     if (m_layerForVerticalScrollbar)
2199         backingMemory += m_layerForVerticalScrollbar->backingStoreMemoryEstimate();
2200
2201     if (m_layerForScrollCorner)
2202         backingMemory += m_layerForScrollCorner->backingStoreMemoryEstimate();
2203     
2204     return backingMemory;
2205 }
2206
2207 #if PLATFORM(BLACKBERRY)
2208 bool RenderLayerBacking::contentsVisible(const GraphicsLayer*, const IntRect& localContentRect) const
2209 {
2210     Frame* frame = renderer()->frame();
2211     FrameView* view = frame ? frame->view() : 0;
2212     if (!view)
2213         return false;
2214
2215     IntRect visibleContentRect(view->visibleContentRect());
2216     FloatQuad absoluteContentQuad = renderer()->localToAbsoluteQuad(FloatRect(localContentRect));
2217     return absoluteContentQuad.enclosingBoundingBox().intersects(visibleContentRect);
2218 }
2219 #endif
2220
2221 void RenderLayerBacking::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
2222 {
2223     MemoryClassInfo info(memoryObjectInfo, this, PlatformMemoryTypes::Rendering);
2224     info.addWeakPointer(m_owningLayer);
2225     info.addMember(m_ancestorClippingLayer, "ancestorClippingLayer");
2226     info.addMember(m_contentsContainmentLayer, "contentsContainmentLayer");
2227     info.addMember(m_graphicsLayer, "graphicsLayer");
2228     info.addMember(m_foregroundLayer, "foregroundLayer");
2229     info.addMember(m_backgroundLayer, "backgroundLayer");
2230     info.addMember(m_childContainmentLayer, "childContainmentLayer");
2231     info.addMember(m_maskLayer, "maskLayer");
2232     info.addMember(m_layerForHorizontalScrollbar, "layerForHorizontalScrollbar");
2233     info.addMember(m_layerForVerticalScrollbar, "layerForVerticalScrollbar");
2234     info.addMember(m_layerForScrollCorner, "layerForScrollCorner");
2235     info.addMember(m_scrollingLayer, "scrollingLayer");
2236     info.addMember(m_scrollingContentsLayer, "scrollingContentsLayer");
2237 }
2238
2239 } // namespace WebCore
2240
2241 #endif // USE(ACCELERATED_COMPOSITING)