RenderLayerBacking.cpp [plain text]
#include "config.h"
#include "RenderLayerBacking.h"
#include "AnimationController.h"
#include "CanvasRenderingContext.h"
#include "CSSPropertyNames.h"
#include "CachedImage.h"
#include "Chrome.h"
#include "FontCache.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "GraphicsLayer.h"
#include "HTMLCanvasElement.h"
#include "HTMLIFrameElement.h"
#include "HTMLMediaElement.h"
#include "HTMLNames.h"
#include "HTMLPlugInElement.h"
#include "InspectorInstrumentation.h"
#include "KeyframeList.h"
#include "MainFrame.h"
#include "PluginViewBase.h"
#include "ProgressTracker.h"
#include "RenderFlowThread.h"
#include "RenderIFrame.h"
#include "RenderImage.h"
#include "RenderLayerCompositor.h"
#include "RenderEmbeddedObject.h"
#include "RenderNamedFlowFragment.h"
#include "RenderRegion.h"
#include "RenderVideo.h"
#include "RenderView.h"
#include "ScrollingCoordinator.h"
#include "Settings.h"
#include "StyleResolver.h"
#include "TiledBacking.h"
#include <wtf/text/StringBuilder.h>
#if ENABLE(CSS_FILTERS)
#include "FilterEffectRenderer.h"
#endif
#if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
#include "GraphicsContext3D.h"
#endif
namespace WebCore {
using namespace HTMLNames;
CanvasCompositingStrategy canvasCompositingStrategy(const RenderObject& renderer)
{
ASSERT(renderer.isCanvas());
const HTMLCanvasElement* canvas = toHTMLCanvasElement(renderer.node());
CanvasRenderingContext* context = canvas->renderingContext();
if (!context || !context->isAccelerated())
return UnacceleratedCanvas;
if (context->is3d())
return CanvasAsLayerContents;
#if ENABLE(ACCELERATED_2D_CANVAS)
return CanvasAsLayerContents;
#else
return CanvasPaintedToLayer; #endif
}
static ScrollingCoordinator* scrollingCoordinatorFromLayer(RenderLayer& layer)
{
Page* page = layer.renderer().frame().page();
if (!page)
return 0;
return page->scrollingCoordinator();
}
bool RenderLayerBacking::m_creatingPrimaryGraphicsLayer = false;
RenderLayerBacking::RenderLayerBacking(RenderLayer& layer)
: m_owningLayer(layer)
, m_viewportConstrainedNodeID(0)
, m_scrollingNodeID(0)
, m_artificiallyInflatedBounds(false)
, m_isMainFrameRenderViewLayer(false)
, m_usingTiledCacheLayer(false)
, m_requiresOwnBackingStore(true)
#if ENABLE(CSS_FILTERS)
, m_canCompositeFilters(false)
#endif
, m_backgroundLayerPaintsFixedRootBackground(false)
{
Page* page = renderer().frame().page();
if (layer.isRootLayer() && page) {
m_isMainFrameRenderViewLayer = renderer().frame().isMainFrame();
m_usingTiledCacheLayer = page->chrome().client().shouldUseTiledBackingForFrameView(renderer().frame().view());
}
createPrimaryGraphicsLayer();
if (m_usingTiledCacheLayer && page) {
TiledBacking* tiledBacking = this->tiledBacking();
tiledBacking->setIsInWindow(page->isInWindow());
if (m_isMainFrameRenderViewLayer)
tiledBacking->setUnparentsOffscreenTiles(true);
tiledBacking->setScrollingPerformanceLoggingEnabled(page->settings().scrollingPerformanceLoggingEnabled());
adjustTiledBackingCoverage();
}
}
RenderLayerBacking::~RenderLayerBacking()
{
updateAncestorClippingLayer(false);
updateDescendantClippingLayer(false);
updateOverflowControlsLayers(false, false, false);
updateForegroundLayer(false);
updateBackgroundLayer(false);
updateMaskLayer(false);
updateScrollingLayers(false);
detachFromScrollingCoordinator();
destroyGraphicsLayers();
}
void RenderLayerBacking::willDestroyLayer(const GraphicsLayer* layer)
{
if (layer && layer->usingTiledBacking())
compositor().layerTiledBackingUsageChanged(layer, false);
}
std::unique_ptr<GraphicsLayer> RenderLayerBacking::createGraphicsLayer(const String& name)
{
GraphicsLayerFactory* graphicsLayerFactory = 0;
if (Page* page = renderer().frame().page())
graphicsLayerFactory = page->chrome().client().graphicsLayerFactory();
std::unique_ptr<GraphicsLayer> graphicsLayer = GraphicsLayer::create(graphicsLayerFactory, *this);
#ifndef NDEBUG
graphicsLayer->setName(name);
#else
UNUSED_PARAM(name);
#endif
#if PLATFORM(COCOA) && USE(CA)
graphicsLayer->setAcceleratesDrawing(compositor().acceleratedDrawingEnabled());
#endif
return graphicsLayer;
}
bool RenderLayerBacking::shouldUseTiledBacking(const GraphicsLayer*) const
{
return m_usingTiledCacheLayer && m_creatingPrimaryGraphicsLayer;
}
void RenderLayerBacking::tiledBackingUsageChanged(const GraphicsLayer* layer, bool usingTiledBacking)
{
compositor().layerTiledBackingUsageChanged(layer, usingTiledBacking);
}
TiledBacking* RenderLayerBacking::tiledBacking() const
{
return m_graphicsLayer->tiledBacking();
}
static TiledBacking::TileCoverage computeTileCoverage(RenderLayerBacking* backing)
{
FrameView& frameView = backing->owningLayer().renderer().view().frameView();
TiledBacking::TileCoverage tileCoverage = TiledBacking::CoverageForVisibleArea;
bool useMinimalTilesDuringLiveResize = frameView.inLiveResize();
if (frameView.speculativeTilingEnabled() && !useMinimalTilesDuringLiveResize) {
bool clipsToExposedRect = !frameView.exposedRect().isInfinite();
if (frameView.horizontalScrollbarMode() != ScrollbarAlwaysOff || clipsToExposedRect)
tileCoverage |= TiledBacking::CoverageForHorizontalScrolling;
if (frameView.verticalScrollbarMode() != ScrollbarAlwaysOff || clipsToExposedRect)
tileCoverage |= TiledBacking::CoverageForVerticalScrolling;
}
return tileCoverage;
}
void RenderLayerBacking::adjustTiledBackingCoverage()
{
if (!m_usingTiledCacheLayer)
return;
TiledBacking::TileCoverage tileCoverage = computeTileCoverage(this);
tiledBacking()->setTileCoverage(tileCoverage);
}
void RenderLayerBacking::setTiledBackingHasMargins(bool hasExtendedBackgroundOnLeftAndRight, bool hasExtendedBackgroundOnTopAndBottom)
{
if (!m_usingTiledCacheLayer)
return;
int marginLeftAndRightSize = hasExtendedBackgroundOnLeftAndRight ? defaultTileWidth : 0;
int marginTopAndBottomSize = hasExtendedBackgroundOnTopAndBottom ? defaultTileHeight : 0;
tiledBacking()->setTileMargins(marginTopAndBottomSize, marginTopAndBottomSize, marginLeftAndRightSize, marginLeftAndRightSize);
}
void RenderLayerBacking::updateDebugIndicators(bool showBorder, bool showRepaintCounter)
{
m_graphicsLayer->setShowDebugBorder(showBorder);
m_graphicsLayer->setShowRepaintCounter(showRepaintCounter);
if (m_ancestorClippingLayer)
m_ancestorClippingLayer->setShowDebugBorder(showBorder);
if (m_foregroundLayer) {
m_foregroundLayer->setShowDebugBorder(showBorder);
m_foregroundLayer->setShowRepaintCounter(showRepaintCounter);
}
if (m_contentsContainmentLayer)
m_contentsContainmentLayer->setShowDebugBorder(showBorder);
if (m_backgroundLayer) {
m_backgroundLayer->setShowDebugBorder(showBorder);
m_backgroundLayer->setShowRepaintCounter(showRepaintCounter);
}
if (m_maskLayer) {
m_maskLayer->setShowDebugBorder(showBorder);
m_maskLayer->setShowRepaintCounter(showRepaintCounter);
}
if (m_layerForHorizontalScrollbar)
m_layerForHorizontalScrollbar->setShowDebugBorder(showBorder);
if (m_layerForVerticalScrollbar)
m_layerForVerticalScrollbar->setShowDebugBorder(showBorder);
if (m_layerForScrollCorner)
m_layerForScrollCorner->setShowDebugBorder(showBorder);
if (m_scrollingLayer)
m_scrollingLayer->setShowDebugBorder(showBorder);
if (m_scrollingContentsLayer) {
m_scrollingContentsLayer->setShowDebugBorder(showBorder);
m_scrollingContentsLayer->setShowRepaintCounter(showRepaintCounter);
}
}
void RenderLayerBacking::createPrimaryGraphicsLayer()
{
String layerName;
#ifndef NDEBUG
layerName = m_owningLayer.name();
#endif
m_creatingPrimaryGraphicsLayer = true;
m_graphicsLayer = createGraphicsLayer(layerName);
m_creatingPrimaryGraphicsLayer = false;
if (m_usingTiledCacheLayer) {
m_childContainmentLayer = createGraphicsLayer("TiledBacking Flattening Layer");
m_graphicsLayer->addChild(m_childContainmentLayer.get());
}
#if !PLATFORM(IOS) && !PLATFORM(EFL)
if (m_isMainFrameRenderViewLayer) {
m_graphicsLayer->setContentsOpaque(true);
m_graphicsLayer->setAppliesPageScale();
}
#endif
#if PLATFORM(COCOA) && USE(CA)
if (!compositor().acceleratedDrawingEnabled() && renderer().isCanvas()) {
const HTMLCanvasElement* canvas = toHTMLCanvasElement(renderer().element());
if (canvas->shouldAccelerate(canvas->size()))
m_graphicsLayer->setAcceleratesDrawing(true);
}
#endif
updateOpacity(renderer().style());
updateTransform(renderer().style());
#if ENABLE(CSS_FILTERS)
updateFilters(renderer().style());
#endif
#if ENABLE(CSS_COMPOSITING)
updateBlendMode(renderer().style());
#endif
}
#if PLATFORM(IOS)
void RenderLayerBacking::layerWillBeDestroyed()
{
RenderObject& renderer = this->renderer();
if (renderer.isEmbeddedObject() && toRenderEmbeddedObject(renderer).allowsAcceleratedCompositing()) {
PluginViewBase* pluginViewBase = toPluginViewBase(toRenderWidget(renderer).widget());
if (pluginViewBase && m_graphicsLayer->contentsLayerForMedia())
pluginViewBase->detachPluginLayer();
}
}
#endif
void RenderLayerBacking::destroyGraphicsLayers()
{
if (m_graphicsLayer) {
willDestroyLayer(m_graphicsLayer.get());
m_graphicsLayer->removeFromParent();
}
m_ancestorClippingLayer = nullptr;
m_contentsContainmentLayer = nullptr;
m_graphicsLayer = nullptr;
m_foregroundLayer = nullptr;
m_backgroundLayer = nullptr;
m_childContainmentLayer = nullptr;
m_maskLayer = nullptr;
m_scrollingLayer = nullptr;
m_scrollingContentsLayer = nullptr;
}
void RenderLayerBacking::updateOpacity(const RenderStyle& style)
{
m_graphicsLayer->setOpacity(compositingOpacity(style.opacity()));
}
void RenderLayerBacking::updateTransform(const RenderStyle& style)
{
TransformationMatrix t;
if (m_owningLayer.hasTransform()) {
RenderBox& renderBox = toRenderBox(renderer());
style.applyTransform(t, pixelSnappedForPainting(renderBox.borderBoxRect(), renderBox.document().deviceScaleFactor()), RenderStyle::ExcludeTransformOrigin);
makeMatrixRenderable(t, compositor().canRender3DTransforms());
}
if (m_contentsContainmentLayer) {
m_contentsContainmentLayer->setTransform(t);
m_graphicsLayer->setTransform(TransformationMatrix());
} else
m_graphicsLayer->setTransform(t);
}
#if ENABLE(CSS_FILTERS)
void RenderLayerBacking::updateFilters(const RenderStyle& style)
{
m_canCompositeFilters = m_graphicsLayer->setFilters(style.filter());
}
#endif
#if ENABLE(CSS_COMPOSITING)
void RenderLayerBacking::updateBlendMode(const RenderStyle& style)
{
if (m_ancestorClippingLayer) {
m_ancestorClippingLayer->setBlendMode(style.blendMode());
m_graphicsLayer->setBlendMode(BlendModeNormal);
} else
m_graphicsLayer->setBlendMode(style.blendMode());
}
#endif
static bool layerOrAncestorIsTransformedOrUsingCompositedScrolling(RenderLayer& layer)
{
for (RenderLayer* curr = &layer; curr; curr = curr->parent()) {
if (curr->hasTransform()
#if PLATFORM(IOS)
|| curr->hasTouchScrollableOverflow()
#else
|| curr->needsCompositedScrolling()
#endif
)
return true;
}
return false;
}
bool RenderLayerBacking::shouldClipCompositedBounds() const
{
#if !PLATFORM(IOS)
if (layerForHorizontalScrollbar() || layerForVerticalScrollbar())
return false;
#endif
if (m_usingTiledCacheLayer)
return false;
if (layerOrAncestorIsTransformedOrUsingCompositedScrolling(m_owningLayer))
return false;
if (m_owningLayer.isFlowThreadCollectingGraphicsLayersUnderRegions())
return false;
return true;
}
static bool hasNonZeroTransformOrigin(const RenderObject& renderer)
{
const RenderStyle& style = renderer.style();
return (style.transformOriginX().type() == Fixed && style.transformOriginX().value())
|| (style.transformOriginY().type() == Fixed && style.transformOriginY().value());
}
void RenderLayerBacking::updateCompositedBounds()
{
LayoutRect layerBounds = m_owningLayer.calculateLayerBounds(&m_owningLayer, LayoutSize(), RenderLayer::DefaultCalculateLayerBoundsFlags | RenderLayer::ExcludeHiddenDescendants | RenderLayer::DontConstrainForMask);
if (shouldClipCompositedBounds()) {
RenderView& view = m_owningLayer.renderer().view();
RenderLayer* rootLayer = view.layer();
LayoutRect clippingBounds;
if (renderer().style().position() == FixedPosition && renderer().container() == &view)
clippingBounds = view.frameView().viewportConstrainedVisibleContentRect();
else
clippingBounds = view.unscaledDocumentRect();
if (&m_owningLayer != rootLayer)
clippingBounds.intersect(m_owningLayer.backgroundClipRect(RenderLayer::ClipRectsContext(rootLayer, AbsoluteClipRects)).rect());
LayoutPoint delta = m_owningLayer.convertToLayerCoords(rootLayer, LayoutPoint(), RenderLayer::AdjustForColumns);
clippingBounds.move(-delta.x(), -delta.y());
layerBounds.intersect(clippingBounds);
}
if (layerBounds.isEmpty() && hasNonZeroTransformOrigin(renderer())) {
layerBounds.setWidth(1);
layerBounds.setHeight(1);
m_artificiallyInflatedBounds = true;
} else
m_artificiallyInflatedBounds = false;
setCompositedBounds(layerBounds);
}
void RenderLayerBacking::updateAfterWidgetResize()
{
if (!renderer().isWidget())
return;
if (RenderLayerCompositor* innerCompositor = RenderLayerCompositor::frameContentsCompositor(toRenderWidget(&renderer()))) {
innerCompositor->frameViewDidChangeSize();
innerCompositor->frameViewDidChangeLocation(flooredIntPoint(contentsBox().location()));
}
}
void RenderLayerBacking::updateAfterLayout(UpdateAfterLayoutFlags flags)
{
if (!compositor().compositingLayersNeedRebuild()) {
updateCompositedBounds();
compositor().updateCompositingDescendantGeometry(m_owningLayer, m_owningLayer, flags & CompositingChildrenOnly);
if (flags & IsUpdateRoot) {
updateGeometry();
compositor().updateRootLayerPosition();
RenderLayer* stackingContainer = m_owningLayer.enclosingStackingContainer();
if (!compositor().compositingLayersNeedRebuild() && stackingContainer && (stackingContainer != &m_owningLayer))
compositor().updateCompositingDescendantGeometry(*stackingContainer, *stackingContainer, flags & CompositingChildrenOnly);
}
}
if (flags & NeedsFullRepaint && !paintsIntoWindow() && !paintsIntoCompositedAncestor())
setContentsNeedDisplay();
}
bool RenderLayerBacking::updateConfiguration()
{
m_owningLayer.updateDescendantDependentFlags();
m_owningLayer.updateZOrderLists();
bool layerConfigChanged = false;
setBackgroundLayerPaintsFixedRootBackground(compositor().needsFixedRootBackgroundLayer(m_owningLayer));
if (updateBackgroundLayer(m_backgroundLayerPaintsFixedRootBackground))
layerConfigChanged = true;
if (updateForegroundLayer(compositor().needsContentsCompositingLayer(m_owningLayer)))
layerConfigChanged = true;
bool needsDescendentsClippingLayer = compositor().clipsCompositingDescendants(m_owningLayer);
if (!renderer().view().needsLayout()) {
bool usesCompositedScrolling;
#if PLATFORM(IOS)
usesCompositedScrolling = m_owningLayer.hasTouchScrollableOverflow();
#else
usesCompositedScrolling = m_owningLayer.needsCompositedScrolling();
#endif
if (usesCompositedScrolling)
needsDescendentsClippingLayer = false;
if (updateScrollingLayers(usesCompositedScrolling))
layerConfigChanged = true;
if (updateDescendantClippingLayer(needsDescendentsClippingLayer))
layerConfigChanged = true;
}
if (updateAncestorClippingLayer(compositor().clippedByAncestor(m_owningLayer)))
layerConfigChanged = true;
if (updateOverflowControlsLayers(requiresHorizontalScrollbarLayer(), requiresVerticalScrollbarLayer(), requiresScrollCornerLayer()))
layerConfigChanged = true;
if (layerConfigChanged)
updateInternalHierarchy();
if (GraphicsLayer* flatteningLayer = tileCacheFlatteningLayer()) {
if (layerConfigChanged || flatteningLayer->parent() != m_graphicsLayer.get())
m_graphicsLayer->addChild(flatteningLayer);
}
updateMaskLayer(renderer().hasMask());
if (m_owningLayer.hasReflection()) {
if (m_owningLayer.reflectionLayer()->backing()) {
GraphicsLayer* reflectionLayer = m_owningLayer.reflectionLayer()->backing()->graphicsLayer();
m_graphicsLayer->setReplicatedByLayer(reflectionLayer);
}
} else
m_graphicsLayer->setReplicatedByLayer(0);
if (!m_owningLayer.isRootLayer()) {
bool isSimpleContainer = isSimpleContainerCompositingLayer();
bool didUpdateContentsRect = false;
updateDirectlyCompositedContents(isSimpleContainer, didUpdateContentsRect);
} else
updateRootLayerConfiguration();
if (isDirectlyCompositedImage())
updateImageContents();
if (renderer().isEmbeddedObject() && toRenderEmbeddedObject(&renderer())->allowsAcceleratedCompositing()) {
PluginViewBase* pluginViewBase = toPluginViewBase(toRenderWidget(&renderer())->widget());
#if PLATFORM(IOS)
if (pluginViewBase && !m_graphicsLayer->contentsLayerForMedia()) {
pluginViewBase->detachPluginLayer();
pluginViewBase->attachPluginLayer();
}
#else
if (!pluginViewBase->shouldNotAddLayer())
m_graphicsLayer->setContentsToMedia(pluginViewBase->platformLayer());
#endif
}
#if ENABLE(VIDEO)
else if (renderer().isVideo()) {
HTMLMediaElement* mediaElement = toHTMLMediaElement(renderer().element());
m_graphicsLayer->setContentsToMedia(mediaElement->platformLayer());
}
#endif
#if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
else if (renderer().isCanvas() && canvasCompositingStrategy(renderer()) == CanvasAsLayerContents) {
const HTMLCanvasElement* canvas = toHTMLCanvasElement(renderer().element());
if (CanvasRenderingContext* context = canvas->renderingContext())
m_graphicsLayer->setContentsToCanvas(context->platformLayer());
layerConfigChanged = true;
}
#endif
if (renderer().isWidget())
layerConfigChanged = RenderLayerCompositor::parentFrameContentLayers(toRenderWidget(&renderer()));
return layerConfigChanged;
}
static LayoutRect clipBox(RenderBox& renderer)
{
LayoutRect result = LayoutRect::infiniteRect();
if (renderer.hasOverflowClip())
result = renderer.overflowClipRect(LayoutPoint(), 0);
if (renderer.hasClip())
result.intersect(renderer.clipRect(LayoutPoint(), 0));
return result;
}
static FloatSize pixelFractionForLayerPainting(const LayoutPoint& point, float pixelSnappingFactor)
{
LayoutUnit x = point.x();
LayoutUnit y = point.y();
x = x >= 0 ? floorToDevicePixel(x, pixelSnappingFactor) : ceilToDevicePixel(x, pixelSnappingFactor);
y = y >= 0 ? floorToDevicePixel(y, pixelSnappingFactor) : ceilToDevicePixel(y, pixelSnappingFactor);
return point - LayoutPoint(x, y);
}
static void calculateDevicePixelOffsetFromRenderer(const LayoutSize& rendererOffsetFromGraphicsLayer, FloatSize& devicePixelOffsetFromRenderer,
LayoutSize& devicePixelFractionFromRenderer, float deviceScaleFactor)
{
devicePixelFractionFromRenderer = LayoutSize(pixelFractionForLayerPainting(toLayoutPoint(rendererOffsetFromGraphicsLayer), deviceScaleFactor));
devicePixelOffsetFromRenderer = rendererOffsetFromGraphicsLayer - devicePixelFractionFromRenderer;
}
void RenderLayerBacking::updateGeometry()
{
if (m_owningLayer.isStackingContainer() && m_owningLayer.m_zOrderListsDirty)
return;
const RenderStyle& style = renderer().style();
if (!renderer().animation().isRunningAcceleratedAnimationOnRenderer(&renderer(), CSSPropertyWebkitTransform, AnimationBase::Running | AnimationBase::Paused | AnimationBase::FillingFowards))
updateTransform(style);
if (!renderer().animation().isRunningAcceleratedAnimationOnRenderer(&renderer(), CSSPropertyOpacity, AnimationBase::Running | AnimationBase::Paused | AnimationBase::FillingFowards))
updateOpacity(style);
#if ENABLE(CSS_FILTERS)
updateFilters(style);
#endif
#if ENABLE(CSS_COMPOSITING)
updateBlendMode(style);
#endif
m_owningLayer.updateDescendantDependentFlags();
bool preserves3D = style.transformStyle3D() == TransformStyle3DPreserve3D && !renderer().hasReflection();
m_graphicsLayer->setPreserves3D(preserves3D);
m_graphicsLayer->setBackfaceVisibility(style.backfaceVisibility() == BackfaceVisibilityVisible);
RenderLayer* compAncestor = m_owningLayer.ancestorCompositingLayer();
LayoutRect ancestorCompositingBounds;
if (compAncestor) {
ASSERT(compAncestor->backing());
ancestorCompositingBounds = compAncestor->backing()->compositedBounds();
}
float deviceScaleFactor = this->deviceScaleFactor();
LayoutRect localCompositingBounds = compositedBounds();
LayoutRect relativeCompositingBounds(localCompositingBounds);
LayoutPoint offsetFromParent = m_owningLayer.convertToLayerCoords(compAncestor, LayoutPoint(), RenderLayer::AdjustForColumns);
offsetFromParent = offsetFromParent + (compAncestor ? compAncestor->backing()->devicePixelFractionFromRenderer() : LayoutSize());
relativeCompositingBounds.moveBy(offsetFromParent);
LayoutRect enclosingRelativeCompositingBounds = LayoutRect(enclosingRectForPainting(relativeCompositingBounds, deviceScaleFactor));
LayoutSize subpixelOffsetAdjustment = enclosingRelativeCompositingBounds.location() - relativeCompositingBounds.location();
LayoutSize rendererOffsetFromGraphicsLayer = toLayoutSize(localCompositingBounds.location()) + subpixelOffsetAdjustment;
FloatSize devicePixelOffsetFromRenderer;
LayoutSize devicePixelFractionFromRenderer;
calculateDevicePixelOffsetFromRenderer(rendererOffsetFromGraphicsLayer, devicePixelOffsetFromRenderer, devicePixelFractionFromRenderer, deviceScaleFactor);
m_devicePixelFractionFromRenderer = LayoutSize(-devicePixelFractionFromRenderer.width(), -devicePixelFractionFromRenderer.height());
adjustAncestorCompositingBoundsForFlowThread(ancestorCompositingBounds, compAncestor);
LayoutPoint graphicsLayerParentLocation;
if (compAncestor && compAncestor->backing()->hasClippingLayer()) {
LayoutRect clippingBox = clipBox(toRenderBox(compAncestor->renderer()));
graphicsLayerParentLocation = clippingBox.location();
} else if (compAncestor)
graphicsLayerParentLocation = ancestorCompositingBounds.location();
else
graphicsLayerParentLocation = renderer().view().documentRect().location();
#if PLATFORM(IOS)
if (compAncestor && compAncestor->hasTouchScrollableOverflow()) {
RenderBox* renderBox = toRenderBox(&compAncestor->renderer());
LayoutRect paddingBox(renderBox->borderLeft(), renderBox->borderTop(),
renderBox->width() - renderBox->borderLeft() - renderBox->borderRight(),
renderBox->height() - renderBox->borderTop() - renderBox->borderBottom());
IntSize scrollOffset = compAncestor->scrolledContentOffset();
graphicsLayerParentLocation = paddingBox.location() - scrollOffset;
}
#else
if (compAncestor && compAncestor->needsCompositedScrolling()) {
RenderBox& renderBox = toRenderBox(compAncestor->renderer());
LayoutSize scrollOffset = compAncestor->scrolledContentOffset();
LayoutPoint scrollOrigin(renderBox.borderLeft(), renderBox.borderTop());
graphicsLayerParentLocation = scrollOrigin - scrollOffset;
}
#endif
if (compAncestor && m_ancestorClippingLayer) {
ShouldRespectOverflowClip shouldRespectOverflowClip = compAncestor->isolatesCompositedBlending() ? RespectOverflowClip : IgnoreOverflowClip;
RenderLayer::ClipRectsContext clipRectsContext(compAncestor, TemporaryClipRects, IgnoreOverlayScrollbarSize, shouldRespectOverflowClip);
LayoutRect parentClipRect = m_owningLayer.backgroundClipRect(clipRectsContext).rect(); ASSERT(parentClipRect != LayoutRect::infiniteRect());
m_ancestorClippingLayer->setPosition(FloatPoint(parentClipRect.location() - graphicsLayerParentLocation));
m_ancestorClippingLayer->setSize(parentClipRect.size());
m_ancestorClippingLayer->setOffsetFromRenderer(parentClipRect.location() - offsetFromParent);
graphicsLayerParentLocation = parentClipRect.location();
}
LayoutSize contentsSize = enclosingRelativeCompositingBounds.size();
if (m_contentsContainmentLayer) {
m_contentsContainmentLayer->setPreserves3D(preserves3D);
m_contentsContainmentLayer->setPosition(FloatPoint(enclosingRelativeCompositingBounds.location() - graphicsLayerParentLocation));
m_contentsContainmentLayer->setSize(contentsSize);
graphicsLayerParentLocation = enclosingRelativeCompositingBounds.location();
}
m_graphicsLayer->setPosition(FloatPoint(enclosingRelativeCompositingBounds.location() - graphicsLayerParentLocation));
m_graphicsLayer->setSize(contentsSize);
if (devicePixelOffsetFromRenderer != m_graphicsLayer->offsetFromRenderer()) {
m_graphicsLayer->setOffsetFromRenderer(devicePixelOffsetFromRenderer);
positionOverflowControlsLayers();
}
if (!m_isMainFrameRenderViewLayer) {
ASSERT(!m_backgroundLayer);
bool hadSubpixelRounding = enclosingRelativeCompositingBounds != relativeCompositingBounds;
m_graphicsLayer->setContentsOpaque(!hadSubpixelRounding && m_owningLayer.backgroundIsKnownToBeOpaqueInRect(localCompositingBounds));
}
LayoutRect clippingBox;
if (GraphicsLayer* clipLayer = clippingLayer()) {
clippingBox = clipBox(toRenderBox(renderer()));
clipLayer->setPosition(FloatPoint(clippingBox.location() - localCompositingBounds.location()));
clipLayer->setSize(clippingBox.size());
clipLayer->setOffsetFromRenderer(toFloatSize(clippingBox.location()));
}
if (m_maskLayer) {
m_maskLayer->setSize(m_graphicsLayer->size());
m_maskLayer->setPosition(FloatPoint());
m_maskLayer->setOffsetFromRenderer(m_graphicsLayer->offsetFromRenderer());
}
if (m_owningLayer.hasTransform()) {
FloatPoint3D transformOrigin = computeTransformOriginForPainting(toRenderBox(renderer()).borderBoxRect());
FloatPoint layerOffset = roundedForPainting(offsetFromParent, deviceScaleFactor);
FloatPoint3D anchor(enclosingRelativeCompositingBounds.width() ? ((layerOffset.x() - enclosingRelativeCompositingBounds.x()) + transformOrigin.x())
/ enclosingRelativeCompositingBounds.width() : 0.5, enclosingRelativeCompositingBounds.height() ? ((layerOffset.y() - enclosingRelativeCompositingBounds.y())
+ transformOrigin.y()) / enclosingRelativeCompositingBounds.height() : 0.5, transformOrigin.z());
if (m_contentsContainmentLayer)
m_contentsContainmentLayer->setAnchorPoint(anchor);
else
m_graphicsLayer->setAnchorPoint(anchor);
GraphicsLayer* clipLayer = clippingLayer();
if (style.hasPerspective()) {
TransformationMatrix t = owningLayer().perspectiveTransform();
if (clipLayer) {
clipLayer->setChildrenTransform(t);
m_graphicsLayer->setChildrenTransform(TransformationMatrix());
}
else
m_graphicsLayer->setChildrenTransform(t);
} else {
if (clipLayer)
clipLayer->setChildrenTransform(TransformationMatrix());
else
m_graphicsLayer->setChildrenTransform(TransformationMatrix());
}
} else {
m_graphicsLayer->setAnchorPoint(FloatPoint3D(0.5, 0.5, 0));
if (m_contentsContainmentLayer)
m_contentsContainmentLayer->setAnchorPoint(FloatPoint3D(0.5, 0.5, 0));
}
if (m_foregroundLayer) {
FloatPoint foregroundPosition;
FloatSize foregroundSize = contentsSize;
FloatSize foregroundOffset = m_graphicsLayer->offsetFromRenderer();
if (hasClippingLayer()) {
foregroundSize = FloatSize(clippingBox.size());
foregroundOffset = toFloatSize(clippingBox.location());
}
m_foregroundLayer->setPosition(foregroundPosition);
m_foregroundLayer->setSize(foregroundSize);
m_foregroundLayer->setOffsetFromRenderer(foregroundOffset);
}
if (m_backgroundLayer) {
FloatPoint backgroundPosition;
FloatSize backgroundSize = contentsSize;
if (backgroundLayerPaintsFixedRootBackground()) {
const FrameView& frameView = renderer().view().frameView();
backgroundPosition = toLayoutPoint(frameView.scrollOffsetForFixedPosition());
backgroundSize = frameView.visibleContentRect().size();
}
m_backgroundLayer->setPosition(backgroundPosition);
m_backgroundLayer->setSize(backgroundSize);
m_backgroundLayer->setOffsetFromRenderer(m_graphicsLayer->offsetFromRenderer());
}
if (m_owningLayer.reflectionLayer() && m_owningLayer.reflectionLayer()->isComposited()) {
RenderLayerBacking* reflectionBacking = m_owningLayer.reflectionLayer()->backing();
reflectionBacking->updateGeometry();
FloatRect layerBounds = compositedBounds();
FloatRect reflectionLayerBounds = reflectionBacking->compositedBounds();
reflectionBacking->graphicsLayer()->setReplicatedLayerPosition(FloatPoint(layerBounds.location() - reflectionLayerBounds.location()));
}
if (m_scrollingLayer) {
ASSERT(m_scrollingContentsLayer);
RenderBox& renderBox = toRenderBox(renderer());
LayoutRect paddingBox(renderBox.borderLeft(), renderBox.borderTop(), renderBox.width() - renderBox.borderLeft() - renderBox.borderRight(), renderBox.height() - renderBox.borderTop() - renderBox.borderBottom());
LayoutSize scrollOffset = m_owningLayer.scrollOffset();
m_scrollingLayer->setPosition(FloatPoint(paddingBox.location() - localCompositingBounds.location()));
IntSize pixelSnappedClientSize(renderBox.pixelSnappedClientWidth(), renderBox.pixelSnappedClientHeight());
m_scrollingLayer->setSize(pixelSnappedClientSize);
#if PLATFORM(IOS)
FloatSize oldScrollingLayerOffset = m_scrollingLayer->offsetFromRenderer();
m_scrollingLayer->setOffsetFromRenderer(FloatPoint() - paddingBox.location());
bool paddingBoxOffsetChanged = oldScrollingLayerOffset != m_scrollingLayer->offsetFromRenderer();
if (m_owningLayer.isInUserScroll()) {
m_scrollingLayer->syncBoundsOrigin(FloatPoint(scrollOffset.width(), scrollOffset.height()));
m_owningLayer.setRequiresScrollBoundsOriginUpdate(true);
} else {
m_scrollingLayer->setBoundsOrigin(FloatPoint(scrollOffset.width(), scrollOffset.height()));
m_owningLayer.setRequiresScrollBoundsOriginUpdate(false);
}
IntSize scrollSize(m_owningLayer.scrollWidth(), m_owningLayer.scrollHeight());
m_scrollingContentsLayer->setPosition(FloatPoint());
if (scrollSize != m_scrollingContentsLayer->size() || paddingBoxOffsetChanged)
m_scrollingContentsLayer->setNeedsDisplay();
m_scrollingContentsLayer->setSize(scrollSize);
m_scrollingContentsLayer->setOffsetFromRenderer(paddingBox.location() - IntPoint() - scrollOffset, GraphicsLayer::DontSetNeedsDisplay);
#else
m_scrollingContentsLayer->setPosition(FloatPoint(-scrollOffset.width(), -scrollOffset.height()));
FloatSize oldScrollingLayerOffset = m_scrollingLayer->offsetFromRenderer();
m_scrollingLayer->setOffsetFromRenderer(-toFloatSize(paddingBox.location()));
bool paddingBoxOffsetChanged = oldScrollingLayerOffset != m_scrollingLayer->offsetFromRenderer();
IntSize scrollSize(m_owningLayer.scrollWidth(), m_owningLayer.scrollHeight());
if (scrollSize != m_scrollingContentsLayer->size() || paddingBoxOffsetChanged)
m_scrollingContentsLayer->setNeedsDisplay();
LayoutSize scrollingContentsOffset = toLayoutSize(paddingBox.location() - scrollOffset);
if (scrollingContentsOffset != m_scrollingContentsLayer->offsetFromRenderer() || scrollSize != m_scrollingContentsLayer->size())
compositor().scrollingLayerDidChange(m_owningLayer);
m_scrollingContentsLayer->setSize(scrollSize);
m_scrollingContentsLayer->setOffsetFromRenderer(scrollingContentsOffset, GraphicsLayer::DontSetNeedsDisplay);
#endif
if (m_foregroundLayer) {
m_foregroundLayer->setSize(m_scrollingContentsLayer->size());
m_foregroundLayer->setOffsetFromRenderer(m_scrollingContentsLayer->offsetFromRenderer());
}
}
setRequiresOwnBackingStore(compositor().requiresOwnBackingStore(m_owningLayer, compAncestor, enclosingRelativeCompositingBounds, ancestorCompositingBounds));
updateAfterWidgetResize();
compositor().updateScrollCoordinatedStatus(m_owningLayer);
}
void RenderLayerBacking::updateAfterDescendants()
{
bool isSimpleContainer = false;
if (!m_owningLayer.isRootLayer()) {
bool didUpdateContentsRect = false;
isSimpleContainer = isSimpleContainerCompositingLayer();
updateDirectlyCompositedContents(isSimpleContainer, didUpdateContentsRect);
if (!didUpdateContentsRect && m_graphicsLayer->usesContentsLayer())
resetContentsRect();
}
updateDrawsContent(isSimpleContainer);
m_graphicsLayer->setContentsVisible(m_owningLayer.hasVisibleContent() || isPaintDestinationForDescendentLayers());
}
void RenderLayerBacking::adjustAncestorCompositingBoundsForFlowThread(LayoutRect& ancestorCompositingBounds, const RenderLayer* compositingAncestor) const
{
if (!m_owningLayer.isInsideFlowThread())
return;
RenderLayer* flowThreadLayer = m_owningLayer.isInsideOutOfFlowThread() ? m_owningLayer.stackingContainer() : nullptr;
if (flowThreadLayer && flowThreadLayer->isRenderFlowThread()) {
if (m_owningLayer.isFlowThreadCollectingGraphicsLayersUnderRegions()) {
IntPoint flowPosition;
flowThreadLayer->convertToPixelSnappedLayerCoords(compositingAncestor, flowPosition);
ancestorCompositingBounds.moveBy(flowPosition);
}
RenderFlowThread& flowThread = toRenderFlowThread(flowThreadLayer->renderer());
RenderNamedFlowFragment* parentRegion = flowThread.cachedRegionForCompositedLayer(m_owningLayer);
if (!parentRegion)
return;
IntPoint flowDelta;
m_owningLayer.convertToPixelSnappedLayerCoords(flowThreadLayer, flowDelta);
parentRegion->adjustRegionBoundsFromFlowThreadPortionRect(flowDelta, ancestorCompositingBounds);
RenderBoxModelObject& layerOwner = toRenderBoxModelObject(parentRegion->layerOwner());
RenderLayerBacking* layerOwnerBacking = layerOwner.layer()->backing();
if (!layerOwnerBacking)
return;
ancestorCompositingBounds.moveBy(roundedIntPoint(layerOwnerBacking->compositedBounds().location()));
ancestorCompositingBounds.move(-layerOwner.borderAndPaddingStart(), -layerOwner.borderAndPaddingBefore());
if (layerOwnerBacking->clippingLayer())
ancestorCompositingBounds.moveBy(roundedIntPoint(layerOwnerBacking->clippingLayer()->position()));
}
}
void RenderLayerBacking::updateDirectlyCompositedContents(bool isSimpleContainer, bool& didUpdateContentsRect)
{
if (!m_owningLayer.hasVisibleContent())
return;
updateDirectlyCompositedBackgroundColor(isSimpleContainer, didUpdateContentsRect);
updateDirectlyCompositedBackgroundImage(isSimpleContainer, didUpdateContentsRect);
}
void RenderLayerBacking::updateInternalHierarchy()
{
if (m_ancestorClippingLayer)
m_ancestorClippingLayer->removeAllChildren();
if (m_contentsContainmentLayer) {
m_contentsContainmentLayer->removeAllChildren();
if (m_ancestorClippingLayer)
m_ancestorClippingLayer->addChild(m_contentsContainmentLayer.get());
}
if (m_backgroundLayer)
m_contentsContainmentLayer->addChild(m_backgroundLayer.get());
if (m_contentsContainmentLayer)
m_contentsContainmentLayer->addChild(m_graphicsLayer.get());
else if (m_ancestorClippingLayer)
m_ancestorClippingLayer->addChild(m_graphicsLayer.get());
if (m_childContainmentLayer) {
m_childContainmentLayer->removeFromParent();
m_graphicsLayer->addChild(m_childContainmentLayer.get());
}
if (m_scrollingLayer) {
GraphicsLayer* superlayer = m_childContainmentLayer ? m_childContainmentLayer.get() : m_graphicsLayer.get();
m_scrollingLayer->removeFromParent();
superlayer->addChild(m_scrollingLayer.get());
}
if (m_layerForHorizontalScrollbar) {
m_layerForHorizontalScrollbar->removeFromParent();
m_graphicsLayer->addChild(m_layerForHorizontalScrollbar.get());
}
if (m_layerForVerticalScrollbar) {
m_layerForVerticalScrollbar->removeFromParent();
m_graphicsLayer->addChild(m_layerForVerticalScrollbar.get());
}
if (m_layerForScrollCorner) {
m_layerForScrollCorner->removeFromParent();
m_graphicsLayer->addChild(m_layerForScrollCorner.get());
}
}
void RenderLayerBacking::resetContentsRect()
{
m_graphicsLayer->setContentsRect(pixelSnappedIntRect(contentsBox()));
LayoutRect contentsClippingRect;
if (renderer().isBox())
contentsClippingRect = toRenderBox(renderer()).contentBoxRect();
contentsClippingRect.move(contentOffsetInCompostingLayer());
m_graphicsLayer->setContentsClippingRect(pixelSnappedIntRect(contentsClippingRect));
m_graphicsLayer->setContentsTileSize(IntSize());
m_graphicsLayer->setContentsTilePhase(IntPoint());
}
void RenderLayerBacking::updateDrawsContent()
{
updateDrawsContent(isSimpleContainerCompositingLayer());
}
void RenderLayerBacking::updateDrawsContent(bool isSimpleContainer)
{
if (m_scrollingLayer) {
bool hasNonScrollingPaintedContent = m_owningLayer.hasVisibleContent() && m_owningLayer.hasBoxDecorationsOrBackground();
m_graphicsLayer->setDrawsContent(hasNonScrollingPaintedContent);
bool hasScrollingPaintedContent = m_owningLayer.hasVisibleContent() && (renderer().hasBackground() || paintsChildren());
m_scrollingContentsLayer->setDrawsContent(hasScrollingPaintedContent);
return;
}
bool hasPaintedContent = containsPaintedContent(isSimpleContainer);
m_graphicsLayer->setDrawsContent(hasPaintedContent);
if (m_foregroundLayer)
m_foregroundLayer->setDrawsContent(hasPaintedContent);
if (m_backgroundLayer)
m_backgroundLayer->setDrawsContent(hasPaintedContent);
}
bool RenderLayerBacking::updateAncestorClippingLayer(bool needsAncestorClip)
{
bool layersChanged = false;
if (needsAncestorClip) {
if (!m_ancestorClippingLayer) {
m_ancestorClippingLayer = createGraphicsLayer("Ancestor clipping Layer");
m_ancestorClippingLayer->setMasksToBounds(true);
layersChanged = true;
}
} else if (hasAncestorClippingLayer()) {
willDestroyLayer(m_ancestorClippingLayer.get());
m_ancestorClippingLayer->removeFromParent();
m_ancestorClippingLayer = nullptr;
layersChanged = true;
}
return layersChanged;
}
bool RenderLayerBacking::updateDescendantClippingLayer(bool needsDescendantClip)
{
bool layersChanged = false;
if (needsDescendantClip) {
if (!m_childContainmentLayer && !m_usingTiledCacheLayer) {
m_childContainmentLayer = createGraphicsLayer("Child clipping Layer");
m_childContainmentLayer->setMasksToBounds(true);
layersChanged = true;
}
} else if (hasClippingLayer()) {
willDestroyLayer(m_childContainmentLayer.get());
m_childContainmentLayer->removeFromParent();
m_childContainmentLayer = nullptr;
layersChanged = true;
}
return layersChanged;
}
void RenderLayerBacking::setBackgroundLayerPaintsFixedRootBackground(bool backgroundLayerPaintsFixedRootBackground)
{
m_backgroundLayerPaintsFixedRootBackground = backgroundLayerPaintsFixedRootBackground;
}
bool RenderLayerBacking::requiresHorizontalScrollbarLayer() const
{
if (!m_owningLayer.hasOverlayScrollbars() && !m_owningLayer.needsCompositedScrolling())
return false;
return m_owningLayer.horizontalScrollbar();
}
bool RenderLayerBacking::requiresVerticalScrollbarLayer() const
{
if (!m_owningLayer.hasOverlayScrollbars() && !m_owningLayer.needsCompositedScrolling())
return false;
return m_owningLayer.verticalScrollbar();
}
bool RenderLayerBacking::requiresScrollCornerLayer() const
{
if (!m_owningLayer.hasOverlayScrollbars() && !m_owningLayer.needsCompositedScrolling())
return false;
return !m_owningLayer.scrollCornerAndResizerRect().isEmpty();
}
bool RenderLayerBacking::updateOverflowControlsLayers(bool needsHorizontalScrollbarLayer, bool needsVerticalScrollbarLayer, bool needsScrollCornerLayer)
{
bool horizontalScrollbarLayerChanged = false;
if (needsHorizontalScrollbarLayer) {
if (!m_layerForHorizontalScrollbar) {
m_layerForHorizontalScrollbar = createGraphicsLayer("horizontal scrollbar");
horizontalScrollbarLayerChanged = true;
}
} else if (m_layerForHorizontalScrollbar) {
willDestroyLayer(m_layerForHorizontalScrollbar.get());
m_layerForHorizontalScrollbar = nullptr;
horizontalScrollbarLayerChanged = true;
}
bool verticalScrollbarLayerChanged = false;
if (needsVerticalScrollbarLayer) {
if (!m_layerForVerticalScrollbar) {
m_layerForVerticalScrollbar = createGraphicsLayer("vertical scrollbar");
verticalScrollbarLayerChanged = true;
}
} else if (m_layerForVerticalScrollbar) {
willDestroyLayer(m_layerForVerticalScrollbar.get());
m_layerForVerticalScrollbar = nullptr;
verticalScrollbarLayerChanged = true;
}
bool scrollCornerLayerChanged = false;
if (needsScrollCornerLayer) {
if (!m_layerForScrollCorner) {
m_layerForScrollCorner = createGraphicsLayer("scroll corner");
scrollCornerLayerChanged = true;
}
} else if (m_layerForScrollCorner) {
willDestroyLayer(m_layerForScrollCorner.get());
m_layerForScrollCorner = nullptr;
scrollCornerLayerChanged = true;
}
if (ScrollingCoordinator* scrollingCoordinator = scrollingCoordinatorFromLayer(m_owningLayer)) {
if (horizontalScrollbarLayerChanged)
scrollingCoordinator->scrollableAreaScrollbarLayerDidChange(&m_owningLayer, HorizontalScrollbar);
if (verticalScrollbarLayerChanged)
scrollingCoordinator->scrollableAreaScrollbarLayerDidChange(&m_owningLayer, VerticalScrollbar);
}
return horizontalScrollbarLayerChanged || verticalScrollbarLayerChanged || scrollCornerLayerChanged;
}
void RenderLayerBacking::positionOverflowControlsLayers()
{
if (!m_owningLayer.hasScrollbars())
return;
const IntRect borderBox = toRenderBox(renderer()).pixelSnappedBorderBoxRect();
FloatSize offsetFromRenderer = m_graphicsLayer->offsetFromRenderer();
if (GraphicsLayer* layer = layerForHorizontalScrollbar()) {
IntRect hBarRect = m_owningLayer.rectForHorizontalScrollbar(borderBox);
layer->setPosition(hBarRect.location() - offsetFromRenderer);
layer->setSize(hBarRect.size());
if (layer->usesContentsLayer()) {
IntRect barRect = IntRect(IntPoint(), hBarRect.size());
layer->setContentsRect(barRect);
layer->setContentsClippingRect(barRect);
}
layer->setDrawsContent(m_owningLayer.horizontalScrollbar() && !layer->usesContentsLayer());
}
if (GraphicsLayer* layer = layerForVerticalScrollbar()) {
IntRect vBarRect = m_owningLayer.rectForVerticalScrollbar(borderBox);
layer->setPosition(vBarRect.location() - offsetFromRenderer);
layer->setSize(vBarRect.size());
if (layer->usesContentsLayer()) {
IntRect barRect = IntRect(IntPoint(), vBarRect.size());
layer->setContentsRect(barRect);
layer->setContentsClippingRect(barRect);
}
layer->setDrawsContent(m_owningLayer.verticalScrollbar() && !layer->usesContentsLayer());
}
if (GraphicsLayer* layer = layerForScrollCorner()) {
const LayoutRect& scrollCornerAndResizer = m_owningLayer.scrollCornerAndResizerRect();
layer->setPosition(scrollCornerAndResizer.location() - offsetFromRenderer);
layer->setSize(scrollCornerAndResizer.size());
layer->setDrawsContent(!scrollCornerAndResizer.isEmpty());
}
}
bool RenderLayerBacking::hasUnpositionedOverflowControlsLayers() const
{
if (GraphicsLayer* layer = layerForHorizontalScrollbar())
if (!layer->drawsContent())
return true;
if (GraphicsLayer* layer = layerForVerticalScrollbar())
if (!layer->drawsContent())
return true;
if (GraphicsLayer* layer = layerForScrollCorner())
if (!layer->drawsContent())
return true;
return false;
}
bool RenderLayerBacking::updateForegroundLayer(bool needsForegroundLayer)
{
bool layerChanged = false;
if (needsForegroundLayer) {
if (!m_foregroundLayer) {
String layerName;
#ifndef NDEBUG
layerName = m_owningLayer.name() + " (foreground)";
#endif
m_foregroundLayer = createGraphicsLayer(layerName);
m_foregroundLayer->setDrawsContent(true);
m_foregroundLayer->setPaintingPhase(GraphicsLayerPaintForeground);
layerChanged = true;
}
} else if (m_foregroundLayer) {
willDestroyLayer(m_foregroundLayer.get());
m_foregroundLayer->removeFromParent();
m_foregroundLayer = nullptr;
layerChanged = true;
}
if (layerChanged) {
m_graphicsLayer->setNeedsDisplay();
m_graphicsLayer->setPaintingPhase(paintingPhaseForPrimaryLayer());
}
return layerChanged;
}
bool RenderLayerBacking::updateBackgroundLayer(bool needsBackgroundLayer)
{
bool layerChanged = false;
if (needsBackgroundLayer) {
if (!m_backgroundLayer) {
String layerName;
#ifndef NDEBUG
layerName = m_owningLayer.name() + " (background)";
#endif
m_backgroundLayer = createGraphicsLayer(layerName);
m_backgroundLayer->setDrawsContent(true);
m_backgroundLayer->setAnchorPoint(FloatPoint3D());
m_backgroundLayer->setPaintingPhase(GraphicsLayerPaintBackground);
layerChanged = true;
}
if (!m_contentsContainmentLayer) {
String layerName;
#ifndef NDEBUG
layerName = m_owningLayer.name() + " (contents containment)";
#endif
m_contentsContainmentLayer = createGraphicsLayer(layerName);
m_contentsContainmentLayer->setAppliesPageScale(true);
m_graphicsLayer->setAppliesPageScale(false);
layerChanged = true;
}
} else {
if (m_backgroundLayer) {
willDestroyLayer(m_backgroundLayer.get());
m_backgroundLayer->removeFromParent();
m_backgroundLayer = nullptr;
layerChanged = true;
}
if (m_contentsContainmentLayer) {
willDestroyLayer(m_contentsContainmentLayer.get());
m_contentsContainmentLayer->removeFromParent();
m_contentsContainmentLayer = nullptr;
layerChanged = true;
m_graphicsLayer->setAppliesPageScale(true);
}
}
if (layerChanged) {
m_graphicsLayer->setNeedsDisplay();
compositor().fixedRootBackgroundLayerChanged();
}
return layerChanged;
}
void RenderLayerBacking::updateMaskLayer(bool needsMaskLayer)
{
bool layerChanged = false;
if (needsMaskLayer) {
if (!m_maskLayer) {
m_maskLayer = createGraphicsLayer("Mask");
m_maskLayer->setDrawsContent(true);
m_maskLayer->setPaintingPhase(GraphicsLayerPaintMask);
layerChanged = true;
m_graphicsLayer->setMaskLayer(m_maskLayer.get());
}
} else if (m_maskLayer) {
m_graphicsLayer->setMaskLayer(nullptr);
willDestroyLayer(m_maskLayer.get());
m_maskLayer = nullptr;
layerChanged = true;
}
if (layerChanged)
m_graphicsLayer->setPaintingPhase(paintingPhaseForPrimaryLayer());
}
bool RenderLayerBacking::updateScrollingLayers(bool needsScrollingLayers)
{
if (needsScrollingLayers == !!m_scrollingLayer)
return false;
if (!m_scrollingLayer) {
m_scrollingLayer = createGraphicsLayer("Scrolling container");
m_scrollingLayer->setDrawsContent(false);
m_scrollingLayer->setMasksToBounds(true);
#if PLATFORM(IOS)
m_scrollingLayer->setCustomBehavior(GraphicsLayer::CustomScrollingBehavior);
#endif
m_scrollingContentsLayer = createGraphicsLayer("Scrolled Contents");
m_scrollingContentsLayer->setDrawsContent(true);
#if PLATFORM(IOS)
m_scrollingContentsLayer->setCustomBehavior(GraphicsLayer::CustomScrolledContentsBehavior);
#endif
GraphicsLayerPaintingPhase paintPhase = GraphicsLayerPaintOverflowContents | GraphicsLayerPaintCompositedScroll;
if (!m_foregroundLayer)
paintPhase |= GraphicsLayerPaintForeground;
m_scrollingContentsLayer->setPaintingPhase(paintPhase);
m_scrollingLayer->addChild(m_scrollingContentsLayer.get());
} else {
compositor().willRemoveScrollingLayerWithBacking(m_owningLayer, *this);
willDestroyLayer(m_scrollingLayer.get());
willDestroyLayer(m_scrollingContentsLayer.get());
m_scrollingLayer = nullptr;
m_scrollingContentsLayer = nullptr;
}
m_graphicsLayer->setPaintingPhase(paintingPhaseForPrimaryLayer());
m_graphicsLayer->setNeedsDisplay();
if (m_scrollingLayer)
compositor().didAddScrollingLayer(m_owningLayer);
return true;
}
void RenderLayerBacking::detachFromScrollingCoordinator()
{
if (!m_scrollingNodeID && !m_viewportConstrainedNodeID)
return;
ScrollingCoordinator* scrollingCoordinator = scrollingCoordinatorFromLayer(m_owningLayer);
if (!scrollingCoordinator)
return;
if (m_scrollingNodeID)
scrollingCoordinator->detachFromStateTree(m_scrollingNodeID);
if (m_viewportConstrainedNodeID)
scrollingCoordinator->detachFromStateTree(m_viewportConstrainedNodeID);
m_scrollingNodeID = 0;
m_viewportConstrainedNodeID = 0;
}
GraphicsLayerPaintingPhase RenderLayerBacking::paintingPhaseForPrimaryLayer() const
{
unsigned phase = 0;
if (!m_backgroundLayer)
phase |= GraphicsLayerPaintBackground;
if (!m_foregroundLayer)
phase |= GraphicsLayerPaintForeground;
if (!m_maskLayer)
phase |= GraphicsLayerPaintMask;
if (m_scrollingContentsLayer) {
phase &= ~GraphicsLayerPaintForeground;
phase |= GraphicsLayerPaintCompositedScroll;
}
return static_cast<GraphicsLayerPaintingPhase>(phase);
}
float RenderLayerBacking::compositingOpacity(float rendererOpacity) const
{
float finalOpacity = rendererOpacity;
for (RenderLayer* curr = m_owningLayer.parent(); curr; curr = curr->parent()) {
if (!curr->isStackingContainer())
continue;
if (curr->isComposited())
break;
finalOpacity *= curr->renderer().opacity();
}
return finalOpacity;
}
static inline bool hasBoxDecorations(const RenderStyle& style)
{
return style.hasBorder() || style.hasBorderRadius() || style.hasOutline() || style.hasAppearance() || style.boxShadow() || style.hasFilter();
}
static bool canCreateTiledImage(const RenderStyle& style)
{
const FillLayer* fillLayer = style.backgroundLayers();
if (fillLayer->next())
return false;
if (!fillLayer->imagesAreLoaded())
return false;
if (fillLayer->attachment() != ScrollBackgroundAttachment)
return false;
Color color = style.visitedDependentColor(CSSPropertyBackgroundColor);
if (color.isValid() && color.alpha())
return false;
StyleImage* styleImage = fillLayer->image();
if (!styleImage->isCachedImage())
return false;
Image* image = styleImage->cachedImage()->image();
if (!image->isBitmapImage())
return false;
return true;
}
static bool hasBoxDecorationsOrBackgroundImage(const RenderStyle& style)
{
if (hasBoxDecorations(style))
return true;
if (!style.hasBackgroundImage())
return false;
return !GraphicsLayer::supportsContentsTiling() || !canCreateTiledImage(style);
}
static inline bool hasPerspectiveOrPreserves3D(const RenderStyle& style)
{
return style.hasPerspective() || style.preserves3D();
}
Color RenderLayerBacking::rendererBackgroundColor() const
{
const auto& backgroundRenderer = renderer().isRoot() ? renderer().rendererForRootBackground() : renderer();
return backgroundRenderer.style().visitedDependentColor(CSSPropertyBackgroundColor);
}
void RenderLayerBacking::updateDirectlyCompositedBackgroundColor(bool isSimpleContainer, bool& didUpdateContentsRect)
{
if (!isSimpleContainer) {
m_graphicsLayer->setContentsToSolidColor(Color());
return;
}
Color backgroundColor = rendererBackgroundColor();
m_graphicsLayer->setContentsToSolidColor(backgroundColor);
FloatRect contentsRect = backgroundBoxForPainting();
m_graphicsLayer->setContentsRect(contentsRect);
m_graphicsLayer->setContentsClippingRect(contentsRect);
didUpdateContentsRect = true;
}
void RenderLayerBacking::updateDirectlyCompositedBackgroundImage(bool isSimpleContainer, bool& didUpdateContentsRect)
{
if (!GraphicsLayer::supportsContentsTiling())
return;
if (isDirectlyCompositedImage())
return;
const RenderStyle& style = renderer().style();
if (!isSimpleContainer || !style.hasBackgroundImage()) {
m_graphicsLayer->setContentsToImage(0);
return;
}
FloatRect destRect = backgroundBoxForPainting();
FloatPoint phase;
FloatSize tileSize;
RefPtr<Image> image = style.backgroundLayers()->image()->cachedImage()->image();
toRenderBox(renderer()).getGeometryForBackgroundImage(&m_owningLayer.renderer(), destRect, phase, tileSize);
m_graphicsLayer->setContentsTileSize(tileSize);
m_graphicsLayer->setContentsTilePhase(phase);
m_graphicsLayer->setContentsRect(destRect);
m_graphicsLayer->setContentsClippingRect(destRect);
m_graphicsLayer->setContentsToImage(image.get());
didUpdateContentsRect = true;
}
void RenderLayerBacking::updateRootLayerConfiguration()
{
if (!m_usingTiledCacheLayer)
return;
Color backgroundColor;
bool viewIsTransparent = compositor().viewHasTransparentBackground(&backgroundColor);
if (m_backgroundLayerPaintsFixedRootBackground && m_backgroundLayer) {
m_backgroundLayer->setBackgroundColor(backgroundColor);
m_backgroundLayer->setContentsOpaque(!viewIsTransparent);
m_graphicsLayer->setBackgroundColor(Color());
m_graphicsLayer->setContentsOpaque(false);
} else {
m_graphicsLayer->setBackgroundColor(backgroundColor);
m_graphicsLayer->setContentsOpaque(!viewIsTransparent);
}
}
static bool supportsDirectBoxDecorationsComposition(const RenderLayerModelObject& renderer)
{
if (!GraphicsLayer::supportsBackgroundColorContent())
return false;
const RenderStyle& style = renderer.style();
if (renderer.hasClip())
return false;
if (hasBoxDecorationsOrBackgroundImage(style))
return false;
if (hasPerspectiveOrPreserves3D(style))
return false;
if (style.backgroundComposite() != CompositeSourceOver)
return false;
if (style.backgroundClip() == TextFillBox)
return false;
return true;
}
bool RenderLayerBacking::paintsBoxDecorations() const
{
if (!m_owningLayer.hasVisibleBoxDecorations())
return false;
if (!supportsDirectBoxDecorationsComposition(renderer()))
return true;
return false;
}
bool RenderLayerBacking::paintsChildren() const
{
if (m_owningLayer.hasVisibleContent() && m_owningLayer.hasNonEmptyChildRenderers())
return true;
if (isPaintDestinationForDescendentLayers())
return true;
return false;
}
static bool isRestartedPlugin(RenderObject* renderer)
{
if (!renderer->isEmbeddedObject())
return false;
Element* element = toElement(renderer->node());
if (!element || !element->isPluginElement())
return false;
return toHTMLPlugInElement(element)->isRestartedPlugin();
}
static bool isCompositedPlugin(RenderObject* renderer)
{
return renderer->isEmbeddedObject() && toRenderEmbeddedObject(renderer)->allowsAcceleratedCompositing();
}
bool RenderLayerBacking::isSimpleContainerCompositingLayer() const
{
if (renderer().isRenderReplaced() && (!isCompositedPlugin(&renderer()) || isRestartedPlugin(&renderer())))
return false;
if (paintsBoxDecorations() || paintsChildren())
return false;
if (renderer().isRenderNamedFlowFragmentContainer())
return false;
if (renderer().isRenderView()) {
RenderObject* rootObject = renderer().document().documentElement() ? renderer().document().documentElement()->renderer() : 0;
if (!rootObject)
return false;
if (hasBoxDecorationsOrBackgroundImage(rootObject->style()))
return false;
HTMLElement* body = renderer().document().body();
RenderObject* bodyObject = (body && body->hasTagName(bodyTag)) ? body->renderer() : 0;
if (!bodyObject)
return false;
if (hasBoxDecorationsOrBackgroundImage(bodyObject->style()))
return false;
}
return true;
}
static bool compositedWithOwnBackingStore(const RenderLayer* layer)
{
return layer->isComposited() && !layer->backing()->paintsIntoCompositedAncestor();
}
static bool descendentLayerPaintsIntoAncestor(RenderLayer& parent)
{
parent.updateLayerListsIfNeeded();
#if !ASSERT_DISABLED
LayerListMutationDetector mutationChecker(&parent);
#endif
if (Vector<RenderLayer*>* normalFlowList = parent.normalFlowList()) {
size_t listSize = normalFlowList->size();
for (size_t i = 0; i < listSize; ++i) {
RenderLayer* curLayer = normalFlowList->at(i);
if (!compositedWithOwnBackingStore(curLayer)
&& (curLayer->isVisuallyNonEmpty() || descendentLayerPaintsIntoAncestor(*curLayer)))
return true;
}
}
if (parent.isStackingContainer()) {
if (!parent.hasVisibleDescendant())
return false;
if (Vector<RenderLayer*>* negZOrderList = parent.negZOrderList()) {
size_t listSize = negZOrderList->size();
for (size_t i = 0; i < listSize; ++i) {
RenderLayer* curLayer = negZOrderList->at(i);
if (!compositedWithOwnBackingStore(curLayer)
&& (curLayer->isVisuallyNonEmpty() || descendentLayerPaintsIntoAncestor(*curLayer)))
return true;
}
}
if (Vector<RenderLayer*>* posZOrderList = parent.posZOrderList()) {
size_t listSize = posZOrderList->size();
for (size_t i = 0; i < listSize; ++i) {
RenderLayer* curLayer = posZOrderList->at(i);
if (!compositedWithOwnBackingStore(curLayer)
&& (curLayer->isVisuallyNonEmpty() || descendentLayerPaintsIntoAncestor(*curLayer)))
return true;
}
}
}
return false;
}
bool RenderLayerBacking::isPaintDestinationForDescendentLayers() const
{
return descendentLayerPaintsIntoAncestor(m_owningLayer);
}
bool RenderLayerBacking::containsPaintedContent(bool isSimpleContainer) const
{
if (isSimpleContainer || paintsIntoWindow() || paintsIntoCompositedAncestor() || m_artificiallyInflatedBounds || m_owningLayer.isReflection())
return false;
if (isDirectlyCompositedImage())
return false;
#if ENABLE(VIDEO)
if (renderer().isVideo() && toRenderVideo(renderer()).shouldDisplayVideo())
return m_owningLayer.hasBoxDecorationsOrBackground();
#endif
#if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
if (renderer().isCanvas() && canvasCompositingStrategy(renderer()) == CanvasAsLayerContents)
return m_owningLayer.hasBoxDecorationsOrBackground();
#endif
return true;
}
bool RenderLayerBacking::isDirectlyCompositedImage() const
{
if (!renderer().isRenderImage() || renderer().isMedia() || m_owningLayer.hasBoxDecorationsOrBackground() || renderer().hasClip())
return false;
RenderImage& imageRenderer = toRenderImage(renderer());
if (CachedImage* cachedImage = imageRenderer.cachedImage()) {
if (!cachedImage->hasImage())
return false;
Image* image = cachedImage->imageForRenderer(&imageRenderer);
if (!image->isBitmapImage())
return false;
if (image->orientationForCurrentFrame() != DefaultImageOrientation)
return false;
return m_graphicsLayer->shouldDirectlyCompositeImage(image);
}
return false;
}
void RenderLayerBacking::contentChanged(ContentChangeType changeType)
{
if ((changeType == ImageChanged) && isDirectlyCompositedImage()) {
updateImageContents();
return;
}
if ((changeType == BackgroundImageChanged) && canCreateTiledImage(renderer().style()))
updateGeometry();
if ((changeType == MaskImageChanged) && m_maskLayer) {
updateAfterLayout(CompositingChildrenOnly | IsUpdateRoot);
}
#if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
if ((changeType == CanvasChanged || changeType == CanvasPixelsChanged) && renderer().isCanvas() && canvasCompositingStrategy(renderer()) == CanvasAsLayerContents) {
m_graphicsLayer->setContentsNeedsDisplay();
return;
}
#endif
}
void RenderLayerBacking::updateImageContents()
{
ASSERT(renderer().isRenderImage());
RenderImage& imageRenderer = toRenderImage(renderer());
CachedImage* cachedImage = imageRenderer.cachedImage();
if (!cachedImage)
return;
Image* image = cachedImage->imageForRenderer(&imageRenderer);
if (!image)
return;
if (!cachedImage->isLoaded())
return;
m_graphicsLayer->setContentsRect(pixelSnappedIntRect(contentsBox()));
LayoutRect contentsClippingRect = imageRenderer.contentBoxRect();
contentsClippingRect.move(contentOffsetInCompostingLayer());
m_graphicsLayer->setContentsClippingRect(pixelSnappedIntRect(contentsClippingRect));
m_graphicsLayer->setContentsToImage(image);
bool isSimpleContainer = false;
updateDrawsContent(isSimpleContainer);
image->startAnimation();
}
FloatPoint3D RenderLayerBacking::computeTransformOriginForPainting(const LayoutRect& borderBox) const
{
const RenderStyle& style = renderer().style();
float deviceScaleFactor = renderer().document().deviceScaleFactor();
FloatPoint3D origin;
origin.setX(roundToDevicePixel(floatValueForLength(style.transformOriginX(), borderBox.width()), deviceScaleFactor));
origin.setY(roundToDevicePixel(floatValueForLength(style.transformOriginY(), borderBox.height()), deviceScaleFactor));
origin.setZ(style.transformOriginZ());
return origin;
}
LayoutSize RenderLayerBacking::contentOffsetInCompostingLayer() const
{
return LayoutSize(-m_compositedBounds.x(), -m_compositedBounds.y()) + m_devicePixelFractionFromRenderer;
}
LayoutRect RenderLayerBacking::contentsBox() const
{
if (!renderer().isBox())
return LayoutRect();
RenderBox& renderBox = toRenderBox(renderer());
LayoutRect contentsRect;
#if ENABLE(VIDEO)
if (renderBox.isVideo())
contentsRect = toRenderVideo(renderBox).videoBox();
else
#endif
if (renderBox.isRenderReplaced()) {
RenderReplaced& renderReplaced = *toRenderReplaced(&renderBox);
contentsRect = renderReplaced.replacedContentRect(renderBox.intrinsicSize());
} else
contentsRect = renderBox.contentBoxRect();
contentsRect.move(contentOffsetInCompostingLayer());
return contentsRect;
}
static LayoutRect backgroundRectForBox(const RenderBox& box)
{
switch (box.style().backgroundClip()) {
case BorderFillBox:
return box.borderBoxRect();
case PaddingFillBox:
return box.paddingBoxRect();
case ContentFillBox:
return box.contentBoxRect();
case TextFillBox:
break;
}
ASSERT_NOT_REACHED();
return LayoutRect();
}
FloatRect RenderLayerBacking::backgroundBoxForPainting() const
{
if (!renderer().isBox())
return FloatRect();
LayoutRect backgroundBox = backgroundRectForBox(toRenderBox(renderer()));
backgroundBox.move(contentOffsetInCompostingLayer());
return pixelSnappedForPainting(backgroundBox, deviceScaleFactor());
}
GraphicsLayer* RenderLayerBacking::parentForSublayers() const
{
if (m_scrollingContentsLayer)
return m_scrollingContentsLayer.get();
return m_childContainmentLayer ? m_childContainmentLayer.get() : m_graphicsLayer.get();
}
GraphicsLayer* RenderLayerBacking::childForSuperlayers() const
{
if (m_ancestorClippingLayer)
return m_ancestorClippingLayer.get();
if (m_contentsContainmentLayer)
return m_contentsContainmentLayer.get();
return m_graphicsLayer.get();
}
bool RenderLayerBacking::paintsIntoWindow() const
{
if (m_usingTiledCacheLayer)
return false;
if (m_owningLayer.isRootLayer()) {
#if PLATFORM(IOS) || USE(COORDINATED_GRAPHICS)
if (compositor().inForcedCompositingMode())
return false;
#endif
return compositor().rootLayerAttachment() != RenderLayerCompositor::RootLayerAttachedViaEnclosingFrame;
}
return false;
}
void RenderLayerBacking::setRequiresOwnBackingStore(bool requiresOwnBacking)
{
if (requiresOwnBacking == m_requiresOwnBackingStore)
return;
m_requiresOwnBackingStore = requiresOwnBacking;
m_owningLayer.clearClipRectsIncludingDescendants(PaintingClipRects);
m_owningLayer.computeRepaintRectsIncludingDescendants();
compositor().repaintInCompositedAncestor(m_owningLayer, compositedBounds());
}
void RenderLayerBacking::setContentsNeedDisplay(GraphicsLayer::ShouldClipToLayer shouldClip)
{
ASSERT(!paintsIntoCompositedAncestor());
FrameView& frameView = owningLayer().renderer().view().frameView();
if (m_isMainFrameRenderViewLayer && frameView.isTrackingRepaints())
frameView.addTrackedRepaintRect(owningLayer().absoluteBoundingBoxForPainting());
if (m_graphicsLayer && m_graphicsLayer->drawsContent()) {
TiledBacking* tiledBacking = this->tiledBacking();
FloatRect rectToRepaint = tiledBacking ? tiledBacking->bounds() : FloatRect(FloatPoint(0, 0), m_graphicsLayer->size());
m_graphicsLayer->setNeedsDisplayInRect(rectToRepaint, shouldClip);
}
if (m_foregroundLayer && m_foregroundLayer->drawsContent())
m_foregroundLayer->setNeedsDisplay();
if (m_backgroundLayer && m_backgroundLayer->drawsContent())
m_backgroundLayer->setNeedsDisplay();
if (m_maskLayer && m_maskLayer->drawsContent())
m_maskLayer->setNeedsDisplay();
if (m_scrollingContentsLayer && m_scrollingContentsLayer->drawsContent())
m_scrollingContentsLayer->setNeedsDisplay();
}
void RenderLayerBacking::setContentsNeedDisplayInRect(const LayoutRect& r, GraphicsLayer::ShouldClipToLayer shouldClip)
{
ASSERT(!paintsIntoCompositedAncestor());
FloatRect pixelSnappedRectForPainting = pixelSnappedForPainting(r, deviceScaleFactor());
FrameView& frameView = owningLayer().renderer().view().frameView();
if (m_isMainFrameRenderViewLayer && frameView.isTrackingRepaints())
frameView.addTrackedRepaintRect(pixelSnappedRectForPainting);
if (m_graphicsLayer && m_graphicsLayer->drawsContent()) {
FloatRect layerDirtyRect = pixelSnappedRectForPainting;
layerDirtyRect.move(-m_graphicsLayer->offsetFromRenderer() + m_devicePixelFractionFromRenderer);
m_graphicsLayer->setNeedsDisplayInRect(layerDirtyRect, shouldClip);
}
if (m_foregroundLayer && m_foregroundLayer->drawsContent()) {
FloatRect layerDirtyRect = pixelSnappedRectForPainting;
layerDirtyRect.move(-m_foregroundLayer->offsetFromRenderer() + m_devicePixelFractionFromRenderer);
m_foregroundLayer->setNeedsDisplayInRect(layerDirtyRect, shouldClip);
}
if (m_backgroundLayer && m_backgroundLayer->drawsContent()) {
FloatRect layerDirtyRect = pixelSnappedRectForPainting;
layerDirtyRect.move(-m_backgroundLayer->offsetFromRenderer() + m_devicePixelFractionFromRenderer);
m_backgroundLayer->setNeedsDisplayInRect(layerDirtyRect, shouldClip);
}
if (m_maskLayer && m_maskLayer->drawsContent()) {
FloatRect layerDirtyRect = pixelSnappedRectForPainting;
layerDirtyRect.move(-m_maskLayer->offsetFromRenderer() + m_devicePixelFractionFromRenderer);
m_maskLayer->setNeedsDisplayInRect(layerDirtyRect, shouldClip);
}
if (m_scrollingContentsLayer && m_scrollingContentsLayer->drawsContent()) {
FloatRect layerDirtyRect = pixelSnappedRectForPainting;
layerDirtyRect.move(-m_scrollingContentsLayer->offsetFromRenderer() + m_devicePixelFractionFromRenderer);
#if PLATFORM(IOS)
layerDirtyRect.move(-m_owningLayer.scrollOffset() + m_devicePixelFractionFromRenderer);
#endif
m_scrollingContentsLayer->setNeedsDisplayInRect(layerDirtyRect, shouldClip);
}
}
void RenderLayerBacking::paintIntoLayer(const GraphicsLayer* graphicsLayer, GraphicsContext* context,
const IntRect& paintDirtyRect, PaintBehavior paintBehavior, GraphicsLayerPaintingPhase paintingPhase)
{
if (paintsIntoWindow() || paintsIntoCompositedAncestor()) {
#if !PLATFORM(IOS)
ASSERT_NOT_REACHED();
#endif
return;
}
FontCachePurgePreventer fontCachePurgePreventer;
RenderLayer::PaintLayerFlags paintFlags = 0;
if (paintingPhase & GraphicsLayerPaintBackground)
paintFlags |= RenderLayer::PaintLayerPaintingCompositingBackgroundPhase;
if (paintingPhase & GraphicsLayerPaintForeground)
paintFlags |= RenderLayer::PaintLayerPaintingCompositingForegroundPhase;
if (paintingPhase & GraphicsLayerPaintMask)
paintFlags |= RenderLayer::PaintLayerPaintingCompositingMaskPhase;
if (paintingPhase & GraphicsLayerPaintOverflowContents)
paintFlags |= RenderLayer::PaintLayerPaintingOverflowContents;
if (paintingPhase & GraphicsLayerPaintCompositedScroll)
paintFlags |= RenderLayer::PaintLayerPaintingCompositingScrollingPhase;
if (graphicsLayer == m_backgroundLayer.get())
paintFlags |= (RenderLayer::PaintLayerPaintingRootBackgroundOnly | RenderLayer::PaintLayerPaintingCompositingForegroundPhase); else if (compositor().fixedRootBackgroundLayer())
paintFlags |= RenderLayer::PaintLayerPaintingSkipRootBackground;
#ifndef NDEBUG
RenderElement::SetLayoutNeededForbiddenScope forbidSetNeedsLayout(&m_owningLayer.renderer());
#endif
FrameView::PaintingState paintingState;
if (m_owningLayer.isRootLayer())
m_owningLayer.renderer().view().frameView().willPaintContents(context, paintDirtyRect, paintingState);
RenderLayer::LayerPaintingInfo paintingInfo(&m_owningLayer, paintDirtyRect, paintBehavior, m_devicePixelFractionFromRenderer);
m_owningLayer.paintLayerContents(context, paintingInfo, paintFlags);
if (m_owningLayer.containsDirtyOverlayScrollbars())
m_owningLayer.paintLayerContents(context, paintingInfo, paintFlags | RenderLayer::PaintLayerPaintingOverlayScrollbars);
if (m_owningLayer.isRootLayer())
m_owningLayer.renderer().view().frameView().didPaintContents(context, paintDirtyRect, paintingState);
compositor().didPaintBacking(this);
ASSERT(!m_owningLayer.m_usedTransparency);
}
static void paintScrollbar(Scrollbar* scrollbar, GraphicsContext& context, const IntRect& clip)
{
if (!scrollbar)
return;
context.save();
const IntRect& scrollbarRect = scrollbar->frameRect();
context.translate(-scrollbarRect.x(), -scrollbarRect.y());
IntRect transformedClip = clip;
transformedClip.moveBy(scrollbarRect.location());
scrollbar->paint(&context, transformedClip);
context.restore();
}
void RenderLayerBacking::paintContents(const GraphicsLayer* graphicsLayer, GraphicsContext& context, GraphicsLayerPaintingPhase paintingPhase, const FloatRect& clip)
{
#ifndef NDEBUG
if (Page* page = renderer().frame().page())
page->setIsPainting(true);
#endif
FloatRect adjustedClipRect = clip;
adjustedClipRect.move(-m_devicePixelFractionFromRenderer);
IntRect dirtyRect = enclosingIntRect(adjustedClipRect);
if (graphicsLayer == m_graphicsLayer.get()
|| graphicsLayer == m_foregroundLayer.get()
|| graphicsLayer == m_backgroundLayer.get()
|| graphicsLayer == m_maskLayer.get()
|| graphicsLayer == m_scrollingContentsLayer.get()) {
InspectorInstrumentation::willPaint(&renderer());
if (!(paintingPhase & GraphicsLayerPaintOverflowContents))
dirtyRect.intersect(enclosingIntRect(compositedBoundsIncludingMargin()));
paintIntoLayer(graphicsLayer, &context, dirtyRect, PaintBehaviorNormal, paintingPhase);
InspectorInstrumentation::didPaint(&renderer(), &context, dirtyRect);
} else if (graphicsLayer == layerForHorizontalScrollbar()) {
paintScrollbar(m_owningLayer.horizontalScrollbar(), context, dirtyRect);
} else if (graphicsLayer == layerForVerticalScrollbar()) {
paintScrollbar(m_owningLayer.verticalScrollbar(), context, dirtyRect);
} else if (graphicsLayer == layerForScrollCorner()) {
const LayoutRect& scrollCornerAndResizer = m_owningLayer.scrollCornerAndResizerRect();
context.save();
context.translate(-scrollCornerAndResizer.x(), -scrollCornerAndResizer.y());
LayoutRect transformedClip = LayoutRect(clip);
transformedClip.moveBy(scrollCornerAndResizer.location());
m_owningLayer.paintScrollCorner(&context, IntPoint(), pixelSnappedIntRect(transformedClip));
m_owningLayer.paintResizer(&context, IntPoint(), transformedClip);
context.restore();
}
#ifndef NDEBUG
if (Page* page = renderer().frame().page())
page->setIsPainting(false);
#endif
}
float RenderLayerBacking::pageScaleFactor() const
{
return compositor().pageScaleFactor();
}
float RenderLayerBacking::zoomedOutPageScaleFactor() const
{
return compositor().zoomedOutPageScaleFactor();
}
float RenderLayerBacking::deviceScaleFactor() const
{
return compositor().deviceScaleFactor();
}
float RenderLayerBacking::contentsScaleMultiplierForNewTiles(const GraphicsLayer* layer) const
{
return compositor().contentsScaleMultiplierForNewTiles(layer);
}
bool RenderLayerBacking::paintsOpaquelyAtNonIntegralScales(const GraphicsLayer*) const
{
return m_isMainFrameRenderViewLayer;
}
void RenderLayerBacking::didCommitChangesForLayer(const GraphicsLayer* layer) const
{
compositor().didFlushChangesForLayer(m_owningLayer, layer);
}
bool RenderLayerBacking::getCurrentTransform(const GraphicsLayer* graphicsLayer, TransformationMatrix& transform) const
{
GraphicsLayer* transformedLayer = m_contentsContainmentLayer.get() ? m_contentsContainmentLayer.get() : m_graphicsLayer.get();
if (graphicsLayer != transformedLayer)
return false;
if (m_owningLayer.hasTransform()) {
transform = m_owningLayer.currentTransform(RenderStyle::ExcludeTransformOrigin);
return true;
}
return false;
}
bool RenderLayerBacking::isTrackingRepaints() const
{
return static_cast<GraphicsLayerClient&>(compositor()).isTrackingRepaints();
}
bool RenderLayerBacking::shouldSkipLayerInDump(const GraphicsLayer* layer) const
{
return m_isMainFrameRenderViewLayer && layer && layer == m_childContainmentLayer.get();
}
bool RenderLayerBacking::shouldDumpPropertyForLayer(const GraphicsLayer* layer, const char* propertyName) const
{
if (m_isMainFrameRenderViewLayer && layer == m_graphicsLayer.get()) {
if (!strcmp(propertyName, "drawsContent"))
return false;
if (!strcmp(propertyName, "backgroundColor") && layer->backgroundColor() == Color::white)
return false;
if (!strcmp(propertyName, "repaintRects"))
return false;
}
return true;
}
bool RenderLayerBacking::shouldAggressivelyRetainTiles(const GraphicsLayer*) const
{
if (!m_isMainFrameRenderViewLayer)
return false;
if (Page* page = renderer().frame().page())
return page->settings().aggressiveTileRetentionEnabled();
return false;
}
bool RenderLayerBacking::shouldTemporarilyRetainTileCohorts(const GraphicsLayer*) const
{
if (Page* page = renderer().frame().page())
return page->settings().temporaryTileCohortRetentionEnabled();
return true;
}
#ifndef NDEBUG
void RenderLayerBacking::verifyNotPainting()
{
ASSERT(!renderer().frame().page() || !renderer().frame().page()->isPainting());
}
#endif
bool RenderLayerBacking::startAnimation(double timeOffset, const Animation* anim, const KeyframeList& keyframes)
{
bool hasOpacity = keyframes.containsProperty(CSSPropertyOpacity);
bool hasTransform = renderer().isBox() && keyframes.containsProperty(CSSPropertyWebkitTransform);
#if ENABLE(CSS_FILTERS)
bool hasFilter = keyframes.containsProperty(CSSPropertyWebkitFilter);
#else
bool hasFilter = false;
#endif
if (!hasOpacity && !hasTransform && !hasFilter)
return false;
KeyframeValueList transformVector(AnimatedPropertyWebkitTransform);
KeyframeValueList opacityVector(AnimatedPropertyOpacity);
#if ENABLE(CSS_FILTERS)
KeyframeValueList filterVector(AnimatedPropertyWebkitFilter);
#endif
size_t numKeyframes = keyframes.size();
for (size_t i = 0; i < numKeyframes; ++i) {
const KeyframeValue& currentKeyframe = keyframes[i];
const RenderStyle* keyframeStyle = currentKeyframe.style();
double key = currentKeyframe.key();
if (!keyframeStyle)
continue;
TimingFunction* tf = currentKeyframe.timingFunction(keyframes.animationName());
bool isFirstOrLastKeyframe = key == 0 || key == 1;
if ((hasTransform && isFirstOrLastKeyframe) || currentKeyframe.containsProperty(CSSPropertyWebkitTransform))
transformVector.insert(TransformAnimationValue::create(key, keyframeStyle->transform(), tf));
if ((hasOpacity && isFirstOrLastKeyframe) || currentKeyframe.containsProperty(CSSPropertyOpacity))
opacityVector.insert(FloatAnimationValue::create(key, keyframeStyle->opacity(), tf));
#if ENABLE(CSS_FILTERS)
if ((hasFilter && isFirstOrLastKeyframe) || currentKeyframe.containsProperty(CSSPropertyWebkitFilter))
filterVector.insert(FilterAnimationValue::create(key, keyframeStyle->filter(), tf));
#endif
}
if (renderer().frame().page() && !renderer().frame().page()->settings().acceleratedCompositedAnimationsEnabled())
return false;
bool didAnimate = false;
if (hasTransform && m_graphicsLayer->addAnimation(transformVector, toRenderBox(renderer()).pixelSnappedBorderBoxRect().size(), anim, keyframes.animationName(), timeOffset))
didAnimate = true;
if (hasOpacity && m_graphicsLayer->addAnimation(opacityVector, IntSize(), anim, keyframes.animationName(), timeOffset))
didAnimate = true;
#if ENABLE(CSS_FILTERS)
if (hasFilter && m_graphicsLayer->addAnimation(filterVector, IntSize(), anim, keyframes.animationName(), timeOffset))
didAnimate = true;
#endif
return didAnimate;
}
void RenderLayerBacking::animationPaused(double timeOffset, const String& animationName)
{
m_graphicsLayer->pauseAnimation(animationName, timeOffset);
}
void RenderLayerBacking::animationFinished(const String& animationName)
{
m_graphicsLayer->removeAnimation(animationName);
}
bool RenderLayerBacking::startTransition(double timeOffset, CSSPropertyID property, const RenderStyle* fromStyle, const RenderStyle* toStyle)
{
bool didAnimate = false;
ASSERT(property != CSSPropertyInvalid);
if (property == CSSPropertyOpacity) {
const Animation* opacityAnim = toStyle->transitionForProperty(CSSPropertyOpacity);
if (opacityAnim && !opacityAnim->isEmptyOrZeroDuration()) {
KeyframeValueList opacityVector(AnimatedPropertyOpacity);
opacityVector.insert(FloatAnimationValue::create(0, compositingOpacity(fromStyle->opacity())));
opacityVector.insert(FloatAnimationValue::create(1, compositingOpacity(toStyle->opacity())));
if (m_graphicsLayer->addAnimation(opacityVector, FloatSize(), opacityAnim, GraphicsLayer::animationNameForTransition(AnimatedPropertyOpacity), timeOffset)) {
updateOpacity(*toStyle);
didAnimate = true;
}
}
}
if (property == CSSPropertyWebkitTransform && m_owningLayer.hasTransform()) {
const Animation* transformAnim = toStyle->transitionForProperty(CSSPropertyWebkitTransform);
if (transformAnim && !transformAnim->isEmptyOrZeroDuration()) {
KeyframeValueList transformVector(AnimatedPropertyWebkitTransform);
transformVector.insert(TransformAnimationValue::create(0, fromStyle->transform()));
transformVector.insert(TransformAnimationValue::create(1, toStyle->transform()));
if (m_graphicsLayer->addAnimation(transformVector, toRenderBox(renderer()).pixelSnappedBorderBoxRect().size(), transformAnim, GraphicsLayer::animationNameForTransition(AnimatedPropertyWebkitTransform), timeOffset)) {
updateTransform(*toStyle);
didAnimate = true;
}
}
}
#if ENABLE(CSS_FILTERS)
if (property == CSSPropertyWebkitFilter && m_owningLayer.hasFilter()) {
const Animation* filterAnim = toStyle->transitionForProperty(CSSPropertyWebkitFilter);
if (filterAnim && !filterAnim->isEmptyOrZeroDuration()) {
KeyframeValueList filterVector(AnimatedPropertyWebkitFilter);
filterVector.insert(FilterAnimationValue::create(0, fromStyle->filter()));
filterVector.insert(FilterAnimationValue::create(1, toStyle->filter()));
if (m_graphicsLayer->addAnimation(filterVector, FloatSize(), filterAnim, GraphicsLayer::animationNameForTransition(AnimatedPropertyWebkitFilter), timeOffset)) {
updateFilters(*toStyle);
didAnimate = true;
}
}
}
#endif
return didAnimate;
}
void RenderLayerBacking::transitionPaused(double timeOffset, CSSPropertyID property)
{
AnimatedPropertyID animatedProperty = cssToGraphicsLayerProperty(property);
if (animatedProperty != AnimatedPropertyInvalid)
m_graphicsLayer->pauseAnimation(GraphicsLayer::animationNameForTransition(animatedProperty), timeOffset);
}
void RenderLayerBacking::transitionFinished(CSSPropertyID property)
{
AnimatedPropertyID animatedProperty = cssToGraphicsLayerProperty(property);
if (animatedProperty != AnimatedPropertyInvalid)
m_graphicsLayer->removeAnimation(GraphicsLayer::animationNameForTransition(animatedProperty));
}
void RenderLayerBacking::notifyAnimationStarted(const GraphicsLayer*, double time)
{
renderer().animation().notifyAnimationStarted(&renderer(), time);
}
void RenderLayerBacking::notifyFlushRequired(const GraphicsLayer* layer)
{
if (renderer().documentBeingDestroyed())
return;
compositor().scheduleLayerFlush(layer->canThrottleLayerFlush());
}
void RenderLayerBacking::notifyFlushBeforeDisplayRefresh(const GraphicsLayer* layer)
{
compositor().notifyFlushBeforeDisplayRefresh(layer);
}
void RenderLayerBacking::suspendAnimations(double time)
{
m_graphicsLayer->suspendAnimations(time);
}
void RenderLayerBacking::resumeAnimations()
{
m_graphicsLayer->resumeAnimations();
}
LayoutRect RenderLayerBacking::compositedBounds() const
{
return m_compositedBounds;
}
void RenderLayerBacking::setCompositedBounds(const LayoutRect& bounds)
{
m_compositedBounds = bounds;
}
LayoutRect RenderLayerBacking::compositedBoundsIncludingMargin() const
{
TiledBacking* tiledBacking = this->tiledBacking();
if (!tiledBacking || !tiledBacking->hasMargins())
return compositedBounds();
LayoutRect boundsIncludingMargin = compositedBounds();
LayoutUnit leftMarginWidth = tiledBacking->leftMarginWidth();
LayoutUnit topMarginHeight = tiledBacking->topMarginHeight();
boundsIncludingMargin.moveBy(LayoutPoint(-leftMarginWidth, -topMarginHeight));
boundsIncludingMargin.expand(leftMarginWidth + tiledBacking->rightMarginWidth(), topMarginHeight + tiledBacking->bottomMarginHeight());
return boundsIncludingMargin;
}
CSSPropertyID RenderLayerBacking::graphicsLayerToCSSProperty(AnimatedPropertyID property)
{
CSSPropertyID cssProperty = CSSPropertyInvalid;
switch (property) {
case AnimatedPropertyWebkitTransform:
cssProperty = CSSPropertyWebkitTransform;
break;
case AnimatedPropertyOpacity:
cssProperty = CSSPropertyOpacity;
break;
case AnimatedPropertyBackgroundColor:
cssProperty = CSSPropertyBackgroundColor;
break;
case AnimatedPropertyWebkitFilter:
#if ENABLE(CSS_FILTERS)
cssProperty = CSSPropertyWebkitFilter;
#else
ASSERT_NOT_REACHED();
#endif
break;
case AnimatedPropertyInvalid:
ASSERT_NOT_REACHED();
}
return cssProperty;
}
AnimatedPropertyID RenderLayerBacking::cssToGraphicsLayerProperty(CSSPropertyID cssProperty)
{
switch (cssProperty) {
case CSSPropertyWebkitTransform:
return AnimatedPropertyWebkitTransform;
case CSSPropertyOpacity:
return AnimatedPropertyOpacity;
case CSSPropertyBackgroundColor:
return AnimatedPropertyBackgroundColor;
#if ENABLE(CSS_FILTERS)
case CSSPropertyWebkitFilter:
return AnimatedPropertyWebkitFilter;
#endif
default:
break;
}
return AnimatedPropertyInvalid;
}
CompositingLayerType RenderLayerBacking::compositingLayerType() const
{
if (m_graphicsLayer->usesContentsLayer())
return MediaCompositingLayer;
if (m_graphicsLayer->drawsContent())
return m_graphicsLayer->usingTiledBacking() ? TiledCompositingLayer : NormalCompositingLayer;
return ContainerCompositingLayer;
}
double RenderLayerBacking::backingStoreMemoryEstimate() const
{
double backingMemory;
backingMemory = m_graphicsLayer->backingStoreMemoryEstimate();
if (m_foregroundLayer)
backingMemory += m_foregroundLayer->backingStoreMemoryEstimate();
if (m_backgroundLayer)
backingMemory += m_backgroundLayer->backingStoreMemoryEstimate();
if (m_maskLayer)
backingMemory += m_maskLayer->backingStoreMemoryEstimate();
if (m_scrollingContentsLayer)
backingMemory += m_scrollingContentsLayer->backingStoreMemoryEstimate();
if (m_layerForHorizontalScrollbar)
backingMemory += m_layerForHorizontalScrollbar->backingStoreMemoryEstimate();
if (m_layerForVerticalScrollbar)
backingMemory += m_layerForVerticalScrollbar->backingStoreMemoryEstimate();
if (m_layerForScrollCorner)
backingMemory += m_layerForScrollCorner->backingStoreMemoryEstimate();
return backingMemory;
}
}