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"
38 class RenderLayerCompositor;
40 // A GraphicsLayerPaintInfo contains all the info needed to paint a partial subtree of RenderLayers into a GraphicsLayer.
41 struct GraphicsLayerPaintInfo {
42 RenderLayer* renderLayer;
44 LayoutRect compositedBounds;
46 // The clip rect to apply, in the local coordinate space of the squashed layer, when painting it.
47 IntRect localClipRectForSquashedLayer;
49 // Offset describing where this squashed RenderLayer paints into the shared GraphicsLayer backing.
50 IntSize offsetFromRenderer;
51 bool offsetFromRendererSet;
53 GraphicsLayerPaintInfo() : renderLayer(0), offsetFromRendererSet(false) { }
56 enum GraphicsLayerUpdateScope {
57 GraphicsLayerUpdateNone,
58 GraphicsLayerUpdateLocal,
59 GraphicsLayerUpdateSubtree,
62 // CompositedLayerMapping keeps track of how RenderLayers of the render tree correspond to
63 // GraphicsLayers of the composited layer tree. Each instance of CompositedLayerMapping
64 // manages a small cluster of GraphicsLayers and the references to which RenderLayers
65 // and paint phases contribute to each GraphicsLayer.
67 // Currently (Oct. 2013) there is one CompositedLayerMapping for each RenderLayer,
68 // but this is likely to evolve soon.
69 class CompositedLayerMapping FINAL : public GraphicsLayerClient {
70 WTF_MAKE_NONCOPYABLE(CompositedLayerMapping); WTF_MAKE_FAST_ALLOCATED;
72 explicit CompositedLayerMapping(RenderLayer&);
73 virtual ~CompositedLayerMapping();
75 RenderLayer& owningLayer() const { return m_owningLayer; }
77 bool updateGraphicsLayerConfiguration();
78 void updateGraphicsLayerGeometry(const RenderLayer* compositingContainer, const RenderLayer* compositingStackingContext, Vector<RenderLayer*>& layersNeedingPaintInvalidation);
80 // Update whether layer needs blending.
81 void updateContentsOpaque();
83 GraphicsLayer* mainGraphicsLayer() const { return m_graphicsLayer.get(); }
85 // Layer to clip children
86 bool hasClippingLayer() const { return m_childContainmentLayer; }
87 GraphicsLayer* clippingLayer() const { return m_childContainmentLayer.get(); }
89 // Layer to get clipped by ancestor
90 bool hasAncestorClippingLayer() const { return m_ancestorClippingLayer; }
91 GraphicsLayer* ancestorClippingLayer() const { return m_ancestorClippingLayer.get(); }
93 bool hasContentsLayer() const { return m_foregroundLayer; }
94 GraphicsLayer* foregroundLayer() const { return m_foregroundLayer.get(); }
96 GraphicsLayer* backgroundLayer() const { return m_backgroundLayer.get(); }
97 bool backgroundLayerPaintsFixedRootBackground() const { return m_backgroundLayerPaintsFixedRootBackground; }
99 bool hasScrollingLayer() const { return m_scrollingLayer; }
100 GraphicsLayer* scrollingLayer() const { return m_scrollingLayer.get(); }
101 GraphicsLayer* scrollingContentsLayer() const { return m_scrollingContentsLayer.get(); }
102 GraphicsLayer* scrollingBlockSelectionLayer() const { return m_scrollingBlockSelectionLayer.get(); }
104 bool hasMaskLayer() const { return m_maskLayer; }
105 GraphicsLayer* maskLayer() const { return m_maskLayer.get(); }
107 bool hasChildClippingMaskLayer() const { return m_childClippingMaskLayer; }
108 GraphicsLayer* childClippingMaskLayer() const { return m_childClippingMaskLayer.get(); }
110 GraphicsLayer* parentForSublayers() const;
111 GraphicsLayer* childForSuperlayers() const;
113 GraphicsLayer* childTransformLayer() const { return m_childTransformLayer.get(); }
115 GraphicsLayer* squashingContainmentLayer() const { return m_squashingContainmentLayer.get(); }
116 GraphicsLayer* squashingLayer() const { return m_squashingLayer.get(); }
117 // Contains the bottommost layer in the hierarchy that can contain the children transform.
118 GraphicsLayer* layerForChildrenTransform() const;
120 // Returns true for a composited layer that has no backing store of its own, so
121 // paints into some ancestor layer.
122 bool paintsIntoCompositedAncestor() const { return !(m_requiresOwnBackingStoreForAncestorReasons || m_requiresOwnBackingStoreForIntrinsicReasons); }
124 // Updates whether a backing store is needed based on the layer's compositing ancestor's
125 // properties; returns true if the need for a backing store for ancestor reasons changed.
126 bool updateRequiresOwnBackingStoreForAncestorReasons(const RenderLayer* compositingAncestor);
128 // Updates whether a backing store is needed for intrinsic reasons (that is, based on the
129 // layer's own properties or compositing reasons); returns true if the intrinsic need for
130 // a backing store changed.
131 bool updateRequiresOwnBackingStoreForIntrinsicReasons();
133 void setSquashingContentsNeedDisplay();
134 void setContentsNeedDisplay();
135 // r is in the coordinate space of the layer's render object
136 void setContentsNeedDisplayInRect(const LayoutRect&);
138 // Notification from the renderer that its content changed.
139 void contentChanged(ContentChangeType);
141 LayoutRect compositedBounds() const { return m_compositedBounds; }
142 IntRect pixelSnappedCompositedBounds() const;
143 void updateCompositedBounds();
145 void positionOverflowControlsLayers(const IntSize& offsetFromRoot);
146 bool hasUnpositionedOverflowControlsLayers() const;
148 // Returns true if the assignment actually changed the assigned squashing layer.
149 bool updateSquashingLayerAssignment(RenderLayer* squashedLayer, const RenderLayer& owningLayer, size_t nextSquashedLayerIndex);
150 void removeRenderLayerFromSquashingGraphicsLayer(const RenderLayer*);
152 void finishAccumulatingSquashingLayers(size_t nextSquashedLayerIndex);
153 void updateRenderingContext();
154 void updateShouldFlattenTransform();
156 // GraphicsLayerClient interface
157 virtual void notifyAnimationStarted(const GraphicsLayer*, double monotonicTime) OVERRIDE;
158 virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect& clip) OVERRIDE;
159 virtual bool isTrackingPaintInvalidations() const OVERRIDE;
162 virtual void verifyNotPainting() OVERRIDE;
165 LayoutRect contentsBox() const;
167 GraphicsLayer* layerForHorizontalScrollbar() const { return m_layerForHorizontalScrollbar.get(); }
168 GraphicsLayer* layerForVerticalScrollbar() const { return m_layerForVerticalScrollbar.get(); }
169 GraphicsLayer* layerForScrollCorner() const { return m_layerForScrollCorner.get(); }
171 // Returns true if the overflow controls cannot be positioned within this
172 // CLM's internal hierarchy without incorrectly stacking under some
173 // scrolling content. If this returns true, these controls must be
174 // repositioned in the graphics layer tree to ensure that they stack above
175 // scrolling content.
176 bool needsToReparentOverflowControls() const;
178 // Removes the overflow controls host layer from its parent and positions it
179 // so that it can be inserted as a sibling to this CLM without changing
181 GraphicsLayer* detachLayerForOverflowControls(const RenderLayer& enclosingLayer);
183 void updateFilters(const RenderStyle*);
185 void setBlendMode(WebBlendMode);
187 bool needsGraphicsLayerUpdate() { return m_pendingUpdateScope > GraphicsLayerUpdateNone; }
188 void setNeedsGraphicsLayerUpdate(GraphicsLayerUpdateScope scope) { m_pendingUpdateScope = std::max(static_cast<GraphicsLayerUpdateScope>(m_pendingUpdateScope), scope); }
189 void clearNeedsGraphicsLayerUpdate() { m_pendingUpdateScope = GraphicsLayerUpdateNone; }
191 GraphicsLayerUpdater::UpdateType updateTypeForChildren(GraphicsLayerUpdater::UpdateType) const;
194 void assertNeedsToUpdateGraphicsLayerBitsCleared() { ASSERT(m_pendingUpdateScope == GraphicsLayerUpdateNone); }
197 virtual String debugName(const GraphicsLayer*) OVERRIDE;
199 LayoutSize contentOffsetInCompositingLayer() const;
201 LayoutPoint squashingOffsetFromTransformedAncestor()
203 return m_squashingLayerOffsetFromTransformedAncestor;
206 // If there is a squashed layer painting into this CLM that is an ancestor of the given RenderObject, return it. Otherwise return 0.
207 const GraphicsLayerPaintInfo* containingSquashedLayer(const RenderObject*);
209 void updateScrollingBlockSelection();
212 static const GraphicsLayerPaintInfo* containingSquashedLayer(const RenderObject*, const Vector<GraphicsLayerPaintInfo>& layers);
214 // Helper methods to updateGraphicsLayerGeometry:
215 void computeGraphicsLayerParentLocation(const RenderLayer* compositingContainer, const IntRect& ancestorCompositingBounds, IntPoint& graphicsLayerParentLocation);
216 void updateSquashingLayerGeometry(const LayoutPoint& offsetFromCompositedAncestor, const IntPoint& graphicsLayerParentLocation, const RenderLayer& referenceLayer, Vector<GraphicsLayerPaintInfo>& layers, GraphicsLayer*, LayoutPoint* offsetFromTransformedAncestor, Vector<RenderLayer*>& layersNeedingPaintInvalidation);
217 void updateMainGraphicsLayerGeometry(const IntRect& relativeCompositingBounds, const IntRect& localCompositingBounds, IntPoint& graphicsLayerParentLocation);
218 void updateAncestorClippingLayerGeometry(const RenderLayer* compositingContainer, const IntPoint& snappedOffsetFromCompositedAncestor, IntPoint& graphicsLayerParentLocation);
219 void updateOverflowControlsHostLayerGeometry(const RenderLayer* compositingStackingContext);
220 void updateChildContainmentLayerGeometry(const IntRect& clippingBox, const IntRect& localCompositingBounds);
221 void updateChildTransformLayerGeometry();
222 void updateMaskLayerGeometry();
223 void updateTransformGeometry(const IntPoint& snappedOffsetFromCompositedAncestor, const IntRect& relativeCompositingBounds);
224 void updateForegroundLayerGeometry(const FloatSize& relativeCompositingBoundsSize, const IntRect& clippingBox);
225 void updateBackgroundLayerGeometry(const FloatSize& relativeCompositingBoundsSize);
226 void updateReflectionLayerGeometry(Vector<RenderLayer*>& layersNeedingPaintInvalidation);
227 void updateScrollingLayerGeometry(const IntRect& localCompositingBounds);
228 void updateChildClippingMaskLayerGeometry();
230 void createPrimaryGraphicsLayer();
231 void destroyGraphicsLayers();
233 PassOwnPtr<GraphicsLayer> createGraphicsLayer(CompositingReasons);
234 bool toggleScrollbarLayerIfNeeded(OwnPtr<GraphicsLayer>&, bool needsLayer, CompositingReasons);
236 RenderLayerModelObject* renderer() const { return m_owningLayer.renderer(); }
237 RenderLayerCompositor* compositor() const { return m_owningLayer.compositor(); }
239 void updateInternalHierarchy();
240 void updatePaintingPhases();
241 bool updateClippingLayers(bool needsAncestorClip, bool needsDescendantClip);
242 bool updateChildTransformLayer(bool needsChildTransformLayer);
243 bool updateOverflowControlsLayers(bool needsHorizontalScrollbarLayer, bool needsVerticalScrollbarLayer, bool needsScrollCornerLayer, bool needsAncestorClip);
244 bool updateForegroundLayer(bool needsForegroundLayer);
245 bool updateBackgroundLayer(bool needsBackgroundLayer);
246 bool updateMaskLayer(bool needsMaskLayer);
247 bool updateClippingMaskLayers(bool needsChildClippingMaskLayer);
248 bool requiresHorizontalScrollbarLayer() const { return m_owningLayer.scrollableArea() && m_owningLayer.scrollableArea()->horizontalScrollbar(); }
249 bool requiresVerticalScrollbarLayer() const { return m_owningLayer.scrollableArea() && m_owningLayer.scrollableArea()->verticalScrollbar(); }
250 bool requiresScrollCornerLayer() const { return m_owningLayer.scrollableArea() && !m_owningLayer.scrollableArea()->scrollCornerAndResizerRect().isEmpty(); }
251 bool updateScrollingLayers(bool scrollingLayers);
252 void updateScrollParent(RenderLayer*);
253 void updateClipParent();
254 bool updateSquashingLayers(bool needsSquashingLayers);
255 void updateDrawsContent();
256 void updateChildrenTransform();
257 void registerScrollingLayers();
259 // Also sets subpixelAccumulation on the layer.
260 void computeBoundsOfOwningLayer(const RenderLayer* compositedAncestor, IntRect& localCompositingBounds, IntRect& compositingBoundsRelativeToCompositedAncestor, LayoutPoint& offsetFromCompositedAncestor, IntPoint& snappedOffsetFromCompositedAncestor);
262 void setBackgroundLayerPaintsFixedRootBackground(bool);
264 GraphicsLayerPaintingPhase paintingPhaseForPrimaryLayer() const;
266 // Result is transform origin in pixels.
267 FloatPoint3D computeTransformOrigin(const IntRect& borderBox) const;
269 void updateOpacity(const RenderStyle*);
270 void updateTransform(const RenderStyle*);
271 void updateLayerBlendMode(const RenderStyle*);
272 void updateIsRootForIsolatedGroup();
273 // Return the opacity value that this layer should use for compositing.
274 float compositingOpacity(float rendererOpacity) const;
276 bool isMainFrameRenderViewLayer() const;
278 bool paintsChildren() const;
280 // Returns true if this layer has content that needs to be rendered by painting into the backing store.
281 bool containsPaintedContent() const;
282 // Returns true if the RenderLayer just contains an image that we can composite directly.
283 bool isDirectlyCompositedImage() const;
284 void updateImageContents();
286 Color rendererBackgroundColor() const;
287 void updateBackgroundColor();
288 void updateContentsRect();
289 void updateAfterWidgetResize();
290 void updateCompositingReasons();
292 static bool hasVisibleNonCompositingDescendant(RenderLayer* parent);
294 void doPaintTask(const GraphicsLayerPaintInfo&, const PaintLayerFlags&, GraphicsContext*, const IntRect& clip);
296 // Computes the background clip rect for the given squashed layer, up to any containing layer that is squashed into the
297 // same squashing layer and contains this squashed layer's clipping ancestor.
298 // The clip rect is returned in the coordinate space of the given squashed layer.
299 // If there is no such containing layer, returns the infinite rect.
300 // FIXME: unify this code with the code that sets up m_ancestorClippingLayer. They are doing very similar things.
301 static IntRect localClipRectForSquashedLayer(const RenderLayer& referenceLayer, const GraphicsLayerPaintInfo&, const Vector<GraphicsLayerPaintInfo>& layers);
303 // Return true if |m_owningLayer|'s compositing ancestor is not a descendant (inclusive) of the
304 // clipping container for |m_owningLayer|.
305 bool owningLayerClippedByLayerNotAboveCompositedAncestor();
307 RenderLayer& m_owningLayer;
309 // The hierarchy of layers that is maintained by the CompositedLayerMapping looks like this:
311 // + m_ancestorClippingLayer [OPTIONAL]
313 // + m_childContainmentLayer [OPTIONAL] <-OR-> m_scrollingLayer [OPTIONAL] <-OR-> m_childTransformLayer
314 // | + m_scrollingContentsLayer [Present iff m_scrollingLayer is present]
315 // | + m_scrollingBlockSelectionLayer [Present iff m_scrollingLayer is present]
317 // + m_overflowControlsClippingLayer [OPTIONAL] // *The overflow controls may need to be repositioned in the
318 // + m_overflowControlsHostLayer // graphics layer tree by the RLC to ensure that they stack
319 // + m_layerForVerticalScrollbar // above scrolling content.
320 // + m_layerForHorizontalScrollbar
321 // + m_layerForScrollCorner
323 // We need an ancestor clipping layer if our clipping ancestor is not our ancestor in the
324 // clipping tree. Here's what that might look like.
326 // Let A = the clipping ancestor,
327 // B = the clip descendant, and
328 // SC = the stacking context that is the ancestor of A and B in the stacking tree.
331 // + A = m_graphicsLayer
332 // | + m_childContainmentLayer
336 // + B = m_ancestorClippingLayer [+]
340 // In this case B is clipped by another layer that doesn't happen to be its ancestor: A.
341 // So we create an ancestor clipping layer for B, [+], which ensures that B is clipped
342 // as if it had been A's descendant.
343 OwnPtr<GraphicsLayer> m_ancestorClippingLayer; // Only used if we are clipped by an ancestor which is not a stacking context.
344 OwnPtr<GraphicsLayer> m_graphicsLayer;
345 OwnPtr<GraphicsLayer> m_childContainmentLayer; // Only used if we have clipping on a stacking context with compositing children.
346 OwnPtr<GraphicsLayer> m_childTransformLayer; // Only used if we have perspective and no m_childContainmentLayer.
347 OwnPtr<GraphicsLayer> m_scrollingLayer; // Only used if the layer is using composited scrolling.
348 OwnPtr<GraphicsLayer> m_scrollingContentsLayer; // Only used if the layer is using composited scrolling.
349 OwnPtr<GraphicsLayer> m_scrollingBlockSelectionLayer; // Only used if the layer is using composited scrolling, but has no scrolling contents apart from block selection gaps.
351 // This layer is also added to the hierarchy by the RLB, but in a different way than
352 // the layers above. It's added to m_graphicsLayer as its mask layer (naturally) if
353 // we have a mask, and isn't part of the typical hierarchy (it has no children).
354 OwnPtr<GraphicsLayer> m_maskLayer; // Only used if we have a mask.
355 OwnPtr<GraphicsLayer> m_childClippingMaskLayer; // Only used if we have to clip child layers or accelerated contents with border radius or clip-path.
357 // There are two other (optional) layers whose painting is managed by the CompositedLayerMapping,
358 // but whose position in the hierarchy is maintained by the RenderLayerCompositor. These
359 // are the foreground and background layers. The foreground layer exists if we have composited
360 // descendants with negative z-order. We need the extra layer in this case because the layer
361 // needs to draw both below (for the background, say) and above (for the normal flow content, say)
362 // the negative z-order descendants and this is impossible with a single layer. The RLC handles
363 // inserting m_foregroundLayer in the correct position in our descendant list for us (right after
364 // the neg z-order dsecendants).
366 // The background layer is only created if this is the root layer and our background is entirely
367 // fixed. In this case we want to put the background in a separate composited layer so that when
368 // we scroll, we don't have to re-raster the background into position. This layer is also inserted
369 // into the tree by the RLC as it gets a special home. This layer becomes a descendant of the
370 // frame clipping layer. That is:
372 // + frame clipping layer
373 // + m_backgroundLayer
374 // + frame scrolling layer
375 // + root content layer
377 // With the hierarchy set up like this, the root content layer is able to scroll without affecting
378 // the background layer (or paint invalidation).
379 OwnPtr<GraphicsLayer> m_foregroundLayer; // Only used in cases where we need to draw the foreground separately.
380 OwnPtr<GraphicsLayer> m_backgroundLayer; // Only used in cases where we need to draw the background separately.
382 OwnPtr<GraphicsLayer> m_layerForHorizontalScrollbar;
383 OwnPtr<GraphicsLayer> m_layerForVerticalScrollbar;
384 OwnPtr<GraphicsLayer> m_layerForScrollCorner;
386 // This layer exists to simplify the reparenting of overflow control that is occasionally required
387 // to ensure that scrollbars appear above scrolling content.
388 OwnPtr<GraphicsLayer> m_overflowControlsHostLayer;
390 // The reparented overflow controls sometimes need to be clipped by a non-ancestor. In just the same
391 // way we need an ancestor clipping layer to clip this CLM's internal hierarchy, we add another layer
392 // to clip the overflow controls. It would be possible to make m_overflowControlsHostLayer be
393 // responsible for applying this clip, but that could require repositioning all of the overflow
394 // controls since the this clip may apply an offset. By using a separate layer, the overflow controls
395 // can remain ignorant of the layers above them and still work correctly.
396 OwnPtr<GraphicsLayer> m_overflowControlsClippingLayer;
398 // A squashing CLM has two possible squashing-related structures.
400 // If m_ancestorClippingLayer is present:
402 // m_ancestorClippingLayer
404 // + m_squashingLayer
408 // m_squashingContainmentLayer
410 // + m_squashingLayer
412 // Stacking children of a squashed layer receive graphics layers that are parented to the compositd ancestor of the
413 // squashed layer (i.e. nearest enclosing composited layer that is not squashed).
414 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.
415 OwnPtr<GraphicsLayer> m_squashingLayer; // Only used if any squashed layers exist, this is the backing that squashed layers paint into.
416 Vector<GraphicsLayerPaintInfo> m_squashedLayers;
417 LayoutPoint m_squashingLayerOffsetFromTransformedAncestor;
419 LayoutRect m_compositedBounds;
421 unsigned m_pendingUpdateScope : 2;
422 unsigned m_isMainFrameRenderViewLayer : 1;
423 unsigned m_requiresOwnBackingStoreForIntrinsicReasons : 1;
424 unsigned m_requiresOwnBackingStoreForAncestorReasons : 1;
425 unsigned m_backgroundLayerPaintsFixedRootBackground : 1;
426 unsigned m_scrollingContentsAreEmpty : 1;
431 #endif // CompositedLayerMapping_h