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"
36 class RenderEmbeddedObject;
38 class ScrollingCoordinator;
43 enum CompositingUpdateType {
44 CompositingUpdateAfterStyleChange,
45 CompositingUpdateAfterLayout,
46 CompositingUpdateOnHitTest,
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 bool inForcedCompositingMode() const { return m_forceCompositingMode; }
71 // Returns true if the accelerated compositing is enabled
72 bool hasAcceleratedCompositing() const { return m_hasAcceleratedCompositing; }
74 bool canRender3DTransforms() const;
76 // Copy the accelerated compositing related flags from Settings
77 void cacheAcceleratedCompositingFlags();
79 // Called when the layer hierarchy needs to be updated (compositing layers have been
80 // created, destroyed or re-parented).
81 void setCompositingLayersNeedRebuild(bool needRebuild = true);
82 bool compositingLayersNeedRebuild() const { return m_compositingLayersNeedRebuild; }
84 // Controls whether or not to consult geometry when deciding which layers need
85 // to be composited. Defaults to true.
86 void setCompositingConsultsOverlap(bool b) { m_compositingConsultsOverlap = b; }
87 bool compositingConsultsOverlap() const { return m_compositingConsultsOverlap; }
89 // GraphicsLayers buffer state, which gets pushed to the underlying platform layers
91 void scheduleLayerFlush();
92 void flushPendingLayerChanges(bool isFlushRoot = true);
94 // flushPendingLayerChanges() flushes the entire GraphicsLayer tree, which can cross frame boundaries.
95 // This call returns the rootmost compositor that is being flushed (including self).
96 RenderLayerCompositor* enclosingCompositorFlushingLayers() const;
98 // Called when the GraphicsLayer for the given RenderLayer has flushed changes inside of flushPendingLayerChanges().
99 void didFlushChangesForLayer(RenderLayer*);
101 // Rebuild the tree of compositing layers
102 void updateCompositingLayers(CompositingUpdateType, RenderLayer* updateRoot = 0);
103 // This is only used when state changes and we do not exepect a style update or layout to happen soon (e.g. when
104 // we discover that an iframe is overlapped during painting).
105 void scheduleCompositingLayerUpdate();
107 // Update the compositing state of the given layer. Returns true if that state changed.
108 enum CompositingChangeRepaint { CompositingChangeRepaintNow, CompositingChangeWillRepaintLater };
109 bool updateLayerCompositingState(RenderLayer*, CompositingChangeRepaint = CompositingChangeRepaintNow);
111 // Update the geometry for compositing children of compositingAncestor.
112 void updateCompositingDescendantGeometry(RenderLayer* compositingAncestor, RenderLayer* layer, RenderLayerBacking::UpdateDepth);
114 // Whether layer's backing needs a graphics layer to do clipping by an ancestor (non-stacking-context parent with overflow).
115 bool clippedByAncestor(RenderLayer*) const;
116 // Whether layer's backing needs a graphics layer to clip z-order children of the given layer.
117 bool clipsCompositingDescendants(const RenderLayer*) const;
119 // Whether the layer is fixed positioned to the view by an ancestor layer.
120 bool fixedPositionedByAncestor(const RenderLayer*) const;
122 // Whether the given layer needs an extra 'contents' layer.
123 bool needsContentsCompositingLayer(const RenderLayer*) const;
124 // Return the bounding box required for compositing layer and its childern, relative to ancestorLayer.
125 // If layerBoundingBox is not 0, on return it contains the bounding box of this layer only.
126 IntRect calculateCompositedBounds(const RenderLayer*, const RenderLayer* ancestorLayer) const;
128 // Repaint the appropriate layers when the given RenderLayer starts or stops being composited.
129 void repaintOnCompositingChange(RenderLayer*);
131 void repaintInCompositedAncestor(RenderLayer*, const LayoutRect&);
133 // Notify us that a layer has been added or removed
134 void layerWasAdded(RenderLayer* parent, RenderLayer* child);
135 void layerWillBeRemoved(RenderLayer* parent, RenderLayer* child);
137 // Get the nearest ancestor layer that has overflow or clip, but is not a stacking context
138 RenderLayer* enclosingNonStackingClippingLayer(const RenderLayer* layer) const;
140 // Repaint parts of all composited layers that intersect the given absolute rectangle.
141 void repaintCompositedLayersAbsoluteRect(const IntRect&);
143 // Returns true if the given layer needs it own backing store.
144 bool requiresOwnBackingStore(const RenderLayer*, const RenderLayer* compositingAncestorLayer) const;
146 RenderLayer* rootRenderLayer() const;
147 GraphicsLayer* rootGraphicsLayer() const;
148 GraphicsLayer* scrollLayer() const;
150 enum RootLayerAttachment {
152 RootLayerAttachedViaChromeClient,
153 RootLayerAttachedViaEnclosingFrame
156 RootLayerAttachment rootLayerAttachment() const { return m_rootLayerAttachment; }
157 void updateRootLayerAttachment();
158 void updateRootLayerPosition();
160 void didMoveOnscreen();
161 void willMoveOffscreen();
163 void clearBackingForAllLayers();
165 void layerBecameComposited(const RenderLayer*) { ++m_compositedLayerCount; }
166 void layerBecameNonComposited(const RenderLayer*)
168 ASSERT(m_compositedLayerCount > 0);
169 --m_compositedLayerCount;
173 // Use by RenderVideo to ask if it should try to use accelerated compositing.
174 bool canAccelerateVideoRendering(RenderVideo*) const;
177 // Walk the tree looking for layers with 3d transforms. Useful in case you need
178 // to know if there is non-affine content, e.g. for drawing into an image.
179 bool has3DContent() const;
181 // Most platforms connect compositing layer trees between iframes and their parent document.
182 // Some (currently just Mac) allow iframes to do their own compositing.
183 static bool allowsIndependentlyCompositedFrames(const FrameView*);
184 bool shouldPropagateCompositingToEnclosingFrame() const;
186 static RenderLayerCompositor* frameContentsCompositor(RenderPart*);
187 // Return true if the layers changed.
188 static bool parentFrameContentLayers(RenderPart*);
190 // Update the geometry of the layers used for clipping and scrolling in frames.
191 void frameViewDidChangeLocation(const IntPoint& contentsOffset);
192 void frameViewDidChangeSize();
193 void frameViewDidScroll();
195 String layerTreeAsText(bool showDebugInfo = false);
197 // These are named to avoid conflicts with the functions in GraphicsLayerClient
198 // These return the actual internal variables.
199 bool compositorShowDebugBorders() const { return m_showDebugBorders; }
200 bool compositorShowRepaintCounter() const { return m_showRepaintCounter; }
202 virtual float deviceScaleFactor() const;
203 virtual float pageScaleFactor() const;
204 virtual void didCommitChangesForLayer(const GraphicsLayer*) const;
206 bool keepLayersPixelAligned() const;
207 bool acceleratedDrawingEnabled() const { return m_acceleratedDrawingEnabled; }
209 void deviceOrPageScaleFactorChanged();
211 GraphicsLayer* layerForHorizontalScrollbar() const { return m_layerForHorizontalScrollbar.get(); }
212 GraphicsLayer* layerForVerticalScrollbar() const { return m_layerForVerticalScrollbar.get(); }
213 GraphicsLayer* layerForScrollCorner() const { return m_layerForScrollCorner.get(); }
214 #if ENABLE(RUBBER_BANDING)
215 GraphicsLayer* layerForOverhangAreas() const { return m_layerForOverhangAreas.get(); }
218 void documentBackgroundColorDidChange();
220 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
221 void platformLayerChanged(RenderLayer*, PlatformLayer* oldLayer, PlatformLayer* newLayer);
222 void scrollingLayerAddedOrUpdated(RenderLayer*, GraphicsLayer* scrollingLayer, GraphicsLayer* contentsLayer, const IntSize& scrollSize);
223 void scrollingLayerRemoved(RenderLayer*, GraphicsLayer* scrollingLayer, GraphicsLayer* contentsLayer);
224 void registerAllScrollingLayers();
225 void unregisterAllScrollingLayers();
231 // GraphicsLayerClient Implementation
232 virtual void notifyAnimationStarted(const GraphicsLayer*, double) { }
233 virtual void notifySyncRequired(const GraphicsLayer*) { scheduleLayerFlush(); }
234 virtual void paintContents(const GraphicsLayer*, GraphicsContext&, GraphicsLayerPaintingPhase, const IntRect&);
236 virtual bool showDebugBorders(const GraphicsLayer*) const;
237 virtual bool showRepaintCounter(const GraphicsLayer*) const;
239 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
240 virtual void platformLayerChanged(GraphicsLayer*, PlatformLayer* /* oldPlatformLayer */, PlatformLayer* /* newPlatformLayer */) { }
243 // Whether the given RL needs a compositing layer.
244 bool needsToBeComposited(const RenderLayer*) const;
245 // Whether the layer has an intrinsic need for compositing layer.
246 bool requiresCompositingLayer(const RenderLayer*) const;
247 // Whether the layer could ever be composited.
248 bool canBeComposited(const RenderLayer*) const;
250 // Make or destroy the backing for this layer; returns true if backing changed.
251 bool updateBacking(RenderLayer*, CompositingChangeRepaint shouldRepaint);
253 void clearBackingForLayerIncludingDescendants(RenderLayer*);
255 // Repaint the given rect (which is layer's coords), and regions of child layers that intersect that rect.
256 void recursiveRepaintLayerRect(RenderLayer*, const IntRect&);
258 void addToOverlapMap(OverlapMap&, RenderLayer*, IntRect& layerBounds, bool& boundsComputed);
259 void addToOverlapMapRecursive(OverlapMap&, RenderLayer*, RenderLayer* ancestorLayer = 0);
261 void updateCompositingLayersTimerFired(Timer<RenderLayerCompositor>*);
263 // Returns true if any layer's compositing changed
264 void computeCompositingRequirements(RenderLayer* ancestorLayer, RenderLayer*, OverlapMap*, struct CompositingState&, bool& layersChanged, bool& descendantHas3DTransform);
266 // Recurses down the tree, parenting descendant compositing layers and collecting an array of child layers for the current compositing layer.
267 void rebuildCompositingLayerTree(RenderLayer*, Vector<GraphicsLayer*>& childGraphicsLayersOfEnclosingLayer, int depth);
269 // Recurses down the tree, updating layer geometry only.
270 void updateLayerTreeGeometry(RenderLayer*, int depth);
272 // Hook compositing layers together
273 void setCompositingParent(RenderLayer* childLayer, RenderLayer* parentLayer);
274 void removeCompositedChildren(RenderLayer*);
276 bool layerHas3DContent(const RenderLayer*) const;
277 bool isRunningAcceleratedTransformAnimation(RenderObject*) const;
279 bool hasAnyAdditionalCompositedLayers(const RenderLayer* rootLayer) const;
281 void ensureRootLayer();
282 void destroyRootLayer();
284 void attachRootLayer(RootLayerAttachment);
285 void detachRootLayer();
287 void rootLayerAttachmentChanged();
289 void updateOverflowControlsLayers();
291 void notifyIFramesOfCompositingChange();
293 bool isFlushingLayers() const { return m_flushingLayers; }
295 ScrollingCoordinator* scrollingCoordinator() const;
297 // Whether a running transition or animation enforces the need for a compositing layer.
298 bool requiresCompositingForAnimation(RenderObject*) const;
299 bool requiresCompositingForTransform(RenderObject*) const;
300 bool requiresCompositingForVideo(RenderObject*) const;
301 bool requiresCompositingForCanvas(RenderObject*) const;
302 bool requiresCompositingForPlugin(RenderObject*) const;
303 bool requiresCompositingForFrame(RenderObject*) const;
304 bool requiresCompositingForFilters(RenderObject*) const;
305 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
306 bool requiresCompositingForScrolling(RenderObject* renderer) const;
308 bool requiresCompositingForScrollableFrame() const;
309 bool requiresCompositingForPosition(RenderObject*, const RenderLayer*) const;
310 bool requiresCompositingForIndirectReason(RenderObject*, bool hasCompositedDescendants, bool has3DTransformedDescendants, RenderLayer::IndirectCompositingReason&) const;
312 bool requiresScrollLayer(RootLayerAttachment) const;
313 bool requiresHorizontalScrollbarLayer() const;
314 bool requiresVerticalScrollbarLayer() const;
315 bool requiresScrollCornerLayer() const;
316 #if ENABLE(RUBBER_BANDING)
317 bool requiresOverhangAreasLayer() const;
318 bool requiresContentShadowLayer() const;
321 #if ENABLE(THREADED_SCROLLING)
322 ScrollingCoordinator* scrollingCoordinator() const;
325 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
326 ChromeClient* chromeClient() const;
330 const char* reasonForCompositing(const RenderLayer*);
331 void logLayerInfo(const RenderLayer*, int depth);
335 RenderView* m_renderView;
336 OwnPtr<GraphicsLayer> m_rootContentLayer;
337 Timer<RenderLayerCompositor> m_updateCompositingLayersTimer;
339 bool m_hasAcceleratedCompositing;
340 ChromeClient::CompositingTriggerFlags m_compositingTriggers;
342 int m_compositedLayerCount;
343 bool m_showDebugBorders;
344 bool m_showRepaintCounter;
345 bool m_acceleratedDrawingEnabled;
346 bool m_compositingConsultsOverlap;
348 // When true, we have to wait until layout has happened before we can decide whether to enter compositing mode,
349 // because only then do we know the final size of plugins and iframes.
350 mutable bool m_reevaluateCompositingAfterLayout;
353 bool m_compositingLayersNeedRebuild;
354 bool m_flushingLayers;
355 bool m_forceCompositingMode;
357 RootLayerAttachment m_rootLayerAttachment;
359 // Enclosing clipping layer for iframe content
360 OwnPtr<GraphicsLayer> m_clipLayer;
361 OwnPtr<GraphicsLayer> m_scrollLayer;
363 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
364 HashSet<RenderLayer*> m_scrollingLayers;
367 // Enclosing layer for overflow controls and the clipping layer
368 OwnPtr<GraphicsLayer> m_overflowControlsHostLayer;
370 // Layers for overflow controls
371 OwnPtr<GraphicsLayer> m_layerForHorizontalScrollbar;
372 OwnPtr<GraphicsLayer> m_layerForVerticalScrollbar;
373 OwnPtr<GraphicsLayer> m_layerForScrollCorner;
374 #if ENABLE(RUBBER_BANDING)
375 OwnPtr<GraphicsLayer> m_layerForOverhangAreas;
376 OwnPtr<GraphicsLayer> m_contentShadowLayer;
380 int m_rootLayerUpdateCount;
381 int m_obligateCompositedLayerCount; // count of layer that have to be composited.
382 int m_secondaryCompositedLayerCount; // count of layers that have to be composited because of stacking or overlap.
383 double m_obligatoryBackingStoreBytes;
384 double m_secondaryBackingStoreBytes;
389 } // namespace WebCore
391 #endif // RenderLayerCompositor_h