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