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 "ChromeClient.h"
30 #include "RenderLayer.h"
31 #include "RenderLayerBacking.h"
35 #define PROFILE_LAYER_REBUILD 0
38 class RenderEmbeddedObject;
44 enum CompositingUpdateType {
45 CompositingUpdateAfterLayoutOrStyleChange,
46 CompositingUpdateOnPaitingOrHitTest,
47 CompositingUpdateOnScroll
50 // RenderLayerCompositor manages the hierarchy of
51 // composited RenderLayers. It determines which RenderLayers
52 // become compositing, and creates and maintains a hierarchy of
53 // GraphicsLayers based on the RenderLayer painting order.
55 // There is one RenderLayerCompositor per RenderView.
57 class RenderLayerCompositor : public GraphicsLayerClient {
59 RenderLayerCompositor(RenderView*);
60 ~RenderLayerCompositor();
62 // Return true if this RenderView is in "compositing mode" (i.e. has one or more
63 // composited RenderLayers)
64 bool inCompositingMode() const { return m_compositing; }
65 // This will make a compositing layer at the root automatically, and hook up to
66 // the native view/window system.
67 void enableCompositingMode(bool enable = true);
69 // Returns true if the accelerated compositing is enabled
70 bool hasAcceleratedCompositing() const { return m_hasAcceleratedCompositing; }
72 bool canRender3DTransforms() const;
74 // Copy the accelerated compositing related flags from Settings
75 void cacheAcceleratedCompositingFlags();
77 // Called when the layer hierarchy needs to be updated (compositing layers have been
78 // created, destroyed or re-parented).
79 void setCompositingLayersNeedRebuild(bool needRebuild = true);
80 bool compositingLayersNeedRebuild() const { return m_compositingLayersNeedRebuild; }
82 // Controls whether or not to consult geometry when deciding which layers need
83 // to be composited. Defaults to true.
84 void setCompositingConsultsOverlap(bool b) { m_compositingConsultsOverlap = b; }
85 bool compositingConsultsOverlap() const { return m_compositingConsultsOverlap; }
87 // GraphicsLayers buffer state, which gets pushed to the underlying platform layers
89 void scheduleLayerFlush();
90 void flushPendingLayerChanges(bool isFlushRoot = true);
92 // flushPendingLayerChanges() flushes the entire GraphicsLayer tree, which can cross frame boundaries.
93 // This call returns the rootmost compositor that is being flushed (including self).
94 RenderLayerCompositor* enclosingCompositorFlushingLayers() const;
96 // Called when the GraphicsLayer for the given RenderLayer has flushed changes inside of flushPendingLayerChanges().
97 void didFlushChangesForLayer(RenderLayer*);
99 // Rebuild the tree of compositing layers
100 void updateCompositingLayers(CompositingUpdateType = CompositingUpdateAfterLayoutOrStyleChange, RenderLayer* updateRoot = 0);
101 // This is only used when state changes and we do not exepect a style update or layout to happen soon (e.g. when
102 // we discover that an iframe is overlapped during painting).
103 void scheduleCompositingLayerUpdate();
105 // Update the compositing state of the given layer. Returns true if that state changed.
106 enum CompositingChangeRepaint { CompositingChangeRepaintNow, CompositingChangeWillRepaintLater };
107 bool updateLayerCompositingState(RenderLayer*, CompositingChangeRepaint = CompositingChangeRepaintNow);
109 // Update the geometry for compositing children of compositingAncestor.
110 void updateCompositingDescendantGeometry(RenderLayer* compositingAncestor, RenderLayer* layer, RenderLayerBacking::UpdateDepth);
112 // Whether layer's backing needs a graphics layer to do clipping by an ancestor (non-stacking-context parent with overflow).
113 bool clippedByAncestor(RenderLayer*) const;
114 // Whether layer's backing needs a graphics layer to clip z-order children of the given layer.
115 bool clipsCompositingDescendants(const RenderLayer*) const;
117 // Whether the given layer needs an extra 'contents' layer.
118 bool needsContentsCompositingLayer(const RenderLayer*) const;
119 // Return the bounding box required for compositing layer and its childern, relative to ancestorLayer.
120 // If layerBoundingBox is not 0, on return it contains the bounding box of this layer only.
121 LayoutRect calculateCompositedBounds(const RenderLayer*, const RenderLayer* ancestorLayer);
123 // Repaint the appropriate layers when the given RenderLayer starts or stops being composited.
124 void repaintOnCompositingChange(RenderLayer*);
126 // Notify us that a layer has been added or removed
127 void layerWasAdded(RenderLayer* parent, RenderLayer* child);
128 void layerWillBeRemoved(RenderLayer* parent, RenderLayer* child);
130 // Get the nearest ancestor layer that has overflow or clip, but is not a stacking context
131 RenderLayer* enclosingNonStackingClippingLayer(const RenderLayer* layer) const;
133 // Repaint parts of all composited layers that intersect the given absolute rectangle.
134 void repaintCompositedLayersAbsoluteRect(const LayoutRect&);
136 RenderLayer* rootRenderLayer() const;
137 GraphicsLayer* rootGraphicsLayer() const;
139 enum RootLayerAttachment {
141 RootLayerAttachedViaChromeClient,
142 RootLayerAttachedViaEnclosingFrame
145 RootLayerAttachment rootLayerAttachment() const { return m_rootLayerAttachment; }
146 void updateRootLayerAttachment();
147 void updateRootLayerPosition();
149 void didMoveOnscreen();
150 void willMoveOffscreen();
152 void clearBackingForAllLayers();
154 void didStartAcceleratedAnimation(CSSPropertyID);
157 // Use by RenderVideo to ask if it should try to use accelerated compositing.
158 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 // Most platforms connect compositing layer trees between iframes and their parent document.
166 // Some (currently just Mac) allow iframes to do their own compositing.
167 static bool allowsIndependentlyCompositedFrames(const FrameView*);
168 bool shouldPropagateCompositingToEnclosingFrame() const;
170 HTMLFrameOwnerElement* enclosingFrameElement() const;
172 static RenderLayerCompositor* frameContentsCompositor(RenderPart*);
173 // Return true if the layers changed.
174 static bool parentFrameContentLayers(RenderPart*);
176 // Update the geometry of the layers used for clipping and scrolling in frames.
177 void frameViewDidChangeLocation(const LayoutPoint& contentsOffset);
178 void frameViewDidChangeSize();
179 void frameViewDidScroll(const LayoutPoint& = LayoutPoint());
181 String layerTreeAsText(bool showDebugInfo = false);
183 // These are named to avoid conflicts with the functions in GraphicsLayerClient
184 // These return the actual internal variables.
185 bool compositorShowDebugBorders() const { return m_showDebugBorders; }
186 bool compositorShowRepaintCounter() const { return m_showRepaintCounter; }
188 virtual float deviceScaleFactor() const;
189 virtual float pageScaleFactor() const;
190 virtual void didCommitChangesForLayer(const GraphicsLayer*) const;
192 bool keepLayersPixelAligned() const;
194 void deviceOrPageScaleFactorChanged();
196 GraphicsLayer* layerForHorizontalScrollbar() const { return m_layerForHorizontalScrollbar.get(); }
197 GraphicsLayer* layerForVerticalScrollbar() const { return m_layerForVerticalScrollbar.get(); }
198 GraphicsLayer* layerForScrollCorner() const { return m_layerForScrollCorner.get(); }
199 #if PLATFORM(CHROMIUM) && ENABLE(RUBBER_BANDING)
200 GraphicsLayer* layerForOverhangAreas() const { return m_layerForOverhangAreas.get(); }
204 // GraphicsLayerClient Implementation
205 virtual void notifyAnimationStarted(const GraphicsLayer*, double) { }
206 virtual void notifySyncRequired(const GraphicsLayer*) { scheduleLayerFlush(); }
207 virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const LayoutRect&);
209 // These calls return false always. They are saying that the layers associated with this client
210 // (the clipLayer and scrollLayer) should never show debugging info.
211 virtual bool showDebugBorders() const { return false; }
212 virtual bool showRepaintCounter() const { return false; }
214 // Whether the given RL needs a compositing layer.
215 bool needsToBeComposited(const RenderLayer*) const;
216 // Whether the layer has an intrinsic need for compositing layer.
217 bool requiresCompositingLayer(const RenderLayer*) const;
218 // Whether the layer could ever be composited.
219 bool canBeComposited(const RenderLayer*) const;
221 // Make or destroy the backing for this layer; returns true if backing changed.
222 bool updateBacking(RenderLayer*, CompositingChangeRepaint shouldRepaint);
224 void clearBackingForLayerIncludingDescendants(RenderLayer*);
226 // Repaint the given rect (which is layer's coords), and regions of child layers that intersect that rect.
227 void recursiveRepaintLayerRect(RenderLayer*, const LayoutRect&);
229 typedef HashMap<RenderLayer*, LayoutRect> OverlapMap;
230 void addToOverlapMap(OverlapMap&, RenderLayer*, LayoutRect& layerBounds, bool& boundsComputed);
231 void addToOverlapMapRecursive(OverlapMap&, RenderLayer*);
232 static bool overlapsCompositedLayers(OverlapMap&, const LayoutRect& layerBounds);
234 void updateCompositingLayersTimerFired(Timer<RenderLayerCompositor>*);
236 // Returns true if any layer's compositing changed
237 void computeCompositingRequirements(RenderLayer*, OverlapMap*, struct CompositingState&, bool& layersChanged);
239 // Recurses down the tree, parenting descendant compositing layers and collecting an array of child layers for the current compositing layer.
240 void rebuildCompositingLayerTree(RenderLayer* layer, const struct CompositingState&, Vector<GraphicsLayer*>& childGraphicsLayersOfEnclosingLayer);
242 // Recurses down the tree, updating layer geometry only.
243 void updateLayerTreeGeometry(RenderLayer*);
245 // Hook compositing layers together
246 void setCompositingParent(RenderLayer* childLayer, RenderLayer* parentLayer);
247 void removeCompositedChildren(RenderLayer*);
249 bool layerHas3DContent(const RenderLayer*) const;
250 bool hasNonIdentity3DTransform(RenderObject*) const;
252 void ensureRootLayer();
253 void destroyRootLayer();
255 void attachRootLayer(RootLayerAttachment);
256 void detachRootLayer();
258 void rootLayerAttachmentChanged();
260 void updateOverflowControlsLayers();
262 void notifyIFramesOfCompositingChange();
264 bool isFlushingLayers() const { return m_flushingLayers; }
266 // Whether a running transition or animation enforces the need for a compositing layer.
267 bool requiresCompositingForAnimation(RenderObject*) const;
268 bool requiresCompositingForTransform(RenderObject*) const;
269 bool requiresCompositingForVideo(RenderObject*) const;
270 bool requiresCompositingForCanvas(RenderObject*) const;
271 bool requiresCompositingForPlugin(RenderObject*) const;
272 bool requiresCompositingForFrame(RenderObject*) const;
273 bool requiresCompositingWhenDescendantsAreCompositing(RenderObject*) const;
274 bool requiresCompositingForFullScreen(RenderObject*) const;
275 bool requiresCompositingForScrollableFrame() const;
277 bool requiresScrollLayer(RootLayerAttachment) const;
278 bool requiresHorizontalScrollbarLayer() const;
279 bool requiresVerticalScrollbarLayer() const;
280 bool requiresScrollCornerLayer() const;
281 #if PLATFORM(CHROMIUM) && ENABLE(RUBBER_BANDING)
282 bool requiresOverhangAreasLayer() const;
286 RenderView* m_renderView;
287 OwnPtr<GraphicsLayer> m_rootContentLayer;
288 Timer<RenderLayerCompositor> m_updateCompositingLayersTimer;
290 bool m_hasAcceleratedCompositing;
291 ChromeClient::CompositingTriggerFlags m_compositingTriggers;
293 bool m_showDebugBorders;
294 bool m_showRepaintCounter;
295 bool m_compositingConsultsOverlap;
297 // When true, we have to wait until layout has happened before we can decide whether to enter compositing mode,
298 // because only then do we know the final size of plugins and iframes.
299 // FIXME: once set, this is never cleared.
300 mutable bool m_compositingDependsOnGeometry;
303 bool m_compositingLayersNeedRebuild;
304 bool m_flushingLayers;
305 bool m_forceCompositingMode;
307 RootLayerAttachment m_rootLayerAttachment;
309 // Enclosing clipping layer for iframe content
310 OwnPtr<GraphicsLayer> m_clipLayer;
311 OwnPtr<GraphicsLayer> m_scrollLayer;
313 // Enclosing layer for overflow controls and the clipping layer
314 OwnPtr<GraphicsLayer> m_overflowControlsHostLayer;
316 // Layers for overflow controls
317 OwnPtr<GraphicsLayer> m_layerForHorizontalScrollbar;
318 OwnPtr<GraphicsLayer> m_layerForVerticalScrollbar;
319 OwnPtr<GraphicsLayer> m_layerForScrollCorner;
320 #if PLATFORM(CHROMIUM) && ENABLE(RUBBER_BANDING)
321 OwnPtr<GraphicsLayer> m_layerForOverhangAreas;
323 #if PROFILE_LAYER_REBUILD
324 int m_rootLayerUpdateCount;
329 } // namespace WebCore
331 #endif // RenderLayerCompositor_h