Fix the issue that Web Audio test case fails on PR3.
[framework/web/webkit-efl.git] / 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 class GraphicsLayer;
36 class RenderEmbeddedObject;
37 class RenderPart;
38 class ScrollingCoordinator;
39 #if ENABLE(VIDEO)
40 class RenderVideo;
41 #endif
42
43 enum CompositingUpdateType {
44     CompositingUpdateAfterStyleChange,
45     CompositingUpdateAfterLayout,
46     CompositingUpdateOnHitTest,
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     bool inForcedCompositingMode() const { return m_forceCompositingMode; }
70
71     // Returns true if the accelerated compositing is enabled
72     bool hasAcceleratedCompositing() const { return m_hasAcceleratedCompositing; }
73
74     bool canRender3DTransforms() const;
75
76     // Copy the accelerated compositing related flags from Settings
77     void cacheAcceleratedCompositingFlags();
78
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; }
83
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; }
88     
89     // GraphicsLayers buffer state, which gets pushed to the underlying platform layers
90     // at specific times.
91     void scheduleLayerFlush();
92     void flushPendingLayerChanges(bool isFlushRoot = true);
93     
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;
97
98     // Called when the GraphicsLayer for the given RenderLayer has flushed changes inside of flushPendingLayerChanges().
99     void didFlushChangesForLayer(RenderLayer*);
100     
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();
106     
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);
110
111     // Update the geometry for compositing children of compositingAncestor.
112     void updateCompositingDescendantGeometry(RenderLayer* compositingAncestor, RenderLayer* layer, RenderLayerBacking::UpdateDepth);
113     
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;
118
119     // Whether the layer is fixed positioned to the view by an ancestor layer.
120     bool fixedPositionedByAncestor(const RenderLayer*) const;
121
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;
127
128     // Repaint the appropriate layers when the given RenderLayer starts or stops being composited.
129     void repaintOnCompositingChange(RenderLayer*);
130     
131     void repaintInCompositedAncestor(RenderLayer*, const LayoutRect&);
132     
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);
136
137     // Get the nearest ancestor layer that has overflow or clip, but is not a stacking context
138     RenderLayer* enclosingNonStackingClippingLayer(const RenderLayer* layer) const;
139
140     // Repaint parts of all composited layers that intersect the given absolute rectangle.
141     void repaintCompositedLayersAbsoluteRect(const IntRect&);
142
143     // Returns true if the given layer needs it own backing store.
144     bool requiresOwnBackingStore(const RenderLayer*, const RenderLayer* compositingAncestorLayer) const;
145
146     RenderLayer* rootRenderLayer() const;
147     GraphicsLayer* rootGraphicsLayer() const;
148     GraphicsLayer* scrollLayer() const;
149
150     enum RootLayerAttachment {
151         RootLayerUnattached,
152         RootLayerAttachedViaChromeClient,
153         RootLayerAttachedViaEnclosingFrame
154     };
155
156     RootLayerAttachment rootLayerAttachment() const { return m_rootLayerAttachment; }
157     void updateRootLayerAttachment();
158     void updateRootLayerPosition();
159     
160     void didMoveOnscreen();
161     void willMoveOffscreen();
162
163     void clearBackingForAllLayers();
164     
165     void layerBecameComposited(const RenderLayer*) { ++m_compositedLayerCount; }
166     void layerBecameNonComposited(const RenderLayer*)
167     {
168         ASSERT(m_compositedLayerCount > 0);
169         --m_compositedLayerCount;
170     }
171     
172 #if ENABLE(VIDEO)
173     // Use by RenderVideo to ask if it should try to use accelerated compositing.
174     bool canAccelerateVideoRendering(RenderVideo*) const;
175 #endif
176
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;
180     
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;
185
186     static RenderLayerCompositor* frameContentsCompositor(RenderPart*);
187     // Return true if the layers changed.
188     static bool parentFrameContentLayers(RenderPart*);
189
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();
194
195     String layerTreeAsText(bool showDebugInfo = false);
196
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; }
201
202     virtual float deviceScaleFactor() const;
203     virtual float pageScaleFactor() const;
204     virtual void didCommitChangesForLayer(const GraphicsLayer*) const;
205     
206     bool keepLayersPixelAligned() const;
207     bool acceleratedDrawingEnabled() const { return m_acceleratedDrawingEnabled; }
208
209     void deviceOrPageScaleFactorChanged();
210
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(); }
216 #endif
217
218     void documentBackgroundColorDidChange();
219
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();
226 #endif
227
228 private:
229     class OverlapMap;
230
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&);
235
236     virtual bool showDebugBorders(const GraphicsLayer*) const;
237     virtual bool showRepaintCounter(const GraphicsLayer*) const;
238
239 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
240     virtual void platformLayerChanged(GraphicsLayer*, PlatformLayer* /* oldPlatformLayer */, PlatformLayer* /* newPlatformLayer */) { }
241 #endif
242     
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;
249
250     // Make or destroy the backing for this layer; returns true if backing changed.
251     bool updateBacking(RenderLayer*, CompositingChangeRepaint shouldRepaint);
252
253     void clearBackingForLayerIncludingDescendants(RenderLayer*);
254
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&);
257
258     void addToOverlapMap(OverlapMap&, RenderLayer*, IntRect& layerBounds, bool& boundsComputed);
259     void addToOverlapMapRecursive(OverlapMap&, RenderLayer*, RenderLayer* ancestorLayer = 0);
260
261     void updateCompositingLayersTimerFired(Timer<RenderLayerCompositor>*);
262
263     // Returns true if any layer's compositing changed
264     void computeCompositingRequirements(RenderLayer* ancestorLayer, RenderLayer*, OverlapMap*, struct CompositingState&, bool& layersChanged, bool& descendantHas3DTransform);
265     
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);
268
269     // Recurses down the tree, updating layer geometry only.
270     void updateLayerTreeGeometry(RenderLayer*, int depth);
271     
272     // Hook compositing layers together
273     void setCompositingParent(RenderLayer* childLayer, RenderLayer* parentLayer);
274     void removeCompositedChildren(RenderLayer*);
275
276     bool layerHas3DContent(const RenderLayer*) const;
277     bool isRunningAcceleratedTransformAnimation(RenderObject*) const;
278
279     bool hasAnyAdditionalCompositedLayers(const RenderLayer* rootLayer) const;
280
281     void ensureRootLayer();
282     void destroyRootLayer();
283
284     void attachRootLayer(RootLayerAttachment);
285     void detachRootLayer();
286     
287     void rootLayerAttachmentChanged();
288
289     void updateOverflowControlsLayers();
290
291     void notifyIFramesOfCompositingChange();
292
293     bool isFlushingLayers() const { return m_flushingLayers; }
294
295     ScrollingCoordinator* scrollingCoordinator() const;
296
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;
307 #endif
308     bool requiresCompositingForScrollableFrame() const;
309     bool requiresCompositingForPosition(RenderObject*, const RenderLayer*) const;
310     bool requiresCompositingForIndirectReason(RenderObject*, bool hasCompositedDescendants, bool has3DTransformedDescendants, RenderLayer::IndirectCompositingReason&) const;
311
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;
319 #endif
320
321 #if ENABLE(THREADED_SCROLLING)
322     ScrollingCoordinator* scrollingCoordinator() const;
323 #endif
324
325 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
326     ChromeClient* chromeClient() const;
327 #endif
328
329 #if !LOG_DISABLED
330     const char* reasonForCompositing(const RenderLayer*);
331     void logLayerInfo(const RenderLayer*, int depth);
332 #endif
333
334 private:
335     RenderView* m_renderView;
336     OwnPtr<GraphicsLayer> m_rootContentLayer;
337     Timer<RenderLayerCompositor> m_updateCompositingLayersTimer;
338
339     bool m_hasAcceleratedCompositing;
340     ChromeClient::CompositingTriggerFlags m_compositingTriggers;
341
342     int m_compositedLayerCount;
343     bool m_showDebugBorders;
344     bool m_showRepaintCounter;
345     bool m_acceleratedDrawingEnabled;
346     bool m_compositingConsultsOverlap;
347
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;
351
352     bool m_compositing;
353     bool m_compositingLayersNeedRebuild;
354     bool m_flushingLayers;
355     bool m_forceCompositingMode;
356
357     RootLayerAttachment m_rootLayerAttachment;
358
359     // Enclosing clipping layer for iframe content
360     OwnPtr<GraphicsLayer> m_clipLayer;
361     OwnPtr<GraphicsLayer> m_scrollLayer;
362
363 #if ENABLE(TIZEN_CSS_OVERFLOW_SCROLL_ACCELERATION)
364     HashSet<RenderLayer*> m_scrollingLayers;
365 #endif
366
367     // Enclosing layer for overflow controls and the clipping layer
368     OwnPtr<GraphicsLayer> m_overflowControlsHostLayer;
369
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;
377 #endif
378
379 #if !LOG_DISABLED
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;
385 #endif
386 };
387
388
389 } // namespace WebCore
390
391 #endif // RenderLayerCompositor_h