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 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.
26 #ifndef RenderLayerBacking_h
27 #define RenderLayerBacking_h
29 #include "FloatPoint.h"
30 #include "FloatPoint3D.h"
31 #include "GraphicsLayer.h"
32 #include "GraphicsLayerClient.h"
33 #include "RenderLayer.h"
34 #include "ScrollingCoordinator.h"
39 class RenderLayerCompositor;
41 class TransformationMatrix;
43 enum CompositingLayerType {
44 NormalCompositingLayer, // non-tiled layer with backing store
45 TiledCompositingLayer, // tiled layer (always has backing store)
46 MediaCompositingLayer, // layer that contains an image, video, webGL or plugin
47 ContainerCompositingLayer // layer with no backing store
50 // RenderLayerBacking controls the compositing behavior for a single RenderLayer.
51 // It holds the various GraphicsLayers, and makes decisions about intra-layer rendering
54 // There is one RenderLayerBacking for each RenderLayer that is composited.
56 class RenderLayerBacking final : public GraphicsLayerClient {
57 WTF_MAKE_NONCOPYABLE(RenderLayerBacking); WTF_MAKE_FAST_ALLOCATED;
59 explicit RenderLayerBacking(RenderLayer&);
60 ~RenderLayerBacking();
63 void layerWillBeDestroyed();
66 RenderLayer& owningLayer() const { return m_owningLayer; }
68 enum UpdateAfterLayoutFlag {
69 CompositingChildrenOnly = 1 << 0,
70 NeedsFullRepaint = 1 << 1,
73 typedef unsigned UpdateAfterLayoutFlags;
74 void updateAfterLayout(UpdateAfterLayoutFlags);
76 // Returns true if layer configuration changed.
77 bool updateConfiguration();
79 // Update graphics layer position and bounds.
80 void updateGeometry();
82 // Update state the requires that descendant layers have been updated.
83 void updateAfterDescendants();
85 // Update contents and clipping structure.
86 void updateDrawsContent();
88 GraphicsLayer* graphicsLayer() const { return m_graphicsLayer.get(); }
90 // Layer to clip children
91 bool hasClippingLayer() const { return (m_childContainmentLayer && !m_usingTiledCacheLayer); }
92 GraphicsLayer* clippingLayer() const { return !m_usingTiledCacheLayer ? m_childContainmentLayer.get() : nullptr; }
94 // Layer to get clipped by ancestor
95 bool hasAncestorClippingLayer() const { return m_ancestorClippingLayer != nullptr; }
96 GraphicsLayer* ancestorClippingLayer() const { return m_ancestorClippingLayer.get(); }
98 GraphicsLayer* contentsContainmentLayer() const { return m_contentsContainmentLayer.get(); }
100 GraphicsLayer* foregroundLayer() const { return m_foregroundLayer.get(); }
101 GraphicsLayer* backgroundLayer() const { return m_backgroundLayer.get(); }
102 bool backgroundLayerPaintsFixedRootBackground() const { return m_backgroundLayerPaintsFixedRootBackground; }
104 bool hasScrollingLayer() const { return m_scrollingLayer != nullptr; }
105 GraphicsLayer* scrollingLayer() const { return m_scrollingLayer.get(); }
106 GraphicsLayer* scrollingContentsLayer() const { return m_scrollingContentsLayer.get(); }
108 void detachFromScrollingCoordinator(LayerScrollCoordinationRoles);
110 ScrollingNodeID scrollingNodeIDForRole(LayerScrollCoordinationRole role) const
114 return m_scrollingNodeID;
115 case ViewportConstrained:
116 return m_viewportConstrainedNodeID;
121 void setScrollingNodeIDForRole(ScrollingNodeID nodeID, LayerScrollCoordinationRole role)
125 m_scrollingNodeID = nodeID;
127 case ViewportConstrained:
128 m_viewportConstrainedNodeID = nodeID;
129 setIsScrollCoordinatedWithViewportConstrainedRole(nodeID);
134 ScrollingNodeID scrollingNodeIDForChildren() const { return m_scrollingNodeID ? m_scrollingNodeID : m_viewportConstrainedNodeID; }
136 void setIsScrollCoordinatedWithViewportConstrainedRole(bool);
138 bool hasMaskLayer() const { return m_maskLayer != 0; }
139 bool hasChildClippingMaskLayer() const { return m_childClippingMaskLayer != nullptr; }
141 GraphicsLayer* parentForSublayers() const;
142 GraphicsLayer* childForSuperlayers() const;
144 // RenderLayers with backing normally short-circuit paintLayer() because
145 // their content is rendered via callbacks from GraphicsLayer. However, the document
146 // layer is special, because it has a GraphicsLayer to act as a container for the GraphicsLayers
147 // for descendants, but its contents usually render into the window (in which case this returns true).
148 // This returns false for other layers, and when the document layer actually needs to paint into its backing store
150 bool paintsIntoWindow() const;
152 // Returns true for a composited layer that has no backing store of its own, so
153 // paints into some ancestor layer.
154 bool paintsIntoCompositedAncestor() const { return !m_requiresOwnBackingStore; }
156 void setRequiresOwnBackingStore(bool);
158 void setContentsNeedDisplay(GraphicsLayer::ShouldClipToLayer = GraphicsLayer::ClipToLayer);
159 // r is in the coordinate space of the layer's render object
160 void setContentsNeedDisplayInRect(const LayoutRect&, GraphicsLayer::ShouldClipToLayer = GraphicsLayer::ClipToLayer);
162 // Notification from the renderer that its content changed.
163 void contentChanged(ContentChangeType);
165 // Interface to start, finish, suspend and resume animations and transitions
166 bool startTransition(double, CSSPropertyID, const RenderStyle* fromStyle, const RenderStyle* toStyle);
167 void transitionPaused(double timeOffset, CSSPropertyID);
168 void transitionFinished(CSSPropertyID);
170 bool startAnimation(double timeOffset, const Animation* anim, const KeyframeList& keyframes);
171 void animationPaused(double timeOffset, const String& name);
172 void animationFinished(const String& name);
174 void suspendAnimations(double time = 0);
175 void resumeAnimations();
177 LayoutRect compositedBounds() const;
178 void setCompositedBounds(const LayoutRect&);
179 void updateCompositedBounds();
181 void updateAfterWidgetResize();
182 void positionOverflowControlsLayers();
183 bool hasUnpositionedOverflowControlsLayers() const;
185 bool usingTiledBacking() const { return m_usingTiledCacheLayer; }
186 WEBCORE_EXPORT TiledBacking* tiledBacking() const;
187 void adjustTiledBackingCoverage();
188 void setTiledBackingHasMargins(bool hasExtendedBackgroundOnLeftAndRight, bool hasExtendedBackgroundOnTopAndBottom);
190 void updateDebugIndicators(bool showBorder, bool showRepaintCounter);
192 // GraphicsLayerClient interface
193 virtual void tiledBackingUsageChanged(const GraphicsLayer*, bool /*usingTiledBacking*/) override;
194 virtual void notifyAnimationStarted(const GraphicsLayer*, const String& animationKey, double startTime) override;
195 virtual void notifyFlushRequired(const GraphicsLayer*) override;
196 virtual void notifyFlushBeforeDisplayRefresh(const GraphicsLayer*) override;
198 virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const FloatRect& clip) override;
200 virtual float deviceScaleFactor() const override;
201 virtual float contentsScaleMultiplierForNewTiles(const GraphicsLayer*) const override;
203 virtual bool paintsOpaquelyAtNonIntegralScales(const GraphicsLayer*) const override;
205 virtual float pageScaleFactor() const override;
206 virtual float zoomedOutPageScaleFactor() const override;
207 virtual void didCommitChangesForLayer(const GraphicsLayer*) const override;
208 virtual bool getCurrentTransform(const GraphicsLayer*, TransformationMatrix&) const override;
210 virtual bool isTrackingRepaints() const override;
211 virtual bool shouldSkipLayerInDump(const GraphicsLayer*, LayerTreeAsTextBehavior) const override;
212 virtual bool shouldDumpPropertyForLayer(const GraphicsLayer*, const char* propertyName) const override;
214 virtual bool shouldAggressivelyRetainTiles(const GraphicsLayer*) const override;
215 virtual bool shouldTemporarilyRetainTileCohorts(const GraphicsLayer*) const override;
216 virtual bool needsPixelAligment() const override { return !m_isMainFrameRenderViewLayer; }
219 virtual bool needsIOSDumpRenderTreeMainFrameRenderViewLayerIsAlwaysOpaqueHack(const GraphicsLayer&) const override;
223 virtual void verifyNotPainting() override;
226 WEBCORE_EXPORT LayoutRect contentsBox() const;
228 // For informative purposes only.
229 WEBCORE_EXPORT CompositingLayerType compositingLayerType() const;
231 GraphicsLayer* layerForHorizontalScrollbar() const { return m_layerForHorizontalScrollbar.get(); }
232 GraphicsLayer* layerForVerticalScrollbar() const { return m_layerForVerticalScrollbar.get(); }
233 GraphicsLayer* layerForScrollCorner() const { return m_layerForScrollCorner.get(); }
235 bool canCompositeFilters() const { return m_canCompositeFilters; }
236 #if ENABLE(FILTERS_LEVEL_2)
237 bool canCompositeBackdropFilters() const { return m_canCompositeBackdropFilters; }
240 // Return an estimate of the backing store area (in pixels) allocated by this object's GraphicsLayers.
241 WEBCORE_EXPORT double backingStoreMemoryEstimate() const;
243 LayoutSize devicePixelFractionFromRenderer() const { return m_devicePixelFractionFromRenderer; }
246 FloatRect backgroundBoxForPainting() const;
248 void createPrimaryGraphicsLayer();
249 void destroyGraphicsLayers();
251 void willDestroyLayer(const GraphicsLayer*);
253 LayoutRect compositedBoundsIncludingMargin() const;
255 std::unique_ptr<GraphicsLayer> createGraphicsLayer(const String&, GraphicsLayer::Type = GraphicsLayer::Type::Normal);
257 RenderLayerModelObject& renderer() const { return m_owningLayer.renderer(); }
258 RenderLayerCompositor& compositor() const { return m_owningLayer.compositor(); }
260 void updateInternalHierarchy();
261 bool updateAncestorClippingLayer(bool needsAncestorClip);
262 bool updateDescendantClippingLayer(bool needsDescendantClip);
263 bool updateOverflowControlsLayers(bool needsHorizontalScrollbarLayer, bool needsVerticalScrollbarLayer, bool needsScrollCornerLayer);
264 bool updateForegroundLayer(bool needsForegroundLayer);
265 bool updateBackgroundLayer(bool needsBackgroundLayer);
266 void updateMaskingLayer(bool hasMask, bool hasClipPath);
267 bool requiresHorizontalScrollbarLayer() const;
268 bool requiresVerticalScrollbarLayer() const;
269 bool requiresScrollCornerLayer() const;
270 bool updateScrollingLayers(bool scrollingLayers);
271 void updateDrawsContent(bool isSimpleContainer);
272 void updateChildClippingStrategy(bool needsDescendantsClippingLayer);
274 void updateMaskingLayerGeometry();
276 void updateRootLayerConfiguration();
278 void setBackgroundLayerPaintsFixedRootBackground(bool);
280 GraphicsLayerPaintingPhase paintingPhaseForPrimaryLayer() const;
282 LayoutSize contentOffsetInCompostingLayer() const;
283 // Result is transform origin in device pixels.
284 FloatPoint3D computeTransformOriginForPainting(const LayoutRect& borderBox) const;
286 void updateOpacity(const RenderStyle&);
287 void updateTransform(const RenderStyle&);
288 void updateFilters(const RenderStyle&);
289 #if ENABLE(FILTERS_LEVEL_2)
290 void updateBackdropFilters(const RenderStyle&);
292 #if ENABLE(CSS_COMPOSITING)
293 void updateBlendMode(const RenderStyle&);
295 void updateCustomAppearance(const RenderStyle&);
297 // Return the opacity value that this layer should use for compositing.
298 float compositingOpacity(float rendererOpacity) const;
300 bool isMainFrameRenderViewLayer() const;
302 bool paintsBoxDecorations() const;
303 bool paintsChildren() const;
305 // Returns true if this compositing layer has no visible content.
306 bool isSimpleContainerCompositingLayer() const;
307 // Returns true if this layer has content that needs to be rendered by painting into the backing store.
308 bool containsPaintedContent(bool isSimpleContainer) const;
309 // Returns true if the RenderLayer just contains an image that we can composite directly.
310 bool isDirectlyCompositedImage() const;
311 void updateImageContents();
313 Color rendererBackgroundColor() const;
314 void updateDirectlyCompositedBackgroundColor(bool isSimpleContainer, bool& didUpdateContentsRect);
315 void updateDirectlyCompositedBackgroundImage(bool isSimpleContainer, bool& didUpdateContentsRect);
316 void updateDirectlyCompositedContents(bool isSimpleContainer, bool& didUpdateContentsRect);
318 void resetContentsRect();
320 bool isPaintDestinationForDescendantLayers() const;
322 bool shouldClipCompositedBounds() const;
324 bool hasTiledBackingFlatteningLayer() const { return (m_childContainmentLayer && m_usingTiledCacheLayer); }
325 GraphicsLayer* tileCacheFlatteningLayer() const { return m_usingTiledCacheLayer ? m_childContainmentLayer.get() : nullptr; }
327 void paintIntoLayer(const GraphicsLayer*, GraphicsContext*, const IntRect& paintDirtyRect, PaintBehavior, GraphicsLayerPaintingPhase);
329 // Helper function for updateGeometry.
330 void adjustAncestorCompositingBoundsForFlowThread(LayoutRect& ancestorCompositingBounds, const RenderLayer* compositingAncestor) const;
332 static CSSPropertyID graphicsLayerToCSSProperty(AnimatedPropertyID);
333 static AnimatedPropertyID cssToGraphicsLayerProperty(CSSPropertyID);
335 RenderLayer& m_owningLayer;
337 std::unique_ptr<GraphicsLayer> m_ancestorClippingLayer; // Only used if we are clipped by an ancestor which is not a stacking context.
338 std::unique_ptr<GraphicsLayer> m_contentsContainmentLayer; // Only used if we have a background layer; takes the transform.
339 std::unique_ptr<GraphicsLayer> m_graphicsLayer;
340 std::unique_ptr<GraphicsLayer> m_foregroundLayer; // Only used in cases where we need to draw the foreground separately.
341 std::unique_ptr<GraphicsLayer> m_backgroundLayer; // Only used in cases where we need to draw the background separately.
342 std::unique_ptr<GraphicsLayer> m_childContainmentLayer; // Only used if we have clipping on a stacking context with compositing children, or if the layer has a tile cache.
343 std::unique_ptr<GraphicsLayer> m_maskLayer; // Only used if we have a mask and/or clip-path.
344 std::unique_ptr<GraphicsLayer> m_childClippingMaskLayer; // Only used if we have to clip child layers or accelerated contents with border radius or clip-path.
346 std::unique_ptr<GraphicsLayer> m_layerForHorizontalScrollbar;
347 std::unique_ptr<GraphicsLayer> m_layerForVerticalScrollbar;
348 std::unique_ptr<GraphicsLayer> m_layerForScrollCorner;
350 std::unique_ptr<GraphicsLayer> m_scrollingLayer; // Only used if the layer is using composited scrolling.
351 std::unique_ptr<GraphicsLayer> m_scrollingContentsLayer; // Only used if the layer is using composited scrolling.
353 ScrollingNodeID m_viewportConstrainedNodeID;
354 ScrollingNodeID m_scrollingNodeID;
356 LayoutRect m_compositedBounds;
357 LayoutSize m_devicePixelFractionFromRenderer;
359 bool m_artificiallyInflatedBounds; // bounds had to be made non-zero to make transform-origin work
360 bool m_isMainFrameRenderViewLayer;
361 bool m_usingTiledCacheLayer;
362 bool m_requiresOwnBackingStore;
363 bool m_canCompositeFilters;
364 #if ENABLE(FILTERS_LEVEL_2)
365 bool m_canCompositeBackdropFilters;
367 bool m_backgroundLayerPaintsFixedRootBackground;
370 enum CanvasCompositingStrategy {
372 CanvasPaintedToLayer,
373 CanvasAsLayerContents
375 CanvasCompositingStrategy canvasCompositingStrategy(const RenderObject&);
377 } // namespace WebCore
379 #endif // RenderLayerBacking_h