2 * Copyright (C) 2009 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 RenderLayerCompositor_h
27 #define RenderLayerCompositor_h
29 #include "core/page/ChromeClient.h"
30 #include "core/rendering/RenderLayer.h"
31 #include "platform/graphics/GraphicsLayerClient.h"
32 #include "wtf/HashMap.h"
36 class FixedPositionViewportConstraints;
38 class RenderEmbeddedObject;
39 class RenderLayerStackingNode;
42 class ScrollingCoordinator;
43 class StickyPositionViewportConstraints;
46 enum CompositingUpdateType {
47 CompositingUpdateAfterStyleChange,
48 CompositingUpdateAfterLayout,
49 CompositingUpdateOnScroll,
50 CompositingUpdateOnCompositedScroll,
53 // RenderLayerCompositor manages the hierarchy of
54 // composited RenderLayers. It determines which RenderLayers
55 // become compositing, and creates and maintains a hierarchy of
56 // GraphicsLayers based on the RenderLayer painting order.
58 // There is one RenderLayerCompositor per RenderView.
60 class RenderLayerCompositor FINAL : public GraphicsLayerClient {
61 WTF_MAKE_FAST_ALLOCATED;
63 explicit RenderLayerCompositor(RenderView*);
64 virtual ~RenderLayerCompositor();
66 // Return true if this RenderView is in "compositing mode" (i.e. has one or more
67 // composited RenderLayers)
68 bool inCompositingMode() const { return m_compositing; }
69 // This will make a compositing layer at the root automatically, and hook up to
70 // the native view/window system.
71 void enableCompositingMode(bool enable = true);
73 bool inForcedCompositingMode() const { return m_forceCompositingMode; }
75 // Returns true if the accelerated compositing is enabled
76 bool hasAcceleratedCompositing() const { return m_hasAcceleratedCompositing; }
77 bool layerSquashingEnabled() const;
79 bool canRender3DTransforms() const;
81 // Copy the accelerated compositing related flags from Settings
82 void cacheAcceleratedCompositingFlags();
84 // Called when the layer hierarchy needs to be updated (compositing layers have been
85 // created, destroyed or re-parented).
86 void setCompositingLayersNeedRebuild(bool needRebuild = true);
87 bool compositingLayersNeedRebuild() const { return m_compositingLayersNeedRebuild; }
89 // Updating properties required for determining if compositing is necessary.
90 void updateCompositingRequirementsState();
91 void setNeedsUpdateCompositingRequirementsState() { m_needsUpdateCompositingRequirementsState = true; }
93 // Used to indicate that a compositing update will be needed for the next frame that gets drawn.
94 void setNeedsCompositingUpdate(CompositingUpdateType);
96 // Main entry point for a full update. As needed, this function will compute compositing requirements,
97 // rebuild the composited layer tree, and/or update all the properties assocaited with each layer of the
98 // composited layer tree.
99 void updateCompositingLayers();
101 // Update the compositing dirty bits, based on the compositing-impacting properties of the layer.
102 // (At the moment, it also has some legacy compatibility hacks.)
103 void updateLayerCompositingState(RenderLayer*);
105 // Update the geometry for compositing children of compositingAncestor.
106 void updateCompositingDescendantGeometry(RenderLayerStackingNode* compositingAncestor, RenderLayer*, bool compositedChildrenOnly);
108 // Whether layer's compositedLayerMapping needs a GraphicsLayer to do clipping by an ancestor (non-stacking-context parent with overflow).
109 bool clippedByAncestor(const RenderLayer*) const;
110 // Whether layer's compositedLayerMapping needs a GraphicsLayer to clip z-order children of the given RenderLayer.
111 bool clipsCompositingDescendants(const RenderLayer*) const;
113 // Whether the given layer needs an extra 'contents' layer.
114 bool needsContentsCompositingLayer(const RenderLayer*) const;
116 bool supportsFixedRootBackgroundCompositing() const;
117 bool needsFixedRootBackgroundLayer(const RenderLayer*) const;
118 GraphicsLayer* fixedRootBackgroundLayer() const;
120 // Return the bounding box required for compositing layer and its childern, relative to ancestorLayer.
121 // If layerBoundingBox is not 0, on return it contains the bounding box of this layer only.
122 LayoutRect calculateCompositedBounds(const RenderLayer*, const RenderLayer* ancestorLayer) const;
124 // Repaint the appropriate layers when the given RenderLayer starts or stops being composited.
125 void repaintOnCompositingChange(RenderLayer*);
127 void repaintInCompositedAncestor(RenderLayer*, const LayoutRect&);
129 // Notify us that a layer has been added or removed
130 void layerWasAdded(RenderLayer* parent, RenderLayer* child);
131 void layerWillBeRemoved(RenderLayer* parent, RenderLayer* child);
133 // Get the nearest ancestor layer that has overflow or clip, but is not a stacking context
134 RenderLayer* enclosingNonStackingClippingLayer(const RenderLayer* layer) const;
136 // Repaint parts of all composited layers that intersect the given absolute rectangle (or the entire layer if the pointer is null).
137 void repaintCompositedLayers(const IntRect* = 0);
139 RenderLayer* rootRenderLayer() const;
140 GraphicsLayer* rootGraphicsLayer() const;
141 GraphicsLayer* scrollLayer() const;
142 GraphicsLayer* containerLayer() const;
144 enum RootLayerAttachment {
146 RootLayerAttachedViaChromeClient,
147 RootLayerAttachedViaEnclosingFrame
150 RootLayerAttachment rootLayerAttachment() const { return m_rootLayerAttachment; }
151 void updateRootLayerAttachment();
152 void updateRootLayerPosition();
154 void setIsInWindow(bool);
156 void clearMappingForAllRenderLayers();
158 // Use by RenderVideo to ask if it should try to use accelerated compositing.
159 bool canAccelerateVideoRendering(RenderVideo*) const;
161 // Walk the tree looking for layers with 3d transforms. Useful in case you need
162 // to know if there is non-affine content, e.g. for drawing into an image.
163 bool has3DContent() const;
165 static RenderLayerCompositor* frameContentsCompositor(RenderPart*);
166 // Return true if the layers changed.
167 static bool parentFrameContentLayers(RenderPart*);
169 // Update the geometry of the layers used for clipping and scrolling in frames.
170 void frameViewDidChangeLocation(const IntPoint& contentsOffset);
171 void frameViewDidChangeSize();
172 void frameViewDidScroll();
173 void frameViewDidLayout();
174 void frameViewScrollbarsExistenceDidChange();
175 void rootFixedBackgroundsChanged();
177 bool scrollingLayerDidChange(RenderLayer*);
179 String layerTreeAsText(LayerTreeFlags);
181 GraphicsLayer* layerForHorizontalScrollbar() const { return m_layerForHorizontalScrollbar.get(); }
182 GraphicsLayer* layerForVerticalScrollbar() const { return m_layerForVerticalScrollbar.get(); }
183 GraphicsLayer* layerForScrollCorner() const { return m_layerForScrollCorner.get(); }
185 void updateViewportConstraintStatus(RenderLayer*);
186 void removeViewportConstrainedLayer(RenderLayer*);
188 void addOutOfFlowPositionedLayer(RenderLayer*);
189 void removeOutOfFlowPositionedLayer(RenderLayer*);
191 void resetTrackedRepaintRects();
192 void setTracksRepaints(bool);
194 void setNeedsToRecomputeCompositingRequirements() { m_needsToRecomputeCompositingRequirements = true; }
196 virtual String debugName(const GraphicsLayer*) OVERRIDE;
201 enum CompositingStateTransitionType {
202 NoCompositingStateChange,
203 AllocateOwnCompositedLayerMapping,
204 RemoveOwnCompositedLayerMapping,
206 RemoveFromSquashingLayer
209 struct SquashingState {
211 : mostRecentMapping(0)
212 , hasMostRecentMapping(false)
213 , nextSquashedLayerIndex(0) { }
215 void updateSquashingStateForNewMapping(CompositedLayerMappingPtr, bool hasNewCompositedLayerMapping, IntPoint newOffsetFromAbsoluteForSquashingCLM);
217 // The most recent composited backing that the layer should squash onto if needed.
218 CompositedLayerMappingPtr mostRecentMapping;
219 bool hasMostRecentMapping;
221 // Absolute coordinates of the compositedLayerMapping's owning layer. This is used for computing the correct
222 // positions of renderlayers when they paint into the squashing layer.
223 IntPoint offsetFromAbsoluteForSquashingCLM;
225 // Counter that tracks what index the next RenderLayer would be if it gets squashed to the current squashing layer.
226 size_t nextSquashedLayerIndex;
229 CompositingStateTransitionType computeCompositedLayerUpdate(const RenderLayer*);
230 // Make updates to the layer based on viewport-constrained properties such as position:fixed. This can in turn affect
232 bool updateLayerIfViewportConstrained(RenderLayer*);
234 // GraphicsLayerClient implementation
235 virtual void notifyAnimationStarted(const GraphicsLayer*, double, double) OVERRIDE { }
236 virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect&) OVERRIDE;
238 virtual bool isTrackingRepaints() const OVERRIDE;
240 // Whether the given RL needs to paint into its own separate backing (and hence would need its own CompositedLayerMapping).
241 bool needsOwnBacking(const RenderLayer*) const;
242 // Whether the layer could ever be composited.
243 bool canBeComposited(const RenderLayer*) const;
245 // Returns all direct reasons that a layer should be composited.
246 CompositingReasons directReasonsForCompositing(const RenderLayer*) const;
248 void updateDirectCompositingReasons(RenderLayer*);
250 // Returns indirect reasons that a layer should be composited because of something in its subtree.
251 CompositingReasons subtreeReasonsForCompositing(RenderObject*, bool hasCompositedDescendants, bool has3DTransformedDescendants) const;
253 // Make or destroy the CompositedLayerMapping for this layer; returns true if the compositedLayerMapping changed.
254 bool allocateOrClearCompositedLayerMapping(RenderLayer*);
255 bool updateSquashingAssignment(RenderLayer*, SquashingState&);
257 void clearMappingForRenderLayerIncludingDescendants(RenderLayer*);
259 // Repaint the given rect (which is layer's coords), and regions of child layers that intersect that rect.
260 void recursiveRepaintLayer(RenderLayer*, const IntRect* = 0);
262 void addToOverlapMap(OverlapMap&, RenderLayer*, IntRect& layerBounds, bool& boundsComputed);
263 void addToOverlapMapRecursive(OverlapMap&, RenderLayer*, RenderLayer* ancestorLayer = 0);
265 // Forces an update for all frames of frame tree recursively. Used only when the mainFrame compositor is ready to
266 // finish all deferred work.
267 static void finishCompositingUpdateForFrameTree(Frame*);
269 // Returns true if any layer's compositing changed
270 void computeCompositingRequirements(RenderLayer* ancestorLayer, RenderLayer*, OverlapMap*, struct CompositingRecursionData&, bool& descendantHas3DTransform, Vector<RenderLayer*>& unclippedDescendants);
272 // Defines which RenderLayers will paint into which composited backings, by allocating and destroying CompositedLayerMappings as needed.
273 void assignLayersToBackings(RenderLayer*, bool& layersChanged);
274 void assignLayersToBackingsInternal(RenderLayer*, SquashingState&, bool& layersChanged);
276 // Allocates, sets up hierarchy, and sets appropriate properties for the GraphicsLayers that correspond to a given
277 // composited RenderLayer. Does nothing if the given RenderLayer does not have a CompositedLayerMapping.
278 void updateGraphicsLayersMappedToRenderLayer(RenderLayer*);
280 // Recurses down the tree, parenting descendant compositing layers and collecting an array of child layers for the current compositing layer.
281 void rebuildCompositingLayerTree(RenderLayer*, Vector<GraphicsLayer*>& childGraphicsLayersOfEnclosingLayer, int depth);
283 // Recurses down the tree, updating layer geometry only.
284 void updateLayerTreeGeometry(RenderLayer*);
286 // Hook compositing layers together
287 void setCompositingParent(RenderLayer* childLayer, RenderLayer* parentLayer);
288 void removeCompositedChildren(RenderLayer*);
290 bool layerHas3DContent(const RenderLayer*) const;
291 bool isRunningAcceleratedTransformAnimation(RenderObject*) const;
293 bool hasAnyAdditionalCompositedLayers(const RenderLayer* rootLayer) const;
295 void ensureRootLayer();
296 void destroyRootLayer();
298 void attachRootLayer(RootLayerAttachment);
299 void detachRootLayer();
301 bool isMainFrame() const;
303 void updateOverflowControlsLayers();
305 void notifyIFramesOfCompositingChange();
309 GraphicsLayerFactory* graphicsLayerFactory() const;
310 ScrollingCoordinator* scrollingCoordinator() const;
312 // Whether a running transition or animation enforces the need for a compositing layer.
313 bool requiresCompositingForAnimation(RenderObject*) const;
314 // Whether a (not necessarily running) transition enforces the need for a compositing layer.
315 bool requiresCompositingForTransition(RenderObject*) const;
316 bool requiresCompositingForTransform(RenderObject*) const;
317 bool requiresCompositingForVideo(RenderObject*) const;
318 bool requiresCompositingForCanvas(RenderObject*) const;
319 bool requiresCompositingForPlugin(RenderObject*) const;
320 bool requiresCompositingForFrame(RenderObject*) const;
321 bool requiresCompositingForBackfaceVisibilityHidden(RenderObject*) const;
322 bool requiresCompositingForFilters(RenderObject*) const;
323 bool requiresCompositingForOverflowScrollingParent(const RenderLayer*) const;
324 bool requiresCompositingForOutOfFlowClipping(const RenderLayer*) const;
325 bool requiresCompositingForScrollableFrame() const;
326 bool requiresCompositingForPosition(RenderObject*, const RenderLayer*, RenderLayer::ViewportConstrainedNotCompositedReason* = 0) const;
327 bool requiresCompositingForOverflowScrolling(const RenderLayer*) const;
329 void addViewportConstrainedLayer(RenderLayer*);
331 FixedPositionViewportConstraints computeFixedViewportConstraints(RenderLayer*) const;
332 StickyPositionViewportConstraints computeStickyViewportConstraints(RenderLayer*) const;
334 bool requiresHorizontalScrollbarLayer() const;
335 bool requiresVerticalScrollbarLayer() const;
336 bool requiresScrollCornerLayer() const;
337 #if USE(RUBBER_BANDING)
338 bool requiresOverhangLayers() const;
341 void applyUpdateLayerCompositingStateChickenEggHacks(RenderLayer*, CompositingStateTransitionType compositedLayerUpdate);
342 void assignLayersToBackingsForReflectionLayer(RenderLayer* reflectionLayer, bool& layersChanged);
345 DocumentLifecycle& lifecycle() const;
347 RenderView* m_renderView;
348 OwnPtr<GraphicsLayer> m_rootContentLayer;
350 bool m_hasAcceleratedCompositing;
351 ChromeClient::CompositingTriggerFlags m_compositingTriggers;
353 bool m_showRepaintCounter;
355 // FIXME: This should absolutely not be mutable.
356 mutable bool m_needsToRecomputeCompositingRequirements;
357 bool m_needsToUpdateLayerTreeGeometry;
360 bool m_compositingLayersNeedRebuild;
361 bool m_forceCompositingMode;
362 bool m_inPostLayoutUpdate; // true when it's OK to trust layout information (e.g. layer sizes and positions)
363 bool m_needsUpdateCompositingRequirementsState;
365 bool m_isTrackingRepaints; // Used for testing.
367 RootLayerAttachment m_rootLayerAttachment;
369 // Enclosing container layer, which clips for iframe content
370 OwnPtr<GraphicsLayer> m_containerLayer;
371 OwnPtr<GraphicsLayer> m_scrollLayer;
373 HashSet<RenderLayer*> m_viewportConstrainedLayers;
374 HashSet<RenderLayer*> m_viewportConstrainedLayersNeedingUpdate;
376 // This is used in updateCompositingRequirementsState to avoid full tree
377 // walks while determining if layers have unclipped descendants.
378 HashSet<RenderLayer*> m_outOfFlowPositionedLayers;
380 // Enclosing layer for overflow controls and the clipping layer
381 OwnPtr<GraphicsLayer> m_overflowControlsHostLayer;
383 // Layers for overflow controls
384 OwnPtr<GraphicsLayer> m_layerForHorizontalScrollbar;
385 OwnPtr<GraphicsLayer> m_layerForVerticalScrollbar;
386 OwnPtr<GraphicsLayer> m_layerForScrollCorner;
387 #if USE(RUBBER_BANDING)
388 OwnPtr<GraphicsLayer> m_layerForOverhangShadow;
393 } // namespace WebCore
395 #endif // RenderLayerCompositor_h