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();
161 void updateDebugIndicators(bool showBorder, bool showRepaintCounter);
163 // GraphicsLayerClient interface
164 virtual bool shouldUseTiledBacking(const GraphicsLayer*) const override;
165 virtual void tiledBackingUsageChanged(const GraphicsLayer*, bool /*usingTiledBacking*/) override;
166 virtual void notifyAnimationStarted(const GraphicsLayer*, double startTime) override;
167 virtual void notifyFlushRequired(const GraphicsLayer*) override;
168 virtual void notifyFlushBeforeDisplayRefresh(const GraphicsLayer*) override;
170 virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect& clip) override;
172 virtual float deviceScaleFactor() const override;
173 virtual float contentsScaleMultiplierForNewTiles(const GraphicsLayer*) const override;
175 virtual float pageScaleFactor() const override;
176 virtual void didCommitChangesForLayer(const GraphicsLayer*) const override;
177 virtual bool getCurrentTransform(const GraphicsLayer*, TransformationMatrix&) const override;
179 virtual bool isTrackingRepaints() const override;
180 virtual bool shouldSkipLayerInDump(const GraphicsLayer*) const override;
181 virtual bool shouldDumpPropertyForLayer(const GraphicsLayer*, const char* propertyName) const override;
184 virtual void verifyNotPainting();
187 LayoutRect contentsBox() const;
188 IntRect backgroundBox() const;
190 // For informative purposes only.
191 CompositingLayerType compositingLayerType() const;
193 GraphicsLayer* layerForHorizontalScrollbar() const { return m_layerForHorizontalScrollbar.get(); }
194 GraphicsLayer* layerForVerticalScrollbar() const { return m_layerForVerticalScrollbar.get(); }
195 GraphicsLayer* layerForScrollCorner() const { return m_layerForScrollCorner.get(); }
197 #if ENABLE(CSS_FILTERS)
198 bool canCompositeFilters() const { return m_canCompositeFilters; }
201 // Return an estimate of the backing store area (in pixels) allocated by this object's GraphicsLayers.
202 double backingStoreMemoryEstimate() const;
204 bool didSwitchToFullTileCoverageDuringLoading() const { return m_didSwitchToFullTileCoverageDuringLoading; }
205 void setDidSwitchToFullTileCoverageDuringLoading() { m_didSwitchToFullTileCoverageDuringLoading = true; }
207 #if ENABLE(CSS_COMPOSITING)
208 void setBlendMode(BlendMode);
212 void createPrimaryGraphicsLayer();
213 void destroyGraphicsLayers();
215 void willDestroyLayer(const GraphicsLayer*);
217 LayoutRect compositedBoundsIncludingMargin() const;
219 std::unique_ptr<GraphicsLayer> createGraphicsLayer(const String&);
221 RenderLayerModelObject& renderer() const { return m_owningLayer.renderer(); }
222 RenderLayerCompositor& compositor() const { return m_owningLayer.compositor(); }
224 void updateInternalHierarchy();
225 bool updateAncestorClippingLayer(bool needsAncestorClip);
226 bool updateDescendantClippingLayer(bool needsDescendantClip);
227 bool updateOverflowControlsLayers(bool needsHorizontalScrollbarLayer, bool needsVerticalScrollbarLayer, bool needsScrollCornerLayer);
228 bool updateForegroundLayer(bool needsForegroundLayer);
229 bool updateBackgroundLayer(bool needsBackgroundLayer);
230 bool updateMaskLayer(bool needsMaskLayer);
231 bool requiresHorizontalScrollbarLayer() const;
232 bool requiresVerticalScrollbarLayer() const;
233 bool requiresScrollCornerLayer() const;
234 bool updateScrollingLayers(bool scrollingLayers);
235 void updateDrawsContent(bool isSimpleContainer);
236 void registerScrollingLayers();
238 void updateRootLayerConfiguration();
240 void setBackgroundLayerPaintsFixedRootBackground(bool);
242 GraphicsLayerPaintingPhase paintingPhaseForPrimaryLayer() const;
244 LayoutSize contentOffsetInCompostingLayer() const;
245 // Result is transform origin in pixels.
246 FloatPoint3D computeTransformOrigin(const IntRect& borderBox) const;
247 // Result is perspective origin in pixels.
248 FloatPoint computePerspectiveOrigin(const IntRect& borderBox) const;
250 void updateOpacity(const RenderStyle*);
251 void updateTransform(const RenderStyle*);
252 #if ENABLE(CSS_FILTERS)
253 void updateFilters(const RenderStyle*);
255 #if ENABLE(CSS_COMPOSITING)
256 void updateBlendMode(const RenderStyle*);
258 // Return the opacity value that this layer should use for compositing.
259 float compositingOpacity(float rendererOpacity) const;
261 bool isMainFrameRenderViewLayer() const;
263 bool paintsBoxDecorations() const;
264 bool paintsChildren() const;
266 // Returns true if this compositing layer has no visible content.
267 bool isSimpleContainerCompositingLayer() const;
268 // Returns true if this layer has content that needs to be rendered by painting into the backing store.
269 bool containsPaintedContent(bool isSimpleContainer) const;
270 // Returns true if the RenderLayer just contains an image that we can composite directly.
271 bool isDirectlyCompositedImage() const;
272 void updateImageContents();
274 Color rendererBackgroundColor() const;
275 void updateDirectlyCompositedBackgroundColor(bool isSimpleContainer, bool& didUpdateContentsRect);
276 void updateDirectlyCompositedBackgroundImage(bool isSimpleContainer, bool& didUpdateContentsRect);
277 void updateDirectlyCompositedContents(bool isSimpleContainer, bool& didUpdateContentsRect);
279 void resetContentsRect();
281 bool hasVisibleNonCompositingDescendantLayers() const;
283 bool shouldClipCompositedBounds() const;
285 bool hasTiledBackingFlatteningLayer() const { return (m_childContainmentLayer && m_usingTiledCacheLayer); }
286 GraphicsLayer* tileCacheFlatteningLayer() const { return m_usingTiledCacheLayer ? m_childContainmentLayer.get() : 0; }
288 void paintIntoLayer(const GraphicsLayer*, GraphicsContext*, const IntRect& paintDirtyRect, PaintBehavior, GraphicsLayerPaintingPhase);
290 // Helper function for updateGraphicsLayerGeometry.
291 void adjustAncestorCompositingBoundsForFlowThread(IntRect& ancestorCompositingBounds, const RenderLayer* compositingAncestor) const;
293 static CSSPropertyID graphicsLayerToCSSProperty(AnimatedPropertyID);
294 static AnimatedPropertyID cssToGraphicsLayerProperty(CSSPropertyID);
296 RenderLayer& m_owningLayer;
298 std::unique_ptr<GraphicsLayer> m_ancestorClippingLayer; // Only used if we are clipped by an ancestor which is not a stacking context.
299 std::unique_ptr<GraphicsLayer> m_contentsContainmentLayer; // Only used if we have a background layer; takes the transform.
300 std::unique_ptr<GraphicsLayer> m_graphicsLayer;
301 std::unique_ptr<GraphicsLayer> m_foregroundLayer; // Only used in cases where we need to draw the foreground separately.
302 std::unique_ptr<GraphicsLayer> m_backgroundLayer; // Only used in cases where we need to draw the background separately.
303 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.
304 std::unique_ptr<GraphicsLayer> m_maskLayer; // Only used if we have a mask.
306 std::unique_ptr<GraphicsLayer> m_layerForHorizontalScrollbar;
307 std::unique_ptr<GraphicsLayer> m_layerForVerticalScrollbar;
308 std::unique_ptr<GraphicsLayer> m_layerForScrollCorner;
310 std::unique_ptr<GraphicsLayer> m_scrollingLayer; // Only used if the layer is using composited scrolling.
311 std::unique_ptr<GraphicsLayer> m_scrollingContentsLayer; // Only used if the layer is using composited scrolling.
313 uint64_t m_scrollLayerID;
315 LayoutRect m_compositedBounds;
316 LayoutSize m_subpixelAccumulation; // The accumulated subpixel offset of the compositedBounds compared to absolute coordinates.
318 bool m_artificiallyInflatedBounds; // bounds had to be made non-zero to make transform-origin work
319 bool m_isMainFrameRenderViewLayer;
320 bool m_usingTiledCacheLayer;
321 bool m_requiresOwnBackingStore;
322 #if ENABLE(CSS_FILTERS)
323 bool m_canCompositeFilters;
325 bool m_backgroundLayerPaintsFixedRootBackground;
326 bool m_didSwitchToFullTileCoverageDuringLoading;
328 static bool m_creatingPrimaryGraphicsLayer;
331 enum CanvasCompositingStrategy {
333 CanvasPaintedToLayer,
334 CanvasAsLayerContents
336 CanvasCompositingStrategy canvasCompositingStrategy(const RenderObject&);
338 } // namespace WebCore
340 #endif // USE(ACCELERATED_COMPOSITING)
342 #endif // RenderLayerBacking_h