OSDN Git Service

Merge WebKit at r71558: Initial merge by git.
[android-x86/external-webkit.git] / 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 "RenderLayer.h"
30 #include "RenderLayerBacking.h"
31
32 namespace WebCore {
33
34 #define PROFILE_LAYER_REBUILD 0
35
36 class GraphicsLayer;
37 class RenderEmbeddedObject;
38 class RenderIFrame;
39 #if ENABLE(VIDEO)
40 class RenderVideo;
41 #endif
42
43 enum CompositingUpdateType {
44     CompositingUpdateAfterLayoutOrStyleChange,
45     CompositingUpdateOnPaitingOrHitTest,
46     CompositingUpdateOnScroll
47 };
48
49 // RenderLayerCompositor manages the hierarchy of
50 // composited RenderLayers. It determines which RenderLayers
51 // become compositing, and creates and maintains a hierarchy of
52 // GraphicsLayers based on the RenderLayer painting order.
53 // 
54 // There is one RenderLayerCompositor per RenderView.
55
56 class RenderLayerCompositor {
57 public:
58     RenderLayerCompositor(RenderView*);
59     ~RenderLayerCompositor();
60     
61     // Return true if this RenderView is in "compositing mode" (i.e. has one or more
62     // composited RenderLayers)
63     bool inCompositingMode() const { return m_compositing; }
64     // This will make a compositing layer at the root automatically, and hook up to
65     // the native view/window system.
66     void enableCompositingMode(bool enable = true);
67     
68     // Returns true if the accelerated compositing is enabled
69     bool hasAcceleratedCompositing() const { return m_hasAcceleratedCompositing; }
70     
71     bool showDebugBorders() const { return m_showDebugBorders; }
72     bool showRepaintCounter() const { return m_showRepaintCounter; }
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     void scheduleSync();
88     
89     // Rebuild the tree of compositing layers
90     void updateCompositingLayers(CompositingUpdateType = CompositingUpdateAfterLayoutOrStyleChange, RenderLayer* updateRoot = 0);
91     // This is only used when state changes and we do not exepect a style update or layout to happen soon (e.g. when
92     // we discover that an iframe is overlapped during painting).
93     void scheduleCompositingLayerUpdate();
94     bool compositingLayerUpdatePending() const;
95     
96     // Update the compositing state of the given layer. Returns true if that state changed.
97     enum CompositingChangeRepaint { CompositingChangeRepaintNow, CompositingChangeWillRepaintLater };
98     bool updateLayerCompositingState(RenderLayer*, CompositingChangeRepaint = CompositingChangeRepaintNow);
99
100     // Update the geometry for compositing children of compositingAncestor.
101     void updateCompositingDescendantGeometry(RenderLayer* compositingAncestor, RenderLayer* layer, RenderLayerBacking::UpdateDepth);
102     
103     // Whether layer's backing needs a graphics layer to do clipping by an ancestor (non-stacking-context parent with overflow).
104     bool clippedByAncestor(RenderLayer*) const;
105     // Whether layer's backing needs a graphics layer to clip z-order children of the given layer.
106     bool clipsCompositingDescendants(const RenderLayer*) const;
107
108     // Whether the given layer needs an extra 'contents' layer.
109     bool needsContentsCompositingLayer(const RenderLayer*) const;
110     // Return the bounding box required for compositing layer and its childern, relative to ancestorLayer.
111     // If layerBoundingBox is not 0, on return it contains the bounding box of this layer only.
112     IntRect calculateCompositedBounds(const RenderLayer* layer, const RenderLayer* ancestorLayer);
113
114     // Repaint the appropriate layers when the given RenderLayer starts or stops being composited.
115     void repaintOnCompositingChange(RenderLayer*);
116     
117     // Notify us that a layer has been added or removed
118     void layerWasAdded(RenderLayer* parent, RenderLayer* child);
119     void layerWillBeRemoved(RenderLayer* parent, RenderLayer* child);
120
121     // Get the nearest ancestor layer that has overflow or clip, but is not a stacking context
122     RenderLayer* enclosingNonStackingClippingLayer(const RenderLayer* layer) const;
123
124     // Repaint parts of all composited layers that intersect the given absolute rectangle.
125     void repaintCompositedLayersAbsoluteRect(const IntRect&);
126
127     RenderLayer* rootRenderLayer() const;
128     GraphicsLayer* rootPlatformLayer() const;
129
130     enum RootLayerAttachment {
131         RootLayerUnattached,
132         RootLayerAttachedViaChromeClient,
133         RootLayerAttachedViaEnclosingIframe
134     };
135
136     RootLayerAttachment rootLayerAttachment() const { return m_rootLayerAttachment; }
137     void updateRootLayerAttachment();
138     void updateRootLayerPosition();
139     
140     void didMoveOnscreen();
141     void willMoveOffscreen();
142     
143     void didStartAcceleratedAnimation();
144     
145 #if ENABLE(VIDEO)
146     // Use by RenderVideo to ask if it should try to use accelerated compositing.
147     bool canAccelerateVideoRendering(RenderVideo*) const;
148 #endif
149
150     // Walk the tree looking for layers with 3d transforms. Useful in case you need
151     // to know if there is non-affine content, e.g. for drawing into an image.
152     bool has3DContent() const;
153     
154     // Most platforms connect compositing layer trees between iframes and their parent document.
155     // Some (currently just Mac) allow iframes to do their own compositing.
156     static bool allowsIndependentlyCompositedIFrames(const FrameView*);
157     bool shouldPropagateCompositingToEnclosingIFrame() const;
158
159     // FIXME: This should be a RenderIFrame*
160     HTMLFrameOwnerElement* enclosingIFrameElement() const;
161
162     static RenderLayerCompositor* iframeContentsCompositor(RenderIFrame*);
163     // Return true if the layers changed.
164     static bool parentIFrameContentLayers(RenderIFrame*);
165
166     // Update the geometry of the layers used for clipping and scrolling in frames.
167     void frameViewDidChangeSize(const IntPoint& contentsOffset = IntPoint());
168     void frameViewDidScroll(const IntPoint& = IntPoint());
169
170     String layerTreeAsText();
171
172 private:
173     // Whether the given RL needs a compositing layer.
174     bool needsToBeComposited(const RenderLayer*) const;
175     // Whether the layer has an intrinsic need for compositing layer.
176     bool requiresCompositingLayer(const RenderLayer*) const;
177     // Whether the layer could ever be composited.
178     bool canBeComposited(const RenderLayer*) const;
179
180     // Make or destroy the backing for this layer; returns true if backing changed.
181     bool updateBacking(RenderLayer*, CompositingChangeRepaint shouldRepaint);
182
183     // Repaint the given rect (which is layer's coords), and regions of child layers that intersect that rect.
184     void recursiveRepaintLayerRect(RenderLayer* layer, const IntRect& rect);
185
186     typedef HashMap<RenderLayer*, IntRect> OverlapMap;
187     static void addToOverlapMap(OverlapMap&, RenderLayer*, IntRect& layerBounds, bool& boundsComputed);
188     static bool overlapsCompositedLayers(OverlapMap&, const IntRect& layerBounds);
189
190     void updateCompositingLayersTimerFired(Timer<RenderLayerCompositor>*);
191
192     // Returns true if any layer's compositing changed
193     void computeCompositingRequirements(RenderLayer*, OverlapMap*, struct CompositingState&, bool& layersChanged);
194     
195     // Recurses down the tree, parenting descendant compositing layers and collecting an array of child layers for the current compositing layer.
196     void rebuildCompositingLayerTree(RenderLayer* layer, const struct CompositingState&, Vector<GraphicsLayer*>& childGraphicsLayersOfEnclosingLayer);
197
198     // Recurses down the tree, updating layer geometry only.
199     void updateLayerTreeGeometry(RenderLayer*);
200     
201     // Hook compositing layers together
202     void setCompositingParent(RenderLayer* childLayer, RenderLayer* parentLayer);
203     void removeCompositedChildren(RenderLayer*);
204
205     bool layerHas3DContent(const RenderLayer*) const;
206
207     void ensureRootPlatformLayer();
208     void destroyRootPlatformLayer();
209
210     void attachRootPlatformLayer(RootLayerAttachment);
211     void detachRootPlatformLayer();
212     
213     void rootLayerAttachmentChanged();
214     
215     void scheduleNeedsStyleRecalc(Element*);
216     void notifyIFramesOfCompositingChange();
217
218     // Whether a running transition or animation enforces the need for a compositing layer.
219     bool requiresCompositingForAnimation(RenderObject*) const;
220     bool requiresCompositingForTransform(RenderObject*) const;
221     bool requiresCompositingForVideo(RenderObject*) const;
222     bool requiresCompositingForCanvas(RenderObject*) const;
223     bool requiresCompositingForPlugin(RenderObject*) const;
224     bool requiresCompositingForIFrame(RenderObject*) const;
225     bool requiresCompositingWhenDescendantsAreCompositing(RenderObject*) const;
226
227 #if PLATFORM(ANDROID)
228     // Whether we are on a mobile site
229     bool requiresCompositingForMobileSites(const RenderLayer* layer) const;
230 #endif
231
232     bool requiresScrollLayer(RootLayerAttachment) const;
233     
234 private:
235     RenderView* m_renderView;
236     OwnPtr<GraphicsLayer> m_rootPlatformLayer;
237     Timer<RenderLayerCompositor> m_updateCompositingLayersTimer;
238
239     bool m_hasAcceleratedCompositing;
240     bool m_showDebugBorders;
241     bool m_showRepaintCounter;
242     bool m_compositingConsultsOverlap;
243
244     // When true, we have to wait until layout has happened before we can decide whether to enter compositing mode,
245     // because only then do we know the final size of plugins and iframes.
246     // FIXME: once set, this is never cleared.
247     mutable bool m_compositingDependsOnGeometry;
248
249     bool m_compositing;
250     bool m_compositingLayersNeedRebuild;
251
252     RootLayerAttachment m_rootLayerAttachment;
253
254     // Enclosing clipping layer for iframe content
255     OwnPtr<GraphicsLayer> m_clipLayer;
256     OwnPtr<GraphicsLayer> m_scrollLayer;
257     
258 #if PROFILE_LAYER_REBUILD
259     int m_rootLayerUpdateCount;
260 #endif
261 };
262
263
264 } // namespace WebCore
265
266 #endif // RenderLayerCompositor_h