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 "core/rendering/compositing/GraphicsLayerUpdater.h"
31 #include "platform/geometry/FloatPoint.h"
32 #include "platform/geometry/FloatPoint3D.h"
33 #include "platform/graphics/GraphicsLayer.h"
34 #include "platform/graphics/GraphicsLayerClient.h"
35 #include "platform/transforms/TransformationMatrix.h"
40 class RenderLayerCompositor;
42 // A GraphicsLayerPaintInfo contains all the info needed to paint a partial subtree of RenderLayers into a GraphicsLayer.
43 struct GraphicsLayerPaintInfo {
44 RenderLayer* renderLayer;
46 LayoutRect compositedBounds;
48 // At first, the m_squashingLayer's bounds/location are not known. The value offsetFromSquashingCLM is
49 // an intermediate offset for a squashed RenderLayer, described with respect to the CompositedLayerMapping's
50 // owning layer that would eventually have the m_squashingLayer. Once the shared GraphicsLayer's bounds are
51 // known, then we can trivially convert this offset to m_squashingLayer's space.
52 LayoutSize offsetFromSquashingCLM;
54 // The clip rect to apply, in the local coordinate space of the squashed layer, when painting it.
55 IntRect localClipRectForSquashedLayer;
57 // Offset describing where this squashed RenderLayer paints into the shared GraphicsLayer backing.
58 IntSize offsetFromRenderer;
59 LayoutSize subpixelAccumulation;
61 GraphicsLayerPaintingPhase paintingPhase;
63 bool isBackgroundLayer;
65 bool isEquivalentForSquashing(const GraphicsLayerPaintInfo& other)
67 // FIXME: offsetFromRenderer and compositedBounds should not be checked here, because
68 // they are not yet fixed at the time this function is used.
69 return renderLayer == other.renderLayer
70 && offsetFromSquashingCLM == other.offsetFromSquashingCLM
71 && paintingPhase == other.paintingPhase
72 && isBackgroundLayer == other.isBackgroundLayer;
76 // CompositedLayerMapping keeps track of how RenderLayers of the render tree correspond to
77 // GraphicsLayers of the composited layer tree. Each instance of CompositedLayerMapping
78 // manages a small cluster of GraphicsLayers and the references to which RenderLayers
79 // and paint phases contribute to each GraphicsLayer.
81 // Currently (Oct. 2013) there is one CompositedLayerMapping for each RenderLayer,
82 // but this is likely to evolve soon.
83 class CompositedLayerMapping FINAL : public GraphicsLayerClient {
84 WTF_MAKE_NONCOPYABLE(CompositedLayerMapping); WTF_MAKE_FAST_ALLOCATED;
86 explicit CompositedLayerMapping(RenderLayer&);
87 virtual ~CompositedLayerMapping();
89 RenderLayer& owningLayer() const { return m_owningLayer; }
91 // Returns true if layer configuration changed.
92 bool updateGraphicsLayerConfiguration(GraphicsLayerUpdater::UpdateType);
93 // Update graphics layer position and bounds.
94 void updateGraphicsLayerGeometry(GraphicsLayerUpdater::UpdateType, const RenderLayer* compositingContainer);
95 // Update whether layer needs blending.
96 void updateContentsOpaque();
98 GraphicsLayer* mainGraphicsLayer() const { return m_graphicsLayer.get(); }
100 // Layer to clip children
101 bool hasClippingLayer() const { return m_childContainmentLayer; }
102 GraphicsLayer* clippingLayer() const { return m_childContainmentLayer.get(); }
104 // Layer to get clipped by ancestor
105 bool hasAncestorClippingLayer() const { return m_ancestorClippingLayer; }
106 GraphicsLayer* ancestorClippingLayer() const { return m_ancestorClippingLayer.get(); }
108 bool hasContentsLayer() const { return m_foregroundLayer; }
109 GraphicsLayer* foregroundLayer() const { return m_foregroundLayer.get(); }
111 GraphicsLayer* backgroundLayer() const { return m_backgroundLayer.get(); }
112 bool backgroundLayerPaintsFixedRootBackground() const { return m_backgroundLayerPaintsFixedRootBackground; }
114 bool hasScrollingLayer() const { return m_scrollingLayer; }
115 GraphicsLayer* scrollingLayer() const { return m_scrollingLayer.get(); }
116 GraphicsLayer* scrollingContentsLayer() const { return m_scrollingContentsLayer.get(); }
118 bool hasMaskLayer() const { return m_maskLayer; }
119 GraphicsLayer* maskLayer() const { return m_maskLayer.get(); }
121 bool hasChildClippingMaskLayer() const { return m_childClippingMaskLayer; }
122 GraphicsLayer* childClippingMaskLayer() const { return m_childClippingMaskLayer.get(); }
124 GraphicsLayer* parentForSublayers() const;
125 GraphicsLayer* childForSuperlayers() const;
126 // localRootForOwningLayer does not include the m_squashingContainmentLayer, which is technically not associated with this CLM's owning layer.
127 GraphicsLayer* localRootForOwningLayer() const;
129 GraphicsLayer* childTransformLayer() const { return m_childTransformLayer.get(); }
131 GraphicsLayer* squashingContainmentLayer() const { return m_squashingContainmentLayer.get(); }
132 GraphicsLayer* squashingLayer() const { return m_squashingLayer.get(); }
133 // Contains the bottommost layer in the hierarchy that can contain the children transform.
134 GraphicsLayer* layerForChildrenTransform() const;
136 // Returns true for a composited layer that has no backing store of its own, so
137 // paints into some ancestor layer.
138 bool paintsIntoCompositedAncestor() const { return !(m_requiresOwnBackingStoreForAncestorReasons || m_requiresOwnBackingStoreForIntrinsicReasons); }
140 // Updates whether a backing store is needed based on the layer's compositing ancestor's
141 // properties; returns true if the need for a backing store for ancestor reasons changed.
142 bool updateRequiresOwnBackingStoreForAncestorReasons(const RenderLayer* compositingAncestor);
144 // Updates whether a backing store is needed for intrinsic reasons (that is, based on the
145 // layer's own properties or compositing reasons); returns true if the intrinsic need for
146 // a backing store changed.
147 bool updateRequiresOwnBackingStoreForIntrinsicReasons();
149 void setContentsNeedDisplay();
150 // r is in the coordinate space of the layer's render object
151 void setContentsNeedDisplayInRect(const IntRect&);
153 // Notification from the renderer that its content changed.
154 void contentChanged(ContentChangeType);
156 LayoutRect compositedBounds() const { return m_compositedBounds; }
157 IntRect pixelSnappedCompositedBounds() const;
158 void updateCompositedBounds(GraphicsLayerUpdater::UpdateType);
160 void positionOverflowControlsLayers(const IntSize& offsetFromRoot);
161 bool hasUnpositionedOverflowControlsLayers() const;
163 // Returns true if the assignment actually changed the assigned squashing layer.
164 bool updateSquashingLayerAssignment(RenderLayer*, LayoutSize offsetFromSquashingCLM, size_t nextSquashedLayerIndex);
165 void removeRenderLayerFromSquashingGraphicsLayer(const RenderLayer*);
167 void finishAccumulatingSquashingLayers(size_t nextSquashedLayerIndex);
168 void updateRenderingContext();
169 void updateShouldFlattenTransform();
171 // GraphicsLayerClient interface
172 virtual void notifyAnimationStarted(const GraphicsLayer*, double monotonicTime) OVERRIDE;
173 virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect& clip) OVERRIDE;
174 virtual bool isTrackingRepaints() const OVERRIDE;
176 PassOwnPtr<Vector<FloatRect> > collectTrackedRepaintRects() const;
179 virtual void verifyNotPainting() OVERRIDE;
182 LayoutRect contentsBox() const;
184 GraphicsLayer* layerForHorizontalScrollbar() const { return m_layerForHorizontalScrollbar.get(); }
185 GraphicsLayer* layerForVerticalScrollbar() const { return m_layerForVerticalScrollbar.get(); }
186 GraphicsLayer* layerForScrollCorner() const { return m_layerForScrollCorner.get(); }
188 void updateFilters(const RenderStyle*);
189 bool canCompositeFilters() const { return m_canCompositeFilters; }
191 void setBlendMode(blink::WebBlendMode);
193 void setNeedsGraphicsLayerUpdate();
194 bool shouldUpdateGraphicsLayer(GraphicsLayerUpdater::UpdateType updateType) const { return m_needToUpdateGraphicsLayer || updateType == GraphicsLayerUpdater::ForceUpdate; }
195 GraphicsLayerUpdater::UpdateType updateTypeForChildren(GraphicsLayerUpdater::UpdateType) const;
196 void clearNeedsGraphicsLayerUpdate();
199 void assertNeedsToUpdateGraphicsLayerBitsCleared();
202 virtual String debugName(const GraphicsLayer*) OVERRIDE;
204 LayoutSize contentOffsetInCompositingLayer() const;
206 LayoutPoint squashingOffsetFromTransformedAncestor()
208 return m_squashingLayerOffsetFromTransformedAncestor;
211 // If there is a squashed layer painting into this CLM that is an ancestor of the given RenderObject, return it. Otherwise return 0.
212 const GraphicsLayerPaintInfo* containingSquashedLayer(const RenderObject*) const;
215 void createPrimaryGraphicsLayer();
216 void destroyGraphicsLayers();
218 PassOwnPtr<GraphicsLayer> createGraphicsLayer(CompositingReasons);
219 bool toggleScrollbarLayerIfNeeded(OwnPtr<GraphicsLayer>&, bool needsLayer, CompositingReasons);
221 RenderLayerModelObject* renderer() const { return m_owningLayer.renderer(); }
222 RenderLayerCompositor* compositor() const { return m_owningLayer.compositor(); }
224 void updateInternalHierarchy();
225 void updatePaintingPhases();
226 bool updateClippingLayers(bool needsAncestorClip, bool needsDescendantClip);
227 bool updateChildTransformLayer(bool needsChildTransformLayer);
228 bool updateOverflowControlsLayers(bool needsHorizontalScrollbarLayer, bool needsVerticalScrollbarLayer, bool needsScrollCornerLayer);
229 bool updateForegroundLayer(bool needsForegroundLayer);
230 bool updateBackgroundLayer(bool needsBackgroundLayer);
231 bool updateMaskLayer(bool needsMaskLayer);
232 bool updateClippingMaskLayers(bool needsChildClippingMaskLayer);
233 bool requiresHorizontalScrollbarLayer() const { return m_owningLayer.scrollableArea() && m_owningLayer.scrollableArea()->horizontalScrollbar(); }
234 bool requiresVerticalScrollbarLayer() const { return m_owningLayer.scrollableArea() && m_owningLayer.scrollableArea()->verticalScrollbar(); }
235 bool requiresScrollCornerLayer() const { return m_owningLayer.scrollableArea() && !m_owningLayer.scrollableArea()->scrollCornerAndResizerRect().isEmpty(); }
236 bool updateScrollingLayers(bool scrollingLayers);
237 void updateScrollParent(RenderLayer*);
238 void updateClipParent(RenderLayer*);
239 bool updateSquashingLayers(bool needsSquashingLayers);
240 void updateDrawsContent();
241 void updateChildrenTransform();
242 void registerScrollingLayers();
244 void adjustBoundsForSubPixelAccumulation(const RenderLayer* compositedAncestor, IntRect& localCompositingBounds, IntRect& relativeCompositingBounds, IntPoint& delta);
246 void setBackgroundLayerPaintsFixedRootBackground(bool);
248 GraphicsLayerPaintingPhase paintingPhaseForPrimaryLayer() const;
250 // Result is transform origin in pixels.
251 FloatPoint3D computeTransformOrigin(const IntRect& borderBox) const;
253 void updateSquashingLayerGeometry(const IntPoint& delta);
255 void updateOpacity(const RenderStyle*);
256 void updateTransform(const RenderStyle*);
257 void updateLayerBlendMode(const RenderStyle*);
258 void updateIsRootForIsolatedGroup();
259 // Return the opacity value that this layer should use for compositing.
260 float compositingOpacity(float rendererOpacity) const;
262 bool isMainFrameRenderViewLayer() const;
264 bool paintsChildren() const;
266 // Returns true if this layer has content that needs to be rendered by painting into the backing store.
267 bool containsPaintedContent() 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 updateBackgroundColor();
274 void updateContentsRect();
275 void updateAfterWidgetResize();
276 void updateCompositingReasons();
278 bool hasVisibleNonCompositingDescendantLayers() const;
280 void paintsIntoCompositedAncestorChanged();
282 void doPaintTask(GraphicsLayerPaintInfo&, GraphicsContext*, const IntRect& clip);
284 // Computes the background clip rect for the given squashed layer, up to any containing layer that is squashed into the
285 // same squashing layer and contains this squashed layer's clipping ancestor.
286 // The clip rect is returned in the coordinate space of the given squashed layer.
287 // If there is no such containing layer, returns the infinite rect.
288 // FIXME: unify this code with the code that sets up m_ancestorClippingLayer. They are doing very similar things.
289 IntRect localClipRectForSquashedLayer(const GraphicsLayerPaintInfo&) const;
291 RenderLayer& m_owningLayer;
293 // The hierarchy of layers that is maintained by the CompositedLayerMapping looks like this:
295 // + m_ancestorClippingLayer [OPTIONAL]
297 // + m_childContainmentLayer [OPTIONAL] <-OR-> m_scrollingLayer [OPTIONAL] <-OR-> m_childTransformLayer
298 // + m_scrollingContentsLayer [Present iff m_scrollingLayer is present]
300 // We need an ancestor clipping layer if our clipping ancestor is not our ancestor in the
301 // clipping tree. Here's what that might look like.
303 // Let A = the clipping ancestor,
304 // B = the clip descendant, and
305 // SC = the stacking context that is the ancestor of A and B in the stacking tree.
308 // + A = m_graphicsLayer
309 // | + m_childContainmentLayer
313 // + B = m_ancestorClippingLayer [+]
317 // In this case B is clipped by another layer that doesn't happen to be its ancestor: A.
318 // So we create an ancestor clipping layer for B, [+], which ensures that B is clipped
319 // as if it had been A's descendant.
320 OwnPtr<GraphicsLayer> m_ancestorClippingLayer; // Only used if we are clipped by an ancestor which is not a stacking context.
321 OwnPtr<GraphicsLayer> m_graphicsLayer;
322 OwnPtr<GraphicsLayer> m_childContainmentLayer; // Only used if we have clipping on a stacking context with compositing children.
323 OwnPtr<GraphicsLayer> m_childTransformLayer; // Only used if we have perspective and no m_childContainmentLayer.
324 OwnPtr<GraphicsLayer> m_scrollingLayer; // Only used if the layer is using composited scrolling.
325 OwnPtr<GraphicsLayer> m_scrollingContentsLayer; // Only used if the layer is using composited scrolling.
327 // This layer is also added to the hierarchy by the RLB, but in a different way than
328 // the layers above. It's added to m_graphicsLayer as its mask layer (naturally) if
329 // we have a mask, and isn't part of the typical hierarchy (it has no children).
330 OwnPtr<GraphicsLayer> m_maskLayer; // Only used if we have a mask.
331 OwnPtr<GraphicsLayer> m_childClippingMaskLayer; // Only used if we have to clip child layers or accelerated contents with border radius or clip-path.
333 // There are two other (optional) layers whose painting is managed by the CompositedLayerMapping,
334 // but whose position in the hierarchy is maintained by the RenderLayerCompositor. These
335 // are the foreground and background layers. The foreground layer exists if we have composited
336 // descendants with negative z-order. We need the extra layer in this case because the layer
337 // needs to draw both below (for the background, say) and above (for the normal flow content, say)
338 // the negative z-order descendants and this is impossible with a single layer. The RLC handles
339 // inserting m_foregroundLayer in the correct position in our descendant list for us (right after
340 // the neg z-order dsecendants).
342 // The background layer is only created if this is the root layer and our background is entirely
343 // fixed. In this case we want to put the background in a separate composited layer so that when
344 // we scroll, we don't have to re-raster the background into position. This layer is also inserted
345 // into the tree by the RLC as it gets a special home. This layer becomes a descendant of the
346 // frame clipping layer. That is:
348 // + frame clipping layer
349 // + m_backgroundLayer
350 // + frame scrolling layer
351 // + root content layer
353 // With the hierarchy set up like this, the root content layer is able to scroll without affecting
354 // the background layer (or repainting).
355 OwnPtr<GraphicsLayer> m_foregroundLayer; // Only used in cases where we need to draw the foreground separately.
356 OwnPtr<GraphicsLayer> m_backgroundLayer; // Only used in cases where we need to draw the background separately.
358 OwnPtr<GraphicsLayer> m_layerForHorizontalScrollbar;
359 OwnPtr<GraphicsLayer> m_layerForVerticalScrollbar;
360 OwnPtr<GraphicsLayer> m_layerForScrollCorner;
362 // A squashing CLM has two possible squashing-related structures.
364 // If m_ancestorClippingLayer is present:
366 // m_ancestorClippingLayer
368 // + m_squashingLayer
372 // m_squashingContainmentLayer
374 // + m_squashingLayer
375 OwnPtr<GraphicsLayer> m_squashingContainmentLayer; // Only used if any squashed layers exist and m_squashingContainmentLayer is not present, to contain the squashed layers as siblings to the rest of the GraphicsLayer tree chunk.
376 OwnPtr<GraphicsLayer> m_squashingLayer; // Only used if any squashed layers exist, this is the backing that squashed layers paint into.
377 Vector<GraphicsLayerPaintInfo> m_squashedLayers;
378 LayoutPoint m_squashingLayerOffsetFromTransformedAncestor;
380 LayoutRect m_compositedBounds;
382 bool m_artificiallyInflatedBounds : 1; // bounds had to be made non-zero to make transform-origin work
383 bool m_isMainFrameRenderViewLayer : 1;
384 bool m_requiresOwnBackingStoreForIntrinsicReasons : 1;
385 bool m_requiresOwnBackingStoreForAncestorReasons : 1;
386 bool m_canCompositeFilters : 1;
387 bool m_backgroundLayerPaintsFixedRootBackground : 1;
388 bool m_needToUpdateGraphicsLayer : 1;
389 bool m_needToUpdateGraphicsLayerOfAllDecendants : 1;
392 } // namespace WebCore
394 #endif // CompositedLayerMapping_h