initial import
[vuplus_webkit] / Source / WebCore / rendering / RenderLayerCompositor.h
1 /*
2  * Copyright (C) 2009 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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. 
24  */
25
26 #ifndef RenderLayerCompositor_h
27 #define RenderLayerCompositor_h
28
29 #include "ChromeClient.h"
30 #include "RenderLayer.h"
31 #include "RenderLayerBacking.h"
32
33 namespace WebCore {
34
35 #define PROFILE_LAYER_REBUILD 0
36
37 class GraphicsLayer;
38 class RenderEmbeddedObject;
39 class RenderPart;
40 #if ENABLE(VIDEO)
41 class RenderVideo;
42 #endif
43
44 enum CompositingUpdateType {
45     CompositingUpdateAfterLayoutOrStyleChange,
46     CompositingUpdateOnPaitingOrHitTest,
47     CompositingUpdateOnScroll
48 };
49
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.
54 // 
55 // There is one RenderLayerCompositor per RenderView.
56
57 class RenderLayerCompositor : public GraphicsLayerClient {
58 public:
59     RenderLayerCompositor(RenderView*);
60     ~RenderLayerCompositor();
61
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);
68     
69     // Returns true if the accelerated compositing is enabled
70     bool hasAcceleratedCompositing() const { return m_hasAcceleratedCompositing; }
71
72     bool canRender3DTransforms() const;
73
74     // Copy the accelerated compositing related flags from Settings
75     void cacheAcceleratedCompositingFlags();
76
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; }
81
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; }
86     
87     // GraphicsLayers buffer state, which gets pushed to the underlying platform layers
88     // at specific times.
89     void scheduleLayerFlush();
90     void flushPendingLayerChanges(bool isFlushRoot = true);
91     
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;
95
96     // Called when the GraphicsLayer for the given RenderLayer has flushed changes inside of flushPendingLayerChanges().
97     void didFlushChangesForLayer(RenderLayer*);
98     
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();
104     
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);
108
109     // Update the geometry for compositing children of compositingAncestor.
110     void updateCompositingDescendantGeometry(RenderLayer* compositingAncestor, RenderLayer* layer, RenderLayerBacking::UpdateDepth);
111     
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;
116
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);
122
123     // Repaint the appropriate layers when the given RenderLayer starts or stops being composited.
124     void repaintOnCompositingChange(RenderLayer*);
125     
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);
129
130     // Get the nearest ancestor layer that has overflow or clip, but is not a stacking context
131     RenderLayer* enclosingNonStackingClippingLayer(const RenderLayer* layer) const;
132
133     // Repaint parts of all composited layers that intersect the given absolute rectangle.
134     void repaintCompositedLayersAbsoluteRect(const LayoutRect&);
135
136     RenderLayer* rootRenderLayer() const;
137     GraphicsLayer* rootGraphicsLayer() const;
138
139     enum RootLayerAttachment {
140         RootLayerUnattached,
141         RootLayerAttachedViaChromeClient,
142         RootLayerAttachedViaEnclosingFrame
143     };
144
145     RootLayerAttachment rootLayerAttachment() const { return m_rootLayerAttachment; }
146     void updateRootLayerAttachment();
147     void updateRootLayerPosition();
148     
149     void didMoveOnscreen();
150     void willMoveOffscreen();
151
152     void clearBackingForAllLayers();
153     
154     void didStartAcceleratedAnimation(CSSPropertyID);
155     
156 #if ENABLE(VIDEO)
157     // Use by RenderVideo to ask if it should try to use accelerated compositing.
158     bool canAccelerateVideoRendering(RenderVideo*) const;
159 #endif
160
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;
164     
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;
169
170     HTMLFrameOwnerElement* enclosingFrameElement() const;
171
172     static RenderLayerCompositor* frameContentsCompositor(RenderPart*);
173     // Return true if the layers changed.
174     static bool parentFrameContentLayers(RenderPart*);
175
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());
180
181     String layerTreeAsText(bool showDebugInfo = false);
182
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; }
187
188     virtual float deviceScaleFactor() const;
189     virtual float pageScaleFactor() const;
190     virtual void didCommitChangesForLayer(const GraphicsLayer*) const;
191     
192     bool keepLayersPixelAligned() const;
193
194     void deviceOrPageScaleFactorChanged();
195
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(); }
201 #endif
202
203 private:
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&);
208
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; }
213     
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;
220
221     // Make or destroy the backing for this layer; returns true if backing changed.
222     bool updateBacking(RenderLayer*, CompositingChangeRepaint shouldRepaint);
223
224     void clearBackingForLayerIncludingDescendants(RenderLayer*);
225
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&);
228
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);
233
234     void updateCompositingLayersTimerFired(Timer<RenderLayerCompositor>*);
235
236     // Returns true if any layer's compositing changed
237     void computeCompositingRequirements(RenderLayer*, OverlapMap*, struct CompositingState&, bool& layersChanged);
238     
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);
241
242     // Recurses down the tree, updating layer geometry only.
243     void updateLayerTreeGeometry(RenderLayer*);
244     
245     // Hook compositing layers together
246     void setCompositingParent(RenderLayer* childLayer, RenderLayer* parentLayer);
247     void removeCompositedChildren(RenderLayer*);
248
249     bool layerHas3DContent(const RenderLayer*) const;
250     bool hasNonIdentity3DTransform(RenderObject*) const;
251
252     void ensureRootLayer();
253     void destroyRootLayer();
254
255     void attachRootLayer(RootLayerAttachment);
256     void detachRootLayer();
257     
258     void rootLayerAttachmentChanged();
259
260     void updateOverflowControlsLayers();
261
262     void notifyIFramesOfCompositingChange();
263
264     bool isFlushingLayers() const { return m_flushingLayers; }
265
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;
276
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;
283 #endif
284
285 private:
286     RenderView* m_renderView;
287     OwnPtr<GraphicsLayer> m_rootContentLayer;
288     Timer<RenderLayerCompositor> m_updateCompositingLayersTimer;
289
290     bool m_hasAcceleratedCompositing;
291     ChromeClient::CompositingTriggerFlags m_compositingTriggers;
292
293     bool m_showDebugBorders;
294     bool m_showRepaintCounter;
295     bool m_compositingConsultsOverlap;
296
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;
301
302     bool m_compositing;
303     bool m_compositingLayersNeedRebuild;
304     bool m_flushingLayers;
305     bool m_forceCompositingMode;
306
307     RootLayerAttachment m_rootLayerAttachment;
308
309     // Enclosing clipping layer for iframe content
310     OwnPtr<GraphicsLayer> m_clipLayer;
311     OwnPtr<GraphicsLayer> m_scrollLayer;
312
313     // Enclosing layer for overflow controls and the clipping layer
314     OwnPtr<GraphicsLayer> m_overflowControlsHostLayer;
315
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;
322 #endif
323 #if PROFILE_LAYER_REBUILD
324     int m_rootLayerUpdateCount;
325 #endif
326 };
327
328
329 } // namespace WebCore
330
331 #endif // RenderLayerCompositor_h