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