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.
28 #include "FloatPoint.h"
29 #include "FloatPoint3D.h"
30 #include "GraphicsLayer.h"
31 #include "GraphicsLayerClient.h"
32 #include "RenderLayer.h"
33 #include "ScrollingCoordinator.h"
38 class PaintedContentsInfo;
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 class UpdateAfterLayoutFlags {
69 NeedsFullRepaint = 1 << 0,
72 void updateAfterLayout(OptionSet<UpdateAfterLayoutFlags>);
74 // Returns true if layer configuration changed.
75 bool updateConfiguration();
77 // Update graphics layer position and bounds.
78 void updateGeometry();
80 // Update state the requires that descendant layers have been updated.
81 void updateAfterDescendants();
83 // Update contents and clipping structure.
84 void updateDrawsContent();
86 GraphicsLayer* graphicsLayer() const { return m_graphicsLayer.get(); }
88 // Layer to clip children
89 bool hasClippingLayer() const { return (m_childContainmentLayer && !m_isFrameLayerWithTiledBacking); }
90 GraphicsLayer* clippingLayer() const { return !m_isFrameLayerWithTiledBacking ? m_childContainmentLayer.get() : nullptr; }
92 // Layer to get clipped by ancestor
93 bool hasAncestorClippingLayer() const { return m_ancestorClippingLayer != nullptr; }
94 GraphicsLayer* ancestorClippingLayer() const { return m_ancestorClippingLayer.get(); }
96 GraphicsLayer* contentsContainmentLayer() const { return m_contentsContainmentLayer.get(); }
98 GraphicsLayer* foregroundLayer() const { return m_foregroundLayer.get(); }
99 GraphicsLayer* backgroundLayer() const { return m_backgroundLayer.get(); }
100 bool backgroundLayerPaintsFixedRootBackground() const { return m_backgroundLayerPaintsFixedRootBackground; }
102 bool requiresBackgroundLayer() const { return m_requiresBackgroundLayer; }
103 void setRequiresBackgroundLayer(bool);
105 bool hasScrollingLayer() const { return m_scrollingLayer != nullptr; }
106 GraphicsLayer* scrollingLayer() const { return m_scrollingLayer.get(); }
107 GraphicsLayer* scrollingContentsLayer() const { return m_scrollingContentsLayer.get(); }
109 void detachFromScrollingCoordinator(OptionSet<LayerScrollCoordinationRole>);
111 ScrollingNodeID scrollingNodeIDForRole(LayerScrollCoordinationRole role) const
115 return m_scrollingNodeID;
116 case ViewportConstrained:
117 return m_viewportConstrainedNodeID;
122 void setScrollingNodeIDForRole(ScrollingNodeID nodeID, LayerScrollCoordinationRole role)
126 m_scrollingNodeID = nodeID;
128 case ViewportConstrained:
129 m_viewportConstrainedNodeID = nodeID;
130 setIsScrollCoordinatedWithViewportConstrainedRole(nodeID);
135 ScrollingNodeID scrollingNodeIDForChildren() const { return m_scrollingNodeID ? m_scrollingNodeID : m_viewportConstrainedNodeID; }
137 void setIsScrollCoordinatedWithViewportConstrainedRole(bool);
139 bool hasMaskLayer() const { return m_maskLayer != 0; }
140 bool hasChildClippingMaskLayer() const { return m_childClippingMaskLayer != nullptr; }
142 GraphicsLayer* parentForSublayers() const;
143 GraphicsLayer* childForSuperlayers() const;
145 // RenderLayers with backing normally short-circuit paintLayer() because
146 // their content is rendered via callbacks from GraphicsLayer. However, the document
147 // layer is special, because it has a GraphicsLayer to act as a container for the GraphicsLayers
148 // for descendants, but its contents usually render into the window (in which case this returns true).
149 // This returns false for other layers, and when the document layer actually needs to paint into its backing store
151 bool paintsIntoWindow() const;
153 // Returns true for a composited layer that has no backing store of its own, so
154 // paints into some ancestor layer.
155 bool paintsIntoCompositedAncestor() const { return !m_requiresOwnBackingStore; }
157 void setRequiresOwnBackingStore(bool);
159 void setContentsNeedDisplay(GraphicsLayer::ShouldClipToLayer = GraphicsLayer::ClipToLayer);
160 // r is in the coordinate space of the layer's render object
161 void setContentsNeedDisplayInRect(const LayoutRect&, GraphicsLayer::ShouldClipToLayer = GraphicsLayer::ClipToLayer);
163 // Notification from the renderer that its content changed.
164 void contentChanged(ContentChangeType);
166 // Interface to start, finish, suspend and resume animations and transitions
167 bool startTransition(double, CSSPropertyID, const RenderStyle* fromStyle, const RenderStyle* toStyle);
168 void transitionPaused(double timeOffset, CSSPropertyID);
169 void transitionFinished(CSSPropertyID);
171 bool startAnimation(double timeOffset, const Animation* anim, const KeyframeList& keyframes);
172 void animationPaused(double timeOffset, const String& name);
173 void animationSeeked(double timeOffset, const String& name);
174 void animationFinished(const String& name);
176 void suspendAnimations(MonotonicTime = MonotonicTime());
177 void resumeAnimations();
179 LayoutRect compositedBounds() const;
180 void setCompositedBounds(const LayoutRect&);
181 void updateCompositedBounds();
183 void updateAfterWidgetResize();
184 void positionOverflowControlsLayers();
185 bool hasUnpositionedOverflowControlsLayers() const;
187 bool isFrameLayerWithTiledBacking() const { return m_isFrameLayerWithTiledBacking; }
189 WEBCORE_EXPORT TiledBacking* tiledBacking() const;
190 void adjustTiledBackingCoverage();
191 void setTiledBackingHasMargins(bool hasExtendedBackgroundOnLeftAndRight, bool hasExtendedBackgroundOnTopAndBottom);
193 void updateDebugIndicators(bool showBorder, bool showRepaintCounter);
194 bool paintsSubpixelAntialiasedText() const { return m_paintsSubpixelAntialiasedText; }
196 // GraphicsLayerClient interface
197 void tiledBackingUsageChanged(const GraphicsLayer*, bool /*usingTiledBacking*/) override;
198 void notifyAnimationStarted(const GraphicsLayer*, const String& animationKey, MonotonicTime startTime) override;
199 void notifyFlushRequired(const GraphicsLayer*) override;
200 void notifyFlushBeforeDisplayRefresh(const GraphicsLayer*) override;
202 void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const FloatRect& clip, GraphicsLayerPaintBehavior) override;
204 float deviceScaleFactor() const override;
205 float contentsScaleMultiplierForNewTiles(const GraphicsLayer*) const override;
207 bool paintsOpaquelyAtNonIntegralScales(const GraphicsLayer*) const override;
209 float pageScaleFactor() const override;
210 float zoomedOutPageScaleFactor() const override;
211 void didCommitChangesForLayer(const GraphicsLayer*) const override;
212 bool getCurrentTransform(const GraphicsLayer*, TransformationMatrix&) const override;
214 bool isTrackingRepaints() const override;
215 bool shouldSkipLayerInDump(const GraphicsLayer*, LayerTreeAsTextBehavior) const override;
216 bool shouldDumpPropertyForLayer(const GraphicsLayer*, const char* propertyName) const override;
218 bool shouldAggressivelyRetainTiles(const GraphicsLayer*) const override;
219 bool shouldTemporarilyRetainTileCohorts(const GraphicsLayer*) const override;
220 bool useGiantTiles() const override;
221 void logFilledVisibleFreshTile(unsigned) override;
222 bool needsPixelAligment() const override { return !m_isMainFrameRenderViewLayer; }
224 LayoutSize subpixelOffsetFromRenderer() const { return m_subpixelOffsetFromRenderer; }
227 bool needsIOSDumpRenderTreeMainFrameRenderViewLayerIsAlwaysOpaqueHack(const GraphicsLayer&) const override;
231 void verifyNotPainting() override;
234 WEBCORE_EXPORT LayoutRect contentsBox() const;
236 // For informative purposes only.
237 WEBCORE_EXPORT CompositingLayerType compositingLayerType() const;
239 GraphicsLayer* layerForHorizontalScrollbar() const { return m_layerForHorizontalScrollbar.get(); }
240 GraphicsLayer* layerForVerticalScrollbar() const { return m_layerForVerticalScrollbar.get(); }
241 GraphicsLayer* layerForScrollCorner() const { return m_layerForScrollCorner.get(); }
243 bool canCompositeFilters() const { return m_canCompositeFilters; }
244 #if ENABLE(FILTERS_LEVEL_2)
245 bool canCompositeBackdropFilters() const { return m_canCompositeBackdropFilters; }
248 // Return an estimate of the backing store area (in pixels) allocated by this object's GraphicsLayers.
249 WEBCORE_EXPORT double backingStoreMemoryEstimate() const;
252 WEBCORE_EXPORT void setUsesDisplayListDrawing(bool);
253 WEBCORE_EXPORT String displayListAsText(DisplayList::AsTextFlags) const;
255 WEBCORE_EXPORT void setIsTrackingDisplayListReplay(bool);
256 WEBCORE_EXPORT String replayDisplayListAsText(DisplayList::AsTextFlags) const;
259 friend class PaintedContentsInfo;
261 FloatRect backgroundBoxForSimpleContainerPainting() const;
263 void createPrimaryGraphicsLayer();
264 void destroyGraphicsLayers();
266 void willDestroyLayer(const GraphicsLayer*);
268 LayoutRect compositedBoundsIncludingMargin() const;
270 std::unique_ptr<GraphicsLayer> createGraphicsLayer(const String&, GraphicsLayer::Type = GraphicsLayer::Type::Normal);
272 RenderLayerModelObject& renderer() const { return m_owningLayer.renderer(); }
273 RenderBox* renderBox() const { return m_owningLayer.renderBox(); }
274 RenderLayerCompositor& compositor() const { return m_owningLayer.compositor(); }
276 void updateInternalHierarchy();
277 bool updateAncestorClippingLayer(bool needsAncestorClip);
278 bool updateDescendantClippingLayer(bool needsDescendantClip);
279 bool updateOverflowControlsLayers(bool needsHorizontalScrollbarLayer, bool needsVerticalScrollbarLayer, bool needsScrollCornerLayer);
280 bool updateForegroundLayer(bool needsForegroundLayer);
281 bool updateBackgroundLayer(bool needsBackgroundLayer);
282 void updateMaskingLayer(bool hasMask, bool hasClipPath);
283 bool requiresHorizontalScrollbarLayer() const;
284 bool requiresVerticalScrollbarLayer() const;
285 bool requiresScrollCornerLayer() const;
286 bool updateScrollingLayers(bool scrollingLayers);
288 void updateChildClippingStrategy(bool needsDescendantsClippingLayer);
290 void updateMaskingLayerGeometry();
292 void updateRootLayerConfiguration();
294 void setBackgroundLayerPaintsFixedRootBackground(bool);
296 GraphicsLayerPaintingPhase paintingPhaseForPrimaryLayer() const;
298 LayoutSize contentOffsetInCompostingLayer() const;
299 // Result is transform origin in device pixels.
300 FloatPoint3D computeTransformOriginForPainting(const LayoutRect& borderBox) const;
302 void updateOpacity(const RenderStyle&);
303 void updateTransform(const RenderStyle&);
304 void updateFilters(const RenderStyle&);
305 #if ENABLE(FILTERS_LEVEL_2)
306 void updateBackdropFilters(const RenderStyle&);
307 void updateBackdropFiltersGeometry();
309 #if ENABLE(CSS_COMPOSITING)
310 void updateBlendMode(const RenderStyle&);
312 void updateCustomAppearance(const RenderStyle&);
314 // Return the opacity value that this layer should use for compositing.
315 float compositingOpacity(float rendererOpacity) const;
316 Color rendererBackgroundColor() const;
318 bool isMainFrameRenderViewLayer() const;
320 bool paintsBoxDecorations() const;
321 bool paintsContent(RenderLayer::PaintedContentRequest&) const;
323 void updateDrawsContent(PaintedContentsInfo&);
325 // Returns true if this compositing layer has no visible content.
326 bool isSimpleContainerCompositingLayer(PaintedContentsInfo&) const;
327 // Returns true if this layer has content that needs to be rendered by painting into the backing store.
328 bool containsPaintedContent(PaintedContentsInfo&) const;
329 // Returns true if the RenderLayer just contains an image that we can composite directly.
330 bool isDirectlyCompositedImage() const;
331 void updateImageContents(PaintedContentsInfo&);
333 void updateDirectlyCompositedBoxDecorations(PaintedContentsInfo&, bool& didUpdateContentsRect);
334 void updateDirectlyCompositedBackgroundColor(PaintedContentsInfo&, bool& didUpdateContentsRect);
335 void updateDirectlyCompositedBackgroundImage(PaintedContentsInfo&, bool& didUpdateContentsRect);
337 void resetContentsRect();
339 bool isPaintDestinationForDescendantLayers(RenderLayer::PaintedContentRequest&) const;
340 bool hasVisibleNonCompositedDescendants() const;
342 bool shouldClipCompositedBounds() const;
344 bool hasTiledBackingFlatteningLayer() const { return (m_childContainmentLayer && m_isFrameLayerWithTiledBacking); }
345 GraphicsLayer* tileCacheFlatteningLayer() const { return m_isFrameLayerWithTiledBacking ? m_childContainmentLayer.get() : nullptr; }
347 void paintIntoLayer(const GraphicsLayer*, GraphicsContext&, const IntRect& paintDirtyRect, OptionSet<PaintBehavior>, GraphicsLayerPaintingPhase);
349 static CSSPropertyID graphicsLayerToCSSProperty(AnimatedPropertyID);
350 static AnimatedPropertyID cssToGraphicsLayerProperty(CSSPropertyID);
352 bool canIssueSetNeedsDisplay() const { return !paintsIntoWindow() && !paintsIntoCompositedAncestor(); }
353 LayoutRect computeParentGraphicsLayerRect(RenderLayer* compositedAncestor, LayoutSize& ancestorClippingLayerOffset) const;
354 LayoutRect computePrimaryGraphicsLayerRect(const LayoutRect& parentGraphicsLayerRect) const;
356 RenderLayer& m_owningLayer;
358 std::unique_ptr<GraphicsLayer> m_ancestorClippingLayer; // Only used if we are clipped by an ancestor which is not a stacking context.
359 std::unique_ptr<GraphicsLayer> m_contentsContainmentLayer; // Only used if we have a background layer; takes the transform.
360 std::unique_ptr<GraphicsLayer> m_graphicsLayer;
361 std::unique_ptr<GraphicsLayer> m_foregroundLayer; // Only used in cases where we need to draw the foreground separately.
362 std::unique_ptr<GraphicsLayer> m_backgroundLayer; // Only used in cases where we need to draw the background separately.
363 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.
364 std::unique_ptr<GraphicsLayer> m_maskLayer; // Only used if we have a mask and/or clip-path.
365 std::unique_ptr<GraphicsLayer> m_childClippingMaskLayer; // Only used if we have to clip child layers or accelerated contents with border radius or clip-path.
367 std::unique_ptr<GraphicsLayer> m_layerForHorizontalScrollbar;
368 std::unique_ptr<GraphicsLayer> m_layerForVerticalScrollbar;
369 std::unique_ptr<GraphicsLayer> m_layerForScrollCorner;
371 std::unique_ptr<GraphicsLayer> m_scrollingLayer; // Only used if the layer is using composited scrolling.
372 std::unique_ptr<GraphicsLayer> m_scrollingContentsLayer; // Only used if the layer is using composited scrolling.
374 LayoutRect m_compositedBounds;
375 LayoutSize m_subpixelOffsetFromRenderer; // This is the subpixel distance between the primary graphics layer and the associated renderer's bounds.
376 LayoutSize m_compositedBoundsOffsetFromGraphicsLayer; // This is the subpixel distance between the primary graphics layer and the render layer bounds.
378 ScrollingNodeID m_viewportConstrainedNodeID { 0 };
379 ScrollingNodeID m_scrollingNodeID { 0 };
381 bool m_artificiallyInflatedBounds { false }; // bounds had to be made non-zero to make transform-origin work
382 bool m_isMainFrameRenderViewLayer { false };
383 bool m_isFrameLayerWithTiledBacking { false };
384 bool m_requiresOwnBackingStore { true };
385 bool m_canCompositeFilters { false };
386 #if ENABLE(FILTERS_LEVEL_2)
387 bool m_canCompositeBackdropFilters { false };
389 bool m_backgroundLayerPaintsFixedRootBackground { false };
390 bool m_requiresBackgroundLayer { false };
391 bool m_paintsSubpixelAntialiasedText { false }; // This is for logging only.
394 enum CanvasCompositingStrategy {
396 CanvasPaintedToLayer,
397 CanvasAsLayerContents
399 CanvasCompositingStrategy canvasCompositingStrategy(const RenderObject&);
401 WTF::TextStream& operator<<(WTF::TextStream&, const RenderLayerBacking&);
403 } // namespace WebCore