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 void tiledBackingUsageChanged(const GraphicsLayer*, bool /*usingTiledBacking*/) override;
194 void notifyAnimationStarted(const GraphicsLayer*, const String& animationKey, double startTime) override;
195 void notifyFlushRequired(const GraphicsLayer*) override;
196 void notifyFlushBeforeDisplayRefresh(const GraphicsLayer*) override;
198 void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const FloatRect& clip) override;
200 float deviceScaleFactor() const override;
201 float contentsScaleMultiplierForNewTiles(const GraphicsLayer*) const override;
203 bool paintsOpaquelyAtNonIntegralScales(const GraphicsLayer*) const override;
205 float pageScaleFactor() const override;
206 float zoomedOutPageScaleFactor() const override;
207 void didCommitChangesForLayer(const GraphicsLayer*) const override;
208 bool getCurrentTransform(const GraphicsLayer*, TransformationMatrix&) const override;
210 bool isTrackingRepaints() const override;
211 bool shouldSkipLayerInDump(const GraphicsLayer*, LayerTreeAsTextBehavior) const override;
212 bool shouldDumpPropertyForLayer(const GraphicsLayer*, const char* propertyName) const override;
214 bool shouldAggressivelyRetainTiles(const GraphicsLayer*) const override;
215 bool shouldTemporarilyRetainTileCohorts(const GraphicsLayer*) const override;
216 bool useGiantTiles() const override;
217 bool needsPixelAligment() const override { return !m_isMainFrameRenderViewLayer; }
220 bool needsIOSDumpRenderTreeMainFrameRenderViewLayerIsAlwaysOpaqueHack(const GraphicsLayer&) const override;
224 void verifyNotPainting() override;
227 WEBCORE_EXPORT LayoutRect contentsBox() const;
229 // For informative purposes only.
230 WEBCORE_EXPORT CompositingLayerType compositingLayerType() const;
232 GraphicsLayer* layerForHorizontalScrollbar() const { return m_layerForHorizontalScrollbar.get(); }
233 GraphicsLayer* layerForVerticalScrollbar() const { return m_layerForVerticalScrollbar.get(); }
234 GraphicsLayer* layerForScrollCorner() const { return m_layerForScrollCorner.get(); }
236 bool canCompositeFilters() const { return m_canCompositeFilters; }
237 #if ENABLE(FILTERS_LEVEL_2)
238 bool canCompositeBackdropFilters() const { return m_canCompositeBackdropFilters; }
241 // Return an estimate of the backing store area (in pixels) allocated by this object's GraphicsLayers.
242 WEBCORE_EXPORT double backingStoreMemoryEstimate() const;
245 WEBCORE_EXPORT void setUsesDisplayListDrawing(bool);
246 WEBCORE_EXPORT String displayListAsText(DisplayList::AsTextFlags) const;
248 WEBCORE_EXPORT void setIsTrackingDisplayListReplay(bool);
249 WEBCORE_EXPORT String replayDisplayListAsText(DisplayList::AsTextFlags) const;
251 LayoutSize devicePixelFractionFromRenderer() const { return m_devicePixelFractionFromRenderer; }
254 FloatRect backgroundBoxForSimpleContainerPainting() const;
256 void createPrimaryGraphicsLayer();
257 void destroyGraphicsLayers();
259 void willDestroyLayer(const GraphicsLayer*);
261 LayoutRect compositedBoundsIncludingMargin() const;
263 std::unique_ptr<GraphicsLayer> createGraphicsLayer(const String&, GraphicsLayer::Type = GraphicsLayer::Type::Normal);
265 RenderLayerModelObject& renderer() const { return m_owningLayer.renderer(); }
266 RenderBox* renderBox() const { return m_owningLayer.renderBox(); }
267 RenderLayerCompositor& compositor() const { return m_owningLayer.compositor(); }
269 void updateInternalHierarchy();
270 bool updateAncestorClippingLayer(bool needsAncestorClip);
271 bool updateDescendantClippingLayer(bool needsDescendantClip);
272 bool updateOverflowControlsLayers(bool needsHorizontalScrollbarLayer, bool needsVerticalScrollbarLayer, bool needsScrollCornerLayer);
273 bool updateForegroundLayer(bool needsForegroundLayer);
274 bool updateBackgroundLayer(bool needsBackgroundLayer);
275 void updateMaskingLayer(bool hasMask, bool hasClipPath);
276 bool requiresHorizontalScrollbarLayer() const;
277 bool requiresVerticalScrollbarLayer() const;
278 bool requiresScrollCornerLayer() const;
279 bool updateScrollingLayers(bool scrollingLayers);
280 void updateDrawsContent(bool isSimpleContainer);
281 void updateChildClippingStrategy(bool needsDescendantsClippingLayer);
283 void updateMaskingLayerGeometry();
285 void updateRootLayerConfiguration();
287 void setBackgroundLayerPaintsFixedRootBackground(bool);
289 GraphicsLayerPaintingPhase paintingPhaseForPrimaryLayer() const;
291 LayoutSize contentOffsetInCompostingLayer() const;
292 // Result is transform origin in device pixels.
293 FloatPoint3D computeTransformOriginForPainting(const LayoutRect& borderBox) const;
295 void updateOpacity(const RenderStyle&);
296 void updateTransform(const RenderStyle&);
297 void updateFilters(const RenderStyle&);
298 #if ENABLE(FILTERS_LEVEL_2)
299 void updateBackdropFilters(const RenderStyle&);
300 void updateBackdropFiltersGeometry();
302 #if ENABLE(CSS_COMPOSITING)
303 void updateBlendMode(const RenderStyle&);
305 void updateCustomAppearance(const RenderStyle&);
307 // Return the opacity value that this layer should use for compositing.
308 float compositingOpacity(float rendererOpacity) const;
310 bool isMainFrameRenderViewLayer() const;
312 bool paintsNonDirectCompositedBoxDecoration() const;
313 bool paintsChildren() const;
315 // Returns true if this compositing layer has no visible content.
316 bool isSimpleContainerCompositingLayer() const;
317 // Returns true if this layer has content that needs to be rendered by painting into the backing store.
318 bool containsPaintedContent(bool isSimpleContainer) const;
319 // Returns true if the RenderLayer just contains an image that we can composite directly.
320 bool isDirectlyCompositedImage() const;
321 void updateImageContents();
323 Color rendererBackgroundColor() const;
324 void updateDirectlyCompositedBackgroundColor(bool isSimpleContainer, bool& didUpdateContentsRect);
325 void updateDirectlyCompositedBackgroundImage(bool isSimpleContainer, bool& didUpdateContentsRect);
326 void updateDirectlyCompositedContents(bool isSimpleContainer, bool& didUpdateContentsRect);
328 void resetContentsRect();
330 bool isPaintDestinationForDescendantLayers() const;
332 bool shouldClipCompositedBounds() const;
334 bool hasTiledBackingFlatteningLayer() const { return (m_childContainmentLayer && m_usingTiledCacheLayer); }
335 GraphicsLayer* tileCacheFlatteningLayer() const { return m_usingTiledCacheLayer ? m_childContainmentLayer.get() : nullptr; }
337 void paintIntoLayer(const GraphicsLayer*, GraphicsContext&, const IntRect& paintDirtyRect, PaintBehavior, GraphicsLayerPaintingPhase);
339 // Helper function for updateGeometry.
340 void adjustAncestorCompositingBoundsForFlowThread(LayoutRect& ancestorCompositingBounds, const RenderLayer* compositingAncestor) const;
342 static CSSPropertyID graphicsLayerToCSSProperty(AnimatedPropertyID);
343 static AnimatedPropertyID cssToGraphicsLayerProperty(CSSPropertyID);
345 bool canIssueSetNeedsDisplay() const { return !paintsIntoWindow() && !paintsIntoCompositedAncestor(); }
347 RenderLayer& m_owningLayer;
349 std::unique_ptr<GraphicsLayer> m_ancestorClippingLayer; // Only used if we are clipped by an ancestor which is not a stacking context.
350 std::unique_ptr<GraphicsLayer> m_contentsContainmentLayer; // Only used if we have a background layer; takes the transform.
351 std::unique_ptr<GraphicsLayer> m_graphicsLayer;
352 std::unique_ptr<GraphicsLayer> m_foregroundLayer; // Only used in cases where we need to draw the foreground separately.
353 std::unique_ptr<GraphicsLayer> m_backgroundLayer; // Only used in cases where we need to draw the background separately.
354 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.
355 std::unique_ptr<GraphicsLayer> m_maskLayer; // Only used if we have a mask and/or clip-path.
356 std::unique_ptr<GraphicsLayer> m_childClippingMaskLayer; // Only used if we have to clip child layers or accelerated contents with border radius or clip-path.
358 std::unique_ptr<GraphicsLayer> m_layerForHorizontalScrollbar;
359 std::unique_ptr<GraphicsLayer> m_layerForVerticalScrollbar;
360 std::unique_ptr<GraphicsLayer> m_layerForScrollCorner;
362 std::unique_ptr<GraphicsLayer> m_scrollingLayer; // Only used if the layer is using composited scrolling.
363 std::unique_ptr<GraphicsLayer> m_scrollingContentsLayer; // Only used if the layer is using composited scrolling.
365 ScrollingNodeID m_viewportConstrainedNodeID;
366 ScrollingNodeID m_scrollingNodeID;
368 LayoutRect m_compositedBounds;
369 LayoutSize m_devicePixelFractionFromRenderer;
371 bool m_artificiallyInflatedBounds; // bounds had to be made non-zero to make transform-origin work
372 bool m_isMainFrameRenderViewLayer;
373 bool m_usingTiledCacheLayer;
374 bool m_requiresOwnBackingStore;
375 bool m_canCompositeFilters;
376 #if ENABLE(FILTERS_LEVEL_2)
377 bool m_canCompositeBackdropFilters;
379 bool m_backgroundLayerPaintsFixedRootBackground;
382 enum CanvasCompositingStrategy {
384 CanvasPaintedToLayer,
385 CanvasAsLayerContents
387 CanvasCompositingStrategy canvasCompositingStrategy(const RenderObject&);
389 } // namespace WebCore
391 #endif // RenderLayerBacking_h