2 * Copyright (C) 2009, 2010, 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef RenderLayerBacking_h
27 #define RenderLayerBacking_h
29 #if USE(ACCELERATED_COMPOSITING)
31 #include "FloatPoint.h"
32 #include "FloatPoint3D.h"
33 #include "GraphicsLayer.h"
34 #include "GraphicsLayerClient.h"
35 #include "RenderLayer.h"
40 class RenderLayerCompositor;
42 class TransformationMatrix;
44 enum CompositingLayerType {
45 NormalCompositingLayer, // non-tiled layer with backing store
46 TiledCompositingLayer, // tiled layer (always has backing store)
47 MediaCompositingLayer, // layer that contains an image, video, webGL or plugin
48 ContainerCompositingLayer // layer with no backing store
51 // RenderLayerBacking controls the compositing behavior for a single RenderLayer.
52 // It holds the various GraphicsLayers, and makes decisions about intra-layer rendering
55 // There is one RenderLayerBacking for each RenderLayer that is composited.
57 class RenderLayerBacking : public GraphicsLayerClient {
58 WTF_MAKE_NONCOPYABLE(RenderLayerBacking); WTF_MAKE_FAST_ALLOCATED;
60 explicit RenderLayerBacking(RenderLayer&);
61 ~RenderLayerBacking();
64 void layerWillBeDestroyed();
67 RenderLayer& owningLayer() const { return m_owningLayer; }
69 enum UpdateAfterLayoutFlag {
70 CompositingChildrenOnly = 1 << 0,
71 NeedsFullRepaint = 1 << 1,
74 typedef unsigned UpdateAfterLayoutFlags;
75 void updateAfterLayout(UpdateAfterLayoutFlags);
77 // Returns true if layer configuration changed.
78 bool updateGraphicsLayerConfiguration();
79 // Update graphics layer position and bounds.
80 void updateGraphicsLayerGeometry(); // make private
81 // Update contents and clipping structure.
82 void updateDrawsContent();
84 GraphicsLayer* graphicsLayer() const { return m_graphicsLayer.get(); }
86 // Layer to clip children
87 bool hasClippingLayer() const { return (m_childContainmentLayer && !m_usingTiledCacheLayer); }
88 GraphicsLayer* clippingLayer() const { return !m_usingTiledCacheLayer ? m_childContainmentLayer.get() : 0; }
90 // Layer to get clipped by ancestor
91 bool hasAncestorClippingLayer() const { return m_ancestorClippingLayer != 0; }
92 GraphicsLayer* ancestorClippingLayer() const { return m_ancestorClippingLayer.get(); }
94 GraphicsLayer* contentsContainmentLayer() const { return m_contentsContainmentLayer.get(); }
96 bool hasContentsLayer() const { return m_foregroundLayer != 0; }
97 GraphicsLayer* foregroundLayer() const { return m_foregroundLayer.get(); }
99 GraphicsLayer* backgroundLayer() const { return m_backgroundLayer.get(); }
100 bool backgroundLayerPaintsFixedRootBackground() const { return m_backgroundLayerPaintsFixedRootBackground; }
102 bool hasScrollingLayer() const { return m_scrollingLayer != nullptr; }
103 GraphicsLayer* scrollingLayer() const { return m_scrollingLayer.get(); }
104 GraphicsLayer* scrollingContentsLayer() const { return m_scrollingContentsLayer.get(); }
106 void attachToScrollingCoordinatorWithParent(RenderLayerBacking* parent);
107 void detachFromScrollingCoordinator();
108 uint64_t scrollLayerID() const { return m_scrollLayerID; }
110 bool hasMaskLayer() const { return m_maskLayer != 0; }
112 GraphicsLayer* parentForSublayers() const;
113 GraphicsLayer* childForSuperlayers() const;
115 // RenderLayers with backing normally short-circuit paintLayer() because
116 // their content is rendered via callbacks from GraphicsLayer. However, the document
117 // layer is special, because it has a GraphicsLayer to act as a container for the GraphicsLayers
118 // for descendants, but its contents usually render into the window (in which case this returns true).
119 // This returns false for other layers, and when the document layer actually needs to paint into its backing store
121 bool paintsIntoWindow() const;
123 // Returns true for a composited layer that has no backing store of its own, so
124 // paints into some ancestor layer.
125 bool paintsIntoCompositedAncestor() const { return !m_requiresOwnBackingStore; }
127 void setRequiresOwnBackingStore(bool);
129 void setContentsNeedDisplay(GraphicsLayer::ShouldClipToLayer = GraphicsLayer::ClipToLayer);
130 // r is in the coordinate space of the layer's render object
131 void setContentsNeedDisplayInRect(const IntRect&, GraphicsLayer::ShouldClipToLayer = GraphicsLayer::ClipToLayer);
133 // Notification from the renderer that its content changed.
134 void contentChanged(ContentChangeType);
136 // Interface to start, finish, suspend and resume animations and transitions
137 bool startTransition(double, CSSPropertyID, const RenderStyle* fromStyle, const RenderStyle* toStyle);
138 void transitionPaused(double timeOffset, CSSPropertyID);
139 void transitionFinished(CSSPropertyID);
141 bool startAnimation(double timeOffset, const Animation* anim, const KeyframeList& keyframes);
142 void animationPaused(double timeOffset, const String& name);
143 void animationFinished(const String& name);
145 void suspendAnimations(double time = 0);
146 void resumeAnimations();
148 LayoutRect compositedBounds() const;
149 void setCompositedBounds(const LayoutRect&);
150 void updateCompositedBounds();
152 void updateAfterWidgetResize();
153 void positionOverflowControlsLayers();
154 bool hasUnpositionedOverflowControlsLayers() const;
156 bool usingTiledBacking() const { return m_usingTiledCacheLayer; }
157 bool tiledBackingHasMargin() const;
158 TiledBacking* tiledBacking() const;
159 void adjustTiledBackingCoverage();
160 void setTiledBackingHasMargins(bool);
162 void updateDebugIndicators(bool showBorder, bool showRepaintCounter);
164 // GraphicsLayerClient interface
165 virtual bool shouldUseTiledBacking(const GraphicsLayer*) const override;
166 virtual void tiledBackingUsageChanged(const GraphicsLayer*, bool /*usingTiledBacking*/) override;
167 virtual void notifyAnimationStarted(const GraphicsLayer*, double startTime) override;
168 virtual void notifyFlushRequired(const GraphicsLayer*) override;
169 virtual void notifyFlushBeforeDisplayRefresh(const GraphicsLayer*) override;
171 virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect& clip) override;
173 virtual float deviceScaleFactor() const override;
174 virtual float contentsScaleMultiplierForNewTiles(const GraphicsLayer*) const override;
176 virtual float pageScaleFactor() const override;
177 virtual void didCommitChangesForLayer(const GraphicsLayer*) const override;
178 virtual bool getCurrentTransform(const GraphicsLayer*, TransformationMatrix&) const override;
180 virtual bool isTrackingRepaints() const override;
181 virtual bool shouldSkipLayerInDump(const GraphicsLayer*) const override;
182 virtual bool shouldDumpPropertyForLayer(const GraphicsLayer*, const char* propertyName) const override;
185 virtual void verifyNotPainting();
188 LayoutRect contentsBox() const;
189 IntRect backgroundBox() const;
191 // For informative purposes only.
192 CompositingLayerType compositingLayerType() const;
194 GraphicsLayer* layerForHorizontalScrollbar() const { return m_layerForHorizontalScrollbar.get(); }
195 GraphicsLayer* layerForVerticalScrollbar() const { return m_layerForVerticalScrollbar.get(); }
196 GraphicsLayer* layerForScrollCorner() const { return m_layerForScrollCorner.get(); }
198 #if ENABLE(CSS_FILTERS)
199 bool canCompositeFilters() const { return m_canCompositeFilters; }
202 // Return an estimate of the backing store area (in pixels) allocated by this object's GraphicsLayers.
203 double backingStoreMemoryEstimate() const;
205 #if ENABLE(CSS_COMPOSITING)
206 void setBlendMode(BlendMode);
210 void createPrimaryGraphicsLayer();
211 void destroyGraphicsLayers();
213 void willDestroyLayer(const GraphicsLayer*);
215 LayoutRect compositedBoundsIncludingMargin() const;
217 std::unique_ptr<GraphicsLayer> createGraphicsLayer(const String&);
219 RenderLayerModelObject& renderer() const { return m_owningLayer.renderer(); }
220 RenderLayerCompositor& compositor() const { return m_owningLayer.compositor(); }
222 void updateInternalHierarchy();
223 bool updateAncestorClippingLayer(bool needsAncestorClip);
224 bool updateDescendantClippingLayer(bool needsDescendantClip);
225 bool updateOverflowControlsLayers(bool needsHorizontalScrollbarLayer, bool needsVerticalScrollbarLayer, bool needsScrollCornerLayer);
226 bool updateForegroundLayer(bool needsForegroundLayer);
227 bool updateBackgroundLayer(bool needsBackgroundLayer);
228 bool updateMaskLayer(bool needsMaskLayer);
229 bool requiresHorizontalScrollbarLayer() const;
230 bool requiresVerticalScrollbarLayer() const;
231 bool requiresScrollCornerLayer() const;
232 bool updateScrollingLayers(bool scrollingLayers);
233 void updateDrawsContent(bool isSimpleContainer);
234 void registerScrollingLayers();
236 void updateRootLayerConfiguration();
238 void setBackgroundLayerPaintsFixedRootBackground(bool);
240 GraphicsLayerPaintingPhase paintingPhaseForPrimaryLayer() const;
242 LayoutSize contentOffsetInCompostingLayer() const;
243 // Result is transform origin in pixels.
244 FloatPoint3D computeTransformOrigin(const IntRect& borderBox) const;
245 // Result is perspective origin in pixels.
246 FloatPoint computePerspectiveOrigin(const IntRect& borderBox) const;
248 void updateOpacity(const RenderStyle*);
249 void updateTransform(const RenderStyle*);
250 #if ENABLE(CSS_FILTERS)
251 void updateFilters(const RenderStyle*);
253 #if ENABLE(CSS_COMPOSITING)
254 void updateBlendMode(const RenderStyle*);
256 // Return the opacity value that this layer should use for compositing.
257 float compositingOpacity(float rendererOpacity) const;
259 bool isMainFrameRenderViewLayer() const;
261 bool paintsBoxDecorations() const;
262 bool paintsChildren() const;
264 // Returns true if this compositing layer has no visible content.
265 bool isSimpleContainerCompositingLayer() const;
266 // Returns true if this layer has content that needs to be rendered by painting into the backing store.
267 bool containsPaintedContent(bool isSimpleContainer) const;
268 // Returns true if the RenderLayer just contains an image that we can composite directly.
269 bool isDirectlyCompositedImage() const;
270 void updateImageContents();
272 Color rendererBackgroundColor() const;
273 void updateDirectlyCompositedBackgroundColor(bool isSimpleContainer, bool& didUpdateContentsRect);
274 void updateDirectlyCompositedBackgroundImage(bool isSimpleContainer, bool& didUpdateContentsRect);
275 void updateDirectlyCompositedContents(bool isSimpleContainer, bool& didUpdateContentsRect);
277 void resetContentsRect();
279 bool hasVisibleNonCompositingDescendantLayers() const;
281 bool shouldClipCompositedBounds() const;
283 bool hasTiledBackingFlatteningLayer() const { return (m_childContainmentLayer && m_usingTiledCacheLayer); }
284 GraphicsLayer* tileCacheFlatteningLayer() const { return m_usingTiledCacheLayer ? m_childContainmentLayer.get() : 0; }
286 void paintIntoLayer(const GraphicsLayer*, GraphicsContext*, const IntRect& paintDirtyRect, PaintBehavior, GraphicsLayerPaintingPhase);
288 // Helper function for updateGraphicsLayerGeometry.
289 void adjustAncestorCompositingBoundsForFlowThread(IntRect& ancestorCompositingBounds, const RenderLayer* compositingAncestor) const;
291 static CSSPropertyID graphicsLayerToCSSProperty(AnimatedPropertyID);
292 static AnimatedPropertyID cssToGraphicsLayerProperty(CSSPropertyID);
294 RenderLayer& m_owningLayer;
296 std::unique_ptr<GraphicsLayer> m_ancestorClippingLayer; // Only used if we are clipped by an ancestor which is not a stacking context.
297 std::unique_ptr<GraphicsLayer> m_contentsContainmentLayer; // Only used if we have a background layer; takes the transform.
298 std::unique_ptr<GraphicsLayer> m_graphicsLayer;
299 std::unique_ptr<GraphicsLayer> m_foregroundLayer; // Only used in cases where we need to draw the foreground separately.
300 std::unique_ptr<GraphicsLayer> m_backgroundLayer; // Only used in cases where we need to draw the background separately.
301 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.
302 std::unique_ptr<GraphicsLayer> m_maskLayer; // Only used if we have a mask.
304 std::unique_ptr<GraphicsLayer> m_layerForHorizontalScrollbar;
305 std::unique_ptr<GraphicsLayer> m_layerForVerticalScrollbar;
306 std::unique_ptr<GraphicsLayer> m_layerForScrollCorner;
308 std::unique_ptr<GraphicsLayer> m_scrollingLayer; // Only used if the layer is using composited scrolling.
309 std::unique_ptr<GraphicsLayer> m_scrollingContentsLayer; // Only used if the layer is using composited scrolling.
311 uint64_t m_scrollLayerID;
313 LayoutRect m_compositedBounds;
314 LayoutSize m_subpixelAccumulation; // The accumulated subpixel offset of the compositedBounds compared to absolute coordinates.
316 bool m_artificiallyInflatedBounds; // bounds had to be made non-zero to make transform-origin work
317 bool m_isMainFrameRenderViewLayer;
318 bool m_usingTiledCacheLayer;
319 bool m_requiresOwnBackingStore;
320 #if ENABLE(CSS_FILTERS)
321 bool m_canCompositeFilters;
323 bool m_backgroundLayerPaintsFixedRootBackground;
325 static bool m_creatingPrimaryGraphicsLayer;
328 enum CanvasCompositingStrategy {
330 CanvasPaintedToLayer,
331 CanvasAsLayerContents
333 CanvasCompositingStrategy canvasCompositingStrategy(const RenderObject&);
335 } // namespace WebCore
337 #endif // USE(ACCELERATED_COMPOSITING)
339 #endif // RenderLayerBacking_h