Use background color for GraphicsLayers when applicable
[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     updateBackgroundColor(isSimpleContainerCompositingLayer());
483
484     if (isDirectlyCompositedImage())
485         updateImageContents();
486
487     if (renderer->isEmbeddedObject() && toRenderEmbeddedObject(renderer)->allowsAcceleratedCompositing()) {
488         PluginViewBase* pluginViewBase = static_cast<PluginViewBase*>(toRenderWidget(renderer)->widget());
489         m_graphicsLayer->setContentsToMedia(pluginViewBase->platformLayer());
490     }
491 #if ENABLE(VIDEO)
492     else if (renderer->isVideo()) {
493         HTMLMediaElement* mediaElement = static_cast<HTMLMediaElement*>(renderer->node());
494         m_graphicsLayer->setContentsToMedia(mediaElement->platformLayer());
495     }
496 #endif
497 #if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
498     else if (isAcceleratedCanvas(renderer)) {
499         HTMLCanvasElement* canvas = static_cast<HTMLCanvasElement*>(renderer->node());
500         if (CanvasRenderingContext* context = canvas->renderingContext())
501             m_graphicsLayer->setContentsToCanvas(context->platformLayer());
502         layerConfigChanged = true;
503     }
504 #endif
505     if (renderer->isRenderPart())
506         layerConfigChanged = RenderLayerCompositor::parentFrameContentLayers(toRenderPart(renderer));
507
508     return layerConfigChanged;
509 }
510
511 static IntRect clipBox(RenderBox* renderer)
512 {
513     LayoutRect result = PaintInfo::infiniteRect();
514     if (renderer->hasOverflowClip())
515         result = renderer->overflowClipRect(LayoutPoint(), 0); // FIXME: Incorrect for CSS regions.
516
517     if (renderer->hasClip())
518         result.intersect(renderer->clipRect(LayoutPoint(), 0)); // FIXME: Incorrect for CSS regions.
519
520     return pixelSnappedIntRect(result);
521
522 }
523
524 void RenderLayerBacking::updateGraphicsLayerGeometry()
525 {
526     // If we haven't built z-order lists yet, wait until later.
527     if (m_owningLayer->isStackingContext() && m_owningLayer->m_zOrderListsDirty)
528         return;
529
530     // Set transform property, if it is not animating. We have to do this here because the transform
531     // is affected by the layer dimensions.
532     if (!renderer()->animation()->isRunningAcceleratedAnimationOnRenderer(renderer(), CSSPropertyWebkitTransform))
533         updateTransform(renderer()->style());
534
535     // Set opacity, if it is not animating.
536     if (!renderer()->animation()->isRunningAcceleratedAnimationOnRenderer(renderer(), CSSPropertyOpacity))
537         updateOpacity(renderer()->style());
538         
539 #if ENABLE(CSS_FILTERS)
540     updateFilters(renderer()->style());
541 #endif
542
543 #if ENABLE(CSS_COMPOSITING)
544     updateLayerBlendMode(renderer()->style());
545 #endif
546
547     bool isSimpleContainer = isSimpleContainerCompositingLayer();
548     
549     m_owningLayer->updateDescendantDependentFlags();
550
551     // m_graphicsLayer is the corresponding GraphicsLayer for this RenderLayer and its non-compositing
552     // descendants. So, the visibility flag for m_graphicsLayer should be true if there are any
553     // non-compositing visible layers.
554     m_graphicsLayer->setContentsVisible(m_owningLayer->hasVisibleContent() || hasVisibleNonCompositingDescendantLayers());
555
556     RenderStyle* style = renderer()->style();
557     m_graphicsLayer->setPreserves3D(style->transformStyle3D() == TransformStyle3DPreserve3D && !renderer()->hasReflection());
558     m_graphicsLayer->setBackfaceVisibility(style->backfaceVisibility() == BackfaceVisibilityVisible);
559
560     // Register fixed position layers and their containers with the scrolling coordinator.
561     if (Page* page = renderer()->frame()->page()) {
562         if (ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator()) {
563             if (style->position() == FixedPosition || compositor()->fixedPositionedByAncestor(m_owningLayer))
564                 scrollingCoordinator->setLayerIsFixedToContainerLayer(childForSuperlayers(), true);
565             else {
566                 if (m_ancestorClippingLayer)
567                     scrollingCoordinator->setLayerIsFixedToContainerLayer(m_ancestorClippingLayer.get(), false);
568                 scrollingCoordinator->setLayerIsFixedToContainerLayer(m_graphicsLayer.get(), false);
569             }
570             // Page scale is applied as a transform on the root render view layer. Because the scroll
571             // layer is further up in the hierarchy, we need to avoid marking the root render view
572             // layer as a container.
573             bool isContainer = m_owningLayer->hasTransform() && !m_owningLayer->isRootLayer();
574             scrollingCoordinator->setLayerIsContainerForFixedPositionLayers(childForSuperlayers(), isContainer);
575         }
576     }
577     RenderLayer* compAncestor = m_owningLayer->ancestorCompositingLayer();
578     
579     // We compute everything relative to the enclosing compositing layer.
580     IntRect ancestorCompositingBounds;
581     if (compAncestor) {
582         ASSERT(compAncestor->backing());
583         ancestorCompositingBounds = pixelSnappedIntRect(compAncestor->backing()->compositedBounds());
584     }
585
586     IntRect localCompositingBounds = pixelSnappedIntRect(compositedBounds());
587
588     IntRect relativeCompositingBounds(localCompositingBounds);
589     IntPoint delta;
590     m_owningLayer->convertToPixelSnappedLayerCoords(compAncestor, delta);
591     relativeCompositingBounds.moveBy(delta);
592
593     IntPoint graphicsLayerParentLocation;
594     if (compAncestor && compAncestor->backing()->hasClippingLayer()) {
595         // If the compositing ancestor has a layer to clip children, we parent in that, and therefore
596         // position relative to it.
597         IntRect clippingBox = clipBox(toRenderBox(compAncestor->renderer()));
598         graphicsLayerParentLocation = clippingBox.location();
599     } else if (compAncestor)
600         graphicsLayerParentLocation = ancestorCompositingBounds.location();
601     else
602         graphicsLayerParentLocation = renderer()->view()->documentRect().location();
603
604     if (compAncestor && compAncestor->usesCompositedScrolling()) {
605         RenderBox* renderBox = toRenderBox(compAncestor->renderer());
606         IntSize scrollOffset = compAncestor->scrolledContentOffset();
607         IntPoint scrollOrigin(renderBox->borderLeft(), renderBox->borderTop());
608         graphicsLayerParentLocation = scrollOrigin - scrollOffset;
609     }
610     
611     if (compAncestor && m_ancestorClippingLayer) {
612         // Call calculateRects to get the backgroundRect which is what is used to clip the contents of this
613         // layer. Note that we call it with temporaryClipRects = true because normally when computing clip rects
614         // for a compositing layer, rootLayer is the layer itself.
615         RenderLayer::ClipRectsContext clipRectsContext(compAncestor, 0, TemporaryClipRects, IgnoreOverlayScrollbarSize, RenderLayer::IgnoreOverflowClip);
616         IntRect parentClipRect = pixelSnappedIntRect(m_owningLayer->backgroundClipRect(clipRectsContext).rect()); // FIXME: Incorrect for CSS regions.
617         ASSERT(parentClipRect != PaintInfo::infiniteRect());
618         m_ancestorClippingLayer->setPosition(FloatPoint() + (parentClipRect.location() - graphicsLayerParentLocation));
619         m_ancestorClippingLayer->setSize(parentClipRect.size());
620
621         // backgroundRect is relative to compAncestor, so subtract deltaX/deltaY to get back to local coords.
622         m_ancestorClippingLayer->setOffsetFromRenderer(parentClipRect.location() - delta);
623
624         // The primary layer is then parented in, and positioned relative to this clipping layer.
625         graphicsLayerParentLocation = parentClipRect.location();
626     }
627
628     m_graphicsLayer->setPosition(FloatPoint() + (relativeCompositingBounds.location() - graphicsLayerParentLocation));
629     m_graphicsLayer->setOffsetFromRenderer(localCompositingBounds.location() - IntPoint());
630     
631     FloatSize oldSize = m_graphicsLayer->size();
632     FloatSize newSize = relativeCompositingBounds.size();
633     if (oldSize != newSize) {
634         m_graphicsLayer->setSize(newSize);
635         // A bounds change will almost always require redisplay. Usually that redisplay
636         // will happen because of a repaint elsewhere, but not always:
637         // e.g. see RenderView::setMaximalOutlineSize()
638         m_graphicsLayer->setNeedsDisplay();
639     }
640
641     // If we have a layer that clips children, position it.
642     IntRect clippingBox;
643     if (GraphicsLayer* clipLayer = clippingLayer()) {
644         clippingBox = clipBox(toRenderBox(renderer()));
645         clipLayer->setPosition(FloatPoint() + (clippingBox.location() - localCompositingBounds.location()));
646         clipLayer->setSize(clippingBox.size());
647         clipLayer->setOffsetFromRenderer(clippingBox.location() - IntPoint());
648     }
649     
650     if (m_maskLayer) {
651         if (m_maskLayer->size() != m_graphicsLayer->size()) {
652             m_maskLayer->setSize(m_graphicsLayer->size());
653             m_maskLayer->setNeedsDisplay();
654         }
655         m_maskLayer->setPosition(FloatPoint());
656         m_maskLayer->setOffsetFromRenderer(m_graphicsLayer->offsetFromRenderer());
657     }
658     
659     if (m_owningLayer->hasTransform()) {
660         const IntRect borderBox = toRenderBox(renderer())->pixelSnappedBorderBoxRect();
661
662         // Get layout bounds in the coords of compAncestor to match relativeCompositingBounds.
663         IntRect layerBounds = IntRect(delta, borderBox.size());
664
665         // Update properties that depend on layer dimensions
666         FloatPoint3D transformOrigin = computeTransformOrigin(borderBox);
667         // Compute the anchor point, which is in the center of the renderer box unless transform-origin is set.
668         FloatPoint3D anchor(relativeCompositingBounds.width()  != 0.0f ? ((layerBounds.x() - relativeCompositingBounds.x()) + transformOrigin.x()) / relativeCompositingBounds.width()  : 0.5f,
669                             relativeCompositingBounds.height() != 0.0f ? ((layerBounds.y() - relativeCompositingBounds.y()) + transformOrigin.y()) / relativeCompositingBounds.height() : 0.5f,
670                             transformOrigin.z());
671         m_graphicsLayer->setAnchorPoint(anchor);
672
673         RenderStyle* style = renderer()->style();
674         GraphicsLayer* clipLayer = clippingLayer();
675         if (style->hasPerspective()) {
676             TransformationMatrix t = owningLayer()->perspectiveTransform();
677             
678             if (clipLayer) {
679                 clipLayer->setChildrenTransform(t);
680                 m_graphicsLayer->setChildrenTransform(TransformationMatrix());
681             }
682             else
683                 m_graphicsLayer->setChildrenTransform(t);
684         } else {
685             if (clipLayer)
686                 clipLayer->setChildrenTransform(TransformationMatrix());
687             else
688                 m_graphicsLayer->setChildrenTransform(TransformationMatrix());
689         }
690     } else {
691         m_graphicsLayer->setAnchorPoint(FloatPoint3D(0.5f, 0.5f, 0));
692     }
693
694     if (m_foregroundLayer) {
695         FloatPoint foregroundPosition;
696         FloatSize foregroundSize = newSize;
697         IntSize foregroundOffset = m_graphicsLayer->offsetFromRenderer();
698         if (hasClippingLayer()) {
699             // If we have a clipping layer (which clips descendants), then the foreground layer is a child of it,
700             // so that it gets correctly sorted with children. In that case, position relative to the clipping layer.
701             foregroundSize = FloatSize(clippingBox.size());
702             foregroundOffset = clippingBox.location() - IntPoint();
703         }
704
705         m_foregroundLayer->setPosition(foregroundPosition);
706         if (foregroundSize != m_foregroundLayer->size()) {
707             m_foregroundLayer->setSize(foregroundSize);
708             m_foregroundLayer->setNeedsDisplay();
709         }
710         m_foregroundLayer->setOffsetFromRenderer(foregroundOffset);
711     }
712
713     if (m_owningLayer->reflectionLayer() && m_owningLayer->reflectionLayer()->isComposited()) {
714         RenderLayerBacking* reflectionBacking = m_owningLayer->reflectionLayer()->backing();
715         reflectionBacking->updateGraphicsLayerGeometry();
716         
717         // The reflection layer has the bounds of m_owningLayer->reflectionLayer(),
718         // but the reflected layer is the bounds of this layer, so we need to position it appropriately.
719         FloatRect layerBounds = compositedBounds();
720         FloatRect reflectionLayerBounds = reflectionBacking->compositedBounds();
721         reflectionBacking->graphicsLayer()->setReplicatedLayerPosition(FloatPoint() + (layerBounds.location() - reflectionLayerBounds.location()));
722     }
723
724     if (m_scrollingLayer) {
725         ASSERT(m_scrollingContentsLayer);
726         RenderBox* renderBox = toRenderBox(renderer());
727         IntRect paddingBox(renderBox->borderLeft(), renderBox->borderTop(), renderBox->width() - renderBox->borderLeft() - renderBox->borderRight(), renderBox->height() - renderBox->borderTop() - renderBox->borderBottom());
728         IntSize scrollOffset = m_owningLayer->scrolledContentOffset();
729
730         m_scrollingLayer->setPosition(FloatPoint() + (paddingBox.location() - localCompositingBounds.location()));
731
732         m_scrollingLayer->setSize(paddingBox.size());
733         m_scrollingContentsLayer->setPosition(FloatPoint(-scrollOffset.width(), -scrollOffset.height()));
734
735         IntSize oldScrollingLayerOffset = m_scrollingLayer->offsetFromRenderer();
736         m_scrollingLayer->setOffsetFromRenderer(IntPoint() - paddingBox.location());
737
738         bool paddingBoxOffsetChanged = oldScrollingLayerOffset != m_scrollingLayer->offsetFromRenderer();
739
740         IntSize scrollSize(m_owningLayer->scrollWidth(), m_owningLayer->scrollHeight());
741         if (scrollSize != m_scrollingContentsLayer->size() || paddingBoxOffsetChanged)
742             m_scrollingContentsLayer->setNeedsDisplay();
743
744         IntSize scrollingContentsOffset = paddingBox.location() - IntPoint() - scrollOffset;
745         if (scrollingContentsOffset != m_scrollingContentsLayer->offsetFromRenderer() || scrollSize != m_scrollingContentsLayer->size())
746             compositor()->scrollingLayerDidChange(m_owningLayer);
747
748         m_scrollingContentsLayer->setSize(scrollSize);
749         // FIXME: The paint offset and the scroll offset should really be separate concepts.
750         m_scrollingContentsLayer->setOffsetFromRenderer(scrollingContentsOffset, GraphicsLayer::DontSetNeedsDisplay);
751     }
752
753     // If this layer was created just for clipping or to apply perspective, it doesn't need its own backing store.
754     setRequiresOwnBackingStore(compositor()->requiresOwnBackingStore(m_owningLayer, compAncestor));
755
756     updateContentsRect(isSimpleContainer);
757     updateDrawsContent(isSimpleContainer);
758     updateAfterWidgetResize();
759 }
760
761 void RenderLayerBacking::updateInternalHierarchy()
762 {
763     // m_foregroundLayer has to be inserted in the correct order with child layers,
764     // so it's not inserted here.
765     if (m_ancestorClippingLayer) {
766         m_ancestorClippingLayer->removeAllChildren();
767         m_graphicsLayer->removeFromParent();
768         m_ancestorClippingLayer->addChild(m_graphicsLayer.get());
769     }
770
771     if (m_containmentLayer) {
772         m_containmentLayer->removeFromParent();
773         m_graphicsLayer->addChild(m_containmentLayer.get());
774     }
775
776     if (m_scrollingLayer) {
777         GraphicsLayer* superlayer = m_containmentLayer ? m_containmentLayer.get() : m_graphicsLayer.get();
778         m_scrollingLayer->removeFromParent();
779         superlayer->addChild(m_scrollingLayer.get());
780     }
781
782     // The clip for child layers does not include space for overflow controls, so they exist as
783     // siblings of the clipping layer if we have one. Normal children of this layer are set as
784     // children of the clipping layer.
785     if (m_layerForHorizontalScrollbar) {
786         m_layerForHorizontalScrollbar->removeFromParent();
787         m_graphicsLayer->addChild(m_layerForHorizontalScrollbar.get());
788     }
789     if (m_layerForVerticalScrollbar) {
790         m_layerForVerticalScrollbar->removeFromParent();
791         m_graphicsLayer->addChild(m_layerForVerticalScrollbar.get());
792     }
793     if (m_layerForScrollCorner) {
794         m_layerForScrollCorner->removeFromParent();
795         m_graphicsLayer->addChild(m_layerForScrollCorner.get());
796     }
797 }
798
799 void RenderLayerBacking::updateContentsRect(bool isSimpleContainer)
800 {
801     IntRect contentsRect;
802     if (isSimpleContainer && renderer()->hasBackground())
803         contentsRect = backgroundBox();
804     else
805         contentsRect = contentsBox();
806
807     m_graphicsLayer->setContentsRect(contentsRect);
808 }
809
810 void RenderLayerBacking::updateDrawsContent()
811 {
812     updateDrawsContent(isSimpleContainerCompositingLayer());
813 }
814
815 void RenderLayerBacking::updateDrawsContent(bool isSimpleContainer)
816 {
817     if (m_scrollingLayer) {
818         // We don't have to consider overflow controls, because we know that the scrollbars are drawn elsewhere.
819         // m_graphicsLayer only needs backing store if the non-scrolling parts (background, outlines, borders, shadows etc) need to paint.
820         // m_scrollingLayer never has backing store.
821         // m_scrollingContentsLayer only needs backing store if the scrolled contents need to paint.
822         bool hasNonScrollingPaintedContent = m_owningLayer->hasVisibleContent() && hasBoxDecorationsOrBackground(renderer());
823         m_graphicsLayer->setDrawsContent(hasNonScrollingPaintedContent);
824
825         bool hasScrollingPaintedContent = m_owningLayer->hasVisibleContent() && (renderer()->hasBackground() || paintsChildren());
826         m_scrollingContentsLayer->setDrawsContent(hasScrollingPaintedContent);
827         return;
828     }
829
830     bool hasPaintedContent = !isSimpleContainer && containsPaintedContent();
831
832     // FIXME: we could refine this to only allocate backing for one of these layers if possible.
833     m_graphicsLayer->setDrawsContent(hasPaintedContent);
834     if (m_foregroundLayer)
835         m_foregroundLayer->setDrawsContent(hasPaintedContent);
836 }
837
838 // Return true if the layers changed.
839 bool RenderLayerBacking::updateClippingLayers(bool needsAncestorClip, bool needsDescendantClip)
840 {
841     bool layersChanged = false;
842
843     if (needsAncestorClip) {
844         if (!m_ancestorClippingLayer) {
845             m_ancestorClippingLayer = createGraphicsLayer("Ancestor clipping Layer");
846             m_ancestorClippingLayer->setMasksToBounds(true);
847             layersChanged = true;
848         }
849     } else if (m_ancestorClippingLayer) {
850         m_ancestorClippingLayer->removeFromParent();
851         m_ancestorClippingLayer = nullptr;
852         layersChanged = true;
853     }
854     
855     if (needsDescendantClip) {
856         if (!m_containmentLayer && !m_usingTiledCacheLayer) {
857             m_containmentLayer = createGraphicsLayer("Child clipping Layer");
858             m_containmentLayer->setMasksToBounds(true);
859             layersChanged = true;
860         }
861     } else if (hasClippingLayer()) {
862         m_containmentLayer->removeFromParent();
863         m_containmentLayer = nullptr;
864         layersChanged = true;
865     }
866     
867     return layersChanged;
868 }
869
870 bool RenderLayerBacking::requiresHorizontalScrollbarLayer() const
871 {
872 #if !PLATFORM(CHROMIUM)
873     if (!m_owningLayer->hasOverlayScrollbars())
874         return false;
875 #endif
876     return m_owningLayer->horizontalScrollbar();
877 }
878
879 bool RenderLayerBacking::requiresVerticalScrollbarLayer() const
880 {
881 #if !PLATFORM(CHROMIUM)
882     if (!m_owningLayer->hasOverlayScrollbars())
883         return false;
884 #endif
885     return m_owningLayer->verticalScrollbar();
886 }
887
888 bool RenderLayerBacking::requiresScrollCornerLayer() const
889 {
890 #if !PLATFORM(CHROMIUM)
891     if (!m_owningLayer->hasOverlayScrollbars())
892         return false;
893 #endif
894     return !m_owningLayer->scrollCornerAndResizerRect().isEmpty();
895 }
896
897 bool RenderLayerBacking::updateOverflowControlsLayers(bool needsHorizontalScrollbarLayer, bool needsVerticalScrollbarLayer, bool needsScrollCornerLayer)
898 {
899     bool layersChanged = false;
900     if (needsHorizontalScrollbarLayer) {
901         if (!m_layerForHorizontalScrollbar) {
902             m_layerForHorizontalScrollbar = createGraphicsLayer("horizontal scrollbar");
903             layersChanged = true;
904         }
905     } else if (m_layerForHorizontalScrollbar) {
906         m_layerForHorizontalScrollbar.clear();
907         layersChanged = true;
908     }
909
910     if (needsVerticalScrollbarLayer) {
911         if (!m_layerForVerticalScrollbar) {
912             m_layerForVerticalScrollbar = createGraphicsLayer("vertical scrollbar");
913             layersChanged = true;
914         }
915     } else if (m_layerForVerticalScrollbar) {
916         m_layerForVerticalScrollbar.clear();
917         layersChanged = true;
918     }
919
920     if (needsScrollCornerLayer) {
921         if (!m_layerForScrollCorner) {
922             m_layerForScrollCorner = createGraphicsLayer("scroll corner");
923             layersChanged = true;
924         }
925     } else if (m_layerForScrollCorner) {
926         m_layerForScrollCorner.clear();
927         layersChanged = true;
928     }
929
930     return layersChanged;
931 }
932
933 void RenderLayerBacking::positionOverflowControlsLayers(const IntSize& offsetFromRoot)
934 {
935     IntSize offsetFromRenderer = m_graphicsLayer->offsetFromRenderer();
936     if (GraphicsLayer* layer = layerForHorizontalScrollbar()) {
937         Scrollbar* hBar = m_owningLayer->horizontalScrollbar();
938         if (hBar) {
939             layer->setPosition(hBar->frameRect().location() - offsetFromRoot - offsetFromRenderer);
940             layer->setSize(hBar->frameRect().size());
941         }
942         layer->setDrawsContent(hBar);
943     }
944     
945     if (GraphicsLayer* layer = layerForVerticalScrollbar()) {
946         Scrollbar* vBar = m_owningLayer->verticalScrollbar();
947         if (vBar) {
948             layer->setPosition(vBar->frameRect().location() - offsetFromRoot - offsetFromRenderer);
949             layer->setSize(vBar->frameRect().size());
950         }
951         layer->setDrawsContent(vBar);
952     }
953
954     if (GraphicsLayer* layer = layerForScrollCorner()) {
955         const LayoutRect& scrollCornerAndResizer = m_owningLayer->scrollCornerAndResizerRect();
956         layer->setPosition(scrollCornerAndResizer.location() - offsetFromRenderer);
957         layer->setSize(scrollCornerAndResizer.size());
958         layer->setDrawsContent(!scrollCornerAndResizer.isEmpty());
959     }
960 }
961
962 bool RenderLayerBacking::updateForegroundLayer(bool needsForegroundLayer)
963 {
964     bool layerChanged = false;
965     if (needsForegroundLayer) {
966         if (!m_foregroundLayer) {
967             String layerName;
968 #ifndef NDEBUG
969             layerName = nameForLayer() + " (foreground)";
970 #endif
971             m_foregroundLayer = createGraphicsLayer(layerName);
972             m_foregroundLayer->setDrawsContent(true);
973             m_foregroundLayer->setPaintingPhase(GraphicsLayerPaintForeground);
974             layerChanged = true;
975         }
976     } else if (m_foregroundLayer) {
977         m_foregroundLayer->removeFromParent();
978         m_foregroundLayer = nullptr;
979         layerChanged = true;
980     }
981
982     if (layerChanged)
983         m_graphicsLayer->setPaintingPhase(paintingPhaseForPrimaryLayer());
984
985     return layerChanged;
986 }
987
988 bool RenderLayerBacking::updateMaskLayer(bool needsMaskLayer)
989 {
990     bool layerChanged = false;
991     if (needsMaskLayer) {
992         if (!m_maskLayer) {
993             m_maskLayer = createGraphicsLayer("Mask");
994             m_maskLayer->setDrawsContent(true);
995             m_maskLayer->setPaintingPhase(GraphicsLayerPaintMask);
996             layerChanged = true;
997         }
998     } else if (m_maskLayer) {
999         m_maskLayer = nullptr;
1000         layerChanged = true;
1001     }
1002
1003     if (layerChanged)
1004         m_graphicsLayer->setPaintingPhase(paintingPhaseForPrimaryLayer());
1005
1006     return layerChanged;
1007 }
1008
1009 bool RenderLayerBacking::updateScrollingLayers(bool needsScrollingLayers)
1010 {
1011     bool layerChanged = false;
1012     if (needsScrollingLayers) {
1013         if (!m_scrollingLayer) {
1014             // Outer layer which corresponds with the scroll view.
1015             m_scrollingLayer = createGraphicsLayer("Scrolling container");
1016             m_scrollingLayer->setDrawsContent(false);
1017             m_scrollingLayer->setMasksToBounds(true);
1018
1019             // Inner layer which renders the content that scrolls.
1020             m_scrollingContentsLayer = createGraphicsLayer("Scrolled Contents");
1021             m_scrollingContentsLayer->setDrawsContent(true);
1022             m_scrollingContentsLayer->setPaintingPhase(GraphicsLayerPaintForeground | GraphicsLayerPaintOverflowContents);
1023             m_scrollingLayer->addChild(m_scrollingContentsLayer.get());
1024
1025             layerChanged = true;
1026         }
1027     } else if (m_scrollingLayer) {
1028         m_scrollingLayer = nullptr;
1029         m_scrollingContentsLayer = nullptr;
1030         layerChanged = true;
1031     }
1032
1033     if (layerChanged) {
1034         updateInternalHierarchy();
1035         m_graphicsLayer->setPaintingPhase(paintingPhaseForPrimaryLayer());
1036         m_graphicsLayer->setNeedsDisplay();
1037         if (renderer()->view())
1038             compositor()->scrollingLayerDidChange(m_owningLayer);
1039     }
1040
1041     return layerChanged;
1042 }
1043
1044 void RenderLayerBacking::attachToScrollingCoordinator(RenderLayerBacking* parent)
1045 {
1046     // If m_scrollLayerID non-zero, then this backing is already attached to the ScrollingCoordinator.
1047     if (m_scrollLayerID)
1048         return;
1049
1050     Page* page = renderer()->frame()->page();
1051     if (!page)
1052         return;
1053
1054     ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator();
1055     if (!scrollingCoordinator)
1056         return;
1057
1058     // FIXME: When we support overflow areas, we will have to refine this for overflow areas that are also
1059     // positon:fixed.
1060     ScrollingNodeType nodeType;
1061     if (renderer()->style()->position() == FixedPosition)
1062         nodeType = FixedNode;
1063     else
1064         nodeType = ScrollingNode;
1065
1066     ScrollingNodeID parentID = parent ? parent->scrollLayerID() : 0;
1067     m_scrollLayerID = scrollingCoordinator->attachToStateTree(nodeType, scrollingCoordinator->uniqueScrollLayerID(), parentID);
1068 }
1069
1070 void RenderLayerBacking::detachFromScrollingCoordinator()
1071 {
1072     // If m_scrollLayerID is 0, then this backing is not attached to the ScrollingCoordinator.
1073     if (!m_scrollLayerID)
1074         return;
1075
1076     Page* page = renderer()->frame()->page();
1077     if (!page)
1078         return;
1079
1080     ScrollingCoordinator* scrollingCoordinator = page->scrollingCoordinator();
1081     if (!scrollingCoordinator)
1082         return;
1083
1084     scrollingCoordinator->detachFromStateTree(m_scrollLayerID);
1085     m_scrollLayerID = 0;
1086 }
1087
1088 GraphicsLayerPaintingPhase RenderLayerBacking::paintingPhaseForPrimaryLayer() const
1089 {
1090     unsigned phase = GraphicsLayerPaintBackground;
1091     if (!m_foregroundLayer)
1092         phase |= GraphicsLayerPaintForeground;
1093     if (!m_maskLayer)
1094         phase |= GraphicsLayerPaintMask;
1095
1096     if (m_scrollingContentsLayer)
1097         phase &= ~GraphicsLayerPaintForeground;
1098
1099     return static_cast<GraphicsLayerPaintingPhase>(phase);
1100 }
1101
1102 float RenderLayerBacking::compositingOpacity(float rendererOpacity) const
1103 {
1104     float finalOpacity = rendererOpacity;
1105     
1106     for (RenderLayer* curr = m_owningLayer->parent(); curr; curr = curr->parent()) {
1107         // We only care about parents that are stacking contexts.
1108         // Recall that opacity creates stacking context.
1109         if (!curr->isStackingContext())
1110             continue;
1111         
1112         // If we found a compositing layer, we want to compute opacity
1113         // relative to it. So we can break here.
1114         if (curr->isComposited())
1115             break;
1116         
1117         finalOpacity *= curr->renderer()->opacity();
1118     }
1119
1120     return finalOpacity;
1121 }
1122
1123 static bool hasBoxDecorations(const RenderStyle* style)
1124 {
1125     return style->hasBorder() || style->hasBorderRadius() || style->hasOutline() || style->hasAppearance() || style->boxShadow() || style->hasFilter();
1126 }
1127
1128 static bool hasBoxDecorationsOrBackground(const RenderObject* renderer)
1129 {
1130     return hasBoxDecorations(renderer->style()) || renderer->hasBackground();
1131 }
1132
1133 static bool hasBoxDecorationsOrBackgroundImage(const RenderStyle* style)
1134 {
1135     return hasBoxDecorations(style) || style->hasBackgroundImage();
1136 }
1137
1138 Color RenderLayerBacking::rendererBackgroundColor() const
1139 {
1140     RenderObject* backgroundRenderer = renderer();
1141     if (backgroundRenderer->isRoot())
1142         backgroundRenderer = backgroundRenderer->rendererForRootBackground();
1143
1144     return backgroundRenderer->style()->visitedDependentColor(CSSPropertyBackgroundColor);
1145 }
1146
1147 void RenderLayerBacking::updateBackgroundColor(bool isSimpleContainer)
1148 {
1149     Color backgroundColor = Color::transparent;
1150     if (isSimpleContainer)
1151         backgroundColor = rendererBackgroundColor();
1152     m_graphicsLayer->setContentsToBackgroundColor(backgroundColor);
1153     if (backgroundColor == Color::transparent)
1154         m_graphicsLayer->clearBackgroundColor();
1155 }
1156
1157 static bool supportsDirectBoxDecorationsComposition(const RenderObject* renderer)
1158 {
1159     if (!GraphicsLayer::supportsBackgroundColorContent())
1160         return false;
1161
1162     if (hasBoxDecorationsOrBackgroundImage(renderer->style()))
1163         return false;
1164
1165     // FIXME: we should be able to allow backgroundComposite; However since this is not a common use case it has been deferred for now.
1166     if (renderer->style()->backgroundComposite() != CompositeSourceOver)
1167         return false;
1168
1169     if (renderer->style()->backgroundClip() == TextFillBox)
1170         return false;
1171
1172     return true;
1173 }
1174
1175 bool RenderLayerBacking::paintsBoxDecorations() const
1176 {
1177     if (!m_owningLayer->hasVisibleContent())
1178         return false;
1179
1180     if (!hasBoxDecorationsOrBackground(renderer()))
1181         return false;
1182
1183     if (!supportsDirectBoxDecorationsComposition(renderer()))
1184         return true;
1185
1186     if (m_owningLayer->hasOverflowControls())
1187         return true;
1188
1189     return false;
1190 }
1191
1192 bool RenderLayerBacking::paintsChildren() const
1193 {
1194     if (m_owningLayer->hasVisibleContent() && containsNonEmptyRenderers())
1195         return true;
1196         
1197     if (hasVisibleNonCompositingDescendantLayers())
1198         return true;
1199
1200     return false;
1201 }
1202
1203 static bool isCompositedPlugin(RenderObject* renderer)
1204 {
1205     return renderer->isEmbeddedObject() && toRenderEmbeddedObject(renderer)->allowsAcceleratedCompositing();
1206 }
1207
1208 // A "simple container layer" is a RenderLayer which has no visible content to render.
1209 // It may have no children, or all its children may be themselves composited.
1210 // This is a useful optimization, because it allows us to avoid allocating backing store.
1211 bool RenderLayerBacking::isSimpleContainerCompositingLayer() const
1212 {
1213     RenderObject* renderObject = renderer();
1214     if (renderObject->hasMask()) // masks require special treatment
1215         return false;
1216
1217     if (renderObject->isReplaced() && !isCompositedPlugin(renderObject))
1218             return false;
1219     
1220     if (paintsBoxDecorations() || paintsChildren())
1221         return false;
1222     
1223     if (renderObject->node() && renderObject->node()->isDocumentNode()) {
1224         // Look to see if the root object has a non-simple background
1225         RenderObject* rootObject = renderObject->document()->documentElement() ? renderObject->document()->documentElement()->renderer() : 0;
1226         if (!rootObject)
1227             return false;
1228         
1229         RenderStyle* style = rootObject->style();
1230         
1231         // Reject anything that has a border, a border-radius or outline,
1232         // or is not a simple background (no background, or solid color).
1233         if (hasBoxDecorationsOrBackgroundImage(style))
1234             return false;
1235         
1236         // Now look at the body's renderer.
1237         HTMLElement* body = renderObject->document()->body();
1238         RenderObject* bodyObject = (body && body->hasLocalName(bodyTag)) ? body->renderer() : 0;
1239         if (!bodyObject)
1240             return false;
1241         
1242         style = bodyObject->style();
1243         
1244         if (hasBoxDecorationsOrBackgroundImage(style))
1245             return false;
1246     }
1247
1248     return true;
1249 }
1250
1251 bool RenderLayerBacking::containsNonEmptyRenderers() const
1252 {
1253     // Some HTML can cause whitespace text nodes to have renderers, like:
1254     // <div>
1255     // <img src=...>
1256     // </div>
1257     // so test for 0x0 RenderTexts here
1258     for (RenderObject* child = renderer()->firstChild(); child; child = child->nextSibling()) {
1259         if (!child->hasLayer()) {
1260             if (child->isRenderInline() || !child->isBox())
1261                 return true;
1262             
1263             if (toRenderBox(child)->width() > 0 || toRenderBox(child)->height() > 0)
1264                 return true;
1265         }
1266     }
1267     return false;
1268 }
1269
1270 // Conservative test for having no rendered children.
1271 bool RenderLayerBacking::hasVisibleNonCompositingDescendantLayers() const
1272 {
1273     // FIXME: We shouldn't be called with a stale z-order lists. See bug 85512.
1274     m_owningLayer->updateLayerListsIfNeeded();
1275
1276 #if !ASSERT_DISABLED
1277     LayerListMutationDetector mutationChecker(m_owningLayer);
1278 #endif
1279
1280     if (Vector<RenderLayer*>* normalFlowList = m_owningLayer->normalFlowList()) {
1281         size_t listSize = normalFlowList->size();
1282         for (size_t i = 0; i < listSize; ++i) {
1283             RenderLayer* curLayer = normalFlowList->at(i);
1284             if (!curLayer->isComposited() && curLayer->hasVisibleContent())
1285                 return true;
1286         }
1287     }
1288
1289     if (m_owningLayer->isStackingContext()) {
1290         if (!m_owningLayer->hasVisibleDescendant())
1291             return false;
1292
1293         // Use the m_hasCompositingDescendant bit to optimize?
1294         if (Vector<RenderLayer*>* negZOrderList = m_owningLayer->negZOrderList()) {
1295             size_t listSize = negZOrderList->size();
1296             for (size_t i = 0; i < listSize; ++i) {
1297                 RenderLayer* curLayer = negZOrderList->at(i);
1298                 if (!curLayer->isComposited() && curLayer->hasVisibleContent())
1299                     return true;
1300             }
1301         }
1302
1303         if (Vector<RenderLayer*>* posZOrderList = m_owningLayer->posZOrderList()) {
1304             size_t listSize = posZOrderList->size();
1305             for (size_t i = 0; i < listSize; ++i) {
1306                 RenderLayer* curLayer = posZOrderList->at(i);
1307                 if (!curLayer->isComposited() && curLayer->hasVisibleContent())
1308                     return true;
1309             }
1310         }
1311     }
1312
1313     return false;
1314 }
1315
1316 bool RenderLayerBacking::containsPaintedContent() const
1317 {
1318     if (isSimpleContainerCompositingLayer() || paintsIntoWindow() || paintsIntoCompositedAncestor() || m_artificiallyInflatedBounds || m_owningLayer->isReflection())
1319         return false;
1320
1321     if (isDirectlyCompositedImage())
1322         return false;
1323
1324     // FIXME: we could optimize cases where the image, video or canvas is known to fill the border box entirely,
1325     // and set background color on the layer in that case, instead of allocating backing store and painting.
1326 #if ENABLE(VIDEO)
1327     if (renderer()->isVideo() && toRenderVideo(renderer())->shouldDisplayVideo())
1328         return hasBoxDecorationsOrBackground(renderer());
1329 #endif
1330 #if PLATFORM(MAC) && USE(CA) && (PLATFORM(IOS) || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070)
1331 #elif ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
1332     if (isAcceleratedCanvas(renderer()))
1333         return hasBoxDecorationsOrBackground(renderer());
1334 #endif
1335
1336     return true;
1337 }
1338
1339 // An image can be directly compositing if it's the sole content of the layer, and has no box decorations
1340 // that require painting. Direct compositing saves backing store.
1341 bool RenderLayerBacking::isDirectlyCompositedImage() const
1342 {
1343     RenderObject* renderObject = renderer();
1344     
1345     if (!renderObject->isImage() || hasBoxDecorationsOrBackground(renderObject) || renderObject->hasClip())
1346         return false;
1347
1348     RenderImage* imageRenderer = toRenderImage(renderObject);
1349     if (CachedImage* cachedImage = imageRenderer->cachedImage()) {
1350         if (!cachedImage->hasImage())
1351             return false;
1352
1353         Image* image = cachedImage->imageForRenderer(imageRenderer);
1354         if (!image->isBitmapImage())
1355             return false;
1356
1357         return m_graphicsLayer->shouldDirectlyCompositeImage(image);
1358     }
1359
1360     return false;
1361 }
1362
1363 void RenderLayerBacking::contentChanged(ContentChangeType changeType)
1364 {
1365     if ((changeType == ImageChanged) && isDirectlyCompositedImage()) {
1366         updateImageContents();
1367         return;
1368     }
1369
1370     if ((changeType == MaskImageChanged) && m_maskLayer) {
1371         // The composited layer bounds relies on box->maskClipRect(), which changes
1372         // when the mask image becomes available.
1373         bool isUpdateRoot = true;
1374         updateAfterLayout(CompositingChildren, isUpdateRoot);
1375     }
1376
1377 #if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
1378     if ((changeType == CanvasChanged || changeType == CanvasPixelsChanged) && isAcceleratedCanvas(renderer())) {
1379         m_graphicsLayer->setContentsNeedsDisplay();
1380         return;
1381     }
1382 #endif
1383 }
1384
1385 void RenderLayerBacking::updateImageContents()
1386 {
1387     ASSERT(renderer()->isImage());
1388     RenderImage* imageRenderer = toRenderImage(renderer());
1389
1390     CachedImage* cachedImage = imageRenderer->cachedImage();
1391     if (!cachedImage)
1392         return;
1393
1394     Image* image = cachedImage->imageForRenderer(imageRenderer);
1395     if (!image)
1396         return;
1397
1398     // We have to wait until the image is fully loaded before setting it on the layer.
1399     if (!cachedImage->isLoaded())
1400         return;
1401
1402     // This is a no-op if the layer doesn't have an inner layer for the image.
1403     m_graphicsLayer->setContentsToImage(image);
1404     bool isSimpleContainer = false;
1405     updateDrawsContent(isSimpleContainer);
1406     
1407     // Image animation is "lazy", in that it automatically stops unless someone is drawing
1408     // the image. So we have to kick the animation each time; this has the downside that the
1409     // image will keep animating, even if its layer is not visible.
1410     image->startAnimation();
1411 }
1412
1413 FloatPoint3D RenderLayerBacking::computeTransformOrigin(const IntRect& borderBox) const
1414 {
1415     RenderStyle* style = renderer()->style();
1416
1417     FloatPoint3D origin;
1418     origin.setX(floatValueForLength(style->transformOriginX(), borderBox.width()));
1419     origin.setY(floatValueForLength(style->transformOriginY(), borderBox.height()));
1420     origin.setZ(style->transformOriginZ());
1421
1422     return origin;
1423 }
1424
1425 FloatPoint RenderLayerBacking::computePerspectiveOrigin(const IntRect& borderBox) const
1426 {
1427     RenderStyle* style = renderer()->style();
1428
1429     float boxWidth = borderBox.width();
1430     float boxHeight = borderBox.height();
1431
1432     FloatPoint origin;
1433     origin.setX(floatValueForLength(style->perspectiveOriginX(), boxWidth));
1434     origin.setY(floatValueForLength(style->perspectiveOriginY(), boxHeight));
1435
1436     return origin;
1437 }
1438
1439 // Return the offset from the top-left of this compositing layer at which the renderer's contents are painted.
1440 IntSize RenderLayerBacking::contentOffsetInCompostingLayer() const
1441 {
1442     return IntSize(-m_compositedBounds.x(), -m_compositedBounds.y());
1443 }
1444
1445 IntRect RenderLayerBacking::contentsBox() const
1446 {
1447     if (!renderer()->isBox())
1448         return IntRect();
1449
1450     IntRect contentsRect;
1451 #if ENABLE(VIDEO)
1452     if (renderer()->isVideo()) {
1453         RenderVideo* videoRenderer = toRenderVideo(renderer());
1454         contentsRect = videoRenderer->videoBox();
1455     } else
1456 #endif
1457         contentsRect = pixelSnappedIntRect(toRenderBox(renderer())->contentBoxRect());
1458
1459     contentsRect.move(contentOffsetInCompostingLayer());
1460     return contentsRect;
1461 }
1462
1463 static LayoutRect backgroundRectForBox(const RenderBox* box)
1464 {
1465     EFillBox clip = box->style()->backgroundClip();
1466     switch (clip) {
1467     case BorderFillBox:
1468         return box->borderBoxRect();
1469     case PaddingFillBox:
1470         return box->paddingBoxRect();
1471     case ContentFillBox:
1472         return box->contentBoxRect();
1473     case TextFillBox:
1474         break;
1475     }
1476
1477     ASSERT_NOT_REACHED();
1478     return LayoutRect();
1479 }
1480
1481 IntRect RenderLayerBacking::backgroundBox() const
1482 {
1483     if (!renderer()->isBox())
1484         return IntRect();
1485
1486     IntRect pixelSnappedBackgroundBox = pixelSnappedIntRect(backgroundRectForBox(toRenderBox(renderer())));
1487     pixelSnappedBackgroundBox.move(contentOffsetInCompostingLayer());
1488     return pixelSnappedBackgroundBox;
1489 }
1490
1491 GraphicsLayer* RenderLayerBacking::parentForSublayers() const
1492 {
1493     if (m_scrollingContentsLayer)
1494         return m_scrollingContentsLayer.get();
1495
1496     return m_containmentLayer ? m_containmentLayer.get() : m_graphicsLayer.get();
1497 }
1498
1499 bool RenderLayerBacking::paintsIntoWindow() const
1500 {
1501     if (m_usingTiledCacheLayer)
1502         return false;
1503
1504     if (m_owningLayer->isRootLayer()) {
1505 #if PLATFORM(BLACKBERRY)
1506         if (compositor()->inForcedCompositingMode())
1507             return false;
1508 #endif
1509
1510         return compositor()->rootLayerAttachment() != RenderLayerCompositor::RootLayerAttachedViaEnclosingFrame;
1511     }
1512     
1513     return false;
1514 }
1515
1516 void RenderLayerBacking::setRequiresOwnBackingStore(bool requiresOwnBacking)
1517 {
1518     if (requiresOwnBacking == m_requiresOwnBackingStore)
1519         return;
1520     
1521     m_requiresOwnBackingStore = requiresOwnBacking;
1522
1523     // This affects the answer to paintsIntoCompositedAncestor(), which in turn affects
1524     // cached clip rects, so when it changes we have to clear clip rects on descendants.
1525     m_owningLayer->clearClipRectsIncludingDescendants(PaintingClipRects);
1526     m_owningLayer->computeRepaintRectsIncludingDescendants();
1527     
1528     compositor()->repaintInCompositedAncestor(m_owningLayer, compositedBounds());
1529 }
1530
1531 #if ENABLE(CSS_COMPOSITING)
1532 void RenderLayerBacking::setBlendMode(BlendMode)
1533 {
1534 }
1535 #endif
1536
1537 void RenderLayerBacking::setContentsNeedDisplay()
1538 {
1539     ASSERT(!paintsIntoCompositedAncestor());
1540     
1541     if (m_graphicsLayer && m_graphicsLayer->drawsContent())
1542         m_graphicsLayer->setNeedsDisplay();
1543     
1544     if (m_foregroundLayer && m_foregroundLayer->drawsContent())
1545         m_foregroundLayer->setNeedsDisplay();
1546
1547     if (m_maskLayer && m_maskLayer->drawsContent())
1548         m_maskLayer->setNeedsDisplay();
1549
1550     if (m_scrollingContentsLayer && m_scrollingContentsLayer->drawsContent())
1551         m_scrollingContentsLayer->setNeedsDisplay();
1552 }
1553
1554 // r is in the coordinate space of the layer's render object
1555 void RenderLayerBacking::setContentsNeedDisplayInRect(const IntRect& r)
1556 {
1557     ASSERT(!paintsIntoCompositedAncestor());
1558
1559     if (m_graphicsLayer && m_graphicsLayer->drawsContent()) {
1560         IntRect layerDirtyRect = r;
1561         layerDirtyRect.move(-m_graphicsLayer->offsetFromRenderer());
1562         m_graphicsLayer->setNeedsDisplayInRect(layerDirtyRect);
1563     }
1564
1565     if (m_foregroundLayer && m_foregroundLayer->drawsContent()) {
1566         IntRect layerDirtyRect = r;
1567         layerDirtyRect.move(-m_foregroundLayer->offsetFromRenderer());
1568         m_foregroundLayer->setNeedsDisplayInRect(layerDirtyRect);
1569     }
1570
1571     if (m_maskLayer && m_maskLayer->drawsContent()) {
1572         IntRect layerDirtyRect = r;
1573         layerDirtyRect.move(-m_maskLayer->offsetFromRenderer());
1574         m_maskLayer->setNeedsDisplayInRect(layerDirtyRect);
1575     }
1576
1577     if (m_scrollingContentsLayer && m_scrollingContentsLayer->drawsContent()) {
1578         IntRect layerDirtyRect = r;
1579         layerDirtyRect.move(-m_scrollingContentsLayer->offsetFromRenderer());
1580         m_scrollingContentsLayer->setNeedsDisplayInRect(layerDirtyRect);
1581     }
1582 }
1583
1584 void RenderLayerBacking::paintIntoLayer(RenderLayer* rootLayer, GraphicsContext* context,
1585                     const IntRect& paintDirtyRect, // In the coords of rootLayer.
1586                     PaintBehavior paintBehavior, GraphicsLayerPaintingPhase paintingPhase)
1587 {
1588     if (paintsIntoWindow() || paintsIntoCompositedAncestor()) {
1589         ASSERT_NOT_REACHED();
1590         return;
1591     }
1592
1593     FontCachePurgePreventer fontCachePurgePreventer;
1594     
1595     RenderLayer::PaintLayerFlags paintFlags = 0;
1596     if (paintingPhase & GraphicsLayerPaintBackground)
1597         paintFlags |= RenderLayer::PaintLayerPaintingCompositingBackgroundPhase;
1598     if (paintingPhase & GraphicsLayerPaintForeground)
1599         paintFlags |= RenderLayer::PaintLayerPaintingCompositingForegroundPhase;
1600     if (paintingPhase & GraphicsLayerPaintMask)
1601         paintFlags |= RenderLayer::PaintLayerPaintingCompositingMaskPhase;
1602     if (paintingPhase & GraphicsLayerPaintOverflowContents)
1603         paintFlags |= RenderLayer::PaintLayerPaintingOverflowContents;
1604     
1605     // FIXME: GraphicsLayers need a way to split for RenderRegions.
1606     RenderLayer::LayerPaintingInfo paintingInfo(rootLayer, paintDirtyRect, paintBehavior, LayoutSize());
1607     m_owningLayer->paintLayerContents(context, paintingInfo, paintFlags);
1608
1609     if (m_owningLayer->containsDirtyOverlayScrollbars())
1610         m_owningLayer->paintLayerContents(context, paintingInfo, paintFlags | RenderLayer::PaintLayerPaintingOverlayScrollbars);
1611
1612     ASSERT(!m_owningLayer->m_usedTransparency);
1613 }
1614
1615 static void paintScrollbar(Scrollbar* scrollbar, GraphicsContext& context, const IntRect& clip)
1616 {
1617     if (!scrollbar)
1618         return;
1619
1620     context.save();
1621     const IntRect& scrollbarRect = scrollbar->frameRect();
1622     context.translate(-scrollbarRect.x(), -scrollbarRect.y());
1623     IntRect transformedClip = clip;
1624     transformedClip.moveBy(scrollbarRect.location());
1625     scrollbar->paint(&context, transformedClip);
1626     context.restore();
1627 }
1628
1629 // Up-call from compositing layer drawing callback.
1630 void RenderLayerBacking::paintContents(const GraphicsLayer* graphicsLayer, GraphicsContext& context, GraphicsLayerPaintingPhase paintingPhase, const IntRect& clip)
1631 {
1632 #ifndef NDEBUG
1633     if (Page* page = renderer()->frame()->page())
1634         page->setIsPainting(true);
1635 #endif
1636
1637     if (graphicsLayer == m_graphicsLayer.get() || graphicsLayer == m_foregroundLayer.get() || graphicsLayer == m_maskLayer.get() || graphicsLayer == m_scrollingContentsLayer.get()) {
1638         InspectorInstrumentationCookie cookie = InspectorInstrumentation::willPaint(m_owningLayer->renderer()->frame());
1639
1640         // The dirtyRect is in the coords of the painting root.
1641         IntRect dirtyRect = clip;
1642         if (!(paintingPhase & GraphicsLayerPaintOverflowContents))
1643             dirtyRect.intersect(compositedBounds());
1644
1645         // We have to use the same root as for hit testing, because both methods can compute and cache clipRects.
1646         paintIntoLayer(m_owningLayer, &context, dirtyRect, PaintBehaviorNormal, paintingPhase);
1647
1648         if (m_usingTiledCacheLayer)
1649             m_owningLayer->renderer()->frame()->view()->setLastPaintTime(currentTime());
1650
1651         InspectorInstrumentation::didPaint(cookie, &context, clip);
1652     } else if (graphicsLayer == layerForHorizontalScrollbar()) {
1653         paintScrollbar(m_owningLayer->horizontalScrollbar(), context, clip);
1654     } else if (graphicsLayer == layerForVerticalScrollbar()) {
1655         paintScrollbar(m_owningLayer->verticalScrollbar(), context, clip);
1656     } else if (graphicsLayer == layerForScrollCorner()) {
1657         const IntRect& scrollCornerAndResizer = m_owningLayer->scrollCornerAndResizerRect();
1658         context.save();
1659         context.translate(-scrollCornerAndResizer.x(), -scrollCornerAndResizer.y());
1660         IntRect transformedClip = clip;
1661         transformedClip.moveBy(scrollCornerAndResizer.location());
1662         m_owningLayer->paintScrollCorner(&context, IntPoint(), transformedClip);
1663         m_owningLayer->paintResizer(&context, IntPoint(), transformedClip);
1664         context.restore();
1665     }
1666 #ifndef NDEBUG
1667     if (Page* page = renderer()->frame()->page())
1668         page->setIsPainting(false);
1669 #endif
1670 }
1671
1672 float RenderLayerBacking::pageScaleFactor() const
1673 {
1674     return compositor()->pageScaleFactor();
1675 }
1676
1677 float RenderLayerBacking::deviceScaleFactor() const
1678 {
1679     return compositor()->deviceScaleFactor();
1680 }
1681
1682 void RenderLayerBacking::didCommitChangesForLayer(const GraphicsLayer*) const
1683 {
1684     compositor()->didFlushChangesForLayer(m_owningLayer);
1685 }
1686
1687 bool RenderLayerBacking::getCurrentTransform(const GraphicsLayer* graphicsLayer, TransformationMatrix& transform) const
1688 {
1689     if (graphicsLayer != m_graphicsLayer)
1690         return false;
1691
1692     if (m_owningLayer->hasTransform()) {
1693         transform = m_owningLayer->currentTransform(RenderStyle::ExcludeTransformOrigin);
1694         return true;
1695     }
1696     return false;
1697 }
1698
1699 bool RenderLayerBacking::isTrackingRepaints() const
1700 {
1701     GraphicsLayerClient* client = compositor();
1702     return client ? client->isTrackingRepaints() : false;
1703 }
1704
1705 #ifndef NDEBUG
1706 void RenderLayerBacking::verifyNotPainting()
1707 {
1708     ASSERT(!renderer()->frame()->page() || !renderer()->frame()->page()->isPainting());
1709 }
1710 #endif
1711
1712 bool RenderLayerBacking::startAnimation(double timeOffset, const Animation* anim, const KeyframeList& keyframes)
1713 {
1714     bool hasOpacity = keyframes.containsProperty(CSSPropertyOpacity);
1715     bool hasTransform = renderer()->isBox() && keyframes.containsProperty(CSSPropertyWebkitTransform);
1716 #if ENABLE(CSS_FILTERS)
1717     bool hasFilter = keyframes.containsProperty(CSSPropertyWebkitFilter);
1718 #else
1719     bool hasFilter = false;
1720 #endif
1721
1722     if (!hasOpacity && !hasTransform && !hasFilter)
1723         return false;
1724     
1725     KeyframeValueList transformVector(AnimatedPropertyWebkitTransform);
1726     KeyframeValueList opacityVector(AnimatedPropertyOpacity);
1727 #if ENABLE(CSS_FILTERS)
1728     KeyframeValueList filterVector(AnimatedPropertyWebkitFilter);
1729 #endif
1730
1731     size_t numKeyframes = keyframes.size();
1732     for (size_t i = 0; i < numKeyframes; ++i) {
1733         const KeyframeValue& currentKeyframe = keyframes[i];
1734         const RenderStyle* keyframeStyle = currentKeyframe.style();
1735         float key = currentKeyframe.key();
1736
1737         if (!keyframeStyle)
1738             continue;
1739             
1740         // Get timing function.
1741         RefPtr<TimingFunction> tf = keyframeStyle->hasAnimations() ? (*keyframeStyle->animations()).animation(0)->timingFunction() : 0;
1742         
1743         bool isFirstOrLastKeyframe = key == 0 || key == 1;
1744         if ((hasTransform && isFirstOrLastKeyframe) || currentKeyframe.containsProperty(CSSPropertyWebkitTransform))
1745             transformVector.insert(new TransformAnimationValue(key, &(keyframeStyle->transform()), tf));
1746         
1747         if ((hasOpacity && isFirstOrLastKeyframe) || currentKeyframe.containsProperty(CSSPropertyOpacity))
1748             opacityVector.insert(new FloatAnimationValue(key, keyframeStyle->opacity(), tf));
1749
1750 #if ENABLE(CSS_FILTERS)
1751         if ((hasFilter && isFirstOrLastKeyframe) || currentKeyframe.containsProperty(CSSPropertyWebkitFilter))
1752             filterVector.insert(new FilterAnimationValue(key, &(keyframeStyle->filter()), tf));
1753 #endif
1754     }
1755
1756     bool didAnimateTransform = false;
1757     bool didAnimateOpacity = false;
1758 #if ENABLE(CSS_FILTERS)
1759     bool didAnimateFilter = false;
1760 #endif
1761     
1762     if (hasTransform && m_graphicsLayer->addAnimation(transformVector, toRenderBox(renderer())->pixelSnappedBorderBoxRect().size(), anim, keyframes.animationName(), timeOffset))
1763         didAnimateTransform = true;
1764
1765     if (hasOpacity && m_graphicsLayer->addAnimation(opacityVector, IntSize(), anim, keyframes.animationName(), timeOffset))
1766         didAnimateOpacity = true;
1767
1768 #if ENABLE(CSS_FILTERS)
1769     if (hasFilter && m_graphicsLayer->addAnimation(filterVector, IntSize(), anim, keyframes.animationName(), timeOffset))
1770         didAnimateFilter = true;
1771 #endif
1772
1773 #if ENABLE(CSS_FILTERS)
1774     return didAnimateTransform || didAnimateOpacity || didAnimateFilter;
1775 #else
1776     return didAnimateTransform || didAnimateOpacity;
1777 #endif
1778 }
1779
1780 void RenderLayerBacking::animationPaused(double timeOffset, const String& animationName)
1781 {
1782     m_graphicsLayer->pauseAnimation(animationName, timeOffset);
1783 }
1784
1785 void RenderLayerBacking::animationFinished(const String& animationName)
1786 {
1787     m_graphicsLayer->removeAnimation(animationName);
1788 }
1789
1790 bool RenderLayerBacking::startTransition(double timeOffset, CSSPropertyID property, const RenderStyle* fromStyle, const RenderStyle* toStyle)
1791 {
1792     bool didAnimateOpacity = false;
1793     bool didAnimateTransform = false;
1794 #if ENABLE(CSS_FILTERS)
1795     bool didAnimateFilter = false;
1796 #endif
1797
1798     ASSERT(property != CSSPropertyInvalid);
1799
1800     if (property == CSSPropertyOpacity) {
1801         const Animation* opacityAnim = toStyle->transitionForProperty(CSSPropertyOpacity);
1802         if (opacityAnim && !opacityAnim->isEmptyOrZeroDuration()) {
1803             KeyframeValueList opacityVector(AnimatedPropertyOpacity);
1804             opacityVector.insert(new FloatAnimationValue(0, compositingOpacity(fromStyle->opacity())));
1805             opacityVector.insert(new FloatAnimationValue(1, compositingOpacity(toStyle->opacity())));
1806             // The boxSize param is only used for transform animations (which can only run on RenderBoxes), so we pass an empty size here.
1807             if (m_graphicsLayer->addAnimation(opacityVector, IntSize(), opacityAnim, GraphicsLayer::animationNameForTransition(AnimatedPropertyOpacity), timeOffset)) {
1808                 // To ensure that the correct opacity is visible when the animation ends, also set the final opacity.
1809                 updateOpacity(toStyle);
1810                 didAnimateOpacity = true;
1811             }
1812         }
1813     }
1814
1815     if (property == CSSPropertyWebkitTransform && m_owningLayer->hasTransform()) {
1816         const Animation* transformAnim = toStyle->transitionForProperty(CSSPropertyWebkitTransform);
1817         if (transformAnim && !transformAnim->isEmptyOrZeroDuration()) {
1818             KeyframeValueList transformVector(AnimatedPropertyWebkitTransform);
1819             transformVector.insert(new TransformAnimationValue(0, &fromStyle->transform()));
1820             transformVector.insert(new TransformAnimationValue(1, &toStyle->transform()));
1821             if (m_graphicsLayer->addAnimation(transformVector, toRenderBox(renderer())->pixelSnappedBorderBoxRect().size(), transformAnim, GraphicsLayer::animationNameForTransition(AnimatedPropertyWebkitTransform), timeOffset)) {
1822                 // To ensure that the correct transform is visible when the animation ends, also set the final transform.
1823                 updateTransform(toStyle);
1824                 didAnimateTransform = true;
1825             }
1826         }
1827     }
1828
1829 #if ENABLE(CSS_FILTERS)
1830     if (property == CSSPropertyWebkitFilter && m_owningLayer->hasFilter()) {
1831         const Animation* filterAnim = toStyle->transitionForProperty(CSSPropertyWebkitFilter);
1832         if (filterAnim && !filterAnim->isEmptyOrZeroDuration()) {
1833             KeyframeValueList filterVector(AnimatedPropertyWebkitFilter);
1834             filterVector.insert(new FilterAnimationValue(0, &fromStyle->filter()));
1835             filterVector.insert(new FilterAnimationValue(1, &toStyle->filter()));
1836             if (m_graphicsLayer->addAnimation(filterVector, IntSize(), filterAnim, GraphicsLayer::animationNameForTransition(AnimatedPropertyWebkitFilter), timeOffset)) {
1837                 // To ensure that the correct filter is visible when the animation ends, also set the final filter.
1838                 updateFilters(toStyle);
1839                 didAnimateFilter = true;
1840             }
1841         }
1842     }
1843 #endif
1844
1845 #if ENABLE(CSS_FILTERS)
1846     return didAnimateOpacity || didAnimateTransform || didAnimateFilter;
1847 #else
1848     return didAnimateOpacity || didAnimateTransform;
1849 #endif
1850 }
1851
1852 void RenderLayerBacking::transitionPaused(double timeOffset, CSSPropertyID property)
1853 {
1854     AnimatedPropertyID animatedProperty = cssToGraphicsLayerProperty(property);
1855     if (animatedProperty != AnimatedPropertyInvalid)
1856         m_graphicsLayer->pauseAnimation(GraphicsLayer::animationNameForTransition(animatedProperty), timeOffset);
1857 }
1858
1859 void RenderLayerBacking::transitionFinished(CSSPropertyID property)
1860 {
1861     AnimatedPropertyID animatedProperty = cssToGraphicsLayerProperty(property);
1862     if (animatedProperty != AnimatedPropertyInvalid)
1863         m_graphicsLayer->removeAnimation(GraphicsLayer::animationNameForTransition(animatedProperty));
1864 }
1865
1866 void RenderLayerBacking::notifyAnimationStarted(const GraphicsLayer*, double time)
1867 {
1868     renderer()->animation()->notifyAnimationStarted(renderer(), time);
1869 }
1870
1871 void RenderLayerBacking::notifyFlushRequired(const GraphicsLayer*)
1872 {
1873     if (!renderer()->documentBeingDestroyed())
1874         compositor()->scheduleLayerFlush();
1875 }
1876
1877 void RenderLayerBacking::notifyFlushBeforeDisplayRefresh(const GraphicsLayer* layer)
1878 {
1879     compositor()->notifyFlushBeforeDisplayRefresh(layer);
1880 }
1881
1882 // This is used for the 'freeze' API, for testing only.
1883 void RenderLayerBacking::suspendAnimations(double time)
1884 {
1885     m_graphicsLayer->suspendAnimations(time);
1886 }
1887
1888 void RenderLayerBacking::resumeAnimations()
1889 {
1890     m_graphicsLayer->resumeAnimations();
1891 }
1892
1893 IntRect RenderLayerBacking::compositedBounds() const
1894 {
1895     return m_compositedBounds;
1896 }
1897
1898 void RenderLayerBacking::setCompositedBounds(const IntRect& bounds)
1899 {
1900     m_compositedBounds = bounds;
1901 }
1902
1903 CSSPropertyID RenderLayerBacking::graphicsLayerToCSSProperty(AnimatedPropertyID property)
1904 {
1905     CSSPropertyID cssProperty = CSSPropertyInvalid;
1906     switch (property) {
1907         case AnimatedPropertyWebkitTransform:
1908             cssProperty = CSSPropertyWebkitTransform;
1909             break;
1910         case AnimatedPropertyOpacity:
1911             cssProperty = CSSPropertyOpacity;
1912             break;
1913         case AnimatedPropertyBackgroundColor:
1914             cssProperty = CSSPropertyBackgroundColor;
1915             break;
1916         case AnimatedPropertyWebkitFilter:
1917 #if ENABLE(CSS_FILTERS)
1918             cssProperty = CSSPropertyWebkitFilter;
1919 #else
1920             ASSERT_NOT_REACHED();
1921 #endif
1922             break;
1923         case AnimatedPropertyInvalid:
1924             ASSERT_NOT_REACHED();
1925     }
1926     return cssProperty;
1927 }
1928
1929 AnimatedPropertyID RenderLayerBacking::cssToGraphicsLayerProperty(CSSPropertyID cssProperty)
1930 {
1931     switch (cssProperty) {
1932         case CSSPropertyWebkitTransform:
1933             return AnimatedPropertyWebkitTransform;
1934         case CSSPropertyOpacity:
1935             return AnimatedPropertyOpacity;
1936         case CSSPropertyBackgroundColor:
1937             return AnimatedPropertyBackgroundColor;
1938 #if ENABLE(CSS_FILTERS)
1939         case CSSPropertyWebkitFilter:
1940             return AnimatedPropertyWebkitFilter;
1941 #endif
1942         default:
1943             // It's fine if we see other css properties here; they are just not accelerated.
1944             break;
1945     }
1946     return AnimatedPropertyInvalid;
1947 }
1948
1949 String RenderLayerBacking::nameForLayer() const
1950 {
1951     StringBuilder name;
1952     name.append(renderer()->renderName());
1953     if (Node* node = renderer()->node()) {
1954         if (node->isElementNode()) {
1955             name.append(' ');
1956             name.append(static_cast<Element*>(node)->tagName());
1957         }
1958         if (node->hasID()) {
1959             name.appendLiteral(" id=\'");
1960             name.append(static_cast<Element*>(node)->getIdAttribute());
1961             name.append('\'');
1962         }
1963
1964         if (node->hasClass()) {
1965             name.appendLiteral(" class=\'");
1966             StyledElement* styledElement = static_cast<StyledElement*>(node);
1967             for (size_t i = 0; i < styledElement->classNames().size(); ++i) {
1968                 if (i > 0)
1969                     name.append(' ');
1970                 name.append(styledElement->classNames()[i]);
1971             }
1972             name.append('\'');
1973         }
1974     }
1975
1976     if (m_owningLayer->isReflection())
1977         name.appendLiteral(" (reflection)");
1978
1979     return name.toString();
1980 }
1981
1982 CompositingLayerType RenderLayerBacking::compositingLayerType() const
1983 {
1984     if (m_graphicsLayer->hasContentsLayer())
1985         return MediaCompositingLayer;
1986
1987     if (m_graphicsLayer->drawsContent())
1988         return m_graphicsLayer->usingTiledLayer() ? TiledCompositingLayer : NormalCompositingLayer;
1989     
1990     return ContainerCompositingLayer;
1991 }
1992
1993 double RenderLayerBacking::backingStoreMemoryEstimate() const
1994 {
1995     double backingMemory;
1996     
1997     // m_ancestorClippingLayer and m_containmentLayer are just used for masking or containment, so have no backing.
1998     backingMemory = m_graphicsLayer->backingStoreMemoryEstimate();
1999     if (m_foregroundLayer)
2000         backingMemory += m_foregroundLayer->backingStoreMemoryEstimate();
2001     if (m_maskLayer)
2002         backingMemory += m_maskLayer->backingStoreMemoryEstimate();
2003
2004     if (m_scrollingContentsLayer)
2005         backingMemory += m_scrollingContentsLayer->backingStoreMemoryEstimate();
2006
2007     if (m_layerForHorizontalScrollbar)
2008         backingMemory += m_layerForHorizontalScrollbar->backingStoreMemoryEstimate();
2009
2010     if (m_layerForVerticalScrollbar)
2011         backingMemory += m_layerForVerticalScrollbar->backingStoreMemoryEstimate();
2012
2013     if (m_layerForScrollCorner)
2014         backingMemory += m_layerForScrollCorner->backingStoreMemoryEstimate();
2015     
2016     return backingMemory;
2017 }
2018
2019 #if PLATFORM(BLACKBERRY)
2020 bool RenderLayerBacking::contentsVisible(const GraphicsLayer*, const IntRect& localContentRect) const
2021 {
2022     Frame* frame = renderer()->frame();
2023     FrameView* view = frame ? frame->view() : 0;
2024     if (!view)
2025         return false;
2026
2027     IntRect visibleContentRect(view->visibleContentRect());
2028     FloatQuad absoluteContentQuad = renderer()->localToAbsoluteQuad(FloatRect(localContentRect), SnapOffsetForTransforms);
2029     return absoluteContentQuad.enclosingBoundingBox().intersects(visibleContentRect);
2030 }
2031 #endif
2032
2033 } // namespace WebCore
2034
2035 #endif // USE(ACCELERATED_COMPOSITING)