2 * Copyright (C) 2009, 2010, 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #if USE(ACCELERATED_COMPOSITING)
30 #include "RenderLayerBacking.h"
32 #include "AnimationController.h"
33 #include "CanvasRenderingContext.h"
34 #include "CSSPropertyNames.h"
35 #include "CachedImage.h"
37 #include "FontCache.h"
38 #include "FrameView.h"
39 #include "GraphicsContext.h"
40 #include "GraphicsLayer.h"
41 #include "HTMLCanvasElement.h"
42 #include "HTMLIFrameElement.h"
43 #include "HTMLMediaElement.h"
44 #include "HTMLNames.h"
45 #include "HTMLPlugInElement.h"
46 #include "InspectorInstrumentation.h"
47 #include "KeyframeList.h"
48 #include "MainFrame.h"
49 #include "PluginViewBase.h"
50 #include "ProgressTracker.h"
51 #include "RenderFlowThread.h"
52 #include "RenderIFrame.h"
53 #include "RenderImage.h"
54 #include "RenderLayerCompositor.h"
55 #include "RenderEmbeddedObject.h"
56 #include "RenderNamedFlowFragment.h"
57 #include "RenderRegion.h"
58 #include "RenderVideo.h"
59 #include "RenderView.h"
60 #include "ScrollingCoordinator.h"
62 #include "StyleResolver.h"
63 #include "TiledBacking.h"
64 #include <wtf/text/StringBuilder.h>
66 #if ENABLE(CSS_FILTERS)
67 #include "FilterEffectRenderer.h"
68 #if ENABLE(CSS_SHADERS)
69 #include "CustomFilterOperation.h"
73 #if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
74 #include "GraphicsContext3D.h"
79 using namespace HTMLNames;
81 static bool hasBoxDecorationsOrBackgroundImage(const RenderStyle*);
82 static IntRect clipBox(RenderBox& renderer);
84 CanvasCompositingStrategy canvasCompositingStrategy(const RenderObject& renderer)
86 ASSERT(renderer.isCanvas());
88 const HTMLCanvasElement* canvas = toHTMLCanvasElement(renderer.node());
89 CanvasRenderingContext* context = canvas->renderingContext();
90 if (!context || !context->isAccelerated())
91 return UnacceleratedCanvas;
94 return CanvasAsLayerContents;
96 #if ENABLE(ACCELERATED_2D_CANVAS)
97 return CanvasAsLayerContents;
99 return CanvasPaintedToLayer; // On Mac and iOS we paint accelerated canvases into their layers.
103 // Get the scrolling coordinator in a way that works inside RenderLayerBacking's destructor.
104 static ScrollingCoordinator* scrollingCoordinatorFromLayer(RenderLayer& layer)
106 Page* page = layer.renderer().frame().page();
110 return page->scrollingCoordinator();
113 bool RenderLayerBacking::m_creatingPrimaryGraphicsLayer = false;
115 RenderLayerBacking::RenderLayerBacking(RenderLayer& layer)
116 : m_owningLayer(layer)
118 , m_artificiallyInflatedBounds(false)
119 , m_isMainFrameRenderViewLayer(false)
120 , m_usingTiledCacheLayer(false)
121 , m_requiresOwnBackingStore(true)
122 #if ENABLE(CSS_FILTERS)
123 , m_canCompositeFilters(false)
125 , m_backgroundLayerPaintsFixedRootBackground(false)
126 , m_didSwitchToFullTileCoverageDuringLoading(false)
128 Page* page = renderer().frame().page();
130 if (layer.isRootLayer() && page) {
131 if (renderer().frame().isMainFrame())
132 m_isMainFrameRenderViewLayer = true;
134 m_usingTiledCacheLayer = page->chrome().client().shouldUseTiledBackingForFrameView(renderer().frame().view());
137 createPrimaryGraphicsLayer();
139 if (m_usingTiledCacheLayer && page) {
140 TiledBacking* tiledBacking = this->tiledBacking();
142 tiledBacking->setIsInWindow(page->isInWindow());
144 if (m_isMainFrameRenderViewLayer) {
145 tiledBacking->setExposedRect(renderer().frame().view()->exposedRect());
146 tiledBacking->setUnparentsOffscreenTiles(true);
147 if (page->settings().backgroundShouldExtendBeyondPage())
148 tiledBacking->setTileMargins(512, 512, 512, 512);
151 tiledBacking->setScrollingPerformanceLoggingEnabled(page->settings().scrollingPerformanceLoggingEnabled());
152 adjustTiledBackingCoverage();
156 RenderLayerBacking::~RenderLayerBacking()
158 updateAncestorClippingLayer(false);
159 updateDescendantClippingLayer(false);
160 updateOverflowControlsLayers(false, false, false);
161 updateForegroundLayer(false);
162 updateBackgroundLayer(false);
163 updateMaskLayer(false);
164 updateScrollingLayers(false);
165 detachFromScrollingCoordinator();
166 destroyGraphicsLayers();
169 void RenderLayerBacking::willDestroyLayer(const GraphicsLayer* layer)
171 if (layer && layer->usingTiledBacking())
172 compositor().layerTiledBackingUsageChanged(layer, false);
175 std::unique_ptr<GraphicsLayer> RenderLayerBacking::createGraphicsLayer(const String& name)
177 GraphicsLayerFactory* graphicsLayerFactory = 0;
178 if (Page* page = renderer().frame().page())
179 graphicsLayerFactory = page->chrome().client().graphicsLayerFactory();
181 std::unique_ptr<GraphicsLayer> graphicsLayer = GraphicsLayer::create(graphicsLayerFactory, this);
184 graphicsLayer->setName(name);
188 graphicsLayer->setMaintainsPixelAlignment(compositor().keepLayersPixelAligned());
190 #if PLATFORM(MAC) && USE(CA)
191 graphicsLayer->setAcceleratesDrawing(compositor().acceleratedDrawingEnabled());
194 return graphicsLayer;
197 bool RenderLayerBacking::shouldUseTiledBacking(const GraphicsLayer*) const
199 return m_usingTiledCacheLayer && m_creatingPrimaryGraphicsLayer;
202 bool RenderLayerBacking::tiledBackingHasMargin() const
204 return m_usingTiledCacheLayer && tiledBacking()->hasMargins();
207 void RenderLayerBacking::tiledBackingUsageChanged(const GraphicsLayer* layer, bool usingTiledBacking)
209 compositor().layerTiledBackingUsageChanged(layer, usingTiledBacking);
212 TiledBacking* RenderLayerBacking::tiledBacking() const
214 return m_graphicsLayer->tiledBacking();
217 static TiledBacking::TileCoverage computeTileCoverage(RenderLayerBacking* backing)
219 // FIXME: When we use TiledBacking for overflow, this should look at RenderView scrollability.
220 FrameView& frameView = backing->owningLayer().renderer().view().frameView();
222 TiledBacking::TileCoverage tileCoverage = TiledBacking::CoverageForVisibleArea;
223 bool useMinimalTilesDuringLiveResize = frameView.inLiveResize();
224 bool useMinimalTilesDuringLoading = false;
226 if (!backing->didSwitchToFullTileCoverageDuringLoading()) {
227 useMinimalTilesDuringLoading = !frameView.isVisuallyNonEmpty() || (frameView.frame().page()->progress().isMainLoadProgressing() && !frameView.wasScrolledByUser());
228 if (!useMinimalTilesDuringLoading)
229 backing->setDidSwitchToFullTileCoverageDuringLoading();
231 if (!(useMinimalTilesDuringLoading || useMinimalTilesDuringLiveResize)) {
232 bool clipsToExposedRect = !frameView.exposedRect().isInfinite();
233 if (frameView.horizontalScrollbarMode() != ScrollbarAlwaysOff || clipsToExposedRect)
234 tileCoverage |= TiledBacking::CoverageForHorizontalScrolling;
236 if (frameView.verticalScrollbarMode() != ScrollbarAlwaysOff || clipsToExposedRect)
237 tileCoverage |= TiledBacking::CoverageForVerticalScrolling;
239 if (ScrollingCoordinator* scrollingCoordinator = scrollingCoordinatorFromLayer(backing->owningLayer())) {
240 // Ask our TiledBacking for large tiles unless the only reason we're main-thread-scrolling
241 // is a page overlay (find-in-page, the Web Inspector highlight mechanism, etc.).
242 if (scrollingCoordinator->synchronousScrollingReasons() & ~ScrollingCoordinator::ForcedOnMainThread)
243 tileCoverage |= TiledBacking::CoverageForSlowScrolling;
248 void RenderLayerBacking::adjustTiledBackingCoverage()
250 if (!m_usingTiledCacheLayer)
253 TiledBacking::TileCoverage tileCoverage = computeTileCoverage(this);
254 tiledBacking()->setTileCoverage(tileCoverage);
257 void RenderLayerBacking::updateDebugIndicators(bool showBorder, bool showRepaintCounter)
259 m_graphicsLayer->setShowDebugBorder(showBorder);
260 m_graphicsLayer->setShowRepaintCounter(showRepaintCounter);
262 if (m_ancestorClippingLayer)
263 m_ancestorClippingLayer->setShowDebugBorder(showBorder);
265 if (m_foregroundLayer) {
266 m_foregroundLayer->setShowDebugBorder(showBorder);
267 m_foregroundLayer->setShowRepaintCounter(showRepaintCounter);
270 if (m_contentsContainmentLayer)
271 m_contentsContainmentLayer->setShowDebugBorder(showBorder);
273 if (m_backgroundLayer) {
274 m_backgroundLayer->setShowDebugBorder(showBorder);
275 m_backgroundLayer->setShowRepaintCounter(showRepaintCounter);
279 m_maskLayer->setShowDebugBorder(showBorder);
280 m_maskLayer->setShowRepaintCounter(showRepaintCounter);
283 if (m_layerForHorizontalScrollbar)
284 m_layerForHorizontalScrollbar->setShowDebugBorder(showBorder);
286 if (m_layerForVerticalScrollbar)
287 m_layerForVerticalScrollbar->setShowDebugBorder(showBorder);
289 if (m_layerForScrollCorner)
290 m_layerForScrollCorner->setShowDebugBorder(showBorder);
292 if (m_scrollingLayer)
293 m_scrollingLayer->setShowDebugBorder(showBorder);
295 if (m_scrollingContentsLayer) {
296 m_scrollingContentsLayer->setShowDebugBorder(showBorder);
297 m_scrollingContentsLayer->setShowRepaintCounter(showRepaintCounter);
301 void RenderLayerBacking::createPrimaryGraphicsLayer()
305 layerName = m_owningLayer.name();
308 // The call to createGraphicsLayer ends calling back into here as
309 // a GraphicsLayerClient to ask if it shouldUseTiledBacking(). We only want
310 // the tile cache on our main layer. This is pretty ugly, but saves us from
311 // exposing the API to all clients.
313 m_creatingPrimaryGraphicsLayer = true;
314 m_graphicsLayer = createGraphicsLayer(layerName);
315 m_creatingPrimaryGraphicsLayer = false;
317 if (m_usingTiledCacheLayer)
318 m_childContainmentLayer = createGraphicsLayer("TiledBacking Flattening Layer");
320 if (m_isMainFrameRenderViewLayer) {
322 // Page scale is applied above the RenderView on iOS.
323 m_graphicsLayer->setContentsOpaque(true);
324 m_graphicsLayer->setAppliesPageScale();
328 #if PLATFORM(MAC) && USE(CA)
329 if (!compositor().acceleratedDrawingEnabled() && renderer().isCanvas()) {
330 const HTMLCanvasElement* canvas = toHTMLCanvasElement(renderer().element());
331 if (canvas->shouldAccelerate(canvas->size()))
332 m_graphicsLayer->setAcceleratesDrawing(true);
336 updateOpacity(&renderer().style());
337 updateTransform(&renderer().style());
338 #if ENABLE(CSS_FILTERS)
339 updateFilters(&renderer().style());
341 #if ENABLE(CSS_COMPOSITING)
342 updateBlendMode(&renderer().style());
347 void RenderLayerBacking::layerWillBeDestroyed()
349 RenderObject& renderer = this->renderer();
350 if (renderer.isEmbeddedObject() && toRenderEmbeddedObject(renderer).allowsAcceleratedCompositing()) {
351 PluginViewBase* pluginViewBase = toPluginViewBase(toRenderWidget(renderer).widget());
352 if (pluginViewBase && m_graphicsLayer->contentsLayerForMedia())
353 pluginViewBase->detachPluginLayer();
358 void RenderLayerBacking::destroyGraphicsLayers()
360 if (m_graphicsLayer) {
361 willDestroyLayer(m_graphicsLayer.get());
362 m_graphicsLayer->removeFromParent();
365 m_ancestorClippingLayer = nullptr;
366 m_contentsContainmentLayer = nullptr;
367 m_graphicsLayer = nullptr;
368 m_foregroundLayer = nullptr;
369 m_backgroundLayer = nullptr;
370 m_childContainmentLayer = nullptr;
371 m_maskLayer = nullptr;
373 m_scrollingLayer = nullptr;
374 m_scrollingContentsLayer = nullptr;
377 void RenderLayerBacking::updateOpacity(const RenderStyle* style)
379 m_graphicsLayer->setOpacity(compositingOpacity(style->opacity()));
382 void RenderLayerBacking::updateTransform(const RenderStyle* style)
384 // FIXME: This could use m_owningLayer.transform(), but that currently has transform-origin
385 // baked into it, and we don't want that.
386 TransformationMatrix t;
387 if (m_owningLayer.hasTransform()) {
388 style->applyTransform(t, toRenderBox(renderer()).pixelSnappedBorderBoxRect().size(), RenderStyle::ExcludeTransformOrigin);
389 makeMatrixRenderable(t, compositor().canRender3DTransforms());
392 if (m_contentsContainmentLayer) {
393 m_contentsContainmentLayer->setTransform(t);
394 m_graphicsLayer->setTransform(TransformationMatrix());
396 m_graphicsLayer->setTransform(t);
399 #if ENABLE(CSS_FILTERS)
400 void RenderLayerBacking::updateFilters(const RenderStyle* style)
402 m_canCompositeFilters = m_graphicsLayer->setFilters(owningLayer().computeFilterOperations(style));
406 #if ENABLE(CSS_COMPOSITING)
407 void RenderLayerBacking::updateBlendMode(const RenderStyle* style)
409 if (m_ancestorClippingLayer)
410 m_ancestorClippingLayer->setBlendMode(style->blendMode());
412 m_graphicsLayer->setBlendMode(style->blendMode());
416 static bool hasNonZeroTransformOrigin(const RenderObject& renderer)
418 const RenderStyle& style = renderer.style();
419 return (style.transformOriginX().type() == Fixed && style.transformOriginX().value())
420 || (style.transformOriginY().type() == Fixed && style.transformOriginY().value());
424 // FIXME: We should merge the concept of RenderLayer::{hasAcceleratedTouchScrolling, needsCompositedScrolling}()
425 // so that we can remove this iOS-specific variant.
426 static bool layerOrAncestorIsTransformedOrScrolling(RenderLayer& layer)
428 for (RenderLayer* curr = &layer; curr; curr = curr->parent()) {
429 if (curr->hasTransform() || curr->hasAcceleratedTouchScrolling())
436 static bool layerOrAncestorIsTransformedOrUsingCompositedScrolling(RenderLayer& layer)
438 for (RenderLayer* curr = &layer; curr; curr = curr->parent()) {
439 if (curr->hasTransform() || curr->needsCompositedScrolling())
447 bool RenderLayerBacking::shouldClipCompositedBounds() const
450 // Scrollbar layers use this layer for relative positioning, so don't clip.
451 if (layerForHorizontalScrollbar() || layerForVerticalScrollbar())
455 if (m_usingTiledCacheLayer)
459 if (layerOrAncestorIsTransformedOrUsingCompositedScrolling(m_owningLayer))
462 if (layerOrAncestorIsTransformedOrScrolling(m_owningLayer))
466 if (m_owningLayer.isFlowThreadCollectingGraphicsLayersUnderRegions())
472 void RenderLayerBacking::updateCompositedBounds()
474 LayoutRect layerBounds = compositor().calculateCompositedBounds(m_owningLayer, m_owningLayer);
476 // Clip to the size of the document or enclosing overflow-scroll layer.
477 // If this or an ancestor is transformed, we can't currently compute the correct rect to intersect with.
478 // We'd need RenderObject::convertContainerToLocalQuad(), which doesn't yet exist.
479 if (shouldClipCompositedBounds()) {
480 RenderView& view = m_owningLayer.renderer().view();
481 RenderLayer* rootLayer = view.layer();
483 LayoutRect clippingBounds;
484 if (renderer().style().position() == FixedPosition && renderer().container() == &view)
485 clippingBounds = view.frameView().viewportConstrainedVisibleContentRect();
487 clippingBounds = view.unscaledDocumentRect();
489 if (&m_owningLayer != rootLayer)
490 clippingBounds.intersect(m_owningLayer.backgroundClipRect(RenderLayer::ClipRectsContext(rootLayer, 0, AbsoluteClipRects)).rect()); // FIXME: Incorrect for CSS regions.
493 m_owningLayer.convertToLayerCoords(rootLayer, delta, RenderLayer::AdjustForColumns);
494 clippingBounds.move(-delta.x(), -delta.y());
496 layerBounds.intersect(clippingBounds);
499 // If the element has a transform-origin that has fixed lengths, and the renderer has zero size,
500 // then we need to ensure that the compositing layer has non-zero size so that we can apply
501 // the transform-origin via the GraphicsLayer anchorPoint (which is expressed as a fractional value).
502 if (layerBounds.isEmpty() && hasNonZeroTransformOrigin(renderer())) {
503 layerBounds.setWidth(1);
504 layerBounds.setHeight(1);
505 m_artificiallyInflatedBounds = true;
507 m_artificiallyInflatedBounds = false;
509 setCompositedBounds(layerBounds);
512 void RenderLayerBacking::updateAfterWidgetResize()
514 if (!renderer().isWidget())
516 if (RenderLayerCompositor* innerCompositor = RenderLayerCompositor::frameContentsCompositor(toRenderWidget(&renderer()))) {
517 innerCompositor->frameViewDidChangeSize();
518 innerCompositor->frameViewDidChangeLocation(flooredIntPoint(contentsBox().location()));
522 void RenderLayerBacking::updateAfterLayout(UpdateAfterLayoutFlags flags)
524 if (!compositor().compositingLayersNeedRebuild()) {
525 // Calling updateGraphicsLayerGeometry() here gives incorrect results, because the
526 // position of this layer's GraphicsLayer depends on the position of our compositing
527 // ancestor's GraphicsLayer. That cannot be determined until all the descendant
528 // RenderLayers of that ancestor have been processed via updateLayerPositions().
530 // The solution is to update compositing children of this layer here,
531 // via updateCompositingChildrenGeometry().
532 updateCompositedBounds();
533 compositor().updateCompositingDescendantGeometry(m_owningLayer, m_owningLayer, flags & CompositingChildrenOnly);
535 if (flags & IsUpdateRoot) {
536 updateGraphicsLayerGeometry();
537 compositor().updateRootLayerPosition();
538 RenderLayer* stackingContainer = m_owningLayer.enclosingStackingContainer();
539 if (!compositor().compositingLayersNeedRebuild() && stackingContainer && (stackingContainer != &m_owningLayer))
540 compositor().updateCompositingDescendantGeometry(*stackingContainer, *stackingContainer, flags & CompositingChildrenOnly);
544 if (flags & NeedsFullRepaint && !paintsIntoWindow() && !paintsIntoCompositedAncestor())
545 setContentsNeedDisplay();
548 bool RenderLayerBacking::updateGraphicsLayerConfiguration()
550 m_owningLayer.updateDescendantDependentFlags();
551 m_owningLayer.updateZOrderLists();
553 bool layerConfigChanged = false;
554 setBackgroundLayerPaintsFixedRootBackground(compositor().needsFixedRootBackgroundLayer(m_owningLayer));
556 // The background layer is currently only used for fixed root backgrounds.
557 if (updateBackgroundLayer(m_backgroundLayerPaintsFixedRootBackground))
558 layerConfigChanged = true;
560 if (updateForegroundLayer(compositor().needsContentsCompositingLayer(m_owningLayer)))
561 layerConfigChanged = true;
563 bool needsDescendentsClippingLayer = compositor().clipsCompositingDescendants(m_owningLayer);
566 // Our scrolling layer will clip.
567 if (m_owningLayer.hasAcceleratedTouchScrolling())
568 needsDescendentsClippingLayer = false;
570 // Our scrolling layer will clip.
571 if (m_owningLayer.needsCompositedScrolling())
572 needsDescendentsClippingLayer = false;
573 #endif // PLATFORM(IOS)
575 if (updateAncestorClippingLayer(compositor().clippedByAncestor(m_owningLayer)))
576 layerConfigChanged = true;
578 if (updateDescendantClippingLayer(needsDescendentsClippingLayer))
579 layerConfigChanged = true;
581 if (updateOverflowControlsLayers(requiresHorizontalScrollbarLayer(), requiresVerticalScrollbarLayer(), requiresScrollCornerLayer()))
582 layerConfigChanged = true;
585 if (updateScrollingLayers(m_owningLayer.hasAcceleratedTouchScrolling()))
586 layerConfigChanged = true;
588 if (updateScrollingLayers(m_owningLayer.needsCompositedScrolling()))
589 layerConfigChanged = true;
590 #endif // PLATFORM(IOS)
592 if (layerConfigChanged)
593 updateInternalHierarchy();
595 if (GraphicsLayer* flatteningLayer = tileCacheFlatteningLayer()) {
596 if (layerConfigChanged || flatteningLayer->parent() != m_graphicsLayer.get())
597 m_graphicsLayer->addChild(flatteningLayer);
600 if (updateMaskLayer(renderer().hasMask()))
601 m_graphicsLayer->setMaskLayer(m_maskLayer.get());
603 if (m_owningLayer.hasReflection()) {
604 if (m_owningLayer.reflectionLayer()->backing()) {
605 GraphicsLayer* reflectionLayer = m_owningLayer.reflectionLayer()->backing()->graphicsLayer();
606 m_graphicsLayer->setReplicatedByLayer(reflectionLayer);
609 m_graphicsLayer->setReplicatedByLayer(0);
611 bool isSimpleContainer = isSimpleContainerCompositingLayer();
612 bool didUpdateContentsRect = false;
613 updateDirectlyCompositedContents(isSimpleContainer, didUpdateContentsRect);
615 updateRootLayerConfiguration();
617 if (isDirectlyCompositedImage())
618 updateImageContents();
620 if (renderer().isEmbeddedObject() && toRenderEmbeddedObject(&renderer())->allowsAcceleratedCompositing()) {
621 PluginViewBase* pluginViewBase = toPluginViewBase(toRenderWidget(&renderer())->widget());
623 if (pluginViewBase && !m_graphicsLayer->contentsLayerForMedia()) {
624 pluginViewBase->detachPluginLayer();
625 pluginViewBase->attachPluginLayer();
628 if (!pluginViewBase->shouldNotAddLayer())
629 m_graphicsLayer->setContentsToMedia(pluginViewBase->platformLayer());
633 else if (renderer().isVideo()) {
634 HTMLMediaElement* mediaElement = toHTMLMediaElement(renderer().element());
635 m_graphicsLayer->setContentsToMedia(mediaElement->platformLayer());
638 #if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
639 else if (renderer().isCanvas() && canvasCompositingStrategy(renderer()) == CanvasAsLayerContents) {
640 const HTMLCanvasElement* canvas = toHTMLCanvasElement(renderer().element());
641 if (CanvasRenderingContext* context = canvas->renderingContext())
642 m_graphicsLayer->setContentsToCanvas(context->platformLayer());
643 layerConfigChanged = true;
646 if (renderer().isWidget())
647 layerConfigChanged = RenderLayerCompositor::parentFrameContentLayers(toRenderWidget(&renderer()));
649 return layerConfigChanged;
652 static IntRect clipBox(RenderBox& renderer)
654 LayoutRect result = LayoutRect::infiniteRect();
655 if (renderer.hasOverflowClip())
656 result = renderer.overflowClipRect(LayoutPoint(), 0); // FIXME: Incorrect for CSS regions.
658 if (renderer.hasClip())
659 result.intersect(renderer.clipRect(LayoutPoint(), 0)); // FIXME: Incorrect for CSS regions.
661 return pixelSnappedIntRect(result);
664 void RenderLayerBacking::updateGraphicsLayerGeometry()
666 // If we haven't built z-order lists yet, wait until later.
667 if (m_owningLayer.isStackingContainer() && m_owningLayer.m_zOrderListsDirty)
670 // Set transform property, if it is not animating. We have to do this here because the transform
671 // is affected by the layer dimensions.
672 if (!renderer().animation().isRunningAcceleratedAnimationOnRenderer(&renderer(), CSSPropertyWebkitTransform))
673 updateTransform(&renderer().style());
675 // Set opacity, if it is not animating.
676 if (!renderer().animation().isRunningAcceleratedAnimationOnRenderer(&renderer(), CSSPropertyOpacity))
677 updateOpacity(&renderer().style());
679 #if ENABLE(CSS_FILTERS)
680 updateFilters(&renderer().style());
683 #if ENABLE(CSS_COMPOSITING)
684 updateBlendMode(&renderer().style());
687 bool isSimpleContainer = isSimpleContainerCompositingLayer();
689 m_owningLayer.updateDescendantDependentFlags();
691 // m_graphicsLayer is the corresponding GraphicsLayer for this RenderLayer and its non-compositing
692 // descendants. So, the visibility flag for m_graphicsLayer should be true if there are any
693 // non-compositing visible layers.
694 m_graphicsLayer->setContentsVisible(m_owningLayer.hasVisibleContent() || hasVisibleNonCompositingDescendantLayers());
696 const RenderStyle& style = renderer().style();
697 // FIXME: reflections should force transform-style to be flat in the style: https://bugs.webkit.org/show_bug.cgi?id=106959
698 bool preserves3D = style.transformStyle3D() == TransformStyle3DPreserve3D && !renderer().hasReflection();
699 m_graphicsLayer->setPreserves3D(preserves3D);
700 m_graphicsLayer->setBackfaceVisibility(style.backfaceVisibility() == BackfaceVisibilityVisible);
702 RenderLayer* compAncestor = m_owningLayer.ancestorCompositingLayer();
704 // We compute everything relative to the enclosing compositing layer.
705 IntRect ancestorCompositingBounds;
707 ASSERT(compAncestor->backing());
708 ancestorCompositingBounds = pixelSnappedIntRect(compAncestor->backing()->compositedBounds());
711 LayoutRect localRawCompositingBounds = compositedBounds();
712 LayoutPoint rawDelta;
713 m_owningLayer.convertToLayerCoords(compAncestor, rawDelta, RenderLayer::AdjustForColumns);
714 IntPoint delta = flooredIntPoint(rawDelta);
715 m_subpixelAccumulation = toLayoutSize(rawDelta.fraction());
716 // Move the bounds by the subpixel accumulation so that it pixel-snaps relative to absolute pixels instead of local coordinates.
717 localRawCompositingBounds.move(m_subpixelAccumulation);
719 IntRect localCompositingBounds = pixelSnappedIntRect(localRawCompositingBounds);
720 IntRect relativeCompositingBounds(localCompositingBounds);
721 relativeCompositingBounds.moveBy(delta);
723 adjustAncestorCompositingBoundsForFlowThread(ancestorCompositingBounds, compAncestor);
725 IntPoint graphicsLayerParentLocation;
726 if (compAncestor && compAncestor->backing()->hasClippingLayer()) {
727 // If the compositing ancestor has a layer to clip children, we parent in that, and therefore
728 // position relative to it.
729 IntRect clippingBox = clipBox(toRenderBox(compAncestor->renderer()));
730 graphicsLayerParentLocation = clippingBox.location();
731 } else if (compAncestor)
732 graphicsLayerParentLocation = ancestorCompositingBounds.location();
734 graphicsLayerParentLocation = renderer().view().documentRect().location();
737 if (compAncestor && compAncestor->hasAcceleratedTouchScrolling()) {
738 RenderBox* renderBox = toRenderBox(&compAncestor->renderer());
739 IntRect paddingBox(renderBox->borderLeft(), renderBox->borderTop(),
740 renderBox->width() - renderBox->borderLeft() - renderBox->borderRight(),
741 renderBox->height() - renderBox->borderTop() - renderBox->borderBottom());
743 IntSize scrollOffset = compAncestor->scrolledContentOffset();
744 graphicsLayerParentLocation = paddingBox.location() - scrollOffset;
748 if (compAncestor && compAncestor->needsCompositedScrolling()) {
749 RenderBox& renderBox = toRenderBox(compAncestor->renderer());
750 IntSize scrollOffset = compAncestor->scrolledContentOffset();
751 IntPoint scrollOrigin(renderBox.borderLeft(), renderBox.borderTop());
752 graphicsLayerParentLocation = scrollOrigin - scrollOffset;
755 if (compAncestor && m_ancestorClippingLayer) {
756 // Call calculateRects to get the backgroundRect which is what is used to clip the contents of this
757 // layer. Note that we call it with temporaryClipRects = true because normally when computing clip rects
758 // for a compositing layer, rootLayer is the layer itself.
759 RenderLayer::ClipRectsContext clipRectsContext(compAncestor, 0, TemporaryClipRects, IgnoreOverlayScrollbarSize, IgnoreOverflowClip);
760 IntRect parentClipRect = pixelSnappedIntRect(m_owningLayer.backgroundClipRect(clipRectsContext).rect()); // FIXME: Incorrect for CSS regions.
761 ASSERT(parentClipRect != IntRect::infiniteRect());
762 m_ancestorClippingLayer->setPosition(FloatPoint(parentClipRect.location() - graphicsLayerParentLocation));
763 m_ancestorClippingLayer->setSize(parentClipRect.size());
765 // backgroundRect is relative to compAncestor, so subtract deltaX/deltaY to get back to local coords.
766 m_ancestorClippingLayer->setOffsetFromRenderer(parentClipRect.location() - delta);
768 // The primary layer is then parented in, and positioned relative to this clipping layer.
769 graphicsLayerParentLocation = parentClipRect.location();
772 FloatSize contentsSize = relativeCompositingBounds.size();
774 if (m_contentsContainmentLayer) {
775 m_contentsContainmentLayer->setPreserves3D(preserves3D);
776 m_contentsContainmentLayer->setPosition(FloatPoint(relativeCompositingBounds.location() - graphicsLayerParentLocation));
777 // Use the same size as m_graphicsLayer so transforms behave correctly.
778 m_contentsContainmentLayer->setSize(contentsSize);
779 graphicsLayerParentLocation = relativeCompositingBounds.location();
782 m_graphicsLayer->setPosition(FloatPoint(relativeCompositingBounds.location() - graphicsLayerParentLocation));
783 m_graphicsLayer->setSize(contentsSize);
784 IntSize offsetFromRenderer = toIntSize(localCompositingBounds.location());
785 if (offsetFromRenderer != m_graphicsLayer->offsetFromRenderer()) {
786 m_graphicsLayer->setOffsetFromRenderer(toIntSize(localCompositingBounds.location()));
787 positionOverflowControlsLayers();
790 if (!m_isMainFrameRenderViewLayer) {
791 // For non-root layers, background is always painted by the primary graphics layer.
792 ASSERT(!m_backgroundLayer);
793 bool hadSubpixelRounding = LayoutSize(relativeCompositingBounds.size()) != localRawCompositingBounds.size();
794 m_graphicsLayer->setContentsOpaque(!hadSubpixelRounding && m_owningLayer.backgroundIsKnownToBeOpaqueInRect(localCompositingBounds));
797 // If we have a layer that clips children, position it.
799 if (GraphicsLayer* clipLayer = clippingLayer()) {
800 clippingBox = clipBox(toRenderBox(renderer()));
801 clipLayer->setPosition(FloatPoint(clippingBox.location() - localCompositingBounds.location()));
802 clipLayer->setSize(clippingBox.size());
803 clipLayer->setOffsetFromRenderer(toIntSize(clippingBox.location()));
807 m_maskLayer->setSize(m_graphicsLayer->size());
808 m_maskLayer->setPosition(FloatPoint());
809 m_maskLayer->setOffsetFromRenderer(m_graphicsLayer->offsetFromRenderer());
812 if (m_owningLayer.hasTransform()) {
813 const IntRect borderBox = toRenderBox(renderer()).pixelSnappedBorderBoxRect();
815 // Get layout bounds in the coords of compAncestor to match relativeCompositingBounds.
816 IntRect layerBounds(delta, borderBox.size());
818 // Update properties that depend on layer dimensions
819 FloatPoint3D transformOrigin = computeTransformOrigin(borderBox);
820 // Compute the anchor point, which is in the center of the renderer box unless transform-origin is set.
821 FloatPoint3D anchor(relativeCompositingBounds.width() != 0.0f ? ((layerBounds.x() - relativeCompositingBounds.x()) + transformOrigin.x()) / relativeCompositingBounds.width() : 0.5f,
822 relativeCompositingBounds.height() != 0.0f ? ((layerBounds.y() - relativeCompositingBounds.y()) + transformOrigin.y()) / relativeCompositingBounds.height() : 0.5f,
823 transformOrigin.z());
824 if (m_contentsContainmentLayer)
825 m_contentsContainmentLayer->setAnchorPoint(anchor);
827 m_graphicsLayer->setAnchorPoint(anchor);
829 const RenderStyle& style = renderer().style();
830 GraphicsLayer* clipLayer = clippingLayer();
831 if (style.hasPerspective()) {
832 TransformationMatrix t = owningLayer().perspectiveTransform();
835 clipLayer->setChildrenTransform(t);
836 m_graphicsLayer->setChildrenTransform(TransformationMatrix());
839 m_graphicsLayer->setChildrenTransform(t);
842 clipLayer->setChildrenTransform(TransformationMatrix());
844 m_graphicsLayer->setChildrenTransform(TransformationMatrix());
847 m_graphicsLayer->setAnchorPoint(FloatPoint3D(0.5f, 0.5f, 0));
848 if (m_contentsContainmentLayer)
849 m_contentsContainmentLayer->setAnchorPoint(FloatPoint3D(0.5f, 0.5f, 0));
852 if (m_foregroundLayer) {
853 FloatPoint foregroundPosition;
854 FloatSize foregroundSize = contentsSize;
855 IntSize foregroundOffset = m_graphicsLayer->offsetFromRenderer();
856 if (hasClippingLayer()) {
857 // If we have a clipping layer (which clips descendants), then the foreground layer is a child of it,
858 // so that it gets correctly sorted with children. In that case, position relative to the clipping layer.
859 foregroundSize = FloatSize(clippingBox.size());
860 foregroundOffset = toIntSize(clippingBox.location());
863 m_foregroundLayer->setPosition(foregroundPosition);
864 m_foregroundLayer->setSize(foregroundSize);
865 m_foregroundLayer->setOffsetFromRenderer(foregroundOffset);
868 if (m_backgroundLayer) {
869 FloatPoint backgroundPosition;
870 FloatSize backgroundSize = contentsSize;
871 if (backgroundLayerPaintsFixedRootBackground()) {
872 const FrameView& frameView = renderer().view().frameView();
873 backgroundPosition = IntPoint(frameView.scrollOffsetForFixedPosition());
874 backgroundSize = frameView.visibleContentRect().size();
876 m_backgroundLayer->setPosition(backgroundPosition);
877 m_backgroundLayer->setSize(backgroundSize);
878 m_backgroundLayer->setOffsetFromRenderer(m_graphicsLayer->offsetFromRenderer());
881 if (m_owningLayer.reflectionLayer() && m_owningLayer.reflectionLayer()->isComposited()) {
882 RenderLayerBacking* reflectionBacking = m_owningLayer.reflectionLayer()->backing();
883 reflectionBacking->updateGraphicsLayerGeometry();
885 // The reflection layer has the bounds of m_owningLayer.reflectionLayer(),
886 // but the reflected layer is the bounds of this layer, so we need to position it appropriately.
887 FloatRect layerBounds = compositedBounds();
888 FloatRect reflectionLayerBounds = reflectionBacking->compositedBounds();
889 reflectionBacking->graphicsLayer()->setReplicatedLayerPosition(FloatPoint(layerBounds.location() - reflectionLayerBounds.location()));
892 if (m_scrollingLayer) {
893 ASSERT(m_scrollingContentsLayer);
894 RenderBox& renderBox = toRenderBox(renderer());
895 IntRect paddingBox(renderBox.borderLeft(), renderBox.borderTop(), renderBox.width() - renderBox.borderLeft() - renderBox.borderRight(), renderBox.height() - renderBox.borderTop() - renderBox.borderBottom());
896 IntSize scrollOffset = m_owningLayer.scrollOffset();
898 m_scrollingLayer->setPosition(FloatPoint(paddingBox.location() - localCompositingBounds.location()));
900 m_scrollingLayer->setSize(paddingBox.size());
902 IntSize oldScrollingLayerOffset = m_scrollingLayer->offsetFromRenderer();
903 m_scrollingLayer->setOffsetFromRenderer(IntPoint() - paddingBox.location());
904 bool paddingBoxOffsetChanged = oldScrollingLayerOffset != m_scrollingLayer->offsetFromRenderer();
906 if (m_owningLayer.isInUserScroll()) {
907 // If scrolling is happening externally, we don't want to touch the layer bounds origin here because that will cause
908 // jitter. Set a flag to ensure that we sync up later.
909 m_owningLayer.setRequiresScrollBoundsOriginUpdate(true);
911 // Note that we implement the contents offset via the bounds origin on this layer, rather than a position on the sublayer.
912 m_scrollingLayer->setBoundsOrigin(FloatPoint(scrollOffset.width(), scrollOffset.height()));
913 m_owningLayer.setRequiresScrollBoundsOriginUpdate(false);
916 IntSize scrollSize(m_owningLayer.scrollWidth(), m_owningLayer.scrollHeight());
918 m_scrollingContentsLayer->setPosition(FloatPoint());
920 if (scrollSize != m_scrollingContentsLayer->size() || paddingBoxOffsetChanged)
921 m_scrollingContentsLayer->setNeedsDisplay();
923 m_scrollingContentsLayer->setSize(scrollSize);
924 // Scrolling the content layer does not need to trigger a repaint. The offset will be compensated away during painting.
925 // FIXME: The paint offset and the scroll offset should really be separate concepts.
926 m_scrollingContentsLayer->setOffsetFromRenderer(paddingBox.location() - IntPoint() - scrollOffset, GraphicsLayer::DontSetNeedsDisplay);
928 compositor().scrollingLayerAddedOrUpdated(&m_owningLayer);
930 m_scrollingContentsLayer->setPosition(FloatPoint(-scrollOffset.width(), -scrollOffset.height()));
932 IntSize oldScrollingLayerOffset = m_scrollingLayer->offsetFromRenderer();
933 m_scrollingLayer->setOffsetFromRenderer(-toIntSize(paddingBox.location()));
935 bool paddingBoxOffsetChanged = oldScrollingLayerOffset != m_scrollingLayer->offsetFromRenderer();
937 IntSize scrollSize(m_owningLayer.scrollWidth(), m_owningLayer.scrollHeight());
938 if (scrollSize != m_scrollingContentsLayer->size() || paddingBoxOffsetChanged)
939 m_scrollingContentsLayer->setNeedsDisplay();
941 IntSize scrollingContentsOffset = toIntSize(paddingBox.location() - scrollOffset);
942 if (scrollingContentsOffset != m_scrollingContentsLayer->offsetFromRenderer() || scrollSize != m_scrollingContentsLayer->size())
943 compositor().scrollingLayerDidChange(m_owningLayer);
945 m_scrollingContentsLayer->setSize(scrollSize);
946 // FIXME: The paint offset and the scroll offset should really be separate concepts.
947 m_scrollingContentsLayer->setOffsetFromRenderer(scrollingContentsOffset, GraphicsLayer::DontSetNeedsDisplay);
950 if (m_foregroundLayer) {
951 m_foregroundLayer->setSize(m_scrollingContentsLayer->size());
952 m_foregroundLayer->setOffsetFromRenderer(m_scrollingContentsLayer->offsetFromRenderer());
956 // If this layer was created just for clipping or to apply perspective, it doesn't need its own backing store.
957 setRequiresOwnBackingStore(compositor().requiresOwnBackingStore(m_owningLayer, compAncestor, relativeCompositingBounds, ancestorCompositingBounds));
959 bool didUpdateContentsRect = false;
960 updateDirectlyCompositedContents(isSimpleContainer, didUpdateContentsRect);
961 if (!didUpdateContentsRect && m_graphicsLayer->hasContentsLayer())
964 updateDrawsContent(isSimpleContainer);
965 updateAfterWidgetResize();
966 registerScrollingLayers();
969 void RenderLayerBacking::adjustAncestorCompositingBoundsForFlowThread(IntRect& ancestorCompositingBounds, const RenderLayer* compositingAncestor) const
971 if (!m_owningLayer.isInsideFlowThread())
974 RenderLayer* flowThreadLayer = m_owningLayer.isInsideOutOfFlowThread() ? m_owningLayer.stackingContainer() : m_owningLayer.enclosingFlowThreadAncestor();
975 if (flowThreadLayer && flowThreadLayer->isRenderFlowThread()) {
976 RenderFlowThread& flowThread = toRenderFlowThread(flowThreadLayer->renderer());
977 if (m_owningLayer.isFlowThreadCollectingGraphicsLayersUnderRegions()) {
978 // The RenderNamedFlowThread is not composited, as we need it to paint the
979 // background layer of the regions. We need to compensate for that by manually
980 // subtracting the position of the flow-thread.
981 IntPoint flowPosition;
982 flowThreadLayer->convertToPixelSnappedLayerCoords(compositingAncestor, flowPosition);
983 ancestorCompositingBounds.moveBy(flowPosition);
986 // Move the ancestor position at the top of the region where the composited layer is going to display.
987 RenderNamedFlowFragment* parentRegion = flowThread.cachedRegionForCompositedLayer(m_owningLayer);
990 m_owningLayer.convertToPixelSnappedLayerCoords(flowThreadLayer, flowDelta);
991 parentRegion->adjustRegionBoundsFromFlowThreadPortionRect(flowDelta, ancestorCompositingBounds);
992 RenderBoxModelObject* layerOwner = toRenderBoxModelObject(parentRegion->layerOwner());
993 if (layerOwner->hasLayer() && layerOwner->layer()->backing()) {
994 // Make sure that the region propagates its borders, paddings, outlines or box-shadows to layers inside it.
995 // Note that the composited bounds of the RenderRegion are already calculated
996 // because RenderLayerCompositor::rebuildCompositingLayerTree will only
997 // iterate on the content of the region after the region itself is computed.
998 ancestorCompositingBounds.moveBy(roundedIntPoint(layerOwner->layer()->backing()->compositedBounds().location()));
999 ancestorCompositingBounds.move(-layerOwner->borderAndPaddingStart(), -layerOwner->borderAndPaddingBefore());
1005 void RenderLayerBacking::updateDirectlyCompositedContents(bool isSimpleContainer, bool& didUpdateContentsRect)
1007 if (!m_owningLayer.hasVisibleContent())
1010 // The order of operations here matters, since the last valid type of contents needs
1011 // to also update the contentsRect.
1012 updateDirectlyCompositedBackgroundColor(isSimpleContainer, didUpdateContentsRect);
1013 updateDirectlyCompositedBackgroundImage(isSimpleContainer, didUpdateContentsRect);
1016 void RenderLayerBacking::registerScrollingLayers()
1019 compositor().updateViewportConstraintStatus(m_owningLayer);
1021 // Register fixed position layers and their containers with the scrolling coordinator.
1022 ScrollingCoordinator* scrollingCoordinator = scrollingCoordinatorFromLayer(m_owningLayer);
1023 if (!scrollingCoordinator)
1026 compositor().updateViewportConstraintStatus(m_owningLayer);
1028 if (!scrollingCoordinator->supportsFixedPositionLayers())
1031 // Page scale is applied as a transform on the root render view layer. Because the scroll
1032 // layer is further up in the hierarchy, we need to avoid marking the root render view
1033 // layer as a container.
1034 bool isContainer = m_owningLayer.hasTransform() && !m_owningLayer.isRootLayer();
1035 scrollingCoordinator->setLayerIsContainerForFixedPositionLayers(childForSuperlayers(), isContainer);
1039 void RenderLayerBacking::updateInternalHierarchy()
1041 // m_foregroundLayer has to be inserted in the correct order with child layers,
1042 // so it's not inserted here.
1043 if (m_ancestorClippingLayer)
1044 m_ancestorClippingLayer->removeAllChildren();
1046 if (m_contentsContainmentLayer) {
1047 m_contentsContainmentLayer->removeAllChildren();
1048 if (m_ancestorClippingLayer)
1049 m_ancestorClippingLayer->addChild(m_contentsContainmentLayer.get());
1052 if (m_backgroundLayer)
1053 m_contentsContainmentLayer->addChild(m_backgroundLayer.get());
1055 if (m_contentsContainmentLayer)
1056 m_contentsContainmentLayer->addChild(m_graphicsLayer.get());
1057 else if (m_ancestorClippingLayer)
1058 m_ancestorClippingLayer->addChild(m_graphicsLayer.get());
1060 if (m_childContainmentLayer) {
1061 m_childContainmentLayer->removeFromParent();
1062 m_graphicsLayer->addChild(m_childContainmentLayer.get());
1065 if (m_scrollingLayer) {
1066 GraphicsLayer* superlayer = m_childContainmentLayer ? m_childContainmentLayer.get() : m_graphicsLayer.get();
1067 m_scrollingLayer->removeFromParent();
1068 superlayer->addChild(m_scrollingLayer.get());
1071 // The clip for child layers does not include space for overflow controls, so they exist as
1072 // siblings of the clipping layer if we have one. Normal children of this layer are set as
1073 // children of the clipping layer.
1074 if (m_layerForHorizontalScrollbar) {
1075 m_layerForHorizontalScrollbar->removeFromParent();
1076 m_graphicsLayer->addChild(m_layerForHorizontalScrollbar.get());
1078 if (m_layerForVerticalScrollbar) {
1079 m_layerForVerticalScrollbar->removeFromParent();
1080 m_graphicsLayer->addChild(m_layerForVerticalScrollbar.get());
1082 if (m_layerForScrollCorner) {
1083 m_layerForScrollCorner->removeFromParent();
1084 m_graphicsLayer->addChild(m_layerForScrollCorner.get());
1088 void RenderLayerBacking::resetContentsRect()
1090 m_graphicsLayer->setContentsRect(pixelSnappedIntRect(contentsBox()));
1092 LayoutRect contentsClippingRect;
1093 if (renderer().isBox())
1094 contentsClippingRect = toRenderBox(renderer()).contentBoxRect();
1096 contentsClippingRect.move(contentOffsetInCompostingLayer());
1097 m_graphicsLayer->setContentsClippingRect(pixelSnappedIntRect(contentsClippingRect));
1099 m_graphicsLayer->setContentsTileSize(IntSize());
1100 m_graphicsLayer->setContentsTilePhase(IntPoint());
1103 void RenderLayerBacking::updateDrawsContent()
1105 updateDrawsContent(isSimpleContainerCompositingLayer());
1108 void RenderLayerBacking::updateDrawsContent(bool isSimpleContainer)
1110 if (m_scrollingLayer) {
1111 // We don't have to consider overflow controls, because we know that the scrollbars are drawn elsewhere.
1112 // m_graphicsLayer only needs backing store if the non-scrolling parts (background, outlines, borders, shadows etc) need to paint.
1113 // m_scrollingLayer never has backing store.
1114 // m_scrollingContentsLayer only needs backing store if the scrolled contents need to paint.
1115 bool hasNonScrollingPaintedContent = m_owningLayer.hasVisibleContent() && m_owningLayer.hasBoxDecorationsOrBackground();
1116 m_graphicsLayer->setDrawsContent(hasNonScrollingPaintedContent);
1118 bool hasScrollingPaintedContent = m_owningLayer.hasVisibleContent() && (renderer().hasBackground() || paintsChildren());
1119 m_scrollingContentsLayer->setDrawsContent(hasScrollingPaintedContent);
1123 bool hasPaintedContent = containsPaintedContent(isSimpleContainer);
1125 // FIXME: we could refine this to only allocate backing for one of these layers if possible.
1126 m_graphicsLayer->setDrawsContent(hasPaintedContent);
1127 if (m_foregroundLayer)
1128 m_foregroundLayer->setDrawsContent(hasPaintedContent);
1130 if (m_backgroundLayer)
1131 m_backgroundLayer->setDrawsContent(hasPaintedContent);
1134 // Return true if the layer changed.
1135 bool RenderLayerBacking::updateAncestorClippingLayer(bool needsAncestorClip)
1137 bool layersChanged = false;
1139 if (needsAncestorClip) {
1140 if (!m_ancestorClippingLayer) {
1141 m_ancestorClippingLayer = createGraphicsLayer("Ancestor clipping Layer");
1142 m_ancestorClippingLayer->setMasksToBounds(true);
1143 layersChanged = true;
1145 } else if (hasAncestorClippingLayer()) {
1146 willDestroyLayer(m_ancestorClippingLayer.get());
1147 m_ancestorClippingLayer->removeFromParent();
1148 m_ancestorClippingLayer = nullptr;
1149 layersChanged = true;
1152 return layersChanged;
1155 // Return true if the layer changed.
1156 bool RenderLayerBacking::updateDescendantClippingLayer(bool needsDescendantClip)
1158 bool layersChanged = false;
1160 if (needsDescendantClip) {
1161 if (!m_childContainmentLayer && !m_usingTiledCacheLayer) {
1162 m_childContainmentLayer = createGraphicsLayer("Child clipping Layer");
1163 m_childContainmentLayer->setMasksToBounds(true);
1164 layersChanged = true;
1166 } else if (hasClippingLayer()) {
1167 willDestroyLayer(m_childContainmentLayer.get());
1168 m_childContainmentLayer->removeFromParent();
1169 m_childContainmentLayer = nullptr;
1170 layersChanged = true;
1173 return layersChanged;
1176 void RenderLayerBacking::setBackgroundLayerPaintsFixedRootBackground(bool backgroundLayerPaintsFixedRootBackground)
1178 m_backgroundLayerPaintsFixedRootBackground = backgroundLayerPaintsFixedRootBackground;
1181 bool RenderLayerBacking::requiresHorizontalScrollbarLayer() const
1183 if (!m_owningLayer.hasOverlayScrollbars() && !m_owningLayer.needsCompositedScrolling())
1185 return m_owningLayer.horizontalScrollbar();
1188 bool RenderLayerBacking::requiresVerticalScrollbarLayer() const
1190 if (!m_owningLayer.hasOverlayScrollbars() && !m_owningLayer.needsCompositedScrolling())
1192 return m_owningLayer.verticalScrollbar();
1195 bool RenderLayerBacking::requiresScrollCornerLayer() const
1197 if (!m_owningLayer.hasOverlayScrollbars() && !m_owningLayer.needsCompositedScrolling())
1199 return !m_owningLayer.scrollCornerAndResizerRect().isEmpty();
1202 bool RenderLayerBacking::updateOverflowControlsLayers(bool needsHorizontalScrollbarLayer, bool needsVerticalScrollbarLayer, bool needsScrollCornerLayer)
1204 bool horizontalScrollbarLayerChanged = false;
1205 if (needsHorizontalScrollbarLayer) {
1206 if (!m_layerForHorizontalScrollbar) {
1207 m_layerForHorizontalScrollbar = createGraphicsLayer("horizontal scrollbar");
1208 horizontalScrollbarLayerChanged = true;
1210 } else if (m_layerForHorizontalScrollbar) {
1211 willDestroyLayer(m_layerForHorizontalScrollbar.get());
1212 m_layerForHorizontalScrollbar = nullptr;
1213 horizontalScrollbarLayerChanged = true;
1216 bool verticalScrollbarLayerChanged = false;
1217 if (needsVerticalScrollbarLayer) {
1218 if (!m_layerForVerticalScrollbar) {
1219 m_layerForVerticalScrollbar = createGraphicsLayer("vertical scrollbar");
1220 verticalScrollbarLayerChanged = true;
1222 } else if (m_layerForVerticalScrollbar) {
1223 willDestroyLayer(m_layerForVerticalScrollbar.get());
1224 m_layerForVerticalScrollbar = nullptr;
1225 verticalScrollbarLayerChanged = true;
1228 bool scrollCornerLayerChanged = false;
1229 if (needsScrollCornerLayer) {
1230 if (!m_layerForScrollCorner) {
1231 m_layerForScrollCorner = createGraphicsLayer("scroll corner");
1232 scrollCornerLayerChanged = true;
1234 } else if (m_layerForScrollCorner) {
1235 willDestroyLayer(m_layerForScrollCorner.get());
1236 m_layerForScrollCorner = nullptr;
1237 scrollCornerLayerChanged = true;
1240 if (ScrollingCoordinator* scrollingCoordinator = scrollingCoordinatorFromLayer(m_owningLayer)) {
1241 if (horizontalScrollbarLayerChanged)
1242 scrollingCoordinator->scrollableAreaScrollbarLayerDidChange(&m_owningLayer, HorizontalScrollbar);
1243 if (verticalScrollbarLayerChanged)
1244 scrollingCoordinator->scrollableAreaScrollbarLayerDidChange(&m_owningLayer, VerticalScrollbar);
1247 return horizontalScrollbarLayerChanged || verticalScrollbarLayerChanged || scrollCornerLayerChanged;
1250 void RenderLayerBacking::positionOverflowControlsLayers()
1252 if (!m_owningLayer.hasScrollbars())
1255 const IntRect borderBox = toRenderBox(renderer()).pixelSnappedBorderBoxRect();
1257 IntSize offsetFromRenderer = m_graphicsLayer->offsetFromRenderer();
1258 if (GraphicsLayer* layer = layerForHorizontalScrollbar()) {
1259 IntRect hBarRect = m_owningLayer.rectForHorizontalScrollbar(borderBox);
1260 layer->setPosition(hBarRect.location() - offsetFromRenderer);
1261 layer->setSize(hBarRect.size());
1262 if (layer->hasContentsLayer()) {
1263 IntRect barRect = IntRect(IntPoint(), hBarRect.size());
1264 layer->setContentsRect(barRect);
1265 layer->setContentsClippingRect(barRect);
1267 layer->setDrawsContent(m_owningLayer.horizontalScrollbar() && !layer->hasContentsLayer());
1270 if (GraphicsLayer* layer = layerForVerticalScrollbar()) {
1271 IntRect vBarRect = m_owningLayer.rectForVerticalScrollbar(borderBox);
1272 layer->setPosition(vBarRect.location() - offsetFromRenderer);
1273 layer->setSize(vBarRect.size());
1274 if (layer->hasContentsLayer()) {
1275 IntRect barRect = IntRect(IntPoint(), vBarRect.size());
1276 layer->setContentsRect(barRect);
1277 layer->setContentsClippingRect(barRect);
1279 layer->setDrawsContent(m_owningLayer.verticalScrollbar() && !layer->hasContentsLayer());
1282 if (GraphicsLayer* layer = layerForScrollCorner()) {
1283 const LayoutRect& scrollCornerAndResizer = m_owningLayer.scrollCornerAndResizerRect();
1284 layer->setPosition(scrollCornerAndResizer.location() - offsetFromRenderer);
1285 layer->setSize(scrollCornerAndResizer.size());
1286 layer->setDrawsContent(!scrollCornerAndResizer.isEmpty());
1290 bool RenderLayerBacking::hasUnpositionedOverflowControlsLayers() const
1292 if (GraphicsLayer* layer = layerForHorizontalScrollbar())
1293 if (!layer->drawsContent())
1296 if (GraphicsLayer* layer = layerForVerticalScrollbar())
1297 if (!layer->drawsContent())
1300 if (GraphicsLayer* layer = layerForScrollCorner())
1301 if (!layer->drawsContent())
1307 bool RenderLayerBacking::updateForegroundLayer(bool needsForegroundLayer)
1309 bool layerChanged = false;
1310 if (needsForegroundLayer) {
1311 if (!m_foregroundLayer) {
1314 layerName = m_owningLayer.name() + " (foreground)";
1316 m_foregroundLayer = createGraphicsLayer(layerName);
1317 m_foregroundLayer->setDrawsContent(true);
1318 m_foregroundLayer->setPaintingPhase(GraphicsLayerPaintForeground);
1319 layerChanged = true;
1321 } else if (m_foregroundLayer) {
1322 willDestroyLayer(m_foregroundLayer.get());
1323 m_foregroundLayer->removeFromParent();
1324 m_foregroundLayer = nullptr;
1325 layerChanged = true;
1329 m_graphicsLayer->setNeedsDisplay();
1330 m_graphicsLayer->setPaintingPhase(paintingPhaseForPrimaryLayer());
1333 return layerChanged;
1336 bool RenderLayerBacking::updateBackgroundLayer(bool needsBackgroundLayer)
1338 bool layerChanged = false;
1339 if (needsBackgroundLayer) {
1340 if (!m_backgroundLayer) {
1343 layerName = m_owningLayer.name() + " (background)";
1345 m_backgroundLayer = createGraphicsLayer(layerName);
1346 m_backgroundLayer->setDrawsContent(true);
1347 m_backgroundLayer->setAnchorPoint(FloatPoint3D());
1348 m_backgroundLayer->setPaintingPhase(GraphicsLayerPaintBackground);
1349 layerChanged = true;
1352 if (!m_contentsContainmentLayer) {
1355 layerName = m_owningLayer.name() + " (contents containment)";
1357 m_contentsContainmentLayer = createGraphicsLayer(layerName);
1358 m_contentsContainmentLayer->setAppliesPageScale(true);
1359 m_graphicsLayer->setAppliesPageScale(false);
1360 layerChanged = true;
1363 if (m_backgroundLayer) {
1364 willDestroyLayer(m_backgroundLayer.get());
1365 m_backgroundLayer->removeFromParent();
1366 m_backgroundLayer = nullptr;
1367 layerChanged = true;
1369 if (m_contentsContainmentLayer) {
1370 willDestroyLayer(m_contentsContainmentLayer.get());
1371 m_contentsContainmentLayer->removeFromParent();
1372 m_contentsContainmentLayer = nullptr;
1373 layerChanged = true;
1374 m_graphicsLayer->setAppliesPageScale(true);
1379 m_graphicsLayer->setNeedsDisplay();
1380 // This assumes that the background layer is only used for fixed backgrounds, which is currently a correct assumption.
1381 compositor().fixedRootBackgroundLayerChanged();
1384 return layerChanged;
1387 bool RenderLayerBacking::updateMaskLayer(bool needsMaskLayer)
1389 bool layerChanged = false;
1390 if (needsMaskLayer) {
1392 m_maskLayer = createGraphicsLayer("Mask");
1393 m_maskLayer->setDrawsContent(true);
1394 m_maskLayer->setPaintingPhase(GraphicsLayerPaintMask);
1395 layerChanged = true;
1397 } else if (m_maskLayer) {
1398 willDestroyLayer(m_maskLayer.get());
1399 m_maskLayer = nullptr;
1400 layerChanged = true;
1404 m_graphicsLayer->setPaintingPhase(paintingPhaseForPrimaryLayer());
1406 return layerChanged;
1409 bool RenderLayerBacking::updateScrollingLayers(bool needsScrollingLayers)
1411 ScrollingCoordinator* scrollingCoordinator = scrollingCoordinatorFromLayer(m_owningLayer);
1413 bool layerChanged = false;
1414 if (needsScrollingLayers) {
1415 if (!m_scrollingLayer) {
1416 // Outer layer which corresponds with the scroll view.
1417 m_scrollingLayer = createGraphicsLayer("Scrolling container");
1418 m_scrollingLayer->setDrawsContent(false);
1419 m_scrollingLayer->setMasksToBounds(true);
1421 // Inner layer which renders the content that scrolls.
1422 m_scrollingContentsLayer = createGraphicsLayer("Scrolled Contents");
1423 m_scrollingContentsLayer->setDrawsContent(true);
1424 GraphicsLayerPaintingPhase paintPhase = GraphicsLayerPaintOverflowContents | GraphicsLayerPaintCompositedScroll;
1425 if (!m_foregroundLayer)
1426 paintPhase |= GraphicsLayerPaintForeground;
1427 m_scrollingContentsLayer->setPaintingPhase(paintPhase);
1428 m_scrollingLayer->addChild(m_scrollingContentsLayer.get());
1430 layerChanged = true;
1431 if (scrollingCoordinator)
1432 scrollingCoordinator->scrollableAreaScrollLayerDidChange(&m_owningLayer);
1434 if (m_owningLayer.parent())
1435 compositor().scrollingLayerAddedOrUpdated(&m_owningLayer);
1438 } else if (m_scrollingLayer) {
1440 if (!renderer().documentBeingDestroyed())
1441 compositor().scrollingLayerRemoved(&m_owningLayer, m_scrollingLayer->platformLayer(), m_scrollingContentsLayer->platformLayer());
1443 willDestroyLayer(m_scrollingLayer.get());
1444 willDestroyLayer(m_scrollingContentsLayer.get());
1445 m_scrollingLayer = nullptr;
1446 m_scrollingContentsLayer = nullptr;
1447 layerChanged = true;
1448 if (scrollingCoordinator)
1449 scrollingCoordinator->scrollableAreaScrollLayerDidChange(&m_owningLayer);
1453 updateInternalHierarchy();
1454 m_graphicsLayer->setPaintingPhase(paintingPhaseForPrimaryLayer());
1456 m_graphicsLayer->setNeedsDisplay();
1457 compositor().scrollingLayerDidChange(m_owningLayer);
1461 return layerChanged;
1464 void RenderLayerBacking::attachToScrollingCoordinatorWithParent(RenderLayerBacking* parent)
1466 ScrollingCoordinator* scrollingCoordinator = scrollingCoordinatorFromLayer(m_owningLayer);
1467 if (!scrollingCoordinator)
1470 // FIXME: When we support overflow areas, we will have to refine this for overflow areas that are also
1472 ScrollingNodeType nodeType;
1473 if (renderer().style().position() == FixedPosition)
1474 nodeType = FixedNode;
1475 else if (renderer().style().position() == StickyPosition)
1476 nodeType = StickyNode;
1478 nodeType = ScrollingNode;
1480 ScrollingNodeID parentID = parent ? parent->scrollLayerID() : 0;
1481 m_scrollLayerID = scrollingCoordinator->attachToStateTree(nodeType, m_scrollLayerID ? m_scrollLayerID : scrollingCoordinator->uniqueScrollLayerID(), parentID);
1484 void RenderLayerBacking::detachFromScrollingCoordinator()
1486 // If m_scrollLayerID is 0, then this backing is not attached to the ScrollingCoordinator.
1487 if (!m_scrollLayerID)
1490 ScrollingCoordinator* scrollingCoordinator = scrollingCoordinatorFromLayer(m_owningLayer);
1491 if (!scrollingCoordinator)
1494 scrollingCoordinator->detachFromStateTree(m_scrollLayerID);
1495 m_scrollLayerID = 0;
1498 GraphicsLayerPaintingPhase RenderLayerBacking::paintingPhaseForPrimaryLayer() const
1501 if (!m_backgroundLayer)
1502 phase |= GraphicsLayerPaintBackground;
1503 if (!m_foregroundLayer)
1504 phase |= GraphicsLayerPaintForeground;
1506 phase |= GraphicsLayerPaintMask;
1508 if (m_scrollingContentsLayer) {
1509 phase &= ~GraphicsLayerPaintForeground;
1510 phase |= GraphicsLayerPaintCompositedScroll;
1513 return static_cast<GraphicsLayerPaintingPhase>(phase);
1516 float RenderLayerBacking::compositingOpacity(float rendererOpacity) const
1518 float finalOpacity = rendererOpacity;
1520 for (RenderLayer* curr = m_owningLayer.parent(); curr; curr = curr->parent()) {
1521 // We only care about parents that are stacking contexts.
1522 // Recall that opacity creates stacking context.
1523 if (!curr->isStackingContainer())
1526 // If we found a compositing layer, we want to compute opacity
1527 // relative to it. So we can break here.
1528 if (curr->isComposited())
1531 finalOpacity *= curr->renderer().opacity();
1534 return finalOpacity;
1537 static bool hasBoxDecorations(const RenderStyle* style)
1539 return style->hasBorder() || style->hasBorderRadius() || style->hasOutline() || style->hasAppearance() || style->boxShadow() || style->hasFilter();
1542 static bool canCreateTiledImage(const RenderStyle*);
1544 static bool hasBoxDecorationsOrBackgroundImage(const RenderStyle* style)
1546 if (hasBoxDecorations(style))
1549 if (!style->hasBackgroundImage())
1552 return !GraphicsLayer::supportsContentsTiling() || !canCreateTiledImage(style);
1555 static bool hasPerspectiveOrPreserves3D(const RenderStyle* style)
1557 return style->hasPerspective() || style->preserves3D();
1560 Color RenderLayerBacking::rendererBackgroundColor() const
1562 const auto& backgroundRenderer = renderer().isRoot() ? renderer().rendererForRootBackground() : renderer();
1563 return backgroundRenderer.style().visitedDependentColor(CSSPropertyBackgroundColor);
1566 void RenderLayerBacking::updateDirectlyCompositedBackgroundColor(bool isSimpleContainer, bool& didUpdateContentsRect)
1568 if (!isSimpleContainer) {
1569 m_graphicsLayer->setContentsToSolidColor(Color());
1573 Color backgroundColor = rendererBackgroundColor();
1575 // An unset (invalid) color will remove the solid color.
1576 m_graphicsLayer->setContentsToSolidColor(backgroundColor);
1577 IntRect contentsRect = backgroundBox();
1578 m_graphicsLayer->setContentsRect(contentsRect);
1579 m_graphicsLayer->setContentsClippingRect(contentsRect);
1580 didUpdateContentsRect = true;
1583 bool canCreateTiledImage(const RenderStyle* style)
1585 const FillLayer* fillLayer = style->backgroundLayers();
1586 if (fillLayer->next())
1589 if (!fillLayer->imagesAreLoaded())
1592 if (fillLayer->attachment() != ScrollBackgroundAttachment)
1595 Color color = style->visitedDependentColor(CSSPropertyBackgroundColor);
1597 // FIXME: Allow color+image compositing when it makes sense.
1598 // For now bailing out.
1599 if (color.isValid() && color.alpha())
1602 StyleImage* styleImage = fillLayer->image();
1604 // FIXME: support gradients with isGeneratedImage.
1605 if (!styleImage->isCachedImage())
1608 Image* image = styleImage->cachedImage()->image();
1609 if (!image->isBitmapImage())
1615 void RenderLayerBacking::updateDirectlyCompositedBackgroundImage(bool isSimpleContainer, bool& didUpdateContentsRect)
1617 if (!GraphicsLayer::supportsContentsTiling())
1620 if (isDirectlyCompositedImage())
1623 const RenderStyle& style = renderer().style();
1625 if (!isSimpleContainer || !style.hasBackgroundImage()) {
1626 m_graphicsLayer->setContentsToImage(0);
1630 IntRect destRect = backgroundBox();
1634 RefPtr<Image> image = style.backgroundLayers()->image()->cachedImage()->image();
1635 toRenderBox(renderer()).getGeometryForBackgroundImage(&m_owningLayer.renderer(), destRect, phase, tileSize);
1636 m_graphicsLayer->setContentsTileSize(tileSize);
1637 m_graphicsLayer->setContentsTilePhase(phase);
1638 m_graphicsLayer->setContentsRect(destRect);
1639 m_graphicsLayer->setContentsClippingRect(destRect);
1640 m_graphicsLayer->setContentsToImage(image.get());
1641 didUpdateContentsRect = true;
1644 void RenderLayerBacking::updateRootLayerConfiguration()
1646 if (!m_usingTiledCacheLayer)
1649 Color backgroundColor;
1650 bool viewIsTransparent = compositor().viewHasTransparentBackground(&backgroundColor);
1652 if (m_backgroundLayerPaintsFixedRootBackground && m_backgroundLayer) {
1653 m_backgroundLayer->setBackgroundColor(backgroundColor);
1654 m_backgroundLayer->setContentsOpaque(!viewIsTransparent);
1656 m_graphicsLayer->setBackgroundColor(Color());
1657 m_graphicsLayer->setContentsOpaque(false);
1659 m_graphicsLayer->setBackgroundColor(backgroundColor);
1660 m_graphicsLayer->setContentsOpaque(!viewIsTransparent);
1664 static bool supportsDirectBoxDecorationsComposition(const RenderLayerModelObject& renderer)
1666 if (!GraphicsLayer::supportsBackgroundColorContent())
1669 if (renderer.hasClip())
1672 if (hasBoxDecorationsOrBackgroundImage(&renderer.style()))
1675 // FIXME: We can't create a directly composited background if this
1676 // layer will have children that intersect with the background layer.
1677 // A better solution might be to introduce a flattening layer if
1678 // we do direct box decoration composition.
1679 // https://bugs.webkit.org/show_bug.cgi?id=119461
1680 if (hasPerspectiveOrPreserves3D(&renderer.style()))
1683 // FIXME: we should be able to allow backgroundComposite; However since this is not a common use case it has been deferred for now.
1684 if (renderer.style().backgroundComposite() != CompositeSourceOver)
1687 if (renderer.style().backgroundClip() == TextFillBox)
1693 bool RenderLayerBacking::paintsBoxDecorations() const
1695 if (!m_owningLayer.hasVisibleBoxDecorations())
1698 if (!supportsDirectBoxDecorationsComposition(renderer()))
1704 bool RenderLayerBacking::paintsChildren() const
1706 if (m_owningLayer.hasVisibleContent() && m_owningLayer.hasNonEmptyChildRenderers())
1709 if (hasVisibleNonCompositingDescendantLayers())
1715 static bool isRestartedPlugin(RenderObject* renderer)
1717 if (!renderer->isEmbeddedObject())
1720 Element* element = toElement(renderer->node());
1721 if (!element || !element->isPluginElement())
1724 return toHTMLPlugInElement(element)->isRestartedPlugin();
1727 static bool isCompositedPlugin(RenderObject* renderer)
1729 return renderer->isEmbeddedObject() && toRenderEmbeddedObject(renderer)->allowsAcceleratedCompositing();
1732 // A "simple container layer" is a RenderLayer which has no visible content to render.
1733 // It may have no children, or all its children may be themselves composited.
1734 // This is a useful optimization, because it allows us to avoid allocating backing store.
1735 bool RenderLayerBacking::isSimpleContainerCompositingLayer() const
1737 if (renderer().hasMask()) // masks require special treatment
1740 if (renderer().isReplaced() && (!isCompositedPlugin(&renderer()) || isRestartedPlugin(&renderer())))
1743 if (paintsBoxDecorations() || paintsChildren())
1746 if (renderer().isRenderNamedFlowFragmentContainer())
1749 if (renderer().isRenderView()) {
1750 // Look to see if the root object has a non-simple background
1751 RenderObject* rootObject = renderer().document().documentElement() ? renderer().document().documentElement()->renderer() : 0;
1755 RenderStyle* style = &rootObject->style();
1757 // Reject anything that has a border, a border-radius or outline,
1758 // or is not a simple background (no background, or solid color).
1759 if (hasBoxDecorationsOrBackgroundImage(style))
1762 // Now look at the body's renderer.
1763 HTMLElement* body = renderer().document().body();
1764 RenderObject* bodyObject = (body && body->hasLocalName(bodyTag)) ? body->renderer() : 0;
1768 style = &bodyObject->style();
1770 if (hasBoxDecorationsOrBackgroundImage(style))
1777 static bool hasVisibleNonCompositingDescendant(RenderLayer& parent)
1779 // FIXME: We shouldn't be called with a stale z-order lists. See bug 85512.
1780 parent.updateLayerListsIfNeeded();
1782 #if !ASSERT_DISABLED
1783 LayerListMutationDetector mutationChecker(&parent);
1786 if (Vector<RenderLayer*>* normalFlowList = parent.normalFlowList()) {
1787 size_t listSize = normalFlowList->size();
1788 for (size_t i = 0; i < listSize; ++i) {
1789 RenderLayer* curLayer = normalFlowList->at(i);
1790 if (!curLayer->isComposited()
1791 && (curLayer->hasVisibleContent() || hasVisibleNonCompositingDescendant(*curLayer)))
1796 if (parent.isStackingContainer()) {
1797 if (!parent.hasVisibleDescendant())
1800 // Use the m_hasCompositingDescendant bit to optimize?
1801 if (Vector<RenderLayer*>* negZOrderList = parent.negZOrderList()) {
1802 size_t listSize = negZOrderList->size();
1803 for (size_t i = 0; i < listSize; ++i) {
1804 RenderLayer* curLayer = negZOrderList->at(i);
1805 if (!curLayer->isComposited()
1806 && (curLayer->hasVisibleContent() || hasVisibleNonCompositingDescendant(*curLayer)))
1811 if (Vector<RenderLayer*>* posZOrderList = parent.posZOrderList()) {
1812 size_t listSize = posZOrderList->size();
1813 for (size_t i = 0; i < listSize; ++i) {
1814 RenderLayer* curLayer = posZOrderList->at(i);
1815 if (!curLayer->isComposited()
1816 && (curLayer->hasVisibleContent() || hasVisibleNonCompositingDescendant(*curLayer)))
1825 // Conservative test for having no rendered children.
1826 bool RenderLayerBacking::hasVisibleNonCompositingDescendantLayers() const
1828 return hasVisibleNonCompositingDescendant(m_owningLayer);
1831 bool RenderLayerBacking::containsPaintedContent(bool isSimpleContainer) const
1833 if (isSimpleContainer || paintsIntoWindow() || paintsIntoCompositedAncestor() || m_artificiallyInflatedBounds || m_owningLayer.isReflection())
1836 if (isDirectlyCompositedImage())
1839 // FIXME: we could optimize cases where the image, video or canvas is known to fill the border box entirely,
1840 // and set background color on the layer in that case, instead of allocating backing store and painting.
1842 if (renderer().isVideo() && toRenderVideo(renderer()).shouldDisplayVideo())
1843 return m_owningLayer.hasBoxDecorationsOrBackground();
1846 #if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
1847 if (renderer().isCanvas() && canvasCompositingStrategy(renderer()) == CanvasAsLayerContents)
1848 return m_owningLayer.hasBoxDecorationsOrBackground();
1854 // An image can be directly compositing if it's the sole content of the layer, and has no box decorations
1855 // that require painting. Direct compositing saves backing store.
1856 bool RenderLayerBacking::isDirectlyCompositedImage() const
1858 if (!renderer().isImage() || m_owningLayer.hasBoxDecorationsOrBackground() || renderer().hasClip())
1861 RenderImage& imageRenderer = toRenderImage(renderer());
1862 if (CachedImage* cachedImage = imageRenderer.cachedImage()) {
1863 if (!cachedImage->hasImage())
1866 Image* image = cachedImage->imageForRenderer(&imageRenderer);
1867 if (!image->isBitmapImage())
1870 if (image->orientationForCurrentFrame() != DefaultImageOrientation)
1873 return m_graphicsLayer->shouldDirectlyCompositeImage(image);
1879 void RenderLayerBacking::contentChanged(ContentChangeType changeType)
1881 if ((changeType == ImageChanged) && isDirectlyCompositedImage()) {
1882 updateImageContents();
1886 if ((changeType == BackgroundImageChanged) && canCreateTiledImage(&renderer().style()))
1887 updateGraphicsLayerGeometry();
1889 if ((changeType == MaskImageChanged) && m_maskLayer) {
1890 // The composited layer bounds relies on box->maskClipRect(), which changes
1891 // when the mask image becomes available.
1892 updateAfterLayout(CompositingChildrenOnly | IsUpdateRoot);
1895 #if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
1896 if ((changeType == CanvasChanged || changeType == CanvasPixelsChanged) && renderer().isCanvas() && canvasCompositingStrategy(renderer()) == CanvasAsLayerContents) {
1897 m_graphicsLayer->setContentsNeedsDisplay();
1903 void RenderLayerBacking::updateImageContents()
1905 ASSERT(renderer().isImage());
1906 RenderImage& imageRenderer = toRenderImage(renderer());
1908 CachedImage* cachedImage = imageRenderer.cachedImage();
1912 Image* image = cachedImage->imageForRenderer(&imageRenderer);
1916 // We have to wait until the image is fully loaded before setting it on the layer.
1917 if (!cachedImage->isLoaded())
1920 // This is a no-op if the layer doesn't have an inner layer for the image.
1921 m_graphicsLayer->setContentsRect(pixelSnappedIntRect(contentsBox()));
1923 LayoutRect contentsClippingRect = imageRenderer.contentBoxRect();
1924 contentsClippingRect.move(contentOffsetInCompostingLayer());
1925 m_graphicsLayer->setContentsClippingRect(pixelSnappedIntRect(contentsClippingRect));
1927 m_graphicsLayer->setContentsToImage(image);
1928 bool isSimpleContainer = false;
1929 updateDrawsContent(isSimpleContainer);
1931 // Image animation is "lazy", in that it automatically stops unless someone is drawing
1932 // the image. So we have to kick the animation each time; this has the downside that the
1933 // image will keep animating, even if its layer is not visible.
1934 image->startAnimation();
1937 FloatPoint3D RenderLayerBacking::computeTransformOrigin(const IntRect& borderBox) const
1939 const RenderStyle& style = renderer().style();
1941 FloatPoint3D origin;
1942 origin.setX(floatValueForLength(style.transformOriginX(), borderBox.width()));
1943 origin.setY(floatValueForLength(style.transformOriginY(), borderBox.height()));
1944 origin.setZ(style.transformOriginZ());
1949 FloatPoint RenderLayerBacking::computePerspectiveOrigin(const IntRect& borderBox) const
1951 const RenderStyle& style = renderer().style();
1953 float boxWidth = borderBox.width();
1954 float boxHeight = borderBox.height();
1957 origin.setX(floatValueForLength(style.perspectiveOriginX(), boxWidth));
1958 origin.setY(floatValueForLength(style.perspectiveOriginY(), boxHeight));
1963 // Return the offset from the top-left of this compositing layer at which the renderer's contents are painted.
1964 LayoutSize RenderLayerBacking::contentOffsetInCompostingLayer() const
1966 return LayoutSize(-m_compositedBounds.x(), -m_compositedBounds.y());
1969 LayoutRect RenderLayerBacking::contentsBox() const
1971 if (!renderer().isBox())
1972 return LayoutRect();
1974 RenderBox& renderBox = toRenderBox(renderer());
1975 LayoutRect contentsRect;
1977 if (renderBox.isVideo())
1978 contentsRect = toRenderVideo(renderBox).videoBox();
1981 if (renderBox.isRenderReplaced()) {
1982 RenderReplaced& renderReplaced = *toRenderReplaced(&renderBox);
1983 contentsRect = renderReplaced.replacedContentRect(renderBox.intrinsicSize());
1985 contentsRect = renderBox.contentBoxRect();
1987 contentsRect.move(contentOffsetInCompostingLayer());
1988 return contentsRect;
1991 static LayoutRect backgroundRectForBox(const RenderBox& box)
1993 switch (box.style().backgroundClip()) {
1995 return box.borderBoxRect();
1996 case PaddingFillBox:
1997 return box.paddingBoxRect();
1998 case ContentFillBox:
1999 return box.contentBoxRect();
2004 ASSERT_NOT_REACHED();
2005 return LayoutRect();
2008 IntRect RenderLayerBacking::backgroundBox() const
2010 if (!renderer().isBox())
2013 LayoutRect backgroundBox = backgroundRectForBox(toRenderBox(renderer()));
2014 backgroundBox.move(contentOffsetInCompostingLayer());
2015 return pixelSnappedIntRect(backgroundBox);
2018 GraphicsLayer* RenderLayerBacking::parentForSublayers() const
2020 if (m_scrollingContentsLayer)
2021 return m_scrollingContentsLayer.get();
2024 // FIXME: Can we remove this iOS-specific code path?
2025 if (GraphicsLayer* clippingLayer = this->clippingLayer())
2026 return clippingLayer;
2027 return m_graphicsLayer.get();
2029 return m_childContainmentLayer ? m_childContainmentLayer.get() : m_graphicsLayer.get();
2033 GraphicsLayer* RenderLayerBacking::childForSuperlayers() const
2035 if (m_ancestorClippingLayer)
2036 return m_ancestorClippingLayer.get();
2038 if (m_contentsContainmentLayer)
2039 return m_contentsContainmentLayer.get();
2041 return m_graphicsLayer.get();
2044 bool RenderLayerBacking::paintsIntoWindow() const
2046 if (m_usingTiledCacheLayer)
2049 if (m_owningLayer.isRootLayer()) {
2050 #if PLATFORM(IOS) || USE(COORDINATED_GRAPHICS)
2051 if (compositor().inForcedCompositingMode())
2055 return compositor().rootLayerAttachment() != RenderLayerCompositor::RootLayerAttachedViaEnclosingFrame;
2061 void RenderLayerBacking::setRequiresOwnBackingStore(bool requiresOwnBacking)
2063 if (requiresOwnBacking == m_requiresOwnBackingStore)
2066 m_requiresOwnBackingStore = requiresOwnBacking;
2068 // This affects the answer to paintsIntoCompositedAncestor(), which in turn affects
2069 // cached clip rects, so when it changes we have to clear clip rects on descendants.
2070 m_owningLayer.clearClipRectsIncludingDescendants(PaintingClipRects);
2071 m_owningLayer.computeRepaintRectsIncludingDescendants();
2073 compositor().repaintInCompositedAncestor(m_owningLayer, compositedBounds());
2076 #if ENABLE(CSS_COMPOSITING)
2077 void RenderLayerBacking::setBlendMode(BlendMode blendMode)
2079 m_graphicsLayer->setBlendMode(blendMode);
2083 void RenderLayerBacking::setContentsNeedDisplay(GraphicsLayer::ShouldClipToLayer shouldClip)
2085 ASSERT(!paintsIntoCompositedAncestor());
2087 FrameView& frameView = owningLayer().renderer().view().frameView();
2088 if (m_isMainFrameRenderViewLayer && frameView.isTrackingRepaints())
2089 frameView.addTrackedRepaintRect(owningLayer().absoluteBoundingBox());
2091 if (m_graphicsLayer && m_graphicsLayer->drawsContent()) {
2092 // By default, setNeedsDisplay will clip to the size of the GraphicsLayer, which does not include margin tiles.
2093 // So if the TiledBacking has a margin that needs to be invalidated, we need to send in a rect to setNeedsDisplayInRect
2094 // that is large enough to include the margin. TiledBacking::bounds() includes the margin.
2095 TiledBacking* tiledBacking = this->tiledBacking();
2096 FloatRect rectToRepaint = tiledBacking ? tiledBacking->bounds() : FloatRect(FloatPoint(0, 0), m_graphicsLayer->size());
2097 m_graphicsLayer->setNeedsDisplayInRect(rectToRepaint, shouldClip);
2100 if (m_foregroundLayer && m_foregroundLayer->drawsContent())
2101 m_foregroundLayer->setNeedsDisplay();
2103 if (m_backgroundLayer && m_backgroundLayer->drawsContent())
2104 m_backgroundLayer->setNeedsDisplay();
2106 if (m_maskLayer && m_maskLayer->drawsContent())
2107 m_maskLayer->setNeedsDisplay();
2109 if (m_scrollingContentsLayer && m_scrollingContentsLayer->drawsContent())
2110 m_scrollingContentsLayer->setNeedsDisplay();
2113 // r is in the coordinate space of the layer's render object
2114 void RenderLayerBacking::setContentsNeedDisplayInRect(const IntRect& r, GraphicsLayer::ShouldClipToLayer shouldClip)
2116 ASSERT(!paintsIntoCompositedAncestor());
2118 FrameView& frameView = owningLayer().renderer().view().frameView();
2119 if (m_isMainFrameRenderViewLayer && frameView.isTrackingRepaints())
2120 frameView.addTrackedRepaintRect(pixelSnappedIntRect(r));
2122 if (m_graphicsLayer && m_graphicsLayer->drawsContent()) {
2123 IntRect layerDirtyRect = r;
2124 layerDirtyRect.move(-m_graphicsLayer->offsetFromRenderer());
2125 m_graphicsLayer->setNeedsDisplayInRect(layerDirtyRect, shouldClip);
2128 if (m_foregroundLayer && m_foregroundLayer->drawsContent()) {
2129 IntRect layerDirtyRect = r;
2130 layerDirtyRect.move(-m_foregroundLayer->offsetFromRenderer());
2131 m_foregroundLayer->setNeedsDisplayInRect(layerDirtyRect, shouldClip);
2134 // FIXME: need to split out repaints for the background.
2135 if (m_backgroundLayer && m_backgroundLayer->drawsContent()) {
2136 IntRect layerDirtyRect = r;
2137 layerDirtyRect.move(-m_backgroundLayer->offsetFromRenderer());
2138 m_backgroundLayer->setNeedsDisplayInRect(layerDirtyRect, shouldClip);
2141 if (m_maskLayer && m_maskLayer->drawsContent()) {
2142 IntRect layerDirtyRect = r;
2143 layerDirtyRect.move(-m_maskLayer->offsetFromRenderer());
2144 m_maskLayer->setNeedsDisplayInRect(layerDirtyRect, shouldClip);
2147 if (m_scrollingContentsLayer && m_scrollingContentsLayer->drawsContent()) {
2148 IntRect layerDirtyRect = r;
2149 layerDirtyRect.move(-m_scrollingContentsLayer->offsetFromRenderer());
2151 // Account for the fact that RenderLayerBacking::updateGraphicsLayerGeometry() bakes scrollOffset into offsetFromRenderer on iOS.
2152 layerDirtyRect.move(-m_owningLayer.scrollOffset());
2154 m_scrollingContentsLayer->setNeedsDisplayInRect(layerDirtyRect, shouldClip);
2158 void RenderLayerBacking::paintIntoLayer(const GraphicsLayer* graphicsLayer, GraphicsContext* context,
2159 const IntRect& paintDirtyRect, // In the coords of rootLayer.
2160 PaintBehavior paintBehavior, GraphicsLayerPaintingPhase paintingPhase)
2162 if (paintsIntoWindow() || paintsIntoCompositedAncestor()) {
2164 // FIXME: Looks like the CALayer tree is out of sync with the GraphicsLayer heirarchy
2165 // when pages are restored from the PageCache.
2166 // <rdar://problem/8712587> ASSERT: When Going Back to Page with Plugins in PageCache
2167 ASSERT_NOT_REACHED();
2172 FontCachePurgePreventer fontCachePurgePreventer;
2174 RenderLayer::PaintLayerFlags paintFlags = 0;
2175 if (paintingPhase & GraphicsLayerPaintBackground)
2176 paintFlags |= RenderLayer::PaintLayerPaintingCompositingBackgroundPhase;
2177 if (paintingPhase & GraphicsLayerPaintForeground)
2178 paintFlags |= RenderLayer::PaintLayerPaintingCompositingForegroundPhase;
2179 if (paintingPhase & GraphicsLayerPaintMask)
2180 paintFlags |= RenderLayer::PaintLayerPaintingCompositingMaskPhase;
2181 if (paintingPhase & GraphicsLayerPaintOverflowContents)
2182 paintFlags |= RenderLayer::PaintLayerPaintingOverflowContents;
2183 if (paintingPhase & GraphicsLayerPaintCompositedScroll)
2184 paintFlags |= RenderLayer::PaintLayerPaintingCompositingScrollingPhase;
2186 if (graphicsLayer == m_backgroundLayer.get())
2187 paintFlags |= (RenderLayer::PaintLayerPaintingRootBackgroundOnly | RenderLayer::PaintLayerPaintingCompositingForegroundPhase); // Need PaintLayerPaintingCompositingForegroundPhase to walk child layers.
2188 else if (compositor().fixedRootBackgroundLayer())
2189 paintFlags |= RenderLayer::PaintLayerPaintingSkipRootBackground;
2191 // FIXME: GraphicsLayers need a way to split for RenderRegions.
2192 RenderLayer::LayerPaintingInfo paintingInfo(&m_owningLayer, paintDirtyRect, paintBehavior, m_subpixelAccumulation);
2193 m_owningLayer.paintLayerContents(context, paintingInfo, paintFlags);
2195 if (m_owningLayer.containsDirtyOverlayScrollbars())
2196 m_owningLayer.paintLayerContents(context, paintingInfo, paintFlags | RenderLayer::PaintLayerPaintingOverlayScrollbars);
2198 compositor().didPaintBacking(this);
2200 ASSERT(!m_owningLayer.m_usedTransparency);
2203 static void paintScrollbar(Scrollbar* scrollbar, GraphicsContext& context, const IntRect& clip)
2209 const IntRect& scrollbarRect = scrollbar->frameRect();
2210 context.translate(-scrollbarRect.x(), -scrollbarRect.y());
2211 IntRect transformedClip = clip;
2212 transformedClip.moveBy(scrollbarRect.location());
2213 scrollbar->paint(&context, transformedClip);
2217 // Up-call from compositing layer drawing callback.
2218 void RenderLayerBacking::paintContents(const GraphicsLayer* graphicsLayer, GraphicsContext& context, GraphicsLayerPaintingPhase paintingPhase, const IntRect& clip)
2221 if (Page* page = renderer().frame().page())
2222 page->setIsPainting(true);
2225 if (graphicsLayer == m_graphicsLayer.get()
2226 || graphicsLayer == m_foregroundLayer.get()
2227 || graphicsLayer == m_backgroundLayer.get()
2228 || graphicsLayer == m_maskLayer.get()
2229 || graphicsLayer == m_scrollingContentsLayer.get()) {
2230 InspectorInstrumentation::willPaint(&renderer());
2232 // The dirtyRect is in the coords of the painting root.
2233 IntRect dirtyRect = clip;
2234 if (!(paintingPhase & GraphicsLayerPaintOverflowContents))
2235 dirtyRect.intersect(enclosingIntRect(compositedBoundsIncludingMargin()));
2237 // We have to use the same root as for hit testing, because both methods can compute and cache clipRects.
2238 paintIntoLayer(graphicsLayer, &context, dirtyRect, PaintBehaviorNormal, paintingPhase);
2240 InspectorInstrumentation::didPaint(&renderer(), &context, clip);
2241 } else if (graphicsLayer == layerForHorizontalScrollbar()) {
2242 paintScrollbar(m_owningLayer.horizontalScrollbar(), context, clip);
2243 } else if (graphicsLayer == layerForVerticalScrollbar()) {
2244 paintScrollbar(m_owningLayer.verticalScrollbar(), context, clip);
2245 } else if (graphicsLayer == layerForScrollCorner()) {
2246 const IntRect& scrollCornerAndResizer = m_owningLayer.scrollCornerAndResizerRect();
2248 context.translate(-scrollCornerAndResizer.x(), -scrollCornerAndResizer.y());
2249 IntRect transformedClip = clip;
2250 transformedClip.moveBy(scrollCornerAndResizer.location());
2251 m_owningLayer.paintScrollCorner(&context, IntPoint(), transformedClip);
2252 m_owningLayer.paintResizer(&context, IntPoint(), transformedClip);
2256 if (Page* page = renderer().frame().page())
2257 page->setIsPainting(false);
2261 float RenderLayerBacking::pageScaleFactor() const
2263 return compositor().pageScaleFactor();
2266 float RenderLayerBacking::deviceScaleFactor() const
2268 return compositor().deviceScaleFactor();
2271 float RenderLayerBacking::contentsScaleMultiplierForNewTiles(const GraphicsLayer* layer) const
2273 return compositor().contentsScaleMultiplierForNewTiles(layer);
2276 void RenderLayerBacking::didCommitChangesForLayer(const GraphicsLayer* layer) const
2278 compositor().didFlushChangesForLayer(m_owningLayer, layer);
2281 bool RenderLayerBacking::getCurrentTransform(const GraphicsLayer* graphicsLayer, TransformationMatrix& transform) const
2283 GraphicsLayer* transformedLayer = m_contentsContainmentLayer.get() ? m_contentsContainmentLayer.get() : m_graphicsLayer.get();
2284 if (graphicsLayer != transformedLayer)
2287 if (m_owningLayer.hasTransform()) {
2288 transform = m_owningLayer.currentTransform(RenderStyle::ExcludeTransformOrigin);
2294 bool RenderLayerBacking::isTrackingRepaints() const
2296 return static_cast<GraphicsLayerClient&>(compositor()).isTrackingRepaints();
2299 bool RenderLayerBacking::shouldSkipLayerInDump(const GraphicsLayer* layer) const
2301 // Skip the root tile cache's flattening layer.
2302 return m_isMainFrameRenderViewLayer && layer && layer == m_childContainmentLayer.get();
2305 bool RenderLayerBacking::shouldDumpPropertyForLayer(const GraphicsLayer* layer, const char* propertyName) const
2307 // For backwards compatibility with WebKit1 and other platforms,
2308 // skip some properties on the root tile cache.
2309 if (m_isMainFrameRenderViewLayer && layer == m_graphicsLayer.get()) {
2310 if (!strcmp(propertyName, "drawsContent"))
2313 // Background color could be of interest to tests or other dumpers if it's non-white.
2314 if (!strcmp(propertyName, "backgroundColor") && layer->backgroundColor() == Color::white)
2317 // The root tile cache's repaints will show up at the top with FrameView's,
2318 // so don't dump them twice.
2319 if (!strcmp(propertyName, "repaintRects"))
2327 void RenderLayerBacking::verifyNotPainting()
2329 ASSERT(!renderer().frame().page() || !renderer().frame().page()->isPainting());
2333 bool RenderLayerBacking::startAnimation(double timeOffset, const Animation* anim, const KeyframeList& keyframes)
2335 bool hasOpacity = keyframes.containsProperty(CSSPropertyOpacity);
2336 bool hasTransform = renderer().isBox() && keyframes.containsProperty(CSSPropertyWebkitTransform);
2337 #if ENABLE(CSS_FILTERS)
2338 bool hasFilter = keyframes.containsProperty(CSSPropertyWebkitFilter);
2340 bool hasFilter = false;
2343 if (!hasOpacity && !hasTransform && !hasFilter)
2346 KeyframeValueList transformVector(AnimatedPropertyWebkitTransform);
2347 KeyframeValueList opacityVector(AnimatedPropertyOpacity);
2348 #if ENABLE(CSS_FILTERS)
2349 KeyframeValueList filterVector(AnimatedPropertyWebkitFilter);
2352 size_t numKeyframes = keyframes.size();
2353 for (size_t i = 0; i < numKeyframes; ++i) {
2354 const KeyframeValue& currentKeyframe = keyframes[i];
2355 const RenderStyle* keyframeStyle = currentKeyframe.style();
2356 double key = currentKeyframe.key();
2361 // Get timing function.
2362 RefPtr<TimingFunction> tf = keyframeStyle->hasAnimations() ? (*keyframeStyle->animations()).animation(0).timingFunction() : 0;
2364 bool isFirstOrLastKeyframe = key == 0 || key == 1;
2365 if ((hasTransform && isFirstOrLastKeyframe) || currentKeyframe.containsProperty(CSSPropertyWebkitTransform))
2366 transformVector.insert(TransformAnimationValue::create(key, keyframeStyle->transform(), tf));
2368 if ((hasOpacity && isFirstOrLastKeyframe) || currentKeyframe.containsProperty(CSSPropertyOpacity))
2369 opacityVector.insert(FloatAnimationValue::create(key, keyframeStyle->opacity(), tf));
2371 #if ENABLE(CSS_FILTERS)
2372 if ((hasFilter && isFirstOrLastKeyframe) || currentKeyframe.containsProperty(CSSPropertyWebkitFilter))
2373 filterVector.insert(FilterAnimationValue::create(key, keyframeStyle->filter(), tf));
2377 if (renderer().frame().page() && !renderer().frame().page()->settings().acceleratedCompositedAnimationsEnabled())
2380 bool didAnimate = false;
2382 if (hasTransform && m_graphicsLayer->addAnimation(transformVector, toRenderBox(renderer()).pixelSnappedBorderBoxRect().size(), anim, keyframes.animationName(), timeOffset))
2385 if (hasOpacity && m_graphicsLayer->addAnimation(opacityVector, IntSize(), anim, keyframes.animationName(), timeOffset))
2388 #if ENABLE(CSS_FILTERS)
2389 if (hasFilter && m_graphicsLayer->addAnimation(filterVector, IntSize(), anim, keyframes.animationName(), timeOffset))
2396 void RenderLayerBacking::animationPaused(double timeOffset, const String& animationName)
2398 m_graphicsLayer->pauseAnimation(animationName, timeOffset);
2401 void RenderLayerBacking::animationFinished(const String& animationName)
2403 m_graphicsLayer->removeAnimation(animationName);
2406 bool RenderLayerBacking::startTransition(double timeOffset, CSSPropertyID property, const RenderStyle* fromStyle, const RenderStyle* toStyle)
2408 bool didAnimate = false;
2410 ASSERT(property != CSSPropertyInvalid);
2412 if (property == CSSPropertyOpacity) {
2413 const Animation* opacityAnim = toStyle->transitionForProperty(CSSPropertyOpacity);
2414 if (opacityAnim && !opacityAnim->isEmptyOrZeroDuration()) {
2415 KeyframeValueList opacityVector(AnimatedPropertyOpacity);
2416 opacityVector.insert(FloatAnimationValue::create(0, compositingOpacity(fromStyle->opacity())));
2417 opacityVector.insert(FloatAnimationValue::create(1, compositingOpacity(toStyle->opacity())));
2418 // The boxSize param is only used for transform animations (which can only run on RenderBoxes), so we pass an empty size here.
2419 if (m_graphicsLayer->addAnimation(opacityVector, IntSize(), opacityAnim, GraphicsLayer::animationNameForTransition(AnimatedPropertyOpacity), timeOffset)) {
2420 // To ensure that the correct opacity is visible when the animation ends, also set the final opacity.
2421 updateOpacity(toStyle);
2427 if (property == CSSPropertyWebkitTransform && m_owningLayer.hasTransform()) {
2428 const Animation* transformAnim = toStyle->transitionForProperty(CSSPropertyWebkitTransform);
2429 if (transformAnim && !transformAnim->isEmptyOrZeroDuration()) {
2430 KeyframeValueList transformVector(AnimatedPropertyWebkitTransform);
2431 transformVector.insert(TransformAnimationValue::create(0, fromStyle->transform()));
2432 transformVector.insert(TransformAnimationValue::create(1, toStyle->transform()));
2433 if (m_graphicsLayer->addAnimation(transformVector, toRenderBox(renderer()).pixelSnappedBorderBoxRect().size(), transformAnim, GraphicsLayer::animationNameForTransition(AnimatedPropertyWebkitTransform), timeOffset)) {
2434 // To ensure that the correct transform is visible when the animation ends, also set the final transform.
2435 updateTransform(toStyle);
2441 #if ENABLE(CSS_FILTERS)
2442 if (property == CSSPropertyWebkitFilter && m_owningLayer.hasFilter()) {
2443 const Animation* filterAnim = toStyle->transitionForProperty(CSSPropertyWebkitFilter);
2444 if (filterAnim && !filterAnim->isEmptyOrZeroDuration()) {
2445 KeyframeValueList filterVector(AnimatedPropertyWebkitFilter);
2446 filterVector.insert(FilterAnimationValue::create(0, fromStyle->filter()));
2447 filterVector.insert(FilterAnimationValue::create(1, toStyle->filter()));
2448 if (m_graphicsLayer->addAnimation(filterVector, IntSize(), filterAnim, GraphicsLayer::animationNameForTransition(AnimatedPropertyWebkitFilter), timeOffset)) {
2449 // To ensure that the correct filter is visible when the animation ends, also set the final filter.
2450 updateFilters(toStyle);
2460 void RenderLayerBacking::transitionPaused(double timeOffset, CSSPropertyID property)
2462 AnimatedPropertyID animatedProperty = cssToGraphicsLayerProperty(property);
2463 if (animatedProperty != AnimatedPropertyInvalid)
2464 m_graphicsLayer->pauseAnimation(GraphicsLayer::animationNameForTransition(animatedProperty), timeOffset);
2467 void RenderLayerBacking::transitionFinished(CSSPropertyID property)
2469 AnimatedPropertyID animatedProperty = cssToGraphicsLayerProperty(property);
2470 if (animatedProperty != AnimatedPropertyInvalid)
2471 m_graphicsLayer->removeAnimation(GraphicsLayer::animationNameForTransition(animatedProperty));
2474 void RenderLayerBacking::notifyAnimationStarted(const GraphicsLayer*, double time)
2476 renderer().animation().notifyAnimationStarted(&renderer(), time);
2479 void RenderLayerBacking::notifyFlushRequired(const GraphicsLayer* layer)
2481 if (renderer().documentBeingDestroyed())
2483 compositor().scheduleLayerFlush(layer->canThrottleLayerFlush());
2486 void RenderLayerBacking::notifyFlushBeforeDisplayRefresh(const GraphicsLayer* layer)
2488 compositor().notifyFlushBeforeDisplayRefresh(layer);
2491 // This is used for the 'freeze' API, for testing only.
2492 void RenderLayerBacking::suspendAnimations(double time)
2494 m_graphicsLayer->suspendAnimations(time);
2497 void RenderLayerBacking::resumeAnimations()
2499 m_graphicsLayer->resumeAnimations();
2502 LayoutRect RenderLayerBacking::compositedBounds() const
2504 return m_compositedBounds;
2507 void RenderLayerBacking::setCompositedBounds(const LayoutRect& bounds)
2509 m_compositedBounds = bounds;
2512 LayoutRect RenderLayerBacking::compositedBoundsIncludingMargin() const
2514 TiledBacking* tiledBacking = this->tiledBacking();
2515 if (!tiledBacking || !tiledBacking->hasMargins())
2516 return compositedBounds();
2518 LayoutRect boundsIncludingMargin = compositedBounds();
2519 LayoutUnit leftMarginWidth = tiledBacking->leftMarginWidth();
2520 LayoutUnit topMarginHeight = tiledBacking->topMarginHeight();
2522 boundsIncludingMargin.moveBy(IntPoint(-leftMarginWidth, -topMarginHeight));
2523 boundsIncludingMargin.expand(leftMarginWidth + (LayoutUnit)tiledBacking->rightMarginWidth(), topMarginHeight + (LayoutUnit)tiledBacking->bottomMarginHeight());
2525 return boundsIncludingMargin;
2528 CSSPropertyID RenderLayerBacking::graphicsLayerToCSSProperty(AnimatedPropertyID property)
2530 CSSPropertyID cssProperty = CSSPropertyInvalid;
2532 case AnimatedPropertyWebkitTransform:
2533 cssProperty = CSSPropertyWebkitTransform;
2535 case AnimatedPropertyOpacity:
2536 cssProperty = CSSPropertyOpacity;
2538 case AnimatedPropertyBackgroundColor:
2539 cssProperty = CSSPropertyBackgroundColor;
2541 case AnimatedPropertyWebkitFilter:
2542 #if ENABLE(CSS_FILTERS)
2543 cssProperty = CSSPropertyWebkitFilter;
2545 ASSERT_NOT_REACHED();
2548 case AnimatedPropertyInvalid:
2549 ASSERT_NOT_REACHED();
2554 AnimatedPropertyID RenderLayerBacking::cssToGraphicsLayerProperty(CSSPropertyID cssProperty)
2556 switch (cssProperty) {
2557 case CSSPropertyWebkitTransform:
2558 return AnimatedPropertyWebkitTransform;
2559 case CSSPropertyOpacity:
2560 return AnimatedPropertyOpacity;
2561 case CSSPropertyBackgroundColor:
2562 return AnimatedPropertyBackgroundColor;
2563 #if ENABLE(CSS_FILTERS)
2564 case CSSPropertyWebkitFilter:
2565 return AnimatedPropertyWebkitFilter;
2568 // It's fine if we see other css properties here; they are just not accelerated.
2571 return AnimatedPropertyInvalid;
2574 CompositingLayerType RenderLayerBacking::compositingLayerType() const
2576 if (m_graphicsLayer->hasContentsLayer())
2577 return MediaCompositingLayer;
2579 if (m_graphicsLayer->drawsContent())
2580 return m_graphicsLayer->usingTiledBacking() ? TiledCompositingLayer : NormalCompositingLayer;
2582 return ContainerCompositingLayer;
2585 double RenderLayerBacking::backingStoreMemoryEstimate() const
2587 double backingMemory;
2589 // m_ancestorClippingLayer, m_contentsContainmentLayer and m_childContainmentLayer are just used for masking or containment, so have no backing.
2590 backingMemory = m_graphicsLayer->backingStoreMemoryEstimate();
2591 if (m_foregroundLayer)
2592 backingMemory += m_foregroundLayer->backingStoreMemoryEstimate();
2593 if (m_backgroundLayer)
2594 backingMemory += m_backgroundLayer->backingStoreMemoryEstimate();
2596 backingMemory += m_maskLayer->backingStoreMemoryEstimate();
2598 if (m_scrollingContentsLayer)
2599 backingMemory += m_scrollingContentsLayer->backingStoreMemoryEstimate();
2601 if (m_layerForHorizontalScrollbar)
2602 backingMemory += m_layerForHorizontalScrollbar->backingStoreMemoryEstimate();
2604 if (m_layerForVerticalScrollbar)
2605 backingMemory += m_layerForVerticalScrollbar->backingStoreMemoryEstimate();
2607 if (m_layerForScrollCorner)
2608 backingMemory += m_layerForScrollCorner->backingStoreMemoryEstimate();
2610 return backingMemory;
2613 } // namespace WebCore
2615 #endif // USE(ACCELERATED_COMPOSITING)