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 CompositedLayerMapping_h
27 #define CompositedLayerMapping_h
29 #include "core/rendering/RenderLayer.h"
30 #include "platform/geometry/FloatPoint.h"
31 #include "platform/geometry/FloatPoint3D.h"
32 #include "platform/graphics/GraphicsLayer.h"
33 #include "platform/graphics/GraphicsLayerClient.h"
34 #include "platform/transforms/TransformationMatrix.h"
39 class RenderLayerCompositor;
41 enum CompositingLayerType {
42 NormalCompositingLayer, // non-tiled layer with backing store
43 MediaCompositingLayer, // layer that contains an image, video, webGL or plugin
44 ContainerCompositingLayer // layer with no backing store
48 // A GraphicsLayerPaintInfo contains all the info needed to paint a partial subtree of RenderLayers into a GraphicsLayer.
49 struct GraphicsLayerPaintInfo {
50 RenderLayer* renderLayer;
52 LayoutRect compositedBounds;
54 // At first, the m_squashingLayer's bounds/location are not known. The value offsetFromSquashingCLM is
55 // an intermediate offset for a squashed RenderLayer, described with respect to the CompositedLayerMapping's
56 // owning layer that would eventually have the m_squashingLayer. Once the shared GraphicsLayer's bounds are
57 // known, then we can trivially convert this offset to m_squashingLayer's space.
58 IntSize offsetFromSquashingCLM;
60 // Offset describing where this squashed RenderLayer paints into the shared GraphicsLayer backing.
61 IntSize offsetFromRenderer;
63 GraphicsLayerPaintingPhase paintingPhase;
65 bool isBackgroundLayer;
67 bool isEquivalentForSquashing(const GraphicsLayerPaintInfo& other)
69 // FIXME: offsetFromRenderer and compositedBounds should not be checked here, because
70 // they are not yet fixed at the time this function is used.
71 return renderLayer == other.renderLayer
72 && offsetFromSquashingCLM == other.offsetFromSquashingCLM
73 && paintingPhase == other.paintingPhase
74 && isBackgroundLayer == other.isBackgroundLayer;
78 // CompositedLayerMapping keeps track of how RenderLayers of the render tree correspond to
79 // GraphicsLayers of the composited layer tree. Each instance of CompositedLayerMapping
80 // manages a small cluster of GraphicsLayers and the references to which RenderLayers
81 // and paint phases contribute to each GraphicsLayer.
83 // Currently (Oct. 2013) there is one CompositedLayerMapping for each RenderLayer,
84 // but this is likely to evolve soon.
85 class CompositedLayerMapping FINAL : public GraphicsLayerClient {
86 WTF_MAKE_NONCOPYABLE(CompositedLayerMapping); WTF_MAKE_FAST_ALLOCATED;
88 explicit CompositedLayerMapping(RenderLayer*);
89 virtual ~CompositedLayerMapping();
91 RenderLayer* owningLayer() const { return m_owningLayer; }
93 enum UpdateAfterLayoutFlag {
94 CompositingChildrenOnly = 1 << 0,
95 NeedsFullRepaint = 1 << 1,
98 typedef unsigned UpdateAfterLayoutFlags;
99 void updateAfterLayout(UpdateAfterLayoutFlags);
101 // Returns true if layer configuration changed.
102 bool updateGraphicsLayerConfiguration();
103 // Update graphics layer position and bounds.
104 void updateGraphicsLayerGeometry(); // make private
105 // Update whether layer needs blending.
106 void updateContentsOpaque();
108 GraphicsLayer* mainGraphicsLayer() const { return m_graphicsLayer.get(); }
110 // Layer to clip children
111 bool hasClippingLayer() const { return m_childContainmentLayer; }
112 GraphicsLayer* clippingLayer() const { return m_childContainmentLayer.get(); }
114 // Layer to get clipped by ancestor
115 bool hasAncestorClippingLayer() const { return m_ancestorClippingLayer; }
116 GraphicsLayer* ancestorClippingLayer() const { return m_ancestorClippingLayer.get(); }
118 bool hasContentsLayer() const { return m_foregroundLayer; }
119 GraphicsLayer* foregroundLayer() const { return m_foregroundLayer.get(); }
121 GraphicsLayer* backgroundLayer() const { return m_backgroundLayer.get(); }
122 bool backgroundLayerPaintsFixedRootBackground() const { return m_backgroundLayerPaintsFixedRootBackground; }
124 bool hasScrollingLayer() const { return m_scrollingLayer; }
125 GraphicsLayer* scrollingLayer() const { return m_scrollingLayer.get(); }
126 GraphicsLayer* scrollingContentsLayer() const { return m_scrollingContentsLayer.get(); }
128 bool hasMaskLayer() const { return m_maskLayer; }
129 GraphicsLayer* maskLayer() const { return m_maskLayer.get(); }
131 bool hasChildClippingMaskLayer() const { return m_childClippingMaskLayer; }
132 GraphicsLayer* childClippingMaskLayer() const { return m_childClippingMaskLayer.get(); }
134 GraphicsLayer* parentForSublayers() const;
135 GraphicsLayer* childForSuperlayers() const;
136 // localRootForOwningLayer does not include the m_squashingContainmentLayer, which is technically not associated with this CLM's owning layer.
137 GraphicsLayer* localRootForOwningLayer() const;
139 GraphicsLayer* childTransformLayer() const { return m_childTransformLayer.get(); }
141 GraphicsLayer* squashingContainmentLayer() const { return m_squashingContainmentLayer.get(); }
142 GraphicsLayer* squashingLayer() const { return m_squashingLayer.get(); }
143 // Contains the bottommost layer in the hierarchy that can contain the children transform.
144 GraphicsLayer* layerForChildrenTransform() const;
146 // Returns true for a composited layer that has no backing store of its own, so
147 // paints into some ancestor layer.
148 bool paintsIntoCompositedAncestor() const { return !(m_requiresOwnBackingStoreForAncestorReasons || m_requiresOwnBackingStoreForIntrinsicReasons); }
150 // Updates whether a backing store is needed based on the layer's compositing ancestor's
151 // properties; returns true if the need for a backing store for ancestor reasons changed.
152 bool updateRequiresOwnBackingStoreForAncestorReasons(const RenderLayer* compositingAncestor);
154 // Updates whether a backing store is needed for intrinsic reasons (that is, based on the
155 // layer's own properties or compositing reasons); returns true if the intrinsic need for
156 // a backing store changed.
157 bool updateRequiresOwnBackingStoreForIntrinsicReasons();
159 void setContentsNeedDisplay();
160 // r is in the coordinate space of the layer's render object
161 void setContentsNeedDisplayInRect(const IntRect&);
163 // Notification from the renderer that its content changed.
164 void contentChanged(ContentChangeType);
166 LayoutRect compositedBounds() const;
167 void setCompositedBounds(const LayoutRect&);
168 void updateCompositedBounds();
170 void updateAfterWidgetResize();
171 void positionOverflowControlsLayers(const IntSize& offsetFromRoot);
172 bool hasUnpositionedOverflowControlsLayers() const;
174 // Returns true if the assignment actually changed the assigned squashing layer.
175 bool updateSquashingLayerAssignment(RenderLayer*, IntSize offsetFromSquashingCLM, size_t nextSquashedLayerIndex);
176 void removeRenderLayerFromSquashingGraphicsLayer(const RenderLayer*);
178 void finishAccumulatingSquashingLayers(size_t nextSquashedLayerIndex);
179 void updateRenderingContext();
180 void updateShouldFlattenTransform();
182 // GraphicsLayerClient interface
183 virtual void notifyAnimationStarted(const GraphicsLayer*, double wallClockTime, double monotonicTime) OVERRIDE;
184 virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect& clip) OVERRIDE;
185 virtual bool isTrackingRepaints() const OVERRIDE;
187 PassOwnPtr<Vector<FloatRect> > collectTrackedRepaintRects() const;
190 virtual void verifyNotPainting() OVERRIDE;
193 LayoutRect contentsBox() const;
194 IntRect backgroundBox() const;
196 // For informative purposes only.
197 CompositingLayerType compositingLayerType() const;
199 GraphicsLayer* layerForHorizontalScrollbar() const { return m_layerForHorizontalScrollbar.get(); }
200 GraphicsLayer* layerForVerticalScrollbar() const { return m_layerForVerticalScrollbar.get(); }
201 GraphicsLayer* layerForScrollCorner() const { return m_layerForScrollCorner.get(); }
203 void updateFilters(const RenderStyle*);
204 bool canCompositeFilters() const { return m_canCompositeFilters; }
206 void setBlendMode(blink::WebBlendMode);
208 virtual String debugName(const GraphicsLayer*) OVERRIDE;
210 LayoutSize subpixelAccumulation() const { return m_subpixelAccumulation; }
212 void createPrimaryGraphicsLayer();
213 void destroyGraphicsLayers();
215 PassOwnPtr<GraphicsLayer> createGraphicsLayer(CompositingReasons);
216 bool toggleScrollbarLayerIfNeeded(OwnPtr<GraphicsLayer>&, bool needsLayer);
218 RenderLayerModelObject* renderer() const { return m_owningLayer->renderer(); }
219 RenderLayerCompositor* compositor() const { return m_owningLayer->compositor(); }
221 void updateInternalHierarchy();
222 bool updateClippingLayers(bool needsAncestorClip, bool needsDescendantClip);
223 bool updateChildTransformLayer(bool needsChildTransformLayer);
224 bool updateOverflowControlsLayers(bool needsHorizontalScrollbarLayer, bool needsVerticalScrollbarLayer, bool needsScrollCornerLayer);
225 bool updateForegroundLayer(bool needsForegroundLayer);
226 bool updateBackgroundLayer(bool needsBackgroundLayer);
227 bool updateMaskLayer(bool needsMaskLayer);
228 bool updateClippingMaskLayers(bool needsChildClippingMaskLayer);
229 bool requiresHorizontalScrollbarLayer() const { return m_owningLayer->scrollableArea() && m_owningLayer->scrollableArea()->horizontalScrollbar(); }
230 bool requiresVerticalScrollbarLayer() const { return m_owningLayer->scrollableArea() && m_owningLayer->scrollableArea()->verticalScrollbar(); }
231 bool requiresScrollCornerLayer() const { return m_owningLayer->scrollableArea() && !m_owningLayer->scrollableArea()->scrollCornerAndResizerRect().isEmpty(); }
232 bool updateScrollingLayers(bool scrollingLayers);
233 void updateScrollParent(RenderLayer*);
234 void updateClipParent(RenderLayer*);
235 bool updateSquashingLayers(bool needsSquashingLayers);
236 void updateDrawsContent(bool isSimpleContainer);
237 void updateChildrenTransform();
238 void registerScrollingLayers();
240 void adjustBoundsForSubPixelAccumulation(const RenderLayer* compositedAncestor, IntRect& localCompositingBounds, IntRect& relativeCompositingBounds, IntPoint& delta);
242 void setBackgroundLayerPaintsFixedRootBackground(bool);
244 GraphicsLayerPaintingPhase paintingPhaseForPrimaryLayer() const;
246 LayoutSize contentOffsetInCompostingLayer() const;
247 // Result is transform origin in pixels.
248 FloatPoint3D computeTransformOrigin(const IntRect& borderBox) const;
249 // Result is perspective origin in pixels.
250 FloatPoint computePerspectiveOrigin(const IntRect& borderBox) const;
252 void updateSquashingLayerGeometry(const IntPoint& delta);
254 void updateOpacity(const RenderStyle*);
255 void updateTransform(const RenderStyle*);
256 void updateLayerBlendMode(const RenderStyle*);
257 void updateIsRootForIsolatedGroup();
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 updateBackgroundColor(bool isSimpleContainer);
276 void updateContentsRect(bool isSimpleContainer);
278 void updateCompositingReasons();
280 bool hasVisibleNonCompositingDescendantLayers() const;
282 bool shouldClipCompositedBounds() const;
284 void paintsIntoCompositedAncestorChanged();
286 void doPaintTask(GraphicsLayerPaintInfo&, GraphicsContext*, const IntRect& clip);
288 RenderLayer* m_owningLayer;
290 // The hierarchy of layers that is maintained by the CompositedLayerMapping looks like this:
292 // + m_ancestorClippingLayer [OPTIONAL]
294 // + m_childContainmentLayer [OPTIONAL] <-OR-> m_scrollingLayer [OPTIONAL] <-OR-> m_childTransformLayer
295 // + m_scrollingContentsLayer [OPTIONAL]
297 // We need an ancestor clipping layer if our clipping ancestor is not our ancestor in the
298 // clipping tree. Here's what that might look like.
300 // Let A = the clipping ancestor,
301 // B = the clip descendant, and
302 // SC = the stacking context that is the ancestor of A and B in the stacking tree.
305 // + A = m_graphicsLayer
306 // | + m_childContainmentLayer
310 // + B = m_ancestorClippingLayer [+]
314 // In this case B is clipped by another layer that doesn't happen to be its ancestor: A.
315 // So we create an ancestor clipping layer for B, [+], which ensures that B is clipped
316 // as if it had been A's descendant.
317 OwnPtr<GraphicsLayer> m_ancestorClippingLayer; // Only used if we are clipped by an ancestor which is not a stacking context.
318 OwnPtr<GraphicsLayer> m_graphicsLayer;
319 OwnPtr<GraphicsLayer> m_childContainmentLayer; // Only used if we have clipping on a stacking context with compositing children.
320 OwnPtr<GraphicsLayer> m_childTransformLayer; // Only used if we have perspective and no m_childContainmentLayer.
321 OwnPtr<GraphicsLayer> m_scrollingLayer; // Only used if the layer is using composited scrolling.
322 OwnPtr<GraphicsLayer> m_scrollingContentsLayer; // Only used if the layer is using composited scrolling.
324 // This layer is also added to the hierarchy by the RLB, but in a different way than
325 // the layers above. It's added to m_graphicsLayer as its mask layer (naturally) if
326 // we have a mask, and isn't part of the typical hierarchy (it has no children).
327 OwnPtr<GraphicsLayer> m_maskLayer; // Only used if we have a mask.
328 OwnPtr<GraphicsLayer> m_childClippingMaskLayer; // Only used if we have to clip child layers or accelerated contents with border radius or clip-path.
330 // There are two other (optional) layers whose painting is managed by the CompositedLayerMapping,
331 // but whose position in the hierarchy is maintained by the RenderLayerCompositor. These
332 // are the foreground and background layers. The foreground layer exists if we have composited
333 // descendants with negative z-order. We need the extra layer in this case because the layer
334 // needs to draw both below (for the background, say) and above (for the normal flow content, say)
335 // the negative z-order descendants and this is impossible with a single layer. The RLC handles
336 // inserting m_foregroundLayer in the correct position in our descendant list for us (right after
337 // the neg z-order dsecendants).
339 // The background layer is only created if this is the root layer and our background is entirely
340 // fixed. In this case we want to put the background in a separate composited layer so that when
341 // we scroll, we don't have to re-raster the background into position. This layer is also inserted
342 // into the tree by the RLC as it gets a special home. This layer becomes a descendant of the
343 // frame clipping layer. That is:
345 // + frame clipping layer
346 // + m_backgroundLayer
347 // + frame scrolling layer
348 // + root content layer
350 // With the hierarchy set up like this, the root content layer is able to scroll without affecting
351 // the background layer (or repainting).
352 OwnPtr<GraphicsLayer> m_foregroundLayer; // Only used in cases where we need to draw the foreground separately.
353 OwnPtr<GraphicsLayer> m_backgroundLayer; // Only used in cases where we need to draw the background separately.
355 OwnPtr<GraphicsLayer> m_layerForHorizontalScrollbar;
356 OwnPtr<GraphicsLayer> m_layerForVerticalScrollbar;
357 OwnPtr<GraphicsLayer> m_layerForScrollCorner;
359 OwnPtr<GraphicsLayer> m_squashingContainmentLayer; // Only used if any squashed layers exist, to contain the squashed layers as siblings to the rest of the GraphicsLayer tree chunk.
360 OwnPtr<GraphicsLayer> m_squashingLayer; // Only used if any squashed layers exist, this is the backing that squashed layers paint into.
361 Vector<GraphicsLayerPaintInfo> m_squashedLayers;
363 LayoutRect m_compositedBounds;
364 LayoutSize m_subpixelAccumulation; // The accumulated subpixel offset of the compositedBounds compared to absolute coordinates.
366 bool m_artificiallyInflatedBounds; // bounds had to be made non-zero to make transform-origin work
367 bool m_isMainFrameRenderViewLayer;
368 bool m_requiresOwnBackingStoreForIntrinsicReasons;
369 bool m_requiresOwnBackingStoreForAncestorReasons;
370 bool m_canCompositeFilters;
371 bool m_backgroundLayerPaintsFixedRootBackground;
374 } // namespace WebCore
376 #endif // CompositedLayerMapping_h